codac 2.0.0
Loading...
Searching...
No Matches
codac2_assert.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <cstdlib>
13#include <cassert>
14#include <sstream>
15// C++20 not fully supported by compilers yet: #include <source_location>
16
17namespace codac2
18{
19 #if defined FAST_RELEASE & defined NDEBUG
20
21 #define assert_release(ignore_test) ((void)0)
22 #define assert_release_constexpr(ignore_test) ((void)0)
23 #define assert_release_unreachable(ignore_msg) ((void)0)
24
25 #else
26
27 // In the following, do..while(0) is used for syntax protection
28
29 #define CODAC_ASSERT_MESSAGE(test, file, line, function) \
30 do { \
31 throw std::invalid_argument( \
32 std::string("\n=============================================================================") \
33 + "\nThe following Codac assertion failed:\n\n\tIn function: " + std::string(function) + "\n\n\t" + std::string(test) \
34 + "\n\nIn file: " + std::string(file) + ":" + std::to_string(line) \
35 + "\nYou need help? Submit an issue on: https://github.com/codac-team/codac/issues" \
36 + "\n=============================================================================" \
37 ); \
38 } while (0)
39
40 #if defined(__PRETTY_FUNCTION__)
41
42 #define assert_release(test) \
43 do { \
44 if(!(test)) { \
45 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
46 } \
47 } while (0)
48
49 #define assert_release_constexpr(test) \
50 do { \
51 if constexpr(!(test)) { \
52 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
53 } \
54 } while (0)
55
56 // For a branch that must never be reached.
57 // To be used instead of assert_release(false && "...")
58 #define assert_release_unreachable(msg) \
59 do { \
60 CODAC_ASSERT_MESSAGE(msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
61 } while (0)
62
63 #else
64
65 #define assert_release(test) \
66 do { \
67 if(!(test)) { \
68 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __func__); \
69 } \
70 } while (0)
71
72 #define assert_release_constexpr(test) \
73 do { \
74 if constexpr(!(test)) { \
75 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __func__); \
76 } \
77 } while (0)
78
79 #define assert_release_unreachable(msg) \
80 do { \
81 CODAC_ASSERT_MESSAGE(msg, __FILE__, __LINE__, __func__); \
82 } while (0)
83
84 #endif
85
86 #endif
87}
88
89#if 0
90
91 #define assert_release(test) \
92 if(!(test)) \
93 { \
94 auto l = std::source_location::current(); \
95 std::ostringstream s; \
96 s << l.file_name() << '(' \
97 << l.line() << ':' \
98 << l.column() << ")\n " \
99 << l.function_name() << "\n"; \
100 throw std::invalid_argument("Wrong assertion in the following function:\n " + s.str()); \
101 abort(); \
102 } \
103 \
104
105#endif
Definition codac2_OctaSym.h:21