1e8eddb2c97749a7f4065f470b7e3317f8528f5e
[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  * Interface for adding input options.
54  *
55  * This interface provides methods to add new options.
56  * Standard usage is for code to receive this interface and populate it with
57  * supported options:
58  * \code
59    // <as class attributes>
60    std::string  arg1;
61    int          arg2;
62
63    void MyClass::initOptions(gmx::IOptionsContainer *options)
64    {
65        options->addOption(gmx::StringOption("arg1").store(&arg1));
66        options->addOption(gmx::IntegerOption("arg2").store(&arg2));
67    }
68    \endcode
69  * The caller can collect options from multiple sources into a single container
70  * (a gmx::Options), and use a parser implementation such as CommandLineParser
71  * to provide values for the options.
72  *
73  * Header basicoptions.h provides declarations of several standard
74  * option types for use with addOption().  Documentation of those classes
75  * also give more examples of how to define options.
76  *
77  * \inpublicapi
78  * \ingroup module_options
79  */
80 class IOptionsContainer
81 {
82     public:
83         /*! \brief
84          * Adds a recognized option.
85          *
86          * \param[in] settings Option description.
87          * \returns   OptionInfo object for the created option (never NULL).
88          * \throws    APIError if invalid option settings are provided.
89          *
90          * This method provides the internal implementation, but in most cases
91          * the templated method is called from user code.
92          * See the templated method for more details.
93          */
94         virtual OptionInfo *addOption(const AbstractOption &settings) = 0;
95         /*! \brief
96          * Adds a recognized option.
97          *
98          * \tparam    OptionType Type of the options description object.
99          * \param[in] settings   Option description.
100          * \returns   OptionInfo object for the created option (never NULL).
101          * \throws    APIError if invalid option settings are provided.
102          *
103          * The return value is a pointer for more convenient use in callers:
104          * often callers need to declare the variable that will hold the return
105          * value in wider scope than would be achieved by declaring it at the
106          * site where addOption() is called.
107          * The returned pointer must not be freed.
108          *
109          * See \link Options class documentation \endlink for example usage.
110          *
111          * \libinternal
112          * \p OptionType::InfoType must specify a type that derives from
113          * OptionInfo and matches the type that is returned by
114          * AbstractOptionStorage::optionInfo() for the storage object that
115          * corresponds to \p OptionType.
116          */
117         template <class OptionType>
118         typename OptionType::InfoType *addOption(const OptionType &settings)
119         {
120             OptionInfo *info
121                 = addOption(static_cast<const AbstractOption &>(settings));
122             GMX_ASSERT(info->isType<typename OptionType::InfoType>(),
123                        "Mismatching option info type declaration and implementation");
124             return info->toType<typename OptionType::InfoType>();
125         }
126
127     protected:
128         // Disallow deletion through the interface.
129         // (no need for the virtual, but some compilers warn otherwise)
130         virtual ~IOptionsContainer();
131
132 };
133
134 } // namespace
135
136 #endif