Merge release-4-6 into release-5-0
[alexxy/gromacs.git] / src / gromacs / options / options.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \file
36  * \brief
37  * Declares gmx::Options.
38  *
39  * Together with basicoptions.h, this header forms the part of the public
40  * API that most classes will use to provide options.
41  *
42  * \author Teemu Murtola <teemu.murtola@gmail.com>
43  * \inpublicapi
44  * \ingroup module_options
45  */
46 #ifndef GMX_OPTIONS_OPTIONS_H
47 #define GMX_OPTIONS_OPTIONS_H
48
49 #include <string>
50
51 #include "../utility/common.h"
52 #include "../utility/gmxassert.h"
53
54 #include "abstractoption.h"
55
56 namespace gmx
57 {
58
59 template <typename T> class ConstArrayRef;
60
61 class AbstractOption;
62 class OptionsAssigner;
63 class OptionsIterator;
64
65 /*! \brief
66  * Collection of options.
67  *
68  * This class provides a standard interface for implementing input options.
69  * Standard usage is to write a method that creates an Options that is owned by
70  * the object, populates it with supported options, and then returns it:
71  * \code
72    // <as class attributes>
73    using gmx::Options;
74    Options      options("common", "Common Options");
75    std::string  arg1;
76    int          arg2;
77
78    // <populating>
79    using gmx::StringOption;
80    using gmx::IntegerOption;
81    options.addOption(StringOption("arg1").store(&arg1));
82    options.addOption(IntegerOption("arg2").store(&arg2));
83    return &options;
84    \endcode
85  * The caller of that method can then use a parser implementation such as
86  * CommandLineParser to provide values for the options.
87  *
88  * Header basicoptions.h provides declarations of several standard
89  * option types for use with addOption().  Documentation of those classes
90  * also give more examples of how to define options.
91  *
92  * In order to keep the public interface of this class simple and to reduce
93  * build dependencies on objects that simply provide options, functionality
94  * to assign values to options is provided by a separate OptionsAssigner class.
95  * Similarly, functionality for looping over all options (e.g., for writing out
96  * help) is provided by OptionsIterator.
97  *
98  * \inpublicapi
99  * \ingroup module_options
100  */
101 class Options
102 {
103     public:
104         /*! \brief
105          * Initializes the name and title of an option collection.
106          *
107          * \param[in] name  Single-word name.
108          * \param[in] title Descriptive title.
109          *
110          * Copies the input strings.
111          */
112         Options(const char *name, const char *title);
113         ~Options();
114
115         //! Returns the short name of the option collection.
116         const std::string &name() const;
117         //! Returns the title of the option collection.
118         const std::string &title() const;
119         //! Returns the full description of the option collection.
120         const std::string &description() const;
121
122         /*! \brief
123          * Sets the full description of the option collection.
124          *
125          * \param[in] desc  String to set as the description.
126          *
127          * This overload is mainly useful if the description is very short.
128          * Currently this is mostly the case in unit testing.
129          */
130         void setDescription(const std::string &desc);
131         /*! \brief
132          * Sets the full description of the option collection from string array.
133          *
134          * \param[in] descArray  String array to set as the description.
135          *
136          * All strings in `descArray` are concatenated to form the description.
137          * Spaces are inserted between strings if they are missing.
138          *
139          * Example usage:
140          * \code
141            const char *const desc[] = {
142                "This is the description",
143                "for the options"
144            };
145
146            gmx::Options options(NULL, NULL);
147            options.setDescription(desc);
148            \endcode
149          *
150          * To use this overload, you must also include
151          * `gromacs/utility/arrayref.h`.
152          */
153         void setDescription(const ConstArrayRef<const char *> &descArray);
154
155         /*! \brief
156          * Adds an option collection as a subsection of this collection.
157          *
158          * \param[in] section Subsection to add.
159          *
160          * The name() field of \p section is used as the name of the
161          * subsection.  If an attempt is made to add two different subsections
162          * with the same name, this function asserts.
163          *
164          * For certain functionality to work properly, no options should
165          * be added to the subsection after it has been added to another
166          * collection.
167          *
168          * Only a pointer to the provided object is stored.  The caller is
169          * responsible that the object exists for the lifetime of the
170          * collection.
171          * It is not possible to add the same Options object as a subsection to
172          * several different Options.
173          * If an attempt is made, the function asserts.
174          */
175         void addSubSection(Options *section);
176         /*! \brief
177          * Adds a recognized option to the collection.
178          *
179          * \param[in] settings Option description.
180          * \returns   OptionInfo object for the created option (never NULL).
181          * \throws    APIError if invalid option settings are provided.
182          *
183          * This method provides the internal implementation, but in most cases
184          * the templated method is called from user code.
185          * See the templated method for more details.
186          */
187         OptionInfo *addOption(const AbstractOption &settings);
188         /*! \brief
189          * Adds a recognized option to the collection.
190          *
191          * \tparam    OptionType Type of the options description object.
192          * \param[in] settings   Option description.
193          * \returns   OptionInfo object for the created option (never NULL).
194          * \throws    APIError if invalid option settings are provided.
195          *
196          * The return value is a pointer for more convenient use in callers:
197          * often callers need to declare the variable that will hold the return
198          * value in wider scope than would be achieved by declaring it at the
199          * site where addOption() is called.
200          * The returned pointer must not be freed.
201          *
202          * See \link Options class documentation \endlink for example usage.
203          *
204          * \libinternal
205          * \p OptionType::InfoType must specify a type that derives from
206          * OptionInfo and matches the type that is returned by
207          * AbstractOptionStorage::optionInfo() for the storage object that
208          * corresponds to \p OptionType.
209          */
210         template <class OptionType>
211         typename OptionType::InfoType *addOption(const OptionType &settings)
212         {
213             OptionInfo *info
214                 = addOption(static_cast<const AbstractOption &>(settings));
215             GMX_ASSERT(info->isType<typename OptionType::InfoType>(),
216                        "Mismatching option info type declaration and implementation");
217             return info->toType<typename OptionType::InfoType>();
218         }
219
220         //! Returns true if option \p name is set.
221         bool isSet(const char *name) const;
222         /*! \brief
223          * Notifies the collection that all option values are assigned.
224          *
225          * \throws InvalidInputError if invalid user input is detected.
226          *
227          * This function should be called after no more option values are
228          * to be assigned.  Values in storage variables are guaranteed to be
229          * available only after this call, although in most cases, they are
230          * available already during assignment.
231          *
232          * If invalid option values, e.g., missing required option, is detected
233          * at this point, this function throws.  The thrown exception contains
234          * information on all errors detected during the call.
235          */
236         void finish();
237
238     private:
239         class Impl;
240
241         PrivateImplPointer<Impl> impl_;
242
243         //! Needed to be able to extend the interface of this object.
244         friend class OptionsAssigner;
245         //! Needed to be able to extend the interface of this object.
246         friend class OptionsIterator;
247         //! Needed to be able to extend the interface of this object.
248         friend class OptionsModifyingIterator;
249 };
250
251 } // namespace gmx
252
253 #endif