2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2014,2015,2016, 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/options/optionsection.h"
51 #include "gromacs/utility/arrayref.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/gmxassert.h"
54 #include "gromacs/utility/stringutil.h"
56 #include "options-impl.h"
61 /********************************************************************
65 IOptionManager::~IOptionManager()
69 /********************************************************************
73 IOptionsContainer::~IOptionsContainer()
77 /********************************************************************
78 * IOptionsContainerWithSections
81 IOptionsContainerWithSections::~IOptionsContainerWithSections()
85 /********************************************************************
86 * IOptionSectionStorage
89 IOptionSectionStorage::~IOptionSectionStorage()
93 /********************************************************************
100 OptionsImpl::OptionsImpl()
101 : rootSection_(managers_, nullptr, "")
105 /********************************************************************
110 OptionSectionImpl::addSectionImpl(const AbstractOptionSection §ion)
112 const char *name = section.name_;
113 // Make sure that there are no duplicate sections.
114 GMX_RELEASE_ASSERT(findSection(name) == NULL, "Duplicate subsection name");
115 std::unique_ptr<IOptionSectionStorage> storage(section.createStorage());
116 subsections_.push_back(SectionPointer(new OptionSectionImpl(managers_, std::move(storage), name)));
117 return subsections_.back().get();
120 IOptionsContainer &OptionSectionImpl::addGroup()
122 return rootGroup_.addGroup();
125 OptionInfo *OptionSectionImpl::addOptionImpl(const AbstractOption &settings)
127 return rootGroup_.addOptionImpl(settings);
130 OptionSectionImpl *OptionSectionImpl::findSection(const char *name) const
132 for (const auto §ion : subsections_)
134 if (section->name_ == name)
136 return section.get();
142 AbstractOptionStorage *OptionSectionImpl::findOption(const char *name) const
144 OptionMap::const_iterator i = optionMap_.find(name);
145 if (i == optionMap_.end())
149 return i->second.get();
152 void OptionSectionImpl::start()
154 for (const auto &entry : optionMap_)
156 entry.second->startSource();
158 if (storage_ != nullptr)
160 if (!storageInitialized_)
162 storage_->initStorage();
163 storageInitialized_ = true;
165 storage_->startSection();
169 void OptionSectionImpl::finish()
171 // TODO: Consider how to customize these error messages based on context.
172 ExceptionInitializer errors("Invalid input values");
173 for (const auto &entry : optionMap_)
175 AbstractOptionStorage &option = *entry.second;
180 catch (UserInputError &ex)
182 ex.prependContext("In option " + option.name());
183 errors.addCurrentExceptionAsNested();
186 if (errors.hasNestedExceptions())
188 // TODO: This exception type may not always be appropriate.
189 GMX_THROW(InvalidInputError(errors));
191 if (storage_ != nullptr)
193 storage_->finishSection();
197 /********************************************************************
198 * OptionSectionImpl::Group
201 IOptionsContainer &OptionSectionImpl::Group::addGroup()
203 subgroups_.emplace_back(parent_);
204 return subgroups_.back();
207 OptionInfo *OptionSectionImpl::Group::addOptionImpl(const AbstractOption &settings)
209 OptionSectionImpl::AbstractOptionStoragePointer
210 option(settings.createStorage(parent_->managers_));
211 options_.reserve(options_.size() + 1);
212 auto insertionResult =
213 parent_->optionMap_.insert(std::make_pair(option->name(),
215 if (!insertionResult.second)
217 const std::string &name = insertionResult.first->second->name();
218 GMX_THROW(APIError("Duplicate option: " + name));
220 AbstractOptionStorage &insertedOption = *insertionResult.first->second;
221 options_.push_back(&insertedOption);
222 return &insertedOption.optionInfo();
225 } // namespace internal
227 using internal::OptionsImpl;
229 /********************************************************************
234 : impl_(new OptionsImpl)
243 void Options::addManager(IOptionManager *manager)
245 // This ensures that all options see the same set of managers.
246 GMX_RELEASE_ASSERT(impl_->rootSection_.optionMap_.empty(),
247 "Can only add a manager before options");
248 // This check could be relaxed if we instead checked that the subsections
249 // do not have options.
250 GMX_RELEASE_ASSERT(impl_->rootSection_.subsections_.empty(),
251 "Can only add a manager before subsections");
252 impl_->managers_.add(manager);
255 internal::OptionSectionImpl *Options::addSectionImpl(const AbstractOptionSection §ion)
257 return impl_->rootSection_.addSectionImpl(section);
260 IOptionsContainer &Options::addGroup()
262 return impl_->rootSection_.addGroup();
265 OptionInfo *Options::addOptionImpl(const AbstractOption &settings)
267 return impl_->rootSection_.addOptionImpl(settings);
270 OptionSectionInfo &Options::rootSection()
272 return impl_->rootSection_.info();
275 const OptionSectionInfo &Options::rootSection() const
277 return impl_->rootSection_.info();
280 void Options::finish()
282 impl_->rootSection_.finish();