bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | * |
| 7 | * |
| 8 | * This header provides some of the helpers (std::integral_constant) and |
| 9 | * type transformations (std::conditional) which will become available with |
| 10 | * C++11 in the type_traits header. |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef SkTLogic_DEFINED |
| 14 | #define SkTLogic_DEFINED |
| 15 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 16 | #include "SkTypes.h" |
| 17 | |
| 18 | #include <stddef.h> |
bungeman | 2bd0285 | 2015-08-05 12:09:57 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 21 | namespace skstd { |
| 22 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 23 | using nullptr_t = decltype(nullptr); |
| 24 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 25 | template <typename T, T v> struct integral_constant { |
| 26 | static const/*expr*/ T value = v; |
| 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; } |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 33 | template <bool B> using bool_constant = integral_constant<bool, B>; |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 34 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 35 | using true_type = bool_constant<true>; |
| 36 | using false_type = bool_constant<false>; |
| 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 conditional<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>::type; |
| 53 | |
| 54 | template <typename T> struct remove_cv { using type = remove_volatile_t<remove_const_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>::type; |
| 61 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 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 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 67 | template <typename T, typename U> struct is_same : false_type {}; |
| 68 | template <typename T> struct is_same<T, T> : true_type {}; |
| 69 | |
| 70 | template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {}; |
| 71 | |
| 72 | template <typename T> struct is_const : false_type {}; |
| 73 | template <typename T> struct is_const<const T> : true_type {}; |
| 74 | |
| 75 | template <typename T> struct is_volatile : false_type {}; |
| 76 | template <typename T> struct is_volatile<volatile T> : true_type {}; |
| 77 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 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 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 82 | template <typename T> struct is_reference : false_type {}; |
| 83 | template <typename T> struct is_reference<T&> : true_type {}; |
| 84 | template <typename T> struct is_reference<T&&> : true_type {}; |
| 85 | |
| 86 | template <typename T> struct is_lvalue_reference : false_type {}; |
| 87 | template <typename T> struct is_lvalue_reference<T&> : true_type {}; |
| 88 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 89 | template <typename T> struct is_rvalue_reference : false_type {}; |
| 90 | template <typename T> struct is_rvalue_reference<T&&> : true_type {}; |
| 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>::value> {}; |
| 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; |
commit-bot@chromium.org | 08bf86c | 2014-05-07 18:01:57 +0000 | [diff] [blame] | 107 | }; |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 108 | template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::value> {}; |
commit-bot@chromium.org | 08bf86c | 2014-05-07 18:01:57 +0000 | [diff] [blame] | 109 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 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_type {}; |
| 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..., ...)> : true_type {}; |
| 133 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 134 | template <typename T> struct add_const { using type = const T; }; |
| 135 | template <typename T> using add_const_t = typename add_const<T>::type; |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 136 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 137 | template <typename T> struct add_volatile { using type = volatile T; }; |
| 138 | template <typename T> using add_volatile_t = typename add_volatile<T>::type; |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 139 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 140 | template <typename T> struct add_cv { using type = add_volatile_t<add_const_t<T>>; }; |
| 141 | template <typename T> using add_cv_t = typename add_cv<T>::type; |
| 142 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 143 | template <typename T> struct add_pointer { using type = remove_reference_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_reference<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> {}; |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 154 | template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type; |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 155 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 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; |
| 162 | }; |
| 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()) convertible(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; |
| 186 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 187 | } // namespace skstd |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 188 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 189 | // The sknonstd namespace contains things we would like to be proposed and feel std-ish. |
| 190 | namespace sknonstd { |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 191 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 192 | // The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'. |
| 193 | // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken). |
| 194 | // std::experimental::propagate_const already exists for other purposes in TSv2. |
| 195 | // These also follow the <dest, source> pattern used by boost. |
| 196 | template <typename D, typename S> struct copy_const { |
| 197 | using type = skstd::conditional_t<skstd::is_const<S>::value, skstd::add_const_t<D>, D>; |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 198 | }; |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 199 | template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type; |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 200 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 201 | template <typename D, typename S> struct copy_volatile { |
| 202 | using type = skstd::conditional_t<skstd::is_volatile<S>::value, skstd::add_volatile_t<D>, D>; |
| 203 | }; |
| 204 | template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type; |
| 205 | |
| 206 | template <typename D, typename S> struct copy_cv { |
| 207 | using type = copy_volatile_t<copy_const_t<D, S>, S>; |
| 208 | }; |
| 209 | template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type; |
| 210 | |
| 211 | // The name 'same' here means 'overwrite'. |
| 212 | // Alternate proposed names are 'replace', 'transfer', or 'qualify_from'. |
| 213 | // same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S> |
| 214 | template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>; |
| 215 | template <typename D, typename S> using same_const_t = typename same_const<D, S>::type; |
| 216 | template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>; |
| 217 | template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type; |
| 218 | template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>; |
| 219 | template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type; |
| 220 | |
| 221 | } // namespace sknonstd |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 222 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 223 | // Just a pithier wrapper for enable_if_t. |
| 224 | #define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T> |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 225 | |
bungeman@google.com | 4b3ef5a | 2013-07-12 18:31:59 +0000 | [diff] [blame] | 226 | #endif |