1 | /* |
2 | * Copyright 2011-present Facebook, Inc. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | // @author: Andrei Alexandrescu |
18 | |
19 | #pragma once |
20 | |
21 | #include <functional> |
22 | #include <limits> |
23 | #include <memory> |
24 | #include <type_traits> |
25 | |
26 | #include <folly/Portability.h> |
27 | |
28 | // libc++ doesn't provide this header, nor does msvc |
29 | #if __has_include(<bits/c++config.h>) |
30 | // This file appears in two locations: inside fbcode and in the |
31 | // libstdc++ source code (when embedding fbstring as std::string). |
32 | // To aid in this schizophrenic use, two macros are defined in |
33 | // c++config.h: |
34 | // _LIBSTDCXX_FBSTRING - Set inside libstdc++. This is useful to |
35 | // gate use inside fbcode v. libstdc++ |
36 | #include <bits/c++config.h> |
37 | #endif |
38 | |
39 | #define FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(classname, type_name) \ |
40 | template <typename TTheClass_> \ |
41 | struct classname##__folly_traits_impl__ { \ |
42 | template <typename UTheClass_> \ |
43 | static constexpr bool test(typename UTheClass_::type_name*) { \ |
44 | return true; \ |
45 | } \ |
46 | template <typename> \ |
47 | static constexpr bool test(...) { \ |
48 | return false; \ |
49 | } \ |
50 | }; \ |
51 | template <typename TTheClass_> \ |
52 | using classname = typename std::conditional< \ |
53 | classname##__folly_traits_impl__<TTheClass_>::template test<TTheClass_>( \ |
54 | nullptr), \ |
55 | std::true_type, \ |
56 | std::false_type>::type |
57 | |
58 | #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \ |
59 | template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \ |
60 | struct classname##__folly_traits_impl__< \ |
61 | TTheClass_, \ |
62 | RTheReturn_(TTheArgs_...) cv_qual> { \ |
63 | template < \ |
64 | typename UTheClass_, \ |
65 | RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual> \ |
66 | struct sfinae {}; \ |
67 | template <typename UTheClass_> \ |
68 | static std::true_type test(sfinae<UTheClass_, &UTheClass_::func_name>*); \ |
69 | template <typename> \ |
70 | static std::false_type test(...); \ |
71 | } |
72 | |
73 | /* |
74 | * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits |
75 | * classes that check for the existence of a member function with |
76 | * a given name and signature. It currently does not support |
77 | * checking for inherited members. |
78 | * |
79 | * Such classes receive two template parameters: the class to be checked |
80 | * and the signature of the member function. A static boolean field |
81 | * named `value` (which is also constexpr) tells whether such member |
82 | * function exists. |
83 | * |
84 | * Each traits class created is bound only to the member name, not to |
85 | * its signature nor to the type of the class containing it. |
86 | * |
87 | * Say you need to know if a given class has a member function named |
88 | * `test` with the following signature: |
89 | * |
90 | * int test() const; |
91 | * |
92 | * You'd need this macro to create a traits class to check for a member |
93 | * named `test`, and then use this traits class to check for the signature: |
94 | * |
95 | * namespace { |
96 | * |
97 | * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test); |
98 | * |
99 | * } // unnamed-namespace |
100 | * |
101 | * void some_func() { |
102 | * cout << "Does class Foo have a member int test() const? " |
103 | * << boolalpha << has_test_traits<Foo, int() const>::value; |
104 | * } |
105 | * |
106 | * You can use the same traits class to test for a completely different |
107 | * signature, on a completely different class, as long as the member name |
108 | * is the same: |
109 | * |
110 | * void some_func() { |
111 | * cout << "Does class Foo have a member int test()? " |
112 | * << boolalpha << has_test_traits<Foo, int()>::value; |
113 | * cout << "Does class Foo have a member int test() const? " |
114 | * << boolalpha << has_test_traits<Foo, int() const>::value; |
115 | * cout << "Does class Bar have a member double test(const string&, long)? " |
116 | * << boolalpha << has_test_traits<Bar, double(const string&, long)>::value; |
117 | * } |
118 | * |
119 | * @author: Marcelo Juchem <marcelo@fb.com> |
120 | */ |
121 | #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name) \ |
122 | template <typename, typename> \ |
123 | struct classname##__folly_traits_impl__; \ |
124 | FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, ); \ |
125 | FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const); \ |
126 | FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \ |
127 | classname, func_name, /* nolint */ volatile); \ |
128 | FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \ |
129 | classname, func_name, /* nolint */ volatile const); \ |
130 | template <typename TTheClass_, typename TTheSignature_> \ |
131 | using classname = \ |
132 | decltype(classname##__folly_traits_impl__<TTheClass_, TTheSignature_>:: \ |
133 | template test<TTheClass_>(nullptr)) |
134 | |
135 | namespace folly { |
136 | |
137 | #if __cpp_lib_bool_constant || _MSC_VER |
138 | |
139 | using std::bool_constant; |
140 | |
141 | #else |
142 | |
143 | // mimic: std::bool_constant, C++17 |
144 | template <bool B> |
145 | using bool_constant = std::integral_constant<bool, B>; |
146 | |
147 | #endif |
148 | |
149 | template <std::size_t I> |
150 | using index_constant = std::integral_constant<std::size_t, I>; |
151 | |
152 | /*** |
153 | * _t |
154 | * |
155 | * Instead of: |
156 | * |
157 | * using decayed = typename std::decay<T>::type; |
158 | * |
159 | * With the C++14 standard trait aliases, we could use: |
160 | * |
161 | * using decayed = std::decay_t<T>; |
162 | * |
163 | * Without them, we could use: |
164 | * |
165 | * using decayed = _t<std::decay<T>>; |
166 | * |
167 | * Also useful for any other library with template types having dependent |
168 | * member types named `type`, like the standard trait types. |
169 | */ |
170 | template <typename T> |
171 | using _t = typename T::type; |
172 | |
173 | /** |
174 | * A type trait to remove all const volatile and reference qualifiers on a |
175 | * type T |
176 | */ |
177 | template <typename T> |
178 | struct remove_cvref { |
179 | using type = |
180 | typename std::remove_cv<typename std::remove_reference<T>::type>::type; |
181 | }; |
182 | template <typename T> |
183 | using remove_cvref_t = typename remove_cvref<T>::type; |
184 | |
185 | namespace detail { |
186 | template <typename Src> |
187 | struct like_ { |
188 | template <typename Dst> |
189 | using apply = Dst; |
190 | }; |
191 | template <typename Src> |
192 | struct like_<Src const> { |
193 | template <typename Dst> |
194 | using apply = Dst const; |
195 | }; |
196 | template <typename Src> |
197 | struct like_<Src volatile> { |
198 | template <typename Dst> |
199 | using apply = Dst volatile; |
200 | }; |
201 | template <typename Src> |
202 | struct like_<Src const volatile> { |
203 | template <typename Dst> |
204 | using apply = Dst const volatile; |
205 | }; |
206 | template <typename Src> |
207 | struct like_<Src&> { |
208 | template <typename Dst> |
209 | using apply = typename like_<Src>::template apply<Dst>&; |
210 | }; |
211 | template <typename Src> |
212 | struct like_<Src&&> { |
213 | template <typename Dst> |
214 | using apply = typename like_<Src>::template apply<Dst>&&; |
215 | }; |
216 | } // namespace detail |
217 | |
218 | // mimic: like_t, p0847r0 |
219 | template <typename Src, typename Dst> |
220 | using like_t = typename detail::like_<Src>::template apply<remove_cvref_t<Dst>>; |
221 | |
222 | // mimic: like, p0847r0 |
223 | template <typename Src, typename Dst> |
224 | struct like { |
225 | using type = like_t<Src, Dst>; |
226 | }; |
227 | |
228 | /** |
229 | * type_t |
230 | * |
231 | * A type alias for the first template type argument. `type_t` is useful for |
232 | * controlling class-template and function-template partial specialization. |
233 | * |
234 | * Example: |
235 | * |
236 | * template <typename Value> |
237 | * class Container { |
238 | * public: |
239 | * template <typename... Args> |
240 | * Container( |
241 | * type_t<in_place_t, decltype(Value(std::declval<Args>()...))>, |
242 | * Args&&...); |
243 | * }; |
244 | * |
245 | * void_t |
246 | * |
247 | * A type alias for `void`. `void_t` is useful for controling class-template |
248 | * and function-template partial specialization. |
249 | * |
250 | * Example: |
251 | * |
252 | * // has_value_type<T>::value is true if T has a nested type `value_type` |
253 | * template <class T, class = void> |
254 | * struct has_value_type |
255 | * : std::false_type {}; |
256 | * |
257 | * template <class T> |
258 | * struct has_value_type<T, folly::void_t<typename T::value_type>> |
259 | * : std::true_type {}; |
260 | */ |
261 | |
262 | /** |
263 | * There is a bug in libstdc++, libc++, and MSVC's STL that causes it to |
264 | * ignore unused template parameter arguments in template aliases and does not |
265 | * cause substitution failures. This defect has been recorded here: |
266 | * http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558. |
267 | * |
268 | * This causes the implementation of std::void_t to be buggy, as it is likely |
269 | * defined as something like the following: |
270 | * |
271 | * template <typename...> |
272 | * using void_t = void; |
273 | * |
274 | * This causes the compiler to ignore all the template arguments and does not |
275 | * help when one wants to cause substitution failures. Rather declarations |
276 | * which have void_t in orthogonal specializations are treated as the same. |
277 | * For example, assuming the possible `T` types are only allowed to have |
278 | * either the alias `one` or `two` and never both or none: |
279 | * |
280 | * template <typename T, |
281 | * typename std::void_t<std::decay_t<T>::one>* = nullptr> |
282 | * void foo(T&&) {} |
283 | * template <typename T, |
284 | * typename std::void_t<std::decay_t<T>::two>* = nullptr> |
285 | * void foo(T&&) {} |
286 | * |
287 | * The second foo() will be a redefinition because it conflicts with the first |
288 | * one; void_t does not cause substitution failures - the template types are |
289 | * just ignored. |
290 | */ |
291 | |
292 | namespace traits_detail { |
293 | template <class T, class...> |
294 | struct type_t_ { |
295 | using type = T; |
296 | }; |
297 | } // namespace traits_detail |
298 | |
299 | template <class T, class... Ts> |
300 | using type_t = typename traits_detail::type_t_<T, Ts...>::type; |
301 | template <class... Ts> |
302 | using void_t = type_t<void, Ts...>; |
303 | |
304 | template <typename T> |
305 | using aligned_storage_for_t = |
306 | typename std::aligned_storage<sizeof(T), alignof(T)>::type; |
307 | |
308 | // Older versions of libstdc++ do not provide std::is_trivially_copyable |
309 | #if defined(__clang__) && !defined(_LIBCPP_VERSION) |
310 | template <class T> |
311 | struct is_trivially_copyable : bool_constant<__is_trivially_copyable(T)> {}; |
312 | #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5 |
313 | template <class T> |
314 | struct is_trivially_copyable : std::is_trivial<T> {}; |
315 | #else |
316 | template <class T> |
317 | using is_trivially_copyable = std::is_trivially_copyable<T>; |
318 | #endif |
319 | |
320 | /** |
321 | * IsRelocatable<T>::value describes the ability of moving around |
322 | * memory a value of type T by using memcpy (as opposed to the |
323 | * conservative approach of calling the copy constructor and then |
324 | * destroying the old temporary. Essentially for a relocatable type, |
325 | * the following two sequences of code should be semantically |
326 | * equivalent: |
327 | * |
328 | * void move1(T * from, T * to) { |
329 | * new(to) T(from); |
330 | * (*from).~T(); |
331 | * } |
332 | * |
333 | * void move2(T * from, T * to) { |
334 | * memcpy(to, from, sizeof(T)); |
335 | * } |
336 | * |
337 | * Most C++ types are relocatable; the ones that aren't would include |
338 | * internal pointers or (very rarely) would need to update remote |
339 | * pointers to pointers tracking them. All C++ primitive types and |
340 | * type constructors are relocatable. |
341 | * |
342 | * This property can be used in a variety of optimizations. Currently |
343 | * fbvector uses this property intensively. |
344 | * |
345 | * The default conservatively assumes the type is not |
346 | * relocatable. Several specializations are defined for known |
347 | * types. You may want to add your own specializations. Do so in |
348 | * namespace folly and make sure you keep the specialization of |
349 | * IsRelocatable<SomeStruct> in the same header as SomeStruct. |
350 | * |
351 | * You may also declare a type to be relocatable by including |
352 | * `typedef std::true_type IsRelocatable;` |
353 | * in the class header. |
354 | * |
355 | * It may be unset in a base class by overriding the typedef to false_type. |
356 | */ |
357 | /* |
358 | * IsZeroInitializable describes the property that default construction is the |
359 | * same as memset(dst, 0, sizeof(T)). |
360 | */ |
361 | |
362 | namespace traits_detail { |
363 | |
364 | #define FOLLY_HAS_TRUE_XXX(name) \ |
365 | FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_##name, name); \ |
366 | template <class T> \ |
367 | struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \ |
368 | template <class T> \ |
369 | struct has_true_##name : std::conditional< \ |
370 | has_##name<T>::value, \ |
371 | name##_is_true<T>, \ |
372 | std::false_type>::type {} |
373 | |
374 | FOLLY_HAS_TRUE_XXX(IsRelocatable); |
375 | FOLLY_HAS_TRUE_XXX(IsZeroInitializable); |
376 | |
377 | #undef FOLLY_HAS_TRUE_XXX |
378 | |
379 | } // namespace traits_detail |
380 | |
381 | struct Ignore { |
382 | Ignore() = default; |
383 | template <class T> |
384 | constexpr /* implicit */ Ignore(const T&) {} |
385 | template <class T> |
386 | const Ignore& operator=(T const&) const { |
387 | return *this; |
388 | } |
389 | }; |
390 | |
391 | template <class...> |
392 | using Ignored = Ignore; |
393 | |
394 | namespace traits_detail_IsEqualityComparable { |
395 | Ignore operator==(Ignore, Ignore); |
396 | |
397 | template <class T, class U = T> |
398 | struct IsEqualityComparable |
399 | : std::is_convertible< |
400 | decltype(std::declval<T>() == std::declval<U>()), |
401 | bool> {}; |
402 | } // namespace traits_detail_IsEqualityComparable |
403 | |
404 | /* using override */ using traits_detail_IsEqualityComparable:: |
405 | IsEqualityComparable; |
406 | |
407 | namespace traits_detail_IsLessThanComparable { |
408 | Ignore operator<(Ignore, Ignore); |
409 | |
410 | template <class T, class U = T> |
411 | struct IsLessThanComparable |
412 | : std::is_convertible< |
413 | decltype(std::declval<T>() < std::declval<U>()), |
414 | bool> {}; |
415 | } // namespace traits_detail_IsLessThanComparable |
416 | |
417 | /* using override */ using traits_detail_IsLessThanComparable:: |
418 | IsLessThanComparable; |
419 | |
420 | namespace traits_detail_IsNothrowSwappable { |
421 | #if defined(__cpp_lib_is_swappable) || (_CPPLIB_VER && _HAS_CXX17) |
422 | // MSVC 2015+ already implements the C++17 P0185R1 proposal which |
423 | // adds std::is_nothrow_swappable, so use it instead if C++17 mode |
424 | // is enabled. |
425 | template <typename T> |
426 | using IsNothrowSwappable = std::is_nothrow_swappable<T>; |
427 | #elif _CPPLIB_VER |
428 | // MSVC 2015+ defines the base even if C++17 is disabled, and |
429 | // MSVC 2015 has issues with our fallback implementation due to |
430 | // over-eager evaluation of noexcept. |
431 | template <typename T> |
432 | using IsNothrowSwappable = std::_Is_nothrow_swappable<T>; |
433 | #else |
434 | /* using override */ using std::swap; |
435 | |
436 | template <class T> |
437 | struct IsNothrowSwappable |
438 | : bool_constant<std::is_nothrow_move_constructible<T>::value&& noexcept( |
439 | swap(std::declval<T&>(), std::declval<T&>()))> {}; |
440 | #endif |
441 | } // namespace traits_detail_IsNothrowSwappable |
442 | |
443 | /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable; |
444 | |
445 | template <class T> |
446 | struct IsRelocatable : std::conditional< |
447 | traits_detail::has_IsRelocatable<T>::value, |
448 | traits_detail::has_true_IsRelocatable<T>, |
449 | // TODO add this line (and some tests for it) when we |
450 | // upgrade to gcc 4.7 |
451 | // std::is_trivially_move_constructible<T>::value || |
452 | is_trivially_copyable<T>>::type {}; |
453 | |
454 | template <class T> |
455 | struct IsZeroInitializable |
456 | : std::conditional< |
457 | traits_detail::has_IsZeroInitializable<T>::value, |
458 | traits_detail::has_true_IsZeroInitializable<T>, |
459 | bool_constant<!std::is_class<T>::value>>::type {}; |
460 | |
461 | template <typename...> |
462 | struct Conjunction : std::true_type {}; |
463 | template <typename T> |
464 | struct Conjunction<T> : T {}; |
465 | template <typename T, typename... TList> |
466 | struct Conjunction<T, TList...> |
467 | : std::conditional<T::value, Conjunction<TList...>, T>::type {}; |
468 | |
469 | template <typename...> |
470 | struct Disjunction : std::false_type {}; |
471 | template <typename T> |
472 | struct Disjunction<T> : T {}; |
473 | template <typename T, typename... TList> |
474 | struct Disjunction<T, TList...> |
475 | : std::conditional<T::value, T, Disjunction<TList...>>::type {}; |
476 | |
477 | template <typename T> |
478 | struct Negation : bool_constant<!T::value> {}; |
479 | |
480 | template <bool... Bs> |
481 | struct Bools { |
482 | using valid_type = bool; |
483 | static constexpr std::size_t size() { |
484 | return sizeof...(Bs); |
485 | } |
486 | }; |
487 | |
488 | // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly. |
489 | template <class... Ts> |
490 | struct StrictConjunction |
491 | : std::is_same<Bools<Ts::value...>, Bools<(Ts::value || true)...>> {}; |
492 | |
493 | template <class... Ts> |
494 | struct StrictDisjunction |
495 | : Negation< |
496 | std::is_same<Bools<Ts::value...>, Bools<(Ts::value && false)...>>> {}; |
497 | |
498 | } // namespace folly |
499 | |
500 | /** |
501 | * Use this macro ONLY inside namespace folly. When using it with a |
502 | * regular type, use it like this: |
503 | * |
504 | * // Make sure you're at namespace ::folly scope |
505 | * template <> FOLLY_ASSUME_RELOCATABLE(MyType) |
506 | * |
507 | * When using it with a template type, use it like this: |
508 | * |
509 | * // Make sure you're at namespace ::folly scope |
510 | * template <class T1, class T2> |
511 | * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>) |
512 | */ |
513 | #define FOLLY_ASSUME_RELOCATABLE(...) \ |
514 | struct IsRelocatable<__VA_ARGS__> : std::true_type {} |
515 | |
516 | /** |
517 | * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode the |
518 | * assumption that the type is relocatable per IsRelocatable |
519 | * above. Many types can be assumed to satisfy this condition, but |
520 | * it is the responsibility of the user to state that assumption. |
521 | * User-defined classes will not be optimized for use with |
522 | * fbvector (see FBVector.h) unless they state that assumption. |
523 | * |
524 | * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this: |
525 | * |
526 | * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType) |
527 | * |
528 | * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4 |
529 | * allow using the macro for describing templatized classes with 1, 2, |
530 | * 3, and 4 template parameters respectively. For template classes |
531 | * just use the macro with the appropriate number and pass the name of |
532 | * the template to it. Example: |
533 | * |
534 | * template <class T1, class T2> class MyType { ... }; |
535 | * ... |
536 | * // Make sure you're at global scope |
537 | * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType) |
538 | */ |
539 | |
540 | // Use this macro ONLY at global level (no namespace) |
541 | #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \ |
542 | namespace folly { \ |
543 | template <> \ |
544 | FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__); \ |
545 | } |
546 | // Use this macro ONLY at global level (no namespace) |
547 | #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \ |
548 | namespace folly { \ |
549 | template <class T1> \ |
550 | FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>); \ |
551 | } |
552 | // Use this macro ONLY at global level (no namespace) |
553 | #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \ |
554 | namespace folly { \ |
555 | template <class T1, class T2> \ |
556 | FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>); \ |
557 | } |
558 | // Use this macro ONLY at global level (no namespace) |
559 | #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...) \ |
560 | namespace folly { \ |
561 | template <class T1, class T2, class T3> \ |
562 | FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>); \ |
563 | } |
564 | // Use this macro ONLY at global level (no namespace) |
565 | #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...) \ |
566 | namespace folly { \ |
567 | template <class T1, class T2, class T3, class T4> \ |
568 | FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>); \ |
569 | } |
570 | |
571 | /** |
572 | * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is |
573 | * safe to assume that pair is compatible if both of its components |
574 | * are. Furthermore, all STL containers can be assumed to comply, |
575 | * although that is not guaranteed by the standard. |
576 | */ |
577 | |
578 | FOLLY_NAMESPACE_STD_BEGIN |
579 | |
580 | template <class T, class U> |
581 | struct pair; |
582 | #ifndef _GLIBCXX_USE_FB |
583 | FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN |
584 | template <class T, class R, class A> |
585 | class basic_string; |
586 | FOLLY_GLIBCXX_NAMESPACE_CXX11_END |
587 | #else |
588 | template <class T, class R, class A, class S> |
589 | class basic_string; |
590 | #endif |
591 | template <class T, class A> |
592 | class vector; |
593 | template <class T, class A> |
594 | class deque; |
595 | template <class T, class C, class A> |
596 | class set; |
597 | template <class K, class V, class C, class A> |
598 | class map; |
599 | template <class T> |
600 | class shared_ptr; |
601 | |
602 | FOLLY_NAMESPACE_STD_END |
603 | |
604 | namespace folly { |
605 | |
606 | // STL commonly-used types |
607 | template <class T, class U> |
608 | struct IsRelocatable<std::pair<T, U>> |
609 | : bool_constant<IsRelocatable<T>::value && IsRelocatable<U>::value> {}; |
610 | |
611 | // Is T one of T1, T2, ..., Tn? |
612 | template <typename T, typename... Ts> |
613 | using IsOneOf = StrictDisjunction<std::is_same<T, Ts>...>; |
614 | |
615 | /* |
616 | * Complementary type traits for integral comparisons. |
617 | * |
618 | * For instance, `if(x < 0)` yields an error in clang for unsigned types |
619 | * when -Werror is used due to -Wtautological-compare |
620 | * |
621 | * |
622 | * @author: Marcelo Juchem <marcelo@fb.com> |
623 | */ |
624 | |
625 | namespace detail { |
626 | |
627 | template <typename T, bool> |
628 | struct is_negative_impl { |
629 | constexpr static bool check(T x) { |
630 | return x < 0; |
631 | } |
632 | }; |
633 | |
634 | template <typename T> |
635 | struct is_negative_impl<T, false> { |
636 | constexpr static bool check(T) { |
637 | return false; |
638 | } |
639 | }; |
640 | |
641 | // folly::to integral specializations can end up generating code |
642 | // inside what are really static ifs (not executed because of the templated |
643 | // types) that violate -Wsign-compare and/or -Wbool-compare so suppress them |
644 | // in order to not prevent all calling code from using it. |
645 | FOLLY_PUSH_WARNING |
646 | FOLLY_GNU_DISABLE_WARNING("-Wsign-compare" ) |
647 | #if __GNUC_PREREQ(5, 0) |
648 | FOLLY_GNU_DISABLE_WARNING("-Wbool-compare" ) |
649 | #endif |
650 | FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare |
651 | FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare |
652 | |
653 | template <typename RHS, RHS rhs, typename LHS> |
654 | bool less_than_impl(LHS const lhs) { |
655 | // clang-format off |
656 | return |
657 | rhs > std::numeric_limits<LHS>::max() ? true : |
658 | rhs <= std::numeric_limits<LHS>::min() ? false : |
659 | lhs < rhs; |
660 | // clang-format on |
661 | } |
662 | |
663 | template <typename RHS, RHS rhs, typename LHS> |
664 | bool greater_than_impl(LHS const lhs) { |
665 | // clang-format off |
666 | return |
667 | rhs > std::numeric_limits<LHS>::max() ? false : |
668 | rhs < std::numeric_limits<LHS>::min() ? true : |
669 | lhs > rhs; |
670 | // clang-format on |
671 | } |
672 | |
673 | FOLLY_POP_WARNING |
674 | |
675 | } // namespace detail |
676 | |
677 | // same as `x < 0` |
678 | template <typename T> |
679 | constexpr bool is_negative(T x) { |
680 | return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x); |
681 | } |
682 | |
683 | // same as `x <= 0` |
684 | template <typename T> |
685 | constexpr bool is_non_positive(T x) { |
686 | return !x || folly::is_negative(x); |
687 | } |
688 | |
689 | // same as `x > 0` |
690 | template <typename T> |
691 | constexpr bool is_positive(T x) { |
692 | return !is_non_positive(x); |
693 | } |
694 | |
695 | // same as `x >= 0` |
696 | template <typename T> |
697 | constexpr bool is_non_negative(T x) { |
698 | return !x || is_positive(x); |
699 | } |
700 | |
701 | template <typename RHS, RHS rhs, typename LHS> |
702 | bool less_than(LHS const lhs) { |
703 | return detail:: |
704 | less_than_impl<RHS, rhs, typename std::remove_reference<LHS>::type>(lhs); |
705 | } |
706 | |
707 | template <typename RHS, RHS rhs, typename LHS> |
708 | bool greater_than(LHS const lhs) { |
709 | return detail:: |
710 | greater_than_impl<RHS, rhs, typename std::remove_reference<LHS>::type>( |
711 | lhs); |
712 | } |
713 | } // namespace folly |
714 | |
715 | // Assume nothing when compiling with MSVC. |
716 | #ifndef _MSC_VER |
717 | // gcc-5.0 changed string's implementation in libstdc++ to be non-relocatable |
718 | #if !_GLIBCXX_USE_CXX11_ABI |
719 | FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string) |
720 | #endif |
721 | FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector) |
722 | FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque) |
723 | FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr) |
724 | FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr) |
725 | FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function) |
726 | #endif |
727 | |
728 | /* Some combinations of compilers and C++ libraries make __int128 and |
729 | * unsigned __int128 available but do not correctly define their standard type |
730 | * traits. |
731 | * |
732 | * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits |
733 | * here. |
734 | * |
735 | * @author: Phil Willoughby <philwill@fb.com> |
736 | */ |
737 | #if FOLLY_SUPPLY_MISSING_INT128_TRAITS |
738 | FOLLY_NAMESPACE_STD_BEGIN |
739 | template <> |
740 | struct is_arithmetic<__int128> : ::std::true_type {}; |
741 | template <> |
742 | struct is_arithmetic<unsigned __int128> : ::std::true_type {}; |
743 | template <> |
744 | struct is_integral<__int128> : ::std::true_type {}; |
745 | template <> |
746 | struct is_integral<unsigned __int128> : ::std::true_type {}; |
747 | template <> |
748 | struct make_unsigned<__int128> { |
749 | typedef unsigned __int128 type; |
750 | }; |
751 | template <> |
752 | struct make_signed<__int128> { |
753 | typedef __int128 type; |
754 | }; |
755 | template <> |
756 | struct make_unsigned<unsigned __int128> { |
757 | typedef unsigned __int128 type; |
758 | }; |
759 | template <> |
760 | struct make_signed<unsigned __int128> { |
761 | typedef __int128 type; |
762 | }; |
763 | template <> |
764 | struct is_signed<__int128> : ::std::true_type {}; |
765 | template <> |
766 | struct is_unsigned<unsigned __int128> : ::std::true_type {}; |
767 | FOLLY_NAMESPACE_STD_END |
768 | #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS |
769 | |