2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,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.
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/cstringutil.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/stringutil.h"
61 * Expands a single value to a vector by copying the value.
63 * \tparam ValueType Type of values to process.
64 * \param[in] length Length of the resulting vector.
65 * \param[in,out] values Values to process.
66 * \throws std::bad_alloc if out of memory.
67 * \throws InvalidInputError if \p values has an invalid number of values.
69 * \p values should have 0, 1, or \p length values.
70 * If \p values has 1 value, it is expanded such that it has \p length
71 * identical values. In other valid cases, nothing is done.
73 template <typename ValueType>
74 void expandVector(size_t length, std::vector<ValueType> *values)
76 if (length > 0 && !values->empty() && values->size() != length)
78 if (values->size() != 1)
80 GMX_THROW(gmx::InvalidInputError(gmx::formatString(
81 "Expected 1 or %d values, got %d", length, values->size())));
83 const ValueType &value = (*values)[0];
84 values->resize(length, value);
93 /********************************************************************
94 * BooleanOptionStorage
97 std::string BooleanOptionStorage::formatSingleValue(const bool &value) const
99 return value ? "yes" : "no";
102 void BooleanOptionStorage::convertValue(const std::string &value)
104 // TODO: Case-independence
105 if (value == "1" || value == "yes" || value == "true")
110 else if (value == "0" || value == "no" || value == "false")
115 GMX_THROW(InvalidInputError("Invalid value: '" + value + "'; supported values are: 1, 0, yes, no, true, false"));
118 /********************************************************************
122 BooleanOptionInfo::BooleanOptionInfo(BooleanOptionStorage *option)
127 const BooleanOptionStorage &BooleanOptionInfo::option() const
129 return static_cast<const BooleanOptionStorage &>(OptionInfo::option());
132 bool BooleanOptionInfo::defaultValue() const
134 return option().defaultValue();
137 /********************************************************************
141 AbstractOptionStorage *
142 BooleanOption::createStorage(const OptionManagerContainer & /*managers*/) const
144 return new BooleanOptionStorage(*this);
148 /********************************************************************
149 * IntegerOptionStorage
152 std::string IntegerOptionStorage::formatSingleValue(const int &value) const
154 return formatString("%d", value);
157 void IntegerOptionStorage::convertValue(const std::string &value)
159 const char *ptr = value.c_str();
162 long int ival = std::strtol(ptr, &endptr, 10);
164 || ival < std::numeric_limits<int>::min()
165 || ival > std::numeric_limits<int>::max())
167 GMX_THROW(InvalidInputError("Invalid value: '" + value
168 + "'; it causes an integer overflow"));
170 if (*ptr == '\0' || *endptr != '\0')
172 GMX_THROW(InvalidInputError("Invalid value: '" + value
173 + "'; expected an integer"));
178 void IntegerOptionStorage::processSetValues(ValueList *values)
182 expandVector(maxValueCount(), values);
186 /********************************************************************
190 IntegerOptionInfo::IntegerOptionInfo(IntegerOptionStorage *option)
195 /********************************************************************
199 AbstractOptionStorage *
200 IntegerOption::createStorage(const OptionManagerContainer & /*managers*/) const
202 return new IntegerOptionStorage(*this);
206 /********************************************************************
210 std::string Int64OptionStorage::formatSingleValue(const gmx_int64_t &value) const
212 return formatString("%" GMX_PRId64, value);
215 void Int64OptionStorage::convertValue(const std::string &value)
217 const char *ptr = value.c_str();
220 const gmx_int64_t ival = str_to_int64_t(ptr, &endptr);
223 GMX_THROW(InvalidInputError("Invalid value: '" + value
224 + "'; it causes an integer overflow"));
226 if (*ptr == '\0' || *endptr != '\0')
228 GMX_THROW(InvalidInputError("Invalid value: '" + value
229 + "'; expected an integer"));
234 /********************************************************************
238 Int64OptionInfo::Int64OptionInfo(Int64OptionStorage *option)
243 /********************************************************************
247 AbstractOptionStorage *
248 Int64Option::createStorage(const OptionManagerContainer & /*managers*/) const
250 return new Int64OptionStorage(*this);
254 /********************************************************************
255 * DoubleOptionStorage
258 DoubleOptionStorage::DoubleOptionStorage(const DoubleOption &settings)
259 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
263 std::string DoubleOptionStorage::typeString() const
265 return isVector() ? "vector" : (isTime() ? "time" : "real");
268 std::string DoubleOptionStorage::formatSingleValue(const double &value) const
270 return formatString("%g", value / factor_);
273 void DoubleOptionStorage::convertValue(const std::string &value)
275 const char *ptr = value.c_str();
278 double dval = std::strtod(ptr, &endptr);
281 GMX_THROW(InvalidInputError("Invalid value: '" + value
282 + "'; it causes an overflow/underflow"));
284 if (*ptr == '\0' || *endptr != '\0')
286 GMX_THROW(InvalidInputError("Invalid value: '" + value
287 + "'; expected a number"));
289 addValue(dval * factor_);
292 void DoubleOptionStorage::processSetValues(ValueList *values)
296 expandVector(maxValueCount(), values);
300 void DoubleOptionStorage::setScaleFactor(double factor)
302 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
303 if (!hasFlag(efOption_HasDefaultValue))
305 double scale = factor / factor_;
306 ValueList::iterator i;
307 for (i = values().begin(); i != values().end(); ++i)
316 /********************************************************************
320 DoubleOptionInfo::DoubleOptionInfo(DoubleOptionStorage *option)
325 DoubleOptionStorage &DoubleOptionInfo::option()
327 return static_cast<DoubleOptionStorage &>(OptionInfo::option());
330 const DoubleOptionStorage &DoubleOptionInfo::option() const
332 return static_cast<const DoubleOptionStorage &>(OptionInfo::option());
335 bool DoubleOptionInfo::isTime() const
337 return option().isTime();
340 void DoubleOptionInfo::setScaleFactor(double factor)
342 option().setScaleFactor(factor);
345 /********************************************************************
349 AbstractOptionStorage *
350 DoubleOption::createStorage(const OptionManagerContainer & /*managers*/) const
352 return new DoubleOptionStorage(*this);
356 /********************************************************************
360 FloatOptionStorage::FloatOptionStorage(const FloatOption &settings)
361 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
365 std::string FloatOptionStorage::typeString() const
367 return isVector() ? "vector" : (isTime() ? "time" : "real");
370 std::string FloatOptionStorage::formatSingleValue(const float &value) const
372 return formatString("%g", value / factor_);
375 void FloatOptionStorage::convertValue(const std::string &value)
377 const char *ptr = value.c_str();
380 double dval = std::strtod(ptr, &endptr);
382 || dval * factor_ < -std::numeric_limits<float>::max()
383 || dval * factor_ > std::numeric_limits<float>::max())
385 GMX_THROW(InvalidInputError("Invalid value: '" + value
386 + "'; it causes an overflow/underflow"));
388 if (*ptr == '\0' || *endptr != '\0')
390 GMX_THROW(InvalidInputError("Invalid value: '" + value
391 + "'; expected a number"));
393 addValue(dval * factor_);
396 void FloatOptionStorage::processSetValues(ValueList *values)
400 expandVector(maxValueCount(), values);
404 void FloatOptionStorage::setScaleFactor(double factor)
406 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
407 if (!hasFlag(efOption_HasDefaultValue))
409 double scale = factor / factor_;
410 ValueList::iterator i;
411 for (i = values().begin(); i != values().end(); ++i)
420 /********************************************************************
424 FloatOptionInfo::FloatOptionInfo(FloatOptionStorage *option)
429 FloatOptionStorage &FloatOptionInfo::option()
431 return static_cast<FloatOptionStorage &>(OptionInfo::option());
434 const FloatOptionStorage &FloatOptionInfo::option() const
436 return static_cast<const FloatOptionStorage &>(OptionInfo::option());
439 bool FloatOptionInfo::isTime() const
441 return option().isTime();
444 void FloatOptionInfo::setScaleFactor(double factor)
446 option().setScaleFactor(factor);
449 /********************************************************************
453 AbstractOptionStorage *
454 FloatOption::createStorage(const OptionManagerContainer & /*managers*/) const
456 return new FloatOptionStorage(*this);
460 /********************************************************************
461 * StringOptionStorage
464 StringOptionStorage::StringOptionStorage(const StringOption &settings)
465 : MyBase(settings), info_(this), enumIndexStore_(NULL)
467 if (settings.defaultEnumIndex_ >= 0 && settings.enumValues_ == NULL)
469 GMX_THROW(APIError("Cannot set default enum index without enum values"));
471 if (settings.enumIndexStore_ != NULL && settings.enumValues_ == NULL)
473 GMX_THROW(APIError("Cannot set enum index store without enum values"));
475 if (settings.enumIndexStore_ != NULL && settings.maxValueCount_ < 0)
477 GMX_THROW(APIError("Cannot set enum index store with arbitrary number of values"));
479 if (settings.enumValues_ != NULL)
481 enumIndexStore_ = settings.enumIndexStore_;
482 const std::string *defaultValue = settings.defaultValue();
484 int count = settings.enumValuesCount_;
488 while (settings.enumValues_[count] != NULL)
493 for (int i = 0; i < count; ++i)
495 if (settings.enumValues_[i] == NULL)
497 GMX_THROW(APIError("Enumeration value cannot be NULL"));
499 if (defaultValue != NULL && settings.enumValues_[i] == *defaultValue)
503 allowed_.push_back(settings.enumValues_[i]);
505 if (defaultValue != NULL)
509 GMX_THROW(APIError("Default value is not one of allowed values"));
512 if (settings.defaultEnumIndex_ >= 0)
514 if (settings.defaultEnumIndex_ >= static_cast<int>(allowed_.size()))
516 GMX_THROW(APIError("Default enumeration index is out of range"));
518 if (defaultValue != NULL && *defaultValue != allowed_[settings.defaultEnumIndex_])
520 GMX_THROW(APIError("Conflicting default values"));
523 // If there is no default value, match is still -1.
524 if (enumIndexStore_ != NULL)
526 *enumIndexStore_ = match;
529 if (settings.defaultEnumIndex_ >= 0)
532 addValue(allowed_[settings.defaultEnumIndex_]);
537 std::string StringOptionStorage::formatExtraDescription() const
540 if (!allowed_.empty())
543 ValueList::const_iterator i;
544 for (i = allowed_.begin(); i != allowed_.end(); ++i)
546 if (i != allowed_.begin())
556 std::string StringOptionStorage::formatSingleValue(const std::string &value) const
561 void StringOptionStorage::convertValue(const std::string &value)
563 if (allowed_.size() == 0)
569 ValueList::const_iterator i;
570 ValueList::const_iterator match = allowed_.end();
571 for (i = allowed_.begin(); i != allowed_.end(); ++i)
573 // TODO: Case independence.
574 if (i->find(value) == 0)
576 if (match == allowed_.end() || i->size() < match->size())
582 if (match == allowed_.end())
584 GMX_THROW(InvalidInputError("Invalid value: " + value));
590 void StringOptionStorage::refreshValues()
592 MyBase::refreshValues();
593 if (enumIndexStore_ != NULL)
595 for (size_t i = 0; i < values().size(); ++i)
597 ValueList::const_iterator match =
598 std::find(allowed_.begin(), allowed_.end(), values()[i]);
599 GMX_ASSERT(match != allowed_.end(),
600 "Enum value not found (internal error)");
601 enumIndexStore_[i] = static_cast<int>(match - allowed_.begin());
606 /********************************************************************
610 StringOptionInfo::StringOptionInfo(StringOptionStorage *option)
615 StringOptionStorage &StringOptionInfo::option()
617 return static_cast<StringOptionStorage &>(OptionInfo::option());
620 const StringOptionStorage &StringOptionInfo::option() const
622 return static_cast<const StringOptionStorage &>(OptionInfo::option());
625 bool StringOptionInfo::isEnumerated() const
627 return !allowedValues().empty();
630 const std::vector<std::string> &StringOptionInfo::allowedValues() const
632 return option().allowedValues();
635 /********************************************************************
639 AbstractOptionStorage *
640 StringOption::createStorage(const OptionManagerContainer & /*managers*/) const
642 return new StringOptionStorage(*this);