Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/tr1/functional
1 // TR1 functional header -*- C++ -*- 2 3 // Copyright (C) 2004-2025 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /** @file tr1/functional 26 * This is a TR1 C++ Library header. 27 */ 28 29 #ifndef _GLIBCXX_TR1_FUNCTIONAL 30 #define _GLIBCXX_TR1_FUNCTIONAL 1 31 32 #ifdef _GLIBCXX_SYSHDR 33 #pragma GCC system_header 34 #endif 35 36 #include <bits/requires_hosted.h> // TR1 37 38 #include <functional> // for std::_Placeholder, std::_Bind, std::_Bind_result 39 40 #include <typeinfo> 41 #include <new> 42 #include <tr1/tuple> 43 #include <tr1/type_traits> 44 #include <bits/stringfwd.h> 45 #include <tr1/functional_hash.h> 46 #include <ext/type_traits.h> 47 #include <bits/move.h> // for std::__addressof 48 49 #pragma GCC diagnostic push 50 #pragma GCC diagnostic ignored "-Wvolatile" // volatile parm/return 51 #pragma GCC diagnostic ignored "-Wc++11-extensions" 52 53 namespace std _GLIBCXX_VISIBILITY(default) 54 { 55 _GLIBCXX_BEGIN_NAMESPACE_VERSION 56 57 #if __cplusplus < 201103L 58 // In C++98 mode, <functional> doesn't declare std::placeholders::_1 etc. 59 // because they are not reserved names in C++98. However, they are reserved 60 // by <tr1/functional> so we can declare them here, in order to redeclare 61 // them in the std::tr1::placeholders namespace below. 62 namespace placeholders 63 { 64 extern const _Placeholder<1> _1; 65 extern const _Placeholder<2> _2; 66 extern const _Placeholder<3> _3; 67 extern const _Placeholder<4> _4; 68 extern const _Placeholder<5> _5; 69 extern const _Placeholder<6> _6; 70 extern const _Placeholder<7> _7; 71 extern const _Placeholder<8> _8; 72 extern const _Placeholder<9> _9; 73 extern const _Placeholder<10> _10; 74 extern const _Placeholder<11> _11; 75 extern const _Placeholder<12> _12; 76 extern const _Placeholder<13> _13; 77 extern const _Placeholder<14> _14; 78 extern const _Placeholder<15> _15; 79 extern const _Placeholder<16> _16; 80 extern const _Placeholder<17> _17; 81 extern const _Placeholder<18> _18; 82 extern const _Placeholder<19> _19; 83 extern const _Placeholder<20> _20; 84 extern const _Placeholder<21> _21; 85 extern const _Placeholder<22> _22; 86 extern const _Placeholder<23> _23; 87 extern const _Placeholder<24> _24; 88 extern const _Placeholder<25> _25; 89 extern const _Placeholder<26> _26; 90 extern const _Placeholder<27> _27; 91 extern const _Placeholder<28> _28; 92 extern const _Placeholder<29> _29; 93 } 94 #endif // C++98 95 96 namespace tr1 97 { 98 template<typename _MemberPointer> 99 class _Mem_fn; 100 template<typename _Tp, typename _Class> 101 _Mem_fn<_Tp _Class::*> 102 mem_fn(_Tp _Class::*); 103 104 /** 105 * Actual implementation of _Has_result_type, which uses SFINAE to 106 * determine if the type _Tp has a publicly-accessible member type 107 * result_type. 108 */ 109 template<typename _Tp> 110 class _Has_result_type_helper : __sfinae_types 111 { 112 template<typename _Up> 113 struct _Wrap_type 114 { }; 115 116 template<typename _Up> 117 static __one __test(_Wrap_type<typename _Up::result_type>*); 118 119 template<typename _Up> 120 static __two __test(...); 121 122 public: 123 static const bool value = sizeof(__test<_Tp>(0)) == 1; 124 }; 125 126 template<typename _Tp> 127 struct _Has_result_type 128 : integral_constant<bool, 129 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value> 130 { }; 131 132 /** 133 * 134 */ 135 /// If we have found a result_type, extract it. 136 template<bool _Has_result_type, typename _Functor> 137 struct _Maybe_get_result_type 138 { }; 139 140 template<typename _Functor> 141 struct _Maybe_get_result_type<true, _Functor> 142 { 143 typedef typename _Functor::result_type result_type; 144 }; 145 146 /** 147 * Base class for any function object that has a weak result type, as 148 * defined in 3.3/3 of TR1. 149 */ 150 template<typename _Functor> 151 struct _Weak_result_type_impl 152 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor> 153 { 154 }; 155 156 /// Retrieve the result type for a function type. 157 template<typename _Res, typename... _ArgTypes> 158 struct _Weak_result_type_impl<_Res(_ArgTypes...)> 159 { 160 typedef _Res result_type; 161 }; 162 163 /// Retrieve the result type for a function reference. 164 template<typename _Res, typename... _ArgTypes> 165 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)> 166 { 167 typedef _Res result_type; 168 }; 169 170 /// Retrieve the result type for a function pointer. 171 template<typename _Res, typename... _ArgTypes> 172 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)> 173 { 174 typedef _Res result_type; 175 }; 176 177 /// Retrieve result type for a member function pointer. 178 template<typename _Res, typename _Class, typename... _ArgTypes> 179 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)> 180 { 181 typedef _Res result_type; 182 }; 183 184 /// Retrieve result type for a const member function pointer. 185 template<typename _Res, typename _Class, typename... _ArgTypes> 186 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const> 187 { 188 typedef _Res result_type; 189 }; 190 191 /// Retrieve result type for a volatile member function pointer. 192 template<typename _Res, typename _Class, typename... _ArgTypes> 193 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile> 194 { 195 typedef _Res result_type; 196 }; 197 198 /// Retrieve result type for a const volatile member function pointer. 199 template<typename _Res, typename _Class, typename... _ArgTypes> 200 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile> 201 { 202 typedef _Res result_type; 203 }; 204 205 /** 206 * Strip top-level cv-qualifiers from the function object and let 207 * _Weak_result_type_impl perform the real work. 208 */ 209 template<typename _Functor> 210 struct _Weak_result_type 211 : _Weak_result_type_impl<typename remove_cv<_Functor>::type> 212 { 213 }; 214 215 template<typename _Signature> 216 class result_of; 217 218 /** 219 * Actual implementation of result_of. When _Has_result_type is 220 * true, gets its result from _Weak_result_type. Otherwise, uses 221 * the function object's member template result to extract the 222 * result type. 223 */ 224 template<bool _Has_result_type, typename _Signature> 225 struct _Result_of_impl; 226 227 // Handle member data pointers using _Mem_fn's logic 228 template<typename _Res, typename _Class, typename _T1> 229 struct _Result_of_impl<false, _Res _Class::*(_T1)> 230 { 231 typedef typename _Mem_fn<_Res _Class::*> 232 ::template _Result_type<_T1>::type type; 233 }; 234 235 /** 236 * Determine whether we can determine a result type from @c Functor 237 * alone. 238 */ 239 template<typename _Functor, typename... _ArgTypes> 240 class result_of<_Functor(_ArgTypes...)> 241 : public _Result_of_impl< 242 _Has_result_type<_Weak_result_type<_Functor> >::value, 243 _Functor(_ArgTypes...)> 244 { 245 }; 246 247 /// We already know the result type for @c Functor; use it. 248 template<typename _Functor, typename... _ArgTypes> 249 struct _Result_of_impl<true, _Functor(_ArgTypes...)> 250 { 251 typedef typename _Weak_result_type<_Functor>::result_type type; 252 }; 253 254 /** 255 * We need to compute the result type for this invocation the hard 256 * way. 257 */ 258 template<typename _Functor, typename... _ArgTypes> 259 struct _Result_of_impl<false, _Functor(_ArgTypes...)> 260 { 261 typedef typename _Functor 262 ::template result<_Functor(_ArgTypes...)>::type type; 263 }; 264 265 /** 266 * It is unsafe to access ::result when there are zero arguments, so we 267 * return @c void instead. 268 */ 269 template<typename _Functor> 270 struct _Result_of_impl<false, _Functor()> 271 { 272 typedef void type; 273 }; 274 275 // Ignore warnings about std::unary_function and std::binary_function. 276 #pragma GCC diagnostic push 277 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 278 279 /// Determines if the type _Tp derives from unary_function. 280 template<typename _Tp> 281 struct _Derives_from_unary_function : __sfinae_types 282 { 283 private: 284 template<typename _T1, typename _Res> 285 static __one __test(const volatile unary_function<_T1, _Res>*); 286 287 // It's tempting to change "..." to const volatile void*, but 288 // that fails when _Tp is a function type. 289 static __two __test(...); 290 291 public: 292 static const bool value = sizeof(__test((_Tp*)0)) == 1; 293 }; 294 295 /// Determines if the type _Tp derives from binary_function. 296 template<typename _Tp> 297 struct _Derives_from_binary_function : __sfinae_types 298 { 299 private: 300 template<typename _T1, typename _T2, typename _Res> 301 static __one __test(const volatile binary_function<_T1, _T2, _Res>*); 302 303 // It's tempting to change "..." to const volatile void*, but 304 // that fails when _Tp is a function type. 305 static __two __test(...); 306 307 public: 308 static const bool value = sizeof(__test((_Tp*)0)) == 1; 309 }; 310 311 /// Turns a function type into a function pointer type 312 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value> 313 struct _Function_to_function_pointer 314 { 315 typedef _Tp type; 316 }; 317 318 template<typename _Tp> 319 struct _Function_to_function_pointer<_Tp, true> 320 { 321 typedef _Tp* type; 322 }; 323 324 /** 325 * Invoke a function object, which may be either a member pointer or a 326 * function object. The first parameter will tell which. 327 */ 328 template<typename _Functor, typename... _Args> 329 inline 330 typename __gnu_cxx::__enable_if< 331 (!is_member_pointer<_Functor>::value 332 && !is_function<_Functor>::value 333 && !is_function<typename remove_pointer<_Functor>::type>::value), 334 typename result_of<_Functor(_Args...)>::type 335 >::__type 336 __invoke(_Functor& __f, _Args&... __args) 337 { 338 return __f(__args...); 339 } 340 341 template<typename _Functor, typename... _Args> 342 inline 343 typename __gnu_cxx::__enable_if< 344 (is_member_pointer<_Functor>::value 345 && !is_function<_Functor>::value 346 && !is_function<typename remove_pointer<_Functor>::type>::value), 347 typename result_of<_Functor(_Args...)>::type 348 >::__type 349 __invoke(_Functor& __f, _Args&... __args) 350 { 351 return mem_fn(__f)(__args...); 352 } 353 354 // To pick up function references (that will become function pointers) 355 template<typename _Functor, typename... _Args> 356 inline 357 typename __gnu_cxx::__enable_if< 358 (is_pointer<_Functor>::value 359 && is_function<typename remove_pointer<_Functor>::type>::value), 360 typename result_of<_Functor(_Args...)>::type 361 >::__type 362 __invoke(_Functor __f, _Args&... __args) 363 { 364 return __f(__args...); 365 } 366 367 /** 368 * Knowing which of unary_function and binary_function _Tp derives 369 * from, derives from the same and ensures that reference_wrapper 370 * will have a weak result type. See cases below. 371 */ 372 template<bool _Unary, bool _Binary, typename _Tp> 373 struct _Reference_wrapper_base_impl; 374 375 // Not a unary_function or binary_function, so try a weak result type. 376 template<typename _Tp> 377 struct _Reference_wrapper_base_impl<false, false, _Tp> 378 : _Weak_result_type<_Tp> 379 { }; 380 381 // unary_function but not binary_function 382 template<typename _Tp> 383 struct _Reference_wrapper_base_impl<true, false, _Tp> 384 : unary_function<typename _Tp::argument_type, 385 typename _Tp::result_type> 386 { }; 387 388 // binary_function but not unary_function 389 template<typename _Tp> 390 struct _Reference_wrapper_base_impl<false, true, _Tp> 391 : binary_function<typename _Tp::first_argument_type, 392 typename _Tp::second_argument_type, 393 typename _Tp::result_type> 394 { }; 395 396 // Both unary_function and binary_function. Import result_type to 397 // avoid conflicts. 398 template<typename _Tp> 399 struct _Reference_wrapper_base_impl<true, true, _Tp> 400 : unary_function<typename _Tp::argument_type, 401 typename _Tp::result_type>, 402 binary_function<typename _Tp::first_argument_type, 403 typename _Tp::second_argument_type, 404 typename _Tp::result_type> 405 { 406 typedef typename _Tp::result_type result_type; 407 }; 408 409 /** 410 * Derives from unary_function or binary_function when it 411 * can. Specializations handle all of the easy cases. The primary 412 * template determines what to do with a class type, which may 413 * derive from both unary_function and binary_function. 414 */ 415 template<typename _Tp> 416 struct _Reference_wrapper_base 417 : _Reference_wrapper_base_impl< 418 _Derives_from_unary_function<_Tp>::value, 419 _Derives_from_binary_function<_Tp>::value, 420 _Tp> 421 { }; 422 423 // - a function type (unary) 424 template<typename _Res, typename _T1> 425 struct _Reference_wrapper_base<_Res(_T1)> 426 : unary_function<_T1, _Res> 427 { }; 428 429 // - a function type (binary) 430 template<typename _Res, typename _T1, typename _T2> 431 struct _Reference_wrapper_base<_Res(_T1, _T2)> 432 : binary_function<_T1, _T2, _Res> 433 { }; 434 435 // - a function pointer type (unary) 436 template<typename _Res, typename _T1> 437 struct _Reference_wrapper_base<_Res(*)(_T1)> 438 : unary_function<_T1, _Res> 439 { }; 440 441 // - a function pointer type (binary) 442 template<typename _Res, typename _T1, typename _T2> 443 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)> 444 : binary_function<_T1, _T2, _Res> 445 { }; 446 447 // - a pointer to member function type (unary, no qualifiers) 448 template<typename _Res, typename _T1> 449 struct _Reference_wrapper_base<_Res (_T1::*)()> 450 : unary_function<_T1*, _Res> 451 { }; 452 453 // - a pointer to member function type (binary, no qualifiers) 454 template<typename _Res, typename _T1, typename _T2> 455 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)> 456 : binary_function<_T1*, _T2, _Res> 457 { }; 458 459 // - a pointer to member function type (unary, const) 460 template<typename _Res, typename _T1> 461 struct _Reference_wrapper_base<_Res (_T1::*)() const> 462 : unary_function<const _T1*, _Res> 463 { }; 464 465 // - a pointer to member function type (binary, const) 466 template<typename _Res, typename _T1, typename _T2> 467 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const> 468 : binary_function<const _T1*, _T2, _Res> 469 { }; 470 471 // - a pointer to member function type (unary, volatile) 472 template<typename _Res, typename _T1> 473 struct _Reference_wrapper_base<_Res (_T1::*)() volatile> 474 : unary_function<volatile _T1*, _Res> 475 { }; 476 477 // - a pointer to member function type (binary, volatile) 478 template<typename _Res, typename _T1, typename _T2> 479 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile> 480 : binary_function<volatile _T1*, _T2, _Res> 481 { }; 482 483 // - a pointer to member function type (unary, const volatile) 484 template<typename _Res, typename _T1> 485 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile> 486 : unary_function<const volatile _T1*, _Res> 487 { }; 488 489 // - a pointer to member function type (binary, const volatile) 490 template<typename _Res, typename _T1, typename _T2> 491 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile> 492 : binary_function<const volatile _T1*, _T2, _Res> 493 { }; 494 495 /// reference_wrapper 496 template<typename _Tp> 497 class reference_wrapper 498 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type> 499 { 500 // If _Tp is a function type, we can't form result_of<_Tp(...)>, 501 // so turn it into a function pointer type. 502 typedef typename _Function_to_function_pointer<_Tp>::type 503 _M_func_type; 504 505 _Tp* _M_data; 506 public: 507 typedef _Tp type; 508 509 explicit 510 reference_wrapper(_Tp& __indata) 511 : _M_data(std::__addressof(__indata)) 512 { } 513 514 reference_wrapper(const reference_wrapper<_Tp>& __inref): 515 _M_data(__inref._M_data) 516 { } 517 518 reference_wrapper& 519 operator=(const reference_wrapper<_Tp>& __inref) 520 { 521 _M_data = __inref._M_data; 522 return *this; 523 } 524 525 operator _Tp&() const 526 { return this->get(); } 527 528 _Tp& 529 get() const 530 { return *_M_data; } 531 532 template<typename... _Args> 533 typename result_of<_M_func_type(_Args...)>::type 534 operator()(_Args&... __args) const 535 { 536 return __invoke(get(), __args...); 537 } 538 }; 539 540 541 // Denotes a reference should be taken to a variable. 542 template<typename _Tp> 543 inline reference_wrapper<_Tp> 544 ref(_Tp& __t) 545 { return reference_wrapper<_Tp>(__t); } 546 547 // Denotes a const reference should be taken to a variable. 548 template<typename _Tp> 549 inline reference_wrapper<const _Tp> 550 cref(const _Tp& __t) 551 { return reference_wrapper<const _Tp>(__t); } 552 553 template<typename _Tp> 554 inline reference_wrapper<_Tp> 555 ref(reference_wrapper<_Tp> __t) 556 { return ref(__t.get()); } 557 558 template<typename _Tp> 559 inline reference_wrapper<const _Tp> 560 cref(reference_wrapper<_Tp> __t) 561 { return cref(__t.get()); } 562 563 template<typename _Tp, bool> 564 struct _Mem_fn_const_or_non 565 { 566 typedef const _Tp& type; 567 }; 568 569 template<typename _Tp> 570 struct _Mem_fn_const_or_non<_Tp, false> 571 { 572 typedef _Tp& type; 573 }; 574 575 /** 576 * Derives from @c unary_function or @c binary_function, or perhaps 577 * nothing, depending on the number of arguments provided. The 578 * primary template is the basis case, which derives nothing. 579 */ 580 template<typename _Res, typename... _ArgTypes> 581 struct _Maybe_unary_or_binary_function { }; 582 583 /// Derives from @c unary_function, as appropriate. 584 template<typename _Res, typename _T1> 585 struct _Maybe_unary_or_binary_function<_Res, _T1> 586 : std::unary_function<_T1, _Res> { }; 587 588 /// Derives from @c binary_function, as appropriate. 589 template<typename _Res, typename _T1, typename _T2> 590 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> 591 : std::binary_function<_T1, _T2, _Res> { }; 592 593 /// Implementation of @c mem_fn for member function pointers. 594 template<typename _Res, typename _Class, typename... _ArgTypes> 595 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)> 596 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...> 597 { 598 typedef _Res (_Class::*_Functor)(_ArgTypes...); 599 600 template<typename _Tp> 601 _Res 602 _M_call(_Tp& __object, const volatile _Class *, 603 _ArgTypes... __args) const 604 { return (__object.*__pmf)(__args...); } 605 606 template<typename _Tp> 607 _Res 608 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const 609 { return ((*__ptr).*__pmf)(__args...); } 610 611 public: 612 typedef _Res result_type; 613 614 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 615 616 // Handle objects 617 _Res 618 operator()(_Class& __object, _ArgTypes... __args) const 619 { return (__object.*__pmf)(__args...); } 620 621 // Handle pointers 622 _Res 623 operator()(_Class* __object, _ArgTypes... __args) const 624 { return (__object->*__pmf)(__args...); } 625 626 // Handle smart pointers, references and pointers to derived 627 template<typename _Tp> 628 _Res 629 operator()(_Tp& __object, _ArgTypes... __args) const 630 { return _M_call(__object, &__object, __args...); } 631 632 private: 633 _Functor __pmf; 634 }; 635 636 /// Implementation of @c mem_fn for const member function pointers. 637 template<typename _Res, typename _Class, typename... _ArgTypes> 638 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const> 639 : public _Maybe_unary_or_binary_function<_Res, const _Class*, 640 _ArgTypes...> 641 { 642 typedef _Res (_Class::*_Functor)(_ArgTypes...) const; 643 644 template<typename _Tp> 645 _Res 646 _M_call(_Tp& __object, const volatile _Class *, 647 _ArgTypes... __args) const 648 { return (__object.*__pmf)(__args...); } 649 650 template<typename _Tp> 651 _Res 652 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const 653 { return ((*__ptr).*__pmf)(__args...); } 654 655 public: 656 typedef _Res result_type; 657 658 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 659 660 // Handle objects 661 _Res 662 operator()(const _Class& __object, _ArgTypes... __args) const 663 { return (__object.*__pmf)(__args...); } 664 665 // Handle pointers 666 _Res 667 operator()(const _Class* __object, _ArgTypes... __args) const 668 { return (__object->*__pmf)(__args...); } 669 670 // Handle smart pointers, references and pointers to derived 671 template<typename _Tp> 672 _Res operator()(_Tp& __object, _ArgTypes... __args) const 673 { return _M_call(__object, &__object, __args...); } 674 675 private: 676 _Functor __pmf; 677 }; 678 679 /// Implementation of @c mem_fn for volatile member function pointers. 680 template<typename _Res, typename _Class, typename... _ArgTypes> 681 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile> 682 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*, 683 _ArgTypes...> 684 { 685 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile; 686 687 template<typename _Tp> 688 _Res 689 _M_call(_Tp& __object, const volatile _Class *, 690 _ArgTypes... __args) const 691 { return (__object.*__pmf)(__args...); } 692 693 template<typename _Tp> 694 _Res 695 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const 696 { return ((*__ptr).*__pmf)(__args...); } 697 698 public: 699 typedef _Res result_type; 700 701 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 702 703 // Handle objects 704 _Res 705 operator()(volatile _Class& __object, _ArgTypes... __args) const 706 { return (__object.*__pmf)(__args...); } 707 708 // Handle pointers 709 _Res 710 operator()(volatile _Class* __object, _ArgTypes... __args) const 711 { return (__object->*__pmf)(__args...); } 712 713 // Handle smart pointers, references and pointers to derived 714 template<typename _Tp> 715 _Res 716 operator()(_Tp& __object, _ArgTypes... __args) const 717 { return _M_call(__object, &__object, __args...); } 718 719 private: 720 _Functor __pmf; 721 }; 722 723 /// Implementation of @c mem_fn for const volatile member function pointers. 724 template<typename _Res, typename _Class, typename... _ArgTypes> 725 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile> 726 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*, 727 _ArgTypes...> 728 { 729 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile; 730 731 template<typename _Tp> 732 _Res 733 _M_call(_Tp& __object, const volatile _Class *, 734 _ArgTypes... __args) const 735 { return (__object.*__pmf)(__args...); } 736 737 template<typename _Tp> 738 _Res 739 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const 740 { return ((*__ptr).*__pmf)(__args...); } 741 742 public: 743 typedef _Res result_type; 744 745 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 746 747 // Handle objects 748 _Res 749 operator()(const volatile _Class& __object, _ArgTypes... __args) const 750 { return (__object.*__pmf)(__args...); } 751 752 // Handle pointers 753 _Res 754 operator()(const volatile _Class* __object, _ArgTypes... __args) const 755 { return (__object->*__pmf)(__args...); } 756 757 // Handle smart pointers, references and pointers to derived 758 template<typename _Tp> 759 _Res operator()(_Tp& __object, _ArgTypes... __args) const 760 { return _M_call(__object, &__object, __args...); } 761 762 private: 763 _Functor __pmf; 764 }; 765 766 767 template<typename _Res, typename _Class> 768 class _Mem_fn<_Res _Class::*> 769 { 770 // This bit of genius is due to Peter Dimov, improved slightly by 771 // Douglas Gregor. 772 template<typename _Tp> 773 _Res& 774 _M_call(_Tp& __object, _Class *) const 775 { return __object.*__pm; } 776 777 template<typename _Tp, typename _Up> 778 _Res& 779 _M_call(_Tp& __object, _Up * const *) const 780 { return (*__object).*__pm; } 781 782 template<typename _Tp, typename _Up> 783 const _Res& 784 _M_call(_Tp& __object, const _Up * const *) const 785 { return (*__object).*__pm; } 786 787 template<typename _Tp> 788 const _Res& 789 _M_call(_Tp& __object, const _Class *) const 790 { return __object.*__pm; } 791 792 template<typename _Tp> 793 const _Res& 794 _M_call(_Tp& __ptr, const volatile void*) const 795 { return (*__ptr).*__pm; } 796 797 template<typename _Tp> static _Tp& __get_ref(); 798 799 template<typename _Tp> 800 static __sfinae_types::__one __check_const(_Tp&, _Class*); 801 template<typename _Tp, typename _Up> 802 static __sfinae_types::__one __check_const(_Tp&, _Up * const *); 803 template<typename _Tp, typename _Up> 804 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *); 805 template<typename _Tp> 806 static __sfinae_types::__two __check_const(_Tp&, const _Class*); 807 template<typename _Tp> 808 static __sfinae_types::__two __check_const(_Tp&, const volatile void*); 809 810 public: 811 template<typename _Tp> 812 struct _Result_type 813 : _Mem_fn_const_or_non<_Res, 814 (sizeof(__sfinae_types::__two) 815 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))> 816 { }; 817 818 template<typename _Signature> 819 struct result; 820 821 template<typename _CVMem, typename _Tp> 822 struct result<_CVMem(_Tp)> 823 : public _Result_type<_Tp> { }; 824 825 template<typename _CVMem, typename _Tp> 826 struct result<_CVMem(_Tp&)> 827 : public _Result_type<_Tp> { }; 828 829 explicit 830 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { } 831 832 // Handle objects 833 _Res& 834 operator()(_Class& __object) const 835 { return __object.*__pm; } 836 837 const _Res& 838 operator()(const _Class& __object) const 839 { return __object.*__pm; } 840 841 // Handle pointers 842 _Res& 843 operator()(_Class* __object) const 844 { return __object->*__pm; } 845 846 const _Res& 847 operator()(const _Class* __object) const 848 { return __object->*__pm; } 849 850 // Handle smart pointers and derived 851 template<typename _Tp> 852 typename _Result_type<_Tp>::type 853 operator()(_Tp& __unknown) const 854 { return _M_call(__unknown, &__unknown); } 855 856 private: 857 _Res _Class::*__pm; 858 }; 859 860 /** 861 * @brief Returns a function object that forwards to the member 862 * pointer @a pm. 863 */ 864 template<typename _Tp, typename _Class> 865 inline _Mem_fn<_Tp _Class::*> 866 mem_fn(_Tp _Class::* __pm) 867 { 868 return _Mem_fn<_Tp _Class::*>(__pm); 869 } 870 871 /** 872 * @brief Determines if the given type _Tp is a function object 873 * should be treated as a subexpression when evaluating calls to 874 * function objects returned by bind(). [TR1 3.6.1] 875 */ 876 template<typename _Tp> 877 struct is_bind_expression 878 { static const bool value = false; }; 879 880 template<typename _Tp> 881 const bool is_bind_expression<_Tp>::value; 882 883 /** 884 * @brief Determines if the given type _Tp is a placeholder in a 885 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2] 886 */ 887 template<typename _Tp> 888 struct is_placeholder 889 { static const int value = 0; }; 890 891 template<typename _Tp> 892 const int is_placeholder<_Tp>::value; 893 894 /// The type of placeholder objects defined by libstdc++. 895 using ::std::_Placeholder; 896 897 /** @namespace std::tr1::placeholders 898 * @brief Sub-namespace for tr1/functional. 899 */ 900 namespace placeholders 901 { 902 // The C++11 std::placeholders are already exported from the library. 903 // Reusing them here avoids needing to export additional symbols for 904 // the TR1 placeholders, and avoids ODR violations due to defining 905 // them with internal linkage (as we used to do). 906 using namespace ::std::placeholders; 907 } 908 909 /** 910 * Partial specialization of is_placeholder that provides the placeholder 911 * number for the placeholder objects defined by libstdc++. 912 */ 913 template<int _Num> 914 struct is_placeholder<_Placeholder<_Num> > 915 : integral_constant<int, _Num> 916 { }; 917 918 template<int _Num> 919 struct is_placeholder<const _Placeholder<_Num> > 920 : integral_constant<int, _Num> 921 { }; 922 923 /** 924 * Stores a tuple of indices. Used by bind() to extract the elements 925 * in a tuple. 926 */ 927 template<int... _Indexes> 928 struct _Index_tuple { }; 929 930 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>. 931 template<std::size_t _Num, typename _Tuple = _Index_tuple<> > 932 struct _Build_index_tuple; 933 934 template<std::size_t _Num, int... _Indexes> 935 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> > 936 : _Build_index_tuple<_Num - 1, 937 _Index_tuple<_Indexes..., sizeof...(_Indexes)> > 938 { 939 }; 940 941 template<int... _Indexes> 942 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> > 943 { 944 typedef _Index_tuple<_Indexes...> __type; 945 }; 946 947 /** 948 * Used by _Safe_tuple_element to indicate that there is no tuple 949 * element at this position. 950 */ 951 struct _No_tuple_element; 952 953 /** 954 * Implementation helper for _Safe_tuple_element. This primary 955 * template handles the case where it is safe to use @c 956 * tuple_element. 957 */ 958 template<int __i, typename _Tuple, bool _IsSafe> 959 struct _Safe_tuple_element_impl 960 : tuple_element<__i, _Tuple> { }; 961 962 /** 963 * Implementation helper for _Safe_tuple_element. This partial 964 * specialization handles the case where it is not safe to use @c 965 * tuple_element. We just return @c _No_tuple_element. 966 */ 967 template<int __i, typename _Tuple> 968 struct _Safe_tuple_element_impl<__i, _Tuple, false> 969 { 970 typedef _No_tuple_element type; 971 }; 972 973 /** 974 * Like tuple_element, but returns @c _No_tuple_element when 975 * tuple_element would return an error. 976 */ 977 template<int __i, typename _Tuple> 978 struct _Safe_tuple_element 979 : _Safe_tuple_element_impl<__i, _Tuple, 980 (__i >= 0 && __i < tuple_size<_Tuple>::value)> 981 { 982 }; 983 984 /** 985 * Maps an argument to bind() into an actual argument to the bound 986 * function object [TR1 3.6.3/5]. Only the first parameter should 987 * be specified: the rest are used to determine among the various 988 * implementations. Note that, although this class is a function 989 * object, it isn't entirely normal because it takes only two 990 * parameters regardless of the number of parameters passed to the 991 * bind expression. The first parameter is the bound argument and 992 * the second parameter is a tuple containing references to the 993 * rest of the arguments. 994 */ 995 template<typename _Arg, 996 bool _IsBindExp = is_bind_expression<_Arg>::value, 997 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)> 998 class _Mu; 999 1000 /** 1001 * If the argument is reference_wrapper<_Tp>, returns the 1002 * underlying reference. [TR1 3.6.3/5 bullet 1] 1003 */ 1004 template<typename _Tp> 1005 class _Mu<reference_wrapper<_Tp>, false, false> 1006 { 1007 public: 1008 typedef _Tp& result_type; 1009 1010 /* Note: This won't actually work for const volatile 1011 * reference_wrappers, because reference_wrapper::get() is const 1012 * but not volatile-qualified. This might be a defect in the TR. 1013 */ 1014 template<typename _CVRef, typename _Tuple> 1015 result_type 1016 operator()(_CVRef& __arg, const _Tuple&) const volatile 1017 { return __arg.get(); } 1018 }; 1019 1020 /** 1021 * If the argument is a bind expression, we invoke the underlying 1022 * function object with the same cv-qualifiers as we are given and 1023 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2] 1024 */ 1025 template<typename _Arg> 1026 class _Mu<_Arg, true, false> 1027 { 1028 public: 1029 template<typename _Signature> class result; 1030 1031 // Determine the result type when we pass the arguments along. This 1032 // involves passing along the cv-qualifiers placed on _Mu and 1033 // unwrapping the argument bundle. 1034 template<typename _CVMu, typename _CVArg, typename... _Args> 1035 class result<_CVMu(_CVArg, tuple<_Args...>)> 1036 : public result_of<_CVArg(_Args...)> { }; 1037 1038 template<typename _CVArg, typename... _Args> 1039 typename result_of<_CVArg(_Args...)>::type 1040 operator()(_CVArg& __arg, 1041 const tuple<_Args...>& __tuple) const volatile 1042 { 1043 // Construct an index tuple and forward to __call 1044 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type 1045 _Indexes; 1046 return this->__call(__arg, __tuple, _Indexes()); 1047 } 1048 1049 private: 1050 // Invokes the underlying function object __arg by unpacking all 1051 // of the arguments in the tuple. 1052 template<typename _CVArg, typename... _Args, int... _Indexes> 1053 typename result_of<_CVArg(_Args...)>::type 1054 __call(_CVArg& __arg, const tuple<_Args...>& __tuple, 1055 const _Index_tuple<_Indexes...>&) const volatile 1056 { 1057 return __arg(tr1::get<_Indexes>(__tuple)...); 1058 } 1059 }; 1060 1061 /** 1062 * If the argument is a placeholder for the Nth argument, returns 1063 * a reference to the Nth argument to the bind function object. 1064 * [TR1 3.6.3/5 bullet 3] 1065 */ 1066 template<typename _Arg> 1067 class _Mu<_Arg, false, true> 1068 { 1069 public: 1070 template<typename _Signature> class result; 1071 1072 template<typename _CVMu, typename _CVArg, typename _Tuple> 1073 class result<_CVMu(_CVArg, _Tuple)> 1074 { 1075 // Add a reference, if it hasn't already been done for us. 1076 // This allows us to be a little bit sloppy in constructing 1077 // the tuple that we pass to result_of<...>. 1078 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value 1079 - 1), _Tuple>::type 1080 __base_type; 1081 1082 public: 1083 typedef typename add_reference<__base_type>::type type; 1084 }; 1085 1086 template<typename _Tuple> 1087 typename result<_Mu(_Arg, _Tuple)>::type 1088 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile 1089 { 1090 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple); 1091 } 1092 }; 1093 1094 /** 1095 * If the argument is just a value, returns a reference to that 1096 * value. The cv-qualifiers on the reference are the same as the 1097 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4] 1098 */ 1099 template<typename _Arg> 1100 class _Mu<_Arg, false, false> 1101 { 1102 public: 1103 template<typename _Signature> struct result; 1104 1105 template<typename _CVMu, typename _CVArg, typename _Tuple> 1106 struct result<_CVMu(_CVArg, _Tuple)> 1107 { 1108 typedef typename add_reference<_CVArg>::type type; 1109 }; 1110 1111 // Pick up the cv-qualifiers of the argument 1112 template<typename _CVArg, typename _Tuple> 1113 _CVArg& 1114 operator()(_CVArg& __arg, const _Tuple&) const volatile 1115 { return __arg; } 1116 }; 1117 1118 /** 1119 * Maps member pointers into instances of _Mem_fn but leaves all 1120 * other function objects untouched. Used by tr1::bind(). The 1121 * primary template handles the non--member-pointer case. 1122 */ 1123 template<typename _Tp> 1124 struct _Maybe_wrap_member_pointer 1125 { 1126 typedef _Tp type; 1127 1128 static const _Tp& 1129 __do_wrap(const _Tp& __x) 1130 { return __x; } 1131 }; 1132 1133 /** 1134 * Maps member pointers into instances of _Mem_fn but leaves all 1135 * other function objects untouched. Used by tr1::bind(). This 1136 * partial specialization handles the member pointer case. 1137 */ 1138 template<typename _Tp, typename _Class> 1139 struct _Maybe_wrap_member_pointer<_Tp _Class::*> 1140 { 1141 typedef _Mem_fn<_Tp _Class::*> type; 1142 1143 static type 1144 __do_wrap(_Tp _Class::* __pm) 1145 { return type(__pm); } 1146 }; 1147 1148 /// Type of the function object returned from bind(). 1149 template<typename _Signature> 1150 struct _Bind; 1151 1152 template<typename _Functor, typename... _Bound_args> 1153 class _Bind<_Functor(_Bound_args...)> 1154 : public _Weak_result_type<_Functor> 1155 { 1156 typedef _Bind __self_type; 1157 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 1158 _Bound_indexes; 1159 1160 _Functor _M_f; 1161 tuple<_Bound_args...> _M_bound_args; 1162 1163 // Call unqualified 1164 template<typename... _Args, int... _Indexes> 1165 typename result_of< 1166 _Functor(typename result_of<_Mu<_Bound_args> 1167 (_Bound_args, tuple<_Args...>)>::type...) 1168 >::type 1169 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) 1170 { 1171 return _M_f(_Mu<_Bound_args>() 1172 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1173 } 1174 1175 // Call as const 1176 template<typename... _Args, int... _Indexes> 1177 typename result_of< 1178 const _Functor(typename result_of<_Mu<_Bound_args> 1179 (const _Bound_args, tuple<_Args...>) 1180 >::type...)>::type 1181 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const 1182 { 1183 return _M_f(_Mu<_Bound_args>() 1184 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1185 } 1186 1187 // Call as volatile 1188 template<typename... _Args, int... _Indexes> 1189 typename result_of< 1190 volatile _Functor(typename result_of<_Mu<_Bound_args> 1191 (volatile _Bound_args, tuple<_Args...>) 1192 >::type...)>::type 1193 __call(const tuple<_Args...>& __args, 1194 _Index_tuple<_Indexes...>) volatile 1195 { 1196 return _M_f(_Mu<_Bound_args>() 1197 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1198 } 1199 1200 // Call as const volatile 1201 template<typename... _Args, int... _Indexes> 1202 typename result_of< 1203 const volatile _Functor(typename result_of<_Mu<_Bound_args> 1204 (const volatile _Bound_args, 1205 tuple<_Args...>) 1206 >::type...)>::type 1207 __call(const tuple<_Args...>& __args, 1208 _Index_tuple<_Indexes...>) const volatile 1209 { 1210 return _M_f(_Mu<_Bound_args>() 1211 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1212 } 1213 1214 public: 1215 explicit _Bind(_Functor __f, _Bound_args... __bound_args) 1216 : _M_f(__f), _M_bound_args(__bound_args...) { } 1217 1218 // Call unqualified 1219 template<typename... _Args> 1220 typename result_of< 1221 _Functor(typename result_of<_Mu<_Bound_args> 1222 (_Bound_args, tuple<_Args...>)>::type...) 1223 >::type 1224 operator()(_Args&... __args) 1225 { 1226 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1227 } 1228 1229 // Call as const 1230 template<typename... _Args> 1231 typename result_of< 1232 const _Functor(typename result_of<_Mu<_Bound_args> 1233 (const _Bound_args, tuple<_Args...>)>::type...) 1234 >::type 1235 operator()(_Args&... __args) const 1236 { 1237 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1238 } 1239 1240 1241 // Call as volatile 1242 template<typename... _Args> 1243 typename result_of< 1244 volatile _Functor(typename result_of<_Mu<_Bound_args> 1245 (volatile _Bound_args, tuple<_Args...>)>::type...) 1246 >::type 1247 operator()(_Args&... __args) volatile 1248 { 1249 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1250 } 1251 1252 1253 // Call as const volatile 1254 template<typename... _Args> 1255 typename result_of< 1256 const volatile _Functor(typename result_of<_Mu<_Bound_args> 1257 (const volatile _Bound_args, 1258 tuple<_Args...>)>::type...) 1259 >::type 1260 operator()(_Args&... __args) const volatile 1261 { 1262 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1263 } 1264 }; 1265 1266 /// Type of the function object returned from bind<R>(). 1267 template<typename _Result, typename _Signature> 1268 struct _Bind_result; 1269 1270 template<typename _Result, typename _Functor, typename... _Bound_args> 1271 class _Bind_result<_Result, _Functor(_Bound_args...)> 1272 { 1273 typedef _Bind_result __self_type; 1274 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 1275 _Bound_indexes; 1276 1277 _Functor _M_f; 1278 tuple<_Bound_args...> _M_bound_args; 1279 1280 // Call unqualified 1281 template<typename... _Args, int... _Indexes> 1282 _Result 1283 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) 1284 { 1285 return _M_f(_Mu<_Bound_args>() 1286 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1287 } 1288 1289 // Call as const 1290 template<typename... _Args, int... _Indexes> 1291 _Result 1292 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const 1293 { 1294 return _M_f(_Mu<_Bound_args>() 1295 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1296 } 1297 1298 // Call as volatile 1299 template<typename... _Args, int... _Indexes> 1300 _Result 1301 __call(const tuple<_Args...>& __args, 1302 _Index_tuple<_Indexes...>) volatile 1303 { 1304 return _M_f(_Mu<_Bound_args>() 1305 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1306 } 1307 1308 // Call as const volatile 1309 template<typename... _Args, int... _Indexes> 1310 _Result 1311 __call(const tuple<_Args...>& __args, 1312 _Index_tuple<_Indexes...>) const volatile 1313 { 1314 return _M_f(_Mu<_Bound_args>() 1315 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1316 } 1317 1318 public: 1319 typedef _Result result_type; 1320 1321 explicit 1322 _Bind_result(_Functor __f, _Bound_args... __bound_args) 1323 : _M_f(__f), _M_bound_args(__bound_args...) { } 1324 1325 // Call unqualified 1326 template<typename... _Args> 1327 result_type 1328 operator()(_Args&... __args) 1329 { 1330 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1331 } 1332 1333 // Call as const 1334 template<typename... _Args> 1335 result_type 1336 operator()(_Args&... __args) const 1337 { 1338 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1339 } 1340 1341 // Call as volatile 1342 template<typename... _Args> 1343 result_type 1344 operator()(_Args&... __args) volatile 1345 { 1346 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1347 } 1348 1349 // Call as const volatile 1350 template<typename... _Args> 1351 result_type 1352 operator()(_Args&... __args) const volatile 1353 { 1354 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1355 } 1356 }; 1357 1358 /// Class template _Bind is always a bind expression. 1359 template<typename _Signature> 1360 struct is_bind_expression<_Bind<_Signature> > 1361 { static const bool value = true; }; 1362 1363 template<typename _Signature> 1364 const bool is_bind_expression<_Bind<_Signature> >::value; 1365 1366 /// Class template _Bind is always a bind expression. 1367 template<typename _Signature> 1368 struct is_bind_expression<const _Bind<_Signature> > 1369 { static const bool value = true; }; 1370 1371 template<typename _Signature> 1372 const bool is_bind_expression<const _Bind<_Signature> >::value; 1373 1374 /// Class template _Bind is always a bind expression. 1375 template<typename _Signature> 1376 struct is_bind_expression<volatile _Bind<_Signature> > 1377 { static const bool value = true; }; 1378 1379 template<typename _Signature> 1380 const bool is_bind_expression<volatile _Bind<_Signature> >::value; 1381 1382 /// Class template _Bind is always a bind expression. 1383 template<typename _Signature> 1384 struct is_bind_expression<const volatile _Bind<_Signature> > 1385 { static const bool value = true; }; 1386 1387 template<typename _Signature> 1388 const bool is_bind_expression<const volatile _Bind<_Signature> >::value; 1389 1390 /// Class template _Bind_result is always a bind expression. 1391 template<typename _Result, typename _Signature> 1392 struct is_bind_expression<_Bind_result<_Result, _Signature> > 1393 { static const bool value = true; }; 1394 1395 template<typename _Result, typename _Signature> 1396 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value; 1397 1398 /// Class template _Bind_result is always a bind expression. 1399 template<typename _Result, typename _Signature> 1400 struct is_bind_expression<const _Bind_result<_Result, _Signature> > 1401 { static const bool value = true; }; 1402 1403 template<typename _Result, typename _Signature> 1404 const bool 1405 is_bind_expression<const _Bind_result<_Result, _Signature> >::value; 1406 1407 /// Class template _Bind_result is always a bind expression. 1408 template<typename _Result, typename _Signature> 1409 struct is_bind_expression<volatile _Bind_result<_Result, _Signature> > 1410 { static const bool value = true; }; 1411 1412 template<typename _Result, typename _Signature> 1413 const bool 1414 is_bind_expression<volatile _Bind_result<_Result, _Signature> >::value; 1415 1416 /// Class template _Bind_result is always a bind expression. 1417 template<typename _Result, typename _Signature> 1418 struct 1419 is_bind_expression<const volatile _Bind_result<_Result, _Signature> > 1420 { static const bool value = true; }; 1421 1422 template<typename _Result, typename _Signature> 1423 const bool 1424 is_bind_expression<const volatile _Bind_result<_Result, 1425 _Signature> >::value; 1426 1427 #if __cplusplus >= 201103L 1428 // Specialize tr1::is_bind_expression for std::bind closure types, 1429 // so that they can also work with tr1::bind. 1430 1431 template<typename _Signature> 1432 struct is_bind_expression<std::_Bind<_Signature>> 1433 : true_type { }; 1434 1435 template<typename _Signature> 1436 struct is_bind_expression<const std::_Bind<_Signature>> 1437 : true_type { }; 1438 1439 template<typename _Signature> 1440 struct is_bind_expression<volatile std::_Bind<_Signature>> 1441 : true_type { }; 1442 1443 template<typename _Signature> 1444 struct is_bind_expression<const volatile std::_Bind<_Signature>> 1445 : true_type { }; 1446 1447 template<typename _Result, typename _Signature> 1448 struct is_bind_expression<std::_Bind_result<_Result, _Signature>> 1449 : true_type { }; 1450 1451 template<typename _Result, typename _Signature> 1452 struct is_bind_expression<const std::_Bind_result<_Result, _Signature>> 1453 : true_type { }; 1454 1455 template<typename _Result, typename _Signature> 1456 struct is_bind_expression<volatile std::_Bind_result<_Result, _Signature>> 1457 : true_type { }; 1458 1459 template<typename _Result, typename _Signature> 1460 struct is_bind_expression<const volatile std::_Bind_result<_Result, 1461 _Signature>> 1462 : true_type { }; 1463 #endif 1464 1465 /// bind 1466 template<typename _Functor, typename... _ArgTypes> 1467 inline 1468 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)> 1469 bind(_Functor __f, _ArgTypes... __args) 1470 { 1471 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type; 1472 typedef typename __maybe_type::type __functor_type; 1473 typedef _Bind<__functor_type(_ArgTypes...)> __result_type; 1474 return __result_type(__maybe_type::__do_wrap(__f), __args...); 1475 } 1476 1477 template<typename _Result, typename _Functor, typename... _ArgTypes> 1478 inline 1479 _Bind_result<_Result, 1480 typename _Maybe_wrap_member_pointer<_Functor>::type 1481 (_ArgTypes...)> 1482 bind(_Functor __f, _ArgTypes... __args) 1483 { 1484 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type; 1485 typedef typename __maybe_type::type __functor_type; 1486 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)> 1487 __result_type; 1488 return __result_type(__maybe_type::__do_wrap(__f), __args...); 1489 } 1490 1491 /** 1492 * @brief Exception class thrown when class template function's 1493 * operator() is called with an empty target. 1494 * @ingroup exceptions 1495 */ 1496 class bad_function_call : public std::exception { }; 1497 1498 /** 1499 * The integral constant expression 0 can be converted into a 1500 * pointer to this type. It is used by the function template to 1501 * accept NULL pointers. 1502 */ 1503 struct _M_clear_type; 1504 1505 /** 1506 * Trait identifying @a location-invariant types, meaning that the 1507 * address of the object (or any of its members) will not escape. 1508 * Also implies a trivial copy constructor and assignment operator. 1509 */ 1510 template<typename _Tp> 1511 struct __is_location_invariant 1512 : integral_constant<bool, 1513 (is_pointer<_Tp>::value 1514 || is_member_pointer<_Tp>::value)> 1515 { 1516 }; 1517 1518 class _Undefined_class; 1519 1520 union _Nocopy_types 1521 { 1522 void* _M_object; 1523 const void* _M_const_object; 1524 void (*_M_function_pointer)(); 1525 void (_Undefined_class::*_M_member_pointer)(); 1526 }; 1527 1528 union _Any_data 1529 { 1530 void* _M_access() { return &_M_pod_data[0]; } 1531 const void* _M_access() const { return &_M_pod_data[0]; } 1532 1533 template<typename _Tp> 1534 _Tp& 1535 _M_access() 1536 { return *static_cast<_Tp*>(_M_access()); } 1537 1538 template<typename _Tp> 1539 const _Tp& 1540 _M_access() const 1541 { return *static_cast<const _Tp*>(_M_access()); } 1542 1543 _Nocopy_types _M_unused; 1544 char _M_pod_data[sizeof(_Nocopy_types)]; 1545 }; 1546 1547 enum _Manager_operation 1548 { 1549 __get_type_info, 1550 __get_functor_ptr, 1551 __clone_functor, 1552 __destroy_functor 1553 }; 1554 1555 // Simple type wrapper that helps avoid annoying const problems 1556 // when casting between void pointers and pointers-to-pointers. 1557 template<typename _Tp> 1558 struct _Simple_type_wrapper 1559 { 1560 _Simple_type_wrapper(_Tp __value) : __value(__value) { } 1561 1562 _Tp __value; 1563 }; 1564 1565 template<typename _Tp> 1566 struct __is_location_invariant<_Simple_type_wrapper<_Tp> > 1567 : __is_location_invariant<_Tp> 1568 { 1569 }; 1570 1571 // Converts a reference to a function object into a callable 1572 // function object. 1573 template<typename _Functor> 1574 inline _Functor& 1575 __callable_functor(_Functor& __f) 1576 { return __f; } 1577 1578 template<typename _Member, typename _Class> 1579 inline _Mem_fn<_Member _Class::*> 1580 __callable_functor(_Member _Class::* &__p) 1581 { return mem_fn(__p); } 1582 1583 template<typename _Member, typename _Class> 1584 inline _Mem_fn<_Member _Class::*> 1585 __callable_functor(_Member _Class::* const &__p) 1586 { return mem_fn(__p); } 1587 1588 template<typename _Signature> 1589 class function; 1590 1591 /// Base class of all polymorphic function object wrappers. 1592 class _Function_base 1593 { 1594 public: 1595 static const std::size_t _M_max_size = sizeof(_Nocopy_types); 1596 static const std::size_t _M_max_align = __alignof__(_Nocopy_types); 1597 1598 template<typename _Functor> 1599 class _Base_manager 1600 { 1601 protected: 1602 static const bool __stored_locally = 1603 (__is_location_invariant<_Functor>::value 1604 && sizeof(_Functor) <= _M_max_size 1605 && __alignof__(_Functor) <= _M_max_align 1606 && (_M_max_align % __alignof__(_Functor) == 0)); 1607 1608 typedef integral_constant<bool, __stored_locally> _Local_storage; 1609 1610 // Retrieve a pointer to the function object 1611 static _Functor* 1612 _M_get_pointer(const _Any_data& __source) 1613 { 1614 const _Functor* __ptr = 1615 __stored_locally? std::__addressof(__source._M_access<_Functor>()) 1616 /* have stored a pointer */ : __source._M_access<_Functor*>(); 1617 return const_cast<_Functor*>(__ptr); 1618 } 1619 1620 // Clone a location-invariant function object that fits within 1621 // an _Any_data structure. 1622 static void 1623 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type) 1624 { 1625 new (__dest._M_access()) _Functor(__source._M_access<_Functor>()); 1626 } 1627 1628 // Clone a function object that is not location-invariant or 1629 // that cannot fit into an _Any_data structure. 1630 static void 1631 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type) 1632 { 1633 __dest._M_access<_Functor*>() = 1634 new _Functor(*__source._M_access<_Functor*>()); 1635 } 1636 1637 // Destroying a location-invariant object may still require 1638 // destruction. 1639 static void 1640 _M_destroy(_Any_data& __victim, true_type) 1641 { 1642 __victim._M_access<_Functor>().~_Functor(); 1643 } 1644 1645 // Destroying an object located on the heap. 1646 static void 1647 _M_destroy(_Any_data& __victim, false_type) 1648 { 1649 delete __victim._M_access<_Functor*>(); 1650 } 1651 1652 public: 1653 static bool 1654 _M_manager(_Any_data& __dest, const _Any_data& __source, 1655 _Manager_operation __op) 1656 { 1657 switch (__op) 1658 { 1659 #if __cpp_rtti 1660 case __get_type_info: 1661 __dest._M_access<const type_info*>() = &typeid(_Functor); 1662 break; 1663 #endif 1664 case __get_functor_ptr: 1665 __dest._M_access<_Functor*>() = _M_get_pointer(__source); 1666 break; 1667 1668 case __clone_functor: 1669 _M_clone(__dest, __source, _Local_storage()); 1670 break; 1671 1672 case __destroy_functor: 1673 _M_destroy(__dest, _Local_storage()); 1674 break; 1675 } 1676 return false; 1677 } 1678 1679 static void 1680 _M_init_functor(_Any_data& __functor, const _Functor& __f) 1681 { _M_init_functor(__functor, __f, _Local_storage()); } 1682 1683 template<typename _Signature> 1684 static bool 1685 _M_not_empty_function(const function<_Signature>& __f) 1686 { return static_cast<bool>(__f); } 1687 1688 template<typename _Tp> 1689 static bool 1690 _M_not_empty_function(const _Tp*& __fp) 1691 { return __fp; } 1692 1693 template<typename _Class, typename _Tp> 1694 static bool 1695 _M_not_empty_function(_Tp _Class::* const& __mp) 1696 { return __mp; } 1697 1698 template<typename _Tp> 1699 static bool 1700 _M_not_empty_function(const _Tp&) 1701 { return true; } 1702 1703 private: 1704 static void 1705 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type) 1706 { new (__functor._M_access()) _Functor(__f); } 1707 1708 static void 1709 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type) 1710 { __functor._M_access<_Functor*>() = new _Functor(__f); } 1711 }; 1712 1713 template<typename _Functor> 1714 class _Ref_manager : public _Base_manager<_Functor*> 1715 { 1716 typedef _Function_base::_Base_manager<_Functor*> _Base; 1717 1718 public: 1719 static bool 1720 _M_manager(_Any_data& __dest, const _Any_data& __source, 1721 _Manager_operation __op) 1722 { 1723 switch (__op) 1724 { 1725 #if __cpp_rtti 1726 case __get_type_info: 1727 __dest._M_access<const type_info*>() = &typeid(_Functor); 1728 break; 1729 #endif 1730 case __get_functor_ptr: 1731 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source); 1732 return is_const<_Functor>::value; 1733 break; 1734 1735 default: 1736 _Base::_M_manager(__dest, __source, __op); 1737 } 1738 return false; 1739 } 1740 1741 static void 1742 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f) 1743 { 1744 _Base::_M_init_functor(__functor, std::__addressof(__f.get())); 1745 } 1746 }; 1747 1748 _Function_base() : _M_manager(0) { } 1749 1750 ~_Function_base() 1751 { 1752 if (_M_manager) 1753 _M_manager(_M_functor, _M_functor, __destroy_functor); 1754 } 1755 1756 1757 bool _M_empty() const { return !_M_manager; } 1758 1759 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&, 1760 _Manager_operation); 1761 1762 _Any_data _M_functor; 1763 _Manager_type _M_manager; 1764 }; 1765 1766 template<typename _Signature, typename _Functor> 1767 class _Function_handler; 1768 1769 template<typename _Res, typename _Functor, typename... _ArgTypes> 1770 class _Function_handler<_Res(_ArgTypes...), _Functor> 1771 : public _Function_base::_Base_manager<_Functor> 1772 { 1773 typedef _Function_base::_Base_manager<_Functor> _Base; 1774 1775 public: 1776 static _Res 1777 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 1778 { 1779 return (*_Base::_M_get_pointer(__functor))(__args...); 1780 } 1781 }; 1782 1783 template<typename _Functor, typename... _ArgTypes> 1784 class _Function_handler<void(_ArgTypes...), _Functor> 1785 : public _Function_base::_Base_manager<_Functor> 1786 { 1787 typedef _Function_base::_Base_manager<_Functor> _Base; 1788 1789 public: 1790 static void 1791 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 1792 { 1793 (*_Base::_M_get_pointer(__functor))(__args...); 1794 } 1795 }; 1796 1797 template<typename _Res, typename _Functor, typename... _ArgTypes> 1798 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> > 1799 : public _Function_base::_Ref_manager<_Functor> 1800 { 1801 typedef _Function_base::_Ref_manager<_Functor> _Base; 1802 1803 public: 1804 static _Res 1805 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 1806 { 1807 return 1808 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...); 1809 } 1810 }; 1811 1812 template<typename _Functor, typename... _ArgTypes> 1813 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> > 1814 : public _Function_base::_Ref_manager<_Functor> 1815 { 1816 typedef _Function_base::_Ref_manager<_Functor> _Base; 1817 1818 public: 1819 static void 1820 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 1821 { 1822 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...); 1823 } 1824 }; 1825 1826 template<typename _Class, typename _Member, typename _Res, 1827 typename... _ArgTypes> 1828 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*> 1829 : public _Function_handler<void(_ArgTypes...), _Member _Class::*> 1830 { 1831 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*> 1832 _Base; 1833 1834 public: 1835 static _Res 1836 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 1837 { 1838 return tr1:: 1839 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...); 1840 } 1841 }; 1842 1843 template<typename _Class, typename _Member, typename... _ArgTypes> 1844 class _Function_handler<void(_ArgTypes...), _Member _Class::*> 1845 : public _Function_base::_Base_manager< 1846 _Simple_type_wrapper< _Member _Class::* > > 1847 { 1848 typedef _Member _Class::* _Functor; 1849 typedef _Simple_type_wrapper<_Functor> _Wrapper; 1850 typedef _Function_base::_Base_manager<_Wrapper> _Base; 1851 1852 public: 1853 static bool 1854 _M_manager(_Any_data& __dest, const _Any_data& __source, 1855 _Manager_operation __op) 1856 { 1857 switch (__op) 1858 { 1859 #if __cpp_rtti 1860 case __get_type_info: 1861 __dest._M_access<const type_info*>() = &typeid(_Functor); 1862 break; 1863 #endif 1864 case __get_functor_ptr: 1865 __dest._M_access<_Functor*>() = 1866 &_Base::_M_get_pointer(__source)->__value; 1867 break; 1868 1869 default: 1870 _Base::_M_manager(__dest, __source, __op); 1871 } 1872 return false; 1873 } 1874 1875 static void 1876 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 1877 { 1878 tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...); 1879 } 1880 }; 1881 1882 /// class function 1883 template<typename _Res, typename... _ArgTypes> 1884 class function<_Res(_ArgTypes...)> 1885 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>, 1886 private _Function_base 1887 { 1888 #if __cplusplus < 201103L 1889 /// This class is used to implement the safe_bool idiom. 1890 struct _Hidden_type 1891 { 1892 _Hidden_type* _M_bool; 1893 }; 1894 1895 /// This typedef is used to implement the safe_bool idiom. 1896 typedef _Hidden_type* _Hidden_type::* _Safe_bool; 1897 #endif 1898 1899 typedef _Res _Signature_type(_ArgTypes...); 1900 1901 struct _Useless { }; 1902 1903 public: 1904 typedef _Res result_type; 1905 1906 // [3.7.2.1] construct/copy/destroy 1907 1908 /** 1909 * @brief Default construct creates an empty function call wrapper. 1910 * @post @c !(bool)*this 1911 */ 1912 function() : _Function_base() { } 1913 1914 /** 1915 * @brief Default construct creates an empty function call wrapper. 1916 * @post @c !(bool)*this 1917 */ 1918 function(_M_clear_type*) : _Function_base() { } 1919 1920 /** 1921 * @brief %Function copy constructor. 1922 * @param x A %function object with identical call signature. 1923 * @post @c (bool)*this == (bool)x 1924 * 1925 * The newly-created %function contains a copy of the target of @a 1926 * x (if it has one). 1927 */ 1928 function(const function& __x); 1929 1930 /** 1931 * @brief Builds a %function that targets a copy of the incoming 1932 * function object. 1933 * @param f A %function object that is callable with parameters of 1934 * type @c T1, @c T2, ..., @c TN and returns a value convertible 1935 * to @c Res. 1936 * 1937 * The newly-created %function object will target a copy of @a 1938 * f. If @a f is @c reference_wrapper<F>, then this function 1939 * object will contain a reference to the function object @c 1940 * f.get(). If @a f is a NULL function pointer or NULL 1941 * pointer-to-member, the newly-created object will be empty. 1942 * 1943 * If @a f is a non-NULL function pointer or an object of type @c 1944 * reference_wrapper<F>, this function will not throw. 1945 */ 1946 template<typename _Functor> 1947 function(_Functor __f, 1948 typename __gnu_cxx::__enable_if< 1949 !is_integral<_Functor>::value, _Useless>::__type 1950 = _Useless()); 1951 1952 /** 1953 * @brief %Function assignment operator. 1954 * @param x A %function with identical call signature. 1955 * @post @c (bool)*this == (bool)x 1956 * @returns @c *this 1957 * 1958 * The target of @a x is copied to @c *this. If @a x has no 1959 * target, then @c *this will be empty. 1960 * 1961 * If @a x targets a function pointer or a reference to a function 1962 * object, then this operation will not throw an %exception. 1963 */ 1964 function& 1965 operator=(const function& __x) 1966 { 1967 function(__x).swap(*this); 1968 return *this; 1969 } 1970 1971 /** 1972 * @brief %Function assignment to zero. 1973 * @post @c !(bool)*this 1974 * @returns @c *this 1975 * 1976 * The target of @c *this is deallocated, leaving it empty. 1977 */ 1978 function& 1979 operator=(_M_clear_type*) 1980 { 1981 if (_M_manager) 1982 { 1983 _M_manager(_M_functor, _M_functor, __destroy_functor); 1984 _M_manager = 0; 1985 _M_invoker = 0; 1986 } 1987 return *this; 1988 } 1989 1990 /** 1991 * @brief %Function assignment to a new target. 1992 * @param f A %function object that is callable with parameters of 1993 * type @c T1, @c T2, ..., @c TN and returns a value convertible 1994 * to @c Res. 1995 * @return @c *this 1996 * 1997 * This %function object wrapper will target a copy of @a 1998 * f. If @a f is @c reference_wrapper<F>, then this function 1999 * object will contain a reference to the function object @c 2000 * f.get(). If @a f is a NULL function pointer or NULL 2001 * pointer-to-member, @c this object will be empty. 2002 * 2003 * If @a f is a non-NULL function pointer or an object of type @c 2004 * reference_wrapper<F>, this function will not throw. 2005 */ 2006 template<typename _Functor> 2007 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value, 2008 function&>::__type 2009 operator=(_Functor __f) 2010 { 2011 function(__f).swap(*this); 2012 return *this; 2013 } 2014 2015 // [3.7.2.2] function modifiers 2016 2017 /** 2018 * @brief Swap the targets of two %function objects. 2019 * @param f A %function with identical call signature. 2020 * 2021 * Swap the targets of @c this function object and @a f. This 2022 * function will not throw an %exception. 2023 */ 2024 void swap(function& __x) 2025 { 2026 std::swap(_M_functor, __x._M_functor); 2027 std::swap(_M_manager, __x._M_manager); 2028 std::swap(_M_invoker, __x._M_invoker); 2029 } 2030 2031 // [3.7.2.3] function capacity 2032 2033 /** 2034 * @brief Determine if the %function wrapper has a target. 2035 * 2036 * @return @c true when this %function object contains a target, 2037 * or @c false when it is empty. 2038 * 2039 * This function will not throw an %exception. 2040 */ 2041 #if __cplusplus >= 201103L 2042 explicit operator bool() const 2043 { return !_M_empty(); } 2044 #else 2045 operator _Safe_bool() const 2046 { 2047 if (_M_empty()) 2048 return 0; 2049 else 2050 return &_Hidden_type::_M_bool; 2051 } 2052 #endif 2053 2054 // [3.7.2.4] function invocation 2055 2056 /** 2057 * @brief Invokes the function targeted by @c *this. 2058 * @returns the result of the target. 2059 * @throws bad_function_call when @c !(bool)*this 2060 * 2061 * The function call operator invokes the target function object 2062 * stored by @c this. 2063 */ 2064 _Res operator()(_ArgTypes... __args) const; 2065 2066 #if __cpp_rtti 2067 // [3.7.2.5] function target access 2068 /** 2069 * @brief Determine the type of the target of this function object 2070 * wrapper. 2071 * 2072 * @returns the type identifier of the target function object, or 2073 * @c typeid(void) if @c !(bool)*this. 2074 * 2075 * This function will not throw an %exception. 2076 */ 2077 const type_info& target_type() const; 2078 2079 /** 2080 * @brief Access the stored target function object. 2081 * 2082 * @return Returns a pointer to the stored target function object, 2083 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL 2084 * pointer. 2085 * 2086 * This function will not throw an %exception. 2087 */ 2088 template<typename _Functor> _Functor* target(); 2089 2090 /// @overload 2091 template<typename _Functor> const _Functor* target() const; 2092 #endif 2093 2094 private: 2095 // [3.7.2.6] undefined operators 2096 template<typename _Function> 2097 void operator==(const function<_Function>&) const; 2098 template<typename _Function> 2099 void operator!=(const function<_Function>&) const; 2100 2101 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...); 2102 _Invoker_type _M_invoker; 2103 }; 2104 #pragma GCC diagnostic pop 2105 2106 template<typename _Res, typename... _ArgTypes> 2107 function<_Res(_ArgTypes...)>:: 2108 function(const function& __x) 2109 : _Function_base() 2110 { 2111 if (static_cast<bool>(__x)) 2112 { 2113 __x._M_manager(_M_functor, __x._M_functor, __clone_functor); 2114 _M_invoker = __x._M_invoker; 2115 _M_manager = __x._M_manager; 2116 } 2117 } 2118 2119 template<typename _Res, typename... _ArgTypes> 2120 template<typename _Functor> 2121 function<_Res(_ArgTypes...)>:: 2122 function(_Functor __f, 2123 typename __gnu_cxx::__enable_if< 2124 !is_integral<_Functor>::value, _Useless>::__type) 2125 : _Function_base() 2126 { 2127 typedef _Function_handler<_Signature_type, _Functor> _My_handler; 2128 2129 if (_My_handler::_M_not_empty_function(__f)) 2130 { 2131 _My_handler::_M_init_functor(_M_functor, __f); 2132 _M_invoker = &_My_handler::_M_invoke; 2133 _M_manager = &_My_handler::_M_manager; 2134 } 2135 } 2136 2137 template<typename _Res, typename... _ArgTypes> 2138 _Res 2139 function<_Res(_ArgTypes...)>:: 2140 operator()(_ArgTypes... __args) const 2141 { 2142 if (_M_empty()) 2143 _GLIBCXX_THROW_OR_ABORT(bad_function_call()); 2144 return _M_invoker(_M_functor, __args...); 2145 } 2146 2147 #if __cpp_rtti 2148 template<typename _Res, typename... _ArgTypes> 2149 const type_info& 2150 function<_Res(_ArgTypes...)>:: 2151 target_type() const 2152 { 2153 if (_M_manager) 2154 { 2155 _Any_data __typeinfo_result; 2156 _M_manager(__typeinfo_result, _M_functor, __get_type_info); 2157 return *__typeinfo_result._M_access<const type_info*>(); 2158 } 2159 else 2160 return typeid(void); 2161 } 2162 2163 template<typename _Res, typename... _ArgTypes> 2164 template<typename _Functor> 2165 _Functor* 2166 function<_Res(_ArgTypes...)>:: 2167 target() 2168 { 2169 if (typeid(_Functor) == target_type() && _M_manager) 2170 { 2171 _Any_data __ptr; 2172 if (_M_manager(__ptr, _M_functor, __get_functor_ptr) 2173 && !is_const<_Functor>::value) 2174 return 0; 2175 else 2176 return __ptr._M_access<_Functor*>(); 2177 } 2178 else 2179 return 0; 2180 } 2181 2182 template<typename _Res, typename... _ArgTypes> 2183 template<typename _Functor> 2184 const _Functor* 2185 function<_Res(_ArgTypes...)>:: 2186 target() const 2187 { 2188 if (typeid(_Functor) == target_type() && _M_manager) 2189 { 2190 _Any_data __ptr; 2191 _M_manager(__ptr, _M_functor, __get_functor_ptr); 2192 return __ptr._M_access<const _Functor*>(); 2193 } 2194 else 2195 return 0; 2196 } 2197 #endif 2198 2199 // [3.7.2.7] null pointer comparisons 2200 2201 /** 2202 * @brief Compares a polymorphic function object wrapper against 0 2203 * (the NULL pointer). 2204 * @returns @c true if the wrapper has no target, @c false otherwise 2205 * 2206 * This function will not throw an %exception. 2207 */ 2208 template<typename _Signature> 2209 inline bool 2210 operator==(const function<_Signature>& __f, _M_clear_type*) 2211 { return !static_cast<bool>(__f); } 2212 2213 /// @overload 2214 template<typename _Signature> 2215 inline bool 2216 operator==(_M_clear_type*, const function<_Signature>& __f) 2217 { return !static_cast<bool>(__f); } 2218 2219 /** 2220 * @brief Compares a polymorphic function object wrapper against 0 2221 * (the NULL pointer). 2222 * @returns @c false if the wrapper has no target, @c true otherwise 2223 * 2224 * This function will not throw an %exception. 2225 */ 2226 template<typename _Signature> 2227 inline bool 2228 operator!=(const function<_Signature>& __f, _M_clear_type*) 2229 { return static_cast<bool>(__f); } 2230 2231 /// @overload 2232 template<typename _Signature> 2233 inline bool 2234 operator!=(_M_clear_type*, const function<_Signature>& __f) 2235 { return static_cast<bool>(__f); } 2236 2237 // [3.7.2.8] specialized algorithms 2238 2239 /** 2240 * @brief Swap the targets of two polymorphic function object wrappers. 2241 * 2242 * This function will not throw an %exception. 2243 */ 2244 template<typename _Signature> 2245 inline void 2246 swap(function<_Signature>& __x, function<_Signature>& __y) 2247 { __x.swap(__y); } 2248 } 2249 2250 #if __cplusplus >= 201103L 2251 // Specialize std::is_bind_expression for tr1::bind closure types, 2252 // so that they can also work with std::bind. 2253 2254 template<typename _Signature> 2255 struct is_bind_expression<tr1::_Bind<_Signature>> 2256 : true_type { }; 2257 2258 template<typename _Signature> 2259 struct is_bind_expression<const tr1::_Bind<_Signature>> 2260 : true_type { }; 2261 2262 template<typename _Signature> 2263 struct is_bind_expression<volatile tr1::_Bind<_Signature>> 2264 : true_type { }; 2265 2266 template<typename _Signature> 2267 struct is_bind_expression<const volatile tr1::_Bind<_Signature>> 2268 : true_type { }; 2269 2270 template<typename _Result, typename _Signature> 2271 struct is_bind_expression<tr1::_Bind_result<_Result, _Signature>> 2272 : true_type { }; 2273 2274 template<typename _Result, typename _Signature> 2275 struct is_bind_expression<const tr1::_Bind_result<_Result, _Signature>> 2276 : true_type { }; 2277 2278 template<typename _Result, typename _Signature> 2279 struct is_bind_expression<volatile tr1::_Bind_result<_Result, _Signature>> 2280 : true_type { }; 2281 2282 template<typename _Result, typename _Signature> 2283 struct is_bind_expression<const volatile tr1::_Bind_result<_Result, 2284 _Signature>> 2285 : true_type { }; 2286 2287 #endif // C++11 2288 _GLIBCXX_END_NAMESPACE_VERSION 2289 } 2290 2291 #pragma GCC diagnostic pop 2292 #endif // _GLIBCXX_TR1_FUNCTIONAL