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