2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014 by the GROMACS development team.
5 * Copyright (c) 2015,2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Declares gmx::TrajectoryAnalysisCommandLineRunner.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_trajectoryanalysis
44 #ifndef GMX_TRAJECTORYANALYSIS_CMDLINERUNNER_H
45 #define GMX_TRAJECTORYANALYSIS_CMDLINERUNNER_H
50 #include "gromacs/trajectoryanalysis/analysismodule.h"
55 class CommandLineModuleManager
;
56 class ICommandLineOptionsModule
;
59 * Runner for command-line trajectory analysis tools.
61 * This class provides static methods to implement a command-line analysis
62 * program, given a TrajectoryAnalysisModule object (or a factory of such).
63 * It takes care of common command-line parameters, initializing and evaluating
64 * selections, and looping over trajectory frames.
67 * \ingroup module_trajectoryanalysis
69 class TrajectoryAnalysisCommandLineRunner
73 * Factory method type for creating a trajectory analysis module.
75 * This method allows the module creation to be postponed to the point
76 * where the module is needed, reducing initialization costs in, e.g.,
77 * the `gmx` binary, and simplifying exception handling.
79 typedef std::function
<TrajectoryAnalysisModulePointer()> ModuleFactoryMethod
;
82 * Implements a main() method that runs a given module.
84 * \tparam ModuleType Trajectory analysis module.
85 * \param argc \c argc passed to main().
86 * \param argv \c argv passed to main().
88 * This method abstracts away all the logic required to implement a
89 * main() method in user tools, allowing that to be changed without
90 * requiring changes to the tools themselves.
92 * \p ModuleType should be default-constructible and derive from
93 * TrajectoryAnalysisModule.
95 * Does not throw. All exceptions are caught and handled internally.
97 template<class ModuleType
>
98 static int runAsMain(int argc
, char* argv
[])
100 return runAsMain(argc
, argv
, &createModule
<ModuleType
>);
103 * Implements a main() method that runs a given module.
105 * \param argc \c argc passed to main().
106 * \param argv \c argv passed to main().
107 * \param factory Function that creates the module on demand.
109 * Implements the template runAsMain(), but can also be used
112 * Does not throw. All exceptions are caught and handled internally.
114 static int runAsMain(int argc
, char* argv
[], const ModuleFactoryMethod
& factory
);
116 * Registers a command-line module that runs a given module.
118 * \param manager Manager to register the module to.
119 * \param name Name of the module to register.
120 * \param description One-line description for the module to register.
121 * \param factory Function that creates the module on demand.
123 * \p name and \p descriptions must be string constants or otherwise
124 * stay valid for the duration of the program execution.
126 static void registerModule(CommandLineModuleManager
* manager
,
128 const char* description
,
129 const ModuleFactoryMethod
& factory
);
131 * Create a command-line module that runs the provided analysis module.
133 * \param[in] module Module to run.
134 * \returns Command-line module that runs the provided analysis
136 * \throws std::bad_alloc if out of memory.
138 * This is mainly provided for testing purposes that want to bypass
139 * CommandLineModuleManager.
141 static std::unique_ptr
<ICommandLineOptionsModule
> createModule(TrajectoryAnalysisModulePointer module
);
144 // Prevent instantiation.
145 TrajectoryAnalysisCommandLineRunner() {}
148 * Creates a trajectory analysis module of a given type.
150 * \tparam ModuleType Module to create.
152 template<class ModuleType
>
153 static TrajectoryAnalysisModulePointer
createModule()
155 return TrajectoryAnalysisModulePointer(new ModuleType());