Rename all source files from - to _ for consistency.
[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,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.
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 <memory>
47 #include <utility>
48
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"
56
57 #include "options_impl.h"
58
59 namespace gmx
60 {
61
62 /********************************************************************
63  * IOptionManager
64  */
65
66 IOptionManager::~IOptionManager()
67 {
68 }
69
70 /********************************************************************
71  * IOptionsContainer
72  */
73
74 IOptionsContainer::~IOptionsContainer()
75 {
76 }
77
78 /********************************************************************
79  * IOptionsContainerWithSections
80  */
81
82 IOptionsContainerWithSections::~IOptionsContainerWithSections()
83 {
84 }
85
86 /********************************************************************
87  * IOptionSectionStorage
88  */
89
90 IOptionSectionStorage::~IOptionSectionStorage()
91 {
92 }
93
94 /********************************************************************
95  * OptionsImpl
96  */
97
98 namespace internal
99 {
100
101 OptionsImpl::OptionsImpl()
102     : rootSection_(managers_, nullptr, "")
103 {
104 }
105
106 /********************************************************************
107  * OptionSectionImpl
108  */
109
110 OptionSectionImpl *
111 OptionSectionImpl::addSectionImpl(const AbstractOptionSection &section)
112 {
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();
119 }
120
121 IOptionsContainer &OptionSectionImpl::addGroup()
122 {
123     return rootGroup_.addGroup();
124 }
125
126 OptionInfo *OptionSectionImpl::addOptionImpl(const AbstractOption &settings)
127 {
128     return rootGroup_.addOptionImpl(settings);
129 }
130
131 OptionSectionImpl *OptionSectionImpl::findSection(const char *name) const
132 {
133     for (const auto &section : subsections_)
134     {
135         if (section->name_ == name)
136         {
137             return section.get();
138         }
139     }
140     return nullptr;
141 }
142
143 AbstractOptionStorage *OptionSectionImpl::findOption(const char *name) const
144 {
145     OptionMap::const_iterator i = optionMap_.find(name);
146     if (i == optionMap_.end())
147     {
148         return nullptr;
149     }
150     return i->second.get();
151 }
152
153 void OptionSectionImpl::start()
154 {
155     for (const auto &entry : optionMap_)
156     {
157         entry.second->startSource();
158     }
159     if (storage_ != nullptr)
160     {
161         if (!storageInitialized_)
162         {
163             storage_->initStorage();
164             storageInitialized_ = true;
165         }
166         storage_->startSection();
167     }
168 }
169
170 void OptionSectionImpl::finish()
171 {
172     // TODO: Consider how to customize these error messages based on context.
173     ExceptionInitializer  errors("Invalid input values");
174     for (const auto &entry : optionMap_)
175     {
176         AbstractOptionStorage &option = *entry.second;
177         try
178         {
179             option.finish();
180         }
181         catch (UserInputError &ex)
182         {
183             ex.prependContext("In option " + option.name());
184             errors.addCurrentExceptionAsNested();
185         }
186     }
187     if (errors.hasNestedExceptions())
188     {
189         // TODO: This exception type may not always be appropriate.
190         GMX_THROW(InvalidInputError(errors));
191     }
192     if (storage_ != nullptr)
193     {
194         storage_->finishSection();
195     }
196 }
197
198 /********************************************************************
199  * OptionSectionImpl::Group
200  */
201
202 IOptionsContainer &OptionSectionImpl::Group::addGroup()
203 {
204     subgroups_.emplace_back(parent_);
205     return subgroups_.back();
206 }
207
208 OptionInfo *OptionSectionImpl::Group::addOptionImpl(const AbstractOption &settings)
209 {
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(),
215                                                   std::move(option)));
216     if (!insertionResult.second)
217     {
218         const std::string &name = insertionResult.first->second->name();
219         GMX_THROW(APIError("Duplicate option: " + name));
220     }
221     AbstractOptionStorage &insertedOption = *insertionResult.first->second;
222     options_.push_back(&insertedOption);
223     return &insertedOption.optionInfo();
224 }
225
226 }   // namespace internal
227
228 using internal::OptionsImpl;
229
230 /********************************************************************
231  * Options
232  */
233
234 Options::Options()
235     : impl_(new OptionsImpl)
236 {
237 }
238
239 Options::~Options()
240 {
241 }
242
243
244 void Options::addManager(IOptionManager *manager)
245 {
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);
254 }
255
256 internal::OptionSectionImpl *Options::addSectionImpl(const AbstractOptionSection &section)
257 {
258     return impl_->rootSection_.addSectionImpl(section);
259 }
260
261 IOptionsContainer &Options::addGroup()
262 {
263     return impl_->rootSection_.addGroup();
264 }
265
266 OptionInfo *Options::addOptionImpl(const AbstractOption &settings)
267 {
268     return impl_->rootSection_.addOptionImpl(settings);
269 }
270
271 OptionSectionInfo &Options::rootSection()
272 {
273     return impl_->rootSection_.info();
274 }
275
276 const OptionSectionInfo &Options::rootSection() const
277 {
278     return impl_->rootSection_.info();
279 }
280
281 void Options::finish()
282 {
283     impl_->rootSection_.finish();
284 }
285
286 } // namespace gmx