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
44 #include "basicoptions.h"
45 #include "basicoptionstorage.h"
55 #include "gromacs/utility/cstringutil.h"
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/stringutil.h"
63 * Expands a single value to a vector by copying the value.
65 * \tparam ValueType Type of values to process.
66 * \param[in] length Length of the resulting vector.
67 * \param[in,out] values Values to process.
68 * \throws std::bad_alloc if out of memory.
69 * \throws InvalidInputError if \p values has an invalid number of values.
71 * \p values should have 0, 1, or \p length values.
72 * If \p values has 1 value, it is expanded such that it has \p length
73 * identical values. In other valid cases, nothing is done.
75 template <typename ValueType>
76 void expandVector(size_t length, std::vector<ValueType> *values)
78 if (length > 0 && !values->empty() && values->size() != length)
80 if (values->size() != 1)
82 GMX_THROW(gmx::InvalidInputError(gmx::formatString(
83 "Expected 1 or %d values, got %d", length, values->size())));
85 const ValueType &value = (*values)[0];
86 values->resize(length, value);
95 /********************************************************************
96 * BooleanOptionStorage
99 std::string BooleanOptionStorage::formatSingleValue(const bool &value) const
101 return value ? "yes" : "no";
104 void BooleanOptionStorage::convertValue(const std::string &value)
106 // TODO: Case-independence
107 if (value == "1" || value == "yes" || value == "true")
112 else if (value == "0" || value == "no" || value == "false")
117 GMX_THROW(InvalidInputError("Invalid value: '" + value + "'; supported values are: 1, 0, yes, no, true, false"));
120 /********************************************************************
124 BooleanOptionInfo::BooleanOptionInfo(BooleanOptionStorage *option)
129 const BooleanOptionStorage &BooleanOptionInfo::option() const
131 return static_cast<const BooleanOptionStorage &>(OptionInfo::option());
134 bool BooleanOptionInfo::defaultValue() const
136 return option().defaultValue();
139 /********************************************************************
143 AbstractOptionStorage *
144 BooleanOption::createStorage(const OptionManagerContainer & /*managers*/) const
146 return new BooleanOptionStorage(*this);
150 /********************************************************************
151 * IntegerOptionStorage
154 std::string IntegerOptionStorage::formatSingleValue(const int &value) const
156 return formatString("%d", value);
159 void IntegerOptionStorage::convertValue(const std::string &value)
161 const char *ptr = value.c_str();
164 long int ival = std::strtol(ptr, &endptr, 10);
166 || ival < std::numeric_limits<int>::min()
167 || ival > std::numeric_limits<int>::max())
169 GMX_THROW(InvalidInputError("Invalid value: '" + value
170 + "'; it causes an integer overflow"));
172 if (*ptr == '\0' || *endptr != '\0')
174 GMX_THROW(InvalidInputError("Invalid value: '" + value
175 + "'; expected an integer"));
180 void IntegerOptionStorage::processSetValues(ValueList *values)
184 expandVector(maxValueCount(), values);
188 /********************************************************************
192 IntegerOptionInfo::IntegerOptionInfo(IntegerOptionStorage *option)
197 /********************************************************************
201 AbstractOptionStorage *
202 IntegerOption::createStorage(const OptionManagerContainer & /*managers*/) const
204 return new IntegerOptionStorage(*this);
208 /********************************************************************
212 std::string Int64OptionStorage::formatSingleValue(const gmx_int64_t &value) const
214 return formatString("%" GMX_PRId64, value);
217 void Int64OptionStorage::convertValue(const std::string &value)
219 const char *ptr = value.c_str();
222 const gmx_int64_t ival = str_to_int64_t(ptr, &endptr);
225 GMX_THROW(InvalidInputError("Invalid value: '" + value
226 + "'; it causes an integer overflow"));
228 if (*ptr == '\0' || *endptr != '\0')
230 GMX_THROW(InvalidInputError("Invalid value: '" + value
231 + "'; expected an integer"));
236 /********************************************************************
240 Int64OptionInfo::Int64OptionInfo(Int64OptionStorage *option)
245 /********************************************************************
249 AbstractOptionStorage *
250 Int64Option::createStorage(const OptionManagerContainer & /*managers*/) const
252 return new Int64OptionStorage(*this);
256 /********************************************************************
257 * DoubleOptionStorage
260 DoubleOptionStorage::DoubleOptionStorage(const DoubleOption &settings)
261 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
265 std::string DoubleOptionStorage::typeString() const
267 return isVector() ? "vector" : (isTime() ? "time" : "real");
270 std::string DoubleOptionStorage::formatSingleValue(const double &value) const
272 return formatString("%g", value / factor_);
275 void DoubleOptionStorage::convertValue(const std::string &value)
277 const char *ptr = value.c_str();
280 double dval = std::strtod(ptr, &endptr);
283 GMX_THROW(InvalidInputError("Invalid value: '" + value
284 + "'; it causes an overflow/underflow"));
286 if (*ptr == '\0' || *endptr != '\0')
288 GMX_THROW(InvalidInputError("Invalid value: '" + value
289 + "'; expected a number"));
291 addValue(dval * factor_);
294 void DoubleOptionStorage::processSetValues(ValueList *values)
298 expandVector(maxValueCount(), values);
302 void DoubleOptionStorage::setScaleFactor(double factor)
304 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
305 if (!hasFlag(efOption_HasDefaultValue))
307 double scale = factor / factor_;
308 ValueList::iterator i;
309 for (i = values().begin(); i != values().end(); ++i)
318 /********************************************************************
322 DoubleOptionInfo::DoubleOptionInfo(DoubleOptionStorage *option)
327 DoubleOptionStorage &DoubleOptionInfo::option()
329 return static_cast<DoubleOptionStorage &>(OptionInfo::option());
332 const DoubleOptionStorage &DoubleOptionInfo::option() const
334 return static_cast<const DoubleOptionStorage &>(OptionInfo::option());
337 bool DoubleOptionInfo::isTime() const
339 return option().isTime();
342 void DoubleOptionInfo::setScaleFactor(double factor)
344 option().setScaleFactor(factor);
347 /********************************************************************
351 AbstractOptionStorage *
352 DoubleOption::createStorage(const OptionManagerContainer & /*managers*/) const
354 return new DoubleOptionStorage(*this);
358 /********************************************************************
362 FloatOptionStorage::FloatOptionStorage(const FloatOption &settings)
363 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
367 std::string FloatOptionStorage::typeString() const
369 return isVector() ? "vector" : (isTime() ? "time" : "real");
372 std::string FloatOptionStorage::formatSingleValue(const float &value) const
374 return formatString("%g", value / factor_);
377 void FloatOptionStorage::convertValue(const std::string &value)
379 const char *ptr = value.c_str();
382 double dval = std::strtod(ptr, &endptr);
384 || dval * factor_ < -std::numeric_limits<float>::max()
385 || dval * factor_ > std::numeric_limits<float>::max())
387 GMX_THROW(InvalidInputError("Invalid value: '" + value
388 + "'; it causes an overflow/underflow"));
390 if (*ptr == '\0' || *endptr != '\0')
392 GMX_THROW(InvalidInputError("Invalid value: '" + value
393 + "'; expected a number"));
395 addValue(dval * factor_);
398 void FloatOptionStorage::processSetValues(ValueList *values)
402 expandVector(maxValueCount(), values);
406 void FloatOptionStorage::setScaleFactor(double factor)
408 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
409 if (!hasFlag(efOption_HasDefaultValue))
411 double scale = factor / factor_;
412 ValueList::iterator i;
413 for (i = values().begin(); i != values().end(); ++i)
422 /********************************************************************
426 FloatOptionInfo::FloatOptionInfo(FloatOptionStorage *option)
431 FloatOptionStorage &FloatOptionInfo::option()
433 return static_cast<FloatOptionStorage &>(OptionInfo::option());
436 const FloatOptionStorage &FloatOptionInfo::option() const
438 return static_cast<const FloatOptionStorage &>(OptionInfo::option());
441 bool FloatOptionInfo::isTime() const
443 return option().isTime();
446 void FloatOptionInfo::setScaleFactor(double factor)
448 option().setScaleFactor(factor);
451 /********************************************************************
455 AbstractOptionStorage *
456 FloatOption::createStorage(const OptionManagerContainer & /*managers*/) const
458 return new FloatOptionStorage(*this);
462 /********************************************************************
463 * StringOptionStorage
466 StringOptionStorage::StringOptionStorage(const StringOption &settings)
467 : MyBase(settings), info_(this), enumIndexStore_(NULL)
469 if (settings.defaultEnumIndex_ >= 0 && settings.enumValues_ == NULL)
471 GMX_THROW(APIError("Cannot set default enum index without enum values"));
473 if (settings.enumIndexStore_ != NULL && settings.enumValues_ == NULL)
475 GMX_THROW(APIError("Cannot set enum index store without enum values"));
477 if (settings.enumIndexStore_ != NULL && settings.maxValueCount_ < 0)
479 GMX_THROW(APIError("Cannot set enum index store with arbitrary number of values"));
481 if (settings.enumValues_ != NULL)
483 enumIndexStore_ = settings.enumIndexStore_;
484 const std::string *defaultValue = settings.defaultValue();
486 int count = settings.enumValuesCount_;
490 while (settings.enumValues_[count] != NULL)
495 for (int i = 0; i < count; ++i)
497 if (settings.enumValues_[i] == NULL)
499 GMX_THROW(APIError("Enumeration value cannot be NULL"));
501 if (defaultValue != NULL && settings.enumValues_[i] == *defaultValue)
505 allowed_.push_back(settings.enumValues_[i]);
507 if (defaultValue != NULL)
511 GMX_THROW(APIError("Default value is not one of allowed values"));
514 if (settings.defaultEnumIndex_ >= 0)
516 if (settings.defaultEnumIndex_ >= static_cast<int>(allowed_.size()))
518 GMX_THROW(APIError("Default enumeration index is out of range"));
520 if (defaultValue != NULL && *defaultValue != allowed_[settings.defaultEnumIndex_])
522 GMX_THROW(APIError("Conflicting default values"));
526 if (settings.defaultEnumIndex_ >= 0)
529 addValue(allowed_[settings.defaultEnumIndex_]);
532 // Somewhat subtly, this does not update the stored enum index if the
533 // caller has not provided store() or storeVector(), because values()
534 // will be empty in such a case. This leads to (desired) behavior of
535 // preserving the existing value in the enum index store variable in such
537 refreshEnumIndexStore();
540 std::string StringOptionStorage::formatExtraDescription() const
543 if (!allowed_.empty())
546 ValueList::const_iterator i;
547 for (i = allowed_.begin(); i != allowed_.end(); ++i)
549 if (i != allowed_.begin())
559 std::string StringOptionStorage::formatSingleValue(const std::string &value) const
564 void StringOptionStorage::convertValue(const std::string &value)
566 if (allowed_.size() == 0)
572 ValueList::const_iterator i;
573 ValueList::const_iterator match = allowed_.end();
574 for (i = allowed_.begin(); i != allowed_.end(); ++i)
576 // TODO: Case independence.
577 if (i->find(value) == 0)
579 if (match == allowed_.end() || i->size() < match->size())
585 if (match == allowed_.end())
587 GMX_THROW(InvalidInputError("Invalid value: " + value));
593 void StringOptionStorage::refreshValues()
595 MyBase::refreshValues();
596 refreshEnumIndexStore();
599 void StringOptionStorage::refreshEnumIndexStore()
601 if (enumIndexStore_ != NULL)
603 for (size_t i = 0; i < values().size(); ++i)
605 if (values()[i].empty())
607 enumIndexStore_[i] = -1;
611 ValueList::const_iterator match =
612 std::find(allowed_.begin(), allowed_.end(), values()[i]);
613 GMX_ASSERT(match != allowed_.end(),
614 "Enum value not found (internal error)");
615 enumIndexStore_[i] = static_cast<int>(match - allowed_.begin());
621 /********************************************************************
625 StringOptionInfo::StringOptionInfo(StringOptionStorage *option)
630 StringOptionStorage &StringOptionInfo::option()
632 return static_cast<StringOptionStorage &>(OptionInfo::option());
635 const StringOptionStorage &StringOptionInfo::option() const
637 return static_cast<const StringOptionStorage &>(OptionInfo::option());
640 bool StringOptionInfo::isEnumerated() const
642 return !allowedValues().empty();
645 const std::vector<std::string> &StringOptionInfo::allowedValues() const
647 return option().allowedValues();
650 /********************************************************************
654 AbstractOptionStorage *
655 StringOption::createStorage(const OptionManagerContainer & /*managers*/) const
657 return new StringOptionStorage(*this);