Extract IOptionsContainer from Options
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlineoptionsmodule.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,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::ICommandLineOptionsModule and supporting routines.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_commandline
42  */
43 #ifndef GMX_COMMANDLINE_CMDLINEOPTIONSMODULE_H
44 #define GMX_COMMANDLINE_CMDLINEOPTIONSMODULE_H
45
46 #include "gromacs/commandline/cmdlinemodule.h"
47
48 namespace gmx
49 {
50
51 template <typename T> class ConstArrayRef;
52
53 class CommandLineModuleManager;
54 class ICommandLineModule;
55 class IOptionsContainer;
56 class Options;
57
58 /*! \brief
59  * Settings to pass information between a CommandLineOptionsModule and generic
60  * code that runs it.
61  *
62  * \inpublicapi
63  * \ingroup module_commandline
64  */
65 class ICommandLineOptionsModuleSettings
66 {
67     public:
68         /*! \brief
69          * Sets the help text for the module from string array.
70          *
71          * \param[in] help  String array to set as the description.
72          * \throws    std::bad_alloc if out of memory.
73          *
74          * Formatting for the help text is described on \ref page_onlinehelp.
75          *
76          * Example usage:
77          * \code
78            const char *const desc[] = {
79                "This is the description",
80                "for the options"
81            };
82
83            settings->setHelpText(desc);
84            \endcode
85          */
86         virtual void setHelpText(const ConstArrayRef<const char *> &help) = 0;
87
88     protected:
89         // Disallow deletion through the interface.
90         // (no need for the virtual, but some compilers warn otherwise)
91         virtual ~ICommandLineOptionsModuleSettings();
92 };
93
94 /*! \brief
95  * Module that can be run from a command line and uses gmx::Options for
96  * argument processing.
97  *
98  * This class provides a higher-level interface on top of
99  * gmx::ICommandLineModule for cases where gmx::Options will be used
100  * for declaring the command-line arguments.  The module only needs to declare
101  * the options it uses, and the framework takes care of command-line parsing
102  * and help output.  The module typically consists of the following parts:
103  *  - init() allows for some interaction between the module and the framework
104  *    when running the module; see ICommandLineModule::init().  If no
105  *    such customization is necessary, an empty implementation is sufficient.
106  *  - initOptions() is called both for running the module and for printing help
107  *    for the module, and it should add the options that the module
108  *    understands.  Values provided for the options are typically stored in
109  *    member variables.
110  *  - optionsFinished() can be implemented in case additional processing is
111  *    needed (e.g., checking whether an option was set by the user).
112  *  - run() is called when running the module, after command-line options have
113  *    been parsed and their values stored in the corresponding member
114  *    variables.
115  *
116  * registerModule(), runAsMain(), or createModule() can be used to use modules
117  * of this type in all contexts where a gmx::ICommandLineModule is
118  * expected.  These methods create a gmx::ICommandLineModule
119  * implementation that contains the common code needed to parse command-line
120  * options and write help, based on the information provided from the methods
121  * in this class.
122  *
123  * \inpublicapi
124  * \ingroup module_commandline
125  */
126 class ICommandLineOptionsModule
127 {
128     public:
129         /*! \brief
130          * Function pointer to a factory method that returns an interface of
131          * this type.
132          *
133          * \returns Module to run (should be allocated with `new`).
134          * \throws  std::bad_alloc if out of memory.
135          *
136          * The caller takes responsibility to `delete` the returned pointer.
137          */
138         typedef ICommandLineOptionsModule *(*FactoryMethod)();
139
140         /*! \brief
141          * Creates a ICommandLineModule to run the specified module.
142          *
143          * \param[in] name        Name for the module.
144          * \param[in] description Short description for the module.
145          * \param[in] factory     Factory that returns the module to run.
146          * \returns ICommandLineModule object that runs the module
147          *     returned by \p factory.  Caller must `delete` the object.
148          * \throws  std::bad_alloc if out of memory.
149          *
150          * This is mainly used by tests that want to bypass
151          * CommandLineModuleManager.
152          */
153         static ICommandLineModule *
154         createModule(const char *name, const char *description,
155                      FactoryMethod factory);
156         /*! \brief
157          * Implements a main() method that runs a single module.
158          *
159          * \param     argc    \c argc passed to main().
160          * \param     argv    \c argv passed to main().
161          * \param[in] name        Name for the module.
162          * \param[in] description Short description for the module.
163          * \param[in] factory     Factory that returns the module to run.
164          *
165          * This method allows for uniform behavior for binaries that only
166          * contain a single module without duplicating any of the
167          * implementation from CommandLineModuleManager (startup headers,
168          * common options etc.).
169          *
170          * \see runCommandLineModule()
171          */
172         static int
173         runAsMain(int argc, char *argv[], const char *name,
174                   const char *description, FactoryMethod factory);
175         /*! \brief
176          * Registers a module of a certain type to this manager.
177          *
178          * \param     manager     Manager to register to.
179          * \param[in] name        Name for the module.
180          * \param[in] description Short description for the module.
181          * \param[in] factory     Factory that returns the module to register.
182          * \throws  std::bad_alloc if out of memory.
183          *
184          * This method internally creates a ICommandLineModule module
185          * with the given \p name and \p description, and adds that to
186          * \p manager.  When run or asked to write the help, the module calls
187          * \p factory to get the actual module, and forwards the necessary
188          * calls.
189          */
190         static void
191         registerModule(CommandLineModuleManager *manager,
192                        const char *name, const char *description,
193                        FactoryMethod factory);
194         /*! \brief
195          * Registers a module to this manager.
196          *
197          * \param     manager     Manager to register to.
198          * \param[in] name        Name for the module.
199          * \param[in] description Short description for the module.
200          * \param[in] module      Module to register.
201          *     The method takes ownership (must have been allocated with `new`).
202          * \throws  std::bad_alloc if out of memory.
203          *
204          * This method internally creates a ICommandLineModule module
205          * with the given \p name and \p description, and adds that to
206          * \p manager.
207          *
208          * This method is mainly used by tests that need to have a reference to
209          * the ICommandLineOptionsModule instance (e.g., for mocking).
210          */
211         static void
212         registerModule(CommandLineModuleManager *manager,
213                        const char *name, const char *description,
214                        ICommandLineOptionsModule *module);
215
216         virtual ~ICommandLineOptionsModule();
217
218         //! \copydoc gmx::ICommandLineModule::init()
219         virtual void init(CommandLineModuleSettings *settings) = 0;
220         /*! \brief
221          * Initializes command-line arguments understood by the module.
222          *
223          * \param[in,out] options  Options object to add the options to.
224          * \param[in,out] settings Settings to communicate information
225          *     to/from generic code running the module.
226          *
227          * When running the module, this method is called after init().
228          * When printing help, there is no call to init(), and this is the only
229          * method called.
230          * In both cases, the implementation should add options understood by
231          * the module to \p options.  Output values from options should be
232          * stored in member variables.
233          */
234         virtual void initOptions(IOptionsContainer                 *options,
235                                  ICommandLineOptionsModuleSettings *settings) = 0;
236         /*! \brief
237          * Called after all option values have been set.
238          *
239          * \param[in,out] options  Options object in which options are stored.
240          *
241          * When running the module, this method is called after all
242          * command-line arguments have been parsed, but while the Options
243          * object still exists.
244          *
245          * If the module needs to call, e.g., Options::isSet(), this is the
246          * place to do that.
247          */
248         virtual void optionsFinished(Options *options) = 0;
249
250         /*! \brief
251          * Runs the module.
252          *
253          * \throws   unspecified  May throw exceptions to indicate errors.
254          * \returns  Exit code for the program.
255          * \retval   0 on successful termination.
256          *
257          * This method is called after optionsFinished() when running the
258          * module, and should do all the processing for the module.
259          */
260         virtual int run() = 0;
261 };
262
263 } // namespace gmx
264
265 #endif