Fix copyright notices for new C++ code.
[alexxy/gromacs.git] / src / gromacs / options / basicoptions.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * 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 option objects for basic option types.
38  *
39  * Together with options.h, this header forms the part of the public API
40  * 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_BASICOPTIONS_H
47 #define GMX_OPTIONS_BASICOPTIONS_H
48
49 #include <string>
50
51 #include "../utility/gmxassert.h"
52
53 #include "abstractoption.h"
54
55 namespace gmx
56 {
57
58 class BooleanOptionInfo;
59 class BooleanOptionStorage;
60 class IntegerOptionInfo;
61 class IntegerOptionStorage;
62 class DoubleOptionInfo;
63 class DoubleOptionStorage;
64 class StringOptionInfo;
65 class StringOptionStorage;
66
67 /*! \addtogroup module_options
68  * \{
69  */
70
71 /*! \brief
72  * Specifies an option that provides boolean values.
73  *
74  * Example:
75  * \code
76    bool  bPBC;
77    using gmx::BooleanOption;
78    options.addOption(BooleanOption("pbc").store(&bPBC));
79  * \endcode
80  *
81  * Public methods in this class do not throw.
82  *
83  * \inpublicapi
84  */
85 class BooleanOption : public OptionTemplate<bool, BooleanOption>
86 {
87     public:
88         //! OptionInfo subclass corresponding to this option type.
89         typedef BooleanOptionInfo InfoType;
90
91         //! Initializes an option with the given name.
92         explicit BooleanOption(const char *name) : MyBase(name) {}
93
94     private:
95         //! Creates a BooleanOptionStorage object.
96         virtual AbstractOptionStoragePointer createStorage() const;
97 };
98
99 /*! \brief
100  * Specifies an option that provides integer values.
101  *
102  * Examples:
103  * \code
104    using gmx::IntegerOption;
105    // Simple option
106    int  rcut = 0;
107    options.addOption(IntegerOption("rcut").store(&rcut));
108    // Vector-valued option
109    int  box[3] = {1, 1, 1};  // Default value
110    options.addOption(IntegerOption("box").store(box).vector());
111  * \endcode
112  *
113  * Public methods in this class do not throw.
114  *
115  * \inpublicapi
116  */
117 class IntegerOption : public OptionTemplate<int, IntegerOption>
118 {
119     public:
120         //! OptionInfo subclass corresponding to this option type.
121         typedef IntegerOptionInfo InfoType;
122
123         //! Initializes an option with the given name.
124         explicit IntegerOption(const char *name) : MyBase(name) {}
125
126         /*! \brief
127          * Sets the option to return a vector value.
128          *
129          * A vector value returns a fixed number of values, the default being
130          * three (can be changed with valueCount()).  However, it also accepts
131          * a single value, in which case the value is used to fill the whole
132          * vector.
133          */
134         MyClass &vector() { setVector(); return me(); }
135
136     private:
137         //! Creates an IntegerOptionStorage object.
138         virtual AbstractOptionStoragePointer createStorage() const;
139
140         /*! \brief
141          * Needed to initialize IntegerOptionStorage from this class without
142          * otherwise unnecessary accessors.
143          */
144         friend class IntegerOptionStorage;
145 };
146
147 /*! \brief
148  * Specifies an option that provides floating-point (double) values.
149  *
150  * Public methods in this class do not throw.
151  *
152  * \inpublicapi
153  */
154 class DoubleOption : public OptionTemplate<double, DoubleOption>
155 {
156     public:
157         //! OptionInfo subclass corresponding to this option type.
158         typedef DoubleOptionInfo InfoType;
159
160         //! Initializes an option with the given name.
161         explicit DoubleOption(const char *name) : MyBase(name), bTime_(false)
162         {
163         }
164
165         //! \copydoc IntegerOption::vector()
166         MyClass &vector() { setVector(); return me(); }
167         /*! \brief
168          * Marks this option as providing a time value whose unit can be changed.
169          *
170          * By itself, this option does nothing.  It marks the option as a time
171          * value such that TimeUnitManager::scaleTimeOptions() can process it.
172          * In typical cases, Gromacs scales the time options just before
173          * Options::finish() has been called, so the option value is only
174          * available after all option values have been processed.
175          * All values in the program are in ps (including any default value);
176          * user-provided values are scaled according to the time unit set in
177          * TimeUnitManager.
178          */
179         MyClass &timeValue() { bTime_ = true; return me(); }
180
181     private:
182         //! Creates a DoubleOptionStorage object.
183         virtual AbstractOptionStoragePointer createStorage() const;
184
185         bool bTime_;
186
187         /*! \brief
188          * Needed to initialize DoubleOptionStorage from this class without
189          * otherwise unnecessary accessors.
190          */
191         friend class DoubleOptionStorage;
192 };
193
194 /*! \brief
195  * Specifies an option that provides string values.
196  *
197  * Examples:
198  * \code
199    using gmx::StringOption;
200    // Simple option
201    std::string  str;
202    options.addOption(StringOption("str").store(&str));
203    // Option that only accepts predefined values
204    const char * const  allowed[] = { "atom", "residue", "molecule", NULL };
205    std::string  str;
206    int          type;
207    options.addOption(StringOption("type").enumValue(allowed).store(&str)
208                         .storeEnumIndex(&type));
209  * \endcode
210  *
211  * Public methods in this class do not throw.
212  *
213  * \inpublicapi
214  */
215 class StringOption : public OptionTemplate<std::string, StringOption>
216 {
217     public:
218         //! OptionInfo subclass corresponding to this option type.
219         typedef StringOptionInfo InfoType;
220
221         //! Initializes an option with the given name.
222         explicit StringOption(const char *name)
223             : MyBase(name), enumValues_(NULL), defaultEnumIndex_(-1),
224               enumIndexStore_(NULL)
225         {
226         }
227
228         /*! \brief
229          * Sets the option to only accept one of a fixed set of strings.
230          *
231          * \param[in] values  Array of strings to accept, a NULL pointer
232          *      following the last string.
233          *
234          * Also accepts prefixes of the strings; if a prefix matches more than
235          * one of the possible strings, the shortest one is used (in a tie, the
236          * first one is).
237          *
238          * It is not possible to provide multiple values for an option with
239          * this property set, i.e., valueCount() and similar attributes cannot
240          * be set.
241          *
242          * The strings are copied once the option is created.
243          */
244         MyClass &enumValue(const char *const *values)
245         { enumValues_ = values; return me(); }
246         /*! \brief
247          * Sets the default value using an index into the enumeration table.
248          *
249          * Cannot be specified without enumValue().
250          */
251         MyClass &defaultEnumIndex(int index)
252         {
253             GMX_RELEASE_ASSERT(index >= 0, "Invalid enumeration index");
254             defaultEnumIndex_ = index;
255             return me();
256         }
257         /*! \brief
258          * Stores the index of the selected value into the provided memory
259          * location.
260          *
261          * The index (zero-based) of the selected value in the array \p values
262          * provided to enumValues() is written into \p *store after the
263          * option gets its value.  If the option has not been provided,
264          * and there is no default value, -1 is stored.
265          *
266          * Cannot be specified without enumValue().
267          */
268         MyClass &storeEnumIndex(int *store)
269         { enumIndexStore_ = store; return me(); }
270
271     private:
272         //! Creates a StringOptionStorage object.
273         virtual AbstractOptionStoragePointer createStorage() const;
274         virtual std::string createDescription() const;
275
276         const char *const      *enumValues_;
277         int                     defaultEnumIndex_;
278         int                    *enumIndexStore_;
279
280         /*! \brief
281          * Needed to initialize StringOptionStorage from this class without
282          * otherwise unnecessary accessors.
283          */
284         friend class StringOptionStorage;
285 };
286
287 /*! \brief
288  * Wrapper class for accessing boolean option information.
289  *
290  * \inpublicapi
291  */
292 class BooleanOptionInfo : public OptionInfo
293 {
294     public:
295         //! Creates an option info object for the given option.
296         explicit BooleanOptionInfo(BooleanOptionStorage *option);
297 };
298
299 /*! \brief
300  * Wrapper class for accessing integer option information.
301  *
302  * \inpublicapi
303  */
304 class IntegerOptionInfo : public OptionInfo
305 {
306     public:
307         //! Creates an option info object for the given option.
308         explicit IntegerOptionInfo(IntegerOptionStorage *option);
309 };
310
311 /*! \brief
312  * Wrapper class for accessing floating-point option information.
313  *
314  * \inpublicapi
315  */
316 class DoubleOptionInfo : public OptionInfo
317 {
318     public:
319         //! Creates an option info object for the given option.
320         explicit DoubleOptionInfo(DoubleOptionStorage *option);
321
322         //! Whether the option specifies a time value.
323         bool isTime() const;
324
325         /*! \brief
326          * Sets a scale factor for user-provided values.
327          *
328          * Any user-provided value is scaled by the provided factor.
329          * Programmatically set default values are not scaled.
330          * If called multiple times, later calls override the previously set
331          * value.  In other words, the scaling is not cumulative.
332          */
333         void setScaleFactor(double factor);
334
335     private:
336         DoubleOptionStorage &option();
337         const DoubleOptionStorage &option() const;
338 };
339
340 /*! \brief
341  * Wrapper class for accessing string option information.
342  *
343  * \inpublicapi
344  */
345 class StringOptionInfo : public OptionInfo
346 {
347     public:
348         //! Creates an option info object for the given option.
349         explicit StringOptionInfo(StringOptionStorage *option);
350 };
351
352 /*!\}*/
353
354 } // namespace gmx
355
356 #endif