f44022fb081ed8ce30b411e5b5c6e6918585b475
[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::CommandLineOptionsModuleInterface 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 class CommandLineModuleInterface;
52 class CommandLineModuleManager;
53 class Options;
54
55 /*! \brief
56  * Module that can be run from a command line and uses gmx::Options for
57  * argument processing.
58  *
59  * This class provides a higher-level interface on top of
60  * gmx::CommandLineModuleInterface for cases where gmx::Options will be used
61  * for declaring the command-line arguments.  The module only needs to declare
62  * the options it uses, and the framework takes care of command-line parsing
63  * and help output.  The module typically consists of the following parts:
64  *  - init() allows for some interaction between the module and the framework
65  *    when running the module; see CommandLineModuleInterface::init().  If no
66  *    such customization is necessary, an empty implementation is sufficient.
67  *  - initOptions() is called both for running the module and for printing help
68  *    for the module, and it should add the options that the module
69  *    understands.  Values provided for the options are typically stored in
70  *    member variables.
71  *  - optionsFinished() can be implemented in case additional processing is
72  *    needed (e.g., checking whether an option was set by the user).
73  *  - run() is called when running the module, after command-line options have
74  *    been parsed and their values stored in the corresponding member
75  *    variables.
76  *
77  * registerModule(), runAsMain(), or createModule() can be used to use modules
78  * of this type in all contexts where a gmx::CommandLineModuleInterface is
79  * expected.  These methods create a gmx::CommandLineModuleInterface
80  * implementation that contains the common code needed to parse command-line
81  * options and write help, based on the information provided from the methods
82  * in this class.
83  *
84  * \inpublicapi
85  * \ingroup module_commandline
86  */
87 class CommandLineOptionsModuleInterface
88 {
89     public:
90         /*! \brief
91          * Function pointer to a factory method that returns an interface of
92          * this type.
93          *
94          * \returns Module to run (should be allocated with `new`).
95          * \throws  std::bad_alloc if out of memory.
96          *
97          * The caller takes responsibility to `delete` the returned pointer.
98          */
99         typedef CommandLineOptionsModuleInterface *(*FactoryMethod)();
100
101         /*! \brief
102          * Creates a CommandLineModuleInterface to run the specified module.
103          *
104          * \param[in] name        Name for the module.
105          * \param[in] description Short description for the module.
106          * \param[in] factory     Factory that returns the module to run.
107          * \returns CommandLineModuleInterface object that runs the module
108          *     returned by \p factory.  Caller must `delete` the object.
109          * \throws  std::bad_alloc if out of memory.
110          *
111          * This is mainly used by tests that want to bypass
112          * CommandLineModuleManager.
113          */
114         static CommandLineModuleInterface *
115         createModule(const char *name, const char *description,
116                      FactoryMethod factory);
117         /*! \brief
118          * Implements a main() method that runs a single module.
119          *
120          * \param     argc    \c argc passed to main().
121          * \param     argv    \c argv passed to main().
122          * \param[in] name        Name for the module.
123          * \param[in] description Short description for the module.
124          * \param[in] factory     Factory that returns the module to run.
125          *
126          * This method allows for uniform behavior for binaries that only
127          * contain a single module without duplicating any of the
128          * implementation from CommandLineModuleManager (startup headers,
129          * common options etc.).
130          *
131          * \see runCommandLineModule()
132          */
133         static int
134         runAsMain(int argc, char *argv[], const char *name,
135                   const char *description, FactoryMethod factory);
136         /*! \brief
137          * Registers a module of a certain type to this manager.
138          *
139          * \param     manager     Manager to register to.
140          * \param[in] name        Name for the module.
141          * \param[in] description Short description for the module.
142          * \param[in] factory     Factory that returns the module to register.
143          * \throws  std::bad_alloc if out of memory.
144          *
145          * This method internally creates a CommandLineModuleInterface module
146          * with the given \p name and \p description, and adds that to
147          * \p manager.  When run or asked to write the help, the module calls
148          * \p factory to get the actual module, and forwards the necessary
149          * calls.
150          */
151         static void
152         registerModule(CommandLineModuleManager *manager,
153                        const char *name, const char *description,
154                        FactoryMethod factory);
155         /*! \brief
156          * Registers a module to this manager.
157          *
158          * \param     manager     Manager to register to.
159          * \param[in] name        Name for the module.
160          * \param[in] description Short description for the module.
161          * \param[in] module      Module to register.
162          *     The method takes ownership (must have been allocated with `new`).
163          * \throws  std::bad_alloc if out of memory.
164          *
165          * This method internally creates a CommandLineModuleInterface module
166          * with the given \p name and \p description, and adds that to
167          * \p manager.
168          *
169          * This method is mainly used by tests that need to have a reference to
170          * the CommandLineOptionsModuleInterface instance (e.g., for mocking).
171          */
172         static void
173         registerModule(CommandLineModuleManager *manager,
174                        const char *name, const char *description,
175                        CommandLineOptionsModuleInterface *module);
176
177         virtual ~CommandLineOptionsModuleInterface();
178
179         //! \copydoc gmx::CommandLineModuleInterface::init()
180         virtual void init(CommandLineModuleSettings *settings) = 0;
181         /*! \brief
182          * Initializes command-line arguments understood by the module.
183          *
184          * \param[in,out] options  Options object to add the options to.
185          *
186          * When running the module, this method is called after init().
187          * When printing help, there is no call to init(), and this is the only
188          * method called.
189          * In both cases, the implementation should add options understood by
190          * the module to \p options.  Output values from options should be
191          * stored in member variables.
192          */
193         virtual void initOptions(Options *options)             = 0;
194         /*! \brief
195          * Called after all option values have been set.
196          *
197          * \param[in,out] options  Options object in which options are stored.
198          *
199          * When running the module, this method is called after all
200          * command-line arguments have been parsed, but while the Options
201          * object still exists.
202          *
203          * If the module needs to call, e.g., Options::isSet(), this is the
204          * place to do that.
205          */
206         virtual void optionsFinished(Options *options)         = 0;
207
208         /*! \brief
209          * Runs the module.
210          *
211          * \throws   unspecified  May throw exceptions to indicate errors.
212          * \returns  Exit code for the program.
213          * \retval   0 on successful termination.
214          *
215          * This method is called after optionsFinished() when running the
216          * module, and should do all the processing for the module.
217          */
218         virtual int run() = 0;
219 };
220
221 } // namespace gmx
222
223 #endif