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"
47 #include "gromacs/options/basicoptioninfo.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/stringutil.h"
51 #include "basicoptionstorage.h"
57 * Expands a single value to a vector by copying the value.
59 * \tparam ValueType Type of values to process.
60 * \param[in] length Length of the resulting vector.
61 * \param[in,out] values Values to process.
62 * \throws std::bad_alloc if out of memory.
63 * \throws InvalidInputError if \p values has an invalid number of values.
65 * \p values should have 0, 1, or \p length values.
66 * If \p values has 1 value, it is expanded such that it has \p length
67 * identical values. In other valid cases, nothing is done.
69 template <typename ValueType>
70 void expandVector(size_t length, std::vector<ValueType> *values)
72 if (length > 0 && values->size() > 0 && values->size() != length)
74 if (values->size() != 1)
76 GMX_THROW(gmx::InvalidInputError(gmx::formatString(
77 "Expected 1 or %d values, got %d", length, values->size())));
79 const ValueType &value = (*values)[0];
80 values->resize(length, value);
89 /********************************************************************
90 * BooleanOptionStorage
93 std::string BooleanOptionStorage::formatSingleValue(const bool &value) const
95 return value ? "yes" : "no";
98 void BooleanOptionStorage::convertValue(const std::string &value)
100 // TODO: Case-independence
101 if (value == "1" || value == "yes" || value == "true")
106 else if (value == "0" || value == "no" || value == "false")
111 GMX_THROW(InvalidInputError("Invalid value: '" + value + "'; supported values are: 1, 0, yes, no, true, false"));
114 /********************************************************************
118 BooleanOptionInfo::BooleanOptionInfo(BooleanOptionStorage *option)
123 /********************************************************************
127 AbstractOptionStoragePointer BooleanOption::createStorage() const
129 return AbstractOptionStoragePointer(new BooleanOptionStorage(*this));
133 /********************************************************************
134 * IntegerOptionStorage
137 std::string IntegerOptionStorage::formatSingleValue(const int &value) const
139 return formatString("%d", value);
142 void IntegerOptionStorage::convertValue(const std::string &value)
144 const char *ptr = value.c_str();
146 long int ival = std::strtol(ptr, &endptr, 10);
149 GMX_THROW(InvalidInputError("Invalid value: " + value));
154 void IntegerOptionStorage::processSetValues(ValueList *values)
158 expandVector(maxValueCount(), values);
162 /********************************************************************
166 IntegerOptionInfo::IntegerOptionInfo(IntegerOptionStorage *option)
171 /********************************************************************
175 AbstractOptionStoragePointer IntegerOption::createStorage() const
177 return AbstractOptionStoragePointer(new IntegerOptionStorage(*this));
181 /********************************************************************
182 * DoubleOptionStorage
185 DoubleOptionStorage::DoubleOptionStorage(const DoubleOption &settings)
186 : MyBase(settings), info_(this), bTime_(settings.bTime_), factor_(1.0)
190 const char *DoubleOptionStorage::typeString() const
192 return isVector() ? "vector" : (isTime() ? "time" : "double");
195 std::string DoubleOptionStorage::formatSingleValue(const double &value) const
197 return formatString("%g", value / factor_);
200 void DoubleOptionStorage::convertValue(const std::string &value)
202 const char *ptr = value.c_str();
204 double dval = std::strtod(ptr, &endptr);
207 GMX_THROW(InvalidInputError("Invalid value: " + value));
209 addValue(dval * factor_);
212 void DoubleOptionStorage::processSetValues(ValueList *values)
216 expandVector(maxValueCount(), values);
220 void DoubleOptionStorage::processAll()
224 void DoubleOptionStorage::setScaleFactor(double factor)
226 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
227 if (!hasFlag(efOption_HasDefaultValue))
229 double scale = factor / factor_;
230 ValueList::iterator i;
231 for (i = values().begin(); i != values().end(); ++i)
240 /********************************************************************
244 DoubleOptionInfo::DoubleOptionInfo(DoubleOptionStorage *option)
249 DoubleOptionStorage &DoubleOptionInfo::option()
251 return static_cast<DoubleOptionStorage &>(OptionInfo::option());
254 const DoubleOptionStorage &DoubleOptionInfo::option() const
256 return static_cast<const DoubleOptionStorage &>(OptionInfo::option());
259 bool DoubleOptionInfo::isTime() const
261 return option().isTime();
264 void DoubleOptionInfo::setScaleFactor(double factor)
266 option().setScaleFactor(factor);
269 /********************************************************************
273 AbstractOptionStoragePointer DoubleOption::createStorage() const
275 return AbstractOptionStoragePointer(new DoubleOptionStorage(*this));
279 /********************************************************************
280 * StringOptionStorage
283 StringOptionStorage::StringOptionStorage(const StringOption &settings)
284 : MyBase(settings), info_(this), enumIndexStore_(NULL)
286 if (settings.defaultEnumIndex_ >= 0 && settings.enumValues_ == NULL)
288 GMX_THROW(APIError("Cannot set default enum index without enum values"));
290 if (settings.enumIndexStore_ != NULL && settings.enumValues_ == NULL)
292 GMX_THROW(APIError("Cannot set enum index store without enum values"));
294 if (settings.enumIndexStore_ != NULL && settings.maxValueCount_ < 0)
296 GMX_THROW(APIError("Cannot set enum index store with arbitrary number of values"));
298 if (settings.enumValues_ != NULL)
300 enumIndexStore_ = settings.enumIndexStore_;
301 const std::string *defaultValue = settings.defaultValue();
303 for (int i = 0; settings.enumValues_[i] != NULL; ++i)
305 if (defaultValue != NULL && settings.enumValues_[i] == *defaultValue)
309 allowed_.push_back(settings.enumValues_[i]);
311 if (defaultValue != NULL)
315 GMX_THROW(APIError("Default value is not one of allowed values"));
318 if (settings.defaultEnumIndex_ >= 0)
320 if (settings.defaultEnumIndex_ >= static_cast<int>(allowed_.size()))
322 GMX_THROW(APIError("Default enumeration index is out of range"));
324 if (defaultValue != NULL && *defaultValue != allowed_[settings.defaultEnumIndex_])
326 GMX_THROW(APIError("Conflicting default values"));
329 // If there is no default value, match is still -1.
330 if (enumIndexStore_ != NULL)
332 *enumIndexStore_ = match;
335 if (settings.defaultEnumIndex_ >= 0)
338 addValue(allowed_[settings.defaultEnumIndex_]);
343 std::string StringOptionStorage::formatSingleValue(const std::string &value) const
348 void StringOptionStorage::convertValue(const std::string &value)
350 if (allowed_.size() == 0)
356 ValueList::const_iterator i;
357 ValueList::const_iterator match = allowed_.end();
358 for (i = allowed_.begin(); i != allowed_.end(); ++i)
360 // TODO: Case independence.
361 if (i->find(value) == 0)
363 if (match == allowed_.end() || i->size() < match->size())
369 if (match == allowed_.end())
371 GMX_THROW(InvalidInputError("Invalid value: " + value));
377 void StringOptionStorage::refreshValues()
379 MyBase::refreshValues();
380 if (enumIndexStore_ != NULL)
382 for (size_t i = 0; i < values().size(); ++i)
384 ValueList::const_iterator match =
385 std::find(allowed_.begin(), allowed_.end(), values()[i]);
386 GMX_ASSERT(match != allowed_.end(),
387 "Enum value not found (internal error)");
388 enumIndexStore_[i] = static_cast<int>(match - allowed_.begin());
393 /********************************************************************
397 StringOptionInfo::StringOptionInfo(StringOptionStorage *option)
402 /********************************************************************
406 AbstractOptionStoragePointer StringOption::createStorage() const
408 return AbstractOptionStoragePointer(new StringOptionStorage(*this));
411 std::string StringOption::createDescription() const
413 std::string value(MyBase::createDescription());
415 if (enumValues_ != NULL)
418 for (int i = 0; enumValues_[i] != NULL; ++i)
420 value.append(enumValues_[i]);
421 if (enumValues_[i + 1] != NULL)
423 value.append(enumValues_[i + 2] != NULL ? ", " : ", or ");