Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / options / abstractoptionstorage.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2014,2015,2016,2019, 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.
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 /*! \libinternal \file
36  * \brief
37  * Declares gmx::AbstractOptionStorage.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_options
42  */
43 #ifndef GMX_OPTIONS_ABSTRACTOPTIONSTORAGE_H
44 #define GMX_OPTIONS_ABSTRACTOPTIONSTORAGE_H
45
46 #include <string>
47 #include <vector>
48
49 #include "gromacs/utility/classhelpers.h"
50
51 #include "optionflags.h"
52
53 namespace gmx
54 {
55
56 class AbstractOption;
57 class OptionInfo;
58 class Options;
59 class Any;
60
61 /*! \libinternal \brief
62  * Abstract base class for converting, validating, and storing option values.
63  *
64  * This class should normally not be subclassed directly, but the
65  * OptionStorageTemplate should be used instead.  The templated class provides
66  * basic functionality for most of the pure virtual methods, and also
67  * integrates well with option setting objects derived from OptionTemplate.
68  *
69  * \inlibraryapi
70  * \ingroup module_options
71  *
72  * \internal
73  * This class really consists of two parts: the public interface that is
74  * used by the internal implementation of the options module, and the
75  * interface that derived classes use to provide type-dependent functionality.
76  * The latter consists of a few pure virtual methods, of which a few simple
77  * query methods are also part of the module-internal interface, others are
78  * protected and called by the non-virtual methods when needed.
79  * The reason why these two roles are in one class is twofold:
80  *  -# Both the derived classes and the internal module implementation may need
81  *     access to the same information like the allowed number of values and the
82  *     name of the option.
83  *  -# Having only one class is consistent with the structure used for options
84  *     settings objects: there is very direct correspondence between
85  *     AbstractOption and AbstractOptionStorage and between OptionTemplate and
86  *     OptionStorageTemplate.
87  */
88 class AbstractOptionStorage
89 {
90 public:
91     virtual ~AbstractOptionStorage();
92
93     //! Returns true if the option has been set.
94     bool isSet() const { return hasFlag(efOption_Set); }
95     /*! \brief
96      * Returns true if the option is a boolean option.
97      *
98      * This is used to optionally support an alternative syntax where an
99      * option provided with no value sets the value to true and an
100      * option prefixed with "no" clears the value.
101      */
102     bool isBoolean() const;
103     //! Returns true if the option is a hidden option.
104     bool isHidden() const { return hasFlag(efOption_Hidden); }
105     //! Returns true if the option is required.
106     bool isRequired() const { return hasFlag(efOption_Required); }
107     //! Returns true if the option is vector-valued.
108     bool isVector() const { return hasFlag(efOption_Vector); }
109     //! Returns the name of the option.
110     const std::string& name() const { return name_; }
111     //! Returns the description of the option set by the calling code.
112     const std::string& description() const { return descr_; }
113
114     //! Returns true if defaultValueIfSet() value is specified.
115     bool defaultValueIfSetExists() const { return hasFlag(efOption_DefaultValueIfSetExists); }
116     //! Returns the minimum number of values required in one set.
117     int minValueCount() const { return minValueCount_; }
118     //! Returns the maximum allowed number of values in one set (-1 = no limit).
119     int maxValueCount() const { return maxValueCount_; }
120
121     /*! \brief
122      * Returns an option info object corresponding to this option.
123      */
124     virtual OptionInfo& optionInfo() = 0;
125     /*! \brief
126      * Returns a short string describing the type of the option.
127      */
128     virtual std::string typeString() const = 0;
129     /*! \brief
130      * Formats additional description for the option.
131      *
132      * If this method returns a non-empty string, it is appended to the
133      * plain description when printing help texts.
134      * The default implementation returns an empty string.
135      */
136     virtual std::string formatExtraDescription() const { return std::string(); }
137     /*! \brief
138      * Returns the number of option values added so far.
139      */
140     virtual int valueCount() const = 0;
141     //! \copydoc OptionInfo::defaultValues()
142     virtual std::vector<Any> defaultValues() const = 0;
143     //! \copydoc OptionInfo::defaultValuesAsStrings()
144     virtual std::vector<std::string> defaultValuesAsStrings() const = 0;
145     //! \copydoc OptionInfo::normalizeValues()
146     virtual std::vector<Any> normalizeValues(const std::vector<Any>& values) const = 0;
147
148     /*! \brief
149      * Starts adding values from a new source for the option.
150      *
151      * This marks the vurrent value of the option as a default value,
152      * causing next call to startSet() to clear it.  This allows values
153      * from the new source to overwrite old values.
154      *
155      * This method does not throw.
156      */
157     void startSource();
158     /*! \brief
159      * Starts adding a new set of values for the option.
160      *
161      * \throws  InvalidInputError if option is specified multiple times,
162      *      but is not specified to accept it.
163      *
164      * If the parameter is specified multiple times, startSet() should be
165      * called before the values for each instance.
166      *
167      * Strong exception safety guarantee.
168      */
169     void startSet();
170     /*! \brief
171      * Adds a new value for the option.
172      *
173      * \param[in] value  Value to convert.
174      * \throws  InvalidInputError if value cannot be converted, or
175      *      if there are too many values.
176      *
177      * This method should only be called between startSet() and
178      * finishSet().
179      */
180     void appendValue(const Any& value);
181     /*! \brief
182      * Performs validation and/or actions once a set of values has been
183      * added.
184      *
185      * \throws  InvalidInputError if too few values have been provided, or
186      *      if the valid values since previous startSet() are invalid as a
187      *      set.
188      *
189      * If the parameter is specified multiple times, finishSet() should be
190      * called after the values for each instance.
191      */
192     void finishSet();
193     /*! \brief
194      * Performs validation and/or actions once all values have been added.
195      *
196      * \throws InvalidInputError if the option is required but not set, or
197      *      if all valid values together are invalid as a set.
198      *
199      * This method should be called after all values have been provided
200      * with appendValue().
201      */
202     void finish();
203
204 protected:
205     /*! \brief
206      * Initializes the storage object from the settings object.
207      *
208      * \param[in] settings  Option settings.
209      * \param[in] staticFlags Option flags that are always set and specify
210      *      generic behavior of the option.
211      * \throws  APIError if invalid settings have been provided.
212      */
213     AbstractOptionStorage(const AbstractOption& settings, OptionFlags staticFlags);
214
215     //! Marks the option as set.
216     void markAsSet();
217     //! Returns true if the given flag is set.
218     bool hasFlag(OptionFlag flag) const { return flags_.test(flag); }
219     //! Sets the given flag.
220     void setFlag(OptionFlag flag) { return flags_.set(flag); }
221     //! Clears the given flag.
222     void clearFlag(OptionFlag flag) { return flags_.clear(flag); }
223
224     /*! \brief
225      * Sets a new minimum number of values required in one set.
226      *
227      * \param[in] count  New minimum number of values (must be > 0).
228      * \throws InvalidInputError if already provided values violate the limit.
229      *
230      * If values have already been provided, it is checked that there are
231      * enough.
232      *
233      * Cannot be called for options with ::efOption_MultipleTimes set,
234      * because it is impossible to check the requirement after the values
235      * have been set.
236      * If attempted, will assert.
237      */
238     void setMinValueCount(int count);
239     /*! \brief
240      * Sets a new maximum number of values required in one set.
241      *
242      * \param[in] count  New maximum number of values
243      *                   (must be > 0, or -1 for no limit).
244      * \throws InvalidInputError if already provided values violate the limit.
245      *
246      * If values have already been provided, it is checked that there are
247      * not too many.
248      *
249      * Cannot be called for options with ::efOption_MultipleTimes set,
250      * because it is impossible to check the requirement after the values
251      * have been set.
252      * If attempted, will assert.
253      */
254     void setMaxValueCount(int count);
255
256     /*! \brief
257      * Removes all values from temporary storage for a set.
258      *
259      * This function is always called before starting to add values to
260      * a set, allowing the storage to clear its internal buffers.
261      *
262      * Should not throw.
263      */
264     virtual void clearSet() = 0;
265     /*! \brief
266      * Adds a new value.
267      *
268      * \param[in] value  Value to convert.
269      * \throws  InvalidInputError if \p value is not valid for this option
270      *      or if there have been too many values in the set.
271      *
272      * This method may be called multiple times if the underlying
273      * option is defined to accept multiple values.
274      *
275      * \see OptionStorageTemplate::convertValue()
276      */
277     virtual void convertValue(const Any& value) = 0;
278     /*! \brief
279      * Performs validation and/or actions once a set of values has been
280      * added.
281      *
282      * \throws  InvalidInputError if the values in the set are not valid
283      *      as a whole.
284      *
285      * This method may be called multiple times if the underlying option
286      * can be specified multiple times.
287      * This method is not currently called if one of the convertValue()
288      * calls throwed.
289      *
290      * \todo
291      * Improve the call semantics.
292      *
293      * \see OptionStorageTemplate::processSetValues()
294      */
295     virtual void processSet() = 0;
296     /*! \brief
297      * Performs validation and/or actions once all values have been added.
298      *
299      * \throws  InvalidInputError if all provided values are not valid as
300      *      a set.
301      *
302      * This method is always called once.
303      *
304      * If the method throws, implementation should take care to leave the
305      * option in a consistent, meaningful state.  However, currently none
306      * of the implementations actually throw in any situation where the
307      * option may be left in an inconsistent state.
308      */
309     virtual void processAll() = 0;
310
311 private:
312     std::string name_;
313     std::string descr_;
314     //! Flags for the option.
315     OptionFlags flags_;
316     bool*       storeIsSet_;
317     //! Minimum number of values required (in one set).
318     int minValueCount_;
319     //! Maximum allowed number of values (in one set), or -1 if no limit.
320     int maxValueCount_;
321     //! Whether we are currently assigning values to a set.
322     bool bInSet_;
323     //! Whether there were errors in set values.
324     bool bSetValuesHadErrors_;
325
326     GMX_DISALLOW_COPY_AND_ASSIGN(AbstractOptionStorage);
327 };
328
329 } // namespace gmx
330
331 #endif