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