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