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