HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
type_traits.hpp
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
5#pragma once
6
7#include <cstdint>
8#include <type_traits>
9#include <string>
10#include <string_view>
11#include <memory>
12
13namespace tt {
14
15// clang-format off
16template<typename T> struct is_numeric_integer : std::false_type {};
17template<> struct is_numeric_integer<signed char> : std::true_type {};
18template<> struct is_numeric_integer<unsigned char> : std::true_type {};
19template<> struct is_numeric_integer<signed short> : std::true_type {};
20template<> struct is_numeric_integer<unsigned short> : std::true_type {};
21template<> struct is_numeric_integer<signed int> : std::true_type {};
22template<> struct is_numeric_integer<unsigned int> : std::true_type {};
23template<> struct is_numeric_integer<signed long> : std::true_type {};
24template<> struct is_numeric_integer<unsigned long> : std::true_type {};
25template<> struct is_numeric_integer<signed long long> : std::true_type {};
26template<> struct is_numeric_integer<unsigned long long> : std::true_type {};
27
31template<typename T>
32inline constexpr bool is_numeric_integer_v = is_numeric_integer<T>::value;
33
34template<typename T> struct is_character : std::false_type {};
35template<> struct is_character<char> : std::true_type {};
36template<> struct is_character<wchar_t> : std::true_type {};
37template<> struct is_character<char8_t> : std::true_type {};
38template<> struct is_character<char16_t> : std::true_type {};
39template<> struct is_character<char32_t> : std::true_type {};
40
44template<typename T>
45inline constexpr bool is_character_v = is_character<T>::value;
46
49template<typename T> struct make_string { };
50template<> struct make_string<char> { using type = std::string; };
51template<> struct make_string<wchar_t> { using type = std::wstring; };
52template<> struct make_string<char8_t> { using type = std::u8string; };
53template<> struct make_string<char16_t> { using type = std::u16string; };
54template<> struct make_string<char32_t> { using type = std::u32string; };
55
58template<typename T>
59using make_string_t = typename make_string<T>::type;
60
63template<typename T> struct make_string_view { using type = void; };
64template<> struct make_string_view<char> { using type = std::string_view; };
65template<> struct make_string_view<wchar_t> { using type = std::wstring_view; };
66template<> struct make_string_view<char8_t> { using type = std::u8string_view; };
67template<> struct make_string_view<char16_t> { using type = std::u16string_view; };
68template<> struct make_string_view<char32_t> { using type = std::u32string_view; };
69
72template<typename T>
73using make_string_view_t = typename make_string_view<T>::type;
74
75template<typename T, typename U>
77 using type = decltype(static_cast<T>(0) + static_cast<U>(0));
78};
79
80template<typename T, typename U>
81using make_promote_t = typename make_promote<T,U>::type;
82
83template<typename T, typename Ei=void>
85 using type = uintmax_t;
86};
87
88template<typename T>
89struct make_intmax<T,std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>>> {
90 using type = uintmax_t;
91};
92
93template<typename T>
94struct make_intmax<T,std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>>> {
95 using type = intmax_t;
96};
97
98template<typename T>
99using make_intmax_t = typename make_intmax<T>::type;
100
103template<typename T> struct make_larger { using type = T; };
104template<> struct make_larger<signed long> { using type = signed long long; };
105template<> struct make_larger<signed int> { using type = signed long; };
106template<> struct make_larger<signed short> { using type = signed int; };
107template<> struct make_larger<signed char> { using type = signed short; };
108template<> struct make_larger<unsigned long> { using type = unsigned long long; };
109template<> struct make_larger<unsigned int> { using type = unsigned long; };
110template<> struct make_larger<unsigned short> { using type = unsigned int; };
111template<> struct make_larger<unsigned char> { using type = unsigned short; };
112template<> struct make_larger<double> { using type = long double; };
113template<> struct make_larger<float> { using type = double; };
114
117template<typename T>
118using make_larger_t = typename make_larger<T>::type;
119
122template<typename To, typename From, typename Ei=void>
123struct copy_cv {};
124
125template<typename To, typename From>
126struct copy_cv<To,From,std::enable_if_t<!std::is_const_v<From> && !std::is_volatile_v<From>>> {
127 using type = std::remove_cv_t<To>;
128};
129
130template<typename To, typename From>
131struct copy_cv<To,From,std::enable_if_t<!std::is_const_v<From> && std::is_volatile_v<From>>> {
132 using type = std::remove_cv_t<To> volatile;
133};
134
135template<typename To, typename From>
136struct copy_cv<To,From,std::enable_if_t<std::is_const_v<From> && !std::is_volatile_v<From>>> {
137 using type = std::remove_cv_t<To> const;
138};
139
140template<typename To, typename From>
141struct copy_cv<To,From,std::enable_if_t<std::is_const_v<From> && std::is_volatile_v<From>>> {
142 using type = std::remove_cv_t<To> const volatile;
143};
144
147template<typename To, typename From>
148using copy_cv_t = typename copy_cv<To,From>::type;
149
150template <typename T> struct has_value_type
151{
152 template <typename C> static std::true_type test(typename C::value_type *);
153 template <typename> static std::false_type test(...);
154
155 static const bool value = std::is_same_v<decltype(test<T>(nullptr)), std::true_type>;
156};
157
158template<typename T>
159inline constexpr bool has_value_type_v = has_value_type<T>::value;
160
161template <typename T> struct has_add_callback
162{
163 template <typename C> static std::true_type test(decltype(&C::add_callback) func_ptr);
164 template <typename> static std::false_type test(...);
165
166 static const bool value = std::is_same_v<decltype(test<T>(nullptr)), std::true_type>;
167};
168
169template<typename T>
170inline constexpr bool has_add_callback_v = has_add_callback<T>::value;
171
172template<typename BaseType, typename DerivedType>
173struct is_decayed_base_of : public std::is_base_of<std::decay_t<BaseType>,std::decay_t<DerivedType>> {};
174
175template<typename BaseType, typename DerivedType>
176constexpr bool is_decayed_base_of_v = is_decayed_base_of<BaseType,DerivedType>::value;
177
178template<typename DerivedType, typename BaseType>
179struct is_derived_from : public std::is_base_of<BaseType,DerivedType> {};
180
181template<typename DerivedType, typename BaseType>
182constexpr bool is_derived_from_v = is_derived_from<DerivedType,BaseType>::value;
183
184template<typename DerivedType, typename BaseType>
185struct is_decayed_derived_from : public is_decayed_base_of<BaseType,DerivedType> {};
186
187template<typename DerivedType, typename BaseType>
188constexpr bool is_decayed_derived_from_v = is_decayed_derived_from<DerivedType,BaseType>::value;
189
190template<typename T1, typename T2>
191constexpr bool is_different_v = !std::is_same_v<T1,T2>;
192
193template<typename T>
194struct is_atomic : public std::false_type {};
195
196template<typename T>
197struct is_atomic<std::atomic<T>> : public std::true_type {};
198
199template<typename T>
200constexpr bool is_atomic_v = is_atomic<T>::value;
201
202template<typename T>
203constexpr bool may_be_atomic_v = std::is_trivially_copyable_v<T> and std::is_copy_constructible_v<T> and
204 std::is_move_constructible_v<T> and std::is_copy_assignable_v<T> and
205 std::is_move_assignable_v<T>;
206
207template<typename First, typename Second>
208struct use_first {
209 using type = First;
210};
211
212template<typename First, typename Second>
214
215template<typename T>
217
218template<typename T> struct acts_as_pointer<std::shared_ptr<T>> : public std::true_type {};
219template<typename T> struct acts_as_pointer<std::shared_ptr<T> &&> : public std::true_type {};
220template<typename T> struct acts_as_pointer<std::shared_ptr<T> &> : public std::true_type {};
221template<typename T> struct acts_as_pointer<std::shared_ptr<T> const &> : public std::true_type {};
222template<typename T> struct acts_as_pointer<std::weak_ptr<T>> : public std::true_type {};
223template<typename T> struct acts_as_pointer<std::weak_ptr<T> &&> : public std::true_type {};
224template<typename T> struct acts_as_pointer<std::weak_ptr<T> &> : public std::true_type {};
225template<typename T> struct acts_as_pointer<std::weak_ptr<T> const &> : public std::true_type {};
226template<typename T> struct acts_as_pointer<std::unique_ptr<T>> : public std::true_type {};
227template<typename T> struct acts_as_pointer<std::unique_ptr<T> &&> : public std::true_type {};
228template<typename T> struct acts_as_pointer<std::unique_ptr<T> &> : public std::true_type {};
229template<typename T> struct acts_as_pointer<std::unique_ptr<T> const &> : public std::true_type {};
230template<typename T> struct acts_as_pointer<T *> : public std::true_type {};
231
232template<typename T>
233constexpr bool acts_as_pointer_v = acts_as_pointer<T>::value;
234
235#define tt_call_method(object, method, ...) \
236 [&]() { \
237 if constexpr (acts_as_pointer_v<decltype(object)>) { \
238 return object->method(__VA_ARGS__); \
239 } else { \
240 return object.method(__VA_ARGS__); \
241 } \
242 }()
243
244}
245
STL namespace.
Definition type_traits.hpp:16
Definition type_traits.hpp:34
type-trait to convert a character to a string type.
Definition type_traits.hpp:49
type-trait to convert a character to a string_view type.
Definition type_traits.hpp:63
Definition type_traits.hpp:76
Definition type_traits.hpp:84
Type-trait to increase the size of an integral type.
Definition type_traits.hpp:103
Type-trait to copy const volitile qualifiers from one type to another.
Definition type_traits.hpp:123
Definition type_traits.hpp:151
Definition type_traits.hpp:162
Definition type_traits.hpp:173
Definition type_traits.hpp:179
Definition type_traits.hpp:185
Definition type_traits.hpp:194
Definition type_traits.hpp:208
Definition type_traits.hpp:216