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 26 template <class _Arg, class _Result> 27 struct _LIBCPP_TYPE_VIS_ONLY unary_function 28 { 29 typedef _Arg argument_type; 30 typedef _Result result_type; 31 }; 32 33 template <class _Arg1, class _Arg2, class _Result> 34 struct _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 41 template <class _Tp> struct _LIBCPP_TYPE_VIS_ONLY hash; 42 43 template <class _Tp> 44 struct __has_result_type 45 { 46 private: 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); 50 public: 51 static const bool value = sizeof(__test<_Tp>(0)) == 1; 52 }; 53 54 #if _LIBCPP_STD_VER > 11 55 template <class _Tp = void> 56 #else 57 template <class _Tp> 58 #endif 59 struct _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 67 template <> 68 struct _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 82 template <class _Tp> 83 inline _LIBCPP_INLINE_VISIBILITY 84 _Tp* 85 addressof(_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. 95 template <class _Tp> 96 inline _LIBCPP_INLINE_VISIBILITY 97 __strong _Tp* 98 addressof(__strong _Tp& __x) _NOEXCEPT 99 { 100 return &__x; 101 } 102 103 #ifdef _LIBCPP_HAS_OBJC_ARC_WEAK 104 template <class _Tp> 105 inline _LIBCPP_INLINE_VISIBILITY 106 __weak _Tp* 107 addressof(__weak _Tp& __x) _NOEXCEPT 108 { 109 return &__x; 110 } 111 #endif 112 113 template <class _Tp> 114 inline _LIBCPP_INLINE_VISIBILITY 115 __autoreleasing _Tp* 116 addressof(__autoreleasing _Tp& __x) _NOEXCEPT 117 { 118 return &__x; 119 } 120 121 template <class _Tp> 122 inline _LIBCPP_INLINE_VISIBILITY 123 __unsafe_unretained _Tp* 124 addressof(__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 138 template <class _Tp> 139 struct __derives_from_unary_function 140 { 141 private: 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>*); 147 public: 148 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value; 149 typedef decltype(__test((_Tp*)0)) type; 150 }; 151 152 template <class _Tp> 153 struct __derives_from_binary_function 154 { 155 private: 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>*); 161 public: 162 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value; 163 typedef decltype(__test((_Tp*)0)) type; 164 }; 165 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 169 { 170 }; 171 172 template <class _Tp> 173 struct __maybe_derive_from_unary_function<_Tp, false> 174 { 175 }; 176 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 180 { 181 }; 182 183 template <class _Tp> 184 struct __maybe_derive_from_binary_function<_Tp, false> 185 { 186 }; 187 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> 192 { 193 typedef typename _Tp::result_type result_type; 194 }; 195 196 template <class _Tp> 197 struct __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 203 template <class _Tp> 204 struct __weak_result_type 205 : public __weak_result_type_imp<_Tp> 206 { 207 }; 208 209 // 0 argument case 210 211 template <class _Rp> 212 struct __weak_result_type<_Rp ()> 213 { 214 typedef _Rp result_type; 215 }; 216 217 template <class _Rp> 218 struct __weak_result_type<_Rp (&)()> 219 { 220 typedef _Rp result_type; 221 }; 222 223 template <class _Rp> 224 struct __weak_result_type<_Rp (*)()> 225 { 226 typedef _Rp result_type; 227 }; 228 229 // 1 argument case 230 231 template <class _Rp, class _A1> 232 struct __weak_result_type<_Rp (_A1)> 233 : public unary_function<_A1, _Rp> 234 { 235 }; 236 237 template <class _Rp, class _A1> 238 struct __weak_result_type<_Rp (&)(_A1)> 239 : public unary_function<_A1, _Rp> 240 { 241 }; 242 243 template <class _Rp, class _A1> 244 struct __weak_result_type<_Rp (*)(_A1)> 245 : public unary_function<_A1, _Rp> 246 { 247 }; 248 249 template <class _Rp, class _Cp> 250 struct __weak_result_type<_Rp (_Cp::*)()> 251 : public unary_function<_Cp*, _Rp> 252 { 253 }; 254 255 template <class _Rp, class _Cp> 256 struct __weak_result_type<_Rp (_Cp::*)() const> 257 : public unary_function<const _Cp*, _Rp> 258 { 259 }; 260 261 template <class _Rp, class _Cp> 262 struct __weak_result_type<_Rp (_Cp::*)() volatile> 263 : public unary_function<volatile _Cp*, _Rp> 264 { 265 }; 266 267 template <class _Rp, class _Cp> 268 struct __weak_result_type<_Rp (_Cp::*)() const volatile> 269 : public unary_function<const volatile _Cp*, _Rp> 270 { 271 }; 272 273 // 2 argument case 274 275 template <class _Rp, class _A1, class _A2> 276 struct __weak_result_type<_Rp (_A1, _A2)> 277 : public binary_function<_A1, _A2, _Rp> 278 { 279 }; 280 281 template <class _Rp, class _A1, class _A2> 282 struct __weak_result_type<_Rp (*)(_A1, _A2)> 283 : public binary_function<_A1, _A2, _Rp> 284 { 285 }; 286 287 template <class _Rp, class _A1, class _A2> 288 struct __weak_result_type<_Rp (&)(_A1, _A2)> 289 : public binary_function<_A1, _A2, _Rp> 290 { 291 }; 292 293 template <class _Rp, class _Cp, class _A1> 294 struct __weak_result_type<_Rp (_Cp::*)(_A1)> 295 : public binary_function<_Cp*, _A1, _Rp> 296 { 297 }; 298 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> 302 { 303 }; 304 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> 308 { 309 }; 310 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> 314 { 315 }; 316 317 // 3 or more arguments 318 319 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4> 320 struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)> 321 { 322 typedef _Rp result_type; 323 }; 324 325 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4> 326 struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)> 327 { 328 typedef _Rp result_type; 329 }; 330 331 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4> 332 struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)> 333 { 334 typedef _Rp result_type; 335 }; 336 337 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3> 338 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)> 339 { 340 typedef _Rp result_type; 341 }; 342 343 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3> 344 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const> 345 { 346 typedef _Rp result_type; 347 }; 348 349 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3> 350 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile> 351 { 352 typedef _Rp result_type; 353 }; 354 355 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3> 356 struct __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 365 template <class _Fp, class _A0, class ..._Args, 366 class> 367 inline _LIBCPP_INLINE_VISIBILITY 368 auto 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 375 template <class _Fp, class _A0, class ..._Args, 376 class> 377 inline _LIBCPP_INLINE_VISIBILITY 378 auto 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 387 template <class _Fp, class _A0, 388 class> 389 inline _LIBCPP_INLINE_VISIBILITY 390 auto 391 __invoke(_Fp&& __f, _A0&& __a0) 392 -> decltype(_VSTD::forward<_A0>(__a0).*__f) 393 { 394 return _VSTD::forward<_A0>(__a0).*__f; 395 } 396 397 template <class _Fp, class _A0, 398 class> 399 inline _LIBCPP_INLINE_VISIBILITY 400 auto 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 409 template <class _Fp, class ..._Args> 410 inline _LIBCPP_INLINE_VISIBILITY 411 auto 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 418 template <class _Tp, class ..._Args> 419 struct __invoke_return 420 { 421 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type; 422 }; 423 424 template <class _Ret> 425 struct __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 434 template <> 435 struct __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 444 template <class _Tp> 445 class _LIBCPP_TYPE_VIS_ONLY reference_wrapper 446 : public __weak_result_type<_Tp> 447 { 448 public: 449 // types 450 typedef _Tp type; 451 private: 452 type* __f_; 453 454 public: 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 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> {}; 480 481 template <class _Tp> 482 inline _LIBCPP_INLINE_VISIBILITY 483 reference_wrapper<_Tp> 484 ref(_Tp& __t) _NOEXCEPT 485 { 486 return reference_wrapper<_Tp>(__t); 487 } 488 489 template <class _Tp> 490 inline _LIBCPP_INLINE_VISIBILITY 491 reference_wrapper<_Tp> 492 ref(reference_wrapper<_Tp> __t) _NOEXCEPT 493 { 494 return ref(__t.get()); 495 } 496 497 template <class _Tp> 498 inline _LIBCPP_INLINE_VISIBILITY 499 reference_wrapper<const _Tp> 500 cref(const _Tp& __t) _NOEXCEPT 501 { 502 return reference_wrapper<const _Tp>(__t); 503 } 504 505 template <class _Tp> 506 inline _LIBCPP_INLINE_VISIBILITY 507 reference_wrapper<const _Tp> 508 cref(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 516 template <class _Tp> void ref(const _Tp&&) = delete; 517 template <class _Tp> void cref(const _Tp&&) = delete; 518 519 #else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS 520 521 template <class _Tp> void ref(const _Tp&&);// = delete; 522 template <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 531 template <class _Tp1, class _Tp2 = void> 532 struct __is_transparent 533 { 534 private: 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); 538 public: 539 static const bool value = sizeof(__test<_Tp1>(0)) == 1; 540 }; 541 #endif 542 543 // allocator_arg_t 544 545 struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { }; 546 547 #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY) 548 extern const allocator_arg_t allocator_arg; 549 #else 550 constexpr allocator_arg_t allocator_arg = allocator_arg_t(); 551 #endif 552 553 // uses_allocator 554 555 template <class _Tp> 556 struct __has_allocator_type 557 { 558 private: 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); 562 public: 563 static const bool value = sizeof(__test<_Tp>(0)) == 1; 564 }; 565 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> 570 { 571 }; 572 573 template <class _Tp, class _Alloc> 574 struct __uses_allocator<_Tp, _Alloc, false> 575 : public false_type 576 { 577 }; 578 579 template <class _Tp, class _Alloc> 580 struct _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 589 template <class _Tp, class _Alloc, class ..._Args> 590 struct __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 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> 601 {}; 602 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 ) 606 { 607 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...); 608 } 609 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 ) 613 { 614 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...); 615 } 616 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 ) 620 { 621 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a); 622 } 623 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) 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