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 #ifndef _TR1_FUNCTIONAL
00035 #define _TR1_FUNCTIONAL 1
00036
00037 #include "../functional"
00038 #include <typeinfo>
00039 #include <tr1/type_traits>
00040 #include <bits/cpp_type_traits.h>
00041 #include <string>
00042 #include <cstdlib>
00043 #include <cmath>
00044 #include <tr1/tuple>
00045
00046 namespace std
00047 {
00048 namespace tr1
00049 {
00050 template<typename _MemberPointer>
00051 class _Mem_fn;
00052
00053
00054
00055
00056
00057
00058
00059
00060 template<typename _Tp>
00061 class _Has_result_type_helper : __sfinae_types
00062 {
00063 template<typename _Up>
00064 struct _Wrap_type
00065 { };
00066
00067 template<typename _Up>
00068 static __one __test(_Wrap_type<typename _Up::result_type>*);
00069
00070 template<typename _Up>
00071 static __two __test(...);
00072
00073 public:
00074 static const bool value = sizeof(__test<_Tp>(0)) == 1;
00075 };
00076
00077 template<typename _Tp>
00078 struct _Has_result_type
00079 : integral_constant<
00080 bool,
00081 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
00082 { };
00083
00084
00085
00086
00087
00088
00089 template<bool _Has_result_type, typename _Functor>
00090 struct _Maybe_get_result_type
00091 { };
00092
00093 template<typename _Functor>
00094 struct _Maybe_get_result_type<true, _Functor>
00095 {
00096 typedef typename _Functor::result_type result_type;
00097 };
00098
00099
00100
00101
00102
00103
00104
00105 template<typename _Functor>
00106 struct _Weak_result_type_impl
00107 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
00108 {
00109 };
00110
00111
00112
00113
00114
00115
00116
00117 template<typename _Functor>
00118 struct _Weak_result_type
00119 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
00120 {
00121 };
00122
00123 template<typename _Signature>
00124 class result_of;
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 template<bool _Has_result_type, typename _Signature>
00135 struct _Result_of_impl;
00136
00137
00138 template<typename _Res, typename _Class, typename _T1>
00139 struct _Result_of_impl<false, _Res _Class::*(_T1)>
00140 {
00141 typedef typename _Mem_fn<_Res _Class::*>
00142 ::template _Result_type<_T1>::type type;
00143 };
00144
00145
00146
00147
00148
00149
00150 template<typename _Tp>
00151 struct _Derives_from_unary_function : __sfinae_types
00152 {
00153 private:
00154 template<typename _T1, typename _Res>
00155 static __one __test(const volatile unary_function<_T1, _Res>*);
00156
00157
00158
00159 static __two __test(...);
00160
00161 public:
00162 static const bool value = sizeof(__test((_Tp*)0)) == 1;
00163 };
00164
00165
00166
00167
00168
00169
00170 template<typename _Tp>
00171 struct _Derives_from_binary_function : __sfinae_types
00172 {
00173 private:
00174 template<typename _T1, typename _T2, typename _Res>
00175 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
00176
00177
00178
00179 static __two __test(...);
00180
00181 public:
00182 static const bool value = sizeof(__test((_Tp*)0)) == 1;
00183 };
00184
00185
00186
00187
00188
00189
00190 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
00191 struct _Function_to_function_pointer
00192 {
00193 typedef _Tp type;
00194 };
00195
00196 template<typename _Tp>
00197 struct _Function_to_function_pointer<_Tp, true>
00198 {
00199 typedef _Tp* type;
00200 };
00201
00202
00203
00204
00205
00206
00207
00208
00209 template<bool _Unary, bool _Binary, typename _Tp>
00210 struct _Reference_wrapper_base_impl;
00211
00212
00213 template<typename _Tp>
00214 struct _Reference_wrapper_base_impl<false, false, _Tp>
00215 : _Weak_result_type<_Tp>
00216 { };
00217
00218
00219 template<typename _Tp>
00220 struct _Reference_wrapper_base_impl<true, false, _Tp>
00221 : unary_function<typename _Tp::argument_type,
00222 typename _Tp::result_type>
00223 { };
00224
00225
00226 template<typename _Tp>
00227 struct _Reference_wrapper_base_impl<false, true, _Tp>
00228 : binary_function<typename _Tp::first_argument_type,
00229 typename _Tp::second_argument_type,
00230 typename _Tp::result_type>
00231 { };
00232
00233
00234
00235 template<typename _Tp>
00236 struct _Reference_wrapper_base_impl<true, true, _Tp>
00237 : unary_function<typename _Tp::argument_type,
00238 typename _Tp::result_type>,
00239 binary_function<typename _Tp::first_argument_type,
00240 typename _Tp::second_argument_type,
00241 typename _Tp::result_type>
00242 {
00243 typedef typename _Tp::result_type result_type;
00244 };
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 template<typename _Tp>
00255 struct _Reference_wrapper_base
00256 : _Reference_wrapper_base_impl<
00257 _Derives_from_unary_function<_Tp>::value,
00258 _Derives_from_binary_function<_Tp>::value,
00259 _Tp>
00260 { };
00261
00262
00263 template<typename _Res, typename _T1>
00264 struct _Reference_wrapper_base<_Res(_T1)>
00265 : unary_function<_T1, _Res>
00266 { };
00267
00268
00269 template<typename _Res, typename _T1, typename _T2>
00270 struct _Reference_wrapper_base<_Res(_T1, _T2)>
00271 : binary_function<_T1, _T2, _Res>
00272 { };
00273
00274
00275 template<typename _Res, typename _T1>
00276 struct _Reference_wrapper_base<_Res(*)(_T1)>
00277 : unary_function<_T1, _Res>
00278 { };
00279
00280
00281 template<typename _Res, typename _T1, typename _T2>
00282 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
00283 : binary_function<_T1, _T2, _Res>
00284 { };
00285
00286
00287 template<typename _Res, typename _T1>
00288 struct _Reference_wrapper_base<_Res (_T1::*)()>
00289 : unary_function<_T1*, _Res>
00290 { };
00291
00292
00293 template<typename _Res, typename _T1, typename _T2>
00294 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
00295 : binary_function<_T1*, _T2, _Res>
00296 { };
00297
00298
00299 template<typename _Res, typename _T1>
00300 struct _Reference_wrapper_base<_Res (_T1::*)() const>
00301 : unary_function<const _T1*, _Res>
00302 { };
00303
00304
00305 template<typename _Res, typename _T1, typename _T2>
00306 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
00307 : binary_function<const _T1*, _T2, _Res>
00308 { };
00309
00310
00311 template<typename _Res, typename _T1>
00312 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
00313 : unary_function<volatile _T1*, _Res>
00314 { };
00315
00316
00317 template<typename _Res, typename _T1, typename _T2>
00318 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
00319 : binary_function<volatile _T1*, _T2, _Res>
00320 { };
00321
00322
00323 template<typename _Res, typename _T1>
00324 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
00325 : unary_function<const volatile _T1*, _Res>
00326 { };
00327
00328
00329 template<typename _Res, typename _T1, typename _T2>
00330 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
00331 : binary_function<const volatile _T1*, _T2, _Res>
00332 { };
00333
00334 template<typename _Tp>
00335 class reference_wrapper
00336 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
00337 {
00338
00339
00340 typedef typename _Function_to_function_pointer<_Tp>::type
00341 _M_func_type;
00342
00343 _Tp* _M_data;
00344 public:
00345 typedef _Tp type;
00346 explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
00347 { }
00348
00349 reference_wrapper(const reference_wrapper<_Tp>& __inref):
00350 _M_data(__inref._M_data)
00351 { }
00352
00353 reference_wrapper&
00354 operator=(const reference_wrapper<_Tp>& __inref)
00355 {
00356 _M_data = __inref._M_data;
00357 return *this;
00358 }
00359
00360 operator _Tp&() const
00361 { return this->get(); }
00362
00363 _Tp&
00364 get() const
00365 { return *_M_data; }
00366
00367 #define _GLIBCXX_REPEAT_HEADER <tr1/ref_wrap_iterate.h>
00368 #include <tr1/repeat.h>
00369 #undef _GLIBCXX_REPEAT_HEADER
00370 };
00371
00372
00373
00374 template<typename _Tp>
00375 reference_wrapper<_Tp>
00376 ref(_Tp& __t)
00377 { return reference_wrapper<_Tp>(__t); }
00378
00379
00380 template<typename _Tp>
00381 reference_wrapper<const _Tp>
00382 cref(const _Tp& __t)
00383 { return reference_wrapper<const _Tp>(__t); }
00384
00385 template<typename _Tp>
00386 reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t)
00387 { return ref(__t.get()); }
00388
00389 template<typename _Tp>
00390 reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t)
00391 { return cref(__t.get()); }
00392
00393 template<typename _Tp, bool>
00394 struct _Mem_fn_const_or_non
00395 {
00396 typedef const _Tp& type;
00397 };
00398
00399 template<typename _Tp>
00400 struct _Mem_fn_const_or_non<_Tp, false>
00401 {
00402 typedef _Tp& type;
00403 };
00404
00405 template<typename _Res, typename _Class>
00406 class _Mem_fn<_Res _Class::*>
00407 {
00408
00409
00410 template<typename _Tp>
00411 _Res&
00412 _M_call(_Tp& __object, _Class *) const
00413 { return __object.*__pm; }
00414
00415 template<typename _Tp, typename _Up>
00416 _Res&
00417 _M_call(_Tp& __object, _Up * const *) const
00418 { return (*__object).*__pm; }
00419
00420 template<typename _Tp, typename _Up>
00421 const _Res&
00422 _M_call(_Tp& __object, const _Up * const *) const
00423 { return (*__object).*__pm; }
00424
00425 template<typename _Tp>
00426 const _Res&
00427 _M_call(_Tp& __object, const _Class *) const
00428 { return __object.*__pm; }
00429
00430 template<typename _Tp>
00431 const _Res&
00432 _M_call(_Tp& __ptr, const volatile void*) const
00433 { return (*__ptr).*__pm; }
00434
00435 template<typename _Tp> static _Tp& __get_ref();
00436
00437 template<typename _Tp>
00438 static __sfinae_types::__one __check_const(_Tp&, _Class*);
00439 template<typename _Tp, typename _Up>
00440 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
00441 template<typename _Tp, typename _Up>
00442 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
00443 template<typename _Tp>
00444 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
00445 template<typename _Tp>
00446 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
00447
00448 public:
00449 template<typename _Tp>
00450 struct _Result_type
00451 : _Mem_fn_const_or_non<
00452 _Res,
00453 (sizeof(__sfinae_types::__two)
00454 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
00455 { };
00456
00457 template<typename _Signature>
00458 struct result;
00459
00460 template<typename _CVMem, typename _Tp>
00461 struct result<_CVMem(_Tp)>
00462 : public _Result_type<_Tp> { };
00463
00464 template<typename _CVMem, typename _Tp>
00465 struct result<_CVMem(_Tp&)>
00466 : public _Result_type<_Tp> { };
00467
00468 explicit _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
00469
00470
00471 _Res& operator()(_Class& __object) const
00472 { return __object.*__pm; }
00473
00474 const _Res& operator()(const _Class& __object) const
00475 { return __object.*__pm; }
00476
00477
00478 _Res& operator()(_Class* __object) const
00479 { return __object->*__pm; }
00480
00481 const _Res&
00482 operator()(const _Class* __object) const
00483 { return __object->*__pm; }
00484
00485
00486 template<typename _Tp>
00487 typename _Result_type<_Tp>::type
00488 operator()(_Tp& __unknown) const
00489 { return _M_call(__unknown, &__unknown); }
00490
00491 private:
00492 _Res _Class::*__pm;
00493 };
00494
00495
00496
00497
00498
00499 template<typename _Tp, typename _Class>
00500 inline _Mem_fn<_Tp _Class::*>
00501 mem_fn(_Tp _Class::* __pm)
00502 {
00503 return _Mem_fn<_Tp _Class::*>(__pm);
00504 }
00505
00506
00507
00508
00509
00510
00511 template<typename _Tp>
00512 struct is_bind_expression
00513 {
00514 static const bool value = false;
00515 };
00516
00517
00518
00519
00520
00521 template<typename _Tp>
00522 struct is_placeholder
00523 {
00524 static const int value = 0;
00525 };
00526
00527
00528
00529
00530
00531
00532 template<int _Num> struct _Placeholder { };
00533
00534
00535
00536
00537
00538
00539
00540 template<int _Num>
00541 struct is_placeholder<_Placeholder<_Num> >
00542 {
00543 static const int value = _Num;
00544 };
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559 template<typename _Arg,
00560 bool _IsBindExp = is_bind_expression<_Arg>::value,
00561 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
00562 class _Mu;
00563
00564
00565
00566
00567
00568
00569
00570 template<typename _Tp>
00571 class _Mu<reference_wrapper<_Tp>, false, false>
00572 {
00573 public:
00574 typedef _Tp& result_type;
00575
00576
00577
00578
00579
00580 template<typename _CVRef, typename _Tuple>
00581 result_type
00582 operator()(_CVRef& __arg, const _Tuple&) const volatile
00583 { return __arg.get(); }
00584 };
00585
00586
00587
00588
00589
00590
00591
00592
00593 template<typename _Arg>
00594 class _Mu<_Arg, true, false>
00595 {
00596 public:
00597 template<typename _Signature> class result;
00598
00599 #define _GLIBCXX_REPEAT_HEADER <tr1/mu_iterate.h>
00600 # include <tr1/repeat.h>
00601 #undef _GLIBCXX_REPEAT_HEADER
00602 };
00603
00604
00605
00606
00607
00608
00609
00610
00611 template<typename _Arg>
00612 class _Mu<_Arg, false, true>
00613 {
00614 public:
00615 template<typename _Signature> class result;
00616
00617 template<typename _CVMu, typename _CVArg, typename _Tuple>
00618 class result<_CVMu(_CVArg, _Tuple)>
00619 {
00620
00621
00622
00623 typedef typename tuple_element<(is_placeholder<_Arg>::value - 1),
00624 _Tuple>::type __base_type;
00625
00626 public:
00627 typedef typename add_reference<__base_type>::type type;
00628 };
00629
00630 template<typename _Tuple>
00631 typename result<_Mu(_Arg, _Tuple)>::type
00632 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
00633 {
00634 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
00635 }
00636 };
00637
00638
00639
00640
00641
00642
00643
00644
00645 template<typename _Arg>
00646 class _Mu<_Arg, false, false>
00647 {
00648 public:
00649 template<typename _Signature> struct result;
00650
00651 template<typename _CVMu, typename _CVArg, typename _Tuple>
00652 struct result<_CVMu(_CVArg, _Tuple)>
00653 {
00654 typedef typename add_reference<_CVArg>::type type;
00655 };
00656
00657
00658 template<typename _CVArg, typename _Tuple>
00659 _CVArg& operator()(_CVArg& __arg, const _Tuple&) const volatile
00660 { return __arg; }
00661 };
00662
00663
00664
00665
00666
00667
00668
00669
00670 template<typename _Tp>
00671 struct _Maybe_wrap_member_pointer
00672 {
00673 typedef _Tp type;
00674 static const _Tp& __do_wrap(const _Tp& __x) { return __x; }
00675 };
00676
00677
00678
00679
00680
00681
00682
00683
00684 template<typename _Tp, typename _Class>
00685 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
00686 {
00687 typedef _Mem_fn<_Tp _Class::*> type;
00688 static type __do_wrap(_Tp _Class::* __pm) { return type(__pm); }
00689 };
00690
00691
00692
00693
00694
00695
00696 template<typename _Signature>
00697 struct _Bind;
00698
00699
00700
00701
00702
00703
00704 template<typename _Result, typename _Signature>
00705 struct _Bind_result;
00706
00707
00708
00709
00710
00711
00712 template<typename _Signature>
00713 struct is_bind_expression<_Bind<_Signature> >
00714 {
00715 static const bool value = true;
00716 };
00717
00718
00719
00720
00721
00722
00723 template<typename _Result, typename _Signature>
00724 struct is_bind_expression<_Bind_result<_Result, _Signature> >
00725 {
00726 static const bool value = true;
00727 };
00728
00729
00730
00731
00732
00733
00734 class bad_function_call : public std::exception { };
00735
00736
00737
00738
00739
00740
00741
00742
00743 struct _M_clear_type;
00744
00745
00746
00747
00748
00749
00750
00751
00752 template<typename _Tp>
00753 struct __is_location_invariant
00754 : integral_constant<bool,
00755 (is_pointer<_Tp>::value
00756 || is_member_pointer<_Tp>::value)>
00757 {
00758 };
00759
00760 class _Undefined_class;
00761
00762 union _Nocopy_types
00763 {
00764 void* _M_object;
00765 const void* _M_const_object;
00766 void (*_M_function_pointer)();
00767 void (_Undefined_class::*_M_member_pointer)();
00768 };
00769
00770 union _Any_data {
00771 void* _M_access() { return &_M_pod_data[0]; }
00772 const void* _M_access() const { return &_M_pod_data[0]; }
00773
00774 template<typename _Tp> _Tp& _M_access()
00775 { return *static_cast<_Tp*>(_M_access()); }
00776
00777 template<typename _Tp> const _Tp& _M_access() const
00778 { return *static_cast<const _Tp*>(_M_access()); }
00779
00780 _Nocopy_types _M_unused;
00781 char _M_pod_data[sizeof(_Nocopy_types)];
00782 };
00783
00784 enum _Manager_operation
00785 {
00786 __get_type_info,
00787 __get_functor_ptr,
00788 __clone_functor,
00789 __destroy_functor
00790 };
00791
00792
00793
00794 template<typename _Tp>
00795 struct _Simple_type_wrapper
00796 {
00797 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
00798
00799 _Tp __value;
00800 };
00801
00802 template<typename _Tp>
00803 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
00804 : __is_location_invariant<_Tp>
00805 {
00806 };
00807
00808
00809
00810 template<typename _Functor>
00811 inline _Functor& __callable_functor(_Functor& __f) { return __f; }
00812
00813 template<typename _Member, typename _Class>
00814 inline _Mem_fn<_Member _Class::*>
00815 __callable_functor(_Member _Class::* &__p)
00816 { return mem_fn(__p); }
00817
00818 template<typename _Member, typename _Class>
00819 inline _Mem_fn<_Member _Class::*>
00820 __callable_functor(_Member _Class::* const &__p)
00821 { return mem_fn(__p); }
00822
00823 template<typename _Signature, typename _Functor>
00824 class _Function_handler;
00825
00826 template<typename _Signature>
00827 class function;
00828
00829
00830
00831
00832
00833
00834
00835 class _Function_base
00836 {
00837 public:
00838 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
00839 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
00840
00841 template<typename _Functor>
00842 class _Base_manager
00843 {
00844 protected:
00845 static const bool __stored_locally =
00846 (__is_location_invariant<_Functor>::value
00847 && sizeof(_Functor) <= _M_max_size
00848 && __alignof__(_Functor) <= _M_max_align
00849 && (_M_max_align % __alignof__(_Functor) == 0));
00850 typedef integral_constant<bool, __stored_locally> _Local_storage;
00851
00852
00853 static _Functor* _M_get_pointer(const _Any_data& __source)
00854 {
00855 const _Functor* __ptr =
00856 __stored_locally? &__source._M_access<_Functor>()
00857 : __source._M_access<_Functor*>();
00858 return const_cast<_Functor*>(__ptr);
00859 }
00860
00861
00862
00863 static void
00864 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
00865 {
00866 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
00867 }
00868
00869
00870
00871 static void
00872 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
00873 {
00874 __dest._M_access<_Functor*>() =
00875 new _Functor(*__source._M_access<_Functor*>());
00876 }
00877
00878
00879
00880 static void
00881 _M_destroy(_Any_data& __victim, true_type)
00882 {
00883 __victim._M_access<_Functor>().~_Functor();
00884 }
00885
00886
00887 static void
00888 _M_destroy(_Any_data& __victim, false_type)
00889 {
00890 delete __victim._M_access<_Functor*>();
00891 }
00892
00893 public:
00894 static bool
00895 _M_manager(_Any_data& __dest, const _Any_data& __source,
00896 _Manager_operation __op)
00897 {
00898 switch (__op) {
00899 case __get_type_info:
00900 __dest._M_access<const type_info*>() = &typeid(_Functor);
00901 break;
00902
00903 case __get_functor_ptr:
00904 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
00905 break;
00906
00907 case __clone_functor:
00908 _M_clone(__dest, __source, _Local_storage());
00909 break;
00910
00911 case __destroy_functor:
00912 _M_destroy(__dest, _Local_storage());
00913 break;
00914 }
00915 return false;
00916 }
00917
00918 static void
00919 _M_init_functor(_Any_data& __functor, const _Functor& __f)
00920 {
00921 _M_init_functor(__functor, __f, _Local_storage());
00922 }
00923
00924 template<typename _Signature>
00925 static bool
00926 _M_not_empty_function(const function<_Signature>& __f)
00927 {
00928 return __f;
00929 }
00930
00931 template<typename _Tp>
00932 static bool
00933 _M_not_empty_function(const _Tp*& __fp)
00934 {
00935 return __fp;
00936 }
00937
00938 template<typename _Class, typename _Tp>
00939 static bool
00940 _M_not_empty_function(_Tp _Class::* const& __mp)
00941 {
00942 return __mp;
00943 }
00944
00945 template<typename _Tp>
00946 static bool
00947 _M_not_empty_function(const _Tp&)
00948 {
00949 return true;
00950 }
00951
00952 private:
00953 static void
00954 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
00955 {
00956 new (__functor._M_access()) _Functor(__f);
00957 }
00958
00959 static void
00960 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
00961 {
00962 __functor._M_access<_Functor*>() = new _Functor(__f);
00963 }
00964 };
00965
00966 template<typename _Functor>
00967 class _Ref_manager : public _Base_manager<_Functor*>
00968 {
00969 typedef _Function_base::_Base_manager<_Functor*> _Base;
00970
00971 public:
00972 static bool
00973 _M_manager(_Any_data& __dest, const _Any_data& __source,
00974 _Manager_operation __op)
00975 {
00976 switch (__op) {
00977 case __get_type_info:
00978 __dest._M_access<const type_info*>() = &typeid(_Functor);
00979 break;
00980
00981 case __get_functor_ptr:
00982 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
00983 return is_const<_Functor>::value;
00984 break;
00985
00986 default:
00987 _Base::_M_manager(__dest, __source, __op);
00988 }
00989 return false;
00990 }
00991
00992 static void
00993 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
00994 {
00995
00996 _Base::_M_init_functor(__functor, &__f.get());
00997 }
00998 };
00999
01000 _Function_base() : _M_manager(0) { }
01001
01002 ~_Function_base()
01003 {
01004 if (_M_manager)
01005 {
01006 _M_manager(_M_functor, _M_functor, __destroy_functor);
01007 }
01008 }
01009
01010
01011 bool _M_empty() const { return !_M_manager; }
01012
01013 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
01014 _Manager_operation);
01015
01016 _Any_data _M_functor;
01017 _Manager_type _M_manager;
01018 };
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029 template<typename _Signature>
01030 inline bool
01031 operator==(const function<_Signature>& __f, _M_clear_type*)
01032 {
01033 return !__f;
01034 }
01035
01036
01037
01038
01039 template<typename _Signature>
01040 inline bool
01041 operator==(_M_clear_type*, const function<_Signature>& __f)
01042 {
01043 return !__f;
01044 }
01045
01046
01047
01048
01049
01050
01051
01052
01053 template<typename _Signature>
01054 inline bool
01055 operator!=(const function<_Signature>& __f, _M_clear_type*)
01056 {
01057 return __f;
01058 }
01059
01060
01061
01062
01063 template<typename _Signature>
01064 inline bool
01065 operator!=(_M_clear_type*, const function<_Signature>& __f)
01066 {
01067 return __f;
01068 }
01069
01070
01071
01072
01073
01074
01075
01076
01077 template<typename _Signature>
01078 inline void
01079 swap(function<_Signature>& __x, function<_Signature>& __y)
01080 {
01081 __x.swap(__y);
01082 }
01083
01084 #define _GLIBCXX_JOIN(X,Y) _GLIBCXX_JOIN2( X , Y )
01085 #define _GLIBCXX_JOIN2(X,Y) _GLIBCXX_JOIN3(X,Y)
01086 #define _GLIBCXX_JOIN3(X,Y) X##Y
01087 #define _GLIBCXX_REPEAT_HEADER <tr1/functional_iterate.h>
01088 #include <tr1/repeat.h>
01089 #undef _GLIBCXX_REPEAT_HEADER
01090 #undef _GLIBCXX_JOIN3
01091 #undef _GLIBCXX_JOIN2
01092 #undef _GLIBCXX_JOIN
01093
01094
01095
01096 template<typename T>
01097 struct hash;
01098
01099 #define tr1_hashtable_define_trivial_hash(T) \
01100 template<> \
01101 struct hash<T> \
01102 { \
01103 std::size_t \
01104 operator()(T val) const \
01105 { return static_cast<std::size_t>(val); } \
01106 }
01107
01108 tr1_hashtable_define_trivial_hash(bool);
01109 tr1_hashtable_define_trivial_hash(char);
01110 tr1_hashtable_define_trivial_hash(signed char);
01111 tr1_hashtable_define_trivial_hash(unsigned char);
01112 tr1_hashtable_define_trivial_hash(wchar_t);
01113 tr1_hashtable_define_trivial_hash(short);
01114 tr1_hashtable_define_trivial_hash(int);
01115 tr1_hashtable_define_trivial_hash(long);
01116 tr1_hashtable_define_trivial_hash(unsigned short);
01117 tr1_hashtable_define_trivial_hash(unsigned int);
01118 tr1_hashtable_define_trivial_hash(unsigned long);
01119
01120 #undef tr1_hashtable_define_trivial_hash
01121
01122 template<typename T>
01123 struct hash<T*>
01124 {
01125 std::size_t
01126 operator()(T* p) const
01127 { return reinterpret_cast<std::size_t>(p); }
01128 };
01129
01130
01131
01132
01133
01134 template<std::size_t = sizeof(std::size_t)>
01135 struct Fnv_hash
01136 {
01137 static std::size_t
01138 hash(const char* first, std::size_t length)
01139 {
01140 std::size_t result = 0;
01141 for (; length > 0; --length)
01142 result = (result * 131) + *first++;
01143 return result;
01144 }
01145 };
01146
01147 template<>
01148 struct Fnv_hash<4>
01149 {
01150 static std::size_t
01151 hash(const char* first, std::size_t length)
01152 {
01153 std::size_t result = 2166136261UL;
01154 for (; length > 0; --length)
01155 {
01156 result ^= (std::size_t)*first++;
01157 result *= 16777619UL;
01158 }
01159 return result;
01160 }
01161 };
01162
01163 template<>
01164 struct Fnv_hash<8>
01165 {
01166 static std::size_t
01167 hash(const char* first, std::size_t length)
01168 {
01169 std::size_t result = 14695981039346656037ULL;
01170 for (; length > 0; --length)
01171 {
01172 result ^= (std::size_t)*first++;
01173 result *= 1099511628211ULL;
01174 }
01175 return result;
01176 }
01177 };
01178
01179
01180
01181
01182 template<>
01183 struct hash<std::string>
01184 {
01185 std::size_t
01186 operator()(const std::string& s) const
01187 { return Fnv_hash<>::hash(s.data(), s.length()); }
01188 };
01189
01190 #ifdef _GLIBCXX_USE_WCHAR_T
01191 template<>
01192 struct hash<std::wstring>
01193 {
01194 std::size_t
01195 operator()(const std::wstring& s) const
01196 {
01197 return Fnv_hash<>::hash(reinterpret_cast<const char*>(s.data()),
01198 s.length() * sizeof(wchar_t));
01199 }
01200 };
01201 #endif
01202
01203 template<>
01204 struct hash<float>
01205 {
01206 std::size_t
01207 operator()(float fval) const
01208 {
01209 std::size_t result = 0;
01210
01211
01212 if (fval != 0.0f)
01213 result = Fnv_hash<>::hash(reinterpret_cast<const char*>(&fval),
01214 sizeof(fval));
01215 return result;
01216 }
01217 };
01218
01219 template<>
01220 struct hash<double>
01221 {
01222 std::size_t
01223 operator()(double dval) const
01224 {
01225 std::size_t result = 0;
01226
01227
01228 if (dval != 0.0)
01229 result = Fnv_hash<>::hash(reinterpret_cast<const char*>(&dval),
01230 sizeof(dval));
01231 return result;
01232 }
01233 };
01234
01235
01236
01237 template<>
01238 struct hash<long double>
01239 {
01240 std::size_t
01241 operator()(long double ldval) const
01242 {
01243 std::size_t result = 0;
01244
01245 int exponent;
01246 ldval = std::frexp(ldval, &exponent);
01247 ldval = ldval < 0.0l ? -(ldval + 0.5l) : ldval;
01248
01249 const long double mult = std::numeric_limits<std::size_t>::max() + 1.0l;
01250 ldval *= mult;
01251
01252
01253
01254 const std::size_t hibits = (std::size_t)ldval;
01255 ldval = (ldval - (long double)hibits) * mult;
01256
01257 const std::size_t coeff =
01258 (std::numeric_limits<std::size_t>::max()
01259 / std::numeric_limits<long double>::max_exponent);
01260
01261 result = hibits + (std::size_t)ldval + coeff * exponent;
01262
01263 return result;
01264 }
01265 };
01266 }
01267 }
01268
01269 #endif