OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 * | 6 * |
7 * | 7 * |
8 * This header provides some of the helpers (std::integral_constant) and | 8 * This header provides some of the helpers (std::integral_constant) and |
9 * type transformations (std::conditional) which will become available with | 9 * type transformations (std::conditional) which will become available with |
10 * C++11 in the type_traits header. | 10 * C++11 in the type_traits header. |
11 */ | 11 */ |
12 | 12 |
13 #ifndef SkTLogic_DEFINED | 13 #ifndef SkTLogic_DEFINED |
14 #define SkTLogic_DEFINED | 14 #define SkTLogic_DEFINED |
15 | 15 |
| 16 #include "SkTypes.h" |
| 17 |
| 18 #include <stddef.h> |
16 #include <stdint.h> | 19 #include <stdint.h> |
17 | 20 |
18 namespace skstd { | 21 namespace skstd { |
19 | 22 |
| 23 using nullptr_t = decltype(nullptr); |
| 24 |
20 template <typename T, T v> struct integral_constant { | 25 template <typename T, T v> struct integral_constant { |
21 static const/*expr*/ T value = v; | 26 static const/*expr*/ T value = v; |
22 using value_type = T; | 27 using value_type = T; |
23 using type = integral_constant<T, v>; | 28 using type = integral_constant<T, v>; |
24 //constexpr operator value_type() const noexcept { return value; } | 29 //constexpr operator value_type() const noexcept { return value; } |
25 //constexpr value_type operator()() const noexcept { return value; } | 30 //constexpr value_type operator()() const noexcept { return value; } |
26 }; | 31 }; |
27 | 32 |
28 template <bool B> using bool_constant = integral_constant<bool, B>; | 33 template <bool B> using bool_constant = integral_constant<bool, B>; |
29 | 34 |
(...skipping 17 matching lines...) Expand all Loading... |
47 template <typename T> using remove_volatile_t = typename remove_volatile<T>::typ
e; | 52 template <typename T> using remove_volatile_t = typename remove_volatile<T>::typ
e; |
48 | 53 |
49 template <typename T> struct remove_cv { using type = remove_volatile_t<remove_c
onst_t<T>>; }; | 54 template <typename T> struct remove_cv { using type = remove_volatile_t<remove_c
onst_t<T>>; }; |
50 template <typename T> using remove_cv_t = typename remove_cv<T>::type; | 55 template <typename T> using remove_cv_t = typename remove_cv<T>::type; |
51 | 56 |
52 template <typename T> struct remove_reference { using type = T; }; | 57 template <typename T> struct remove_reference { using type = T; }; |
53 template <typename T> struct remove_reference<T&> { using type = T; }; | 58 template <typename T> struct remove_reference<T&> { using type = T; }; |
54 template <typename T> struct remove_reference<T&&> { using type = T; }; | 59 template <typename T> struct remove_reference<T&&> { using type = T; }; |
55 template <typename T> using remove_reference_t = typename remove_reference<T>::t
ype; | 60 template <typename T> using remove_reference_t = typename remove_reference<T>::t
ype; |
56 | 61 |
| 62 template <typename T> struct remove_extent { using type = T; }; |
| 63 template <typename T> struct remove_extent<T[]> { using type = T; }; |
| 64 template <typename T, size_t N> struct remove_extent<T[N]> { using type = T;}; |
| 65 template <typename T> using remove_extent_t = typename remove_extent<T>::type; |
| 66 |
57 template <typename T, typename U> struct is_same : false_type {}; | 67 template <typename T, typename U> struct is_same : false_type {}; |
58 template <typename T> struct is_same<T, T> : true_type {}; | 68 template <typename T> struct is_same<T, T> : true_type {}; |
59 | 69 |
60 template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {}; | 70 template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {}; |
61 | 71 |
62 template <typename T> struct is_const : false_type {}; | 72 template <typename T> struct is_const : false_type {}; |
63 template <typename T> struct is_const<const T> : true_type {}; | 73 template <typename T> struct is_const<const T> : true_type {}; |
64 | 74 |
65 template <typename T> struct is_volatile : false_type {}; | 75 template <typename T> struct is_volatile : false_type {}; |
66 template <typename T> struct is_volatile<volatile T> : true_type {}; | 76 template <typename T> struct is_volatile<volatile T> : true_type {}; |
67 | 77 |
| 78 template <typename T> struct is_pointer_detector : false_type {}; |
| 79 template <typename T> struct is_pointer_detector<T*> : true_type {}; |
| 80 template <typename T> struct is_pointer : is_pointer_detector<remove_cv_t<T>> {}
; |
| 81 |
68 template <typename T> struct is_reference : false_type {}; | 82 template <typename T> struct is_reference : false_type {}; |
69 template <typename T> struct is_reference<T&> : true_type {}; | 83 template <typename T> struct is_reference<T&> : true_type {}; |
70 template <typename T> struct is_reference<T&&> : true_type {}; | 84 template <typename T> struct is_reference<T&&> : true_type {}; |
71 | 85 |
72 template <typename T> struct is_lvalue_reference : false_type {}; | 86 template <typename T> struct is_lvalue_reference : false_type {}; |
73 template <typename T> struct is_lvalue_reference<T&> : true_type {}; | 87 template <typename T> struct is_lvalue_reference<T&> : true_type {}; |
74 | 88 |
75 template <typename T> struct is_empty_detector { | 89 template <typename T> struct is_rvalue_reference : false_type {}; |
76 struct Derived : public remove_cv_t<T> { char unused; }; | 90 template <typename T> struct is_rvalue_reference<T&&> : true_type {}; |
77 static const bool value = sizeof(Derived) == sizeof(char); | 91 |
| 92 template <typename T> struct is_class_detector { |
| 93 using yes_type = uint8_t; |
| 94 using no_type = uint16_t; |
| 95 template <typename U> static yes_type clazz(int U::*); |
| 96 template <typename U> static no_type clazz(...); |
| 97 static const/*expr*/ bool value = sizeof(clazz<T>(0)) == sizeof(yes_type) /*
&& !is_union<T>::value*/; |
| 98 }; |
| 99 template <typename T> struct is_class : bool_constant<is_class_detector<T>::valu
e> {}; |
| 100 |
| 101 template <typename T, bool = is_class<T>::value> struct is_empty_detector { |
| 102 struct Derived : public T { char unused; }; |
| 103 static const/*expr*/ bool value = sizeof(Derived) == sizeof(char); |
| 104 }; |
| 105 template <typename T> struct is_empty_detector<T, false> { |
| 106 static const/*expr*/ bool value = false; |
78 }; | 107 }; |
79 template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::valu
e> {}; | 108 template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::valu
e> {}; |
80 | 109 |
| 110 template <typename T> struct is_array : false_type {}; |
| 111 template <typename T> struct is_array<T[]> : true_type {}; |
| 112 template <typename T, size_t N> struct is_array<T[N]> : true_type {}; |
| 113 |
| 114 // template<typename R, typename... Args> struct is_function< |
| 115 // R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : true
_type {}; |
| 116 // The cv and ref-qualified versions are strange types we're currently avoiding,
so not supported. |
| 117 // On all platforms, variadic functions only exist in the c calling convention. |
| 118 template <typename> struct is_function : false_type { }; |
| 119 #if !defined(SK_BUILD_FOR_WIN) |
| 120 template <typename R, typename... Args> struct is_function<R(Args...)> : true_ty
pe {}; |
| 121 #else |
| 122 #if defined(_M_IX86) |
| 123 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: true_type {}; |
| 124 template <typename R, typename... Args> struct is_function<R __stdcall (Args...)
> : true_type {}; |
| 125 template <typename R, typename... Args> struct is_function<R __fastcall (Args...
)> : true_type {}; |
| 126 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : true_type {}; |
| 127 #else |
| 128 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: true_type {}; |
| 129 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : true_type {}; |
| 130 #endif |
| 131 #endif |
| 132 template <typename R, typename... Args> struct is_function<R(Args..., ...)> : tr
ue_type {}; |
| 133 |
81 template <typename T> struct add_const { using type = const T; }; | 134 template <typename T> struct add_const { using type = const T; }; |
82 template <typename T> using add_const_t = typename add_const<T>::type; | 135 template <typename T> using add_const_t = typename add_const<T>::type; |
83 | 136 |
84 template <typename T> struct add_volatile { using type = volatile T; }; | 137 template <typename T> struct add_volatile { using type = volatile T; }; |
85 template <typename T> using add_volatile_t = typename add_volatile<T>::type; | 138 template <typename T> using add_volatile_t = typename add_volatile<T>::type; |
86 | 139 |
87 template <typename T> struct add_cv { using type = add_volatile_t<add_const_t<T>
>; }; | 140 template <typename T> struct add_cv { using type = add_volatile_t<add_const_t<T>
>; }; |
88 template <typename T> using add_cv_t = typename add_cv<T>::type; | 141 template <typename T> using add_cv_t = typename add_cv<T>::type; |
89 | 142 |
90 template <typename T> struct add_rvalue_reference { | 143 template <typename T> struct add_pointer { using type = remove_reference_t<T>*;
}; |
91 using type = conditional_t<is_void<T>::value || is_reference<T>::value, T, T
&&>; | 144 template <typename T> using add_pointer_t = typename add_pointer<T>::type; |
| 145 |
| 146 template <typename T, bool=is_void<T>::value> struct add_lvalue_reference_init {
using type = T; }; |
| 147 template <typename T> struct add_lvalue_reference_init<T, false> { using type =
T&; }; |
| 148 template <typename T> struct add_lvalue_reference : add_lvalue_reference_init<T>
{ }; |
| 149 template <typename T> using add_lvalue_reference_t = typename add_lvalue_referen
ce<T>::type; |
| 150 |
| 151 template <typename T, bool=is_void<T>::value> struct add_rvalue_reference_init {
using type = T; }; |
| 152 template <typename T> struct add_rvalue_reference_init<T, false> { using type =
T&&; }; |
| 153 template <typename T> struct add_rvalue_reference : add_rvalue_reference_init<T>
{}; |
| 154 template <typename T> using add_rvalue_reference_t = typename add_rvalue_referen
ce<T>::type; |
| 155 |
| 156 /* This is 'just' a forward declaration. */ |
| 157 template <typename T> add_rvalue_reference_t<T> declval() /*noexcept*/; |
| 158 |
| 159 template <typename S, typename D, bool=is_void<S>::value||is_function<D>::value|
|is_array<D>::value> |
| 160 struct is_convertible_detector { |
| 161 static const/*expr*/ bool value = is_void<D>::value; |
92 }; | 162 }; |
93 template <typename T> using add_rvalue_reference_t = typename add_rvalue_referen
ce<T>::type; | 163 template <typename S, typename D> struct is_convertible_detector<S, D, false> { |
| 164 using yes_type = uint8_t; |
| 165 using no_type = uint16_t; |
| 166 |
| 167 template <typename To> static void param_convertable_to(To); |
| 168 |
| 169 template <typename From, typename To> |
| 170 static decltype(param_convertable_to<To>(declval<From>()), yes_type()) conve
rtible(int); |
| 171 |
| 172 template <typename, typename> static no_type convertible(...); |
| 173 |
| 174 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes
_type); |
| 175 }; |
| 176 template<typename S, typename D> struct is_convertible |
| 177 : bool_constant<is_convertible_detector<S, D>::value> { }; |
| 178 |
| 179 template <typename T> struct decay { |
| 180 using U = remove_reference_t<T>; |
| 181 using type = conditional_t<is_array<U>::value, |
| 182 remove_extent_t<U>*, |
| 183 conditional_t<is_function<U>::value, add_pointer_t<U>, remove_cv_t<U>>>; |
| 184 }; |
| 185 template <typename T> using decay_t = typename decay<T>::type; |
94 | 186 |
95 } // namespace skstd | 187 } // namespace skstd |
96 | 188 |
97 // The sknonstd namespace contains things we would like to be proposed and feel
std-ish. | 189 // The sknonstd namespace contains things we would like to be proposed and feel
std-ish. |
98 namespace sknonstd { | 190 namespace sknonstd { |
99 | 191 |
100 // The name 'copy' here is fraught with peril. In this case it means 'append', n
ot 'overwrite'. | 192 // The name 'copy' here is fraught with peril. In this case it means 'append', n
ot 'overwrite'. |
101 // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add',
but already taken). | 193 // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add',
but already taken). |
102 // std::experimental::propagate_const already exists for other purposes in TSv2. | 194 // std::experimental::propagate_const already exists for other purposes in TSv2. |
103 // These also follow the <dest, source> pattern used by boost. | 195 // These also follow the <dest, source> pattern used by boost. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 #define SK_CREATE_TYPE_DETECTOR(type) \ | 245 #define SK_CREATE_TYPE_DETECTOR(type) \ |
154 template <typename T> \ | 246 template <typename T> \ |
155 class HasType_##type { \ | 247 class HasType_##type { \ |
156 template <typename U> static uint8_t func(typename U::type*); \ | 248 template <typename U> static uint8_t func(typename U::type*); \ |
157 template <typename U> static uint16_t func(...); \ | 249 template <typename U> static uint16_t func(...); \ |
158 public: \ | 250 public: \ |
159 static const bool value = sizeof(func<T>(NULL)) == sizeof(uint8_t); \ | 251 static const bool value = sizeof(func<T>(NULL)) == sizeof(uint8_t); \ |
160 } | 252 } |
161 | 253 |
162 #endif | 254 #endif |
OLD | NEW |