Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1/*
2* Runtime assertion checking
3*/
4
5#ifndef IV_SRC_CORE_ASSERT_HPP_
6#define IV_SRC_CORE_ASSERT_HPP_
7
8#include <string>
9
10namespace iv::assert
11{
12
13template<class...>
14inline constexpr bool always_false = false;
15
20void assertionFailure(const char *exprString, const char *assertionMade, const char *func, const char *file, int line);
21
22void throwInvalidArgument(const char *message, const char *func, const char *file);
23void throwInvalidState(const char *expr, const char *func, const char *file);
24
31template<typename T>
32void ignoreParam(T &&)
33{
34}
35
36template<typename... T>
37void ignoreParams(T &&...args)
38{
39 (iv::assert::ignoreParam(args), ...);
40}
41
57void assertUnreachable(const char *file, int line);
58
59void assertionMessage(const char *message, const char *func, const char *file, int line);
60
61void debugInfo(std::string_view message);
62void debugMessage(std::string_view message);
63void debugError(std::string_view message);
64
65}//namespace iv::assert
66
67#ifndef NDEBUG
68#define ASSERT_FUNCTION_ __extension__ __PRETTY_FUNCTION__
69#else
70#define ASSERT_FUNCTION_ __func__
71#endif
72
77#define IV_ARG_CHECK(expr, msg, ...) \
78 do \
79 { \
80 if (not(expr)) \
81 iv::assert::throwInvalidArgument(msg, ASSERT_FUNCTION_, __FILE__); \
82 } while (0)
83
88#define IV_STATE_CHECK(expr) \
89 do \
90 { \
91 if (not(expr)) \
92 iv::assert::throwInvalidState(#expr, ASSERT_FUNCTION_, __FILE__); \
93 } while (0)
94
98#define IV_ASSERT(expr, assertion_made, ...) \
99 do \
100 { \
101 if (not(expr)) \
102 iv::assert::assertionFailure(#expr, assertion_made, ASSERT_FUNCTION_, __FILE__, __LINE__); \
103 } while (0)
104
108#define IV_ASSERT_NOMSG(expr, ...) \
109 do \
110 { \
111 if (not(expr)) \
112 iv::assert::assertionFailure(#expr, "", ASSERT_FUNCTION_, __FILE__, __LINE__); \
113 } while (0)
114
118#define IV_ASSERT_IMPLICATION(expr1, expr2, msg, ...) \
119 do \
120 { \
121 if ((expr1) && not(expr2)) \
122 iv::assert::assertionFailure(#expr1 " implies " #expr2, msg, ASSERT_FUNCTION_, __FILE__, __LINE__); \
123 } while (0)
124
128#define IV_ASSERT_NULL_POINTER(ptr) \
129 do \
130 { \
131 if ((ptr) == nullptr) \
132 iv::assert::assertionFailure(#ptr " is null", "", ASSERT_FUNCTION_, __FILE__, __LINE__); \
133 } while (0)
134
141#define IV_UNUSED iv::assert::ignoreParams
142
146#define IV_ASSERT_UNREACHABLE() iv::assert::assertUnreachable(__FILE__, __LINE__)
147
152#define IV_ASSERT_MSG(msg, ...) \
153 do \
154 { \
155 iv::assert::assertionMessage(msg, ASSERT_FUNCTION_, __FILE__, __LINE__); \
156 } while (0)
157
158static_assert(sizeof(std::size_t) == 8 || sizeof(std::size_t) == 4, "This platform has an unexpected size for size_t");
159
164void throwMissingCase(const char *message, const char *func, const char *file);
165
166#define IV_MISSING_CASE_CHECK(msg, ...) \
167 do \
168 { \
169 throwMissingCase(#msg, __func__, __FILE__); \
170 } while (0)
171
175#define IV_ASSERT_EQUAL(expr1, expr2, assertion_made) \
176 do \
177 { \
178 if ((expr1) != (expr2)) \
179 iv::assert::assertionFailure(#expr1 " == " #expr2, assertion_made, __func__, __FILE__, __LINE__); \
180 } while (0)
181
182#endif//IV_SRC_CORE_ASSERT_HPP_
void throwMissingCase(const char *message, const char *func, const char *file)
Definition assert.cpp:149
Definition assert.cpp:15
void assertionFailure(const char *exprString, const char *assertionMade, const char *func, const char *file, int line)
Definition assert.cpp:17
void assertionMessage(const char *message, const char *func, const char *file, int line)
Definition assert.cpp:87
void throwInvalidState(const char *expr, const char *func, const char *file)
Definition assert.cpp:61
void debugInfo(std::string_view message)
Definition assert.cpp:123
void debugMessage(std::string_view message)
Definition assert.cpp:131
void ignoreParams(T &&...args)
Definition assert.hpp:37
void debugError(std::string_view message)
Definition assert.cpp:139
void assertUnreachable(const char *file, int line)
Definition assert.cpp:69
void throwInvalidArgument(const char *message, const char *func, const char *file)
Definition assert.cpp:53
constexpr bool always_false
Definition assert.hpp:14
void ignoreParam(T &&)
Definition assert.hpp:32