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
8#pragma once
9
10#include "architecture.hpp"
11#include "debugger.hpp"
12#include "utility.hpp"
13#include "type_traits.hpp"
14#include "exception.hpp"
15#include <exception>
16#include <ranges>
17
18hi_warning_push();
19// "C26472: Don't use a static_cast for arithmetic", asserts use static_cast specifically for savety.
21
22namespace hi { inline namespace v1 {
23
30[[nodiscard]] constexpr bool bound_check(std::unsigned_integral auto index, std::unsigned_integral auto upper) noexcept
31{
32 using value_type = common_integer_t<decltype(index), decltype(upper)>;
33
34 hilet index_ = static_cast<value_type>(index);
35 hilet upper_ = static_cast<value_type>(upper);
36 return index_ < upper_;
37}
38
47[[nodiscard]] constexpr bool bound_check(std::integral auto index, std::integral auto lower, std::integral auto upper) noexcept
48{
49 using value_type = common_integer_t<decltype(index), decltype(lower), decltype(upper)>;
50
51 auto index_ = static_cast<value_type>(index);
52 auto lower_ = static_cast<value_type>(lower);
53 auto upper_ = static_cast<value_type>(upper);
54
55 hi_axiom(lower_ <= upper_);
56 return index_ >= lower_ and index_ < upper_;
57}
58
59template<typename Context>
60concept bound_check_range_helper = requires(Context&& range) {
61 {
62 std::ranges::size(range)
63 } -> std::unsigned_integral;
64 };
65
72[[nodiscard]] constexpr bool bound_check(std::integral auto index, bound_check_range_helper auto&& range) noexcept
73{
74 static_assert(sizeof(index) <= sizeof(size_t));
75
76 if constexpr (std::is_signed_v<std::decay_t<decltype(index)>>) {
77 if (index < 0) {
78 return false;
79 }
80 }
81
82 return static_cast<size_t>(index) < std::ranges::size(range);
83}
84
95#define hi_check(expression, message, ...) \
96 do { \
97 if (not(expression)) { \
98 if constexpr (__VA_OPT__(not ) false) { \
99 throw parse_error(std::format(message __VA_OPT__(, ) __VA_ARGS__)); \
100 } else { \
101 throw parse_error(message); \
102 } \
103 } \
104 } while (false)
105
117#define hi_check_bounds(x, ...) \
118 do { \
119 if (not ::hi::bound_check(x, __VA_ARGS__)) { \
120 throw parse_error("assert bounds: " hi_stringify(x) " between " hi_stringify(__VA_ARGS__)); \
121 } \
122 } while (false)
123
136#define hi_check_subspan(span, offset, ...) \
137 [&](auto _hi_check_subspan_span, size_t _hi_check_subspan_offset, auto... _hi_check_subspan_count) { \
138 if constexpr (sizeof...(_hi_check_subspan_count) == 0) { \
139 if (_hi_check_subspan_offset < _hi_check_subspan_span.size()) { \
140 return _hi_check_subspan_span.subspan(_hi_check_subspan_offset); \
141 } \
142 } else if constexpr (sizeof...(_hi_check_subspan_count) == 1) { \
143 if (_hi_check_subspan_offset + wide_cast<size_t>(_hi_check_subspan_count...) <= _hi_check_subspan_span.size()) { \
144 return _hi_check_subspan_span.subspan(_hi_check_subspan_offset, _hi_check_subspan_count...); \
145 } \
146 } \
147 throw parse_error( \
148 "assert bounds on: " hi_stringify(span) ".subspan(" hi_stringify(offset __VA_OPT__(", ") __VA_ARGS__) ")"); \
149 }(span, offset __VA_OPT__(, ) __VA_ARGS__)
150
161#define hi_check_at(span, index) \
162 [&](auto _hi_check_subspan_span, size_t _hi_check_subspan_index) { \
163 if (_hi_check_subspan_index < _hi_check_subspan_span.size()) { \
164 return _hi_check_subspan_span[_hi_check_subspan_index]; \
165 } else { \
166 throw parse_error("assert bounds on: " hi_stringify(span) "[" hi_stringify(index) "]"); \
167 } \
168 }(span, index)
169
170#define hi_hresult_check(expression) \
171 ([](HRESULT result) { \
172 if (FAILED(result)) { \
173 throw ::hi::io_error(std::format("Call to '{}' failed with {:08x}", #expression, result)); \
174 } \
175 return result; \
176 }(expression))
177
184#define hi_assert(expression, ...) \
185 do { \
186 if (not(expression)) { \
187 hi_debug_abort("assert: " __VA_ARGS__ " (" hi_stringify(expression) ")"); \
188 } \
189 } while (false)
190
197#define hi_assert_or_return(x, y) \
198 if (!(x)) { \
199 [[unlikely]] return y; \
200 }
201
210#define hi_assert_bounds(x, ...) \
211 do { \
212 if (not ::hi::bound_check(x, __VA_ARGS__)) { \
213 hi_debug_abort("assert bounds: " hi_stringify(x) " between " hi_stringify(__VA_ARGS__)); \
214 } \
215 } while (false)
216
223#define hi_assert_not_null(x, ...) \
224 do { \
225 if (x == nullptr) { \
226 hi_debug_abort("assert not-null: " __VA_ARGS__ " (" hi_stringify(x) ")"); \
227 } \
228 } while (false)
229
230#ifndef NDEBUG
238#define hi_axiom(expression, ...) hi_assert(expression __VA_OPT__(, ) __VA_ARGS__)
239
249#define hi_axiom_bounds(x, ...) hi_assert_bounds(x, __VA_ARGS__)
250
257#define hi_axiom_not_null(expression, ...) hi_assert_not_null(expression __VA_OPT__(, ) __VA_ARGS__)
258
264#define hi_no_default(...) [[unlikely]] hi_debug_abort("Reached no-default:" __VA_ARGS__)
265
266#else
274#define hi_axiom(expression, ...) hi_assume(expression)
275
285#define hi_axiom_bounds(x, ...) hi_assume(not ::hi::bound_check(x, __VA_ARGS__))
286
293#define hi_axiom_not_null(expression, ...) hi_assume(expression != nullptr)
294
300#define hi_no_default(...) hi_unreachable()
301#endif
302
308#define hi_static_no_default(...) \
309 []<bool Flag = false>() \
310 { \
311 static_assert(Flag, "No default: " __VA_ARGS__); \
312 } \
313 ()
314
320#define hi_not_implemented(...) [[unlikely]] hi_debug_abort("Not implemented: " __VA_ARGS__);
321
327#define hi_static_not_implemented(...) hi_static_no_default("Not implemented: " __VA_ARGS__)
328
329}} // namespace hi::v1
330
331hi_warning_pop();
Utilities to interact with the debugger this application runs under.
hi_warning_ignore_msvc(26472)
Definition int_carry.hpp:27
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
Utilities for throwing exceptions and terminating the application.
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
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:30
Definition assert.hpp:60