Rename gmx Variant to Any
[alexxy/gromacs.git] / src / gromacs / options / optionsassigner.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements gmx::OptionsAssigner.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_options
41  */
42 #include "gmxpre.h"
43
44 #include "optionsassigner.h"
45
46 #include <deque>
47
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"
53
54 #include "options-impl.h"
55
56 namespace gmx
57 {
58
59 /********************************************************************
60  * OptionsAssigner::Impl
61  */
62
63 /*! \internal \brief
64  * Private implementation class for OptionsAssigner.
65  *
66  * \ingroup module_options
67  */
68 class OptionsAssigner::Impl
69 {
70     public:
71         //! Shorthand for the internal type used to represent a section.
72         typedef internal::OptionSectionImpl Section;
73
74         //! Sets the option object to assign to.
75         explicit Impl(Options *options);
76
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 &currentSection() const { return *sectionStack_.back(); }
81         /*! \brief
82          * Finds an option by the given name.
83          *
84          * \param[in] name  Name of the option to look for.
85          * \returns Pointer to the found option, or NULL if none found.
86          *
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.
90          */
91         AbstractOptionStorage *findOption(const char *name);
92
93         //! Options object to assign to.
94         Options                &options_;
95         //! Recognize boolean option "name" also as "noname".
96         bool                    bAcceptBooleanNoPrefix_;
97         /*! \brief
98          * List of (sub)sections being assigned to.
99          *
100          * The first element always points to \a options_.
101          */
102         std::vector<Section *>  sectionStack_;
103         //! Current option being assigned to, or NULL if none.
104         AbstractOptionStorage  *currentOption_;
105         /*! \brief
106          * Number of values assigned so far to the current option.
107          *
108          * Counts the number of attempted assignments, whether they have been
109          * successful or not.
110          */
111         int                     currentValueCount_;
112         //! If true, a "no" prefix was given for the current boolean option.
113         bool                    reverseBoolean_;
114 };
115
116 OptionsAssigner::Impl::Impl(Options *options)
117     : options_(*options), bAcceptBooleanNoPrefix_(false),
118       currentOption_(nullptr), currentValueCount_(0), reverseBoolean_(false)
119 {
120     sectionStack_.push_back(&options_.impl_->rootSection_);
121 }
122
123 AbstractOptionStorage *
124 OptionsAssigner::Impl::findOption(const char *name)
125 {
126     GMX_RELEASE_ASSERT(currentOption_ == nullptr,
127                        "Cannot search for another option while processing one");
128     const Section         &section = currentSection();
129     AbstractOptionStorage *option  = section.findOption(name);
130     if (option == nullptr && bAcceptBooleanNoPrefix_)
131     {
132         if (name[0] == 'n' && name[1] == 'o')
133         {
134             option = section.findOption(name + 2);
135             if (option != nullptr && option->isBoolean())
136             {
137                 reverseBoolean_ = true;
138             }
139             else
140             {
141                 option = nullptr;
142             }
143         }
144     }
145     return option;
146 }
147
148 /********************************************************************
149  * OptionsAssigner
150  */
151
152 OptionsAssigner::OptionsAssigner(Options *options)
153     : impl_(new Impl(options))
154 {
155 }
156
157 OptionsAssigner::~OptionsAssigner()
158 {
159 }
160
161 void OptionsAssigner::setAcceptBooleanNoPrefix(bool bEnabled)
162 {
163     impl_->bAcceptBooleanNoPrefix_ = bEnabled;
164 }
165
166 void OptionsAssigner::start()
167 {
168     impl_->options_.impl_->rootSection_.start();
169 }
170
171 void OptionsAssigner::startSection(const char *name)
172 {
173     Impl::Section *section = impl_->currentSection().findSection(name);
174     if (section == nullptr)
175     {
176         GMX_THROW(InvalidInputError("Unknown subsection"));
177     }
178     impl_->sectionStack_.push_back(section);
179     section->start();
180 }
181
182 void OptionsAssigner::startOption(const char *name)
183 {
184     if (!tryStartOption(name))
185     {
186         GMX_THROW(InvalidInputError("Unknown option " + std::string(name)));
187     }
188 }
189
190 bool OptionsAssigner::tryStartOption(const char *name)
191 {
192     GMX_RELEASE_ASSERT(impl_->currentOption_ == nullptr, "finishOption() not called");
193     AbstractOptionStorage *option = impl_->findOption(name);
194     if (option == nullptr)
195     {
196         return false;
197     }
198     option->startSet();
199     impl_->currentOption_     = option;
200     impl_->currentValueCount_ = 0;
201     return true;
202 }
203
204 void OptionsAssigner::appendValue(const std::string &value)
205 {
206     appendValue(Any(value));
207 }
208
209 void OptionsAssigner::appendValue(const Any &value)
210 {
211     AbstractOptionStorage *option = impl_->currentOption_;
212     GMX_RELEASE_ASSERT(option != nullptr, "startOption() not called");
213     ++impl_->currentValueCount_;
214     option->appendValue(value);
215 }
216
217 void OptionsAssigner::finishOption()
218 {
219     AbstractOptionStorage *option = impl_->currentOption_;
220     GMX_RELEASE_ASSERT(option != nullptr, "startOption() not called");
221     bool                   bBoolReverseValue = false;
222     if (option->isBoolean())
223     {
224         if (impl_->currentValueCount_ == 0)
225         {
226             // Should not throw, otherwise something is wrong.
227             option->appendValue(Any::create<bool>(!impl_->reverseBoolean_));
228         }
229         else if (impl_->reverseBoolean_)
230         {
231             bBoolReverseValue = true;
232         }
233     }
234     impl_->currentOption_  = nullptr;
235     impl_->reverseBoolean_ = false;
236     option->finishSet();
237     if (bBoolReverseValue)
238     {
239         GMX_THROW(InvalidInputError("Cannot specify a value together with 'no' prefix"));
240     }
241 }
242
243 void OptionsAssigner::finishSection()
244 {
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();
248     section->finish();
249     impl_->sectionStack_.pop_back();
250 }
251
252 void OptionsAssigner::finish()
253 {
254     GMX_RELEASE_ASSERT(impl_->currentOption_ == nullptr, "finishOption() not called");
255     GMX_RELEASE_ASSERT(!impl_->inSection(), "finishSection() not called");
256 }
257
258 } // namespace gmx