2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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/exceptions.h"
47 #include "gromacs/utility/gmxassert.h"
48 #include "gromacs/utility/messagestringcollector.h"
49 #include "gromacs/utility/stringutil.h"
51 #include "options-impl.h"
56 /********************************************************************
60 Options::Impl::Impl(const char *name, const char *title)
61 : name_(name != NULL ? name : ""), title_(title != NULL ? title : ""),
66 Options::Impl::~Impl()
70 Options *Options::Impl::findSubSection(const char *name) const
72 SubSectionList::const_iterator i;
73 for (i = subSections_.begin(); i != subSections_.end(); ++i)
75 if ((*i)->name() == name)
83 AbstractOptionStorage *Options::Impl::findOption(const char *name) const
85 OptionList::const_iterator i;
86 for (i = options_.begin(); i != options_.end(); ++i)
88 if ((*i)->name() == name)
96 void Options::Impl::startSource()
98 OptionList::const_iterator i;
99 for (i = options_.begin(); i != options_.end(); ++i)
101 AbstractOptionStorage &option = **i;
102 option.startSource();
104 SubSectionList::const_iterator j;
105 for (j = subSections_.begin(); j != subSections_.end(); ++j)
107 Options §ion = **j;
108 section.impl_->startSource();
112 /********************************************************************
116 Options::Options(const char *name, const char *title)
117 : impl_(new Impl(name, title))
125 const std::string &Options::name() const
130 const std::string &Options::title() const
132 return impl_->title_;
135 const std::string &Options::description() const
137 return impl_->description_;
140 void Options::setDescription(const std::string &desc)
142 impl_->description_ = desc;
145 void Options::addSubSection(Options *section)
147 // Make sure that section is not already inserted somewhere.
148 GMX_RELEASE_ASSERT(section->impl_->parent_ == NULL,
149 "Cannot add as subsection twice");
150 // Make sure that there are no duplicate sections.
151 GMX_RELEASE_ASSERT(impl_->findSubSection(section->name().c_str()) == NULL,
152 "Duplicate subsection name");
153 impl_->subSections_.push_back(section);
154 section->impl_->parent_ = this;
157 OptionInfo *Options::addOption(const AbstractOption &settings)
159 AbstractOptionStoragePointer option(settings.createStorage());
160 if (impl_->findOption(option->name().c_str()) != NULL)
162 GMX_THROW(APIError("Duplicate option: " + option->name()));
164 impl_->options_.push_back(move(option));
165 return &impl_->options_.back()->optionInfo();
168 bool Options::isSet(const char *name) const
170 AbstractOptionStorage *option = impl_->findOption(name);
171 return (option != NULL ? option->isSet() : false);
174 void Options::finish()
176 // TODO: Consider how to customize these error messages based on context.
177 ExceptionInitializer errors("Invalid input values");
178 Impl::OptionList::const_iterator i;
179 for (i = impl_->options_.begin(); i != impl_->options_.end(); ++i)
181 AbstractOptionStorage &option = **i;
186 catch (UserInputError &ex)
188 ex.prependContext("In option " + option.name());
189 errors.addCurrentExceptionAsNested();
192 Impl::SubSectionList::const_iterator j;
193 for (j = impl_->subSections_.begin(); j != impl_->subSections_.end(); ++j)
195 Options §ion = **j;
200 catch (const UserInputError &)
202 errors.addCurrentExceptionAsNested();
205 if (errors.hasNestedExceptions())
207 // TODO: This exception type may not always be appropriate.
208 GMX_THROW(InvalidInputError(errors));