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