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/utility/exceptions.h"
43 #include "gromacs/utility/gmxassert.h"
45 #include "basicoptionstorage.h"
50 /********************************************************************
51 * AbstractOptionStorage
54 AbstractOptionStorage::AbstractOptionStorage(const AbstractOption &settings,
55 OptionFlags staticFlags)
56 : _flags(settings._flags | staticFlags),
57 _minValueCount(settings._minValueCount),
58 _maxValueCount(settings._maxValueCount),
61 // If the maximum number of values is not known, storage to
62 // caller-allocated memory is unsafe.
63 if ((_maxValueCount < 0 || hasFlag(efMulti)) && hasFlag(efExternalStore))
65 GMX_THROW(APIError("Cannot set user-allocated storage for arbitrary number of values"));
67 // Check that user has not provided incorrect values for vectors.
68 if (hasFlag(efVector) && (_minValueCount > 1 || _maxValueCount < 1))
70 GMX_THROW(APIError("Inconsistent value counts for vector values"));
73 if (settings._name != NULL)
75 _name = settings._name;
77 _descr = settings.createDescription();
80 AbstractOptionStorage::~AbstractOptionStorage()
84 bool AbstractOptionStorage::isBoolean() const
86 return dynamic_cast<const BooleanOptionStorage *>(this) != NULL;
89 void AbstractOptionStorage::startSource()
91 setFlag(efClearOnNextSet);
94 void AbstractOptionStorage::startSet()
96 GMX_RELEASE_ASSERT(!_inSet, "finishSet() not called");
97 // The last condition takes care of the situation where multiple
98 // sources are used, and a later source should be able to reassign
99 // the value even though the option is already set.
100 if (isSet() && !hasFlag(efMulti) && !hasFlag(efClearOnNextSet))
102 GMX_THROW(InvalidInputError("Option specified multiple times"));
108 void AbstractOptionStorage::appendValue(const std::string &value)
110 GMX_RELEASE_ASSERT(_inSet, "startSet() not called");
114 void AbstractOptionStorage::finishSet()
116 GMX_RELEASE_ASSERT(_inSet, "startSet() not called");
118 // TODO: Should this be done only when processSet() does not throw?
123 void AbstractOptionStorage::finish()
125 GMX_RELEASE_ASSERT(!_inSet, "finishSet() not called");
127 if (hasFlag(efRequired) && !isSet())
129 GMX_THROW(InvalidInputError("Option is required, but not set"));
133 void AbstractOptionStorage::setMinValueCount(int count)
135 GMX_RELEASE_ASSERT(!hasFlag(efMulti),
136 "setMinValueCount() not supported with efMulti");
137 GMX_RELEASE_ASSERT(count >= 0, "Invalid value count");
138 _minValueCount = count;
140 && !hasFlag(efDontCheckMinimumCount) && valueCount() < _minValueCount)
142 GMX_THROW(InvalidInputError("Too few values"));
146 void AbstractOptionStorage::setMaxValueCount(int count)
148 GMX_RELEASE_ASSERT(!hasFlag(efMulti),
149 "setMaxValueCount() not supported with efMulti");
150 GMX_RELEASE_ASSERT(count >= -1, "Invalid value count");
151 _maxValueCount = count;
152 if (isSet() && _maxValueCount >= 0 && valueCount() > _maxValueCount)
154 GMX_THROW(InvalidInputError("Too many values"));