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 "type_traits.hpp"
11#include "terminate.hpp"
12#include "../macros.hpp"
13#include "exception.hpp"
14#include <exception>
15#include <ranges>
16
17hi_export_module(hikogui.utility.assert);
18
19hi_warning_push();
20// "C26472: Don't use a static_cast for arithmetic", asserts use static_cast specifically for savety.
21hi_warning_ignore_msvc(26472);
22
23hi_export namespace hi { inline namespace v1 {
24
31hi_export [[nodiscard]] constexpr bool bound_check(std::integral auto index, std::integral auto upper) noexcept
32{
33 return std::cmp_less(index, upper);
34}
35
44hi_export [[nodiscard]] constexpr bool bound_check(std::integral auto index, std::integral auto lower, std::integral auto upper) noexcept
45{
46#ifndef NDEBUG
47 if (std::cmp_greater(lower, upper)) {
48 hi_assert_abort("bound_check() lower is greater than upper.");
49 }
50#else
51 hi_assume(std::cmp_less_equal(lower, upper));
52#endif
53
54 return std::cmp_greater_equal(index, lower) and std::cmp_less(index, upper);
55}
56
65hi_export [[nodiscard]] constexpr bool bound_check(std::floating_point auto index, std::floating_point auto lower, std::floating_point auto upper) noexcept
66{
67#ifndef NDEBUG
68 if (lower > upper) {
69 hi_assert_abort("bound_check() lower is greater than upper.");
70 }
71#else
72 hi_assume(lower <= upper);
73#endif
74
75 return index >= lower and index <= upper;
76}
77
78template<typename Context>
79concept bound_check_range_helper = requires(Context&& range) {
80 {
81 std::ranges::size(range)
82 } -> std::unsigned_integral;
83};
84
91hi_export [[nodiscard]] constexpr bool bound_check(std::integral auto index, bound_check_range_helper auto&& range) noexcept
92{
93 static_assert(sizeof(index) <= sizeof(size_t));
94
95 if constexpr (std::is_signed_v<std::decay_t<decltype(index)>>) {
96 if (index < 0) {
97 return false;
98 }
99 }
100
101 return static_cast<size_t>(index) < std::ranges::size(range);
102}
103
104
105}} // namespace hi::v1
106
107hi_warning_pop();
Utilities for throwing exceptions and terminating the application.
The HikoGUI namespace.
Definition array_generic.hpp:20
hi_export constexpr bool bound_check(std::integral auto index, std::integral auto upper) noexcept
Check if an index is less than the bound.
Definition assert.hpp:31
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition assert.hpp:79