# 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 <tr1/tuple>
    00044
    00045 namespace std
    00046 {
    00047 namespace tr1
    00048 {
    00049   template<typename _MemberPointer>
    00050     class _Mem_fn;
    00051
    00052   /**
    00053    *  @if maint
    00054    *  Actual implementation of _Has_result_type, which uses SFINAE to
    00055    *  determine if the type _Tp has a publicly-accessible member type
    00056    *  result_type.
    00057    *  @endif
    00058   */
    00059   template<typename _Tp>
    00060     class _Has_result_type_helper : __sfinae_types
    00061     {
    00062       template<typename _Up>
    00063       struct _Wrap_type
    00064       { };
    00065
    00066       template<typename _Up>
    00067         static __one __test(_Wrap_type<typename _Up::result_type>*);
    00068
    00069       template<typename _Up>
    00070         static __two __test(...);
    00071
    00072     public:
    00073       static const bool value = sizeof(__test<_Tp>(0)) == 1;
    00074     };
    00075
    00076   template<typename _Tp>
    00077     struct _Has_result_type
    00078        : integral_constant<
    00079            bool,
    00080            _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
    00081     { };
    00082
    00083   /**
    00084    *  @if maint
    00085    *  If we have found a result_type, extract it.
    00086    *  @endif
    00087   */
    00088   template<bool _Has_result_type, typename _Functor>
    00089     struct _Maybe_get_result_type
    00090     { };
    00091
    00092   template<typename _Functor>
    00093     struct _Maybe_get_result_type<true, _Functor>
    00094     {
    00095       typedef typename _Functor::result_type result_type;
    00096     };
    00097
    00098   /**
    00099    *  @if maint
    00100    *  Base class for any function object that has a weak result type, as
    00101    *  defined in 3.3/3 of TR1.
    00102    *  @endif
    00103   */
    00104   template<typename _Functor>
    00105     struct _Weak_result_type_impl
    00106       : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
    00107     {
    00108     };
    00109
    00110   /**
    00111    *  @if maint
    00112    *  Strip top-level cv-qualifiers from the function object and let
    00113    *  _Weak_result_type_impl perform the real work.
    00114    *  @endif
    00115   */
    00116   template<typename _Functor>
    00117     struct _Weak_result_type
    00118     : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
    00119     {
    00120     };
    00121
    00122   template<typename _Signature>
    00123     class result_of;
    00124
    00125   /**
    00126    *  @if maint
    00127    *  Actual implementation of result_of. When _Has_result_type is
    00128    *  true, gets its result from _Weak_result_type. Otherwise, uses
    00129    *  the function object's member template result to extract the
    00130    *  result type.
    00131    *  @endif
    00132   */
    00133   template<bool _Has_result_type, typename _Signature>
    00134     struct _Result_of_impl;
    00135
    00136   // Handle member data pointers using _Mem_fn's logic
    00137   template<typename _Res, typename _Class, typename _T1>
    00138     struct _Result_of_impl<false, _Res _Class::*(_T1)>
    00139     {
    00140       typedef typename _Mem_fn<_Res _Class::*>
    00141                 ::template _Result_type<_T1>::type type;
    00142     };
    00143
    00144   /**
    00145    *  @if maint
    00146    *  Determines if the type _Tp derives from unary_function.
    00147    *  @endif
    00148   */
    00149   template<typename _Tp>
    00150     struct _Derives_from_unary_function : __sfinae_types
    00151     {
    00152     private:
    00153       template<typename _T1, typename _Res>
    00154         static __one __test(const volatile unary_function<_T1, _Res>*);
    00155
    00156       // It's tempting to change "..." to const volatile void*, but
    00157       // that fails when _Tp is a function type.
    00158       static __two __test(...);
    00159
    00160     public:
    00161       static const bool value = sizeof(__test((_Tp*)0)) == 1;
    00162     };
    00163
    00164   /**
    00165    *  @if maint
    00166    *  Determines if the type _Tp derives from binary_function.
    00167    *  @endif
    00168   */
    00169   template<typename _Tp>
    00170     struct _Derives_from_binary_function : __sfinae_types
    00171     {
    00172     private:
    00173       template<typename _T1, typename _T2, typename _Res>
    00174         static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
    00175
    00176       // It's tempting to change "..." to const volatile void*, but
    00177       // that fails when _Tp is a function type.
    00178       static __two __test(...);
    00179
    00180     public:
    00181       static const bool value = sizeof(__test((_Tp*)0)) == 1;
    00182     };
    00183
    00184   /**
    00185    *  @if maint
    00186    *  Turns a function type into a function pointer type
    00187    *  @endif
    00188   */
    00189   template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
    00190     struct _Function_to_function_pointer
    00191     {
    00192       typedef _Tp type;
    00193     };
    00194
    00195   template<typename _Tp>
    00196     struct _Function_to_function_pointer<_Tp, true>
    00197     {
    00198       typedef _Tp* type;
    00199     };
    00200
    00201   /**
    00202    *  @if maint
    00203    *  Knowing which of unary_function and binary_function _Tp derives
    00204    *  from, derives from the same and ensures that reference_wrapper
    00205    *  will have a weak result type. See cases below.
    00206    *  @endif
    00207    */
    00208   template<bool _Unary, bool _Binary, typename _Tp>
    00209     struct _Reference_wrapper_base_impl;
    00210
    00211   // Not a unary_function or binary_function, so try a weak result type
    00212   template<typename _Tp>
    00213     struct _Reference_wrapper_base_impl<false, false, _Tp>
    00214       : _Weak_result_type<_Tp>
    00215     { };
    00216
    00217   // unary_function but not binary_function
    00218   template<typename _Tp>
    00219     struct _Reference_wrapper_base_impl<true, false, _Tp>
    00220       : unary_function<typename _Tp::argument_type,
    00221                        typename _Tp::result_type>
    00222     { };
    00223
    00224   // binary_function but not unary_function
    00225   template<typename _Tp>
    00226     struct _Reference_wrapper_base_impl<false, true, _Tp>
    00227       : binary_function<typename _Tp::first_argument_type,
    00228                         typename _Tp::second_argument_type,
    00229                         typename _Tp::result_type>
    00230     { };
    00231
    00232   // both unary_function and binary_function. import result_type to
    00233   // avoid conflicts.
    00234    template<typename _Tp>
    00235     struct _Reference_wrapper_base_impl<true, true, _Tp>
    00236       : unary_function<typename _Tp::argument_type,
    00237                        typename _Tp::result_type>,
    00238         binary_function<typename _Tp::first_argument_type,
    00239                         typename _Tp::second_argument_type,
    00240                         typename _Tp::result_type>
    00241     {
    00242       typedef typename _Tp::result_type result_type;
    00243     };
    00244
    00245   /**
    00246    *  @if maint
    00247    *  Derives from unary_function or binary_function when it
    00248    *  can. Specializations handle all of the easy cases. The primary
    00249    *  template determines what to do with a class type, which may
    00250    *  derive from both unary_function and binary_function.
    00251    *  @endif
    00252   */
    00253   template<typename _Tp>
    00254     struct _Reference_wrapper_base
    00255       : _Reference_wrapper_base_impl<
    00256           _Derives_from_unary_function<_Tp>::value,
    00257           _Derives_from_binary_function<_Tp>::value,
    00258           _Tp>
    00259     { };
    00260
    00261   // - a function type (unary)
    00262   template<typename _Res, typename _T1>
    00263     struct _Reference_wrapper_base<_Res(_T1)>
    00264       : unary_function<_T1, _Res>
    00265     { };
    00266
    00267   // - a function type (binary)
    00268   template<typename _Res, typename _T1, typename _T2>
    00269     struct _Reference_wrapper_base<_Res(_T1, _T2)>
    00270       : binary_function<_T1, _T2, _Res>
    00271     { };
    00272
    00273   // - a function pointer type (unary)
    00274   template<typename _Res, typename _T1>
    00275     struct _Reference_wrapper_base<_Res(*)(_T1)>
    00276       : unary_function<_T1, _Res>
    00277     { };
    00278
    00279   // - a function pointer type (binary)
    00280   template<typename _Res, typename _T1, typename _T2>
    00281     struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
    00282       : binary_function<_T1, _T2, _Res>
    00283     { };
    00284
    00285   // - a pointer to member function type (unary, no qualifiers)
    00286   template<typename _Res, typename _T1>
    00287     struct _Reference_wrapper_base<_Res (_T1::*)()>
    00288       : unary_function<_T1*, _Res>
    00289     { };
    00290
    00291   // - a pointer to member function type (binary, no qualifiers)
    00292   template<typename _Res, typename _T1, typename _T2>
    00293     struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
    00294       : binary_function<_T1*, _T2, _Res>
    00295     { };
    00296
    00297   // - a pointer to member function type (unary, const)
    00298   template<typename _Res, typename _T1>
    00299     struct _Reference_wrapper_base<_Res (_T1::*)() const>
    00300       : unary_function<const _T1*, _Res>
    00301     { };
    00302
    00303   // - a pointer to member function type (binary, const)
    00304   template<typename _Res, typename _T1, typename _T2>
    00305     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
    00306       : binary_function<const _T1*, _T2, _Res>
    00307     { };
    00308
    00309   // - a pointer to member function type (unary, volatile)
    00310   template<typename _Res, typename _T1>
    00311     struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
    00312       : unary_function<volatile _T1*, _Res>
    00313     { };
    00314
    00315   // - a pointer to member function type (binary, volatile)
    00316   template<typename _Res, typename _T1, typename _T2>
    00317     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
    00318       : binary_function<volatile _T1*, _T2, _Res>
    00319     { };
    00320
    00321   // - a pointer to member function type (unary, const volatile)
    00322   template<typename _Res, typename _T1>
    00323     struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
    00324       : unary_function<const volatile _T1*, _Res>
    00325     { };
    00326
    00327   // - a pointer to member function type (binary, const volatile)
    00328   template<typename _Res, typename _T1, typename _T2>
    00329     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
    00330       : binary_function<const volatile _T1*, _T2, _Res>
    00331     { };
    00332
    00333   template<typename _Tp>
    00334     class reference_wrapper
    00335       : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
    00336     {
    00337       // If _Tp is a function type, we can't form result_of<_Tp(...)>,
    00338       // so turn it into a function pointer type.
    00339       typedef typename _Function_to_function_pointer<_Tp>::type
    00340         _M_func_type;
    00341
    00342       _Tp* _M_data;
    00343     public:
    00344       typedef _Tp type;
    00345       explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
    00346       { }
    00347
    00348       reference_wrapper(const reference_wrapper<_Tp>& __inref):
    00349       _M_data(__inref._M_data)
    00350       { }
    00351
    00352       reference_wrapper&
    00353       operator=(const reference_wrapper<_Tp>& __inref)
    00354       {
    00355         _M_data = __inref._M_data;
    00356         return *this;
    00357       }
    00358
    00359       operator _Tp&() const
    00360       { return this->get(); }
    00361
    00362       _Tp&
    00363       get() const
    00364       { return *_M_data; }
    00365
    00366 #define _GLIBCXX_REPEAT_HEADER <tr1/ref_wrap_iterate.h>
    00367 #include <tr1/repeat.h>
    00368 #undef _GLIBCXX_REPEAT_HEADER
    00369     };
    00370
    00371
    00372   // Denotes a reference should be taken to a variable.
    00373   template<typename _Tp>
    00374     reference_wrapper<_Tp>
    00375     ref(_Tp& __t)
    00376     { return reference_wrapper<_Tp>(__t); }
    00377
    00378   // Denotes a const reference should be taken to a variable.
    00379   template<typename _Tp>
    00380     reference_wrapper<const _Tp>
    00381     cref(const _Tp& __t)
    00382     { return reference_wrapper<const _Tp>(__t); }
    00383
    00384   template<typename _Tp>
    00385     reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t)
    00386     { return ref(__t.get()); }
    00387
    00388   template<typename _Tp>
    00389     reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t)
    00390     { return cref(__t.get()); }
    00391
    00392    template<typename _Tp, bool>
    00393      struct _Mem_fn_const_or_non
    00394      {
    00395        typedef const _Tp& type;
    00396      };
    00397
    00398     template<typename _Tp>
    00399       struct _Mem_fn_const_or_non<_Tp, false>
    00400       {
    00401         typedef _Tp& type;
    00402       };
    00403
    00404   template<typename _Res, typename _Class>
    00405   class _Mem_fn<_Res _Class::*>
    00406   {
    00407     // This bit of genius is due to Peter Dimov, improved slightly by
    00408     // Douglas Gregor.
    00409     template<typename _Tp>
    00410       _Res&
    00411       _M_call(_Tp& __object, _Class *) const
    00412       { return __object.*__pm; }
    00413
    00414     template<typename _Tp, typename _Up>
    00415       _Res&
    00416       _M_call(_Tp& __object, _Up * const *) const
    00417       { return (*__object).*__pm; }
    00418
    00419     template<typename _Tp, typename _Up>
    00420       const _Res&
    00421       _M_call(_Tp& __object, const _Up * const *) const
    00422       { return (*__object).*__pm; }
    00423
    00424     template<typename _Tp>
    00425       const _Res&
    00426       _M_call(_Tp& __object, const _Class *) const
    00427       { return __object.*__pm; }
    00428
    00429     template<typename _Tp>
    00430       const _Res&
    00431       _M_call(_Tp& __ptr, const volatile void*) const
    00432       { return (*__ptr).*__pm; }
    00433
    00434     template<typename _Tp> static _Tp& __get_ref();
    00435
    00436     template<typename _Tp>
    00437       static __sfinae_types::__one __check_const(_Tp&, _Class*);
    00438     template<typename _Tp, typename _Up>
    00439       static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
    00440     template<typename _Tp, typename _Up>
    00441       static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
    00442     template<typename _Tp>
    00443       static __sfinae_types::__two __check_const(_Tp&, const _Class*);
    00444     template<typename _Tp>
    00445       static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
    00446
    00447   public:
    00448     template<typename _Tp>
    00449       struct _Result_type
    00450         : _Mem_fn_const_or_non<
    00451             _Res,
    00452             (sizeof(__sfinae_types::__two)
    00453              == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
    00454       { };
    00455
    00456     template<typename _Signature>
    00457       struct result;
    00458
    00459     template<typename _CVMem, typename _Tp>
    00460       struct result<_CVMem(_Tp)>
    00461         : public _Result_type<_Tp> { };
    00462
    00463     template<typename _CVMem, typename _Tp>
    00464       struct result<_CVMem(_Tp&)>
    00465         : public _Result_type<_Tp> { };
    00466
    00467     explicit _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
    00468
    00469     // Handle objects
    00470     _Res&       operator()(_Class& __object)       const
    00471     { return __object.*__pm; }
    00472
    00473     const _Res& operator()(const _Class& __object) const
    00474     { return __object.*__pm; }
    00475
    00476     // Handle pointers
    00477     _Res&       operator()(_Class* __object)       const
    00478     { return __object->*__pm; }
    00479
    00480     const _Res&
    00481     operator()(const _Class* __object) const
    00482     { return __object->*__pm; }
    00483
    00484     // Handle smart pointers and derived
    00485     template<typename _Tp>
    00486       typename _Result_type<_Tp>::type
    00487       operator()(_Tp& __unknown) const
    00488       { return _M_call(__unknown, &__unknown); }
    00489
    00490   private:
    00491     _Res _Class::*__pm;
    00492   };
    00493
    00494   /**
    00495    *  @brief Returns a function object that forwards to the member
    00496    *  pointer @a pm.
    00497    */
    00498   template<typename _Tp, typename _Class>
    00499     inline _Mem_fn<_Tp _Class::*>
    00500     mem_fn(_Tp _Class::* __pm)
    00501     {
    00502       return _Mem_fn<_Tp _Class::*>(__pm);
    00503     }
    00504
    00505   /**
    00506    *  @brief Determines if the given type _Tp is a function object
    00507    *  should be treated as a subexpression when evaluating calls to
    00508    *  function objects returned by bind(). [TR1 3.6.1]
    00509    */
    00510   template<typename _Tp>
    00511     struct is_bind_expression
    00512     {
    00513       static const bool value = false;
    00514     };
    00515
    00516   /**
    00517    *  @brief Determines if the given type _Tp is a placeholder in a
    00518    *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
    00519    */
    00520   template<typename _Tp>
    00521     struct is_placeholder
    00522     {
    00523       static const int value = 0;
    00524     };
    00525
    00526   /**
    00527    *  @if maint
    00528    *  The type of placeholder objects defined by libstdc++.
    00529    *  @endif
    00530    */
    00531   template<int _Num> struct _Placeholder { };
    00532
    00533   /**
    00534    *  @if maint
    00535    *  Partial specialization of is_placeholder that provides the placeholder
    00536    *  number for the placeholder objects defined by libstdc++.
    00537    *  @endif
    00538    */
    00539   template<int _Num>
    00540     struct is_placeholder<_Placeholder<_Num> >
    00541     {
    00542       static const int value = _Num;
    00543     };
    00544
    00545   /**
    00546    *  @if maint
    00547    *  Maps an argument to bind() into an actual argument to the bound
    00548    *  function object [TR1 3.6.3/5]. Only the first parameter should
    00549    *  be specified: the rest are used to determine among the various
    00550    *  implementations. Note that, although this class is a function
    00551    *  object, isn't not entirely normal because it takes only two
    00552    *  parameters regardless of the number of parameters passed to the
    00553    *  bind expression. The first parameter is the bound argument and
    00554    *  the second parameter is a tuple containing references to the
    00555    *  rest of the arguments.
    00556    *  @endif
    00557    */
    00558   template<typename _Arg,
    00559            bool _IsBindExp = is_bind_expression<_Arg>::value,
    00560            bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
    00561     class _Mu;
    00562
    00563   /**
    00564    *  @if maint
    00565    *  If the argument is reference_wrapper<_Tp>, returns the
    00566    *  underlying reference. [TR1 3.6.3/5 bullet 1]
    00567    *  @endif
    00568    */
    00569   template<typename _Tp>
    00570     class _Mu<reference_wrapper<_Tp>, false, false>
    00571     {
    00572     public:
    00573       typedef _Tp& result_type;
    00574
    00575       /* Note: This won't actually work for const volatile
    00576        * reference_wrappers, because reference_wrapper::get() is const
    00577        * but not volatile-qualified. This might be a defect in the TR.
    00578        */
    00579       template<typename _CVRef, typename _Tuple>
    00580       result_type
    00581       operator()(_CVRef& __arg, const _Tuple&) const volatile
    00582       { return __arg.get(); }
    00583     };
    00584
    00585   /**
    00586    *  @if maint
    00587    *  If the argument is a bind expression, we invoke the underlying
    00588    *  function object with the same cv-qualifiers as we are given and
    00589    *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
    00590    *  @endif
    00591    */
    00592   template<typename _Arg>
    00593     class _Mu<_Arg, true, false>
    00594     {
    00595     public:
    00596       template<typename _Signature> class result;
    00597
    00598 #define _GLIBCXX_REPEAT_HEADER <tr1/mu_iterate.h>
    00599 #  include <tr1/repeat.h>
    00600 #undef _GLIBCXX_REPEAT_HEADER
    00601     };
    00602
    00603   /**
    00604    *  @if maint
    00605    *  If the argument is a placeholder for the Nth argument, returns
    00606    *  a reference to the Nth argument to the bind function object.
    00607    *  [TR1 3.6.3/5 bullet 3]
    00608    *  @endif
    00609    */
    00610   template<typename _Arg>
    00611     class _Mu<_Arg, false, true>
    00612     {
    00613     public:
    00614       template<typename _Signature> class result;
    00615
    00616       template<typename _CVMu, typename _CVArg, typename _Tuple>
    00617       class result<_CVMu(_CVArg, _Tuple)>
    00618       {
    00619         // Add a reference, if it hasn't already been done for us.
    00620         // This allows us to be a little bit sloppy in constructing
    00621         // the tuple that we pass to result_of<...>.
    00622         typedef typename tuple_element<(is_placeholder<_Arg>::value - 1),
    00623                                        _Tuple>::type __base_type;
    00624
    00625       public:
    00626         typedef typename add_reference<__base_type>::type type;
    00627       };
    00628
    00629       template<typename _Tuple>
    00630       typename result<_Mu(_Arg, _Tuple)>::type
    00631       operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
    00632       {
    00633         return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
    00634       }
    00635     };
    00636
    00637   /**
    00638    *  @if maint
    00639    *  If the argument is just a value, returns a reference to that
    00640    *  value. The cv-qualifiers on the reference are the same as the
    00641    *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
    00642    *  @endif
    00643    */
    00644   template<typename _Arg>
    00645     class _Mu<_Arg, false, false>
    00646     {
    00647     public:
    00648       template<typename _Signature> struct result;
    00649
    00650       template<typename _CVMu, typename _CVArg, typename _Tuple>
    00651       struct result<_CVMu(_CVArg, _Tuple)>
    00652       {
    00653         typedef typename add_reference<_CVArg>::type type;
    00654       };
    00655
    00656       // Pick up the cv-qualifiers of the argument
    00657       template<typename _CVArg, typename _Tuple>
    00658       _CVArg& operator()(_CVArg& __arg, const _Tuple&) const volatile
    00659       { return __arg; }
    00660     };
    00661
    00662   /**
    00663    *  @if maint
    00664    *  Maps member pointers into instances of _Mem_fn but leaves all
    00665    *  other function objects untouched. Used by tr1::bind(). The
    00666    *  primary template handles the non--member-pointer case.
    00667    *  @endif
    00668    */
    00669   template<typename _Tp>
    00670     struct _Maybe_wrap_member_pointer
    00671     {
    00672       typedef _Tp type;
    00673       static const _Tp& __do_wrap(const _Tp& __x) { return __x; }
    00674     };
    00675
    00676   /**
    00677    *  @if maint
    00678    *  Maps member pointers into instances of _Mem_fn but leaves all
    00679    *  other function objects untouched. Used by tr1::bind(). This
    00680    *  partial specialization handles the member pointer case.
    00681    *  @endif
    00682    */
    00683   template<typename _Tp, typename _Class>
    00684     struct _Maybe_wrap_member_pointer<_Tp _Class::*>
    00685     {
    00686       typedef _Mem_fn<_Tp _Class::*> type;
    00687       static type __do_wrap(_Tp _Class::* __pm) { return type(__pm); }
    00688     };
    00689
    00690   /**
    00691    *  @if maint
    00692    *  Type of the function object returned from bind().
    00693    *  @endif
    00694    */
    00695    template<typename _Signature>
    00696      struct _Bind;
    00697
    00698   /**
    00699    *  @if maint
    00700    *  Type of the function object returned from bind<R>().
    00701    *  @endif
    00702    */
    00703    template<typename _Result, typename _Signature>
    00704      struct _Bind_result;
    00705
    00706   /**
    00707    *  @if maint
    00708    *  Class template _Bind is always a bind expression.
    00709    *  @endif
    00710    */
    00711    template<typename _Signature>
    00712     struct is_bind_expression<_Bind<_Signature> >
    00713     {
    00714       static const bool value = true;
    00715     };
    00716
    00717   /**
    00718    *  @if maint
    00719    *  Class template _Bind_result is always a bind expression.
    00720    *  @endif
    00721    */
    00722    template<typename _Result, typename _Signature>
    00723    struct is_bind_expression<_Bind_result<_Result, _Signature> >
    00724     {
    00725       static const bool value = true;
    00726     };
    00727
    00728   /**
    00729    *  @brief Exception class thrown when class template function's
    00730    *  operator() is called with an empty target.
    00731    *
    00732    */
    00733   class bad_function_call : public std::exception { };
    00734
    00735   /**
    00736    *  @if maint
    00737    *  The integral constant expression 0 can be converted into a
    00738    *  pointer to this type. It is used by the function template to
    00739    *  accept NULL pointers.
    00740    *  @endif
    00741    */
    00742   struct _M_clear_type;
    00743
    00744   /**
    00745    *  @if maint
    00746    *  Trait identifying "location-invariant" types, meaning that the
    00747    *  address of the object (or any of its members) will not escape.
    00748    *  Also implies a trivial copy constructor and assignment operator.
    00749    *   @endif
    00750    */
    00751   template<typename _Tp>
    00752     struct __is_location_invariant
    00753     : integral_constant<bool,
    00754                         (is_pointer<_Tp>::value
    00755                          || is_member_pointer<_Tp>::value)>
    00756     {
    00757     };
    00758
    00759   class _Undefined_class;
    00760
    00761   union _Nocopy_types
    00762   {
    00763     void*       _M_object;
    00764     const void* _M_const_object;
    00765     void (*_M_function_pointer)();
    00766     void (_Undefined_class::*_M_member_pointer)();
    00767   };
    00768
    00769   union _Any_data {
    00770     void*       _M_access()       { return &_M_pod_data[0]; }
    00771     const void* _M_access() const { return &_M_pod_data[0]; }
    00772
    00773     template<typename _Tp> _Tp& _M_access()
    00774     { return *static_cast<_Tp*>(_M_access()); }
    00775
    00776     template<typename _Tp> const _Tp& _M_access() const
    00777     { return *static_cast<const _Tp*>(_M_access()); }
    00778
    00779     _Nocopy_types _M_unused;
    00780     char _M_pod_data[sizeof(_Nocopy_types)];
    00781   };
    00782
    00783   enum _Manager_operation
    00784   {
    00785     __get_type_info,
    00786     __get_functor_ptr,
    00787     __clone_functor,
    00788     __destroy_functor
    00789   };
    00790
    00791   /* Simple type wrapper that helps avoid annoying const problems
    00792      when casting between void pointers and pointers-to-pointers. */
    00793   template<typename _Tp>
    00794     struct _Simple_type_wrapper
    00795     {
    00796       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
    00797
    00798       _Tp __value;
    00799     };
    00800
    00801   template<typename _Tp>
    00802     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
    00803       : __is_location_invariant<_Tp>
    00804     {
    00805     };
    00806
    00807   // Converts a reference to a function object into a callable
    00808   // function object.
    00809   template<typename _Functor>
    00810     inline _Functor& __callable_functor(_Functor& __f) { return __f; }
    00811
    00812   template<typename _Member, typename _Class>
    00813     inline _Mem_fn<_Member _Class::*>
    00814     __callable_functor(_Member _Class::* &__p)
    00815     { return mem_fn(__p); }
    00816
    00817   template<typename _Member, typename _Class>
    00818     inline _Mem_fn<_Member _Class::*>
    00819     __callable_functor(_Member _Class::* const &__p)
    00820     { return mem_fn(__p); }
    00821
    00822   template<typename _Signature, typename _Functor>
    00823     class _Function_handler;
    00824
    00825   template<typename _Signature>
    00826     class function;
    00827
    00828
    00829   /**
    00830    *  @if maint
    00831    *  Base class of all polymorphic function object wrappers.
    00832    *  @endif
    00833    */
    00834   class _Function_base
    00835   {
    00836   public:
    00837     static const std::size_t _M_max_size = sizeof(_Nocopy_types);
    00838     static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
    00839
    00840     template<typename _Functor>
    00841     class _Base_manager
    00842     {
    00843     protected:
    00844       static const bool __stored_locally =
    00845         (__is_location_invariant<_Functor>::value
    00846          && sizeof(_Functor) <= _M_max_size
    00847          && __alignof__(_Functor) <= _M_max_align
    00848          && (_M_max_align % __alignof__(_Functor) == 0));
    00849       typedef integral_constant<bool, __stored_locally> _Local_storage;
    00850
    00851       // Retrieve a pointer to the function object
    00852       static _Functor* _M_get_pointer(const _Any_data& __source)
    00853       {
    00854         const _Functor* __ptr =
    00855           __stored_locally? &__source._M_access<_Functor>()
    00856           /* have stored a pointer */ : __source._M_access<_Functor*>();
    00857         return const_cast<_Functor*>(__ptr);
    00858       }
    00859
    00860       // Clone a location-invariant function object that fits within
    00861       // an _Any_data structure.
    00862       static void
    00863       _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
    00864       {
    00865         new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
    00866       }
    00867
    00868       // Clone a function object that is not location-invariant or
    00869       // that cannot fit into an _Any_data structure.
    00870       static void
    00871       _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
    00872       {
    00873         __dest._M_access<_Functor*>() =
    00874           new _Functor(*__source._M_access<_Functor*>());
    00875       }
    00876
    00877       // Destroying a location-invariant object may still require
    00878       // destruction.
    00879       static void
    00880       _M_destroy(_Any_data& __victim, true_type)
    00881       {
    00882         __victim._M_access<_Functor>().~_Functor();
    00883       }
    00884
    00885       // Destroying an object located on the heap.
    00886       static void
    00887       _M_destroy(_Any_data& __victim, false_type)
    00888       {
    00889         delete __victim._M_access<_Functor*>();
    00890       }
    00891
    00892     public:
    00893       static bool
    00894       _M_manager(_Any_data& __dest, const _Any_data& __source,
    00895                  _Manager_operation __op)
    00896       {
    00897         switch (__op) {
    00898         case __get_type_info:
    00899           __dest._M_access<const type_info*>() = &typeid(_Functor);
    00900           break;
    00901
    00902         case __get_functor_ptr:
    00903           __dest._M_access<_Functor*>() = _M_get_pointer(__source);
    00904           break;
    00905
    00906         case __clone_functor:
    00907           _M_clone(__dest, __source, _Local_storage());
    00908           break;
    00909
    00910         case __destroy_functor:
    00911           _M_destroy(__dest, _Local_storage());
    00912           break;
    00913         }
    00914         return false;
    00915       }
    00916
    00917       static void
    00918       _M_init_functor(_Any_data& __functor, const _Functor& __f)
    00919       {
    00920         _M_init_functor(__functor, __f, _Local_storage());
    00921       }
    00922
    00923       template<typename _Signature>
    00924       static bool
    00925       _M_not_empty_function(const function<_Signature>& __f)
    00926       {
    00927         return __f;
    00928       }
    00929
    00930       template<typename _Tp>
    00931       static bool
    00932       _M_not_empty_function(const _Tp*& __fp)
    00933       {
    00934         return __fp;
    00935       }
    00936
    00937       template<typename _Class, typename _Tp>
    00938       static bool
    00939       _M_not_empty_function(_Tp _Class::* const& __mp)
    00940       {
    00941         return __mp;
    00942       }
    00943
    00944       template<typename _Tp>
    00945       static bool
    00946       _M_not_empty_function(const _Tp&)
    00947       {
    00948         return true;
    00949       }
    00950
    00951     private:
    00952       static void
    00953       _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
    00954       {
    00955         new (__functor._M_access()) _Functor(__f);
    00956       }
    00957
    00958       static void
    00959       _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
    00960       {
    00961         __functor._M_access<_Functor*>() = new _Functor(__f);
    00962       }
    00963     };
    00964
    00965     template<typename _Functor>
    00966     class _Ref_manager : public _Base_manager<_Functor*>
    00967     {
    00968       typedef _Function_base::_Base_manager<_Functor*> _Base;
    00969
    00970     public:
    00971       static bool
    00972       _M_manager(_Any_data& __dest, const _Any_data& __source,
    00973                  _Manager_operation __op)
    00974       {
    00975         switch (__op) {
    00976         case __get_type_info:
    00977           __dest._M_access<const type_info*>() = &typeid(_Functor);
    00978           break;
    00979
    00980         case __get_functor_ptr:
    00981           __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
    00982           return is_const<_Functor>::value;
    00983           break;
    00984
    00985         default:
    00986           _Base::_M_manager(__dest, __source, __op);
    00987         }
    00988         return false;
    00989       }
    00990
    00991       static void
    00992       _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
    00993       {
    00994         // TBD: Use address_of function instead
    00995         _Base::_M_init_functor(__functor, &__f.get());
    00996       }
    00997     };
    00998
    00999     _Function_base() : _M_manager(0) { }
    01000
    01001     ~_Function_base()
    01002     {
    01003       if (_M_manager)
    01004         {
    01005           _M_manager(_M_functor, _M_functor, __destroy_functor);
    01006         }
    01007     }
    01008
    01009
    01010     bool _M_empty() const { return !_M_manager; }
    01011
    01012     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
    01013                                   _Manager_operation);
    01014
    01015     _Any_data     _M_functor;
    01016     _Manager_type _M_manager;
    01017   };
    01018
    01019   // [3.7.2.7] null pointer comparisons
    01020
    01021   /**
    01022    *  @brief Compares a polymorphic function object wrapper against 0
    01023    *  (the NULL pointer).
    01024    *  @returns @c true if the wrapper has no target, @c false otherwise
    01025    *
    01026    *  This function will not throw an exception.
    01027    */
    01028   template<typename _Signature>
    01029     inline bool
    01030     operator==(const function<_Signature>& __f, _M_clear_type*)
    01031     {
    01032       return !__f;
    01033     }
    01034
    01035   /**
    01036    *  @overload
    01037    */
    01038   template<typename _Signature>
    01039     inline bool
    01040     operator==(_M_clear_type*, const function<_Signature>& __f)
    01041     {
    01042       return !__f;
    01043     }
    01044
    01045   /**
    01046    *  @brief Compares a polymorphic function object wrapper against 0
    01047    *  (the NULL pointer).
    01048    *  @returns @c false if the wrapper has no target, @c true otherwise
    01049    *
    01050    *  This function will not throw an exception.
    01051    */
    01052   template<typename _Signature>
    01053     inline bool
    01054     operator!=(const function<_Signature>& __f, _M_clear_type*)
    01055     {
    01056       return __f;
    01057     }
    01058
    01059   /**
    01060    *  @overload
    01061    */
    01062   template<typename _Signature>
    01063     inline bool
    01064     operator!=(_M_clear_type*, const function<_Signature>& __f)
    01065     {
    01066       return __f;
    01067     }
    01068
    01069   // [3.7.2.8] specialized algorithms
    01070
    01071   /**
    01072    *  @brief Swap the targets of two polymorphic function object wrappers.
    01073    *
    01074    *  This function will not throw an exception.
    01075    */
    01076   template<typename _Signature>
    01077     inline void
    01078     swap(function<_Signature>& __x, function<_Signature>& __y)
    01079     {
    01080       __x.swap(__y);
    01081     }
    01082
    01083 #define _GLIBCXX_JOIN(X,Y) _GLIBCXX_JOIN2( X , Y )
    01084 #define _GLIBCXX_JOIN2(X,Y) _GLIBCXX_JOIN3(X,Y)
    01085 #define _GLIBCXX_JOIN3(X,Y) X##Y
    01086 #define _GLIBCXX_REPEAT_HEADER <tr1/functional_iterate.h>
    01087 #include <tr1/repeat.h>
    01088 #undef _GLIBCXX_REPEAT_HEADER
    01089 #undef _GLIBCXX_JOIN3
    01090 #undef _GLIBCXX_JOIN2
    01091 #undef _GLIBCXX_JOIN
    01092
    01093 // Definition of default hash function std::tr1::hash<>.  The types for
    01094 // which std::tr1::hash<T> is defined is in clause 6.3.3. of the PDTR.
    01095
    01096   template <typename T> struct hash;
    01097
    01098   #define tr1_hashtable_define_trivial_hash(T)                              \
    01099     template <> struct hash<T> {                                                    \
    01100       std::size_t operator()(T val) const { return static_cast<std::size_t>(val); } \
    01101     }                                                                       \
    01102
    01103   tr1_hashtable_define_trivial_hash(bool);
    01104   tr1_hashtable_define_trivial_hash(char);
    01105   tr1_hashtable_define_trivial_hash(signed char);
    01106   tr1_hashtable_define_trivial_hash(unsigned char);
    01107   tr1_hashtable_define_trivial_hash(wchar_t);
    01108   tr1_hashtable_define_trivial_hash(short);
    01109   tr1_hashtable_define_trivial_hash(int);
    01110   tr1_hashtable_define_trivial_hash(long);
    01111   tr1_hashtable_define_trivial_hash(unsigned short);
    01112   tr1_hashtable_define_trivial_hash(unsigned int);
    01113   tr1_hashtable_define_trivial_hash(unsigned long);
    01114
    01115   tr1_hashtable_define_trivial_hash(float);
    01116   tr1_hashtable_define_trivial_hash(double);
    01117   tr1_hashtable_define_trivial_hash(long double);
    01118
    01119   #undef tr1_hashtable_define_trivial_hash
    01120
    01121   template <typename T>
    01122     struct hash<T*> {
    01123       std::size_t operator()(T* p) const {
    01124         return reinterpret_cast<std::size_t>(p);
    01125       }
    01126     };
    01127
    01128   // ??? We can probably find a better hash function than this (i.e. one
    01129   // that vectorizes better and that produces a more uniform distribution).
    01130
    01131   // XXX String hash probably shouldn't be an inline member function,
    01132   // since it's nontrivial.  Once we have the framework for TR1 .cc
    01133   // files, this should go in one.
    01134
    01135   template <>
    01136     struct hash<std::string>
    01137     {
    01138       std::size_t operator()(const std::string& s) const
    01139       {
    01140         std::size_t result = 0;
    01141         for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
    01142           result = (result * 131) + *i;
    01143         return result;
    01144       }
    01145     };
    01146
    01147 #ifdef _GLIBCXX_USE_WCHAR_T
    01148   template <>
    01149     struct hash<std::wstring>
    01150     {
    01151       std::size_t operator()(const std::wstring& s) const
    01152       {
    01153         std::size_t result = 0;
    01154         for (std::wstring::const_iterator i = s.begin(); i != s.end(); ++i)
    01155           result = (result * 131) + *i;
    01156         return result;
    01157       }
    01158     };
    01159 #endif
    01160
    01161 }
    01162 }
    01163
    01164 #endif