Apply clang-format to source tree
[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-2018, The GROMACS development team.
5  * Copyright (c) 2019, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Declares private implementation class for gmx::Options.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_options
42  */
43 #ifndef GMX_OPTIONS_OPTIONS_IMPL_H
44 #define GMX_OPTIONS_OPTIONS_IMPL_H
45
46 #include <list>
47 #include <map>
48 #include <memory>
49 #include <string>
50 #include <vector>
51
52 #include "gromacs/options/abstractoption.h"
53 #include "gromacs/options/abstractoptionstorage.h"
54 #include "gromacs/options/ioptionscontainer.h"
55 #include "gromacs/options/ioptionscontainerwithsections.h"
56 #include "gromacs/options/optionmanagercontainer.h"
57 #include "gromacs/options/options.h"
58 #include "gromacs/options/optionsection.h"
59
60 #include "isectionstorage.h"
61
62 namespace gmx
63 {
64
65 namespace internal
66 {
67
68 /*! \internal
69  * \brief
70  * Internal implementation class for storing an option section.
71  *
72  * All options are stored within a section: the top-level contents of an
73  * Options object are handled within an unnamed, "root" section.
74  * This class handles the common functionality for all sections, related to
75  * storing the options and subsections.  Functionality specific to a section
76  * type is provided by IOptionSectionStorage.
77  *
78  * \ingroup module_options
79  */
80 class OptionSectionImpl : public IOptionsContainerWithSections
81 {
82 public:
83     /*! \internal \brief
84      * Describes a group of options (see Options::addGroup()).
85      *
86      * \ingroup module_options
87      */
88     class Group : public IOptionsContainer
89     {
90     public:
91         //! Convenience typedef for list of options.
92         typedef std::vector<AbstractOptionStorage*> OptionList;
93         //! Convenience typedef for list of subgroups.
94         typedef std::list<Group> SubgroupList;
95
96         //! Creates a group within the given Options.
97         explicit Group(OptionSectionImpl* parent) : parent_(parent) {}
98
99         // From IOptionsContainer
100         IOptionsContainer& addGroup() override;
101         OptionInfo*        addOptionImpl(const AbstractOption& settings) override;
102
103         //! Containing options object.
104         OptionSectionImpl* parent_;
105         /*! \brief
106          * List of options, in insertion order.
107          *
108          * Pointers in this container point to the objects managed by
109          * Impl::optionsMap_.
110          */
111         OptionList options_;
112         //! List of groups, in insertion order.
113         SubgroupList subgroups_;
114     };
115
116     //! Smart pointer for managing an AbstractOptionStorage object.
117     typedef std::unique_ptr<AbstractOptionStorage> AbstractOptionStoragePointer;
118     //! Convenience typedef for a map that contains all the options.
119     typedef std::map<std::string, AbstractOptionStoragePointer> OptionMap;
120     //! Smart pointer for managing subsections.
121     typedef std::unique_ptr<OptionSectionImpl> SectionPointer;
122     //! Convenience typedef for a container for subsections.
123     typedef std::vector<SectionPointer> SectionList;
124
125     //! Creates storage for a new section.
126     OptionSectionImpl(const OptionManagerContainer&          managers,
127                       std::unique_ptr<IOptionSectionStorage> storage,
128                       const char*                            name) :
129         managers_(managers),
130         storage_(std::move(storage)),
131         info_(this),
132         name_(name),
133         rootGroup_(this),
134         storageInitialized_(false)
135     {
136     }
137
138     // From IOptionsContainerWithSections
139     OptionSectionImpl* addSectionImpl(const AbstractOptionSection& section) override;
140
141     // From IOptionsContainer
142     IOptionsContainer& addGroup() override;
143     OptionInfo*        addOptionImpl(const AbstractOption& settings) override;
144
145     //! Returns section info object for this section.
146     OptionSectionInfo& info() { return info_; }
147     //! Returns section info object for this section.
148     const OptionSectionInfo& info() const { return info_; }
149
150     /*! \brief
151      * Finds a subsection by name.
152      *
153      * \param[in] name  Name to search for.
154      * \returns Pointer to the found subsection, or NULL if not found.
155      *
156      * Does not throw.
157      */
158     OptionSectionImpl* findSection(const char* name) const;
159     /*! \brief
160      * Finds an option by name.
161      *
162      * \param[in] name  Name to search for.
163      * \returns Pointer to the found option, or NULL if not found.
164      *
165      * Does not throw.
166      */
167     AbstractOptionStorage* findOption(const char* name) const;
168
169     /*! \brief
170      * Called when entering the section.
171      *
172      * Calls AbstractOptionStorage::startSource() for all options.
173      */
174     void start();
175     /*! \brief
176      * Calls AbstractOptionStorage::finish() for all options.
177      */
178     void finish();
179
180     //! Reference to the option managers in the parent Options object.
181     const OptionManagerContainer& managers_;
182     //! Type-specific storage object for this section.
183     std::unique_ptr<IOptionSectionStorage> storage_;
184     //! Info object for this section.
185     OptionSectionInfo info_;
186     //! Name of this section (empty and unused for the root section).
187     std::string name_;
188     /*! \brief
189      * Group that contains all options (and subgroups).
190      *
191      * This is used to store the insertion order of options.
192      */
193     Group rootGroup_;
194     //! Map from option names to options; owns the option storage objects.
195     OptionMap optionMap_;
196     //! List of subsections, in insertion order.
197     SectionList subsections_;
198     //! Whether initStorage() has been called for `storage_`.
199     bool storageInitialized_;
200
201     GMX_DISALLOW_COPY_AND_ASSIGN(OptionSectionImpl);
202 };
203
204 /*! \internal
205  * \brief
206  * Private implementation class for Options.
207  *
208  * Note that in addition to Options, the OptionsAssigner class also directly
209  * accesses this class.
210  *
211  * \ingroup module_options
212  */
213 class OptionsImpl
214 {
215 public:
216     OptionsImpl();
217
218     //! Option managers set for this collection.
219     OptionManagerContainer managers_;
220     //! Root section for this collection.
221     OptionSectionImpl rootSection_;
222 };
223
224 } // namespace internal
225
226 } // namespace gmx
227
228 #endif