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"
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)
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 isVector() ? "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)
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(efOption_HasDefaultValue))
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 ");