1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP_FUNCTIONAL_BASE 12#define _LIBCPP_FUNCTIONAL_BASE 13 14#include <__config> 15#include <type_traits> 16#include <typeinfo> 17#include <exception> 18#include <new> 19 20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 21#pragma GCC system_header 22#endif 23 24_LIBCPP_BEGIN_NAMESPACE_STD 25 26template <class _Arg, class _Result> 27struct _LIBCPP_TYPE_VIS_ONLY unary_function 28{ 29 typedef _Arg argument_type; 30 typedef _Result result_type; 31}; 32 33template <class _Arg1, class _Arg2, class _Result> 34struct _LIBCPP_TYPE_VIS_ONLY binary_function 35{ 36 typedef _Arg1 first_argument_type; 37 typedef _Arg2 second_argument_type; 38 typedef _Result result_type; 39}; 40 41template <class _Tp> struct _LIBCPP_TYPE_VIS_ONLY hash; 42 43template <class _Tp> 44struct __has_result_type 45{ 46private: 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); 50public: 51 static const bool value = sizeof(__test<_Tp>(0)) == 1; 52}; 53 54#if _LIBCPP_STD_VER > 11 55template <class _Tp = void> 56#else 57template <class _Tp> 58#endif 59struct _LIBCPP_TYPE_VIS_ONLY less : binary_function<_Tp, _Tp, bool> 60{ 61 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY 62 bool operator()(const _Tp& __x, const _Tp& __y) const 63 {return __x < __y;} 64}; 65 66#if _LIBCPP_STD_VER > 11 67template <> 68struct _LIBCPP_TYPE_VIS_ONLY less<void> 69{ 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; 77}; 78#endif 79 80// addressof 81 82template <class _Tp> 83inline _LIBCPP_INLINE_VISIBILITY 84_Tp* 85addressof(_Tp& __x) _NOEXCEPT 86{ 87 return (_Tp*)&reinterpret_cast<const volatile char&>(__x); 88} 89 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. 95template <class _Tp> 96inline _LIBCPP_INLINE_VISIBILITY 97__strong _Tp* 98addressof(__strong _Tp& __x) _NOEXCEPT 99{ 100 return &__x; 101} 102 103#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK 104template <class _Tp> 105inline _LIBCPP_INLINE_VISIBILITY 106__weak _Tp* 107addressof(__weak _Tp& __x) _NOEXCEPT 108{ 109 return &__x; 110} 111#endif 112 113template <class _Tp> 114inline _LIBCPP_INLINE_VISIBILITY 115__autoreleasing _Tp* 116addressof(__autoreleasing _Tp& __x) _NOEXCEPT 117{ 118 return &__x; 119} 120 121template <class _Tp> 122inline _LIBCPP_INLINE_VISIBILITY 123__unsafe_unretained _Tp* 124addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT 125{ 126 return &__x; 127} 128#endif 129 130#ifdef _LIBCPP_HAS_NO_VARIADICS 131 132#include <__functional_base_03> 133 134#else // _LIBCPP_HAS_NO_VARIADICS 135 136// __weak_result_type 137 138template <class _Tp> 139struct __derives_from_unary_function 140{ 141private: 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>*); 147public: 148 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value; 149 typedef decltype(__test((_Tp*)0)) type; 150}; 151 152template <class _Tp> 153struct __derives_from_binary_function 154{ 155private: 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>*); 161public: 162 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value; 163 typedef decltype(__test((_Tp*)0)) type; 164}; 165 166template <class _Tp, bool = __derives_from_unary_function<_Tp>::value> 167struct __maybe_derive_from_unary_function // bool is true 168 : public __derives_from_unary_function<_Tp>::type 169{ 170}; 171 172template <class _Tp> 173struct __maybe_derive_from_unary_function<_Tp, false> 174{ 175}; 176 177template <class _Tp, bool = __derives_from_binary_function<_Tp>::value> 178struct __maybe_derive_from_binary_function // bool is true 179 : public __derives_from_binary_function<_Tp>::type 180{ 181}; 182 183template <class _Tp> 184struct __maybe_derive_from_binary_function<_Tp, false> 185{ 186}; 187 188template <class _Tp, bool = __has_result_type<_Tp>::value> 189struct __weak_result_type_imp // bool is true 190 : public __maybe_derive_from_unary_function<_Tp>, 191 public __maybe_derive_from_binary_function<_Tp> 192{ 193 typedef typename _Tp::result_type result_type; 194}; 195 196template <class _Tp> 197struct __weak_result_type_imp<_Tp, false> 198 : public __maybe_derive_from_unary_function<_Tp>, 199 public __maybe_derive_from_binary_function<_Tp> 200{ 201}; 202 203template <class _Tp> 204struct __weak_result_type 205 : public __weak_result_type_imp<_Tp> 206{ 207}; 208 209// 0 argument case 210 211template <class _Rp> 212struct __weak_result_type<_Rp ()> 213{ 214 typedef _Rp result_type; 215}; 216 217template <class _Rp> 218struct __weak_result_type<_Rp (&)()> 219{ 220 typedef _Rp result_type; 221}; 222 223template <class _Rp> 224struct __weak_result_type<_Rp (*)()> 225{ 226 typedef _Rp result_type; 227}; 228 229// 1 argument case 230 231template <class _Rp, class _A1> 232struct __weak_result_type<_Rp (_A1)> 233 : public unary_function<_A1, _Rp> 234{ 235}; 236 237template <class _Rp, class _A1> 238struct __weak_result_type<_Rp (&)(_A1)> 239 : public unary_function<_A1, _Rp> 240{ 241}; 242 243template <class _Rp, class _A1> 244struct __weak_result_type<_Rp (*)(_A1)> 245 : public unary_function<_A1, _Rp> 246{ 247}; 248 249template <class _Rp, class _Cp> 250struct __weak_result_type<_Rp (_Cp::*)()> 251 : public unary_function<_Cp*, _Rp> 252{ 253}; 254 255template <class _Rp, class _Cp> 256struct __weak_result_type<_Rp (_Cp::*)() const> 257 : public unary_function<const _Cp*, _Rp> 258{ 259}; 260 261template <class _Rp, class _Cp> 262struct __weak_result_type<_Rp (_Cp::*)() volatile> 263 : public unary_function<volatile _Cp*, _Rp> 264{ 265}; 266 267template <class _Rp, class _Cp> 268struct __weak_result_type<_Rp (_Cp::*)() const volatile> 269 : public unary_function<const volatile _Cp*, _Rp> 270{ 271}; 272 273// 2 argument case 274 275template <class _Rp, class _A1, class _A2> 276struct __weak_result_type<_Rp (_A1, _A2)> 277 : public binary_function<_A1, _A2, _Rp> 278{ 279}; 280 281template <class _Rp, class _A1, class _A2> 282struct __weak_result_type<_Rp (*)(_A1, _A2)> 283 : public binary_function<_A1, _A2, _Rp> 284{ 285}; 286 287template <class _Rp, class _A1, class _A2> 288struct __weak_result_type<_Rp (&)(_A1, _A2)> 289 : public binary_function<_A1, _A2, _Rp> 290{ 291}; 292 293template <class _Rp, class _Cp, class _A1> 294struct __weak_result_type<_Rp (_Cp::*)(_A1)> 295 : public binary_function<_Cp*, _A1, _Rp> 296{ 297}; 298 299template <class _Rp, class _Cp, class _A1> 300struct __weak_result_type<_Rp (_Cp::*)(_A1) const> 301 : public binary_function<const _Cp*, _A1, _Rp> 302{ 303}; 304 305template <class _Rp, class _Cp, class _A1> 306struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile> 307 : public binary_function<volatile _Cp*, _A1, _Rp> 308{ 309}; 310 311template <class _Rp, class _Cp, class _A1> 312struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile> 313 : public binary_function<const volatile _Cp*, _A1, _Rp> 314{ 315}; 316 317// 3 or more arguments 318 319template <class _Rp, class _A1, class _A2, class _A3, class ..._A4> 320struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)> 321{ 322 typedef _Rp result_type; 323}; 324 325template <class _Rp, class _A1, class _A2, class _A3, class ..._A4> 326struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)> 327{ 328 typedef _Rp result_type; 329}; 330 331template <class _Rp, class _A1, class _A2, class _A3, class ..._A4> 332struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)> 333{ 334 typedef _Rp result_type; 335}; 336 337template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3> 338struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)> 339{ 340 typedef _Rp result_type; 341}; 342 343template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3> 344struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const> 345{ 346 typedef _Rp result_type; 347}; 348 349template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3> 350struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile> 351{ 352 typedef _Rp result_type; 353}; 354 355template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3> 356struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile> 357{ 358 typedef _Rp result_type; 359}; 360 361// __invoke 362 363// bullets 1 and 2 364 365template <class _Fp, class _A0, class ..._Args, 366 class> 367inline _LIBCPP_INLINE_VISIBILITY 368auto 369__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args) 370 -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...)) 371{ 372 return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...); 373} 374 375template <class _Fp, class _A0, class ..._Args, 376 class> 377inline _LIBCPP_INLINE_VISIBILITY 378auto 379__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args) 380 -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...)) 381{ 382 return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...); 383} 384 385// bullets 3 and 4 386 387template <class _Fp, class _A0, 388 class> 389inline _LIBCPP_INLINE_VISIBILITY 390auto 391__invoke(_Fp&& __f, _A0&& __a0) 392 -> decltype(_VSTD::forward<_A0>(__a0).*__f) 393{ 394 return _VSTD::forward<_A0>(__a0).*__f; 395} 396 397template <class _Fp, class _A0, 398 class> 399inline _LIBCPP_INLINE_VISIBILITY 400auto 401__invoke(_Fp&& __f, _A0&& __a0) 402 -> decltype((*_VSTD::forward<_A0>(__a0)).*__f) 403{ 404 return (*_VSTD::forward<_A0>(__a0)).*__f; 405} 406 407// bullet 5 408 409template <class _Fp, class ..._Args> 410inline _LIBCPP_INLINE_VISIBILITY 411auto 412__invoke(_Fp&& __f, _Args&& ...__args) 413 -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...)) 414{ 415 return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...); 416} 417 418template <class _Tp, class ..._Args> 419struct __invoke_return 420{ 421 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type; 422}; 423 424template <class _Ret> 425struct __invoke_void_return_wrapper 426{ 427 template <class ..._Args> 428 static _Ret __call(_Args&&... __args) 429 { 430 return __invoke(_VSTD::forward<_Args>(__args)...); 431 } 432}; 433 434template <> 435struct __invoke_void_return_wrapper<void> 436{ 437 template <class ..._Args> 438 static void __call(_Args&&... __args) 439 { 440 __invoke(_VSTD::forward<_Args>(__args)...); 441 } 442}; 443 444template <class _Tp> 445class _LIBCPP_TYPE_VIS_ONLY reference_wrapper 446 : public __weak_result_type<_Tp> 447{ 448public: 449 // types 450 typedef _Tp type; 451private: 452 type* __f_; 453 454public: 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 460#endif 461 462 // access 463 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;} 464 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;} 465 466 // invoke 467 template <class... _ArgTypes> 468 _LIBCPP_INLINE_VISIBILITY 469 typename __invoke_of<type&, _ArgTypes...>::type 470 operator() (_ArgTypes&&... __args) const 471 { 472 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...); 473 } 474}; 475 476template <class _Tp> struct __is_reference_wrapper_impl : public false_type {}; 477template <class _Tp> struct __is_reference_wrapper_impl<reference_wrapper<_Tp> > : public true_type {}; 478template <class _Tp> struct __is_reference_wrapper 479 : public __is_reference_wrapper_impl<typename remove_cv<_Tp>::type> {}; 480 481template <class _Tp> 482inline _LIBCPP_INLINE_VISIBILITY 483reference_wrapper<_Tp> 484ref(_Tp& __t) _NOEXCEPT 485{ 486 return reference_wrapper<_Tp>(__t); 487} 488 489template <class _Tp> 490inline _LIBCPP_INLINE_VISIBILITY 491reference_wrapper<_Tp> 492ref(reference_wrapper<_Tp> __t) _NOEXCEPT 493{ 494 return ref(__t.get()); 495} 496 497template <class _Tp> 498inline _LIBCPP_INLINE_VISIBILITY 499reference_wrapper<const _Tp> 500cref(const _Tp& __t) _NOEXCEPT 501{ 502 return reference_wrapper<const _Tp>(__t); 503} 504 505template <class _Tp> 506inline _LIBCPP_INLINE_VISIBILITY 507reference_wrapper<const _Tp> 508cref(reference_wrapper<_Tp> __t) _NOEXCEPT 509{ 510 return cref(__t.get()); 511} 512 513#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 514#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS 515 516template <class _Tp> void ref(const _Tp&&) = delete; 517template <class _Tp> void cref(const _Tp&&) = delete; 518 519#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS 520 521template <class _Tp> void ref(const _Tp&&);// = delete; 522template <class _Tp> void cref(const _Tp&&);// = delete; 523 524#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS 525 526#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 527 528#endif // _LIBCPP_HAS_NO_VARIADICS 529 530#if _LIBCPP_STD_VER > 11 531template <class _Tp1, class _Tp2 = void> 532struct __is_transparent 533{ 534private: 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); 538public: 539 static const bool value = sizeof(__test<_Tp1>(0)) == 1; 540}; 541#endif 542 543// allocator_arg_t 544 545struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { }; 546 547#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY) 548extern const allocator_arg_t allocator_arg; 549#else 550constexpr allocator_arg_t allocator_arg = allocator_arg_t(); 551#endif 552 553// uses_allocator 554 555template <class _Tp> 556struct __has_allocator_type 557{ 558private: 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); 562public: 563 static const bool value = sizeof(__test<_Tp>(0)) == 1; 564}; 565 566template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value> 567struct __uses_allocator 568 : public integral_constant<bool, 569 is_convertible<_Alloc, typename _Tp::allocator_type>::value> 570{ 571}; 572 573template <class _Tp, class _Alloc> 574struct __uses_allocator<_Tp, _Alloc, false> 575 : public false_type 576{ 577}; 578 579template <class _Tp, class _Alloc> 580struct _LIBCPP_TYPE_VIS_ONLY uses_allocator 581 : public __uses_allocator<_Tp, _Alloc> 582{ 583}; 584 585#ifndef _LIBCPP_HAS_NO_VARIADICS 586 587// allocator construction 588 589template <class _Tp, class _Alloc, class ..._Args> 590struct __uses_alloc_ctor_imp 591{ 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; 596}; 597 598template <class _Tp, class _Alloc, class ..._Args> 599struct __uses_alloc_ctor 600 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value> 601 {}; 602 603template <class _Tp, class _Allocator, class... _Args> 604inline _LIBCPP_INLINE_VISIBILITY 605void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args ) 606{ 607 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...); 608} 609 610template <class _Tp, class _Allocator, class... _Args> 611inline _LIBCPP_INLINE_VISIBILITY 612void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args ) 613{ 614 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...); 615} 616 617template <class _Tp, class _Allocator, class... _Args> 618inline _LIBCPP_INLINE_VISIBILITY 619void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args ) 620{ 621 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a); 622} 623 624template <class _Tp, class _Allocator, class... _Args> 625inline _LIBCPP_INLINE_VISIBILITY 626void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args) 627{ 628 __user_alloc_construct_impl( 629 __uses_alloc_ctor<_Tp, _Allocator>(), 630 __storage, __a, _VSTD::forward<_Args>(__args)... 631 ); 632} 633#endif // _LIBCPP_HAS_NO_VARIADICS 634 635_LIBCPP_END_NAMESPACE_STD 636 637#endif // _LIBCPP_FUNCTIONAL_BASE 638