2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014,2015, 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/exceptions.h"
51 #include "gromacs/utility/gmxassert.h"
53 #include "options-impl.h"
58 /********************************************************************
59 * OptionsAssigner::Impl
63 * Private implementation class for OptionsAssigner.
65 * \ingroup module_options
67 class OptionsAssigner::Impl
70 //! Sets the option object to assign to.
71 explicit Impl(Options *options);
73 //! Returns true if a subsection has been set.
74 bool inSubSection() const { return sectionStack_.size() > 1; }
75 //! Returns the Options object for the current section.
76 Options ¤tSection() const { return *sectionStack_.back(); }
78 * Finds an option by the given name.
80 * \param[in] name Name of the option to look for.
81 * \returns Pointer to the found option, or NULL if none found.
83 * This function takes into account the flags specified, and may change
84 * the internal state of the assigner to match the option found.
85 * If no option is found, the internal state is not modified.
87 AbstractOptionStorage *findOption(const char *name);
89 //! Options object to assign to.
91 //! Recognize boolean option "name" also as "noname".
92 bool bAcceptBooleanNoPrefix_;
94 * List of (sub)sections being assigned to.
96 * The first element always points to \a options_.
98 std::vector<Options *> sectionStack_;
99 //! Current option being assigned to, or NULL if none.
100 AbstractOptionStorage *currentOption_;
102 * Number of values assigned so far to the current option.
104 * Counts the number of attempted assignments, whether they have been
107 int currentValueCount_;
108 //! If true, a "no" prefix was given for the current boolean option.
109 bool reverseBoolean_;
112 OptionsAssigner::Impl::Impl(Options *options)
113 : options_(*options), bAcceptBooleanNoPrefix_(false),
114 currentOption_(NULL), currentValueCount_(0), reverseBoolean_(false)
116 sectionStack_.push_back(&options_);
119 AbstractOptionStorage *
120 OptionsAssigner::Impl::findOption(const char *name)
122 GMX_RELEASE_ASSERT(currentOption_ == NULL,
123 "Cannot search for another option while processing one");
124 const Options §ion = currentSection();
125 AbstractOptionStorage *option = section.impl_->findOption(name);
126 if (option == NULL && bAcceptBooleanNoPrefix_)
128 if (name[0] == 'n' && name[1] == 'o')
130 option = section.impl_->findOption(name + 2);
131 if (option != NULL && option->isBoolean())
133 reverseBoolean_ = true;
144 /********************************************************************
148 OptionsAssigner::OptionsAssigner(Options *options)
149 : impl_(new Impl(options))
153 OptionsAssigner::~OptionsAssigner()
157 void OptionsAssigner::setAcceptBooleanNoPrefix(bool bEnabled)
159 impl_->bAcceptBooleanNoPrefix_ = bEnabled;
162 void OptionsAssigner::start()
164 impl_->options_.impl_->startSource();
167 void OptionsAssigner::startSubSection(const char *name)
169 Options *section = impl_->currentSection().impl_->findSubSection(name);
172 GMX_THROW(InvalidInputError("Unknown subsection"));
174 impl_->sectionStack_.push_back(section);
177 void OptionsAssigner::startOption(const char *name)
179 if (!tryStartOption(name))
181 GMX_THROW(InvalidInputError("Unknown option " + std::string(name)));
185 bool OptionsAssigner::tryStartOption(const char *name)
187 GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
188 AbstractOptionStorage *option = impl_->findOption(name);
194 impl_->currentOption_ = option;
195 impl_->currentValueCount_ = 0;
199 void OptionsAssigner::appendValue(const std::string &value)
201 AbstractOptionStorage *option = impl_->currentOption_;
202 GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
203 ++impl_->currentValueCount_;
204 option->appendValue(value);
207 void OptionsAssigner::finishOption()
209 AbstractOptionStorage *option = impl_->currentOption_;
210 GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
211 bool bBoolReverseValue = false;
212 if (option->isBoolean())
214 if (impl_->currentValueCount_ == 0)
216 // Should not throw, otherwise something is wrong.
217 // TODO: Get rid of the hard-coded values.
218 option->appendValue(impl_->reverseBoolean_ ? "0" : "1");
220 else if (impl_->reverseBoolean_)
222 bBoolReverseValue = true;
225 impl_->currentOption_ = NULL;
226 impl_->reverseBoolean_ = false;
228 if (bBoolReverseValue)
230 GMX_THROW(InvalidInputError("Cannot specify a value together with 'no' prefix"));
234 void OptionsAssigner::finishSubSection()
236 // Should only be called if we are in a subsection.
237 GMX_RELEASE_ASSERT(impl_->inSubSection(), "startSubSection() not called");
238 impl_->sectionStack_.pop_back();
241 void OptionsAssigner::finish()
243 GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
244 GMX_RELEASE_ASSERT(!impl_->inSubSection(), "finishSubSection() not called");