00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #ifndef _GLIBCXX_FUNCTIONAL
00045 #define _GLIBCXX_FUNCTIONAL 1
00046
00047 #pragma GCC system_header
00048
00049 #include <bits/c++config.h>
00050 #include <bits/stl_function.h>
00051
00052 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00053
00054 #include <typeinfo>
00055 #include <new>
00056 #include <tuple>
00057 #include <type_traits>
00058 #include <bits/functional_hash.h>
00059 #include <ext/type_traits.h>
00060
00061 namespace std
00062 {
00063 template<typename _MemberPointer>
00064 class _Mem_fn;
00065
00066
00067
00068
00069
00070
00071 template<typename _Tp>
00072 class _Has_result_type_helper : __sfinae_types
00073 {
00074 template<typename _Up>
00075 struct _Wrap_type
00076 { };
00077
00078 template<typename _Up>
00079 static __one __test(_Wrap_type<typename _Up::result_type>*);
00080
00081 template<typename _Up>
00082 static __two __test(...);
00083
00084 public:
00085 static const bool value = sizeof(__test<_Tp>(0)) == 1;
00086 };
00087
00088 template<typename _Tp>
00089 struct _Has_result_type
00090 : integral_constant<bool,
00091 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
00092 { };
00093
00094
00095
00096
00097
00098 template<bool _Has_result_type, typename _Functor>
00099 struct _Maybe_get_result_type
00100 { };
00101
00102 template<typename _Functor>
00103 struct _Maybe_get_result_type<true, _Functor>
00104 {
00105 typedef typename _Functor::result_type result_type;
00106 };
00107
00108
00109
00110
00111
00112 template<typename _Functor>
00113 struct _Weak_result_type_impl
00114 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
00115 {
00116 };
00117
00118
00119 template<typename _Res, typename... _ArgTypes>
00120 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
00121 {
00122 typedef _Res result_type;
00123 };
00124
00125
00126 template<typename _Res, typename... _ArgTypes>
00127 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
00128 {
00129 typedef _Res result_type;
00130 };
00131
00132
00133 template<typename _Res, typename... _ArgTypes>
00134 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
00135 {
00136 typedef _Res result_type;
00137 };
00138
00139
00140 template<typename _Res, typename _Class, typename... _ArgTypes>
00141 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
00142 {
00143 typedef _Res result_type;
00144 };
00145
00146
00147 template<typename _Res, typename _Class, typename... _ArgTypes>
00148 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
00149 {
00150 typedef _Res result_type;
00151 };
00152
00153
00154 template<typename _Res, typename _Class, typename... _ArgTypes>
00155 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
00156 {
00157 typedef _Res result_type;
00158 };
00159
00160
00161 template<typename _Res, typename _Class, typename... _ArgTypes>
00162 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
00163 {
00164 typedef _Res result_type;
00165 };
00166
00167
00168
00169
00170
00171 template<typename _Functor>
00172 struct _Weak_result_type
00173 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
00174 {
00175 };
00176
00177 template<typename _Signature>
00178 class result_of;
00179
00180
00181
00182
00183
00184
00185
00186 template<bool _Has_result_type, typename _Signature>
00187 struct _Result_of_impl;
00188
00189
00190 template<typename _Res, typename _Class, typename _T1>
00191 struct _Result_of_impl<false, _Res _Class::*(_T1)>
00192 {
00193 typedef typename _Mem_fn<_Res _Class::*>
00194 ::template _Result_type<_T1>::type type;
00195 };
00196
00197
00198
00199
00200
00201 template<typename _Functor, typename... _ArgTypes>
00202 class result_of<_Functor(_ArgTypes...)>
00203 : public _Result_of_impl<
00204 _Has_result_type<_Weak_result_type<_Functor> >::value,
00205 _Functor(_ArgTypes...)>
00206 {
00207 };
00208
00209
00210 template<typename _Functor, typename... _ArgTypes>
00211 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
00212 {
00213 typedef typename _Weak_result_type<_Functor>::result_type type;
00214 };
00215
00216
00217
00218
00219
00220 template<typename _Functor, typename... _ArgTypes>
00221 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
00222 {
00223 typedef typename _Functor
00224 ::template result<_Functor(_ArgTypes...)>::type type;
00225 };
00226
00227
00228
00229
00230
00231 template<typename _Functor>
00232 struct _Result_of_impl<false, _Functor()>
00233 {
00234 typedef void type;
00235 };
00236
00237
00238 template<typename _Tp>
00239 struct _Derives_from_unary_function : __sfinae_types
00240 {
00241 private:
00242 template<typename _T1, typename _Res>
00243 static __one __test(const volatile unary_function<_T1, _Res>*);
00244
00245
00246
00247 static __two __test(...);
00248
00249 public:
00250 static const bool value = sizeof(__test((_Tp*)0)) == 1;
00251 };
00252
00253
00254 template<typename _Tp>
00255 struct _Derives_from_binary_function : __sfinae_types
00256 {
00257 private:
00258 template<typename _T1, typename _T2, typename _Res>
00259 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
00260
00261
00262
00263 static __two __test(...);
00264
00265 public:
00266 static const bool value = sizeof(__test((_Tp*)0)) == 1;
00267 };
00268
00269
00270 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
00271 struct _Function_to_function_pointer
00272 {
00273 typedef _Tp type;
00274 };
00275
00276 template<typename _Tp>
00277 struct _Function_to_function_pointer<_Tp, true>
00278 {
00279 typedef _Tp* type;
00280 };
00281
00282
00283
00284
00285
00286 template<typename _Functor, typename... _Args>
00287 inline
00288 typename __gnu_cxx::__enable_if<
00289 (!is_member_pointer<_Functor>::value
00290 && !is_function<_Functor>::value
00291 && !is_function<typename remove_pointer<_Functor>::type>::value),
00292 typename result_of<_Functor(_Args...)>::type
00293 >::__type
00294 __invoke(_Functor& __f, _Args&... __args)
00295 {
00296 return __f(__args...);
00297 }
00298
00299 template<typename _Functor, typename... _Args>
00300 inline
00301 typename __gnu_cxx::__enable_if<
00302 (is_member_pointer<_Functor>::value
00303 && !is_function<_Functor>::value
00304 && !is_function<typename remove_pointer<_Functor>::type>::value),
00305 typename result_of<_Functor(_Args...)>::type
00306 >::__type
00307 __invoke(_Functor& __f, _Args&... __args)
00308 {
00309 return mem_fn(__f)(__args...);
00310 }
00311
00312
00313 template<typename _Functor, typename... _Args>
00314 inline
00315 typename __gnu_cxx::__enable_if<
00316 (is_pointer<_Functor>::value
00317 && is_function<typename remove_pointer<_Functor>::type>::value),
00318 typename result_of<_Functor(_Args...)>::type
00319 >::__type
00320 __invoke(_Functor __f, _Args&... __args)
00321 {
00322 return __f(__args...);
00323 }
00324
00325
00326
00327
00328
00329
00330 template<bool _Unary, bool _Binary, typename _Tp>
00331 struct _Reference_wrapper_base_impl;
00332
00333
00334 template<typename _Tp>
00335 struct _Reference_wrapper_base_impl<false, false, _Tp>
00336 : _Weak_result_type<_Tp>
00337 { };
00338
00339
00340 template<typename _Tp>
00341 struct _Reference_wrapper_base_impl<true, false, _Tp>
00342 : unary_function<typename _Tp::argument_type,
00343 typename _Tp::result_type>
00344 { };
00345
00346
00347 template<typename _Tp>
00348 struct _Reference_wrapper_base_impl<false, true, _Tp>
00349 : binary_function<typename _Tp::first_argument_type,
00350 typename _Tp::second_argument_type,
00351 typename _Tp::result_type>
00352 { };
00353
00354
00355
00356 template<typename _Tp>
00357 struct _Reference_wrapper_base_impl<true, true, _Tp>
00358 : unary_function<typename _Tp::argument_type,
00359 typename _Tp::result_type>,
00360 binary_function<typename _Tp::first_argument_type,
00361 typename _Tp::second_argument_type,
00362 typename _Tp::result_type>
00363 {
00364 typedef typename _Tp::result_type result_type;
00365 };
00366
00367
00368
00369
00370
00371
00372
00373 template<typename _Tp>
00374 struct _Reference_wrapper_base
00375 : _Reference_wrapper_base_impl<
00376 _Derives_from_unary_function<_Tp>::value,
00377 _Derives_from_binary_function<_Tp>::value,
00378 _Tp>
00379 { };
00380
00381
00382 template<typename _Res, typename _T1>
00383 struct _Reference_wrapper_base<_Res(_T1)>
00384 : unary_function<_T1, _Res>
00385 { };
00386
00387
00388 template<typename _Res, typename _T1, typename _T2>
00389 struct _Reference_wrapper_base<_Res(_T1, _T2)>
00390 : binary_function<_T1, _T2, _Res>
00391 { };
00392
00393
00394 template<typename _Res, typename _T1>
00395 struct _Reference_wrapper_base<_Res(*)(_T1)>
00396 : unary_function<_T1, _Res>
00397 { };
00398
00399
00400 template<typename _Res, typename _T1, typename _T2>
00401 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
00402 : binary_function<_T1, _T2, _Res>
00403 { };
00404
00405
00406 template<typename _Res, typename _T1>
00407 struct _Reference_wrapper_base<_Res (_T1::*)()>
00408 : unary_function<_T1*, _Res>
00409 { };
00410
00411
00412 template<typename _Res, typename _T1, typename _T2>
00413 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
00414 : binary_function<_T1*, _T2, _Res>
00415 { };
00416
00417
00418 template<typename _Res, typename _T1>
00419 struct _Reference_wrapper_base<_Res (_T1::*)() const>
00420 : unary_function<const _T1*, _Res>
00421 { };
00422
00423
00424 template<typename _Res, typename _T1, typename _T2>
00425 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
00426 : binary_function<const _T1*, _T2, _Res>
00427 { };
00428
00429
00430 template<typename _Res, typename _T1>
00431 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
00432 : unary_function<volatile _T1*, _Res>
00433 { };
00434
00435
00436 template<typename _Res, typename _T1, typename _T2>
00437 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
00438 : binary_function<volatile _T1*, _T2, _Res>
00439 { };
00440
00441
00442 template<typename _Res, typename _T1>
00443 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
00444 : unary_function<const volatile _T1*, _Res>
00445 { };
00446
00447
00448 template<typename _Res, typename _T1, typename _T2>
00449 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
00450 : binary_function<const volatile _T1*, _T2, _Res>
00451 { };
00452
00453
00454 template<typename _Tp>
00455 class reference_wrapper
00456 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
00457 {
00458
00459
00460 typedef typename _Function_to_function_pointer<_Tp>::type
00461 _M_func_type;
00462
00463 _Tp* _M_data;
00464 public:
00465 typedef _Tp type;
00466
00467 explicit
00468 reference_wrapper(_Tp& __indata): _M_data(&__indata)
00469 { }
00470
00471 reference_wrapper(const reference_wrapper<_Tp>& __inref):
00472 _M_data(__inref._M_data)
00473 { }
00474
00475 reference_wrapper&
00476 operator=(const reference_wrapper<_Tp>& __inref)
00477 {
00478 _M_data = __inref._M_data;
00479 return *this;
00480 }
00481
00482 operator _Tp&() const
00483 { return this->get(); }
00484
00485 _Tp&
00486 get() const
00487 { return *_M_data; }
00488
00489 template<typename... _Args>
00490 typename result_of<_M_func_type(_Args...)>::type
00491 operator()(_Args&... __args) const
00492 {
00493 return __invoke(get(), __args...);
00494 }
00495 };
00496
00497
00498
00499 template<typename _Tp>
00500 inline reference_wrapper<_Tp>
00501 ref(_Tp& __t)
00502 { return reference_wrapper<_Tp>(__t); }
00503
00504
00505 template<typename _Tp>
00506 inline reference_wrapper<const _Tp>
00507 cref(const _Tp& __t)
00508 { return reference_wrapper<const _Tp>(__t); }
00509
00510 template<typename _Tp>
00511 inline reference_wrapper<_Tp>
00512 ref(reference_wrapper<_Tp> __t)
00513 { return ref(__t.get()); }
00514
00515 template<typename _Tp>
00516 inline reference_wrapper<const _Tp>
00517 cref(reference_wrapper<_Tp> __t)
00518 { return cref(__t.get()); }
00519
00520 template<typename _Tp, bool>
00521 struct _Mem_fn_const_or_non
00522 {
00523 typedef const _Tp& type;
00524 };
00525
00526 template<typename _Tp>
00527 struct _Mem_fn_const_or_non<_Tp, false>
00528 {
00529 typedef _Tp& type;
00530 };
00531
00532
00533
00534
00535
00536
00537 template<typename _Res, typename... _ArgTypes>
00538 struct _Maybe_unary_or_binary_function { };
00539
00540
00541 template<typename _Res, typename _T1>
00542 struct _Maybe_unary_or_binary_function<_Res, _T1>
00543 : std::unary_function<_T1, _Res> { };
00544
00545
00546 template<typename _Res, typename _T1, typename _T2>
00547 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
00548 : std::binary_function<_T1, _T2, _Res> { };
00549
00550
00551 template<typename _Res, typename _Class, typename... _ArgTypes>
00552 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
00553 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
00554 {
00555 typedef _Res (_Class::*_Functor)(_ArgTypes...);
00556
00557 template<typename _Tp>
00558 _Res
00559 _M_call(_Tp& __object, const volatile _Class *,
00560 _ArgTypes... __args) const
00561 { return (__object.*__pmf)(__args...); }
00562
00563 template<typename _Tp>
00564 _Res
00565 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00566 { return ((*__ptr).*__pmf)(__args...); }
00567
00568 public:
00569 typedef _Res result_type;
00570
00571 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00572
00573
00574 _Res
00575 operator()(_Class& __object, _ArgTypes... __args) const
00576 { return (__object.*__pmf)(__args...); }
00577
00578
00579 _Res
00580 operator()(_Class* __object, _ArgTypes... __args) const
00581 { return (__object->*__pmf)(__args...); }
00582
00583
00584 template<typename _Tp>
00585 _Res
00586 operator()(_Tp& __object, _ArgTypes... __args) const
00587 { return _M_call(__object, &__object, __args...); }
00588
00589 private:
00590 _Functor __pmf;
00591 };
00592
00593
00594 template<typename _Res, typename _Class, typename... _ArgTypes>
00595 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
00596 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
00597 _ArgTypes...>
00598 {
00599 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
00600
00601 template<typename _Tp>
00602 _Res
00603 _M_call(_Tp& __object, const volatile _Class *,
00604 _ArgTypes... __args) const
00605 { return (__object.*__pmf)(__args...); }
00606
00607 template<typename _Tp>
00608 _Res
00609 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00610 { return ((*__ptr).*__pmf)(__args...); }
00611
00612 public:
00613 typedef _Res result_type;
00614
00615 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00616
00617
00618 _Res
00619 operator()(const _Class& __object, _ArgTypes... __args) const
00620 { return (__object.*__pmf)(__args...); }
00621
00622
00623 _Res
00624 operator()(const _Class* __object, _ArgTypes... __args) const
00625 { return (__object->*__pmf)(__args...); }
00626
00627
00628 template<typename _Tp>
00629 _Res operator()(_Tp& __object, _ArgTypes... __args) const
00630 { return _M_call(__object, &__object, __args...); }
00631
00632 private:
00633 _Functor __pmf;
00634 };
00635
00636
00637 template<typename _Res, typename _Class, typename... _ArgTypes>
00638 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
00639 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
00640 _ArgTypes...>
00641 {
00642 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
00643
00644 template<typename _Tp>
00645 _Res
00646 _M_call(_Tp& __object, const volatile _Class *,
00647 _ArgTypes... __args) const
00648 { return (__object.*__pmf)(__args...); }
00649
00650 template<typename _Tp>
00651 _Res
00652 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00653 { return ((*__ptr).*__pmf)(__args...); }
00654
00655 public:
00656 typedef _Res result_type;
00657
00658 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00659
00660
00661 _Res
00662 operator()(volatile _Class& __object, _ArgTypes... __args) const
00663 { return (__object.*__pmf)(__args...); }
00664
00665
00666 _Res
00667 operator()(volatile _Class* __object, _ArgTypes... __args) const
00668 { return (__object->*__pmf)(__args...); }
00669
00670
00671 template<typename _Tp>
00672 _Res
00673 operator()(_Tp& __object, _ArgTypes... __args) const
00674 { return _M_call(__object, &__object, __args...); }
00675
00676 private:
00677 _Functor __pmf;
00678 };
00679
00680
00681 template<typename _Res, typename _Class, typename... _ArgTypes>
00682 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
00683 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
00684 _ArgTypes...>
00685 {
00686 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
00687
00688 template<typename _Tp>
00689 _Res
00690 _M_call(_Tp& __object, const volatile _Class *,
00691 _ArgTypes... __args) const
00692 { return (__object.*__pmf)(__args...); }
00693
00694 template<typename _Tp>
00695 _Res
00696 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00697 { return ((*__ptr).*__pmf)(__args...); }
00698
00699 public:
00700 typedef _Res result_type;
00701
00702 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00703
00704
00705 _Res
00706 operator()(const volatile _Class& __object, _ArgTypes... __args) const
00707 { return (__object.*__pmf)(__args...); }
00708
00709
00710 _Res
00711 operator()(const volatile _Class* __object, _ArgTypes... __args) const
00712 { return (__object->*__pmf)(__args...); }
00713
00714
00715 template<typename _Tp>
00716 _Res operator()(_Tp& __object, _ArgTypes... __args) const
00717 { return _M_call(__object, &__object, __args...); }
00718
00719 private:
00720 _Functor __pmf;
00721 };
00722
00723
00724 template<typename _Res, typename _Class>
00725 class _Mem_fn<_Res _Class::*>
00726 {
00727
00728
00729 template<typename _Tp>
00730 _Res&
00731 _M_call(_Tp& __object, _Class *) const
00732 { return __object.*__pm; }
00733
00734 template<typename _Tp, typename _Up>
00735 _Res&
00736 _M_call(_Tp& __object, _Up * const *) const
00737 { return (*__object).*__pm; }
00738
00739 template<typename _Tp, typename _Up>
00740 const _Res&
00741 _M_call(_Tp& __object, const _Up * const *) const
00742 { return (*__object).*__pm; }
00743
00744 template<typename _Tp>
00745 const _Res&
00746 _M_call(_Tp& __object, const _Class *) const
00747 { return __object.*__pm; }
00748
00749 template<typename _Tp>
00750 const _Res&
00751 _M_call(_Tp& __ptr, const volatile void*) const
00752 { return (*__ptr).*__pm; }
00753
00754 template<typename _Tp> static _Tp& __get_ref();
00755
00756 template<typename _Tp>
00757 static __sfinae_types::__one __check_const(_Tp&, _Class*);
00758 template<typename _Tp, typename _Up>
00759 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
00760 template<typename _Tp, typename _Up>
00761 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
00762 template<typename _Tp>
00763 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
00764 template<typename _Tp>
00765 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
00766
00767 public:
00768 template<typename _Tp>
00769 struct _Result_type
00770 : _Mem_fn_const_or_non<_Res,
00771 (sizeof(__sfinae_types::__two)
00772 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
00773 { };
00774
00775 template<typename _Signature>
00776 struct result;
00777
00778 template<typename _CVMem, typename _Tp>
00779 struct result<_CVMem(_Tp)>
00780 : public _Result_type<_Tp> { };
00781
00782 template<typename _CVMem, typename _Tp>
00783 struct result<_CVMem(_Tp&)>
00784 : public _Result_type<_Tp> { };
00785
00786 explicit
00787 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
00788
00789
00790 _Res&
00791 operator()(_Class& __object) const
00792 { return __object.*__pm; }
00793
00794 const _Res&
00795 operator()(const _Class& __object) const
00796 { return __object.*__pm; }
00797
00798
00799 _Res&
00800 operator()(_Class* __object) const
00801 { return __object->*__pm; }
00802
00803 const _Res&
00804 operator()(const _Class* __object) const
00805 { return __object->*__pm; }
00806
00807
00808 template<typename _Tp>
00809 typename _Result_type<_Tp>::type
00810 operator()(_Tp& __unknown) const
00811 { return _M_call(__unknown, &__unknown); }
00812
00813 private:
00814 _Res _Class::*__pm;
00815 };
00816
00817
00818
00819
00820
00821 template<typename _Tp, typename _Class>
00822 inline _Mem_fn<_Tp _Class::*>
00823 mem_fn(_Tp _Class::* __pm)
00824 {
00825 return _Mem_fn<_Tp _Class::*>(__pm);
00826 }
00827
00828
00829
00830
00831
00832
00833 template<typename _Tp>
00834 struct is_bind_expression
00835 { static const bool value = false; };
00836
00837 template<typename _Tp>
00838 const bool is_bind_expression<_Tp>::value;
00839
00840
00841
00842
00843
00844 template<typename _Tp>
00845 struct is_placeholder
00846 { static const int value = 0; };
00847
00848 template<typename _Tp>
00849 const int is_placeholder<_Tp>::value;
00850
00851
00852 template<int _Num> struct _Placeholder { };
00853
00854
00855
00856
00857
00858
00859
00860
00861 namespace placeholders
00862 {
00863 namespace
00864 {
00865 _Placeholder<1> _1;
00866 _Placeholder<2> _2;
00867 _Placeholder<3> _3;
00868 _Placeholder<4> _4;
00869 _Placeholder<5> _5;
00870 _Placeholder<6> _6;
00871 _Placeholder<7> _7;
00872 _Placeholder<8> _8;
00873 _Placeholder<9> _9;
00874 _Placeholder<10> _10;
00875 _Placeholder<11> _11;
00876 _Placeholder<12> _12;
00877 _Placeholder<13> _13;
00878 _Placeholder<14> _14;
00879 _Placeholder<15> _15;
00880 _Placeholder<16> _16;
00881 _Placeholder<17> _17;
00882 _Placeholder<18> _18;
00883 _Placeholder<19> _19;
00884 _Placeholder<20> _20;
00885 _Placeholder<21> _21;
00886 _Placeholder<22> _22;
00887 _Placeholder<23> _23;
00888 _Placeholder<24> _24;
00889 _Placeholder<25> _25;
00890 _Placeholder<26> _26;
00891 _Placeholder<27> _27;
00892 _Placeholder<28> _28;
00893 _Placeholder<29> _29;
00894 }
00895 }
00896
00897
00898
00899
00900
00901 template<int _Num>
00902 struct is_placeholder<_Placeholder<_Num> >
00903 { static const int value = _Num; };
00904
00905 template<int _Num>
00906 const int is_placeholder<_Placeholder<_Num> >::value;
00907
00908
00909
00910
00911
00912 template<int... _Indexes>
00913 struct _Index_tuple { };
00914
00915
00916 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
00917 struct _Build_index_tuple;
00918
00919 template<std::size_t _Num, int... _Indexes>
00920 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
00921 : _Build_index_tuple<_Num - 1,
00922 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
00923 {
00924 };
00925
00926 template<int... _Indexes>
00927 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
00928 {
00929 typedef _Index_tuple<_Indexes...> __type;
00930 };
00931
00932
00933
00934
00935
00936 struct _No_tuple_element;
00937
00938
00939
00940
00941
00942
00943 template<int __i, typename _Tuple, bool _IsSafe>
00944 struct _Safe_tuple_element_impl
00945 : tuple_element<__i, _Tuple> { };
00946
00947
00948
00949
00950
00951
00952 template<int __i, typename _Tuple>
00953 struct _Safe_tuple_element_impl<__i, _Tuple, false>
00954 {
00955 typedef _No_tuple_element type;
00956 };
00957
00958
00959
00960
00961
00962 template<int __i, typename _Tuple>
00963 struct _Safe_tuple_element
00964 : _Safe_tuple_element_impl<__i, _Tuple,
00965 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
00966 {
00967 };
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980 template<typename _Arg,
00981 bool _IsBindExp = is_bind_expression<_Arg>::value,
00982 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
00983 class _Mu;
00984
00985
00986
00987
00988
00989 template<typename _Tp>
00990 class _Mu<reference_wrapper<_Tp>, false, false>
00991 {
00992 public:
00993 typedef _Tp& result_type;
00994
00995
00996
00997
00998
00999 template<typename _CVRef, typename _Tuple>
01000 result_type
01001 operator()(_CVRef& __arg, const _Tuple&) const volatile
01002 { return __arg.get(); }
01003 };
01004
01005
01006
01007
01008
01009
01010 template<typename _Arg>
01011 class _Mu<_Arg, true, false>
01012 {
01013 public:
01014 template<typename _Signature> class result;
01015
01016
01017
01018
01019 template<typename _CVMu, typename _CVArg, typename... _Args>
01020 class result<_CVMu(_CVArg, tuple<_Args...>)>
01021 : public result_of<_CVArg(_Args...)> { };
01022
01023 template<typename _CVArg, typename... _Args>
01024 typename result_of<_CVArg(_Args...)>::type
01025 operator()(_CVArg& __arg,
01026 const tuple<_Args...>& __tuple) const volatile
01027 {
01028
01029 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
01030 _Indexes;
01031 return this->__call(__arg, __tuple, _Indexes());
01032 }
01033
01034 private:
01035
01036
01037 template<typename _CVArg, typename... _Args, int... _Indexes>
01038 typename result_of<_CVArg(_Args...)>::type
01039 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
01040 const _Index_tuple<_Indexes...>&) const volatile
01041 {
01042 return __arg(get<_Indexes>(__tuple)...);
01043 }
01044 };
01045
01046
01047
01048
01049
01050
01051 template<typename _Arg>
01052 class _Mu<_Arg, false, true>
01053 {
01054 public:
01055 template<typename _Signature> class result;
01056
01057 template<typename _CVMu, typename _CVArg, typename _Tuple>
01058 class result<_CVMu(_CVArg, _Tuple)>
01059 {
01060
01061
01062
01063 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
01064 - 1), _Tuple>::type
01065 __base_type;
01066
01067 public:
01068 typedef typename add_lvalue_reference<__base_type>::type type;
01069 };
01070
01071 template<typename _Tuple>
01072 typename result<_Mu(_Arg, _Tuple)>::type
01073 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
01074 {
01075 return ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
01076 }
01077 };
01078
01079
01080
01081
01082
01083
01084 template<typename _Arg>
01085 class _Mu<_Arg, false, false>
01086 {
01087 public:
01088 template<typename _Signature> struct result;
01089
01090 template<typename _CVMu, typename _CVArg, typename _Tuple>
01091 struct result<_CVMu(_CVArg, _Tuple)>
01092 {
01093 typedef typename add_lvalue_reference<_CVArg>::type type;
01094 };
01095
01096
01097 template<typename _CVArg, typename _Tuple>
01098 _CVArg&
01099 operator()(_CVArg& __arg, const _Tuple&) const volatile
01100 { return __arg; }
01101 };
01102
01103
01104
01105
01106
01107
01108 template<typename _Tp>
01109 struct _Maybe_wrap_member_pointer
01110 {
01111 typedef _Tp type;
01112
01113 static const _Tp&
01114 __do_wrap(const _Tp& __x)
01115 { return __x; }
01116 };
01117
01118
01119
01120
01121
01122
01123 template<typename _Tp, typename _Class>
01124 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
01125 {
01126 typedef _Mem_fn<_Tp _Class::*> type;
01127
01128 static type
01129 __do_wrap(_Tp _Class::* __pm)
01130 { return type(__pm); }
01131 };
01132
01133
01134
01135
01136
01137 template<>
01138 struct _Maybe_wrap_member_pointer<void>
01139 {
01140 typedef void type;
01141 };
01142
01143
01144 template<typename _Signature>
01145 struct _Bind;
01146
01147 template<typename _Functor, typename... _Bound_args>
01148 class _Bind<_Functor(_Bound_args...)>
01149 : public _Weak_result_type<_Functor>
01150 {
01151 typedef _Bind __self_type;
01152 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
01153 _Bound_indexes;
01154
01155 _Functor _M_f;
01156 tuple<_Bound_args...> _M_bound_args;
01157
01158
01159 template<typename... _Args, int... _Indexes>
01160 typename result_of<
01161 _Functor(typename result_of<_Mu<_Bound_args>
01162 (_Bound_args, tuple<_Args...>)>::type...)
01163 >::type
01164 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
01165 {
01166 return _M_f(_Mu<_Bound_args>()
01167 (get<_Indexes>(_M_bound_args), __args)...);
01168 }
01169
01170
01171 template<typename... _Args, int... _Indexes>
01172 typename result_of<
01173 const _Functor(typename result_of<_Mu<_Bound_args>
01174 (const _Bound_args, tuple<_Args...>)
01175 >::type...)>::type
01176 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
01177 {
01178 return _M_f(_Mu<_Bound_args>()
01179 (get<_Indexes>(_M_bound_args), __args)...);
01180 }
01181
01182
01183 template<typename... _Args, int... _Indexes>
01184 typename result_of<
01185 volatile _Functor(typename result_of<_Mu<_Bound_args>
01186 (volatile _Bound_args, tuple<_Args...>)
01187 >::type...)>::type
01188 __call(const tuple<_Args...>& __args,
01189 _Index_tuple<_Indexes...>) volatile
01190 {
01191 return _M_f(_Mu<_Bound_args>()
01192 (get<_Indexes>(_M_bound_args), __args)...);
01193 }
01194
01195
01196 template<typename... _Args, int... _Indexes>
01197 typename result_of<
01198 const volatile _Functor(typename result_of<_Mu<_Bound_args>
01199 (const volatile _Bound_args,
01200 tuple<_Args...>)
01201 >::type...)>::type
01202 __call(const tuple<_Args...>& __args,
01203 _Index_tuple<_Indexes...>) const volatile
01204 {
01205 return _M_f(_Mu<_Bound_args>()
01206 (get<_Indexes>(_M_bound_args), __args)...);
01207 }
01208
01209 public:
01210 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
01211 : _M_f(__f), _M_bound_args(__bound_args...) { }
01212
01213
01214 template<typename... _Args>
01215 typename result_of<
01216 _Functor(typename result_of<_Mu<_Bound_args>
01217 (_Bound_args, tuple<_Args...>)>::type...)
01218 >::type
01219 operator()(_Args&... __args)
01220 {
01221 return this->__call(tie(__args...), _Bound_indexes());
01222 }
01223
01224
01225 template<typename... _Args>
01226 typename result_of<
01227 const _Functor(typename result_of<_Mu<_Bound_args>
01228 (const _Bound_args, tuple<_Args...>)>::type...)
01229 >::type
01230 operator()(_Args&... __args) const
01231 {
01232 return this->__call(tie(__args...), _Bound_indexes());
01233 }
01234
01235
01236
01237 template<typename... _Args>
01238 typename result_of<
01239 volatile _Functor(typename result_of<_Mu<_Bound_args>
01240 (volatile _Bound_args, tuple<_Args...>)>::type...)
01241 >::type
01242 operator()(_Args&... __args) volatile
01243 {
01244 return this->__call(tie(__args...), _Bound_indexes());
01245 }
01246
01247
01248
01249 template<typename... _Args>
01250 typename result_of<
01251 const volatile _Functor(typename result_of<_Mu<_Bound_args>
01252 (const volatile _Bound_args,
01253 tuple<_Args...>)>::type...)
01254 >::type
01255 operator()(_Args&... __args) const volatile
01256 {
01257 return this->__call(tie(__args...), _Bound_indexes());
01258 }
01259 };
01260
01261
01262 template<typename _Result, typename _Signature>
01263 struct _Bind_result;
01264
01265 template<typename _Result, typename _Functor, typename... _Bound_args>
01266 class _Bind_result<_Result, _Functor(_Bound_args...)>
01267 {
01268 typedef _Bind_result __self_type;
01269 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
01270 _Bound_indexes;
01271
01272 _Functor _M_f;
01273 tuple<_Bound_args...> _M_bound_args;
01274
01275
01276 template<typename _Res>
01277 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
01278 template<typename _Res>
01279 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
01280
01281
01282 template<typename _Res, typename... _Args, int... _Indexes>
01283 _Result
01284 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01285 typename __disable_if_void<_Res>::type = 0)
01286 {
01287 return _M_f(_Mu<_Bound_args>()
01288 (get<_Indexes>(_M_bound_args), __args)...);
01289 }
01290
01291
01292 template<typename _Res, typename... _Args, int... _Indexes>
01293 void
01294 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01295 typename __enable_if_void<_Res>::type = 0)
01296 {
01297 _M_f(_Mu<_Bound_args>()
01298 (get<_Indexes>(_M_bound_args), __args)...);
01299 }
01300
01301
01302 template<typename _Res, typename... _Args, int... _Indexes>
01303 _Result
01304 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01305 typename __disable_if_void<_Res>::type = 0) const
01306 {
01307 return _M_f(_Mu<_Bound_args>()
01308 (get<_Indexes>(_M_bound_args), __args)...);
01309 }
01310
01311
01312 template<typename _Res, typename... _Args, int... _Indexes>
01313 void
01314 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01315 typename __enable_if_void<_Res>::type = 0) const
01316 {
01317 _M_f(_Mu<_Bound_args>()
01318 (get<_Indexes>(_M_bound_args), __args)...);
01319 }
01320
01321
01322 template<typename _Res, typename... _Args, int... _Indexes>
01323 _Result
01324 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01325 typename __disable_if_void<_Res>::type = 0) volatile
01326 {
01327 return _M_f(_Mu<_Bound_args>()
01328 (get<_Indexes>(_M_bound_args), __args)...);
01329 }
01330
01331
01332 template<typename _Res, typename... _Args, int... _Indexes>
01333 void
01334 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01335 typename __enable_if_void<_Res>::type = 0) volatile
01336 {
01337 _M_f(_Mu<_Bound_args>()
01338 (get<_Indexes>(_M_bound_args), __args)...);
01339 }
01340
01341
01342 template<typename _Res, typename... _Args, int... _Indexes>
01343 _Result
01344 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01345 typename __disable_if_void<_Res>::type = 0) const volatile
01346 {
01347 return _M_f(_Mu<_Bound_args>()
01348 (get<_Indexes>(_M_bound_args), __args)...);
01349 }
01350
01351
01352 template<typename _Res, typename... _Args, int... _Indexes>
01353 void
01354 __call(const tuple<_Args...>& __args,
01355 _Index_tuple<_Indexes...>,
01356 typename __enable_if_void<_Res>::type = 0) const volatile
01357 {
01358 _M_f(_Mu<_Bound_args>()
01359 (get<_Indexes>(_M_bound_args), __args)...);
01360 }
01361
01362 public:
01363 typedef _Result result_type;
01364
01365 explicit
01366 _Bind_result(_Functor __f, _Bound_args... __bound_args)
01367 : _M_f(__f), _M_bound_args(__bound_args...) { }
01368
01369
01370 template<typename... _Args>
01371 result_type
01372 operator()(_Args&... __args)
01373 {
01374 return this->__call<_Result>(tie(__args...), _Bound_indexes());
01375 }
01376
01377
01378 template<typename... _Args>
01379 result_type
01380 operator()(_Args&... __args) const
01381 {
01382 return this->__call<_Result>(tie(__args...), _Bound_indexes());
01383 }
01384
01385
01386 template<typename... _Args>
01387 result_type
01388 operator()(_Args&... __args) volatile
01389 {
01390 return this->__call<_Result>(tie(__args...), _Bound_indexes());
01391 }
01392
01393
01394 template<typename... _Args>
01395 result_type
01396 operator()(_Args&... __args) const volatile
01397 {
01398 return this->__call<_Result>(tie(__args...), _Bound_indexes());
01399 }
01400 };
01401
01402
01403 template<typename _Signature>
01404 struct is_bind_expression<_Bind<_Signature> >
01405 { static const bool value = true; };
01406
01407 template<typename _Signature>
01408 const bool is_bind_expression<_Bind<_Signature> >::value;
01409
01410
01411 template<typename _Result, typename _Signature>
01412 struct is_bind_expression<_Bind_result<_Result, _Signature> >
01413 { static const bool value = true; };
01414
01415 template<typename _Result, typename _Signature>
01416 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
01417
01418
01419 template<typename _Functor, typename... _ArgTypes>
01420 inline
01421 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
01422 bind(_Functor __f, _ArgTypes... __args)
01423 {
01424 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
01425 typedef typename __maybe_type::type __functor_type;
01426 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
01427 return __result_type(__maybe_type::__do_wrap(__f), __args...);
01428 }
01429
01430 template<typename _Result, typename _Functor, typename... _ArgTypes>
01431 inline
01432 _Bind_result<_Result,
01433 typename _Maybe_wrap_member_pointer<_Functor>::type
01434 (_ArgTypes...)>
01435 bind(_Functor __f, _ArgTypes... __args)
01436 {
01437 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
01438 typedef typename __maybe_type::type __functor_type;
01439 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
01440 __result_type;
01441 return __result_type(__maybe_type::__do_wrap(__f), __args...);
01442 }
01443
01444
01445
01446
01447
01448
01449 class bad_function_call : public std::exception { };
01450
01451
01452
01453
01454
01455
01456 struct _M_clear_type;
01457
01458
01459
01460
01461
01462
01463 template<typename _Tp>
01464 struct __is_location_invariant
01465 : integral_constant<bool,
01466 (is_pointer<_Tp>::value
01467 || is_member_pointer<_Tp>::value)>
01468 {
01469 };
01470
01471 class _Undefined_class;
01472
01473 union _Nocopy_types
01474 {
01475 void* _M_object;
01476 const void* _M_const_object;
01477 void (*_M_function_pointer)();
01478 void (_Undefined_class::*_M_member_pointer)();
01479 };
01480
01481 union _Any_data
01482 {
01483 void* _M_access() { return &_M_pod_data[0]; }
01484 const void* _M_access() const { return &_M_pod_data[0]; }
01485
01486 template<typename _Tp>
01487 _Tp&
01488 _M_access()
01489 { return *static_cast<_Tp*>(_M_access()); }
01490
01491 template<typename _Tp>
01492 const _Tp&
01493 _M_access() const
01494 { return *static_cast<const _Tp*>(_M_access()); }
01495
01496 _Nocopy_types _M_unused;
01497 char _M_pod_data[sizeof(_Nocopy_types)];
01498 };
01499
01500 enum _Manager_operation
01501 {
01502 __get_type_info,
01503 __get_functor_ptr,
01504 __clone_functor,
01505 __destroy_functor
01506 };
01507
01508
01509
01510 template<typename _Tp>
01511 struct _Simple_type_wrapper
01512 {
01513 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
01514
01515 _Tp __value;
01516 };
01517
01518 template<typename _Tp>
01519 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
01520 : __is_location_invariant<_Tp>
01521 {
01522 };
01523
01524
01525
01526 template<typename _Functor>
01527 inline _Functor&
01528 __callable_functor(_Functor& __f)
01529 { return __f; }
01530
01531 template<typename _Member, typename _Class>
01532 inline _Mem_fn<_Member _Class::*>
01533 __callable_functor(_Member _Class::* &__p)
01534 { return mem_fn(__p); }
01535
01536 template<typename _Member, typename _Class>
01537 inline _Mem_fn<_Member _Class::*>
01538 __callable_functor(_Member _Class::* const &__p)
01539 { return mem_fn(__p); }
01540
01541 template<typename _Signature>
01542 class function;
01543
01544
01545 class _Function_base
01546 {
01547 public:
01548 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
01549 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
01550
01551 template<typename _Functor>
01552 class _Base_manager
01553 {
01554 protected:
01555 static const bool __stored_locally =
01556 (__is_location_invariant<_Functor>::value
01557 && sizeof(_Functor) <= _M_max_size
01558 && __alignof__(_Functor) <= _M_max_align
01559 && (_M_max_align % __alignof__(_Functor) == 0));
01560
01561 typedef integral_constant<bool, __stored_locally> _Local_storage;
01562
01563
01564 static _Functor*
01565 _M_get_pointer(const _Any_data& __source)
01566 {
01567 const _Functor* __ptr =
01568 __stored_locally? &__source._M_access<_Functor>()
01569 : __source._M_access<_Functor*>();
01570 return const_cast<_Functor*>(__ptr);
01571 }
01572
01573
01574
01575 static void
01576 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
01577 {
01578 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
01579 }
01580
01581
01582
01583 static void
01584 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
01585 {
01586 __dest._M_access<_Functor*>() =
01587 new _Functor(*__source._M_access<_Functor*>());
01588 }
01589
01590
01591
01592 static void
01593 _M_destroy(_Any_data& __victim, true_type)
01594 {
01595 __victim._M_access<_Functor>().~_Functor();
01596 }
01597
01598
01599 static void
01600 _M_destroy(_Any_data& __victim, false_type)
01601 {
01602 delete __victim._M_access<_Functor*>();
01603 }
01604
01605 public:
01606 static bool
01607 _M_manager(_Any_data& __dest, const _Any_data& __source,
01608 _Manager_operation __op)
01609 {
01610 switch (__op)
01611 {
01612 #ifdef __GXX_RTTI
01613 case __get_type_info:
01614 __dest._M_access<const type_info*>() = &typeid(_Functor);
01615 break;
01616 #endif
01617 case __get_functor_ptr:
01618 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
01619 break;
01620
01621 case __clone_functor:
01622 _M_clone(__dest, __source, _Local_storage());
01623 break;
01624
01625 case __destroy_functor:
01626 _M_destroy(__dest, _Local_storage());
01627 break;
01628 }
01629 return false;
01630 }
01631
01632 static void
01633 _M_init_functor(_Any_data& __functor, _Functor&& __f)
01634 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
01635
01636 template<typename _Signature>
01637 static bool
01638 _M_not_empty_function(const function<_Signature>& __f)
01639 { return static_cast<bool>(__f); }
01640
01641 template<typename _Tp>
01642 static bool
01643 _M_not_empty_function(const _Tp*& __fp)
01644 { return __fp; }
01645
01646 template<typename _Class, typename _Tp>
01647 static bool
01648 _M_not_empty_function(_Tp _Class::* const& __mp)
01649 { return __mp; }
01650
01651 template<typename _Tp>
01652 static bool
01653 _M_not_empty_function(const _Tp&)
01654 { return true; }
01655
01656 private:
01657 static void
01658 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
01659 { new (__functor._M_access()) _Functor(std::move(__f)); }
01660
01661 static void
01662 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
01663 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
01664 };
01665
01666 template<typename _Functor>
01667 class _Ref_manager : public _Base_manager<_Functor*>
01668 {
01669 typedef _Function_base::_Base_manager<_Functor*> _Base;
01670
01671 public:
01672 static bool
01673 _M_manager(_Any_data& __dest, const _Any_data& __source,
01674 _Manager_operation __op)
01675 {
01676 switch (__op)
01677 {
01678 #ifdef __GXX_RTTI
01679 case __get_type_info:
01680 __dest._M_access<const type_info*>() = &typeid(_Functor);
01681 break;
01682 #endif
01683 case __get_functor_ptr:
01684 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
01685 return is_const<_Functor>::value;
01686 break;
01687
01688 default:
01689 _Base::_M_manager(__dest, __source, __op);
01690 }
01691 return false;
01692 }
01693
01694 static void
01695 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
01696 {
01697
01698 _Base::_M_init_functor(__functor, &__f.get());
01699 }
01700 };
01701
01702 _Function_base() : _M_manager(0) { }
01703
01704 ~_Function_base()
01705 {
01706 if (_M_manager)
01707 _M_manager(_M_functor, _M_functor, __destroy_functor);
01708 }
01709
01710
01711 bool _M_empty() const { return !_M_manager; }
01712
01713 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
01714 _Manager_operation);
01715
01716 _Any_data _M_functor;
01717 _Manager_type _M_manager;
01718 };
01719
01720 template<typename _Signature, typename _Functor>
01721 class _Function_handler;
01722
01723 template<typename _Res, typename _Functor, typename... _ArgTypes>
01724 class _Function_handler<_Res(_ArgTypes...), _Functor>
01725 : public _Function_base::_Base_manager<_Functor>
01726 {
01727 typedef _Function_base::_Base_manager<_Functor> _Base;
01728
01729 public:
01730 static _Res
01731 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01732 {
01733 return (*_Base::_M_get_pointer(__functor))(
01734 std::forward<_ArgTypes>(__args)...);
01735 }
01736 };
01737
01738 template<typename _Functor, typename... _ArgTypes>
01739 class _Function_handler<void(_ArgTypes...), _Functor>
01740 : public _Function_base::_Base_manager<_Functor>
01741 {
01742 typedef _Function_base::_Base_manager<_Functor> _Base;
01743
01744 public:
01745 static void
01746 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01747 {
01748 (*_Base::_M_get_pointer(__functor))(
01749 std::forward<_ArgTypes>(__args)...);
01750 }
01751 };
01752
01753 template<typename _Res, typename _Functor, typename... _ArgTypes>
01754 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
01755 : public _Function_base::_Ref_manager<_Functor>
01756 {
01757 typedef _Function_base::_Ref_manager<_Functor> _Base;
01758
01759 public:
01760 static _Res
01761 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01762 {
01763 return __callable_functor(**_Base::_M_get_pointer(__functor))(
01764 std::forward<_ArgTypes>(__args)...);
01765 }
01766 };
01767
01768 template<typename _Functor, typename... _ArgTypes>
01769 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
01770 : public _Function_base::_Ref_manager<_Functor>
01771 {
01772 typedef _Function_base::_Ref_manager<_Functor> _Base;
01773
01774 public:
01775 static void
01776 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01777 {
01778 __callable_functor(**_Base::_M_get_pointer(__functor))(
01779 std::forward<_ArgTypes>(__args)...);
01780 }
01781 };
01782
01783 template<typename _Class, typename _Member, typename _Res,
01784 typename... _ArgTypes>
01785 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
01786 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
01787 {
01788 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
01789 _Base;
01790
01791 public:
01792 static _Res
01793 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01794 {
01795 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
01796 std::forward<_ArgTypes>(__args)...);
01797 }
01798 };
01799
01800 template<typename _Class, typename _Member, typename... _ArgTypes>
01801 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
01802 : public _Function_base::_Base_manager<
01803 _Simple_type_wrapper< _Member _Class::* > >
01804 {
01805 typedef _Member _Class::* _Functor;
01806 typedef _Simple_type_wrapper<_Functor> _Wrapper;
01807 typedef _Function_base::_Base_manager<_Wrapper> _Base;
01808
01809 public:
01810 static bool
01811 _M_manager(_Any_data& __dest, const _Any_data& __source,
01812 _Manager_operation __op)
01813 {
01814 switch (__op)
01815 {
01816 #ifdef __GXX_RTTI
01817 case __get_type_info:
01818 __dest._M_access<const type_info*>() = &typeid(_Functor);
01819 break;
01820 #endif
01821 case __get_functor_ptr:
01822 __dest._M_access<_Functor*>() =
01823 &_Base::_M_get_pointer(__source)->__value;
01824 break;
01825
01826 default:
01827 _Base::_M_manager(__dest, __source, __op);
01828 }
01829 return false;
01830 }
01831
01832 static void
01833 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01834 {
01835 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
01836 std::forward<_ArgTypes>(__args)...);
01837 }
01838 };
01839
01840
01841 template<typename _Res, typename... _ArgTypes>
01842 class function<_Res(_ArgTypes...)>
01843 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
01844 private _Function_base
01845 {
01846 typedef _Res _Signature_type(_ArgTypes...);
01847
01848 struct _Useless { };
01849
01850 public:
01851 typedef _Res result_type;
01852
01853
01854
01855
01856
01857
01858
01859 explicit
01860 function() : _Function_base() { }
01861
01862
01863
01864
01865
01866 function(_M_clear_type*) : _Function_base() { }
01867
01868
01869
01870
01871
01872
01873
01874
01875
01876 function(const function& __x);
01877
01878
01879
01880
01881
01882
01883
01884
01885 function(function&& __x) : _Function_base()
01886 {
01887 __x.swap(*this);
01888 }
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908 template<typename _Functor>
01909 function(_Functor __f,
01910 typename __gnu_cxx::__enable_if<
01911 !is_integral<_Functor>::value, _Useless>::__type
01912 = _Useless());
01913
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924
01925
01926 function&
01927 operator=(const function& __x)
01928 {
01929 function(__x).swap(*this);
01930 return *this;
01931 }
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941
01942
01943
01944 function&
01945 operator=(function&& __x)
01946 {
01947 function(std::move(__x)).swap(*this);
01948 return *this;
01949 }
01950
01951
01952
01953
01954
01955
01956
01957
01958 function&
01959 operator=(_M_clear_type*)
01960 {
01961 if (_M_manager)
01962 {
01963 _M_manager(_M_functor, _M_functor, __destroy_functor);
01964 _M_manager = 0;
01965 _M_invoker = 0;
01966 }
01967 return *this;
01968 }
01969
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986 template<typename _Functor>
01987 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
01988 function&>::__type
01989 operator=(_Functor&& __f)
01990 {
01991 function(std::forward<_Functor>(__f)).swap(*this);
01992 return *this;
01993 }
01994
01995
01996 template<typename _Functor>
01997 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
01998 function&>::__type
01999 operator=(reference_wrapper<_Functor> __f)
02000 {
02001 function(__f).swap(*this);
02002 return *this;
02003 }
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014 void swap(function& __x)
02015 {
02016 _Any_data __old_functor = _M_functor;
02017 _M_functor = __x._M_functor;
02018 __x._M_functor = __old_functor;
02019 _Manager_type __old_manager = _M_manager;
02020 _M_manager = __x._M_manager;
02021 __x._M_manager = __old_manager;
02022 _Invoker_type __old_invoker = _M_invoker;
02023 _M_invoker = __x._M_invoker;
02024 __x._M_invoker = __old_invoker;
02025 }
02026
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040
02041
02042
02043
02044
02045
02046
02047
02048 explicit operator bool() const
02049 { return !_M_empty(); }
02050
02051
02052
02053
02054
02055
02056
02057
02058
02059
02060
02061 _Res operator()(_ArgTypes... __args) const;
02062
02063 #ifdef __GXX_RTTI
02064
02065
02066
02067
02068
02069
02070
02071
02072
02073
02074 const type_info& target_type() const;
02075
02076
02077
02078
02079
02080
02081
02082
02083
02084
02085 template<typename _Functor> _Functor* target();
02086
02087
02088 template<typename _Functor> const _Functor* target() const;
02089 #endif
02090
02091
02092 template<typename _Res2, typename... _ArgTypes2>
02093 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
02094 template<typename _Res2, typename... _ArgTypes2>
02095 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
02096
02097 private:
02098 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
02099 _Invoker_type _M_invoker;
02100 };
02101
02102 template<typename _Res, typename... _ArgTypes>
02103 function<_Res(_ArgTypes...)>::
02104 function(const function& __x)
02105 : _Function_base()
02106 {
02107 if (static_cast<bool>(__x))
02108 {
02109 _M_invoker = __x._M_invoker;
02110 _M_manager = __x._M_manager;
02111 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
02112 }
02113 }
02114
02115 template<typename _Res, typename... _ArgTypes>
02116 template<typename _Functor>
02117 function<_Res(_ArgTypes...)>::
02118 function(_Functor __f,
02119 typename __gnu_cxx::__enable_if<
02120 !is_integral<_Functor>::value, _Useless>::__type)
02121 : _Function_base()
02122 {
02123 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
02124
02125 if (_My_handler::_M_not_empty_function(__f))
02126 {
02127 _M_invoker = &_My_handler::_M_invoke;
02128 _M_manager = &_My_handler::_M_manager;
02129 _My_handler::_M_init_functor(_M_functor, std::move(__f));
02130 }
02131 }
02132
02133 template<typename _Res, typename... _ArgTypes>
02134 _Res
02135 function<_Res(_ArgTypes...)>::
02136 operator()(_ArgTypes... __args) const
02137 {
02138 if (_M_empty())
02139 __throw_bad_function_call();
02140 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
02141 }
02142
02143 #ifdef __GXX_RTTI
02144 template<typename _Res, typename... _ArgTypes>
02145 const type_info&
02146 function<_Res(_ArgTypes...)>::
02147 target_type() const
02148 {
02149 if (_M_manager)
02150 {
02151 _Any_data __typeinfo_result;
02152 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
02153 return *__typeinfo_result._M_access<const type_info*>();
02154 }
02155 else
02156 return typeid(void);
02157 }
02158
02159 template<typename _Res, typename... _ArgTypes>
02160 template<typename _Functor>
02161 _Functor*
02162 function<_Res(_ArgTypes...)>::
02163 target()
02164 {
02165 if (typeid(_Functor) == target_type() && _M_manager)
02166 {
02167 _Any_data __ptr;
02168 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
02169 && !is_const<_Functor>::value)
02170 return 0;
02171 else
02172 return __ptr._M_access<_Functor*>();
02173 }
02174 else
02175 return 0;
02176 }
02177
02178 template<typename _Res, typename... _ArgTypes>
02179 template<typename _Functor>
02180 const _Functor*
02181 function<_Res(_ArgTypes...)>::
02182 target() const
02183 {
02184 if (typeid(_Functor) == target_type() && _M_manager)
02185 {
02186 _Any_data __ptr;
02187 _M_manager(__ptr, _M_functor, __get_functor_ptr);
02188 return __ptr._M_access<const _Functor*>();
02189 }
02190 else
02191 return 0;
02192 }
02193 #endif
02194
02195
02196
02197
02198
02199
02200
02201
02202
02203
02204 template<typename _Res, typename... _Args>
02205 inline bool
02206 operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
02207 { return !static_cast<bool>(__f); }
02208
02209
02210 template<typename _Res, typename... _Args>
02211 inline bool
02212 operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
02213 { return !static_cast<bool>(__f); }
02214
02215
02216
02217
02218
02219
02220
02221
02222 template<typename _Res, typename... _Args>
02223 inline bool
02224 operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
02225 { return static_cast<bool>(__f); }
02226
02227
02228 template<typename _Res, typename... _Args>
02229 inline bool
02230 operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
02231 { return static_cast<bool>(__f); }
02232
02233
02234
02235
02236
02237
02238
02239
02240 template<typename _Res, typename... _Args>
02241 inline void
02242 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
02243 { __x.swap(__y); }
02244 }
02245
02246 #endif // __GXX_EXPERIMENTAL_CXX0X__
02247
02248 #endif // _GLIBCXX_FUNCTIONAL