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