HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
type_traits.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2019-2021.
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 <cstdint>
12#include <type_traits>
13#include <string>
14#include <string_view>
15#include <memory>
16
17namespace hi::inline v1 {
18
19// clang-format off
20
28template<typename T> struct is_numeric_signed_integral : std::false_type {};
29template<> struct is_numeric_signed_integral<signed char> : std::true_type {};
30template<> struct is_numeric_signed_integral<signed short> : std::true_type {};
31template<> struct is_numeric_signed_integral<signed int> : std::true_type {};
32template<> struct is_numeric_signed_integral<signed long> : std::true_type {};
33template<> struct is_numeric_signed_integral<signed long long> : std::true_type {};
34
37template<typename T>
39
47template<typename T> struct is_numeric_unsigned_integral : std::false_type {};
48template<> struct is_numeric_unsigned_integral<unsigned int> : std::true_type {};
49template<> struct is_numeric_unsigned_integral<unsigned char> : std::true_type {};
50template<> struct is_numeric_unsigned_integral<unsigned short> : std::true_type {};
51template<> struct is_numeric_unsigned_integral<unsigned long> : std::true_type {};
52template<> struct is_numeric_unsigned_integral<unsigned long long> : std::true_type {};
53
56template<typename T>
58
67template<typename T> struct is_numeric_integral : std::false_type {};
68template<> struct is_numeric_integral<unsigned int> : std::true_type {};
69template<> struct is_numeric_integral<unsigned char> : std::true_type {};
70template<> struct is_numeric_integral<unsigned short> : std::true_type {};
71template<> struct is_numeric_integral<unsigned long> : std::true_type {};
72template<> struct is_numeric_integral<unsigned long long> : std::true_type {};
73template<> struct is_numeric_integral<signed char> : std::true_type {};
74template<> struct is_numeric_integral<signed short> : std::true_type {};
75template<> struct is_numeric_integral<signed int> : std::true_type {};
76template<> struct is_numeric_integral<signed long> : std::true_type {};
77template<> struct is_numeric_integral<signed long long> : std::true_type {};
78
81template<typename T> inline constexpr bool is_numeric_integral_v = is_numeric_integral<T>::value;
82
92template<typename T> struct is_numeric : std::false_type {};
93template<> struct is_numeric<unsigned char> : std::true_type {};
94template<> struct is_numeric<unsigned short> : std::true_type {};
95template<> struct is_numeric<unsigned int> : std::true_type {};
96template<> struct is_numeric<unsigned long> : std::true_type {};
97template<> struct is_numeric<unsigned long long> : std::true_type {};
98template<> struct is_numeric<signed char> : std::true_type {};
99template<> struct is_numeric<signed short> : std::true_type {};
100template<> struct is_numeric<signed int> : std::true_type {};
101template<> struct is_numeric<signed long> : std::true_type {};
102template<> struct is_numeric<signed long long> : std::true_type {};
103template<> struct is_numeric<float> : std::true_type {};
104template<> struct is_numeric<double> : std::true_type {};
105template<> struct is_numeric<long double> : std::true_type {};
106
109template<typename T> inline constexpr bool is_numeric_v = is_numeric<T>::value;
110
111template<typename T> struct is_character : std::false_type {};
112template<> struct is_character<char> : std::true_type {};
113template<> struct is_character<wchar_t> : std::true_type {};
114template<> struct is_character<char8_t> : std::true_type {};
115template<> struct is_character<char16_t> : std::true_type {};
116template<> struct is_character<char32_t> : std::true_type {};
117
121template<typename T> inline constexpr bool is_character_v = is_character<T>::value;
122
125template<typename T> struct make_string { };
126template<> struct make_string<char> { using type = std::string; };
127template<> struct make_string<wchar_t> { using type = std::wstring; };
128template<> struct make_string<char8_t> { using type = std::u8string; };
129template<> struct make_string<char16_t> { using type = std::u16string; };
130template<> struct make_string<char32_t> { using type = std::u32string; };
131
134template<typename T> using make_string_t = typename make_string<T>::type;
135
138template<typename T> struct make_string_view { using type = void; };
139template<> struct make_string_view<char> { using type = std::string_view; };
140template<> struct make_string_view<wchar_t> { using type = std::wstring_view; };
141template<> struct make_string_view<char8_t> { using type = std::u8string_view; };
142template<> struct make_string_view<char16_t> { using type = std::u16string_view; };
143template<> struct make_string_view<char32_t> { using type = std::u32string_view; };
144
147template<typename T>
148using make_string_view_t = typename make_string_view<T>::type;
149
150template<typename T, typename U>
152 using type = decltype(static_cast<T>(0) + static_cast<U>(0));
153};
154
155template<typename T, typename U>
156using make_promote_t = typename make_promote<T,U>::type;
157
158template<typename T, typename Ei=void>
160 using type = uintmax_t;
161};
162
163template<std::unsigned_integral T>
164struct make_intmax<T> {
165 using type = uintmax_t;
166};
167
168template<std::signed_integral T>
169struct make_intmax<T> {
170 using type = intmax_t;
171};
172
173template<typename T>
174using make_intmax_t = typename make_intmax<T>::type;
175
179template<std::size_t N> struct has_intxx : public std::false_type {};
180
184template<std::size_t N> struct has_uintxx : public std::false_type {};
185
189template<std::size_t N> struct has_floatxx : public std::false_type {};
190
194template<std::size_t N> struct make_intxx {};
195
199template<std::size_t N> struct make_uintxx {};
200
204template<std::size_t N> struct make_floatxx {};
205
206#if (HI_COMPILER == HI_CC_CLANG || HI_COMPILER == HI_CC_GCC) && (HI_PROCESSOR == HI_CPU_X64)
207template<> struct has_intxx<128> : public std::true_type {};
208template<> struct has_uintxx<128> : public std::true_type {};
209template<> struct make_intxx<128> { using type = __int128; };
210template<> struct make_uintxx<128> { using type = unsigned __int128; };
211#endif
212template<> struct has_intxx<64> : public std::true_type {};
213template<> struct has_uintxx<64> : public std::true_type {};
214template<> struct has_floatxx<64> : public std::true_type {};
215template<> struct make_intxx<64> { using type = int64_t; };
216template<> struct make_uintxx<64> { using type = uint64_t; };
217template<> struct make_floatxx<64> { using type = double; };
218template<> struct has_intxx<32> : public std::true_type {};
219template<> struct has_uintxx<32> : public std::true_type {};
220template<> struct has_floatxx<32> : public std::true_type {};
221template<> struct make_intxx<32> { using type = int32_t; };
222template<> struct make_uintxx<32> { using type = uint32_t; };
223template<> struct make_floatxx<32> { using type = float; };
224template<> struct has_intxx<16> : public std::true_type {};
225template<> struct has_uintxx<16> : public std::true_type {};
226template<> struct make_intxx<16> { using type = int16_t; };
227template<> struct make_uintxx<16> { using type = uint16_t; };
228template<> struct has_intxx<8> : public std::true_type {};
229template<> struct has_uintxx<8> : public std::true_type {};
230template<> struct make_intxx<8> { using type = int8_t; };
231template<> struct make_uintxx<8> { using type = uint8_t; };
232
233template<std::size_t N> constexpr bool has_intxx_v = has_intxx<N>::value;
234template<std::size_t N> constexpr bool has_uintxx_v = has_uintxx<N>::value;
235template<std::size_t N> constexpr bool has_floatxx_v = has_floatxx<N>::value;
236template<std::size_t N> using make_intxx_t = typename make_intxx<N>::type;
237template<std::size_t N> using make_uintxx_t = typename make_uintxx<N>::type;
238template<std::size_t N> using make_floatxx_t = typename make_floatxx<N>::type;
239
240
243template<typename To, typename From, typename Ei=void>
244struct copy_cv {};
245
246template<typename To, typename From> requires(not std::is_const_v<From> and not std::is_volatile_v<From>)
248 using type = std::remove_cv_t<To>;
249};
250
251template<typename To, typename From> requires(not std::is_const_v<From> and std::is_volatile_v<From>)
252struct copy_cv<To,From> {
253 using type = std::remove_cv_t<To> volatile;
254};
255
256template<typename To, typename From> requires(std::is_const_v<From> and not std::is_volatile_v<From>)
257struct copy_cv<To,From> {
258 using type = std::remove_cv_t<To> const;
259};
260
261template<typename To, typename From> requires(std::is_const_v<From> and std::is_volatile_v<From>)
262struct copy_cv<To,From> {
263 using type = std::remove_cv_t<To> const volatile;
264};
265
268template<typename To, typename From>
270
271template <typename T> struct has_value_type
272{
273 template <typename C> static std::true_type test(typename C::value_type *);
274 template <typename> static std::false_type test(...);
275
276 static const bool value = std::is_same_v<decltype(test<T>(nullptr)), std::true_type>;
277};
278
279template<typename T>
280inline constexpr bool has_value_type_v = has_value_type<T>::value;
281
282template <typename T> struct has_add_callback
283{
284 template <typename C> static std::true_type test(decltype(&C::add_callback) func_ptr);
285 template <typename> static std::false_type test(...);
286
287 static const bool value = std::is_same_v<decltype(test<T>(nullptr)), std::true_type>;
288};
289
290template<typename T>
291inline constexpr bool has_add_callback_v = has_add_callback<T>::value;
292
293template<typename BaseType, typename DerivedType>
294struct is_decayed_base_of : public std::is_base_of<std::decay_t<BaseType>,std::decay_t<DerivedType>> {};
295
296template<typename BaseType, typename DerivedType>
297constexpr bool is_decayed_base_of_v = is_decayed_base_of<BaseType,DerivedType>::value;
298
299template<typename DerivedType, typename BaseType>
300struct is_derived_from : public std::is_base_of<BaseType,DerivedType> {};
301
302template<typename DerivedType, typename BaseType>
303constexpr bool is_derived_from_v = is_derived_from<DerivedType,BaseType>::value;
304
305template<typename DerivedType, typename BaseType>
306struct is_decayed_derived_from : public is_decayed_base_of<BaseType,DerivedType> {};
307
308template<typename DerivedType, typename BaseType>
309constexpr bool is_decayed_derived_from_v = is_decayed_derived_from<DerivedType,BaseType>::value;
310
314template<typename T1, typename T2>
315constexpr bool is_different_v = not std::is_same_v<std::remove_cvref_t<T1>,std::remove_cvref_t<T2>>;
316
317template<typename T>
318struct is_atomic : public std::false_type {};
319
320template<typename T>
321struct is_atomic<std::atomic<T>> : public std::true_type {};
322
323template<typename T>
324constexpr bool is_atomic_v = is_atomic<T>::value;
325
326template<typename First, typename Second>
327struct use_first {
328 using type = First;
329};
330
331template<typename First, typename Second>
333
334template<typename T>
336
337template<typename T> struct acts_as_pointer<std::shared_ptr<T>> : public std::true_type {};
338template<typename T> struct acts_as_pointer<std::shared_ptr<T> &&> : public std::true_type {};
339template<typename T> struct acts_as_pointer<std::shared_ptr<T> &> : public std::true_type {};
340template<typename T> struct acts_as_pointer<std::shared_ptr<T> const &> : public std::true_type {};
341template<typename T> struct acts_as_pointer<std::weak_ptr<T>> : public std::true_type {};
342template<typename T> struct acts_as_pointer<std::weak_ptr<T> &&> : public std::true_type {};
343template<typename T> struct acts_as_pointer<std::weak_ptr<T> &> : public std::true_type {};
344template<typename T> struct acts_as_pointer<std::weak_ptr<T> const &> : public std::true_type {};
345template<typename T> struct acts_as_pointer<std::unique_ptr<T>> : public std::true_type {};
346template<typename T> struct acts_as_pointer<std::unique_ptr<T> &&> : public std::true_type {};
347template<typename T> struct acts_as_pointer<std::unique_ptr<T> &> : public std::true_type {};
348template<typename T> struct acts_as_pointer<std::unique_ptr<T> const &> : public std::true_type {};
349template<typename T> struct acts_as_pointer<T *> : public std::true_type {};
350
351template<typename T>
352constexpr bool acts_as_pointer_v = acts_as_pointer<T>::value;
353
354#define hi_call_method(object, method, ...) \
355 [&]() { \
356 if constexpr (acts_as_pointer_v<decltype(object)>) { \
357 return object->method(__VA_ARGS__); \
358 } else { \
359 return object.method(__VA_ARGS__); \
360 } \
361 }()
362
363// clang-format on
364
367template<typename Out, typename In>
370
380template<typename T, typename Forward>
382
383template<typename T>
384struct is_forward_of<T, T> : public std::true_type {};
385
386template<typename T>
387struct is_forward_of<T, T const &> : public std::true_type {
388};
389
390template<typename T>
391struct is_forward_of<T, T &> : public std::true_type {
392};
393
394template<typename T, typename Forward>
395constexpr bool is_forward_of_v = is_forward_of<T, Forward>::value;
396
401template<typename T>
403 using type = decltype(std::declval<T>().await_resume());
404};
405
410template<typename T>
411using await_resume_result_t = await_resume_result<T>::type;
412
413} // namespace hi::inline v1
constexpr bool is_numeric_v
Definition type_traits.hpp:109
constexpr bool is_different_v
If the types are different.
Definition type_traits.hpp:315
typename make_string< T >::type make_string_t
type-trait to convert a character to a string type.
Definition type_traits.hpp:134
typename make_string_view< T >::type make_string_view_t
type-trait to convert a character to a string_view type.
Definition type_traits.hpp:148
constexpr bool is_numeric_unsigned_integral_v
Definition type_traits.hpp:57
await_resume_result< T >::type await_resume_result_t
Get the result type of an awaitable.
Definition type_traits.hpp:411
typename copy_cv< To, From >::type copy_cv_t
Type-trait to copy const volatile qualifiers from one type to another.
Definition type_traits.hpp:269
constexpr bool is_numeric_integral_v
Definition type_traits.hpp:81
constexpr bool type_in_range_v
All values of numeric type In can be represented without loss of precision by numeric type Out.
Definition type_traits.hpp:368
constexpr bool is_character_v
Definition type_traits.hpp:121
constexpr bool is_numeric_signed_integral_v
Definition type_traits.hpp:38
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
STL namespace.
Is a numeric signed integer.
Definition type_traits.hpp:28
Is a numeric unsigned integer.
Definition type_traits.hpp:47
Is a numeric integer.
Definition type_traits.hpp:67
Is a numeric.
Definition type_traits.hpp:92
Definition type_traits.hpp:111
type-trait to convert a character to a string type.
Definition type_traits.hpp:125
type-trait to convert a character to a string_view type.
Definition type_traits.hpp:138
Definition type_traits.hpp:151
Definition type_traits.hpp:159
Has an signed integer of a specific size.
Definition type_traits.hpp:179
Has an unsigned integer of a specific size.
Definition type_traits.hpp:184
Has an float of a specific size.
Definition type_traits.hpp:189
Make an signed integer.
Definition type_traits.hpp:194
Make an unsigned integer.
Definition type_traits.hpp:199
Make an floating point.
Definition type_traits.hpp:204
Type-trait to copy const volitile qualifiers from one type to another.
Definition type_traits.hpp:244
Definition type_traits.hpp:272
Definition type_traits.hpp:283
Definition type_traits.hpp:294
Definition type_traits.hpp:300
Definition type_traits.hpp:306
Definition type_traits.hpp:318
Definition type_traits.hpp:327
Definition type_traits.hpp:335
True if T is a forwarded type of OfType.
Definition type_traits.hpp:381
Get the result type of an awaitable.
Definition type_traits.hpp:402