Merge remote-tracking branch 'origin/release-4-6'
[alexxy/gromacs.git] / src / gromacs / options / basicoptions.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 option settings objects for basic option types.
34  *
35  * Together with options.h, this header forms the part of the public API
36  * 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_BASICOPTIONS_H
43 #define GMX_OPTIONS_BASICOPTIONS_H
44
45 #include <string>
46
47 #include "../utility/gmxassert.h"
48
49 #include "abstractoption.h"
50
51 namespace gmx
52 {
53
54 class IntegerOptionStorage;
55 class DoubleOptionStorage;
56 class StringOptionStorage;
57
58 /*! \addtogroup module_options
59  * \{
60  */
61
62 /*! \brief
63  * Specifies an option that provides boolean values.
64  *
65  * Example:
66  * \code
67 bool  bPBC;
68 using gmx::BooleanOption;
69 options.addOption(BooleanOption("pbc").store(&bPBC));
70  * \endcode
71  *
72  * Public methods in this class do not throw.
73  *
74  * \inpublicapi
75  */
76 class BooleanOption : public OptionTemplate<bool, BooleanOption>
77 {
78     public:
79         //! Initializes an option with the given name.
80         explicit BooleanOption(const char *name) : MyBase(name) {}
81
82     private:
83         //! Creates a BooleanOptionStorage object.
84         virtual AbstractOptionStoragePointer createStorage() const;
85 };
86
87 /*! \brief
88  * Specifies an option that provides integer values.
89  *
90  * Examples:
91  * \code
92 using gmx::IntegerOption;
93 // Simple option
94 int  rcut = 0;
95 options.addOption(IntegerOption("rcut").store(&rcut));
96 // Vector-valued option
97 int  box[3] = {1, 1, 1};  // Default value
98 options.addOption(IntegerOption("box").store(box).vector());
99  * \endcode
100  *
101  * Public methods in this class do not throw.
102  *
103  * \inpublicapi
104  */
105 class IntegerOption : public OptionTemplate<int, IntegerOption>
106 {
107     public:
108         //! Initializes an option with the given name.
109         explicit IntegerOption(const char *name) : MyBase(name) {}
110
111         /*! \brief
112          * Sets the option to return a vector value.
113          *
114          * A vector value returns a fixed number of values, the default being
115          * three (can be changed with valueCount()).  However, it also accepts
116          * a single value, in which case the value is used to fill the whole
117          * vector.
118          */
119         MyClass &vector() { setVector(); return me(); }
120
121     private:
122         //! Creates an IntegerOptionStorage object.
123         virtual AbstractOptionStoragePointer createStorage() const;
124
125         /*! \brief
126          * Needed to initialize IntegerOptionStorage from this class without
127          * otherwise unnecessary accessors.
128          */
129         friend class IntegerOptionStorage;
130 };
131
132 /*! \brief
133  * Specifies an option that provides floating-point (double) values.
134  *
135  * Public methods in this class do not throw.
136  *
137  * \inpublicapi
138  */
139 class DoubleOption : public OptionTemplate<double, DoubleOption>
140 {
141     public:
142         //! Initializes an option with the given name.
143         explicit DoubleOption(const char *name) : MyBase(name), _bTime(false)
144         {
145         }
146
147         //! \copydoc IntegerOption::vector()
148         MyClass &vector() { setVector(); return me(); }
149         /*! \brief
150          * Marks this option as providing a time value whose unit can be changed.
151          *
152          * By itself, this option does nothing.  It marks the option as a time
153          * value such that TimeUnitManager::scaleTimeOptions() can process it.
154          * In typical cases, Gromacs scales the time options just before
155          * Options::finish() has been called, so the option value is only
156          * available after all option values have been processed.
157          * All values in the program are in ps (including any default value);
158          * user-provided values are scaled according to the time unit set in
159          * TimeUnitManager.
160          */
161         MyClass &timeValue() { _bTime = true; return me(); }
162
163     private:
164         //! Creates a DoubleOptionStorage object.
165         virtual AbstractOptionStoragePointer createStorage() const;
166
167         bool _bTime;
168
169         /*! \brief
170          * Needed to initialize DoubleOptionStorage from this class without
171          * otherwise unnecessary accessors.
172          */
173         friend class DoubleOptionStorage;
174 };
175
176 /*! \brief
177  * Specifies an option that provides string values.
178  *
179  * Examples:
180  * \code
181 using gmx::StringOption;
182 // Simple option
183 std::string  str;
184 options.addOption(StringOption("str").store(&str));
185 // Option that only accepts predefined values
186 const char * const  allowed[] = { "atom", "residue", "molecule", NULL };
187 std::string  str;
188 int          type;
189 options.addOption(StringOption("type").enumValue(allowed).store(&str)
190                      .storeEnumIndex(&type));
191  * \endcode
192  *
193  * Public methods in this class do not throw.
194  *
195  * \inpublicapi
196  */
197 class StringOption : public OptionTemplate<std::string, StringOption>
198 {
199     public:
200         //! Initializes an option with the given name.
201         explicit StringOption(const char *name)
202             : MyBase(name), _enumValues(NULL), _defaultEnumIndex(-1),
203               _enumIndexStore(NULL)
204         {
205         }
206
207         /*! \brief
208          * Sets the option to only accept one of a fixed set of strings.
209          *
210          * \param[in] values  Array of strings to accept, a NULL pointer
211          *      following the last string.
212          *
213          * Also accepts prefixes of the strings; if a prefix matches more than
214          * one of the possible strings, the shortest one is used (in a tie, the
215          * first one is).
216          *
217          * It is not possible to provide multiple values for an option with
218          * this property set, i.e., valueCount() and similar attributes cannot
219          * be set.
220          *
221          * The strings are copied once the option is created.
222          */
223         MyClass &enumValue(const char *const *values)
224         { _enumValues = values; return me(); }
225         /*! \brief
226          * Sets the default value using an index into the enumeration table.
227          *
228          * Cannot be specified without enumValue().
229          */
230         MyClass &defaultEnumIndex(int index)
231         {
232             GMX_RELEASE_ASSERT(index >= 0, "Invalid enumeration index");
233             _defaultEnumIndex = index;
234             return me();
235         }
236         /*! \brief
237          * Stores the index of the selected value into the provided memory
238          * location.
239          *
240          * The index (zero-based) of the selected value in the array \p values
241          * provided to enumValues() is written into \p *store after the
242          * option gets its value.  If the option has not been provided,
243          * and there is no default value, -1 is stored.
244          *
245          * Cannot be specified without enumValue().
246          */
247         MyClass &storeEnumIndex(int *store)
248         { _enumIndexStore = store; return me(); }
249
250     private:
251         //! Creates a StringOptionStorage object.
252         virtual AbstractOptionStoragePointer createStorage() const;
253         virtual std::string createDescription() const;
254
255         const char *const      *_enumValues;
256         int                     _defaultEnumIndex;
257         int                    *_enumIndexStore;
258
259         /*! \brief
260          * Needed to initialize StringOptionStorage from this class without
261          * otherwise unnecessary accessors.
262          */
263         friend class StringOptionStorage;
264 };
265
266 /*!\}*/
267
268 } // namespace gmx
269
270 #endif