TRIQS/mpi 1.3.0
C++ interface to MPI
Loading...
Searching...
No Matches
macros.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet
16
22#ifndef _CCQ_MACROS_GUARD_H
23#define _CCQ_MACROS_GUARD_H
24
25// CCQ, TRIQS general macros
26// GUARD IT do not use pragma once
27// hence one can simply include them in every projects
28
29// ---------------- Stringify ----------------
30
31#define AS_STRING(...) AS_STRING2(__VA_ARGS__)
32#define AS_STRING2(...) #__VA_ARGS__
33
34// ---------------- Print ----------------
35
36#define PRINT(X) std::cerr << AS_STRING(X) << " = " << X << " at " << __FILE__ << ":" << __LINE__ << '\n'
37#define NDA_PRINT(X) std::cerr << AS_STRING(X) << " = " << X << " at " << __FILE__ << ":" << __LINE__ << '\n'
38
39// ---------------- C++20 specific ----------------
40
41#if (__cplusplus > 201703L)
42
43// C++20
44// REQUIRES17 : only in 17, same for 20
45// REQUIRES : in both
46
47#define AUTO(X) X auto
48#define CONCEPT(X) X
49
50#define REQUIRES17(...)
51#define REQUIRES requires
52#define REQUIRES20 requires
53
54// C++20 explicit(bool) : degrade it NOTHING in c++17, we can not check easily
55#define EXPLICIT explicit
56
57// WARNING : it is critical for our doctools to have REQUIRES as requires, NOT a (...) with __VA_ARGS__
58// It is the same effect, but raises unnecessary complications in traversing the AST in libtooling with macros.
59
60#else
61
62// C++17 backward compat mode
63
64#define AUTO(X) auto
65#define CONCEPT(X) typename
66#define REQUIRES20(...)
67
68#define EXPLICIT(...)
69
70#ifdef __clang__
71#define REQUIRES17(...) __attribute__((enable_if(__VA_ARGS__, AS_STRING(__VA_ARGS__))))
72#define REQUIRES(...) __attribute__((enable_if(__VA_ARGS__, AS_STRING(__VA_ARGS__))))
73#elif __GNUC__
74// with the -fconcepts TS only. A degraded concept mode, not exactly the C++20. We return to C++17 + basic require
75#define REQUIRES17 requires
76#define REQUIRES requires
77
78#endif
79
80#endif // C++20
81
82// ---------------- Inline ----------------
83
84#define FORCEINLINE __inline__ __attribute__((always_inline))
85
86// ---------------- Debugging ----------------
87
88#ifdef NDEBUG
89
90#define EXPECTS(X)
91#define ASSERT(X)
92#define ENSURES(X)
93#define EXPECTS_WITH_MESSAGE(X, ...)
94#define ASSERT_WITH_MESSAGE(X, ...)
95#define ENSURES_WITH_MESSAGE(X, ...)
96
97#else
98
99#include <iostream>
100#include <exception>
101
102#define EXPECTS(X) \
103 if (!(X)) { \
104 std::cerr << "Precondition " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
105 std::terminate(); \
106 }
107#define ASSERT(X) \
108 if (!(X)) { \
109 std::cerr << "Assertion " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
110 std::terminate(); \
111 }
112#define ENSURES(X) \
113 if (!(X)) { \
114 std::cerr << "Postcondition " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
115 std::terminate(); \
116 }
117
118#define EXPECTS_WITH_MESSAGE(X, ...) \
119 if (!(X)) { \
120 std::cerr << "Precondition " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
121 std::cerr << "Error message : " << __VA_ARGS__ << std::endl; \
122 std::terminate(); \
123 }
124#define ASSERT_WITH_MESSAGE(X, ...) \
125 if (!(X)) { \
126 std::cerr << "Assertion " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
127 std::cerr << "Error message : " << __VA_ARGS__ << std::endl; \
128 std::terminate(); \
129 }
130#define ENSURES_WITH_MESSAGE(X, ...) \
131 if (!(X)) { \
132 std::cerr << "Postcondition " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
133 std::cerr << "Error message : " << __VA_ARGS__ << std::endl; \
134 std::terminate(); \
135 }
136
137#endif // NDEBUG
138
139#endif // _CCQ_MACROS_GUARD_H