Tidy: modernize-use-nullptr
[alexxy/gromacs.git] / src / gromacs / options / options.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2014,2015,2016,2017, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements gmx::Options.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_options
41  */
42 #include "gmxpre.h"
43
44 #include "options.h"
45
46 #include <utility>
47
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"
55
56 #include "options-impl.h"
57
58 namespace gmx
59 {
60
61 /********************************************************************
62  * IOptionManager
63  */
64
65 IOptionManager::~IOptionManager()
66 {
67 }
68
69 /********************************************************************
70  * IOptionsContainer
71  */
72
73 IOptionsContainer::~IOptionsContainer()
74 {
75 }
76
77 /********************************************************************
78  * IOptionsContainerWithSections
79  */
80
81 IOptionsContainerWithSections::~IOptionsContainerWithSections()
82 {
83 }
84
85 /********************************************************************
86  * IOptionSectionStorage
87  */
88
89 IOptionSectionStorage::~IOptionSectionStorage()
90 {
91 }
92
93 /********************************************************************
94  * OptionsImpl
95  */
96
97 namespace internal
98 {
99
100 OptionsImpl::OptionsImpl()
101     : rootSection_(managers_, nullptr, "")
102 {
103 }
104
105 /********************************************************************
106  * OptionSectionImpl
107  */
108
109 OptionSectionImpl *
110 OptionSectionImpl::addSectionImpl(const AbstractOptionSection &section)
111 {
112     const char *name = section.name_;
113     // Make sure that there are no duplicate sections.
114     GMX_RELEASE_ASSERT(findSection(name) == nullptr, "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();
118 }
119
120 IOptionsContainer &OptionSectionImpl::addGroup()
121 {
122     return rootGroup_.addGroup();
123 }
124
125 OptionInfo *OptionSectionImpl::addOptionImpl(const AbstractOption &settings)
126 {
127     return rootGroup_.addOptionImpl(settings);
128 }
129
130 OptionSectionImpl *OptionSectionImpl::findSection(const char *name) const
131 {
132     for (const auto &section : subsections_)
133     {
134         if (section->name_ == name)
135         {
136             return section.get();
137         }
138     }
139     return nullptr;
140 }
141
142 AbstractOptionStorage *OptionSectionImpl::findOption(const char *name) const
143 {
144     OptionMap::const_iterator i = optionMap_.find(name);
145     if (i == optionMap_.end())
146     {
147         return nullptr;
148     }
149     return i->second.get();
150 }
151
152 void OptionSectionImpl::start()
153 {
154     for (const auto &entry : optionMap_)
155     {
156         entry.second->startSource();
157     }
158     if (storage_ != nullptr)
159     {
160         if (!storageInitialized_)
161         {
162             storage_->initStorage();
163             storageInitialized_ = true;
164         }
165         storage_->startSection();
166     }
167 }
168
169 void OptionSectionImpl::finish()
170 {
171     // TODO: Consider how to customize these error messages based on context.
172     ExceptionInitializer  errors("Invalid input values");
173     for (const auto &entry : optionMap_)
174     {
175         AbstractOptionStorage &option = *entry.second;
176         try
177         {
178             option.finish();
179         }
180         catch (UserInputError &ex)
181         {
182             ex.prependContext("In option " + option.name());
183             errors.addCurrentExceptionAsNested();
184         }
185     }
186     if (errors.hasNestedExceptions())
187     {
188         // TODO: This exception type may not always be appropriate.
189         GMX_THROW(InvalidInputError(errors));
190     }
191     if (storage_ != nullptr)
192     {
193         storage_->finishSection();
194     }
195 }
196
197 /********************************************************************
198  * OptionSectionImpl::Group
199  */
200
201 IOptionsContainer &OptionSectionImpl::Group::addGroup()
202 {
203     subgroups_.emplace_back(parent_);
204     return subgroups_.back();
205 }
206
207 OptionInfo *OptionSectionImpl::Group::addOptionImpl(const AbstractOption &settings)
208 {
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(),
214                                                   std::move(option)));
215     if (!insertionResult.second)
216     {
217         const std::string &name = insertionResult.first->second->name();
218         GMX_THROW(APIError("Duplicate option: " + name));
219     }
220     AbstractOptionStorage &insertedOption = *insertionResult.first->second;
221     options_.push_back(&insertedOption);
222     return &insertedOption.optionInfo();
223 }
224
225 }   // namespace internal
226
227 using internal::OptionsImpl;
228
229 /********************************************************************
230  * Options
231  */
232
233 Options::Options()
234     : impl_(new OptionsImpl)
235 {
236 }
237
238 Options::~Options()
239 {
240 }
241
242
243 void Options::addManager(IOptionManager *manager)
244 {
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);
253 }
254
255 internal::OptionSectionImpl *Options::addSectionImpl(const AbstractOptionSection &section)
256 {
257     return impl_->rootSection_.addSectionImpl(section);
258 }
259
260 IOptionsContainer &Options::addGroup()
261 {
262     return impl_->rootSection_.addGroup();
263 }
264
265 OptionInfo *Options::addOptionImpl(const AbstractOption &settings)
266 {
267     return impl_->rootSection_.addOptionImpl(settings);
268 }
269
270 OptionSectionInfo &Options::rootSection()
271 {
272     return impl_->rootSection_.info();
273 }
274
275 const OptionSectionInfo &Options::rootSection() const
276 {
277     return impl_->rootSection_.info();
278 }
279
280 void Options::finish()
281 {
282     impl_->rootSection_.finish();
283 }
284
285 } // namespace gmx