3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
31 /*! \libinternal \file
33 * Declares gmx::AbstractOptionStorage.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
37 * \ingroup module_options
39 #ifndef GMX_OPTIONS_ABSTRACTOPTIONSTORAGE_H
40 #define GMX_OPTIONS_ABSTRACTOPTIONSTORAGE_H
44 #include "../utility/common.h"
46 #include "optionflags.h"
55 /*! \libinternal \brief
56 * Abstract base class for converting, validating, and storing option values.
58 * This class should normally not be subclassed directly, but the
59 * OptionStorageTemplate should be used instead. The templated class provides
60 * basic functionality for most of the pure virtual methods, and also
61 * integrates well with option setting objects derived from OptionTemplate.
64 * \ingroup module_options
67 * This class really consists of two parts: the public interface that is
68 * used by the internal implementation of the options module, and the
69 * interface that derived classes use to provide type-dependent functionality.
70 * The latter consists of a few pure virtual methods, of which a few simple
71 * query methods are also part of the module-internal interface, others are
72 * protected and called by the non-virtual methods when needed.
73 * The reason why these two roles are in one class is twofold:
74 * -# Both the derived classes and the internal module implementation may need
75 * access to the same information like the allowed number of values and the
77 * -# Having only one class is consistent with the structure used for options
78 * settings objects: there is very direct correspondence between
79 * AbstractOption and AbstractOptionStorage and between OptionTemplate and
80 * OptionStorageTemplate.
82 class AbstractOptionStorage
85 virtual ~AbstractOptionStorage();
87 //! Returns true if the option has been set.
88 bool isSet() const { return hasFlag(efSet); }
90 * Returns true if the option is a boolean option.
92 * This is used to optionally support an alternative syntax where an
93 * option provided with no value sets the value to true and an
94 * option prefixed with "no" clears the value.
96 bool isBoolean() const;
97 //! Returns true if the option is a hidden option.
98 bool isHidden() const { return hasFlag(efHidden); }
99 //! Returns true if the option is required.
100 bool isRequired() const { return hasFlag(efRequired); }
101 //! Returns the name of the option.
102 const std::string &name() const { return _name; }
103 //! Returns the description of the option.
104 const std::string &description() const { return _descr; }
107 * Returns an option info object corresponding to this option.
109 virtual OptionInfo &optionInfo() = 0;
111 * Returns a short string describing the type of the option.
113 * The caller is free to discard the returned string.
115 virtual const char *typeString() const = 0;
117 * Returns the number of option values added so far.
119 virtual int valueCount() const = 0;
121 * Returns the i'th value formatted as a string.
123 * If \p i is DefaultValueIfSetIndex, should format the default value
124 * if set (see OptionTemplate::defaultValueIfSet()).
126 virtual std::string formatValue(int i) const = 0;
127 //! \copydoc OptionInfo::formatDefaultValueIfSet()
128 std::string formatDefaultValueIfSet() const
129 { return formatValue(DefaultValueIfSetIndex); }
132 * Starts adding values from a new source for the option.
134 * This marks the vurrent value of the option as a default value,
135 * causing next call to startSet() to clear it. This allows values
136 * from the new source to overwrite old values.
138 * This method does not throw.
142 * Starts adding a new set of values for the option.
144 * \throws InvalidInputError if option is specified multiple times,
145 * but is not specified to accept it.
147 * If the parameter is specified multiple times, startSet() should be
148 * called before the values for each instance.
150 * Strong exception safety guarantee.
154 * Adds a new value for the option, converting it from a string.
156 * \param[in] value String value to convert.
157 * \throws InvalidInputError if value cannot be converted, or
158 * if there are too many values.
160 * This method should only be called between startSet() and
163 void appendValue(const std::string &value);
165 * Performs validation and/or actions once a set of values has been
168 * \throws InvalidInputError if too few values have been provided, or
169 * if the valid values since previous startSet() are invalid as a
172 * If the parameter is specified multiple times, finishSet() should be
173 * called after the values for each instance.
177 * Performs validation and/or actions once all values have been added.
179 * \throws InvalidInputError if the option is required but not set, or
180 * if all valid values together are invalid as a set.
182 * This method should be called after all values have been provided
183 * with appendValue().
188 //! Index used with formatValue() for formatting default value if set.
189 static const int DefaultValueIfSetIndex = -1;
192 * Initializes the storage object from the settings object.
194 * \param[in] settings Option settings.
195 * \param[in] staticFlags Option flags that are always set and specify
196 * generic behavior of the option.
197 * \throws APIError if invalid settings have been provided.
199 AbstractOptionStorage(const AbstractOption &settings,
200 OptionFlags staticFlags);
202 //! Returns true if the given flag is set.
203 bool hasFlag(OptionFlag flag) const { return _flags.test(flag); }
204 //! Sets the given flag.
205 void setFlag(OptionFlag flag) { return _flags.set(flag); }
206 //! Clears the given flag.
207 void clearFlag(OptionFlag flag) { return _flags.clear(flag); }
209 //! Returns the minimum number of values required in one set.
210 int minValueCount() const { return _minValueCount; }
211 //! Returns the maximum allowed number of values in one set (-1 = no limit).
212 int maxValueCount() const { return _maxValueCount; }
214 * Sets a new minimum number of values required in one set.
216 * \param[in] count New minimum number of values (must be > 0).
217 * \throws InvalidInputError if already provided values violate the limit.
219 * If values have already been provided, it is checked that there are
222 * Cannot be called for options with ::efMulti set, because it is
223 * impossible to check the requirement after the values have been set.
224 * If attempted, will assert.
226 void setMinValueCount(int count);
228 * Sets a new maximum number of values required in one set.
230 * \param[in] count New maximum number of values
231 * (must be > 0, or -1 for no limit).
232 * \throws InvalidInputError if already provided values violate the limit.
234 * If values have already been provided, it is checked that there are
237 * Cannot be called for options with ::efMulti set, because it is
238 * impossible to check the requirement after the values have been set.
239 * If attempted, will assert.
241 void setMaxValueCount(int count);
244 * Removes all values from temporary storage for a set.
246 * This function is always called before starting to add values to
247 * a set, allowing the storage to clear its internal buffers.
251 virtual void clearSet() = 0;
253 * Adds a new value, converting it from a string.
255 * \param[in] value String value to convert.
256 * \throws InvalidInputError if \p value is not valid for this option
257 * or if there have been too many values in the set.
259 * This method may be called multiple times if the underlying
260 * option is defined to accept multiple values.
262 * \see OptionStorageTemplate::convertValue()
264 virtual void convertValue(const std::string &value) = 0;
266 * Performs validation and/or actions once a set of values has been
269 * \throws InvalidInputError if the values in the set are not valid
272 * This method may be called multiple times if the underlying option
273 * can be specified multiple times.
275 * \see OptionStorageTemplate::processSetValues()
277 virtual void processSet() = 0;
279 * Performs validation and/or actions once all values have been added.
281 * \throws InvalidInputError if all provided values are not valid as
284 * This method is always called once.
286 * If the method throws, implementation should take care to leave the
287 * option in a consistent, meaningful state. However, currently none
288 * of the implementations actually throw in any situation where the
289 * option may be left in an inconsistent state.
291 virtual void processAll() = 0;
296 //! Flags for the option.
298 //! Minimum number of values required (in one set).
300 //! Maximum allowed number of values (in one set), or -1 if no limit.
302 //! Whether we are currently assigning values to a set.
305 GMX_DISALLOW_COPY_AND_ASSIGN(AbstractOptionStorage);