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// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_CASSERT_ASSERT_HPP
5#define TETL_CASSERT_ASSERT_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_utility/ignore_unused.hpp>
10#include <etl/_version/implementation.hpp>
11
12#if __has_include(<stdlib.h>)
13 #include <stdlib.h>
14
15 #include <etl/_config/_workarounds/001_avr_macros.hpp> // For AVR macros
16#else
17inline auto exit(int /*ignore*/) -> void { }
18#endif
19
20namespace etl {
21/// \brief Payload for an assertion.
22struct assert_msg {
23 int line{};
24 char const* file{nullptr};
25 char const* func{nullptr};
26 char const* expression{nullptr};
27};
28
29} // namespace etl
30
31namespace etl {
32#if defined(TETL_ENABLE_CUSTOM_ASSERT_HANDLER)
33
34template <typename Assertion>
35[[noreturn]] auto assert_handler(Assertion const& msg) -> void; // NOLINT
36
37#else
38
39template <typename Assertion>
40[[noreturn]] auto assert_handler(Assertion const& msg) -> void // NOLINT
41{
43 ::exit(1); // NOLINT
44}
45
46#endif
47
48} // namespace etl
49
50#define TETL_ASSERT_IMPL(...)
51 do {
52 if (not(__VA_ARGS__)) [[unlikely]] {
53 /* TETL_DEBUG_TRAP(); */
54 etl::assert_handler(
55 etl::assert_msg{
56 .line = __LINE__,
57 .file = __FILE__,
58 .func = etl::is_hosted() ? TETL_BUILTIN_FUNCTION() : nullptr,
59 .expression = etl::is_hosted() ? TETL_STRINGIFY((__VA_ARGS__)) : nullptr,
60 }
61 );
62 }
63 } while (false)
64
65#if not defined(TETL_NDEBUG) or (TETL_NDEBUG == 0) or defined(TETL_ENABLE_ASSERTIONS)
66 #define TETL_ASSERT(...) TETL_ASSERT_IMPL(__VA_ARGS__)
67#else
68 #define TETL_ASSERT(...)
69#endif
70
71#endif // TETL_CASSERT_ASSERT_HPP
Definition adjacent_find.hpp:9
Payload for an assertion.
Definition assert.hpp:22
int line
Definition assert.hpp:23
char const * expression
Definition assert.hpp:26
char const * file
Definition assert.hpp:24
char const * func
Definition assert.hpp:25