Introduce SimulatorBuilder
[gromacs.git] / src / gromacs / commandline / cmdlinemodulemanager.h
blob7bdd30bf8eb718f8f6bbd174db0bd16e1f367989
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016, 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.
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.
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.
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.
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.
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.
35 /*! \libinternal \file
36 * \brief
37 * Declares gmx::CommandLineModuleManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \inlibraryapi
41 * \ingroup module_commandline
43 #ifndef GMX_COMMANDLINE_CMDLINEMODULEMANAGER_H
44 #define GMX_COMMANDLINE_CMDLINEMODULEMANAGER_H
46 #include <memory>
48 #include "gromacs/onlinehelp/ihelptopic.h"
49 #include "gromacs/utility/classhelpers.h"
51 namespace gmx
54 class CommandLineModuleGroup;
55 class CommandLineModuleGroupData;
56 class CommandLineModuleSettings;
57 class CommandLineProgramContext;
58 class ICommandLineModule;
59 class IFileOutputRedirector;
61 //! \addtogroup module_commandline
62 //! \{
64 //! Smart pointer type for managing a ICommandLineModule.
65 typedef std::unique_ptr<ICommandLineModule>
66 CommandLineModulePointer;
68 /*! \libinternal \brief
69 * Implements a wrapper command-line interface for multiple modules.
71 * Typical usage:
72 * \code
73 int main(int argc, char *argv[])
75 gmx::CommandLineProgramContext &programContext = gmx::initForCommandLine(&argc, &argv);
76 try
78 gmx::CommandLineModuleManager manager("gmx", &programContext);
79 // <register all necessary modules>
80 int rc = manager.run(argc, argv);
81 gmx::finalizeForCommandLine();
82 return rc;
84 catch (const std::exception &ex)
86 gmx::printFatalErrorMessage(stderr, ex);
87 return gmx::processExceptionAtExitForCommandLine(ex);
90 * \endcode
92 * \see page_wrapperbinary
93 * \inlibraryapi
95 class CommandLineModuleManager
97 public:
98 //! Function pointer type for a C main function.
99 typedef int (*CMainFunction)(int argc, char *argv[]);
100 //! Function pointer to a settings provider.
101 typedef void (*InitSettingsFunction)(CommandLineModuleSettings *settings);
103 /*! \brief
104 * Implements a main() method that runs a single module.
106 * \param argc \c argc passed to main().
107 * \param argv \c argv passed to main().
108 * \param module Module to run.
110 * This method allows for uniform behavior for binaries that only
111 * contain a single module without duplicating any of the
112 * implementation from CommandLineModuleManager (startup headers,
113 * common options etc.).
115 * The signature assumes that \p module construction does not throw
116 * (because otherwise the caller would need to duplicate all the
117 * exception handling code). It is possible to move the construction
118 * inside the try/catch in this method using an indirection similar to
119 * TrajectoryAnalysisCommandLineRunner::runAsMain(), but until that is
120 * necessary, the current approach leads to simpler code.
122 * Usage:
123 * \code
124 int main(int argc, char *argv[])
126 CustomCommandLineModule module;
127 return gmx::CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
129 * \endcode
131 * Does not throw. All exceptions are caught and handled internally.
133 static int runAsMainSingleModule(int argc, char *argv[],
134 ICommandLineModule *module);
135 /*! \brief
136 * Implements a main() method that runs a given function.
138 * \param argc \c argc passed to main().
139 * \param argv \c argv passed to main().
140 * \param mainFunction The main()-like method to wrap.
142 * This method creates a dummy command-line module that does its
143 * processing by calling \p mainFunction; see addModuleCMain() for
144 * details. It then runs this module with runAsMainSingleModule().
145 * This allows the resulting executable to handle common options and do
146 * other common actions (e.g., startup headers) without duplicate code
147 * in the main methods.
149 * Usage:
150 * \code
151 int my_main(int argc, char *argv[])
153 // <...>
156 int main(int argc, char *argv[])
158 return gmx::CommandLineModuleManager::runAsMainCMain(argc, argv, &my_main);
160 * \endcode
162 * Does not throw. All exceptions are caught and handled internally.
164 static int runAsMainCMain(int argc, char *argv[],
165 CMainFunction mainFunction);
166 /*! \brief
167 * Implements a main() method that runs a given function with custom
168 * settings.
170 * This method does the same as runAsMainCMain(), but additionally
171 * calls \p settingsFunction to initialize CommandLineModuleSettings.
172 * This allows specifying, e.g., a different default nice level.
174 static int runAsMainCMainWithSettings(int argc, char *argv[],
175 CMainFunction mainFunction,
176 InitSettingsFunction settingsFunction);
178 /*! \brief
179 * Initializes a command-line module manager.
181 * \param[in] binaryName Name of the running binary
182 * (without Gromacs binary suffix or .exe on Windows).
183 * \param programContext Program information for the running binary.
184 * \throws std::bad_alloc if out of memory.
186 * \p binaryName is used to detect when the binary is run through a
187 * symlink, and automatically invoke a matching module in such a case.
189 * \p programInfo is non-const to allow the manager to amend it based
190 * on the actual module that is getting executed.
192 CommandLineModuleManager(const char *binaryName,
193 CommandLineProgramContext *programContext);
194 ~CommandLineModuleManager();
196 /*! \brief
197 * Sets the module manager to quiet mode: don't print anything.
199 * \param[in] bQuiet Whether the module manager should remain silent.
201 * Normally, the module manager prints out some information to `stderr`
202 * before it starts the module and after it finishes. This removes
203 * that output, which is useful in particular for unit tests so that
204 * they don't spam `stderr`.
206 void setQuiet(bool bQuiet);
207 /*! \brief
208 * Redirects the output of the module manager to a file.
210 * \param[in] output File redirector to use for output.
212 * Normally, the module manager prints explicitly requested text such
213 * as help output to `stdout`, but this method can be used to redirect
214 * that output to a file. For exporting help from the module manager,
215 * several files are written, and can be redirected with this method as
216 * well.
218 * This is used for unit tests, either to keep them quiet or to verify
219 * that output. To keep implementation options open, behavior with
220 * `output == NULL` is undefined and should not be relied on.
221 * For tests, there should only be need to call this a single time,
222 * right after creating the manager.
224 void setOutputRedirector(IFileOutputRedirector *output);
226 /*! \brief
227 * Makes the manager always run a single module.
229 * \param module Module to run.
231 * This method disables all mechanisms for selecting a module, and
232 * directly passes all command-line arguments to \p module.
233 * Help arguments are an exception: these are still recognized by the
234 * manager and translated into a call to
235 * ICommandLineModule::writeHelp().
237 * This is public mainly for unit testing purposes; for other code,
238 * runAsMainSingleModule() typically provides the desired
239 * functionality.
241 * Does not throw.
243 void setSingleModule(ICommandLineModule *module);
244 /*! \brief
245 * Adds a given module to this manager.
247 * \param module Module to add.
248 * \throws std::bad_alloc if out of memory.
250 * The manager takes ownership of the object.
252 * This method is public mostly for testing purposes; for typical uses,
253 * registerModule() is a more convenient way of adding modules.
255 * \see registerModule()
257 void addModule(CommandLineModulePointer module);
258 /*! \brief
259 * Adds a module that runs a given main()-like function.
261 * \param[in] name Name for the module.
262 * \param[in] shortDescription One-line description for the module.
263 * \param[in] mainFunction Main function to wrap.
264 * \throws std::bad_alloc if out of memory.
266 * There is normally no need to call this method outside the Gromacs
267 * library. User code usually wants to use runAsMainCMain().
269 * \p name and \p shortDescription should be string constants, or the
270 * caller should otherwise ensure that they stay in scope for the
271 * duration the CommandLineModuleManager object exists.
272 * \p mainFunction should call parse_common_args() to process its
273 * command-line arguments.
275 void addModuleCMain(const char *name, const char *shortDescription,
276 CMainFunction mainFunction);
277 /*! \brief
278 * Adds a module that runs a given main()-like function with custom
279 * settings.
281 * This method does the same as runAsMainCMain(), but additionally
282 * calls \p settingsFunction to initialize CommandLineModuleSettings.
283 * This allows specifying, e.g., a different default nice level.
285 void addModuleCMainWithSettings(const char *name, const char *shortDescription,
286 CMainFunction mainFunction,
287 InitSettingsFunction settingsFunction);
288 /*! \brief
289 * Registers a module of a certain type to this manager.
291 * \tparam Module Type of module to register.
292 * \throws std::bad_alloc if out of memory.
294 * \p Module must be default-constructible and implement
295 * ICommandLineModule.
297 * This method is provided as a convenient alternative to addModule()
298 * for cases where each module is implemented by a different type
299 * (which should be the case for typical situations outside unit
300 * tests).
302 template <class Module>
303 void registerModule()
305 addModule(CommandLineModulePointer(new Module));
308 /*! \brief
309 * Adds a group for modules to use in help output.
311 * \param[in] title Short title for the group.
312 * \returns Handle that can be used to add modules to the group.
313 * \throws std::bad_alloc if out of memory.
315 * Creates a group that is used to structure the list of all modules in
316 * help output. Modules are added to the group using the returned
317 * object.
319 CommandLineModuleGroup addModuleGroup(const char *title);
321 /*! \brief
322 * Makes given help topic available through the manager's help module.
324 * \param[in] topic Help topic to add.
325 * \throws std::bad_alloc if out of memory.
327 * The manager takes ownership of the help topic.
329 void addHelpTopic(HelpTopicPointer topic);
331 /*! \brief
332 * Runs a module based on given command line.
334 * \param[in] argc Number of elements in \p argv.
335 * \param[in] argv Command-line arguments.
336 * \throws unspecified Throws any exception that the selected module
337 * throws.
338 * \returns Exit code for the program.
339 * \retval 0 on successful termination.
340 * \retval 2 if no module is specified, or if the module is not found.
342 * Runs the module whose name matches \p argv[1].
344 int run(int argc, char *argv[]);
346 private:
347 class Impl;
349 PrivateImplPointer<Impl> impl_;
352 /*! \libinternal \brief
353 * Handle to add content to a group added with
354 * CommandLineModuleManager::addModuleGroup().
356 * This class only provides a public interface to construct a module group for
357 * CommandLineModuleManager, and has semantics similar to a pointer: copies all
358 * point to the same group. The actual state of the group is maintained in an
359 * internal implementation class.
361 * \inlibraryapi
363 class CommandLineModuleGroup
365 public:
366 /*! \cond internal */
367 //! Shorthand for the implementation type that holds all the data.
368 typedef CommandLineModuleGroupData Impl;
370 //! Creates a new group (only called by CommandLineModuleManager).
371 explicit CommandLineModuleGroup(Impl *impl) : impl_(impl) {}
372 //! \endcond
374 /*! \brief
375 * Adds a module to this group.
377 * \param[in] name Name of the module.
378 * \throws std::bad_alloc if out of memory.
380 * This works as addModuleWithDescription(), but uses the short
381 * description of the module itself as the description.
383 * \see addModuleWithDescription()
385 void addModule(const char *name);
386 /*! \brief
387 * Adds a module to this group with a custom description.
389 * \param[in] name Name of the module.
390 * \param[in] description Description of the module in this group.
391 * \throws std::bad_alloc if out of memory.
393 * \p name must name a module added into the CommandLineModuleManager.
394 * It is possible to add the same module into multiple groups.
396 void addModuleWithDescription(const char *name, const char *description);
398 private:
399 //! Pointer to the data owned by CommandLineModuleManager.
400 Impl *impl_;
403 //! \}
405 } // namespace gmx
407 #endif