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;
42 struct __has_result_type
45 struct __two {char __lx; char __lxx;};
46 template <class _Up> static __two __test(...);
47 template <class _Up> static char __test(typename _Up::result_type* = 0);
49 static const bool value = sizeof(__test<_Tp>(0)) == 1;
52 #if _LIBCPP_STD_VER > 11
53 template <class _Tp = void>
57 struct _LIBCPP_TYPE_VIS_ONLY less : binary_function<_Tp, _Tp, bool>
59 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
60 bool operator()(const _Tp& __x, const _Tp& __y) const
64 #if _LIBCPP_STD_VER > 11
66 struct _LIBCPP_TYPE_VIS_ONLY less<void>
68 template <class _T1, class _T2>
69 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
70 auto operator()(_T1&& __t, _T2&& __u) const
71 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u)))
72 -> decltype (_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u))
73 { return _VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u); }
74 typedef void is_transparent;
81 struct __derives_from_unary_function
84 struct __two {char __lx; char __lxx;};
85 static __two __test(...);
86 template <class _Ap, class _Rp>
87 static unary_function<_Ap, _Rp>
88 __test(const volatile unary_function<_Ap, _Rp>*);
90 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
91 typedef decltype(__test((_Tp*)0)) type;
95 struct __derives_from_binary_function
98 struct __two {char __lx; char __lxx;};
99 static __two __test(...);
100 template <class _A1, class _A2, class _Rp>
101 static binary_function<_A1, _A2, _Rp>
102 __test(const volatile binary_function<_A1, _A2, _Rp>*);
104 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
105 typedef decltype(__test((_Tp*)0)) type;
108 template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
109 struct __maybe_derive_from_unary_function // bool is true
110 : public __derives_from_unary_function<_Tp>::type
115 struct __maybe_derive_from_unary_function<_Tp, false>
119 template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
120 struct __maybe_derive_from_binary_function // bool is true
121 : public __derives_from_binary_function<_Tp>::type
126 struct __maybe_derive_from_binary_function<_Tp, false>
130 template <class _Tp, bool = __has_result_type<_Tp>::value>
131 struct __weak_result_type_imp // bool is true
132 : public __maybe_derive_from_unary_function<_Tp>,
133 public __maybe_derive_from_binary_function<_Tp>
135 typedef typename _Tp::result_type result_type;
139 struct __weak_result_type_imp<_Tp, false>
140 : public __maybe_derive_from_unary_function<_Tp>,
141 public __maybe_derive_from_binary_function<_Tp>
146 struct __weak_result_type
147 : public __weak_result_type_imp<_Tp>
154 struct __weak_result_type<_Rp ()>
156 typedef _Rp result_type;
160 struct __weak_result_type<_Rp (&)()>
162 typedef _Rp result_type;
166 struct __weak_result_type<_Rp (*)()>
168 typedef _Rp result_type;
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 _A1>
186 struct __weak_result_type<_Rp (*)(_A1)>
187 : public unary_function<_A1, _Rp>
191 template <class _Rp, class _Cp>
192 struct __weak_result_type<_Rp (_Cp::*)()>
193 : public unary_function<_Cp*, _Rp>
197 template <class _Rp, class _Cp>
198 struct __weak_result_type<_Rp (_Cp::*)() const>
199 : public unary_function<const _Cp*, _Rp>
203 template <class _Rp, class _Cp>
204 struct __weak_result_type<_Rp (_Cp::*)() volatile>
205 : public unary_function<volatile _Cp*, _Rp>
209 template <class _Rp, class _Cp>
210 struct __weak_result_type<_Rp (_Cp::*)() const volatile>
211 : public unary_function<const volatile _Cp*, _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 _A1, class _A2>
230 struct __weak_result_type<_Rp (&)(_A1, _A2)>
231 : public binary_function<_A1, _A2, _Rp>
235 template <class _Rp, class _Cp, class _A1>
236 struct __weak_result_type<_Rp (_Cp::*)(_A1)>
237 : public binary_function<_Cp*, _A1, _Rp>
241 template <class _Rp, class _Cp, class _A1>
242 struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
243 : public binary_function<const _Cp*, _A1, _Rp>
247 template <class _Rp, class _Cp, class _A1>
248 struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
249 : public binary_function<volatile _Cp*, _A1, _Rp>
253 template <class _Rp, class _Cp, class _A1>
254 struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
255 : public binary_function<const volatile _Cp*, _A1, _Rp>
260 #ifndef _LIBCPP_HAS_NO_VARIADICS
261 // 3 or more arguments
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 _A1, class _A2, class _A3, class ..._A4>
276 struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
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...)>
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...) const>
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...) volatile>
296 typedef _Rp result_type;
299 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
300 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
302 typedef _Rp result_type;
305 #endif // _LIBCPP_HAS_NO_VARIADICS
307 #ifndef _LIBCPP_CXX03_LANG
309 template <class _Tp, class ..._Args>
310 struct __invoke_return
312 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
315 #else // defined(_LIBCPP_CXX03_LANG)
317 #include <__functional_base_03>
319 #endif // !defined(_LIBCPP_CXX03_LANG)
322 template <class _Ret>
323 struct __invoke_void_return_wrapper
325 #ifndef _LIBCPP_HAS_NO_VARIADICS
326 template <class ..._Args>
327 static _Ret __call(_Args&&... __args) {
328 return __invoke(_VSTD::forward<_Args>(__args)...);
332 static _Ret __call(_Fn __f) {
333 return __invoke(__f);
336 template <class _Fn, class _A0>
337 static _Ret __call(_Fn __f, _A0& __a0) {
338 return __invoke(__f, __a0);
341 template <class _Fn, class _A0, class _A1>
342 static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1) {
343 return __invoke(__f, __a0, __a1);
346 template <class _Fn, class _A0, class _A1, class _A2>
347 static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2){
348 return __invoke(__f, __a0, __a1, __a2);
354 struct __invoke_void_return_wrapper<void>
356 #ifndef _LIBCPP_HAS_NO_VARIADICS
357 template <class ..._Args>
358 static void __call(_Args&&... __args) {
359 __invoke(_VSTD::forward<_Args>(__args)...);
363 static void __call(_Fn __f) {
367 template <class _Fn, class _A0>
368 static void __call(_Fn __f, _A0& __a0) {
372 template <class _Fn, class _A0, class _A1>
373 static void __call(_Fn __f, _A0& __a0, _A1& __a1) {
374 __invoke(__f, __a0, __a1);
377 template <class _Fn, class _A0, class _A1, class _A2>
378 static void __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2) {
379 __invoke(__f, __a0, __a1, __a2);
385 class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
386 : public __weak_result_type<_Tp>
395 // construct/copy/destroy
396 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
397 : __f_(_VSTD::addressof(__f)) {}
398 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
399 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
403 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
404 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
406 #ifndef _LIBCPP_HAS_NO_VARIADICS
408 template <class... _ArgTypes>
409 _LIBCPP_INLINE_VISIBILITY
410 typename __invoke_of<type&, _ArgTypes...>::type
411 operator() (_ArgTypes&&... __args) const {
412 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
416 _LIBCPP_INLINE_VISIBILITY
417 typename __invoke_return<type>::type
418 operator() () const {
419 return __invoke(get());
423 _LIBCPP_INLINE_VISIBILITY
424 typename __invoke_return0<type, _A0>::type
425 operator() (_A0& __a0) const {
426 return __invoke(get(), __a0);
430 _LIBCPP_INLINE_VISIBILITY
431 typename __invoke_return0<type, _A0 const>::type
432 operator() (_A0 const& __a0) const {
433 return __invoke(get(), __a0);
436 template <class _A0, class _A1>
437 _LIBCPP_INLINE_VISIBILITY
438 typename __invoke_return1<type, _A0, _A1>::type
439 operator() (_A0& __a0, _A1& __a1) const {
440 return __invoke(get(), __a0, __a1);
443 template <class _A0, class _A1>
444 _LIBCPP_INLINE_VISIBILITY
445 typename __invoke_return1<type, _A0 const, _A1>::type
446 operator() (_A0 const& __a0, _A1& __a1) const {
447 return __invoke(get(), __a0, __a1);
450 template <class _A0, class _A1>
451 _LIBCPP_INLINE_VISIBILITY
452 typename __invoke_return1<type, _A0, _A1 const>::type
453 operator() (_A0& __a0, _A1 const& __a1) const {
454 return __invoke(get(), __a0, __a1);
457 template <class _A0, class _A1>
458 _LIBCPP_INLINE_VISIBILITY
459 typename __invoke_return1<type, _A0 const, _A1 const>::type
460 operator() (_A0 const& __a0, _A1 const& __a1) const {
461 return __invoke(get(), __a0, __a1);
464 template <class _A0, class _A1, class _A2>
465 _LIBCPP_INLINE_VISIBILITY
466 typename __invoke_return2<type, _A0, _A1, _A2>::type
467 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
468 return __invoke(get(), __a0, __a1, __a2);
471 template <class _A0, class _A1, class _A2>
472 _LIBCPP_INLINE_VISIBILITY
473 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
474 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
475 return __invoke(get(), __a0, __a1, __a2);
478 template <class _A0, class _A1, class _A2>
479 _LIBCPP_INLINE_VISIBILITY
480 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
481 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
482 return __invoke(get(), __a0, __a1, __a2);
485 template <class _A0, class _A1, class _A2>
486 _LIBCPP_INLINE_VISIBILITY
487 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
488 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
489 return __invoke(get(), __a0, __a1, __a2);
492 template <class _A0, class _A1, class _A2>
493 _LIBCPP_INLINE_VISIBILITY
494 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
495 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
496 return __invoke(get(), __a0, __a1, __a2);
499 template <class _A0, class _A1, class _A2>
500 _LIBCPP_INLINE_VISIBILITY
501 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
502 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
503 return __invoke(get(), __a0, __a1, __a2);
506 template <class _A0, class _A1, class _A2>
507 _LIBCPP_INLINE_VISIBILITY
508 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
509 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
510 return __invoke(get(), __a0, __a1, __a2);
513 template <class _A0, class _A1, class _A2>
514 _LIBCPP_INLINE_VISIBILITY
515 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
516 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
517 return __invoke(get(), __a0, __a1, __a2);
519 #endif // _LIBCPP_HAS_NO_VARIADICS
524 inline _LIBCPP_INLINE_VISIBILITY
525 reference_wrapper<_Tp>
526 ref(_Tp& __t) _NOEXCEPT
528 return reference_wrapper<_Tp>(__t);
532 inline _LIBCPP_INLINE_VISIBILITY
533 reference_wrapper<_Tp>
534 ref(reference_wrapper<_Tp> __t) _NOEXCEPT
536 return ref(__t.get());
540 inline _LIBCPP_INLINE_VISIBILITY
541 reference_wrapper<const _Tp>
542 cref(const _Tp& __t) _NOEXCEPT
544 return reference_wrapper<const _Tp>(__t);
548 inline _LIBCPP_INLINE_VISIBILITY
549 reference_wrapper<const _Tp>
550 cref(reference_wrapper<_Tp> __t) _NOEXCEPT
552 return cref(__t.get());
555 #ifndef _LIBCPP_HAS_NO_VARIADICS
556 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
557 #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
559 template <class _Tp> void ref(const _Tp&&) = delete;
560 template <class _Tp> void cref(const _Tp&&) = delete;
562 #else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
564 template <class _Tp> void ref(const _Tp&&);// = delete;
565 template <class _Tp> void cref(const _Tp&&);// = delete;
567 #endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
569 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
571 #endif // _LIBCPP_HAS_NO_VARIADICS
573 #if _LIBCPP_STD_VER > 11
574 template <class _Tp1, class _Tp2 = void>
575 struct __is_transparent
578 struct __two {char __lx; char __lxx;};
579 template <class _Up> static __two __test(...);
580 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
582 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
588 struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
590 #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
591 extern const allocator_arg_t allocator_arg;
593 constexpr allocator_arg_t allocator_arg = allocator_arg_t();
599 struct __has_allocator_type
602 struct __two {char __lx; char __lxx;};
603 template <class _Up> static __two __test(...);
604 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
606 static const bool value = sizeof(__test<_Tp>(0)) == 1;
609 template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
610 struct __uses_allocator
611 : public integral_constant<bool,
612 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
616 template <class _Tp, class _Alloc>
617 struct __uses_allocator<_Tp, _Alloc, false>
622 template <class _Tp, class _Alloc>
623 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
624 : public __uses_allocator<_Tp, _Alloc>
628 #ifndef _LIBCPP_HAS_NO_VARIADICS
630 // allocator construction
632 template <class _Tp, class _Alloc, class ..._Args>
633 struct __uses_alloc_ctor_imp
635 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
636 static const bool __ic =
637 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
638 static const int value = __ua ? 2 - __ic : 0;
641 template <class _Tp, class _Alloc, class ..._Args>
642 struct __uses_alloc_ctor
643 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
646 template <class _Tp, class _Allocator, class... _Args>
647 inline _LIBCPP_INLINE_VISIBILITY
648 void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
650 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
653 template <class _Tp, class _Allocator, class... _Args>
654 inline _LIBCPP_INLINE_VISIBILITY
655 void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
657 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
660 template <class _Tp, class _Allocator, class... _Args>
661 inline _LIBCPP_INLINE_VISIBILITY
662 void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
664 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
667 template <class _Tp, class _Allocator, class... _Args>
668 inline _LIBCPP_INLINE_VISIBILITY
669 void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
671 __user_alloc_construct_impl(
672 __uses_alloc_ctor<_Tp, _Allocator>(),
673 __storage, __a, _VSTD::forward<_Args>(__args)...
676 #endif // _LIBCPP_HAS_NO_VARIADICS
678 _LIBCPP_END_NAMESPACE_STD
680 #endif // _LIBCPP_FUNCTIONAL_BASE