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 <cstddef> 18 #include <type_traits> 19 20 namespace skstd { 21 22 template <bool B> using bool_constant = std::integral_constant<bool, B>; 23 24 template <bool B, typename T, typename F> using conditional_t = typename std::conditional<B, T, F>::type; 25 template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type; 26 27 template <typename T> using remove_const_t = typename std::remove_const<T>::type; 28 template <typename T> using remove_volatile_t = typename std::remove_volatile<T>::type; 29 template <typename T> using remove_cv_t = typename std::remove_cv<T>::type; 30 template <typename T> using remove_pointer_t = typename std::remove_pointer<T>::type; 31 template <typename T> using remove_reference_t = typename std::remove_reference<T>::type; 32 template <typename T> using remove_extent_t = typename std::remove_extent<T>::type; 33 34 template <typename T> using add_const_t = typename std::add_const<T>::type; 35 template <typename T> using add_volatile_t = typename std::add_volatile<T>::type; 36 template <typename T> using add_cv_t = typename std::add_cv<T>::type; 37 template <typename T> using add_pointer_t = typename std::add_pointer<T>::type; 38 template <typename T> using add_lvalue_reference_t = typename std::add_lvalue_reference<T>::type; 39 40 template <typename T> using result_of_t = typename std::result_of<T>::type; 41 42 template <typename... T> using common_type_t = typename std::common_type<T...>::type; 43 44 template <std::size_t... Ints> struct index_sequence { 45 using type = index_sequence; 46 using value_type = std::size_t; sizeindex_sequence47 static constexpr std::size_t size() noexcept { return sizeof...(Ints); } 48 }; 49 50 template <typename S1, typename S2> struct make_index_sequence_combine; 51 template <std::size_t... I1, std::size_t... I2> 52 struct make_index_sequence_combine<skstd::index_sequence<I1...>, skstd::index_sequence<I2...>> 53 : skstd::index_sequence<I1..., (sizeof...(I1)+I2)...> 54 { }; 55 56 template <std::size_t N> struct make_index_sequence 57 : make_index_sequence_combine<typename skstd::make_index_sequence< N/2>::type, 58 typename skstd::make_index_sequence<N - N/2>::type>{}; 59 template<> struct make_index_sequence<0> : skstd::index_sequence< >{}; 60 template<> struct make_index_sequence<1> : skstd::index_sequence<0>{}; 61 62 } // namespace skstd 63 64 // The sknonstd namespace contains things we would like to be proposed and feel std-ish. 65 namespace sknonstd { 66 67 // The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'. 68 // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken). 69 // std::experimental::propagate_const already exists for other purposes in TSv2. 70 // These also follow the <dest, source> pattern used by boost. 71 template <typename D, typename S> struct copy_const { 72 using type = skstd::conditional_t<std::is_const<S>::value, skstd::add_const_t<D>, D>; 73 }; 74 template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type; 75 76 template <typename D, typename S> struct copy_volatile { 77 using type = skstd::conditional_t<std::is_volatile<S>::value, skstd::add_volatile_t<D>, D>; 78 }; 79 template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type; 80 81 template <typename D, typename S> struct copy_cv { 82 using type = copy_volatile_t<copy_const_t<D, S>, S>; 83 }; 84 template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type; 85 86 // The name 'same' here means 'overwrite'. 87 // Alternate proposed names are 'replace', 'transfer', or 'qualify_from'. 88 // same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S> 89 template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>; 90 template <typename D, typename S> using same_const_t = typename same_const<D, S>::type; 91 template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>; 92 template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type; 93 template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>; 94 template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type; 95 96 } // namespace sknonstd 97 98 // Just a pithier wrapper for enable_if_t. 99 #define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T> 100 101 #endif 102