7f9cfa191d19b6e96747d0c486d4cfe876ab5db6
[alexxy/gromacs.git] / src / gromacs / utility / programinfo.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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::ProgramInfo.
38  *
39  * This header is installed to support init.h because some compilers don't
40  * allow returning a reference to an incomplete type from a function.
41  * It should not be necessary to use gmx::ProgramInfo outside the Gromacs
42  * library.
43  *
44  * \author Teemu Murtola <teemu.murtola@gmail.com>
45  * \inlibraryapi
46  * \ingroup module_utility
47  */
48 #ifndef GMX_UTILITY_PROGRAMINFO_H
49 #define GMX_UTILITY_PROGRAMINFO_H
50
51 #include <string>
52 #include <vector>
53
54 #include "common.h"
55 #include "uniqueptr.h"
56
57 namespace gmx
58 {
59
60 //! \addtogroup module_utility
61 //! \{
62
63 /*! \libinternal \brief
64  * Allows customization of the way various directories are found by
65  * ProgramInfo.
66  *
67  * For the ProgramInfo constructors that do not take this interface as a
68  * parameter, a default implementation is used that forwards the calls to the
69  * corresponding methods in gmx::Path.
70  *
71  * \inlibraryapi
72  */
73 class ExecutableEnvironmentInterface
74 {
75     public:
76         virtual ~ExecutableEnvironmentInterface() {}
77
78         /*! \brief
79          * Returns the working directory when the program was launched.
80          */
81         virtual std::string getWorkingDirectory() const = 0;
82         /*! \brief
83          * Returns list of paths where executables are searched for.
84          *
85          * The returned list should be in priority order.  An empty string in
86          * the returned list corresponds to getWorkindDirectory().
87          */
88         virtual std::vector<std::string> getExecutablePaths() const = 0;
89 };
90
91 //! Shorthand for a smart pointer to ExecutableEnvironmentInterface.
92 typedef gmx_unique_ptr<ExecutableEnvironmentInterface>::type
93     ExecutableEnvironmentPointer;
94
95 /*! \libinternal \brief
96  * Helper class for managing information about the running binary.
97  *
98  * This class provides access to the name of the binary currently running, as
99  * well as other information derived from it.
100  *
101  * ProgramInfo::init() should be called before any other (C++) Gromacs calls in
102  * a command-line program, as the information is used for printing error
103  * messages.
104  *
105  * Constructors are provided mostly for unit testing purposes; in normal usage,
106  * a single ProgramInfo object is constructed with init() in the beginning of
107  * the program.  The returned object can be explicitly passed to other methods,
108  * or accessed through getInstance().
109  *
110  * Unless explicitly noted otherwise, methods in this class may throw
111  * std::bad_alloc on out-of-memory conditions, but do not throw other
112  * exceptions.
113  *
114  * \inlibraryapi
115  */
116 class ProgramInfo
117 {
118     public:
119         /*! \brief
120          * Returns the singleton ProgramInfo object.
121          *
122          * \returns The same object as initialized with the last call to init().
123          * \throws  std::bad_alloc if out of memory (only if this is the first
124          *      call and init() has not been called either).
125          * \throws  tMPI::system_error on thread synchronization errors.
126          */
127         static const ProgramInfo &getInstance();
128         /*! \brief
129          * Initializes global program information.
130          *
131          * \param[in] argc  argc value passed to main().
132          * \param[in] argv  argv array passed to main().
133          * \returns   Reference to initialized program information object.
134          *
135          * The return value of realBinaryName() is the same as
136          * invariantProgramName().
137          *
138          * Does not throw. Terminates the program on out-of-memory error.
139          */
140         static ProgramInfo &init(int argc, const char *const argv[]);
141         /*! \brief
142          * Initializes global program information with explicit binary name.
143          *
144          * \param[in] realBinaryName  Name of the binary
145          *     (without Gromacs binary suffix or .exe on Windows).
146          * \param[in] argc  argc value passed to main().
147          * \param[in] argv  argv array passed to main().
148          * \returns   Reference to initialized program information object.
149          *
150          * This overload is provided for cases where the program may be invoked
151          * through a symlink, and it is necessary to know the real name of the
152          * binary.
153          *
154          * Does not throw. Terminates the program on out-of-memory error.
155          */
156         static ProgramInfo &init(const char *realBinaryName,
157                                  int argc, const char *const argv[]);
158
159         /*! \brief
160          * Constructs an empty program info objects.
161          *
162          * All methods in the constructed object return dummy values.
163          */
164         ProgramInfo();
165         /*! \brief
166          * Initializes a program information object with binary name only.
167          *
168          * \param[in] realBinaryName  Name of the binary
169          *     (without Gromacs binary suffix or .exe on Windows).
170          *
171          * This is needed for unit testing purposes.
172          * The constructed object works as if the command line consisted of
173          * only of the binary name.
174          */
175         explicit ProgramInfo(const char *realBinaryName);
176         /*! \brief
177          * Initializes a program information object based on command line.
178          *
179          * \param[in] argc  argc value passed to main().
180          * \param[in] argv  argv array passed to main().
181          */
182         ProgramInfo(int argc, const char *const argv[]);
183         /*! \brief
184          * Initializes a program information object based on binary name and
185          * command line.
186          *
187          * \param[in] realBinaryName  Name of the binary
188          *     (without Gromacs binary suffix or .exe on Windows).
189          * \param[in] argc  argc value passed to main().
190          * \param[in] argv  argv array passed to main().
191          */
192         ProgramInfo(const char *realBinaryName,
193                     int argc, const char *const argv[]);
194         /*! \brief
195          * Initializes a program information object based on binary name and
196          * command line.
197          *
198          * \param[in] realBinaryName  Name of the binary
199          *     (without Gromacs binary suffix or .exe on Windows).
200          * \param[in] argc  argc value passed to main().
201          * \param[in] argv  argv array passed to main().
202          * \param[in] env   Customizes the way the binary name is handled.
203          *
204          * This overload allows one to customize the way the binary is located
205          * by providing a custom ExecutableEnvironmentInterface implementation.
206          * This is mainly useful for testing purposes to make it possible to
207          * test different paths without setting environment variables, changing
208          * the working directory or doing other process-wide operations.
209          * It may also be useful for making Gromacs behave better when linked
210          * into a non-Gromacs executable (with possible extensions in
211          * ExecutableEnvironmentInterface).
212          */
213         ProgramInfo(const char *realBinaryName,
214                     int argc, const char *const argv[],
215                     ExecutableEnvironmentPointer env);
216         ~ProgramInfo();
217
218         /*! \brief
219          * Sets a display name for the binary.
220          *
221          * \throws std::bad_alloc if out of memory.
222          * \throws tMPI::system_error on thread synchronization errors.
223          *
224          * This is used with the wrapper binary to add the name of the invoked
225          * module to the name of the binary shown.
226          */
227         void setDisplayName(const std::string &name);
228
229         /*! \brief
230          * Returns the real name of the binary.
231          *
232          * The returned value is comparable to invariantProgramName(), i.e., it
233          * has suffixes and OS-specific extensions removed.
234          *
235          * Does not throw.
236          */
237         const std::string &realBinaryName() const;
238         /*! \brief
239          * Returns the name of the binary as it was invoked without any path.
240          *
241          * Does not throw.
242          */
243         const std::string &programName() const;
244         /*! \brief
245          * Returns an invariant name of the binary.
246          *
247          * The returned value has OS-specific extensions (.exe on Windows)
248          * removed, as well as any binary suffix that was configured.
249          *
250          * Does not throw.
251          */
252         const std::string &invariantProgramName() const;
253         /*! \brief
254          * Returns a display name of the current module.
255          *
256          * \throws  tMPI::system_error on thread synchronization errors.
257          *
258          * The returned value equals programName(), unless a separate display
259          * name has been set with setDisplayName().
260          */
261         const std::string &displayName() const;
262         /*! \brief
263          * Returns the full command line used to invoke the binary.
264          *
265          * Does not throw.
266          */
267         const std::string &commandLine() const;
268
269         /*! \brief
270          * Returns the full path of the invoked binary.
271          *
272          * Returns argv[0] if there was an error in finding the absolute path.
273          *
274          * Does not throw.
275          */
276         const std::string &fullBinaryPath() const;
277
278     private:
279         class Impl;
280
281         PrivateImplPointer<Impl> impl_;
282 };
283
284 } // namespace gmx
285
286 #endif