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