1// TR1 functional -*- C++ -*- 2 3// Copyright (C) 2005, 2006 Free Software Foundation, Inc. 4// Written by Douglas Gregor <doug.gregor -at- gmail.com> 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 2, 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// You should have received a copy of the GNU General Public License along 18// with this library; see the file COPYING. If not, write to the Free 19// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 20// USA. 21 22// As a special exception, you may use this file as part of a free software 23// library without restriction. Specifically, if other files instantiate 24// templates or use macros or inline functions from this file, or you compile 25// this file and link it with other files to produce an executable, this 26// file does not by itself cause the resulting executable to be covered by 27// the GNU General Public License. This exception does not however 28// invalidate any other reasons why the executable file might be covered by 29// the GNU General Public License. 30 31/** @file functional_iterate.h 32 * This is an internal header file, included by other library headers. 33 * You should not attempt to use it directly. 34 */ 35 36template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 37 struct _Weak_result_type_impl<_Res(_GLIBCXX_TEMPLATE_ARGS)> 38 { 39 typedef _Res result_type; 40 }; 41 42template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 43 struct _Weak_result_type_impl<_Res (&)(_GLIBCXX_TEMPLATE_ARGS)> 44 { 45 typedef _Res result_type; 46 }; 47 48template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 49 struct _Weak_result_type_impl<_Res (*)(_GLIBCXX_TEMPLATE_ARGS)> 50 { 51 typedef _Res result_type; 52 }; 53 54#if _GLIBCXX_NUM_ARGS > 0 55template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED 56 _GLIBCXX_TEMPLATE_PARAMS_SHIFTED> 57 struct _Weak_result_type_impl< 58 _Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED)> 59 { 60 typedef _Res result_type; 61 }; 62 63template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED 64 _GLIBCXX_TEMPLATE_PARAMS_SHIFTED> 65 struct _Weak_result_type_impl< 66 _Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) const> 67 { 68 typedef _Res result_type; 69 }; 70 71template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED 72 _GLIBCXX_TEMPLATE_PARAMS_SHIFTED> 73 struct _Weak_result_type_impl< 74 _Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) volatile> 75 { 76 typedef _Res result_type; 77 }; 78 79template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED 80 _GLIBCXX_TEMPLATE_PARAMS_SHIFTED> 81 struct _Weak_result_type_impl< 82 _Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) const volatile> 83 { 84 typedef _Res result_type; 85 }; 86#endif 87 88template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 89 class result_of<_Functor(_GLIBCXX_TEMPLATE_ARGS)> 90 : public _Result_of_impl< 91 _Has_result_type<_Weak_result_type<_Functor> >::value, 92 _Functor(_GLIBCXX_TEMPLATE_ARGS)> 93 { }; 94 95template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 96 struct _Result_of_impl<true, _Functor(_GLIBCXX_TEMPLATE_ARGS)> 97 { 98 typedef typename _Weak_result_type<_Functor>::result_type type; 99 }; 100 101template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 102 struct _Result_of_impl<false, _Functor(_GLIBCXX_TEMPLATE_ARGS)> 103 { 104#if _GLIBCXX_NUM_ARGS > 0 105 typedef typename _Functor 106 ::template result<_Functor(_GLIBCXX_TEMPLATE_ARGS)>::type type; 107#else 108 typedef void type; 109#endif 110 }; 111 112/** 113 * @if maint 114 * Invoke a function object, which may be either a member pointer or a 115 * function object. The first parameter will tell which. 116 * @endif 117 */ 118template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 119 inline 120 typename __enable_if< 121 typename result_of<_Functor(_GLIBCXX_TEMPLATE_ARGS)>::type, 122 (!is_member_pointer<_Functor>::value 123 && !is_function<_Functor>::value 124 && !is_function<typename remove_pointer<_Functor>::type>::value) 125 >::__type 126 __invoke(_Functor& __f _GLIBCXX_COMMA _GLIBCXX_REF_PARAMS) 127 { 128 return __f(_GLIBCXX_ARGS); 129 } 130 131#if _GLIBCXX_NUM_ARGS > 0 132template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 133 inline 134 typename __enable_if< 135 typename result_of<_Functor(_GLIBCXX_TEMPLATE_ARGS)>::type, 136 (is_member_pointer<_Functor>::value 137 && !is_function<_Functor>::value 138 && !is_function<typename remove_pointer<_Functor>::type>::value) 139 >::__type 140 __invoke(_Functor& __f _GLIBCXX_COMMA _GLIBCXX_REF_PARAMS) 141 { 142 return mem_fn(__f)(_GLIBCXX_ARGS); 143 } 144#endif 145 146// To pick up function references (that will become function pointers) 147template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 148 inline 149 typename __enable_if< 150 typename result_of<_Functor(_GLIBCXX_TEMPLATE_ARGS)>::type, 151 (is_pointer<_Functor>::value 152 && is_function<typename remove_pointer<_Functor>::type>::value) 153 >::__type 154 __invoke(_Functor __f _GLIBCXX_COMMA _GLIBCXX_REF_PARAMS) 155 { 156 return __f(_GLIBCXX_ARGS); 157 } 158 159/** 160 * @if maint 161 * Implementation of reference_wrapper::operator() 162 * @endif 163*/ 164#if _GLIBCXX_NUM_ARGS > 0 165template<typename _Tp> 166template<_GLIBCXX_TEMPLATE_PARAMS> 167 typename result_of< 168 typename reference_wrapper<_Tp>::_M_func_type(_GLIBCXX_TEMPLATE_ARGS)>::type 169 reference_wrapper<_Tp>::operator()(_GLIBCXX_REF_PARAMS) const 170 { 171 return __invoke(get(), _GLIBCXX_ARGS); 172 } 173#endif 174 175#if _GLIBCXX_NUM_ARGS > 0 176template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED 177 _GLIBCXX_TEMPLATE_PARAMS_SHIFTED> 178 class _Mem_fn<_Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED)> 179#if _GLIBCXX_NUM_ARGS == 1 180 : public unary_function<_Class*, _Res> 181#elif _GLIBCXX_NUM_ARGS == 2 182 : public binary_function<_Class*, _T1, _Res> 183#endif 184 { 185 typedef _Res (_Class::*_Functor)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED); 186 187 template<typename _Tp> 188 _Res 189 _M_call(_Tp& __object, const volatile _Class * _GLIBCXX_COMMA_SHIFTED 190 _GLIBCXX_PARAMS_SHIFTED) const 191 { return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 192 193 template<typename _Tp> 194 _Res 195 _M_call(_Tp& __ptr, const volatile void * _GLIBCXX_COMMA_SHIFTED 196 _GLIBCXX_PARAMS_SHIFTED) const 197 { return ((*__ptr).*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 198 199 public: 200 typedef _Res result_type; 201 202 explicit _Mem_fn(_Functor __pf) : __pmf(__pf) { } 203 204 // Handle objects 205 _Res 206 operator()(_Class& __object _GLIBCXX_COMMA_SHIFTED 207 _GLIBCXX_PARAMS_SHIFTED) const 208 { return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 209 210 // Handle pointers 211 _Res 212 operator()(_Class* __object _GLIBCXX_COMMA_SHIFTED 213 _GLIBCXX_PARAMS_SHIFTED) const 214 { return (__object->*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 215 216 // Handle smart pointers, references and pointers to derived 217 template<typename _Tp> 218 _Res 219 operator()(_Tp& __object _GLIBCXX_COMMA_SHIFTED 220 _GLIBCXX_PARAMS_SHIFTED) const 221 { 222 return _M_call(__object, &__object _GLIBCXX_COMMA_SHIFTED 223 _GLIBCXX_ARGS_SHIFTED); 224 } 225 226 private: 227 _Functor __pmf; 228 }; 229 230template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED 231 _GLIBCXX_TEMPLATE_PARAMS_SHIFTED> 232 class _Mem_fn<_Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) const> 233#if _GLIBCXX_NUM_ARGS == 1 234 : public unary_function<const _Class*, _Res> 235#elif _GLIBCXX_NUM_ARGS == 2 236 : public binary_function<const _Class*, _T1, _Res> 237#endif 238 { 239 typedef _Res (_Class::*_Functor)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) const; 240 241 template<typename _Tp> 242 _Res 243 _M_call(_Tp& __object, const volatile _Class * _GLIBCXX_COMMA_SHIFTED 244 _GLIBCXX_PARAMS_SHIFTED) const 245 { return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 246 247 template<typename _Tp> 248 _Res 249 _M_call(_Tp& __ptr, const volatile void * _GLIBCXX_COMMA_SHIFTED 250 _GLIBCXX_PARAMS_SHIFTED) const 251 { return ((*__ptr).*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 252 253 public: 254 typedef _Res result_type; 255 256 explicit _Mem_fn(_Functor __pf) : __pmf(__pf) { } 257 258 // Handle objects 259 _Res 260 operator()(const _Class& __object _GLIBCXX_COMMA_SHIFTED 261 _GLIBCXX_PARAMS_SHIFTED) const 262 { return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 263 264 // Handle pointers 265 _Res 266 operator()(const _Class* __object _GLIBCXX_COMMA_SHIFTED 267 _GLIBCXX_PARAMS_SHIFTED) const 268 { return (__object->*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 269 270 // Handle smart pointers, references and pointers to derived 271 template<typename _Tp> 272 _Res 273 operator()(_Tp& __object _GLIBCXX_COMMA_SHIFTED 274 _GLIBCXX_PARAMS_SHIFTED) const 275 { 276 return _M_call(__object, &__object _GLIBCXX_COMMA_SHIFTED 277 _GLIBCXX_ARGS_SHIFTED); 278 } 279 280 private: 281 _Functor __pmf; 282 }; 283 284template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED 285 _GLIBCXX_TEMPLATE_PARAMS_SHIFTED> 286 class _Mem_fn<_Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) volatile> 287#if _GLIBCXX_NUM_ARGS == 1 288 : public unary_function<volatile _Class*, _Res> 289#elif _GLIBCXX_NUM_ARGS == 2 290 : public binary_function<volatile _Class*, _T1, _Res> 291#endif 292 { 293 typedef _Res (_Class::*_Functor)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) volatile; 294 295 template<typename _Tp> 296 _Res 297 _M_call(_Tp& __object, const volatile _Class * _GLIBCXX_COMMA_SHIFTED 298 _GLIBCXX_PARAMS_SHIFTED) const 299 { return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 300 301 template<typename _Tp> 302 _Res 303 _M_call(_Tp& __ptr, const volatile void * _GLIBCXX_COMMA_SHIFTED 304 _GLIBCXX_PARAMS_SHIFTED) const 305 { return ((*__ptr).*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 306 307 public: 308 typedef _Res result_type; 309 310 explicit _Mem_fn(_Functor __pf) : __pmf(__pf) { } 311 312 // Handle objects 313 _Res 314 operator()(volatile _Class& __object _GLIBCXX_COMMA_SHIFTED 315 _GLIBCXX_PARAMS_SHIFTED) const 316 { return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 317 318 // Handle pointers 319 _Res 320 operator()(volatile _Class* __object _GLIBCXX_COMMA_SHIFTED 321 _GLIBCXX_PARAMS_SHIFTED) const 322 { return (__object->*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 323 324 // Handle smart pointers, references and pointers to derived 325 template<typename _Tp> 326 _Res 327 operator()(_Tp& __object _GLIBCXX_COMMA_SHIFTED 328 _GLIBCXX_PARAMS_SHIFTED) const 329 { 330 return _M_call(__object, &__object _GLIBCXX_COMMA_SHIFTED 331 _GLIBCXX_ARGS_SHIFTED); 332 } 333 private: 334 _Functor __pmf; 335 }; 336 337template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED 338 _GLIBCXX_TEMPLATE_PARAMS_SHIFTED> 339 class _Mem_fn<_Res(_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) const volatile> 340#if _GLIBCXX_NUM_ARGS == 1 341 : public unary_function<const volatile _Class*, _Res> 342#elif _GLIBCXX_NUM_ARGS == 2 343 : public binary_function<const volatile _Class*, _T1, _Res> 344#endif 345 { 346 typedef _Res (_Class::*_Functor)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) 347 const volatile; 348 349 template<typename _Tp> 350 _Res 351 _M_call(_Tp& __object, const volatile _Class * _GLIBCXX_COMMA_SHIFTED 352 _GLIBCXX_PARAMS_SHIFTED) const 353 { return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 354 355 template<typename _Tp> 356 _Res 357 _M_call(_Tp& __ptr, const volatile void * _GLIBCXX_COMMA_SHIFTED 358 _GLIBCXX_PARAMS_SHIFTED) const 359 { return ((*__ptr).*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 360 361 public: 362 typedef _Res result_type; 363 364 explicit _Mem_fn(_Functor __pf) : __pmf(__pf) { } 365 366 // Handle objects 367 _Res 368 operator()(const volatile _Class& __object _GLIBCXX_COMMA_SHIFTED 369 _GLIBCXX_PARAMS_SHIFTED) const 370 { return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 371 372 // Handle pointers 373 _Res 374 operator()(const volatile _Class* __object _GLIBCXX_COMMA_SHIFTED 375 _GLIBCXX_PARAMS_SHIFTED) const 376 { return (__object->*__pmf)(_GLIBCXX_ARGS_SHIFTED); } 377 378 // Handle smart pointers, references and pointers to derived 379 template<typename _Tp> 380 _Res 381 operator()(_Tp& __object _GLIBCXX_COMMA_SHIFTED 382 _GLIBCXX_PARAMS_SHIFTED) const 383 { 384 return _M_call(__object, &__object _GLIBCXX_COMMA_SHIFTED 385 _GLIBCXX_ARGS_SHIFTED); 386 } 387 388 private: 389 _Functor __pmf; 390 }; 391#endif 392 393#if _GLIBCXX_NUM_ARGS > 0 394namespace placeholders 395{ 396namespace 397{ 398 _Placeholder<_GLIBCXX_NUM_ARGS> _GLIBCXX_JOIN(_,_GLIBCXX_NUM_ARGS); 399} 400} 401#endif 402 403template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 404class _Bind<_Functor(_GLIBCXX_TEMPLATE_ARGS)> 405 : public _Weak_result_type<_Functor> 406{ 407 typedef _Bind __self_type; 408 409 _Functor _M_f; 410 _GLIBCXX_BIND_MEMBERS 411 412 public: 413#if _GLIBCXX_NUM_ARGS == 0 414 explicit 415#endif 416 _Bind(_Functor __f _GLIBCXX_COMMA _GLIBCXX_PARAMS) 417 : _M_f(__f) _GLIBCXX_COMMA _GLIBCXX_BIND_MEMBERS_INIT { } 418 419#define _GLIBCXX_BIND_REPEAT_HEADER <tr1/bind_iterate.h> 420#include <tr1/bind_repeat.h> 421#undef _GLIBCXX_BIND_REPEAT_HEADER 422}; 423 424template<typename _Result, typename _Functor 425 _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 426class _Bind_result<_Result, _Functor(_GLIBCXX_TEMPLATE_ARGS)> 427{ 428 _Functor _M_f; 429 _GLIBCXX_BIND_MEMBERS 430 431 public: 432 typedef _Result result_type; 433 434#if _GLIBCXX_NUM_ARGS == 0 435 explicit 436#endif 437 _Bind_result(_Functor __f _GLIBCXX_COMMA _GLIBCXX_PARAMS) 438 : _M_f(__f) _GLIBCXX_COMMA _GLIBCXX_BIND_MEMBERS_INIT { } 439 440#define _GLIBCXX_BIND_REPEAT_HEADER <tr1/bind_iterate.h> 441#define _GLIBCXX_BIND_HAS_RESULT_TYPE 442#include <tr1/bind_repeat.h> 443#undef _GLIBCXX_BIND_HAS_RESULT_TYPE 444#undef _GLIBCXX_BIND_REPEAT_HEADER 445}; 446 447// Handle arbitrary function objects 448template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 449inline 450_Bind<typename _Maybe_wrap_member_pointer<_Functor>::type 451 (_GLIBCXX_TEMPLATE_ARGS)> 452bind(_Functor __f _GLIBCXX_COMMA _GLIBCXX_PARAMS) 453{ 454 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type; 455 typedef typename __maybe_type::type __functor_type; 456 typedef _Bind<__functor_type(_GLIBCXX_TEMPLATE_ARGS)> __result_type; 457 return __result_type(__maybe_type::__do_wrap(__f) 458 _GLIBCXX_COMMA _GLIBCXX_ARGS); 459} 460 461template<typename _Result, typename _Functor 462 _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 463inline 464_Bind_result<_Result, 465 typename _Maybe_wrap_member_pointer<_Functor>::type 466 (_GLIBCXX_TEMPLATE_ARGS)> 467bind(_Functor __f _GLIBCXX_COMMA _GLIBCXX_PARAMS) 468{ 469 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type; 470 typedef typename __maybe_type::type __functor_type; 471 typedef _Bind_result<_Result, __functor_type(_GLIBCXX_TEMPLATE_ARGS)> 472 __result_type; 473 return __result_type(__maybe_type::__do_wrap(__f) 474 _GLIBCXX_COMMA _GLIBCXX_ARGS); 475} 476 477template<typename _Res, typename _Functor _GLIBCXX_COMMA 478 _GLIBCXX_TEMPLATE_PARAMS> 479class _Function_handler<_Res(_GLIBCXX_TEMPLATE_ARGS), _Functor> 480 : public _Function_base::_Base_manager<_Functor> 481{ 482 typedef _Function_base::_Base_manager<_Functor> _Base; 483 484 public: 485 static _Res 486 _M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS) 487 { 488 return (*_Base::_M_get_pointer(__functor))(_GLIBCXX_ARGS); 489 } 490}; 491 492template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 493class _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS), _Functor> 494 : public _Function_base::_Base_manager<_Functor> 495{ 496 typedef _Function_base::_Base_manager<_Functor> _Base; 497 498 public: 499 static void 500 _M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS) 501 { 502 (*_Base::_M_get_pointer(__functor))(_GLIBCXX_ARGS); 503 } 504}; 505 506template<typename _Res, typename _Functor _GLIBCXX_COMMA 507 _GLIBCXX_TEMPLATE_PARAMS> 508class _Function_handler<_Res(_GLIBCXX_TEMPLATE_ARGS), 509 reference_wrapper<_Functor> > 510 : public _Function_base::_Ref_manager<_Functor> 511{ 512 typedef _Function_base::_Ref_manager<_Functor> _Base; 513 514 public: 515 static _Res 516 _M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS) 517 { 518 return __callable_functor(**_Base::_M_get_pointer(__functor)) 519 (_GLIBCXX_ARGS); 520 } 521}; 522 523template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 524class _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS), 525 reference_wrapper<_Functor> > 526 : public _Function_base::_Ref_manager<_Functor> 527{ 528 typedef _Function_base::_Ref_manager<_Functor> _Base; 529 530 public: 531 static void 532 _M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS) 533 { 534 __callable_functor(**_Base::_M_get_pointer(__functor))(_GLIBCXX_ARGS); 535 } 536}; 537 538template<typename _Class, typename _Member, typename _Res 539 _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 540class _Function_handler<_Res(_GLIBCXX_TEMPLATE_ARGS), _Member _Class::*> 541 : public _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS), _Member _Class::*> 542{ 543 typedef _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS), _Member _Class::*> 544 _Base; 545 546 public: 547 static _Res 548 _M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS) 549 { 550 return std::tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value) 551 (_GLIBCXX_ARGS); 552 } 553}; 554 555template<typename _Class, typename _Member 556 _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 557class _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS), _Member _Class::*> 558 : public _Function_base::_Base_manager< 559 _Simple_type_wrapper< _Member _Class::* > > 560{ 561 typedef _Member _Class::* _Functor; 562 typedef _Simple_type_wrapper< _Functor > _Wrapper; 563 typedef _Function_base::_Base_manager<_Wrapper> _Base; 564 565 public: 566 static bool 567 _M_manager(_Any_data& __dest, const _Any_data& __source, 568 _Manager_operation __op) 569 { 570 switch (__op) { 571 case __get_type_info: 572 __dest._M_access<const type_info*>() = &typeid(_Functor); 573 break; 574 575 case __get_functor_ptr: 576 __dest._M_access<_Functor*>() = 577 &_Base::_M_get_pointer(__source)->__value; 578 break; 579 580 default: 581 _Base::_M_manager(__dest, __source, __op); 582 } 583 return false; 584 } 585 586 static void 587 _M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS) 588 { 589 std::tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value) 590 (_GLIBCXX_ARGS); 591 } 592}; 593 594template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 595class function<_Res(_GLIBCXX_TEMPLATE_ARGS)> 596#if _GLIBCXX_NUM_ARGS == 1 597 : public unary_function<_T1, _Res>, private _Function_base 598#elif _GLIBCXX_NUM_ARGS == 2 599 : public binary_function<_T1, _T2, _Res>, private _Function_base 600#else 601 : private _Function_base 602#endif 603{ 604 /** 605 * @if maint 606 * This class is used to implement the safe_bool idiom. 607 * @endif 608 */ 609 struct _Hidden_type 610 { 611 _Hidden_type* _M_bool; 612 }; 613 614 /** 615 * @if maint 616 * This typedef is used to implement the safe_bool idiom. 617 * @endif 618 */ 619 typedef _Hidden_type* _Hidden_type::* _Safe_bool; 620 621 typedef _Res _Signature_type(_GLIBCXX_TEMPLATE_ARGS); 622 623 struct _Useless {}; 624 625 public: 626 typedef _Res result_type; 627 628 // [3.7.2.1] construct/copy/destroy 629 630 /** 631 * @brief Default construct creates an empty function call wrapper. 632 * @post @c !(bool)*this 633 */ 634 function() : _Function_base() { } 635 636 /** 637 * @brief Default construct creates an empty function call wrapper. 638 * @post @c !(bool)*this 639 */ 640 function(_M_clear_type*) : _Function_base() { } 641 642 /** 643 * @brief %Function copy constructor. 644 * @param x A %function object with identical call signature. 645 * @pre @c (bool)*this == (bool)x 646 * 647 * The newly-created %function contains a copy of the target of @a 648 * x (if it has one). 649 */ 650 function(const function& __x); 651 652 /** 653 * @brief Builds a %function that targets a copy of the incoming 654 * function object. 655 * @param f A %function object that is callable with parameters of 656 * type @c T1, @c T2, ..., @c TN and returns a value convertible 657 * to @c Res. 658 * 659 * The newly-created %function object will target a copy of @a 660 * f. If @a f is @c reference_wrapper<F>, then this function 661 * object will contain a reference to the function object @c 662 * f.get(). If @a f is a NULL function pointer or NULL 663 * pointer-to-member, the newly-created object will be empty. 664 * 665 * If @a f is a non-NULL function pointer or an object of type @c 666 * reference_wrapper<F>, this function will not throw. 667 */ 668 template<typename _Functor> 669 function(_Functor __f, 670 typename __enable_if<_Useless, 671 !is_integral<_Functor>::value>::__type 672 = _Useless()); 673 674 /** 675 * @brief %Function assignment operator. 676 * @param x A %function with identical call signature. 677 * @post @c (bool)*this == (bool)x 678 * @returns @c *this 679 * 680 * The target of @a x is copied to @c *this. If @a x has no 681 * target, then @c *this will be empty. 682 * 683 * If @a x targets a function pointer or a reference to a function 684 * object, then this operation will not throw an exception. 685 */ 686 function& operator=(const function& __x) 687 { 688 function(__x).swap(*this); 689 return *this; 690 } 691 692 /** 693 * @brief %Function assignment to zero. 694 * @post @c !(bool)*this 695 * @returns @c *this 696 * 697 * The target of @a *this is deallocated, leaving it empty. 698 */ 699 function& operator=(_M_clear_type*) 700 { 701 if (_M_manager) { 702 _M_manager(_M_functor, _M_functor, __destroy_functor); 703 _M_manager = 0; 704 _M_invoker = 0; 705 } 706 return *this; 707 } 708 709 /** 710 * @brief %Function assignment to a new target. 711 * @param f A %function object that is callable with parameters of 712 * type @c T1, @c T2, ..., @c TN and returns a value convertible 713 * to @c Res. 714 * @return @c *this 715 * 716 * This %function object wrapper will target a copy of @a 717 * f. If @a f is @c reference_wrapper<F>, then this function 718 * object will contain a reference to the function object @c 719 * f.get(). If @a f is a NULL function pointer or NULL 720 * pointer-to-member, @c this object will be empty. 721 * 722 * If @a f is a non-NULL function pointer or an object of type @c 723 * reference_wrapper<F>, this function will not throw. 724 */ 725 template<typename _Functor> 726 typename __enable_if<function&, !is_integral<_Functor>::value>::__type 727 operator=(_Functor __f) 728 { 729 function(__f).swap(*this); 730 return *this; 731 } 732 733 // [3.7.2.2] function modifiers 734 735 /** 736 * @brief Swap the targets of two %function objects. 737 * @param f A %function with identical call signature. 738 * 739 * Swap the targets of @c this function object and @a f. This 740 * function will not throw an exception. 741 */ 742 void swap(function& __x) 743 { 744 _Any_data __old_functor = _M_functor; 745 _M_functor = __x._M_functor; 746 __x._M_functor = __old_functor; 747 _Manager_type __old_manager = _M_manager; 748 _M_manager = __x._M_manager; 749 __x._M_manager = __old_manager; 750 _Invoker_type __old_invoker = _M_invoker; 751 _M_invoker = __x._M_invoker; 752 __x._M_invoker = __old_invoker; 753 } 754 755 // [3.7.2.3] function capacity 756 757 /** 758 * @brief Determine if the %function wrapper has a target. 759 * 760 * @return @c true when this %function object contains a target, 761 * or @c false when it is empty. 762 * 763 * This function will not throw an exception. 764 */ 765 operator _Safe_bool() const 766 { 767 if (_M_empty()) 768 { 769 return 0; 770 } 771 else 772 { 773 return &_Hidden_type::_M_bool; 774 } 775 } 776 777 // [3.7.2.4] function invocation 778 779 /** 780 * @brief Invokes the function targeted by @c *this. 781 * @returns the result of the target. 782 * @throws bad_function_call when @c !(bool)*this 783 * 784 * The function call operator invokes the target function object 785 * stored by @c this. 786 */ 787 _Res operator()(_GLIBCXX_PARAMS) const; 788 789 // [3.7.2.5] function target access 790 /** 791 * @brief Determine the type of the target of this function object 792 * wrapper. 793 * 794 * @returns the type identifier of the target function object, or 795 * @c typeid(void) if @c !(bool)*this. 796 * 797 * This function will not throw an exception. 798 */ 799 const type_info& target_type() const; 800 801 /** 802 * @brief Access the stored target function object. 803 * 804 * @return Returns a pointer to the stored target function object, 805 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL 806 * pointer. 807 * 808 * This function will not throw an exception. 809 */ 810 template<typename _Functor> _Functor* target(); 811 812 /** 813 * @overload 814 */ 815 template<typename _Functor> const _Functor* target() const; 816 817 private: 818 // [3.7.2.6] undefined operators 819 template<typename _Function> 820 void operator==(const function<_Function>&) const; 821 template<typename _Function> 822 void operator!=(const function<_Function>&) const; 823 824 typedef _Res (*_Invoker_type)(const _Any_data& _GLIBCXX_COMMA 825 _GLIBCXX_PARAMS); 826 _Invoker_type _M_invoker; 827}; 828 829template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 830 function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::function(const function& __x) 831 : _Function_base() 832 { 833 if (__x) { 834 _M_invoker = __x._M_invoker; 835 _M_manager = __x._M_manager; 836 __x._M_manager(_M_functor, __x._M_functor, __clone_functor); 837 } 838 } 839 840template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 841template<typename _Functor> 842 function<_Res(_GLIBCXX_TEMPLATE_ARGS)> 843 ::function(_Functor __f, 844 typename __enable_if<_Useless, 845 !is_integral<_Functor>::value>::__type) 846 : _Function_base() 847{ 848 typedef _Function_handler<_Signature_type, _Functor> _My_handler; 849 if (_My_handler::_M_not_empty_function(__f)) { 850 _M_invoker = &_My_handler::_M_invoke; 851 _M_manager = &_My_handler::_M_manager; 852 _My_handler::_M_init_functor(_M_functor, __f); 853 } 854} 855 856template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 857 _Res 858 function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::operator()(_GLIBCXX_PARAMS) const 859 { 860 if (_M_empty()) 861 { 862#if __EXCEPTIONS 863 throw bad_function_call(); 864#else 865 std::abort(); 866#endif 867 } 868 return _M_invoker(_M_functor _GLIBCXX_COMMA _GLIBCXX_ARGS); 869 } 870 871template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 872 const type_info& 873 function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::target_type() const 874 { 875 if (_M_manager) 876 { 877 _Any_data __typeinfo_result; 878 _M_manager(__typeinfo_result, _M_functor, __get_type_info); 879 return *__typeinfo_result._M_access<const type_info*>(); 880 } 881 else 882 { 883 return typeid(void); 884 } 885 } 886 887template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 888template<typename _Functor> 889 _Functor* 890 function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::target() 891 { 892 if (typeid(_Functor) == target_type() && _M_manager) 893 { 894 _Any_data __ptr; 895 if (_M_manager(__ptr, _M_functor, __get_functor_ptr) 896 && !is_const<_Functor>::value) 897 return 0; 898 else 899 return __ptr._M_access<_Functor*>(); 900 } 901 else 902 { 903 return 0; 904 } 905 } 906 907template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS> 908template<typename _Functor> 909 const _Functor* 910 function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::target() const 911 { 912 if (typeid(_Functor) == target_type() && _M_manager) 913 { 914 _Any_data __ptr; 915 _M_manager(__ptr, _M_functor, __get_functor_ptr); 916 return __ptr._M_access<const _Functor*>(); 917 } 918 else 919 { 920 return 0; 921 } 922 } 923 924