Merge branch 'release-4-6'
[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, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * 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 "../utility/common.h"
47 #include "../utility/uniqueptr.h"
48
49 namespace gmx
50 {
51
52 class CommandLineModuleManager;
53 class CommandLineHelpContext;
54 class TrajectoryAnalysisModule;
55
56 /*! \brief
57  * Runner class for command-line analysis tools.
58  *
59  * This class implements a command-line analysis program, given a
60  * TrajectoryAnalysisModule object.  It takes care of converting command-line
61  * parameters to a form understood by the module, as well as parsing common
62  * options, initializing and evaluating selections, and looping over trajectory
63  * frames.
64  *
65  * \inpublicapi
66  * \ingroup module_trajectoryanalysis
67  */
68 class TrajectoryAnalysisCommandLineRunner
69 {
70     public:
71         /*! \brief
72          * Implements a main() method that runs a given module.
73          *
74          * \tparam ModuleType  Trajectory analysis module.
75          * \param  argc        \c argc passed to main().
76          * \param  argv        \c argv passed to main().
77          *
78          * This method abstracts away all the logic required to implement a
79          * main() method in user tools, allowing that to be changed without
80          * requiring changes to the tools themselves.
81          *
82          * \p ModuleType should be default-constructible and derive from
83          * TrajectoryAnalysisModule.
84          *
85          * Does not throw.  All exceptions are caught and handled internally.
86          */
87         template <class ModuleType>
88         static int runAsMain(int argc, char *argv[])
89         {
90             return runAsMain(argc, argv, &createModule<ModuleType>);
91         }
92         /*! \brief
93          * Registers a command-line module that runs a given module.
94          *
95          * \tparam ModuleType  Trajectory analysis module.
96          * \param  manager     Manager to register the module to.
97          * \param  name        Name of the module to register.
98          * \param  description One-line description for the module to register.
99          *
100          * \p ModuleType should be default-constructible and derive from
101          * TrajectoryAnalysisModule.
102          *
103          * \p name and \p descriptions must be string constants or otherwise
104          * stay valid for the duration of the program execution.
105          */
106         template <class ModuleType>
107         static void registerModule(CommandLineModuleManager *manager,
108                                    const char *name, const char *description)
109         {
110             registerModule(manager, name, description, &createModule<ModuleType>);
111         }
112
113         /*! \brief
114          * Create a new runner with the provided module.
115          *
116          * \param  module  Analysis module to run using the runner.
117          * \throws std::bad_alloc if out of memory.
118          *
119          * The caller should ensure that the provided module is not destroyed
120          * while the runner exists.
121          */
122         TrajectoryAnalysisCommandLineRunner(TrajectoryAnalysisModule *module);
123         ~TrajectoryAnalysisCommandLineRunner();
124
125         /*! \brief
126          * Sets the default debugging level for selections.
127          *
128          * This is intended only for use by internal debugging tools.
129          *
130          * Does not throw.
131          *
132          * \see SelectionCollection::setDebugLevel()
133          */
134         void setSelectionDebugLevel(int debuglevel);
135         /*! \brief
136          * Parses options from the given command line and runs the analysis.
137          *
138          * \throws  multiple  Exceptions are used to indicate errors.
139          * \returns Zero on success.
140          */
141         int run(int argc, char *argv[]);
142         /*! \brief
143          * Prints help for the module, including common options from the runner.
144          *
145          * \param[in] context  Context object for writing the help.
146          * \throws    std::bad_alloc if out of memory.
147          * \throws    FileIOError on any I/O error.
148          */
149         void writeHelp(const CommandLineHelpContext &context);
150
151     private:
152         //! Smart pointer type for managing a trajectory analysis module.
153         typedef gmx_unique_ptr<TrajectoryAnalysisModule>::type
154             TrajectoryAnalysisModulePointer;
155
156         /*! \brief
157          * Factory method type for creating a trajectory analysis module.
158          *
159          * This method allows the module creation to be postponed to be inside
160          * the try/catch block in runAsMain()/registerModule() implementation
161          * methods and still keep the implementation out of the header, making
162          * the ABI more stable.
163          */
164         typedef TrajectoryAnalysisModulePointer (*ModuleFactoryMethod)();
165         /*! \brief
166          * Creates a trajectory analysis module of a given type.
167          *
168          * \tparam ModuleType  Module to create.
169          */
170         template <class ModuleType>
171         static TrajectoryAnalysisModulePointer createModule()
172         {
173             return TrajectoryAnalysisModulePointer(new ModuleType());
174         }
175
176         //! Implements the template runAsMain() method.
177         static int runAsMain(int argc, char *argv[],
178                              ModuleFactoryMethod factory);
179         //! Implements the template registerModule() method.
180         static void registerModule(CommandLineModuleManager *manager,
181                                    const char *name, const char *description,
182                                    ModuleFactoryMethod factory);
183
184         class Impl;
185
186         PrivateImplPointer<Impl> impl_;
187 };
188
189 } // namespace gmx
190
191 #endif