2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source 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/basedefinitions.h"
52 #include "../utility/gmxassert.h"
54 #include "abstractoption.h"
59 class BooleanOptionInfo;
60 class BooleanOptionStorage;
61 class IntegerOptionInfo;
62 class IntegerOptionStorage;
63 class Int64OptionInfo;
64 class Int64OptionStorage;
65 class DoubleOptionInfo;
66 class DoubleOptionStorage;
67 class FloatOptionInfo;
68 class FloatOptionStorage;
69 class StringOptionInfo;
70 class StringOptionStorage;
72 /*! \addtogroup module_options
77 * Specifies an option that provides boolean values.
82 using gmx::BooleanOption;
83 options.addOption(BooleanOption("pbc").store(&bPBC));
86 * Public methods in this class do not throw.
90 class BooleanOption : public OptionTemplate<bool, BooleanOption>
93 //! OptionInfo subclass corresponding to this option type.
94 typedef BooleanOptionInfo InfoType;
96 //! Initializes an option with the given name.
97 explicit BooleanOption(const char *name) : MyBase(name) {}
100 //! Creates a BooleanOptionStorage object.
101 virtual AbstractOptionStoragePointer createStorage() const;
105 * Specifies an option that provides integer values.
109 using gmx::IntegerOption;
112 options.addOption(IntegerOption("rcut").store(&rcut));
113 // Vector-valued option
114 int box[3] = {1, 1, 1}; // Default value
115 options.addOption(IntegerOption("box").store(box).vector());
118 * Public methods in this class do not throw.
122 class IntegerOption : public OptionTemplate<int, IntegerOption>
125 //! OptionInfo subclass corresponding to this option type.
126 typedef IntegerOptionInfo InfoType;
128 //! Initializes an option with the given name.
129 explicit IntegerOption(const char *name) : MyBase(name) {}
132 * Sets the option to return a vector value.
134 * A vector value returns a fixed number of values, the default being
135 * three (can be changed with valueCount()). However, it also accepts
136 * a single value, in which case the value is used to fill the whole
139 MyClass &vector() { setVector(); return me(); }
142 //! Creates an IntegerOptionStorage object.
143 virtual AbstractOptionStoragePointer createStorage() const;
146 * Needed to initialize IntegerOptionStorage from this class without
147 * otherwise unnecessary accessors.
149 friend class IntegerOptionStorage;
153 * Specifies an option that provides 64-bit integer values.
155 * Public methods in this class do not throw.
161 class Int64Option : public OptionTemplate<gmx_int64_t, Int64Option>
164 //! OptionInfo subclass corresponding to this option type.
165 typedef Int64OptionInfo InfoType;
167 //! Initializes an option with the given name.
168 explicit Int64Option(const char *name) : MyBase(name) {}
171 //! Creates an Int64OptionStorage object.
172 virtual AbstractOptionStoragePointer createStorage() const;
175 * Needed to initialize Int64OptionStorage from this class without
176 * otherwise unnecessary accessors.
178 friend class Int64OptionStorage;
182 * Specifies an option that provides floating-point (double) values.
184 * Public methods in this class do not throw.
188 class DoubleOption : public OptionTemplate<double, DoubleOption>
191 //! OptionInfo subclass corresponding to this option type.
192 typedef DoubleOptionInfo InfoType;
194 //! Initializes an option with the given name.
195 explicit DoubleOption(const char *name) : MyBase(name), bTime_(false)
199 //! \copydoc IntegerOption::vector()
200 MyClass &vector() { setVector(); return me(); }
202 * Marks this option as providing a time value whose unit can be changed.
204 * By itself, this option does nothing. It marks the option as a time
205 * value such that TimeUnitManager::scaleTimeOptions() can process it.
206 * In typical cases, \Gromacs scales the time options just before
207 * Options::finish() has been called, so the option value is only
208 * available after all option values have been processed.
209 * All values in the program are in ps (including any default value);
210 * user-provided values are scaled according to the time unit set in
213 MyClass &timeValue() { bTime_ = true; return me(); }
216 //! Creates a DoubleOptionStorage object.
217 virtual AbstractOptionStoragePointer createStorage() const;
222 * Needed to initialize DoubleOptionStorage from this class without
223 * otherwise unnecessary accessors.
225 friend class DoubleOptionStorage;
229 * Specifies an option that provides floating-point (float) values.
231 * Public methods in this class do not throw.
237 class FloatOption : public OptionTemplate<float, FloatOption>
240 //! OptionInfo subclass corresponding to this option type.
241 typedef FloatOptionInfo InfoType;
243 //! Initializes an option with the given name.
244 explicit FloatOption(const char *name) : MyBase(name), bTime_(false)
248 //! \copydoc IntegerOption::vector()
249 MyClass &vector() { setVector(); return me(); }
250 //! \copydoc DoubleOption::timeValue()
251 MyClass &timeValue() { bTime_ = true; return me(); }
254 //! Creates a FloatOptionStorage object.
255 virtual AbstractOptionStoragePointer createStorage() const;
260 * Needed to initialize FloatOptionStorage from this class without
261 * otherwise unnecessary accessors.
263 friend class FloatOptionStorage;
267 * Specifies an option that provides string values.
271 using gmx::StringOption;
274 options.addOption(StringOption("str").store(&str));
275 // Option that only accepts predefined values
276 const char * const allowed[] = { "atom", "residue", "molecule" };
279 options.addOption(StringOption("type").enumValue(allowed).store(&str)
280 .storeEnumIndex(&type));
283 * Public methods in this class do not throw.
287 class StringOption : public OptionTemplate<std::string, StringOption>
290 //! OptionInfo subclass corresponding to this option type.
291 typedef StringOptionInfo InfoType;
293 //! Initializes an option with the given name.
294 explicit StringOption(const char *name)
295 : MyBase(name), enumValues_(NULL), enumValuesCount_(0),
296 defaultEnumIndex_(-1), enumIndexStore_(NULL)
301 * Sets the option to only accept one of a fixed set of strings.
303 * \param[in] values Array of strings to accept.
305 * Also accepts prefixes of the strings; if a prefix matches more than
306 * one of the possible strings, the shortest one is used (in a tie, the
309 * It is not possible to provide multiple values for an option with
310 * this property set, i.e., valueCount() and similar attributes cannot
313 * The strings are copied once the option is created.
315 template <size_t count>
316 MyClass &enumValue(const char *const (&values)[count])
318 GMX_ASSERT(enumValues_ == NULL,
319 "Multiple sets of enumerated values specified");
320 enumValues_ = values;
321 enumValuesCount_ = count;
325 * Sets the option to only accept one of a fixed set of strings.
327 * \param[in] values Array of strings to accept, with a NULL pointer
328 * following the last string.
330 * Works otherwise as the array version, but accepts a pointer to
331 * an array of undetermined length. The end of the array is indicated
332 * by a NULL pointer in the array.
336 MyClass &enumValueFromNullTerminatedArray(const char *const *values)
338 GMX_ASSERT(enumValues_ == NULL,
339 "Multiple sets of enumerated values specified");
340 enumValues_ = values;
341 enumValuesCount_ = -1;
345 * Sets the default value using an index into the enumeration table.
347 * Cannot be specified without enumValue().
349 MyClass &defaultEnumIndex(int index)
351 GMX_ASSERT(index >= 0, "Invalid enumeration index");
352 defaultEnumIndex_ = index;
356 * Stores the index of the selected value into the provided memory
359 * The index (zero-based) of the selected value in the array \p values
360 * provided to enumValues() is written into \p *store after the
361 * option gets its value. If the option has not been provided,
362 * and there is no default value, -1 is stored.
364 * Cannot be specified without enumValue().
367 * Implement this such that it is also possible to store the value
368 * directly into a real enum type.
370 MyClass &storeEnumIndex(int *store)
371 { enumIndexStore_ = store; return me(); }
374 //! Creates a StringOptionStorage object.
375 virtual AbstractOptionStoragePointer createStorage() const;
377 const char *const *enumValues_;
378 int enumValuesCount_;
379 int defaultEnumIndex_;
380 int *enumIndexStore_;
383 * Needed to initialize StringOptionStorage from this class without
384 * otherwise unnecessary accessors.
386 friend class StringOptionStorage;
390 * Wrapper class for accessing boolean option information.
394 class BooleanOptionInfo : public OptionInfo
397 //! Creates an option info object for the given option.
398 explicit BooleanOptionInfo(BooleanOptionStorage *option);
400 //! Returns the default value for this option.
401 bool defaultValue() const;
404 const BooleanOptionStorage &option() const;
408 * Wrapper class for accessing integer option information.
412 class IntegerOptionInfo : public OptionInfo
415 //! Creates an option info object for the given option.
416 explicit IntegerOptionInfo(IntegerOptionStorage *option);
420 * Wrapper class for accessing 64-bit integer option information.
424 class Int64OptionInfo : public OptionInfo
427 //! Creates an option info object for the given option.
428 explicit Int64OptionInfo(Int64OptionStorage *option);
432 * Wrapper class for accessing floating-point option information.
436 class DoubleOptionInfo : public OptionInfo
439 //! Creates an option info object for the given option.
440 explicit DoubleOptionInfo(DoubleOptionStorage *option);
442 //! Whether the option specifies a time value.
446 * Sets a scale factor for user-provided values.
448 * Any user-provided value is scaled by the provided factor.
449 * Programmatically set default values are not scaled.
450 * If called multiple times, later calls override the previously set
451 * value. In other words, the scaling is not cumulative.
453 void setScaleFactor(double factor);
456 DoubleOptionStorage &option();
457 const DoubleOptionStorage &option() const;
461 * Wrapper class for accessing floating-point option information.
465 class FloatOptionInfo : public OptionInfo
468 //! Creates an option info object for the given option.
469 explicit FloatOptionInfo(FloatOptionStorage *option);
471 //! Whether the option specifies a time value.
474 //! \copydoc DoubleOptionInfo::setScaleFactor()
475 void setScaleFactor(double factor);
478 FloatOptionStorage &option();
479 const FloatOptionStorage &option() const;
483 * Wrapper class for accessing string option information.
487 class StringOptionInfo : public OptionInfo
490 //! Creates an option info object for the given option.
491 explicit StringOptionInfo(StringOptionStorage *option);
494 * Whether this option accepts an enumerated set of values.
496 * Returns true if StringOption::enumValues() was used when creating
499 bool isEnumerated() const;
501 * Returns the set of allowed values for this option.
503 * Returns an empty vector if isEnumerated() returns false.
505 const std::vector<std::string> &allowedValues() const;
508 StringOptionStorage &option();
509 const StringOptionStorage &option() const;
512 /*! \typedef RealOption
514 * Typedef for either DoubleOption or FloatOption, depending on precision.
516 * Generally, new would be better using DoubleOption, but this is provided for
517 * cases where the output value needs to be of type `real` for some reason.
519 /*! \typedef RealOptionInfo
521 * Typedef for either DoubleOptionInfo or FloatOptionInfo, depending on precision.
523 * Generally, new would be better using DoubleOption, but this is provided for
524 * cases where the output value needs to be of type `real` for some reason.
527 typedef DoubleOption RealOption;
528 typedef DoubleOptionInfo RealOptionInfo;
530 typedef FloatOption RealOption;
531 typedef FloatOptionInfo RealOptionInfo;