template_util.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEMPLATE_UTIL_H_
  5. #define BASE_TEMPLATE_UTIL_H_
  6. #include <stddef.h>
  7. #include <iosfwd>
  8. #include <iterator>
  9. #include <type_traits>
  10. #include <utility>
  11. #include <vector>
  12. #include "build/build_config.h"
  13. // Some versions of libstdc++ have partial support for type_traits, but misses
  14. // a smaller subset while removing some of the older non-standard stuff. Assume
  15. // that all versions below 5.0 fall in this category, along with one 5.0
  16. // experimental release. Test for this by consulting compiler major version,
  17. // the only reliable option available, so theoretically this could fail should
  18. // you attempt to mix an earlier version of libstdc++ with >= GCC5. But
  19. // that's unlikely to work out, especially as GCC5 changed ABI.
  20. #define CR_GLIBCXX_5_0_0 20150123
  21. #if (defined(__GNUC__) && __GNUC__ < 5) || \
  22. (defined(__GLIBCXX__) && __GLIBCXX__ == CR_GLIBCXX_5_0_0)
  23. #define CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX
  24. #endif
  25. // This hacks around using gcc with libc++ which has some incompatibilies.
  26. // - is_trivially_* doesn't work: https://llvm.org/bugs/show_bug.cgi?id=27538
  27. // TODO(danakj): Remove this when android builders are all using a newer version
  28. // of gcc, or the android ndk is updated to a newer libc++ that works with older
  29. // gcc versions.
  30. #if !defined(__clang__) && defined(_LIBCPP_VERSION)
  31. #define CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX
  32. #endif
  33. namespace base {
  34. template <class T> struct is_non_const_reference : std::false_type {};
  35. template <class T> struct is_non_const_reference<T&> : std::true_type {};
  36. template <class T> struct is_non_const_reference<const T&> : std::false_type {};
  37. namespace internal {
  38. // Implementation detail of base::void_t below.
  39. template <typename...>
  40. struct make_void {
  41. using type = void;
  42. };
  43. } // namespace internal
  44. // base::void_t is an implementation of std::void_t from C++17.
  45. //
  46. // We use |base::internal::make_void| as a helper struct to avoid a C++14
  47. // defect:
  48. // http://en.cppreference.com/w/cpp/types/void_t
  49. // http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558
  50. template <typename... Ts>
  51. using void_t = typename ::base::internal::make_void<Ts...>::type;
  52. namespace internal {
  53. // Uses expression SFINAE to detect whether using operator<< would work.
  54. template <typename T, typename = void>
  55. struct SupportsOstreamOperator : std::false_type {};
  56. template <typename T>
  57. struct SupportsOstreamOperator<T,
  58. decltype(void(std::declval<std::ostream&>()
  59. << std::declval<T>()))>
  60. : std::true_type {};
  61. template <typename T, typename = void>
  62. struct SupportsToString : std::false_type {};
  63. template <typename T>
  64. struct SupportsToString<T, decltype(void(std::declval<T>().ToString()))>
  65. : std::true_type {};
  66. // Used to detech whether the given type is an iterator. This is normally used
  67. // with std::enable_if to provide disambiguation for functions that take
  68. // templatzed iterators as input.
  69. template <typename T, typename = void>
  70. struct is_iterator : std::false_type {};
  71. template <typename T>
  72. struct is_iterator<T,
  73. void_t<typename std::iterator_traits<T>::iterator_category>>
  74. : std::true_type {};
  75. } // namespace internal
  76. // is_trivially_copyable is especially hard to get right.
  77. // - Older versions of libstdc++ will fail to have it like they do for other
  78. // type traits. This has become a subset of the second point, but used to be
  79. // handled independently.
  80. // - An experimental release of gcc includes most of type_traits but misses
  81. // is_trivially_copyable, so we still have to avoid using libstdc++ in this
  82. // case, which is covered by CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX.
  83. // - When compiling libc++ from before r239653, with a gcc compiler, the
  84. // std::is_trivially_copyable can fail. So we need to work around that by not
  85. // using the one in libc++ in this case. This is covered by the
  86. // CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX define, and is discussed in
  87. // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 where they point out that
  88. // in libc++'s commit r239653 this is fixed by libc++ checking for gcc 5.1.
  89. // - In both of the above cases we are using the gcc compiler. When defining
  90. // this ourselves on compiler intrinsics, the __is_trivially_copyable()
  91. // intrinsic is not available on gcc before version 5.1 (see the discussion in
  92. // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 again), so we must check for
  93. // that version.
  94. // - When __is_trivially_copyable() is not available because we are on gcc older
  95. // than 5.1, we need to fall back to something, so we use __has_trivial_copy()
  96. // instead based on what was done one-off in bit_cast() previously.
  97. // TODO(crbug.com/554293): Remove this when all platforms have this in the std
  98. // namespace and it works with gcc as needed.
  99. #if defined(CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX) || \
  100. defined(CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX)
  101. template <typename T>
  102. struct is_trivially_copyable {
  103. // TODO(danakj): Remove this when android builders are all using a newer version
  104. // of gcc, or the android ndk is updated to a newer libc++ that does this for
  105. // us.
  106. #if _GNUC_VER >= 501
  107. static constexpr bool value = __is_trivially_copyable(T);
  108. #else
  109. static constexpr bool value =
  110. __has_trivial_copy(T) && __has_trivial_destructor(T);
  111. #endif
  112. };
  113. #else
  114. template <class T>
  115. using is_trivially_copyable = std::is_trivially_copyable<T>;
  116. #endif
  117. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 7
  118. // Workaround for g++7 and earlier family.
  119. // Due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654, without this
  120. // Optional<std::vector<T>> where T is non-copyable causes a compile error.
  121. // As we know it is not trivially copy constructible, explicitly declare so.
  122. template <typename T>
  123. struct is_trivially_copy_constructible
  124. : std::is_trivially_copy_constructible<T> {};
  125. template <typename... T>
  126. struct is_trivially_copy_constructible<std::vector<T...>> : std::false_type {};
  127. #else
  128. // Otherwise use std::is_trivially_copy_constructible as is.
  129. template <typename T>
  130. using is_trivially_copy_constructible = std::is_trivially_copy_constructible<T>;
  131. #endif
  132. // base::in_place_t is an implementation of std::in_place_t from
  133. // C++17. A tag type used to request in-place construction in template vararg
  134. // constructors.
  135. // Specification:
  136. // https://en.cppreference.com/w/cpp/utility/in_place
  137. struct in_place_t {};
  138. constexpr in_place_t in_place = {};
  139. // base::in_place_type_t is an implementation of std::in_place_type_t from
  140. // C++17. A tag type used for in-place construction when the type to construct
  141. // needs to be specified, such as with base::unique_any, designed to be a
  142. // drop-in replacement.
  143. // Specification:
  144. // http://en.cppreference.com/w/cpp/utility/in_place
  145. template <typename T>
  146. struct in_place_type_t {};
  147. template <typename T>
  148. struct is_in_place_type_t {
  149. static constexpr bool value = false;
  150. };
  151. template <typename... Ts>
  152. struct is_in_place_type_t<in_place_type_t<Ts...>> {
  153. static constexpr bool value = true;
  154. };
  155. // C++14 implementation of C++17's std::bool_constant.
  156. //
  157. // Reference: https://en.cppreference.com/w/cpp/types/integral_constant
  158. // Specification: https://wg21.link/meta.type.synop
  159. template <bool B>
  160. using bool_constant = std::integral_constant<bool, B>;
  161. // C++14 implementation of C++17's std::conjunction.
  162. //
  163. // Reference: https://en.cppreference.com/w/cpp/types/conjunction
  164. // Specification: https://wg21.link/meta.logical#1.itemdecl:1
  165. template <typename...>
  166. struct conjunction : std::true_type {};
  167. template <typename B1>
  168. struct conjunction<B1> : B1 {};
  169. template <typename B1, typename... Bn>
  170. struct conjunction<B1, Bn...>
  171. : std::conditional_t<static_cast<bool>(B1::value), conjunction<Bn...>, B1> {
  172. };
  173. // C++14 implementation of C++17's std::disjunction.
  174. //
  175. // Reference: https://en.cppreference.com/w/cpp/types/disjunction
  176. // Specification: https://wg21.link/meta.logical#itemdecl:2
  177. template <typename...>
  178. struct disjunction : std::false_type {};
  179. template <typename B1>
  180. struct disjunction<B1> : B1 {};
  181. template <typename B1, typename... Bn>
  182. struct disjunction<B1, Bn...>
  183. : std::conditional_t<static_cast<bool>(B1::value), B1, disjunction<Bn...>> {
  184. };
  185. // C++14 implementation of C++17's std::negation.
  186. //
  187. // Reference: https://en.cppreference.com/w/cpp/types/negation
  188. // Specification: https://wg21.link/meta.logical#itemdecl:3
  189. template <typename B>
  190. struct negation : bool_constant<!static_cast<bool>(B::value)> {};
  191. } // namespace base
  192. #undef CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX
  193. #undef CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX
  194. #endif // BASE_TEMPLATE_UTIL_H_