Merge remote-tracking branch 'gerrit/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 Array of strings to form the description, terminated
117          *      by a NULL pointer.
118          *
119          * The strings in the \p desc array are concatenated to form the
120          * description, adding a single space between the strings if there is
121          * no whitespace in the end of a string.
122          */
123         void setDescription(const char *const *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          * \throws APIError if invalid option settings are provided.
151          *
152          * See \link Options class documentation \endlink for example usage.
153          */
154         void addOption(const AbstractOption &settings);
155
156         //! Returns true if option \p name is set.
157         bool isSet(const char *name) const;
158         /*! \brief
159          * Notifies the collection that all option values are assigned.
160          *
161          * \throws InvalidInputError if invalid user input is detected.
162          *
163          * This function should be called after no more option values are
164          * to be assigned.  Values in storage variables are guaranteed to be
165          * available only after this call, although in most cases, they are
166          * available already during assignment.
167          *
168          * If invalid option values, e.g., missing required option, is detected
169          * at this point, this function throws.  The thrown exception contains
170          * information on all errors detected during the call.
171          */
172         void finish();
173
174     private:
175         class Impl;
176
177         PrivateImplPointer<Impl> _impl;
178
179         //! Needed to be able to extend the interface of this object.
180         friend class OptionsAssigner;
181         //! Needed to be able to extend the interface of this object.
182         friend class OptionsIterator;
183         //! Needed to be able to extend the interface of this object.
184         friend class OptionsModifyingIterator;
185 };
186
187 } // namespace gmx
188
189 #endif