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/format.h"
51 #include "basicoptionstorage.h"
53 template <typename T> static
54 void expandVector(size_t length, std::vector<T> *values)
56 if (length > 0 && values->size() > 0 && values->size() != length)
58 if (values->size() != 1)
60 GMX_THROW(gmx::InvalidInputError(gmx::formatString(
61 "Expected 1 or %d values, got %d", length, values->size())));
63 const T &value = (*values)[0];
64 values->resize(length, value);
71 /********************************************************************
72 * BooleanOptionStorage
75 std::string BooleanOptionStorage::formatSingleValue(const bool &value) const
77 return value ? "yes" : "no";
80 void BooleanOptionStorage::convertValue(const std::string &value)
82 // TODO: Case-independence
83 if (value == "1" || value == "yes" || value == "true")
88 else if (value == "0" || value == "no" || value == "false")
93 GMX_THROW(InvalidInputError("Invalid value: '" + value + "'; supported values are: 1, 0, yes, no, true, false"));
96 /********************************************************************
100 BooleanOptionInfo::BooleanOptionInfo(BooleanOptionStorage *option)
105 /********************************************************************
109 AbstractOptionStoragePointer BooleanOption::createStorage() const
111 return AbstractOptionStoragePointer(new BooleanOptionStorage(*this));
115 /********************************************************************
116 * IntegerOptionStorage
119 std::string IntegerOptionStorage::formatSingleValue(const int &value) const
121 return formatString("%d", value);
124 void IntegerOptionStorage::convertValue(const std::string &value)
126 const char *ptr = value.c_str();
128 long int ival = std::strtol(ptr, &endptr, 10);
131 GMX_THROW(InvalidInputError("Invalid value: " + value));
136 void IntegerOptionStorage::processSetValues(ValueList *values)
138 if (hasFlag(efVector))
140 expandVector(maxValueCount(), values);
144 /********************************************************************
148 IntegerOptionInfo::IntegerOptionInfo(IntegerOptionStorage *option)
153 /********************************************************************
157 AbstractOptionStoragePointer IntegerOption::createStorage() const
159 return AbstractOptionStoragePointer(new IntegerOptionStorage(*this));
163 /********************************************************************
164 * DoubleOptionStorage
167 DoubleOptionStorage::DoubleOptionStorage(const DoubleOption &settings)
168 : MyBase(settings), info_(this), bTime_(settings._bTime), factor_(1.0)
172 const char *DoubleOptionStorage::typeString() const
174 return hasFlag(efVector) ? "vector" : (isTime() ? "time" : "double");
177 std::string DoubleOptionStorage::formatSingleValue(const double &value) const
179 return formatString("%g", value / factor_);
182 void DoubleOptionStorage::convertValue(const std::string &value)
184 const char *ptr = value.c_str();
186 double dval = std::strtod(ptr, &endptr);
189 GMX_THROW(InvalidInputError("Invalid value: " + value));
191 addValue(dval * factor_);
194 void DoubleOptionStorage::processSetValues(ValueList *values)
196 if (hasFlag(efVector))
198 expandVector(maxValueCount(), values);
202 void DoubleOptionStorage::processAll()
206 void DoubleOptionStorage::setScaleFactor(double factor)
208 GMX_RELEASE_ASSERT(factor > 0.0, "Invalid scaling factor");
209 if (!hasFlag(efHasDefaultValue))
211 double scale = factor / factor_;
212 ValueList::iterator i;
213 for (i = values().begin(); i != values().end(); ++i)
222 /********************************************************************
226 DoubleOptionInfo::DoubleOptionInfo(DoubleOptionStorage *option)
231 DoubleOptionStorage &DoubleOptionInfo::option()
233 return static_cast<DoubleOptionStorage &>(OptionInfo::option());
236 const DoubleOptionStorage &DoubleOptionInfo::option() const
238 return static_cast<const DoubleOptionStorage &>(OptionInfo::option());
241 bool DoubleOptionInfo::isTime() const
243 return option().isTime();
246 void DoubleOptionInfo::setScaleFactor(double factor)
248 option().setScaleFactor(factor);
251 /********************************************************************
255 AbstractOptionStoragePointer DoubleOption::createStorage() const
257 return AbstractOptionStoragePointer(new DoubleOptionStorage(*this));
261 /********************************************************************
262 * StringOptionStorage
265 StringOptionStorage::StringOptionStorage(const StringOption &settings)
266 : MyBase(settings), _info(this), _enumIndexStore(NULL)
268 if (settings._defaultEnumIndex >= 0 && settings._enumValues == NULL)
270 GMX_THROW(APIError("Cannot set default enum index without enum values"));
272 if (settings._enumIndexStore != NULL && settings._enumValues == NULL)
274 GMX_THROW(APIError("Cannot set enum index store without enum values"));
276 if (settings._enumIndexStore != NULL && settings._maxValueCount < 0)
278 GMX_THROW(APIError("Cannot set enum index store with arbitrary number of values"));
280 if (settings._enumValues != NULL)
282 _enumIndexStore = settings._enumIndexStore;
283 const std::string *defaultValue = settings.defaultValue();
285 for (int i = 0; settings._enumValues[i] != NULL; ++i)
287 if (defaultValue != NULL && settings._enumValues[i] == *defaultValue)
291 _allowed.push_back(settings._enumValues[i]);
293 if (defaultValue != NULL)
297 GMX_THROW(APIError("Default value is not one of allowed values"));
300 if (settings._defaultEnumIndex >= 0)
302 if (settings._defaultEnumIndex >= static_cast<int>(_allowed.size()))
304 GMX_THROW(APIError("Default enumeration index is out of range"));
306 if (defaultValue != NULL && *defaultValue != _allowed[settings._defaultEnumIndex])
308 GMX_THROW(APIError("Conflicting default values"));
311 // If there is no default value, match is still -1.
312 if (_enumIndexStore != NULL)
314 *_enumIndexStore = match;
317 if (settings._defaultEnumIndex >= 0)
320 addValue(_allowed[settings._defaultEnumIndex]);
325 std::string StringOptionStorage::formatSingleValue(const std::string &value) const
330 void StringOptionStorage::convertValue(const std::string &value)
332 if (_allowed.size() == 0)
338 ValueList::const_iterator i;
339 ValueList::const_iterator match = _allowed.end();
340 for (i = _allowed.begin(); i != _allowed.end(); ++i)
342 // TODO: Case independence.
343 if (i->find(value) == 0)
345 if (match == _allowed.end() || i->size() < match->size())
351 if (match == _allowed.end())
353 GMX_THROW(InvalidInputError("Invalid value: " + value));
359 void StringOptionStorage::refreshValues()
361 MyBase::refreshValues();
362 if (_enumIndexStore != NULL)
364 for (size_t i = 0; i < values().size(); ++i)
366 ValueList::const_iterator match =
367 std::find(_allowed.begin(), _allowed.end(), values()[i]);
368 GMX_ASSERT(match != _allowed.end(),
369 "Enum value not found (internal error)");
370 _enumIndexStore[i] = static_cast<int>(match - _allowed.begin());
375 /********************************************************************
379 StringOptionInfo::StringOptionInfo(StringOptionStorage *option)
384 /********************************************************************
388 AbstractOptionStoragePointer StringOption::createStorage() const
390 return AbstractOptionStoragePointer(new StringOptionStorage(*this));
393 std::string StringOption::createDescription() const
395 std::string value(MyBase::createDescription());
397 if (_enumValues != NULL)
400 for (int i = 0; _enumValues[i] != NULL; ++i)
402 value.append(_enumValues[i]);
403 if (_enumValues[i + 1] != NULL)
405 value.append(_enumValues[i + 2] != NULL ? ", " : ", or ");