Rename gmx Variant to Any
[alexxy/gromacs.git] / src / gromacs / options / basicoptionstorage.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017,2018,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 /*! \internal \file
36  * \brief
37  * Declares storage classes for basic option types.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_options
41  */
42 #ifndef GMX_OPTIONS_BASICOPTIONSTORAGE_H
43 #define GMX_OPTIONS_BASICOPTIONSTORAGE_H
44
45 #include <memory>
46 #include <string>
47 #include <vector>
48
49 #include "gromacs/options/basicoptions.h"
50 #include "gromacs/options/optionstoragetemplate.h"
51
52 namespace gmx
53 {
54
55 /*! \addtogroup module_options
56  * \{
57  */
58
59 /*! \internal \brief
60  * Converts, validates, and stores boolean values.
61  */
62 class BooleanOptionStorage : public OptionStorageTemplateSimple<bool>
63 {
64     public:
65         /*! \brief
66          * Initializes the storage from option settings.
67          *
68          * \param[in] settings   Storage settings.
69          */
70         explicit BooleanOptionStorage(const BooleanOption &settings)
71             : MyBase(settings), info_(this)
72         {
73         }
74
75         OptionInfo &optionInfo() override { return info_; }
76         std::string typeString() const override { return "bool"; }
77         std::string formatSingleValue(const bool &value) const override;
78
79         //! \copydoc BooleanOptionInfo::defaultValue()
80         bool defaultValue() const { return valueCount() > 0 && values()[0]; }
81
82     private:
83         void initConverter(ConverterType *converter) override;
84
85         BooleanOptionInfo       info_;
86 };
87
88 /*! \internal \brief
89  * Converts, validates, and stores integer values.
90  */
91 class IntegerOptionStorage : public OptionStorageTemplateSimple<int>
92 {
93     public:
94         //! \copydoc BooleanOptionStorage::BooleanOptionStorage()
95         explicit IntegerOptionStorage(const IntegerOption &settings)
96             : MyBase(settings), info_(this)
97         {
98         }
99
100         OptionInfo &optionInfo() override { return info_; }
101         std::string typeString() const override
102         { return isVector() ? "vector" : "int"; }
103         std::string formatSingleValue(const int &value) const override;
104
105     private:
106         void initConverter(ConverterType *converter) override;
107         void processSetValues(ValueList *values) override;
108
109         IntegerOptionInfo       info_;
110 };
111
112 /*! \internal \brief
113  * Converts, validates, and stores integer values.
114  */
115 class Int64OptionStorage : public OptionStorageTemplateSimple<int64_t>
116 {
117     public:
118         //! \copydoc BooleanOptionStorage::BooleanOptionStorage()
119         explicit Int64OptionStorage(const Int64Option &settings)
120             : MyBase(settings), info_(this)
121         {
122         }
123
124         OptionInfo &optionInfo() override { return info_; }
125         std::string typeString() const override { return "int"; }
126         std::string formatSingleValue(const int64_t &value) const override;
127
128     private:
129         void initConverter(ConverterType *converter) override;
130
131         Int64OptionInfo       info_;
132 };
133
134 /*! \internal \brief
135  * Converts, validates, and stores floating-point (double) values.
136  */
137 class DoubleOptionStorage : public OptionStorageTemplateSimple<double>
138 {
139     public:
140         //! \copydoc IntegerOptionStorage::IntegerOptionStorage()
141         explicit DoubleOptionStorage(const DoubleOption &settings);
142
143         OptionInfo &optionInfo() override { return info_; }
144         std::string typeString() const override;
145         std::string formatSingleValue(const double &value) const override;
146
147         //! \copydoc DoubleOptionInfo::isTime()
148         bool isTime() const { return bTime_; }
149         //! \copydoc DoubleOptionInfo::setScaleFactor()
150         void setScaleFactor(double factor);
151
152     private:
153         void initConverter(ConverterType *converter) override;
154         double processValue(const double &value) const override;
155         void processSetValues(ValueList *values) override;
156
157         DoubleOptionInfo        info_;
158         bool                    bTime_;
159         double                  factor_;
160 };
161
162 /*! \internal \brief
163  * Converts, validates, and stores floating-point (float) values.
164  */
165 class FloatOptionStorage : public OptionStorageTemplateSimple<float>
166 {
167     public:
168         //! \copydoc IntegerOptionStorage::IntegerOptionStorage()
169         explicit FloatOptionStorage(const FloatOption &settings);
170
171         OptionInfo &optionInfo() override { return info_; }
172         std::string typeString() const override;
173         std::string formatSingleValue(const float &value) const override;
174
175         //! \copydoc DoubleOptionStorage::isTime()
176         bool isTime() const { return bTime_; }
177         //! \copydoc DoubleOptionStorage::setScaleFactor()
178         void setScaleFactor(double factor);
179
180     private:
181         void initConverter(ConverterType *converter) override;
182         float processValue(const float &value) const override;
183         void processSetValues(ValueList *values) override;
184
185         FloatOptionInfo         info_;
186         bool                    bTime_;
187         double                  factor_;
188 };
189
190 /*! \internal \brief
191  * Converts, validates, and stores string values.
192  */
193 class StringOptionStorage : public OptionStorageTemplateSimple<std::string>
194 {
195     public:
196         //! \copydoc DoubleOptionStorage::DoubleOptionStorage()
197         explicit StringOptionStorage(const StringOption &settings);
198
199         OptionInfo &optionInfo() override { return info_; }
200         std::string typeString() const override
201         { return allowed_.empty() ? "string" : "enum"; }
202         std::string formatExtraDescription() const override;
203         std::string formatSingleValue(const std::string &value) const override;
204
205         //! \copydoc StringOptionInfo::allowedValues()
206         const ValueList &allowedValues() const { return allowed_; }
207
208     private:
209         void initConverter(ConverterType *converter) override;
210         std::string processValue(const std::string &value) const override;
211
212         StringOptionInfo        info_;
213         ValueList               allowed_;
214 };
215
216 /*! \internal \brief
217  * Converts, validates, and stores enum values.
218  */
219 class EnumOptionStorage : public OptionStorageTemplateSimple<int>
220 {
221     public:
222         /*! \brief
223          * Initializes the storage from option settings.
224          *
225          * \param[in] settings      Basic storage settings.
226          * \param[in] enumValues    Allowed values.
227          * \param[in] count         Number of elements in \p enumValues,
228          *     or -1 if \p enumValues is `NULL`-terminated.
229          * \param[in] defaultValue  Default value, or -1 if no default.
230          * \param[in] defaultValueIfSet  Default value if set, or -1 if none.
231          * \param[in] store         Storage to convert the values to/from `int`.
232          *
233          * This constructor takes more parameters than other storage parameters
234          * because the front-end option type is a template, and as such cannot
235          * be passed here without exposing also this header as an installed
236          * header.
237          */
238         EnumOptionStorage(const AbstractOption &settings,
239                           const char *const *enumValues, int count,
240                           int defaultValue, int defaultValueIfSet,
241                           StorePointer store);
242
243         OptionInfo &optionInfo() override { return info_; }
244         std::string typeString() const override { return "enum"; }
245         std::string formatExtraDescription() const override;
246         std::string formatSingleValue(const int &value) const override;
247         Any normalizeValue(const int &value) const override;
248
249         //! \copydoc EnumOptionInfo::allowedValues()
250         const std::vector<std::string> &allowedValues() const { return allowed_; }
251
252     private:
253         void initConverter(ConverterType *converter) override;
254
255         EnumOptionInfo            info_;
256         std::vector<std::string>  allowed_;
257 };
258
259 /*!\}*/
260
261 } // namespace gmx
262
263 #endif