2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_FUNCTIONAL_BASE
12 #define _LIBCPP_FUNCTIONAL_BASE
15 #include <type_traits>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 template <class _Arg, class _Result>
27 struct _LIBCPP_TYPE_VIS_ONLY unary_function
29 typedef _Arg argument_type;
30 typedef _Result result_type;
33 template <class _Arg1, class _Arg2, class _Result>
34 struct _LIBCPP_TYPE_VIS_ONLY binary_function
36 typedef _Arg1 first_argument_type;
37 typedef _Arg2 second_argument_type;
38 typedef _Result result_type;
41 template <class _Tp> struct _LIBCPP_TYPE_VIS_ONLY hash;
44 struct __has_result_type
47 struct __two {char __lx; char __lxx;};
48 template <class _Up> static __two __test(...);
49 template <class _Up> static char __test(typename _Up::result_type* = 0);
51 static const bool value = sizeof(__test<_Tp>(0)) == 1;
54 #if _LIBCPP_STD_VER > 11
55 template <class _Tp = void>
59 struct _LIBCPP_TYPE_VIS_ONLY less : binary_function<_Tp, _Tp, bool>
61 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
62 bool operator()(const _Tp& __x, const _Tp& __y) const
66 #if _LIBCPP_STD_VER > 11
68 struct _LIBCPP_TYPE_VIS_ONLY less<void>
70 template <class _T1, class _T2>
71 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
72 auto operator()(_T1&& __t, _T2&& __u) const
73 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u)))
74 -> decltype (_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u))
75 { return _VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u); }
76 typedef void is_transparent;
83 inline _LIBCPP_INLINE_VISIBILITY
85 addressof(_Tp& __x) _NOEXCEPT
87 return (_Tp*)&reinterpret_cast<const volatile char&>(__x);
90 #if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
91 // Objective-C++ Automatic Reference Counting uses qualified pointers
92 // that require special addressof() signatures. When
93 // _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
94 // itself is providing these definitions. Otherwise, we provide them.
96 inline _LIBCPP_INLINE_VISIBILITY
98 addressof(__strong _Tp& __x) _NOEXCEPT
103 #ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
105 inline _LIBCPP_INLINE_VISIBILITY
107 addressof(__weak _Tp& __x) _NOEXCEPT
114 inline _LIBCPP_INLINE_VISIBILITY
116 addressof(__autoreleasing _Tp& __x) _NOEXCEPT
122 inline _LIBCPP_INLINE_VISIBILITY
123 __unsafe_unretained _Tp*
124 addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
130 #ifdef _LIBCPP_HAS_NO_VARIADICS
132 #include <__functional_base_03>
134 #else // _LIBCPP_HAS_NO_VARIADICS
136 // __weak_result_type
139 struct __derives_from_unary_function
142 struct __two {char __lx; char __lxx;};
143 static __two __test(...);
144 template <class _Ap, class _Rp>
145 static unary_function<_Ap, _Rp>
146 __test(const volatile unary_function<_Ap, _Rp>*);
148 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
149 typedef decltype(__test((_Tp*)0)) type;
153 struct __derives_from_binary_function
156 struct __two {char __lx; char __lxx;};
157 static __two __test(...);
158 template <class _A1, class _A2, class _Rp>
159 static binary_function<_A1, _A2, _Rp>
160 __test(const volatile binary_function<_A1, _A2, _Rp>*);
162 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
163 typedef decltype(__test((_Tp*)0)) type;
166 template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
167 struct __maybe_derive_from_unary_function // bool is true
168 : public __derives_from_unary_function<_Tp>::type
173 struct __maybe_derive_from_unary_function<_Tp, false>
177 template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
178 struct __maybe_derive_from_binary_function // bool is true
179 : public __derives_from_binary_function<_Tp>::type
184 struct __maybe_derive_from_binary_function<_Tp, false>
188 template <class _Tp, bool = __has_result_type<_Tp>::value>
189 struct __weak_result_type_imp // bool is true
190 : public __maybe_derive_from_unary_function<_Tp>,
191 public __maybe_derive_from_binary_function<_Tp>
193 typedef typename _Tp::result_type result_type;
197 struct __weak_result_type_imp<_Tp, false>
198 : public __maybe_derive_from_unary_function<_Tp>,
199 public __maybe_derive_from_binary_function<_Tp>
204 struct __weak_result_type
205 : public __weak_result_type_imp<_Tp>
212 struct __weak_result_type<_Rp ()>
214 typedef _Rp result_type;
218 struct __weak_result_type<_Rp (&)()>
220 typedef _Rp result_type;
224 struct __weak_result_type<_Rp (*)()>
226 typedef _Rp result_type;
231 template <class _Rp, class _A1>
232 struct __weak_result_type<_Rp (_A1)>
233 : public unary_function<_A1, _Rp>
237 template <class _Rp, class _A1>
238 struct __weak_result_type<_Rp (&)(_A1)>
239 : public unary_function<_A1, _Rp>
243 template <class _Rp, class _A1>
244 struct __weak_result_type<_Rp (*)(_A1)>
245 : public unary_function<_A1, _Rp>
249 template <class _Rp, class _Cp>
250 struct __weak_result_type<_Rp (_Cp::*)()>
251 : public unary_function<_Cp*, _Rp>
255 template <class _Rp, class _Cp>
256 struct __weak_result_type<_Rp (_Cp::*)() const>
257 : public unary_function<const _Cp*, _Rp>
261 template <class _Rp, class _Cp>
262 struct __weak_result_type<_Rp (_Cp::*)() volatile>
263 : public unary_function<volatile _Cp*, _Rp>
267 template <class _Rp, class _Cp>
268 struct __weak_result_type<_Rp (_Cp::*)() const volatile>
269 : public unary_function<const volatile _Cp*, _Rp>
275 template <class _Rp, class _A1, class _A2>
276 struct __weak_result_type<_Rp (_A1, _A2)>
277 : public binary_function<_A1, _A2, _Rp>
281 template <class _Rp, class _A1, class _A2>
282 struct __weak_result_type<_Rp (*)(_A1, _A2)>
283 : public binary_function<_A1, _A2, _Rp>
287 template <class _Rp, class _A1, class _A2>
288 struct __weak_result_type<_Rp (&)(_A1, _A2)>
289 : public binary_function<_A1, _A2, _Rp>
293 template <class _Rp, class _Cp, class _A1>
294 struct __weak_result_type<_Rp (_Cp::*)(_A1)>
295 : public binary_function<_Cp*, _A1, _Rp>
299 template <class _Rp, class _Cp, class _A1>
300 struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
301 : public binary_function<const _Cp*, _A1, _Rp>
305 template <class _Rp, class _Cp, class _A1>
306 struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
307 : public binary_function<volatile _Cp*, _A1, _Rp>
311 template <class _Rp, class _Cp, class _A1>
312 struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
313 : public binary_function<const volatile _Cp*, _A1, _Rp>
317 // 3 or more arguments
319 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
320 struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
322 typedef _Rp result_type;
325 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
326 struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
328 typedef _Rp result_type;
331 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
332 struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
334 typedef _Rp result_type;
337 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
338 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
340 typedef _Rp result_type;
343 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
344 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
346 typedef _Rp result_type;
349 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
350 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
352 typedef _Rp result_type;
355 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
356 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
358 typedef _Rp result_type;
365 template <class _Fp, class _A0, class ..._Args,
367 inline _LIBCPP_INLINE_VISIBILITY
369 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
370 -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
372 return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
375 template <class _Fp, class _A0, class ..._Args,
377 inline _LIBCPP_INLINE_VISIBILITY
379 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
380 -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
382 return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
387 template <class _Fp, class _A0,
389 inline _LIBCPP_INLINE_VISIBILITY
391 __invoke(_Fp&& __f, _A0&& __a0)
392 -> decltype(_VSTD::forward<_A0>(__a0).*__f)
394 return _VSTD::forward<_A0>(__a0).*__f;
397 template <class _Fp, class _A0,
399 inline _LIBCPP_INLINE_VISIBILITY
401 __invoke(_Fp&& __f, _A0&& __a0)
402 -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
404 return (*_VSTD::forward<_A0>(__a0)).*__f;
409 template <class _Fp, class ..._Args>
410 inline _LIBCPP_INLINE_VISIBILITY
412 __invoke(_Fp&& __f, _Args&& ...__args)
413 -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
415 return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
418 template <class _Tp, class ..._Args>
419 struct __invoke_return
421 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
424 template <class _Ret>
425 struct __invoke_void_return_wrapper
427 template <class ..._Args>
428 static _Ret __call(_Args&&... __args)
430 return __invoke(_VSTD::forward<_Args>(__args)...);
435 struct __invoke_void_return_wrapper<void>
437 template <class ..._Args>
438 static void __call(_Args&&... __args)
440 __invoke(_VSTD::forward<_Args>(__args)...);
445 class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
446 : public __weak_result_type<_Tp>
455 // construct/copy/destroy
456 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
457 : __f_(_VSTD::addressof(__f)) {}
458 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
459 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
463 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
464 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
467 template <class... _ArgTypes>
468 _LIBCPP_INLINE_VISIBILITY
469 typename __invoke_of<type&, _ArgTypes...>::type
470 operator() (_ArgTypes&&... __args) const
472 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
476 template <class _Tp> struct __is_reference_wrapper_impl : public false_type {};
477 template <class _Tp> struct __is_reference_wrapper_impl<reference_wrapper<_Tp> > : public true_type {};
478 template <class _Tp> struct __is_reference_wrapper
479 : public __is_reference_wrapper_impl<typename remove_cv<_Tp>::type> {};
482 inline _LIBCPP_INLINE_VISIBILITY
483 reference_wrapper<_Tp>
484 ref(_Tp& __t) _NOEXCEPT
486 return reference_wrapper<_Tp>(__t);
490 inline _LIBCPP_INLINE_VISIBILITY
491 reference_wrapper<_Tp>
492 ref(reference_wrapper<_Tp> __t) _NOEXCEPT
494 return ref(__t.get());
498 inline _LIBCPP_INLINE_VISIBILITY
499 reference_wrapper<const _Tp>
500 cref(const _Tp& __t) _NOEXCEPT
502 return reference_wrapper<const _Tp>(__t);
506 inline _LIBCPP_INLINE_VISIBILITY
507 reference_wrapper<const _Tp>
508 cref(reference_wrapper<_Tp> __t) _NOEXCEPT
510 return cref(__t.get());
513 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
514 #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
516 template <class _Tp> void ref(const _Tp&&) = delete;
517 template <class _Tp> void cref(const _Tp&&) = delete;
519 #else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
521 template <class _Tp> void ref(const _Tp&&);// = delete;
522 template <class _Tp> void cref(const _Tp&&);// = delete;
524 #endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
526 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
528 #endif // _LIBCPP_HAS_NO_VARIADICS
530 #if _LIBCPP_STD_VER > 11
531 template <class _Tp1, class _Tp2 = void>
532 struct __is_transparent
535 struct __two {char __lx; char __lxx;};
536 template <class _Up> static __two __test(...);
537 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
539 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
545 struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
547 #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
548 extern const allocator_arg_t allocator_arg;
550 constexpr allocator_arg_t allocator_arg = allocator_arg_t();
556 struct __has_allocator_type
559 struct __two {char __lx; char __lxx;};
560 template <class _Up> static __two __test(...);
561 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
563 static const bool value = sizeof(__test<_Tp>(0)) == 1;
566 template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
567 struct __uses_allocator
568 : public integral_constant<bool,
569 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
573 template <class _Tp, class _Alloc>
574 struct __uses_allocator<_Tp, _Alloc, false>
579 template <class _Tp, class _Alloc>
580 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
581 : public __uses_allocator<_Tp, _Alloc>
585 #ifndef _LIBCPP_HAS_NO_VARIADICS
587 // allocator construction
589 template <class _Tp, class _Alloc, class ..._Args>
590 struct __uses_alloc_ctor_imp
592 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
593 static const bool __ic =
594 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
595 static const int value = __ua ? 2 - __ic : 0;
598 template <class _Tp, class _Alloc, class ..._Args>
599 struct __uses_alloc_ctor
600 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
603 template <class _Tp, class _Allocator, class... _Args>
604 inline _LIBCPP_INLINE_VISIBILITY
605 void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
607 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
610 template <class _Tp, class _Allocator, class... _Args>
611 inline _LIBCPP_INLINE_VISIBILITY
612 void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
614 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
617 template <class _Tp, class _Allocator, class... _Args>
618 inline _LIBCPP_INLINE_VISIBILITY
619 void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
621 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
624 template <class _Tp, class _Allocator, class... _Args>
625 inline _LIBCPP_INLINE_VISIBILITY
626 void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
628 __user_alloc_construct_impl(
629 __uses_alloc_ctor<_Tp, _Allocator>(),
630 __storage, __a, _VSTD::forward<_Args>(__args)...
633 #endif // _LIBCPP_HAS_NO_VARIADICS
635 _LIBCPP_END_NAMESPACE_STD
637 #endif // _LIBCPP_FUNCTIONAL_BASE