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 abstractoption.h and abstractoptionstorage.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_options
38 #include "gromacs/options/abstractoption.h"
40 #include "gromacs/options/abstractoptionstorage.h"
41 #include "gromacs/options/optionflags.h"
42 #include "gromacs/options/optioninfo.h"
43 #include "gromacs/utility/exceptions.h"
44 #include "gromacs/utility/gmxassert.h"
46 #include "basicoptionstorage.h"
51 /********************************************************************
52 * AbstractOptionStorage
55 AbstractOptionStorage::AbstractOptionStorage(const AbstractOption &settings,
56 OptionFlags staticFlags)
57 : flags_(settings.flags_ | staticFlags),
58 minValueCount_(settings.minValueCount_),
59 maxValueCount_(settings.maxValueCount_),
62 // Check that user has not provided incorrect values for vectors.
63 if (hasFlag(efOption_Vector) && (minValueCount_ > 1 || maxValueCount_ < 1))
65 GMX_THROW(APIError("Inconsistent value counts for vector values"));
68 if (settings.name_ != NULL)
70 name_ = settings.name_;
72 descr_ = settings.createDescription();
73 setFlag(efOption_ClearOnNextSet);
76 AbstractOptionStorage::~AbstractOptionStorage()
80 bool AbstractOptionStorage::isBoolean() const
82 return dynamic_cast<const BooleanOptionStorage *>(this) != NULL;
85 void AbstractOptionStorage::startSource()
87 setFlag(efOption_ClearOnNextSet);
90 void AbstractOptionStorage::startSet()
92 GMX_RELEASE_ASSERT(!inSet_, "finishSet() not called");
93 // The last condition takes care of the situation where multiple
94 // sources are used, and a later source should be able to reassign
95 // the value even though the option is already set.
96 if (isSet() && !hasFlag(efOption_MultipleTimes)
97 && !hasFlag(efOption_ClearOnNextSet))
99 GMX_THROW(InvalidInputError("Option specified multiple times"));
105 void AbstractOptionStorage::appendValue(const std::string &value)
107 GMX_RELEASE_ASSERT(inSet_, "startSet() not called");
111 void AbstractOptionStorage::finishSet()
113 GMX_RELEASE_ASSERT(inSet_, "startSet() not called");
115 // TODO: Should this be set or not when processSet() throws?
116 setFlag(efOption_Set);
117 // TODO: Correct handling of the efOption_ClearOnNextSet requires
118 // processSet() and/or convertValue() to check it internally.
119 // OptionStorageTemplate takes care of it, but it's error-prone if
120 // a custom option is implemented that doesn't use it.
122 clearFlag(efOption_ClearOnNextSet);
125 void AbstractOptionStorage::finish()
127 GMX_RELEASE_ASSERT(!inSet_, "finishSet() not called");
129 if (isRequired() && !isSet())
131 GMX_THROW(InvalidInputError("Option is required, but not set"));
135 void AbstractOptionStorage::setMinValueCount(int count)
137 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
138 "setMinValueCount() not supported with efOption_MultipleTimes");
139 GMX_RELEASE_ASSERT(count >= 0, "Invalid value count");
140 minValueCount_ = count;
141 if (isSet() && !hasFlag(efOption_DontCheckMinimumCount)
142 && valueCount() < minValueCount_)
144 GMX_THROW(InvalidInputError("Too few values"));
148 void AbstractOptionStorage::setMaxValueCount(int count)
150 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
151 "setMaxValueCount() not supported with efOption_MultipleTimes");
152 GMX_RELEASE_ASSERT(count >= -1, "Invalid value count");
153 maxValueCount_ = count;
154 if (isSet() && maxValueCount_ >= 0 && valueCount() > maxValueCount_)
156 GMX_THROW(InvalidInputError("Too many values"));
160 /********************************************************************
165 OptionInfo::OptionInfo(AbstractOptionStorage *option)
171 OptionInfo::~OptionInfo()
175 bool OptionInfo::isSet() const
177 return option().isSet();
180 bool OptionInfo::isHidden() const
182 return option().isHidden();
185 bool OptionInfo::isRequired() const
187 return option().isRequired();
190 const std::string &OptionInfo::name() const
192 return option().name();
195 const std::string &OptionInfo::description() const
197 return option().description();
200 const char *OptionInfo::type() const
202 return option().typeString();
205 int OptionInfo::valueCount() const
207 return option().valueCount();
210 std::string OptionInfo::formatValue(int i) const
212 return option().formatValue(i);
215 std::string OptionInfo::formatDefaultValueIfSet() const
217 return option().formatDefaultValueIfSet();