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         // This is a workaround for a buggy gcc 4.2, which seems to 
97         // require access to all constructors: 
98         friend class OptionsIterator;
99 };
100
101 /*! \libinternal \brief
102  * Pure interface for visiting options in a Options object.
103  *
104  * \see OptionsIterator
105  *
106  * \inlibraryapi
107  * \ingroup module_options
108  */
109 class OptionsVisitor
110 {
111     public:
112         virtual ~OptionsVisitor() {}
113
114         /*! \brief
115          * Called for each subsection in Options.
116          */
117         virtual void visitSubSection(const Options &section) = 0;
118         /*! \brief
119          * Called for each option in Options.
120          */
121         virtual void visitOption(const OptionInfo &option) = 0;
122 };
123
124 /*! \libinternal \brief
125  * Decorator class for visiting options in a Options object.
126  *
127  * This class provides an interface for looping through subsections and
128  * options in a Options object.
129  *
130  * Typical use (loop over all options, iteratively descending into
131  * subsections):
132  * \code
133 class Visitor : public gmx::options::OptionsVisitor
134 {
135     public:
136         void visitSubSection(const Options &section)
137         {
138             OptionsIterator iterator(section);
139             iterator.acceptSubSections(this);
140             iterator.acceptOptions(this);
141         }
142
143         void visitOption(const OptionInfo &option)
144         {
145             // Do something.
146         }
147 }
148
149 Visitor().visitSubSection(options);
150  * \endcode
151  *
152  * \inlibraryapi
153  * \ingroup module_options
154  */
155 class OptionsIterator
156 {
157     public:
158         /*! \brief
159          * Creates an object for visiting options in a Options object.
160          */
161         OptionsIterator(const Options &options);
162
163         /*! \brief
164          * Visits each subsection in the wrapped Options object.
165          */
166         void acceptSubSections(OptionsVisitor *visitor) const;
167         /*! \brief
168          * Visits each option in the wrapped Options object.
169          */
170         void acceptOptions(OptionsVisitor *visitor) const;
171
172     private:
173         //! The wrapped Options object.
174         const Options          &_options;
175
176         // Disallow copy and assign.
177         OptionsIterator(const OptionsIterator &);
178         void operator =(const OptionsIterator &);
179 };
180
181 } // namespace gmx
182
183 #endif