Remove (part of) underscore-prefixed C++ symbols.
[alexxy/gromacs.git] / src / gromacs / options / optionsvisitor.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \libinternal \file
32  * \brief
33  * Declares gmx::OptionsVisitor interface and supporting classes.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \inlibraryapi
37  * \ingroup module_options
38  */
39 #ifndef GMX_OPTIONS_OPTIONSVISITOR_H
40 #define GMX_OPTIONS_OPTIONSVISITOR_H
41
42 #include <cstddef>
43
44 #include <string>
45
46 #include "../utility/common.h"
47
48 #include "optioninfo.h"
49
50 namespace gmx
51 {
52
53 class Options;
54
55 /*! \libinternal \brief
56  * Pure interface for visiting options in a Options object.
57  *
58  * \see OptionsIterator
59  *
60  * \inlibraryapi
61  * \ingroup module_options
62  */
63 class OptionsVisitor
64 {
65     public:
66         virtual ~OptionsVisitor() {}
67
68         /*! \brief
69          * Called for each subsection in Options.
70          */
71         virtual void visitSubSection(const Options &section) = 0;
72         /*! \brief
73          * Called for each option in Options.
74          */
75         virtual void visitOption(const OptionInfo &option) = 0;
76 };
77
78 /*! \libinternal \brief
79  * Abstract base class for visiting options of a particular type.
80  *
81  * \see OptionsIterator
82  * \see OptionsVisitor
83  *
84  * \inlibraryapi
85  * \ingroup module_options
86  */
87 template <class InfoType>
88 class OptionsTypeVisitor : public OptionsVisitor
89 {
90     public:
91         virtual ~OptionsTypeVisitor() {}
92
93         virtual void visitSubSection(const Options &section) = 0;
94         /*! \brief
95          * Called for each option of type \p InfoType.
96          */
97         virtual void visitOptionType(const InfoType &option) = 0;
98
99     private:
100         virtual void visitOption(const OptionInfo &option)
101         {
102             const InfoType *subtype = option.toType<InfoType>();
103             if (subtype != NULL)
104             {
105                 visitOptionType(*subtype);
106             }
107         }
108 };
109
110 /*! \libinternal \brief
111  * Decorator class for visiting options in a Options object.
112  *
113  * This class provides an interface for looping through subsections and
114  * options in a Options object.
115  *
116  * Typical use (loop over all options, iteratively descending into
117  * subsections):
118  * \code
119 class Visitor : public gmx::OptionsVisitor
120 {
121     public:
122         void visitSubSection(const Options &section)
123         {
124             OptionsIterator iterator(section);
125             iterator.acceptSubSections(this);
126             iterator.acceptOptions(this);
127         }
128
129         void visitOption(const OptionInfo &option)
130         {
131             // Do something.
132         }
133 }
134
135 Visitor().visitSubSection(options);
136  * \endcode
137  *
138  * \inlibraryapi
139  * \ingroup module_options
140  */
141 class OptionsIterator
142 {
143     public:
144         /*! \brief
145          * Creates an object for visiting options in a Options object.
146          */
147         explicit OptionsIterator(const Options &options);
148
149         /*! \brief
150          * Visits each subsection in the wrapped Options object.
151          */
152         void acceptSubSections(OptionsVisitor *visitor) const;
153         /*! \brief
154          * Visits each option in the wrapped Options object.
155          */
156         void acceptOptions(OptionsVisitor *visitor) const;
157
158     private:
159         //! The wrapped Options object.
160         const Options          &options_;
161
162         GMX_DISALLOW_COPY_AND_ASSIGN(OptionsIterator);
163 };
164
165 /*! \libinternal \brief
166  * Pure interface for visiting options in a Options object, allowing
167  * modifications.
168  *
169  * \see OptionsModifyingIterator
170  *
171  * \inlibraryapi
172  * \ingroup module_options
173  */
174 class OptionsModifyingVisitor
175 {
176     public:
177         virtual ~OptionsModifyingVisitor() {}
178
179         /*! \brief
180          * Called for each subsection in Options.
181          */
182         virtual void visitSubSection(Options *section) = 0;
183         /*! \brief
184          * Called for each option in Options.
185          */
186         virtual void visitOption(OptionInfo *option) = 0;
187 };
188
189 /*! \libinternal \brief
190  * Abstract base class for visiting options of a particular type, allowing
191  * modifications.
192  *
193  * \see OptionsModifyingIterator
194  * \see OptionsModifyingVisitor
195  *
196  * \inlibraryapi
197  * \ingroup module_options
198  */
199 template <class InfoType>
200 class OptionsModifyingTypeVisitor : public OptionsModifyingVisitor
201 {
202     public:
203         virtual ~OptionsModifyingTypeVisitor() {}
204
205         virtual void visitSubSection(Options *section) = 0;
206         /*! \brief
207          * Called for each option of type \p InfoType.
208          */
209         virtual void visitOptionType(InfoType *option) = 0;
210
211     private:
212         virtual void visitOption(OptionInfo *option)
213         {
214             InfoType *subtype = option->toType<InfoType>();
215             if (subtype != NULL)
216             {
217                 visitOptionType(subtype);
218             }
219         }
220 };
221
222 /*! \libinternal \brief
223  * Decorator class for visiting options in a Options object, allowing changes.
224  *
225  * This class works exactly like OptionsIterator, except that it uses
226  * OptionsModifyingVisitor interface, which allows modifying the options.
227  *
228  * \see OptionsIterator
229  *
230  * \inlibraryapi
231  * \ingroup module_options
232  */
233 class OptionsModifyingIterator
234 {
235     public:
236         /*! \brief
237          * Creates an object for visiting options in a Options object.
238          */
239         explicit OptionsModifyingIterator(Options *options);
240
241         /*! \brief
242          * Visits each subsection in the wrapped Options object.
243          */
244         void acceptSubSections(OptionsModifyingVisitor *visitor) const;
245         /*! \brief
246          * Visits each option in the wrapped Options object.
247          */
248         void acceptOptions(OptionsModifyingVisitor *visitor) const;
249
250     private:
251         //! The wrapped Options object.
252         Options                &options_;
253
254         GMX_DISALLOW_COPY_AND_ASSIGN(OptionsModifyingIterator);
255 };
256
257 } // namespace gmx
258
259 #endif