2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements classes in abstractoption.h and abstractoptionstorage.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
44 #include "abstractoption.h"
46 #include "gromacs/options/abstractoptionstorage.h"
47 #include "gromacs/options/optionflags.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/gmxassert.h"
50 #include "gromacs/utility/variant.h"
52 #include "basicoptionstorage.h"
57 /********************************************************************
58 * AbstractOptionStorage
61 AbstractOptionStorage::AbstractOptionStorage(const AbstractOption
&settings
,
62 OptionFlags staticFlags
)
63 : flags_(settings
.flags_
| staticFlags
),
64 storeIsSet_(settings
.storeIsSet_
),
65 minValueCount_(settings
.minValueCount_
),
66 maxValueCount_(settings
.maxValueCount_
),
67 bInSet_(false), bSetValuesHadErrors_(false)
69 // Check that user has not provided incorrect values for vectors.
70 if (hasFlag(efOption_Vector
) && (minValueCount_
> 1 || maxValueCount_
< 1))
72 GMX_THROW(APIError("Inconsistent value counts for vector values"));
75 if (settings
.name_
!= nullptr)
77 name_
= settings
.name_
;
79 if (settings
.descr_
!= nullptr)
81 descr_
= settings
.descr_
;
83 if (storeIsSet_
!= nullptr)
87 setFlag(efOption_ClearOnNextSet
);
90 AbstractOptionStorage::~AbstractOptionStorage()
94 bool AbstractOptionStorage::isBoolean() const
96 return dynamic_cast<const BooleanOptionStorage
*>(this) != nullptr;
99 void AbstractOptionStorage::startSource()
101 setFlag(efOption_ClearOnNextSet
);
104 void AbstractOptionStorage::startSet()
106 GMX_RELEASE_ASSERT(!bInSet_
, "finishSet() not called");
107 // The last condition takes care of the situation where multiple
108 // sources are used, and a later source should be able to reassign
109 // the value even though the option is already set.
110 if (isSet() && !hasFlag(efOption_MultipleTimes
)
111 && !hasFlag(efOption_ClearOnNextSet
))
113 GMX_THROW(InvalidInputError("Option specified multiple times"));
117 bSetValuesHadErrors_
= false;
120 void AbstractOptionStorage::appendValue(const Variant
&value
)
122 GMX_RELEASE_ASSERT(bInSet_
, "startSet() not called");
127 catch (const std::exception
&)
129 bSetValuesHadErrors_
= true;
134 void AbstractOptionStorage::markAsSet()
136 setFlag(efOption_Set
);
137 if (storeIsSet_
!= nullptr)
143 void AbstractOptionStorage::finishSet()
145 GMX_RELEASE_ASSERT(bInSet_
, "startSet() not called");
147 // We mark the option as set even when there are errors to avoid additional
148 // errors from required options not set.
149 // TODO: There could be a separate flag for this purpose.
151 if (!bSetValuesHadErrors_
)
153 // TODO: Correct handling of the efOption_ClearOnNextSet requires
154 // processSet() and/or convertValue() to check it internally.
155 // OptionStorageTemplate takes care of it, but it's error-prone if
156 // a custom option is implemented that doesn't use it.
159 bSetValuesHadErrors_
= false;
160 clearFlag(efOption_ClearOnNextSet
);
164 void AbstractOptionStorage::finish()
166 GMX_RELEASE_ASSERT(!bInSet_
, "finishSet() not called");
168 if (isRequired() && !(isSet() || hasFlag(efOption_ExplicitDefaultValue
)))
170 GMX_THROW(InvalidInputError("Option is required, but not set"));
174 void AbstractOptionStorage::setMinValueCount(int count
)
176 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes
),
177 "setMinValueCount() not supported with efOption_MultipleTimes");
178 GMX_RELEASE_ASSERT(count
>= 0, "Invalid value count");
179 minValueCount_
= count
;
180 if (isSet() && !hasFlag(efOption_DontCheckMinimumCount
)
181 && valueCount() < minValueCount_
)
183 GMX_THROW(InvalidInputError("Too few values"));
187 void AbstractOptionStorage::setMaxValueCount(int count
)
189 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes
),
190 "setMaxValueCount() not supported with efOption_MultipleTimes");
191 GMX_RELEASE_ASSERT(count
>= -1, "Invalid value count");
192 maxValueCount_
= count
;
193 if (isSet() && maxValueCount_
>= 0 && valueCount() > maxValueCount_
)
195 GMX_THROW(InvalidInputError("Too many values"));
199 /********************************************************************
204 OptionInfo::OptionInfo(AbstractOptionStorage
*option
)
210 OptionInfo::~OptionInfo()
214 bool OptionInfo::isSet() const
216 return option().isSet();
219 bool OptionInfo::isHidden() const
221 return option().isHidden();
224 bool OptionInfo::isRequired() const
226 return option().isRequired();
229 int OptionInfo::minValueCount() const
231 if (option().defaultValueIfSetExists())
235 return option().minValueCount();
238 int OptionInfo::maxValueCount() const
240 return option().maxValueCount();
243 const std::string
&OptionInfo::name() const
245 return option().name();
248 std::string
OptionInfo::type() const
250 return option().typeString();
253 std::string
OptionInfo::formatDescription() const
255 std::string
description(option().description());
256 std::string
extraDescription(option().formatExtraDescription());
257 if (!extraDescription
.empty())
259 description
.append(extraDescription
);
264 std::vector
<Variant
> OptionInfo::defaultValues() const
266 return option().defaultValues();
269 std::vector
<std::string
> OptionInfo::defaultValuesAsStrings() const
271 return option().defaultValuesAsStrings();
274 std::vector
<Variant
> OptionInfo::normalizeValues(const std::vector
<Variant
> &values
) const
276 return option().normalizeValues(values
);