1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004, 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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.
31 * This is a TR1 C++ Library header.
34 #ifndef _TR1_FUNCTIONAL
35 #define _TR1_FUNCTIONAL 1
37 #include "../functional"
39 #include <tr1/type_traits>
40 #include <bits/cpp_type_traits.h>
41 #include <string> // for std::tr1::hash
42 #include <cstdlib> // for std::abort
49 template<typename _MemberPointer>
54 * Actual implementation of _Has_result_type, which uses SFINAE to
55 * determine if the type _Tp has a publicly-accessible member type
59 template<typename _Tp>
60 class _Has_result_type_helper : __sfinae_types
62 template<typename _Up>
66 template<typename _Up>
67 static __one __test(_Wrap_type<typename _Up::result_type>*);
69 template<typename _Up>
70 static __two __test(...);
73 static const bool value = sizeof(__test<_Tp>(0)) == 1;
76 template<typename _Tp>
77 struct _Has_result_type
80 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
85 * If we have found a result_type, extract it.
88 template<bool _Has_result_type, typename _Functor>
89 struct _Maybe_get_result_type
92 template<typename _Functor>
93 struct _Maybe_get_result_type<true, _Functor>
95 typedef typename _Functor::result_type result_type;
100 * Base class for any function object that has a weak result type, as
101 * defined in 3.3/3 of TR1.
104 template<typename _Functor>
105 struct _Weak_result_type_impl
106 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
112 * Strip top-level cv-qualifiers from the function object and let
113 * _Weak_result_type_impl perform the real work.
116 template<typename _Functor>
117 struct _Weak_result_type
118 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
122 template<typename _Signature>
127 * Actual implementation of result_of. When _Has_result_type is
128 * true, gets its result from _Weak_result_type. Otherwise, uses
129 * the function object's member template result to extract the
133 template<bool _Has_result_type, typename _Signature>
134 struct _Result_of_impl;
136 // Handle member data pointers using _Mem_fn's logic
137 template<typename _Res, typename _Class, typename _T1>
138 struct _Result_of_impl<false, _Res _Class::*(_T1)>
140 typedef typename _Mem_fn<_Res _Class::*>
141 ::template _Result_type<_T1>::type type;
146 * Determines if the type _Tp derives from unary_function.
149 template<typename _Tp>
150 struct _Derives_from_unary_function : __sfinae_types
153 template<typename _T1, typename _Res>
154 static __one __test(const volatile unary_function<_T1, _Res>*);
156 // It's tempting to change "..." to const volatile void*, but
157 // that fails when _Tp is a function type.
158 static __two __test(...);
161 static const bool value = sizeof(__test((_Tp*)0)) == 1;
166 * Determines if the type _Tp derives from binary_function.
169 template<typename _Tp>
170 struct _Derives_from_binary_function : __sfinae_types
173 template<typename _T1, typename _T2, typename _Res>
174 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
176 // It's tempting to change "..." to const volatile void*, but
177 // that fails when _Tp is a function type.
178 static __two __test(...);
181 static const bool value = sizeof(__test((_Tp*)0)) == 1;
186 * Turns a function type into a function pointer type
189 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
190 struct _Function_to_function_pointer
195 template<typename _Tp>
196 struct _Function_to_function_pointer<_Tp, true>
203 * Knowing which of unary_function and binary_function _Tp derives
204 * from, derives from the same and ensures that reference_wrapper
205 * will have a weak result type. See cases below.
208 template<bool _Unary, bool _Binary, typename _Tp>
209 struct _Reference_wrapper_base_impl;
211 // Not a unary_function or binary_function, so try a weak result type
212 template<typename _Tp>
213 struct _Reference_wrapper_base_impl<false, false, _Tp>
214 : _Weak_result_type<_Tp>
217 // unary_function but not binary_function
218 template<typename _Tp>
219 struct _Reference_wrapper_base_impl<true, false, _Tp>
220 : unary_function<typename _Tp::argument_type,
221 typename _Tp::result_type>
224 // binary_function but not unary_function
225 template<typename _Tp>
226 struct _Reference_wrapper_base_impl<false, true, _Tp>
227 : binary_function<typename _Tp::first_argument_type,
228 typename _Tp::second_argument_type,
229 typename _Tp::result_type>
232 // both unary_function and binary_function. import result_type to
234 template<typename _Tp>
235 struct _Reference_wrapper_base_impl<true, true, _Tp>
236 : unary_function<typename _Tp::argument_type,
237 typename _Tp::result_type>,
238 binary_function<typename _Tp::first_argument_type,
239 typename _Tp::second_argument_type,
240 typename _Tp::result_type>
242 typedef typename _Tp::result_type result_type;
247 * Derives from unary_function or binary_function when it
248 * can. Specializations handle all of the easy cases. The primary
249 * template determines what to do with a class type, which may
250 * derive from both unary_function and binary_function.
253 template<typename _Tp>
254 struct _Reference_wrapper_base
255 : _Reference_wrapper_base_impl<
256 _Derives_from_unary_function<_Tp>::value,
257 _Derives_from_binary_function<_Tp>::value,
261 // - a function type (unary)
262 template<typename _Res, typename _T1>
263 struct _Reference_wrapper_base<_Res(_T1)>
264 : unary_function<_T1, _Res>
267 // - a function type (binary)
268 template<typename _Res, typename _T1, typename _T2>
269 struct _Reference_wrapper_base<_Res(_T1, _T2)>
270 : binary_function<_T1, _T2, _Res>
273 // - a function pointer type (unary)
274 template<typename _Res, typename _T1>
275 struct _Reference_wrapper_base<_Res(*)(_T1)>
276 : unary_function<_T1, _Res>
279 // - a function pointer type (binary)
280 template<typename _Res, typename _T1, typename _T2>
281 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
282 : binary_function<_T1, _T2, _Res>
285 // - a pointer to member function type (unary, no qualifiers)
286 template<typename _Res, typename _T1>
287 struct _Reference_wrapper_base<_Res (_T1::*)()>
288 : unary_function<_T1*, _Res>
291 // - a pointer to member function type (binary, no qualifiers)
292 template<typename _Res, typename _T1, typename _T2>
293 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
294 : binary_function<_T1*, _T2, _Res>
297 // - a pointer to member function type (unary, const)
298 template<typename _Res, typename _T1>
299 struct _Reference_wrapper_base<_Res (_T1::*)() const>
300 : unary_function<const _T1*, _Res>
303 // - a pointer to member function type (binary, const)
304 template<typename _Res, typename _T1, typename _T2>
305 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
306 : binary_function<const _T1*, _T2, _Res>
309 // - a pointer to member function type (unary, volatile)
310 template<typename _Res, typename _T1>
311 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
312 : unary_function<volatile _T1*, _Res>
315 // - a pointer to member function type (binary, volatile)
316 template<typename _Res, typename _T1, typename _T2>
317 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
318 : binary_function<volatile _T1*, _T2, _Res>
321 // - a pointer to member function type (unary, const volatile)
322 template<typename _Res, typename _T1>
323 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
324 : unary_function<const volatile _T1*, _Res>
327 // - a pointer to member function type (binary, const volatile)
328 template<typename _Res, typename _T1, typename _T2>
329 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
330 : binary_function<const volatile _T1*, _T2, _Res>
333 template<typename _Tp>
334 class reference_wrapper
335 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
337 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
338 // so turn it into a function pointer type.
339 typedef typename _Function_to_function_pointer<_Tp>::type
345 explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
348 reference_wrapper(const reference_wrapper<_Tp>& __inref):
349 _M_data(__inref._M_data)
353 operator=(const reference_wrapper<_Tp>& __inref)
355 _M_data = __inref._M_data;
359 operator _Tp&() const
360 { return this->get(); }
366 #define _GLIBCXX_REPEAT_HEADER <tr1/ref_wrap_iterate.h>
367 #include <tr1/repeat.h>
368 #undef _GLIBCXX_REPEAT_HEADER
372 // Denotes a reference should be taken to a variable.
373 template<typename _Tp>
374 reference_wrapper<_Tp>
376 { return reference_wrapper<_Tp>(__t); }
378 // Denotes a const reference should be taken to a variable.
379 template<typename _Tp>
380 reference_wrapper<const _Tp>
382 { return reference_wrapper<const _Tp>(__t); }
384 template<typename _Tp>
385 reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t)
386 { return ref(__t.get()); }
388 template<typename _Tp>
389 reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t)
390 { return cref(__t.get()); }
392 template<typename _Tp, bool>
393 struct _Mem_fn_const_or_non
395 typedef const _Tp& type;
398 template<typename _Tp>
399 struct _Mem_fn_const_or_non<_Tp, false>
404 template<typename _Res, typename _Class>
405 class _Mem_fn<_Res _Class::*>
407 // This bit of genius is due to Peter Dimov, improved slightly by
409 template<typename _Tp>
411 _M_call(_Tp& __object, _Class *) const
412 { return __object.*__pm; }
414 template<typename _Tp, typename _Up>
416 _M_call(_Tp& __object, _Up * const *) const
417 { return (*__object).*__pm; }
419 template<typename _Tp, typename _Up>
421 _M_call(_Tp& __object, const _Up * const *) const
422 { return (*__object).*__pm; }
424 template<typename _Tp>
426 _M_call(_Tp& __object, const _Class *) const
427 { return __object.*__pm; }
429 template<typename _Tp>
431 _M_call(_Tp& __ptr, const volatile void*) const
432 { return (*__ptr).*__pm; }
434 template<typename _Tp> static _Tp& __get_ref();
436 template<typename _Tp>
437 static __sfinae_types::__one __check_const(_Tp&, _Class*);
438 template<typename _Tp, typename _Up>
439 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
440 template<typename _Tp, typename _Up>
441 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
442 template<typename _Tp>
443 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
444 template<typename _Tp>
445 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
448 template<typename _Tp>
450 : _Mem_fn_const_or_non<
452 (sizeof(__sfinae_types::__two)
453 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
456 template<typename _Signature>
459 template<typename _CVMem, typename _Tp>
460 struct result<_CVMem(_Tp)>
461 : public _Result_type<_Tp> { };
463 template<typename _CVMem, typename _Tp>
464 struct result<_CVMem(_Tp&)>
465 : public _Result_type<_Tp> { };
467 explicit _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
470 _Res& operator()(_Class& __object) const
471 { return __object.*__pm; }
473 const _Res& operator()(const _Class& __object) const
474 { return __object.*__pm; }
477 _Res& operator()(_Class* __object) const
478 { return __object->*__pm; }
481 operator()(const _Class* __object) const
482 { return __object->*__pm; }
484 // Handle smart pointers and derived
485 template<typename _Tp>
486 typename _Result_type<_Tp>::type
487 operator()(_Tp& __unknown) const
488 { return _M_call(__unknown, &__unknown); }
495 * @brief Returns a function object that forwards to the member
498 template<typename _Tp, typename _Class>
499 inline _Mem_fn<_Tp _Class::*>
500 mem_fn(_Tp _Class::* __pm)
502 return _Mem_fn<_Tp _Class::*>(__pm);
506 * @brief Determines if the given type _Tp is a function object
507 * should be treated as a subexpression when evaluating calls to
508 * function objects returned by bind(). [TR1 3.6.1]
510 template<typename _Tp>
511 struct is_bind_expression
513 static const bool value = false;
517 * @brief Determines if the given type _Tp is a placeholder in a
518 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
520 template<typename _Tp>
521 struct is_placeholder
523 static const int value = 0;
528 * The type of placeholder objects defined by libstdc++.
531 template<int _Num> struct _Placeholder { };
535 * Partial specialization of is_placeholder that provides the placeholder
536 * number for the placeholder objects defined by libstdc++.
540 struct is_placeholder<_Placeholder<_Num> >
542 static const int value = _Num;
547 * Maps an argument to bind() into an actual argument to the bound
548 * function object [TR1 3.6.3/5]. Only the first parameter should
549 * be specified: the rest are used to determine among the various
550 * implementations. Note that, although this class is a function
551 * object, isn't not entirely normal because it takes only two
552 * parameters regardless of the number of parameters passed to the
553 * bind expression. The first parameter is the bound argument and
554 * the second parameter is a tuple containing references to the
555 * rest of the arguments.
558 template<typename _Arg,
559 bool _IsBindExp = is_bind_expression<_Arg>::value,
560 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
565 * If the argument is reference_wrapper<_Tp>, returns the
566 * underlying reference. [TR1 3.6.3/5 bullet 1]
569 template<typename _Tp>
570 class _Mu<reference_wrapper<_Tp>, false, false>
573 typedef _Tp& result_type;
575 /* Note: This won't actually work for const volatile
576 * reference_wrappers, because reference_wrapper::get() is const
577 * but not volatile-qualified. This might be a defect in the TR.
579 template<typename _CVRef, typename _Tuple>
581 operator()(_CVRef& __arg, const _Tuple&) const volatile
582 { return __arg.get(); }
587 * If the argument is a bind expression, we invoke the underlying
588 * function object with the same cv-qualifiers as we are given and
589 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
592 template<typename _Arg>
593 class _Mu<_Arg, true, false>
596 template<typename _Signature> class result;
598 #define _GLIBCXX_REPEAT_HEADER <tr1/mu_iterate.h>
599 # include <tr1/repeat.h>
600 #undef _GLIBCXX_REPEAT_HEADER
605 * If the argument is a placeholder for the Nth argument, returns
606 * a reference to the Nth argument to the bind function object.
607 * [TR1 3.6.3/5 bullet 3]
610 template<typename _Arg>
611 class _Mu<_Arg, false, true>
614 template<typename _Signature> class result;
616 template<typename _CVMu, typename _CVArg, typename _Tuple>
617 class result<_CVMu(_CVArg, _Tuple)>
619 // Add a reference, if it hasn't already been done for us.
620 // This allows us to be a little bit sloppy in constructing
621 // the tuple that we pass to result_of<...>.
622 typedef typename tuple_element<(is_placeholder<_Arg>::value - 1),
623 _Tuple>::type __base_type;
626 typedef typename add_reference<__base_type>::type type;
629 template<typename _Tuple>
630 typename result<_Mu(_Arg, _Tuple)>::type
631 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
633 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
639 * If the argument is just a value, returns a reference to that
640 * value. The cv-qualifiers on the reference are the same as the
641 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
644 template<typename _Arg>
645 class _Mu<_Arg, false, false>
648 template<typename _Signature> struct result;
650 template<typename _CVMu, typename _CVArg, typename _Tuple>
651 struct result<_CVMu(_CVArg, _Tuple)>
653 typedef typename add_reference<_CVArg>::type type;
656 // Pick up the cv-qualifiers of the argument
657 template<typename _CVArg, typename _Tuple>
658 _CVArg& operator()(_CVArg& __arg, const _Tuple&) const volatile
664 * Maps member pointers into instances of _Mem_fn but leaves all
665 * other function objects untouched. Used by tr1::bind(). The
666 * primary template handles the non--member-pointer case.
669 template<typename _Tp>
670 struct _Maybe_wrap_member_pointer
673 static const _Tp& __do_wrap(const _Tp& __x) { return __x; }
678 * Maps member pointers into instances of _Mem_fn but leaves all
679 * other function objects untouched. Used by tr1::bind(). This
680 * partial specialization handles the member pointer case.
683 template<typename _Tp, typename _Class>
684 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
686 typedef _Mem_fn<_Tp _Class::*> type;
687 static type __do_wrap(_Tp _Class::* __pm) { return type(__pm); }
692 * Type of the function object returned from bind().
695 template<typename _Signature>
700 * Type of the function object returned from bind<R>().
703 template<typename _Result, typename _Signature>
708 * Class template _Bind is always a bind expression.
711 template<typename _Signature>
712 struct is_bind_expression<_Bind<_Signature> >
714 static const bool value = true;
719 * Class template _Bind_result is always a bind expression.
722 template<typename _Result, typename _Signature>
723 struct is_bind_expression<_Bind_result<_Result, _Signature> >
725 static const bool value = true;
729 * @brief Exception class thrown when class template function's
730 * operator() is called with an empty target.
733 class bad_function_call : public std::exception { };
737 * The integral constant expression 0 can be converted into a
738 * pointer to this type. It is used by the function template to
739 * accept NULL pointers.
742 struct _M_clear_type;
746 * Trait identifying "location-invariant" types, meaning that the
747 * address of the object (or any of its members) will not escape.
748 * Also implies a trivial copy constructor and assignment operator.
751 template<typename _Tp>
752 struct __is_location_invariant
753 : integral_constant<bool,
754 (is_pointer<_Tp>::value
755 || is_member_pointer<_Tp>::value)>
759 class _Undefined_class;
764 const void* _M_const_object;
765 void (*_M_function_pointer)();
766 void (_Undefined_class::*_M_member_pointer)();
770 void* _M_access() { return &_M_pod_data[0]; }
771 const void* _M_access() const { return &_M_pod_data[0]; }
773 template<typename _Tp> _Tp& _M_access()
774 { return *static_cast<_Tp*>(_M_access()); }
776 template<typename _Tp> const _Tp& _M_access() const
777 { return *static_cast<const _Tp*>(_M_access()); }
779 _Nocopy_types _M_unused;
780 char _M_pod_data[sizeof(_Nocopy_types)];
783 enum _Manager_operation
791 /* Simple type wrapper that helps avoid annoying const problems
792 when casting between void pointers and pointers-to-pointers. */
793 template<typename _Tp>
794 struct _Simple_type_wrapper
796 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
801 template<typename _Tp>
802 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
803 : __is_location_invariant<_Tp>
807 // Converts a reference to a function object into a callable
809 template<typename _Functor>
810 inline _Functor& __callable_functor(_Functor& __f) { return __f; }
812 template<typename _Member, typename _Class>
813 inline _Mem_fn<_Member _Class::*>
814 __callable_functor(_Member _Class::* &__p)
815 { return mem_fn(__p); }
817 template<typename _Member, typename _Class>
818 inline _Mem_fn<_Member _Class::*>
819 __callable_functor(_Member _Class::* const &__p)
820 { return mem_fn(__p); }
822 template<typename _Signature, typename _Functor>
823 class _Function_handler;
825 template<typename _Signature>
831 * Base class of all polymorphic function object wrappers.
837 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
838 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
840 template<typename _Functor>
844 static const bool __stored_locally =
845 (__is_location_invariant<_Functor>::value
846 && sizeof(_Functor) <= _M_max_size
847 && __alignof__(_Functor) <= _M_max_align
848 && (_M_max_align % __alignof__(_Functor) == 0));
849 typedef integral_constant<bool, __stored_locally> _Local_storage;
851 // Retrieve a pointer to the function object
852 static _Functor* _M_get_pointer(const _Any_data& __source)
854 const _Functor* __ptr =
855 __stored_locally? &__source._M_access<_Functor>()
856 /* have stored a pointer */ : __source._M_access<_Functor*>();
857 return const_cast<_Functor*>(__ptr);
860 // Clone a location-invariant function object that fits within
861 // an _Any_data structure.
863 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
865 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
868 // Clone a function object that is not location-invariant or
869 // that cannot fit into an _Any_data structure.
871 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
873 __dest._M_access<_Functor*>() =
874 new _Functor(*__source._M_access<_Functor*>());
877 // Destroying a location-invariant object may still require
880 _M_destroy(_Any_data& __victim, true_type)
882 __victim._M_access<_Functor>().~_Functor();
885 // Destroying an object located on the heap.
887 _M_destroy(_Any_data& __victim, false_type)
889 delete __victim._M_access<_Functor*>();
894 _M_manager(_Any_data& __dest, const _Any_data& __source,
895 _Manager_operation __op)
898 case __get_type_info:
899 __dest._M_access<const type_info*>() = &typeid(_Functor);
902 case __get_functor_ptr:
903 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
906 case __clone_functor:
907 _M_clone(__dest, __source, _Local_storage());
910 case __destroy_functor:
911 _M_destroy(__dest, _Local_storage());
918 _M_init_functor(_Any_data& __functor, const _Functor& __f)
920 _M_init_functor(__functor, __f, _Local_storage());
923 template<typename _Signature>
925 _M_not_empty_function(const function<_Signature>& __f)
930 template<typename _Tp>
932 _M_not_empty_function(const _Tp*& __fp)
937 template<typename _Class, typename _Tp>
939 _M_not_empty_function(_Tp _Class::* const& __mp)
944 template<typename _Tp>
946 _M_not_empty_function(const _Tp&)
953 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
955 new (__functor._M_access()) _Functor(__f);
959 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
961 __functor._M_access<_Functor*>() = new _Functor(__f);
965 template<typename _Functor>
966 class _Ref_manager : public _Base_manager<_Functor*>
968 typedef _Function_base::_Base_manager<_Functor*> _Base;
972 _M_manager(_Any_data& __dest, const _Any_data& __source,
973 _Manager_operation __op)
976 case __get_type_info:
977 __dest._M_access<const type_info*>() = &typeid(_Functor);
980 case __get_functor_ptr:
981 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
982 return is_const<_Functor>::value;
986 _Base::_M_manager(__dest, __source, __op);
992 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
994 // TBD: Use address_of function instead
995 _Base::_M_init_functor(__functor, &__f.get());
999 _Function_base() : _M_manager(0) { }
1005 _M_manager(_M_functor, _M_functor, __destroy_functor);
1010 bool _M_empty() const { return !_M_manager; }
1012 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1013 _Manager_operation);
1015 _Any_data _M_functor;
1016 _Manager_type _M_manager;
1019 // [3.7.2.7] null pointer comparisons
1022 * @brief Compares a polymorphic function object wrapper against 0
1023 * (the NULL pointer).
1024 * @returns @c true if the wrapper has no target, @c false otherwise
1026 * This function will not throw an exception.
1028 template<typename _Signature>
1030 operator==(const function<_Signature>& __f, _M_clear_type*)
1038 template<typename _Signature>
1040 operator==(_M_clear_type*, const function<_Signature>& __f)
1046 * @brief Compares a polymorphic function object wrapper against 0
1047 * (the NULL pointer).
1048 * @returns @c false if the wrapper has no target, @c true otherwise
1050 * This function will not throw an exception.
1052 template<typename _Signature>
1054 operator!=(const function<_Signature>& __f, _M_clear_type*)
1062 template<typename _Signature>
1064 operator!=(_M_clear_type*, const function<_Signature>& __f)
1069 // [3.7.2.8] specialized algorithms
1072 * @brief Swap the targets of two polymorphic function object wrappers.
1074 * This function will not throw an exception.
1076 template<typename _Signature>
1078 swap(function<_Signature>& __x, function<_Signature>& __y)
1083 #define _GLIBCXX_JOIN(X,Y) _GLIBCXX_JOIN2( X , Y )
1084 #define _GLIBCXX_JOIN2(X,Y) _GLIBCXX_JOIN3(X,Y)
1085 #define _GLIBCXX_JOIN3(X,Y) X##Y
1086 #define _GLIBCXX_REPEAT_HEADER <tr1/functional_iterate.h>
1087 #include <tr1/repeat.h>
1088 #undef _GLIBCXX_REPEAT_HEADER
1089 #undef _GLIBCXX_JOIN3
1090 #undef _GLIBCXX_JOIN2
1091 #undef _GLIBCXX_JOIN
1093 // Definition of default hash function std::tr1::hash<>. The types for
1094 // which std::tr1::hash<T> is defined is in clause 6.3.3. of the PDTR.
1096 template <typename T> struct hash;
1098 #define tr1_hashtable_define_trivial_hash(T) \
1099 template <> struct hash<T> { \
1100 std::size_t operator()(T val) const { return static_cast<std::size_t>(val); } \
1103 tr1_hashtable_define_trivial_hash(bool);
1104 tr1_hashtable_define_trivial_hash(char);
1105 tr1_hashtable_define_trivial_hash(signed char);
1106 tr1_hashtable_define_trivial_hash(unsigned char);
1107 tr1_hashtable_define_trivial_hash(wchar_t);
1108 tr1_hashtable_define_trivial_hash(short);
1109 tr1_hashtable_define_trivial_hash(int);
1110 tr1_hashtable_define_trivial_hash(long);
1111 tr1_hashtable_define_trivial_hash(unsigned short);
1112 tr1_hashtable_define_trivial_hash(unsigned int);
1113 tr1_hashtable_define_trivial_hash(unsigned long);
1115 tr1_hashtable_define_trivial_hash(float);
1116 tr1_hashtable_define_trivial_hash(double);
1117 tr1_hashtable_define_trivial_hash(long double);
1119 #undef tr1_hashtable_define_trivial_hash
1121 template <typename T>
1123 std::size_t operator()(T* p) const {
1124 return reinterpret_cast<std::size_t>(p);
1128 // ??? We can probably find a better hash function than this (i.e. one
1129 // that vectorizes better and that produces a more uniform distribution).
1131 // XXX String hash probably shouldn't be an inline member function,
1132 // since it's nontrivial. Once we have the framework for TR1 .cc
1133 // files, this should go in one.
1136 struct hash<std::string>
1138 std::size_t operator()(const std::string& s) const
1140 std::size_t result = 0;
1141 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
1142 result = (result * 131) + *i;
1147 #ifdef _GLIBCXX_USE_WCHAR_T
1149 struct hash<std::wstring>
1151 std::size_t operator()(const std::wstring& s) const
1153 std::size_t result = 0;
1154 for (std::wstring::const_iterator i = s.begin(); i != s.end(); ++i)
1155 result = (result * 131) + *i;