cmdlinerunner.cpp: use ICommandLineOptionsModule
[alexxy/gromacs.git] / src / gromacs / options / ioptionscontainer.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015, 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 /*! \file
36  * \brief
37  * Declares gmx::IOptionsContainer.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_options
42  */
43 #ifndef GMX_OPTIONS_IOPTIONSCONTAINER_H
44 #define GMX_OPTIONS_IOPTIONSCONTAINER_H
45
46 #include "gromacs/options/abstractoption.h"
47 #include "gromacs/utility/gmxassert.h"
48
49 namespace gmx
50 {
51
52 /*! \brief
53  * Base class for option managers.
54  *
55  * This class is used as a marker for all classes that are used with
56  * Options::addManager().  It doesn't provide any methods, but only supports
57  * transporting these classes through the Options collection into the
58  * individual option implementation classes.
59  *
60  * The virtual destructor is present to make this class polymorphic, such that
61  * `dynamic_cast` can be used when retrieving a manager of a certain type for
62  * the individual options.
63  *
64  * \inlibraryapi
65  * \ingroup module_options
66  */
67 class IOptionManager
68 {
69     protected:
70         virtual ~IOptionManager();
71 };
72
73 /*! \brief
74  * Interface for adding input options.
75  *
76  * This interface provides methods to add new options.
77  * Standard usage is for code to receive this interface and populate it with
78  * supported options:
79  * \code
80    // <as class attributes>
81    std::string  arg1;
82    int          arg2;
83
84    void MyClass::initOptions(gmx::IOptionsContainer *options)
85    {
86        options->addOption(gmx::StringOption("arg1").store(&arg1));
87        options->addOption(gmx::IntegerOption("arg2").store(&arg2));
88    }
89    \endcode
90  * The caller can collect options from multiple sources into a single container
91  * (a gmx::Options), and use a parser implementation such as CommandLineParser
92  * to provide values for the options.
93  *
94  * Header basicoptions.h provides declarations of several standard
95  * option types for use with addOption().  Documentation of those classes
96  * also give more examples of how to define options.
97  *
98  * \inpublicapi
99  * \ingroup module_options
100  */
101 class IOptionsContainer
102 {
103     public:
104         /*! \brief
105          * Adds a recognized option.
106          *
107          * \param[in] settings Option description.
108          * \returns   OptionInfo object for the created option (never NULL).
109          * \throws    APIError if invalid option settings are provided.
110          *
111          * This method provides the internal implementation, but in most cases
112          * the templated method is called from user code.
113          * See the templated method for more details.
114          */
115         virtual OptionInfo *addOption(const AbstractOption &settings) = 0;
116         /*! \brief
117          * Adds a recognized option.
118          *
119          * \tparam    OptionType Type of the options description object.
120          * \param[in] settings   Option description.
121          * \returns   OptionInfo object for the created option (never NULL).
122          * \throws    APIError if invalid option settings are provided.
123          *
124          * The return value is a pointer for more convenient use in callers:
125          * often callers need to declare the variable that will hold the return
126          * value in wider scope than would be achieved by declaring it at the
127          * site where addOption() is called.
128          * The returned pointer must not be freed.
129          *
130          * See \link Options class documentation \endlink for example usage.
131          *
132          * \libinternal
133          * \p OptionType::InfoType must specify a type that derives from
134          * OptionInfo and matches the type that is returned by
135          * AbstractOptionStorage::optionInfo() for the storage object that
136          * corresponds to \p OptionType.
137          */
138         template <class OptionType>
139         typename OptionType::InfoType *addOption(const OptionType &settings)
140         {
141             OptionInfo *info
142                 = addOption(static_cast<const AbstractOption &>(settings));
143             GMX_ASSERT(info->isType<typename OptionType::InfoType>(),
144                        "Mismatching option info type declaration and implementation");
145             return info->toType<typename OptionType::InfoType>();
146         }
147
148         /*! \brief
149          * Adds an option manager.
150          *
151          * \param    manager Manager to add.
152          * \throws   std::bad_alloc if out of memory.
153          *
154          * Option managers are used by some types of options that require
155          * interaction between different option instances (e.g., selection
156          * options), or need to support globally set properties (e.g., a global
157          * default file prefix).  Option objects can retrieve the pointer to
158          * their manager when they are created, and the caller can alter the
159          * behavior of the options through the manager.
160          * See the individual managers for details.
161          *
162          * Caller is responsible for memory management of \p manager.
163          * The Options object (and its contained options) only stores a
164          * reference to the object.
165          *
166          * This method cannot be called after adding options or subsections.
167          */
168         virtual void addManager(IOptionManager *manager) = 0;
169
170         /*! \brief
171          * Creates a subgroup of options within the current options.
172          *
173          * To add options to the group, use the returned interface.
174          *
175          * Currently, this is only used to influence the order of options:
176          * all options in a group appear before options in a group added after
177          * it, no matter in which order the options are added to the groups.
178          * In the future, the groups could also be used to influence the help
179          * output.
180          */
181         virtual IOptionsContainer &addGroup() = 0;
182     protected:
183         // Disallow deletion through the interface.
184         // (no need for the virtual, but some compilers warn otherwise)
185         virtual ~IOptionsContainer();
186
187 };
188
189 } // namespace
190
191 #endif