Sort all includes in src/gromacs
[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 "gromacs/options/abstractoption.h"
52 #include "gromacs/utility/common.h"
53 #include "gromacs/utility/gmxassert.h"
54
55 namespace gmx
56 {
57
58 template <typename T> class ConstArrayRef;
59
60 class AbstractOption;
61 class OptionsAssigner;
62 class OptionsIterator;
63
64 /*! \brief
65  * Base class for option managers.
66  *
67  * This class is used as a marker for all classes that are used with
68  * Options::addManager().  It doesn't provide any methods, but only supports
69  * transporting these classes through the Options collection into the
70  * individual option implementation classes.
71  *
72  * The virtual destructor is present to make this class polymorphic, such that
73  * `dynamic_cast` can be used when retrieving a manager of a certain type for
74  * the individual options.
75  *
76  * \inlibraryapi
77  * \ingroup module_options
78  */
79 class OptionManagerInterface
80 {
81     protected:
82         virtual ~OptionManagerInterface();
83 };
84
85 /*! \brief
86  * Collection of options.
87  *
88  * This class provides a standard interface for implementing input options.
89  * Standard usage is to write a method that creates an Options that is owned by
90  * the object, populates it with supported options, and then returns it:
91  * \code
92    // <as class attributes>
93    using gmx::Options;
94    Options      options("common", "Common Options");
95    std::string  arg1;
96    int          arg2;
97
98    // <populating>
99    using gmx::StringOption;
100    using gmx::IntegerOption;
101    options.addOption(StringOption("arg1").store(&arg1));
102    options.addOption(IntegerOption("arg2").store(&arg2));
103    return &options;
104    \endcode
105  * The caller of that method can then use a parser implementation such as
106  * CommandLineParser to provide values for the options.
107  *
108  * Header basicoptions.h provides declarations of several standard
109  * option types for use with addOption().  Documentation of those classes
110  * also give more examples of how to define options.
111  *
112  * In order to keep the public interface of this class simple and to reduce
113  * build dependencies on objects that simply provide options, functionality
114  * to assign values to options is provided by a separate OptionsAssigner class.
115  * Similarly, functionality for looping over all options (e.g., for writing out
116  * help) is provided by OptionsIterator.
117  *
118  * \inpublicapi
119  * \ingroup module_options
120  */
121 class Options
122 {
123     public:
124         /*! \brief
125          * Initializes the name and title of an option collection.
126          *
127          * \param[in] name  Single-word name.
128          * \param[in] title Descriptive title.
129          *
130          * Copies the input strings.
131          */
132         Options(const char *name, const char *title);
133         ~Options();
134
135         //! Returns the short name of the option collection.
136         const std::string &name() const;
137         //! Returns the title of the option collection.
138         const std::string &title() const;
139         //! Returns the full description of the option collection.
140         const std::string &description() const;
141
142         /*! \brief
143          * Sets the full description of the option collection.
144          *
145          * \param[in] desc  String to set as the description.
146          *
147          * This overload is mainly useful if the description is very short.
148          * Currently this is mostly the case in unit testing.
149          */
150         void setDescription(const std::string &desc);
151         /*! \brief
152          * Sets the full description of the option collection from string array.
153          *
154          * \param[in] descArray  String array to set as the description.
155          *
156          * All strings in `descArray` are concatenated to form the description.
157          * Spaces are inserted between strings if they are missing.
158          *
159          * Example usage:
160          * \code
161            const char *const desc[] = {
162                "This is the description",
163                "for the options"
164            };
165
166            gmx::Options options(NULL, NULL);
167            options.setDescription(desc);
168            \endcode
169          *
170          * To use this overload, you must also include
171          * `gromacs/utility/arrayref.h`.
172          */
173         void setDescription(const ConstArrayRef<const char *> &descArray);
174
175         /*! \brief
176          * Adds an option manager.
177          *
178          * \param    manager Manager to add.
179          * \throws   std::bad_alloc if out of memory.
180          *
181          * Option managers are used by some types of options that require
182          * interaction between different option instances (e.g., selection
183          * options), or need to support globally set properties (e.g., a global
184          * default file prefix).  Option objects can retrieve the pointer to
185          * their manager when they are created, and the caller can alter the
186          * behavior of the options through the manager.
187          * See the individual managers for details.
188          *
189          * Caller is responsible for memory management of \p manager.
190          * The Options object (and its contained options) only stores a
191          * reference to the object.
192          *
193          * This method cannot be called after adding options or subsections.
194          */
195         void addManager(OptionManagerInterface *manager);
196
197         /*! \brief
198          * Adds an option collection as a subsection of this collection.
199          *
200          * \param[in] section Subsection to add.
201          *
202          * The name() field of \p section is used as the name of the
203          * subsection.  If an attempt is made to add two different subsections
204          * with the same name, this function asserts.
205          *
206          * \p section should not have any options added at the point this
207          * method is called.
208          *
209          * Only a pointer to the provided object is stored.  The caller is
210          * responsible that the object exists for the lifetime of the
211          * collection.
212          * It is not possible to add the same Options object as a subsection to
213          * several different Options.
214          * If an attempt is made, the function asserts.
215          */
216         void addSubSection(Options *section);
217         /*! \brief
218          * Adds a recognized option to the collection.
219          *
220          * \param[in] settings Option description.
221          * \returns   OptionInfo object for the created option (never NULL).
222          * \throws    APIError if invalid option settings are provided.
223          *
224          * This method provides the internal implementation, but in most cases
225          * the templated method is called from user code.
226          * See the templated method for more details.
227          */
228         OptionInfo *addOption(const AbstractOption &settings);
229         /*! \brief
230          * Adds a recognized option to the collection.
231          *
232          * \tparam    OptionType Type of the options description object.
233          * \param[in] settings   Option description.
234          * \returns   OptionInfo object for the created option (never NULL).
235          * \throws    APIError if invalid option settings are provided.
236          *
237          * The return value is a pointer for more convenient use in callers:
238          * often callers need to declare the variable that will hold the return
239          * value in wider scope than would be achieved by declaring it at the
240          * site where addOption() is called.
241          * The returned pointer must not be freed.
242          *
243          * See \link Options class documentation \endlink for example usage.
244          *
245          * \libinternal
246          * \p OptionType::InfoType must specify a type that derives from
247          * OptionInfo and matches the type that is returned by
248          * AbstractOptionStorage::optionInfo() for the storage object that
249          * corresponds to \p OptionType.
250          */
251         template <class OptionType>
252         typename OptionType::InfoType *addOption(const OptionType &settings)
253         {
254             OptionInfo *info
255                 = addOption(static_cast<const AbstractOption &>(settings));
256             GMX_ASSERT(info->isType<typename OptionType::InfoType>(),
257                        "Mismatching option info type declaration and implementation");
258             return info->toType<typename OptionType::InfoType>();
259         }
260
261         //! Returns true if option \p name is set.
262         bool isSet(const char *name) const;
263         /*! \brief
264          * Notifies the collection that all option values are assigned.
265          *
266          * \throws InvalidInputError if invalid user input is detected.
267          *
268          * This function should be called after no more option values are
269          * to be assigned.  Values in storage variables are guaranteed to be
270          * available only after this call, although in most cases, they are
271          * available already during assignment.
272          *
273          * If invalid option values, e.g., missing required option, is detected
274          * at this point, this function throws.  The thrown exception contains
275          * information on all errors detected during the call.
276          */
277         void finish();
278
279     private:
280         class Impl;
281
282         PrivateImplPointer<Impl> impl_;
283
284         //! Needed to be able to extend the interface of this object.
285         friend class OptionsAssigner;
286         //! Needed to be able to extend the interface of this object.
287         friend class OptionsIterator;
288         //! Needed to be able to extend the interface of this object.
289         friend class OptionsModifyingIterator;
290 };
291
292 } // namespace gmx
293
294 #endif