2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2014,2015,2016,2017,2018,2019, 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
49 #include "gromacs/options/abstractoption.h"
50 #include "gromacs/options/abstractoptionstorage.h"
51 #include "gromacs/options/optionsection.h"
52 #include "gromacs/utility/arrayref.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/gmxassert.h"
55 #include "gromacs/utility/stringutil.h"
57 #include "options-impl.h"
62 /********************************************************************
66 IOptionManager::~IOptionManager()
70 /********************************************************************
74 IOptionsContainer::~IOptionsContainer()
78 /********************************************************************
79 * IOptionsContainerWithSections
82 IOptionsContainerWithSections::~IOptionsContainerWithSections()
86 /********************************************************************
87 * IOptionSectionStorage
90 IOptionSectionStorage::~IOptionSectionStorage()
94 /********************************************************************
101 OptionsImpl::OptionsImpl()
102 : rootSection_(managers_, nullptr, "")
106 /********************************************************************
111 OptionSectionImpl::addSectionImpl(const AbstractOptionSection §ion)
113 const char *name = section.name_;
114 // Make sure that there are no duplicate sections.
115 GMX_RELEASE_ASSERT(findSection(name) == nullptr, "Duplicate subsection name");
116 std::unique_ptr<IOptionSectionStorage> storage(section.createStorage());
117 subsections_.push_back(std::make_unique<OptionSectionImpl>(managers_, std::move(storage), name));
118 return subsections_.back().get();
121 IOptionsContainer &OptionSectionImpl::addGroup()
123 return rootGroup_.addGroup();
126 OptionInfo *OptionSectionImpl::addOptionImpl(const AbstractOption &settings)
128 return rootGroup_.addOptionImpl(settings);
131 OptionSectionImpl *OptionSectionImpl::findSection(const char *name) const
133 for (const auto §ion : subsections_)
135 if (section->name_ == name)
137 return section.get();
143 AbstractOptionStorage *OptionSectionImpl::findOption(const char *name) const
145 OptionMap::const_iterator i = optionMap_.find(name);
146 if (i == optionMap_.end())
150 return i->second.get();
153 void OptionSectionImpl::start()
155 for (const auto &entry : optionMap_)
157 entry.second->startSource();
159 if (storage_ != nullptr)
161 if (!storageInitialized_)
163 storage_->initStorage();
164 storageInitialized_ = true;
166 storage_->startSection();
170 void OptionSectionImpl::finish()
172 // TODO: Consider how to customize these error messages based on context.
173 ExceptionInitializer errors("Invalid input values");
174 for (const auto &entry : optionMap_)
176 AbstractOptionStorage &option = *entry.second;
181 catch (UserInputError &ex)
183 ex.prependContext("In option " + option.name());
184 errors.addCurrentExceptionAsNested();
187 if (errors.hasNestedExceptions())
189 // TODO: This exception type may not always be appropriate.
190 GMX_THROW(InvalidInputError(errors));
192 if (storage_ != nullptr)
194 storage_->finishSection();
198 /********************************************************************
199 * OptionSectionImpl::Group
202 IOptionsContainer &OptionSectionImpl::Group::addGroup()
204 subgroups_.emplace_back(parent_);
205 return subgroups_.back();
208 OptionInfo *OptionSectionImpl::Group::addOptionImpl(const AbstractOption &settings)
210 OptionSectionImpl::AbstractOptionStoragePointer
211 option(settings.createStorage(parent_->managers_));
212 options_.reserve(options_.size() + 1);
213 auto insertionResult =
214 parent_->optionMap_.insert(std::make_pair(option->name(),
216 if (!insertionResult.second)
218 const std::string &name = insertionResult.first->second->name();
219 GMX_THROW(APIError("Duplicate option: " + name));
221 AbstractOptionStorage &insertedOption = *insertionResult.first->second;
222 options_.push_back(&insertedOption);
223 return &insertedOption.optionInfo();
226 } // namespace internal
228 using internal::OptionsImpl;
230 /********************************************************************
235 : impl_(new OptionsImpl)
244 void Options::addManager(IOptionManager *manager)
246 // This ensures that all options see the same set of managers.
247 GMX_RELEASE_ASSERT(impl_->rootSection_.optionMap_.empty(),
248 "Can only add a manager before options");
249 // This check could be relaxed if we instead checked that the subsections
250 // do not have options.
251 GMX_RELEASE_ASSERT(impl_->rootSection_.subsections_.empty(),
252 "Can only add a manager before subsections");
253 impl_->managers_.add(manager);
256 internal::OptionSectionImpl *Options::addSectionImpl(const AbstractOptionSection §ion)
258 return impl_->rootSection_.addSectionImpl(section);
261 IOptionsContainer &Options::addGroup()
263 return impl_->rootSection_.addGroup();
266 OptionInfo *Options::addOptionImpl(const AbstractOption &settings)
268 return impl_->rootSection_.addOptionImpl(settings);
271 OptionSectionInfo &Options::rootSection()
273 return impl_->rootSection_.info();
276 const OptionSectionInfo &Options::rootSection() const
278 return impl_->rootSection_.info();
281 void Options::finish()
283 impl_->rootSection_.finish();