Github   Telegram
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <string_view>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace utils::impl {
11
12[[noreturn]] void UASSERT_failed(std::string_view expr, const char* file,
13 unsigned int line, const char* function,
14 std::string_view msg) noexcept;
15
16[[noreturn]] void LogAndThrowInvariantError(std::string_view condition,
17 std::string_view message);
18
19#ifdef NDEBUG
20inline constexpr bool kEnableAssert = false;
21#else
22inline constexpr bool kEnableAssert = true;
23#endif
24
25extern bool dump_stacktrace_on_assert_failure;
26
27} // namespace utils::impl
28
29USERVER_NAMESPACE_END
30
35// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
36#define UASSERT_MSG(expr, msg) \
37 do { \
38 if (USERVER_NAMESPACE::utils::impl::kEnableAssert) { \
39 if (expr) { \
40 } else { \
41 USERVER_NAMESPACE::utils::impl::UASSERT_failed( \
42 #expr, __FILE__, __LINE__, __func__, msg); \
43 } \
44 } \
45 } while (0)
46
51// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
52#define UASSERT(expr) UASSERT_MSG(expr, std::string_view{})
53
57// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
58#define UINVARIANT(condition, message) \
59 do { \
60 if (condition) { \
61 } else { \
62 if constexpr (USERVER_NAMESPACE::utils::impl::kEnableAssert) { \
63 USERVER_NAMESPACE::utils::impl::UASSERT_failed( \
64 #condition, __FILE__, __LINE__, __func__, message); \
65 } else { \
66 USERVER_NAMESPACE::utils::impl::LogAndThrowInvariantError(#condition, \
67 message); \
68 } \
69 } \
70 } while (0)