tr1/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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 
00030 /** @file
00031  *  This is a TR1 C++ Library header.
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>               // for std::tr1::hash
00042 #include <cstdlib>              // for std::abort
00043 #include <cmath>                // for std::frexp
00044 #include <tr1/tuple>
00045 
00046 namespace std
00047 {
00048 namespace tr1
00049 {
00050   template<typename _MemberPointer>
00051     class _Mem_fn;
00052 
00053   /**
00054    *  @if maint
00055    *  Actual implementation of _Has_result_type, which uses SFINAE to
00056    *  determine if the type _Tp has a publicly-accessible member type
00057    *  result_type.
00058    *  @endif
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    *  @if maint
00086    *  If we have found a result_type, extract it.
00087    *  @endif
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    *  @if maint
00101    *  Base class for any function object that has a weak result type, as
00102    *  defined in 3.3/3 of TR1.
00103    *  @endif
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    *  @if maint
00113    *  Strip top-level cv-qualifiers from the function object and let
00114    *  _Weak_result_type_impl perform the real work.
00115    *  @endif
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    *  @if maint
00128    *  Actual implementation of result_of. When _Has_result_type is
00129    *  true, gets its result from _Weak_result_type. Otherwise, uses
00130    *  the function object's member template result to extract the
00131    *  result type.
00132    *  @endif
00133   */
00134   template<bool _Has_result_type, typename _Signature>
00135     struct _Result_of_impl;
00136 
00137   // Handle member data pointers using _Mem_fn's logic
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    *  @if maint
00147    *  Determines if the type _Tp derives from unary_function.
00148    *  @endif
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       // It's tempting to change "..." to const volatile void*, but
00158       // that fails when _Tp is a function type.
00159       static __two __test(...);
00160 
00161     public:
00162       static const bool value = sizeof(__test((_Tp*)0)) == 1;
00163     };
00164 
00165   /**
00166    *  @if maint
00167    *  Determines if the type _Tp derives from binary_function.
00168    *  @endif
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       // It's tempting to change "..." to const volatile void*, but
00178       // that fails when _Tp is a function type.
00179       static __two __test(...);
00180 
00181     public:
00182       static const bool value = sizeof(__test((_Tp*)0)) == 1;
00183     };
00184 
00185   /**
00186    *  @if maint
00187    *  Turns a function type into a function pointer type
00188    *  @endif
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    *  @if maint
00204    *  Knowing which of unary_function and binary_function _Tp derives
00205    *  from, derives from the same and ensures that reference_wrapper
00206    *  will have a weak result type. See cases below.
00207    *  @endif
00208    */
00209   template<bool _Unary, bool _Binary, typename _Tp>
00210     struct _Reference_wrapper_base_impl;
00211 
00212   // Not a unary_function or binary_function, so try a weak result type
00213   template<typename _Tp>
00214     struct _Reference_wrapper_base_impl<false, false, _Tp>
00215       : _Weak_result_type<_Tp>
00216     { };
00217 
00218   // unary_function but not binary_function
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   // binary_function but not unary_function
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   // both unary_function and binary_function. import result_type to
00234   // avoid conflicts.
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    *  @if maint
00248    *  Derives from unary_function or binary_function when it
00249    *  can. Specializations handle all of the easy cases. The primary
00250    *  template determines what to do with a class type, which may
00251    *  derive from both unary_function and binary_function.
00252    *  @endif
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   // - a function type (unary)
00263   template<typename _Res, typename _T1>
00264     struct _Reference_wrapper_base<_Res(_T1)>
00265       : unary_function<_T1, _Res>
00266     { };
00267 
00268   // - a function type (binary)
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   // - a function pointer type (unary)
00275   template<typename _Res, typename _T1>
00276     struct _Reference_wrapper_base<_Res(*)(_T1)>
00277       : unary_function<_T1, _Res>
00278     { };
00279 
00280   // - a function pointer type (binary)
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   // - a pointer to member function type (unary, no qualifiers)
00287   template<typename _Res, typename _T1>
00288     struct _Reference_wrapper_base<_Res (_T1::*)()>
00289       : unary_function<_T1*, _Res>
00290     { };
00291 
00292   // - a pointer to member function type (binary, no qualifiers)
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   // - a pointer to member function type (unary, const)
00299   template<typename _Res, typename _T1>
00300     struct _Reference_wrapper_base<_Res (_T1::*)() const>
00301       : unary_function<const _T1*, _Res>
00302     { };
00303 
00304   // - a pointer to member function type (binary, const)
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   // - a pointer to member function type (unary, volatile)
00311   template<typename _Res, typename _T1>
00312     struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
00313       : unary_function<volatile _T1*, _Res>
00314     { };
00315 
00316   // - a pointer to member function type (binary, volatile)
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   // - a pointer to member function type (unary, const volatile)
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   // - a pointer to member function type (binary, const volatile)
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       // If _Tp is a function type, we can't form result_of<_Tp(...)>,
00339       // so turn it into a function pointer type.
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   // Denotes a reference should be taken to a variable.
00374   template<typename _Tp>
00375     reference_wrapper<_Tp>
00376     ref(_Tp& __t)
00377     { return reference_wrapper<_Tp>(__t); }
00378 
00379   // Denotes a const reference should be taken to a variable.
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     // This bit of genius is due to Peter Dimov, improved slightly by
00409     // Douglas Gregor.
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     // Handle objects
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     // Handle pointers
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     // Handle smart pointers and derived
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    *  @brief Returns a function object that forwards to the member
00497    *  pointer @a pm.
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    *  @brief Determines if the given type _Tp is a function object
00508    *  should be treated as a subexpression when evaluating calls to
00509    *  function objects returned by bind(). [TR1 3.6.1]
00510    */
00511   template<typename _Tp>
00512     struct is_bind_expression
00513     {
00514       static const bool value = false;
00515     };
00516 
00517   /**
00518    *  @brief Determines if the given type _Tp is a placeholder in a
00519    *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
00520    */
00521   template<typename _Tp>
00522     struct is_placeholder
00523     {
00524       static const int value = 0;
00525     };
00526 
00527   /**
00528    *  @if maint
00529    *  The type of placeholder objects defined by libstdc++.
00530    *  @endif
00531    */
00532   template<int _Num> struct _Placeholder { };
00533 
00534   /**
00535    *  @if maint
00536    *  Partial specialization of is_placeholder that provides the placeholder
00537    *  number for the placeholder objects defined by libstdc++.
00538    *  @endif
00539    */
00540   template<int _Num>
00541     struct is_placeholder<_Placeholder<_Num> >
00542     {
00543       static const int value = _Num;
00544     };
00545 
00546   /**
00547    *  @if maint
00548    *  Maps an argument to bind() into an actual argument to the bound
00549    *  function object [TR1 3.6.3/5]. Only the first parameter should
00550    *  be specified: the rest are used to determine among the various
00551    *  implementations. Note that, although this class is a function
00552    *  object, isn't not entirely normal because it takes only two
00553    *  parameters regardless of the number of parameters passed to the
00554    *  bind expression. The first parameter is the bound argument and
00555    *  the second parameter is a tuple containing references to the
00556    *  rest of the arguments.
00557    *  @endif
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    *  @if maint
00566    *  If the argument is reference_wrapper<_Tp>, returns the
00567    *  underlying reference. [TR1 3.6.3/5 bullet 1]
00568    *  @endif
00569    */
00570   template<typename _Tp>
00571     class _Mu<reference_wrapper<_Tp>, false, false>
00572     {
00573     public:
00574       typedef _Tp& result_type;
00575 
00576       /* Note: This won't actually work for const volatile
00577        * reference_wrappers, because reference_wrapper::get() is const
00578        * but not volatile-qualified. This might be a defect in the TR.
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    *  @if maint
00588    *  If the argument is a bind expression, we invoke the underlying
00589    *  function object with the same cv-qualifiers as we are given and
00590    *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
00591    *  @endif
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    *  @if maint
00606    *  If the argument is a placeholder for the Nth argument, returns
00607    *  a reference to the Nth argument to the bind function object.
00608    *  [TR1 3.6.3/5 bullet 3]
00609    *  @endif
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         // Add a reference, if it hasn't already been done for us.
00621         // This allows us to be a little bit sloppy in constructing
00622         // the tuple that we pass to result_of<...>.
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    *  @if maint
00640    *  If the argument is just a value, returns a reference to that
00641    *  value. The cv-qualifiers on the reference are the same as the
00642    *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
00643    *  @endif
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       // Pick up the cv-qualifiers of the argument
00658       template<typename _CVArg, typename _Tuple>
00659       _CVArg& operator()(_CVArg& __arg, const _Tuple&) const volatile
00660       { return __arg; }
00661     };
00662 
00663   /**
00664    *  @if maint
00665    *  Maps member pointers into instances of _Mem_fn but leaves all
00666    *  other function objects untouched. Used by tr1::bind(). The
00667    *  primary template handles the non--member-pointer case.
00668    *  @endif
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    *  @if maint
00679    *  Maps member pointers into instances of _Mem_fn but leaves all
00680    *  other function objects untouched. Used by tr1::bind(). This
00681    *  partial specialization handles the member pointer case.
00682    *  @endif
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    *  @if maint
00693    *  Type of the function object returned from bind().
00694    *  @endif
00695    */
00696    template<typename _Signature>
00697      struct _Bind;
00698 
00699   /**
00700    *  @if maint
00701    *  Type of the function object returned from bind<R>().
00702    *  @endif
00703    */
00704    template<typename _Result, typename _Signature>
00705      struct _Bind_result;
00706 
00707   /**
00708    *  @if maint
00709    *  Class template _Bind is always a bind expression.
00710    *  @endif
00711    */
00712    template<typename _Signature>
00713     struct is_bind_expression<_Bind<_Signature> >
00714     {
00715       static const bool value = true;
00716     };
00717 
00718   /**
00719    *  @if maint
00720    *  Class template _Bind_result is always a bind expression.
00721    *  @endif
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    *  @brief Exception class thrown when class template function's
00731    *  operator() is called with an empty target.
00732    *
00733    */
00734   class bad_function_call : public std::exception { };
00735 
00736   /**
00737    *  @if maint
00738    *  The integral constant expression 0 can be converted into a
00739    *  pointer to this type. It is used by the function template to
00740    *  accept NULL pointers.
00741    *  @endif
00742    */
00743   struct _M_clear_type;
00744 
00745   /**
00746    *  @if maint
00747    *  Trait identifying "location-invariant" types, meaning that the
00748    *  address of the object (or any of its members) will not escape.
00749    *  Also implies a trivial copy constructor and assignment operator.
00750    *   @endif
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   /* Simple type wrapper that helps avoid annoying const problems
00793      when casting between void pointers and pointers-to-pointers. */
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   // Converts a reference to a function object into a callable
00809   // function object.
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    *  @if maint
00832    *  Base class of all polymorphic function object wrappers.
00833    *  @endif
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       // Retrieve a pointer to the function object
00853       static _Functor* _M_get_pointer(const _Any_data& __source)
00854       {
00855         const _Functor* __ptr =
00856           __stored_locally? &__source._M_access<_Functor>()
00857           /* have stored a pointer */ : __source._M_access<_Functor*>();
00858         return const_cast<_Functor*>(__ptr);
00859       }
00860 
00861       // Clone a location-invariant function object that fits within
00862       // an _Any_data structure.
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       // Clone a function object that is not location-invariant or
00870       // that cannot fit into an _Any_data structure.
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       // Destroying a location-invariant object may still require
00879       // destruction.
00880       static void
00881       _M_destroy(_Any_data& __victim, true_type)
00882       {
00883         __victim._M_access<_Functor>().~_Functor();
00884       }
00885 
00886       // Destroying an object located on the heap.
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         // TBD: Use address_of function instead
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   // [3.7.2.7] null pointer comparisons
01021 
01022   /**
01023    *  @brief Compares a polymorphic function object wrapper against 0
01024    *  (the NULL pointer).
01025    *  @returns @c true if the wrapper has no target, @c false otherwise
01026    *
01027    *  This function will not throw an exception.
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    *  @overload
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    *  @brief Compares a polymorphic function object wrapper against 0
01048    *  (the NULL pointer).
01049    *  @returns @c false if the wrapper has no target, @c true otherwise
01050    *
01051    *  This function will not throw an exception.
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    *  @overload
01062    */
01063   template<typename _Signature>
01064     inline bool
01065     operator!=(_M_clear_type*, const function<_Signature>& __f)
01066     {
01067       return __f;
01068     }
01069 
01070   // [3.7.2.8] specialized algorithms
01071 
01072   /**
01073    *  @brief Swap the targets of two polymorphic function object wrappers.
01074    *
01075    *  This function will not throw an exception.
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   // Definition of default hash function std::tr1::hash<>.  The types for
01095   // which std::tr1::hash<T> is defined is in clause 6.3.3. of the PDTR.
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   // Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
01131   // (used by the next specializations of std::tr1::hash<>)
01132 
01133   // Dummy generic implementation (for sizeof(size_t) != 4, 8).
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   // XXX String and floating point hashes probably shouldn't be inline
01180   // member functions, since are nontrivial.  Once we have the framework
01181   // for TR1 .cc files, these should go in one.
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     // 0 and -0 both hash to zero.
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     // 0 and -0 both hash to zero.
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   // For long double, careful with random padding bits (e.g., on x86,
01236   // 10 bytes -> 12 bytes) and resort to frexp.
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     // Try to use all the bits of the mantissa (really necessary only
01253     // on 32-bit targets, at least for 80-bit floating point formats).
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

Generated on Sat Oct 1 15:08:51 2005 for libstdc++ source by  doxygen 1.4.4