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, basicoptioninfo.h and
34 * basicoptionstorage.h.
36 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
37 * \ingroup module_options
39 #include "gromacs/options/basicoptions.h"
49 #include "gromacs/options/basicoptioninfo.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/stringutil.h"
53 #include "basicoptionstorage.h"
59 * Expands a single value to a vector by copying the value.
61 * \tparam ValueType Type of values to process.
62 * \param[in] length Length of the resulting vector.
63 * \param[in,out] values Values to process.
64 * \throws std::bad_alloc if out of memory.
65 * \throws InvalidInputError if \p values has an invalid number of values.
67 * \p values should have 0, 1, or \p length values.
68 * If \p values has 1 value, it is expanded such that it has \p length
69 * identical values. In other valid cases, nothing is done.
71 template <typename ValueType>
72 void expandVector(size_t length, std::vector<ValueType> *values)
74 if (length > 0 && values->size() > 0 && values->size() != length)
76 if (values->size() != 1)
78 GMX_THROW(gmx::InvalidInputError(gmx::formatString(
79 "Expected 1 or %d values, got %d", length, values->size())));
81 const ValueType &value = (*values)[0];
82 values->resize(length, value);
91 /********************************************************************
92 * BooleanOptionStorage
95 std::string BooleanOptionStorage::formatSingleValue(const bool &value) const
97 return value ? "yes" : "no";
100 void BooleanOptionStorage::convertValue(const std::string &value)
102 // TODO: Case-independence
103 if (value == "1" || value == "yes" || value == "true")
108 else if (value == "0" || value == "no" || value == "false")
113 GMX_THROW(InvalidInputError("Invalid value: '" + value + "'; supported values are: 1, 0, yes, no, true, false"));
116 /********************************************************************
120 BooleanOptionInfo::BooleanOptionInfo(BooleanOptionStorage *option)
125 /********************************************************************
129 AbstractOptionStoragePointer BooleanOption::createStorage() const
131 return AbstractOptionStoragePointer(new BooleanOptionStorage(*this));
135 /********************************************************************
136 * IntegerOptionStorage
139 std::string IntegerOptionStorage::formatSingleValue(const int &value) const
141 return formatString("%d", value);
144 void IntegerOptionStorage::convertValue(const std::string &value)
146 const char *ptr = value.c_str();
149 long int ival = std::strtol(ptr, &endptr, 10);
151 || ival < std::numeric_limits<int>::min()
152 || ival > std::numeric_limits<int>::max())
154 GMX_THROW(InvalidInputError("Invalid value: '" + value
155 + "'; it causes an integer overflow"));
157 if (*ptr == '\0' || *endptr != '\0')
159 GMX_THROW(InvalidInputError("Invalid value: '" + value
160 + "'; expected an integer"));
165 void IntegerOptionStorage::processSetValues(ValueList *values)
169 expandVector(maxValueCount(), values);
173 /********************************************************************
177 IntegerOptionInfo::IntegerOptionInfo(IntegerOptionStorage *option)
182 /********************************************************************
186 AbstractOptionStoragePointer IntegerOption::createStorage() const
188 return AbstractOptionStoragePointer(new IntegerOptionStorage(*this));
192 /********************************************************************
193 * DoubleOptionStorage
196 DoubleOptionStorage::DoubleOptionStorage(const DoubleOption &settings)
197 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
201 const char *DoubleOptionStorage::typeString() const
203 return isVector() ? "vector" : (isTime() ? "time" : "double");
206 std::string DoubleOptionStorage::formatSingleValue(const double &value) const
208 return formatString("%g", value / factor_);
211 void DoubleOptionStorage::convertValue(const std::string &value)
213 const char *ptr = value.c_str();
216 double dval = std::strtod(ptr, &endptr);
219 GMX_THROW(InvalidInputError("Invalid value: '" + value
220 + "'; it causes an overflow/underflow"));
222 if (*ptr == '\0' || *endptr != '\0')
224 GMX_THROW(InvalidInputError("Invalid value: '" + value
225 + "'; expected a number"));
227 addValue(dval * factor_);
230 void DoubleOptionStorage::processSetValues(ValueList *values)
234 expandVector(maxValueCount(), values);
238 void DoubleOptionStorage::processAll()
242 void DoubleOptionStorage::setScaleFactor(double factor)
244 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
245 if (!hasFlag(efOption_HasDefaultValue))
247 double scale = factor / factor_;
248 ValueList::iterator i;
249 for (i = values().begin(); i != values().end(); ++i)
258 /********************************************************************
262 DoubleOptionInfo::DoubleOptionInfo(DoubleOptionStorage *option)
267 DoubleOptionStorage &DoubleOptionInfo::option()
269 return static_cast<DoubleOptionStorage &>(OptionInfo::option());
272 const DoubleOptionStorage &DoubleOptionInfo::option() const
274 return static_cast<const DoubleOptionStorage &>(OptionInfo::option());
277 bool DoubleOptionInfo::isTime() const
279 return option().isTime();
282 void DoubleOptionInfo::setScaleFactor(double factor)
284 option().setScaleFactor(factor);
287 /********************************************************************
291 AbstractOptionStoragePointer DoubleOption::createStorage() const
293 return AbstractOptionStoragePointer(new DoubleOptionStorage(*this));
297 /********************************************************************
298 * StringOptionStorage
301 StringOptionStorage::StringOptionStorage(const StringOption &settings)
302 : MyBase(settings), info_(this), enumIndexStore_(NULL)
304 if (settings.defaultEnumIndex_ >= 0 && settings.enumValues_ == NULL)
306 GMX_THROW(APIError("Cannot set default enum index without enum values"));
308 if (settings.enumIndexStore_ != NULL && settings.enumValues_ == NULL)
310 GMX_THROW(APIError("Cannot set enum index store without enum values"));
312 if (settings.enumIndexStore_ != NULL && settings.maxValueCount_ < 0)
314 GMX_THROW(APIError("Cannot set enum index store with arbitrary number of values"));
316 if (settings.enumValues_ != NULL)
318 enumIndexStore_ = settings.enumIndexStore_;
319 const std::string *defaultValue = settings.defaultValue();
321 for (int i = 0; settings.enumValues_[i] != NULL; ++i)
323 if (defaultValue != NULL && settings.enumValues_[i] == *defaultValue)
327 allowed_.push_back(settings.enumValues_[i]);
329 if (defaultValue != NULL)
333 GMX_THROW(APIError("Default value is not one of allowed values"));
336 if (settings.defaultEnumIndex_ >= 0)
338 if (settings.defaultEnumIndex_ >= static_cast<int>(allowed_.size()))
340 GMX_THROW(APIError("Default enumeration index is out of range"));
342 if (defaultValue != NULL && *defaultValue != allowed_[settings.defaultEnumIndex_])
344 GMX_THROW(APIError("Conflicting default values"));
347 // If there is no default value, match is still -1.
348 if (enumIndexStore_ != NULL)
350 *enumIndexStore_ = match;
353 if (settings.defaultEnumIndex_ >= 0)
356 addValue(allowed_[settings.defaultEnumIndex_]);
361 std::string StringOptionStorage::formatSingleValue(const std::string &value) const
366 void StringOptionStorage::convertValue(const std::string &value)
368 if (allowed_.size() == 0)
374 ValueList::const_iterator i;
375 ValueList::const_iterator match = allowed_.end();
376 for (i = allowed_.begin(); i != allowed_.end(); ++i)
378 // TODO: Case independence.
379 if (i->find(value) == 0)
381 if (match == allowed_.end() || i->size() < match->size())
387 if (match == allowed_.end())
389 GMX_THROW(InvalidInputError("Invalid value: " + value));
395 void StringOptionStorage::refreshValues()
397 MyBase::refreshValues();
398 if (enumIndexStore_ != NULL)
400 for (size_t i = 0; i < values().size(); ++i)
402 ValueList::const_iterator match =
403 std::find(allowed_.begin(), allowed_.end(), values()[i]);
404 GMX_ASSERT(match != allowed_.end(),
405 "Enum value not found (internal error)");
406 enumIndexStore_[i] = static_cast<int>(match - allowed_.begin());
411 /********************************************************************
415 StringOptionInfo::StringOptionInfo(StringOptionStorage *option)
420 /********************************************************************
424 AbstractOptionStoragePointer StringOption::createStorage() const
426 return AbstractOptionStoragePointer(new StringOptionStorage(*this));
429 std::string StringOption::createDescription() const
431 std::string value(MyBase::createDescription());
433 if (enumValues_ != NULL)
436 for (int i = 0; enumValues_[i] != NULL; ++i)
438 value.append(enumValues_[i]);
439 if (enumValues_[i + 1] != NULL)
441 value.append(enumValues_[i + 2] != NULL ? ", " : ", or ");