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 <array>
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <type_traits>
21 #include <utility>
22 
23 namespace skstd {
24 
25 template <bool B> using bool_constant = std::integral_constant<bool, B>;
26 
27 template <bool B, typename T, typename F> using conditional_t = typename std::conditional<B, T, F>::type;
28 template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type;
29 
30 template <typename T> using remove_const_t = typename std::remove_const<T>::type;
31 template <typename T> using remove_volatile_t = typename std::remove_volatile<T>::type;
32 template <typename T> using remove_cv_t = typename std::remove_cv<T>::type;
33 template <typename T> using remove_pointer_t = typename std::remove_pointer<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>::type;
36 
37 template <typename T> using add_const_t = typename std::add_const<T>::type;
38 template <typename T> using add_volatile_t = typename std::add_volatile<T>::type;
39 template <typename T> using add_cv_t = typename std::add_cv<T>::type;
40 template <typename T> using add_pointer_t = typename std::add_pointer<T>::type;
41 template <typename T> using add_lvalue_reference_t = typename std::add_lvalue_reference<T>::type;
42 
43 template <typename T> using result_of_t = typename std::result_of<T>::type;
44 
45 template <typename... T> using common_type_t = typename std::common_type<T...>::type;
46 
47 // Chromium currently requires gcc 4.8.2 or a recent clang compiler, but uses libstdc++4.6.4.
48 // Note that Precise actually uses libstdc++4.6.3.
49 // Unfortunately, libstdc++ STL before libstdc++4.7 do not define std::underlying_type.
50 // Newer gcc and clang compilers have __underlying_type which does not depend on runtime support.
51 // See https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html for __GLIBCXX__ values.
52 // Unfortunately __GLIBCXX__ is a date, but no updates to versions before 4.7 are now anticipated.
53 #define SK_GLIBCXX_4_7_0 20120322
54 // Updates to versions before 4.7 but released after 4.7 was released.
55 #define SK_GLIBCXX_4_5_4 20120702
56 #define SK_GLIBCXX_4_6_4 20121127
57 #if defined(__GLIBCXX__) && (__GLIBCXX__ <  SK_GLIBCXX_4_7_0 || \
58                              __GLIBCXX__ == SK_GLIBCXX_4_5_4 || \
59                              __GLIBCXX__ == SK_GLIBCXX_4_6_4)
60 template <typename T> struct underlying_type {
61     using type = __underlying_type(T);
62 };
63 template <typename T> using is_trivially_destructible = std::has_trivial_destructor<T>;
64 #else
65 template <typename T> using underlying_type = std::underlying_type<T>;
66 template <typename T> using is_trivially_destructible = std::is_trivially_destructible<T>;
67 #endif
68 template <typename T> using underlying_type_t = typename skstd::underlying_type<T>::type;
69 
70 
71 template <std::size_t... Ints> struct index_sequence {
72     using type = index_sequence;
73     using value_type = size_t;
74     static constexpr std::size_t size() noexcept { return sizeof...(Ints); }
75 };
76 
77 template <typename S1, typename S2> struct make_index_sequence_combine;
78 template <size_t... I1, size_t... I2>
79 struct make_index_sequence_combine<skstd::index_sequence<I1...>, skstd::index_sequence<I2...>>
80     : skstd::index_sequence<I1..., (sizeof...(I1)+I2)...>
81 { };
82 
83 template <size_t N> struct make_index_sequence
84     : make_index_sequence_combine<typename skstd::make_index_sequence<    N/2>::type,
85                                   typename skstd::make_index_sequence<N - N/2>::type>{};
86 template<> struct make_index_sequence<0> : skstd::index_sequence< >{};
87 template<> struct make_index_sequence<1> : skstd::index_sequence<0>{};
88 
89 }  // namespace skstd
90 
91 // The sknonstd namespace contains things we would like to be proposed and feel std-ish.
92 namespace sknonstd {
93 
94 // The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'.
95 // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken).
96 // std::experimental::propagate_const already exists for other purposes in TSv2.
97 // These also follow the <dest, source> pattern used by boost.
98 template <typename D, typename S> struct copy_const {
99     using type = skstd::conditional_t<std::is_const<S>::value, skstd::add_const_t<D>, D>;
100 };
101 template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type;
102 
103 template <typename D, typename S> struct copy_volatile {
104     using type = skstd::conditional_t<std::is_volatile<S>::value, skstd::add_volatile_t<D>, D>;
105 };
106 template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type;
107 
108 template <typename D, typename S> struct copy_cv {
109     using type = copy_volatile_t<copy_const_t<D, S>, S>;
110 };
111 template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type;
112 
113 // The name 'same' here means 'overwrite'.
114 // Alternate proposed names are 'replace', 'transfer', or 'qualify_from'.
115 // same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S>
116 template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>;
117 template <typename D, typename S> using same_const_t = typename same_const<D, S>::type;
118 template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>;
119 template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type;
120 template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>;
121 template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type;
122 
123 }  // namespace sknonstd
124 
125 // Just a pithier wrapper for enable_if_t.
126 #define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T>
127 
128 #endif
129