functional

Go to the documentation of this file.
00001 // <functional> -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /*
00027  * Copyright (c) 1997
00028  * Silicon Graphics Computer Systems, Inc.
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Silicon Graphics makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  *
00038  */
00039 
00040 /** @file include/functional
00041  *  This is a Standard C++ Library header.
00042  */
00043 
00044 #ifndef _GLIBCXX_FUNCTIONAL
00045 #define _GLIBCXX_FUNCTIONAL 1
00046 
00047 #pragma GCC system_header
00048 
00049 #include <bits/c++config.h>
00050 #include <bits/stl_function.h>
00051 
00052 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00053 
00054 #include <typeinfo>
00055 #include <new>
00056 #include <tuple>
00057 #include <type_traits>
00058 #include <bits/functional_hash.h>
00059 #include <ext/type_traits.h>
00060 
00061 namespace std
00062 {
00063   template<typename _MemberPointer>
00064     class _Mem_fn;
00065 
00066   /**
00067    *  Actual implementation of _Has_result_type, which uses SFINAE to
00068    *  determine if the type _Tp has a publicly-accessible member type
00069    *  result_type.
00070   */
00071   template<typename _Tp>
00072     class _Has_result_type_helper : __sfinae_types
00073     {
00074       template<typename _Up>
00075         struct _Wrap_type
00076     { };
00077 
00078       template<typename _Up>
00079         static __one __test(_Wrap_type<typename _Up::result_type>*);
00080 
00081       template<typename _Up>
00082         static __two __test(...);
00083 
00084     public:
00085       static const bool value = sizeof(__test<_Tp>(0)) == 1;
00086     };
00087 
00088   template<typename _Tp>
00089     struct _Has_result_type
00090     : integral_constant<bool,
00091           _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
00092     { };
00093 
00094   /**
00095    *  
00096   */
00097   /// If we have found a result_type, extract it.
00098   template<bool _Has_result_type, typename _Functor>
00099     struct _Maybe_get_result_type
00100     { };
00101 
00102   template<typename _Functor>
00103     struct _Maybe_get_result_type<true, _Functor>
00104     {
00105       typedef typename _Functor::result_type result_type;
00106     };
00107 
00108   /**
00109    *  Base class for any function object that has a weak result type, as
00110    *  defined in 3.3/3 of TR1.
00111   */
00112   template<typename _Functor>
00113     struct _Weak_result_type_impl
00114     : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
00115     {
00116     };
00117 
00118   /// Retrieve the result type for a function type.
00119   template<typename _Res, typename... _ArgTypes> 
00120     struct _Weak_result_type_impl<_Res(_ArgTypes...)>
00121     {
00122       typedef _Res result_type;
00123     };
00124 
00125   /// Retrieve the result type for a function reference.
00126   template<typename _Res, typename... _ArgTypes> 
00127     struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
00128     {
00129       typedef _Res result_type;
00130     };
00131 
00132   /// Retrieve the result type for a function pointer.
00133   template<typename _Res, typename... _ArgTypes> 
00134     struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
00135     {
00136       typedef _Res result_type;
00137     };
00138 
00139   /// Retrieve result type for a member function pointer. 
00140   template<typename _Res, typename _Class, typename... _ArgTypes> 
00141     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
00142     {
00143       typedef _Res result_type;
00144     };
00145 
00146   /// Retrieve result type for a const member function pointer. 
00147   template<typename _Res, typename _Class, typename... _ArgTypes> 
00148     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
00149     {
00150       typedef _Res result_type;
00151     };
00152 
00153   /// Retrieve result type for a volatile member function pointer. 
00154   template<typename _Res, typename _Class, typename... _ArgTypes> 
00155     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
00156     {
00157       typedef _Res result_type;
00158     };
00159 
00160   /// Retrieve result type for a const volatile member function pointer. 
00161   template<typename _Res, typename _Class, typename... _ArgTypes> 
00162     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
00163     {
00164       typedef _Res result_type;
00165     };
00166 
00167   /**
00168    *  Strip top-level cv-qualifiers from the function object and let
00169    *  _Weak_result_type_impl perform the real work.
00170   */
00171   template<typename _Functor>
00172     struct _Weak_result_type
00173     : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
00174     {
00175     };
00176 
00177   template<typename _Signature>
00178     class result_of;
00179 
00180   /**
00181    *  Actual implementation of result_of. When _Has_result_type is
00182    *  true, gets its result from _Weak_result_type. Otherwise, uses
00183    *  the function object's member template result to extract the
00184    *  result type.
00185   */
00186   template<bool _Has_result_type, typename _Signature>
00187     struct _Result_of_impl;
00188 
00189   // Handle member data pointers using _Mem_fn's logic
00190   template<typename _Res, typename _Class, typename _T1>
00191     struct _Result_of_impl<false, _Res _Class::*(_T1)>
00192     {
00193       typedef typename _Mem_fn<_Res _Class::*>
00194                 ::template _Result_type<_T1>::type type;
00195     };
00196 
00197   /**
00198    * Determine whether we can determine a result type from @c Functor 
00199    * alone.
00200    */ 
00201   template<typename _Functor, typename... _ArgTypes>
00202     class result_of<_Functor(_ArgTypes...)>
00203     : public _Result_of_impl<
00204                _Has_result_type<_Weak_result_type<_Functor> >::value,
00205                _Functor(_ArgTypes...)>
00206     {
00207     };
00208 
00209   /// We already know the result type for @c Functor; use it.
00210   template<typename _Functor, typename... _ArgTypes>
00211     struct _Result_of_impl<true, _Functor(_ArgTypes...)>
00212     {
00213       typedef typename _Weak_result_type<_Functor>::result_type type;
00214     };
00215 
00216   /**
00217    * We need to compute the result type for this invocation the hard 
00218    * way.
00219    */
00220   template<typename _Functor, typename... _ArgTypes>
00221     struct _Result_of_impl<false, _Functor(_ArgTypes...)>
00222     {
00223       typedef typename _Functor
00224                 ::template result<_Functor(_ArgTypes...)>::type type;
00225     };
00226 
00227   /**
00228    * It is unsafe to access ::result when there are zero arguments, so we 
00229    * return @c void instead.
00230    */
00231   template<typename _Functor>
00232     struct _Result_of_impl<false, _Functor()>
00233     {
00234       typedef void type;
00235     };
00236 
00237   /// Determines if the type _Tp derives from unary_function.
00238   template<typename _Tp>
00239     struct _Derives_from_unary_function : __sfinae_types
00240     {
00241     private:
00242       template<typename _T1, typename _Res>
00243         static __one __test(const volatile unary_function<_T1, _Res>*);
00244 
00245       // It's tempting to change "..." to const volatile void*, but
00246       // that fails when _Tp is a function type.
00247       static __two __test(...);
00248 
00249     public:
00250       static const bool value = sizeof(__test((_Tp*)0)) == 1;
00251     };
00252 
00253   /// Determines if the type _Tp derives from binary_function.
00254   template<typename _Tp>
00255     struct _Derives_from_binary_function : __sfinae_types
00256     {
00257     private:
00258       template<typename _T1, typename _T2, typename _Res>
00259         static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
00260 
00261       // It's tempting to change "..." to const volatile void*, but
00262       // that fails when _Tp is a function type.
00263       static __two __test(...);
00264 
00265     public:
00266       static const bool value = sizeof(__test((_Tp*)0)) == 1;
00267     };
00268 
00269   /// Turns a function type into a function pointer type
00270   template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
00271     struct _Function_to_function_pointer
00272     {
00273       typedef _Tp type;
00274     };
00275 
00276   template<typename _Tp>
00277     struct _Function_to_function_pointer<_Tp, true>
00278     {
00279       typedef _Tp* type;
00280     };
00281 
00282   /**
00283    * Invoke a function object, which may be either a member pointer or a
00284    * function object. The first parameter will tell which.
00285    */
00286   template<typename _Functor, typename... _Args>
00287     inline
00288     typename __gnu_cxx::__enable_if<
00289              (!is_member_pointer<_Functor>::value
00290               && !is_function<_Functor>::value
00291               && !is_function<typename remove_pointer<_Functor>::type>::value),
00292              typename result_of<_Functor(_Args...)>::type
00293            >::__type
00294     __invoke(_Functor& __f, _Args&... __args)
00295     {
00296       return __f(__args...);
00297     }
00298 
00299   template<typename _Functor, typename... _Args>
00300     inline
00301     typename __gnu_cxx::__enable_if<
00302              (is_member_pointer<_Functor>::value
00303               && !is_function<_Functor>::value
00304               && !is_function<typename remove_pointer<_Functor>::type>::value),
00305              typename result_of<_Functor(_Args...)>::type
00306            >::__type
00307     __invoke(_Functor& __f, _Args&... __args)
00308     {
00309       return mem_fn(__f)(__args...);
00310     }
00311 
00312   // To pick up function references (that will become function pointers)
00313   template<typename _Functor, typename... _Args>
00314     inline
00315     typename __gnu_cxx::__enable_if<
00316              (is_pointer<_Functor>::value
00317               && is_function<typename remove_pointer<_Functor>::type>::value),
00318              typename result_of<_Functor(_Args...)>::type
00319            >::__type
00320     __invoke(_Functor __f, _Args&... __args)
00321     {
00322       return __f(__args...);
00323     }
00324 
00325   /**
00326    *  Knowing which of unary_function and binary_function _Tp derives
00327    *  from, derives from the same and ensures that reference_wrapper
00328    *  will have a weak result type. See cases below.
00329    */
00330   template<bool _Unary, bool _Binary, typename _Tp>
00331     struct _Reference_wrapper_base_impl;
00332 
00333   // Not a unary_function or binary_function, so try a weak result type.
00334   template<typename _Tp>
00335     struct _Reference_wrapper_base_impl<false, false, _Tp>
00336     : _Weak_result_type<_Tp>
00337     { };
00338 
00339   // unary_function but not binary_function
00340   template<typename _Tp>
00341     struct _Reference_wrapper_base_impl<true, false, _Tp>
00342     : unary_function<typename _Tp::argument_type,
00343              typename _Tp::result_type>
00344     { };
00345 
00346   // binary_function but not unary_function
00347   template<typename _Tp>
00348     struct _Reference_wrapper_base_impl<false, true, _Tp>
00349     : binary_function<typename _Tp::first_argument_type,
00350               typename _Tp::second_argument_type,
00351               typename _Tp::result_type>
00352     { };
00353 
00354   // Both unary_function and binary_function. Import result_type to
00355   // avoid conflicts.
00356    template<typename _Tp>
00357     struct _Reference_wrapper_base_impl<true, true, _Tp>
00358     : unary_function<typename _Tp::argument_type,
00359              typename _Tp::result_type>,
00360       binary_function<typename _Tp::first_argument_type,
00361               typename _Tp::second_argument_type,
00362               typename _Tp::result_type>
00363     {
00364       typedef typename _Tp::result_type result_type;
00365     };
00366 
00367   /**
00368    *  Derives from unary_function or binary_function when it
00369    *  can. Specializations handle all of the easy cases. The primary
00370    *  template determines what to do with a class type, which may
00371    *  derive from both unary_function and binary_function.
00372   */
00373   template<typename _Tp>
00374     struct _Reference_wrapper_base
00375     : _Reference_wrapper_base_impl<
00376       _Derives_from_unary_function<_Tp>::value,
00377       _Derives_from_binary_function<_Tp>::value,
00378       _Tp>
00379     { };
00380 
00381   // - a function type (unary)
00382   template<typename _Res, typename _T1>
00383     struct _Reference_wrapper_base<_Res(_T1)>
00384     : unary_function<_T1, _Res>
00385     { };
00386 
00387   // - a function type (binary)
00388   template<typename _Res, typename _T1, typename _T2>
00389     struct _Reference_wrapper_base<_Res(_T1, _T2)>
00390     : binary_function<_T1, _T2, _Res>
00391     { };
00392 
00393   // - a function pointer type (unary)
00394   template<typename _Res, typename _T1>
00395     struct _Reference_wrapper_base<_Res(*)(_T1)>
00396     : unary_function<_T1, _Res>
00397     { };
00398 
00399   // - a function pointer type (binary)
00400   template<typename _Res, typename _T1, typename _T2>
00401     struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
00402     : binary_function<_T1, _T2, _Res>
00403     { };
00404 
00405   // - a pointer to member function type (unary, no qualifiers)
00406   template<typename _Res, typename _T1>
00407     struct _Reference_wrapper_base<_Res (_T1::*)()>
00408     : unary_function<_T1*, _Res>
00409     { };
00410 
00411   // - a pointer to member function type (binary, no qualifiers)
00412   template<typename _Res, typename _T1, typename _T2>
00413     struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
00414     : binary_function<_T1*, _T2, _Res>
00415     { };
00416 
00417   // - a pointer to member function type (unary, const)
00418   template<typename _Res, typename _T1>
00419     struct _Reference_wrapper_base<_Res (_T1::*)() const>
00420     : unary_function<const _T1*, _Res>
00421     { };
00422 
00423   // - a pointer to member function type (binary, const)
00424   template<typename _Res, typename _T1, typename _T2>
00425     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
00426     : binary_function<const _T1*, _T2, _Res>
00427     { };
00428 
00429   // - a pointer to member function type (unary, volatile)
00430   template<typename _Res, typename _T1>
00431     struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
00432     : unary_function<volatile _T1*, _Res>
00433     { };
00434 
00435   // - a pointer to member function type (binary, volatile)
00436   template<typename _Res, typename _T1, typename _T2>
00437     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
00438     : binary_function<volatile _T1*, _T2, _Res>
00439     { };
00440 
00441   // - a pointer to member function type (unary, const volatile)
00442   template<typename _Res, typename _T1>
00443     struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
00444     : unary_function<const volatile _T1*, _Res>
00445     { };
00446 
00447   // - a pointer to member function type (binary, const volatile)
00448   template<typename _Res, typename _T1, typename _T2>
00449     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
00450     : binary_function<const volatile _T1*, _T2, _Res>
00451     { };
00452 
00453   /// reference_wrapper
00454   template<typename _Tp>
00455     class reference_wrapper
00456     : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
00457     {
00458       // If _Tp is a function type, we can't form result_of<_Tp(...)>,
00459       // so turn it into a function pointer type.
00460       typedef typename _Function_to_function_pointer<_Tp>::type
00461         _M_func_type;
00462 
00463       _Tp* _M_data;
00464     public:
00465       typedef _Tp type;
00466 
00467       explicit
00468       reference_wrapper(_Tp& __indata): _M_data(&__indata)
00469       { }
00470 
00471       reference_wrapper(const reference_wrapper<_Tp>& __inref):
00472       _M_data(__inref._M_data)
00473       { }
00474 
00475       reference_wrapper&
00476       operator=(const reference_wrapper<_Tp>& __inref)
00477       {
00478         _M_data = __inref._M_data;
00479         return *this;
00480       }
00481 
00482       operator _Tp&() const
00483       { return this->get(); }
00484 
00485       _Tp&
00486       get() const
00487       { return *_M_data; }
00488 
00489       template<typename... _Args>
00490         typename result_of<_M_func_type(_Args...)>::type
00491         operator()(_Args&... __args) const
00492         {
00493       return __invoke(get(), __args...);
00494     }
00495     };
00496 
00497 
00498   // Denotes a reference should be taken to a variable.
00499   template<typename _Tp>
00500     inline reference_wrapper<_Tp>
00501     ref(_Tp& __t)
00502     { return reference_wrapper<_Tp>(__t); }
00503 
00504   // Denotes a const reference should be taken to a variable.
00505   template<typename _Tp>
00506     inline reference_wrapper<const _Tp>
00507     cref(const _Tp& __t)
00508     { return reference_wrapper<const _Tp>(__t); }
00509 
00510   template<typename _Tp>
00511     inline reference_wrapper<_Tp>
00512     ref(reference_wrapper<_Tp> __t)
00513     { return ref(__t.get()); }
00514 
00515   template<typename _Tp>
00516     inline reference_wrapper<const _Tp>
00517     cref(reference_wrapper<_Tp> __t)
00518     { return cref(__t.get()); }
00519 
00520   template<typename _Tp, bool>
00521     struct _Mem_fn_const_or_non
00522     {
00523       typedef const _Tp& type;
00524     };
00525 
00526   template<typename _Tp>
00527     struct _Mem_fn_const_or_non<_Tp, false>
00528     {
00529       typedef _Tp& type;
00530     };
00531 
00532   /**
00533    * Derives from @c unary_function or @c binary_function, or perhaps
00534    * nothing, depending on the number of arguments provided. The
00535    * primary template is the basis case, which derives nothing.
00536    */
00537   template<typename _Res, typename... _ArgTypes> 
00538     struct _Maybe_unary_or_binary_function { };
00539 
00540   /// Derives from @c unary_function, as appropriate. 
00541   template<typename _Res, typename _T1> 
00542     struct _Maybe_unary_or_binary_function<_Res, _T1>
00543     : std::unary_function<_T1, _Res> { };
00544 
00545   /// Derives from @c binary_function, as appropriate. 
00546   template<typename _Res, typename _T1, typename _T2> 
00547     struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
00548     : std::binary_function<_T1, _T2, _Res> { };
00549 
00550   /// Implementation of @c mem_fn for member function pointers.
00551   template<typename _Res, typename _Class, typename... _ArgTypes>
00552     class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
00553     : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
00554     {
00555       typedef _Res (_Class::*_Functor)(_ArgTypes...);
00556 
00557       template<typename _Tp>
00558         _Res
00559         _M_call(_Tp& __object, const volatile _Class *, 
00560                 _ArgTypes... __args) const
00561         { return (__object.*__pmf)(__args...); }
00562 
00563       template<typename _Tp>
00564         _Res
00565         _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00566         { return ((*__ptr).*__pmf)(__args...); }
00567 
00568     public:
00569       typedef _Res result_type;
00570 
00571       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00572 
00573       // Handle objects
00574       _Res
00575       operator()(_Class& __object, _ArgTypes... __args) const
00576       { return (__object.*__pmf)(__args...); }
00577 
00578       // Handle pointers
00579       _Res
00580       operator()(_Class* __object, _ArgTypes... __args) const
00581       { return (__object->*__pmf)(__args...); }
00582 
00583       // Handle smart pointers, references and pointers to derived
00584       template<typename _Tp>
00585         _Res
00586     operator()(_Tp& __object, _ArgTypes... __args) const
00587         { return _M_call(__object, &__object, __args...); }
00588 
00589     private:
00590       _Functor __pmf;
00591     };
00592 
00593   /// Implementation of @c mem_fn for const member function pointers.
00594   template<typename _Res, typename _Class, typename... _ArgTypes>
00595     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
00596     : public _Maybe_unary_or_binary_function<_Res, const _Class*, 
00597                          _ArgTypes...>
00598     {
00599       typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
00600 
00601       template<typename _Tp>
00602         _Res
00603         _M_call(_Tp& __object, const volatile _Class *, 
00604                 _ArgTypes... __args) const
00605         { return (__object.*__pmf)(__args...); }
00606 
00607       template<typename _Tp>
00608         _Res
00609         _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00610         { return ((*__ptr).*__pmf)(__args...); }
00611 
00612     public:
00613       typedef _Res result_type;
00614 
00615       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00616 
00617       // Handle objects
00618       _Res
00619       operator()(const _Class& __object, _ArgTypes... __args) const
00620       { return (__object.*__pmf)(__args...); }
00621 
00622       // Handle pointers
00623       _Res
00624       operator()(const _Class* __object, _ArgTypes... __args) const
00625       { return (__object->*__pmf)(__args...); }
00626 
00627       // Handle smart pointers, references and pointers to derived
00628       template<typename _Tp>
00629         _Res operator()(_Tp& __object, _ArgTypes... __args) const
00630         { return _M_call(__object, &__object, __args...); }
00631 
00632     private:
00633       _Functor __pmf;
00634     };
00635 
00636   /// Implementation of @c mem_fn for volatile member function pointers.
00637   template<typename _Res, typename _Class, typename... _ArgTypes>
00638     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
00639     : public _Maybe_unary_or_binary_function<_Res, volatile _Class*, 
00640                          _ArgTypes...>
00641     {
00642       typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
00643 
00644       template<typename _Tp>
00645         _Res
00646         _M_call(_Tp& __object, const volatile _Class *, 
00647                 _ArgTypes... __args) const
00648         { return (__object.*__pmf)(__args...); }
00649 
00650       template<typename _Tp>
00651         _Res
00652         _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00653         { return ((*__ptr).*__pmf)(__args...); }
00654 
00655     public:
00656       typedef _Res result_type;
00657 
00658       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00659 
00660       // Handle objects
00661       _Res
00662       operator()(volatile _Class& __object, _ArgTypes... __args) const
00663       { return (__object.*__pmf)(__args...); }
00664 
00665       // Handle pointers
00666       _Res
00667       operator()(volatile _Class* __object, _ArgTypes... __args) const
00668       { return (__object->*__pmf)(__args...); }
00669 
00670       // Handle smart pointers, references and pointers to derived
00671       template<typename _Tp>
00672         _Res
00673     operator()(_Tp& __object, _ArgTypes... __args) const
00674         { return _M_call(__object, &__object, __args...); }
00675 
00676     private:
00677       _Functor __pmf;
00678     };
00679 
00680   /// Implementation of @c mem_fn for const volatile member function pointers.
00681   template<typename _Res, typename _Class, typename... _ArgTypes>
00682     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
00683     : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*, 
00684                          _ArgTypes...>
00685     {
00686       typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
00687 
00688       template<typename _Tp>
00689         _Res
00690         _M_call(_Tp& __object, const volatile _Class *, 
00691                 _ArgTypes... __args) const
00692         { return (__object.*__pmf)(__args...); }
00693 
00694       template<typename _Tp>
00695         _Res
00696         _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
00697         { return ((*__ptr).*__pmf)(__args...); }
00698 
00699     public:
00700       typedef _Res result_type;
00701 
00702       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
00703 
00704       // Handle objects
00705       _Res 
00706       operator()(const volatile _Class& __object, _ArgTypes... __args) const
00707       { return (__object.*__pmf)(__args...); }
00708 
00709       // Handle pointers
00710       _Res 
00711       operator()(const volatile _Class* __object, _ArgTypes... __args) const
00712       { return (__object->*__pmf)(__args...); }
00713 
00714       // Handle smart pointers, references and pointers to derived
00715       template<typename _Tp>
00716         _Res operator()(_Tp& __object, _ArgTypes... __args) const
00717         { return _M_call(__object, &__object, __args...); }
00718 
00719     private:
00720       _Functor __pmf;
00721     };
00722 
00723 
00724   template<typename _Res, typename _Class>
00725     class _Mem_fn<_Res _Class::*>
00726     {
00727       // This bit of genius is due to Peter Dimov, improved slightly by
00728       // Douglas Gregor.
00729       template<typename _Tp>
00730         _Res&
00731         _M_call(_Tp& __object, _Class *) const
00732         { return __object.*__pm; }
00733 
00734       template<typename _Tp, typename _Up>
00735         _Res&
00736         _M_call(_Tp& __object, _Up * const *) const
00737         { return (*__object).*__pm; }
00738 
00739       template<typename _Tp, typename _Up>
00740         const _Res&
00741         _M_call(_Tp& __object, const _Up * const *) const
00742         { return (*__object).*__pm; }
00743 
00744       template<typename _Tp>
00745         const _Res&
00746         _M_call(_Tp& __object, const _Class *) const
00747         { return __object.*__pm; }
00748 
00749       template<typename _Tp>
00750         const _Res&
00751         _M_call(_Tp& __ptr, const volatile void*) const
00752         { return (*__ptr).*__pm; }
00753 
00754       template<typename _Tp> static _Tp& __get_ref();
00755 
00756       template<typename _Tp>
00757         static __sfinae_types::__one __check_const(_Tp&, _Class*);
00758       template<typename _Tp, typename _Up>
00759         static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
00760       template<typename _Tp, typename _Up>
00761         static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
00762       template<typename _Tp>
00763         static __sfinae_types::__two __check_const(_Tp&, const _Class*);
00764       template<typename _Tp>
00765         static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
00766 
00767     public:
00768       template<typename _Tp>
00769         struct _Result_type
00770     : _Mem_fn_const_or_non<_Res,
00771       (sizeof(__sfinae_types::__two)
00772        == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
00773         { };
00774 
00775       template<typename _Signature>
00776         struct result;
00777 
00778       template<typename _CVMem, typename _Tp>
00779         struct result<_CVMem(_Tp)>
00780     : public _Result_type<_Tp> { };
00781 
00782       template<typename _CVMem, typename _Tp>
00783         struct result<_CVMem(_Tp&)>
00784     : public _Result_type<_Tp> { };
00785 
00786       explicit
00787       _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
00788 
00789       // Handle objects
00790       _Res&
00791       operator()(_Class& __object) const
00792       { return __object.*__pm; }
00793 
00794       const _Res&
00795       operator()(const _Class& __object) const
00796       { return __object.*__pm; }
00797 
00798       // Handle pointers
00799       _Res&
00800       operator()(_Class* __object) const
00801       { return __object->*__pm; }
00802 
00803       const _Res&
00804       operator()(const _Class* __object) const
00805       { return __object->*__pm; }
00806 
00807       // Handle smart pointers and derived
00808       template<typename _Tp>
00809         typename _Result_type<_Tp>::type
00810         operator()(_Tp& __unknown) const
00811         { return _M_call(__unknown, &__unknown); }
00812 
00813     private:
00814       _Res _Class::*__pm;
00815     };
00816 
00817   /**
00818    *  @brief Returns a function object that forwards to the member
00819    *  pointer @a pm.
00820    */
00821   template<typename _Tp, typename _Class>
00822     inline _Mem_fn<_Tp _Class::*>
00823     mem_fn(_Tp _Class::* __pm)
00824     {
00825       return _Mem_fn<_Tp _Class::*>(__pm);
00826     }
00827 
00828   /**
00829    *  @brief Determines if the given type _Tp is a function object
00830    *  should be treated as a subexpression when evaluating calls to
00831    *  function objects returned by bind(). [TR1 3.6.1]
00832    */
00833   template<typename _Tp>
00834     struct is_bind_expression
00835     { static const bool value = false; };
00836 
00837   template<typename _Tp>
00838     const bool is_bind_expression<_Tp>::value;
00839 
00840   /**
00841    *  @brief Determines if the given type _Tp is a placeholder in a
00842    *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
00843    */
00844   template<typename _Tp>
00845     struct is_placeholder
00846     { static const int value = 0; };
00847 
00848   template<typename _Tp>
00849     const int is_placeholder<_Tp>::value;
00850 
00851   /// The type of placeholder objects defined by libstdc++.
00852   template<int _Num> struct _Placeholder { };
00853 
00854   /** @namespace std::placeholders
00855    *  @brief ISO C++ 0x entities sub namespace for functional.
00856    *
00857    *  Define a large number of placeholders. There is no way to
00858    *  simplify this with variadic templates, because we're introducing
00859    *  unique names for each.
00860    */
00861   namespace placeholders 
00862   { 
00863     namespace 
00864     {
00865       _Placeholder<1> _1;
00866       _Placeholder<2> _2;
00867       _Placeholder<3> _3;
00868       _Placeholder<4> _4;
00869       _Placeholder<5> _5;
00870       _Placeholder<6> _6;
00871       _Placeholder<7> _7;
00872       _Placeholder<8> _8;
00873       _Placeholder<9> _9;
00874       _Placeholder<10> _10;
00875       _Placeholder<11> _11;
00876       _Placeholder<12> _12;
00877       _Placeholder<13> _13;
00878       _Placeholder<14> _14;
00879       _Placeholder<15> _15;
00880       _Placeholder<16> _16;
00881       _Placeholder<17> _17;
00882       _Placeholder<18> _18;
00883       _Placeholder<19> _19;
00884       _Placeholder<20> _20;
00885       _Placeholder<21> _21;
00886       _Placeholder<22> _22;
00887       _Placeholder<23> _23;
00888       _Placeholder<24> _24;
00889       _Placeholder<25> _25;
00890       _Placeholder<26> _26;
00891       _Placeholder<27> _27;
00892       _Placeholder<28> _28;
00893       _Placeholder<29> _29;
00894     } 
00895   }
00896 
00897   /**
00898    *  Partial specialization of is_placeholder that provides the placeholder
00899    *  number for the placeholder objects defined by libstdc++.
00900    */
00901   template<int _Num>
00902     struct is_placeholder<_Placeholder<_Num> >
00903     { static const int value = _Num; };
00904 
00905   template<int _Num>
00906     const int is_placeholder<_Placeholder<_Num> >::value;
00907 
00908   /**
00909    * Stores a tuple of indices. Used by bind() to extract the elements
00910    * in a tuple. 
00911    */
00912   template<int... _Indexes>
00913     struct _Index_tuple { };
00914 
00915   /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
00916   template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
00917     struct _Build_index_tuple;
00918  
00919   template<std::size_t _Num, int... _Indexes> 
00920     struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
00921     : _Build_index_tuple<_Num - 1, 
00922                          _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
00923     {
00924     };
00925 
00926   template<int... _Indexes>
00927     struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
00928     {
00929       typedef _Index_tuple<_Indexes...> __type;
00930     };
00931 
00932   /** 
00933    * Used by _Safe_tuple_element to indicate that there is no tuple
00934    * element at this position.
00935    */
00936   struct _No_tuple_element;
00937 
00938   /**
00939    * Implementation helper for _Safe_tuple_element. This primary
00940    * template handles the case where it is safe to use @c
00941    * tuple_element.
00942    */
00943   template<int __i, typename _Tuple, bool _IsSafe>
00944     struct _Safe_tuple_element_impl
00945     : tuple_element<__i, _Tuple> { };
00946 
00947   /**
00948    * Implementation helper for _Safe_tuple_element. This partial
00949    * specialization handles the case where it is not safe to use @c
00950    * tuple_element. We just return @c _No_tuple_element.
00951    */
00952   template<int __i, typename _Tuple>
00953     struct _Safe_tuple_element_impl<__i, _Tuple, false>
00954     {
00955       typedef _No_tuple_element type;
00956     };
00957 
00958   /**
00959    * Like tuple_element, but returns @c _No_tuple_element when
00960    * tuple_element would return an error.
00961    */
00962  template<int __i, typename _Tuple>
00963    struct _Safe_tuple_element
00964    : _Safe_tuple_element_impl<__i, _Tuple, 
00965                               (__i >= 0 && __i < tuple_size<_Tuple>::value)>
00966    {
00967    };
00968 
00969   /**
00970    *  Maps an argument to bind() into an actual argument to the bound
00971    *  function object [TR1 3.6.3/5]. Only the first parameter should
00972    *  be specified: the rest are used to determine among the various
00973    *  implementations. Note that, although this class is a function
00974    *  object, it isn't entirely normal because it takes only two
00975    *  parameters regardless of the number of parameters passed to the
00976    *  bind expression. The first parameter is the bound argument and
00977    *  the second parameter is a tuple containing references to the
00978    *  rest of the arguments.
00979    */
00980   template<typename _Arg,
00981            bool _IsBindExp = is_bind_expression<_Arg>::value,
00982            bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
00983     class _Mu;
00984 
00985   /**
00986    *  If the argument is reference_wrapper<_Tp>, returns the
00987    *  underlying reference. [TR1 3.6.3/5 bullet 1]
00988    */
00989   template<typename _Tp>
00990     class _Mu<reference_wrapper<_Tp>, false, false>
00991     {
00992     public:
00993       typedef _Tp& result_type;
00994 
00995       /* Note: This won't actually work for const volatile
00996        * reference_wrappers, because reference_wrapper::get() is const
00997        * but not volatile-qualified. This might be a defect in the TR.
00998        */
00999       template<typename _CVRef, typename _Tuple>
01000         result_type
01001         operator()(_CVRef& __arg, const _Tuple&) const volatile
01002         { return __arg.get(); }
01003     };
01004 
01005   /**
01006    *  If the argument is a bind expression, we invoke the underlying
01007    *  function object with the same cv-qualifiers as we are given and
01008    *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
01009    */
01010   template<typename _Arg>
01011     class _Mu<_Arg, true, false>
01012     {
01013     public:
01014       template<typename _Signature> class result;
01015 
01016       // Determine the result type when we pass the arguments along. This
01017       // involves passing along the cv-qualifiers placed on _Mu and
01018       // unwrapping the argument bundle.
01019       template<typename _CVMu, typename _CVArg, typename... _Args>
01020         class result<_CVMu(_CVArg, tuple<_Args...>)>
01021     : public result_of<_CVArg(_Args...)> { };
01022 
01023       template<typename _CVArg, typename... _Args>
01024         typename result_of<_CVArg(_Args...)>::type
01025         operator()(_CVArg& __arg,
01026            const tuple<_Args...>& __tuple) const volatile
01027         {
01028       // Construct an index tuple and forward to __call
01029       typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
01030         _Indexes;
01031       return this->__call(__arg, __tuple, _Indexes());
01032     }
01033 
01034     private:
01035       // Invokes the underlying function object __arg by unpacking all
01036       // of the arguments in the tuple. 
01037       template<typename _CVArg, typename... _Args, int... _Indexes>
01038         typename result_of<_CVArg(_Args...)>::type
01039         __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
01040            const _Index_tuple<_Indexes...>&) const volatile
01041         {
01042       return __arg(get<_Indexes>(__tuple)...);
01043     }
01044     };
01045 
01046   /**
01047    *  If the argument is a placeholder for the Nth argument, returns
01048    *  a reference to the Nth argument to the bind function object.
01049    *  [TR1 3.6.3/5 bullet 3]
01050    */
01051   template<typename _Arg>
01052     class _Mu<_Arg, false, true>
01053     {
01054     public:
01055       template<typename _Signature> class result;
01056 
01057       template<typename _CVMu, typename _CVArg, typename _Tuple>
01058         class result<_CVMu(_CVArg, _Tuple)>
01059         {
01060       // Add a reference, if it hasn't already been done for us.
01061       // This allows us to be a little bit sloppy in constructing
01062       // the tuple that we pass to result_of<...>.
01063       typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
01064                         - 1), _Tuple>::type
01065         __base_type;
01066 
01067     public:
01068       typedef typename add_lvalue_reference<__base_type>::type type;
01069     };
01070 
01071       template<typename _Tuple>
01072         typename result<_Mu(_Arg, _Tuple)>::type
01073         operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
01074         {
01075       return ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
01076     }
01077     };
01078 
01079   /**
01080    *  If the argument is just a value, returns a reference to that
01081    *  value. The cv-qualifiers on the reference are the same as the
01082    *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
01083    */
01084   template<typename _Arg>
01085     class _Mu<_Arg, false, false>
01086     {
01087     public:
01088       template<typename _Signature> struct result;
01089 
01090       template<typename _CVMu, typename _CVArg, typename _Tuple>
01091         struct result<_CVMu(_CVArg, _Tuple)>
01092         {
01093       typedef typename add_lvalue_reference<_CVArg>::type type;
01094     };
01095 
01096       // Pick up the cv-qualifiers of the argument
01097       template<typename _CVArg, typename _Tuple>
01098         _CVArg&
01099         operator()(_CVArg& __arg, const _Tuple&) const volatile
01100         { return __arg; }
01101     };
01102 
01103   /**
01104    *  Maps member pointers into instances of _Mem_fn but leaves all
01105    *  other function objects untouched. Used by tr1::bind(). The
01106    *  primary template handles the non--member-pointer case.
01107    */
01108   template<typename _Tp>
01109     struct _Maybe_wrap_member_pointer
01110     {
01111       typedef _Tp type;
01112       
01113       static const _Tp&
01114       __do_wrap(const _Tp& __x)
01115       { return __x; }
01116     };
01117 
01118   /**
01119    *  Maps member pointers into instances of _Mem_fn but leaves all
01120    *  other function objects untouched. Used by tr1::bind(). This
01121    *  partial specialization handles the member pointer case.
01122    */
01123   template<typename _Tp, typename _Class>
01124     struct _Maybe_wrap_member_pointer<_Tp _Class::*>
01125     {
01126       typedef _Mem_fn<_Tp _Class::*> type;
01127       
01128       static type
01129       __do_wrap(_Tp _Class::* __pm)
01130       { return type(__pm); }
01131     };
01132 
01133   // Specialization needed to prevent "forming reference to void" errors when
01134   // bind<void>() is called, because argument deduction instantiates
01135   // _Maybe_wrap_member_pointer<void> outside the immediate context where
01136   // SFINAE applies.
01137   template<>
01138     struct _Maybe_wrap_member_pointer<void>
01139     {
01140       typedef void type;
01141     };
01142 
01143   /// Type of the function object returned from bind().
01144   template<typename _Signature>
01145     struct _Bind;
01146 
01147    template<typename _Functor, typename... _Bound_args>
01148     class _Bind<_Functor(_Bound_args...)>
01149     : public _Weak_result_type<_Functor>
01150     {
01151       typedef _Bind __self_type;
01152       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 
01153         _Bound_indexes;
01154 
01155       _Functor _M_f;
01156       tuple<_Bound_args...> _M_bound_args;
01157 
01158       // Call unqualified
01159       template<typename... _Args, int... _Indexes>
01160         typename result_of<
01161                    _Functor(typename result_of<_Mu<_Bound_args> 
01162                             (_Bound_args, tuple<_Args...>)>::type...)
01163                  >::type
01164         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
01165         {
01166           return _M_f(_Mu<_Bound_args>()
01167                       (get<_Indexes>(_M_bound_args), __args)...);
01168         }
01169 
01170       // Call as const
01171       template<typename... _Args, int... _Indexes>
01172         typename result_of<
01173                    const _Functor(typename result_of<_Mu<_Bound_args> 
01174                                     (const _Bound_args, tuple<_Args...>)
01175                                   >::type...)>::type
01176         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
01177         {
01178           return _M_f(_Mu<_Bound_args>()
01179                       (get<_Indexes>(_M_bound_args), __args)...);
01180         }
01181 
01182       // Call as volatile
01183       template<typename... _Args, int... _Indexes>
01184         typename result_of<
01185                    volatile _Functor(typename result_of<_Mu<_Bound_args> 
01186                                     (volatile _Bound_args, tuple<_Args...>)
01187                                   >::type...)>::type
01188         __call(const tuple<_Args...>& __args, 
01189                _Index_tuple<_Indexes...>) volatile
01190         {
01191           return _M_f(_Mu<_Bound_args>()
01192                       (get<_Indexes>(_M_bound_args), __args)...);
01193         }
01194 
01195       // Call as const volatile
01196       template<typename... _Args, int... _Indexes>
01197         typename result_of<
01198                    const volatile _Functor(typename result_of<_Mu<_Bound_args> 
01199                                     (const volatile _Bound_args, 
01200                                      tuple<_Args...>)
01201                                   >::type...)>::type
01202         __call(const tuple<_Args...>& __args, 
01203                _Index_tuple<_Indexes...>) const volatile
01204         {
01205           return _M_f(_Mu<_Bound_args>()
01206                       (get<_Indexes>(_M_bound_args), __args)...);
01207         }
01208 
01209      public:
01210       explicit _Bind(_Functor __f, _Bound_args... __bound_args)
01211         : _M_f(__f), _M_bound_args(__bound_args...) { }
01212 
01213       // Call unqualified
01214       template<typename... _Args>
01215         typename result_of<
01216                    _Functor(typename result_of<_Mu<_Bound_args> 
01217                             (_Bound_args, tuple<_Args...>)>::type...)
01218                  >::type
01219         operator()(_Args&... __args)
01220         {
01221           return this->__call(tie(__args...), _Bound_indexes());
01222         }
01223 
01224       // Call as const
01225       template<typename... _Args>
01226         typename result_of<
01227                    const _Functor(typename result_of<_Mu<_Bound_args> 
01228                             (const _Bound_args, tuple<_Args...>)>::type...)
01229                  >::type
01230         operator()(_Args&... __args) const
01231         {
01232           return this->__call(tie(__args...), _Bound_indexes());
01233         }
01234 
01235 
01236       // Call as volatile
01237       template<typename... _Args>
01238         typename result_of<
01239                    volatile _Functor(typename result_of<_Mu<_Bound_args> 
01240                             (volatile _Bound_args, tuple<_Args...>)>::type...)
01241                  >::type
01242         operator()(_Args&... __args) volatile
01243         {
01244           return this->__call(tie(__args...), _Bound_indexes());
01245         }
01246 
01247 
01248       // Call as const volatile
01249       template<typename... _Args>
01250         typename result_of<
01251                    const volatile _Functor(typename result_of<_Mu<_Bound_args> 
01252                             (const volatile _Bound_args, 
01253                              tuple<_Args...>)>::type...)
01254                  >::type
01255         operator()(_Args&... __args) const volatile
01256         {
01257           return this->__call(tie(__args...), _Bound_indexes());
01258         }
01259     };
01260 
01261   /// Type of the function object returned from bind<R>().
01262   template<typename _Result, typename _Signature>
01263     struct _Bind_result;
01264 
01265   template<typename _Result, typename _Functor, typename... _Bound_args>
01266     class _Bind_result<_Result, _Functor(_Bound_args...)>
01267     {
01268       typedef _Bind_result __self_type;
01269       typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 
01270         _Bound_indexes;
01271 
01272       _Functor _M_f;
01273       tuple<_Bound_args...> _M_bound_args;
01274 
01275       // sfinae types
01276       template<typename _Res>
01277         struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
01278       template<typename _Res>
01279         struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
01280 
01281       // Call unqualified
01282       template<typename _Res, typename... _Args, int... _Indexes>
01283         _Result
01284         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01285             typename __disable_if_void<_Res>::type = 0)
01286         {
01287           return _M_f(_Mu<_Bound_args>()
01288                       (get<_Indexes>(_M_bound_args), __args)...);
01289         }
01290 
01291       // Call unqualified, return void
01292       template<typename _Res, typename... _Args, int... _Indexes>
01293         void
01294         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01295             typename __enable_if_void<_Res>::type = 0)
01296         {
01297           _M_f(_Mu<_Bound_args>()
01298                       (get<_Indexes>(_M_bound_args), __args)...);
01299         }
01300 
01301       // Call as const
01302       template<typename _Res, typename... _Args, int... _Indexes>
01303         _Result
01304         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01305             typename __disable_if_void<_Res>::type = 0) const
01306         {
01307           return _M_f(_Mu<_Bound_args>()
01308                       (get<_Indexes>(_M_bound_args), __args)...);
01309         }
01310 
01311       // Call as const, return void
01312       template<typename _Res, typename... _Args, int... _Indexes>
01313         void
01314         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01315             typename __enable_if_void<_Res>::type = 0) const
01316         {
01317           _M_f(_Mu<_Bound_args>()
01318                       (get<_Indexes>(_M_bound_args), __args)...);
01319         }
01320 
01321       // Call as volatile
01322       template<typename _Res, typename... _Args, int... _Indexes>
01323         _Result
01324         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01325             typename __disable_if_void<_Res>::type = 0) volatile
01326         {
01327           return _M_f(_Mu<_Bound_args>()
01328                       (get<_Indexes>(_M_bound_args), __args)...);
01329         }
01330 
01331       // Call as volatile, return void
01332       template<typename _Res, typename... _Args, int... _Indexes>
01333         void
01334         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01335             typename __enable_if_void<_Res>::type = 0) volatile
01336         {
01337           _M_f(_Mu<_Bound_args>()
01338                       (get<_Indexes>(_M_bound_args), __args)...);
01339         }
01340 
01341       // Call as const volatile
01342       template<typename _Res, typename... _Args, int... _Indexes>
01343         _Result
01344         __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>,
01345             typename __disable_if_void<_Res>::type = 0) const volatile
01346         {
01347           return _M_f(_Mu<_Bound_args>()
01348                       (get<_Indexes>(_M_bound_args), __args)...);
01349         }
01350 
01351       // Call as const volatile, return void
01352       template<typename _Res, typename... _Args, int... _Indexes>
01353         void
01354         __call(const tuple<_Args...>& __args, 
01355                _Index_tuple<_Indexes...>,
01356             typename __enable_if_void<_Res>::type = 0) const volatile
01357         {
01358           _M_f(_Mu<_Bound_args>()
01359                       (get<_Indexes>(_M_bound_args), __args)...);
01360         }
01361 
01362     public:
01363       typedef _Result result_type;
01364 
01365       explicit
01366       _Bind_result(_Functor __f, _Bound_args... __bound_args)
01367       : _M_f(__f), _M_bound_args(__bound_args...) { }
01368 
01369       // Call unqualified
01370       template<typename... _Args>
01371         result_type
01372         operator()(_Args&... __args)
01373         {
01374           return this->__call<_Result>(tie(__args...), _Bound_indexes());
01375         }
01376 
01377       // Call as const
01378       template<typename... _Args>
01379         result_type
01380         operator()(_Args&... __args) const
01381         {
01382           return this->__call<_Result>(tie(__args...), _Bound_indexes());
01383         }
01384 
01385       // Call as volatile
01386       template<typename... _Args>
01387         result_type
01388         operator()(_Args&... __args) volatile
01389         {
01390           return this->__call<_Result>(tie(__args...), _Bound_indexes());
01391         }
01392 
01393       // Call as const volatile
01394       template<typename... _Args>
01395         result_type
01396         operator()(_Args&... __args) const volatile
01397         {
01398           return this->__call<_Result>(tie(__args...), _Bound_indexes());
01399         }
01400     };
01401 
01402   /// Class template _Bind is always a bind expression.
01403   template<typename _Signature>
01404     struct is_bind_expression<_Bind<_Signature> >
01405     { static const bool value = true; };
01406 
01407   template<typename _Signature>
01408     const bool is_bind_expression<_Bind<_Signature> >::value;
01409 
01410   /// Class template _Bind_result is always a bind expression.
01411   template<typename _Result, typename _Signature>
01412     struct is_bind_expression<_Bind_result<_Result, _Signature> >
01413     { static const bool value = true; };
01414 
01415   template<typename _Result, typename _Signature>
01416     const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
01417 
01418   /// bind
01419   template<typename _Functor, typename... _ArgTypes>
01420     inline
01421     _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
01422     bind(_Functor __f, _ArgTypes... __args)
01423     {
01424       typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
01425       typedef typename __maybe_type::type __functor_type;
01426       typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
01427       return __result_type(__maybe_type::__do_wrap(__f), __args...);
01428     } 
01429 
01430   template<typename _Result, typename _Functor, typename... _ArgTypes>
01431     inline
01432     _Bind_result<_Result,
01433          typename _Maybe_wrap_member_pointer<_Functor>::type
01434                             (_ArgTypes...)>
01435     bind(_Functor __f, _ArgTypes... __args)
01436     {
01437       typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
01438       typedef typename __maybe_type::type __functor_type;
01439       typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
01440     __result_type;
01441       return __result_type(__maybe_type::__do_wrap(__f), __args...);
01442     }
01443 
01444   /**
01445    *  @brief Exception class thrown when class template function's
01446    *  operator() is called with an empty target.
01447    *  @ingroup exceptions
01448    */
01449   class bad_function_call : public std::exception { };
01450 
01451   /**
01452    *  The integral constant expression 0 can be converted into a
01453    *  pointer to this type. It is used by the function template to
01454    *  accept NULL pointers.
01455    */
01456   struct _M_clear_type;
01457 
01458   /**
01459    *  Trait identifying "location-invariant" types, meaning that the
01460    *  address of the object (or any of its members) will not escape.
01461    *  Also implies a trivial copy constructor and assignment operator.
01462    */
01463   template<typename _Tp>
01464     struct __is_location_invariant
01465     : integral_constant<bool,
01466                         (is_pointer<_Tp>::value
01467                          || is_member_pointer<_Tp>::value)>
01468     {
01469     };
01470 
01471   class _Undefined_class;
01472 
01473   union _Nocopy_types
01474   {
01475     void*       _M_object;
01476     const void* _M_const_object;
01477     void (*_M_function_pointer)();
01478     void (_Undefined_class::*_M_member_pointer)();
01479   };
01480 
01481   union _Any_data
01482   {
01483     void*       _M_access()       { return &_M_pod_data[0]; }
01484     const void* _M_access() const { return &_M_pod_data[0]; }
01485 
01486     template<typename _Tp>
01487       _Tp&
01488       _M_access()
01489       { return *static_cast<_Tp*>(_M_access()); }
01490 
01491     template<typename _Tp>
01492       const _Tp&
01493       _M_access() const
01494       { return *static_cast<const _Tp*>(_M_access()); }
01495 
01496     _Nocopy_types _M_unused;
01497     char _M_pod_data[sizeof(_Nocopy_types)];
01498   };
01499 
01500   enum _Manager_operation
01501   {
01502     __get_type_info,
01503     __get_functor_ptr,
01504     __clone_functor,
01505     __destroy_functor
01506   };
01507 
01508   // Simple type wrapper that helps avoid annoying const problems
01509   // when casting between void pointers and pointers-to-pointers.
01510   template<typename _Tp>
01511     struct _Simple_type_wrapper
01512     {
01513       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
01514 
01515       _Tp __value;
01516     };
01517 
01518   template<typename _Tp>
01519     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
01520     : __is_location_invariant<_Tp>
01521     {
01522     };
01523 
01524   // Converts a reference to a function object into a callable
01525   // function object.
01526   template<typename _Functor>
01527     inline _Functor&
01528     __callable_functor(_Functor& __f)
01529     { return __f; }
01530 
01531   template<typename _Member, typename _Class>
01532     inline _Mem_fn<_Member _Class::*>
01533     __callable_functor(_Member _Class::* &__p)
01534     { return mem_fn(__p); }
01535 
01536   template<typename _Member, typename _Class>
01537     inline _Mem_fn<_Member _Class::*>
01538     __callable_functor(_Member _Class::* const &__p)
01539     { return mem_fn(__p); }
01540 
01541   template<typename _Signature>
01542     class function;
01543 
01544   /// Base class of all polymorphic function object wrappers.
01545   class _Function_base
01546   {
01547   public:
01548     static const std::size_t _M_max_size = sizeof(_Nocopy_types);
01549     static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
01550 
01551     template<typename _Functor>
01552       class _Base_manager
01553       {
01554       protected:
01555     static const bool __stored_locally =
01556         (__is_location_invariant<_Functor>::value
01557          && sizeof(_Functor) <= _M_max_size
01558          && __alignof__(_Functor) <= _M_max_align
01559          && (_M_max_align % __alignof__(_Functor) == 0));
01560     
01561     typedef integral_constant<bool, __stored_locally> _Local_storage;
01562 
01563     // Retrieve a pointer to the function object
01564     static _Functor*
01565     _M_get_pointer(const _Any_data& __source)
01566     {
01567       const _Functor* __ptr =
01568         __stored_locally? &__source._M_access<_Functor>()
01569         /* have stored a pointer */ : __source._M_access<_Functor*>();
01570       return const_cast<_Functor*>(__ptr);
01571     }
01572 
01573     // Clone a location-invariant function object that fits within
01574     // an _Any_data structure.
01575     static void
01576     _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
01577     {
01578       new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
01579     }
01580 
01581     // Clone a function object that is not location-invariant or
01582     // that cannot fit into an _Any_data structure.
01583     static void
01584     _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
01585     {
01586       __dest._M_access<_Functor*>() =
01587         new _Functor(*__source._M_access<_Functor*>());
01588     }
01589 
01590     // Destroying a location-invariant object may still require
01591     // destruction.
01592     static void
01593     _M_destroy(_Any_data& __victim, true_type)
01594     {
01595       __victim._M_access<_Functor>().~_Functor();
01596     }
01597     
01598     // Destroying an object located on the heap.
01599     static void
01600     _M_destroy(_Any_data& __victim, false_type)
01601     {
01602       delete __victim._M_access<_Functor*>();
01603     }
01604     
01605       public:
01606     static bool
01607     _M_manager(_Any_data& __dest, const _Any_data& __source,
01608            _Manager_operation __op)
01609     {
01610       switch (__op)
01611         {
01612 #ifdef __GXX_RTTI
01613         case __get_type_info:
01614           __dest._M_access<const type_info*>() = &typeid(_Functor);
01615           break;
01616 #endif
01617         case __get_functor_ptr:
01618           __dest._M_access<_Functor*>() = _M_get_pointer(__source);
01619           break;
01620           
01621         case __clone_functor:
01622           _M_clone(__dest, __source, _Local_storage());
01623           break;
01624 
01625         case __destroy_functor:
01626           _M_destroy(__dest, _Local_storage());
01627           break;
01628         }
01629       return false;
01630     }
01631 
01632     static void
01633     _M_init_functor(_Any_data& __functor, _Functor&& __f)
01634     { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
01635     
01636     template<typename _Signature>
01637       static bool
01638       _M_not_empty_function(const function<_Signature>& __f)
01639           { return static_cast<bool>(__f); }
01640 
01641     template<typename _Tp>
01642       static bool
01643       _M_not_empty_function(const _Tp*& __fp)
01644       { return __fp; }
01645 
01646     template<typename _Class, typename _Tp>
01647       static bool
01648       _M_not_empty_function(_Tp _Class::* const& __mp)
01649       { return __mp; }
01650 
01651     template<typename _Tp>
01652       static bool
01653       _M_not_empty_function(const _Tp&)
01654       { return true; }
01655 
01656       private:
01657         static void
01658     _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
01659     { new (__functor._M_access()) _Functor(std::move(__f)); }
01660 
01661     static void
01662     _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
01663     { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
01664       };
01665 
01666     template<typename _Functor>
01667       class _Ref_manager : public _Base_manager<_Functor*>
01668       {
01669     typedef _Function_base::_Base_manager<_Functor*> _Base;
01670 
01671     public:
01672     static bool
01673     _M_manager(_Any_data& __dest, const _Any_data& __source,
01674            _Manager_operation __op)
01675     {
01676       switch (__op)
01677         {
01678 #ifdef __GXX_RTTI
01679         case __get_type_info:
01680           __dest._M_access<const type_info*>() = &typeid(_Functor);
01681           break;
01682 #endif
01683         case __get_functor_ptr:
01684           __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
01685           return is_const<_Functor>::value;
01686           break;
01687           
01688         default:
01689           _Base::_M_manager(__dest, __source, __op);
01690         }
01691       return false;
01692     }
01693 
01694     static void
01695     _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
01696     {
01697       // TBD: Use address_of function instead.
01698       _Base::_M_init_functor(__functor, &__f.get());
01699     }
01700       };
01701 
01702     _Function_base() : _M_manager(0) { }
01703     
01704     ~_Function_base()
01705     {
01706       if (_M_manager)
01707     _M_manager(_M_functor, _M_functor, __destroy_functor);
01708     }
01709 
01710 
01711     bool _M_empty() const { return !_M_manager; }
01712 
01713     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
01714                                   _Manager_operation);
01715 
01716     _Any_data     _M_functor;
01717     _Manager_type _M_manager;
01718   };
01719 
01720   template<typename _Signature, typename _Functor>
01721     class _Function_handler;
01722 
01723   template<typename _Res, typename _Functor, typename... _ArgTypes>
01724     class _Function_handler<_Res(_ArgTypes...), _Functor>
01725     : public _Function_base::_Base_manager<_Functor>
01726     {
01727       typedef _Function_base::_Base_manager<_Functor> _Base;
01728 
01729     public:
01730       static _Res
01731       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01732       {
01733         return (*_Base::_M_get_pointer(__functor))(
01734             std::forward<_ArgTypes>(__args)...);
01735       }
01736     };
01737 
01738   template<typename _Functor, typename... _ArgTypes>
01739     class _Function_handler<void(_ArgTypes...), _Functor>
01740     : public _Function_base::_Base_manager<_Functor>
01741     {
01742       typedef _Function_base::_Base_manager<_Functor> _Base;
01743 
01744      public:
01745       static void
01746       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01747       {
01748         (*_Base::_M_get_pointer(__functor))(
01749             std::forward<_ArgTypes>(__args)...);
01750       }
01751     };
01752 
01753   template<typename _Res, typename _Functor, typename... _ArgTypes>
01754     class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
01755     : public _Function_base::_Ref_manager<_Functor>
01756     {
01757       typedef _Function_base::_Ref_manager<_Functor> _Base;
01758 
01759      public:
01760       static _Res
01761       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01762       {
01763         return __callable_functor(**_Base::_M_get_pointer(__functor))(
01764               std::forward<_ArgTypes>(__args)...);
01765       }
01766     };
01767 
01768   template<typename _Functor, typename... _ArgTypes>
01769     class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
01770     : public _Function_base::_Ref_manager<_Functor>
01771     {
01772       typedef _Function_base::_Ref_manager<_Functor> _Base;
01773 
01774      public:
01775       static void
01776       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01777       {
01778         __callable_functor(**_Base::_M_get_pointer(__functor))(
01779             std::forward<_ArgTypes>(__args)...);
01780       }
01781     };
01782 
01783   template<typename _Class, typename _Member, typename _Res, 
01784            typename... _ArgTypes>
01785     class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
01786     : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
01787     {
01788       typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
01789         _Base;
01790 
01791      public:
01792       static _Res
01793       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01794       {
01795         return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
01796             std::forward<_ArgTypes>(__args)...);
01797       }
01798     };
01799 
01800   template<typename _Class, typename _Member, typename... _ArgTypes>
01801     class _Function_handler<void(_ArgTypes...), _Member _Class::*>
01802     : public _Function_base::_Base_manager<
01803                  _Simple_type_wrapper< _Member _Class::* > >
01804     {
01805       typedef _Member _Class::* _Functor;
01806       typedef _Simple_type_wrapper<_Functor> _Wrapper;
01807       typedef _Function_base::_Base_manager<_Wrapper> _Base;
01808 
01809      public:
01810       static bool
01811       _M_manager(_Any_data& __dest, const _Any_data& __source,
01812                  _Manager_operation __op)
01813       {
01814         switch (__op)
01815       {
01816 #ifdef __GXX_RTTI
01817       case __get_type_info:
01818         __dest._M_access<const type_info*>() = &typeid(_Functor);
01819         break;
01820 #endif      
01821       case __get_functor_ptr:
01822         __dest._M_access<_Functor*>() =
01823           &_Base::_M_get_pointer(__source)->__value;
01824         break;
01825         
01826       default:
01827         _Base::_M_manager(__dest, __source, __op);
01828       }
01829         return false;
01830       }
01831 
01832       static void
01833       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
01834       {
01835     mem_fn(_Base::_M_get_pointer(__functor)->__value)(
01836             std::forward<_ArgTypes>(__args)...);
01837       }
01838     };
01839 
01840   /// class function
01841   template<typename _Res, typename... _ArgTypes>
01842     class function<_Res(_ArgTypes...)>
01843     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
01844       private _Function_base
01845     {
01846       typedef _Res _Signature_type(_ArgTypes...);
01847       
01848       struct _Useless { };
01849       
01850     public:
01851       typedef _Res result_type;
01852       
01853       // [3.7.2.1] construct/copy/destroy
01854       
01855       /**
01856        *  @brief Default construct creates an empty function call wrapper.
01857        *  @post @c !(bool)*this
01858        */
01859       explicit
01860       function() : _Function_base() { }
01861       
01862       /**
01863        *  @brief Default construct creates an empty function call wrapper.
01864        *  @post @c !(bool)*this
01865        */
01866       function(_M_clear_type*) : _Function_base() { }
01867       
01868       /**
01869        *  @brief %Function copy constructor.
01870        *  @param x A %function object with identical call signature.
01871        *  @post @c (bool)*this == (bool)x
01872        *
01873        *  The newly-created %function contains a copy of the target of @a
01874        *  x (if it has one).
01875        */
01876       function(const function& __x);
01877 
01878       /**
01879        *  @brief %Function move constructor.
01880        *  @param x A %function object rvalue with identical call signature.
01881        *
01882        *  The newly-created %function contains the target of @a x
01883        *  (if it has one).
01884        */
01885       function(function&& __x) : _Function_base()
01886       {
01887         __x.swap(*this);
01888       }
01889 
01890       // TODO: needs allocator_arg_t
01891       
01892       /**
01893        *  @brief Builds a %function that targets a copy of the incoming
01894        *  function object.
01895        *  @param f A %function object that is callable with parameters of
01896        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
01897        *  to @c Res.
01898        *
01899        *  The newly-created %function object will target a copy of @a
01900        *  f. If @a f is @c reference_wrapper<F>, then this function
01901        *  object will contain a reference to the function object @c
01902        *  f.get(). If @a f is a NULL function pointer or NULL
01903        *  pointer-to-member, the newly-created object will be empty.
01904        *
01905        *  If @a f is a non-NULL function pointer or an object of type @c
01906        *  reference_wrapper<F>, this function will not throw.
01907        */
01908       template<typename _Functor>
01909         function(_Functor __f,
01910                  typename __gnu_cxx::__enable_if<
01911                            !is_integral<_Functor>::value, _Useless>::__type
01912                    = _Useless());
01913 
01914       /**
01915        *  @brief %Function assignment operator.
01916        *  @param x A %function with identical call signature.
01917        *  @post @c (bool)*this == (bool)x
01918        *  @returns @c *this
01919        *
01920        *  The target of @a x is copied to @c *this. If @a x has no
01921        *  target, then @c *this will be empty.
01922        *
01923        *  If @a x targets a function pointer or a reference to a function
01924        *  object, then this operation will not throw an %exception.
01925        */
01926       function&
01927       operator=(const function& __x)
01928       {
01929         function(__x).swap(*this);
01930         return *this;
01931       }
01932 
01933       /**
01934        *  @brief %Function move-assignment operator.
01935        *  @param x A %function rvalue with identical call signature.
01936        *  @returns @c *this
01937        *
01938        *  The target of @a x is moved to @c *this. If @a x has no
01939        *  target, then @c *this will be empty.
01940        *
01941        *  If @a x targets a function pointer or a reference to a function
01942        *  object, then this operation will not throw an %exception.
01943        */
01944       function&
01945       operator=(function&& __x)
01946       {
01947         function(std::move(__x)).swap(*this);
01948         return *this;
01949       }
01950 
01951       /**
01952        *  @brief %Function assignment to zero.
01953        *  @post @c !(bool)*this
01954        *  @returns @c *this
01955        *
01956        *  The target of @c *this is deallocated, leaving it empty.
01957        */
01958       function&
01959       operator=(_M_clear_type*)
01960       {
01961         if (_M_manager)
01962       {
01963         _M_manager(_M_functor, _M_functor, __destroy_functor);
01964         _M_manager = 0;
01965         _M_invoker = 0;
01966       }
01967         return *this;
01968       }
01969 
01970       /**
01971        *  @brief %Function assignment to a new target.
01972        *  @param f A %function object that is callable with parameters of
01973        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
01974        *  to @c Res.
01975        *  @return @c *this
01976        *
01977        *  This  %function object wrapper will target a copy of @a
01978        *  f. If @a f is @c reference_wrapper<F>, then this function
01979        *  object will contain a reference to the function object @c
01980        *  f.get(). If @a f is a NULL function pointer or NULL
01981        *  pointer-to-member, @c this object will be empty.
01982        *
01983        *  If @a f is a non-NULL function pointer or an object of type @c
01984        *  reference_wrapper<F>, this function will not throw.
01985        */
01986       template<typename _Functor>
01987         typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
01988                                     function&>::__type
01989     operator=(_Functor&& __f)
01990     {
01991       function(std::forward<_Functor>(__f)).swap(*this);
01992       return *this;
01993     }
01994 
01995       /// @overload
01996       template<typename _Functor>
01997         typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
01998                                     function&>::__type
01999     operator=(reference_wrapper<_Functor> __f)
02000     {
02001       function(__f).swap(*this);
02002       return *this;
02003     }
02004 
02005       // [3.7.2.2] function modifiers
02006       
02007       /**
02008        *  @brief Swap the targets of two %function objects.
02009        *  @param f A %function with identical call signature.
02010        *
02011        *  Swap the targets of @c this function object and @a f. This
02012        *  function will not throw an %exception.
02013        */
02014       void swap(function& __x)
02015       {
02016     _Any_data __old_functor = _M_functor;
02017     _M_functor = __x._M_functor;
02018     __x._M_functor = __old_functor;
02019     _Manager_type __old_manager = _M_manager;
02020     _M_manager = __x._M_manager;
02021     __x._M_manager = __old_manager;
02022     _Invoker_type __old_invoker = _M_invoker;
02023     _M_invoker = __x._M_invoker;
02024     __x._M_invoker = __old_invoker;
02025       }
02026 
02027       // TODO: needs allocator_arg_t
02028       /*
02029       template<typename _Functor, typename _Alloc>
02030         void
02031         assign(_Functor&& __f, const _Alloc& __a)
02032         {
02033           function(allocator_arg, __a,
02034                    std::forward<_Functor>(__f)).swap(*this);
02035         }
02036       */
02037       
02038       // [3.7.2.3] function capacity
02039 
02040       /**
02041        *  @brief Determine if the %function wrapper has a target.
02042        *
02043        *  @return @c true when this %function object contains a target,
02044        *  or @c false when it is empty.
02045        *
02046        *  This function will not throw an %exception.
02047        */
02048       explicit operator bool() const
02049       { return !_M_empty(); }
02050 
02051       // [3.7.2.4] function invocation
02052 
02053       /**
02054        *  @brief Invokes the function targeted by @c *this.
02055        *  @returns the result of the target.
02056        *  @throws bad_function_call when @c !(bool)*this
02057        *
02058        *  The function call operator invokes the target function object
02059        *  stored by @c this.
02060        */
02061       _Res operator()(_ArgTypes... __args) const;
02062 
02063 #ifdef __GXX_RTTI
02064       // [3.7.2.5] function target access
02065       /**
02066        *  @brief Determine the type of the target of this function object
02067        *  wrapper.
02068        *
02069        *  @returns the type identifier of the target function object, or
02070        *  @c typeid(void) if @c !(bool)*this.
02071        *
02072        *  This function will not throw an %exception.
02073        */
02074       const type_info& target_type() const;
02075       
02076       /**
02077        *  @brief Access the stored target function object.
02078        *
02079        *  @return Returns a pointer to the stored target function object,
02080        *  if @c typeid(Functor).equals(target_type()); otherwise, a NULL
02081        *  pointer.
02082        *
02083        * This function will not throw an %exception.
02084        */
02085       template<typename _Functor>       _Functor* target();
02086       
02087       /// @overload
02088       template<typename _Functor> const _Functor* target() const;
02089 #endif
02090 
02091       // deleted overloads
02092       template<typename _Res2, typename... _ArgTypes2>
02093     void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
02094       template<typename _Res2, typename... _ArgTypes2>
02095     void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
02096 
02097     private:
02098       typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
02099       _Invoker_type _M_invoker;
02100   };
02101 
02102   template<typename _Res, typename... _ArgTypes>
02103     function<_Res(_ArgTypes...)>::
02104     function(const function& __x)
02105     : _Function_base()
02106     {
02107       if (static_cast<bool>(__x))
02108     {
02109       _M_invoker = __x._M_invoker;
02110       _M_manager = __x._M_manager;
02111       __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
02112     }
02113     }
02114 
02115   template<typename _Res, typename... _ArgTypes>
02116     template<typename _Functor>
02117       function<_Res(_ArgTypes...)>::
02118       function(_Functor __f,
02119            typename __gnu_cxx::__enable_if<
02120                        !is_integral<_Functor>::value, _Useless>::__type)
02121       : _Function_base()
02122       {
02123     typedef _Function_handler<_Signature_type, _Functor> _My_handler;
02124 
02125     if (_My_handler::_M_not_empty_function(__f))
02126       {
02127         _M_invoker = &_My_handler::_M_invoke;
02128         _M_manager = &_My_handler::_M_manager;
02129         _My_handler::_M_init_functor(_M_functor, std::move(__f));
02130       }
02131       }
02132 
02133   template<typename _Res, typename... _ArgTypes>
02134     _Res
02135     function<_Res(_ArgTypes...)>::
02136     operator()(_ArgTypes... __args) const
02137     {
02138       if (_M_empty())
02139         __throw_bad_function_call();
02140       return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
02141     }
02142 
02143 #ifdef __GXX_RTTI
02144   template<typename _Res, typename... _ArgTypes>
02145     const type_info&
02146     function<_Res(_ArgTypes...)>::
02147     target_type() const
02148     {
02149       if (_M_manager)
02150         {
02151           _Any_data __typeinfo_result;
02152           _M_manager(__typeinfo_result, _M_functor, __get_type_info);
02153           return *__typeinfo_result._M_access<const type_info*>();
02154         }
02155       else
02156     return typeid(void);
02157     }
02158 
02159   template<typename _Res, typename... _ArgTypes>
02160     template<typename _Functor>
02161       _Functor*
02162       function<_Res(_ArgTypes...)>::
02163       target()
02164       {
02165     if (typeid(_Functor) == target_type() && _M_manager)
02166       {
02167         _Any_data __ptr;
02168         if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
02169         && !is_const<_Functor>::value)
02170           return 0;
02171         else
02172           return __ptr._M_access<_Functor*>();
02173       }
02174     else
02175       return 0;
02176       }
02177 
02178   template<typename _Res, typename... _ArgTypes>
02179     template<typename _Functor>
02180       const _Functor*
02181       function<_Res(_ArgTypes...)>::
02182       target() const
02183       {
02184     if (typeid(_Functor) == target_type() && _M_manager)
02185       {
02186         _Any_data __ptr;
02187         _M_manager(__ptr, _M_functor, __get_functor_ptr);
02188         return __ptr._M_access<const _Functor*>();
02189       }
02190     else
02191       return 0;
02192       }
02193 #endif
02194 
02195   // [20.7.15.2.6] null pointer comparisons
02196 
02197   /**
02198    *  @brief Compares a polymorphic function object wrapper against 0
02199    *  (the NULL pointer).
02200    *  @returns @c true if the wrapper has no target, @c false otherwise
02201    *
02202    *  This function will not throw an %exception.
02203    */
02204   template<typename _Res, typename... _Args>
02205     inline bool
02206     operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
02207     { return !static_cast<bool>(__f); }
02208 
02209   /// @overload
02210   template<typename _Res, typename... _Args>
02211     inline bool
02212     operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
02213     { return !static_cast<bool>(__f); }
02214 
02215   /**
02216    *  @brief Compares a polymorphic function object wrapper against 0
02217    *  (the NULL pointer).
02218    *  @returns @c false if the wrapper has no target, @c true otherwise
02219    *
02220    *  This function will not throw an %exception.
02221    */
02222   template<typename _Res, typename... _Args>
02223     inline bool
02224     operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
02225     { return static_cast<bool>(__f); }
02226 
02227   /// @overload
02228   template<typename _Res, typename... _Args>
02229     inline bool
02230     operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
02231     { return static_cast<bool>(__f); }
02232 
02233   // [20.7.15.2.7] specialized algorithms
02234 
02235   /**
02236    *  @brief Swap the targets of two polymorphic function object wrappers.
02237    *
02238    *  This function will not throw an %exception.
02239    */
02240   template<typename _Res, typename... _Args>
02241     inline void
02242     swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
02243     { __x.swap(__y); }
02244 }
02245 
02246 #endif // __GXX_EXPERIMENTAL_CXX0X__
02247 
02248 #endif // _GLIBCXX_FUNCTIONAL

Generated on 11 Jan 2010 for libstdc++ by  doxygen 1.6.1