| 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 (like std::enable_if_t) which will |
| 9 * type transformations (std::conditional) which will become available with | 9 * become available with C++14 in the type_traits header (in the skstd |
| 10 * C++11 in the type_traits header. | 10 * namespace). This header also provides several Skia specific additions such |
| 11 * as SK_WHEN and the sknonstd namespace. |
| 11 */ | 12 */ |
| 12 | 13 |
| 13 #ifndef SkTLogic_DEFINED | 14 #ifndef SkTLogic_DEFINED |
| 14 #define SkTLogic_DEFINED | 15 #define SkTLogic_DEFINED |
| 15 | 16 |
| 16 #include "SkTypes.h" | 17 #include "SkTypes.h" |
| 17 | 18 |
| 18 #include <stddef.h> | 19 #include <stddef.h> |
| 19 #include <stdint.h> | 20 #include <stdint.h> |
| 21 #include <type_traits> |
| 22 #include <utility> |
| 20 | 23 |
| 21 namespace skstd { | 24 namespace skstd { |
| 22 | 25 |
| 23 using nullptr_t = decltype(nullptr); | 26 template <bool B> using bool_constant = std::integral_constant<bool, B>; |
| 24 | 27 |
| 25 template <typename T, T v> struct integral_constant { | 28 template <bool B, typename T, typename F> using conditional_t = typename std::co
nditional<B, T, F>::type; |
| 26 static const/*expr*/ T value = v; | 29 template <bool B, typename T = void> using enable_if_t = typename std::enable_if
<B, T>::type; |
| 27 using value_type = T; | |
| 28 using type = integral_constant<T, v>; | |
| 29 //constexpr operator value_type() const noexcept { return value; } | |
| 30 //constexpr value_type operator()() const noexcept { return value; } | |
| 31 }; | |
| 32 | 30 |
| 33 template <bool B> using bool_constant = integral_constant<bool, B>; | 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; |
| 33 template <typename T> using remove_cv_t = typename std::remove_cv<T>::type; |
| 34 template <typename T> using remove_reference_t = typename std::remove_reference<
T>::type; |
| 35 template <typename T> using remove_extent_t = typename std::remove_extent<T>::ty
pe; |
| 34 | 36 |
| 35 using true_type = bool_constant<true>; | 37 template <typename T, typename U> struct is_same : std::false_type {}; |
| 36 using false_type = bool_constant<false>; | 38 template <typename T> struct is_same<T, T> : std::true_type {}; |
| 37 | |
| 38 template <bool B, typename T, typename F> struct conditional { using type = T; }
; | |
| 39 template <typename T, typename F> struct conditional<false, T, F> { using type =
F; }; | |
| 40 template <bool B, typename T, typename F> using conditional_t = typename conditi
onal<B, T, F>::type; | |
| 41 | |
| 42 template <bool B, typename T = void> struct enable_if { using type = T; }; | |
| 43 template <typename T> struct enable_if<false, T> {}; | |
| 44 template <bool B, typename T = void> using enable_if_t = typename enable_if<B, T
>::type; | |
| 45 | |
| 46 template <typename T> struct remove_const { using type = T; }; | |
| 47 template <typename T> struct remove_const<const T> { using type = T; }; | |
| 48 template <typename T> using remove_const_t = typename remove_const<T>::type; | |
| 49 | |
| 50 template <typename T> struct remove_volatile { using type = T; }; | |
| 51 template <typename T> struct remove_volatile<volatile T> { using type = T; }; | |
| 52 template <typename T> using remove_volatile_t = typename remove_volatile<T>::typ
e; | |
| 53 | |
| 54 template <typename T> struct remove_cv { using type = remove_volatile_t<remove_c
onst_t<T>>; }; | |
| 55 template <typename T> using remove_cv_t = typename remove_cv<T>::type; | |
| 56 | |
| 57 template <typename T> struct remove_reference { using type = T; }; | |
| 58 template <typename T> struct remove_reference<T&> { using type = T; }; | |
| 59 template <typename T> struct remove_reference<T&&> { using type = T; }; | |
| 60 template <typename T> using remove_reference_t = typename remove_reference<T>::t
ype; | |
| 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 | |
| 67 template <typename T, typename U> struct is_same : false_type {}; | |
| 68 template <typename T> struct is_same<T, T> : true_type {}; | |
| 69 | 39 |
| 70 template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {}; | 40 template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {}; |
| 71 | 41 |
| 72 template <typename T> struct is_const : false_type {}; | 42 template <typename T> struct is_const : std::false_type {}; |
| 73 template <typename T> struct is_const<const T> : true_type {}; | 43 template <typename T> struct is_const<const T> : std::true_type {}; |
| 74 | 44 |
| 75 template <typename T> struct is_volatile : false_type {}; | 45 template <typename T> struct is_volatile : std::false_type {}; |
| 76 template <typename T> struct is_volatile<volatile T> : true_type {}; | 46 template <typename T> struct is_volatile<volatile T> : std::true_type {}; |
| 77 | 47 |
| 78 template <typename T> struct is_pointer_detector : false_type {}; | 48 template <typename T> struct is_pointer_detector : std::false_type {}; |
| 79 template <typename T> struct is_pointer_detector<T*> : true_type {}; | 49 template <typename T> struct is_pointer_detector<T*> : std::true_type {}; |
| 80 template <typename T> struct is_pointer : is_pointer_detector<remove_cv_t<T>> {}
; | 50 template <typename T> struct is_pointer : is_pointer_detector<remove_cv_t<T>> {}
; |
| 81 | 51 |
| 82 template <typename T> struct is_reference : false_type {}; | 52 template <typename T> struct is_reference : std::false_type {}; |
| 83 template <typename T> struct is_reference<T&> : true_type {}; | 53 template <typename T> struct is_reference<T&> : std::true_type {}; |
| 84 template <typename T> struct is_reference<T&&> : true_type {}; | 54 template <typename T> struct is_reference<T&&> : std::true_type {}; |
| 85 | 55 |
| 86 template <typename T> struct is_lvalue_reference : false_type {}; | 56 template <typename T> struct is_lvalue_reference : std::false_type {}; |
| 87 template <typename T> struct is_lvalue_reference<T&> : true_type {}; | 57 template <typename T> struct is_lvalue_reference<T&> : std::true_type {}; |
| 88 | 58 |
| 89 template <typename T> struct is_rvalue_reference : false_type {}; | 59 template <typename T> struct is_rvalue_reference : std::false_type {}; |
| 90 template <typename T> struct is_rvalue_reference<T&&> : true_type {}; | 60 template <typename T> struct is_rvalue_reference<T&&> : std::true_type {}; |
| 91 | 61 |
| 92 template <typename T> struct is_class_detector { | 62 template <typename T> struct is_class_detector { |
| 93 using yes_type = uint8_t; | 63 using yes_type = uint8_t; |
| 94 using no_type = uint16_t; | 64 using no_type = uint16_t; |
| 95 template <typename U> static yes_type clazz(int U::*); | 65 template <typename U> static yes_type clazz(int U::*); |
| 96 template <typename U> static no_type clazz(...); | 66 template <typename U> static no_type clazz(...); |
| 97 static const/*expr*/ bool value = sizeof(clazz<T>(0)) == sizeof(yes_type) /*
&& !is_union<T>::value*/; | 67 static const/*expr*/ bool value = sizeof(clazz<T>(0)) == sizeof(yes_type) /*
&& !is_union<T>::value*/; |
| 98 }; | 68 }; |
| 99 template <typename T> struct is_class : bool_constant<is_class_detector<T>::valu
e> {}; | 69 template <typename T> struct is_class : bool_constant<is_class_detector<T>::valu
e> {}; |
| 100 | 70 |
| 101 template <typename T, bool = is_class<T>::value> struct is_empty_detector { | 71 template <typename T, bool = is_class<T>::value> struct is_empty_detector { |
| 102 struct Derived : public T { char unused; }; | 72 struct Derived : public T { char unused; }; |
| 103 static const/*expr*/ bool value = sizeof(Derived) == sizeof(char); | 73 static const/*expr*/ bool value = sizeof(Derived) == sizeof(char); |
| 104 }; | 74 }; |
| 105 template <typename T> struct is_empty_detector<T, false> { | 75 template <typename T> struct is_empty_detector<T, false> { |
| 106 static const/*expr*/ bool value = false; | 76 static const/*expr*/ bool value = false; |
| 107 }; | 77 }; |
| 108 template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::valu
e> {}; | 78 template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::valu
e> {}; |
| 109 | 79 |
| 110 template <typename T> struct is_array : false_type {}; | 80 template <typename T> struct is_array : std::false_type {}; |
| 111 template <typename T> struct is_array<T[]> : true_type {}; | 81 template <typename T> struct is_array<T[]> : std::true_type {}; |
| 112 template <typename T, size_t N> struct is_array<T[N]> : true_type {}; | 82 template <typename T, size_t N> struct is_array<T[N]> : std::true_type {}; |
| 113 | 83 |
| 114 // template<typename R, typename... Args> struct is_function< | 84 // template<typename R, typename... Args> struct is_function< |
| 115 // R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : true
_type {}; | 85 // R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : std:
:true_type {}; |
| 116 // The cv and ref-qualified versions are strange types we're currently avoiding,
so not supported. | 86 // 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. | 87 // On all platforms, variadic functions only exist in the c calling convention. |
| 118 template <typename> struct is_function : false_type { }; | 88 template <typename> struct is_function : std::false_type {}; |
| 119 #if !defined(SK_BUILD_FOR_WIN) | 89 #if !defined(SK_BUILD_FOR_WIN) |
| 120 template <typename R, typename... Args> struct is_function<R(Args...)> : true_ty
pe {}; | 90 template <typename R, typename... Args> struct is_function<R(Args...)> : std::tr
ue_type {}; |
| 121 #else | 91 #else |
| 122 #if defined(_M_IX86) | 92 #if defined(_M_IX86) |
| 123 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: true_type {}; | 93 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: std::true_type {}; |
| 124 template <typename R, typename... Args> struct is_function<R __stdcall (Args...)
> : true_type {}; | 94 template <typename R, typename... Args> struct is_function<R __stdcall (Args...)
> : std::true_type {}; |
| 125 template <typename R, typename... Args> struct is_function<R __fastcall (Args...
)> : true_type {}; | 95 template <typename R, typename... Args> struct is_function<R __fastcall (Args...
)> : std::true_type {}; |
| 126 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 | 96 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
| 127 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : true_type {}; | 97 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : std::true_type {}; |
| 128 #endif | 98 #endif |
| 129 #else | 99 #else |
| 130 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: true_type {}; | 100 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
: std::true_type {}; |
| 131 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : true_type {}; | 101 template <typename R, typename... Args> struct is_function<R __vectorcall (Args.
..)> : std::true_type {}; |
| 132 #endif | 102 #endif |
| 133 #endif | 103 #endif |
| 134 template <typename R, typename... Args> struct is_function<R(Args..., ...)> : tr
ue_type {}; | 104 template <typename R, typename... Args> struct is_function<R(Args..., ...)> : st
d::true_type {}; |
| 135 | 105 |
| 136 template <typename T> struct add_const { using type = const T; }; | 106 template <typename T> using add_const_t = typename std::add_const<T>::type; |
| 137 template <typename T> using add_const_t = typename add_const<T>::type; | 107 template <typename T> using add_volatile_t = typename std::add_volatile<T>::type
; |
| 138 | 108 template <typename T> using add_cv_t = typename std::add_cv<T>::type; |
| 139 template <typename T> struct add_volatile { using type = volatile T; }; | 109 template <typename T> using add_pointer_t = typename std::add_pointer<T>::type; |
| 140 template <typename T> using add_volatile_t = typename add_volatile<T>::type; | |
| 141 | |
| 142 template <typename T> struct add_cv { using type = add_volatile_t<add_const_t<T>
>; }; | |
| 143 template <typename T> using add_cv_t = typename add_cv<T>::type; | |
| 144 | |
| 145 template <typename T> struct add_pointer { using type = remove_reference_t<T>*;
}; | |
| 146 template <typename T> using add_pointer_t = typename add_pointer<T>::type; | |
| 147 | 110 |
| 148 template <typename T, bool=is_void<T>::value> struct add_lvalue_reference_init {
using type = T; }; | 111 template <typename T, bool=is_void<T>::value> struct add_lvalue_reference_init {
using type = T; }; |
| 149 template <typename T> struct add_lvalue_reference_init<T, false> { using type =
T&; }; | 112 template <typename T> struct add_lvalue_reference_init<T, false> { using type =
T&; }; |
| 150 template <typename T> struct add_lvalue_reference : add_lvalue_reference_init<T>
{ }; | 113 template <typename T> struct add_lvalue_reference : add_lvalue_reference_init<T>
{}; |
| 151 template <typename T> using add_lvalue_reference_t = typename add_lvalue_referen
ce<T>::type; | 114 template <typename T> using add_lvalue_reference_t = typename add_lvalue_referen
ce<T>::type; |
| 152 | 115 |
| 153 template <typename T, bool=is_void<T>::value> struct add_rvalue_reference_init {
using type = T; }; | 116 template <typename T, bool=is_void<T>::value> struct add_rvalue_reference_init {
using type = T; }; |
| 154 template <typename T> struct add_rvalue_reference_init<T, false> { using type =
T&&; }; | 117 template <typename T> struct add_rvalue_reference_init<T, false> { using type =
T&&; }; |
| 155 template <typename T> struct add_rvalue_reference : add_rvalue_reference_init<T>
{}; | 118 template <typename T> struct add_rvalue_reference : add_rvalue_reference_init<T>
{}; |
| 156 template <typename T> using add_rvalue_reference_t = typename add_rvalue_referen
ce<T>::type; | 119 template <typename T> using add_rvalue_reference_t = typename add_rvalue_referen
ce<T>::type; |
| 157 | 120 |
| 158 /* This is 'just' a forward declaration. */ | |
| 159 template <typename T> add_rvalue_reference_t<T> declval() /*noexcept*/; | |
| 160 | |
| 161 template <typename S, typename D, bool=is_void<S>::value||is_function<D>::value|
|is_array<D>::value> | 121 template <typename S, typename D, bool=is_void<S>::value||is_function<D>::value|
|is_array<D>::value> |
| 162 struct is_convertible_detector { | 122 struct is_convertible_detector { |
| 163 static const/*expr*/ bool value = is_void<D>::value; | 123 static const/*expr*/ bool value = is_void<D>::value; |
| 164 }; | 124 }; |
| 165 template <typename S, typename D> struct is_convertible_detector<S, D, false> { | 125 template <typename S, typename D> struct is_convertible_detector<S, D, false> { |
| 166 using yes_type = uint8_t; | 126 using yes_type = uint8_t; |
| 167 using no_type = uint16_t; | 127 using no_type = uint16_t; |
| 168 | 128 |
| 169 template <typename To> static void param_convertable_to(To); | 129 template <typename To> static void param_convertable_to(To); |
| 170 | 130 |
| 171 template <typename From, typename To> | 131 template <typename From, typename To> |
| 172 static decltype(param_convertable_to<To>(declval<From>()), yes_type()) conve
rtible(int); | 132 static decltype(param_convertable_to<To>(std::declval<From>()), yes_type())
convertible(int); |
| 173 | 133 |
| 174 template <typename, typename> static no_type convertible(...); | 134 template <typename, typename> static no_type convertible(...); |
| 175 | 135 |
| 176 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes
_type); | 136 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes
_type); |
| 177 }; | 137 }; |
| 178 template<typename S, typename D> struct is_convertible | 138 template<typename S, typename D> struct is_convertible |
| 179 : bool_constant<is_convertible_detector<S, D>::value> { }; | 139 : bool_constant<is_convertible_detector<S, D>::value> {}; |
| 180 | 140 |
| 181 template <typename T> struct decay { | 141 template <typename T> struct decay { |
| 182 using U = remove_reference_t<T>; | 142 using U = remove_reference_t<T>; |
| 183 using type = conditional_t<is_array<U>::value, | 143 using type = conditional_t<is_array<U>::value, |
| 184 remove_extent_t<U>*, | 144 remove_extent_t<U>*, |
| 185 conditional_t<is_function<U>::value, add_pointer_t<U>, remove_cv_t<U>>>; | 145 conditional_t<is_function<U>::value, add_pointer_t<U>, remove_cv_t<U>>>; |
| 186 }; | 146 }; |
| 187 template <typename T> using decay_t = typename decay<T>::type; | 147 template <typename T> using decay_t = typename decay<T>::type; |
| 188 | 148 |
| 189 } // namespace skstd | 149 } // namespace skstd |
| (...skipping 29 matching lines...) Expand all Loading... |
| 219 template <typename D, typename S> using same_volatile_t = typename same_volatile
<D, S>::type; | 179 template <typename D, typename S> using same_volatile_t = typename same_volatile
<D, S>::type; |
| 220 template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>,
S>; | 180 template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>,
S>; |
| 221 template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type
; | 181 template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type
; |
| 222 | 182 |
| 223 } // namespace sknonstd | 183 } // namespace sknonstd |
| 224 | 184 |
| 225 // Just a pithier wrapper for enable_if_t. | 185 // Just a pithier wrapper for enable_if_t. |
| 226 #define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T> | 186 #define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T> |
| 227 | 187 |
| 228 #endif | 188 #endif |
| OLD | NEW |