1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006 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.
31 * This is a TR1 C++ Library header.
34 #ifndef _TR1_FUNCTIONAL
35 #define _TR1_FUNCTIONAL 1
37 #pragma GCC system_header
39 #include "../functional"
41 #include <tr1/type_traits>
42 #include <ext/type_traits.h>
43 #include <string> // for std::tr1::hash
44 #include <cstdlib> // for std::abort
45 #include <cmath> // for std::frexp
50 _GLIBCXX_BEGIN_NAMESPACE(tr1)
52 template<typename _MemberPointer>
57 * Actual implementation of _Has_result_type, which uses SFINAE to
58 * determine if the type _Tp has a publicly-accessible member type
62 template<typename _Tp>
63 class _Has_result_type_helper : __sfinae_types
65 template<typename _Up>
69 template<typename _Up>
70 static __one __test(_Wrap_type<typename _Up::result_type>*);
72 template<typename _Up>
73 static __two __test(...);
76 static const bool value = sizeof(__test<_Tp>(0)) == 1;
79 template<typename _Tp>
80 struct _Has_result_type
83 _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
88 * If we have found a result_type, extract it.
91 template<bool _Has_result_type, typename _Functor>
92 struct _Maybe_get_result_type
95 template<typename _Functor>
96 struct _Maybe_get_result_type<true, _Functor>
98 typedef typename _Functor::result_type result_type;
103 * Base class for any function object that has a weak result type, as
104 * defined in 3.3/3 of TR1.
107 template<typename _Functor>
108 struct _Weak_result_type_impl
109 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
115 * Strip top-level cv-qualifiers from the function object and let
116 * _Weak_result_type_impl perform the real work.
119 template<typename _Functor>
120 struct _Weak_result_type
121 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
125 template<typename _Signature>
130 * Actual implementation of result_of. When _Has_result_type is
131 * true, gets its result from _Weak_result_type. Otherwise, uses
132 * the function object's member template result to extract the
136 template<bool _Has_result_type, typename _Signature>
137 struct _Result_of_impl;
139 // Handle member data pointers using _Mem_fn's logic
140 template<typename _Res, typename _Class, typename _T1>
141 struct _Result_of_impl<false, _Res _Class::*(_T1)>
143 typedef typename _Mem_fn<_Res _Class::*>
144 ::template _Result_type<_T1>::type type;
149 * Determines if the type _Tp derives from unary_function.
152 template<typename _Tp>
153 struct _Derives_from_unary_function : __sfinae_types
156 template<typename _T1, typename _Res>
157 static __one __test(const volatile unary_function<_T1, _Res>*);
159 // It's tempting to change "..." to const volatile void*, but
160 // that fails when _Tp is a function type.
161 static __two __test(...);
164 static const bool value = sizeof(__test((_Tp*)0)) == 1;
169 * Determines if the type _Tp derives from binary_function.
172 template<typename _Tp>
173 struct _Derives_from_binary_function : __sfinae_types
176 template<typename _T1, typename _T2, typename _Res>
177 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
179 // It's tempting to change "..." to const volatile void*, but
180 // that fails when _Tp is a function type.
181 static __two __test(...);
184 static const bool value = sizeof(__test((_Tp*)0)) == 1;
189 * Turns a function type into a function pointer type
192 template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
193 struct _Function_to_function_pointer
198 template<typename _Tp>
199 struct _Function_to_function_pointer<_Tp, true>
206 * Knowing which of unary_function and binary_function _Tp derives
207 * from, derives from the same and ensures that reference_wrapper
208 * will have a weak result type. See cases below.
211 template<bool _Unary, bool _Binary, typename _Tp>
212 struct _Reference_wrapper_base_impl;
214 // Not a unary_function or binary_function, so try a weak result type
215 template<typename _Tp>
216 struct _Reference_wrapper_base_impl<false, false, _Tp>
217 : _Weak_result_type<_Tp>
220 // unary_function but not binary_function
221 template<typename _Tp>
222 struct _Reference_wrapper_base_impl<true, false, _Tp>
223 : unary_function<typename _Tp::argument_type,
224 typename _Tp::result_type>
227 // binary_function but not unary_function
228 template<typename _Tp>
229 struct _Reference_wrapper_base_impl<false, true, _Tp>
230 : binary_function<typename _Tp::first_argument_type,
231 typename _Tp::second_argument_type,
232 typename _Tp::result_type>
235 // both unary_function and binary_function. import result_type to
237 template<typename _Tp>
238 struct _Reference_wrapper_base_impl<true, true, _Tp>
239 : unary_function<typename _Tp::argument_type,
240 typename _Tp::result_type>,
241 binary_function<typename _Tp::first_argument_type,
242 typename _Tp::second_argument_type,
243 typename _Tp::result_type>
245 typedef typename _Tp::result_type result_type;
250 * Derives from unary_function or binary_function when it
251 * can. Specializations handle all of the easy cases. The primary
252 * template determines what to do with a class type, which may
253 * derive from both unary_function and binary_function.
256 template<typename _Tp>
257 struct _Reference_wrapper_base
258 : _Reference_wrapper_base_impl<
259 _Derives_from_unary_function<_Tp>::value,
260 _Derives_from_binary_function<_Tp>::value,
264 // - a function type (unary)
265 template<typename _Res, typename _T1>
266 struct _Reference_wrapper_base<_Res(_T1)>
267 : unary_function<_T1, _Res>
270 // - a function type (binary)
271 template<typename _Res, typename _T1, typename _T2>
272 struct _Reference_wrapper_base<_Res(_T1, _T2)>
273 : binary_function<_T1, _T2, _Res>
276 // - a function pointer type (unary)
277 template<typename _Res, typename _T1>
278 struct _Reference_wrapper_base<_Res(*)(_T1)>
279 : unary_function<_T1, _Res>
282 // - a function pointer type (binary)
283 template<typename _Res, typename _T1, typename _T2>
284 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
285 : binary_function<_T1, _T2, _Res>
288 // - a pointer to member function type (unary, no qualifiers)
289 template<typename _Res, typename _T1>
290 struct _Reference_wrapper_base<_Res (_T1::*)()>
291 : unary_function<_T1*, _Res>
294 // - a pointer to member function type (binary, no qualifiers)
295 template<typename _Res, typename _T1, typename _T2>
296 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
297 : binary_function<_T1*, _T2, _Res>
300 // - a pointer to member function type (unary, const)
301 template<typename _Res, typename _T1>
302 struct _Reference_wrapper_base<_Res (_T1::*)() const>
303 : unary_function<const _T1*, _Res>
306 // - a pointer to member function type (binary, const)
307 template<typename _Res, typename _T1, typename _T2>
308 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
309 : binary_function<const _T1*, _T2, _Res>
312 // - a pointer to member function type (unary, volatile)
313 template<typename _Res, typename _T1>
314 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
315 : unary_function<volatile _T1*, _Res>
318 // - a pointer to member function type (binary, volatile)
319 template<typename _Res, typename _T1, typename _T2>
320 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
321 : binary_function<volatile _T1*, _T2, _Res>
324 // - a pointer to member function type (unary, const volatile)
325 template<typename _Res, typename _T1>
326 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
327 : unary_function<const volatile _T1*, _Res>
330 // - a pointer to member function type (binary, const volatile)
331 template<typename _Res, typename _T1, typename _T2>
332 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
333 : binary_function<const volatile _T1*, _T2, _Res>
336 template<typename _Tp>
337 class reference_wrapper
338 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
340 // If _Tp is a function type, we can't form result_of<_Tp(...)>,
341 // so turn it into a function pointer type.
342 typedef typename _Function_to_function_pointer<_Tp>::type
348 explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
351 reference_wrapper(const reference_wrapper<_Tp>& __inref):
352 _M_data(__inref._M_data)
356 operator=(const reference_wrapper<_Tp>& __inref)
358 _M_data = __inref._M_data;
362 operator _Tp&() const
363 { return this->get(); }
369 #define _GLIBCXX_REPEAT_HEADER <tr1/ref_wrap_iterate.h>
370 #include <tr1/repeat.h>
371 #undef _GLIBCXX_REPEAT_HEADER
375 // Denotes a reference should be taken to a variable.
376 template<typename _Tp>
377 inline reference_wrapper<_Tp>
379 { return reference_wrapper<_Tp>(__t); }
381 // Denotes a const reference should be taken to a variable.
382 template<typename _Tp>
383 inline reference_wrapper<const _Tp>
385 { return reference_wrapper<const _Tp>(__t); }
387 template<typename _Tp>
388 inline reference_wrapper<_Tp>
389 ref(reference_wrapper<_Tp> __t)
390 { return ref(__t.get()); }
392 template<typename _Tp>
393 inline reference_wrapper<const _Tp>
394 cref(reference_wrapper<_Tp> __t)
395 { return cref(__t.get()); }
397 template<typename _Tp, bool>
398 struct _Mem_fn_const_or_non
400 typedef const _Tp& type;
403 template<typename _Tp>
404 struct _Mem_fn_const_or_non<_Tp, false>
409 template<typename _Res, typename _Class>
410 class _Mem_fn<_Res _Class::*>
412 // This bit of genius is due to Peter Dimov, improved slightly by
414 template<typename _Tp>
416 _M_call(_Tp& __object, _Class *) const
417 { return __object.*__pm; }
419 template<typename _Tp, typename _Up>
421 _M_call(_Tp& __object, _Up * const *) const
422 { return (*__object).*__pm; }
424 template<typename _Tp, typename _Up>
426 _M_call(_Tp& __object, const _Up * const *) const
427 { return (*__object).*__pm; }
429 template<typename _Tp>
431 _M_call(_Tp& __object, const _Class *) const
432 { return __object.*__pm; }
434 template<typename _Tp>
436 _M_call(_Tp& __ptr, const volatile void*) const
437 { return (*__ptr).*__pm; }
439 template<typename _Tp> static _Tp& __get_ref();
441 template<typename _Tp>
442 static __sfinae_types::__one __check_const(_Tp&, _Class*);
443 template<typename _Tp, typename _Up>
444 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
445 template<typename _Tp, typename _Up>
446 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
447 template<typename _Tp>
448 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
449 template<typename _Tp>
450 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
453 template<typename _Tp>
455 : _Mem_fn_const_or_non<
457 (sizeof(__sfinae_types::__two)
458 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
461 template<typename _Signature>
464 template<typename _CVMem, typename _Tp>
465 struct result<_CVMem(_Tp)>
466 : public _Result_type<_Tp> { };
468 template<typename _CVMem, typename _Tp>
469 struct result<_CVMem(_Tp&)>
470 : public _Result_type<_Tp> { };
472 explicit _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
475 _Res& operator()(_Class& __object) const
476 { return __object.*__pm; }
478 const _Res& operator()(const _Class& __object) const
479 { return __object.*__pm; }
482 _Res& operator()(_Class* __object) const
483 { return __object->*__pm; }
486 operator()(const _Class* __object) const
487 { return __object->*__pm; }
489 // Handle smart pointers and derived
490 template<typename _Tp>
491 typename _Result_type<_Tp>::type
492 operator()(_Tp& __unknown) const
493 { return _M_call(__unknown, &__unknown); }
500 * @brief Returns a function object that forwards to the member
503 template<typename _Tp, typename _Class>
504 inline _Mem_fn<_Tp _Class::*>
505 mem_fn(_Tp _Class::* __pm)
507 return _Mem_fn<_Tp _Class::*>(__pm);
511 * @brief Determines if the given type _Tp is a function object
512 * should be treated as a subexpression when evaluating calls to
513 * function objects returned by bind(). [TR1 3.6.1]
515 template<typename _Tp>
516 struct is_bind_expression
517 { static const bool value = false; };
519 template<typename _Tp>
520 const bool is_bind_expression<_Tp>::value;
523 * @brief Determines if the given type _Tp is a placeholder in a
524 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
526 template<typename _Tp>
527 struct is_placeholder
528 { static const int value = 0; };
530 template<typename _Tp>
531 const int is_placeholder<_Tp>::value;
535 * The type of placeholder objects defined by libstdc++.
538 template<int _Num> struct _Placeholder { };
542 * Partial specialization of is_placeholder that provides the placeholder
543 * number for the placeholder objects defined by libstdc++.
547 struct is_placeholder<_Placeholder<_Num> >
548 { static const int value = _Num; };
551 const int is_placeholder<_Placeholder<_Num> >::value;
555 * Maps an argument to bind() into an actual argument to the bound
556 * function object [TR1 3.6.3/5]. Only the first parameter should
557 * be specified: the rest are used to determine among the various
558 * implementations. Note that, although this class is a function
559 * object, isn't not entirely normal because it takes only two
560 * parameters regardless of the number of parameters passed to the
561 * bind expression. The first parameter is the bound argument and
562 * the second parameter is a tuple containing references to the
563 * rest of the arguments.
566 template<typename _Arg,
567 bool _IsBindExp = is_bind_expression<_Arg>::value,
568 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
573 * If the argument is reference_wrapper<_Tp>, returns the
574 * underlying reference. [TR1 3.6.3/5 bullet 1]
577 template<typename _Tp>
578 class _Mu<reference_wrapper<_Tp>, false, false>
581 typedef _Tp& result_type;
583 /* Note: This won't actually work for const volatile
584 * reference_wrappers, because reference_wrapper::get() is const
585 * but not volatile-qualified. This might be a defect in the TR.
587 template<typename _CVRef, typename _Tuple>
589 operator()(_CVRef& __arg, const _Tuple&) const volatile
590 { return __arg.get(); }
595 * If the argument is a bind expression, we invoke the underlying
596 * function object with the same cv-qualifiers as we are given and
597 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
600 template<typename _Arg>
601 class _Mu<_Arg, true, false>
604 template<typename _Signature> class result;
606 #define _GLIBCXX_REPEAT_HEADER <tr1/mu_iterate.h>
607 # include <tr1/repeat.h>
608 #undef _GLIBCXX_REPEAT_HEADER
613 * If the argument is a placeholder for the Nth argument, returns
614 * a reference to the Nth argument to the bind function object.
615 * [TR1 3.6.3/5 bullet 3]
618 template<typename _Arg>
619 class _Mu<_Arg, false, true>
622 template<typename _Signature> class result;
624 template<typename _CVMu, typename _CVArg, typename _Tuple>
625 class result<_CVMu(_CVArg, _Tuple)>
627 // Add a reference, if it hasn't already been done for us.
628 // This allows us to be a little bit sloppy in constructing
629 // the tuple that we pass to result_of<...>.
630 typedef typename tuple_element<(is_placeholder<_Arg>::value - 1),
631 _Tuple>::type __base_type;
634 typedef typename add_reference<__base_type>::type type;
637 template<typename _Tuple>
638 typename result<_Mu(_Arg, _Tuple)>::type
639 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
641 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
647 * If the argument is just a value, returns a reference to that
648 * value. The cv-qualifiers on the reference are the same as the
649 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
652 template<typename _Arg>
653 class _Mu<_Arg, false, false>
656 template<typename _Signature> struct result;
658 template<typename _CVMu, typename _CVArg, typename _Tuple>
659 struct result<_CVMu(_CVArg, _Tuple)>
661 typedef typename add_reference<_CVArg>::type type;
664 // Pick up the cv-qualifiers of the argument
665 template<typename _CVArg, typename _Tuple>
666 _CVArg& operator()(_CVArg& __arg, const _Tuple&) const volatile
672 * Maps member pointers into instances of _Mem_fn but leaves all
673 * other function objects untouched. Used by tr1::bind(). The
674 * primary template handles the non--member-pointer case.
677 template<typename _Tp>
678 struct _Maybe_wrap_member_pointer
681 static const _Tp& __do_wrap(const _Tp& __x) { return __x; }
686 * Maps member pointers into instances of _Mem_fn but leaves all
687 * other function objects untouched. Used by tr1::bind(). This
688 * partial specialization handles the member pointer case.
691 template<typename _Tp, typename _Class>
692 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
694 typedef _Mem_fn<_Tp _Class::*> type;
695 static type __do_wrap(_Tp _Class::* __pm) { return type(__pm); }
700 * Type of the function object returned from bind().
703 template<typename _Signature>
708 * Type of the function object returned from bind<R>().
711 template<typename _Result, typename _Signature>
716 * Class template _Bind is always a bind expression.
719 template<typename _Signature>
720 struct is_bind_expression<_Bind<_Signature> >
721 { static const bool value = true; };
723 template<typename _Signature>
724 const bool is_bind_expression<_Bind<_Signature> >::value;
728 * Class template _Bind_result is always a bind expression.
731 template<typename _Result, typename _Signature>
732 struct is_bind_expression<_Bind_result<_Result, _Signature> >
733 { static const bool value = true; };
735 template<typename _Result, typename _Signature>
736 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
739 * @brief Exception class thrown when class template function's
740 * operator() is called with an empty target.
743 class bad_function_call : public std::exception { };
747 * The integral constant expression 0 can be converted into a
748 * pointer to this type. It is used by the function template to
749 * accept NULL pointers.
752 struct _M_clear_type;
756 * Trait identifying "location-invariant" types, meaning that the
757 * address of the object (or any of its members) will not escape.
758 * Also implies a trivial copy constructor and assignment operator.
761 template<typename _Tp>
762 struct __is_location_invariant
763 : integral_constant<bool,
764 (is_pointer<_Tp>::value
765 || is_member_pointer<_Tp>::value)>
769 class _Undefined_class;
774 const void* _M_const_object;
775 void (*_M_function_pointer)();
776 void (_Undefined_class::*_M_member_pointer)();
780 void* _M_access() { return &_M_pod_data[0]; }
781 const void* _M_access() const { return &_M_pod_data[0]; }
783 template<typename _Tp> _Tp& _M_access()
784 { return *static_cast<_Tp*>(_M_access()); }
786 template<typename _Tp> const _Tp& _M_access() const
787 { return *static_cast<const _Tp*>(_M_access()); }
789 _Nocopy_types _M_unused;
790 char _M_pod_data[sizeof(_Nocopy_types)];
793 enum _Manager_operation
801 /* Simple type wrapper that helps avoid annoying const problems
802 when casting between void pointers and pointers-to-pointers. */
803 template<typename _Tp>
804 struct _Simple_type_wrapper
806 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
811 template<typename _Tp>
812 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
813 : __is_location_invariant<_Tp>
817 // Converts a reference to a function object into a callable
819 template<typename _Functor>
820 inline _Functor& __callable_functor(_Functor& __f) { return __f; }
822 template<typename _Member, typename _Class>
823 inline _Mem_fn<_Member _Class::*>
824 __callable_functor(_Member _Class::* &__p)
825 { return mem_fn(__p); }
827 template<typename _Member, typename _Class>
828 inline _Mem_fn<_Member _Class::*>
829 __callable_functor(_Member _Class::* const &__p)
830 { return mem_fn(__p); }
832 template<typename _Signature, typename _Functor>
833 class _Function_handler;
835 template<typename _Signature>
841 * Base class of all polymorphic function object wrappers.
847 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
848 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
850 template<typename _Functor>
854 static const bool __stored_locally =
855 (__is_location_invariant<_Functor>::value
856 && sizeof(_Functor) <= _M_max_size
857 && __alignof__(_Functor) <= _M_max_align
858 && (_M_max_align % __alignof__(_Functor) == 0));
859 typedef integral_constant<bool, __stored_locally> _Local_storage;
861 // Retrieve a pointer to the function object
862 static _Functor* _M_get_pointer(const _Any_data& __source)
864 const _Functor* __ptr =
865 __stored_locally? &__source._M_access<_Functor>()
866 /* have stored a pointer */ : __source._M_access<_Functor*>();
867 return const_cast<_Functor*>(__ptr);
870 // Clone a location-invariant function object that fits within
871 // an _Any_data structure.
873 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
875 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
878 // Clone a function object that is not location-invariant or
879 // that cannot fit into an _Any_data structure.
881 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
883 __dest._M_access<_Functor*>() =
884 new _Functor(*__source._M_access<_Functor*>());
887 // Destroying a location-invariant object may still require
890 _M_destroy(_Any_data& __victim, true_type)
892 __victim._M_access<_Functor>().~_Functor();
895 // Destroying an object located on the heap.
897 _M_destroy(_Any_data& __victim, false_type)
899 delete __victim._M_access<_Functor*>();
904 _M_manager(_Any_data& __dest, const _Any_data& __source,
905 _Manager_operation __op)
908 case __get_type_info:
909 __dest._M_access<const type_info*>() = &typeid(_Functor);
912 case __get_functor_ptr:
913 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
916 case __clone_functor:
917 _M_clone(__dest, __source, _Local_storage());
920 case __destroy_functor:
921 _M_destroy(__dest, _Local_storage());
928 _M_init_functor(_Any_data& __functor, const _Functor& __f)
930 _M_init_functor(__functor, __f, _Local_storage());
933 template<typename _Signature>
935 _M_not_empty_function(const function<_Signature>& __f)
940 template<typename _Tp>
942 _M_not_empty_function(const _Tp*& __fp)
947 template<typename _Class, typename _Tp>
949 _M_not_empty_function(_Tp _Class::* const& __mp)
954 template<typename _Tp>
956 _M_not_empty_function(const _Tp&)
963 _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
965 new (__functor._M_access()) _Functor(__f);
969 _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
971 __functor._M_access<_Functor*>() = new _Functor(__f);
975 template<typename _Functor>
976 class _Ref_manager : public _Base_manager<_Functor*>
978 typedef _Function_base::_Base_manager<_Functor*> _Base;
982 _M_manager(_Any_data& __dest, const _Any_data& __source,
983 _Manager_operation __op)
986 case __get_type_info:
987 __dest._M_access<const type_info*>() = &typeid(_Functor);
990 case __get_functor_ptr:
991 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
992 return is_const<_Functor>::value;
996 _Base::_M_manager(__dest, __source, __op);
1002 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1004 // TBD: Use address_of function instead
1005 _Base::_M_init_functor(__functor, &__f.get());
1009 _Function_base() : _M_manager(0) { }
1015 _M_manager(_M_functor, _M_functor, __destroy_functor);
1020 bool _M_empty() const { return !_M_manager; }
1022 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1023 _Manager_operation);
1025 _Any_data _M_functor;
1026 _Manager_type _M_manager;
1029 // [3.7.2.7] null pointer comparisons
1032 * @brief Compares a polymorphic function object wrapper against 0
1033 * (the NULL pointer).
1034 * @returns @c true if the wrapper has no target, @c false otherwise
1036 * This function will not throw an exception.
1038 template<typename _Signature>
1040 operator==(const function<_Signature>& __f, _M_clear_type*)
1048 template<typename _Signature>
1050 operator==(_M_clear_type*, const function<_Signature>& __f)
1056 * @brief Compares a polymorphic function object wrapper against 0
1057 * (the NULL pointer).
1058 * @returns @c false if the wrapper has no target, @c true otherwise
1060 * This function will not throw an exception.
1062 template<typename _Signature>
1064 operator!=(const function<_Signature>& __f, _M_clear_type*)
1072 template<typename _Signature>
1074 operator!=(_M_clear_type*, const function<_Signature>& __f)
1079 // [3.7.2.8] specialized algorithms
1082 * @brief Swap the targets of two polymorphic function object wrappers.
1084 * This function will not throw an exception.
1086 template<typename _Signature>
1088 swap(function<_Signature>& __x, function<_Signature>& __y)
1093 #define _GLIBCXX_JOIN(X,Y) _GLIBCXX_JOIN2( X , Y )
1094 #define _GLIBCXX_JOIN2(X,Y) _GLIBCXX_JOIN3(X,Y)
1095 #define _GLIBCXX_JOIN3(X,Y) X##Y
1096 #define _GLIBCXX_REPEAT_HEADER <tr1/functional_iterate.h>
1097 #include <tr1/repeat.h>
1098 #undef _GLIBCXX_REPEAT_HEADER
1099 #undef _GLIBCXX_JOIN3
1100 #undef _GLIBCXX_JOIN2
1101 #undef _GLIBCXX_JOIN
1103 // Definition of default hash function std::tr1::hash<>. The types for
1104 // which std::tr1::hash<T> is defined is in clause 6.3.3. of the PDTR.
1105 template<typename T>
1108 #define _TR1_hashtable_define_trivial_hash(_Tp) \
1111 : public std::unary_function<_Tp, std::size_t> \
1114 operator()(_Tp __val) const \
1115 { return static_cast<std::size_t>(__val); } \
1118 _TR1_hashtable_define_trivial_hash(bool);
1119 _TR1_hashtable_define_trivial_hash(char);
1120 _TR1_hashtable_define_trivial_hash(signed char);
1121 _TR1_hashtable_define_trivial_hash(unsigned char);
1122 _TR1_hashtable_define_trivial_hash(wchar_t);
1123 _TR1_hashtable_define_trivial_hash(short);
1124 _TR1_hashtable_define_trivial_hash(int);
1125 _TR1_hashtable_define_trivial_hash(long);
1126 _TR1_hashtable_define_trivial_hash(unsigned short);
1127 _TR1_hashtable_define_trivial_hash(unsigned int);
1128 _TR1_hashtable_define_trivial_hash(unsigned long);
1130 #undef _TR1_hashtable_define_trivial_hash
1132 template<typename _Tp>
1134 : public std::unary_function<_Tp*, std::size_t>
1137 operator()(_Tp* __p) const
1138 { return reinterpret_cast<std::size_t>(__p); }
1141 // Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
1142 // (used by the next specializations of std::tr1::hash<>)
1144 // Dummy generic implementation (for sizeof(size_t) != 4, 8).
1145 template<std::size_t = sizeof(std::size_t)>
1149 hash(const char* __first, std::size_t __length)
1151 std::size_t __result = 0;
1152 for (; __length > 0; --__length)
1153 __result = (__result * 131) + *__first++;
1162 hash(const char* __first, std::size_t __length)
1164 std::size_t __result = static_cast<std::size_t>(2166136261UL);
1165 for (; __length > 0; --__length)
1167 __result ^= (std::size_t)*__first++;
1168 __result *= 16777619UL;
1178 hash(const char* __first, std::size_t __length)
1180 std::size_t __result =
1181 static_cast<std::size_t>(14695981039346656037ULL);
1182 for (; __length > 0; --__length)
1184 __result ^= (std::size_t)*__first++;
1185 __result *= 1099511628211ULL;
1191 // XXX String and floating point hashes probably shouldn't be inline
1192 // member functions, since are nontrivial. Once we have the framework
1193 // for TR1 .cc files, these should go in one.
1195 struct hash<std::string>
1196 : public std::unary_function<std::string, std::size_t>
1199 operator()(const std::string& __s) const
1200 { return _Fnv_hash<>::hash(__s.data(), __s.length()); }
1203 #ifdef _GLIBCXX_USE_WCHAR_T
1205 struct hash<std::wstring>
1206 : public std::unary_function<std::wstring, std::size_t>
1209 operator()(const std::wstring& __s) const
1211 return _Fnv_hash<>::hash(reinterpret_cast<const char*>(__s.data()),
1212 __s.length() * sizeof(wchar_t));
1219 : public std::unary_function<float, std::size_t>
1222 operator()(float __fval) const
1224 std::size_t __result = 0;
1226 // 0 and -0 both hash to zero.
1228 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__fval),
1236 : public std::unary_function<double, std::size_t>
1239 operator()(double __dval) const
1241 std::size_t __result = 0;
1243 // 0 and -0 both hash to zero.
1245 __result = _Fnv_hash<>::hash(reinterpret_cast<const char*>(&__dval),
1251 // For long double, careful with random padding bits (e.g., on x86,
1252 // 10 bytes -> 12 bytes) and resort to frexp.
1254 struct hash<long double>
1255 : public std::unary_function<long double, std::size_t>
1258 operator()(long double __ldval) const
1260 std::size_t __result = 0;
1263 __ldval = std::frexp(__ldval, &__exponent);
1264 __ldval = __ldval < 0.0l ? -(__ldval + 0.5l) : __ldval;
1266 const long double __mult =
1267 std::numeric_limits<std::size_t>::max() + 1.0l;
1270 // Try to use all the bits of the mantissa (really necessary only
1271 // on 32-bit targets, at least for 80-bit floating point formats).
1272 const std::size_t __hibits = (std::size_t)__ldval;
1273 __ldval = (__ldval - (long double)__hibits) * __mult;
1275 const std::size_t __coeff =
1276 (std::numeric_limits<std::size_t>::max()
1277 / std::numeric_limits<long double>::max_exponent);
1279 __result = __hibits + (std::size_t)__ldval + __coeff * __exponent;
1285 _GLIBCXX_END_NAMESPACE