HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "architecture.hpp"
8#include "debugger.hpp"
9#include "utility.hpp"
10#include "type_traits.hpp"
11#include "exception.hpp"
12#include <exception>
13#include <ranges>
14
18namespace hi { inline namespace v1 {
19
26[[nodiscard]] constexpr bool bound_check(std::unsigned_integral auto index, std::unsigned_integral auto upper) noexcept
27{
28 using value_type = common_integer_t<decltype(index), decltype(upper)>;
29
30 auto index_ = static_cast<value_type>(index);
31 auto upper_ = static_cast<value_type>(upper);
32 return index_ < upper_;
33}
34
43[[nodiscard]] constexpr bool bound_check(std::integral auto index, std::integral auto lower, std::integral auto upper) noexcept
44{
45 using value_type = common_integer_t<decltype(index), decltype(lower), decltype(upper)>;
46
47 auto index_ = static_cast<value_type>(index);
48 auto lower_ = static_cast<value_type>(lower);
49 auto upper_ = static_cast<value_type>(upper);
50
51 hi_axiom(lower_ <= upper_);
52 return index_ >= lower_ and index_ < upper_;
53}
54
55template<typename Context>
56concept bound_check_range_helper = requires(Context&& range) {
57 {
58 std::ranges::size(range)
59 } -> std::unsigned_integral;
60 };
61
68[[nodiscard]] constexpr bool bound_check(std::integral auto index, bound_check_range_helper auto&& range) noexcept
69{
70 static_assert(sizeof(index) <= sizeof(size_t));
71
72 if constexpr (std::is_signed_v<std::decay_t<decltype(index)>>) {
73 if (index < 0) {
74 return false;
75 }
76 }
77
78 return static_cast<size_t>(index) < std::ranges::size(range);
79}
80
87#define hi_assert(expression, ...) \
88 do { \
89 if (not(expression)) { \
90 hi_debug_abort("assert: " __VA_ARGS__ " (" hi_stringify(expression) ")"); \
91 } \
92 } while (false)
93
100#define hi_assert_or_return(x, y) \
101 if (!(x)) { \
102 [[unlikely]] return y; \
103 }
104
105#define hi_assert_bounds(x, ...) \
106 do { \
107 if (not ::hi::bound_check(x, __VA_ARGS__)) { \
108 hi_debug_abort("assert bounds: " hi_stringify(x) " between " hi_stringify(__VA_ARGS__)); \
109 } \
110 } while (false)
111
118#define hi_assert_not_null(x, ...) \
119 do { \
120 if (x == nullptr) { \
121 hi_debug_abort("assert not-null: " __VA_ARGS__ " (" hi_stringify(x) ")"); \
122 } \
123 } while (false)
124
125#ifndef NDEBUG
133#define hi_axiom(expression, ...) hi_assert(expression __VA_OPT__(, ) __VA_ARGS__)
134
141#define hi_axiom_not_null(expression, ...) hi_assert_not_null(expression __VA_OPT__(, ) __VA_ARGS__)
142
148#define hi_no_default(...) [[unlikely]] hi_debug_abort("Reached no-default:" __VA_ARGS__)
149
150#else
158#define hi_axiom(expression, ...) hi_assume(expression)
159
166#define hi_axiom_not_null(expression, ...) hi_assume(expression != nullptr)
167
173#define hi_no_default(...) hi_unreachable()
174#endif
175
181#define hi_static_no_default(...) \
182 []<bool Flag = false>() \
183 { \
184 static_assert(Flag, "No default: " __VA_ARGS__); \
185 } \
186 ()
187
193#define hi_not_implemented(...) [[unlikely]] hi_debug_abort("Not implemented: " __VA_ARGS__);
194
200#define hi_static_not_implemented(...) hi_static_no_default("Not implemented: " __VA_ARGS__)
201
202}} // namespace hi::v1
Utilities to interact with the debugger this application runs under.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
Utilities used by the HikoGUI library itself.
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
DOXYGEN BUG.
Definition algorithm.hpp:15
geometry/margins.hpp
Definition assert.hpp:18
constexpr bool bound_check(std::unsigned_integral auto index, std::unsigned_integral auto upper) noexcept
Check if an unsigned index is less than the bound.
Definition assert.hpp:26
Definition assert.hpp:56