2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Declares option objects for basic option types.
39 * Together with options.h, this header forms the part of the public API
40 * that most classes will use to provide options.
42 * \author Teemu Murtola <teemu.murtola@gmail.com>
44 * \ingroup module_options
46 #ifndef GMX_OPTIONS_BASICOPTIONS_H
47 #define GMX_OPTIONS_BASICOPTIONS_H
51 #include "../utility/gmxassert.h"
53 #include "abstractoption.h"
58 class BooleanOptionInfo;
59 class BooleanOptionStorage;
60 class IntegerOptionInfo;
61 class IntegerOptionStorage;
62 class DoubleOptionInfo;
63 class DoubleOptionStorage;
64 class StringOptionInfo;
65 class StringOptionStorage;
67 /*! \addtogroup module_options
72 * Specifies an option that provides boolean values.
77 using gmx::BooleanOption;
78 options.addOption(BooleanOption("pbc").store(&bPBC));
81 * Public methods in this class do not throw.
85 class BooleanOption : public OptionTemplate<bool, BooleanOption>
88 //! OptionInfo subclass corresponding to this option type.
89 typedef BooleanOptionInfo InfoType;
91 //! Initializes an option with the given name.
92 explicit BooleanOption(const char *name) : MyBase(name) {}
95 //! Creates a BooleanOptionStorage object.
96 virtual AbstractOptionStoragePointer createStorage() const;
100 * Specifies an option that provides integer values.
104 using gmx::IntegerOption;
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());
113 * Public methods in this class do not throw.
117 class IntegerOption : public OptionTemplate<int, IntegerOption>
120 //! OptionInfo subclass corresponding to this option type.
121 typedef IntegerOptionInfo InfoType;
123 //! Initializes an option with the given name.
124 explicit IntegerOption(const char *name) : MyBase(name) {}
127 * Sets the option to return a vector value.
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
134 MyClass &vector() { setVector(); return me(); }
137 //! Creates an IntegerOptionStorage object.
138 virtual AbstractOptionStoragePointer createStorage() const;
141 * Needed to initialize IntegerOptionStorage from this class without
142 * otherwise unnecessary accessors.
144 friend class IntegerOptionStorage;
148 * Specifies an option that provides floating-point (double) values.
150 * Public methods in this class do not throw.
154 class DoubleOption : public OptionTemplate<double, DoubleOption>
157 //! OptionInfo subclass corresponding to this option type.
158 typedef DoubleOptionInfo InfoType;
160 //! Initializes an option with the given name.
161 explicit DoubleOption(const char *name) : MyBase(name), bTime_(false)
165 //! \copydoc IntegerOption::vector()
166 MyClass &vector() { setVector(); return me(); }
168 * Marks this option as providing a time value whose unit can be changed.
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
179 MyClass &timeValue() { bTime_ = true; return me(); }
182 //! Creates a DoubleOptionStorage object.
183 virtual AbstractOptionStoragePointer createStorage() const;
188 * Needed to initialize DoubleOptionStorage from this class without
189 * otherwise unnecessary accessors.
191 friend class DoubleOptionStorage;
195 * Specifies an option that provides string values.
199 using gmx::StringOption;
202 options.addOption(StringOption("str").store(&str));
203 // Option that only accepts predefined values
204 const char * const allowed[] = { "atom", "residue", "molecule", NULL };
207 options.addOption(StringOption("type").enumValue(allowed).store(&str)
208 .storeEnumIndex(&type));
211 * Public methods in this class do not throw.
215 class StringOption : public OptionTemplate<std::string, StringOption>
218 //! OptionInfo subclass corresponding to this option type.
219 typedef StringOptionInfo InfoType;
221 //! Initializes an option with the given name.
222 explicit StringOption(const char *name)
223 : MyBase(name), enumValues_(NULL), defaultEnumIndex_(-1),
224 enumIndexStore_(NULL)
229 * Sets the option to only accept one of a fixed set of strings.
231 * \param[in] values Array of strings to accept, a NULL pointer
232 * following the last string.
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
238 * It is not possible to provide multiple values for an option with
239 * this property set, i.e., valueCount() and similar attributes cannot
242 * The strings are copied once the option is created.
244 MyClass &enumValue(const char *const *values)
245 { enumValues_ = values; return me(); }
247 * Sets the default value using an index into the enumeration table.
249 * Cannot be specified without enumValue().
251 MyClass &defaultEnumIndex(int index)
253 GMX_RELEASE_ASSERT(index >= 0, "Invalid enumeration index");
254 defaultEnumIndex_ = index;
258 * Stores the index of the selected value into the provided memory
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.
266 * Cannot be specified without enumValue().
268 MyClass &storeEnumIndex(int *store)
269 { enumIndexStore_ = store; return me(); }
272 //! Creates a StringOptionStorage object.
273 virtual AbstractOptionStoragePointer createStorage() const;
274 virtual std::string createDescription() const;
276 const char *const *enumValues_;
277 int defaultEnumIndex_;
278 int *enumIndexStore_;
281 * Needed to initialize StringOptionStorage from this class without
282 * otherwise unnecessary accessors.
284 friend class StringOptionStorage;
288 * Wrapper class for accessing boolean option information.
292 class BooleanOptionInfo : public OptionInfo
295 //! Creates an option info object for the given option.
296 explicit BooleanOptionInfo(BooleanOptionStorage *option);
300 * Wrapper class for accessing integer option information.
304 class IntegerOptionInfo : public OptionInfo
307 //! Creates an option info object for the given option.
308 explicit IntegerOptionInfo(IntegerOptionStorage *option);
312 * Wrapper class for accessing floating-point option information.
316 class DoubleOptionInfo : public OptionInfo
319 //! Creates an option info object for the given option.
320 explicit DoubleOptionInfo(DoubleOptionStorage *option);
322 //! Whether the option specifies a time value.
326 * Sets a scale factor for user-provided values.
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.
333 void setScaleFactor(double factor);
336 DoubleOptionStorage &option();
337 const DoubleOptionStorage &option() const;
341 * Wrapper class for accessing string option information.
345 class StringOptionInfo : public OptionInfo
348 //! Creates an option info object for the given option.
349 explicit StringOptionInfo(StringOptionStorage *option);