3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements classes in basicoptions.h and basicoptionstorage.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_options
38 #include "basicoptions.h"
39 #include "basicoptionstorage.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/stringutil.h"
56 * Expands a single value to a vector by copying the value.
58 * \tparam ValueType Type of values to process.
59 * \param[in] length Length of the resulting vector.
60 * \param[in,out] values Values to process.
61 * \throws std::bad_alloc if out of memory.
62 * \throws InvalidInputError if \p values has an invalid number of values.
64 * \p values should have 0, 1, or \p length values.
65 * If \p values has 1 value, it is expanded such that it has \p length
66 * identical values. In other valid cases, nothing is done.
68 template <typename ValueType>
69 void expandVector(size_t length, std::vector<ValueType> *values)
71 if (length > 0 && values->size() > 0 && values->size() != length)
73 if (values->size() != 1)
75 GMX_THROW(gmx::InvalidInputError(gmx::formatString(
76 "Expected 1 or %d values, got %d", length, values->size())));
78 const ValueType &value = (*values)[0];
79 values->resize(length, value);
88 /********************************************************************
89 * BooleanOptionStorage
92 std::string BooleanOptionStorage::formatSingleValue(const bool &value) const
94 return value ? "yes" : "no";
97 void BooleanOptionStorage::convertValue(const std::string &value)
99 // TODO: Case-independence
100 if (value == "1" || value == "yes" || value == "true")
105 else if (value == "0" || value == "no" || value == "false")
110 GMX_THROW(InvalidInputError("Invalid value: '" + value + "'; supported values are: 1, 0, yes, no, true, false"));
113 /********************************************************************
117 BooleanOptionInfo::BooleanOptionInfo(BooleanOptionStorage *option)
122 /********************************************************************
126 AbstractOptionStoragePointer BooleanOption::createStorage() const
128 return AbstractOptionStoragePointer(new BooleanOptionStorage(*this));
132 /********************************************************************
133 * IntegerOptionStorage
136 std::string IntegerOptionStorage::formatSingleValue(const int &value) const
138 return formatString("%d", value);
141 void IntegerOptionStorage::convertValue(const std::string &value)
143 const char *ptr = value.c_str();
146 long int ival = std::strtol(ptr, &endptr, 10);
148 || ival < std::numeric_limits<int>::min()
149 || ival > std::numeric_limits<int>::max())
151 GMX_THROW(InvalidInputError("Invalid value: '" + value
152 + "'; it causes an integer overflow"));
154 if (*ptr == '\0' || *endptr != '\0')
156 GMX_THROW(InvalidInputError("Invalid value: '" + value
157 + "'; expected an integer"));
162 void IntegerOptionStorage::processSetValues(ValueList *values)
166 expandVector(maxValueCount(), values);
170 /********************************************************************
174 IntegerOptionInfo::IntegerOptionInfo(IntegerOptionStorage *option)
179 /********************************************************************
183 AbstractOptionStoragePointer IntegerOption::createStorage() const
185 return AbstractOptionStoragePointer(new IntegerOptionStorage(*this));
189 /********************************************************************
190 * DoubleOptionStorage
193 DoubleOptionStorage::DoubleOptionStorage(const DoubleOption &settings)
194 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
198 const char *DoubleOptionStorage::typeString() const
200 return isVector() ? "vector" : (isTime() ? "time" : "double");
203 std::string DoubleOptionStorage::formatSingleValue(const double &value) const
205 return formatString("%g", value / factor_);
208 void DoubleOptionStorage::convertValue(const std::string &value)
210 const char *ptr = value.c_str();
213 double dval = std::strtod(ptr, &endptr);
216 GMX_THROW(InvalidInputError("Invalid value: '" + value
217 + "'; it causes an overflow/underflow"));
219 if (*ptr == '\0' || *endptr != '\0')
221 GMX_THROW(InvalidInputError("Invalid value: '" + value
222 + "'; expected a number"));
224 addValue(dval * factor_);
227 void DoubleOptionStorage::processSetValues(ValueList *values)
231 expandVector(maxValueCount(), values);
235 void DoubleOptionStorage::processAll()
239 void DoubleOptionStorage::setScaleFactor(double factor)
241 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
242 if (!hasFlag(efOption_HasDefaultValue))
244 double scale = factor / factor_;
245 ValueList::iterator i;
246 for (i = values().begin(); i != values().end(); ++i)
255 /********************************************************************
259 DoubleOptionInfo::DoubleOptionInfo(DoubleOptionStorage *option)
264 DoubleOptionStorage &DoubleOptionInfo::option()
266 return static_cast<DoubleOptionStorage &>(OptionInfo::option());
269 const DoubleOptionStorage &DoubleOptionInfo::option() const
271 return static_cast<const DoubleOptionStorage &>(OptionInfo::option());
274 bool DoubleOptionInfo::isTime() const
276 return option().isTime();
279 void DoubleOptionInfo::setScaleFactor(double factor)
281 option().setScaleFactor(factor);
284 /********************************************************************
288 AbstractOptionStoragePointer DoubleOption::createStorage() const
290 return AbstractOptionStoragePointer(new DoubleOptionStorage(*this));
294 /********************************************************************
295 * StringOptionStorage
298 StringOptionStorage::StringOptionStorage(const StringOption &settings)
299 : MyBase(settings), info_(this), enumIndexStore_(NULL)
301 if (settings.defaultEnumIndex_ >= 0 && settings.enumValues_ == NULL)
303 GMX_THROW(APIError("Cannot set default enum index without enum values"));
305 if (settings.enumIndexStore_ != NULL && settings.enumValues_ == NULL)
307 GMX_THROW(APIError("Cannot set enum index store without enum values"));
309 if (settings.enumIndexStore_ != NULL && settings.maxValueCount_ < 0)
311 GMX_THROW(APIError("Cannot set enum index store with arbitrary number of values"));
313 if (settings.enumValues_ != NULL)
315 enumIndexStore_ = settings.enumIndexStore_;
316 const std::string *defaultValue = settings.defaultValue();
318 for (int i = 0; settings.enumValues_[i] != NULL; ++i)
320 if (defaultValue != NULL && settings.enumValues_[i] == *defaultValue)
324 allowed_.push_back(settings.enumValues_[i]);
326 if (defaultValue != NULL)
330 GMX_THROW(APIError("Default value is not one of allowed values"));
333 if (settings.defaultEnumIndex_ >= 0)
335 if (settings.defaultEnumIndex_ >= static_cast<int>(allowed_.size()))
337 GMX_THROW(APIError("Default enumeration index is out of range"));
339 if (defaultValue != NULL && *defaultValue != allowed_[settings.defaultEnumIndex_])
341 GMX_THROW(APIError("Conflicting default values"));
344 // If there is no default value, match is still -1.
345 if (enumIndexStore_ != NULL)
347 *enumIndexStore_ = match;
350 if (settings.defaultEnumIndex_ >= 0)
353 addValue(allowed_[settings.defaultEnumIndex_]);
358 std::string StringOptionStorage::formatSingleValue(const std::string &value) const
363 void StringOptionStorage::convertValue(const std::string &value)
365 if (allowed_.size() == 0)
371 ValueList::const_iterator i;
372 ValueList::const_iterator match = allowed_.end();
373 for (i = allowed_.begin(); i != allowed_.end(); ++i)
375 // TODO: Case independence.
376 if (i->find(value) == 0)
378 if (match == allowed_.end() || i->size() < match->size())
384 if (match == allowed_.end())
386 GMX_THROW(InvalidInputError("Invalid value: " + value));
392 void StringOptionStorage::refreshValues()
394 MyBase::refreshValues();
395 if (enumIndexStore_ != NULL)
397 for (size_t i = 0; i < values().size(); ++i)
399 ValueList::const_iterator match =
400 std::find(allowed_.begin(), allowed_.end(), values()[i]);
401 GMX_ASSERT(match != allowed_.end(),
402 "Enum value not found (internal error)");
403 enumIndexStore_[i] = static_cast<int>(match - allowed_.begin());
408 /********************************************************************
412 StringOptionInfo::StringOptionInfo(StringOptionStorage *option)
417 /********************************************************************
421 AbstractOptionStoragePointer StringOption::createStorage() const
423 return AbstractOptionStoragePointer(new StringOptionStorage(*this));
426 std::string StringOption::createDescription() const
428 std::string value(MyBase::createDescription());
430 if (enumValues_ != NULL)
433 for (int i = 0; enumValues_[i] != NULL; ++i)
435 value.append(enumValues_[i]);
436 if (enumValues_[i + 1] != NULL)
438 value.append(enumValues_[i + 2] != NULL ? ", " : ", or ");