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