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