5c0fc026eb2b3aae9c9b84c96242502b6916b76e
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlineinit.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 functions for initializing the \Gromacs library for command line use.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_commandline
42  */
43 #ifndef GMX_COMMANDLINE_CMDLINEINIT_H
44 #define GMX_COMMANDLINE_CMDLINEINIT_H
45
46 #ifdef __cplusplus
47
48 // Forward declaration of class CommandLineProgramContext is not sufficient for
49 // MSVC if the return value of initForCommandLine() is ignored(!)
50 #include "cmdlineprogramcontext.h"
51
52 namespace gmx
53 {
54
55 class CommandLineModuleInterface;
56
57 /*! \brief
58  * Initializes the \Gromacs library for command-line use.
59  *
60  * \param[in] argc  argc value passed to main().
61  * \param[in] argv  argv array passed to main().
62  * \returns   Reference to initialized program context object.
63  *
64  * This function is tailored for use in command line applications.
65  * For other usage, combination of gmx::init() and gmx::setProgramContext()
66  * provides more flexible initialization alternatives.
67  * Unlike gmx::init(), calls to this method cannot be nested.
68  *
69  * The command line arguments are communicated so that they can be
70  * parsed on each processor.
71  * \p argc and \p argv are passed to gmx::init(); see there for additional
72  * discussion.  This method does not place any additional limitations, but
73  * generally there should be no need to pass NULL values.
74  *
75  * Does not throw. Terminates the program on out-of-memory error.
76  *
77  * This method is not thread-safe, since it is intended to be the first method
78  * called.  See setProgramContext() for additional discussion.
79  *
80  * \see gmx::init()
81  * \see setProgramContext()
82  * \ingroup module_commandline
83  */
84 CommandLineProgramContext &initForCommandLine(int *argc, char ***argv);
85 /*! \brief
86  * Deinitializes the \Gromacs library after initForCommandLine().
87  *
88  * Calls gmx::finalize() and additionally undoes the work done by
89  * initForCommandLine().
90  *
91  * \see gmx::finalize()
92  * \ingroup module_commandline
93  */
94 void finalizeForCommandLine();
95
96 /*! \brief
97  * Implements a main() method that runs a single module.
98  *
99  * \param argc   \c argc passed to main().
100  * \param argv   \c argv passed to main().
101  * \param module Module to run.
102  *
103  * This method allows for uniform behavior for binaries that only
104  * contain a single module without duplicating any of the
105  * implementation from CommandLineModuleManager (startup headers,
106  * common options etc.).
107  *
108  * The signature assumes that \p module construction does not throw
109  * (because otherwise the caller would need to duplicate all the
110  * exception handling code).  It is possible to move the construction
111  * inside the try/catch in this method using an indirection similar to
112  * TrajectoryAnalysisCommandLineRunner::runAsMain(), but until that is
113  * necessary, the current approach leads to simpler code.
114  *
115  * Usage:
116  * \code
117    int main(int argc, char *argv[])
118    {
119        CustomCommandLineModule module;
120        return gmx::runCommandLineModule(argc, argv, &module);
121    }
122    \endcode
123  *
124  * Does not throw.  All exceptions are caught and handled internally.
125  */
126 int runCommandLineModule(int argc, char *argv[],
127                          CommandLineModuleInterface *module);
128
129 } // namespace gmx
130
131 extern "C"
132 {
133 #endif
134
135 /*! \brief
136  * Implements a main() method that runs a given C main function.
137  *
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.
141  *
142  * This method creates a dummy command line module that does its
143  * processing by calling \p mainFunction.  It then runs this module as with
144  * gmx::runCommandLineModule().
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.
148  *
149  * \p mainFunction should call parse_common_args() to process its command-line
150  * arguments.
151  *
152  * Usage:
153  * \code
154    int my_main(int argc, char *argv[])
155    {
156        // <...>
157    }
158
159    int main(int argc, char *argv[])
160    {
161        return gmx_run_cmain(argc, argv, &my_main);
162    }
163    \endcode
164  *
165  * Does not throw.  All exceptions are caught and handled internally.
166  */
167 int gmx_run_cmain(int argc, char *argv[], int (*mainFunction)(int, char *[]));
168
169 #ifdef __cplusplus
170 }
171 #endif
172
173 #endif