9bcc013418d1ee58b9f30c31d73e5820388bf2ab
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / cmdlinerunner.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013,2014, 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::TrajectoryAnalysisCommandLineRunner.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_trajectoryanalysis
42  */
43 #ifndef GMX_TRAJECTORYANALYSIS_CMDLINERUNNER_H
44 #define GMX_TRAJECTORYANALYSIS_CMDLINERUNNER_H
45
46 #include "gromacs/trajectoryanalysis/analysismodule.h"
47 #include "gromacs/utility/classhelpers.h"
48
49 namespace gmx
50 {
51
52 class CommandLineModuleManager;
53 class CommandLineHelpContext;
54
55 /*! \brief
56  * Runner class for command-line analysis tools.
57  *
58  * This class implements a command-line analysis program, given a
59  * TrajectoryAnalysisModule object.  It takes care of converting command-line
60  * parameters to a form understood by the module, as well as parsing common
61  * options, initializing and evaluating selections, and looping over trajectory
62  * frames.
63  *
64  * \inpublicapi
65  * \ingroup module_trajectoryanalysis
66  */
67 class TrajectoryAnalysisCommandLineRunner
68 {
69     public:
70         /*! \brief
71          * Factory method type for creating a trajectory analysis module.
72          *
73          * This method allows the module creation to be postponed to be inside
74          * the try/catch block in runAsMain()/registerModule() implementation
75          * methods and still keep the implementation out of the header, making
76          * the ABI more stable.
77          */
78         typedef TrajectoryAnalysisModulePointer (*ModuleFactoryMethod)();
79
80         /*! \brief
81          * Factory functor class for creating a trajectory analysis module.
82          *
83          * Old compilers that still must be supported do not have std::function,
84          * so we have to implement runAsMain overload accepting a functor instead
85          * of a function pointer.
86          *
87          * This abstract class should be subclassed to be used. The main usage is for
88          * python bindings.
89          */
90         class ModuleFactoryFunctor {
91             public:
92                 /*! \brief
93                  * operator() that returns a pointer to valid TrajectoryAnalysisModule
94                  */
95                 virtual TrajectoryAnalysisModulePointer operator() () = 0;
96                 virtual ~ModuleFactoryFunctor() {};
97         };
98
99         /*! \brief
100          * Implements a main() method that runs a given module.
101          *
102          * \tparam ModuleType  Trajectory analysis module.
103          * \param  argc        \c argc passed to main().
104          * \param  argv        \c argv passed to main().
105          *
106          * This method abstracts away all the logic required to implement a
107          * main() method in user tools, allowing that to be changed without
108          * requiring changes to the tools themselves.
109          *
110          * \p ModuleType should be default-constructible and derive from
111          * TrajectoryAnalysisModule.
112          *
113          * Does not throw.  All exceptions are caught and handled internally.
114          */
115         template <class ModuleType>
116         static int runAsMain(int argc, char *argv[])
117         {
118             return runAsMain(argc, argv, &createModule<ModuleType>);
119         }
120         /*! \brief
121          * Registers a command-line module that runs a given module.
122          *
123          * \tparam ModuleType  Trajectory analysis module.
124          * \param  manager     Manager to register the module to.
125          * \param  name        Name of the module to register.
126          * \param  description One-line description for the module to register.
127          *
128          * \p ModuleType should be default-constructible and derive from
129          * TrajectoryAnalysisModule.
130          *
131          * \p name and \p descriptions must be string constants or otherwise
132          * stay valid for the duration of the program execution.
133          */
134         template <class ModuleType>
135         static void registerModule(CommandLineModuleManager *manager,
136                                    const char *name, const char *description)
137         {
138             registerModule(manager, name, description, &createModule<ModuleType>);
139         }
140         /*! \brief
141          * Registers a command-line module that runs a given module.
142          *
143          * \tparam ModuleType  Trajectory analysis module.
144          * \param  manager     Manager to register the module to.
145          * \param  name        Name of the module to register.
146          * \param  description One-line description for the module to register.
147          * \param  factory     Function that creates the module on demand.
148          *
149          * \p name and \p descriptions must be string constants or otherwise
150          * stay valid for the duration of the program execution.
151          *
152          * Implements the template registerModule() method, but can also be
153          * used independently.
154          */
155         static void registerModule(CommandLineModuleManager *manager,
156                                    const char *name, const char *description,
157                                    ModuleFactoryMethod factory);
158
159         /*! \brief
160          * Create a new runner with the provided module.
161          *
162          * \param  module  Analysis module to run using the runner.
163          * \throws std::bad_alloc if out of memory.
164          *
165          * The caller should ensure that the provided module is not destroyed
166          * while the runner exists.
167          */
168         TrajectoryAnalysisCommandLineRunner(TrajectoryAnalysisModule *module);
169         ~TrajectoryAnalysisCommandLineRunner();
170
171         /*! \brief
172          * Sets whether default index groups are initialized.
173          *
174          * This is intended only for internal unit testing purposes to avoid
175          * repeated, unnecessary initialization of the default groups, which
176          * can be expensive under, e.g., valgrind.
177          *
178          * Does not throw.
179          */
180         void setUseDefaultGroups(bool bUseDefaults);
181         /*! \brief
182          * Sets the default debugging level for selections.
183          *
184          * \param[in] debuglevel  Level of debugging verbosity.
185          *
186          * This is intended only for use by internal debugging tools.
187          *
188          * Does not throw.
189          *
190          * \see SelectionCollection::setDebugLevel()
191          */
192         void setSelectionDebugLevel(int debuglevel);
193         /*! \brief
194          * Parses options from the given command line and runs the analysis.
195          *
196          * \throws  multiple  Exceptions are used to indicate errors.
197          * \returns Zero on success.
198          */
199         int run(int argc, char *argv[]);
200         /*! \brief
201          * Prints help for the module, including common options from the runner.
202          *
203          * \param[in] context  Context object for writing the help.
204          * \throws    std::bad_alloc if out of memory.
205          * \throws    FileIOError on any I/O error.
206          */
207         void writeHelp(const CommandLineHelpContext &context);
208
209         //! Implements the template runAsMain() method.
210         static int runAsMain(int argc, char *argv[],
211                              ModuleFactoryMethod factory);
212         //! Overload of runAsMain accepting functor.
213         static int runAsMain(int argc, char *argv[],
214                              ModuleFactoryFunctor *factory);
215     private:
216         /*! \brief
217          * Creates a trajectory analysis module of a given type.
218          *
219          * \tparam ModuleType  Module to create.
220          */
221         template <class ModuleType>
222         static TrajectoryAnalysisModulePointer createModule()
223         {
224             return TrajectoryAnalysisModulePointer(new ModuleType());
225         }
226
227
228         class Impl;
229
230         PrivateImplPointer<Impl> impl_;
231 };
232
233 } // namespace gmx
234
235 #endif