2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2014,2015 by the GROMACS development team.
5 * Copyright (c) 2016,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \libinternal \file
38 * Declares gmx::AbstractOptionStorage.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_options
44 #ifndef GMX_OPTIONS_ABSTRACTOPTIONSTORAGE_H
45 #define GMX_OPTIONS_ABSTRACTOPTIONSTORAGE_H
50 #include "gromacs/utility/classhelpers.h"
52 #include "optionflags.h"
62 /*! \libinternal \brief
63 * Abstract base class for converting, validating, and storing option values.
65 * This class should normally not be subclassed directly, but the
66 * OptionStorageTemplate should be used instead. The templated class provides
67 * basic functionality for most of the pure virtual methods, and also
68 * integrates well with option setting objects derived from OptionTemplate.
71 * \ingroup module_options
74 * This class really consists of two parts: the public interface that is
75 * used by the internal implementation of the options module, and the
76 * interface that derived classes use to provide type-dependent functionality.
77 * The latter consists of a few pure virtual methods, of which a few simple
78 * query methods are also part of the module-internal interface, others are
79 * protected and called by the non-virtual methods when needed.
80 * The reason why these two roles are in one class is twofold:
81 * -# Both the derived classes and the internal module implementation may need
82 * access to the same information like the allowed number of values and the
84 * -# Having only one class is consistent with the structure used for options
85 * settings objects: there is very direct correspondence between
86 * AbstractOption and AbstractOptionStorage and between OptionTemplate and
87 * OptionStorageTemplate.
89 class AbstractOptionStorage
92 virtual ~AbstractOptionStorage();
94 //! Returns true if the option has been set.
95 bool isSet() const { return hasFlag(efOption_Set
); }
97 * Returns true if the option is a boolean option.
99 * This is used to optionally support an alternative syntax where an
100 * option provided with no value sets the value to true and an
101 * option prefixed with "no" clears the value.
103 bool isBoolean() const;
104 //! Returns true if the option is a hidden option.
105 bool isHidden() const { return hasFlag(efOption_Hidden
); }
106 //! Returns true if the option is required.
107 bool isRequired() const { return hasFlag(efOption_Required
); }
108 //! Returns true if the option is vector-valued.
109 bool isVector() const { return hasFlag(efOption_Vector
); }
110 //! Returns the name of the option.
111 const std::string
& name() const { return name_
; }
112 //! Returns the description of the option set by the calling code.
113 const std::string
& description() const { return descr_
; }
115 //! Returns true if defaultValueIfSet() value is specified.
116 bool defaultValueIfSetExists() const { return hasFlag(efOption_DefaultValueIfSetExists
); }
117 //! Returns the minimum number of values required in one set.
118 int minValueCount() const { return minValueCount_
; }
119 //! Returns the maximum allowed number of values in one set (-1 = no limit).
120 int maxValueCount() const { return maxValueCount_
; }
123 * Returns an option info object corresponding to this option.
125 virtual OptionInfo
& optionInfo() = 0;
127 * Returns a short string describing the type of the option.
129 virtual std::string
typeString() const = 0;
131 * Formats additional description for the option.
133 * If this method returns a non-empty string, it is appended to the
134 * plain description when printing help texts.
135 * The default implementation returns an empty string.
137 virtual std::string
formatExtraDescription() const { return std::string(); }
139 * Returns the number of option values added so far.
141 virtual int valueCount() const = 0;
142 //! \copydoc OptionInfo::defaultValues()
143 virtual std::vector
<Any
> defaultValues() const = 0;
144 //! \copydoc OptionInfo::defaultValuesAsStrings()
145 virtual std::vector
<std::string
> defaultValuesAsStrings() const = 0;
146 //! \copydoc OptionInfo::normalizeValues()
147 virtual std::vector
<Any
> normalizeValues(const std::vector
<Any
>& values
) const = 0;
150 * Starts adding values from a new source for the option.
152 * This marks the vurrent value of the option as a default value,
153 * causing next call to startSet() to clear it. This allows values
154 * from the new source to overwrite old values.
156 * This method does not throw.
160 * Starts adding a new set of values for the option.
162 * \throws InvalidInputError if option is specified multiple times,
163 * but is not specified to accept it.
165 * If the parameter is specified multiple times, startSet() should be
166 * called before the values for each instance.
168 * Strong exception safety guarantee.
172 * Adds a new value for the option.
174 * \param[in] value Value to convert.
175 * \throws InvalidInputError if value cannot be converted, or
176 * if there are too many values.
178 * This method should only be called between startSet() and
181 void appendValue(const Any
& value
);
183 * Performs validation and/or actions once a set of values has been
186 * \throws InvalidInputError if too few values have been provided, or
187 * if the valid values since previous startSet() are invalid as a
190 * If the parameter is specified multiple times, finishSet() should be
191 * called after the values for each instance.
195 * Performs validation and/or actions once all values have been added.
197 * \throws InvalidInputError if the option is required but not set, or
198 * if all valid values together are invalid as a set.
200 * This method should be called after all values have been provided
201 * with appendValue().
207 * Initializes the storage object from the settings object.
209 * \param[in] settings Option settings.
210 * \param[in] staticFlags Option flags that are always set and specify
211 * generic behavior of the option.
212 * \throws APIError if invalid settings have been provided.
214 AbstractOptionStorage(const AbstractOption
& settings
, OptionFlags staticFlags
);
216 //! Marks the option as set.
218 //! Returns true if the given flag is set.
219 bool hasFlag(OptionFlag flag
) const { return flags_
.test(flag
); }
220 //! Sets the given flag.
221 void setFlag(OptionFlag flag
) { return flags_
.set(flag
); }
222 //! Clears the given flag.
223 void clearFlag(OptionFlag flag
) { return flags_
.clear(flag
); }
226 * Sets a new minimum number of values required in one set.
228 * \param[in] count New minimum number of values (must be > 0).
229 * \throws InvalidInputError if already provided values violate the limit.
231 * If values have already been provided, it is checked that there are
234 * Cannot be called for options with ::efOption_MultipleTimes set,
235 * because it is impossible to check the requirement after the values
237 * If attempted, will assert.
239 void setMinValueCount(int count
);
241 * Sets a new maximum number of values required in one set.
243 * \param[in] count New maximum number of values
244 * (must be > 0, or -1 for no limit).
245 * \throws InvalidInputError if already provided values violate the limit.
247 * If values have already been provided, it is checked that there are
250 * Cannot be called for options with ::efOption_MultipleTimes set,
251 * because it is impossible to check the requirement after the values
253 * If attempted, will assert.
255 void setMaxValueCount(int count
);
258 * Removes all values from temporary storage for a set.
260 * This function is always called before starting to add values to
261 * a set, allowing the storage to clear its internal buffers.
265 virtual void clearSet() = 0;
269 * \param[in] value Value to convert.
270 * \throws InvalidInputError if \p value is not valid for this option
271 * or if there have been too many values in the set.
273 * This method may be called multiple times if the underlying
274 * option is defined to accept multiple values.
276 * \see OptionStorageTemplate::convertValue()
278 virtual void convertValue(const Any
& value
) = 0;
280 * Performs validation and/or actions once a set of values has been
283 * \throws InvalidInputError if the values in the set are not valid
286 * This method may be called multiple times if the underlying option
287 * can be specified multiple times.
288 * This method is not currently called if one of the convertValue()
292 * Improve the call semantics.
294 * \see OptionStorageTemplate::processSetValues()
296 virtual void processSet() = 0;
298 * Performs validation and/or actions once all values have been added.
300 * \throws InvalidInputError if all provided values are not valid as
303 * This method is always called once.
305 * If the method throws, implementation should take care to leave the
306 * option in a consistent, meaningful state. However, currently none
307 * of the implementations actually throw in any situation where the
308 * option may be left in an inconsistent state.
310 virtual void processAll() = 0;
315 //! Flags for the option.
318 //! Minimum number of values required (in one set).
320 //! Maximum allowed number of values (in one set), or -1 if no limit.
322 //! Whether we are currently assigning values to a set.
324 //! Whether there were errors in set values.
325 bool bSetValuesHadErrors_
;
327 GMX_DISALLOW_COPY_AND_ASSIGN(AbstractOptionStorage
);