Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlineprogramcontext.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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::CommandLineProgramContext.
38  *
39  * This header is installed to support cmdlineinit.h because some compilers
40  * don't allow returning a reference to an incomplete type from a function.
41  * It should not be necessary to use gmx::CommandLineProgramContext outside the
42  * \Gromacs library.
43  *
44  * \author Teemu Murtola <teemu.murtola@gmail.com>
45  * \inlibraryapi
46  * \ingroup module_commandline
47  */
48 #ifndef GMX_COMMANDLINE_CMDLINEPROGRAMCONTEXT_H
49 #define GMX_COMMANDLINE_CMDLINEPROGRAMCONTEXT_H
50
51 #include <string>
52 #include <vector>
53
54 #include <boost/shared_ptr.hpp>
55
56 #include "gromacs/utility/common.h"
57 #include "gromacs/utility/programcontext.h"
58
59 namespace gmx
60 {
61
62 //! \addtogroup module_commandline
63 //! \{
64
65 /*! \libinternal \brief
66  * Allows customization of the way various directories are found by
67  * CommandLineProgramContext.
68  *
69  * For the CommandLineProgramContext constructors that do not take this
70  * interface as a parameter, a default implementation is used that forwards
71  * the calls to the corresponding methods in gmx::Path.
72  *
73  * \inlibraryapi
74  */
75 class ExecutableEnvironmentInterface
76 {
77     public:
78         virtual ~ExecutableEnvironmentInterface() {}
79
80         /*! \brief
81          * Returns the working directory when the program was launched.
82          */
83         virtual std::string getWorkingDirectory() const = 0;
84         /*! \brief
85          * Returns list of paths where executables are searched for.
86          *
87          * The returned list should be in priority order.  An empty string in
88          * the returned list corresponds to getWorkindDirectory().
89          */
90         virtual std::vector<std::string> getExecutablePaths() const = 0;
91 };
92
93 //! Shorthand for a smart pointer to ExecutableEnvironmentInterface.
94 typedef boost::shared_ptr<ExecutableEnvironmentInterface>
95     ExecutableEnvironmentPointer;
96
97 /*! \libinternal \brief
98  * Program context implementation for command line programs.
99  *
100  * Constructors are provided mostly for unit testing purposes; in normal usage,
101  * a single CommandLineProgramContext object is constructed with
102  * initForCommandLine() in the beginning of the program.
103  * The returned object can be explicitly passed to other methods, or accessed
104  * through getProgramContext().
105  *
106  * Unless explicitly noted otherwise, methods in this class may throw
107  * std::bad_alloc on out-of-memory conditions, but do not throw other
108  * exceptions.
109  *
110  * \inlibraryapi
111  */
112 class CommandLineProgramContext : public ProgramContextInterface
113 {
114     public:
115         /*! \brief
116          * Constructs an empty context object.
117          *
118          * All methods in the constructed object return dummy values.
119          */
120         CommandLineProgramContext();
121         /*! \brief
122          * Initializes a program context object with binary name only.
123          *
124          * \param[in] binaryName  Name of the binary.
125          *
126          * This is needed for unit testing purposes.
127          * The constructed object works as if the command line consisted of
128          * only of the binary name.
129          */
130         explicit CommandLineProgramContext(const char *binaryName);
131         /*! \brief
132          * Initializes a program context object based on command line.
133          *
134          * \param[in] argc  argc value passed to main().
135          * \param[in] argv  argv array passed to main().
136          */
137         CommandLineProgramContext(int argc, const char *const argv[]);
138         /*! \brief
139          * Initializes a program context object based on command line.
140          *
141          * \param[in] argc  argc value passed to main().
142          * \param[in] argv  argv array passed to main().
143          * \param[in] env   Customizes the way the binary name is handled.
144          *
145          * This overload allows one to customize the way the binary is located
146          * by providing a custom ExecutableEnvironmentInterface implementation.
147          * This is mainly useful for testing purposes to make it possible to
148          * test different paths without setting environment variables, changing
149          * the working directory or doing other process-wide operations.
150          * It may also be useful for making Gromacs behave better when linked
151          * into a non-Gromacs executable (with possible extensions in
152          * ExecutableEnvironmentInterface).
153          */
154         CommandLineProgramContext(int argc, const char *const argv[],
155                                   ExecutableEnvironmentPointer env);
156         virtual ~CommandLineProgramContext();
157
158         /*! \brief
159          * Sets a display name for the binary.
160          *
161          * \throws std::bad_alloc if out of memory.
162          *
163          * This is used with the wrapper binary to add the name of the invoked
164          * module to the name of the binary shown.
165          *
166          * It is not threadsafe if there are concurrent calls to displayName()
167          * before this method has returned.  Thread safety is not required for
168          * the normal initialization sequence of command line programs; it is
169          * called in the wrapper binary before the control passes to the actual
170          * module which may create threads.
171          */
172         void setDisplayName(const std::string &name);
173
174         /*! \brief
175          * Returns the name of the binary as it was invoked without any path.
176          *
177          * Does not throw.
178          */
179         virtual const char *programName() const;
180         /*! \brief
181          * Returns a display name of the current module.
182          *
183          * The returned value equals programName(), unless a separate display
184          * name has been set with setDisplayName().
185          *
186          * Does not throw.
187          */
188         virtual const char *displayName() const;
189         /*! \brief
190          * Returns the full path of the running binary.
191          *
192          * \throws std::bad_alloc if out of memory.
193          * \throws tMPI::system_error on thread synchronization errors.
194          *
195          * Returns argv[0] if there was an error in finding the absolute path.
196          */
197         virtual const char *fullBinaryPath() const;
198         /*! \brief
199          * Returns the default path for \Gromacs data files.
200          *
201          * \throws std::bad_alloc if out of memory.
202          * \throws tMPI::system_error on thread synchronization errors.
203          *
204          * Returns a hardcoded path set during configuration time if there is
205          * an error in finding the library data files.
206          */
207         virtual const char *defaultLibraryDataPath() const;
208         /*! \brief
209          * Returns the full command line used to invoke the binary.
210          *
211          * Does not throw.
212          */
213         virtual const char *commandLine() const;
214
215     private:
216         class Impl;
217
218         PrivateImplPointer<Impl> impl_;
219 };
220
221 } // namespace gmx
222
223 #endif