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