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