1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
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 3, or (at your option)
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.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file tr1_impl/functional
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
32 _GLIBCXX_BEGIN_NAMESPACE_TR1
34 template<typename _MemberPointer>
38 * Actual implementation of _Has_result_type, which uses SFINAE to
39 * determine if the type _Tp has a publicly-accessible member type
42 template<typename _Tp>
43 class _Has_result_type_helper : __sfinae_types
45 template<typename _Up>
49 template<typename _Up>
50 static __one __test(_Wrap_type<typename _Up::result_type>*);
52 template<typename _Up>
53 static __two __test(...);
56 static const bool value = sizeof(__test<_Tp>(0)) == 1;
59 template<typename _Tp>
60 struct _Has_result_type
61 : integral_constant<bool,
62 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
68 /// If we have found a result_type, extract it.
69 template<bool _Has_result_type, typename _Functor>
70 struct _Maybe_get_result_type
73 template<typename _Functor>
74 struct _Maybe_get_result_type<true, _Functor>
76 typedef typename _Functor::result_type result_type;
80 * Base class for any function object that has a weak result type, as
81 * defined in 3.3/3 of TR1.
83 template<typename _Functor>
84 struct _Weak_result_type_impl
85 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
89 /// Retrieve the result type for a function type.
90 template<typename _Res, typename... _ArgTypes>
91 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
93 typedef _Res result_type;
96 /// Retrieve the result type for a function reference.
97 template<typename _Res, typename... _ArgTypes>
98 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
100 typedef _Res result_type;
103 /// Retrieve the result type for a function pointer.
104 template<typename _Res, typename... _ArgTypes>
105 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
107 typedef _Res result_type;
110 /// Retrieve result type for a member function pointer.
111 template<typename _Res, typename _Class, typename... _ArgTypes>
112 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
114 typedef _Res result_type;
117 /// Retrieve result type for a const member function pointer.
118 template<typename _Res, typename _Class, typename... _ArgTypes>
119 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
121 typedef _Res result_type;
124 /// Retrieve result type for a volatile member function pointer.
125 template<typename _Res, typename _Class, typename... _ArgTypes>
126 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
128 typedef _Res result_type;
131 /// Retrieve result type for a const volatile member function pointer.
132 template<typename _Res, typename _Class, typename... _ArgTypes>
133 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
135 typedef _Res result_type;
139 * Strip top-level cv-qualifiers from the function object and let
140 * _Weak_result_type_impl perform the real work.
142 template<typename _Functor>
143 struct _Weak_result_type
144 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
148 template<typename _Signature>
152 * Actual implementation of result_of. When _Has_result_type is
153 * true, gets its result from _Weak_result_type. Otherwise, uses
154 * the function object's member template result to extract the
157 template<bool _Has_result_type, typename _Signature>
158 struct _Result_of_impl;
160 // Handle member data pointers using _Mem_fn's logic
161 template<typename _Res, typename _Class, typename _T1>
162 struct _Result_of_impl<false, _Res _Class::*(_T1)>
164 typedef typename _Mem_fn<_Res _Class::*>
165 ::template _Result_type<_T1>::type type;
169 * Determine whether we can determine a result type from @c Functor
172 template<typename _Functor, typename... _ArgTypes>
173 class result_of<_Functor(_ArgTypes...)>
174 : public _Result_of_impl<
175 _Has_result_type<_Weak_result_type<_Functor> >::value,
176 _Functor(_ArgTypes...)>
180 /// We already know the result type for @c Functor; use it.
181 template<typename _Functor, typename... _ArgTypes>
182 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
184 typedef typename _Weak_result_type<_Functor>::result_type type;
188 * We need to compute the result type for this invocation the hard
191 template<typename _Functor, typename... _ArgTypes>
192 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
194 typedef typename _Functor
195 ::template result<_Functor(_ArgTypes...)>::type type;
199 * It is unsafe to access ::result when there are zero arguments, so we
200 * return @c void instead.
202 template<typename _Functor>
203 struct _Result_of_impl<false, _Functor()>
208 /// Determines if the type _Tp derives from unary_function.
209 template<typename _Tp>
210 struct _Derives_from_unary_function : __sfinae_types
213 template<typename _T1, typename _Res>
214 static __one __test(const volatile unary_function<_T1, _Res>*);
216 // It's tempting to change "..." to const volatile void*, but
217 // that fails when _Tp is a function type.
218 static __two __test(...);
221 static const bool value = sizeof(__test((_Tp*)0)) == 1;
224 /// Determines if the type _Tp derives from binary_function.
225 template<typename _Tp>
226 struct _Derives_from_binary_function : __sfinae_types
229 template<typename _T1, typename _T2, typename _Res>
230 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
232 // It's tempting to change "..." to const volatile void*, but
233 // that fails when _Tp is a function type.
234 static __two __test(...);
237 static const bool value = sizeof(__test((_Tp*)0)) == 1;
240 /// Turns a function type into a function pointer type
241 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
242 struct _Function_to_function_pointer
247 template<typename _Tp>
248 struct _Function_to_function_pointer<_Tp, true>
254 * Invoke a function object, which may be either a member pointer or a
255 * function object. The first parameter will tell which.
257 template<typename _Functor, typename... _Args>
259 typename __gnu_cxx::__enable_if<
260 (!is_member_pointer<_Functor>::value
261 && !is_function<_Functor>::value
262 && !is_function<typename remove_pointer<_Functor>::type>::value),
263 typename result_of<_Functor(_Args...)>::type
265 __invoke(_Functor& __f, _Args&... __args)
267 return __f(__args...);
270 template<typename _Functor, typename... _Args>
272 typename __gnu_cxx::__enable_if<
273 (is_member_pointer<_Functor>::value
274 && !is_function<_Functor>::value
275 && !is_function<typename remove_pointer<_Functor>::type>::value),
276 typename result_of<_Functor(_Args...)>::type
278 __invoke(_Functor& __f, _Args&... __args)
280 return mem_fn(__f)(__args...);
283 // To pick up function references (that will become function pointers)
284 template<typename _Functor, typename... _Args>
286 typename __gnu_cxx::__enable_if<
287 (is_pointer<_Functor>::value
288 && is_function<typename remove_pointer<_Functor>::type>::value),
289 typename result_of<_Functor(_Args...)>::type
291 __invoke(_Functor __f, _Args&... __args)
293 return __f(__args...);
297 * Knowing which of unary_function and binary_function _Tp derives
298 * from, derives from the same and ensures that reference_wrapper
299 * will have a weak result type. See cases below.
301 template<bool _Unary, bool _Binary, typename _Tp>
302 struct _Reference_wrapper_base_impl;
304 // Not a unary_function or binary_function, so try a weak result type.
305 template<typename _Tp>
306 struct _Reference_wrapper_base_impl<false, false, _Tp>
307 : _Weak_result_type<_Tp>
310 // unary_function but not binary_function
311 template<typename _Tp>
312 struct _Reference_wrapper_base_impl<true, false, _Tp>
313 : unary_function<typename _Tp::argument_type,
314 typename _Tp::result_type>
317 // binary_function but not unary_function
318 template<typename _Tp>
319 struct _Reference_wrapper_base_impl<false, true, _Tp>
320 : binary_function<typename _Tp::first_argument_type,
321 typename _Tp::second_argument_type,
322 typename _Tp::result_type>
325 // Both unary_function and binary_function. Import result_type to
327 template<typename _Tp>
328 struct _Reference_wrapper_base_impl<true, true, _Tp>
329 : unary_function<typename _Tp::argument_type,
330 typename _Tp::result_type>,
331 binary_function<typename _Tp::first_argument_type,
332 typename _Tp::second_argument_type,
333 typename _Tp::result_type>
335 typedef typename _Tp::result_type result_type;
339 * Derives from unary_function or binary_function when it
340 * can. Specializations handle all of the easy cases. The primary
341 * template determines what to do with a class type, which may
342 * derive from both unary_function and binary_function.
344 template<typename _Tp>
345 struct _Reference_wrapper_base
346 : _Reference_wrapper_base_impl<
347 _Derives_from_unary_function<_Tp>::value,
348 _Derives_from_binary_function<_Tp>::value,
352 // - a function type (unary)
353 template<typename _Res, typename _T1>
354 struct _Reference_wrapper_base<_Res(_T1)>
355 : unary_function<_T1, _Res>
358 // - a function type (binary)
359 template<typename _Res, typename _T1, typename _T2>
360 struct _Reference_wrapper_base<_Res(_T1, _T2)>
361 : binary_function<_T1, _T2, _Res>
364 // - a function pointer type (unary)
365 template<typename _Res, typename _T1>
366 struct _Reference_wrapper_base<_Res(*)(_T1)>
367 : unary_function<_T1, _Res>
370 // - a function pointer type (binary)
371 template<typename _Res, typename _T1, typename _T2>
372 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
373 : binary_function<_T1, _T2, _Res>
376 // - a pointer to member function type (unary, no qualifiers)
377 template<typename _Res, typename _T1>
378 struct _Reference_wrapper_base<_Res (_T1::*)()>
379 : unary_function<_T1*, _Res>
382 // - a pointer to member function type (binary, no qualifiers)
383 template<typename _Res, typename _T1, typename _T2>
384 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
385 : binary_function<_T1*, _T2, _Res>
388 // - a pointer to member function type (unary, const)
389 template<typename _Res, typename _T1>
390 struct _Reference_wrapper_base<_Res (_T1::*)() const>
391 : unary_function<const _T1*, _Res>
394 // - a pointer to member function type (binary, const)
395 template<typename _Res, typename _T1, typename _T2>
396 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
397 : binary_function<const _T1*, _T2, _Res>
400 // - a pointer to member function type (unary, volatile)
401 template<typename _Res, typename _T1>
402 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
403 : unary_function<volatile _T1*, _Res>
406 // - a pointer to member function type (binary, volatile)
407 template<typename _Res, typename _T1, typename _T2>
408 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
409 : binary_function<volatile _T1*, _T2, _Res>
412 // - a pointer to member function type (unary, const volatile)
413 template<typename _Res, typename _T1>
414 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
415 : unary_function<const volatile _T1*, _Res>
418 // - a pointer to member function type (binary, const volatile)
419 template<typename _Res, typename _T1, typename _T2>
420 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
421 : binary_function<const volatile _T1*, _T2, _Res>
424 /// reference_wrapper
425 template<typename _Tp>
426 class reference_wrapper
427 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
429 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
430 // so turn it into a function pointer type.
431 typedef typename _Function_to_function_pointer<_Tp>::type
439 reference_wrapper(_Tp& __indata): _M_data(&__indata)
442 reference_wrapper(const reference_wrapper<_Tp>& __inref):
443 _M_data(__inref._M_data)
447 operator=(const reference_wrapper<_Tp>& __inref)
449 _M_data = __inref._M_data;
453 operator _Tp&() const
454 { return this->get(); }
460 template<typename... _Args>
461 typename result_of<_M_func_type(_Args...)>::type
462 operator()(_Args&... __args) const
464 return __invoke(get(), __args...);
469 // Denotes a reference should be taken to a variable.
470 template<typename _Tp>
471 inline reference_wrapper<_Tp>
473 { return reference_wrapper<_Tp>(__t); }
475 // Denotes a const reference should be taken to a variable.
476 template<typename _Tp>
477 inline reference_wrapper<const _Tp>
479 { return reference_wrapper<const _Tp>(__t); }
481 template<typename _Tp>
482 inline reference_wrapper<_Tp>
483 ref(reference_wrapper<_Tp> __t)
484 { return ref(__t.get()); }
486 template<typename _Tp>
487 inline reference_wrapper<const _Tp>
488 cref(reference_wrapper<_Tp> __t)
489 { return cref(__t.get()); }
491 template<typename _Tp, bool>
492 struct _Mem_fn_const_or_non
494 typedef const _Tp& type;
497 template<typename _Tp>
498 struct _Mem_fn_const_or_non<_Tp, false>
504 * Derives from @c unary_function or @c binary_function, or perhaps
505 * nothing, depending on the number of arguments provided. The
506 * primary template is the basis case, which derives nothing.
508 template<typename _Res, typename... _ArgTypes>
509 struct _Maybe_unary_or_binary_function { };
511 /// Derives from @c unary_function, as appropriate.
512 template<typename _Res, typename _T1>
513 struct _Maybe_unary_or_binary_function<_Res, _T1>
514 : std::unary_function<_T1, _Res> { };
516 /// Derives from @c binary_function, as appropriate.
517 template<typename _Res, typename _T1, typename _T2>
518 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
519 : std::binary_function<_T1, _T2, _Res> { };
521 /// Implementation of @c mem_fn for member function pointers.
522 template<typename _Res, typename _Class, typename... _ArgTypes>
523 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
524 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
526 typedef _Res (_Class::*_Functor)(_ArgTypes...);
528 template<typename _Tp>
530 _M_call(_Tp& __object, const volatile _Class *,
531 _ArgTypes... __args) const
532 { return (__object.*__pmf)(__args...); }
534 template<typename _Tp>
536 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
537 { return ((*__ptr).*__pmf)(__args...); }
540 typedef _Res result_type;
542 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
546 operator()(_Class& __object, _ArgTypes... __args) const
547 { return (__object.*__pmf)(__args...); }
551 operator()(_Class* __object, _ArgTypes... __args) const
552 { return (__object->*__pmf)(__args...); }
554 // Handle smart pointers, references and pointers to derived
555 template<typename _Tp>
557 operator()(_Tp& __object, _ArgTypes... __args) const
558 { return _M_call(__object, &__object, __args...); }
564 /// Implementation of @c mem_fn for const member function pointers.
565 template<typename _Res, typename _Class, typename... _ArgTypes>
566 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
567 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
570 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
572 template<typename _Tp>
574 _M_call(_Tp& __object, const volatile _Class *,
575 _ArgTypes... __args) const
576 { return (__object.*__pmf)(__args...); }
578 template<typename _Tp>
580 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
581 { return ((*__ptr).*__pmf)(__args...); }
584 typedef _Res result_type;
586 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
590 operator()(const _Class& __object, _ArgTypes... __args) const
591 { return (__object.*__pmf)(__args...); }
595 operator()(const _Class* __object, _ArgTypes... __args) const
596 { return (__object->*__pmf)(__args...); }
598 // Handle smart pointers, references and pointers to derived
599 template<typename _Tp>
600 _Res operator()(_Tp& __object, _ArgTypes... __args) const
601 { return _M_call(__object, &__object, __args...); }
607 /// Implementation of @c mem_fn for volatile member function pointers.
608 template<typename _Res, typename _Class, typename... _ArgTypes>
609 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
610 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
613 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
615 template<typename _Tp>
617 _M_call(_Tp& __object, const volatile _Class *,
618 _ArgTypes... __args) const
619 { return (__object.*__pmf)(__args...); }
621 template<typename _Tp>
623 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
624 { return ((*__ptr).*__pmf)(__args...); }
627 typedef _Res result_type;
629 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
633 operator()(volatile _Class& __object, _ArgTypes... __args) const
634 { return (__object.*__pmf)(__args...); }
638 operator()(volatile _Class* __object, _ArgTypes... __args) const
639 { return (__object->*__pmf)(__args...); }
641 // Handle smart pointers, references and pointers to derived
642 template<typename _Tp>
644 operator()(_Tp& __object, _ArgTypes... __args) const
645 { return _M_call(__object, &__object, __args...); }
651 /// Implementation of @c mem_fn for const volatile member function pointers.
652 template<typename _Res, typename _Class, typename... _ArgTypes>
653 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
654 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
657 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
659 template<typename _Tp>
661 _M_call(_Tp& __object, const volatile _Class *,
662 _ArgTypes... __args) const
663 { return (__object.*__pmf)(__args...); }
665 template<typename _Tp>
667 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
668 { return ((*__ptr).*__pmf)(__args...); }
671 typedef _Res result_type;
673 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
677 operator()(const volatile _Class& __object, _ArgTypes... __args) const
678 { return (__object.*__pmf)(__args...); }
682 operator()(const volatile _Class* __object, _ArgTypes... __args) const
683 { return (__object->*__pmf)(__args...); }
685 // Handle smart pointers, references and pointers to derived
686 template<typename _Tp>
687 _Res operator()(_Tp& __object, _ArgTypes... __args) const
688 { return _M_call(__object, &__object, __args...); }
695 template<typename _Res, typename _Class>
696 class _Mem_fn<_Res _Class::*>
698 // This bit of genius is due to Peter Dimov, improved slightly by
700 template<typename _Tp>
702 _M_call(_Tp& __object, _Class *) const
703 { return __object.*__pm; }
705 template<typename _Tp, typename _Up>
707 _M_call(_Tp& __object, _Up * const *) const
708 { return (*__object).*__pm; }
710 template<typename _Tp, typename _Up>
712 _M_call(_Tp& __object, const _Up * const *) const
713 { return (*__object).*__pm; }
715 template<typename _Tp>
717 _M_call(_Tp& __object, const _Class *) const
718 { return __object.*__pm; }
720 template<typename _Tp>
722 _M_call(_Tp& __ptr, const volatile void*) const
723 { return (*__ptr).*__pm; }
725 template<typename _Tp> static _Tp& __get_ref();
727 template<typename _Tp>
728 static __sfinae_types::__one __check_const(_Tp&, _Class*);
729 template<typename _Tp, typename _Up>
730 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
731 template<typename _Tp, typename _Up>
732 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
733 template<typename _Tp>
734 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
735 template<typename _Tp>
736 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
739 template<typename _Tp>
741 : _Mem_fn_const_or_non<_Res,
742 (sizeof(__sfinae_types::__two)
743 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
746 template<typename _Signature>
749 template<typename _CVMem, typename _Tp>
750 struct result<_CVMem(_Tp)>
751 : public _Result_type<_Tp> { };
753 template<typename _CVMem, typename _Tp>
754 struct result<_CVMem(_Tp&)>
755 : public _Result_type<_Tp> { };
758 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
762 operator()(_Class& __object) const
763 { return __object.*__pm; }
766 operator()(const _Class& __object) const
767 { return __object.*__pm; }
771 operator()(_Class* __object) const
772 { return __object->*__pm; }
775 operator()(const _Class* __object) const
776 { return __object->*__pm; }
778 // Handle smart pointers and derived
779 template<typename _Tp>
780 typename _Result_type<_Tp>::type
781 operator()(_Tp& __unknown) const
782 { return _M_call(__unknown, &__unknown); }
789 * @brief Returns a function object that forwards to the member
792 template<typename _Tp, typename _Class>
793 inline _Mem_fn<_Tp _Class::*>
794 mem_fn(_Tp _Class::* __pm)
796 return _Mem_fn<_Tp _Class::*>(__pm);
800 * @brief Determines if the given type _Tp is a function object
801 * should be treated as a subexpression when evaluating calls to
802 * function objects returned by bind(). [TR1 3.6.1]
804 template<typename _Tp>
805 struct is_bind_expression
806 { static const bool value = false; };
808 template<typename _Tp>
809 const bool is_bind_expression<_Tp>::value;
812 * @brief Determines if the given type _Tp is a placeholder in a
813 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
815 template<typename _Tp>
816 struct is_placeholder
817 { static const int value = 0; };
819 template<typename _Tp>
820 const int is_placeholder<_Tp>::value;
822 /// The type of placeholder objects defined by libstdc++.
823 template<int _Num> struct _Placeholder { };
825 /** @namespace std::placeholders
826 * @brief ISO C++ 0x entities sub namespace for functional.
828 * Define a large number of placeholders. There is no way to
829 * simplify this with variadic templates, because we're introducing
830 * unique names for each.
832 namespace placeholders
845 _Placeholder<10> _10;
846 _Placeholder<11> _11;
847 _Placeholder<12> _12;
848 _Placeholder<13> _13;
849 _Placeholder<14> _14;
850 _Placeholder<15> _15;
851 _Placeholder<16> _16;
852 _Placeholder<17> _17;
853 _Placeholder<18> _18;
854 _Placeholder<19> _19;
855 _Placeholder<20> _20;
856 _Placeholder<21> _21;
857 _Placeholder<22> _22;
858 _Placeholder<23> _23;
859 _Placeholder<24> _24;
860 _Placeholder<25> _25;
861 _Placeholder<26> _26;
862 _Placeholder<27> _27;
863 _Placeholder<28> _28;
864 _Placeholder<29> _29;
869 * Partial specialization of is_placeholder that provides the placeholder
870 * number for the placeholder objects defined by libstdc++.
873 struct is_placeholder<_Placeholder<_Num> >
874 { static const int value = _Num; };
877 const int is_placeholder<_Placeholder<_Num> >::value;
880 * Stores a tuple of indices. Used by bind() to extract the elements
883 template<int... _Indexes>
884 struct _Index_tuple { };
886 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
887 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
888 struct _Build_index_tuple;
890 template<std::size_t _Num, int... _Indexes>
891 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
892 : _Build_index_tuple<_Num - 1,
893 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
897 template<int... _Indexes>
898 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
900 typedef _Index_tuple<_Indexes...> __type;
904 * Used by _Safe_tuple_element to indicate that there is no tuple
905 * element at this position.
907 struct _No_tuple_element;
910 * Implementation helper for _Safe_tuple_element. This primary
911 * template handles the case where it is safe to use @c
914 template<int __i, typename _Tuple, bool _IsSafe>
915 struct _Safe_tuple_element_impl
916 : tuple_element<__i, _Tuple> { };
919 * Implementation helper for _Safe_tuple_element. This partial
920 * specialization handles the case where it is not safe to use @c
921 * tuple_element. We just return @c _No_tuple_element.
923 template<int __i, typename _Tuple>
924 struct _Safe_tuple_element_impl<__i, _Tuple, false>
926 typedef _No_tuple_element type;
930 * Like tuple_element, but returns @c _No_tuple_element when
931 * tuple_element would return an error.
933 template<int __i, typename _Tuple>
934 struct _Safe_tuple_element
935 : _Safe_tuple_element_impl<__i, _Tuple,
936 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
941 * Maps an argument to bind() into an actual argument to the bound
942 * function object [TR1 3.6.3/5]. Only the first parameter should
943 * be specified: the rest are used to determine among the various
944 * implementations. Note that, although this class is a function
945 * object, it isn't entirely normal because it takes only two
946 * parameters regardless of the number of parameters passed to the
947 * bind expression. The first parameter is the bound argument and
948 * the second parameter is a tuple containing references to the
949 * rest of the arguments.
951 template<typename _Arg,
952 bool _IsBindExp = is_bind_expression<_Arg>::value,
953 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
957 * If the argument is reference_wrapper<_Tp>, returns the
958 * underlying reference. [TR1 3.6.3/5 bullet 1]
960 template<typename _Tp>
961 class _Mu<reference_wrapper<_Tp>, false, false>
964 typedef _Tp& result_type;
966 /* Note: This won't actually work for const volatile
967 * reference_wrappers, because reference_wrapper::get() is const
968 * but not volatile-qualified. This might be a defect in the TR.
970 template<typename _CVRef, typename _Tuple>
972 operator()(_CVRef& __arg, const _Tuple&) const volatile
973 { return __arg.get(); }
977 * If the argument is a bind expression, we invoke the underlying
978 * function object with the same cv-qualifiers as we are given and
979 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
981 template<typename _Arg>
982 class _Mu<_Arg, true, false>
985 template<typename _Signature> class result;
987 // Determine the result type when we pass the arguments along. This
988 // involves passing along the cv-qualifiers placed on _Mu and
989 // unwrapping the argument bundle.
990 template<typename _CVMu, typename _CVArg, typename... _Args>
991 class result<_CVMu(_CVArg, tuple<_Args...>)>
992 : public result_of<_CVArg(_Args...)> { };
994 template<typename _CVArg, typename... _Args>
995 typename result_of<_CVArg(_Args...)>::type
996 operator()(_CVArg& __arg,
997 const tuple<_Args...>& __tuple) const volatile
999 // Construct an index tuple and forward to __call
1000 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1002 return this->__call(__arg, __tuple, _Indexes());
1006 // Invokes the underlying function object __arg by unpacking all
1007 // of the arguments in the tuple.
1008 template<typename _CVArg, typename... _Args, int... _Indexes>
1009 typename result_of<_CVArg(_Args...)>::type
1010 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1011 const _Index_tuple<_Indexes...>&) const volatile
1013 return __arg(_GLIBCXX_TR1 get<_Indexes>(__tuple)...);
1018 * If the argument is a placeholder for the Nth argument, returns
1019 * a reference to the Nth argument to the bind function object.
1020 * [TR1 3.6.3/5 bullet 3]
1022 template<typename _Arg>
1023 class _Mu<_Arg, false, true>
1026 template<typename _Signature> class result;
1028 template<typename _CVMu, typename _CVArg, typename _Tuple>
1029 class result<_CVMu(_CVArg, _Tuple)>
1031 // Add a reference, if it hasn't already been done for us.
1032 // This allows us to be a little bit sloppy in constructing
1033 // the tuple that we pass to result_of<...>.
1034 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1039 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
1040 typedef typename add_lvalue_reference<__base_type>::type type;
1042 typedef typename add_reference<__base_type>::type type;
1046 template<typename _Tuple>
1047 typename result<_Mu(_Arg, _Tuple)>::type
1048 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1050 return ::std::_GLIBCXX_TR1 get<(is_placeholder<_Arg>::value
1056 * If the argument is just a value, returns a reference to that
1057 * value. The cv-qualifiers on the reference are the same as the
1058 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1060 template<typename _Arg>
1061 class _Mu<_Arg, false, false>
1064 template<typename _Signature> struct result;
1066 template<typename _CVMu, typename _CVArg, typename _Tuple>
1067 struct result<_CVMu(_CVArg, _Tuple)>
1069 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
1070 typedef typename add_lvalue_reference<_CVArg>::type type;
1072 typedef typename add_reference<_CVArg>::type type;
1076 // Pick up the cv-qualifiers of the argument
1077 template<typename _CVArg, typename _Tuple>
1079 operator()(_CVArg& __arg, const _Tuple&) const volatile
1084 * Maps member pointers into instances of _Mem_fn but leaves all
1085 * other function objects untouched. Used by tr1::bind(). The
1086 * primary template handles the non--member-pointer case.
1088 template<typename _Tp>
1089 struct _Maybe_wrap_member_pointer
1094 __do_wrap(const _Tp& __x)
1099 * Maps member pointers into instances of _Mem_fn but leaves all
1100 * other function objects untouched. Used by tr1::bind(). This
1101 * partial specialization handles the member pointer case.
1103 template<typename _Tp, typename _Class>
1104 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1106 typedef _Mem_fn<_Tp _Class::*> type;
1109 __do_wrap(_Tp _Class::* __pm)
1110 { return type(__pm); }
1113 /// Type of the function object returned from bind().
1114 template<typename _Signature>
1117 template<typename _Functor, typename... _Bound_args>
1118 class _Bind<_Functor(_Bound_args...)>
1119 : public _Weak_result_type<_Functor>
1121 typedef _Bind __self_type;
1122 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1126 tuple<_Bound_args...> _M_bound_args;
1129 template<typename... _Args, int... _Indexes>
1131 _Functor(typename result_of<_Mu<_Bound_args>
1132 (_Bound_args, tuple<_Args...>)>::type...)
1134 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1136 return _M_f(_Mu<_Bound_args>()
1137 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1141 template<typename... _Args, int... _Indexes>
1143 const _Functor(typename result_of<_Mu<_Bound_args>
1144 (const _Bound_args, tuple<_Args...>)
1146 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1148 return _M_f(_Mu<_Bound_args>()
1149 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1153 template<typename... _Args, int... _Indexes>
1155 volatile _Functor(typename result_of<_Mu<_Bound_args>
1156 (volatile _Bound_args, tuple<_Args...>)
1158 __call(const tuple<_Args...>& __args,
1159 _Index_tuple<_Indexes...>) volatile
1161 return _M_f(_Mu<_Bound_args>()
1162 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1165 // Call as const volatile
1166 template<typename... _Args, int... _Indexes>
1168 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1169 (const volatile _Bound_args,
1172 __call(const tuple<_Args...>& __args,
1173 _Index_tuple<_Indexes...>) const volatile
1175 return _M_f(_Mu<_Bound_args>()
1176 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1180 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1181 : _M_f(__f), _M_bound_args(__bound_args...) { }
1184 template<typename... _Args>
1186 _Functor(typename result_of<_Mu<_Bound_args>
1187 (_Bound_args, tuple<_Args...>)>::type...)
1189 operator()(_Args&... __args)
1191 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1195 template<typename... _Args>
1197 const _Functor(typename result_of<_Mu<_Bound_args>
1198 (const _Bound_args, tuple<_Args...>)>::type...)
1200 operator()(_Args&... __args) const
1202 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1207 template<typename... _Args>
1209 volatile _Functor(typename result_of<_Mu<_Bound_args>
1210 (volatile _Bound_args, tuple<_Args...>)>::type...)
1212 operator()(_Args&... __args) volatile
1214 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1218 // Call as const volatile
1219 template<typename... _Args>
1221 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1222 (const volatile _Bound_args,
1223 tuple<_Args...>)>::type...)
1225 operator()(_Args&... __args) const volatile
1227 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1231 /// Type of the function object returned from bind<R>().
1232 template<typename _Result, typename _Signature>
1233 struct _Bind_result;
1235 template<typename _Result, typename _Functor, typename... _Bound_args>
1236 class _Bind_result<_Result, _Functor(_Bound_args...)>
1238 typedef _Bind_result __self_type;
1239 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1243 tuple<_Bound_args...> _M_bound_args;
1246 template<typename... _Args, int... _Indexes>
1248 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1250 return _M_f(_Mu<_Bound_args>()
1251 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1255 template<typename... _Args, int... _Indexes>
1257 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1259 return _M_f(_Mu<_Bound_args>()
1260 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1264 template<typename... _Args, int... _Indexes>
1266 __call(const tuple<_Args...>& __args,
1267 _Index_tuple<_Indexes...>) volatile
1269 return _M_f(_Mu<_Bound_args>()
1270 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1273 // Call as const volatile
1274 template<typename... _Args, int... _Indexes>
1276 __call(const tuple<_Args...>& __args,
1277 _Index_tuple<_Indexes...>) const volatile
1279 return _M_f(_Mu<_Bound_args>()
1280 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1284 typedef _Result result_type;
1287 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1288 : _M_f(__f), _M_bound_args(__bound_args...) { }
1291 template<typename... _Args>
1293 operator()(_Args&... __args)
1295 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1299 template<typename... _Args>
1301 operator()(_Args&... __args) const
1303 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1307 template<typename... _Args>
1309 operator()(_Args&... __args) volatile
1311 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1314 // Call as const volatile
1315 template<typename... _Args>
1317 operator()(_Args&... __args) const volatile
1319 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1323 /// Class template _Bind is always a bind expression.
1324 template<typename _Signature>
1325 struct is_bind_expression<_Bind<_Signature> >
1326 { static const bool value = true; };
1328 template<typename _Signature>
1329 const bool is_bind_expression<_Bind<_Signature> >::value;
1331 /// Class template _Bind_result is always a bind expression.
1332 template<typename _Result, typename _Signature>
1333 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1334 { static const bool value = true; };
1336 template<typename _Result, typename _Signature>
1337 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1340 template<typename _Functor, typename... _ArgTypes>
1342 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1343 bind(_Functor __f, _ArgTypes... __args)
1345 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1346 typedef typename __maybe_type::type __functor_type;
1347 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1348 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1351 template<typename _Result, typename _Functor, typename... _ArgTypes>
1353 _Bind_result<_Result,
1354 typename _Maybe_wrap_member_pointer<_Functor>::type
1356 bind(_Functor __f, _ArgTypes... __args)
1358 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1359 typedef typename __maybe_type::type __functor_type;
1360 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1362 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1366 * @brief Exception class thrown when class template function's
1367 * operator() is called with an empty target.
1370 class bad_function_call : public std::exception { };
1373 * The integral constant expression 0 can be converted into a
1374 * pointer to this type. It is used by the function template to
1375 * accept NULL pointers.
1377 struct _M_clear_type;
1380 * Trait identifying "location-invariant" types, meaning that the
1381 * address of the object (or any of its members) will not escape.
1382 * Also implies a trivial copy constructor and assignment operator.
1384 template<typename _Tp>
1385 struct __is_location_invariant
1386 : integral_constant<bool,
1387 (is_pointer<_Tp>::value
1388 || is_member_pointer<_Tp>::value)>
1392 class _Undefined_class;
1397 const void* _M_const_object;
1398 void (*_M_function_pointer)();
1399 void (_Undefined_class::*_M_member_pointer)();
1404 void* _M_access() { return &_M_pod_data[0]; }
1405 const void* _M_access() const { return &_M_pod_data[0]; }
1407 template<typename _Tp>
1410 { return *static_cast<_Tp*>(_M_access()); }
1412 template<typename _Tp>
1415 { return *static_cast<const _Tp*>(_M_access()); }
1417 _Nocopy_types _M_unused;
1418 char _M_pod_data[sizeof(_Nocopy_types)];
1421 enum _Manager_operation
1429 // Simple type wrapper that helps avoid annoying const problems
1430 // when casting between void pointers and pointers-to-pointers.
1431 template<typename _Tp>
1432 struct _Simple_type_wrapper
1434 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1439 template<typename _Tp>
1440 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1441 : __is_location_invariant<_Tp>
1445 // Converts a reference to a function object into a callable
1447 template<typename _Functor>
1449 __callable_functor(_Functor& __f)
1452 template<typename _Member, typename _Class>
1453 inline _Mem_fn<_Member _Class::*>
1454 __callable_functor(_Member _Class::* &__p)
1455 { return mem_fn(__p); }
1457 template<typename _Member, typename _Class>
1458 inline _Mem_fn<_Member _Class::*>
1459 __callable_functor(_Member _Class::* const &__p)
1460 { return mem_fn(__p); }
1462 template<typename _Signature>
1465 /// Base class of all polymorphic function object wrappers.
1466 class _Function_base
1469 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1470 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1472 template<typename _Functor>
1476 static const bool __stored_locally =
1477 (__is_location_invariant<_Functor>::value
1478 && sizeof(_Functor) <= _M_max_size
1479 && __alignof__(_Functor) <= _M_max_align
1480 && (_M_max_align % __alignof__(_Functor) == 0));
1482 typedef integral_constant<bool, __stored_locally> _Local_storage;
1484 // Retrieve a pointer to the function object
1486 _M_get_pointer(const _Any_data& __source)
1488 const _Functor* __ptr =
1489 __stored_locally? &__source._M_access<_Functor>()
1490 /* have stored a pointer */ : __source._M_access<_Functor*>();
1491 return const_cast<_Functor*>(__ptr);
1494 // Clone a location-invariant function object that fits within
1495 // an _Any_data structure.
1497 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1499 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1502 // Clone a function object that is not location-invariant or
1503 // that cannot fit into an _Any_data structure.
1505 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1507 __dest._M_access<_Functor*>() =
1508 new _Functor(*__source._M_access<_Functor*>());
1511 // Destroying a location-invariant object may still require
1514 _M_destroy(_Any_data& __victim, true_type)
1516 __victim._M_access<_Functor>().~_Functor();
1519 // Destroying an object located on the heap.
1521 _M_destroy(_Any_data& __victim, false_type)
1523 delete __victim._M_access<_Functor*>();
1528 _M_manager(_Any_data& __dest, const _Any_data& __source,
1529 _Manager_operation __op)
1534 case __get_type_info:
1535 __dest._M_access<const type_info*>() = &typeid(_Functor);
1538 case __get_functor_ptr:
1539 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1542 case __clone_functor:
1543 _M_clone(__dest, __source, _Local_storage());
1546 case __destroy_functor:
1547 _M_destroy(__dest, _Local_storage());
1554 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1555 { _M_init_functor(__functor, __f, _Local_storage()); }
1557 template<typename _Signature>
1559 _M_not_empty_function(const function<_Signature>& __f)
1562 template<typename _Tp>
1564 _M_not_empty_function(const _Tp*& __fp)
1567 template<typename _Class, typename _Tp>
1569 _M_not_empty_function(_Tp _Class::* const& __mp)
1572 template<typename _Tp>
1574 _M_not_empty_function(const _Tp&)
1579 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1580 { new (__functor._M_access()) _Functor(__f); }
1583 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1584 { __functor._M_access<_Functor*>() = new _Functor(__f); }
1587 template<typename _Functor>
1588 class _Ref_manager : public _Base_manager<_Functor*>
1590 typedef _Function_base::_Base_manager<_Functor*> _Base;
1594 _M_manager(_Any_data& __dest, const _Any_data& __source,
1595 _Manager_operation __op)
1600 case __get_type_info:
1601 __dest._M_access<const type_info*>() = &typeid(_Functor);
1604 case __get_functor_ptr:
1605 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1606 return is_const<_Functor>::value;
1610 _Base::_M_manager(__dest, __source, __op);
1616 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1618 // TBD: Use address_of function instead.
1619 _Base::_M_init_functor(__functor, &__f.get());
1623 _Function_base() : _M_manager(0) { }
1628 _M_manager(_M_functor, _M_functor, __destroy_functor);
1632 bool _M_empty() const { return !_M_manager; }
1634 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1635 _Manager_operation);
1637 _Any_data _M_functor;
1638 _Manager_type _M_manager;
1641 template<typename _Signature, typename _Functor>
1642 class _Function_handler;
1644 template<typename _Res, typename _Functor, typename... _ArgTypes>
1645 class _Function_handler<_Res(_ArgTypes...), _Functor>
1646 : public _Function_base::_Base_manager<_Functor>
1648 typedef _Function_base::_Base_manager<_Functor> _Base;
1652 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1654 return (*_Base::_M_get_pointer(__functor))(__args...);
1658 template<typename _Functor, typename... _ArgTypes>
1659 class _Function_handler<void(_ArgTypes...), _Functor>
1660 : public _Function_base::_Base_manager<_Functor>
1662 typedef _Function_base::_Base_manager<_Functor> _Base;
1666 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1668 (*_Base::_M_get_pointer(__functor))(__args...);
1672 template<typename _Res, typename _Functor, typename... _ArgTypes>
1673 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1674 : public _Function_base::_Ref_manager<_Functor>
1676 typedef _Function_base::_Ref_manager<_Functor> _Base;
1680 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1683 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1687 template<typename _Functor, typename... _ArgTypes>
1688 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1689 : public _Function_base::_Ref_manager<_Functor>
1691 typedef _Function_base::_Ref_manager<_Functor> _Base;
1695 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1697 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1701 template<typename _Class, typename _Member, typename _Res,
1702 typename... _ArgTypes>
1703 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1704 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1706 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1711 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1714 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1718 template<typename _Class, typename _Member, typename... _ArgTypes>
1719 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1720 : public _Function_base::_Base_manager<
1721 _Simple_type_wrapper< _Member _Class::* > >
1723 typedef _Member _Class::* _Functor;
1724 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1725 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1729 _M_manager(_Any_data& __dest, const _Any_data& __source,
1730 _Manager_operation __op)
1735 case __get_type_info:
1736 __dest._M_access<const type_info*>() = &typeid(_Functor);
1739 case __get_functor_ptr:
1740 __dest._M_access<_Functor*>() =
1741 &_Base::_M_get_pointer(__source)->__value;
1745 _Base::_M_manager(__dest, __source, __op);
1751 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1754 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1759 template<typename _Res, typename... _ArgTypes>
1760 class function<_Res(_ArgTypes...)>
1761 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1762 private _Function_base
1764 /// This class is used to implement the safe_bool idiom.
1767 _Hidden_type* _M_bool;
1770 /// This typedef is used to implement the safe_bool idiom.
1771 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1773 typedef _Res _Signature_type(_ArgTypes...);
1775 struct _Useless { };
1778 typedef _Res result_type;
1780 // [3.7.2.1] construct/copy/destroy
1783 * @brief Default construct creates an empty function call wrapper.
1784 * @post @c !(bool)*this
1786 function() : _Function_base() { }
1789 * @brief Default construct creates an empty function call wrapper.
1790 * @post @c !(bool)*this
1792 function(_M_clear_type*) : _Function_base() { }
1795 * @brief %Function copy constructor.
1796 * @param x A %function object with identical call signature.
1797 * @pre @c (bool)*this == (bool)x
1799 * The newly-created %function contains a copy of the target of @a
1800 * x (if it has one).
1802 function(const function& __x);
1805 * @brief Builds a %function that targets a copy of the incoming
1807 * @param f A %function object that is callable with parameters of
1808 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1811 * The newly-created %function object will target a copy of @a
1812 * f. If @a f is @c reference_wrapper<F>, then this function
1813 * object will contain a reference to the function object @c
1814 * f.get(). If @a f is a NULL function pointer or NULL
1815 * pointer-to-member, the newly-created object will be empty.
1817 * If @a f is a non-NULL function pointer or an object of type @c
1818 * reference_wrapper<F>, this function will not throw.
1820 template<typename _Functor>
1821 function(_Functor __f,
1822 typename __gnu_cxx::__enable_if<
1823 !is_integral<_Functor>::value, _Useless>::__type
1827 * @brief %Function assignment operator.
1828 * @param x A %function with identical call signature.
1829 * @post @c (bool)*this == (bool)x
1832 * The target of @a x is copied to @c *this. If @a x has no
1833 * target, then @c *this will be empty.
1835 * If @a x targets a function pointer or a reference to a function
1836 * object, then this operation will not throw an exception.
1839 operator=(const function& __x)
1841 function(__x).swap(*this);
1846 * @brief %Function assignment to zero.
1847 * @post @c !(bool)*this
1850 * The target of @a *this is deallocated, leaving it empty.
1853 operator=(_M_clear_type*)
1857 _M_manager(_M_functor, _M_functor, __destroy_functor);
1865 * @brief %Function assignment to a new target.
1866 * @param f A %function object that is callable with parameters of
1867 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1871 * This %function object wrapper will target a copy of @a
1872 * f. If @a f is @c reference_wrapper<F>, then this function
1873 * object will contain a reference to the function object @c
1874 * f.get(). If @a f is a NULL function pointer or NULL
1875 * pointer-to-member, @c this object will be empty.
1877 * If @a f is a non-NULL function pointer or an object of type @c
1878 * reference_wrapper<F>, this function will not throw.
1880 template<typename _Functor>
1881 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
1883 operator=(_Functor __f)
1885 function(__f).swap(*this);
1889 // [3.7.2.2] function modifiers
1892 * @brief Swap the targets of two %function objects.
1893 * @param f A %function with identical call signature.
1895 * Swap the targets of @c this function object and @a f. This
1896 * function will not throw an exception.
1898 void swap(function& __x)
1900 _Any_data __old_functor = _M_functor;
1901 _M_functor = __x._M_functor;
1902 __x._M_functor = __old_functor;
1903 _Manager_type __old_manager = _M_manager;
1904 _M_manager = __x._M_manager;
1905 __x._M_manager = __old_manager;
1906 _Invoker_type __old_invoker = _M_invoker;
1907 _M_invoker = __x._M_invoker;
1908 __x._M_invoker = __old_invoker;
1911 // [3.7.2.3] function capacity
1914 * @brief Determine if the %function wrapper has a target.
1916 * @return @c true when this %function object contains a target,
1917 * or @c false when it is empty.
1919 * This function will not throw an exception.
1921 operator _Safe_bool() const
1926 return &_Hidden_type::_M_bool;
1929 // [3.7.2.4] function invocation
1932 * @brief Invokes the function targeted by @c *this.
1933 * @returns the result of the target.
1934 * @throws bad_function_call when @c !(bool)*this
1936 * The function call operator invokes the target function object
1937 * stored by @c this.
1939 _Res operator()(_ArgTypes... __args) const;
1942 // [3.7.2.5] function target access
1944 * @brief Determine the type of the target of this function object
1947 * @returns the type identifier of the target function object, or
1948 * @c typeid(void) if @c !(bool)*this.
1950 * This function will not throw an exception.
1952 const type_info& target_type() const;
1955 * @brief Access the stored target function object.
1957 * @return Returns a pointer to the stored target function object,
1958 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
1961 * This function will not throw an exception.
1963 template<typename _Functor> _Functor* target();
1966 template<typename _Functor> const _Functor* target() const;
1970 // [3.7.2.6] undefined operators
1971 template<typename _Function>
1972 void operator==(const function<_Function>&) const;
1973 template<typename _Function>
1974 void operator!=(const function<_Function>&) const;
1976 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
1977 _Invoker_type _M_invoker;
1980 template<typename _Res, typename... _ArgTypes>
1981 function<_Res(_ArgTypes...)>::
1982 function(const function& __x)
1987 _M_invoker = __x._M_invoker;
1988 _M_manager = __x._M_manager;
1989 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
1993 template<typename _Res, typename... _ArgTypes>
1994 template<typename _Functor>
1995 function<_Res(_ArgTypes...)>::
1996 function(_Functor __f,
1997 typename __gnu_cxx::__enable_if<
1998 !is_integral<_Functor>::value, _Useless>::__type)
2001 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2003 if (_My_handler::_M_not_empty_function(__f))
2005 _M_invoker = &_My_handler::_M_invoke;
2006 _M_manager = &_My_handler::_M_manager;
2007 _My_handler::_M_init_functor(_M_functor, __f);
2011 template<typename _Res, typename... _ArgTypes>
2013 function<_Res(_ArgTypes...)>::
2014 operator()(_ArgTypes... __args) const
2019 throw bad_function_call();
2024 return _M_invoker(_M_functor, __args...);
2028 template<typename _Res, typename... _ArgTypes>
2030 function<_Res(_ArgTypes...)>::
2035 _Any_data __typeinfo_result;
2036 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2037 return *__typeinfo_result._M_access<const type_info*>();
2040 return typeid(void);
2043 template<typename _Res, typename... _ArgTypes>
2044 template<typename _Functor>
2046 function<_Res(_ArgTypes...)>::
2049 if (typeid(_Functor) == target_type() && _M_manager)
2052 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2053 && !is_const<_Functor>::value)
2056 return __ptr._M_access<_Functor*>();
2062 template<typename _Res, typename... _ArgTypes>
2063 template<typename _Functor>
2065 function<_Res(_ArgTypes...)>::
2068 if (typeid(_Functor) == target_type() && _M_manager)
2071 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2072 return __ptr._M_access<const _Functor*>();
2079 // [3.7.2.7] null pointer comparisons
2082 * @brief Compares a polymorphic function object wrapper against 0
2083 * (the NULL pointer).
2084 * @returns @c true if the wrapper has no target, @c false otherwise
2086 * This function will not throw an exception.
2088 template<typename _Signature>
2090 operator==(const function<_Signature>& __f, _M_clear_type*)
2094 template<typename _Signature>
2096 operator==(_M_clear_type*, const function<_Signature>& __f)
2100 * @brief Compares a polymorphic function object wrapper against 0
2101 * (the NULL pointer).
2102 * @returns @c false if the wrapper has no target, @c true otherwise
2104 * This function will not throw an exception.
2106 template<typename _Signature>
2108 operator!=(const function<_Signature>& __f, _M_clear_type*)
2112 template<typename _Signature>
2114 operator!=(_M_clear_type*, const function<_Signature>& __f)
2117 // [3.7.2.8] specialized algorithms
2120 * @brief Swap the targets of two polymorphic function object wrappers.
2122 * This function will not throw an exception.
2124 template<typename _Signature>
2126 swap(function<_Signature>& __x, function<_Signature>& __y)
2129 _GLIBCXX_END_NAMESPACE_TR1