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 (like std::enable_if_t) which will 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 11 * as SK_WHEN and the sknonstd namespace. 12 */ 13 14 #ifndef SkTLogic_DEFINED 15 #define SkTLogic_DEFINED 16 17 #include "SkTypes.h" 18 19 #include <stddef.h> 20 #include <stdint.h> 21 #include <type_traits> 22 #include <utility> 23 24 namespace skstd { 25 26 template <bool B> using bool_constant = std::integral_constant<bool, B>; 27 28 template <bool B, typename T, typename F> using conditional_t = typename std::conditional<B, T, F>::type; 29 template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type; 30 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_pointer_t = typename std::remove_pointer<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>::type; 37 38 // template<typename R, typename... Args> struct is_function< 39 // R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : std::true_type {}; 40 // The cv and ref-qualified versions are strange types we're currently avoiding, so not supported. 41 // These aren't supported in msvc either until vs2015u2. 42 // On all platforms, variadic functions only exist in the c calling convention. 43 // mcvc 2013 introduced __vectorcall, but it wan't until 2015 that it was added to is_function. 44 template <typename> struct is_function : std::false_type {}; 45 #if !defined(SK_BUILD_FOR_WIN) 46 template <typename R, typename... Args> struct is_function<R(Args...)> : std::true_type {}; 47 #else 48 template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : std::true_type {}; 49 #if defined(_M_IX86) 50 template <typename R, typename... Args> struct is_function<R __stdcall (Args...)> : std::true_type {}; 51 template <typename R, typename... Args> struct is_function<R __fastcall (Args...)> : std::true_type {}; 52 #endif 53 #if defined(_MSC_VER) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 54 template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : std::true_type {}; 55 #endif 56 #endif 57 template <typename R, typename... Args> struct is_function<R(Args..., ...)> : std::true_type {}; 58 59 template <typename T> using add_const_t = typename std::add_const<T>::type; 60 template <typename T> using add_volatile_t = typename std::add_volatile<T>::type; 61 template <typename T> using add_cv_t = typename std::add_cv<T>::type; 62 template <typename T> using add_pointer_t = typename std::add_pointer<T>::type; 63 template <typename T> using add_lvalue_reference_t = typename std::add_lvalue_reference<T>::type; 64 65 template <typename S, typename D, 66 bool=std::is_void<S>::value || is_function<D>::value || std::is_array<D>::value> 67 struct is_convertible_detector { 68 static const/*expr*/ bool value = std::is_void<D>::value; 69 }; 70 template <typename S, typename D> struct is_convertible_detector<S, D, false> { 71 using yes_type = uint8_t; 72 using no_type = uint16_t; 73 74 template <typename To> static void param_convertable_to(To); 75 76 template <typename From, typename To> 77 static decltype(param_convertable_to<To>(std::declval<From>()), yes_type()) convertible(int); 78 79 template <typename, typename> static no_type convertible(...); 80 81 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes_type); 82 }; 83 // std::is_convertable is known to be broken (not work with incomplete types) in Android clang NDK. 84 // This is currently what prevents us from using std::unique_ptr. 85 template<typename S, typename D> struct is_convertible 86 : bool_constant<is_convertible_detector<S, D>::value> {}; 87 88 } // namespace skstd 89 90 // The sknonstd namespace contains things we would like to be proposed and feel std-ish. 91 namespace sknonstd { 92 93 // The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'. 94 // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken). 95 // std::experimental::propagate_const already exists for other purposes in TSv2. 96 // These also follow the <dest, source> pattern used by boost. 97 template <typename D, typename S> struct copy_const { 98 using type = skstd::conditional_t<std::is_const<S>::value, skstd::add_const_t<D>, D>; 99 }; 100 template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type; 101 102 template <typename D, typename S> struct copy_volatile { 103 using type = skstd::conditional_t<std::is_volatile<S>::value, skstd::add_volatile_t<D>, D>; 104 }; 105 template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type; 106 107 template <typename D, typename S> struct copy_cv { 108 using type = copy_volatile_t<copy_const_t<D, S>, S>; 109 }; 110 template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type; 111 112 // The name 'same' here means 'overwrite'. 113 // Alternate proposed names are 'replace', 'transfer', or 'qualify_from'. 114 // same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S> 115 template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>; 116 template <typename D, typename S> using same_const_t = typename same_const<D, S>::type; 117 template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>; 118 template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type; 119 template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>; 120 template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type; 121 122 } // namespace sknonstd 123 124 // Just a pithier wrapper for enable_if_t. 125 #define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T> 126 127 #endif 128