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 { return _VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u); }
74 typedef void is_transparent;
81 inline _LIBCPP_INLINE_VISIBILITY
83 addressof(_Tp& __x) _NOEXCEPT
85 return (_Tp*)&reinterpret_cast<const volatile char&>(__x);
88 #if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
89 // Objective-C++ Automatic Reference Counting uses qualified pointers
90 // that require special addressof() signatures. When
91 // _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
92 // itself is providing these definitions. Otherwise, we provide them.
94 inline _LIBCPP_INLINE_VISIBILITY
96 addressof(__strong _Tp& __x) _NOEXCEPT
101 #ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
103 inline _LIBCPP_INLINE_VISIBILITY
105 addressof(__weak _Tp& __x) _NOEXCEPT
112 inline _LIBCPP_INLINE_VISIBILITY
114 addressof(__autoreleasing _Tp& __x) _NOEXCEPT
120 inline _LIBCPP_INLINE_VISIBILITY
121 __unsafe_unretained _Tp*
122 addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
128 #ifdef _LIBCPP_HAS_NO_VARIADICS
130 #include <__functional_base_03>
132 #else // _LIBCPP_HAS_NO_VARIADICS
134 // __weak_result_type
137 struct __derives_from_unary_function
140 struct __two {char __lx; char __lxx;};
141 static __two __test(...);
142 template <class _Ap, class _Rp>
143 static unary_function<_Ap, _Rp>
144 __test(const volatile unary_function<_Ap, _Rp>*);
146 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
147 typedef decltype(__test((_Tp*)0)) type;
151 struct __derives_from_binary_function
154 struct __two {char __lx; char __lxx;};
155 static __two __test(...);
156 template <class _A1, class _A2, class _Rp>
157 static binary_function<_A1, _A2, _Rp>
158 __test(const volatile binary_function<_A1, _A2, _Rp>*);
160 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
161 typedef decltype(__test((_Tp*)0)) type;
164 template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
165 struct __maybe_derive_from_unary_function // bool is true
166 : public __derives_from_unary_function<_Tp>::type
171 struct __maybe_derive_from_unary_function<_Tp, false>
175 template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
176 struct __maybe_derive_from_binary_function // bool is true
177 : public __derives_from_binary_function<_Tp>::type
182 struct __maybe_derive_from_binary_function<_Tp, false>
186 template <class _Tp, bool = __has_result_type<_Tp>::value>
187 struct __weak_result_type_imp // bool is true
188 : public __maybe_derive_from_unary_function<_Tp>,
189 public __maybe_derive_from_binary_function<_Tp>
191 typedef typename _Tp::result_type result_type;
195 struct __weak_result_type_imp<_Tp, false>
196 : public __maybe_derive_from_unary_function<_Tp>,
197 public __maybe_derive_from_binary_function<_Tp>
202 struct __weak_result_type
203 : public __weak_result_type_imp<_Tp>
210 struct __weak_result_type<_Rp ()>
212 typedef _Rp result_type;
216 struct __weak_result_type<_Rp (&)()>
218 typedef _Rp result_type;
222 struct __weak_result_type<_Rp (*)()>
224 typedef _Rp result_type;
229 template <class _Rp, class _A1>
230 struct __weak_result_type<_Rp (_A1)>
231 : public unary_function<_A1, _Rp>
235 template <class _Rp, class _A1>
236 struct __weak_result_type<_Rp (&)(_A1)>
237 : public unary_function<_A1, _Rp>
241 template <class _Rp, class _A1>
242 struct __weak_result_type<_Rp (*)(_A1)>
243 : public unary_function<_A1, _Rp>
247 template <class _Rp, class _Cp>
248 struct __weak_result_type<_Rp (_Cp::*)()>
249 : public unary_function<_Cp*, _Rp>
253 template <class _Rp, class _Cp>
254 struct __weak_result_type<_Rp (_Cp::*)() const>
255 : public unary_function<const _Cp*, _Rp>
259 template <class _Rp, class _Cp>
260 struct __weak_result_type<_Rp (_Cp::*)() volatile>
261 : public unary_function<volatile _Cp*, _Rp>
265 template <class _Rp, class _Cp>
266 struct __weak_result_type<_Rp (_Cp::*)() const volatile>
267 : public unary_function<const volatile _Cp*, _Rp>
273 template <class _Rp, class _A1, class _A2>
274 struct __weak_result_type<_Rp (_A1, _A2)>
275 : public binary_function<_A1, _A2, _Rp>
279 template <class _Rp, class _A1, class _A2>
280 struct __weak_result_type<_Rp (*)(_A1, _A2)>
281 : public binary_function<_A1, _A2, _Rp>
285 template <class _Rp, class _A1, class _A2>
286 struct __weak_result_type<_Rp (&)(_A1, _A2)>
287 : public binary_function<_A1, _A2, _Rp>
291 template <class _Rp, class _Cp, class _A1>
292 struct __weak_result_type<_Rp (_Cp::*)(_A1)>
293 : public binary_function<_Cp*, _A1, _Rp>
297 template <class _Rp, class _Cp, class _A1>
298 struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
299 : public binary_function<const _Cp*, _A1, _Rp>
303 template <class _Rp, class _Cp, class _A1>
304 struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
305 : public binary_function<volatile _Cp*, _A1, _Rp>
309 template <class _Rp, class _Cp, class _A1>
310 struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
311 : public binary_function<const volatile _Cp*, _A1, _Rp>
315 // 3 or more arguments
317 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
318 struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
320 typedef _Rp result_type;
323 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
324 struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
326 typedef _Rp result_type;
329 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
330 struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
332 typedef _Rp result_type;
335 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
336 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
338 typedef _Rp result_type;
341 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
342 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
344 typedef _Rp result_type;
347 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
348 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
350 typedef _Rp result_type;
353 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
354 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
356 typedef _Rp result_type;
363 template <class _Fp, class _A0, class ..._Args,
365 inline _LIBCPP_INLINE_VISIBILITY
367 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
368 -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
370 return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
373 template <class _Fp, class _A0, class ..._Args,
375 inline _LIBCPP_INLINE_VISIBILITY
377 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
378 -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
380 return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
385 template <class _Fp, class _A0,
387 inline _LIBCPP_INLINE_VISIBILITY
389 __invoke(_Fp&& __f, _A0&& __a0)
390 -> decltype(_VSTD::forward<_A0>(__a0).*__f)
392 return _VSTD::forward<_A0>(__a0).*__f;
395 template <class _Fp, class _A0,
397 inline _LIBCPP_INLINE_VISIBILITY
399 __invoke(_Fp&& __f, _A0&& __a0)
400 -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
402 return (*_VSTD::forward<_A0>(__a0)).*__f;
407 template <class _Fp, class ..._Args>
408 inline _LIBCPP_INLINE_VISIBILITY
410 __invoke(_Fp&& __f, _Args&& ...__args)
411 -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
413 return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
416 template <class _Tp, class ..._Args>
417 struct __invoke_return
419 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
423 class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
424 : public __weak_result_type<_Tp>
433 // construct/copy/destroy
434 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
435 : __f_(_VSTD::addressof(__f)) {}
436 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
437 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
441 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
442 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
445 template <class... _ArgTypes>
446 _LIBCPP_INLINE_VISIBILITY
447 typename __invoke_of<type&, _ArgTypes...>::type
448 operator() (_ArgTypes&&... __args) const
450 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
454 template <class _Tp> struct ____is_reference_wrapper : public false_type {};
455 template <class _Tp> struct ____is_reference_wrapper<reference_wrapper<_Tp> > : public true_type {};
456 template <class _Tp> struct __is_reference_wrapper
457 : public ____is_reference_wrapper<typename remove_cv<_Tp>::type> {};
460 inline _LIBCPP_INLINE_VISIBILITY
461 reference_wrapper<_Tp>
462 ref(_Tp& __t) _NOEXCEPT
464 return reference_wrapper<_Tp>(__t);
468 inline _LIBCPP_INLINE_VISIBILITY
469 reference_wrapper<_Tp>
470 ref(reference_wrapper<_Tp> __t) _NOEXCEPT
472 return ref(__t.get());
476 inline _LIBCPP_INLINE_VISIBILITY
477 reference_wrapper<const _Tp>
478 cref(const _Tp& __t) _NOEXCEPT
480 return reference_wrapper<const _Tp>(__t);
484 inline _LIBCPP_INLINE_VISIBILITY
485 reference_wrapper<const _Tp>
486 cref(reference_wrapper<_Tp> __t) _NOEXCEPT
488 return cref(__t.get());
491 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
492 #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
494 template <class _Tp> void ref(const _Tp&&) = delete;
495 template <class _Tp> void cref(const _Tp&&) = delete;
497 #else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
499 template <class _Tp> void ref(const _Tp&&);// = delete;
500 template <class _Tp> void cref(const _Tp&&);// = delete;
502 #endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
504 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
506 #endif // _LIBCPP_HAS_NO_VARIADICS
508 #if _LIBCPP_STD_VER > 11
509 template <class _Tp1, class _Tp2 = void>
510 struct __is_transparent
513 struct __two {char __lx; char __lxx;};
514 template <class _Up> static __two __test(...);
515 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
517 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
523 struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
525 #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
526 extern const allocator_arg_t allocator_arg;
528 constexpr allocator_arg_t allocator_arg = allocator_arg_t();
534 struct __has_allocator_type
537 struct __two {char __lx; char __lxx;};
538 template <class _Up> static __two __test(...);
539 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
541 static const bool value = sizeof(__test<_Tp>(0)) == 1;
544 template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
545 struct __uses_allocator
546 : public integral_constant<bool,
547 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
551 template <class _Tp, class _Alloc>
552 struct __uses_allocator<_Tp, _Alloc, false>
557 template <class _Tp, class _Alloc>
558 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
559 : public __uses_allocator<_Tp, _Alloc>
563 #ifndef _LIBCPP_HAS_NO_VARIADICS
565 // allocator construction
567 template <class _Tp, class _Alloc, class ..._Args>
568 struct __uses_alloc_ctor_imp
570 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
571 static const bool __ic =
572 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
573 static const int value = __ua ? 2 - __ic : 0;
576 template <class _Tp, class _Alloc, class ..._Args>
577 struct __uses_alloc_ctor
578 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
581 template <class _Tp, class _Allocator, class... _Args>
582 inline _LIBCPP_INLINE_VISIBILITY
583 void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
585 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
588 template <class _Tp, class _Allocator, class... _Args>
589 inline _LIBCPP_INLINE_VISIBILITY
590 void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
592 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
595 template <class _Tp, class _Allocator, class... _Args>
596 inline _LIBCPP_INLINE_VISIBILITY
597 void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
599 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
602 template <class _Tp, class _Allocator, class... _Args>
603 inline _LIBCPP_INLINE_VISIBILITY
604 void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
606 __user_alloc_construct_impl(
607 __uses_alloc_ctor<_Tp, _Allocator>(),
608 __storage, __a, _VSTD::forward<_Args>(__args)...
611 #endif // _LIBCPP_HAS_NO_VARIADICS
613 _LIBCPP_END_NAMESPACE_STD
615 #endif // _LIBCPP_FUNCTIONAL_BASE