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"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/stringutil.h"
58 #include "basicoptionstorage.h"
64 * Expands a single value to a vector by copying the value.
66 * \tparam ValueType Type of values to process.
67 * \param[in] length Length of the resulting vector.
68 * \param[in,out] values Values to process.
69 * \throws std::bad_alloc if out of memory.
70 * \throws InvalidInputError if \p values has an invalid number of values.
72 * \p values should have 0, 1, or \p length values.
73 * If \p values has 1 value, it is expanded such that it has \p length
74 * identical values. In other valid cases, nothing is done.
76 template <typename ValueType>
77 void expandVector(size_t length, std::vector<ValueType> *values)
79 if (length > 0 && !values->empty() && values->size() != length)
81 if (values->size() != 1)
83 GMX_THROW(gmx::InvalidInputError(gmx::formatString(
84 "Expected 1 or %d values, got %d", length, values->size())));
86 const ValueType &value = (*values)[0];
87 values->resize(length, value);
96 /********************************************************************
97 * BooleanOptionStorage
100 std::string BooleanOptionStorage::formatSingleValue(const bool &value) const
102 return value ? "yes" : "no";
105 void BooleanOptionStorage::convertValue(const std::string &value)
107 // TODO: Case-independence
108 if (value == "1" || value == "yes" || value == "true")
113 else if (value == "0" || value == "no" || value == "false")
118 GMX_THROW(InvalidInputError("Invalid value: '" + value + "'; supported values are: 1, 0, yes, no, true, false"));
121 /********************************************************************
125 BooleanOptionInfo::BooleanOptionInfo(BooleanOptionStorage *option)
130 const BooleanOptionStorage &BooleanOptionInfo::option() const
132 return static_cast<const BooleanOptionStorage &>(OptionInfo::option());
135 bool BooleanOptionInfo::defaultValue() const
137 return option().defaultValue();
140 /********************************************************************
144 AbstractOptionStorage *
145 BooleanOption::createStorage(const OptionManagerContainer & /*managers*/) const
147 return new BooleanOptionStorage(*this);
151 /********************************************************************
152 * IntegerOptionStorage
155 std::string IntegerOptionStorage::formatSingleValue(const int &value) const
157 return formatString("%d", value);
160 void IntegerOptionStorage::convertValue(const std::string &value)
162 const char *ptr = value.c_str();
165 long int ival = std::strtol(ptr, &endptr, 10);
167 || ival < std::numeric_limits<int>::min()
168 || ival > std::numeric_limits<int>::max())
170 GMX_THROW(InvalidInputError("Invalid value: '" + value
171 + "'; it causes an integer overflow"));
173 if (*ptr == '\0' || *endptr != '\0')
175 GMX_THROW(InvalidInputError("Invalid value: '" + value
176 + "'; expected an integer"));
181 void IntegerOptionStorage::processSetValues(ValueList *values)
185 expandVector(maxValueCount(), values);
189 /********************************************************************
193 IntegerOptionInfo::IntegerOptionInfo(IntegerOptionStorage *option)
198 /********************************************************************
202 AbstractOptionStorage *
203 IntegerOption::createStorage(const OptionManagerContainer & /*managers*/) const
205 return new IntegerOptionStorage(*this);
209 /********************************************************************
213 std::string Int64OptionStorage::formatSingleValue(const gmx_int64_t &value) const
215 return formatString("%" GMX_PRId64, value);
218 void Int64OptionStorage::convertValue(const std::string &value)
220 const char *ptr = value.c_str();
223 const gmx_int64_t ival = str_to_int64_t(ptr, &endptr);
226 GMX_THROW(InvalidInputError("Invalid value: '" + value
227 + "'; it causes an integer overflow"));
229 if (*ptr == '\0' || *endptr != '\0')
231 GMX_THROW(InvalidInputError("Invalid value: '" + value
232 + "'; expected an integer"));
237 /********************************************************************
241 Int64OptionInfo::Int64OptionInfo(Int64OptionStorage *option)
246 /********************************************************************
250 AbstractOptionStorage *
251 Int64Option::createStorage(const OptionManagerContainer & /*managers*/) const
253 return new Int64OptionStorage(*this);
257 /********************************************************************
258 * DoubleOptionStorage
261 DoubleOptionStorage::DoubleOptionStorage(const DoubleOption &settings)
262 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
266 std::string DoubleOptionStorage::typeString() const
268 return isVector() ? "vector" : (isTime() ? "time" : "real");
271 std::string DoubleOptionStorage::formatSingleValue(const double &value) const
273 return formatString("%g", value / factor_);
276 void DoubleOptionStorage::convertValue(const std::string &value)
278 const char *ptr = value.c_str();
281 double dval = std::strtod(ptr, &endptr);
284 GMX_THROW(InvalidInputError("Invalid value: '" + value
285 + "'; it causes an overflow/underflow"));
287 if (*ptr == '\0' || *endptr != '\0')
289 GMX_THROW(InvalidInputError("Invalid value: '" + value
290 + "'; expected a number"));
292 addValue(dval * factor_);
295 void DoubleOptionStorage::processSetValues(ValueList *values)
299 expandVector(maxValueCount(), values);
303 void DoubleOptionStorage::setScaleFactor(double factor)
305 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
306 if (!hasFlag(efOption_HasDefaultValue))
308 double scale = factor / factor_;
309 ValueList::iterator i;
310 for (i = values().begin(); i != values().end(); ++i)
319 /********************************************************************
323 DoubleOptionInfo::DoubleOptionInfo(DoubleOptionStorage *option)
328 DoubleOptionStorage &DoubleOptionInfo::option()
330 return static_cast<DoubleOptionStorage &>(OptionInfo::option());
333 const DoubleOptionStorage &DoubleOptionInfo::option() const
335 return static_cast<const DoubleOptionStorage &>(OptionInfo::option());
338 bool DoubleOptionInfo::isTime() const
340 return option().isTime();
343 void DoubleOptionInfo::setScaleFactor(double factor)
345 option().setScaleFactor(factor);
348 /********************************************************************
352 AbstractOptionStorage *
353 DoubleOption::createStorage(const OptionManagerContainer & /*managers*/) const
355 return new DoubleOptionStorage(*this);
359 /********************************************************************
363 FloatOptionStorage::FloatOptionStorage(const FloatOption &settings)
364 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
368 std::string FloatOptionStorage::typeString() const
370 return isVector() ? "vector" : (isTime() ? "time" : "real");
373 std::string FloatOptionStorage::formatSingleValue(const float &value) const
375 return formatString("%g", value / factor_);
378 void FloatOptionStorage::convertValue(const std::string &value)
380 const char *ptr = value.c_str();
383 double dval = std::strtod(ptr, &endptr);
385 || dval * factor_ < -std::numeric_limits<float>::max()
386 || dval * factor_ > std::numeric_limits<float>::max())
388 GMX_THROW(InvalidInputError("Invalid value: '" + value
389 + "'; it causes an overflow/underflow"));
391 if (*ptr == '\0' || *endptr != '\0')
393 GMX_THROW(InvalidInputError("Invalid value: '" + value
394 + "'; expected a number"));
396 addValue(dval * factor_);
399 void FloatOptionStorage::processSetValues(ValueList *values)
403 expandVector(maxValueCount(), values);
407 void FloatOptionStorage::setScaleFactor(double factor)
409 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
410 if (!hasFlag(efOption_HasDefaultValue))
412 double scale = factor / factor_;
413 ValueList::iterator i;
414 for (i = values().begin(); i != values().end(); ++i)
423 /********************************************************************
427 FloatOptionInfo::FloatOptionInfo(FloatOptionStorage *option)
432 FloatOptionStorage &FloatOptionInfo::option()
434 return static_cast<FloatOptionStorage &>(OptionInfo::option());
437 const FloatOptionStorage &FloatOptionInfo::option() const
439 return static_cast<const FloatOptionStorage &>(OptionInfo::option());
442 bool FloatOptionInfo::isTime() const
444 return option().isTime();
447 void FloatOptionInfo::setScaleFactor(double factor)
449 option().setScaleFactor(factor);
452 /********************************************************************
456 AbstractOptionStorage *
457 FloatOption::createStorage(const OptionManagerContainer & /*managers*/) const
459 return new FloatOptionStorage(*this);
463 /********************************************************************
464 * StringOptionStorage
467 StringOptionStorage::StringOptionStorage(const StringOption &settings)
468 : MyBase(settings), info_(this), enumIndexStore_(NULL)
470 if (settings.defaultEnumIndex_ >= 0 && settings.enumValues_ == NULL)
472 GMX_THROW(APIError("Cannot set default enum index without enum values"));
474 if (settings.enumIndexStore_ != NULL && settings.enumValues_ == NULL)
476 GMX_THROW(APIError("Cannot set enum index store without enum values"));
478 if (settings.enumIndexStore_ != NULL && settings.maxValueCount_ < 0)
480 GMX_THROW(APIError("Cannot set enum index store with arbitrary number of values"));
482 if (settings.enumValues_ != NULL)
484 enumIndexStore_ = settings.enumIndexStore_;
485 const std::string *defaultValue = settings.defaultValue();
487 int count = settings.enumValuesCount_;
491 while (settings.enumValues_[count] != NULL)
496 for (int i = 0; i < count; ++i)
498 if (settings.enumValues_[i] == NULL)
500 GMX_THROW(APIError("Enumeration value cannot be NULL"));
502 if (defaultValue != NULL && settings.enumValues_[i] == *defaultValue)
506 allowed_.push_back(settings.enumValues_[i]);
508 if (defaultValue != NULL)
512 GMX_THROW(APIError("Default value is not one of allowed values"));
515 if (settings.defaultEnumIndex_ >= 0)
517 if (settings.defaultEnumIndex_ >= static_cast<int>(allowed_.size()))
519 GMX_THROW(APIError("Default enumeration index is out of range"));
521 if (defaultValue != NULL && *defaultValue != allowed_[settings.defaultEnumIndex_])
523 GMX_THROW(APIError("Conflicting default values"));
527 if (settings.defaultEnumIndex_ >= 0)
530 addValue(allowed_[settings.defaultEnumIndex_]);
533 // Somewhat subtly, this does not update the stored enum index if the
534 // caller has not provided store() or storeVector(), because values()
535 // will be empty in such a case. This leads to (desired) behavior of
536 // preserving the existing value in the enum index store variable in such
538 refreshEnumIndexStore();
541 std::string StringOptionStorage::formatExtraDescription() const
544 if (!allowed_.empty())
547 ValueList::const_iterator i;
548 for (i = allowed_.begin(); i != allowed_.end(); ++i)
550 if (i != allowed_.begin())
560 std::string StringOptionStorage::formatSingleValue(const std::string &value) const
565 void StringOptionStorage::convertValue(const std::string &value)
567 if (allowed_.size() == 0)
573 ValueList::const_iterator i;
574 ValueList::const_iterator match = allowed_.end();
575 for (i = allowed_.begin(); i != allowed_.end(); ++i)
577 // TODO: Case independence.
578 if (i->find(value) == 0)
580 if (match == allowed_.end() || i->size() < match->size())
586 if (match == allowed_.end())
588 GMX_THROW(InvalidInputError("Invalid value: " + value));
594 void StringOptionStorage::refreshValues()
596 MyBase::refreshValues();
597 refreshEnumIndexStore();
600 void StringOptionStorage::refreshEnumIndexStore()
602 if (enumIndexStore_ != NULL)
604 for (size_t i = 0; i < values().size(); ++i)
606 if (values()[i].empty())
608 enumIndexStore_[i] = -1;
612 ValueList::const_iterator match =
613 std::find(allowed_.begin(), allowed_.end(), values()[i]);
614 GMX_ASSERT(match != allowed_.end(),
615 "Enum value not found (internal error)");
616 enumIndexStore_[i] = static_cast<int>(match - allowed_.begin());
622 /********************************************************************
626 StringOptionInfo::StringOptionInfo(StringOptionStorage *option)
631 StringOptionStorage &StringOptionInfo::option()
633 return static_cast<StringOptionStorage &>(OptionInfo::option());
636 const StringOptionStorage &StringOptionInfo::option() const
638 return static_cast<const StringOptionStorage &>(OptionInfo::option());
641 bool StringOptionInfo::isEnumerated() const
643 return !allowedValues().empty();
646 const std::vector<std::string> &StringOptionInfo::allowedValues() const
648 return option().allowedValues();
651 /********************************************************************
655 AbstractOptionStorage *
656 StringOption::createStorage(const OptionManagerContainer & /*managers*/) const
658 return new StringOptionStorage(*this);