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\tIn function: " + std::string(function) + "\n\n\t" + std::string(test) \
33 + "\n\nIn file: " + std::string(file) + ":" + std::to_string(line) \
34 + "\nYou need help? Submit an issue on: https://github.com/codac-team/codac/issues" \
35 + "\n=============================================================================" \
36 ); \
37 } while (0)
38
39 #if defined(__PRETTY_FUNCTION__)
40
41 #define assert_release(test) \
42 do { \
43 if(!(test)) { \
44 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
45 } \
46 } while (0)
47
48 #define assert_release_constexpr(test) \
49 do { \
50 if constexpr(!(test)) { \
51 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
52 } \
53 } while (0)
54
55 #else
56
57 #define assert_release(test) \
58 do { \
59 if(!(test)) { \
60 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __func__); \
61 } \
62 } while (0)
63
64 #define assert_release_constexpr(test) \
65 do { \
66 if constexpr(!(test)) { \
67 CODAC_ASSERT_MESSAGE(#test, __FILE__, __LINE__, __func__); \
68 } \
69 } while (0)
70
71 #endif
72
73 #endif
74}
75
76#if 0
77
78 #define assert_release(test) \
79 if(!(test)) \
80 { \
81 auto l = std::source_location::current(); \
82 std::ostringstream s; \
83 s << l.file_name() << '(' \
84 << l.line() << ':' \
85 << l.column() << ")\n " \
86 << l.function_name() << "\n"; \
87 throw std::invalid_argument("Wrong assertion in the following function:\n " + s.str()); \
88 abort(); \
89 } \
90 \
91
92#endif