1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2007 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 2, 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 // 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,
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.
30 /** @file tr1_impl/functional
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
37 _GLIBCXX_BEGIN_NAMESPACE_TR1
39 template<typename _MemberPointer>
44 * Actual implementation of _Has_result_type, which uses SFINAE to
45 * determine if the type _Tp has a publicly-accessible member type
49 template<typename _Tp>
50 class _Has_result_type_helper : __sfinae_types
52 template<typename _Up>
56 template<typename _Up>
57 static __one __test(_Wrap_type<typename _Up::result_type>*);
59 template<typename _Up>
60 static __two __test(...);
63 static const bool value = sizeof(__test<_Tp>(0)) == 1;
66 template<typename _Tp>
67 struct _Has_result_type
68 : integral_constant<bool,
69 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
74 * If we have found a result_type, extract it.
77 template<bool _Has_result_type, typename _Functor>
78 struct _Maybe_get_result_type
81 template<typename _Functor>
82 struct _Maybe_get_result_type<true, _Functor>
84 typedef typename _Functor::result_type result_type;
89 * Base class for any function object that has a weak result type, as
90 * defined in 3.3/3 of TR1.
93 template<typename _Functor>
94 struct _Weak_result_type_impl
95 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
101 * Retrieve the result type for a function type.
104 template<typename _Res, typename... _ArgTypes>
105 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
107 typedef _Res result_type;
112 * Retrieve the result type for a function reference.
115 template<typename _Res, typename... _ArgTypes>
116 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
118 typedef _Res result_type;
123 * Retrieve the result type for a function pointer.
126 template<typename _Res, typename... _ArgTypes>
127 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
129 typedef _Res result_type;
134 * Retrieve result type for a member function pointer.
137 template<typename _Res, typename _Class, typename... _ArgTypes>
138 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
140 typedef _Res result_type;
145 * Retrieve result type for a const member function pointer.
148 template<typename _Res, typename _Class, typename... _ArgTypes>
149 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
151 typedef _Res result_type;
156 * Retrieve result type for a volatile member function pointer.
159 template<typename _Res, typename _Class, typename... _ArgTypes>
160 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
162 typedef _Res result_type;
167 * Retrieve result type for a const volatile member function pointer.
170 template<typename _Res, typename _Class, typename... _ArgTypes>
171 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
173 typedef _Res result_type;
178 * Strip top-level cv-qualifiers from the function object and let
179 * _Weak_result_type_impl perform the real work.
182 template<typename _Functor>
183 struct _Weak_result_type
184 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
188 template<typename _Signature>
193 * Actual implementation of result_of. When _Has_result_type is
194 * true, gets its result from _Weak_result_type. Otherwise, uses
195 * the function object's member template result to extract the
199 template<bool _Has_result_type, typename _Signature>
200 struct _Result_of_impl;
202 // Handle member data pointers using _Mem_fn's logic
203 template<typename _Res, typename _Class, typename _T1>
204 struct _Result_of_impl<false, _Res _Class::*(_T1)>
206 typedef typename _Mem_fn<_Res _Class::*>
207 ::template _Result_type<_T1>::type type;
212 * Determine whether we can determine a result type from @c Functor
216 template<typename _Functor, typename... _ArgTypes>
217 class result_of<_Functor(_ArgTypes...)>
218 : public _Result_of_impl<
219 _Has_result_type<_Weak_result_type<_Functor> >::value,
220 _Functor(_ArgTypes...)>
226 * We already know the result type for @c Functor; use it.
229 template<typename _Functor, typename... _ArgTypes>
230 struct _Result_of_impl<true, _Functor(_ArgTypes...)>
232 typedef typename _Weak_result_type<_Functor>::result_type type;
237 * We need to compute the result type for this invocation the hard
241 template<typename _Functor, typename... _ArgTypes>
242 struct _Result_of_impl<false, _Functor(_ArgTypes...)>
244 typedef typename _Functor
245 ::template result<_Functor(_ArgTypes...)>::type type;
250 * It is unsafe to access ::result when there are zero arguments, so we
251 * return @c void instead.
254 template<typename _Functor>
255 struct _Result_of_impl<false, _Functor()>
262 * Determines if the type _Tp derives from unary_function.
265 template<typename _Tp>
266 struct _Derives_from_unary_function : __sfinae_types
269 template<typename _T1, typename _Res>
270 static __one __test(const volatile unary_function<_T1, _Res>*);
272 // It's tempting to change "..." to const volatile void*, but
273 // that fails when _Tp is a function type.
274 static __two __test(...);
277 static const bool value = sizeof(__test((_Tp*)0)) == 1;
282 * Determines if the type _Tp derives from binary_function.
285 template<typename _Tp>
286 struct _Derives_from_binary_function : __sfinae_types
289 template<typename _T1, typename _T2, typename _Res>
290 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
292 // It's tempting to change "..." to const volatile void*, but
293 // that fails when _Tp is a function type.
294 static __two __test(...);
297 static const bool value = sizeof(__test((_Tp*)0)) == 1;
302 * Turns a function type into a function pointer type
305 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
306 struct _Function_to_function_pointer
311 template<typename _Tp>
312 struct _Function_to_function_pointer<_Tp, true>
319 * Invoke a function object, which may be either a member pointer or a
320 * function object. The first parameter will tell which.
323 template<typename _Functor, typename... _Args>
325 typename __gnu_cxx::__enable_if<
326 (!is_member_pointer<_Functor>::value
327 && !is_function<_Functor>::value
328 && !is_function<typename remove_pointer<_Functor>::type>::value),
329 typename result_of<_Functor(_Args...)>::type
331 __invoke(_Functor& __f, _Args&... __args)
333 return __f(__args...);
336 template<typename _Functor, typename... _Args>
338 typename __gnu_cxx::__enable_if<
339 (is_member_pointer<_Functor>::value
340 && !is_function<_Functor>::value
341 && !is_function<typename remove_pointer<_Functor>::type>::value),
342 typename result_of<_Functor(_Args...)>::type
344 __invoke(_Functor& __f, _Args&... __args)
346 return mem_fn(__f)(__args...);
349 // To pick up function references (that will become function pointers)
350 template<typename _Functor, typename... _Args>
352 typename __gnu_cxx::__enable_if<
353 (is_pointer<_Functor>::value
354 && is_function<typename remove_pointer<_Functor>::type>::value),
355 typename result_of<_Functor(_Args...)>::type
357 __invoke(_Functor __f, _Args&... __args)
359 return __f(__args...);
364 * Knowing which of unary_function and binary_function _Tp derives
365 * from, derives from the same and ensures that reference_wrapper
366 * will have a weak result type. See cases below.
369 template<bool _Unary, bool _Binary, typename _Tp>
370 struct _Reference_wrapper_base_impl;
372 // Not a unary_function or binary_function, so try a weak result type
373 template<typename _Tp>
374 struct _Reference_wrapper_base_impl<false, false, _Tp>
375 : _Weak_result_type<_Tp>
378 // unary_function but not binary_function
379 template<typename _Tp>
380 struct _Reference_wrapper_base_impl<true, false, _Tp>
381 : unary_function<typename _Tp::argument_type,
382 typename _Tp::result_type>
385 // binary_function but not unary_function
386 template<typename _Tp>
387 struct _Reference_wrapper_base_impl<false, true, _Tp>
388 : binary_function<typename _Tp::first_argument_type,
389 typename _Tp::second_argument_type,
390 typename _Tp::result_type>
393 // both unary_function and binary_function. import result_type to
395 template<typename _Tp>
396 struct _Reference_wrapper_base_impl<true, true, _Tp>
397 : unary_function<typename _Tp::argument_type,
398 typename _Tp::result_type>,
399 binary_function<typename _Tp::first_argument_type,
400 typename _Tp::second_argument_type,
401 typename _Tp::result_type>
403 typedef typename _Tp::result_type result_type;
408 * Derives from unary_function or binary_function when it
409 * can. Specializations handle all of the easy cases. The primary
410 * template determines what to do with a class type, which may
411 * derive from both unary_function and binary_function.
414 template<typename _Tp>
415 struct _Reference_wrapper_base
416 : _Reference_wrapper_base_impl<
417 _Derives_from_unary_function<_Tp>::value,
418 _Derives_from_binary_function<_Tp>::value,
422 // - a function type (unary)
423 template<typename _Res, typename _T1>
424 struct _Reference_wrapper_base<_Res(_T1)>
425 : unary_function<_T1, _Res>
428 // - a function type (binary)
429 template<typename _Res, typename _T1, typename _T2>
430 struct _Reference_wrapper_base<_Res(_T1, _T2)>
431 : binary_function<_T1, _T2, _Res>
434 // - a function pointer type (unary)
435 template<typename _Res, typename _T1>
436 struct _Reference_wrapper_base<_Res(*)(_T1)>
437 : unary_function<_T1, _Res>
440 // - a function pointer type (binary)
441 template<typename _Res, typename _T1, typename _T2>
442 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
443 : binary_function<_T1, _T2, _Res>
446 // - a pointer to member function type (unary, no qualifiers)
447 template<typename _Res, typename _T1>
448 struct _Reference_wrapper_base<_Res (_T1::*)()>
449 : unary_function<_T1*, _Res>
452 // - a pointer to member function type (binary, no qualifiers)
453 template<typename _Res, typename _T1, typename _T2>
454 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
455 : binary_function<_T1*, _T2, _Res>
458 // - a pointer to member function type (unary, const)
459 template<typename _Res, typename _T1>
460 struct _Reference_wrapper_base<_Res (_T1::*)() const>
461 : unary_function<const _T1*, _Res>
464 // - a pointer to member function type (binary, const)
465 template<typename _Res, typename _T1, typename _T2>
466 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
467 : binary_function<const _T1*, _T2, _Res>
470 // - a pointer to member function type (unary, volatile)
471 template<typename _Res, typename _T1>
472 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
473 : unary_function<volatile _T1*, _Res>
476 // - a pointer to member function type (binary, volatile)
477 template<typename _Res, typename _T1, typename _T2>
478 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
479 : binary_function<volatile _T1*, _T2, _Res>
482 // - a pointer to member function type (unary, const volatile)
483 template<typename _Res, typename _T1>
484 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
485 : unary_function<const volatile _T1*, _Res>
488 // - a pointer to member function type (binary, const volatile)
489 template<typename _Res, typename _T1, typename _T2>
490 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
491 : binary_function<const volatile _T1*, _T2, _Res>
494 template<typename _Tp>
495 class reference_wrapper
496 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
498 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
499 // so turn it into a function pointer type.
500 typedef typename _Function_to_function_pointer<_Tp>::type
508 reference_wrapper(_Tp& __indata): _M_data(&__indata)
511 reference_wrapper(const reference_wrapper<_Tp>& __inref):
512 _M_data(__inref._M_data)
516 operator=(const reference_wrapper<_Tp>& __inref)
518 _M_data = __inref._M_data;
522 operator _Tp&() const
523 { return this->get(); }
529 template<typename... _Args>
530 typename result_of<_M_func_type(_Args...)>::type
531 operator()(_Args&... __args) const
533 return __invoke(get(), __args...);
538 // Denotes a reference should be taken to a variable.
539 template<typename _Tp>
540 inline reference_wrapper<_Tp>
542 { return reference_wrapper<_Tp>(__t); }
544 // Denotes a const reference should be taken to a variable.
545 template<typename _Tp>
546 inline reference_wrapper<const _Tp>
548 { return reference_wrapper<const _Tp>(__t); }
550 template<typename _Tp>
551 inline reference_wrapper<_Tp>
552 ref(reference_wrapper<_Tp> __t)
553 { return ref(__t.get()); }
555 template<typename _Tp>
556 inline reference_wrapper<const _Tp>
557 cref(reference_wrapper<_Tp> __t)
558 { return cref(__t.get()); }
560 template<typename _Tp, bool>
561 struct _Mem_fn_const_or_non
563 typedef const _Tp& type;
566 template<typename _Tp>
567 struct _Mem_fn_const_or_non<_Tp, false>
574 * Derives from @c unary_function or @c binary_function, or perhaps
575 * nothing, depending on the number of arguments provided. The
576 * primary template is the basis case, which derives nothing.
579 template<typename _Res, typename... _ArgTypes>
580 struct _Maybe_unary_or_binary_function { };
584 * Derives from @c unary_function, as appropriate.
587 template<typename _Res, typename _T1>
588 struct _Maybe_unary_or_binary_function<_Res, _T1>
589 : std::unary_function<_T1, _Res> { };
593 * Derives from @c binary_function, as appropriate.
596 template<typename _Res, typename _T1, typename _T2>
597 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
598 : std::binary_function<_T1, _T2, _Res> { };
602 * Implementation of @c mem_fn for member function pointers.
605 template<typename _Res, typename _Class, typename... _ArgTypes>
606 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
607 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
609 typedef _Res (_Class::*_Functor)(_ArgTypes...);
611 template<typename _Tp>
613 _M_call(_Tp& __object, const volatile _Class *,
614 _ArgTypes... __args) const
615 { return (__object.*__pmf)(__args...); }
617 template<typename _Tp>
619 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
620 { return ((*__ptr).*__pmf)(__args...); }
623 typedef _Res result_type;
625 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
629 operator()(_Class& __object, _ArgTypes... __args) const
630 { return (__object.*__pmf)(__args...); }
634 operator()(_Class* __object, _ArgTypes... __args) const
635 { return (__object->*__pmf)(__args...); }
637 // Handle smart pointers, references and pointers to derived
638 template<typename _Tp>
640 operator()(_Tp& __object, _ArgTypes... __args) const
641 { return _M_call(__object, &__object, __args...); }
649 * Implementation of @c mem_fn for const member function pointers.
652 template<typename _Res, typename _Class, typename... _ArgTypes>
653 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
654 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
657 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
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 _Class& __object, _ArgTypes... __args) const
678 { return (__object.*__pmf)(__args...); }
682 operator()(const _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...); }
696 * Implementation of @c mem_fn for volatile member function pointers.
699 template<typename _Res, typename _Class, typename... _ArgTypes>
700 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
701 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
704 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
706 template<typename _Tp>
708 _M_call(_Tp& __object, const volatile _Class *,
709 _ArgTypes... __args) const
710 { return (__object.*__pmf)(__args...); }
712 template<typename _Tp>
714 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
715 { return ((*__ptr).*__pmf)(__args...); }
718 typedef _Res result_type;
720 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
724 operator()(volatile _Class& __object, _ArgTypes... __args) const
725 { return (__object.*__pmf)(__args...); }
729 operator()(volatile _Class* __object, _ArgTypes... __args) const
730 { return (__object->*__pmf)(__args...); }
732 // Handle smart pointers, references and pointers to derived
733 template<typename _Tp>
735 operator()(_Tp& __object, _ArgTypes... __args) const
736 { return _M_call(__object, &__object, __args...); }
744 * Implementation of @c mem_fn for const volatile member function pointers.
747 template<typename _Res, typename _Class, typename... _ArgTypes>
748 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
749 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
752 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
754 template<typename _Tp>
756 _M_call(_Tp& __object, const volatile _Class *,
757 _ArgTypes... __args) const
758 { return (__object.*__pmf)(__args...); }
760 template<typename _Tp>
762 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
763 { return ((*__ptr).*__pmf)(__args...); }
766 typedef _Res result_type;
768 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
772 operator()(const volatile _Class& __object, _ArgTypes... __args) const
773 { return (__object.*__pmf)(__args...); }
777 operator()(const volatile _Class* __object, _ArgTypes... __args) const
778 { return (__object->*__pmf)(__args...); }
780 // Handle smart pointers, references and pointers to derived
781 template<typename _Tp>
782 _Res operator()(_Tp& __object, _ArgTypes... __args) const
783 { return _M_call(__object, &__object, __args...); }
790 template<typename _Res, typename _Class>
791 class _Mem_fn<_Res _Class::*>
793 // This bit of genius is due to Peter Dimov, improved slightly by
795 template<typename _Tp>
797 _M_call(_Tp& __object, _Class *) const
798 { return __object.*__pm; }
800 template<typename _Tp, typename _Up>
802 _M_call(_Tp& __object, _Up * const *) const
803 { return (*__object).*__pm; }
805 template<typename _Tp, typename _Up>
807 _M_call(_Tp& __object, const _Up * const *) const
808 { return (*__object).*__pm; }
810 template<typename _Tp>
812 _M_call(_Tp& __object, const _Class *) const
813 { return __object.*__pm; }
815 template<typename _Tp>
817 _M_call(_Tp& __ptr, const volatile void*) const
818 { return (*__ptr).*__pm; }
820 template<typename _Tp> static _Tp& __get_ref();
822 template<typename _Tp>
823 static __sfinae_types::__one __check_const(_Tp&, _Class*);
824 template<typename _Tp, typename _Up>
825 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
826 template<typename _Tp, typename _Up>
827 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
828 template<typename _Tp>
829 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
830 template<typename _Tp>
831 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
834 template<typename _Tp>
836 : _Mem_fn_const_or_non<_Res,
837 (sizeof(__sfinae_types::__two)
838 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
841 template<typename _Signature>
844 template<typename _CVMem, typename _Tp>
845 struct result<_CVMem(_Tp)>
846 : public _Result_type<_Tp> { };
848 template<typename _CVMem, typename _Tp>
849 struct result<_CVMem(_Tp&)>
850 : public _Result_type<_Tp> { };
853 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
857 operator()(_Class& __object) const
858 { return __object.*__pm; }
861 operator()(const _Class& __object) const
862 { return __object.*__pm; }
866 operator()(_Class* __object) const
867 { return __object->*__pm; }
870 operator()(const _Class* __object) const
871 { return __object->*__pm; }
873 // Handle smart pointers and derived
874 template<typename _Tp>
875 typename _Result_type<_Tp>::type
876 operator()(_Tp& __unknown) const
877 { return _M_call(__unknown, &__unknown); }
884 * @brief Returns a function object that forwards to the member
887 template<typename _Tp, typename _Class>
888 inline _Mem_fn<_Tp _Class::*>
889 mem_fn(_Tp _Class::* __pm)
891 return _Mem_fn<_Tp _Class::*>(__pm);
895 * @brief Determines if the given type _Tp is a function object
896 * should be treated as a subexpression when evaluating calls to
897 * function objects returned by bind(). [TR1 3.6.1]
899 template<typename _Tp>
900 struct is_bind_expression
901 { static const bool value = false; };
903 template<typename _Tp>
904 const bool is_bind_expression<_Tp>::value;
907 * @brief Determines if the given type _Tp is a placeholder in a
908 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
910 template<typename _Tp>
911 struct is_placeholder
912 { static const int value = 0; };
914 template<typename _Tp>
915 const int is_placeholder<_Tp>::value;
919 * The type of placeholder objects defined by libstdc++.
922 template<int _Num> struct _Placeholder { };
924 // Define a large number of placeholders. There is no way to
925 // simplify this with variadic templates, because we're introducing
926 // unique names for each.
927 namespace placeholders { namespace {
937 _Placeholder<10> _10;
938 _Placeholder<11> _11;
939 _Placeholder<12> _12;
940 _Placeholder<13> _13;
941 _Placeholder<14> _14;
942 _Placeholder<15> _15;
943 _Placeholder<16> _16;
944 _Placeholder<17> _17;
945 _Placeholder<18> _18;
946 _Placeholder<19> _19;
947 _Placeholder<20> _20;
948 _Placeholder<21> _21;
949 _Placeholder<22> _22;
950 _Placeholder<23> _23;
951 _Placeholder<24> _24;
952 _Placeholder<25> _25;
953 _Placeholder<26> _26;
954 _Placeholder<27> _27;
955 _Placeholder<28> _28;
956 _Placeholder<29> _29;
961 * Partial specialization of is_placeholder that provides the placeholder
962 * number for the placeholder objects defined by libstdc++.
966 struct is_placeholder<_Placeholder<_Num> >
967 { static const int value = _Num; };
970 const int is_placeholder<_Placeholder<_Num> >::value;
974 * Stores a tuple of indices. Used by bind() to extract the elements
978 template<int... _Indexes>
979 struct _Index_tuple { };
983 * Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
986 template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
987 struct _Build_index_tuple;
989 template<std::size_t _Num, int... _Indexes>
990 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
991 : _Build_index_tuple<_Num - 1,
992 _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
996 template<int... _Indexes>
997 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
999 typedef _Index_tuple<_Indexes...> __type;
1004 * Used by _Safe_tuple_element to indicate that there is no tuple
1005 * element at this position.
1008 struct _No_tuple_element;
1012 * Implementation helper for _Safe_tuple_element. This primary
1013 * template handles the case where it is safe to use @c
1017 template<int __i, typename _Tuple, bool _IsSafe>
1018 struct _Safe_tuple_element_impl
1019 : tuple_element<__i, _Tuple> { };
1023 * Implementation helper for _Safe_tuple_element. This partial
1024 * specialization handles the case where it is not safe to use @c
1025 * tuple_element. We just return @c _No_tuple_element.
1028 template<int __i, typename _Tuple>
1029 struct _Safe_tuple_element_impl<__i, _Tuple, false>
1031 typedef _No_tuple_element type;
1035 * Like tuple_element, but returns @c _No_tuple_element when
1036 * tuple_element would return an error.
1038 template<int __i, typename _Tuple>
1039 struct _Safe_tuple_element
1040 : _Safe_tuple_element_impl<__i, _Tuple,
1041 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
1047 * Maps an argument to bind() into an actual argument to the bound
1048 * function object [TR1 3.6.3/5]. Only the first parameter should
1049 * be specified: the rest are used to determine among the various
1050 * implementations. Note that, although this class is a function
1051 * object, isn't not entirely normal because it takes only two
1052 * parameters regardless of the number of parameters passed to the
1053 * bind expression. The first parameter is the bound argument and
1054 * the second parameter is a tuple containing references to the
1055 * rest of the arguments.
1058 template<typename _Arg,
1059 bool _IsBindExp = is_bind_expression<_Arg>::value,
1060 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
1065 * If the argument is reference_wrapper<_Tp>, returns the
1066 * underlying reference. [TR1 3.6.3/5 bullet 1]
1069 template<typename _Tp>
1070 class _Mu<reference_wrapper<_Tp>, false, false>
1073 typedef _Tp& result_type;
1075 /* Note: This won't actually work for const volatile
1076 * reference_wrappers, because reference_wrapper::get() is const
1077 * but not volatile-qualified. This might be a defect in the TR.
1079 template<typename _CVRef, typename _Tuple>
1081 operator()(_CVRef& __arg, const _Tuple&) const volatile
1082 { return __arg.get(); }
1087 * If the argument is a bind expression, we invoke the underlying
1088 * function object with the same cv-qualifiers as we are given and
1089 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
1092 template<typename _Arg>
1093 class _Mu<_Arg, true, false>
1096 template<typename _Signature> class result;
1098 // Determine the result type when we pass the arguments along. This
1099 // involves passing along the cv-qualifiers placed on _Mu and
1100 // unwrapping the argument bundle.
1101 template<typename _CVMu, typename _CVArg, typename... _Args>
1102 class result<_CVMu(_CVArg, tuple<_Args...>)>
1103 : public result_of<_CVArg(_Args...)> { };
1105 template<typename _CVArg, typename... _Args>
1106 typename result_of<_CVArg(_Args...)>::type
1107 operator()(_CVArg& __arg,
1108 const tuple<_Args...>& __tuple) const volatile
1110 // Construct an index tuple and forward to __call
1111 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1113 return this->__call(__arg, __tuple, _Indexes());
1117 // Invokes the underlying function object __arg by unpacking all
1118 // of the arguments in the tuple.
1119 template<typename _CVArg, typename... _Args, int... _Indexes>
1120 typename result_of<_CVArg(_Args...)>::type
1121 __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
1122 const _Index_tuple<_Indexes...>&) const volatile
1124 return __arg(_GLIBCXX_TR1 get<_Indexes>(__tuple)...);
1130 * If the argument is a placeholder for the Nth argument, returns
1131 * a reference to the Nth argument to the bind function object.
1132 * [TR1 3.6.3/5 bullet 3]
1135 template<typename _Arg>
1136 class _Mu<_Arg, false, true>
1139 template<typename _Signature> class result;
1141 template<typename _CVMu, typename _CVArg, typename _Tuple>
1142 class result<_CVMu(_CVArg, _Tuple)>
1144 // Add a reference, if it hasn't already been done for us.
1145 // This allows us to be a little bit sloppy in constructing
1146 // the tuple that we pass to result_of<...>.
1147 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1152 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
1153 typedef typename add_lvalue_reference<__base_type>::type type;
1155 typedef typename add_reference<__base_type>::type type;
1159 template<typename _Tuple>
1160 typename result<_Mu(_Arg, _Tuple)>::type
1161 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
1163 return ::std::_GLIBCXX_TR1 get<(is_placeholder<_Arg>::value
1170 * If the argument is just a value, returns a reference to that
1171 * value. The cv-qualifiers on the reference are the same as the
1172 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1175 template<typename _Arg>
1176 class _Mu<_Arg, false, false>
1179 template<typename _Signature> struct result;
1181 template<typename _CVMu, typename _CVArg, typename _Tuple>
1182 struct result<_CVMu(_CVArg, _Tuple)>
1184 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
1185 typedef typename add_lvalue_reference<_CVArg>::type type;
1187 typedef typename add_reference<_CVArg>::type type;
1191 // Pick up the cv-qualifiers of the argument
1192 template<typename _CVArg, typename _Tuple>
1194 operator()(_CVArg& __arg, const _Tuple&) const volatile
1200 * Maps member pointers into instances of _Mem_fn but leaves all
1201 * other function objects untouched. Used by tr1::bind(). The
1202 * primary template handles the non--member-pointer case.
1205 template<typename _Tp>
1206 struct _Maybe_wrap_member_pointer
1211 __do_wrap(const _Tp& __x)
1217 * Maps member pointers into instances of _Mem_fn but leaves all
1218 * other function objects untouched. Used by tr1::bind(). This
1219 * partial specialization handles the member pointer case.
1222 template<typename _Tp, typename _Class>
1223 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1225 typedef _Mem_fn<_Tp _Class::*> type;
1228 __do_wrap(_Tp _Class::* __pm)
1229 { return type(__pm); }
1234 * Type of the function object returned from bind().
1237 template<typename _Signature>
1240 template<typename _Functor, typename... _Bound_args>
1241 class _Bind<_Functor(_Bound_args...)>
1242 : public _Weak_result_type<_Functor>
1244 typedef _Bind __self_type;
1245 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1249 tuple<_Bound_args...> _M_bound_args;
1252 template<typename... _Args, int... _Indexes>
1254 _Functor(typename result_of<_Mu<_Bound_args>
1255 (_Bound_args, tuple<_Args...>)>::type...)
1257 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1259 return _M_f(_Mu<_Bound_args>()
1260 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1264 template<typename... _Args, int... _Indexes>
1266 const _Functor(typename result_of<_Mu<_Bound_args>
1267 (const _Bound_args, tuple<_Args...>)
1269 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1271 return _M_f(_Mu<_Bound_args>()
1272 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1276 template<typename... _Args, int... _Indexes>
1278 volatile _Functor(typename result_of<_Mu<_Bound_args>
1279 (volatile _Bound_args, tuple<_Args...>)
1281 __call(const tuple<_Args...>& __args,
1282 _Index_tuple<_Indexes...>) volatile
1284 return _M_f(_Mu<_Bound_args>()
1285 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1288 // Call as const volatile
1289 template<typename... _Args, int... _Indexes>
1291 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1292 (const volatile _Bound_args,
1295 __call(const tuple<_Args...>& __args,
1296 _Index_tuple<_Indexes...>) const volatile
1298 return _M_f(_Mu<_Bound_args>()
1299 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1303 explicit _Bind(_Functor __f, _Bound_args... __bound_args)
1304 : _M_f(__f), _M_bound_args(__bound_args...) { }
1307 template<typename... _Args>
1309 _Functor(typename result_of<_Mu<_Bound_args>
1310 (_Bound_args, tuple<_Args...>)>::type...)
1312 operator()(_Args&... __args)
1314 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1318 template<typename... _Args>
1320 const _Functor(typename result_of<_Mu<_Bound_args>
1321 (const _Bound_args, tuple<_Args...>)>::type...)
1323 operator()(_Args&... __args) const
1325 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1330 template<typename... _Args>
1332 volatile _Functor(typename result_of<_Mu<_Bound_args>
1333 (volatile _Bound_args, tuple<_Args...>)>::type...)
1335 operator()(_Args&... __args) volatile
1337 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1341 // Call as const volatile
1342 template<typename... _Args>
1344 const volatile _Functor(typename result_of<_Mu<_Bound_args>
1345 (const volatile _Bound_args,
1346 tuple<_Args...>)>::type...)
1348 operator()(_Args&... __args) const volatile
1350 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1356 * Type of the function object returned from bind<R>().
1359 template<typename _Result, typename _Signature>
1360 struct _Bind_result;
1362 template<typename _Result, typename _Functor, typename... _Bound_args>
1363 class _Bind_result<_Result, _Functor(_Bound_args...)>
1365 typedef _Bind_result __self_type;
1366 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1370 tuple<_Bound_args...> _M_bound_args;
1373 template<typename... _Args, int... _Indexes>
1375 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
1377 return _M_f(_Mu<_Bound_args>()
1378 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1382 template<typename... _Args, int... _Indexes>
1384 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
1386 return _M_f(_Mu<_Bound_args>()
1387 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1391 template<typename... _Args, int... _Indexes>
1393 __call(const tuple<_Args...>& __args,
1394 _Index_tuple<_Indexes...>) volatile
1396 return _M_f(_Mu<_Bound_args>()
1397 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1400 // Call as const volatile
1401 template<typename... _Args, int... _Indexes>
1403 __call(const tuple<_Args...>& __args,
1404 _Index_tuple<_Indexes...>) const volatile
1406 return _M_f(_Mu<_Bound_args>()
1407 (_GLIBCXX_TR1 get<_Indexes>(_M_bound_args), __args)...);
1411 typedef _Result result_type;
1414 _Bind_result(_Functor __f, _Bound_args... __bound_args)
1415 : _M_f(__f), _M_bound_args(__bound_args...) { }
1418 template<typename... _Args>
1420 operator()(_Args&... __args)
1422 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1426 template<typename... _Args>
1428 operator()(_Args&... __args) const
1430 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1434 template<typename... _Args>
1436 operator()(_Args&... __args) volatile
1438 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1441 // Call as const volatile
1442 template<typename... _Args>
1444 operator()(_Args&... __args) const volatile
1446 return this->__call(_GLIBCXX_TR1 tie(__args...), _Bound_indexes());
1452 * Class template _Bind is always a bind expression.
1455 template<typename _Signature>
1456 struct is_bind_expression<_Bind<_Signature> >
1457 { static const bool value = true; };
1459 template<typename _Signature>
1460 const bool is_bind_expression<_Bind<_Signature> >::value;
1464 * Class template _Bind_result is always a bind expression.
1467 template<typename _Result, typename _Signature>
1468 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1469 { static const bool value = true; };
1471 template<typename _Result, typename _Signature>
1472 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
1474 template<typename _Functor, typename... _ArgTypes>
1476 _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
1477 bind(_Functor __f, _ArgTypes... __args)
1479 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1480 typedef typename __maybe_type::type __functor_type;
1481 typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
1482 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1485 template<typename _Result, typename _Functor, typename... _ArgTypes>
1487 _Bind_result<_Result,
1488 typename _Maybe_wrap_member_pointer<_Functor>::type
1490 bind(_Functor __f, _ArgTypes... __args)
1492 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
1493 typedef typename __maybe_type::type __functor_type;
1494 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
1496 return __result_type(__maybe_type::__do_wrap(__f), __args...);
1500 * @brief Exception class thrown when class template function's
1501 * operator() is called with an empty target.
1504 class bad_function_call : public std::exception { };
1508 * The integral constant expression 0 can be converted into a
1509 * pointer to this type. It is used by the function template to
1510 * accept NULL pointers.
1513 struct _M_clear_type;
1517 * Trait identifying "location-invariant" types, meaning that the
1518 * address of the object (or any of its members) will not escape.
1519 * Also implies a trivial copy constructor and assignment operator.
1522 template<typename _Tp>
1523 struct __is_location_invariant
1524 : integral_constant<bool,
1525 (is_pointer<_Tp>::value
1526 || is_member_pointer<_Tp>::value)>
1530 class _Undefined_class;
1535 const void* _M_const_object;
1536 void (*_M_function_pointer)();
1537 void (_Undefined_class::*_M_member_pointer)();
1542 void* _M_access() { return &_M_pod_data[0]; }
1543 const void* _M_access() const { return &_M_pod_data[0]; }
1545 template<typename _Tp>
1548 { return *static_cast<_Tp*>(_M_access()); }
1550 template<typename _Tp>
1553 { return *static_cast<const _Tp*>(_M_access()); }
1555 _Nocopy_types _M_unused;
1556 char _M_pod_data[sizeof(_Nocopy_types)];
1559 enum _Manager_operation
1567 /* Simple type wrapper that helps avoid annoying const problems
1568 when casting between void pointers and pointers-to-pointers. */
1569 template<typename _Tp>
1570 struct _Simple_type_wrapper
1572 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1577 template<typename _Tp>
1578 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1579 : __is_location_invariant<_Tp>
1583 // Converts a reference to a function object into a callable
1585 template<typename _Functor>
1587 __callable_functor(_Functor& __f)
1590 template<typename _Member, typename _Class>
1591 inline _Mem_fn<_Member _Class::*>
1592 __callable_functor(_Member _Class::* &__p)
1593 { return mem_fn(__p); }
1595 template<typename _Member, typename _Class>
1596 inline _Mem_fn<_Member _Class::*>
1597 __callable_functor(_Member _Class::* const &__p)
1598 { return mem_fn(__p); }
1600 template<typename _Signature>
1605 * Base class of all polymorphic function object wrappers.
1608 class _Function_base
1611 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1612 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1614 template<typename _Functor>
1618 static const bool __stored_locally =
1619 (__is_location_invariant<_Functor>::value
1620 && sizeof(_Functor) <= _M_max_size
1621 && __alignof__(_Functor) <= _M_max_align
1622 && (_M_max_align % __alignof__(_Functor) == 0));
1624 typedef integral_constant<bool, __stored_locally> _Local_storage;
1626 // Retrieve a pointer to the function object
1628 _M_get_pointer(const _Any_data& __source)
1630 const _Functor* __ptr =
1631 __stored_locally? &__source._M_access<_Functor>()
1632 /* have stored a pointer */ : __source._M_access<_Functor*>();
1633 return const_cast<_Functor*>(__ptr);
1636 // Clone a location-invariant function object that fits within
1637 // an _Any_data structure.
1639 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1641 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1644 // Clone a function object that is not location-invariant or
1645 // that cannot fit into an _Any_data structure.
1647 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1649 __dest._M_access<_Functor*>() =
1650 new _Functor(*__source._M_access<_Functor*>());
1653 // Destroying a location-invariant object may still require
1656 _M_destroy(_Any_data& __victim, true_type)
1658 __victim._M_access<_Functor>().~_Functor();
1661 // Destroying an object located on the heap.
1663 _M_destroy(_Any_data& __victim, false_type)
1665 delete __victim._M_access<_Functor*>();
1670 _M_manager(_Any_data& __dest, const _Any_data& __source,
1671 _Manager_operation __op)
1675 case __get_type_info:
1676 __dest._M_access<const type_info*>() = &typeid(_Functor);
1679 case __get_functor_ptr:
1680 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1683 case __clone_functor:
1684 _M_clone(__dest, __source, _Local_storage());
1687 case __destroy_functor:
1688 _M_destroy(__dest, _Local_storage());
1695 _M_init_functor(_Any_data& __functor, const _Functor& __f)
1697 _M_init_functor(__functor, __f, _Local_storage());
1700 template<typename _Signature>
1702 _M_not_empty_function(const function<_Signature>& __f)
1707 template<typename _Tp>
1709 _M_not_empty_function(const _Tp*& __fp)
1714 template<typename _Class, typename _Tp>
1716 _M_not_empty_function(_Tp _Class::* const& __mp)
1721 template<typename _Tp>
1723 _M_not_empty_function(const _Tp&)
1730 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
1732 new (__functor._M_access()) _Functor(__f);
1736 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
1738 __functor._M_access<_Functor*>() = new _Functor(__f);
1742 template<typename _Functor>
1743 class _Ref_manager : public _Base_manager<_Functor*>
1745 typedef _Function_base::_Base_manager<_Functor*> _Base;
1749 _M_manager(_Any_data& __dest, const _Any_data& __source,
1750 _Manager_operation __op)
1754 case __get_type_info:
1755 __dest._M_access<const type_info*>() = &typeid(_Functor);
1758 case __get_functor_ptr:
1759 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1760 return is_const<_Functor>::value;
1764 _Base::_M_manager(__dest, __source, __op);
1770 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1772 // TBD: Use address_of function instead
1773 _Base::_M_init_functor(__functor, &__f.get());
1777 _Function_base() : _M_manager(0) { }
1782 _M_manager(_M_functor, _M_functor, __destroy_functor);
1786 bool _M_empty() const { return !_M_manager; }
1788 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1789 _Manager_operation);
1791 _Any_data _M_functor;
1792 _Manager_type _M_manager;
1795 template<typename _Signature, typename _Functor>
1796 class _Function_handler;
1798 template<typename _Res, typename _Functor, typename... _ArgTypes>
1799 class _Function_handler<_Res(_ArgTypes...), _Functor>
1800 : public _Function_base::_Base_manager<_Functor>
1802 typedef _Function_base::_Base_manager<_Functor> _Base;
1806 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1808 return (*_Base::_M_get_pointer(__functor))(__args...);
1812 template<typename _Functor, typename... _ArgTypes>
1813 class _Function_handler<void(_ArgTypes...), _Functor>
1814 : public _Function_base::_Base_manager<_Functor>
1816 typedef _Function_base::_Base_manager<_Functor> _Base;
1820 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1822 (*_Base::_M_get_pointer(__functor))(__args...);
1826 template<typename _Res, typename _Functor, typename... _ArgTypes>
1827 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1828 : public _Function_base::_Ref_manager<_Functor>
1830 typedef _Function_base::_Ref_manager<_Functor> _Base;
1834 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1837 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1841 template<typename _Functor, typename... _ArgTypes>
1842 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1843 : public _Function_base::_Ref_manager<_Functor>
1845 typedef _Function_base::_Ref_manager<_Functor> _Base;
1849 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1851 __callable_functor(**_Base::_M_get_pointer(__functor))(__args...);
1855 template<typename _Class, typename _Member, typename _Res,
1856 typename... _ArgTypes>
1857 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1858 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1860 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1865 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1868 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1872 template<typename _Class, typename _Member, typename... _ArgTypes>
1873 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1874 : public _Function_base::_Base_manager<
1875 _Simple_type_wrapper< _Member _Class::* > >
1877 typedef _Member _Class::* _Functor;
1878 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1879 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1883 _M_manager(_Any_data& __dest, const _Any_data& __source,
1884 _Manager_operation __op)
1888 case __get_type_info:
1889 __dest._M_access<const type_info*>() = &typeid(_Functor);
1892 case __get_functor_ptr:
1893 __dest._M_access<_Functor*>() =
1894 &_Base::_M_get_pointer(__source)->__value;
1898 _Base::_M_manager(__dest, __source, __op);
1904 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1907 mem_fn(_Base::_M_get_pointer(__functor)->__value)(__args...);
1911 template<typename _Res, typename... _ArgTypes>
1912 class function<_Res(_ArgTypes...)>
1913 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1914 private _Function_base
1918 * This class is used to implement the safe_bool idiom.
1923 _Hidden_type* _M_bool;
1928 * This typedef is used to implement the safe_bool idiom.
1931 typedef _Hidden_type* _Hidden_type::* _Safe_bool;
1933 typedef _Res _Signature_type(_ArgTypes...);
1938 typedef _Res result_type;
1940 // [3.7.2.1] construct/copy/destroy
1943 * @brief Default construct creates an empty function call wrapper.
1944 * @post @c !(bool)*this
1946 function() : _Function_base() { }
1949 * @brief Default construct creates an empty function call wrapper.
1950 * @post @c !(bool)*this
1952 function(_M_clear_type*) : _Function_base() { }
1955 * @brief %Function copy constructor.
1956 * @param x A %function object with identical call signature.
1957 * @pre @c (bool)*this == (bool)x
1959 * The newly-created %function contains a copy of the target of @a
1960 * x (if it has one).
1962 function(const function& __x);
1965 * @brief Builds a %function that targets a copy of the incoming
1967 * @param f A %function object that is callable with parameters of
1968 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1971 * The newly-created %function object will target a copy of @a
1972 * f. If @a f is @c reference_wrapper<F>, then this function
1973 * object will contain a reference to the function object @c
1974 * f.get(). If @a f is a NULL function pointer or NULL
1975 * pointer-to-member, the newly-created object will be empty.
1977 * If @a f is a non-NULL function pointer or an object of type @c
1978 * reference_wrapper<F>, this function will not throw.
1980 template<typename _Functor>
1981 function(_Functor __f,
1982 typename __gnu_cxx::__enable_if<
1983 !is_integral<_Functor>::value, _Useless>::__type
1987 * @brief %Function assignment operator.
1988 * @param x A %function with identical call signature.
1989 * @post @c (bool)*this == (bool)x
1992 * The target of @a x is copied to @c *this. If @a x has no
1993 * target, then @c *this will be empty.
1995 * If @a x targets a function pointer or a reference to a function
1996 * object, then this operation will not throw an exception.
1999 operator=(const function& __x)
2001 function(__x).swap(*this);
2006 * @brief %Function assignment to zero.
2007 * @post @c !(bool)*this
2010 * The target of @a *this is deallocated, leaving it empty.
2013 operator=(_M_clear_type*)
2017 _M_manager(_M_functor, _M_functor, __destroy_functor);
2025 * @brief %Function assignment to a new target.
2026 * @param f A %function object that is callable with parameters of
2027 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2031 * This %function object wrapper will target a copy of @a
2032 * f. If @a f is @c reference_wrapper<F>, then this function
2033 * object will contain a reference to the function object @c
2034 * f.get(). If @a f is a NULL function pointer or NULL
2035 * pointer-to-member, @c this object will be empty.
2037 * If @a f is a non-NULL function pointer or an object of type @c
2038 * reference_wrapper<F>, this function will not throw.
2040 template<typename _Functor>
2041 typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
2043 operator=(_Functor __f)
2045 function(__f).swap(*this);
2049 // [3.7.2.2] function modifiers
2052 * @brief Swap the targets of two %function objects.
2053 * @param f A %function with identical call signature.
2055 * Swap the targets of @c this function object and @a f. This
2056 * function will not throw an exception.
2058 void swap(function& __x)
2060 _Any_data __old_functor = _M_functor;
2061 _M_functor = __x._M_functor;
2062 __x._M_functor = __old_functor;
2063 _Manager_type __old_manager = _M_manager;
2064 _M_manager = __x._M_manager;
2065 __x._M_manager = __old_manager;
2066 _Invoker_type __old_invoker = _M_invoker;
2067 _M_invoker = __x._M_invoker;
2068 __x._M_invoker = __old_invoker;
2071 // [3.7.2.3] function capacity
2074 * @brief Determine if the %function wrapper has a target.
2076 * @return @c true when this %function object contains a target,
2077 * or @c false when it is empty.
2079 * This function will not throw an exception.
2081 operator _Safe_bool() const
2086 return &_Hidden_type::_M_bool;
2089 // [3.7.2.4] function invocation
2092 * @brief Invokes the function targeted by @c *this.
2093 * @returns the result of the target.
2094 * @throws bad_function_call when @c !(bool)*this
2096 * The function call operator invokes the target function object
2097 * stored by @c this.
2099 _Res operator()(_ArgTypes... __args) const;
2101 // [3.7.2.5] function target access
2103 * @brief Determine the type of the target of this function object
2106 * @returns the type identifier of the target function object, or
2107 * @c typeid(void) if @c !(bool)*this.
2109 * This function will not throw an exception.
2111 const type_info& target_type() const;
2114 * @brief Access the stored target function object.
2116 * @return Returns a pointer to the stored target function object,
2117 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2120 * This function will not throw an exception.
2122 template<typename _Functor> _Functor* target();
2127 template<typename _Functor> const _Functor* target() const;
2130 // [3.7.2.6] undefined operators
2131 template<typename _Function>
2132 void operator==(const function<_Function>&) const;
2133 template<typename _Function>
2134 void operator!=(const function<_Function>&) const;
2136 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2137 _Invoker_type _M_invoker;
2140 template<typename _Res, typename... _ArgTypes>
2141 function<_Res(_ArgTypes...)>::
2142 function(const function& __x)
2147 _M_invoker = __x._M_invoker;
2148 _M_manager = __x._M_manager;
2149 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2153 template<typename _Res, typename... _ArgTypes>
2154 template<typename _Functor>
2155 function<_Res(_ArgTypes...)>::
2156 function(_Functor __f,
2157 typename __gnu_cxx::__enable_if<
2158 !is_integral<_Functor>::value, _Useless>::__type)
2161 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2163 if (_My_handler::_M_not_empty_function(__f))
2165 _M_invoker = &_My_handler::_M_invoke;
2166 _M_manager = &_My_handler::_M_manager;
2167 _My_handler::_M_init_functor(_M_functor, __f);
2171 template<typename _Res, typename... _ArgTypes>
2173 function<_Res(_ArgTypes...)>::
2174 operator()(_ArgTypes... __args) const
2179 throw bad_function_call();
2184 return _M_invoker(_M_functor, __args...);
2187 template<typename _Res, typename... _ArgTypes>
2189 function<_Res(_ArgTypes...)>::
2194 _Any_data __typeinfo_result;
2195 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2196 return *__typeinfo_result._M_access<const type_info*>();
2199 return typeid(void);
2202 template<typename _Res, typename... _ArgTypes>
2203 template<typename _Functor>
2205 function<_Res(_ArgTypes...)>::
2208 if (typeid(_Functor) == target_type() && _M_manager)
2211 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2212 && !is_const<_Functor>::value)
2215 return __ptr._M_access<_Functor*>();
2221 template<typename _Res, typename... _ArgTypes>
2222 template<typename _Functor>
2224 function<_Res(_ArgTypes...)>::
2227 if (typeid(_Functor) == target_type() && _M_manager)
2230 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2231 return __ptr._M_access<const _Functor*>();
2237 // [3.7.2.7] null pointer comparisons
2240 * @brief Compares a polymorphic function object wrapper against 0
2241 * (the NULL pointer).
2242 * @returns @c true if the wrapper has no target, @c false otherwise
2244 * This function will not throw an exception.
2246 template<typename _Signature>
2248 operator==(const function<_Signature>& __f, _M_clear_type*)
2256 template<typename _Signature>
2258 operator==(_M_clear_type*, const function<_Signature>& __f)
2264 * @brief Compares a polymorphic function object wrapper against 0
2265 * (the NULL pointer).
2266 * @returns @c false if the wrapper has no target, @c true otherwise
2268 * This function will not throw an exception.
2270 template<typename _Signature>
2272 operator!=(const function<_Signature>& __f, _M_clear_type*)
2280 template<typename _Signature>
2282 operator!=(_M_clear_type*, const function<_Signature>& __f)
2287 // [3.7.2.8] specialized algorithms
2290 * @brief Swap the targets of two polymorphic function object wrappers.
2292 * This function will not throw an exception.
2294 template<typename _Signature>
2296 swap(function<_Signature>& __x, function<_Signature>& __y)
2301 _GLIBCXX_END_NAMESPACE_TR1