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
24 #else
25
26 // In the following, do..while(0) is used for syntax protection
27
28 #define CODAC_ASSERT_MESSAGE(test, file, line, function) \
29 do { \
30 throw std::invalid_argument( \
31 std::string("\n=============================================================================") \
32 + "\nThe following Codac assertion failed:\n\n\t" + std::string(test) \
33 + "\n\nIn: " + std::string(file) + ":" + std::to_string(line) \
34 + "\nFunction: " + std::string(function) \
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 #else
57
58 #define assert_release(test) \
59 do { \
60 if(!(test)) { \
61 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __func__); \
62 } \
63 } while (0)
64
65 #define assert_release_constexpr(test) \
66 do { \
67 if constexpr(!(test)) { \
68 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __func__); \
69 } \
70 } while (0)
71
72 #endif
73
74 #endif
75}
76
77#if 0
78
79 #define assert_release(test) \
80 if(!(test)) \
81 { \
82 auto l = std::source_location::current(); \
83 std::ostringstream s; \
84 s << l.file_name() << '(' \
85 << l.line() << ':' \
86 << l.column() << ")\n " \
87 << l.function_name() << "\n"; \
88 throw std::invalid_argument("Wrong assertion in the following function:\n " + s.str()); \
89 abort(); \
90 } \
91 \
92
93#endif