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 template <typename T, typename = void>
76 struct SupportsToString : std::false_type {};
77 template <typename T>
78 struct SupportsToString<T, decltype(void(std::declval<T>().ToString()))>
79     : std::true_type {};
80 
81 // Used to detech whether the given type is an iterator.  This is normally used
82 // with std::enable_if to provide disambiguation for functions that take
83 // templatzed iterators as input.
84 template <typename T, typename = void>
85 struct is_iterator : std::false_type {};
86 
87 template <typename T>
88 struct is_iterator<T,
89                    void_t<typename std::iterator_traits<T>::iterator_category>>
90     : std::true_type {};
91 
92 }  // namespace internal
93 
94 // is_trivially_copyable is especially hard to get right.
95 // - Older versions of libstdc++ will fail to have it like they do for other
96 //   type traits. This has become a subset of the second point, but used to be
97 //   handled independently.
98 // - An experimental release of gcc includes most of type_traits but misses
99 //   is_trivially_copyable, so we still have to avoid using libstdc++ in this
100 //   case, which is covered by CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX.
101 // - When compiling libc++ from before r239653, with a gcc compiler, the
102 //   std::is_trivially_copyable can fail. So we need to work around that by not
103 //   using the one in libc++ in this case. This is covered by the
104 //   CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX define, and is discussed in
105 //   https://llvm.org/bugs/show_bug.cgi?id=27538#c1 where they point out that
106 //   in libc++'s commit r239653 this is fixed by libc++ checking for gcc 5.1.
107 // - In both of the above cases we are using the gcc compiler. When defining
108 //   this ourselves on compiler intrinsics, the __is_trivially_copyable()
109 //   intrinsic is not available on gcc before version 5.1 (see the discussion in
110 //   https://llvm.org/bugs/show_bug.cgi?id=27538#c1 again), so we must check for
111 //   that version.
112 // - When __is_trivially_copyable() is not available because we are on gcc older
113 //   than 5.1, we need to fall back to something, so we use __has_trivial_copy()
114 //   instead based on what was done one-off in bit_cast() previously.
115 
116 // TODO(crbug.com/554293): Remove this when all platforms have this in the std
117 // namespace and it works with gcc as needed.
118 #if defined(CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX) || \
119     defined(CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX)
120 template <typename T>
121 struct is_trivially_copyable {
122 // TODO(danakj): Remove this when android builders are all using a newer version
123 // of gcc, or the android ndk is updated to a newer libc++ that does this for
124 // us.
125 #if _GNUC_VER >= 501
126   static constexpr bool value = __is_trivially_copyable(T);
127 #else
128   static constexpr bool value =
129       __has_trivial_copy(T) && __has_trivial_destructor(T);
130 #endif
131 };
132 #else
133 template <class T>
134 using is_trivially_copyable = std::is_trivially_copyable<T>;
135 #endif
136 
137 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 7
138 // Workaround for g++7 and earlier family.
139 // Due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654, without this
140 // Optional<std::vector<T>> where T is non-copyable causes a compile error.
141 // As we know it is not trivially copy constructible, explicitly declare so.
142 template <typename T>
143 struct is_trivially_copy_constructible
144     : std::is_trivially_copy_constructible<T> {};
145 
146 template <typename... T>
147 struct is_trivially_copy_constructible<std::vector<T...>> : std::false_type {};
148 #else
149 // Otherwise use std::is_trivially_copy_constructible as is.
150 template <typename T>
151 using is_trivially_copy_constructible = std::is_trivially_copy_constructible<T>;
152 #endif
153 
154 // base::in_place_t is an implementation of std::in_place_t from
155 // C++17. A tag type used to request in-place construction in template vararg
156 // constructors.
157 
158 // Specification:
159 // https://en.cppreference.com/w/cpp/utility/in_place
160 struct in_place_t {};
161 constexpr in_place_t in_place = {};
162 
163 // base::in_place_type_t is an implementation of std::in_place_type_t from
164 // C++17. A tag type used for in-place construction when the type to construct
165 // needs to be specified, such as with base::unique_any, designed to be a
166 // drop-in replacement.
167 
168 // Specification:
169 // http://en.cppreference.com/w/cpp/utility/in_place
170 template <typename T>
171 struct in_place_type_t {};
172 
173 template <typename T>
174 struct is_in_place_type_t {
175   static constexpr bool value = false;
176 };
177 
178 template <typename... Ts>
179 struct is_in_place_type_t<in_place_type_t<Ts...>> {
180   static constexpr bool value = true;
181 };
182 
183 }  // namespace base
184 
185 #undef CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX
186 #undef CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX
187 
188 #endif  // BASE_TEMPLATE_UTIL_H_
189