2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Implements gmx::Options.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
42 #include "gromacs/options/options.h"
44 #include "gromacs/options/abstractoption.h"
45 #include "gromacs/options/abstractoptionstorage.h"
46 #include "gromacs/utility/arrayref.h"
47 #include "gromacs/utility/exceptions.h"
48 #include "gromacs/utility/gmxassert.h"
49 #include "gromacs/utility/messagestringcollector.h"
50 #include "gromacs/utility/stringutil.h"
52 #include "options-impl.h"
57 /********************************************************************
58 * OptionManagerInterface
61 OptionManagerInterface::~OptionManagerInterface()
65 /********************************************************************
69 Options::Impl::Impl(const char *name, const char *title)
70 : name_(name != NULL ? name : ""), title_(title != NULL ? title : ""),
75 Options::Impl::~Impl()
79 Options *Options::Impl::findSubSection(const char *name) const
81 SubSectionList::const_iterator i;
82 for (i = subSections_.begin(); i != subSections_.end(); ++i)
84 if ((*i)->name() == name)
92 AbstractOptionStorage *Options::Impl::findOption(const char *name) const
94 OptionList::const_iterator i;
95 for (i = options_.begin(); i != options_.end(); ++i)
97 if ((*i)->name() == name)
105 void Options::Impl::startSource()
107 OptionList::const_iterator i;
108 for (i = options_.begin(); i != options_.end(); ++i)
110 AbstractOptionStorage &option = **i;
111 option.startSource();
113 SubSectionList::const_iterator j;
114 for (j = subSections_.begin(); j != subSections_.end(); ++j)
116 Options §ion = **j;
117 section.impl_->startSource();
121 /********************************************************************
125 Options::Options(const char *name, const char *title)
126 : impl_(new Impl(name, title))
134 const std::string &Options::name() const
139 const std::string &Options::title() const
141 return impl_->title_;
144 const std::string &Options::description() const
146 return impl_->description_;
149 void Options::setDescription(const std::string &desc)
151 impl_->description_ = desc;
154 void Options::setDescription(const ConstArrayRef<const char *> &descArray)
156 impl_->description_ = concatenateStrings(descArray.data(), descArray.size());
159 void Options::addManager(OptionManagerInterface *manager)
161 GMX_RELEASE_ASSERT(impl_->parent_ == NULL,
162 "Can only add a manager in a top-level Options object");
163 // This ensures that all options see the same set of managers.
164 GMX_RELEASE_ASSERT(impl_->options_.empty(),
165 "Can only add a manager before options");
166 // This check could be relaxed if we instead checked that the subsections
167 // do not have options.
168 GMX_RELEASE_ASSERT(impl_->subSections_.empty(),
169 "Can only add a manager before subsections");
170 impl_->managers_.add(manager);
173 void Options::addSubSection(Options *section)
175 // This is required, because managers are used from the root Options
176 // object, so they are only seen after the subsection has been added.
177 GMX_RELEASE_ASSERT(section->impl_->options_.empty(),
178 "Can only add a subsection before it has any options");
179 GMX_RELEASE_ASSERT(section->impl_->managers_.empty(),
180 "Can only have managers in a top-level Options object");
181 // Make sure that section is not already inserted somewhere.
182 GMX_RELEASE_ASSERT(section->impl_->parent_ == NULL,
183 "Cannot add as subsection twice");
184 // Make sure that there are no duplicate sections.
185 GMX_RELEASE_ASSERT(impl_->findSubSection(section->name().c_str()) == NULL,
186 "Duplicate subsection name");
187 impl_->subSections_.push_back(section);
188 section->impl_->parent_ = this;
191 OptionInfo *Options::addOption(const AbstractOption &settings)
193 Options::Impl *root = impl_.get();
194 while (root->parent_ != NULL)
196 root = root->parent_->impl_.get();
198 AbstractOptionStoragePointer option(settings.createStorage(root->managers_));
199 if (impl_->findOption(option->name().c_str()) != NULL)
201 GMX_THROW(APIError("Duplicate option: " + option->name()));
203 impl_->options_.push_back(move(option));
204 return &impl_->options_.back()->optionInfo();
207 bool Options::isSet(const char *name) const
209 AbstractOptionStorage *option = impl_->findOption(name);
210 return (option != NULL ? option->isSet() : false);
213 void Options::finish()
215 // TODO: Consider how to customize these error messages based on context.
216 ExceptionInitializer errors("Invalid input values");
217 Impl::OptionList::const_iterator i;
218 for (i = impl_->options_.begin(); i != impl_->options_.end(); ++i)
220 AbstractOptionStorage &option = **i;
225 catch (UserInputError &ex)
227 ex.prependContext("In option " + option.name());
228 errors.addCurrentExceptionAsNested();
231 Impl::SubSectionList::const_iterator j;
232 for (j = impl_->subSections_.begin(); j != impl_->subSections_.end(); ++j)
234 Options §ion = **j;
239 catch (const UserInputError &)
241 errors.addCurrentExceptionAsNested();
244 if (errors.hasNestedExceptions())
246 // TODO: This exception type may not always be appropriate.
247 GMX_THROW(InvalidInputError(errors));