Sort all includes in src/gromacs
[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, 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/exceptions.h"
51 #include "gromacs/utility/gmxassert.h"
52
53 #include "options-impl.h"
54
55 namespace gmx
56 {
57
58 /********************************************************************
59  * OptionsAssigner::Impl
60  */
61
62 /*! \internal \brief
63  * Private implementation class for OptionsAssigner.
64  *
65  * \ingroup module_options
66  */
67 class OptionsAssigner::Impl
68 {
69     public:
70         //! Sets the option object to assign to.
71         explicit Impl(Options *options);
72
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 &currentSection() const { return *sectionStack_.back(); }
77         /*! \brief
78          * Finds an option by the given name.
79          *
80          * \param[in] name  Name of the option to look for.
81          * \returns Pointer to the found option, or NULL if none found.
82          *
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.
86          */
87         AbstractOptionStorage *findOption(const char *name);
88
89         //! Options object to assign to.
90         Options                &options_;
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_;
95         /*! \brief
96          * List of (sub)sections being assigned to.
97          *
98          * The first element always points to \a options_.
99          */
100         std::vector<Options *>  sectionStack_;
101         //! Current option being assigned to, or NULL if none.
102         AbstractOptionStorage  *currentOption_;
103         /*! \brief
104          * Number of values assigned so far to the current option.
105          *
106          * Counts the number of attempted assignments, whether they have been
107          * successful or not.
108          */
109         int                     currentValueCount_;
110         //! If true, a "no" prefix was given for the current boolean option.
111         bool                    reverseBoolean_;
112 };
113
114 OptionsAssigner::Impl::Impl(Options *options)
115     : options_(*options), bAcceptBooleanNoPrefix_(false),
116       bNoStrictSectioning_(false), currentOption_(NULL),
117       currentValueCount_(0), reverseBoolean_(false)
118 {
119     sectionStack_.push_back(&options_);
120 }
121
122 AbstractOptionStorage *
123 OptionsAssigner::Impl::findOption(const char *name)
124 {
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    = &currentSection();
130     Options               *oldRoot = NULL;
131     int                    upcount = 0;
132     std::deque<Options *>  searchList;
133     searchList.push_back(root);
134     while (option == NULL && !searchList.empty())
135     {
136         section = searchList.front();
137         option  = section->impl_->findOption(name);
138         if (option == NULL && bAcceptBooleanNoPrefix_)
139         {
140             if (name[0] == 'n' && name[1] == 'o')
141             {
142                 option = section->impl_->findOption(name + 2);
143                 if (option != NULL && option->isBoolean())
144                 {
145                     reverseBoolean_ = true;
146                 }
147                 else
148                 {
149                     option = NULL;
150                 }
151             }
152         }
153         searchList.pop_front();
154         if (bNoStrictSectioning_)
155         {
156             Options::Impl::SubSectionList::const_iterator i;
157             for (i = section->impl_->subSections_.begin();
158                  i != section->impl_->subSections_.end(); ++i)
159             {
160                 if (*i != oldRoot)
161                 {
162                     searchList.push_back(*i);
163                 }
164             }
165             if (searchList.empty() && root != &options_)
166             {
167                 root = root->impl_->parent_;
168                 ++upcount;
169                 searchList.push_back(root);
170             }
171         }
172     }
173     if (bNoStrictSectioning_ && option != NULL)
174     {
175         while (upcount > 0)
176         {
177             sectionStack_.pop_back();
178             --upcount;
179         }
180         std::vector<Options *> sections;
181         while (section != &currentSection())
182         {
183             sections.push_back(section);
184             section = section->impl_->parent_;
185         }
186         while (!sections.empty())
187         {
188             sectionStack_.push_back(sections.back());
189             sections.pop_back();
190         }
191     }
192     return option;
193 }
194
195 /********************************************************************
196  * OptionsAssigner
197  */
198
199 OptionsAssigner::OptionsAssigner(Options *options)
200     : impl_(new Impl(options))
201 {
202 }
203
204 OptionsAssigner::~OptionsAssigner()
205 {
206 }
207
208 void OptionsAssigner::setAcceptBooleanNoPrefix(bool bEnabled)
209 {
210     impl_->bAcceptBooleanNoPrefix_ = bEnabled;
211 }
212
213 void OptionsAssigner::setNoStrictSectioning(bool bEnabled)
214 {
215     impl_->bNoStrictSectioning_ = bEnabled;
216 }
217
218 void OptionsAssigner::start()
219 {
220     impl_->options_.impl_->startSource();
221 }
222
223 void OptionsAssigner::startSubSection(const char *name)
224 {
225     Options *section = impl_->currentSection().impl_->findSubSection(name);
226     if (section == NULL)
227     {
228         GMX_THROW(InvalidInputError("Unknown subsection"));
229     }
230     impl_->sectionStack_.push_back(section);
231 }
232
233 void OptionsAssigner::startOption(const char *name)
234 {
235     if (!tryStartOption(name))
236     {
237         GMX_THROW(InvalidInputError("Unknown option " + std::string(name)));
238     }
239 }
240
241 bool OptionsAssigner::tryStartOption(const char *name)
242 {
243     GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
244     AbstractOptionStorage *option = impl_->findOption(name);
245     if (option == NULL)
246     {
247         return false;
248     }
249     option->startSet();
250     impl_->currentOption_     = option;
251     impl_->currentValueCount_ = 0;
252     return true;
253 }
254
255 void OptionsAssigner::appendValue(const std::string &value)
256 {
257     AbstractOptionStorage *option = impl_->currentOption_;
258     GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
259     ++impl_->currentValueCount_;
260     option->appendValue(value);
261 }
262
263 void OptionsAssigner::finishOption()
264 {
265     AbstractOptionStorage *option = impl_->currentOption_;
266     GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
267     bool                   bBoolReverseValue = false;
268     if (option->isBoolean())
269     {
270         if (impl_->currentValueCount_ == 0)
271         {
272             // Should not throw, otherwise something is wrong.
273             // TODO: Get rid of the hard-coded values.
274             option->appendValue(impl_->reverseBoolean_ ? "0" : "1");
275         }
276         else if (impl_->reverseBoolean_)
277         {
278             bBoolReverseValue = true;
279         }
280     }
281     impl_->currentOption_  = NULL;
282     impl_->reverseBoolean_ = false;
283     option->finishSet();
284     if (bBoolReverseValue)
285     {
286         GMX_THROW(InvalidInputError("Cannot specify a value together with 'no' prefix"));
287     }
288 }
289
290 void OptionsAssigner::finishSubSection()
291 {
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();
295 }
296
297 void OptionsAssigner::finish()
298 {
299     GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
300     if (impl_->bNoStrictSectioning_)
301     {
302         while (impl_->inSubSection())
303         {
304             finishSubSection();
305         }
306     }
307     GMX_RELEASE_ASSERT(!impl_->inSubSection(), "finishSubSection() not called");
308 }
309
310 } // namespace gmx