2 * This file is part of the GROMACS molecular simulation package.
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.
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
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"
55 #include "options-impl.h"
60 /********************************************************************
64 IOptionManager::~IOptionManager()
68 /********************************************************************
72 IOptionsContainer::~IOptionsContainer()
76 /********************************************************************
83 OptionsImpl::OptionsImpl(const char *name, const char * /*title*/)
84 : name_(name != NULL ? name : ""), rootGroup_(this),
89 Options *OptionsImpl::findSubSection(const char *name) const
91 SubSectionList::const_iterator i;
92 for (i = subSections_.begin(); i != subSections_.end(); ++i)
94 if ((*i)->name() == name)
102 AbstractOptionStorage *OptionsImpl::findOption(const char *name) const
104 OptionMap::const_iterator i = optionMap_.find(name);
105 if (i == optionMap_.end())
109 return i->second.get();
112 void OptionsImpl::startSource()
114 OptionMap::const_iterator i;
115 for (i = optionMap_.begin(); i != optionMap_.end(); ++i)
117 AbstractOptionStorage &option = *i->second;
118 option.startSource();
120 SubSectionList::const_iterator j;
121 for (j = subSections_.begin(); j != subSections_.end(); ++j)
123 Options §ion = **j;
124 section.impl_->startSource();
128 /********************************************************************
132 IOptionsContainer &OptionsImpl::Group::addGroup()
134 subgroups_.push_back(Group(parent_));
135 return subgroups_.back();
138 OptionInfo *OptionsImpl::Group::addOption(const AbstractOption &settings)
140 OptionsImpl *root = parent_;
141 while (root->parent_ != NULL)
143 root = root->parent_->impl_.get();
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(),
150 if (!insertionResult.second)
152 GMX_THROW(APIError("Duplicate option: " + option->name()));
154 AbstractOptionStorage &insertedOption = *insertionResult.first->second;
155 options_.push_back(&insertedOption);
156 return &insertedOption.optionInfo();
159 } // namespace internal
161 using internal::OptionsImpl;
163 /********************************************************************
167 Options::Options(const char *name, const char *title)
168 : impl_(new OptionsImpl(name, title))
176 const std::string &Options::name() const
182 void Options::addManager(IOptionManager *manager)
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);
196 void Options::addSubSection(Options *section)
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;
214 IOptionsContainer &Options::addGroup()
216 return impl_->rootGroup_.addGroup();
219 OptionInfo *Options::addOption(const AbstractOption &settings)
221 return impl_->rootGroup_.addOption(settings);
224 void Options::finish()
226 // TODO: Consider how to customize these error messages based on context.
227 ExceptionInitializer errors("Invalid input values");
228 OptionsImpl::OptionMap::const_iterator i;
229 for (i = impl_->optionMap_.begin(); i != impl_->optionMap_.end(); ++i)
231 AbstractOptionStorage &option = *i->second;
236 catch (UserInputError &ex)
238 ex.prependContext("In option " + option.name());
239 errors.addCurrentExceptionAsNested();
242 OptionsImpl::SubSectionList::const_iterator j;
243 for (j = impl_->subSections_.begin(); j != impl_->subSections_.end(); ++j)
245 Options §ion = **j;
250 catch (const UserInputError &)
252 errors.addCurrentExceptionAsNested();
255 if (errors.hasNestedExceptions())
257 // TODO: This exception type may not always be appropriate.
258 GMX_THROW(InvalidInputError(errors));