Rename all source files from - to _ for consistency.
[alexxy/gromacs.git] / src / gromacs / options / options_impl.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,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  * Declares private implementation class for gmx::Options.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_options
41  */
42 #ifndef GMX_OPTIONS_OPTIONS_IMPL_H
43 #define GMX_OPTIONS_OPTIONS_IMPL_H
44
45 #include <list>
46 #include <map>
47 #include <memory>
48 #include <string>
49 #include <vector>
50
51 #include "gromacs/options/abstractoption.h"
52 #include "gromacs/options/abstractoptionstorage.h"
53 #include "gromacs/options/ioptionscontainer.h"
54 #include "gromacs/options/ioptionscontainerwithsections.h"
55 #include "gromacs/options/isectionstorage.h"
56 #include "gromacs/options/optionmanagercontainer.h"
57 #include "gromacs/options/options.h"
58 #include "gromacs/options/optionsection.h"
59
60 namespace gmx
61 {
62
63 namespace internal
64 {
65
66 /*! \internal
67  * \brief
68  * Internal implementation class for storing an option section.
69  *
70  * All options are stored within a section: the top-level contents of an
71  * Options object are handled within an unnamed, "root" section.
72  * This class handles the common functionality for all sections, related to
73  * storing the options and subsections.  Functionality specific to a section
74  * type is provided by IOptionSectionStorage.
75  *
76  * \ingroup module_options
77  */
78 class OptionSectionImpl : public IOptionsContainerWithSections
79 {
80     public:
81         /*! \internal \brief
82          * Describes a group of options (see Options::addGroup()).
83          *
84          * \ingroup module_options
85          */
86         class Group : public IOptionsContainer
87         {
88             public:
89                 //! Convenience typedef for list of options.
90                 typedef std::vector<AbstractOptionStorage *> OptionList;
91                 //! Convenience typedef for list of subgroups.
92                 typedef std::list<Group> SubgroupList;
93
94                 //! Creates a group within the given Options.
95                 explicit Group(OptionSectionImpl *parent) : parent_(parent) {}
96
97                 // From IOptionsContainer
98                 IOptionsContainer &addGroup() override;
99                 OptionInfo *addOptionImpl(const AbstractOption &settings) override;
100
101                 //! Containing options object.
102                 OptionSectionImpl  *parent_;
103                 /*! \brief
104                  * List of options, in insertion order.
105                  *
106                  * Pointers in this container point to the objects managed by
107                  * Impl::optionsMap_.
108                  */
109                 OptionList          options_;
110                 //! List of groups, in insertion order.
111                 SubgroupList        subgroups_;
112         };
113
114         //! Smart pointer for managing an AbstractOptionStorage object.
115         typedef std::unique_ptr<AbstractOptionStorage>
116             AbstractOptionStoragePointer;
117         //! Convenience typedef for a map that contains all the options.
118         typedef std::map<std::string, AbstractOptionStoragePointer> OptionMap;
119         //! Smart pointer for managing subsections.
120         typedef std::unique_ptr<OptionSectionImpl> SectionPointer;
121         //! Convenience typedef for a container for subsections.
122         typedef std::vector<SectionPointer> SectionList;
123
124         //! Creates storage for a new section.
125         OptionSectionImpl(const OptionManagerContainer          &managers,
126                           std::unique_ptr<IOptionSectionStorage> storage,
127                           const char                            *name)
128             : managers_(managers), storage_(std::move(storage)), info_(this),
129               name_(name), rootGroup_(this), storageInitialized_(false)
130         {
131         }
132
133         // From IOptionsContainerWithSections
134         OptionSectionImpl *addSectionImpl(const AbstractOptionSection &section) override;
135
136         // From IOptionsContainer
137         IOptionsContainer &addGroup() override;
138         OptionInfo *addOptionImpl(const AbstractOption &settings) override;
139
140         //! Returns section info object for this section.
141         OptionSectionInfo       &info() { return info_; }
142         //! Returns section info object for this section.
143         const OptionSectionInfo &info() const { return info_; }
144
145         /*! \brief
146          * Finds a subsection by name.
147          *
148          * \param[in] name  Name to search for.
149          * \returns Pointer to the found subsection, or NULL if not found.
150          *
151          * Does not throw.
152          */
153         OptionSectionImpl *findSection(const char *name) const;
154         /*! \brief
155          * Finds an option by name.
156          *
157          * \param[in] name  Name to search for.
158          * \returns Pointer to the found option, or NULL if not found.
159          *
160          * Does not throw.
161          */
162         AbstractOptionStorage *findOption(const char *name) const;
163
164         /*! \brief
165          * Called when entering the section.
166          *
167          * Calls AbstractOptionStorage::startSource() for all options.
168          */
169         void start();
170         /*! \brief
171          * Calls AbstractOptionStorage::finish() for all options.
172          */
173         void finish();
174
175         //! Reference to the option managers in the parent Options object.
176         const OptionManagerContainer           &managers_;
177         //! Type-specific storage object for this section.
178         std::unique_ptr<IOptionSectionStorage>  storage_;
179         //! Info object for this section.
180         OptionSectionInfo                       info_;
181         //! Name of this section (empty and unused for the root section).
182         std::string                             name_;
183         /*! \brief
184          * Group that contains all options (and subgroups).
185          *
186          * This is used to store the insertion order of options.
187          */
188         Group                          rootGroup_;
189         //! Map from option names to options; owns the option storage objects.
190         OptionMap                      optionMap_;
191         //! List of subsections, in insertion order.
192         SectionList                    subsections_;
193         //! Whether initStorage() has been called for `storage_`.
194         bool                           storageInitialized_;
195
196         GMX_DISALLOW_COPY_AND_ASSIGN(OptionSectionImpl);
197 };
198
199 /*! \internal
200  * \brief
201  * Private implementation class for Options.
202  *
203  * Note that in addition to Options, the OptionsAssigner class also directly
204  * accesses this class.
205  *
206  * \ingroup module_options
207  */
208 class OptionsImpl
209 {
210     public:
211         OptionsImpl();
212
213         //! Option managers set for this collection.
214         OptionManagerContainer  managers_;
215         //! Root section for this collection.
216         OptionSectionImpl       rootSection_;
217 };
218
219 } // namespace internal
220
221 } // namespace gmx
222
223 #endif