Establish `api/` as the home for installed headers.
[alexxy/gromacs.git] / api / legacy / include / gromacs / utility / basedefinitions.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 /*! \file
39  * \brief
40  * Basic types and macros used throughout \Gromacs.
41  *
42  * \inpublicapi
43  * \ingroup module_utility
44  */
45 #ifndef GMX_UTILITY_BASEDEFINITIONS_H
46 #define GMX_UTILITY_BASEDEFINITIONS_H
47
48 #include <stdint.h>
49
50 #include <cinttypes>
51 #include <cstddef>
52
53 //! Identical to bool
54 typedef bool gmx_bool;
55
56 #ifndef FALSE
57 /** False value for ::gmx_bool. */
58 #    define FALSE false
59 #endif
60 #ifndef TRUE
61 /** True value for ::gmx_bool. */
62 #    define TRUE true
63 #endif
64 /** Number of gmx_bool values. */
65 #define BOOL_NR 2
66
67 namespace gmx
68 {
69 /*! \brief Integer type for indexing into arrays or vectors
70  *
71  * Same as ptrdiff_t.
72  */
73 using index = std::ptrdiff_t;
74
75 //! Return signed size of container
76 template<typename T>
77 index ssize(const T& t)
78 {
79     return t.size();
80 }
81 } // namespace gmx
82
83 /* ICC, GCC, MSVC, Pathscale, PGI, XLC support __restrict.
84  * Any other compiler can be added here. */
85 /*! \brief
86  * Keyword to use in instead of C99 `restrict`.
87  *
88  * We cannot use `restrict` because it is only in C99, but not in C++.
89  * This macro should instead be used to allow easily supporting different
90  * compilers.
91  */
92 #define gmx_restrict __restrict
93
94 /*! \def gmx_unused
95  * \brief
96  * Attribute to suppress compiler warnings about unused function parameters.
97  *
98  * This attribute suppresses compiler warnings about unused function arguments
99  * by marking them as possibly unused.  Some arguments are unused but
100  * have to be retained to preserve a function signature
101  * that must match that of another function.
102  * Some arguments are only used in *some* conditional compilation code paths
103  * (e.g. MPI).
104  */
105 #ifndef gmx_unused
106 #    ifdef __GNUC__
107 /* GCC, clang, and some ICC pretending to be GCC */
108 #        define gmx_unused __attribute__((unused))
109 #    elif (defined(__INTEL_COMPILER) || defined(__ECC)) && !defined(_MSC_VER)
110 /* ICC on *nix */
111 #        define gmx_unused __attribute__((unused))
112 #    elif defined(__PGI)
113 /* Portland group compilers */
114 #        define gmx_unused __attribute__((unused))
115 #    elif defined _MSC_VER
116 /* MSVC */
117 #        define gmx_unused /*@unused@*/
118 #    elif defined(__xlC__)
119 /* IBM */
120 #        define gmx_unused __attribute__((unused))
121 #    else
122 #        define gmx_unused
123 #    endif
124 #endif
125
126 /*! \brief Attribute to explicitly indicate that a parameter or
127  * locally scoped variable is used just in debug mode.
128  *
129  * \ingroup module_utility
130  */
131 #ifdef NDEBUG
132 #    define gmx_used_in_debug gmx_unused
133 #else
134 #    define gmx_used_in_debug
135 #endif
136
137 #ifndef __has_feature
138 /** For compatibility with non-clang compilers. */
139 #    define __has_feature(x) 0
140 #endif
141
142 /*! \brief
143  * Macro to explicitly ignore an unused value.
144  *
145  * \ingroup module_utility
146  *
147  * \todo Deprecated - use gmx_unused
148  */
149 #define GMX_UNUSED_VALUE(value) (void)value
150
151 #ifdef __clang__
152 #    define DO_PRAGMA(x) _Pragma(#    x)
153 #    define CLANG_DIAGNOSTIC_IGNORE(warning) \
154         _Pragma("clang diagnostic push") DO_PRAGMA(clang diagnostic ignored #warning)
155 #    define CLANG_DIAGNOSTIC_RESET _Pragma("clang diagnostic pop")
156 #else
157 //! Ignore specified clang warning until CLANG_DIAGNOSTIC_RESET
158 #    define CLANG_DIAGNOSTIC_IGNORE(warning)
159 //! Reset all diagnostics to default
160 #    define CLANG_DIAGNOSTIC_RESET
161 #endif
162
163 #ifdef _MSC_VER
164 #    define MSVC_DIAGNOSTIC_IGNORE(id) __pragma(warning(push)) __pragma(warning(disable : id))
165 #    define MSVC_DIAGNOSTIC_RESET __pragma(warning(pop))
166 #else
167 //! Ignore specified MSVC warning until MSVC_DIAGNOSTIC_RESET
168 #    define MSVC_DIAGNOSTIC_IGNORE(warning)
169 //! Reset all diagnostics to default
170 #    define MSVC_DIAGNOSTIC_RESET
171 #endif
172
173 #ifdef __INTEL_COMPILER
174 //! Ignore unused loop variable warning - it was used until the compiler removes the use!
175 #    define DO_PRAGMA(x) _Pragma(#    x)
176 #    define INTEL_DIAGNOSTIC_IGNORE(id) DO_PRAGMA(warning push) DO_PRAGMA(warning(disable : id))
177 #    define INTEL_DIAGNOSTIC_RESET DO_PRAGMA(warning pop)
178 #else
179 //! Ignore specified diagnostic message from Intel compiler.
180 #    define INTEL_DIAGNOSTIC_IGNORE(id)
181 //! Reset the diagnostic message setting.
182 #    define INTEL_DIAGNOSTIC_RESET
183 #endif
184
185 namespace gmx
186 {
187 namespace internal
188 {
189 /*! \cond internal */
190 /*! \internal \brief
191  * Helper for ignoring values in macros.
192  *
193  * \ingroup module_utility
194  */
195 template<typename T>
196 static inline void ignoreValueHelper(const T& /*unused*/)
197 {
198 }
199 //! \endcond
200 } // namespace internal
201 } // namespace gmx
202
203 /*! \brief
204  * Macro to explicitly ignore a return value of a call.
205  *
206  * Mainly meant for ignoring values of functions declared with
207  * `__attribute__((warn_unused_return))`.  Makes it easy to find those places if
208  * they need to be fixed, and document the intent in cases where the return
209  * value really can be ignored.  It also makes it easy to adapt the approach so
210  * that they don't produce warnings.  A cast to void doesn't remove the warning
211  * in gcc, while adding a dummy variable can cause warnings about an unused
212  * variable.
213  *
214  * \ingroup module_utility
215  */
216 #define GMX_IGNORE_RETURN_VALUE(call) ::gmx::internal::ignoreValueHelper(call)
217
218 #endif