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 | * |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 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. |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #ifndef SkTLogic_DEFINED |
| 15 | #define SkTLogic_DEFINED |
| 16 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 17 | #include <stddef.h> |
bungeman | 2bd0285 | 2015-08-05 12:09:57 -0700 | [diff] [blame] | 18 | #include <stdint.h> |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 19 | #include <type_traits> |
| 20 | #include <utility> |
bungeman | 2bd0285 | 2015-08-05 12:09:57 -0700 | [diff] [blame] | 21 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 22 | namespace skstd { |
| 23 | |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 24 | template <bool B> using bool_constant = std::integral_constant<bool, B>; |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 25 | |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 26 | template <bool B, typename T, typename F> using conditional_t = typename std::conditional<B, T, F>::type; |
| 27 | template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type; |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 28 | |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 29 | template <typename T> using remove_const_t = typename std::remove_const<T>::type; |
| 30 | template <typename T> using remove_volatile_t = typename std::remove_volatile<T>::type; |
| 31 | template <typename T> using remove_cv_t = typename std::remove_cv<T>::type; |
bungeman | 54e6308 | 2016-01-06 08:30:59 -0800 | [diff] [blame] | 32 | template <typename T> using remove_pointer_t = typename std::remove_pointer<T>::type; |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 33 | template <typename T> using remove_reference_t = typename std::remove_reference<T>::type; |
| 34 | template <typename T> using remove_extent_t = typename std::remove_extent<T>::type; |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 35 | |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 36 | template <typename T> using add_const_t = typename std::add_const<T>::type; |
| 37 | template <typename T> using add_volatile_t = typename std::add_volatile<T>::type; |
| 38 | template <typename T> using add_cv_t = typename std::add_cv<T>::type; |
| 39 | template <typename T> using add_pointer_t = typename std::add_pointer<T>::type; |
bungeman | 0e10166 | 2016-01-08 11:05:09 -0800 | [diff] [blame] | 40 | template <typename T> using add_lvalue_reference_t = typename std::add_lvalue_reference<T>::type; |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 41 | |
bungeman | c901c11 | 2016-03-08 08:35:23 -0800 | [diff] [blame] | 42 | template <typename... T> using common_type_t = typename std::common_type<T...>::type; |
| 43 | |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 44 | // Chromium currently requires gcc 4.8.2 or a recent clang compiler, but uses libstdc++4.6.4. |
| 45 | // Note that Precise actually uses libstdc++4.6.3. |
| 46 | // Unfortunately, libstdc++ STL before libstdc++4.7 do not define std::underlying_type. |
| 47 | // Newer gcc and clang compilers have __underlying_type which does not depend on runtime support. |
| 48 | // See https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html for __GLIBCXX__ values. |
| 49 | // Unfortunately __GLIBCXX__ is a date, but no updates to versions before 4.7 are now anticipated. |
| 50 | #define SK_GLIBCXX_4_7_0 20120322 |
| 51 | // Updates to versions before 4.7 but released after 4.7 was released. |
| 52 | #define SK_GLIBCXX_4_5_4 20120702 |
| 53 | #define SK_GLIBCXX_4_6_4 20121127 |
| 54 | #if defined(__GLIBCXX__) && (__GLIBCXX__ < SK_GLIBCXX_4_7_0 || \ |
| 55 | __GLIBCXX__ == SK_GLIBCXX_4_5_4 || \ |
| 56 | __GLIBCXX__ == SK_GLIBCXX_4_6_4) |
| 57 | template <typename T> struct underlying_type { |
| 58 | using type = __underlying_type(T); |
| 59 | }; |
Herb Derby | 4fa20ce | 2017-01-13 15:17:02 -0500 | [diff] [blame] | 60 | template <typename T> using is_trivially_destructible = std::has_trivial_destructor<T>; |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 61 | #else |
| 62 | template <typename T> using underlying_type = std::underlying_type<T>; |
Herb Derby | 0497f08 | 2017-01-13 11:30:44 -0500 | [diff] [blame] | 63 | template <typename T> using is_trivially_destructible = std::is_trivially_destructible<T>; |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 64 | #endif |
| 65 | template <typename T> using underlying_type_t = typename skstd::underlying_type<T>::type; |
| 66 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 67 | } // namespace skstd |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 68 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 69 | // The sknonstd namespace contains things we would like to be proposed and feel std-ish. |
| 70 | namespace sknonstd { |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 71 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 72 | // The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'. |
| 73 | // Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken). |
| 74 | // std::experimental::propagate_const already exists for other purposes in TSv2. |
| 75 | // These also follow the <dest, source> pattern used by boost. |
| 76 | template <typename D, typename S> struct copy_const { |
bungeman | de029d0 | 2016-01-07 12:39:11 -0800 | [diff] [blame] | 77 | using type = skstd::conditional_t<std::is_const<S>::value, skstd::add_const_t<D>, D>; |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 78 | }; |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 79 | 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] | 80 | |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 81 | template <typename D, typename S> struct copy_volatile { |
bungeman | de029d0 | 2016-01-07 12:39:11 -0800 | [diff] [blame] | 82 | using type = skstd::conditional_t<std::is_volatile<S>::value, skstd::add_volatile_t<D>, D>; |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 83 | }; |
| 84 | template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type; |
| 85 | |
| 86 | template <typename D, typename S> struct copy_cv { |
| 87 | using type = copy_volatile_t<copy_const_t<D, S>, S>; |
| 88 | }; |
| 89 | template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type; |
| 90 | |
| 91 | // The name 'same' here means 'overwrite'. |
| 92 | // Alternate proposed names are 'replace', 'transfer', or 'qualify_from'. |
| 93 | // same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S> |
| 94 | template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>; |
| 95 | template <typename D, typename S> using same_const_t = typename same_const<D, S>::type; |
| 96 | template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>; |
| 97 | template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type; |
| 98 | template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>; |
| 99 | template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type; |
| 100 | |
| 101 | } // namespace sknonstd |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 102 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 103 | // Just a pithier wrapper for enable_if_t. |
| 104 | #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] | 105 | |
bungeman@google.com | 4b3ef5a | 2013-07-12 18:31:59 +0000 | [diff] [blame] | 106 | #endif |