2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017,2019, 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 gmx::OptionsAssigner.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
44 #include "optionsassigner.h"
48 #include "gromacs/options/abstractoptionstorage.h"
49 #include "gromacs/options/options.h"
50 #include "gromacs/utility/any.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/gmxassert.h"
54 #include "options-impl.h"
59 /********************************************************************
60 * OptionsAssigner::Impl
64 * Private implementation class for OptionsAssigner.
66 * \ingroup module_options
68 class OptionsAssigner::Impl
71 //! Shorthand for the internal type used to represent a section.
72 typedef internal::OptionSectionImpl Section;
74 //! Sets the option object to assign to.
75 explicit Impl(Options *options);
77 //! Returns true if a subsection has been set.
78 bool inSection() const { return sectionStack_.size() > 1; }
79 //! Returns the Options object for the current section.
80 Section ¤tSection() const { return *sectionStack_.back(); }
82 * Finds an option by the given name.
84 * \param[in] name Name of the option to look for.
85 * \returns Pointer to the found option, or NULL if none found.
87 * This function takes into account the flags specified, and may change
88 * the internal state of the assigner to match the option found.
89 * If no option is found, the internal state is not modified.
91 AbstractOptionStorage *findOption(const char *name);
93 //! Options object to assign to.
95 //! Recognize boolean option "name" also as "noname".
96 bool bAcceptBooleanNoPrefix_;
98 * List of (sub)sections being assigned to.
100 * The first element always points to \a options_.
102 std::vector<Section *> sectionStack_;
103 //! Current option being assigned to, or NULL if none.
104 AbstractOptionStorage *currentOption_;
106 * Number of values assigned so far to the current option.
108 * Counts the number of attempted assignments, whether they have been
111 int currentValueCount_;
112 //! If true, a "no" prefix was given for the current boolean option.
113 bool reverseBoolean_;
116 OptionsAssigner::Impl::Impl(Options *options)
117 : options_(*options), bAcceptBooleanNoPrefix_(false),
118 currentOption_(nullptr), currentValueCount_(0), reverseBoolean_(false)
120 sectionStack_.push_back(&options_.impl_->rootSection_);
123 AbstractOptionStorage *
124 OptionsAssigner::Impl::findOption(const char *name)
126 GMX_RELEASE_ASSERT(currentOption_ == nullptr,
127 "Cannot search for another option while processing one");
128 const Section §ion = currentSection();
129 AbstractOptionStorage *option = section.findOption(name);
130 if (option == nullptr && bAcceptBooleanNoPrefix_)
132 if (name[0] == 'n' && name[1] == 'o')
134 option = section.findOption(name + 2);
135 if (option != nullptr && option->isBoolean())
137 reverseBoolean_ = true;
148 /********************************************************************
152 OptionsAssigner::OptionsAssigner(Options *options)
153 : impl_(new Impl(options))
157 OptionsAssigner::~OptionsAssigner()
161 void OptionsAssigner::setAcceptBooleanNoPrefix(bool bEnabled)
163 impl_->bAcceptBooleanNoPrefix_ = bEnabled;
166 void OptionsAssigner::start()
168 impl_->options_.impl_->rootSection_.start();
171 void OptionsAssigner::startSection(const char *name)
173 Impl::Section *section = impl_->currentSection().findSection(name);
174 if (section == nullptr)
176 GMX_THROW(InvalidInputError("Unknown subsection"));
178 impl_->sectionStack_.push_back(section);
182 void OptionsAssigner::startOption(const char *name)
184 if (!tryStartOption(name))
186 GMX_THROW(InvalidInputError("Unknown option " + std::string(name)));
190 bool OptionsAssigner::tryStartOption(const char *name)
192 GMX_RELEASE_ASSERT(impl_->currentOption_ == nullptr, "finishOption() not called");
193 AbstractOptionStorage *option = impl_->findOption(name);
194 if (option == nullptr)
199 impl_->currentOption_ = option;
200 impl_->currentValueCount_ = 0;
204 void OptionsAssigner::appendValue(const std::string &value)
206 appendValue(Any(value));
209 void OptionsAssigner::appendValue(const Any &value)
211 AbstractOptionStorage *option = impl_->currentOption_;
212 GMX_RELEASE_ASSERT(option != nullptr, "startOption() not called");
213 ++impl_->currentValueCount_;
214 option->appendValue(value);
217 void OptionsAssigner::finishOption()
219 AbstractOptionStorage *option = impl_->currentOption_;
220 GMX_RELEASE_ASSERT(option != nullptr, "startOption() not called");
221 bool bBoolReverseValue = false;
222 if (option->isBoolean())
224 if (impl_->currentValueCount_ == 0)
226 // Should not throw, otherwise something is wrong.
227 option->appendValue(Any::create<bool>(!impl_->reverseBoolean_));
229 else if (impl_->reverseBoolean_)
231 bBoolReverseValue = true;
234 impl_->currentOption_ = nullptr;
235 impl_->reverseBoolean_ = false;
237 if (bBoolReverseValue)
239 GMX_THROW(InvalidInputError("Cannot specify a value together with 'no' prefix"));
243 void OptionsAssigner::finishSection()
245 // Should only be called if we are in a subsection.
246 GMX_RELEASE_ASSERT(impl_->inSection(), "startSection() not called");
247 Impl::Section *section = impl_->sectionStack_.back();
249 impl_->sectionStack_.pop_back();
252 void OptionsAssigner::finish()
254 GMX_RELEASE_ASSERT(impl_->currentOption_ == nullptr, "finishOption() not called");
255 GMX_RELEASE_ASSERT(!impl_->inSection(), "finishSection() not called");