Add basic grouping to Options
[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, 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         /*! \brief
94          * List of (sub)sections being assigned to.
95          *
96          * The first element always points to \a options_.
97          */
98         std::vector<Options *>  sectionStack_;
99         //! Current option being assigned to, or NULL if none.
100         AbstractOptionStorage  *currentOption_;
101         /*! \brief
102          * Number of values assigned so far to the current option.
103          *
104          * Counts the number of attempted assignments, whether they have been
105          * successful or not.
106          */
107         int                     currentValueCount_;
108         //! If true, a "no" prefix was given for the current boolean option.
109         bool                    reverseBoolean_;
110 };
111
112 OptionsAssigner::Impl::Impl(Options *options)
113     : options_(*options), bAcceptBooleanNoPrefix_(false),
114       currentOption_(NULL), currentValueCount_(0), reverseBoolean_(false)
115 {
116     sectionStack_.push_back(&options_);
117 }
118
119 AbstractOptionStorage *
120 OptionsAssigner::Impl::findOption(const char *name)
121 {
122     GMX_RELEASE_ASSERT(currentOption_ == NULL,
123                        "Cannot search for another option while processing one");
124     const Options         &section = currentSection();
125     AbstractOptionStorage *option  = section.impl_->findOption(name);
126     if (option == NULL && bAcceptBooleanNoPrefix_)
127     {
128         if (name[0] == 'n' && name[1] == 'o')
129         {
130             option = section.impl_->findOption(name + 2);
131             if (option != NULL && option->isBoolean())
132             {
133                 reverseBoolean_ = true;
134             }
135             else
136             {
137                 option = NULL;
138             }
139         }
140     }
141     return option;
142 }
143
144 /********************************************************************
145  * OptionsAssigner
146  */
147
148 OptionsAssigner::OptionsAssigner(Options *options)
149     : impl_(new Impl(options))
150 {
151 }
152
153 OptionsAssigner::~OptionsAssigner()
154 {
155 }
156
157 void OptionsAssigner::setAcceptBooleanNoPrefix(bool bEnabled)
158 {
159     impl_->bAcceptBooleanNoPrefix_ = bEnabled;
160 }
161
162 void OptionsAssigner::start()
163 {
164     impl_->options_.impl_->startSource();
165 }
166
167 void OptionsAssigner::startSubSection(const char *name)
168 {
169     Options *section = impl_->currentSection().impl_->findSubSection(name);
170     if (section == NULL)
171     {
172         GMX_THROW(InvalidInputError("Unknown subsection"));
173     }
174     impl_->sectionStack_.push_back(section);
175 }
176
177 void OptionsAssigner::startOption(const char *name)
178 {
179     if (!tryStartOption(name))
180     {
181         GMX_THROW(InvalidInputError("Unknown option " + std::string(name)));
182     }
183 }
184
185 bool OptionsAssigner::tryStartOption(const char *name)
186 {
187     GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
188     AbstractOptionStorage *option = impl_->findOption(name);
189     if (option == NULL)
190     {
191         return false;
192     }
193     option->startSet();
194     impl_->currentOption_     = option;
195     impl_->currentValueCount_ = 0;
196     return true;
197 }
198
199 void OptionsAssigner::appendValue(const std::string &value)
200 {
201     AbstractOptionStorage *option = impl_->currentOption_;
202     GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
203     ++impl_->currentValueCount_;
204     option->appendValue(value);
205 }
206
207 void OptionsAssigner::finishOption()
208 {
209     AbstractOptionStorage *option = impl_->currentOption_;
210     GMX_RELEASE_ASSERT(option != NULL, "startOption() not called");
211     bool                   bBoolReverseValue = false;
212     if (option->isBoolean())
213     {
214         if (impl_->currentValueCount_ == 0)
215         {
216             // Should not throw, otherwise something is wrong.
217             // TODO: Get rid of the hard-coded values.
218             option->appendValue(impl_->reverseBoolean_ ? "0" : "1");
219         }
220         else if (impl_->reverseBoolean_)
221         {
222             bBoolReverseValue = true;
223         }
224     }
225     impl_->currentOption_  = NULL;
226     impl_->reverseBoolean_ = false;
227     option->finishSet();
228     if (bBoolReverseValue)
229     {
230         GMX_THROW(InvalidInputError("Cannot specify a value together with 'no' prefix"));
231     }
232 }
233
234 void OptionsAssigner::finishSubSection()
235 {
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();
239 }
240
241 void OptionsAssigner::finish()
242 {
243     GMX_RELEASE_ASSERT(impl_->currentOption_ == NULL, "finishOption() not called");
244     GMX_RELEASE_ASSERT(!impl_->inSubSection(), "finishSubSection() not called");
245 }
246
247 } // namespace gmx