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; |
47 | 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 | struct monostate {}; |
63 | |
64 | template<typename...> struct conjunction : std::true_type { }; |
65 | template<typename T> struct conjunction<T> : T { }; |
66 | template<typename T, typename... Ts> |
67 | struct conjunction<T, Ts...> : std::conditional<bool(T::value), conjunction<Ts...>, T>::type { }; |
68 | } // namespace skstd |
69 | |
70 | // The sknonstd namespace contains things we would like to be proposed and feel std-ish. |
71 | namespace sknonstd { |
72 | |
73 | // The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'. |
74 | // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken). |
75 | // std::experimental::propagate_const already exists for other purposes in TSv2. |
76 | // These also follow the <dest, source> pattern used by boost. |
77 | template <typename D, typename S> struct copy_const { |
78 | using type = skstd::conditional_t<std::is_const<S>::value, skstd::add_const_t<D>, D>; |
79 | }; |
80 | template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type; |
81 | |
82 | template <typename D, typename S> struct copy_volatile { |
83 | using type = skstd::conditional_t<std::is_volatile<S>::value, skstd::add_volatile_t<D>, D>; |
84 | }; |
85 | template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type; |
86 | |
87 | template <typename D, typename S> struct copy_cv { |
88 | using type = copy_volatile_t<copy_const_t<D, S>, S>; |
89 | }; |
90 | template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type; |
91 | |
92 | // The name 'same' here means 'overwrite'. |
93 | // Alternate proposed names are 'replace', 'transfer', or 'qualify_from'. |
94 | // same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S> |
95 | template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>; |
96 | template <typename D, typename S> using same_const_t = typename same_const<D, S>::type; |
97 | template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>; |
98 | template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type; |
99 | template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>; |
100 | template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type; |
101 | |
102 | } // namespace sknonstd |
103 | |
104 | // Just a pithier wrapper for enable_if_t. |
105 | #define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T> |
106 | |
107 | #endif |
108 | |