xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/tr1/functional (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert// TR1 functional header -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert// Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4*404b540aSrobert//
5*404b540aSrobert// This file is part of the GNU ISO C++ Library.  This library is free
6*404b540aSrobert// software; you can redistribute it and/or modify it under the
7*404b540aSrobert// terms of the GNU General Public License as published by the
8*404b540aSrobert// Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert// any later version.
10*404b540aSrobert
11*404b540aSrobert// This library is distributed in the hope that it will be useful,
12*404b540aSrobert// but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*404b540aSrobert// GNU General Public License for more details.
15*404b540aSrobert
16*404b540aSrobert// You should have received a copy of the GNU General Public License along
17*404b540aSrobert// with this library; see the file COPYING.  If not, write to the Free
18*404b540aSrobert// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert// USA.
20*404b540aSrobert
21*404b540aSrobert// As a special exception, you may use this file as part of a free software
22*404b540aSrobert// library without restriction.  Specifically, if other files instantiate
23*404b540aSrobert// templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert// this file and link it with other files to produce an executable, this
25*404b540aSrobert// file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert// the GNU General Public License.  This exception does not however
27*404b540aSrobert// invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert// the GNU General Public License.
29*404b540aSrobert
30*404b540aSrobert/** @file tr1/functional
31*404b540aSrobert *  This is a TR1 C++ Library header.
32*404b540aSrobert */
33*404b540aSrobert
34*404b540aSrobert#ifndef _TR1_FUNCTIONAL
35*404b540aSrobert#define _TR1_FUNCTIONAL 1
36*404b540aSrobert
37*404b540aSrobert#pragma GCC system_header
38*404b540aSrobert
39*404b540aSrobert#include "../functional"
40*404b540aSrobert#include <typeinfo>
41*404b540aSrobert#include <tr1/type_traits>
42*404b540aSrobert#include <ext/type_traits.h>
43*404b540aSrobert#include <cstdlib>  // for std::abort
44*404b540aSrobert#include <tr1/tuple>
45*404b540aSrobert
46*404b540aSrobertnamespace std
47*404b540aSrobert{
48*404b540aSrobert_GLIBCXX_BEGIN_NAMESPACE(tr1)
49*404b540aSrobert
50*404b540aSrobert  template<typename _MemberPointer>
51*404b540aSrobert    class _Mem_fn;
52*404b540aSrobert
53*404b540aSrobert  /**
54*404b540aSrobert   *  @if maint
55*404b540aSrobert   *  Actual implementation of _Has_result_type, which uses SFINAE to
56*404b540aSrobert   *  determine if the type _Tp has a publicly-accessible member type
57*404b540aSrobert   *  result_type.
58*404b540aSrobert   *  @endif
59*404b540aSrobert  */
60*404b540aSrobert  template<typename _Tp>
61*404b540aSrobert    class _Has_result_type_helper : __sfinae_types
62*404b540aSrobert    {
63*404b540aSrobert      template<typename _Up>
64*404b540aSrobert      struct _Wrap_type
65*404b540aSrobert      { };
66*404b540aSrobert
67*404b540aSrobert      template<typename _Up>
68*404b540aSrobert        static __one __test(_Wrap_type<typename _Up::result_type>*);
69*404b540aSrobert
70*404b540aSrobert      template<typename _Up>
71*404b540aSrobert        static __two __test(...);
72*404b540aSrobert
73*404b540aSrobert    public:
74*404b540aSrobert      static const bool value = sizeof(__test<_Tp>(0)) == 1;
75*404b540aSrobert    };
76*404b540aSrobert
77*404b540aSrobert  template<typename _Tp>
78*404b540aSrobert    struct _Has_result_type
79*404b540aSrobert       : integral_constant<
80*404b540aSrobert           bool,
81*404b540aSrobert           _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
82*404b540aSrobert    { };
83*404b540aSrobert
84*404b540aSrobert  /**
85*404b540aSrobert   *  @if maint
86*404b540aSrobert   *  If we have found a result_type, extract it.
87*404b540aSrobert   *  @endif
88*404b540aSrobert  */
89*404b540aSrobert  template<bool _Has_result_type, typename _Functor>
90*404b540aSrobert    struct _Maybe_get_result_type
91*404b540aSrobert    { };
92*404b540aSrobert
93*404b540aSrobert  template<typename _Functor>
94*404b540aSrobert    struct _Maybe_get_result_type<true, _Functor>
95*404b540aSrobert    {
96*404b540aSrobert      typedef typename _Functor::result_type result_type;
97*404b540aSrobert    };
98*404b540aSrobert
99*404b540aSrobert  /**
100*404b540aSrobert   *  @if maint
101*404b540aSrobert   *  Base class for any function object that has a weak result type, as
102*404b540aSrobert   *  defined in 3.3/3 of TR1.
103*404b540aSrobert   *  @endif
104*404b540aSrobert  */
105*404b540aSrobert  template<typename _Functor>
106*404b540aSrobert    struct _Weak_result_type_impl
107*404b540aSrobert    : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
108*404b540aSrobert    {
109*404b540aSrobert    };
110*404b540aSrobert
111*404b540aSrobert  /**
112*404b540aSrobert   *  @if maint
113*404b540aSrobert   *  Strip top-level cv-qualifiers from the function object and let
114*404b540aSrobert   *  _Weak_result_type_impl perform the real work.
115*404b540aSrobert   *  @endif
116*404b540aSrobert  */
117*404b540aSrobert  template<typename _Functor>
118*404b540aSrobert    struct _Weak_result_type
119*404b540aSrobert    : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
120*404b540aSrobert    {
121*404b540aSrobert    };
122*404b540aSrobert
123*404b540aSrobert  template<typename _Signature>
124*404b540aSrobert    class result_of;
125*404b540aSrobert
126*404b540aSrobert  /**
127*404b540aSrobert   *  @if maint
128*404b540aSrobert   *  Actual implementation of result_of. When _Has_result_type is
129*404b540aSrobert   *  true, gets its result from _Weak_result_type. Otherwise, uses
130*404b540aSrobert   *  the function object's member template result to extract the
131*404b540aSrobert   *  result type.
132*404b540aSrobert   *  @endif
133*404b540aSrobert  */
134*404b540aSrobert  template<bool _Has_result_type, typename _Signature>
135*404b540aSrobert    struct _Result_of_impl;
136*404b540aSrobert
137*404b540aSrobert  // Handle member data pointers using _Mem_fn's logic
138*404b540aSrobert  template<typename _Res, typename _Class, typename _T1>
139*404b540aSrobert    struct _Result_of_impl<false, _Res _Class::*(_T1)>
140*404b540aSrobert    {
141*404b540aSrobert      typedef typename _Mem_fn<_Res _Class::*>
142*404b540aSrobert                ::template _Result_type<_T1>::type type;
143*404b540aSrobert    };
144*404b540aSrobert
145*404b540aSrobert  /**
146*404b540aSrobert   *  @if maint
147*404b540aSrobert   *  Determines if the type _Tp derives from unary_function.
148*404b540aSrobert   *  @endif
149*404b540aSrobert  */
150*404b540aSrobert  template<typename _Tp>
151*404b540aSrobert    struct _Derives_from_unary_function : __sfinae_types
152*404b540aSrobert    {
153*404b540aSrobert    private:
154*404b540aSrobert      template<typename _T1, typename _Res>
155*404b540aSrobert        static __one __test(const volatile unary_function<_T1, _Res>*);
156*404b540aSrobert
157*404b540aSrobert      // It's tempting to change "..." to const volatile void*, but
158*404b540aSrobert      // that fails when _Tp is a function type.
159*404b540aSrobert      static __two __test(...);
160*404b540aSrobert
161*404b540aSrobert    public:
162*404b540aSrobert      static const bool value = sizeof(__test((_Tp*)0)) == 1;
163*404b540aSrobert    };
164*404b540aSrobert
165*404b540aSrobert  /**
166*404b540aSrobert   *  @if maint
167*404b540aSrobert   *  Determines if the type _Tp derives from binary_function.
168*404b540aSrobert   *  @endif
169*404b540aSrobert  */
170*404b540aSrobert  template<typename _Tp>
171*404b540aSrobert    struct _Derives_from_binary_function : __sfinae_types
172*404b540aSrobert    {
173*404b540aSrobert    private:
174*404b540aSrobert      template<typename _T1, typename _T2, typename _Res>
175*404b540aSrobert        static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
176*404b540aSrobert
177*404b540aSrobert      // It's tempting to change "..." to const volatile void*, but
178*404b540aSrobert      // that fails when _Tp is a function type.
179*404b540aSrobert      static __two __test(...);
180*404b540aSrobert
181*404b540aSrobert    public:
182*404b540aSrobert      static const bool value = sizeof(__test((_Tp*)0)) == 1;
183*404b540aSrobert    };
184*404b540aSrobert
185*404b540aSrobert  /**
186*404b540aSrobert   *  @if maint
187*404b540aSrobert   *  Turns a function type into a function pointer type
188*404b540aSrobert   *  @endif
189*404b540aSrobert  */
190*404b540aSrobert  template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
191*404b540aSrobert    struct _Function_to_function_pointer
192*404b540aSrobert    {
193*404b540aSrobert      typedef _Tp type;
194*404b540aSrobert    };
195*404b540aSrobert
196*404b540aSrobert  template<typename _Tp>
197*404b540aSrobert    struct _Function_to_function_pointer<_Tp, true>
198*404b540aSrobert    {
199*404b540aSrobert      typedef _Tp* type;
200*404b540aSrobert    };
201*404b540aSrobert
202*404b540aSrobert  /**
203*404b540aSrobert   *  @if maint
204*404b540aSrobert   *  Knowing which of unary_function and binary_function _Tp derives
205*404b540aSrobert   *  from, derives from the same and ensures that reference_wrapper
206*404b540aSrobert   *  will have a weak result type. See cases below.
207*404b540aSrobert   *  @endif
208*404b540aSrobert   */
209*404b540aSrobert  template<bool _Unary, bool _Binary, typename _Tp>
210*404b540aSrobert    struct _Reference_wrapper_base_impl;
211*404b540aSrobert
212*404b540aSrobert  // Not a unary_function or binary_function, so try a weak result type
213*404b540aSrobert  template<typename _Tp>
214*404b540aSrobert    struct _Reference_wrapper_base_impl<false, false, _Tp>
215*404b540aSrobert    : _Weak_result_type<_Tp>
216*404b540aSrobert    { };
217*404b540aSrobert
218*404b540aSrobert  // unary_function but not binary_function
219*404b540aSrobert  template<typename _Tp>
220*404b540aSrobert    struct _Reference_wrapper_base_impl<true, false, _Tp>
221*404b540aSrobert    : unary_function<typename _Tp::argument_type,
222*404b540aSrobert		     typename _Tp::result_type>
223*404b540aSrobert    { };
224*404b540aSrobert
225*404b540aSrobert  // binary_function but not unary_function
226*404b540aSrobert  template<typename _Tp>
227*404b540aSrobert    struct _Reference_wrapper_base_impl<false, true, _Tp>
228*404b540aSrobert    : binary_function<typename _Tp::first_argument_type,
229*404b540aSrobert		      typename _Tp::second_argument_type,
230*404b540aSrobert		      typename _Tp::result_type>
231*404b540aSrobert    { };
232*404b540aSrobert
233*404b540aSrobert  // both unary_function and binary_function. import result_type to
234*404b540aSrobert  // avoid conflicts.
235*404b540aSrobert   template<typename _Tp>
236*404b540aSrobert    struct _Reference_wrapper_base_impl<true, true, _Tp>
237*404b540aSrobert    : unary_function<typename _Tp::argument_type,
238*404b540aSrobert		     typename _Tp::result_type>,
239*404b540aSrobert      binary_function<typename _Tp::first_argument_type,
240*404b540aSrobert		      typename _Tp::second_argument_type,
241*404b540aSrobert		      typename _Tp::result_type>
242*404b540aSrobert    {
243*404b540aSrobert      typedef typename _Tp::result_type result_type;
244*404b540aSrobert    };
245*404b540aSrobert
246*404b540aSrobert  /**
247*404b540aSrobert   *  @if maint
248*404b540aSrobert   *  Derives from unary_function or binary_function when it
249*404b540aSrobert   *  can. Specializations handle all of the easy cases. The primary
250*404b540aSrobert   *  template determines what to do with a class type, which may
251*404b540aSrobert   *  derive from both unary_function and binary_function.
252*404b540aSrobert   *  @endif
253*404b540aSrobert  */
254*404b540aSrobert  template<typename _Tp>
255*404b540aSrobert    struct _Reference_wrapper_base
256*404b540aSrobert    : _Reference_wrapper_base_impl<
257*404b540aSrobert      _Derives_from_unary_function<_Tp>::value,
258*404b540aSrobert      _Derives_from_binary_function<_Tp>::value,
259*404b540aSrobert      _Tp>
260*404b540aSrobert    { };
261*404b540aSrobert
262*404b540aSrobert  // - a function type (unary)
263*404b540aSrobert  template<typename _Res, typename _T1>
264*404b540aSrobert    struct _Reference_wrapper_base<_Res(_T1)>
265*404b540aSrobert    : unary_function<_T1, _Res>
266*404b540aSrobert    { };
267*404b540aSrobert
268*404b540aSrobert  // - a function type (binary)
269*404b540aSrobert  template<typename _Res, typename _T1, typename _T2>
270*404b540aSrobert    struct _Reference_wrapper_base<_Res(_T1, _T2)>
271*404b540aSrobert    : binary_function<_T1, _T2, _Res>
272*404b540aSrobert    { };
273*404b540aSrobert
274*404b540aSrobert  // - a function pointer type (unary)
275*404b540aSrobert  template<typename _Res, typename _T1>
276*404b540aSrobert    struct _Reference_wrapper_base<_Res(*)(_T1)>
277*404b540aSrobert    : unary_function<_T1, _Res>
278*404b540aSrobert    { };
279*404b540aSrobert
280*404b540aSrobert  // - a function pointer type (binary)
281*404b540aSrobert  template<typename _Res, typename _T1, typename _T2>
282*404b540aSrobert    struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
283*404b540aSrobert    : binary_function<_T1, _T2, _Res>
284*404b540aSrobert    { };
285*404b540aSrobert
286*404b540aSrobert  // - a pointer to member function type (unary, no qualifiers)
287*404b540aSrobert  template<typename _Res, typename _T1>
288*404b540aSrobert    struct _Reference_wrapper_base<_Res (_T1::*)()>
289*404b540aSrobert    : unary_function<_T1*, _Res>
290*404b540aSrobert    { };
291*404b540aSrobert
292*404b540aSrobert  // - a pointer to member function type (binary, no qualifiers)
293*404b540aSrobert  template<typename _Res, typename _T1, typename _T2>
294*404b540aSrobert    struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
295*404b540aSrobert    : binary_function<_T1*, _T2, _Res>
296*404b540aSrobert    { };
297*404b540aSrobert
298*404b540aSrobert  // - a pointer to member function type (unary, const)
299*404b540aSrobert  template<typename _Res, typename _T1>
300*404b540aSrobert    struct _Reference_wrapper_base<_Res (_T1::*)() const>
301*404b540aSrobert    : unary_function<const _T1*, _Res>
302*404b540aSrobert    { };
303*404b540aSrobert
304*404b540aSrobert  // - a pointer to member function type (binary, const)
305*404b540aSrobert  template<typename _Res, typename _T1, typename _T2>
306*404b540aSrobert    struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
307*404b540aSrobert    : binary_function<const _T1*, _T2, _Res>
308*404b540aSrobert    { };
309*404b540aSrobert
310*404b540aSrobert  // - a pointer to member function type (unary, volatile)
311*404b540aSrobert  template<typename _Res, typename _T1>
312*404b540aSrobert    struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
313*404b540aSrobert    : unary_function<volatile _T1*, _Res>
314*404b540aSrobert    { };
315*404b540aSrobert
316*404b540aSrobert  // - a pointer to member function type (binary, volatile)
317*404b540aSrobert  template<typename _Res, typename _T1, typename _T2>
318*404b540aSrobert    struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
319*404b540aSrobert    : binary_function<volatile _T1*, _T2, _Res>
320*404b540aSrobert    { };
321*404b540aSrobert
322*404b540aSrobert  // - a pointer to member function type (unary, const volatile)
323*404b540aSrobert  template<typename _Res, typename _T1>
324*404b540aSrobert    struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
325*404b540aSrobert    : unary_function<const volatile _T1*, _Res>
326*404b540aSrobert    { };
327*404b540aSrobert
328*404b540aSrobert  // - a pointer to member function type (binary, const volatile)
329*404b540aSrobert  template<typename _Res, typename _T1, typename _T2>
330*404b540aSrobert    struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
331*404b540aSrobert    : binary_function<const volatile _T1*, _T2, _Res>
332*404b540aSrobert    { };
333*404b540aSrobert
334*404b540aSrobert  template<typename _Tp>
335*404b540aSrobert    class reference_wrapper
336*404b540aSrobert      : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
337*404b540aSrobert    {
338*404b540aSrobert      // If _Tp is a function type, we can't form result_of<_Tp(...)>,
339*404b540aSrobert      // so turn it into a function pointer type.
340*404b540aSrobert      typedef typename _Function_to_function_pointer<_Tp>::type
341*404b540aSrobert        _M_func_type;
342*404b540aSrobert
343*404b540aSrobert      _Tp* _M_data;
344*404b540aSrobert    public:
345*404b540aSrobert      typedef _Tp type;
346*404b540aSrobert      explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
347*404b540aSrobert      { }
348*404b540aSrobert
349*404b540aSrobert      reference_wrapper(const reference_wrapper<_Tp>& __inref):
350*404b540aSrobert      _M_data(__inref._M_data)
351*404b540aSrobert      { }
352*404b540aSrobert
353*404b540aSrobert      reference_wrapper&
354*404b540aSrobert      operator=(const reference_wrapper<_Tp>& __inref)
355*404b540aSrobert      {
356*404b540aSrobert        _M_data = __inref._M_data;
357*404b540aSrobert        return *this;
358*404b540aSrobert      }
359*404b540aSrobert
360*404b540aSrobert      operator _Tp&() const
361*404b540aSrobert      { return this->get(); }
362*404b540aSrobert
363*404b540aSrobert      _Tp&
364*404b540aSrobert      get() const
365*404b540aSrobert      { return *_M_data; }
366*404b540aSrobert
367*404b540aSrobert#define _GLIBCXX_REPEAT_HEADER <tr1/ref_wrap_iterate.h>
368*404b540aSrobert#include <tr1/repeat.h>
369*404b540aSrobert#undef _GLIBCXX_REPEAT_HEADER
370*404b540aSrobert    };
371*404b540aSrobert
372*404b540aSrobert
373*404b540aSrobert  // Denotes a reference should be taken to a variable.
374*404b540aSrobert  template<typename _Tp>
375*404b540aSrobert    inline reference_wrapper<_Tp>
376*404b540aSrobert    ref(_Tp& __t)
377*404b540aSrobert    { return reference_wrapper<_Tp>(__t); }
378*404b540aSrobert
379*404b540aSrobert  // Denotes a const reference should be taken to a variable.
380*404b540aSrobert  template<typename _Tp>
381*404b540aSrobert    inline reference_wrapper<const _Tp>
382*404b540aSrobert    cref(const _Tp& __t)
383*404b540aSrobert    { return reference_wrapper<const _Tp>(__t); }
384*404b540aSrobert
385*404b540aSrobert  template<typename _Tp>
386*404b540aSrobert    inline reference_wrapper<_Tp>
387*404b540aSrobert    ref(reference_wrapper<_Tp> __t)
388*404b540aSrobert    { return ref(__t.get()); }
389*404b540aSrobert
390*404b540aSrobert  template<typename _Tp>
391*404b540aSrobert    inline reference_wrapper<const _Tp>
392*404b540aSrobert    cref(reference_wrapper<_Tp> __t)
393*404b540aSrobert    { return cref(__t.get()); }
394*404b540aSrobert
395*404b540aSrobert   template<typename _Tp, bool>
396*404b540aSrobert     struct _Mem_fn_const_or_non
397*404b540aSrobert     {
398*404b540aSrobert       typedef const _Tp& type;
399*404b540aSrobert     };
400*404b540aSrobert
401*404b540aSrobert    template<typename _Tp>
402*404b540aSrobert      struct _Mem_fn_const_or_non<_Tp, false>
403*404b540aSrobert      {
404*404b540aSrobert        typedef _Tp& type;
405*404b540aSrobert      };
406*404b540aSrobert
407*404b540aSrobert  template<typename _Res, typename _Class>
408*404b540aSrobert  class _Mem_fn<_Res _Class::*>
409*404b540aSrobert  {
410*404b540aSrobert    // This bit of genius is due to Peter Dimov, improved slightly by
411*404b540aSrobert    // Douglas Gregor.
412*404b540aSrobert    template<typename _Tp>
413*404b540aSrobert      _Res&
414*404b540aSrobert      _M_call(_Tp& __object, _Class *) const
415*404b540aSrobert      { return __object.*__pm; }
416*404b540aSrobert
417*404b540aSrobert    template<typename _Tp, typename _Up>
418*404b540aSrobert      _Res&
419*404b540aSrobert      _M_call(_Tp& __object, _Up * const *) const
420*404b540aSrobert      { return (*__object).*__pm; }
421*404b540aSrobert
422*404b540aSrobert    template<typename _Tp, typename _Up>
423*404b540aSrobert      const _Res&
424*404b540aSrobert      _M_call(_Tp& __object, const _Up * const *) const
425*404b540aSrobert      { return (*__object).*__pm; }
426*404b540aSrobert
427*404b540aSrobert    template<typename _Tp>
428*404b540aSrobert      const _Res&
429*404b540aSrobert      _M_call(_Tp& __object, const _Class *) const
430*404b540aSrobert      { return __object.*__pm; }
431*404b540aSrobert
432*404b540aSrobert    template<typename _Tp>
433*404b540aSrobert      const _Res&
434*404b540aSrobert      _M_call(_Tp& __ptr, const volatile void*) const
435*404b540aSrobert      { return (*__ptr).*__pm; }
436*404b540aSrobert
437*404b540aSrobert    template<typename _Tp> static _Tp& __get_ref();
438*404b540aSrobert
439*404b540aSrobert    template<typename _Tp>
440*404b540aSrobert      static __sfinae_types::__one __check_const(_Tp&, _Class*);
441*404b540aSrobert    template<typename _Tp, typename _Up>
442*404b540aSrobert      static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
443*404b540aSrobert    template<typename _Tp, typename _Up>
444*404b540aSrobert      static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
445*404b540aSrobert    template<typename _Tp>
446*404b540aSrobert      static __sfinae_types::__two __check_const(_Tp&, const _Class*);
447*404b540aSrobert    template<typename _Tp>
448*404b540aSrobert      static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
449*404b540aSrobert
450*404b540aSrobert  public:
451*404b540aSrobert    template<typename _Tp>
452*404b540aSrobert      struct _Result_type
453*404b540aSrobert      : _Mem_fn_const_or_non<
454*404b540aSrobert        _Res,
455*404b540aSrobert        (sizeof(__sfinae_types::__two)
456*404b540aSrobert	 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
457*404b540aSrobert      { };
458*404b540aSrobert
459*404b540aSrobert    template<typename _Signature>
460*404b540aSrobert      struct result;
461*404b540aSrobert
462*404b540aSrobert    template<typename _CVMem, typename _Tp>
463*404b540aSrobert      struct result<_CVMem(_Tp)>
464*404b540aSrobert      : public _Result_type<_Tp> { };
465*404b540aSrobert
466*404b540aSrobert    template<typename _CVMem, typename _Tp>
467*404b540aSrobert      struct result<_CVMem(_Tp&)>
468*404b540aSrobert      : public _Result_type<_Tp> { };
469*404b540aSrobert
470*404b540aSrobert    explicit _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
471*404b540aSrobert
472*404b540aSrobert    // Handle objects
473*404b540aSrobert    _Res&       operator()(_Class& __object)       const
474*404b540aSrobert    { return __object.*__pm; }
475*404b540aSrobert
476*404b540aSrobert    const _Res& operator()(const _Class& __object) const
477*404b540aSrobert    { return __object.*__pm; }
478*404b540aSrobert
479*404b540aSrobert    // Handle pointers
480*404b540aSrobert    _Res&       operator()(_Class* __object)       const
481*404b540aSrobert    { return __object->*__pm; }
482*404b540aSrobert
483*404b540aSrobert    const _Res&
484*404b540aSrobert    operator()(const _Class* __object) const
485*404b540aSrobert    { return __object->*__pm; }
486*404b540aSrobert
487*404b540aSrobert    // Handle smart pointers and derived
488*404b540aSrobert    template<typename _Tp>
489*404b540aSrobert      typename _Result_type<_Tp>::type
490*404b540aSrobert      operator()(_Tp& __unknown) const
491*404b540aSrobert      { return _M_call(__unknown, &__unknown); }
492*404b540aSrobert
493*404b540aSrobert  private:
494*404b540aSrobert    _Res _Class::*__pm;
495*404b540aSrobert  };
496*404b540aSrobert
497*404b540aSrobert  /**
498*404b540aSrobert   *  @brief Returns a function object that forwards to the member
499*404b540aSrobert   *  pointer @a pm.
500*404b540aSrobert   */
501*404b540aSrobert  template<typename _Tp, typename _Class>
502*404b540aSrobert    inline _Mem_fn<_Tp _Class::*>
503*404b540aSrobert    mem_fn(_Tp _Class::* __pm)
504*404b540aSrobert    {
505*404b540aSrobert      return _Mem_fn<_Tp _Class::*>(__pm);
506*404b540aSrobert    }
507*404b540aSrobert
508*404b540aSrobert  /**
509*404b540aSrobert   *  @brief Determines if the given type _Tp is a function object
510*404b540aSrobert   *  should be treated as a subexpression when evaluating calls to
511*404b540aSrobert   *  function objects returned by bind(). [TR1 3.6.1]
512*404b540aSrobert   */
513*404b540aSrobert  template<typename _Tp>
514*404b540aSrobert    struct is_bind_expression
515*404b540aSrobert    { static const bool value = false; };
516*404b540aSrobert
517*404b540aSrobert  template<typename _Tp>
518*404b540aSrobert    const bool is_bind_expression<_Tp>::value;
519*404b540aSrobert
520*404b540aSrobert  /**
521*404b540aSrobert   *  @brief Determines if the given type _Tp is a placeholder in a
522*404b540aSrobert   *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
523*404b540aSrobert   */
524*404b540aSrobert  template<typename _Tp>
525*404b540aSrobert    struct is_placeholder
526*404b540aSrobert    { static const int value = 0; };
527*404b540aSrobert
528*404b540aSrobert  template<typename _Tp>
529*404b540aSrobert    const int is_placeholder<_Tp>::value;
530*404b540aSrobert
531*404b540aSrobert  /**
532*404b540aSrobert   *  @if maint
533*404b540aSrobert   *  The type of placeholder objects defined by libstdc++.
534*404b540aSrobert   *  @endif
535*404b540aSrobert   */
536*404b540aSrobert  template<int _Num> struct _Placeholder { };
537*404b540aSrobert
538*404b540aSrobert  /**
539*404b540aSrobert   *  @if maint
540*404b540aSrobert   *  Partial specialization of is_placeholder that provides the placeholder
541*404b540aSrobert   *  number for the placeholder objects defined by libstdc++.
542*404b540aSrobert   *  @endif
543*404b540aSrobert   */
544*404b540aSrobert  template<int _Num>
545*404b540aSrobert    struct is_placeholder<_Placeholder<_Num> >
546*404b540aSrobert    { static const int value = _Num; };
547*404b540aSrobert
548*404b540aSrobert  template<int _Num>
549*404b540aSrobert    const int is_placeholder<_Placeholder<_Num> >::value;
550*404b540aSrobert
551*404b540aSrobert  /**
552*404b540aSrobert   *  @if maint
553*404b540aSrobert   *  Maps an argument to bind() into an actual argument to the bound
554*404b540aSrobert   *  function object [TR1 3.6.3/5]. Only the first parameter should
555*404b540aSrobert   *  be specified: the rest are used to determine among the various
556*404b540aSrobert   *  implementations. Note that, although this class is a function
557*404b540aSrobert   *  object, isn't not entirely normal because it takes only two
558*404b540aSrobert   *  parameters regardless of the number of parameters passed to the
559*404b540aSrobert   *  bind expression. The first parameter is the bound argument and
560*404b540aSrobert   *  the second parameter is a tuple containing references to the
561*404b540aSrobert   *  rest of the arguments.
562*404b540aSrobert   *  @endif
563*404b540aSrobert   */
564*404b540aSrobert  template<typename _Arg,
565*404b540aSrobert           bool _IsBindExp = is_bind_expression<_Arg>::value,
566*404b540aSrobert           bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
567*404b540aSrobert    class _Mu;
568*404b540aSrobert
569*404b540aSrobert  /**
570*404b540aSrobert   *  @if maint
571*404b540aSrobert   *  If the argument is reference_wrapper<_Tp>, returns the
572*404b540aSrobert   *  underlying reference. [TR1 3.6.3/5 bullet 1]
573*404b540aSrobert   *  @endif
574*404b540aSrobert   */
575*404b540aSrobert  template<typename _Tp>
576*404b540aSrobert    class _Mu<reference_wrapper<_Tp>, false, false>
577*404b540aSrobert    {
578*404b540aSrobert    public:
579*404b540aSrobert      typedef _Tp& result_type;
580*404b540aSrobert
581*404b540aSrobert      /* Note: This won't actually work for const volatile
582*404b540aSrobert       * reference_wrappers, because reference_wrapper::get() is const
583*404b540aSrobert       * but not volatile-qualified. This might be a defect in the TR.
584*404b540aSrobert       */
585*404b540aSrobert      template<typename _CVRef, typename _Tuple>
586*404b540aSrobert      result_type
587*404b540aSrobert      operator()(_CVRef& __arg, const _Tuple&) const volatile
588*404b540aSrobert      { return __arg.get(); }
589*404b540aSrobert    };
590*404b540aSrobert
591*404b540aSrobert  /**
592*404b540aSrobert   *  @if maint
593*404b540aSrobert   *  If the argument is a bind expression, we invoke the underlying
594*404b540aSrobert   *  function object with the same cv-qualifiers as we are given and
595*404b540aSrobert   *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
596*404b540aSrobert   *  @endif
597*404b540aSrobert   */
598*404b540aSrobert  template<typename _Arg>
599*404b540aSrobert    class _Mu<_Arg, true, false>
600*404b540aSrobert    {
601*404b540aSrobert    public:
602*404b540aSrobert      template<typename _Signature> class result;
603*404b540aSrobert
604*404b540aSrobert#define _GLIBCXX_REPEAT_HEADER <tr1/mu_iterate.h>
605*404b540aSrobert#  include <tr1/repeat.h>
606*404b540aSrobert#undef _GLIBCXX_REPEAT_HEADER
607*404b540aSrobert    };
608*404b540aSrobert
609*404b540aSrobert  /**
610*404b540aSrobert   *  @if maint
611*404b540aSrobert   *  If the argument is a placeholder for the Nth argument, returns
612*404b540aSrobert   *  a reference to the Nth argument to the bind function object.
613*404b540aSrobert   *  [TR1 3.6.3/5 bullet 3]
614*404b540aSrobert   *  @endif
615*404b540aSrobert   */
616*404b540aSrobert  template<typename _Arg>
617*404b540aSrobert    class _Mu<_Arg, false, true>
618*404b540aSrobert    {
619*404b540aSrobert    public:
620*404b540aSrobert      template<typename _Signature> class result;
621*404b540aSrobert
622*404b540aSrobert      template<typename _CVMu, typename _CVArg, typename _Tuple>
623*404b540aSrobert      class result<_CVMu(_CVArg, _Tuple)>
624*404b540aSrobert      {
625*404b540aSrobert        // Add a reference, if it hasn't already been done for us.
626*404b540aSrobert        // This allows us to be a little bit sloppy in constructing
627*404b540aSrobert        // the tuple that we pass to result_of<...>.
628*404b540aSrobert        typedef typename tuple_element<(is_placeholder<_Arg>::value - 1),
629*404b540aSrobert                                       _Tuple>::type __base_type;
630*404b540aSrobert
631*404b540aSrobert      public:
632*404b540aSrobert        typedef typename add_reference<__base_type>::type type;
633*404b540aSrobert      };
634*404b540aSrobert
635*404b540aSrobert      template<typename _Tuple>
636*404b540aSrobert      typename result<_Mu(_Arg, _Tuple)>::type
637*404b540aSrobert      operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
638*404b540aSrobert      {
639*404b540aSrobert        return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
640*404b540aSrobert      }
641*404b540aSrobert    };
642*404b540aSrobert
643*404b540aSrobert  /**
644*404b540aSrobert   *  @if maint
645*404b540aSrobert   *  If the argument is just a value, returns a reference to that
646*404b540aSrobert   *  value. The cv-qualifiers on the reference are the same as the
647*404b540aSrobert   *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
648*404b540aSrobert   *  @endif
649*404b540aSrobert   */
650*404b540aSrobert  template<typename _Arg>
651*404b540aSrobert    class _Mu<_Arg, false, false>
652*404b540aSrobert    {
653*404b540aSrobert    public:
654*404b540aSrobert      template<typename _Signature> struct result;
655*404b540aSrobert
656*404b540aSrobert      template<typename _CVMu, typename _CVArg, typename _Tuple>
657*404b540aSrobert      struct result<_CVMu(_CVArg, _Tuple)>
658*404b540aSrobert      {
659*404b540aSrobert        typedef typename add_reference<_CVArg>::type type;
660*404b540aSrobert      };
661*404b540aSrobert
662*404b540aSrobert      // Pick up the cv-qualifiers of the argument
663*404b540aSrobert      template<typename _CVArg, typename _Tuple>
664*404b540aSrobert      _CVArg& operator()(_CVArg& __arg, const _Tuple&) const volatile
665*404b540aSrobert      { return __arg; }
666*404b540aSrobert    };
667*404b540aSrobert
668*404b540aSrobert  /**
669*404b540aSrobert   *  @if maint
670*404b540aSrobert   *  Maps member pointers into instances of _Mem_fn but leaves all
671*404b540aSrobert   *  other function objects untouched. Used by tr1::bind(). The
672*404b540aSrobert   *  primary template handles the non--member-pointer case.
673*404b540aSrobert   *  @endif
674*404b540aSrobert   */
675*404b540aSrobert  template<typename _Tp>
676*404b540aSrobert    struct _Maybe_wrap_member_pointer
677*404b540aSrobert    {
678*404b540aSrobert      typedef _Tp type;
679*404b540aSrobert      static const _Tp& __do_wrap(const _Tp& __x) { return __x; }
680*404b540aSrobert    };
681*404b540aSrobert
682*404b540aSrobert  /**
683*404b540aSrobert   *  @if maint
684*404b540aSrobert   *  Maps member pointers into instances of _Mem_fn but leaves all
685*404b540aSrobert   *  other function objects untouched. Used by tr1::bind(). This
686*404b540aSrobert   *  partial specialization handles the member pointer case.
687*404b540aSrobert   *  @endif
688*404b540aSrobert   */
689*404b540aSrobert  template<typename _Tp, typename _Class>
690*404b540aSrobert    struct _Maybe_wrap_member_pointer<_Tp _Class::*>
691*404b540aSrobert    {
692*404b540aSrobert      typedef _Mem_fn<_Tp _Class::*> type;
693*404b540aSrobert      static type __do_wrap(_Tp _Class::* __pm) { return type(__pm); }
694*404b540aSrobert    };
695*404b540aSrobert
696*404b540aSrobert  /**
697*404b540aSrobert   *  @if maint
698*404b540aSrobert   *  Type of the function object returned from bind().
699*404b540aSrobert   *  @endif
700*404b540aSrobert   */
701*404b540aSrobert   template<typename _Signature>
702*404b540aSrobert     struct _Bind;
703*404b540aSrobert
704*404b540aSrobert  /**
705*404b540aSrobert   *  @if maint
706*404b540aSrobert   *  Type of the function object returned from bind<R>().
707*404b540aSrobert   *  @endif
708*404b540aSrobert   */
709*404b540aSrobert   template<typename _Result, typename _Signature>
710*404b540aSrobert     struct _Bind_result;
711*404b540aSrobert
712*404b540aSrobert  /**
713*404b540aSrobert   *  @if maint
714*404b540aSrobert   *  Class template _Bind is always a bind expression.
715*404b540aSrobert   *  @endif
716*404b540aSrobert   */
717*404b540aSrobert   template<typename _Signature>
718*404b540aSrobert     struct is_bind_expression<_Bind<_Signature> >
719*404b540aSrobert     { static const bool value = true; };
720*404b540aSrobert
721*404b540aSrobert   template<typename _Signature>
722*404b540aSrobert     const bool is_bind_expression<_Bind<_Signature> >::value;
723*404b540aSrobert
724*404b540aSrobert  /**
725*404b540aSrobert   *  @if maint
726*404b540aSrobert   *  Class template _Bind_result is always a bind expression.
727*404b540aSrobert   *  @endif
728*404b540aSrobert   */
729*404b540aSrobert   template<typename _Result, typename _Signature>
730*404b540aSrobert     struct is_bind_expression<_Bind_result<_Result, _Signature> >
731*404b540aSrobert     { static const bool value = true; };
732*404b540aSrobert
733*404b540aSrobert   template<typename _Result, typename _Signature>
734*404b540aSrobert     const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
735*404b540aSrobert
736*404b540aSrobert  /**
737*404b540aSrobert   *  @brief Exception class thrown when class template function's
738*404b540aSrobert   *  operator() is called with an empty target.
739*404b540aSrobert   *
740*404b540aSrobert   */
741*404b540aSrobert  class bad_function_call : public std::exception { };
742*404b540aSrobert
743*404b540aSrobert  /**
744*404b540aSrobert   *  @if maint
745*404b540aSrobert   *  The integral constant expression 0 can be converted into a
746*404b540aSrobert   *  pointer to this type. It is used by the function template to
747*404b540aSrobert   *  accept NULL pointers.
748*404b540aSrobert   *  @endif
749*404b540aSrobert   */
750*404b540aSrobert  struct _M_clear_type;
751*404b540aSrobert
752*404b540aSrobert  /**
753*404b540aSrobert   *  @if maint
754*404b540aSrobert   *  Trait identifying "location-invariant" types, meaning that the
755*404b540aSrobert   *  address of the object (or any of its members) will not escape.
756*404b540aSrobert   *  Also implies a trivial copy constructor and assignment operator.
757*404b540aSrobert   *   @endif
758*404b540aSrobert   */
759*404b540aSrobert  template<typename _Tp>
760*404b540aSrobert    struct __is_location_invariant
761*404b540aSrobert    : integral_constant<bool,
762*404b540aSrobert                        (is_pointer<_Tp>::value
763*404b540aSrobert                         || is_member_pointer<_Tp>::value)>
764*404b540aSrobert    {
765*404b540aSrobert    };
766*404b540aSrobert
767*404b540aSrobert  class _Undefined_class;
768*404b540aSrobert
769*404b540aSrobert  union _Nocopy_types
770*404b540aSrobert  {
771*404b540aSrobert    void*       _M_object;
772*404b540aSrobert    const void* _M_const_object;
773*404b540aSrobert    void (*_M_function_pointer)();
774*404b540aSrobert    void (_Undefined_class::*_M_member_pointer)();
775*404b540aSrobert  };
776*404b540aSrobert
777*404b540aSrobert  union _Any_data {
778*404b540aSrobert    void*       _M_access()       { return &_M_pod_data[0]; }
779*404b540aSrobert    const void* _M_access() const { return &_M_pod_data[0]; }
780*404b540aSrobert
781*404b540aSrobert    template<typename _Tp> _Tp& _M_access()
782*404b540aSrobert    { return *static_cast<_Tp*>(_M_access()); }
783*404b540aSrobert
784*404b540aSrobert    template<typename _Tp> const _Tp& _M_access() const
785*404b540aSrobert    { return *static_cast<const _Tp*>(_M_access()); }
786*404b540aSrobert
787*404b540aSrobert    _Nocopy_types _M_unused;
788*404b540aSrobert    char _M_pod_data[sizeof(_Nocopy_types)];
789*404b540aSrobert  };
790*404b540aSrobert
791*404b540aSrobert  enum _Manager_operation
792*404b540aSrobert  {
793*404b540aSrobert    __get_type_info,
794*404b540aSrobert    __get_functor_ptr,
795*404b540aSrobert    __clone_functor,
796*404b540aSrobert    __destroy_functor
797*404b540aSrobert  };
798*404b540aSrobert
799*404b540aSrobert  /* Simple type wrapper that helps avoid annoying const problems
800*404b540aSrobert     when casting between void pointers and pointers-to-pointers. */
801*404b540aSrobert  template<typename _Tp>
802*404b540aSrobert    struct _Simple_type_wrapper
803*404b540aSrobert    {
804*404b540aSrobert      _Simple_type_wrapper(_Tp __value) : __value(__value) { }
805*404b540aSrobert
806*404b540aSrobert      _Tp __value;
807*404b540aSrobert    };
808*404b540aSrobert
809*404b540aSrobert  template<typename _Tp>
810*404b540aSrobert    struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
811*404b540aSrobert      : __is_location_invariant<_Tp>
812*404b540aSrobert    {
813*404b540aSrobert    };
814*404b540aSrobert
815*404b540aSrobert  // Converts a reference to a function object into a callable
816*404b540aSrobert  // function object.
817*404b540aSrobert  template<typename _Functor>
818*404b540aSrobert    inline _Functor& __callable_functor(_Functor& __f) { return __f; }
819*404b540aSrobert
820*404b540aSrobert  template<typename _Member, typename _Class>
821*404b540aSrobert    inline _Mem_fn<_Member _Class::*>
822*404b540aSrobert    __callable_functor(_Member _Class::* &__p)
823*404b540aSrobert    { return mem_fn(__p); }
824*404b540aSrobert
825*404b540aSrobert  template<typename _Member, typename _Class>
826*404b540aSrobert    inline _Mem_fn<_Member _Class::*>
827*404b540aSrobert    __callable_functor(_Member _Class::* const &__p)
828*404b540aSrobert    { return mem_fn(__p); }
829*404b540aSrobert
830*404b540aSrobert  template<typename _Signature, typename _Functor>
831*404b540aSrobert    class _Function_handler;
832*404b540aSrobert
833*404b540aSrobert  template<typename _Signature>
834*404b540aSrobert    class function;
835*404b540aSrobert
836*404b540aSrobert
837*404b540aSrobert  /**
838*404b540aSrobert   *  @if maint
839*404b540aSrobert   *  Base class of all polymorphic function object wrappers.
840*404b540aSrobert   *  @endif
841*404b540aSrobert   */
842*404b540aSrobert  class _Function_base
843*404b540aSrobert  {
844*404b540aSrobert  public:
845*404b540aSrobert    static const std::size_t _M_max_size = sizeof(_Nocopy_types);
846*404b540aSrobert    static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
847*404b540aSrobert
848*404b540aSrobert    template<typename _Functor>
849*404b540aSrobert    class _Base_manager
850*404b540aSrobert    {
851*404b540aSrobert    protected:
852*404b540aSrobert      static const bool __stored_locally =
853*404b540aSrobert        (__is_location_invariant<_Functor>::value
854*404b540aSrobert         && sizeof(_Functor) <= _M_max_size
855*404b540aSrobert         && __alignof__(_Functor) <= _M_max_align
856*404b540aSrobert         && (_M_max_align % __alignof__(_Functor) == 0));
857*404b540aSrobert      typedef integral_constant<bool, __stored_locally> _Local_storage;
858*404b540aSrobert
859*404b540aSrobert      // Retrieve a pointer to the function object
860*404b540aSrobert      static _Functor* _M_get_pointer(const _Any_data& __source)
861*404b540aSrobert      {
862*404b540aSrobert        const _Functor* __ptr =
863*404b540aSrobert          __stored_locally? &__source._M_access<_Functor>()
864*404b540aSrobert          /* have stored a pointer */ : __source._M_access<_Functor*>();
865*404b540aSrobert        return const_cast<_Functor*>(__ptr);
866*404b540aSrobert      }
867*404b540aSrobert
868*404b540aSrobert      // Clone a location-invariant function object that fits within
869*404b540aSrobert      // an _Any_data structure.
870*404b540aSrobert      static void
871*404b540aSrobert      _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
872*404b540aSrobert      {
873*404b540aSrobert        new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
874*404b540aSrobert      }
875*404b540aSrobert
876*404b540aSrobert      // Clone a function object that is not location-invariant or
877*404b540aSrobert      // that cannot fit into an _Any_data structure.
878*404b540aSrobert      static void
879*404b540aSrobert      _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
880*404b540aSrobert      {
881*404b540aSrobert        __dest._M_access<_Functor*>() =
882*404b540aSrobert          new _Functor(*__source._M_access<_Functor*>());
883*404b540aSrobert      }
884*404b540aSrobert
885*404b540aSrobert      // Destroying a location-invariant object may still require
886*404b540aSrobert      // destruction.
887*404b540aSrobert      static void
888*404b540aSrobert      _M_destroy(_Any_data& __victim, true_type)
889*404b540aSrobert      {
890*404b540aSrobert        __victim._M_access<_Functor>().~_Functor();
891*404b540aSrobert      }
892*404b540aSrobert
893*404b540aSrobert      // Destroying an object located on the heap.
894*404b540aSrobert      static void
895*404b540aSrobert      _M_destroy(_Any_data& __victim, false_type)
896*404b540aSrobert      {
897*404b540aSrobert        delete __victim._M_access<_Functor*>();
898*404b540aSrobert      }
899*404b540aSrobert
900*404b540aSrobert    public:
901*404b540aSrobert      static bool
902*404b540aSrobert      _M_manager(_Any_data& __dest, const _Any_data& __source,
903*404b540aSrobert                 _Manager_operation __op)
904*404b540aSrobert      {
905*404b540aSrobert        switch (__op) {
906*404b540aSrobert        case __get_type_info:
907*404b540aSrobert          __dest._M_access<const type_info*>() = &typeid(_Functor);
908*404b540aSrobert          break;
909*404b540aSrobert
910*404b540aSrobert        case __get_functor_ptr:
911*404b540aSrobert          __dest._M_access<_Functor*>() = _M_get_pointer(__source);
912*404b540aSrobert          break;
913*404b540aSrobert
914*404b540aSrobert        case __clone_functor:
915*404b540aSrobert          _M_clone(__dest, __source, _Local_storage());
916*404b540aSrobert          break;
917*404b540aSrobert
918*404b540aSrobert        case __destroy_functor:
919*404b540aSrobert          _M_destroy(__dest, _Local_storage());
920*404b540aSrobert          break;
921*404b540aSrobert        }
922*404b540aSrobert        return false;
923*404b540aSrobert      }
924*404b540aSrobert
925*404b540aSrobert      static void
926*404b540aSrobert      _M_init_functor(_Any_data& __functor, const _Functor& __f)
927*404b540aSrobert      {
928*404b540aSrobert        _M_init_functor(__functor, __f, _Local_storage());
929*404b540aSrobert      }
930*404b540aSrobert
931*404b540aSrobert      template<typename _Signature>
932*404b540aSrobert      static bool
933*404b540aSrobert      _M_not_empty_function(const function<_Signature>& __f)
934*404b540aSrobert      {
935*404b540aSrobert        return __f;
936*404b540aSrobert      }
937*404b540aSrobert
938*404b540aSrobert      template<typename _Tp>
939*404b540aSrobert      static bool
940*404b540aSrobert      _M_not_empty_function(const _Tp*& __fp)
941*404b540aSrobert      {
942*404b540aSrobert        return __fp;
943*404b540aSrobert      }
944*404b540aSrobert
945*404b540aSrobert      template<typename _Class, typename _Tp>
946*404b540aSrobert      static bool
947*404b540aSrobert      _M_not_empty_function(_Tp _Class::* const& __mp)
948*404b540aSrobert      {
949*404b540aSrobert        return __mp;
950*404b540aSrobert      }
951*404b540aSrobert
952*404b540aSrobert      template<typename _Tp>
953*404b540aSrobert      static bool
954*404b540aSrobert      _M_not_empty_function(const _Tp&)
955*404b540aSrobert      {
956*404b540aSrobert        return true;
957*404b540aSrobert      }
958*404b540aSrobert
959*404b540aSrobert    private:
960*404b540aSrobert      static void
961*404b540aSrobert      _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
962*404b540aSrobert      {
963*404b540aSrobert        new (__functor._M_access()) _Functor(__f);
964*404b540aSrobert      }
965*404b540aSrobert
966*404b540aSrobert      static void
967*404b540aSrobert      _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
968*404b540aSrobert      {
969*404b540aSrobert        __functor._M_access<_Functor*>() = new _Functor(__f);
970*404b540aSrobert      }
971*404b540aSrobert    };
972*404b540aSrobert
973*404b540aSrobert    template<typename _Functor>
974*404b540aSrobert    class _Ref_manager : public _Base_manager<_Functor*>
975*404b540aSrobert    {
976*404b540aSrobert      typedef _Function_base::_Base_manager<_Functor*> _Base;
977*404b540aSrobert
978*404b540aSrobert    public:
979*404b540aSrobert      static bool
980*404b540aSrobert      _M_manager(_Any_data& __dest, const _Any_data& __source,
981*404b540aSrobert                 _Manager_operation __op)
982*404b540aSrobert      {
983*404b540aSrobert        switch (__op) {
984*404b540aSrobert        case __get_type_info:
985*404b540aSrobert          __dest._M_access<const type_info*>() = &typeid(_Functor);
986*404b540aSrobert          break;
987*404b540aSrobert
988*404b540aSrobert        case __get_functor_ptr:
989*404b540aSrobert          __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
990*404b540aSrobert          return is_const<_Functor>::value;
991*404b540aSrobert          break;
992*404b540aSrobert
993*404b540aSrobert        default:
994*404b540aSrobert          _Base::_M_manager(__dest, __source, __op);
995*404b540aSrobert        }
996*404b540aSrobert        return false;
997*404b540aSrobert      }
998*404b540aSrobert
999*404b540aSrobert      static void
1000*404b540aSrobert      _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1001*404b540aSrobert      {
1002*404b540aSrobert        // TBD: Use address_of function instead
1003*404b540aSrobert        _Base::_M_init_functor(__functor, &__f.get());
1004*404b540aSrobert      }
1005*404b540aSrobert    };
1006*404b540aSrobert
1007*404b540aSrobert    _Function_base() : _M_manager(0) { }
1008*404b540aSrobert
1009*404b540aSrobert    ~_Function_base()
1010*404b540aSrobert    {
1011*404b540aSrobert      if (_M_manager)
1012*404b540aSrobert        {
1013*404b540aSrobert          _M_manager(_M_functor, _M_functor, __destroy_functor);
1014*404b540aSrobert        }
1015*404b540aSrobert    }
1016*404b540aSrobert
1017*404b540aSrobert
1018*404b540aSrobert    bool _M_empty() const { return !_M_manager; }
1019*404b540aSrobert
1020*404b540aSrobert    typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1021*404b540aSrobert                                  _Manager_operation);
1022*404b540aSrobert
1023*404b540aSrobert    _Any_data     _M_functor;
1024*404b540aSrobert    _Manager_type _M_manager;
1025*404b540aSrobert  };
1026*404b540aSrobert
1027*404b540aSrobert  // [3.7.2.7] null pointer comparisons
1028*404b540aSrobert
1029*404b540aSrobert  /**
1030*404b540aSrobert   *  @brief Compares a polymorphic function object wrapper against 0
1031*404b540aSrobert   *  (the NULL pointer).
1032*404b540aSrobert   *  @returns @c true if the wrapper has no target, @c false otherwise
1033*404b540aSrobert   *
1034*404b540aSrobert   *  This function will not throw an exception.
1035*404b540aSrobert   */
1036*404b540aSrobert  template<typename _Signature>
1037*404b540aSrobert    inline bool
1038*404b540aSrobert    operator==(const function<_Signature>& __f, _M_clear_type*)
1039*404b540aSrobert    {
1040*404b540aSrobert      return !__f;
1041*404b540aSrobert    }
1042*404b540aSrobert
1043*404b540aSrobert  /**
1044*404b540aSrobert   *  @overload
1045*404b540aSrobert   */
1046*404b540aSrobert  template<typename _Signature>
1047*404b540aSrobert    inline bool
1048*404b540aSrobert    operator==(_M_clear_type*, const function<_Signature>& __f)
1049*404b540aSrobert    {
1050*404b540aSrobert      return !__f;
1051*404b540aSrobert    }
1052*404b540aSrobert
1053*404b540aSrobert  /**
1054*404b540aSrobert   *  @brief Compares a polymorphic function object wrapper against 0
1055*404b540aSrobert   *  (the NULL pointer).
1056*404b540aSrobert   *  @returns @c false if the wrapper has no target, @c true otherwise
1057*404b540aSrobert   *
1058*404b540aSrobert   *  This function will not throw an exception.
1059*404b540aSrobert   */
1060*404b540aSrobert  template<typename _Signature>
1061*404b540aSrobert    inline bool
1062*404b540aSrobert    operator!=(const function<_Signature>& __f, _M_clear_type*)
1063*404b540aSrobert    {
1064*404b540aSrobert      return __f;
1065*404b540aSrobert    }
1066*404b540aSrobert
1067*404b540aSrobert  /**
1068*404b540aSrobert   *  @overload
1069*404b540aSrobert   */
1070*404b540aSrobert  template<typename _Signature>
1071*404b540aSrobert    inline bool
1072*404b540aSrobert    operator!=(_M_clear_type*, const function<_Signature>& __f)
1073*404b540aSrobert    {
1074*404b540aSrobert      return __f;
1075*404b540aSrobert    }
1076*404b540aSrobert
1077*404b540aSrobert  // [3.7.2.8] specialized algorithms
1078*404b540aSrobert
1079*404b540aSrobert  /**
1080*404b540aSrobert   *  @brief Swap the targets of two polymorphic function object wrappers.
1081*404b540aSrobert   *
1082*404b540aSrobert   *  This function will not throw an exception.
1083*404b540aSrobert   */
1084*404b540aSrobert  template<typename _Signature>
1085*404b540aSrobert    inline void
1086*404b540aSrobert    swap(function<_Signature>& __x, function<_Signature>& __y)
1087*404b540aSrobert    {
1088*404b540aSrobert      __x.swap(__y);
1089*404b540aSrobert    }
1090*404b540aSrobert
1091*404b540aSrobert_GLIBCXX_END_NAMESPACE
1092*404b540aSrobert}
1093*404b540aSrobert
1094*404b540aSrobert#define _GLIBCXX_JOIN(X,Y) _GLIBCXX_JOIN2( X , Y )
1095*404b540aSrobert#define _GLIBCXX_JOIN2(X,Y) _GLIBCXX_JOIN3(X,Y)
1096*404b540aSrobert#define _GLIBCXX_JOIN3(X,Y) X##Y
1097*404b540aSrobert#define _GLIBCXX_REPEAT_HEADER <tr1/functional_iterate.h>
1098*404b540aSrobert#include <tr1/repeat.h>
1099*404b540aSrobert#undef _GLIBCXX_REPEAT_HEADER
1100*404b540aSrobert#undef _GLIBCXX_JOIN3
1101*404b540aSrobert#undef _GLIBCXX_JOIN2
1102*404b540aSrobert#undef _GLIBCXX_JOIN
1103*404b540aSrobert
1104*404b540aSrobert#include <tr1/functional_hash.h>
1105*404b540aSrobert
1106*404b540aSrobert#endif
1107