HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-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
10#pragma once
11
12#include <cstddef>
13#include <string>
14#include <chrono>
15
16#ifndef hilet
23#define hilet auto const
24#endif
25
26#ifndef hi_forward
29#define hi_forward(x) std::forward<decltype(x)>(x)
30#endif
31
32// One clang-format off is not enough to stop clang-format to format.
33// clang-format off
34// clang-format off
35#define hi_num_va_args_impl( \
36 _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
37 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
38 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
39 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
40 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
41 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
42 _61,_62,_63,N,...) N
43#define hi_num_va_args_(...) hi_num_va_args_impl(__VA_ARGS__)
44
50#define hi_num_va_args(...) hi_num_va_args_(__VA_ARGS__ __VA_OPT__(,) \
51 63,62,61,60, \
52 59,58,57,56,55,54,53,52,51,50, \
53 49,48,47,46,45,44,43,42,41,40, \
54 39,38,37,36,35,34,33,32,31,30, \
55 29,28,27,26,25,24,23,22,21,20, \
56 19,18,17,16,15,14,13,12,11,10, \
57 9,8,7,6,5,4,3,2,1,0)
58// clang-format on
59
60#define hi_parans ()
61
62#define hi_expand1(...) __VA_ARGS__
63#define hi_expand2(...) hi_expand1(hi_expand1(hi_expand1(hi_expand1(__VA_ARGS__))))
64#define hi_expand3(...) hi_expand2(hi_expand2(hi_expand2(hi_expand2(__VA_ARGS__))))
65#define hi_expand4(...) hi_expand3(hi_expand3(hi_expand3(hi_expand3(__VA_ARGS__))))
66
71#define hi_expand(...) hi_expand4(hi_expand4(hi_expand4(hi_expand4(__VA_ARGS__))))
72
73#define hi_for_each_again() hi_for_each_helper
74#define hi_for_each_helper(macro, first_arg, ...) macro(first_arg) __VA_OPT__(hi_for_each_again hi_parans(macro, __VA_ARGS__))
75
81#define hi_for_each(macro, ...) __VA_OPT__(hi_expand(hi_for_each_helper(macro, __VA_ARGS__)))
82
83#define hi_stringify_(x) #x
84#define hi_stringify(x) hi_stringify_(x)
85
86#define hi_cat_(a, b) a##b
87#define hi_cat(a, b) hi_cat_(a, b)
88
89#define hi_return_on_self_assignment(other) \
90 if (&(other) == this) [[unlikely]] \
91 return *this;
92
102#define hi_get_overloaded_macro2(_1, _2, name, ...) name
103
104#if defined(__clang__)
105#define hi_unreachable() __builtin_unreachable()
106#define hi_assume(condition) __builtin_assume(to_bool(condition))
107#define hi_force_inline inline __attribute__((always_inline))
108#define hi_no_inline __attribute__((noinline))
109#define hi_restrict __restrict__
110#define hi_warning_push() _Pragma("warning(push)")
111#define hi_warning_pop() _Pragma("warning(push)")
112#define hi_warning_ignore_msvc(code)
113#define hi_warning_ignore_clang(a) _Pragma(hi_stringify(clang diagnostic ignored a))
114#define hi_export
115#define hi_typename typename
116#define hi_constexpr
117
118#elif defined(_MSC_BUILD)
119#define hi_unreachable() __assume(0)
120#define hi_assume(condition) __assume(condition)
121#define hi_force_inline __forceinline
122#define hi_no_inline __declspec(noinline)
123#define hi_restrict __restrict
124#define hi_warning_push() _Pragma("warning( push )")
125#define hi_warning_pop() _Pragma("warning( pop )")
126#define hi_msvc_pragma(a) _Pragma(a)
127#define hi_warning_ignore_msvc(code) _Pragma(hi_stringify(warning(disable : code)))
128#define hi_warning_ignore_clang(a)
129#define hi_export __declspec(dllexport)
130#define hi_typename
131#define hi_constexpr constexpr
132
133#elif defined(__GNUC__)
134#define hi_unreachable() __builtin_unreachable()
135#define hi_assume(condition) \
136 do { \
137 if (!(condition)) \
138 hi_unreachable(); \
139 } while (false)
140#define hi_force_inline inline __attribute__((always_inline))
141#define hi_no_inline __attribute__((noinline))
142#define hi_restrict __restrict__
143#define hi_warning_push() _Pragma("warning(push)")
144#define hi_warning_pop() _Pragma("warning(pop)")
145#define hi_msvc_pragma(a)
146#define hi_warning_ignore_clang(a)
147#define msvc_pragma(a)
148#define hi_typename
149
150#else
151#define hi_unreachable() std::terminate()
152#define hi_assume(condition) static_assert(sizeof(condition) == 1)
153#define hi_force_inline inline
154#define hi_no_inline
155#define hi_restrict
156#define hi_warning_push()
157#define hi_warning_pop()
158#define hi_msvc_pragma(a)
159#define hi_warning_ignore_clang(a)
160#define msvc_pragma(a)
161#define hi_typename
162#endif
163
164hi_warning_push();
165// C26472: Don't use static_cast for arithmetic conversions, Use brace initialization, gsl::narrow_cast or gsl::narrow (type.1).
166// We do not have access to narrow_cast in this file.
167hi_warning_ignore_msvc(26472);
168
169namespace hi { inline namespace v1 {
170
174
175#define ssizeof(x) (static_cast<ssize_t>(sizeof(x)))
176
177constexpr std::size_t operator"" _uz(unsigned long long lhs) noexcept
178{
179 return static_cast<std::size_t>(lhs);
180}
181
182constexpr std::size_t operator"" _zu(unsigned long long lhs) noexcept
183{
184 return static_cast<std::size_t>(lhs);
185}
186
187constexpr std::ptrdiff_t operator"" _z(unsigned long long lhs) noexcept
188{
189 return static_cast<std::ptrdiff_t>(lhs);
190}
191
195template<typename T, typename U>
196[[nodiscard]] bool compare_store(T& lhs, U&& rhs) noexcept
197{
198 if (lhs != rhs) {
199 lhs = std::forward<U>(rhs);
200 return true;
201 } else {
202 return false;
203 }
204}
205
211template<typename T, typename U>
212[[nodiscard]] bool compare_store(std::atomic<T>& lhs, U&& rhs) noexcept
213{
214 return lhs.exchange(rhs, std::memory_order::relaxed) != rhs;
215}
216
217}} // namespace hi::v1
218
219hi_warning_pop();
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:196