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