2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, 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.
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.
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.
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.
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.
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.
37 * Implements classes in basicoptions.h and basicoptionstorage.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
42 #include "basicoptions.h"
43 #include "basicoptionstorage.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/stringutil.h"
60 * Expands a single value to a vector by copying the value.
62 * \tparam ValueType Type of values to process.
63 * \param[in] length Length of the resulting vector.
64 * \param[in,out] values Values to process.
65 * \throws std::bad_alloc if out of memory.
66 * \throws InvalidInputError if \p values has an invalid number of values.
68 * \p values should have 0, 1, or \p length values.
69 * If \p values has 1 value, it is expanded such that it has \p length
70 * identical values. In other valid cases, nothing is done.
72 template <typename ValueType>
73 void expandVector(size_t length, std::vector<ValueType> *values)
75 if (length > 0 && !values->empty() && values->size() != length)
77 if (values->size() != 1)
79 GMX_THROW(gmx::InvalidInputError(gmx::formatString(
80 "Expected 1 or %d values, got %d", length, values->size())));
82 const ValueType &value = (*values)[0];
83 values->resize(length, value);
92 /********************************************************************
93 * BooleanOptionStorage
96 std::string BooleanOptionStorage::formatSingleValue(const bool &value) const
98 return value ? "yes" : "no";
101 void BooleanOptionStorage::convertValue(const std::string &value)
103 // TODO: Case-independence
104 if (value == "1" || value == "yes" || value == "true")
109 else if (value == "0" || value == "no" || value == "false")
114 GMX_THROW(InvalidInputError("Invalid value: '" + value + "'; supported values are: 1, 0, yes, no, true, false"));
117 /********************************************************************
121 BooleanOptionInfo::BooleanOptionInfo(BooleanOptionStorage *option)
126 /********************************************************************
130 AbstractOptionStoragePointer BooleanOption::createStorage() const
132 return AbstractOptionStoragePointer(new BooleanOptionStorage(*this));
136 /********************************************************************
137 * IntegerOptionStorage
140 std::string IntegerOptionStorage::formatSingleValue(const int &value) const
142 return formatString("%d", value);
145 void IntegerOptionStorage::convertValue(const std::string &value)
147 const char *ptr = value.c_str();
150 long int ival = std::strtol(ptr, &endptr, 10);
152 || ival < std::numeric_limits<int>::min()
153 || ival > std::numeric_limits<int>::max())
155 GMX_THROW(InvalidInputError("Invalid value: '" + value
156 + "'; it causes an integer overflow"));
158 if (*ptr == '\0' || *endptr != '\0')
160 GMX_THROW(InvalidInputError("Invalid value: '" + value
161 + "'; expected an integer"));
166 void IntegerOptionStorage::processSetValues(ValueList *values)
170 expandVector(maxValueCount(), values);
174 /********************************************************************
178 IntegerOptionInfo::IntegerOptionInfo(IntegerOptionStorage *option)
183 /********************************************************************
187 AbstractOptionStoragePointer IntegerOption::createStorage() const
189 return AbstractOptionStoragePointer(new IntegerOptionStorage(*this));
193 /********************************************************************
194 * DoubleOptionStorage
197 DoubleOptionStorage::DoubleOptionStorage(const DoubleOption &settings)
198 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
202 const char *DoubleOptionStorage::typeString() const
204 return isVector() ? "vector" : (isTime() ? "time" : "double");
207 std::string DoubleOptionStorage::formatSingleValue(const double &value) const
209 return formatString("%g", value / factor_);
212 void DoubleOptionStorage::convertValue(const std::string &value)
214 const char *ptr = value.c_str();
217 double dval = std::strtod(ptr, &endptr);
220 GMX_THROW(InvalidInputError("Invalid value: '" + value
221 + "'; it causes an overflow/underflow"));
223 if (*ptr == '\0' || *endptr != '\0')
225 GMX_THROW(InvalidInputError("Invalid value: '" + value
226 + "'; expected a number"));
228 addValue(dval * factor_);
231 void DoubleOptionStorage::processSetValues(ValueList *values)
235 expandVector(maxValueCount(), values);
239 void DoubleOptionStorage::processAll()
243 void DoubleOptionStorage::setScaleFactor(double factor)
245 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
246 if (!hasFlag(efOption_HasDefaultValue))
248 double scale = factor / factor_;
249 ValueList::iterator i;
250 for (i = values().begin(); i != values().end(); ++i)
259 /********************************************************************
263 DoubleOptionInfo::DoubleOptionInfo(DoubleOptionStorage *option)
268 DoubleOptionStorage &DoubleOptionInfo::option()
270 return static_cast<DoubleOptionStorage &>(OptionInfo::option());
273 const DoubleOptionStorage &DoubleOptionInfo::option() const
275 return static_cast<const DoubleOptionStorage &>(OptionInfo::option());
278 bool DoubleOptionInfo::isTime() const
280 return option().isTime();
283 void DoubleOptionInfo::setScaleFactor(double factor)
285 option().setScaleFactor(factor);
288 /********************************************************************
292 AbstractOptionStoragePointer DoubleOption::createStorage() const
294 return AbstractOptionStoragePointer(new DoubleOptionStorage(*this));
298 /********************************************************************
299 * StringOptionStorage
302 StringOptionStorage::StringOptionStorage(const StringOption &settings)
303 : MyBase(settings), info_(this), enumIndexStore_(NULL)
305 if (settings.defaultEnumIndex_ >= 0 && settings.enumValues_ == NULL)
307 GMX_THROW(APIError("Cannot set default enum index without enum values"));
309 if (settings.enumIndexStore_ != NULL && settings.enumValues_ == NULL)
311 GMX_THROW(APIError("Cannot set enum index store without enum values"));
313 if (settings.enumIndexStore_ != NULL && settings.maxValueCount_ < 0)
315 GMX_THROW(APIError("Cannot set enum index store with arbitrary number of values"));
317 if (settings.enumValues_ != NULL)
319 enumIndexStore_ = settings.enumIndexStore_;
320 const std::string *defaultValue = settings.defaultValue();
322 int count = settings.enumValuesCount_;
326 while (settings.enumValues_[count] != NULL)
331 for (int i = 0; i < count; ++i)
333 if (settings.enumValues_[i] == NULL)
335 GMX_THROW(APIError("Enumeration value cannot be NULL"));
337 if (defaultValue != NULL && settings.enumValues_[i] == *defaultValue)
341 allowed_.push_back(settings.enumValues_[i]);
343 if (defaultValue != NULL)
347 GMX_THROW(APIError("Default value is not one of allowed values"));
350 if (settings.defaultEnumIndex_ >= 0)
352 if (settings.defaultEnumIndex_ >= static_cast<int>(allowed_.size()))
354 GMX_THROW(APIError("Default enumeration index is out of range"));
356 if (defaultValue != NULL && *defaultValue != allowed_[settings.defaultEnumIndex_])
358 GMX_THROW(APIError("Conflicting default values"));
361 // If there is no default value, match is still -1.
362 if (enumIndexStore_ != NULL)
364 *enumIndexStore_ = match;
367 if (settings.defaultEnumIndex_ >= 0)
370 addValue(allowed_[settings.defaultEnumIndex_]);
375 std::string StringOptionStorage::formatSingleValue(const std::string &value) const
380 void StringOptionStorage::convertValue(const std::string &value)
382 if (allowed_.size() == 0)
388 ValueList::const_iterator i;
389 ValueList::const_iterator match = allowed_.end();
390 for (i = allowed_.begin(); i != allowed_.end(); ++i)
392 // TODO: Case independence.
393 if (i->find(value) == 0)
395 if (match == allowed_.end() || i->size() < match->size())
401 if (match == allowed_.end())
403 GMX_THROW(InvalidInputError("Invalid value: " + value));
409 void StringOptionStorage::refreshValues()
411 MyBase::refreshValues();
412 if (enumIndexStore_ != NULL)
414 for (size_t i = 0; i < values().size(); ++i)
416 ValueList::const_iterator match =
417 std::find(allowed_.begin(), allowed_.end(), values()[i]);
418 GMX_ASSERT(match != allowed_.end(),
419 "Enum value not found (internal error)");
420 enumIndexStore_[i] = static_cast<int>(match - allowed_.begin());
425 /********************************************************************
429 StringOptionInfo::StringOptionInfo(StringOptionStorage *option)
434 StringOptionStorage &StringOptionInfo::option()
436 return static_cast<StringOptionStorage &>(OptionInfo::option());
439 const StringOptionStorage &StringOptionInfo::option() const
441 return static_cast<const StringOptionStorage &>(OptionInfo::option());
444 bool StringOptionInfo::isEnumerated() const
446 return !allowedValues().empty();
449 const std::vector<std::string> &StringOptionInfo::allowedValues() const
451 return option().allowedValues();
454 /********************************************************************
458 AbstractOptionStoragePointer StringOption::createStorage() const
460 return AbstractOptionStoragePointer(new StringOptionStorage(*this));