Update instructions in containers.rst
[gromacs.git] / src / gromacs / options / abstractoption.cpp
blobb2d060a1e6424e13d9604c18f01834465349c24e
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010-2017, The GROMACS development team.
5 * Copyright (c) 2019, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \internal \file
37 * \brief
38 * Implements classes in abstractoption.h and abstractoptionstorage.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_options
43 #include "gmxpre.h"
45 #include "abstractoption.h"
47 #include "gromacs/options/abstractoptionstorage.h"
48 #include "gromacs/utility/any.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/gmxassert.h"
52 #include "basicoptionstorage.h"
53 #include "optionflags.h"
55 namespace gmx
58 /********************************************************************
59 * AbstractOptionStorage
62 AbstractOptionStorage::AbstractOptionStorage(const AbstractOption& settings, OptionFlags staticFlags) :
63 flags_(settings.flags_ | staticFlags),
64 storeIsSet_(settings.storeIsSet_),
65 minValueCount_(settings.minValueCount_),
66 maxValueCount_(settings.maxValueCount_),
67 bInSet_(false),
68 bSetValuesHadErrors_(false)
70 // Check that user has not provided incorrect values for vectors.
71 if (hasFlag(efOption_Vector) && (minValueCount_ > 1 || maxValueCount_ < 1))
73 GMX_THROW(APIError("Inconsistent value counts for vector values"));
76 if (settings.name_ != nullptr)
78 name_ = settings.name_;
80 if (settings.descr_ != nullptr)
82 descr_ = settings.descr_;
84 if (storeIsSet_ != nullptr)
86 *storeIsSet_ = false;
88 setFlag(efOption_ClearOnNextSet);
91 AbstractOptionStorage::~AbstractOptionStorage() {}
93 bool AbstractOptionStorage::isBoolean() const
95 return dynamic_cast<const BooleanOptionStorage*>(this) != nullptr;
98 void AbstractOptionStorage::startSource()
100 setFlag(efOption_ClearOnNextSet);
103 void AbstractOptionStorage::startSet()
105 GMX_RELEASE_ASSERT(!bInSet_, "finishSet() not called");
106 // The last condition takes care of the situation where multiple
107 // sources are used, and a later source should be able to reassign
108 // the value even though the option is already set.
109 if (isSet() && !hasFlag(efOption_MultipleTimes) && !hasFlag(efOption_ClearOnNextSet))
111 GMX_THROW(InvalidInputError("Option specified multiple times"));
113 clearSet();
114 bInSet_ = true;
115 bSetValuesHadErrors_ = false;
118 void AbstractOptionStorage::appendValue(const Any& value)
120 GMX_RELEASE_ASSERT(bInSet_, "startSet() not called");
123 convertValue(value);
125 catch (const std::exception&)
127 bSetValuesHadErrors_ = true;
128 throw;
132 void AbstractOptionStorage::markAsSet()
134 setFlag(efOption_Set);
135 if (storeIsSet_ != nullptr)
137 *storeIsSet_ = true;
141 void AbstractOptionStorage::finishSet()
143 GMX_RELEASE_ASSERT(bInSet_, "startSet() not called");
144 bInSet_ = false;
145 // We mark the option as set even when there are errors to avoid additional
146 // errors from required options not set.
147 // TODO: There could be a separate flag for this purpose.
148 markAsSet();
149 if (!bSetValuesHadErrors_)
151 // TODO: Correct handling of the efOption_ClearOnNextSet requires
152 // processSet() and/or convertValue() to check it internally.
153 // OptionStorageTemplate takes care of it, but it's error-prone if
154 // a custom option is implemented that doesn't use it.
155 processSet();
157 bSetValuesHadErrors_ = false;
158 clearFlag(efOption_ClearOnNextSet);
159 clearSet();
162 void AbstractOptionStorage::finish()
164 GMX_RELEASE_ASSERT(!bInSet_, "finishSet() not called");
165 processAll();
166 if (isRequired() && !(isSet() || hasFlag(efOption_ExplicitDefaultValue)))
168 GMX_THROW(InvalidInputError("Option is required, but not set"));
172 void AbstractOptionStorage::setMinValueCount(int count)
174 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
175 "setMinValueCount() not supported with efOption_MultipleTimes");
176 GMX_RELEASE_ASSERT(count >= 0, "Invalid value count");
177 minValueCount_ = count;
178 if (isSet() && !hasFlag(efOption_DontCheckMinimumCount) && valueCount() < minValueCount_)
180 GMX_THROW(InvalidInputError("Too few values"));
184 void AbstractOptionStorage::setMaxValueCount(int count)
186 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
187 "setMaxValueCount() not supported with efOption_MultipleTimes");
188 GMX_RELEASE_ASSERT(count >= -1, "Invalid value count");
189 maxValueCount_ = count;
190 if (isSet() && maxValueCount_ >= 0 && valueCount() > maxValueCount_)
192 GMX_THROW(InvalidInputError("Too many values"));
196 /********************************************************************
197 * OptionInfo
200 /*! \cond libapi */
201 OptionInfo::OptionInfo(AbstractOptionStorage* option) : option_(*option) {}
202 //! \endcond
204 OptionInfo::~OptionInfo() {}
206 bool OptionInfo::isSet() const
208 return option().isSet();
211 bool OptionInfo::isHidden() const
213 return option().isHidden();
216 bool OptionInfo::isRequired() const
218 return option().isRequired();
221 int OptionInfo::minValueCount() const
223 if (option().defaultValueIfSetExists())
225 return 0;
227 return option().minValueCount();
230 int OptionInfo::maxValueCount() const
232 return option().maxValueCount();
235 const std::string& OptionInfo::name() const
237 return option().name();
240 std::string OptionInfo::type() const
242 return option().typeString();
245 std::string OptionInfo::formatDescription() const
247 std::string description(option().description());
248 std::string extraDescription(option().formatExtraDescription());
249 if (!extraDescription.empty())
251 description.append(extraDescription);
253 return description;
256 std::vector<Any> OptionInfo::defaultValues() const
258 return option().defaultValues();
261 std::vector<std::string> OptionInfo::defaultValuesAsStrings() const
263 return option().defaultValuesAsStrings();
266 std::vector<Any> OptionInfo::normalizeValues(const std::vector<Any>& values) const
268 return option().normalizeValues(values);
271 } // namespace gmx