d6ac5a5474b4e5b11d7ecf6f28c8f880d8d06a28
[alexxy/gromacs.git] / src / gromacs / options / optionsvisitor.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2014,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 /*! \libinternal \file
36  * \brief
37  * Declares gmx::OptionsVisitor interface and supporting classes.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_options
42  */
43 #ifndef GMX_OPTIONS_OPTIONSVISITOR_H
44 #define GMX_OPTIONS_OPTIONSVISITOR_H
45
46 #include <cstddef>
47
48 #include <string>
49
50 #include "gromacs/options/abstractoption.h"
51 #include "gromacs/utility/classhelpers.h"
52
53 namespace gmx
54 {
55
56 class Options;
57 class OptionSectionInfo;
58
59 namespace internal
60 {
61 class OptionSectionImpl;
62 }
63
64 /*! \libinternal \brief
65  * Pure interface for visiting options in a Options object.
66  *
67  * \see OptionsIterator
68  *
69  * \inlibraryapi
70  * \ingroup module_options
71  */
72 class OptionsVisitor
73 {
74 public:
75     virtual ~OptionsVisitor() {}
76
77     //! Called for each section.
78     virtual void visitSection(const OptionSectionInfo& section) = 0;
79     //! Called for each option.
80     virtual void visitOption(const OptionInfo& option) = 0;
81 };
82
83 /*! \libinternal \brief
84  * Abstract base class for visiting options of a particular type.
85  *
86  * \see OptionsIterator
87  * \see OptionsVisitor
88  *
89  * \inlibraryapi
90  * \ingroup module_options
91  */
92 template<class InfoType>
93 class OptionsTypeVisitor : public OptionsVisitor
94 {
95 public:
96     ~OptionsTypeVisitor() override {}
97
98     void visitSection(const OptionSectionInfo& section) override = 0;
99     /*! \brief
100      * Called for each option of type \p InfoType.
101      */
102     virtual void visitOptionType(const InfoType& option) = 0;
103
104 private:
105     void visitOption(const OptionInfo& option) override
106     {
107         const InfoType* subtype = option.toType<InfoType>();
108         if (subtype != NULL)
109         {
110             visitOptionType(*subtype);
111         }
112     }
113 };
114
115 /*! \libinternal \brief
116  * Decorator class for visiting options in a Options object.
117  *
118  * This class provides an interface for looping through sections and
119  * options in a Options object.
120  *
121  * Typical use (loop over all options, iteratively descending into
122  * subsections):
123  * \code
124    class Visitor : public gmx::OptionsVisitor
125    {
126        public:
127            virtual void visitSection(const OptionSectionInfo &section)
128            {
129                OptionsIterator iterator(section);
130                iterator.acceptSections(this);
131                iterator.acceptOptions(this);
132            }
133
134            virtual void visitOption(const OptionInfo &option)
135            {
136                // Do something.
137            }
138    }
139
140    Visitor().visitSection(options.rootSection());
141  * \endcode
142  *
143  * \inlibraryapi
144  * \ingroup module_options
145  */
146 class OptionsIterator
147 {
148 public:
149     /*! \brief
150      * Creates an object for visiting options in a Options object.
151      *
152      * The created iterator iterates over the "root" section in the Options
153      * object.
154      */
155     explicit OptionsIterator(const Options& options);
156     //! Creates an object for visiting options in an options section.
157     explicit OptionsIterator(const OptionSectionInfo& section);
158
159     //! Visits each section in the wrapped section.
160     void acceptSections(OptionsVisitor* visitor) const;
161     //! Visits each option in the wrapped section.
162     void acceptOptions(OptionsVisitor* visitor) const;
163
164 private:
165     //! The wrapped section object.
166     const internal::OptionSectionImpl& section_;
167
168     GMX_DISALLOW_COPY_AND_ASSIGN(OptionsIterator);
169 };
170
171 /*! \libinternal \brief
172  * Pure interface for visiting options in a Options object, allowing
173  * modifications.
174  *
175  * \see OptionsModifyingIterator
176  *
177  * \inlibraryapi
178  * \ingroup module_options
179  */
180 class OptionsModifyingVisitor
181 {
182 public:
183     virtual ~OptionsModifyingVisitor() {}
184
185     //! Called for each section.
186     virtual void visitSection(OptionSectionInfo* section) = 0;
187     //! Called for each option.
188     virtual void visitOption(OptionInfo* option) = 0;
189 };
190
191 /*! \libinternal \brief
192  * Abstract base class for visiting options of a particular type, allowing
193  * modifications.
194  *
195  * \see OptionsModifyingIterator
196  * \see OptionsModifyingVisitor
197  *
198  * \inlibraryapi
199  * \ingroup module_options
200  */
201 template<class InfoType>
202 class OptionsModifyingTypeVisitor : public OptionsModifyingVisitor
203 {
204 public:
205     ~OptionsModifyingTypeVisitor() override {}
206
207     void visitSection(OptionSectionInfo* section) override = 0;
208     /*! \brief
209      * Called for each option of type \p InfoType.
210      */
211     virtual void visitOptionType(InfoType* option) = 0;
212
213 private:
214     void visitOption(OptionInfo* option) override
215     {
216         InfoType* subtype = option->toType<InfoType>();
217         if (subtype != nullptr)
218         {
219             visitOptionType(subtype);
220         }
221     }
222 };
223
224 /*! \libinternal \brief
225  * Decorator class for visiting options in a Options object, allowing changes.
226  *
227  * This class works exactly like OptionsIterator, except that it uses
228  * OptionsModifyingVisitor interface, which allows modifying the options.
229  *
230  * \see OptionsIterator
231  *
232  * \inlibraryapi
233  * \ingroup module_options
234  */
235 class OptionsModifyingIterator
236 {
237 public:
238     /*! \brief
239      * Creates an object for visiting options in a Options object.
240      *
241      * The created iterator iterates over the "root" section in the Options
242      * object.
243      */
244     explicit OptionsModifyingIterator(Options* options);
245     //! Creates an object for visiting options in an options section.
246     explicit OptionsModifyingIterator(OptionSectionInfo* section);
247
248     //! Visits each section in the wrapped section.
249     void acceptSections(OptionsModifyingVisitor* visitor) const;
250     //! Visits each option in the wrapped section.
251     void acceptOptions(OptionsModifyingVisitor* visitor) const;
252
253 private:
254     //! The wrapped section object.
255     internal::OptionSectionImpl& section_;
256
257     GMX_DISALLOW_COPY_AND_ASSIGN(OptionsModifyingIterator);
258 };
259
260 } // namespace gmx
261
262 #endif