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 (like std::enable_if_t) which will | 8 * This header provides some of the helpers (like std::enable_if_t) which will |
9 * become available with C++14 in the type_traits header (in the skstd | 9 * become available with C++14 in the type_traits header (in the skstd |
10 * namespace). This header also provides several Skia specific additions such | 10 * namespace). This header also provides several Skia specific additions such |
(...skipping 17 matching lines...) Expand all Loading... |
28 template <bool B, typename T, typename F> using conditional_t = typename std::co
nditional<B, T, F>::type; | 28 template <bool B, typename T, typename F> using conditional_t = typename std::co
nditional<B, T, F>::type; |
29 template <bool B, typename T = void> using enable_if_t = typename std::enable_if
<B, T>::type; | 29 template <bool B, typename T = void> using enable_if_t = typename std::enable_if
<B, T>::type; |
30 | 30 |
31 template <typename T> using remove_const_t = typename std::remove_const<T>::type
; | 31 template <typename T> using remove_const_t = typename std::remove_const<T>::type
; |
32 template <typename T> using remove_volatile_t = typename std::remove_volatile<T>
::type; | 32 template <typename T> using remove_volatile_t = typename std::remove_volatile<T>
::type; |
33 template <typename T> using remove_cv_t = typename std::remove_cv<T>::type; | 33 template <typename T> using remove_cv_t = typename std::remove_cv<T>::type; |
34 template <typename T> using remove_pointer_t = typename std::remove_pointer<T>::
type; | 34 template <typename T> using remove_pointer_t = typename std::remove_pointer<T>::
type; |
35 template <typename T> using remove_reference_t = typename std::remove_reference<
T>::type; | 35 template <typename T> using remove_reference_t = typename std::remove_reference<
T>::type; |
36 template <typename T> using remove_extent_t = typename std::remove_extent<T>::ty
pe; | 36 template <typename T> using remove_extent_t = typename std::remove_extent<T>::ty
pe; |
37 | 37 |
38 template <typename T, typename U> struct is_same : std::false_type {}; | |
39 template <typename T> struct is_same<T, T> : std::true_type {}; | |
40 | |
41 template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {}; | |
42 | |
43 template <typename T> struct is_const : std::false_type {}; | |
44 template <typename T> struct is_const<const T> : std::true_type {}; | |
45 | |
46 template <typename T> struct is_volatile : std::false_type {}; | |
47 template <typename T> struct is_volatile<volatile T> : std::true_type {}; | |
48 | |
49 template <typename T> struct is_pointer_detector : std::false_type {}; | |
50 template <typename T> struct is_pointer_detector<T*> : std::true_type {}; | |
51 template <typename T> struct is_pointer : is_pointer_detector<remove_cv_t<T>> {}
; | |
52 | |
53 template <typename T> struct is_reference : std::false_type {}; | |
54 template <typename T> struct is_reference<T&> : std::true_type {}; | |
55 template <typename T> struct is_reference<T&&> : std::true_type {}; | |
56 | |
57 template <typename T> struct is_lvalue_reference : std::false_type {}; | |
58 template <typename T> struct is_lvalue_reference<T&> : std::true_type {}; | |
59 | |
60 template <typename T> struct is_rvalue_reference : std::false_type {}; | |
61 template <typename T> struct is_rvalue_reference<T&&> : std::true_type {}; | |
62 | |
63 template <typename T> struct is_class_detector { | 38 template <typename T> struct is_class_detector { |
64 using yes_type = uint8_t; | 39 using yes_type = uint8_t; |
65 using no_type = uint16_t; | 40 using no_type = uint16_t; |
66 template <typename U> static yes_type clazz(int U::*); | 41 template <typename U> static yes_type clazz(int U::*); |
67 template <typename U> static no_type clazz(...); | 42 template <typename U> static no_type clazz(...); |
68 static const/*expr*/ bool value = sizeof(clazz<T>(0)) == sizeof(yes_type) /*
&& !is_union<T>::value*/; | 43 static const/*expr*/ bool value = sizeof(clazz<T>(0)) == sizeof(yes_type) && |
| 44 !std::is_union<T>::value; |
69 }; | 45 }; |
70 template <typename T> struct is_class : bool_constant<is_class_detector<T>::valu
e> {}; | 46 template <typename T> struct is_class : bool_constant<is_class_detector<T>::valu
e> {}; |
71 | 47 |
72 template <typename T, bool = is_class<T>::value> struct is_empty_detector { | 48 template <typename T, bool = is_class<T>::value> struct is_empty_detector { |
73 struct Derived : public T { char unused; }; | 49 struct Derived : public T { char unused; }; |
74 static const/*expr*/ bool value = sizeof(Derived) == sizeof(char); | 50 static const/*expr*/ bool value = sizeof(Derived) == sizeof(char); |
75 }; | 51 }; |
76 template <typename T> struct is_empty_detector<T, false> { | 52 template <typename T> struct is_empty_detector<T, false> { |
77 static const/*expr*/ bool value = false; | 53 static const/*expr*/ bool value = false; |
78 }; | 54 }; |
79 template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::valu
e> {}; | 55 template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::valu
e> {}; |
80 | 56 |
81 template <typename T> struct is_array : std::false_type {}; | |
82 template <typename T> struct is_array<T[]> : std::true_type {}; | |
83 template <typename T, size_t N> struct is_array<T[N]> : std::true_type {}; | |
84 | |
85 // template<typename R, typename... Args> struct is_function< | 57 // template<typename R, typename... Args> struct is_function< |
86 // R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : std:
:true_type {}; | 58 // R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : std:
:true_type {}; |
87 // The cv and ref-qualified versions are strange types we're currently avoiding,
so not supported. | 59 // The cv and ref-qualified versions are strange types we're currently avoiding,
so not supported. |
88 // On all platforms, variadic functions only exist in the c calling convention. | 60 // On all platforms, variadic functions only exist in the c calling convention. |
89 template <typename> struct is_function : std::false_type {}; | 61 template <typename> struct is_function : std::false_type {}; |
90 #if !defined(SK_BUILD_FOR_WIN) | 62 #if !defined(SK_BUILD_FOR_WIN) |
91 template <typename R, typename... Args> struct is_function<R(Args...)> : std::tr
ue_type {}; | 63 template <typename R, typename... Args> struct is_function<R(Args...)> : std::tr
ue_type {}; |
92 #else | 64 #else |
93 #if defined(_M_IX86) | 65 #if defined(_M_IX86) |
94 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: std::true_type {}; | 66 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: std::true_type {}; |
95 template <typename R, typename... Args> struct is_function<R __stdcall (Args...)
> : std::true_type {}; | 67 template <typename R, typename... Args> struct is_function<R __stdcall (Args...)
> : std::true_type {}; |
96 template <typename R, typename... Args> struct is_function<R __fastcall (Args...
)> : std::true_type {}; | 68 template <typename R, typename... Args> struct is_function<R __fastcall (Args...
)> : std::true_type {}; |
97 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 | 69 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
98 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : std::true_type {}; | 70 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : std::true_type {}; |
99 #endif | 71 #endif |
100 #else | 72 #else |
101 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: std::true_type {}; | 73 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: std::true_type {}; |
102 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : std::true_type {}; | 74 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : std::true_type {}; |
103 #endif | 75 #endif |
104 #endif | 76 #endif |
105 template <typename R, typename... Args> struct is_function<R(Args..., ...)> : st
d::true_type {}; | 77 template <typename R, typename... Args> struct is_function<R(Args..., ...)> : st
d::true_type {}; |
106 | 78 |
107 template <typename T> using add_const_t = typename std::add_const<T>::type; | 79 template <typename T> using add_const_t = typename std::add_const<T>::type; |
108 template <typename T> using add_volatile_t = typename std::add_volatile<T>::type
; | 80 template <typename T> using add_volatile_t = typename std::add_volatile<T>::type
; |
109 template <typename T> using add_cv_t = typename std::add_cv<T>::type; | 81 template <typename T> using add_cv_t = typename std::add_cv<T>::type; |
110 template <typename T> using add_pointer_t = typename std::add_pointer<T>::type; | 82 template <typename T> using add_pointer_t = typename std::add_pointer<T>::type; |
111 | 83 |
112 template <typename T, bool=is_void<T>::value> struct add_lvalue_reference_init {
using type = T; }; | 84 template <typename T, bool=std::is_void<T>::value> struct add_lvalue_reference_i
nit { using type = T; }; |
113 template <typename T> struct add_lvalue_reference_init<T, false> { using type =
T&; }; | 85 template <typename T> struct add_lvalue_reference_init<T, false> { using type =
T&; }; |
114 template <typename T> struct add_lvalue_reference : add_lvalue_reference_init<T>
{}; | 86 template <typename T> struct add_lvalue_reference : add_lvalue_reference_init<T>
{}; |
115 template <typename T> using add_lvalue_reference_t = typename add_lvalue_referen
ce<T>::type; | 87 template <typename T> using add_lvalue_reference_t = typename add_lvalue_referen
ce<T>::type; |
116 | 88 |
117 template <typename T, bool=is_void<T>::value> struct add_rvalue_reference_init {
using type = T; }; | 89 template <typename T, bool=std::is_void<T>::value> struct add_rvalue_reference_i
nit { using type = T; }; |
118 template <typename T> struct add_rvalue_reference_init<T, false> { using type =
T&&; }; | 90 template <typename T> struct add_rvalue_reference_init<T, false> { using type =
T&&; }; |
119 template <typename T> struct add_rvalue_reference : add_rvalue_reference_init<T>
{}; | 91 template <typename T> struct add_rvalue_reference : add_rvalue_reference_init<T>
{}; |
120 template <typename T> using add_rvalue_reference_t = typename add_rvalue_referen
ce<T>::type; | 92 template <typename T> using add_rvalue_reference_t = typename add_rvalue_referen
ce<T>::type; |
121 | 93 |
122 template <typename S, typename D, bool=is_void<S>::value||is_function<D>::value|
|is_array<D>::value> | 94 template <typename S, typename D, |
| 95 bool=std::is_void<S>::value || is_function<D>::value || std::is_array<
D>::value> |
123 struct is_convertible_detector { | 96 struct is_convertible_detector { |
124 static const/*expr*/ bool value = is_void<D>::value; | 97 static const/*expr*/ bool value = std::is_void<D>::value; |
125 }; | 98 }; |
126 template <typename S, typename D> struct is_convertible_detector<S, D, false> { | 99 template <typename S, typename D> struct is_convertible_detector<S, D, false> { |
127 using yes_type = uint8_t; | 100 using yes_type = uint8_t; |
128 using no_type = uint16_t; | 101 using no_type = uint16_t; |
129 | 102 |
130 template <typename To> static void param_convertable_to(To); | 103 template <typename To> static void param_convertable_to(To); |
131 | 104 |
132 template <typename From, typename To> | 105 template <typename From, typename To> |
133 static decltype(param_convertable_to<To>(std::declval<From>()), yes_type())
convertible(int); | 106 static decltype(param_convertable_to<To>(std::declval<From>()), yes_type())
convertible(int); |
134 | 107 |
135 template <typename, typename> static no_type convertible(...); | 108 template <typename, typename> static no_type convertible(...); |
136 | 109 |
137 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes
_type); | 110 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes
_type); |
138 }; | 111 }; |
139 template<typename S, typename D> struct is_convertible | 112 template<typename S, typename D> struct is_convertible |
140 : bool_constant<is_convertible_detector<S, D>::value> {}; | 113 : bool_constant<is_convertible_detector<S, D>::value> {}; |
141 | 114 |
142 template <typename T> struct decay { | 115 template <typename T> struct decay { |
143 using U = remove_reference_t<T>; | 116 using U = remove_reference_t<T>; |
144 using type = conditional_t<is_array<U>::value, | 117 using type = conditional_t<std::is_array<U>::value, |
145 remove_extent_t<U>*, | 118 remove_extent_t<U>*, |
146 conditional_t<is_function<U>::value, add_pointer_t<U>, remove_cv_t<U>>>; | 119 conditional_t<is_function<U>::value, |
| 120 add_pointer_t<U>, |
| 121 remove_cv_t<U>>>; |
147 }; | 122 }; |
148 template <typename T> using decay_t = typename decay<T>::type; | 123 template <typename T> using decay_t = typename decay<T>::type; |
149 | 124 |
150 } // namespace skstd | 125 } // namespace skstd |
151 | 126 |
152 // The sknonstd namespace contains things we would like to be proposed and feel
std-ish. | 127 // The sknonstd namespace contains things we would like to be proposed and feel
std-ish. |
153 namespace sknonstd { | 128 namespace sknonstd { |
154 | 129 |
155 // The name 'copy' here is fraught with peril. In this case it means 'append', n
ot 'overwrite'. | 130 // The name 'copy' here is fraught with peril. In this case it means 'append', n
ot 'overwrite'. |
156 // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add',
but already taken). | 131 // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add',
but already taken). |
157 // std::experimental::propagate_const already exists for other purposes in TSv2. | 132 // std::experimental::propagate_const already exists for other purposes in TSv2. |
158 // These also follow the <dest, source> pattern used by boost. | 133 // These also follow the <dest, source> pattern used by boost. |
159 template <typename D, typename S> struct copy_const { | 134 template <typename D, typename S> struct copy_const { |
160 using type = skstd::conditional_t<skstd::is_const<S>::value, skstd::add_cons
t_t<D>, D>; | 135 using type = skstd::conditional_t<std::is_const<S>::value, skstd::add_const_
t<D>, D>; |
161 }; | 136 }; |
162 template <typename D, typename S> using copy_const_t = typename copy_const<D, S>
::type; | 137 template <typename D, typename S> using copy_const_t = typename copy_const<D, S>
::type; |
163 | 138 |
164 template <typename D, typename S> struct copy_volatile { | 139 template <typename D, typename S> struct copy_volatile { |
165 using type = skstd::conditional_t<skstd::is_volatile<S>::value, skstd::add_v
olatile_t<D>, D>; | 140 using type = skstd::conditional_t<std::is_volatile<S>::value, skstd::add_vol
atile_t<D>, D>; |
166 }; | 141 }; |
167 template <typename D, typename S> using copy_volatile_t = typename copy_volatile
<D, S>::type; | 142 template <typename D, typename S> using copy_volatile_t = typename copy_volatile
<D, S>::type; |
168 | 143 |
169 template <typename D, typename S> struct copy_cv { | 144 template <typename D, typename S> struct copy_cv { |
170 using type = copy_volatile_t<copy_const_t<D, S>, S>; | 145 using type = copy_volatile_t<copy_const_t<D, S>, S>; |
171 }; | 146 }; |
172 template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type
; | 147 template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type
; |
173 | 148 |
174 // The name 'same' here means 'overwrite'. | 149 // The name 'same' here means 'overwrite'. |
175 // Alternate proposed names are 'replace', 'transfer', or 'qualify_from'. | 150 // Alternate proposed names are 'replace', 'transfer', or 'qualify_from'. |
176 // same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S> | 151 // same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S> |
177 template <typename D, typename S> using same_const = copy_const<skstd::remove_co
nst_t<D>, S>; | 152 template <typename D, typename S> using same_const = copy_const<skstd::remove_co
nst_t<D>, S>; |
178 template <typename D, typename S> using same_const_t = typename same_const<D, S>
::type; | 153 template <typename D, typename S> using same_const_t = typename same_const<D, S>
::type; |
179 template <typename D, typename S> using same_volatile =copy_volatile<skstd::remo
ve_volatile_t<D>,S>; | 154 template <typename D, typename S> using same_volatile =copy_volatile<skstd::remo
ve_volatile_t<D>,S>; |
180 template <typename D, typename S> using same_volatile_t = typename same_volatile
<D, S>::type; | 155 template <typename D, typename S> using same_volatile_t = typename same_volatile
<D, S>::type; |
181 template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>,
S>; | 156 template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>,
S>; |
182 template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type
; | 157 template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type
; |
183 | 158 |
184 } // namespace sknonstd | 159 } // namespace sknonstd |
185 | 160 |
186 // Just a pithier wrapper for enable_if_t. | 161 // Just a pithier wrapper for enable_if_t. |
187 #define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T> | 162 #define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T> |
188 | 163 |
189 #endif | 164 #endif |
OLD | NEW |