2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2019, 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.
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.
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.
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.
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.
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.
37 * Declares gmx::IProgramContext and related methods.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_PROGRAMCONTEXT_H
44 #define GMX_UTILITY_PROGRAMCONTEXT_H
49 //! \addtogroup module_utility
53 * Provides information about installation prefix (see
54 * IProgramContext::installationPrefix()).
58 struct InstallationPrefixInfo
60 //! Initializes the structure with given values.
61 InstallationPrefixInfo(const char* path
, bool bSource
) : path(path
), bSourceLayout(bSource
) {}
64 * Path to the installation prefix of the current \Gromacs instance.
66 * If this is `NULL` or empty, data files cannot be looked up from the
67 * install tree and \Gromacs functions that access such files may fail.
68 * This can also contain a path to the source tree (see \a bSourceLayout).
70 const char* const path
;
72 * Whether \a path points to a source tree -like layout.
74 * For testing, it is useful to read data files from the source tree.
75 * For such cases, the program context can return the source tree root path
76 * in \a path, and set this to `true` to indicate that the data files
77 * should be searched using the layout of the source tree instead of the
80 const bool bSourceLayout
;
85 * Provides context information about the program that is calling the library.
87 * This class provides access to information about the program that is
88 * currently running. This information is used to provide better information
89 * to the user, and to locate the \Gromacs data files.
91 * setProgramContext() should be called before any other \Gromacs calls in
92 * a program (except for gmx::init()). This avoids thread safety issues, and
93 * allows nicely formatted error messages.
95 * For thread safety, the implementations of the interface should ensure that
96 * the returned strings remain valid until the end of the program (see
97 * getProgramContext() for discussion on deinitialization). Callers of the
98 * interface within \Gromacs are prepared for exceptions, but generally
99 * terminate the program on any exception. Implementations should avoid
100 * exception except for truly fatal conditions.
102 * The destructor is protected to signify that the context is never destroyed
103 * through the interface.
105 * \see getProgramContext()
106 * \see setProgramContext()
109 class IProgramContext
113 * Returns the name of the binary as it was invoked without any path.
115 * This is typically `argv[0]` with any leading directory stripped.
116 * Currently, this should be a valid file name.
118 virtual const char* programName() const = 0;
120 * Returns a display name for the program.
122 * For simple programs, this can equal programName(). For the \Gromacs
123 * `gmx` wrapper binary, this includes the name of the module (e.g.,
124 * `gmx angle`). This is used only for informational purposes, and
125 * there are no constraints on contents, except that it should not be
128 virtual const char* displayName() const = 0;
130 * Returns the full path of the running binary.
132 * This is mainly used for informational purposes. There are no
133 * constraints on contents, except that it should not be `NULL`.
134 * Currently, this is also used for sanity checks in checkpointing.
136 * The implementation can provide an empty string if the path to the
137 * binary is not available. In such a case, the information is not
140 virtual const char* fullBinaryPath() const = 0;
142 * Returns the installation prefix for \Gromacs.
144 * This path is used to locate the data files that are in `share/top/`
145 * in the source directory.
146 * The implementation can provide an empty string if the path is not
147 * available; in such a case, functions that require data files may
150 * The returned structure also contains a flag to indicate whether the
151 * prefix actually points to the source tree. This is used for tests
152 * and to support running binaries directly from the build tree.
154 virtual InstallationPrefixInfo
installationPrefix() const = 0;
156 * Returns the full command line used to invoke the binary.
158 * The implementation can provide an empty string if no command line is
161 virtual const char* commandLine() const = 0;
164 virtual ~IProgramContext() {}
168 * Returns the global IProgramContext instance.
170 * \returns The context set with setProgramContext().
172 * If nothing has been set with setProgramContext(), returns a default
173 * implementation that returns `"GROMACS"` for the program and display names,
174 * and empty strings for other values.
175 * The default implementation never throws.
179 * See setProgramContext() for thread safety notes. You should not call this
180 * method in global deinitialization methods (e.g., destructors of global
181 * variables), since it is very difficult to clean up the state correctly in
182 * the presence of such calls. For example, initForCommandLine() assumes that
183 * such calls do not exist to be able to free the context before exiting.
185 * \see IProgramContext
187 const IProgramContext
& getProgramContext();
189 * Sets the global IProgramContext instance.
191 * \param[in] context Program context to set
192 * (can be NULL to restore the default context).
194 * The library does not take ownership of \p context.
195 * The provided object must remain valid until the global instance is changed
196 * by another call to setProgramContext().
198 * This method is not thread-safe. It must be the first call to the library
199 * after gmx::init(), and multi-threaded access is only supported after the
200 * call completes. If \Gromacs is getting called from multiple threads, or
201 * uses multiple threads simultaneously, changing the program context is not
202 * supported once it is set.
203 * If the context is cleared at the end of the program, the caller must ensure
204 * that all other threads have been terminated at this point.
205 * These constraints simplify the implementation significantly.
209 * \see IProgramContext
211 void setProgramContext(const IProgramContext
* context
);