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>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #pragma GCC system_header
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 template <class _Arg1, class _Arg2, class _Result>
28 struct _LIBCPP_TEMPLATE_VIS binary_function
30 typedef _Arg1 first_argument_type;
31 typedef _Arg2 second_argument_type;
32 typedef _Result result_type;
36 struct __has_result_type
39 struct __two {char __lx; char __lxx;};
40 template <class _Up> static __two __test(...);
41 template <class _Up> static char __test(typename _Up::result_type* = 0);
43 static const bool value = sizeof(__test<_Tp>(0)) == 1;
46 #if _LIBCPP_STD_VER > 11
47 template <class _Tp = void>
51 struct _LIBCPP_TEMPLATE_VIS less : binary_function<_Tp, _Tp, bool>
53 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
54 bool operator()(const _Tp& __x, const _Tp& __y) const
58 #if _LIBCPP_STD_VER > 11
60 struct _LIBCPP_TEMPLATE_VIS less<void>
62 template <class _T1, class _T2>
63 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
64 auto operator()(_T1&& __t, _T2&& __u) const
65 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u)))
66 -> decltype (_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u))
67 { return _VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u); }
68 typedef void is_transparent;
75 struct __derives_from_unary_function
78 struct __two {char __lx; char __lxx;};
79 static __two __test(...);
80 template <class _Ap, class _Rp>
81 static unary_function<_Ap, _Rp>
82 __test(const volatile unary_function<_Ap, _Rp>*);
84 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
85 typedef decltype(__test((_Tp*)0)) type;
89 struct __derives_from_binary_function
92 struct __two {char __lx; char __lxx;};
93 static __two __test(...);
94 template <class _A1, class _A2, class _Rp>
95 static binary_function<_A1, _A2, _Rp>
96 __test(const volatile binary_function<_A1, _A2, _Rp>*);
98 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
99 typedef decltype(__test((_Tp*)0)) type;
102 template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
103 struct __maybe_derive_from_unary_function // bool is true
104 : public __derives_from_unary_function<_Tp>::type
109 struct __maybe_derive_from_unary_function<_Tp, false>
113 template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
114 struct __maybe_derive_from_binary_function // bool is true
115 : public __derives_from_binary_function<_Tp>::type
120 struct __maybe_derive_from_binary_function<_Tp, false>
124 template <class _Tp, bool = __has_result_type<_Tp>::value>
125 struct __weak_result_type_imp // bool is true
126 : public __maybe_derive_from_unary_function<_Tp>,
127 public __maybe_derive_from_binary_function<_Tp>
129 typedef typename _Tp::result_type result_type;
133 struct __weak_result_type_imp<_Tp, false>
134 : public __maybe_derive_from_unary_function<_Tp>,
135 public __maybe_derive_from_binary_function<_Tp>
140 struct __weak_result_type
141 : public __weak_result_type_imp<_Tp>
148 struct __weak_result_type<_Rp ()>
150 typedef _Rp result_type;
154 struct __weak_result_type<_Rp (&)()>
156 typedef _Rp result_type;
160 struct __weak_result_type<_Rp (*)()>
162 typedef _Rp result_type;
167 template <class _Rp, class _A1>
168 struct __weak_result_type<_Rp (_A1)>
169 : public unary_function<_A1, _Rp>
173 template <class _Rp, class _A1>
174 struct __weak_result_type<_Rp (&)(_A1)>
175 : public unary_function<_A1, _Rp>
179 template <class _Rp, class _A1>
180 struct __weak_result_type<_Rp (*)(_A1)>
181 : public unary_function<_A1, _Rp>
185 template <class _Rp, class _Cp>
186 struct __weak_result_type<_Rp (_Cp::*)()>
187 : public unary_function<_Cp*, _Rp>
191 template <class _Rp, class _Cp>
192 struct __weak_result_type<_Rp (_Cp::*)() const>
193 : public unary_function<const _Cp*, _Rp>
197 template <class _Rp, class _Cp>
198 struct __weak_result_type<_Rp (_Cp::*)() volatile>
199 : public unary_function<volatile _Cp*, _Rp>
203 template <class _Rp, class _Cp>
204 struct __weak_result_type<_Rp (_Cp::*)() const volatile>
205 : public unary_function<const volatile _Cp*, _Rp>
211 template <class _Rp, class _A1, class _A2>
212 struct __weak_result_type<_Rp (_A1, _A2)>
213 : public binary_function<_A1, _A2, _Rp>
217 template <class _Rp, class _A1, class _A2>
218 struct __weak_result_type<_Rp (*)(_A1, _A2)>
219 : public binary_function<_A1, _A2, _Rp>
223 template <class _Rp, class _A1, class _A2>
224 struct __weak_result_type<_Rp (&)(_A1, _A2)>
225 : public binary_function<_A1, _A2, _Rp>
229 template <class _Rp, class _Cp, class _A1>
230 struct __weak_result_type<_Rp (_Cp::*)(_A1)>
231 : public binary_function<_Cp*, _A1, _Rp>
235 template <class _Rp, class _Cp, class _A1>
236 struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
237 : public binary_function<const _Cp*, _A1, _Rp>
241 template <class _Rp, class _Cp, class _A1>
242 struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
243 : public binary_function<volatile _Cp*, _A1, _Rp>
247 template <class _Rp, class _Cp, class _A1>
248 struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
249 : public binary_function<const volatile _Cp*, _A1, _Rp>
254 #ifndef _LIBCPP_CXX03_LANG
255 // 3 or more arguments
257 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
258 struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
260 typedef _Rp result_type;
263 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
264 struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
266 typedef _Rp result_type;
269 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
270 struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
272 typedef _Rp result_type;
275 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
276 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
278 typedef _Rp result_type;
281 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
282 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
284 typedef _Rp result_type;
287 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
288 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
290 typedef _Rp result_type;
293 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
294 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
296 typedef _Rp result_type;
299 template <class _Tp, class ..._Args>
300 struct __invoke_return
302 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
305 #else // defined(_LIBCPP_CXX03_LANG)
307 #include <__functional_base_03>
309 #endif // !defined(_LIBCPP_CXX03_LANG)
312 template <class _Ret>
313 struct __invoke_void_return_wrapper
315 #ifndef _LIBCPP_CXX03_LANG
316 template <class ..._Args>
317 static _Ret __call(_Args&&... __args) {
318 return __invoke(_VSTD::forward<_Args>(__args)...);
322 static _Ret __call(_Fn __f) {
323 return __invoke(__f);
326 template <class _Fn, class _A0>
327 static _Ret __call(_Fn __f, _A0& __a0) {
328 return __invoke(__f, __a0);
331 template <class _Fn, class _A0, class _A1>
332 static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1) {
333 return __invoke(__f, __a0, __a1);
336 template <class _Fn, class _A0, class _A1, class _A2>
337 static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2){
338 return __invoke(__f, __a0, __a1, __a2);
344 struct __invoke_void_return_wrapper<void>
346 #ifndef _LIBCPP_CXX03_LANG
347 template <class ..._Args>
348 static void __call(_Args&&... __args) {
349 __invoke(_VSTD::forward<_Args>(__args)...);
353 static void __call(_Fn __f) {
357 template <class _Fn, class _A0>
358 static void __call(_Fn __f, _A0& __a0) {
362 template <class _Fn, class _A0, class _A1>
363 static void __call(_Fn __f, _A0& __a0, _A1& __a1) {
364 __invoke(__f, __a0, __a1);
367 template <class _Fn, class _A0, class _A1, class _A2>
368 static void __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2) {
369 __invoke(__f, __a0, __a1, __a2);
375 class _LIBCPP_TEMPLATE_VIS reference_wrapper
376 : public __weak_result_type<_Tp>
385 // construct/copy/destroy
386 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
387 : __f_(_VSTD::addressof(__f)) {}
388 #ifndef _LIBCPP_CXX03_LANG
389 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
393 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
394 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
396 #ifndef _LIBCPP_CXX03_LANG
398 template <class... _ArgTypes>
399 _LIBCPP_INLINE_VISIBILITY
400 typename __invoke_of<type&, _ArgTypes...>::type
401 operator() (_ArgTypes&&... __args) const {
402 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
406 _LIBCPP_INLINE_VISIBILITY
407 typename __invoke_return<type>::type
408 operator() () const {
409 return __invoke(get());
413 _LIBCPP_INLINE_VISIBILITY
414 typename __invoke_return0<type, _A0>::type
415 operator() (_A0& __a0) const {
416 return __invoke(get(), __a0);
420 _LIBCPP_INLINE_VISIBILITY
421 typename __invoke_return0<type, _A0 const>::type
422 operator() (_A0 const& __a0) const {
423 return __invoke(get(), __a0);
426 template <class _A0, class _A1>
427 _LIBCPP_INLINE_VISIBILITY
428 typename __invoke_return1<type, _A0, _A1>::type
429 operator() (_A0& __a0, _A1& __a1) const {
430 return __invoke(get(), __a0, __a1);
433 template <class _A0, class _A1>
434 _LIBCPP_INLINE_VISIBILITY
435 typename __invoke_return1<type, _A0 const, _A1>::type
436 operator() (_A0 const& __a0, _A1& __a1) const {
437 return __invoke(get(), __a0, __a1);
440 template <class _A0, class _A1>
441 _LIBCPP_INLINE_VISIBILITY
442 typename __invoke_return1<type, _A0, _A1 const>::type
443 operator() (_A0& __a0, _A1 const& __a1) const {
444 return __invoke(get(), __a0, __a1);
447 template <class _A0, class _A1>
448 _LIBCPP_INLINE_VISIBILITY
449 typename __invoke_return1<type, _A0 const, _A1 const>::type
450 operator() (_A0 const& __a0, _A1 const& __a1) const {
451 return __invoke(get(), __a0, __a1);
454 template <class _A0, class _A1, class _A2>
455 _LIBCPP_INLINE_VISIBILITY
456 typename __invoke_return2<type, _A0, _A1, _A2>::type
457 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
458 return __invoke(get(), __a0, __a1, __a2);
461 template <class _A0, class _A1, class _A2>
462 _LIBCPP_INLINE_VISIBILITY
463 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
464 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
465 return __invoke(get(), __a0, __a1, __a2);
468 template <class _A0, class _A1, class _A2>
469 _LIBCPP_INLINE_VISIBILITY
470 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
471 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
472 return __invoke(get(), __a0, __a1, __a2);
475 template <class _A0, class _A1, class _A2>
476 _LIBCPP_INLINE_VISIBILITY
477 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
478 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
479 return __invoke(get(), __a0, __a1, __a2);
482 template <class _A0, class _A1, class _A2>
483 _LIBCPP_INLINE_VISIBILITY
484 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
485 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
486 return __invoke(get(), __a0, __a1, __a2);
489 template <class _A0, class _A1, class _A2>
490 _LIBCPP_INLINE_VISIBILITY
491 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
492 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
493 return __invoke(get(), __a0, __a1, __a2);
496 template <class _A0, class _A1, class _A2>
497 _LIBCPP_INLINE_VISIBILITY
498 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
499 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
500 return __invoke(get(), __a0, __a1, __a2);
503 template <class _A0, class _A1, class _A2>
504 _LIBCPP_INLINE_VISIBILITY
505 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
506 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
507 return __invoke(get(), __a0, __a1, __a2);
509 #endif // _LIBCPP_CXX03_LANG
514 inline _LIBCPP_INLINE_VISIBILITY
515 reference_wrapper<_Tp>
516 ref(_Tp& __t) _NOEXCEPT
518 return reference_wrapper<_Tp>(__t);
522 inline _LIBCPP_INLINE_VISIBILITY
523 reference_wrapper<_Tp>
524 ref(reference_wrapper<_Tp> __t) _NOEXCEPT
526 return ref(__t.get());
530 inline _LIBCPP_INLINE_VISIBILITY
531 reference_wrapper<const _Tp>
532 cref(const _Tp& __t) _NOEXCEPT
534 return reference_wrapper<const _Tp>(__t);
538 inline _LIBCPP_INLINE_VISIBILITY
539 reference_wrapper<const _Tp>
540 cref(reference_wrapper<_Tp> __t) _NOEXCEPT
542 return cref(__t.get());
545 #ifndef _LIBCPP_CXX03_LANG
546 template <class _Tp> void ref(const _Tp&&) = delete;
547 template <class _Tp> void cref(const _Tp&&) = delete;
550 #if _LIBCPP_STD_VER > 11
551 template <class _Tp1, class _Tp2 = void>
552 struct __is_transparent
555 struct __two {char __lx; char __lxx;};
556 template <class _Up> static __two __test(...);
557 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
559 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
565 struct _LIBCPP_TEMPLATE_VIS allocator_arg_t { };
567 #if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_MEMORY)
568 extern const allocator_arg_t allocator_arg;
570 constexpr allocator_arg_t allocator_arg = allocator_arg_t();
576 struct __has_allocator_type
579 struct __two {char __lx; char __lxx;};
580 template <class _Up> static __two __test(...);
581 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
583 static const bool value = sizeof(__test<_Tp>(0)) == 1;
586 template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
587 struct __uses_allocator
588 : public integral_constant<bool,
589 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
593 template <class _Tp, class _Alloc>
594 struct __uses_allocator<_Tp, _Alloc, false>
599 template <class _Tp, class _Alloc>
600 struct _LIBCPP_TEMPLATE_VIS uses_allocator
601 : public __uses_allocator<_Tp, _Alloc>
605 #if _LIBCPP_STD_VER > 14
606 template <class _Tp, class _Alloc>
607 constexpr size_t uses_allocator_v = uses_allocator<_Tp, _Alloc>::value;
610 #ifndef _LIBCPP_CXX03_LANG
612 // allocator construction
614 template <class _Tp, class _Alloc, class ..._Args>
615 struct __uses_alloc_ctor_imp
617 typedef typename __uncvref<_Alloc>::type _RawAlloc;
618 static const bool __ua = uses_allocator<_Tp, _RawAlloc>::value;
619 static const bool __ic =
620 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
621 static const int value = __ua ? 2 - __ic : 0;
624 template <class _Tp, class _Alloc, class ..._Args>
625 struct __uses_alloc_ctor
626 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
629 template <class _Tp, class _Allocator, class... _Args>
630 inline _LIBCPP_INLINE_VISIBILITY
631 void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
633 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
636 // FIXME: This should have a version which takes a non-const alloc.
637 template <class _Tp, class _Allocator, class... _Args>
638 inline _LIBCPP_INLINE_VISIBILITY
639 void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
641 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
644 // FIXME: This should have a version which takes a non-const alloc.
645 template <class _Tp, class _Allocator, class... _Args>
646 inline _LIBCPP_INLINE_VISIBILITY
647 void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
649 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
652 // FIXME: Theis should have a version which takes a non-const alloc.
653 template <class _Tp, class _Allocator, class... _Args>
654 inline _LIBCPP_INLINE_VISIBILITY
655 void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
657 __user_alloc_construct_impl(
658 __uses_alloc_ctor<_Tp, _Allocator>(),
659 __storage, __a, _VSTD::forward<_Args>(__args)...
662 #endif // _LIBCPP_CXX03_LANG
664 _LIBCPP_END_NAMESPACE_STD
666 #endif // _LIBCPP_FUNCTIONAL_BASE