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 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
86#define hi_assert(expression) \
87 do { \
88 if (not(expression)) { \
89 hi_set_terminate_message("assert: " hi_stringify(expression)); \
90 hi_debug_abort(); \
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_set_terminate_message("assert bounds: " hi_stringify(x) " between " hi_stringify(__VA_ARGS__)); \
109 hi_debug_abort(); \
110 } \
111 } while (false)
112
118#define hi_assert_not_null(x) \
119 do { \
120 if (x == nullptr) { \
121 hi_set_terminate_message("assert not-null: " hi_stringify(x)); \
122 hi_debug_abort(); \
123 } \
124 } while (false)
125
126#ifndef NDEBUG
133#define hi_axiom(expression) hi_assert(expression)
134
140#define hi_axiom_not_null(expression) hi_assert_not_null(expression)
141
145#define hi_no_default() [[unlikely]] hi_debug_abort()
146
147#else
154#define hi_axiom(expression) hi_assume(expression)
155
161#define hi_axiom_not_null(expression) hi_assume(expression != nullptr)
162
166#define hi_no_default() hi_unreachable()
167#endif
168
172#define hi_static_no_default() \
173 []<bool Flag = false>() \
174 { \
175 static_assert(Flag); \
176 } \
177 ()
178
182#define hi_not_implemented() [[unlikely]] hi_debug_abort();
183
187#define hi_static_not_implemented() hi_static_no_default()
188
189} // namespace hi::inline v1
#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
common_integer< L, R... >::type common_integer_t
Get an integer type that will fit all values from all template parameters.
Definition type_traits.hpp:304
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