tr1/functional

Go to the documentation of this file.
00001 // TR1 functional header -*- C++ -*-
00002 
00003 // Copyright (C) 2004, 2005, 2006, 2007 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 
00030 /** @file tr1/functional
00031  *  This is a TR1 C++ Library header.
00032  */
00033 
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 <ext/type_traits.h>
00043 #include <cstdlib>  // for std::abort
00044 #include <tr1/tuple>
00045 
00046 namespace std
00047 {
00048 _GLIBCXX_BEGIN_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     inline 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     inline reference_wrapper<const _Tp>
00382     cref(const _Tp& __t)
00383     { return reference_wrapper<const _Tp>(__t); }
00384 
00385   template<typename _Tp>
00386     inline reference_wrapper<_Tp>
00387     ref(reference_wrapper<_Tp> __t)
00388     { return ref(__t.get()); }
00389 
00390   template<typename _Tp>
00391     inline reference_wrapper<const _Tp>
00392     cref(reference_wrapper<_Tp> __t)
00393     { return cref(__t.get()); }
00394 
00395    template<typename _Tp, bool>
00396      struct _Mem_fn_const_or_non
00397      {
00398        typedef const _Tp& type;
00399      };
00400 
00401     template<typename _Tp>
00402       struct _Mem_fn_const_or_non<_Tp, false>
00403       {
00404         typedef _Tp& type;
00405       };
00406 
00407   template<typename _Res, typename _Class>
00408   class _Mem_fn<_Res _Class::*>
00409   {
00410     // This bit of genius is due to Peter Dimov, improved slightly by
00411     // Douglas Gregor.
00412     template<typename _Tp>
00413       _Res&
00414       _M_call(_Tp& __object, _Class *) const
00415       { return __object.*__pm; }
00416 
00417     template<typename _Tp, typename _Up>
00418       _Res&
00419       _M_call(_Tp& __object, _Up * const *) const
00420       { return (*__object).*__pm; }
00421 
00422     template<typename _Tp, typename _Up>
00423       const _Res&
00424       _M_call(_Tp& __object, const _Up * const *) const
00425       { return (*__object).*__pm; }
00426 
00427     template<typename _Tp>
00428       const _Res&
00429       _M_call(_Tp& __object, const _Class *) const
00430       { return __object.*__pm; }
00431 
00432     template<typename _Tp>
00433       const _Res&
00434       _M_call(_Tp& __ptr, const volatile void*) const
00435       { return (*__ptr).*__pm; }
00436 
00437     template<typename _Tp> static _Tp& __get_ref();
00438 
00439     template<typename _Tp>
00440       static __sfinae_types::__one __check_const(_Tp&, _Class*);
00441     template<typename _Tp, typename _Up>
00442       static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
00443     template<typename _Tp, typename _Up>
00444       static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
00445     template<typename _Tp>
00446       static __sfinae_types::__two __check_const(_Tp&, const _Class*);
00447     template<typename _Tp>
00448       static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
00449 
00450   public:
00451     template<typename _Tp>
00452       struct _Result_type
00453       : _Mem_fn_const_or_non<
00454         _Res,
00455         (sizeof(__sfinae_types::__two)
00456      == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
00457       { };
00458 
00459     template<typename _Signature>
00460       struct result;
00461 
00462     template<typename _CVMem, typename _Tp>
00463       struct result<_CVMem(_Tp)>
00464       : public _Result_type<_Tp> { };
00465 
00466     template<typename _CVMem, typename _Tp>
00467       struct result<_CVMem(_Tp&)>
00468       : public _Result_type<_Tp> { };
00469 
00470     explicit _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
00471 
00472     // Handle objects
00473     _Res&       operator()(_Class& __object)       const
00474     { return __object.*__pm; }
00475 
00476     const _Res& operator()(const _Class& __object) const
00477     { return __object.*__pm; }
00478 
00479     // Handle pointers
00480     _Res&       operator()(_Class* __object)       const
00481     { return __object->*__pm; }
00482 
00483     const _Res&
00484     operator()(const _Class* __object) const
00485     { return __object->*__pm; }
00486 
00487     // Handle smart pointers and derived
00488     template<typename _Tp>
00489       typename _Result_type<_Tp>::type
00490       operator()(_Tp& __unknown) const
00491       { return _M_call(__unknown, &__unknown); }
00492 
00493   private:
00494     _Res _Class::*__pm;
00495   };
00496 
00497   /**
00498    *  @brief Returns a function object that forwards to the member
00499    *  pointer @a pm.
00500    */
00501   template<typename _Tp, typename _Class>
00502     inline _Mem_fn<_Tp _Class::*>
00503     mem_fn(_Tp _Class::* __pm)
00504     {
00505       return _Mem_fn<_Tp _Class::*>(__pm);
00506     }
00507 
00508   /**
00509    *  @brief Determines if the given type _Tp is a function object
00510    *  should be treated as a subexpression when evaluating calls to
00511    *  function objects returned by bind(). [TR1 3.6.1]
00512    */
00513   template<typename _Tp>
00514     struct is_bind_expression
00515     { static const bool value = false; };
00516 
00517   template<typename _Tp>
00518     const bool is_bind_expression<_Tp>::value;
00519 
00520   /**
00521    *  @brief Determines if the given type _Tp is a placeholder in a
00522    *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
00523    */
00524   template<typename _Tp>
00525     struct is_placeholder
00526     { static const int value = 0; };
00527 
00528   template<typename _Tp>
00529     const int is_placeholder<_Tp>::value;
00530 
00531   /**
00532    *  @if maint
00533    *  The type of placeholder objects defined by libstdc++.
00534    *  @endif
00535    */
00536   template<int _Num> struct _Placeholder { };
00537 
00538   /**
00539    *  @if maint
00540    *  Partial specialization of is_placeholder that provides the placeholder
00541    *  number for the placeholder objects defined by libstdc++.
00542    *  @endif
00543    */
00544   template<int _Num>
00545     struct is_placeholder<_Placeholder<_Num> >
00546     { static const int value = _Num; };
00547 
00548   template<int _Num>
00549     const int is_placeholder<_Placeholder<_Num> >::value;
00550 
00551   /**
00552    *  @if maint
00553    *  Maps an argument to bind() into an actual argument to the bound
00554    *  function object [TR1 3.6.3/5]. Only the first parameter should
00555    *  be specified: the rest are used to determine among the various
00556    *  implementations. Note that, although this class is a function
00557    *  object, isn't not entirely normal because it takes only two
00558    *  parameters regardless of the number of parameters passed to the
00559    *  bind expression. The first parameter is the bound argument and
00560    *  the second parameter is a tuple containing references to the
00561    *  rest of the arguments.
00562    *  @endif
00563    */
00564   template<typename _Arg,
00565            bool _IsBindExp = is_bind_expression<_Arg>::value,
00566            bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
00567     class _Mu;
00568 
00569   /**
00570    *  @if maint
00571    *  If the argument is reference_wrapper<_Tp>, returns the
00572    *  underlying reference. [TR1 3.6.3/5 bullet 1]
00573    *  @endif
00574    */
00575   template<typename _Tp>
00576     class _Mu<reference_wrapper<_Tp>, false, false>
00577     {
00578     public:
00579       typedef _Tp& result_type;
00580 
00581       /* Note: This won't actually work for const volatile
00582        * reference_wrappers, because reference_wrapper::get() is const
00583        * but not volatile-qualified. This might be a defect in the TR.
00584        */
00585       template<typename _CVRef, typename _Tuple>
00586       result_type
00587       operator()(_CVRef& __arg, const _Tuple&) const volatile
00588       { return __arg.get(); }
00589     };
00590 
00591   /**
00592    *  @if maint
00593    *  If the argument is a bind expression, we invoke the underlying
00594    *  function object with the same cv-qualifiers as we are given and
00595    *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
00596    *  @endif
00597    */
00598   template<typename _Arg>
00599     class _Mu<_Arg, true, false>
00600     {
00601     public:
00602       template<typename _Signature> class result;
00603 
00604 #define _GLIBCXX_REPEAT_HEADER <tr1/mu_iterate.h>
00605 #  include <tr1/repeat.h>
00606 #undef _GLIBCXX_REPEAT_HEADER
00607     };
00608 
00609   /**
00610    *  @if maint
00611    *  If the argument is a placeholder for the Nth argument, returns
00612    *  a reference to the Nth argument to the bind function object.
00613    *  [TR1 3.6.3/5 bullet 3]
00614    *  @endif
00615    */
00616   template<typename _Arg>
00617     class _Mu<_Arg, false, true>
00618     {
00619     public:
00620       template<typename _Signature> class result;
00621 
00622       template<typename _CVMu, typename _CVArg, typename _Tuple>
00623       class result<_CVMu(_CVArg, _Tuple)>
00624       {
00625         // Add a reference, if it hasn't already been done for us.
00626         // This allows us to be a little bit sloppy in constructing
00627         // the tuple that we pass to result_of<...>.
00628         typedef typename tuple_element<(is_placeholder<_Arg>::value - 1),
00629                                        _Tuple>::type __base_type;
00630 
00631       public:
00632         typedef typename add_reference<__base_type>::type type;
00633       };
00634 
00635       template<typename _Tuple>
00636       typename result<_Mu(_Arg, _Tuple)>::type
00637       operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
00638       {
00639         return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
00640       }
00641     };
00642 
00643   /**
00644    *  @if maint
00645    *  If the argument is just a value, returns a reference to that
00646    *  value. The cv-qualifiers on the reference are the same as the
00647    *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
00648    *  @endif
00649    */
00650   template<typename _Arg>
00651     class _Mu<_Arg, false, false>
00652     {
00653     public:
00654       template<typename _Signature> struct result;
00655 
00656       template<typename _CVMu, typename _CVArg, typename _Tuple>
00657       struct result<_CVMu(_CVArg, _Tuple)>
00658       {
00659         typedef typename add_reference<_CVArg>::type type;
00660       };
00661 
00662       // Pick up the cv-qualifiers of the argument
00663       template<typename _CVArg, typename _Tuple>
00664       _CVArg& operator()(_CVArg& __arg, const _Tuple&) const volatile
00665       { return __arg; }
00666     };
00667 
00668   /**
00669    *  @if maint
00670    *  Maps member pointers into instances of _Mem_fn but leaves all
00671    *  other function objects untouched. Used by tr1::bind(). The
00672    *  primary template handles the non--member-pointer case.
00673    *  @endif
00674    */
00675   template<typename _Tp>
00676     struct _Maybe_wrap_member_pointer
00677     {
00678       typedef _Tp type;
00679       static const _Tp& __do_wrap(const _Tp& __x) { return __x; }
00680     };
00681 
00682   /**
00683    *  @if maint
00684    *  Maps member pointers into instances of _Mem_fn but leaves all
00685    *  other function objects untouched. Used by tr1::bind(). This
00686    *  partial specialization handles the member pointer case.
00687    *  @endif
00688    */
00689   template<typename _Tp, typename _Class>
00690     struct _Maybe_wrap_member_pointer<_Tp _Class::*>
00691     {
00692       typedef _Mem_fn<_Tp _Class::*> type;
00693       static type __do_wrap(_Tp _Class::* __pm) { return type(__pm); }
00694     };
00695 
00696   /**
00697    *  @if maint
00698    *  Type of the function object returned from bind().
00699    *  @endif
00700    */
00701    template<typename _Signature>
00702      struct _Bind;
00703 
00704   /**
00705    *  @if maint
00706    *  Type of the function object returned from bind<R>().
00707    *  @endif
00708    */
00709    template<typename _Result, typename _Signature>
00710      struct _Bind_result;
00711 
00712   /**
00713    *  @if maint
00714    *  Class template _Bind is always a bind expression.
00715    *  @endif
00716    */
00717    template<typename _Signature>
00718      struct is_bind_expression<_Bind<_Signature> >
00719      { static const bool value = true; };
00720 
00721    template<typename _Signature>
00722      const bool is_bind_expression<_Bind<_Signature> >::value;
00723 
00724   /**
00725    *  @if maint
00726    *  Class template _Bind_result is always a bind expression.
00727    *  @endif
00728    */
00729    template<typename _Result, typename _Signature>
00730      struct is_bind_expression<_Bind_result<_Result, _Signature> >
00731      { static const bool value = true; };
00732 
00733    template<typename _Result, typename _Signature>
00734      const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
00735 
00736   /**
00737    *  @brief Exception class thrown when class template function's
00738    *  operator() is called with an empty target.
00739    *
00740    */
00741   class bad_function_call : public std::exception { };
00742 
00743   /**
00744    *  @if maint
00745    *  The integral constant expression 0 can be converted into a
00746    *  pointer to this type. It is used by the function template to
00747    *  accept NULL pointers.
00748    *  @endif
00749    */
00750   struct _M_clear_type;
00751 
00752   /**
00753    *  @if maint
00754    *  Trait identifying "location-invariant" types, meaning that the
00755    *  address of the object (or any of its members) will not escape.
00756    *  Also implies a trivial copy constructor and assignment operator.
00757    *   @endif
00758    */
00759   template<typename _Tp>
00760     struct __is_location_invariant
00761     : integral_constant<bool,
00762                         (is_pointer<_Tp>::value
00763                          || is_member_pointer<_Tp>::value)>
00764     {
00765     };
00766 
00767   class _Undefined_class;
00768 
00769   union _Nocopy_types
00770   {
00771     void*       _M_object;
00772     const void* _M_const_object;
00773     void (*_M_function_pointer)();
00774     void (_Undefined_class::*_M_member_pointer)();
00775   };
00776 
00777   union _Any_data {
00778     void*       _M_access()       { return &_M_pod_data[0]; }
00779     const void* _M_access() const { return &_M_pod_data[0]; }
00780 
00781     template<typename _Tp> _Tp& _M_access()
00782     { return *static_cast<_Tp*>(_M_access()); }
00783 
00784     template<typename _Tp> const _Tp& _M_access() const
00785     { return *static_cast<const _Tp*>(_M_access()); }
00786 
00787     _Nocopy_types _M_unused;
00788     char _M_pod_data[sizeof(_Nocopy_types)];
00789   };
00790 
00791   enum _Manager_operation
00792   {
00793     __get_type_info,
00794     __get_functor_ptr,
00795     __clone_functor,
00796     __destroy_functor
00797   };
00798 
00799   /* Simple type wrapper that helps avoid annoying const problems
00800      when casting between void pointers and pointers-to-pointers. */
00801   template<typename _Tp>
00802     struct _Simple_type_wrapper
00803     {
00804       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
00805 
00806       _Tp __value;
00807     };
00808 
00809   template<typename _Tp>
00810     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
00811       : __is_location_invariant<_Tp>
00812     {
00813     };
00814 
00815   // Converts a reference to a function object into a callable
00816   // function object.
00817   template<typename _Functor>
00818     inline _Functor& __callable_functor(_Functor& __f) { return __f; }
00819 
00820   template<typename _Member, typename _Class>
00821     inline _Mem_fn<_Member _Class::*>
00822     __callable_functor(_Member _Class::* &__p)
00823     { return mem_fn(__p); }
00824 
00825   template<typename _Member, typename _Class>
00826     inline _Mem_fn<_Member _Class::*>
00827     __callable_functor(_Member _Class::* const &__p)
00828     { return mem_fn(__p); }
00829 
00830   template<typename _Signature, typename _Functor>
00831     class _Function_handler;
00832 
00833   template<typename _Signature>
00834     class function;
00835 
00836 
00837   /**
00838    *  @if maint
00839    *  Base class of all polymorphic function object wrappers.
00840    *  @endif
00841    */
00842   class _Function_base
00843   {
00844   public:
00845     static const std::size_t _M_max_size = sizeof(_Nocopy_types);
00846     static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
00847 
00848     template<typename _Functor>
00849     class _Base_manager
00850     {
00851     protected:
00852       static const bool __stored_locally =
00853         (__is_location_invariant<_Functor>::value
00854          && sizeof(_Functor) <= _M_max_size
00855          && __alignof__(_Functor) <= _M_max_align
00856          && (_M_max_align % __alignof__(_Functor) == 0));
00857       typedef integral_constant<bool, __stored_locally> _Local_storage;
00858 
00859       // Retrieve a pointer to the function object
00860       static _Functor* _M_get_pointer(const _Any_data& __source)
00861       {
00862         const _Functor* __ptr =
00863           __stored_locally? &__source._M_access<_Functor>()
00864           /* have stored a pointer */ : __source._M_access<_Functor*>();
00865         return const_cast<_Functor*>(__ptr);
00866       }
00867 
00868       // Clone a location-invariant function object that fits within
00869       // an _Any_data structure.
00870       static void
00871       _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
00872       {
00873         new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
00874       }
00875 
00876       // Clone a function object that is not location-invariant or
00877       // that cannot fit into an _Any_data structure.
00878       static void
00879       _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
00880       {
00881         __dest._M_access<_Functor*>() =
00882           new _Functor(*__source._M_access<_Functor*>());
00883       }
00884 
00885       // Destroying a location-invariant object may still require
00886       // destruction.
00887       static void
00888       _M_destroy(_Any_data& __victim, true_type)
00889       {
00890         __victim._M_access<_Functor>().~_Functor();
00891       }
00892 
00893       // Destroying an object located on the heap.
00894       static void
00895       _M_destroy(_Any_data& __victim, false_type)
00896       {
00897         delete __victim._M_access<_Functor*>();
00898       }
00899 
00900     public:
00901       static bool
00902       _M_manager(_Any_data& __dest, const _Any_data& __source,
00903                  _Manager_operation __op)
00904       {
00905         switch (__op) {
00906         case __get_type_info:
00907           __dest._M_access<const type_info*>() = &typeid(_Functor);
00908           break;
00909 
00910         case __get_functor_ptr:
00911           __dest._M_access<_Functor*>() = _M_get_pointer(__source);
00912           break;
00913 
00914         case __clone_functor:
00915           _M_clone(__dest, __source, _Local_storage());
00916           break;
00917 
00918         case __destroy_functor:
00919           _M_destroy(__dest, _Local_storage());
00920           break;
00921         }
00922         return false;
00923       }
00924 
00925       static void
00926       _M_init_functor(_Any_data& __functor, const _Functor& __f)
00927       {
00928         _M_init_functor(__functor, __f, _Local_storage());
00929       }
00930 
00931       template<typename _Signature>
00932       static bool
00933       _M_not_empty_function(const function<_Signature>& __f)
00934       {
00935         return __f;
00936       }
00937 
00938       template<typename _Tp>
00939       static bool
00940       _M_not_empty_function(const _Tp*& __fp)
00941       {
00942         return __fp;
00943       }
00944 
00945       template<typename _Class, typename _Tp>
00946       static bool
00947       _M_not_empty_function(_Tp _Class::* const& __mp)
00948       {
00949         return __mp;
00950       }
00951 
00952       template<typename _Tp>
00953       static bool
00954       _M_not_empty_function(const _Tp&)
00955       {
00956         return true;
00957       }
00958 
00959     private:
00960       static void
00961       _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
00962       {
00963         new (__functor._M_access()) _Functor(__f);
00964       }
00965 
00966       static void
00967       _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
00968       {
00969         __functor._M_access<_Functor*>() = new _Functor(__f);
00970       }
00971     };
00972 
00973     template<typename _Functor>
00974     class _Ref_manager : public _Base_manager<_Functor*>
00975     {
00976       typedef _Function_base::_Base_manager<_Functor*> _Base;
00977 
00978     public:
00979       static bool
00980       _M_manager(_Any_data& __dest, const _Any_data& __source,
00981                  _Manager_operation __op)
00982       {
00983         switch (__op) {
00984         case __get_type_info:
00985           __dest._M_access<const type_info*>() = &typeid(_Functor);
00986           break;
00987 
00988         case __get_functor_ptr:
00989           __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
00990           return is_const<_Functor>::value;
00991           break;
00992 
00993         default:
00994           _Base::_M_manager(__dest, __source, __op);
00995         }
00996         return false;
00997       }
00998 
00999       static void
01000       _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
01001       {
01002         // TBD: Use address_of function instead
01003         _Base::_M_init_functor(__functor, &__f.get());
01004       }
01005     };
01006 
01007     _Function_base() : _M_manager(0) { }
01008 
01009     ~_Function_base()
01010     {
01011       if (_M_manager)
01012         {
01013           _M_manager(_M_functor, _M_functor, __destroy_functor);
01014         }
01015     }
01016 
01017 
01018     bool _M_empty() const { return !_M_manager; }
01019 
01020     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
01021                                   _Manager_operation);
01022 
01023     _Any_data     _M_functor;
01024     _Manager_type _M_manager;
01025   };
01026 
01027   // [3.7.2.7] null pointer comparisons
01028 
01029   /**
01030    *  @brief Compares a polymorphic function object wrapper against 0
01031    *  (the NULL pointer).
01032    *  @returns @c true if the wrapper has no target, @c false otherwise
01033    *
01034    *  This function will not throw an exception.
01035    */
01036   template<typename _Signature>
01037     inline bool
01038     operator==(const function<_Signature>& __f, _M_clear_type*)
01039     {
01040       return !__f;
01041     }
01042 
01043   /**
01044    *  @overload
01045    */
01046   template<typename _Signature>
01047     inline bool
01048     operator==(_M_clear_type*, const function<_Signature>& __f)
01049     {
01050       return !__f;
01051     }
01052 
01053   /**
01054    *  @brief Compares a polymorphic function object wrapper against 0
01055    *  (the NULL pointer).
01056    *  @returns @c false if the wrapper has no target, @c true otherwise
01057    *
01058    *  This function will not throw an exception.
01059    */
01060   template<typename _Signature>
01061     inline bool
01062     operator!=(const function<_Signature>& __f, _M_clear_type*)
01063     {
01064       return __f;
01065     }
01066 
01067   /**
01068    *  @overload
01069    */
01070   template<typename _Signature>
01071     inline bool
01072     operator!=(_M_clear_type*, const function<_Signature>& __f)
01073     {
01074       return __f;
01075     }
01076 
01077   // [3.7.2.8] specialized algorithms
01078 
01079   /**
01080    *  @brief Swap the targets of two polymorphic function object wrappers.
01081    *
01082    *  This function will not throw an exception.
01083    */
01084   template<typename _Signature>
01085     inline void
01086     swap(function<_Signature>& __x, function<_Signature>& __y)
01087     {
01088       __x.swap(__y);
01089     }
01090 
01091 _GLIBCXX_END_NAMESPACE
01092 }
01093 
01094 #define _GLIBCXX_JOIN(X,Y) _GLIBCXX_JOIN2( X , Y )
01095 #define _GLIBCXX_JOIN2(X,Y) _GLIBCXX_JOIN3(X,Y)
01096 #define _GLIBCXX_JOIN3(X,Y) X##Y
01097 #define _GLIBCXX_REPEAT_HEADER <tr1/functional_iterate.h>
01098 #include <tr1/repeat.h>
01099 #undef _GLIBCXX_REPEAT_HEADER
01100 #undef _GLIBCXX_JOIN3
01101 #undef _GLIBCXX_JOIN2
01102 #undef _GLIBCXX_JOIN
01103 
01104 #include <tr1/functional_hash.h>
01105 
01106 #endif

Generated on Thu Nov 1 13:11:30 2007 for libstdc++ by  doxygen 1.5.1