template_util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. // Helper to express preferences in an overload set. If more than one overload
  76. // are available for a given set of parameters the overload with the higher
  77. // priority will be chosen.
  78. template <size_t I>
  79. struct priority_tag : priority_tag<I - 1> {};
  80. template <>
  81. struct priority_tag<0> {};
  82. } // namespace internal
  83. // is_trivially_copyable is especially hard to get right.
  84. // - Older versions of libstdc++ will fail to have it like they do for other
  85. // type traits. This has become a subset of the second point, but used to be
  86. // handled independently.
  87. // - An experimental release of gcc includes most of type_traits but misses
  88. // is_trivially_copyable, so we still have to avoid using libstdc++ in this
  89. // case, which is covered by CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX.
  90. // - When compiling libc++ from before r239653, with a gcc compiler, the
  91. // std::is_trivially_copyable can fail. So we need to work around that by not
  92. // using the one in libc++ in this case. This is covered by the
  93. // CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX define, and is discussed in
  94. // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 where they point out that
  95. // in libc++'s commit r239653 this is fixed by libc++ checking for gcc 5.1.
  96. // - In both of the above cases we are using the gcc compiler. When defining
  97. // this ourselves on compiler intrinsics, the __is_trivially_copyable()
  98. // intrinsic is not available on gcc before version 5.1 (see the discussion in
  99. // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 again), so we must check for
  100. // that version.
  101. // - When __is_trivially_copyable() is not available because we are on gcc older
  102. // than 5.1, we need to fall back to something, so we use __has_trivial_copy()
  103. // instead based on what was done one-off in bit_cast() previously.
  104. // TODO(crbug.com/554293): Remove this when all platforms have this in the std
  105. // namespace and it works with gcc as needed.
  106. #if defined(CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX) || \
  107. defined(CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX)
  108. template <typename T>
  109. struct is_trivially_copyable {
  110. // TODO(danakj): Remove this when android builders are all using a newer version
  111. // of gcc, or the android ndk is updated to a newer libc++ that does this for
  112. // us.
  113. #if _GNUC_VER >= 501
  114. static constexpr bool value = __is_trivially_copyable(T);
  115. #else
  116. static constexpr bool value =
  117. __has_trivial_copy(T) && __has_trivial_destructor(T);
  118. #endif
  119. };
  120. #else
  121. template <class T>
  122. using is_trivially_copyable = std::is_trivially_copyable<T>;
  123. #endif
  124. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 7
  125. // Workaround for g++7 and earlier family.
  126. // Due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654, without this
  127. // Optional<std::vector<T>> where T is non-copyable causes a compile error.
  128. // As we know it is not trivially copy constructible, explicitly declare so.
  129. template <typename T>
  130. struct is_trivially_copy_constructible
  131. : std::is_trivially_copy_constructible<T> {};
  132. template <typename... T>
  133. struct is_trivially_copy_constructible<std::vector<T...>> : std::false_type {};
  134. #else
  135. // Otherwise use std::is_trivially_copy_constructible as is.
  136. template <typename T>
  137. using is_trivially_copy_constructible = std::is_trivially_copy_constructible<T>;
  138. #endif
  139. // base::in_place_t is an implementation of std::in_place_t from
  140. // C++17. A tag type used to request in-place construction in template vararg
  141. // constructors.
  142. // Specification:
  143. // https://en.cppreference.com/w/cpp/utility/in_place
  144. struct in_place_t {};
  145. constexpr in_place_t in_place = {};
  146. // base::in_place_type_t is an implementation of std::in_place_type_t from
  147. // C++17. A tag type used for in-place construction when the type to construct
  148. // needs to be specified, such as with base::unique_any, designed to be a
  149. // drop-in replacement.
  150. // Specification:
  151. // http://en.cppreference.com/w/cpp/utility/in_place
  152. template <typename T>
  153. struct in_place_type_t {};
  154. template <typename T>
  155. struct is_in_place_type_t {
  156. static constexpr bool value = false;
  157. };
  158. template <typename... Ts>
  159. struct is_in_place_type_t<in_place_type_t<Ts...>> {
  160. static constexpr bool value = true;
  161. };
  162. // C++14 implementation of C++17's std::bool_constant.
  163. //
  164. // Reference: https://en.cppreference.com/w/cpp/types/integral_constant
  165. // Specification: https://wg21.link/meta.type.synop
  166. template <bool B>
  167. using bool_constant = std::integral_constant<bool, B>;
  168. // C++14 implementation of C++17's std::conjunction.
  169. //
  170. // Reference: https://en.cppreference.com/w/cpp/types/conjunction
  171. // Specification: https://wg21.link/meta.logical#1.itemdecl:1
  172. template <typename...>
  173. struct conjunction : std::true_type {};
  174. template <typename B1>
  175. struct conjunction<B1> : B1 {};
  176. template <typename B1, typename... Bn>
  177. struct conjunction<B1, Bn...>
  178. : std::conditional_t<static_cast<bool>(B1::value), conjunction<Bn...>, B1> {
  179. };
  180. // C++14 implementation of C++17's std::disjunction.
  181. //
  182. // Reference: https://en.cppreference.com/w/cpp/types/disjunction
  183. // Specification: https://wg21.link/meta.logical#itemdecl:2
  184. template <typename...>
  185. struct disjunction : std::false_type {};
  186. template <typename B1>
  187. struct disjunction<B1> : B1 {};
  188. template <typename B1, typename... Bn>
  189. struct disjunction<B1, Bn...>
  190. : std::conditional_t<static_cast<bool>(B1::value), B1, disjunction<Bn...>> {
  191. };
  192. // C++14 implementation of C++17's std::negation.
  193. //
  194. // Reference: https://en.cppreference.com/w/cpp/types/negation
  195. // Specification: https://wg21.link/meta.logical#itemdecl:3
  196. template <typename B>
  197. struct negation : bool_constant<!static_cast<bool>(B::value)> {};
  198. // Implementation of C++17's std::invoke_result_t.
  199. //
  200. // This implementation adds references to `Functor` and `Args` to work around
  201. // some quirks of std::result_of_t. See the #Notes section of [1] for details.
  202. //
  203. // References:
  204. // [1] https://en.cppreference.com/w/cpp/types/result_of
  205. // [2] https://wg21.link/meta.type.synop#lib:invoke_result_t
  206. template <typename Functor, typename... Args>
  207. using invoke_result_t = std::result_of_t<Functor && (Args && ...)>;
  208. // Simplified implementation of C++20's std::iter_value_t.
  209. // As opposed to std::iter_value_t, this implementation does not restrict
  210. // the type of `Iter` and does not consider specializations of
  211. // `indirectly_readable_traits`.
  212. //
  213. // Reference: https://wg21.link/readable.traits#2
  214. template <typename Iter>
  215. using iter_value_t = typename std::iterator_traits<
  216. std::remove_cv_t<std::remove_reference_t<Iter>>>::value_type;
  217. // Simplified implementation of C++20's std::iter_reference_t.
  218. // As opposed to std::iter_reference_t, this implementation does not restrict
  219. // the type of `Iter`.
  220. //
  221. // Reference: https://wg21.link/iterator.synopsis#:~:text=iter_reference_t
  222. template <typename Iter>
  223. using iter_reference_t = decltype(*std::declval<Iter&>());
  224. // Simplified implementation of C++20's std::indirect_result_t. As opposed to
  225. // std::indirect_result_t, this implementation does not restrict the type of
  226. // `Func` and `Iters`.
  227. //
  228. // Reference: https://wg21.link/iterator.synopsis#:~:text=indirect_result_t
  229. template <typename Func, typename... Iters>
  230. using indirect_result_t = invoke_result_t<Func, iter_reference_t<Iters>...>;
  231. // Simplified implementation of C++20's std::projected. As opposed to
  232. // std::projected, this implementation does not explicitly restrict the type of
  233. // `Iter` and `Proj`, but rather does so implicitly by requiring
  234. // `indirect_result_t<Proj, Iter>` is a valid type. This is required for SFINAE
  235. // friendliness.
  236. //
  237. // Reference: https://wg21.link/projected
  238. template <typename Iter,
  239. typename Proj,
  240. typename IndirectResultT = indirect_result_t<Proj, Iter>>
  241. struct projected {
  242. using value_type = std::remove_cv_t<std::remove_reference_t<IndirectResultT>>;
  243. IndirectResultT operator*() const; // not defined
  244. };
  245. } // namespace base
  246. #undef CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX
  247. #undef CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX
  248. #endif // BASE_TEMPLATE_UTIL_H_