Merge branch 'release-4-5-patches'
[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 <string>
43
44 namespace gmx
45 {
46
47 class AbstractOptionStorage;
48 class Options;
49
50 /*! \libinternal \brief
51  * Wrapper class for accessing option information.
52  *
53  * This class isolates the details of the internal option implementation
54  * from option visitors.
55  *
56  * \see OptionsVisitor
57  *
58  * \inlibraryapi
59  * \ingroup module_options
60  */
61 class OptionInfo
62 {
63     public:
64         /*! \brief
65          * Wraps a given option object.
66          */
67         OptionInfo(const AbstractOptionStorage &option);
68
69         //! Returns true if the option is a boolean option.
70         bool isBoolean() const;
71         //! Returns true if the option is a file name option.
72         bool isFile() const;
73         //! Returns true if the option is a hidden option.
74         bool isHidden() const;
75         //! Returns the name of the option.
76         const std::string &name() const;
77         //! Returns the description of the option.
78         const std::string &description() const;
79         //! Returns the type of the option as a string.
80         const char *type() const;
81         //! Returns the number of values given for the option.
82         int valueCount() const;
83         //! Returns the i'th value of the option as a string.
84         std::string formatValue(int i) const;
85         //! Returns all the values of the option as a single string.
86         std::string formatValues() const;
87
88     private:
89         //! The wrapped option.
90         const AbstractOptionStorage &_option;
91
92         // Disallow copy and assign.
93         OptionInfo(const OptionInfo &);
94         void operator =(const OptionInfo &);
95 };
96
97 /*! \libinternal \brief
98  * Pure interface for visiting options in a Options object.
99  *
100  * \see OptionsIterator
101  *
102  * \inlibraryapi
103  * \ingroup module_options
104  */
105 class OptionsVisitor
106 {
107     public:
108         virtual ~OptionsVisitor() {}
109
110         /*! \brief
111          * Called for each subsection in Options.
112          */
113         virtual void visitSubSection(const Options &section) = 0;
114         /*! \brief
115          * Called for each option in Options.
116          */
117         virtual void visitOption(const OptionInfo &option) = 0;
118 };
119
120 /*! \libinternal \brief
121  * Decorator class for visiting options in a Options object.
122  *
123  * This class provides an interface for looping through subsections and
124  * options in a Options object.
125  *
126  * Typical use (loop over all options, iteratively descending into
127  * subsections):
128  * \code
129 class Visitor : public gmx::options::OptionsVisitor
130 {
131     public:
132         void visitSubSection(const Options &section)
133         {
134             OptionsIterator iterator(section);
135             iterator.acceptSubSections(this);
136             iterator.acceptOptions(this);
137         }
138
139         void visitOption(const OptionInfo &option)
140         {
141             // Do something.
142         }
143 }
144
145 Visitor().visitSubSection(options);
146  * \endcode
147  *
148  * \inlibraryapi
149  * \ingroup module_options
150  */
151 class OptionsIterator
152 {
153     public:
154         /*! \brief
155          * Creates an object for visiting options in a Options object.
156          */
157         OptionsIterator(const Options &options);
158
159         /*! \brief
160          * Visits each subsection in the wrapped Options object.
161          */
162         void acceptSubSections(OptionsVisitor *visitor) const;
163         /*! \brief
164          * Visits each option in the wrapped Options object.
165          */
166         void acceptOptions(OptionsVisitor *visitor) const;
167
168     private:
169         //! The wrapped Options object.
170         const Options          &_options;
171
172         // Disallow copy and assign.
173         OptionsIterator(const OptionsIterator &);
174         void operator =(const OptionsIterator &);
175 };
176
177 } // namespace gmx
178
179 #endif