functional

Go to the documentation of this file.
00001 // TR1 functional header -*- C++ -*-
00002 
00003 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00034 #ifndef _TR1_FUNCTIONAL
00035 #define _TR1_FUNCTIONAL 1
00036 
00037 #pragma GCC system_header
00038 
00039 #include "../functional"
00040 #include <typeinfo>
00041 #include <tr1/type_traits>
00042 #include <bits/cpp_type_traits.h>
00043 #include <string>               // for std::tr1::hash
00044 #include <cstdlib>              // for std::abort
00045 #include <cmath>                // for std::frexp
00046 #include <tr1/tuple>
00047 
00048 namespace std
00049 {
00050 namespace tr1
00051 {
00052   template<typename _MemberPointer>
00053     class _Mem_fn;
00054 
00062   template<typename _Tp>
00063     class _Has_result_type_helper : __sfinae_types
00064     {
00065       template<typename _Up>
00066       struct _Wrap_type
00067       { };
00068 
00069       template<typename _Up>
00070         static __one __test(_Wrap_type<typename _Up::result_type>*);
00071 
00072       template<typename _Up>
00073         static __two __test(...);
00074 
00075     public:
00076       static const bool value = sizeof(__test<_Tp>(0)) == 1;
00077     };
00078 
00079   template<typename _Tp>
00080     struct _Has_result_type
00081        : integral_constant<
00082            bool,
00083            _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
00084     { };
00085 
00091   template<bool _Has_result_type, typename _Functor>
00092     struct _Maybe_get_result_type
00093     { };
00094 
00095   template<typename _Functor>
00096     struct _Maybe_get_result_type<true, _Functor>
00097     {
00098       typedef typename _Functor::result_type result_type;
00099     };
00100 
00107   template<typename _Functor>
00108     struct _Weak_result_type_impl
00109       : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
00110     {
00111     };
00112 
00119   template<typename _Functor>
00120     struct _Weak_result_type
00121     : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
00122     {
00123     };
00124 
00125   template<typename _Signature>
00126     class result_of;
00127 
00136   template<bool _Has_result_type, typename _Signature>
00137     struct _Result_of_impl;
00138 
00139   // Handle member data pointers using _Mem_fn's logic
00140   template<typename _Res, typename _Class, typename _T1>
00141     struct _Result_of_impl<false, _Res _Class::*(_T1)>
00142     {
00143       typedef typename _Mem_fn<_Res _Class::*>
00144                 ::template _Result_type<_T1>::type type;
00145     };
00146 
00152   template<typename _Tp>
00153     struct _Derives_from_unary_function : __sfinae_types
00154     {
00155     private:
00156       template<typename _T1, typename _Res>
00157         static __one __test(const volatile unary_function<_T1, _Res>*);
00158 
00159       // It's tempting to change "..." to const volatile void*, but
00160       // that fails when _Tp is a function type.
00161       static __two __test(...);
00162 
00163     public:
00164       static const bool value = sizeof(__test((_Tp*)0)) == 1;
00165     };
00166 
00172   template<typename _Tp>
00173     struct _Derives_from_binary_function : __sfinae_types
00174     {
00175     private:
00176       template<typename _T1, typename _T2, typename _Res>
00177         static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
00178 
00179       // It's tempting to change "..." to const volatile void*, but
00180       // that fails when _Tp is a function type.
00181       static __two __test(...);
00182 
00183     public:
00184       static const bool value = sizeof(__test((_Tp*)0)) == 1;
00185     };
00186 
00192   template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
00193     struct _Function_to_function_pointer
00194     {
00195       typedef _Tp type;
00196     };
00197 
00198   template<typename _Tp>
00199     struct _Function_to_function_pointer<_Tp, true>
00200     {
00201       typedef _Tp* type;
00202     };
00203 
00211   template<bool _Unary, bool _Binary, typename _Tp>
00212     struct _Reference_wrapper_base_impl;
00213 
00214   // Not a unary_function or binary_function, so try a weak result type
00215   template<typename _Tp>
00216     struct _Reference_wrapper_base_impl<false, false, _Tp>
00217       : _Weak_result_type<_Tp>
00218     { };
00219 
00220   // unary_function but not binary_function
00221   template<typename _Tp>
00222     struct _Reference_wrapper_base_impl<true, false, _Tp>
00223       : unary_function<typename _Tp::argument_type,
00224                        typename _Tp::result_type>
00225     { };
00226 
00227   // binary_function but not unary_function
00228   template<typename _Tp>
00229     struct _Reference_wrapper_base_impl<false, true, _Tp>
00230       : binary_function<typename _Tp::first_argument_type,
00231                         typename _Tp::second_argument_type,
00232                         typename _Tp::result_type>
00233     { };
00234 
00235   // both unary_function and binary_function. import result_type to
00236   // avoid conflicts.
00237    template<typename _Tp>
00238     struct _Reference_wrapper_base_impl<true, true, _Tp>
00239       : unary_function<typename _Tp::argument_type,
00240                        typename _Tp::result_type>,
00241         binary_function<typename _Tp::first_argument_type,
00242                         typename _Tp::second_argument_type,
00243                         typename _Tp::result_type>
00244     {
00245       typedef typename _Tp::result_type result_type;
00246     };
00247 
00256   template<typename _Tp>
00257     struct _Reference_wrapper_base
00258       : _Reference_wrapper_base_impl<
00259           _Derives_from_unary_function<_Tp>::value,
00260           _Derives_from_binary_function<_Tp>::value,
00261           _Tp>
00262     { };
00263 
00264   // - a function type (unary)
00265   template<typename _Res, typename _T1>
00266     struct _Reference_wrapper_base<_Res(_T1)>
00267       : unary_function<_T1, _Res>
00268     { };
00269 
00270   // - a function type (binary)
00271   template<typename _Res, typename _T1, typename _T2>
00272     struct _Reference_wrapper_base<_Res(_T1, _T2)>
00273       : binary_function<_T1, _T2, _Res>
00274     { };
00275 
00276   // - a function pointer type (unary)
00277   template<typename _Res, typename _T1>
00278     struct _Reference_wrapper_base<_Res(*)(_T1)>
00279       : unary_function<_T1, _Res>
00280     { };
00281 
00282   // - a function pointer type (binary)
00283   template<typename _Res, typename _T1, typename _T2>
00284     struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
00285       : binary_function<_T1, _T2, _Res>
00286     { };
00287 
00288   // - a pointer to member function type (unary, no qualifiers)
00289   template<typename _Res, typename _T1>
00290     struct _Reference_wrapper_base<_Res (_T1::*)()>
00291       : unary_function<_T1*, _Res>
00292     { };
00293 
00294   // - a pointer to member function type (binary, no qualifiers)
00295   template<typename _Res, typename _T1, typename _T2>
00296     struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
00297       : binary_function<_T1*, _T2, _Res>
00298     { };
00299 
00300   // - a pointer to member function type (unary, const)
00301   template<typename _Res, typename _T1>
00302     struct _Reference_wrapper_base<_Res (_T1::*)() const>
00303       : unary_function<const _T1*, _Res>
00304     { };
00305 
00306   // - a pointer to member function type (binary, const)
00307   template<typename _Res, typename _T1, typename _T2>
00308     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
00309       : binary_function<const _T1*, _T2, _Res>
00310     { };
00311 
00312   // - a pointer to member function type (unary, volatile)
00313   template<typename _Res, typename _T1>
00314     struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
00315       : unary_function<volatile _T1*, _Res>
00316     { };
00317 
00318   // - a pointer to member function type (binary, volatile)
00319   template<typename _Res, typename _T1, typename _T2>
00320     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
00321       : binary_function<volatile _T1*, _T2, _Res>
00322     { };
00323 
00324   // - a pointer to member function type (unary, const volatile)
00325   template<typename _Res, typename _T1>
00326     struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
00327       : unary_function<const volatile _T1*, _Res>
00328     { };
00329 
00330   // - a pointer to member function type (binary, const volatile)
00331   template<typename _Res, typename _T1, typename _T2>
00332     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
00333       : binary_function<const volatile _T1*, _T2, _Res>
00334     { };
00335 
00336   template<typename _Tp>
00337     class reference_wrapper
00338       : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
00339     {
00340       // If _Tp is a function type, we can't form result_of<_Tp(...)>,
00341       // so turn it into a function pointer type.
00342       typedef typename _Function_to_function_pointer<_Tp>::type
00343         _M_func_type;
00344 
00345       _Tp* _M_data;
00346     public:
00347       typedef _Tp type;
00348       explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
00349       { }
00350 
00351       reference_wrapper(const reference_wrapper<_Tp>& __inref):
00352       _M_data(__inref._M_data)
00353       { }
00354 
00355       reference_wrapper&
00356       operator=(const reference_wrapper<_Tp>& __inref)
00357       {
00358         _M_data = __inref._M_data;
00359         return *this;
00360       }
00361 
00362       operator _Tp&() const
00363       { return this->get(); }
00364 
00365       _Tp&
00366       get() const
00367       { return *_M_data; }
00368 
00369 #define _GLIBCXX_REPEAT_HEADER <tr1/ref_wrap_iterate.h>
00370 #include <tr1/repeat.h>
00371 #undef _GLIBCXX_REPEAT_HEADER
00372     };
00373 
00374 
00375   // Denotes a reference should be taken to a variable.
00376   template<typename _Tp>
00377     inline reference_wrapper<_Tp>
00378     ref(_Tp& __t)
00379     { return reference_wrapper<_Tp>(__t); }
00380 
00381   // Denotes a const reference should be taken to a variable.
00382   template<typename _Tp>
00383     inline reference_wrapper<const _Tp>
00384     cref(const _Tp& __t)
00385     { return reference_wrapper<const _Tp>(__t); }
00386 
00387   template<typename _Tp>
00388     inline reference_wrapper<_Tp>
00389     ref(reference_wrapper<_Tp> __t)
00390     { return ref(__t.get()); }
00391 
00392   template<typename _Tp>
00393     inline reference_wrapper<const _Tp>
00394     cref(reference_wrapper<_Tp> __t)
00395     { return cref(__t.get()); }
00396 
00397    template<typename _Tp, bool>
00398      struct _Mem_fn_const_or_non
00399      {
00400        typedef const _Tp& type;
00401      };
00402 
00403     template<typename _Tp>
00404       struct _Mem_fn_const_or_non<_Tp, false>
00405       {
00406         typedef _Tp& type;
00407       };
00408 
00409   template<typename _Res, typename _Class>
00410   class _Mem_fn<_Res _Class::*>
00411   {
00412     // This bit of genius is due to Peter Dimov, improved slightly by
00413     // Douglas Gregor.
00414     template<typename _Tp>
00415       _Res&
00416       _M_call(_Tp& __object, _Class *) const
00417       { return __object.*__pm; }
00418 
00419     template<typename _Tp, typename _Up>
00420       _Res&
00421       _M_call(_Tp& __object, _Up * const *) const
00422       { return (*__object).*__pm; }
00423 
00424     template<typename _Tp, typename _Up>
00425       const _Res&
00426       _M_call(_Tp& __object, const _Up * const *) const
00427       { return (*__object).*__pm; }
00428 
00429     template<typename _Tp>
00430       const _Res&
00431       _M_call(_Tp& __object, const _Class *) const
00432       { return __object.*__pm; }
00433 
00434     template<typename _Tp>
00435       const _Res&
00436       _M_call(_Tp& __ptr, const volatile void*) const
00437       { return (*__ptr).*__pm; }
00438 
00439     template<typename _Tp> static _Tp& __get_ref();
00440 
00441     template<typename _Tp>
00442       static __sfinae_types::__one __check_const(_Tp&, _Class*);
00443     template<typename _Tp, typename _Up>
00444       static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
00445     template<typename _Tp, typename _Up>
00446       static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
00447     template<typename _Tp>
00448       static __sfinae_types::__two __check_const(_Tp&, const _Class*);
00449     template<typename _Tp>
00450       static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
00451 
00452   public:
00453     template<typename _Tp>
00454       struct _Result_type
00455         : _Mem_fn_const_or_non<
00456             _Res,
00457             (sizeof(__sfinae_types::__two)
00458              == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
00459       { };
00460 
00461     template<typename _Signature>
00462       struct result;
00463 
00464     template<typename _CVMem, typename _Tp>
00465       struct result<_CVMem(_Tp)>
00466         : public _Result_type<_Tp> { };
00467 
00468     template<typename _CVMem, typename _Tp>
00469       struct result<_CVMem(_Tp&)>
00470         : public _Result_type<_Tp> { };
00471 
00472     explicit _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
00473 
00474     // Handle objects
00475     _Res&       operator()(_Class& __object)       const
00476     { return __object.*__pm; }
00477 
00478     const _Res& operator()(const _Class& __object) const
00479     { return __object.*__pm; }
00480 
00481     // Handle pointers
00482     _Res&       operator()(_Class* __object)       const
00483     { return __object->*__pm; }
00484 
00485     const _Res&
00486     operator()(const _Class* __object) const
00487     { return __object->*__pm; }
00488 
00489     // Handle smart pointers and derived
00490     template<typename _Tp>
00491       typename _Result_type<_Tp>::type
00492       operator()(_Tp& __unknown) const
00493       { return _M_call(__unknown, &__unknown); }
00494 
00495   private:
00496     _Res _Class::*__pm;
00497   };
00498 
00503   template<typename _Tp, typename _Class>
00504     inline _Mem_fn<_Tp _Class::*>
00505     mem_fn(_Tp _Class::* __pm)
00506     {
00507       return _Mem_fn<_Tp _Class::*>(__pm);
00508     }
00509 
00515   template<typename _Tp>
00516     struct is_bind_expression
00517     {
00518       static const bool value = false;
00519     };
00520 
00525   template<typename _Tp>
00526     struct is_placeholder
00527     {
00528       static const int value = 0;
00529     };
00530 
00536   template<int _Num> struct _Placeholder { };
00537 
00544   template<int _Num>
00545     struct is_placeholder<_Placeholder<_Num> >
00546     {
00547       static const int value = _Num;
00548     };
00549 
00563   template<typename _Arg,
00564            bool _IsBindExp = is_bind_expression<_Arg>::value,
00565            bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
00566     class _Mu;
00567 
00574   template<typename _Tp>
00575     class _Mu<reference_wrapper<_Tp>, false, false>
00576     {
00577     public:
00578       typedef _Tp& result_type;
00579 
00580       /* Note: This won't actually work for const volatile
00581        * reference_wrappers, because reference_wrapper::get() is const
00582        * but not volatile-qualified. This might be a defect in the TR.
00583        */
00584       template<typename _CVRef, typename _Tuple>
00585       result_type
00586       operator()(_CVRef& __arg, const _Tuple&) const volatile
00587       { return __arg.get(); }
00588     };
00589 
00597   template<typename _Arg>
00598     class _Mu<_Arg, true, false>
00599     {
00600     public:
00601       template<typename _Signature> class result;
00602 
00603 #define _GLIBCXX_REPEAT_HEADER <tr1/mu_iterate.h>
00604 #  include <tr1/repeat.h>
00605 #undef _GLIBCXX_REPEAT_HEADER
00606     };
00607 
00615   template<typename _Arg>
00616     class _Mu<_Arg, false, true>
00617     {
00618     public:
00619       template<typename _Signature> class result;
00620 
00621       template<typename _CVMu, typename _CVArg, typename _Tuple>
00622       class result<_CVMu(_CVArg, _Tuple)>
00623       {
00624         // Add a reference, if it hasn't already been done for us.
00625         // This allows us to be a little bit sloppy in constructing
00626         // the tuple that we pass to result_of<...>.
00627         typedef typename tuple_element<(is_placeholder<_Arg>::value - 1),
00628                                        _Tuple>::type __base_type;
00629 
00630       public:
00631         typedef typename add_reference<__base_type>::type type;
00632       };
00633 
00634       template<typename _Tuple>
00635       typename result<_Mu(_Arg, _Tuple)>::type
00636       operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
00637       {
00638         return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
00639       }
00640     };
00641 
00649   template<typename _Arg>
00650     class _Mu<_Arg, false, false>
00651     {
00652     public:
00653       template<typename _Signature> struct result;
00654 
00655       template<typename _CVMu, typename _CVArg, typename _Tuple>
00656       struct result<_CVMu(_CVArg, _Tuple)>
00657       {
00658         typedef typename add_reference<_CVArg>::type type;
00659       };
00660 
00661       // Pick up the cv-qualifiers of the argument
00662       template<typename _CVArg, typename _Tuple>
00663       _CVArg& operator()(_CVArg& __arg, const _Tuple&) const volatile
00664       { return __arg; }
00665     };
00666 
00674   template<typename _Tp>
00675     struct _Maybe_wrap_member_pointer
00676     {
00677       typedef _Tp type;
00678       static const _Tp& __do_wrap(const _Tp& __x) { return __x; }
00679     };
00680 
00688   template<typename _Tp, typename _Class>
00689     struct _Maybe_wrap_member_pointer<_Tp _Class::*>
00690     {
00691       typedef _Mem_fn<_Tp _Class::*> type;
00692       static type __do_wrap(_Tp _Class::* __pm) { return type(__pm); }
00693     };
00694 
00700    template<typename _Signature>
00701      struct _Bind;
00702 
00708    template<typename _Result, typename _Signature>
00709      struct _Bind_result;
00710 
00716    template<typename _Signature>
00717     struct is_bind_expression<_Bind<_Signature> >
00718     {
00719       static const bool value = true;
00720     };
00721 
00727    template<typename _Result, typename _Signature>
00728    struct is_bind_expression<_Bind_result<_Result, _Signature> >
00729     {
00730       static const bool value = true;
00731     };
00732 
00738   class bad_function_call : public std::exception { };
00739 
00747   struct _M_clear_type;
00748 
00756   template<typename _Tp>
00757     struct __is_location_invariant
00758     : integral_constant<bool,
00759                         (is_pointer<_Tp>::value
00760                          || is_member_pointer<_Tp>::value)>
00761     {
00762     };
00763 
00764   class _Undefined_class;
00765 
00766   union _Nocopy_types
00767   {
00768     void*       _M_object;
00769     const void* _M_const_object;
00770     void (*_M_function_pointer)();
00771     void (_Undefined_class::*_M_member_pointer)();
00772   };
00773 
00774   union _Any_data {
00775     void*       _M_access()       { return &_M_pod_data[0]; }
00776     const void* _M_access() const { return &_M_pod_data[0]; }
00777 
00778     template<typename _Tp> _Tp& _M_access()
00779     { return *static_cast<_Tp*>(_M_access()); }
00780 
00781     template<typename _Tp> const _Tp& _M_access() const
00782     { return *static_cast<const _Tp*>(_M_access()); }
00783 
00784     _Nocopy_types _M_unused;
00785     char _M_pod_data[sizeof(_Nocopy_types)];
00786   };
00787 
00788   enum _Manager_operation
00789   {
00790     __get_type_info,
00791     __get_functor_ptr,
00792     __clone_functor,
00793     __destroy_functor
00794   };
00795 
00796   /* Simple type wrapper that helps avoid annoying const problems
00797      when casting between void pointers and pointers-to-pointers. */
00798   template<typename _Tp>
00799     struct _Simple_type_wrapper
00800     {
00801       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
00802 
00803       _Tp __value;
00804     };
00805 
00806   template<typename _Tp>
00807     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
00808       : __is_location_invariant<_Tp>
00809     {
00810     };
00811 
00812   // Converts a reference to a function object into a callable
00813   // function object.
00814   template<typename _Functor>
00815     inline _Functor& __callable_functor(_Functor& __f) { return __f; }
00816 
00817   template<typename _Member, typename _Class>
00818     inline _Mem_fn<_Member _Class::*>
00819     __callable_functor(_Member _Class::* &__p)
00820     { return mem_fn(__p); }
00821 
00822   template<typename _Member, typename _Class>
00823     inline _Mem_fn<_Member _Class::*>
00824     __callable_functor(_Member _Class::* const &__p)
00825     { return mem_fn(__p); }
00826 
00827   template<typename _Signature, typename _Functor>
00828     class _Function_handler;
00829 
00830   template<typename _Signature>
00831     class function;
00832 
00833 
00839   class _Function_base
00840   {
00841   public:
00842     static const std::size_t _M_max_size = sizeof(_Nocopy_types);
00843     static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
00844 
00845     template<typename _Functor>
00846     class _Base_manager
00847     {
00848     protected:
00849       static const bool __stored_locally =
00850         (__is_location_invariant<_Functor>::value
00851          && sizeof(_Functor) <= _M_max_size
00852          && __alignof__(_Functor) <= _M_max_align
00853          && (_M_max_align % __alignof__(_Functor) == 0));
00854       typedef integral_constant<bool, __stored_locally> _Local_storage;
00855 
00856       // Retrieve a pointer to the function object
00857       static _Functor* _M_get_pointer(const _Any_data& __source)
00858       {
00859         const _Functor* __ptr =
00860           __stored_locally? &__source._M_access<_Functor>()
00861           /* have stored a pointer */ : __source._M_access<_Functor*>();
00862         return const_cast<_Functor*>(__ptr);
00863       }
00864 
00865       // Clone a location-invariant function object that fits within
00866       // an _Any_data structure.
00867       static void
00868       _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
00869       {
00870         new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
00871       }
00872 
00873       // Clone a function object that is not location-invariant or
00874       // that cannot fit into an _Any_data structure.
00875       static void
00876       _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
00877       {
00878         __dest._M_access<_Functor*>() =
00879           new _Functor(*__source._M_access<_Functor*>());
00880       }
00881 
00882       // Destroying a location-invariant object may still require
00883       // destruction.
00884       static void
00885       _M_destroy(_Any_data& __victim, true_type)
00886       {
00887         __victim._M_access<_Functor>().~_Functor();
00888       }
00889 
00890       // Destroying an object located on the heap.
00891       static void
00892       _M_destroy(_Any_data& __victim, false_type)
00893       {
00894         delete __victim._M_access<_Functor*>();
00895       }
00896 
00897     public:
00898       static bool
00899       _M_manager(_Any_data& __dest, const _Any_data& __source,
00900                  _Manager_operation __op)
00901       {
00902         switch (__op) {
00903         case __get_type_info:
00904           __dest._M_access<const type_info*>() = &typeid(_Functor);
00905           break;
00906 
00907         case __get_functor_ptr:
00908           __dest._M_access<_Functor*>() = _M_get_pointer(__source);
00909           break;
00910 
00911         case __clone_functor:
00912           _M_clone(__dest, __source, _Local_storage());
00913           break;
00914 
00915         case __destroy_functor:
00916           _M_destroy(__dest, _Local_storage());
00917           break;
00918         }
00919         return false;
00920       }
00921 
00922       static void
00923       _M_init_functor(_Any_data& __functor, const _Functor& __f)
00924       {
00925         _M_init_functor(__functor, __f, _Local_storage());
00926       }
00927 
00928       template<typename _Signature>
00929       static bool
00930       _M_not_empty_function(const function<_Signature>& __f)
00931       {
00932         return __f;
00933       }
00934 
00935       template<typename _Tp>
00936       static bool
00937       _M_not_empty_function(const _Tp*& __fp)
00938       {
00939         return __fp;
00940       }
00941 
00942       template<typename _Class, typename _Tp>
00943       static bool
00944       _M_not_empty_function(_Tp _Class::* const& __mp)
00945       {
00946         return __mp;
00947       }
00948 
00949       template<typename _Tp>
00950       static bool
00951       _M_not_empty_function(const _Tp&)
00952       {
00953         return true;
00954       }
00955 
00956     private:
00957       static void
00958       _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
00959       {
00960         new (__functor._M_access()) _Functor(__f);
00961       }
00962 
00963       static void
00964       _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
00965       {
00966         __functor._M_access<_Functor*>() = new _Functor(__f);
00967       }
00968     };
00969 
00970     template<typename _Functor>
00971     class _Ref_manager : public _Base_manager<_Functor*>
00972     {
00973       typedef _Function_base::_Base_manager<_Functor*> _Base;
00974 
00975     public:
00976       static bool
00977       _M_manager(_Any_data& __dest, const _Any_data& __source,
00978                  _Manager_operation __op)
00979       {
00980         switch (__op) {
00981         case __get_type_info:
00982           __dest._M_access<const type_info*>() = &typeid(_Functor);
00983           break;
00984 
00985         case __get_functor_ptr:
00986           __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
00987           return is_const<_Functor>::value;
00988           break;
00989 
00990         default:
00991           _Base::_M_manager(__dest, __source, __op);
00992         }
00993         return false;
00994       }
00995 
00996       static void
00997       _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
00998       {
00999         // TBD: Use address_of function instead
01000         _Base::_M_init_functor(__functor, &__f.get());
01001       }
01002     };
01003 
01004     _Function_base() : _M_manager(0) { }
01005 
01006     ~_Function_base()
01007     {
01008       if (_M_manager)
01009         {
01010           _M_manager(_M_functor, _M_functor, __destroy_functor);
01011         }
01012     }
01013 
01014 
01015     bool _M_empty() const { return !_M_manager; }
01016 
01017     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
01018                                   _Manager_operation);
01019 
01020     _Any_data     _M_functor;
01021     _Manager_type _M_manager;
01022   };
01023 
01024   // [3.7.2.7] null pointer comparisons
01025 
01033   template<typename _Signature>
01034     inline bool
01035     operator==(const function<_Signature>& __f, _M_clear_type*)
01036     {
01037       return !__f;
01038     }
01039 
01043   template<typename _Signature>
01044     inline bool
01045     operator==(_M_clear_type*, const function<_Signature>& __f)
01046     {
01047       return !__f;
01048     }
01049 
01057   template<typename _Signature>
01058     inline bool
01059     operator!=(const function<_Signature>& __f, _M_clear_type*)
01060     {
01061       return __f;
01062     }
01063 
01067   template<typename _Signature>
01068     inline bool
01069     operator!=(_M_clear_type*, const function<_Signature>& __f)
01070     {
01071       return __f;
01072     }
01073 
01074   // [3.7.2.8] specialized algorithms
01075 
01081   template<typename _Signature>
01082     inline void
01083     swap(function<_Signature>& __x, function<_Signature>& __y)
01084     {
01085       __x.swap(__y);
01086     }
01087 
01088 #define _GLIBCXX_JOIN(X,Y) _GLIBCXX_JOIN2( X , Y )
01089 #define _GLIBCXX_JOIN2(X,Y) _GLIBCXX_JOIN3(X,Y)
01090 #define _GLIBCXX_JOIN3(X,Y) X##Y
01091 #define _GLIBCXX_REPEAT_HEADER <tr1/functional_iterate.h>
01092 #include <tr1/repeat.h>
01093 #undef _GLIBCXX_REPEAT_HEADER
01094 #undef _GLIBCXX_JOIN3
01095 #undef _GLIBCXX_JOIN2
01096 #undef _GLIBCXX_JOIN
01097 
01098   // Definition of default hash function std::tr1::hash<>.  The types for
01099   // which std::tr1::hash<T> is defined is in clause 6.3.3. of the PDTR.
01100   template<typename T>
01101     struct hash;
01102 
01103 #define tr1_hashtable_define_trivial_hash(T)            \
01104   template<>                                            \
01105     struct hash<T>                                      \
01106     : public std::unary_function<T, std::size_t>        \
01107     {                                                   \
01108       std::size_t                                       \
01109       operator()(T val) const                           \
01110       { return static_cast<std::size_t>(val); }         \
01111     }                                                     
01112 
01113   tr1_hashtable_define_trivial_hash(bool);
01114   tr1_hashtable_define_trivial_hash(char);
01115   tr1_hashtable_define_trivial_hash(signed char);
01116   tr1_hashtable_define_trivial_hash(unsigned char);
01117   tr1_hashtable_define_trivial_hash(wchar_t);
01118   tr1_hashtable_define_trivial_hash(short);
01119   tr1_hashtable_define_trivial_hash(int);
01120   tr1_hashtable_define_trivial_hash(long);
01121   tr1_hashtable_define_trivial_hash(unsigned short);
01122   tr1_hashtable_define_trivial_hash(unsigned int);
01123   tr1_hashtable_define_trivial_hash(unsigned long);
01124 
01125 #undef tr1_hashtable_define_trivial_hash
01126 
01127   template<typename T>
01128     struct hash<T*>
01129     : public std::unary_function<T*, std::size_t>
01130     {
01131       std::size_t
01132       operator()(T* p) const
01133       { return reinterpret_cast<std::size_t>(p); }
01134     };
01135 
01136   // Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
01137   // (used by the next specializations of std::tr1::hash<>)
01138 
01139   // Dummy generic implementation (for sizeof(size_t) != 4, 8).
01140   template<std::size_t = sizeof(std::size_t)>
01141     struct Fnv_hash
01142     {
01143       static std::size_t
01144       hash(const char* first, std::size_t length)
01145       {
01146     std::size_t result = 0;
01147     for (; length > 0; --length)
01148       result = (result * 131) + *first++;
01149     return result;
01150       }
01151     };
01152 
01153   template<>
01154     struct Fnv_hash<4>
01155     {
01156       static std::size_t
01157       hash(const char* first, std::size_t length)
01158       {
01159     std::size_t result = static_cast<std::size_t>(2166136261UL);
01160     for (; length > 0; --length)
01161       {
01162         result ^= (std::size_t)*first++;
01163         result *= 16777619UL;
01164       }
01165     return result;
01166       }
01167     };
01168   
01169   template<>
01170     struct Fnv_hash<8>
01171     {
01172       static std::size_t
01173       hash(const char* first, std::size_t length)
01174       {
01175     std::size_t result = static_cast<std::size_t>(14695981039346656037ULL);
01176     for (; length > 0; --length)
01177       {
01178         result ^= (std::size_t)*first++;
01179         result *= 1099511628211ULL;
01180       }
01181     return result;
01182       }
01183     };
01184 
01185   // XXX String and floating point hashes probably shouldn't be inline
01186   // member functions, since are nontrivial.  Once we have the framework
01187   // for TR1 .cc files, these should go in one.
01188   template<>
01189     struct hash<std::string>
01190     : public std::unary_function<std::string, std::size_t>
01191     {      
01192       std::size_t
01193       operator()(const std::string& s) const
01194       { return Fnv_hash<>::hash(s.data(), s.length()); }
01195     };
01196 
01197 #ifdef _GLIBCXX_USE_WCHAR_T
01198   template<>
01199     struct hash<std::wstring>
01200     : public std::unary_function<std::wstring, std::size_t>
01201     {
01202       std::size_t
01203       operator()(const std::wstring& s) const
01204       {
01205     return Fnv_hash<>::hash(reinterpret_cast<const char*>(s.data()),
01206                 s.length() * sizeof(wchar_t));
01207       }
01208     };
01209 #endif
01210 
01211   template<>
01212     struct hash<float>
01213     : public std::unary_function<float, std::size_t>
01214     {
01215       std::size_t
01216       operator()(float fval) const
01217       {
01218     std::size_t result = 0;
01219 
01220     // 0 and -0 both hash to zero.
01221     if (fval != 0.0f)
01222       result = Fnv_hash<>::hash(reinterpret_cast<const char*>(&fval),
01223                     sizeof(fval));
01224     return result;
01225       }
01226     };
01227 
01228   template<>
01229     struct hash<double>
01230     : public std::unary_function<double, std::size_t>
01231     {
01232       std::size_t
01233       operator()(double dval) const
01234       {
01235     std::size_t result = 0;
01236 
01237     // 0 and -0 both hash to zero.
01238     if (dval != 0.0)
01239       result = Fnv_hash<>::hash(reinterpret_cast<const char*>(&dval),
01240                     sizeof(dval));
01241     return result;
01242       }
01243     };
01244 
01245   // For long double, careful with random padding bits (e.g., on x86,
01246   // 10 bytes -> 12 bytes) and resort to frexp.
01247   template<>
01248     struct hash<long double>
01249     : public std::unary_function<long double, std::size_t>
01250     {
01251       std::size_t
01252       operator()(long double ldval) const
01253       {
01254     std::size_t result = 0;
01255 
01256     int exponent;
01257     ldval = std::frexp(ldval, &exponent);
01258     ldval = ldval < 0.0l ? -(ldval + 0.5l) : ldval;
01259 
01260     const long double mult = std::numeric_limits<std::size_t>::max() + 1.0l;
01261     ldval *= mult;
01262 
01263     // Try to use all the bits of the mantissa (really necessary only
01264     // on 32-bit targets, at least for 80-bit floating point formats).
01265     const std::size_t hibits = (std::size_t)ldval;
01266     ldval = (ldval - (long double)hibits) * mult;
01267 
01268     const std::size_t coeff =
01269       (std::numeric_limits<std::size_t>::max()
01270        / std::numeric_limits<long double>::max_exponent);
01271 
01272     result = hibits + (std::size_t)ldval + coeff * exponent;
01273 
01274     return result;
01275       }
01276     };
01277 }
01278 }
01279 
01280 #endif

Generated on Tue Feb 2 16:55:57 2010 for GNU C++ STL by  doxygen 1.4.7