| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TEMPLATE_UTIL_H_ | 5 #ifndef BASE_TEMPLATE_UTIL_H_ |
| 6 #define BASE_TEMPLATE_UTIL_H_ | 6 #define BASE_TEMPLATE_UTIL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <iosfwd> | 9 #include <iosfwd> |
| 10 #include <type_traits> | 10 #include <type_traits> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 | 14 |
| 15 // This hacks around libstdc++ 4.6 missing stuff in type_traits, while we need | |
| 16 // to support it. | |
| 17 #define CR_GLIBCXX_4_7_0 20120322 | |
| 18 #define CR_GLIBCXX_4_5_4 20120702 | |
| 19 #define CR_GLIBCXX_4_6_4 20121127 | |
| 20 #if defined(__GLIBCXX__) && \ | |
| 21 (__GLIBCXX__ < CR_GLIBCXX_4_7_0 || __GLIBCXX__ == CR_GLIBCXX_4_5_4 || \ | |
| 22 __GLIBCXX__ == CR_GLIBCXX_4_6_4) | |
| 23 #define CR_USE_FALLBACKS_FOR_OLD_GLIBCXX | |
| 24 #endif | |
| 25 | |
| 26 // Some versions of libstdc++ have partial support for type_traits, but misses | 15 // Some versions of libstdc++ have partial support for type_traits, but misses |
| 27 // a smaller subset while removing some of the older non-standard stuff. Assume | 16 // a smaller subset while removing some of the older non-standard stuff. Assume |
| 28 // that all versions below 5.0 fall in this category, along with one 5.0 | 17 // that all versions below 5.0 fall in this category, along with one 5.0 |
| 29 // experimental release. Test for this by consulting compiler major version, | 18 // experimental release. Test for this by consulting compiler major version, |
| 30 // the only reliable option available, so theoretically this could fail should | 19 // the only reliable option available, so theoretically this could fail should |
| 31 // you attempt to mix an earlier version of libstdc++ with >= GCC5. But | 20 // you attempt to mix an earlier version of libstdc++ with >= GCC5. But |
| 32 // that's unlikely to work out, especially as GCC5 changed ABI. | 21 // that's unlikely to work out, especially as GCC5 changed ABI. |
| 33 #define CR_GLIBCXX_5_0_0 20150123 | 22 #define CR_GLIBCXX_5_0_0 20150123 |
| 34 #if (defined(__GNUC__) && __GNUC__ < 5) || \ | 23 #if (defined(__GNUC__) && __GNUC__ < 5) || \ |
| 35 (defined(__GLIBCXX__) && __GLIBCXX__ == CR_GLIBCXX_5_0_0) | 24 (defined(__GLIBCXX__) && __GLIBCXX__ == CR_GLIBCXX_5_0_0) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 57 template <typename T, typename = void> | 46 template <typename T, typename = void> |
| 58 struct SupportsOstreamOperator : std::false_type {}; | 47 struct SupportsOstreamOperator : std::false_type {}; |
| 59 template <typename T> | 48 template <typename T> |
| 60 struct SupportsOstreamOperator<T, | 49 struct SupportsOstreamOperator<T, |
| 61 decltype(void(std::declval<std::ostream&>() | 50 decltype(void(std::declval<std::ostream&>() |
| 62 << std::declval<T>()))> | 51 << std::declval<T>()))> |
| 63 : std::true_type {}; | 52 : std::true_type {}; |
| 64 | 53 |
| 65 } // namespace internal | 54 } // namespace internal |
| 66 | 55 |
| 67 // TODO(crbug.com/554293): Remove this when all platforms have this in the std | |
| 68 // namespace. | |
| 69 #if defined(CR_USE_FALLBACKS_FOR_OLD_GLIBCXX) | |
| 70 template <class T> | |
| 71 using is_trivially_destructible = std::has_trivial_destructor<T>; | |
| 72 #else | |
| 73 template <class T> | |
| 74 using is_trivially_destructible = std::is_trivially_destructible<T>; | |
| 75 #endif | |
| 76 | |
| 77 // is_trivially_copyable is especially hard to get right. | 56 // is_trivially_copyable is especially hard to get right. |
| 78 // - Older versions of libstdc++ will fail to have it like they do for other | 57 // - Older versions of libstdc++ will fail to have it like they do for other |
| 79 // type traits. In this case we should provide it based on compiler | 58 // type traits. This has become a subset of the second point, but used to be |
| 80 // intrinsics. This is covered by the CR_USE_FALLBACKS_FOR_OLD_GLIBCXX define. | 59 // handled independently. |
| 81 // - An experimental release of gcc includes most of type_traits but misses | 60 // - An experimental release of gcc includes most of type_traits but misses |
| 82 // is_trivially_copyable, so we still have to avoid using libstdc++ in this | 61 // is_trivially_copyable, so we still have to avoid using libstdc++ in this |
| 83 // case, which is covered by CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX. | 62 // case, which is covered by CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX. |
| 84 // - When compiling libc++ from before r239653, with a gcc compiler, the | 63 // - When compiling libc++ from before r239653, with a gcc compiler, the |
| 85 // std::is_trivially_copyable can fail. So we need to work around that by not | 64 // std::is_trivially_copyable can fail. So we need to work around that by not |
| 86 // using the one in libc++ in this case. This is covered by the | 65 // using the one in libc++ in this case. This is covered by the |
| 87 // CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX define, and is discussed in | 66 // CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX define, and is discussed in |
| 88 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 where they point out that | 67 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 where they point out that |
| 89 // in libc++'s commit r239653 this is fixed by libc++ checking for gcc 5.1. | 68 // in libc++'s commit r239653 this is fixed by libc++ checking for gcc 5.1. |
| 90 // - In both of the above cases we are using the gcc compiler. When defining | 69 // - In both of the above cases we are using the gcc compiler. When defining |
| 91 // this ourselves on compiler intrinsics, the __is_trivially_copyable() | 70 // this ourselves on compiler intrinsics, the __is_trivially_copyable() |
| 92 // intrinsic is not available on gcc before version 5.1 (see the discussion in | 71 // intrinsic is not available on gcc before version 5.1 (see the discussion in |
| 93 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 again), so we must check for | 72 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 again), so we must check for |
| 94 // that version. | 73 // that version. |
| 95 // - When __is_trivially_copyable() is not available because we are on gcc older | 74 // - When __is_trivially_copyable() is not available because we are on gcc older |
| 96 // than 5.1, we need to fall back to something, so we use __has_trivial_copy() | 75 // than 5.1, we need to fall back to something, so we use __has_trivial_copy() |
| 97 // instead based on what was done one-off in bit_cast() previously. | 76 // instead based on what was done one-off in bit_cast() previously. |
| 98 | 77 |
| 99 // TODO(crbug.com/554293): Remove this when all platforms have this in the std | 78 // TODO(crbug.com/554293): Remove this when all platforms have this in the std |
| 100 // namespace and it works with gcc as needed. | 79 // namespace and it works with gcc as needed. |
| 101 #if defined(CR_USE_FALLBACKS_FOR_OLD_GLIBCXX) || \ | 80 #if defined(CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX) || \ |
| 102 defined(CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX) || \ | |
| 103 defined(CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX) | 81 defined(CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX) |
| 104 template <typename T> | 82 template <typename T> |
| 105 struct is_trivially_copyable { | 83 struct is_trivially_copyable { |
| 106 // TODO(danakj): Remove this when android builders are all using a newer version | 84 // TODO(danakj): Remove this when android builders are all using a newer version |
| 107 // of gcc, or the android ndk is updated to a newer libc++ that does this for | 85 // of gcc, or the android ndk is updated to a newer libc++ that does this for |
| 108 // us. | 86 // us. |
| 109 #if _GNUC_VER >= 501 | 87 #if _GNUC_VER >= 501 |
| 110 static constexpr bool value = __is_trivially_copyable(T); | 88 static constexpr bool value = __is_trivially_copyable(T); |
| 111 #else | 89 #else |
| 112 static constexpr bool value = __has_trivial_copy(T); | 90 static constexpr bool value = __has_trivial_copy(T); |
| 113 #endif | 91 #endif |
| 114 }; | 92 }; |
| 115 #else | 93 #else |
| 116 template <class T> | 94 template <class T> |
| 117 using is_trivially_copyable = std::is_trivially_copyable<T>; | 95 using is_trivially_copyable = std::is_trivially_copyable<T>; |
| 118 #endif | 96 #endif |
| 119 | 97 |
| 120 } // namespace base | 98 } // namespace base |
| 121 | 99 |
| 122 #undef CR_USE_FALLBACKS_FOR_OLD_GLIBCXX | |
| 123 #undef CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX | 100 #undef CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX |
| 124 #undef CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX | 101 #undef CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX |
| 125 | 102 |
| 126 #endif // BASE_TEMPLATE_UTIL_H_ | 103 #endif // BASE_TEMPLATE_UTIL_H_ |
| OLD | NEW |