tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_CASSERT_ASSERT_HPP
4#define TETL_CASSERT_ASSERT_HPP
5
6#include <etl/_config/all.hpp>
7
10
11#if __has_include(<stdlib.h>)
12 #include <stdlib.h>
13
14 #include <etl/_config/_workarounds/001_avr_macros.hpp> // For AVR macros
15#else
16inline auto exit(int /*ignore*/) -> void { }
17#endif
18
19namespace etl {
21struct assert_msg {
22 int line{};
23 char const* file{nullptr};
24 char const* func{nullptr};
25 char const* expression{nullptr};
26};
27
28} // namespace etl
29
30namespace etl {
31#if defined(TETL_ENABLE_CUSTOM_ASSERT_HANDLER)
32
33template <typename Assertion>
34[[noreturn]] auto assert_handler(Assertion const& msg) -> void; // NOLINT
35
36#else
37
38template <typename Assertion>
39[[noreturn]] auto assert_handler(Assertion const& msg) -> void // NOLINT
40{
42 ::exit(1); // NOLINT
43}
44
45#endif
46
47} // namespace etl
48
49#define TETL_ASSERT_IMPL(...) \
50 do { \
51 if (not(__VA_ARGS__)) [[unlikely]] { \
52 /* TETL_DEBUG_TRAP(); */ \
53 etl::assert_handler(etl::assert_msg{ \
54 .line = __LINE__, \
55 .file = __FILE__, \
56 .func = etl::is_hosted() ? TETL_BUILTIN_FUNCTION() : nullptr, \
57 .expression = etl::is_hosted() ? TETL_STRINGIFY((__VA_ARGS__)) : nullptr, \
58 }); \
59 } \
60 } while (false)
61
62#if not defined(TETL_NDEBUG) or (TETL_NDEBUG == 0) or defined(TETL_ENABLE_ASSERTIONS)
63 #define TETL_ASSERT(...) TETL_ASSERT_IMPL(__VA_ARGS__)
64#else
65 #define TETL_ASSERT(...)
66#endif
67
68#endif // TETL_CASSERT_ASSERT_HPP
auto exit(int) -> void
Definition assert.hpp:16
Definition adjacent_find.hpp:8
constexpr auto ignore_unused(Types &&...) -> void
Explicitly ignore arguments or variables.
Definition ignore_unused.hpp:17
auto assert_handler(Assertion const &msg) -> void
Definition assert.hpp:39
Payload for an assertion.
Definition assert.hpp:21
int line
Definition assert.hpp:22
char const * expression
Definition assert.hpp:25
char const * file
Definition assert.hpp:23
char const * func
Definition assert.hpp:24