97dabb99d699a9cd26528811762ceaaeb8eceef6
[alexxy/gromacs.git] / src / gromacs / options / options.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,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::Options.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_options
41  */
42 #include "gmxpre.h"
43
44 #include "options.h"
45
46 #include <utility>
47
48 #include "gromacs/options/abstractoption.h"
49 #include "gromacs/options/abstractoptionstorage.h"
50 #include "gromacs/utility/arrayref.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/gmxassert.h"
53 #include "gromacs/utility/stringutil.h"
54
55 #include "options-impl.h"
56
57 namespace gmx
58 {
59
60 /********************************************************************
61  * IOptionManager
62  */
63
64 IOptionManager::~IOptionManager()
65 {
66 }
67
68 /********************************************************************
69  * IOptionsContainer
70  */
71
72 IOptionsContainer::~IOptionsContainer()
73 {
74 }
75
76 /********************************************************************
77  * OptionsImpl
78  */
79
80 namespace internal
81 {
82
83 OptionsImpl::OptionsImpl(const char *name, const char * /*title*/)
84     : name_(name != NULL ? name : ""), rootGroup_(this),
85       parent_(NULL)
86 {
87 }
88
89 Options *OptionsImpl::findSubSection(const char *name) const
90 {
91     SubSectionList::const_iterator i;
92     for (i = subSections_.begin(); i != subSections_.end(); ++i)
93     {
94         if ((*i)->name() == name)
95         {
96             return *i;
97         }
98     }
99     return NULL;
100 }
101
102 AbstractOptionStorage *OptionsImpl::findOption(const char *name) const
103 {
104     OptionMap::const_iterator i = optionMap_.find(name);
105     if (i == optionMap_.end())
106     {
107         return NULL;
108     }
109     return i->second.get();
110 }
111
112 void OptionsImpl::startSource()
113 {
114     OptionMap::const_iterator i;
115     for (i = optionMap_.begin(); i != optionMap_.end(); ++i)
116     {
117         AbstractOptionStorage &option = *i->second;
118         option.startSource();
119     }
120     SubSectionList::const_iterator j;
121     for (j = subSections_.begin(); j != subSections_.end(); ++j)
122     {
123         Options &section = **j;
124         section.impl_->startSource();
125     }
126 }
127
128 /********************************************************************
129  * OptionsImpl::Group
130  */
131
132 IOptionsContainer &OptionsImpl::Group::addGroup()
133 {
134     subgroups_.push_back(Group(parent_));
135     return subgroups_.back();
136 }
137
138 OptionInfo *OptionsImpl::Group::addOption(const AbstractOption &settings)
139 {
140     OptionsImpl *root = parent_;
141     while (root->parent_ != NULL)
142     {
143         root = root->parent_->impl_.get();
144     }
145     AbstractOptionStoragePointer         option(settings.createStorage(root->managers_));
146     options_.reserve(options_.size() + 1);
147     std::pair<OptionMap::iterator, bool> insertionResult =
148         parent_->optionMap_.insert(std::make_pair(option->name(),
149                                                   move(option)));
150     if (!insertionResult.second)
151     {
152         GMX_THROW(APIError("Duplicate option: " + option->name()));
153     }
154     AbstractOptionStorage &insertedOption = *insertionResult.first->second;
155     options_.push_back(&insertedOption);
156     return &insertedOption.optionInfo();
157 }
158
159 }   // namespace internal
160
161 using internal::OptionsImpl;
162
163 /********************************************************************
164  * Options
165  */
166
167 Options::Options(const char *name, const char *title)
168     : impl_(new OptionsImpl(name, title))
169 {
170 }
171
172 Options::~Options()
173 {
174 }
175
176 const std::string &Options::name() const
177 {
178     return impl_->name_;
179 }
180
181
182 void Options::addManager(IOptionManager *manager)
183 {
184     GMX_RELEASE_ASSERT(impl_->parent_ == NULL,
185                        "Can only add a manager in a top-level Options object");
186     // This ensures that all options see the same set of managers.
187     GMX_RELEASE_ASSERT(impl_->optionMap_.empty(),
188                        "Can only add a manager before options");
189     // This check could be relaxed if we instead checked that the subsections
190     // do not have options.
191     GMX_RELEASE_ASSERT(impl_->subSections_.empty(),
192                        "Can only add a manager before subsections");
193     impl_->managers_.add(manager);
194 }
195
196 void Options::addSubSection(Options *section)
197 {
198     // This is required, because managers are used from the root Options
199     // object, so they are only seen after the subsection has been added.
200     GMX_RELEASE_ASSERT(section->impl_->optionMap_.empty(),
201                        "Can only add a subsection before it has any options");
202     GMX_RELEASE_ASSERT(section->impl_->managers_.empty(),
203                        "Can only have managers in a top-level Options object");
204     // Make sure that section is not already inserted somewhere.
205     GMX_RELEASE_ASSERT(section->impl_->parent_ == NULL,
206                        "Cannot add as subsection twice");
207     // Make sure that there are no duplicate sections.
208     GMX_RELEASE_ASSERT(impl_->findSubSection(section->name().c_str()) == NULL,
209                        "Duplicate subsection name");
210     impl_->subSections_.push_back(section);
211     section->impl_->parent_ = this;
212 }
213
214 IOptionsContainer &Options::addGroup()
215 {
216     return impl_->rootGroup_.addGroup();
217 }
218
219 OptionInfo *Options::addOption(const AbstractOption &settings)
220 {
221     return impl_->rootGroup_.addOption(settings);
222 }
223
224 bool Options::isSet(const char *name) const
225 {
226     AbstractOptionStorage *option = impl_->findOption(name);
227     return (option != NULL ? option->isSet() : false);
228 }
229
230 void Options::finish()
231 {
232     // TODO: Consider how to customize these error messages based on context.
233     ExceptionInitializer                    errors("Invalid input values");
234     OptionsImpl::OptionMap::const_iterator  i;
235     for (i = impl_->optionMap_.begin(); i != impl_->optionMap_.end(); ++i)
236     {
237         AbstractOptionStorage &option = *i->second;
238         try
239         {
240             option.finish();
241         }
242         catch (UserInputError &ex)
243         {
244             ex.prependContext("In option " + option.name());
245             errors.addCurrentExceptionAsNested();
246         }
247     }
248     OptionsImpl::SubSectionList::const_iterator j;
249     for (j = impl_->subSections_.begin(); j != impl_->subSections_.end(); ++j)
250     {
251         Options &section = **j;
252         try
253         {
254             section.finish();
255         }
256         catch (const UserInputError &)
257         {
258             errors.addCurrentExceptionAsNested();
259         }
260     }
261     if (errors.hasNestedExceptions())
262     {
263         // TODO: This exception type may not always be appropriate.
264         GMX_THROW(InvalidInputError(errors));
265     }
266 }
267
268 } // namespace gmx