2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014, 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_;
93 //! Look for options in all sections, not just the current one.
94 bool bNoStrictSectioning_;
96 * List of (sub)sections being assigned to.
98 * The first element always points to \a options_.
100 std::vector<Options *> sectionStack_;
101 //! Current option being assigned to, or NULL if none.
102 AbstractOptionStorage *currentOption_;
104 * Number of values assigned so far to the current option.
106 * Counts the number of attempted assignments, whether they have been
109 int currentValueCount_;
110 //! If true, a "no" prefix was given for the current boolean option.
111 bool reverseBoolean_;
114 OptionsAssigner::Impl::Impl(Options *options)
115 : options_(*options), bAcceptBooleanNoPrefix_(false),
116 bNoStrictSectioning_(false), currentOption_(NULL),
117 currentValueCount_(0), reverseBoolean_(false)
119 sectionStack_.push_back(&options_);
122 AbstractOptionStorage *
123 OptionsAssigner::Impl::findOption(const char *name)
125 GMX_RELEASE_ASSERT(currentOption_ == NULL,
126 "Cannot search for another option while processing one");
127 AbstractOptionStorage *option = NULL;
128 Options *section = NULL;
129 Options *root = ¤tSection();
130 Options *oldRoot = NULL;
132 std::deque<Options *> searchList;
133 searchList.push_back(root);
134 while (option == NULL && !searchList.empty())
136 section = searchList.front();
137 option = section->impl_->findOption(name);
138 if (option == NULL && bAcceptBooleanNoPrefix_)
140 if (name[0] == 'n' && name[1] == 'o')
142 option = section->impl_->findOption(name + 2);
143 if (option != NULL && option->isBoolean())
145 reverseBoolean_ = true;
153 searchList.pop_front();
154 if (bNoStrictSectioning_)
156 Options::Impl::SubSectionList::const_iterator i;
157 for (i = section->impl_->subSections_.begin();
158 i != section->impl_->subSections_.end(); ++i)
162 searchList.push_back(*i);
165 if (searchList.empty() && root != &options_)
167 root = root->impl_->parent_;
169 searchList.push_back(root);
173 if (bNoStrictSectioning_ && option != NULL)
177 sectionStack_.pop_back();
180 std::vector<Options *> sections;
181 while (section != ¤tSection())
183 sections.push_back(section);
184 section = section->impl_->parent_;
186 while (!sections.empty())
188 sectionStack_.push_back(sections.back());
195 /********************************************************************
199 OptionsAssigner::OptionsAssigner(Options *options)
200 : impl_(new Impl(options))
204 OptionsAssigner::~OptionsAssigner()
208 void OptionsAssigner::setAcceptBooleanNoPrefix(bool bEnabled)
210 impl_->bAcceptBooleanNoPrefix_ = bEnabled;
213 void OptionsAssigner::setNoStrictSectioning(bool bEnabled)
215 impl_->bNoStrictSectioning_ = bEnabled;
218 void OptionsAssigner::start()
220 impl_->options_.impl_->startSource();
223 void OptionsAssigner::startSubSection(const char *name)
225 Options *section = impl_->currentSection().impl_->findSubSection(name);
228 GMX_THROW(InvalidInputError("Unknown subsection"));
230 impl_->sectionStack_.push_back(section);
233 void OptionsAssigner::startOption(const char *name)
235 if (!tryStartOption(name))
237 GMX_THROW(InvalidInputError("Unknown option " + std::string(name)));
241 bool OptionsAssigner::tryStartOption(const char *name)
243 GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
244 AbstractOptionStorage *option = impl_->findOption(name);
250 impl_->currentOption_ = option;
251 impl_->currentValueCount_ = 0;
255 void OptionsAssigner::appendValue(const std::string &value)
257 AbstractOptionStorage *option = impl_->currentOption_;
258 GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
259 ++impl_->currentValueCount_;
260 option->appendValue(value);
263 void OptionsAssigner::finishOption()
265 AbstractOptionStorage *option = impl_->currentOption_;
266 GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
267 bool bBoolReverseValue = false;
268 if (option->isBoolean())
270 if (impl_->currentValueCount_ == 0)
272 // Should not throw, otherwise something is wrong.
273 // TODO: Get rid of the hard-coded values.
274 option->appendValue(impl_->reverseBoolean_ ? "0" : "1");
276 else if (impl_->reverseBoolean_)
278 bBoolReverseValue = true;
281 impl_->currentOption_ = NULL;
282 impl_->reverseBoolean_ = false;
284 if (bBoolReverseValue)
286 GMX_THROW(InvalidInputError("Cannot specify a value together with 'no' prefix"));
290 void OptionsAssigner::finishSubSection()
292 // Should only be called if we are in a subsection.
293 GMX_RELEASE_ASSERT(impl_->inSubSection(), "startSubSection() not called");
294 impl_->sectionStack_.pop_back();
297 void OptionsAssigner::finish()
299 GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
300 if (impl_->bNoStrictSectioning_)
302 while (impl_->inSubSection())
307 GMX_RELEASE_ASSERT(!impl_->inSubSection(), "finishSubSection() not called");