Merge "Merge remote-tracking branch 'origin/release-4-6'"
[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
49 namespace gmx
50 {
51
52 class AbstractOption;
53 class OptionsAssigner;
54 class OptionsIterator;
55
56 /*! \brief
57  * Collection of options.
58  *
59  * This class provides a standard interface for implementing input options.
60  * Standard usage is to write a method that creates an Options that is owned by
61  * the object, populates it with supported options, and then returns it:
62  * \code
63 // <as class attributes>
64 using gmx::Options;
65 Options      options("common", "Common Options");
66 std::string  arg1;
67 int          arg2;
68
69 // <populating>
70 using gmx::StringOption;
71 using gmx::IntegerOption;
72 options.addOption(StringOption("arg1").store(&arg1));
73 options.addOption(IntegerOption("arg2").store(&arg2));
74 return &options;
75  * \endcode
76  * The caller of that method can then use a parser implementation such as
77  * CommandLineParser to provide values for the options.
78  *
79  * Header basicoptions.h provides declarations of several standard
80  * option types for use with addOption().  Documentation of those classes
81  * also give more examples of how to define options.
82  *
83  * In order to keep the public interface of this class simple and to reduce
84  * build dependencies on objects that simply provide options, functionality
85  * to assign values to options is provided by a separate OptionsAssigner class.
86  * Similarly, functionality for looping over all options (e.g., for writing out
87  * help) is provided by OptionsIterator.
88  *
89  * \inpublicapi
90  * \ingroup module_options
91  */
92 class Options
93 {
94     public:
95         /*! \brief
96          * Initializes the name and title of an option collection.
97          *
98          * \param[in] name  Single-word name.
99          * \param[in] title Descriptive title.
100          *
101          * Copies the input strings.
102          */
103         Options(const char *name, const char *title);
104         ~Options();
105
106         //! Returns the short name of the option collection.
107         const std::string &name() const;
108         //! Returns the title of the option collection.
109         const std::string &title() const;
110         //! Returns the full description of the option collection.
111         const std::string &description() const;
112
113         /*! \brief
114          * Sets the full description of the option collection.
115          *
116          * \param[in] desc  String to set as the description.
117          *
118          * concatenateStrings() is useful for forming the input string.
119          */
120         void setDescription(const std::string &desc);
121         //int addBugs(int nbugs, const char *const *bugs);
122
123         /*! \brief
124          * Adds an option collection as a subsection of this collection.
125          *
126          * \param[in] section Subsection to add.
127          *
128          * The name() field of \p section is used as the name of the
129          * subsection.  If an attempt is made to add two different subsections
130          * with the same name, this function asserts.
131          *
132          * For certain functionality to work properly, no options should
133          * be added to the subsection after it has been added to another
134          * collection.
135          *
136          * Only a pointer to the provided object is stored.  The caller is
137          * responsible that the object exists for the lifetime of the
138          * collection.
139          * It is not possible to add the same Options object as a subsection to
140          * several different Options.
141          * If an attempt is made, the function asserts.
142          */
143         void addSubSection(Options *section);
144         /*! \brief
145          * Adds a recognized option to the collection.
146          *
147          * \throws APIError if invalid option settings are provided.
148          *
149          * See \link Options class documentation \endlink for example usage.
150          */
151         void addOption(const AbstractOption &settings);
152
153         //! Returns true if option \p name is set.
154         bool isSet(const char *name) const;
155         /*! \brief
156          * Notifies the collection that all option values are assigned.
157          *
158          * \throws InvalidInputError if invalid user input is detected.
159          *
160          * This function should be called after no more option values are
161          * to be assigned.  Values in storage variables are guaranteed to be
162          * available only after this call, although in most cases, they are
163          * available already during assignment.
164          *
165          * If invalid option values, e.g., missing required option, is detected
166          * at this point, this function throws.  The thrown exception contains
167          * information on all errors detected during the call.
168          */
169         void finish();
170
171     private:
172         class Impl;
173
174         PrivateImplPointer<Impl> impl_;
175
176         //! Needed to be able to extend the interface of this object.
177         friend class OptionsAssigner;
178         //! Needed to be able to extend the interface of this object.
179         friend class OptionsIterator;
180         //! Needed to be able to extend the interface of this object.
181         friend class OptionsModifyingIterator;
182 };
183
184 } // namespace gmx
185
186 #endif