Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / utility / programcontext.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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::IProgramContext and related methods.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_utility
42  */
43 #ifndef GMX_UTILITY_PROGRAMCONTEXT_H
44 #define GMX_UTILITY_PROGRAMCONTEXT_H
45
46 namespace gmx
47 {
48
49 //! \addtogroup module_utility
50 //! \{
51
52 /*! \brief
53  * Provides information about installation prefix (see
54  * IProgramContext::installationPrefix()).
55  *
56  * \inpublicapi
57  */
58 struct InstallationPrefixInfo
59 {
60     //! Initializes the structure with given values.
61     InstallationPrefixInfo(const char* path, bool bSource) : path(path), bSourceLayout(bSource) {}
62
63     /*! \brief
64      * Path to the installation prefix of the current \Gromacs instance.
65      *
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).
69      */
70     const char* const path;
71     /*! \brief
72      * Whether \a path points to a source tree -like layout.
73      *
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
78      * installation.
79      */
80     const bool bSourceLayout;
81 };
82
83
84 /*! \brief
85  * Provides context information about the program that is calling the library.
86  *
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.
90  *
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.
94  *
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.
101  *
102  * The destructor is protected to signify that the context is never destroyed
103  * through the interface.
104  *
105  * \see getProgramContext()
106  * \see setProgramContext()
107  * \inpublicapi
108  */
109 class IProgramContext
110 {
111 public:
112     /*! \brief
113      * Returns the name of the binary as it was invoked without any path.
114      *
115      * This is typically `argv[0]` with any leading directory stripped.
116      * Currently, this should be a valid file name.
117      */
118     virtual const char* programName() const = 0;
119     /*! \brief
120      * Returns a display name for the program.
121      *
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
126      * `NULL`.
127      */
128     virtual const char* displayName() const = 0;
129     /*! \brief
130      * Returns the full path of the running binary.
131      *
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.
135      *
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
138      * shown.
139      */
140     virtual const char* fullBinaryPath() const = 0;
141     /*! \brief
142      * Returns the installation prefix for \Gromacs.
143      *
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
148      * fail.
149      *
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.
153      */
154     virtual InstallationPrefixInfo installationPrefix() const = 0;
155     /*! \brief
156      * Returns the full command line used to invoke the binary.
157      *
158      * The implementation can provide an empty string if no command line is
159      * available.
160      */
161     virtual const char* commandLine() const = 0;
162
163 protected:
164     virtual ~IProgramContext() {}
165 };
166
167 /*! \brief
168  * Returns the global IProgramContext instance.
169  *
170  * \returns The context set with setProgramContext().
171  *
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.
176  *
177  * Does not throw.
178  *
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.
184  *
185  * \see IProgramContext
186  */
187 const IProgramContext& getProgramContext();
188 /*! \brief
189  * Sets the global IProgramContext instance.
190  *
191  * \param[in] context  Program context to set
192  *     (can be NULL to restore the default context).
193  *
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().
197  *
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.
206  *
207  * Does not throw.
208  *
209  * \see IProgramContext
210  */
211 void setProgramContext(const IProgramContext* context);
212
213 //! \}
214
215 } // namespace gmx
216
217 #endif