TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
macros.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-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, Nils Wentzell
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 project
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// ---------------- Inline ----------------
40
41#define FORCEINLINE __inline__ __attribute__((always_inline))
42
43// ---------------- Debugging ----------------
44
45#ifdef NDEBUG
46
47#define EXPECTS(X)
48#define ASSERT(X)
49#define ENSURES(X)
50#define EXPECTS_WITH_MESSAGE(X, ...)
51#define ASSERT_WITH_MESSAGE(X, ...)
52#define ENSURES_WITH_MESSAGE(X, ...)
53
54#else
55
56#include <exception>
57#include <iostream>
58
59#define EXPECTS(X) \
60 if (!(X)) { \
61 std::cerr << "Precondition " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
62 std::terminate(); \
63 }
64#define ASSERT(X) \
65 if (!(X)) { \
66 std::cerr << "Assertion " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
67 std::terminate(); \
68 }
69#define ENSURES(X) \
70 if (!(X)) { \
71 std::cerr << "Postcondition " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
72 std::terminate(); \
73 }
74
75#define EXPECTS_WITH_MESSAGE(X, ...) \
76 if (!(X)) { \
77 std::cerr << "Precondition " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
78 std::cerr << "Error message : " << __VA_ARGS__ << std::endl; \
79 std::terminate(); \
80 }
81#define ASSERT_WITH_MESSAGE(X, ...) \
82 if (!(X)) { \
83 std::cerr << "Assertion " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
84 std::cerr << "Error message : " << __VA_ARGS__ << std::endl; \
85 std::terminate(); \
86 }
87#define ENSURES_WITH_MESSAGE(X, ...) \
88 if (!(X)) { \
89 std::cerr << "Postcondition " << AS_STRING(X) << " violated at " << __FILE__ << ":" << __LINE__ << "\n"; \
90 std::cerr << "Error message : " << __VA_ARGS__ << std::endl; \
91 std::terminate(); \
92 }
93
94#endif // NDEBUG
95
96#endif // _CCQ_MACROS_GUARD_H