Make OptionsInfo more generic.
[alexxy/gromacs.git] / src / gromacs / options / abstractoptionstorage.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
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.
13
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.
18  *
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.
25  *
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.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \libinternal \file
32  * \brief
33  * Declares gmx::AbstractOptionStorage.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \inlibraryapi
37  * \ingroup module_options
38  */
39 #ifndef GMX_OPTIONS_ABSTRACTOPTIONSTORAGE_H
40 #define GMX_OPTIONS_ABSTRACTOPTIONSTORAGE_H
41
42 #include <string>
43
44 #include "optionflags.h"
45
46 namespace gmx
47 {
48
49 class AbstractOption;
50 class OptionInfo;
51 class Options;
52
53 /*! \libinternal \brief
54  * Abstract base class for converting, validating, and storing option values.
55  *
56  * This class should normally not be subclassed directly, but the
57  * OptionStorageTemplate should be used instead.  The templated class provides
58  * basic functionality for most of the pure virtual methods, and also
59  * integrates well with option setting objects derived from OptionTemplate.
60  *
61  * \inlibraryapi
62  * \ingroup module_options
63  *
64  * \internal
65  * This class really consists of two parts: the public interface that is
66  * used by the internal implementation of the options module, and the
67  * interface that derived classes use to provide type-dependent functionality.
68  * The latter consists of a few pure virtual methods, of which a few simple
69  * query methods are also part of the module-internal interface, others are
70  * protected and called by the non-virtual methods when needed.
71  * The reason why these two roles are in one class is twofold:
72  *  -# Both the derived classes and the internal module implementation may need
73  *     access to the same information like the allowed number of values and the
74  *     name of the option.
75  *  -# Having only one class is consistent with the structure used for options
76  *     settings objects: there is very direct correspondence between
77  *     AbstractOption and AbstractOptionStorage and between OptionTemplate and
78  *     OptionStorageTemplate.
79  */
80 class AbstractOptionStorage
81 {
82     public:
83         virtual ~AbstractOptionStorage();
84
85         //! Returns true if the option has been set.
86         bool isSet() const { return hasFlag(efSet); }
87         /*! \brief
88          * Returns true if the option is a boolean option.
89          *
90          * This is used to optionally support an alternative syntax where an
91          * option provided with no value sets the value to true and an
92          * option prefixed with "no" clears the value.
93          */
94         bool isBoolean() const;
95         //! Returns true if the option is a hidden option.
96         bool isHidden() const { return hasFlag(efHidden); }
97         //! Returns the name of the option.
98         const std::string &name() const { return _name; }
99         //! Returns the description of the option.
100         const std::string &description() const { return _descr; }
101
102         /*! \brief
103          * Returns an option info object corresponding to this option.
104          */
105         virtual OptionInfo &optionInfo() = 0;
106         /*! \brief
107          * Returns a short string describing the type of the option.
108          *
109          * The caller is free to discard the returned string.
110          */
111         virtual const char *typeString() const = 0;
112         /*! \brief
113          * Returns the number of option values added so far.
114          */
115         virtual int valueCount() const = 0;
116         /*! \brief
117          * Returns the i'th value formatted as a string.
118          */
119         virtual std::string formatValue(int i) const = 0;
120
121         /*! \brief
122          * Starts adding values from a new source for the option.
123          *
124          * This marks the vurrent value of the option as a default value,
125          * causing next call to startSet() to clear it.  This allows values
126          * from the new source to overwrite old values.
127          *
128          * This method does not throw.
129          */
130         void startSource();
131         /*! \brief
132          * Starts adding a new set of values for the option.
133          *
134          * \throws  InvalidInputError if option is specified multiple times,
135          *      but is not specified to accept it.
136          *
137          * If the parameter is specified multiple times, startSet() should be
138          * called before the values for each instance.
139          *
140          * Strong exception safety guarantee.
141          */
142         void startSet();
143         /*! \brief
144          * Adds a new value for the option, converting it from a string.
145          *
146          * \param[in] value  String value to convert.
147          * \throws  InvalidInputError if value cannot be converted, or
148          *      if there are too many values.
149          *
150          * This method should only be called between startSet() and
151          * finishSet().
152          */
153         void appendValue(const std::string &value);
154         /*! \brief
155          * Performs validation and/or actions once a set of values has been
156          * added.
157          *
158          * \throws  InvalidInputError if too few values have been provided, or
159          *      if the valid values since previous startSet() are invalid as a
160          *      set.
161          *
162          * If the parameter is specified multiple times, finishSet() should be
163          * called after the values for each instance.
164          */
165         void finishSet();
166         /*! \brief
167          * Performs validation and/or actions once all values have been added.
168          *
169          * \throws InvalidInputError if the option is required but not set, or
170          *      if all valid values together are invalid as a set.
171          *
172          * This method should be called after all values have been provided
173          * with appendValue().
174          */
175         void finish();
176
177     protected:
178         /*! \brief
179          * Initializes the storage object from the settings object.
180          *
181          * \param[in] settings  Option settings.
182          * \param[in] options   Option collection that will contain the
183          *      option.
184          * \param[in] staticFlags Option flags that are always set and specify
185          *      generic behavior of the option.
186          * \throws  APIError if invalid settings have been provided.
187          */
188         AbstractOptionStorage(const AbstractOption &settings, Options *options,
189                               OptionFlags staticFlags);
190
191         //! Returns true if the given flag is set.
192         bool hasFlag(OptionFlag flag) const { return _flags.test(flag); }
193         //! Sets the given flag.
194         void setFlag(OptionFlag flag) { return _flags.set(flag); }
195         //! Clears the given flag.
196         void clearFlag(OptionFlag flag) { return _flags.clear(flag); }
197
198         //! Returns the minimum number of values required in one set.
199         int minValueCount() const { return _minValueCount; }
200         //! Returns the maximum allowed number of values in one set (-1 = no limit).
201         int maxValueCount() const { return _maxValueCount; }
202         /*! \brief
203          * Sets a new minimum number of values required in one set.
204          *
205          * \param[in] count  New minimum number of values (must be > 0).
206          * \throws InvalidInputError if already provided values violate the limit.
207          *
208          * If values have already been provided, it is checked that there are
209          * enough.
210          *
211          * Cannot be called for options with ::efMulti set, because it is
212          * impossible to check the requirement after the values have been set.
213          * If attempted, will assert.
214          */
215         void setMinValueCount(int count);
216         /*! \brief
217          * Sets a new maximum number of values required in one set.
218          *
219          * \param[in] count  New maximum number of values
220          *                   (must be > 0, or -1 for no limit).
221          * \throws InvalidInputError if already provided values violate the limit.
222          *
223          * If values have already been provided, it is checked that there are
224          * not too many.
225          *
226          * Cannot be called for options with ::efMulti set, because it is
227          * impossible to check the requirement after the values have been set.
228          * If attempted, will assert.
229          */
230         void setMaxValueCount(int count);
231
232         //! Returns the Options object that houses the option.
233         Options &hostOptions() { return *_options; }
234         //! \copydoc hostOptions()
235         const Options &hostOptions() const { return *_options; }
236
237         /*! \brief
238          * Removes all values from temporary storage for a set.
239          *
240          * This function is always called before starting to add values to
241          * a set, allowing the storage to clear its internal buffers.
242          *
243          * Should not throw.
244          */
245         virtual void clearSet() = 0;
246         /*! \brief
247          * Adds a new value, converting it from a string.
248          *
249          * \param[in] value  String value to convert.
250          * \throws  InvalidInputError if \p value is not valid for this option
251          *      or if there have been too many values in the set.
252          *
253          * This method may be called multiple times if the underlying
254          * option is defined to accept multiple values.
255          *
256          * \see OptionStorageTemplate::convertValue()
257          */
258         virtual void convertValue(const std::string &value) = 0;
259         /*! \brief
260          * Performs validation and/or actions once a set of values has been
261          * added.
262          *
263          * \throws  InvalidInputError if the values in the set are not valid
264          *      as a whole.
265          *
266          * This method may be called multiple times if the underlying option
267          * can be specified multiple times.
268          *
269          * \see OptionStorageTemplate::processSetValues()
270          */
271         virtual void processSet() = 0;
272         /*! \brief
273          * Performs validation and/or actions once all values have been added.
274          *
275          * \throws  InvalidInputError if all provided values are not valid as
276          *      a set.
277          *
278          * This method is always called once.
279          *
280          * If the method throws, implementation should take care to leave the
281          * option in a consistent, meaningful state.  However, currently none
282          * of the implementations actually throw in any situation where the
283          * option may be left in an inconsistent state.
284          */
285         virtual void processAll() = 0;
286
287     private:
288         std::string             _name;
289         std::string             _descr;
290         //! Flags for the option.
291         OptionFlags             _flags;
292         //! Minimum number of values required (in one set).
293         int                     _minValueCount;
294         //! Maximum allowed number of values (in one set), or -1 if no limit.
295         int                     _maxValueCount;
296         //! Whether we are currently assigning values to a set.
297         bool                    _inSet;
298         //! Parent Options object.
299         Options                *_options;
300
301         // Disallow copy and assign.
302         AbstractOptionStorage(const AbstractOptionStorage &);
303         void operator =(const AbstractOptionStorage &);
304 };
305
306 } // namespace gmx
307
308 #endif