1169691Skan// TR1 functional header -*- C++ -*-
2169691Skan
3169691Skan// Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4169691Skan//
5169691Skan// This file is part of the GNU ISO C++ Library.  This library is free
6169691Skan// software; you can redistribute it and/or modify it under the
7169691Skan// terms of the GNU General Public License as published by the
8169691Skan// Free Software Foundation; either version 2, or (at your option)
9169691Skan// any later version.
10169691Skan
11169691Skan// This library is distributed in the hope that it will be useful,
12169691Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14169691Skan// GNU General Public License for more details.
15169691Skan
16169691Skan// You should have received a copy of the GNU General Public License along
17169691Skan// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19169691Skan// USA.
20169691Skan
21169691Skan// As a special exception, you may use this file as part of a free software
22169691Skan// library without restriction.  Specifically, if other files instantiate
23169691Skan// templates or use macros or inline functions from this file, or you compile
24169691Skan// this file and link it with other files to produce an executable, this
25169691Skan// file does not by itself cause the resulting executable to be covered by
26169691Skan// the GNU General Public License.  This exception does not however
27169691Skan// invalidate any other reasons why the executable file might be covered by
28169691Skan// the GNU General Public License.
29169691Skan
30169691Skan/** @file tr1/functional
31169691Skan *  This is a TR1 C++ Library header.
32169691Skan */
33169691Skan
34169691Skan#ifndef _TR1_FUNCTIONAL
35169691Skan#define _TR1_FUNCTIONAL 1
36169691Skan
37169691Skan#pragma GCC system_header
38169691Skan
39169691Skan#include "../functional"
40169691Skan#include <typeinfo>
41169691Skan#include <tr1/type_traits>
42169691Skan#include <ext/type_traits.h>
43169691Skan#include <cstdlib>  // for std::abort
44169691Skan#include <tr1/tuple>
45169691Skan
46169691Skannamespace std
47169691Skan{
48169691Skan_GLIBCXX_BEGIN_NAMESPACE(tr1)
49169691Skan
50169691Skan  template<typename _MemberPointer>
51169691Skan    class _Mem_fn;
52169691Skan
53169691Skan  /**
54169691Skan   *  @if maint
55169691Skan   *  Actual implementation of _Has_result_type, which uses SFINAE to
56169691Skan   *  determine if the type _Tp has a publicly-accessible member type
57169691Skan   *  result_type.
58169691Skan   *  @endif
59169691Skan  */
60169691Skan  template<typename _Tp>
61169691Skan    class _Has_result_type_helper : __sfinae_types
62169691Skan    {
63169691Skan      template<typename _Up>
64169691Skan      struct _Wrap_type
65169691Skan      { };
66169691Skan
67169691Skan      template<typename _Up>
68169691Skan        static __one __test(_Wrap_type<typename _Up::result_type>*);
69169691Skan
70169691Skan      template<typename _Up>
71169691Skan        static __two __test(...);
72169691Skan
73169691Skan    public:
74169691Skan      static const bool value = sizeof(__test<_Tp>(0)) == 1;
75169691Skan    };
76169691Skan
77169691Skan  template<typename _Tp>
78169691Skan    struct _Has_result_type
79169691Skan       : integral_constant<
80169691Skan           bool,
81169691Skan           _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
82169691Skan    { };
83169691Skan
84169691Skan  /**
85169691Skan   *  @if maint
86169691Skan   *  If we have found a result_type, extract it.
87169691Skan   *  @endif
88169691Skan  */
89169691Skan  template<bool _Has_result_type, typename _Functor>
90169691Skan    struct _Maybe_get_result_type
91169691Skan    { };
92169691Skan
93169691Skan  template<typename _Functor>
94169691Skan    struct _Maybe_get_result_type<true, _Functor>
95169691Skan    {
96169691Skan      typedef typename _Functor::result_type result_type;
97169691Skan    };
98169691Skan
99169691Skan  /**
100169691Skan   *  @if maint
101169691Skan   *  Base class for any function object that has a weak result type, as
102169691Skan   *  defined in 3.3/3 of TR1.
103169691Skan   *  @endif
104169691Skan  */
105169691Skan  template<typename _Functor>
106169691Skan    struct _Weak_result_type_impl
107169691Skan    : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
108169691Skan    {
109169691Skan    };
110169691Skan
111169691Skan  /**
112169691Skan   *  @if maint
113169691Skan   *  Strip top-level cv-qualifiers from the function object and let
114169691Skan   *  _Weak_result_type_impl perform the real work.
115169691Skan   *  @endif
116169691Skan  */
117169691Skan  template<typename _Functor>
118169691Skan    struct _Weak_result_type
119169691Skan    : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
120169691Skan    {
121169691Skan    };
122169691Skan
123169691Skan  template<typename _Signature>
124169691Skan    class result_of;
125169691Skan
126169691Skan  /**
127169691Skan   *  @if maint
128169691Skan   *  Actual implementation of result_of. When _Has_result_type is
129169691Skan   *  true, gets its result from _Weak_result_type. Otherwise, uses
130169691Skan   *  the function object's member template result to extract the
131169691Skan   *  result type.
132169691Skan   *  @endif
133169691Skan  */
134169691Skan  template<bool _Has_result_type, typename _Signature>
135169691Skan    struct _Result_of_impl;
136169691Skan
137169691Skan  // Handle member data pointers using _Mem_fn's logic
138169691Skan  template<typename _Res, typename _Class, typename _T1>
139169691Skan    struct _Result_of_impl<false, _Res _Class::*(_T1)>
140169691Skan    {
141169691Skan      typedef typename _Mem_fn<_Res _Class::*>
142169691Skan                ::template _Result_type<_T1>::type type;
143169691Skan    };
144169691Skan
145169691Skan  /**
146169691Skan   *  @if maint
147169691Skan   *  Determines if the type _Tp derives from unary_function.
148169691Skan   *  @endif
149169691Skan  */
150169691Skan  template<typename _Tp>
151169691Skan    struct _Derives_from_unary_function : __sfinae_types
152169691Skan    {
153169691Skan    private:
154169691Skan      template<typename _T1, typename _Res>
155169691Skan        static __one __test(const volatile unary_function<_T1, _Res>*);
156169691Skan
157169691Skan      // It's tempting to change "..." to const volatile void*, but
158169691Skan      // that fails when _Tp is a function type.
159169691Skan      static __two __test(...);
160169691Skan
161169691Skan    public:
162169691Skan      static const bool value = sizeof(__test((_Tp*)0)) == 1;
163169691Skan    };
164169691Skan
165169691Skan  /**
166169691Skan   *  @if maint
167169691Skan   *  Determines if the type _Tp derives from binary_function.
168169691Skan   *  @endif
169169691Skan  */
170169691Skan  template<typename _Tp>
171169691Skan    struct _Derives_from_binary_function : __sfinae_types
172169691Skan    {
173169691Skan    private:
174169691Skan      template<typename _T1, typename _T2, typename _Res>
175169691Skan        static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
176169691Skan
177169691Skan      // It's tempting to change "..." to const volatile void*, but
178169691Skan      // that fails when _Tp is a function type.
179169691Skan      static __two __test(...);
180169691Skan
181169691Skan    public:
182169691Skan      static const bool value = sizeof(__test((_Tp*)0)) == 1;
183169691Skan    };
184169691Skan
185169691Skan  /**
186169691Skan   *  @if maint
187169691Skan   *  Turns a function type into a function pointer type
188169691Skan   *  @endif
189169691Skan  */
190169691Skan  template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
191169691Skan    struct _Function_to_function_pointer
192169691Skan    {
193169691Skan      typedef _Tp type;
194169691Skan    };
195169691Skan
196169691Skan  template<typename _Tp>
197169691Skan    struct _Function_to_function_pointer<_Tp, true>
198169691Skan    {
199169691Skan      typedef _Tp* type;
200169691Skan    };
201169691Skan
202169691Skan  /**
203169691Skan   *  @if maint
204169691Skan   *  Knowing which of unary_function and binary_function _Tp derives
205169691Skan   *  from, derives from the same and ensures that reference_wrapper
206169691Skan   *  will have a weak result type. See cases below.
207169691Skan   *  @endif
208169691Skan   */
209169691Skan  template<bool _Unary, bool _Binary, typename _Tp>
210169691Skan    struct _Reference_wrapper_base_impl;
211169691Skan
212169691Skan  // Not a unary_function or binary_function, so try a weak result type
213169691Skan  template<typename _Tp>
214169691Skan    struct _Reference_wrapper_base_impl<false, false, _Tp>
215169691Skan    : _Weak_result_type<_Tp>
216169691Skan    { };
217169691Skan
218169691Skan  // unary_function but not binary_function
219169691Skan  template<typename _Tp>
220169691Skan    struct _Reference_wrapper_base_impl<true, false, _Tp>
221169691Skan    : unary_function<typename _Tp::argument_type,
222169691Skan		     typename _Tp::result_type>
223169691Skan    { };
224169691Skan
225169691Skan  // binary_function but not unary_function
226169691Skan  template<typename _Tp>
227169691Skan    struct _Reference_wrapper_base_impl<false, true, _Tp>
228169691Skan    : binary_function<typename _Tp::first_argument_type,
229169691Skan		      typename _Tp::second_argument_type,
230169691Skan		      typename _Tp::result_type>
231169691Skan    { };
232169691Skan
233169691Skan  // both unary_function and binary_function. import result_type to
234169691Skan  // avoid conflicts.
235169691Skan   template<typename _Tp>
236169691Skan    struct _Reference_wrapper_base_impl<true, true, _Tp>
237169691Skan    : unary_function<typename _Tp::argument_type,
238169691Skan		     typename _Tp::result_type>,
239169691Skan      binary_function<typename _Tp::first_argument_type,
240169691Skan		      typename _Tp::second_argument_type,
241169691Skan		      typename _Tp::result_type>
242169691Skan    {
243169691Skan      typedef typename _Tp::result_type result_type;
244169691Skan    };
245169691Skan
246169691Skan  /**
247169691Skan   *  @if maint
248169691Skan   *  Derives from unary_function or binary_function when it
249169691Skan   *  can. Specializations handle all of the easy cases. The primary
250169691Skan   *  template determines what to do with a class type, which may
251169691Skan   *  derive from both unary_function and binary_function.
252169691Skan   *  @endif
253169691Skan  */
254169691Skan  template<typename _Tp>
255169691Skan    struct _Reference_wrapper_base
256169691Skan    : _Reference_wrapper_base_impl<
257169691Skan      _Derives_from_unary_function<_Tp>::value,
258169691Skan      _Derives_from_binary_function<_Tp>::value,
259169691Skan      _Tp>
260169691Skan    { };
261169691Skan
262169691Skan  // - a function type (unary)
263169691Skan  template<typename _Res, typename _T1>
264169691Skan    struct _Reference_wrapper_base<_Res(_T1)>
265169691Skan    : unary_function<_T1, _Res>
266169691Skan    { };
267169691Skan
268169691Skan  // - a function type (binary)
269169691Skan  template<typename _Res, typename _T1, typename _T2>
270169691Skan    struct _Reference_wrapper_base<_Res(_T1, _T2)>
271169691Skan    : binary_function<_T1, _T2, _Res>
272169691Skan    { };
273169691Skan
274169691Skan  // - a function pointer type (unary)
275169691Skan  template<typename _Res, typename _T1>
276169691Skan    struct _Reference_wrapper_base<_Res(*)(_T1)>
277169691Skan    : unary_function<_T1, _Res>
278169691Skan    { };
279169691Skan
280169691Skan  // - a function pointer type (binary)
281169691Skan  template<typename _Res, typename _T1, typename _T2>
282169691Skan    struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
283169691Skan    : binary_function<_T1, _T2, _Res>
284169691Skan    { };
285169691Skan
286169691Skan  // - a pointer to member function type (unary, no qualifiers)
287169691Skan  template<typename _Res, typename _T1>
288169691Skan    struct _Reference_wrapper_base<_Res (_T1::*)()>
289169691Skan    : unary_function<_T1*, _Res>
290169691Skan    { };
291169691Skan
292169691Skan  // - a pointer to member function type (binary, no qualifiers)
293169691Skan  template<typename _Res, typename _T1, typename _T2>
294169691Skan    struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
295169691Skan    : binary_function<_T1*, _T2, _Res>
296169691Skan    { };
297169691Skan
298169691Skan  // - a pointer to member function type (unary, const)
299169691Skan  template<typename _Res, typename _T1>
300169691Skan    struct _Reference_wrapper_base<_Res (_T1::*)() const>
301169691Skan    : unary_function<const _T1*, _Res>
302169691Skan    { };
303169691Skan
304169691Skan  // - a pointer to member function type (binary, const)
305169691Skan  template<typename _Res, typename _T1, typename _T2>
306169691Skan    struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
307169691Skan    : binary_function<const _T1*, _T2, _Res>
308169691Skan    { };
309169691Skan
310169691Skan  // - a pointer to member function type (unary, volatile)
311169691Skan  template<typename _Res, typename _T1>
312169691Skan    struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
313169691Skan    : unary_function<volatile _T1*, _Res>
314169691Skan    { };
315169691Skan
316169691Skan  // - a pointer to member function type (binary, volatile)
317169691Skan  template<typename _Res, typename _T1, typename _T2>
318169691Skan    struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
319169691Skan    : binary_function<volatile _T1*, _T2, _Res>
320169691Skan    { };
321169691Skan
322169691Skan  // - a pointer to member function type (unary, const volatile)
323169691Skan  template<typename _Res, typename _T1>
324169691Skan    struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
325169691Skan    : unary_function<const volatile _T1*, _Res>
326169691Skan    { };
327169691Skan
328169691Skan  // - a pointer to member function type (binary, const volatile)
329169691Skan  template<typename _Res, typename _T1, typename _T2>
330169691Skan    struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
331169691Skan    : binary_function<const volatile _T1*, _T2, _Res>
332169691Skan    { };
333169691Skan
334169691Skan  template<typename _Tp>
335169691Skan    class reference_wrapper
336169691Skan      : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
337169691Skan    {
338169691Skan      // If _Tp is a function type, we can't form result_of<_Tp(...)>,
339169691Skan      // so turn it into a function pointer type.
340169691Skan      typedef typename _Function_to_function_pointer<_Tp>::type
341169691Skan        _M_func_type;
342169691Skan
343169691Skan      _Tp* _M_data;
344169691Skan    public:
345169691Skan      typedef _Tp type;
346169691Skan      explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
347169691Skan      { }
348169691Skan
349169691Skan      reference_wrapper(const reference_wrapper<_Tp>& __inref):
350169691Skan      _M_data(__inref._M_data)
351169691Skan      { }
352169691Skan
353169691Skan      reference_wrapper&
354169691Skan      operator=(const reference_wrapper<_Tp>& __inref)
355169691Skan      {
356169691Skan        _M_data = __inref._M_data;
357169691Skan        return *this;
358169691Skan      }
359169691Skan
360169691Skan      operator _Tp&() const
361169691Skan      { return this->get(); }
362169691Skan
363169691Skan      _Tp&
364169691Skan      get() const
365169691Skan      { return *_M_data; }
366169691Skan
367169691Skan#define _GLIBCXX_REPEAT_HEADER <tr1/ref_wrap_iterate.h>
368169691Skan#include <tr1/repeat.h>
369169691Skan#undef _GLIBCXX_REPEAT_HEADER
370169691Skan    };
371169691Skan
372169691Skan
373169691Skan  // Denotes a reference should be taken to a variable.
374169691Skan  template<typename _Tp>
375169691Skan    inline reference_wrapper<_Tp>
376169691Skan    ref(_Tp& __t)
377169691Skan    { return reference_wrapper<_Tp>(__t); }
378169691Skan
379169691Skan  // Denotes a const reference should be taken to a variable.
380169691Skan  template<typename _Tp>
381169691Skan    inline reference_wrapper<const _Tp>
382169691Skan    cref(const _Tp& __t)
383169691Skan    { return reference_wrapper<const _Tp>(__t); }
384169691Skan
385169691Skan  template<typename _Tp>
386169691Skan    inline reference_wrapper<_Tp>
387169691Skan    ref(reference_wrapper<_Tp> __t)
388169691Skan    { return ref(__t.get()); }
389169691Skan
390169691Skan  template<typename _Tp>
391169691Skan    inline reference_wrapper<const _Tp>
392169691Skan    cref(reference_wrapper<_Tp> __t)
393169691Skan    { return cref(__t.get()); }
394169691Skan
395169691Skan   template<typename _Tp, bool>
396169691Skan     struct _Mem_fn_const_or_non
397169691Skan     {
398169691Skan       typedef const _Tp& type;
399169691Skan     };
400169691Skan
401169691Skan    template<typename _Tp>
402169691Skan      struct _Mem_fn_const_or_non<_Tp, false>
403169691Skan      {
404169691Skan        typedef _Tp& type;
405169691Skan      };
406169691Skan
407169691Skan  template<typename _Res, typename _Class>
408169691Skan  class _Mem_fn<_Res _Class::*>
409169691Skan  {
410169691Skan    // This bit of genius is due to Peter Dimov, improved slightly by
411169691Skan    // Douglas Gregor.
412169691Skan    template<typename _Tp>
413169691Skan      _Res&
414169691Skan      _M_call(_Tp& __object, _Class *) const
415169691Skan      { return __object.*__pm; }
416169691Skan
417169691Skan    template<typename _Tp, typename _Up>
418169691Skan      _Res&
419169691Skan      _M_call(_Tp& __object, _Up * const *) const
420169691Skan      { return (*__object).*__pm; }
421169691Skan
422169691Skan    template<typename _Tp, typename _Up>
423169691Skan      const _Res&
424169691Skan      _M_call(_Tp& __object, const _Up * const *) const
425169691Skan      { return (*__object).*__pm; }
426169691Skan
427169691Skan    template<typename _Tp>
428169691Skan      const _Res&
429169691Skan      _M_call(_Tp& __object, const _Class *) const
430169691Skan      { return __object.*__pm; }
431169691Skan
432169691Skan    template<typename _Tp>
433169691Skan      const _Res&
434169691Skan      _M_call(_Tp& __ptr, const volatile void*) const
435169691Skan      { return (*__ptr).*__pm; }
436169691Skan
437169691Skan    template<typename _Tp> static _Tp& __get_ref();
438169691Skan
439169691Skan    template<typename _Tp>
440169691Skan      static __sfinae_types::__one __check_const(_Tp&, _Class*);
441169691Skan    template<typename _Tp, typename _Up>
442169691Skan      static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
443169691Skan    template<typename _Tp, typename _Up>
444169691Skan      static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
445169691Skan    template<typename _Tp>
446169691Skan      static __sfinae_types::__two __check_const(_Tp&, const _Class*);
447169691Skan    template<typename _Tp>
448169691Skan      static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
449169691Skan
450169691Skan  public:
451169691Skan    template<typename _Tp>
452169691Skan      struct _Result_type
453169691Skan      : _Mem_fn_const_or_non<
454169691Skan        _Res,
455169691Skan        (sizeof(__sfinae_types::__two)
456169691Skan	 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
457169691Skan      { };
458169691Skan
459169691Skan    template<typename _Signature>
460169691Skan      struct result;
461169691Skan
462169691Skan    template<typename _CVMem, typename _Tp>
463169691Skan      struct result<_CVMem(_Tp)>
464169691Skan      : public _Result_type<_Tp> { };
465169691Skan
466169691Skan    template<typename _CVMem, typename _Tp>
467169691Skan      struct result<_CVMem(_Tp&)>
468169691Skan      : public _Result_type<_Tp> { };
469169691Skan
470169691Skan    explicit _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
471169691Skan
472169691Skan    // Handle objects
473169691Skan    _Res&       operator()(_Class& __object)       const
474169691Skan    { return __object.*__pm; }
475169691Skan
476169691Skan    const _Res& operator()(const _Class& __object) const
477169691Skan    { return __object.*__pm; }
478169691Skan
479169691Skan    // Handle pointers
480169691Skan    _Res&       operator()(_Class* __object)       const
481169691Skan    { return __object->*__pm; }
482169691Skan
483169691Skan    const _Res&
484169691Skan    operator()(const _Class* __object) const
485169691Skan    { return __object->*__pm; }
486169691Skan
487169691Skan    // Handle smart pointers and derived
488169691Skan    template<typename _Tp>
489169691Skan      typename _Result_type<_Tp>::type
490169691Skan      operator()(_Tp& __unknown) const
491169691Skan      { return _M_call(__unknown, &__unknown); }
492169691Skan
493169691Skan  private:
494169691Skan    _Res _Class::*__pm;
495169691Skan  };
496169691Skan
497169691Skan  /**
498169691Skan   *  @brief Returns a function object that forwards to the member
499169691Skan   *  pointer @a pm.
500169691Skan   */
501169691Skan  template<typename _Tp, typename _Class>
502169691Skan    inline _Mem_fn<_Tp _Class::*>
503169691Skan    mem_fn(_Tp _Class::* __pm)
504169691Skan    {
505169691Skan      return _Mem_fn<_Tp _Class::*>(__pm);
506169691Skan    }
507169691Skan
508169691Skan  /**
509169691Skan   *  @brief Determines if the given type _Tp is a function object
510169691Skan   *  should be treated as a subexpression when evaluating calls to
511169691Skan   *  function objects returned by bind(). [TR1 3.6.1]
512169691Skan   */
513169691Skan  template<typename _Tp>
514169691Skan    struct is_bind_expression
515169691Skan    { static const bool value = false; };
516169691Skan
517169691Skan  template<typename _Tp>
518169691Skan    const bool is_bind_expression<_Tp>::value;
519169691Skan
520169691Skan  /**
521169691Skan   *  @brief Determines if the given type _Tp is a placeholder in a
522169691Skan   *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
523169691Skan   */
524169691Skan  template<typename _Tp>
525169691Skan    struct is_placeholder
526169691Skan    { static const int value = 0; };
527169691Skan
528169691Skan  template<typename _Tp>
529169691Skan    const int is_placeholder<_Tp>::value;
530169691Skan
531169691Skan  /**
532169691Skan   *  @if maint
533169691Skan   *  The type of placeholder objects defined by libstdc++.
534169691Skan   *  @endif
535169691Skan   */
536169691Skan  template<int _Num> struct _Placeholder { };
537169691Skan
538169691Skan  /**
539169691Skan   *  @if maint
540169691Skan   *  Partial specialization of is_placeholder that provides the placeholder
541169691Skan   *  number for the placeholder objects defined by libstdc++.
542169691Skan   *  @endif
543169691Skan   */
544169691Skan  template<int _Num>
545169691Skan    struct is_placeholder<_Placeholder<_Num> >
546169691Skan    { static const int value = _Num; };
547169691Skan
548169691Skan  template<int _Num>
549169691Skan    const int is_placeholder<_Placeholder<_Num> >::value;
550169691Skan
551169691Skan  /**
552169691Skan   *  @if maint
553169691Skan   *  Maps an argument to bind() into an actual argument to the bound
554169691Skan   *  function object [TR1 3.6.3/5]. Only the first parameter should
555169691Skan   *  be specified: the rest are used to determine among the various
556169691Skan   *  implementations. Note that, although this class is a function
557169691Skan   *  object, isn't not entirely normal because it takes only two
558169691Skan   *  parameters regardless of the number of parameters passed to the
559169691Skan   *  bind expression. The first parameter is the bound argument and
560169691Skan   *  the second parameter is a tuple containing references to the
561169691Skan   *  rest of the arguments.
562169691Skan   *  @endif
563169691Skan   */
564169691Skan  template<typename _Arg,
565169691Skan           bool _IsBindExp = is_bind_expression<_Arg>::value,
566169691Skan           bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
567169691Skan    class _Mu;
568169691Skan
569169691Skan  /**
570169691Skan   *  @if maint
571169691Skan   *  If the argument is reference_wrapper<_Tp>, returns the
572169691Skan   *  underlying reference. [TR1 3.6.3/5 bullet 1]
573169691Skan   *  @endif
574169691Skan   */
575169691Skan  template<typename _Tp>
576169691Skan    class _Mu<reference_wrapper<_Tp>, false, false>
577169691Skan    {
578169691Skan    public:
579169691Skan      typedef _Tp& result_type;
580169691Skan
581169691Skan      /* Note: This won't actually work for const volatile
582169691Skan       * reference_wrappers, because reference_wrapper::get() is const
583169691Skan       * but not volatile-qualified. This might be a defect in the TR.
584169691Skan       */
585169691Skan      template<typename _CVRef, typename _Tuple>
586169691Skan      result_type
587169691Skan      operator()(_CVRef& __arg, const _Tuple&) const volatile
588169691Skan      { return __arg.get(); }
589169691Skan    };
590169691Skan
591169691Skan  /**
592169691Skan   *  @if maint
593169691Skan   *  If the argument is a bind expression, we invoke the underlying
594169691Skan   *  function object with the same cv-qualifiers as we are given and
595169691Skan   *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
596169691Skan   *  @endif
597169691Skan   */
598169691Skan  template<typename _Arg>
599169691Skan    class _Mu<_Arg, true, false>
600169691Skan    {
601169691Skan    public:
602169691Skan      template<typename _Signature> class result;
603169691Skan
604169691Skan#define _GLIBCXX_REPEAT_HEADER <tr1/mu_iterate.h>
605169691Skan#  include <tr1/repeat.h>
606169691Skan#undef _GLIBCXX_REPEAT_HEADER
607169691Skan    };
608169691Skan
609169691Skan  /**
610169691Skan   *  @if maint
611169691Skan   *  If the argument is a placeholder for the Nth argument, returns
612169691Skan   *  a reference to the Nth argument to the bind function object.
613169691Skan   *  [TR1 3.6.3/5 bullet 3]
614169691Skan   *  @endif
615169691Skan   */
616169691Skan  template<typename _Arg>
617169691Skan    class _Mu<_Arg, false, true>
618169691Skan    {
619169691Skan    public:
620169691Skan      template<typename _Signature> class result;
621169691Skan
622169691Skan      template<typename _CVMu, typename _CVArg, typename _Tuple>
623169691Skan      class result<_CVMu(_CVArg, _Tuple)>
624169691Skan      {
625169691Skan        // Add a reference, if it hasn't already been done for us.
626169691Skan        // This allows us to be a little bit sloppy in constructing
627169691Skan        // the tuple that we pass to result_of<...>.
628169691Skan        typedef typename tuple_element<(is_placeholder<_Arg>::value - 1),
629169691Skan                                       _Tuple>::type __base_type;
630169691Skan
631169691Skan      public:
632169691Skan        typedef typename add_reference<__base_type>::type type;
633169691Skan      };
634169691Skan
635169691Skan      template<typename _Tuple>
636169691Skan      typename result<_Mu(_Arg, _Tuple)>::type
637169691Skan      operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
638169691Skan      {
639169691Skan        return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
640169691Skan      }
641169691Skan    };
642169691Skan
643169691Skan  /**
644169691Skan   *  @if maint
645169691Skan   *  If the argument is just a value, returns a reference to that
646169691Skan   *  value. The cv-qualifiers on the reference are the same as the
647169691Skan   *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
648169691Skan   *  @endif
649169691Skan   */
650169691Skan  template<typename _Arg>
651169691Skan    class _Mu<_Arg, false, false>
652169691Skan    {
653169691Skan    public:
654169691Skan      template<typename _Signature> struct result;
655169691Skan
656169691Skan      template<typename _CVMu, typename _CVArg, typename _Tuple>
657169691Skan      struct result<_CVMu(_CVArg, _Tuple)>
658169691Skan      {
659169691Skan        typedef typename add_reference<_CVArg>::type type;
660169691Skan      };
661169691Skan
662169691Skan      // Pick up the cv-qualifiers of the argument
663169691Skan      template<typename _CVArg, typename _Tuple>
664169691Skan      _CVArg& operator()(_CVArg& __arg, const _Tuple&) const volatile
665169691Skan      { return __arg; }
666169691Skan    };
667169691Skan
668169691Skan  /**
669169691Skan   *  @if maint
670169691Skan   *  Maps member pointers into instances of _Mem_fn but leaves all
671169691Skan   *  other function objects untouched. Used by tr1::bind(). The
672169691Skan   *  primary template handles the non--member-pointer case.
673169691Skan   *  @endif
674169691Skan   */
675169691Skan  template<typename _Tp>
676169691Skan    struct _Maybe_wrap_member_pointer
677169691Skan    {
678169691Skan      typedef _Tp type;
679169691Skan      static const _Tp& __do_wrap(const _Tp& __x) { return __x; }
680169691Skan    };
681169691Skan
682169691Skan  /**
683169691Skan   *  @if maint
684169691Skan   *  Maps member pointers into instances of _Mem_fn but leaves all
685169691Skan   *  other function objects untouched. Used by tr1::bind(). This
686169691Skan   *  partial specialization handles the member pointer case.
687169691Skan   *  @endif
688169691Skan   */
689169691Skan  template<typename _Tp, typename _Class>
690169691Skan    struct _Maybe_wrap_member_pointer<_Tp _Class::*>
691169691Skan    {
692169691Skan      typedef _Mem_fn<_Tp _Class::*> type;
693169691Skan      static type __do_wrap(_Tp _Class::* __pm) { return type(__pm); }
694169691Skan    };
695169691Skan
696169691Skan  /**
697169691Skan   *  @if maint
698169691Skan   *  Type of the function object returned from bind().
699169691Skan   *  @endif
700169691Skan   */
701169691Skan   template<typename _Signature>
702169691Skan     struct _Bind;
703169691Skan
704169691Skan  /**
705169691Skan   *  @if maint
706169691Skan   *  Type of the function object returned from bind<R>().
707169691Skan   *  @endif
708169691Skan   */
709169691Skan   template<typename _Result, typename _Signature>
710169691Skan     struct _Bind_result;
711169691Skan
712169691Skan  /**
713169691Skan   *  @if maint
714169691Skan   *  Class template _Bind is always a bind expression.
715169691Skan   *  @endif
716169691Skan   */
717169691Skan   template<typename _Signature>
718169691Skan     struct is_bind_expression<_Bind<_Signature> >
719169691Skan     { static const bool value = true; };
720169691Skan
721169691Skan   template<typename _Signature>
722169691Skan     const bool is_bind_expression<_Bind<_Signature> >::value;
723169691Skan
724169691Skan  /**
725169691Skan   *  @if maint
726169691Skan   *  Class template _Bind_result is always a bind expression.
727169691Skan   *  @endif
728169691Skan   */
729169691Skan   template<typename _Result, typename _Signature>
730169691Skan     struct is_bind_expression<_Bind_result<_Result, _Signature> >
731169691Skan     { static const bool value = true; };
732169691Skan
733169691Skan   template<typename _Result, typename _Signature>
734169691Skan     const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
735169691Skan
736169691Skan  /**
737169691Skan   *  @brief Exception class thrown when class template function's
738169691Skan   *  operator() is called with an empty target.
739169691Skan   *
740169691Skan   */
741169691Skan  class bad_function_call : public std::exception { };
742169691Skan
743169691Skan  /**
744169691Skan   *  @if maint
745169691Skan   *  The integral constant expression 0 can be converted into a
746169691Skan   *  pointer to this type. It is used by the function template to
747169691Skan   *  accept NULL pointers.
748169691Skan   *  @endif
749169691Skan   */
750169691Skan  struct _M_clear_type;
751169691Skan
752169691Skan  /**
753169691Skan   *  @if maint
754169691Skan   *  Trait identifying "location-invariant" types, meaning that the
755169691Skan   *  address of the object (or any of its members) will not escape.
756169691Skan   *  Also implies a trivial copy constructor and assignment operator.
757169691Skan   *   @endif
758169691Skan   */
759169691Skan  template<typename _Tp>
760169691Skan    struct __is_location_invariant
761169691Skan    : integral_constant<bool,
762169691Skan                        (is_pointer<_Tp>::value
763169691Skan                         || is_member_pointer<_Tp>::value)>
764169691Skan    {
765169691Skan    };
766169691Skan
767169691Skan  class _Undefined_class;
768169691Skan
769169691Skan  union _Nocopy_types
770169691Skan  {
771169691Skan    void*       _M_object;
772169691Skan    const void* _M_const_object;
773169691Skan    void (*_M_function_pointer)();
774169691Skan    void (_Undefined_class::*_M_member_pointer)();
775169691Skan  };
776169691Skan
777169691Skan  union _Any_data {
778169691Skan    void*       _M_access()       { return &_M_pod_data[0]; }
779169691Skan    const void* _M_access() const { return &_M_pod_data[0]; }
780169691Skan
781169691Skan    template<typename _Tp> _Tp& _M_access()
782169691Skan    { return *static_cast<_Tp*>(_M_access()); }
783169691Skan
784169691Skan    template<typename _Tp> const _Tp& _M_access() const
785169691Skan    { return *static_cast<const _Tp*>(_M_access()); }
786169691Skan
787169691Skan    _Nocopy_types _M_unused;
788169691Skan    char _M_pod_data[sizeof(_Nocopy_types)];
789169691Skan  };
790169691Skan
791169691Skan  enum _Manager_operation
792169691Skan  {
793169691Skan    __get_type_info,
794169691Skan    __get_functor_ptr,
795169691Skan    __clone_functor,
796169691Skan    __destroy_functor
797169691Skan  };
798169691Skan
799169691Skan  /* Simple type wrapper that helps avoid annoying const problems
800169691Skan     when casting between void pointers and pointers-to-pointers. */
801169691Skan  template<typename _Tp>
802169691Skan    struct _Simple_type_wrapper
803169691Skan    {
804169691Skan      _Simple_type_wrapper(_Tp __value) : __value(__value) { }
805169691Skan
806169691Skan      _Tp __value;
807169691Skan    };
808169691Skan
809169691Skan  template<typename _Tp>
810169691Skan    struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
811169691Skan      : __is_location_invariant<_Tp>
812169691Skan    {
813169691Skan    };
814169691Skan
815169691Skan  // Converts a reference to a function object into a callable
816169691Skan  // function object.
817169691Skan  template<typename _Functor>
818169691Skan    inline _Functor& __callable_functor(_Functor& __f) { return __f; }
819169691Skan
820169691Skan  template<typename _Member, typename _Class>
821169691Skan    inline _Mem_fn<_Member _Class::*>
822169691Skan    __callable_functor(_Member _Class::* &__p)
823169691Skan    { return mem_fn(__p); }
824169691Skan
825169691Skan  template<typename _Member, typename _Class>
826169691Skan    inline _Mem_fn<_Member _Class::*>
827169691Skan    __callable_functor(_Member _Class::* const &__p)
828169691Skan    { return mem_fn(__p); }
829169691Skan
830169691Skan  template<typename _Signature, typename _Functor>
831169691Skan    class _Function_handler;
832169691Skan
833169691Skan  template<typename _Signature>
834169691Skan    class function;
835169691Skan
836169691Skan
837169691Skan  /**
838169691Skan   *  @if maint
839169691Skan   *  Base class of all polymorphic function object wrappers.
840169691Skan   *  @endif
841169691Skan   */
842169691Skan  class _Function_base
843169691Skan  {
844169691Skan  public:
845169691Skan    static const std::size_t _M_max_size = sizeof(_Nocopy_types);
846169691Skan    static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
847169691Skan
848169691Skan    template<typename _Functor>
849169691Skan    class _Base_manager
850169691Skan    {
851169691Skan    protected:
852169691Skan      static const bool __stored_locally =
853169691Skan        (__is_location_invariant<_Functor>::value
854169691Skan         && sizeof(_Functor) <= _M_max_size
855169691Skan         && __alignof__(_Functor) <= _M_max_align
856169691Skan         && (_M_max_align % __alignof__(_Functor) == 0));
857169691Skan      typedef integral_constant<bool, __stored_locally> _Local_storage;
858169691Skan
859169691Skan      // Retrieve a pointer to the function object
860169691Skan      static _Functor* _M_get_pointer(const _Any_data& __source)
861169691Skan      {
862169691Skan        const _Functor* __ptr =
863169691Skan          __stored_locally? &__source._M_access<_Functor>()
864169691Skan          /* have stored a pointer */ : __source._M_access<_Functor*>();
865169691Skan        return const_cast<_Functor*>(__ptr);
866169691Skan      }
867169691Skan
868169691Skan      // Clone a location-invariant function object that fits within
869169691Skan      // an _Any_data structure.
870169691Skan      static void
871169691Skan      _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
872169691Skan      {
873169691Skan        new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
874169691Skan      }
875169691Skan
876169691Skan      // Clone a function object that is not location-invariant or
877169691Skan      // that cannot fit into an _Any_data structure.
878169691Skan      static void
879169691Skan      _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
880169691Skan      {
881169691Skan        __dest._M_access<_Functor*>() =
882169691Skan          new _Functor(*__source._M_access<_Functor*>());
883169691Skan      }
884169691Skan
885169691Skan      // Destroying a location-invariant object may still require
886169691Skan      // destruction.
887169691Skan      static void
888169691Skan      _M_destroy(_Any_data& __victim, true_type)
889169691Skan      {
890169691Skan        __victim._M_access<_Functor>().~_Functor();
891169691Skan      }
892169691Skan
893169691Skan      // Destroying an object located on the heap.
894169691Skan      static void
895169691Skan      _M_destroy(_Any_data& __victim, false_type)
896169691Skan      {
897169691Skan        delete __victim._M_access<_Functor*>();
898169691Skan      }
899169691Skan
900169691Skan    public:
901169691Skan      static bool
902169691Skan      _M_manager(_Any_data& __dest, const _Any_data& __source,
903169691Skan                 _Manager_operation __op)
904169691Skan      {
905169691Skan        switch (__op) {
906169691Skan        case __get_type_info:
907169691Skan          __dest._M_access<const type_info*>() = &typeid(_Functor);
908169691Skan          break;
909169691Skan
910169691Skan        case __get_functor_ptr:
911169691Skan          __dest._M_access<_Functor*>() = _M_get_pointer(__source);
912169691Skan          break;
913169691Skan
914169691Skan        case __clone_functor:
915169691Skan          _M_clone(__dest, __source, _Local_storage());
916169691Skan          break;
917169691Skan
918169691Skan        case __destroy_functor:
919169691Skan          _M_destroy(__dest, _Local_storage());
920169691Skan          break;
921169691Skan        }
922169691Skan        return false;
923169691Skan      }
924169691Skan
925169691Skan      static void
926169691Skan      _M_init_functor(_Any_data& __functor, const _Functor& __f)
927169691Skan      {
928169691Skan        _M_init_functor(__functor, __f, _Local_storage());
929169691Skan      }
930169691Skan
931169691Skan      template<typename _Signature>
932169691Skan      static bool
933169691Skan      _M_not_empty_function(const function<_Signature>& __f)
934169691Skan      {
935169691Skan        return __f;
936169691Skan      }
937169691Skan
938169691Skan      template<typename _Tp>
939169691Skan      static bool
940169691Skan      _M_not_empty_function(const _Tp*& __fp)
941169691Skan      {
942169691Skan        return __fp;
943169691Skan      }
944169691Skan
945169691Skan      template<typename _Class, typename _Tp>
946169691Skan      static bool
947169691Skan      _M_not_empty_function(_Tp _Class::* const& __mp)
948169691Skan      {
949169691Skan        return __mp;
950169691Skan      }
951169691Skan
952169691Skan      template<typename _Tp>
953169691Skan      static bool
954169691Skan      _M_not_empty_function(const _Tp&)
955169691Skan      {
956169691Skan        return true;
957169691Skan      }
958169691Skan
959169691Skan    private:
960169691Skan      static void
961169691Skan      _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
962169691Skan      {
963169691Skan        new (__functor._M_access()) _Functor(__f);
964169691Skan      }
965169691Skan
966169691Skan      static void
967169691Skan      _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
968169691Skan      {
969169691Skan        __functor._M_access<_Functor*>() = new _Functor(__f);
970169691Skan      }
971169691Skan    };
972169691Skan
973169691Skan    template<typename _Functor>
974169691Skan    class _Ref_manager : public _Base_manager<_Functor*>
975169691Skan    {
976169691Skan      typedef _Function_base::_Base_manager<_Functor*> _Base;
977169691Skan
978169691Skan    public:
979169691Skan      static bool
980169691Skan      _M_manager(_Any_data& __dest, const _Any_data& __source,
981169691Skan                 _Manager_operation __op)
982169691Skan      {
983169691Skan        switch (__op) {
984169691Skan        case __get_type_info:
985169691Skan          __dest._M_access<const type_info*>() = &typeid(_Functor);
986169691Skan          break;
987169691Skan
988169691Skan        case __get_functor_ptr:
989169691Skan          __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
990169691Skan          return is_const<_Functor>::value;
991169691Skan          break;
992169691Skan
993169691Skan        default:
994169691Skan          _Base::_M_manager(__dest, __source, __op);
995169691Skan        }
996169691Skan        return false;
997169691Skan      }
998169691Skan
999169691Skan      static void
1000169691Skan      _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1001169691Skan      {
1002169691Skan        // TBD: Use address_of function instead
1003169691Skan        _Base::_M_init_functor(__functor, &__f.get());
1004169691Skan      }
1005169691Skan    };
1006169691Skan
1007169691Skan    _Function_base() : _M_manager(0) { }
1008169691Skan
1009169691Skan    ~_Function_base()
1010169691Skan    {
1011169691Skan      if (_M_manager)
1012169691Skan        {
1013169691Skan          _M_manager(_M_functor, _M_functor, __destroy_functor);
1014169691Skan        }
1015169691Skan    }
1016169691Skan
1017169691Skan
1018169691Skan    bool _M_empty() const { return !_M_manager; }
1019169691Skan
1020169691Skan    typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1021169691Skan                                  _Manager_operation);
1022169691Skan
1023169691Skan    _Any_data     _M_functor;
1024169691Skan    _Manager_type _M_manager;
1025169691Skan  };
1026169691Skan
1027169691Skan  // [3.7.2.7] null pointer comparisons
1028169691Skan
1029169691Skan  /**
1030169691Skan   *  @brief Compares a polymorphic function object wrapper against 0
1031169691Skan   *  (the NULL pointer).
1032169691Skan   *  @returns @c true if the wrapper has no target, @c false otherwise
1033169691Skan   *
1034169691Skan   *  This function will not throw an exception.
1035169691Skan   */
1036169691Skan  template<typename _Signature>
1037169691Skan    inline bool
1038169691Skan    operator==(const function<_Signature>& __f, _M_clear_type*)
1039169691Skan    {
1040169691Skan      return !__f;
1041169691Skan    }
1042169691Skan
1043169691Skan  /**
1044169691Skan   *  @overload
1045169691Skan   */
1046169691Skan  template<typename _Signature>
1047169691Skan    inline bool
1048169691Skan    operator==(_M_clear_type*, const function<_Signature>& __f)
1049169691Skan    {
1050169691Skan      return !__f;
1051169691Skan    }
1052169691Skan
1053169691Skan  /**
1054169691Skan   *  @brief Compares a polymorphic function object wrapper against 0
1055169691Skan   *  (the NULL pointer).
1056169691Skan   *  @returns @c false if the wrapper has no target, @c true otherwise
1057169691Skan   *
1058169691Skan   *  This function will not throw an exception.
1059169691Skan   */
1060169691Skan  template<typename _Signature>
1061169691Skan    inline bool
1062169691Skan    operator!=(const function<_Signature>& __f, _M_clear_type*)
1063169691Skan    {
1064169691Skan      return __f;
1065169691Skan    }
1066169691Skan
1067169691Skan  /**
1068169691Skan   *  @overload
1069169691Skan   */
1070169691Skan  template<typename _Signature>
1071169691Skan    inline bool
1072169691Skan    operator!=(_M_clear_type*, const function<_Signature>& __f)
1073169691Skan    {
1074169691Skan      return __f;
1075169691Skan    }
1076169691Skan
1077169691Skan  // [3.7.2.8] specialized algorithms
1078169691Skan
1079169691Skan  /**
1080169691Skan   *  @brief Swap the targets of two polymorphic function object wrappers.
1081169691Skan   *
1082169691Skan   *  This function will not throw an exception.
1083169691Skan   */
1084169691Skan  template<typename _Signature>
1085169691Skan    inline void
1086169691Skan    swap(function<_Signature>& __x, function<_Signature>& __y)
1087169691Skan    {
1088169691Skan      __x.swap(__y);
1089169691Skan    }
1090169691Skan
1091169691Skan_GLIBCXX_END_NAMESPACE
1092169691Skan}
1093169691Skan
1094169691Skan#define _GLIBCXX_JOIN(X,Y) _GLIBCXX_JOIN2( X , Y )
1095169691Skan#define _GLIBCXX_JOIN2(X,Y) _GLIBCXX_JOIN3(X,Y)
1096169691Skan#define _GLIBCXX_JOIN3(X,Y) X##Y
1097169691Skan#define _GLIBCXX_REPEAT_HEADER <tr1/functional_iterate.h>
1098169691Skan#include <tr1/repeat.h>
1099169691Skan#undef _GLIBCXX_REPEAT_HEADER
1100169691Skan#undef _GLIBCXX_JOIN3
1101169691Skan#undef _GLIBCXX_JOIN2
1102169691Skan#undef _GLIBCXX_JOIN
1103169691Skan
1104169691Skan#include <tr1/functional_hash.h>
1105169691Skan
1106169691Skan#endif
1107