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 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : true_type {}; | |
123 template <typename R, typename... Args> struct is_function<R __stdcall (Args...) > : true_type {}; | |
bungeman-skia
2015/09/03 23:15:17
As it turns out the compiler on Windows just ignor
| |
124 template <typename R, typename... Args> struct is_function<R __fastcall (Args... )> : true_type {}; | |
125 template <typename R, typename... Args> struct is_function<R __vectorcall (Args. ..)> : true_type {}; | |
126 #endif | |
127 template <typename R, typename... Args> struct is_function<R(Args..., ...)> : tr ue_type {}; | |
128 | |
81 template <typename T> struct add_const { using type = const T; }; | 129 template <typename T> struct add_const { using type = const T; }; |
82 template <typename T> using add_const_t = typename add_const<T>::type; | 130 template <typename T> using add_const_t = typename add_const<T>::type; |
83 | 131 |
84 template <typename T> struct add_volatile { using type = volatile T; }; | 132 template <typename T> struct add_volatile { using type = volatile T; }; |
85 template <typename T> using add_volatile_t = typename add_volatile<T>::type; | 133 template <typename T> using add_volatile_t = typename add_volatile<T>::type; |
86 | 134 |
87 template <typename T> struct add_cv { using type = add_volatile_t<add_const_t<T> >; }; | 135 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; | 136 template <typename T> using add_cv_t = typename add_cv<T>::type; |
89 | 137 |
138 template <typename T> struct add_pointer { using type = remove_reference_t<T>*; }; | |
139 template <typename T> using add_pointer_t = typename add_pointer<T>::type; | |
140 | |
141 template <typename T> struct add_lvalue_reference { | |
142 using type = conditional_t<is_void<T>::value, T, T&>; | |
bungeman-skia
2015/09/03 23:15:17
This (and the existing add_rvalue_reference below)
| |
143 }; | |
144 template <typename T> using add_lvalue_reference_t = typename add_lvalue_referen ce<T>::type; | |
145 | |
90 template <typename T> struct add_rvalue_reference { | 146 template <typename T> struct add_rvalue_reference { |
91 using type = conditional_t<is_void<T>::value || is_reference<T>::value, T, T &&>; | 147 using type = conditional_t<is_void<T>::value || is_reference<T>::value, T, T &&>; |
bungeman-skia
2015/09/03 23:15:17
I honestly don't remember why I added this 'is_ref
| |
92 }; | 148 }; |
93 template <typename T> using add_rvalue_reference_t = typename add_rvalue_referen ce<T>::type; | 149 template <typename T> using add_rvalue_reference_t = typename add_rvalue_referen ce<T>::type; |
94 | 150 |
151 /* This is 'just' a forward declaration. */ | |
152 template <typename T> add_rvalue_reference_t<T> declval() /*noexcept*/; | |
153 | |
154 template <typename S, typename D, bool=is_void<S>::value||is_function<D>::value| |is_array<D>::value> | |
155 struct is_convertible_detector { | |
156 static const/*expr*/ bool value = is_void<D>::value; | |
157 }; | |
158 template <typename S, typename D> struct is_convertible_detector<S, D, false> { | |
159 using yes_type = uint8_t; | |
160 using no_type = uint16_t; | |
161 | |
162 template <typename To> static void param_convertable_to(To); | |
163 | |
164 template <typename From, typename To> | |
165 static decltype(param_convertable_to<To>(declval<From>()), yes_type()) conve rtible(int); | |
166 | |
167 template <typename, typename> static no_type convertible(...); | |
168 | |
169 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes _type); | |
170 }; | |
171 template<typename S, typename D> struct is_convertible | |
172 : bool_constant<is_convertible_detector<S, D>::value> { }; | |
173 | |
174 template <typename T> struct decay { | |
175 using U = remove_reference_t<T>; | |
176 using type = conditional_t<is_array<U>::value, | |
177 remove_extent_t<U>*, | |
178 conditional_t<is_function<U>::value, add_pointer_t<U>, remove_cv_t<U>>>; | |
179 }; | |
180 template <typename T> using decay_t = typename decay<T>::type; | |
181 | |
95 } // namespace skstd | 182 } // namespace skstd |
96 | 183 |
97 // The sknonstd namespace contains things we would like to be proposed and feel std-ish. | 184 // The sknonstd namespace contains things we would like to be proposed and feel std-ish. |
98 namespace sknonstd { | 185 namespace sknonstd { |
99 | 186 |
100 // The name 'copy' here is fraught with peril. In this case it means 'append', n ot 'overwrite'. | 187 // 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). | 188 // 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. | 189 // std::experimental::propagate_const already exists for other purposes in TSv2. |
103 // These also follow the <dest, source> pattern used by boost. | 190 // These also follow the <dest, source> pattern used by boost. |
104 template <typename D, typename S> struct copy_const { | 191 template <typename D, typename S> struct copy_const { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 #define SK_CREATE_TYPE_DETECTOR(type) \ | 240 #define SK_CREATE_TYPE_DETECTOR(type) \ |
154 template <typename T> \ | 241 template <typename T> \ |
155 class HasType_##type { \ | 242 class HasType_##type { \ |
156 template <typename U> static uint8_t func(typename U::type*); \ | 243 template <typename U> static uint8_t func(typename U::type*); \ |
157 template <typename U> static uint16_t func(...); \ | 244 template <typename U> static uint16_t func(...); \ |
158 public: \ | 245 public: \ |
159 static const bool value = sizeof(func<T>(NULL)) == sizeof(uint8_t); \ | 246 static const bool value = sizeof(func<T>(NULL)) == sizeof(uint8_t); \ |
160 } | 247 } |
161 | 248 |
162 #endif | 249 #endif |
OLD | NEW |