// -*- C++ -*- // Copyright (C) 2001-2015 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Under Section 7 of GPL version 3, you are granted additional // permissions described in the GCC Runtime Library Exception, version // 3.1, as published by the Free Software Foundation. // You should have received a copy of the GNU General Public License and // a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see // . /* * Copyright (c) 1997 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Silicon Graphics makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * */ /** @file include/functional * This is a Standard C++ Library header. */ #ifndef _GLIBCXX_FUNCTIONAL #define _GLIBCXX_FUNCTIONAL 1 #pragma GCC system_header #include #include #if __cplusplus >= 201103L #include #include #include #include #include #include namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION template class _Mem_fn; template _Mem_fn<_Tp _Class::*> mem_fn(_Tp _Class::*) noexcept; /// If we have found a result_type, extract it. template> struct _Maybe_get_result_type { }; template struct _Maybe_get_result_type<_Functor, __void_t> { typedef typename _Functor::result_type result_type; }; /** * Base class for any function object that has a weak result type, as * defined in 20.8.2 [func.require] of C++11. */ template struct _Weak_result_type_impl : _Maybe_get_result_type<_Functor> { }; /// Retrieve the result type for a function type. template struct _Weak_result_type_impl<_Res(_ArgTypes...)> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res(_ArgTypes......)> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res(_ArgTypes...) const> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res(_ArgTypes......) const> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile> { typedef _Res result_type; }; /// Retrieve the result type for a function reference. template struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)> { typedef _Res result_type; }; /// Retrieve the result type for a function pointer. template struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)> { typedef _Res result_type; }; /// Retrieve result type for a member function pointer. template struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)> { typedef _Res result_type; }; /// Retrieve result type for a const member function pointer. template struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const> { typedef _Res result_type; }; /// Retrieve result type for a volatile member function pointer. template struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile> { typedef _Res result_type; }; /// Retrieve result type for a const volatile member function pointer. template struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const volatile> { typedef _Res result_type; }; template struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const volatile> { typedef _Res result_type; }; /** * Strip top-level cv-qualifiers from the function object and let * _Weak_result_type_impl perform the real work. */ template struct _Weak_result_type : _Weak_result_type_impl::type> { }; /** * Invoke a function object, which may be either a member pointer or a * function object. The first parameter will tell which. */ template inline typename enable_if< (!is_member_pointer<_Functor>::value && !is_function<_Functor>::value && !is_function::type>::value), typename result_of<_Functor&(_Args&&...)>::type >::type __invoke(_Functor& __f, _Args&&... __args) { return __f(std::forward<_Args>(__args)...); } template inline typename enable_if< (is_member_pointer<_Functor>::value && !is_function<_Functor>::value && !is_function::type>::value), typename result_of<_Functor(_Args&&...)>::type >::type __invoke(_Functor& __f, _Args&&... __args) { return std::mem_fn(__f)(std::forward<_Args>(__args)...); } // To pick up function references (that will become function pointers) template inline typename enable_if< (is_pointer<_Functor>::value && is_function::type>::value), typename result_of<_Functor(_Args&&...)>::type >::type __invoke(_Functor __f, _Args&&... __args) { return __f(std::forward<_Args>(__args)...); } /** * Knowing which of unary_function and binary_function _Tp derives * from, derives from the same and ensures that reference_wrapper * will have a weak result type. See cases below. */ template struct _Reference_wrapper_base_impl; // None of the nested argument types. template struct _Reference_wrapper_base_impl : _Weak_result_type<_Tp> { }; // Nested argument_type only. template struct _Reference_wrapper_base_impl : _Weak_result_type<_Tp> { typedef typename _Tp::argument_type argument_type; }; // Nested first_argument_type and second_argument_type only. template struct _Reference_wrapper_base_impl : _Weak_result_type<_Tp> { typedef typename _Tp::first_argument_type first_argument_type; typedef typename _Tp::second_argument_type second_argument_type; }; // All the nested argument types. template struct _Reference_wrapper_base_impl : _Weak_result_type<_Tp> { typedef typename _Tp::argument_type argument_type; typedef typename _Tp::first_argument_type first_argument_type; typedef typename _Tp::second_argument_type second_argument_type; }; _GLIBCXX_HAS_NESTED_TYPE(argument_type) _GLIBCXX_HAS_NESTED_TYPE(first_argument_type) _GLIBCXX_HAS_NESTED_TYPE(second_argument_type) /** * Derives from unary_function or binary_function when it * can. Specializations handle all of the easy cases. The primary * template determines what to do with a class type, which may * derive from both unary_function and binary_function. */ template struct _Reference_wrapper_base : _Reference_wrapper_base_impl< __has_argument_type<_Tp>::value, __has_first_argument_type<_Tp>::value && __has_second_argument_type<_Tp>::value, _Tp> { }; // - a function type (unary) template struct _Reference_wrapper_base<_Res(_T1)> : unary_function<_T1, _Res> { }; template struct _Reference_wrapper_base<_Res(_T1) const> : unary_function<_T1, _Res> { }; template struct _Reference_wrapper_base<_Res(_T1) volatile> : unary_function<_T1, _Res> { }; template struct _Reference_wrapper_base<_Res(_T1) const volatile> : unary_function<_T1, _Res> { }; // - a function type (binary) template struct _Reference_wrapper_base<_Res(_T1, _T2)> : binary_function<_T1, _T2, _Res> { }; template struct _Reference_wrapper_base<_Res(_T1, _T2) const> : binary_function<_T1, _T2, _Res> { }; template struct _Reference_wrapper_base<_Res(_T1, _T2) volatile> : binary_function<_T1, _T2, _Res> { }; template struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile> : binary_function<_T1, _T2, _Res> { }; // - a function pointer type (unary) template struct _Reference_wrapper_base<_Res(*)(_T1)> : unary_function<_T1, _Res> { }; // - a function pointer type (binary) template struct _Reference_wrapper_base<_Res(*)(_T1, _T2)> : binary_function<_T1, _T2, _Res> { }; // - a pointer to member function type (unary, no qualifiers) template struct _Reference_wrapper_base<_Res (_T1::*)()> : unary_function<_T1*, _Res> { }; // - a pointer to member function type (binary, no qualifiers) template struct _Reference_wrapper_base<_Res (_T1::*)(_T2)> : binary_function<_T1*, _T2, _Res> { }; // - a pointer to member function type (unary, const) template struct _Reference_wrapper_base<_Res (_T1::*)() const> : unary_function { }; // - a pointer to member function type (binary, const) template struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const> : binary_function { }; // - a pointer to member function type (unary, volatile) template struct _Reference_wrapper_base<_Res (_T1::*)() volatile> : unary_function { }; // - a pointer to member function type (binary, volatile) template struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile> : binary_function { }; // - a pointer to member function type (unary, const volatile) template struct _Reference_wrapper_base<_Res (_T1::*)() const volatile> : unary_function { }; // - a pointer to member function type (binary, const volatile) template struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile> : binary_function { }; /** * @brief Primary class template for reference_wrapper. * @ingroup functors * @{ */ template class reference_wrapper : public _Reference_wrapper_base::type> { _Tp* _M_data; public: typedef _Tp type; reference_wrapper(_Tp& __indata) noexcept : _M_data(std::__addressof(__indata)) { } reference_wrapper(_Tp&&) = delete; reference_wrapper(const reference_wrapper&) = default; reference_wrapper& operator=(const reference_wrapper&) = default; operator _Tp&() const noexcept { return this->get(); } _Tp& get() const noexcept { return *_M_data; } template typename result_of<_Tp&(_Args&&...)>::type operator()(_Args&&... __args) const { return __invoke(get(), std::forward<_Args>(__args)...); } }; /// Denotes a reference should be taken to a variable. template inline reference_wrapper<_Tp> ref(_Tp& __t) noexcept { return reference_wrapper<_Tp>(__t); } /// Denotes a const reference should be taken to a variable. template inline reference_wrapper cref(const _Tp& __t) noexcept { return reference_wrapper(__t); } template void ref(const _Tp&&) = delete; template void cref(const _Tp&&) = delete; /// Partial specialization. template inline reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t) noexcept { return ref(__t.get()); } /// Partial specialization. template inline reference_wrapper cref(reference_wrapper<_Tp> __t) noexcept { return cref(__t.get()); } // @} group functors template struct _Pack : integral_constant { }; template struct _AllConvertible : false_type { }; template struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true> : __and_...> { }; template using _NotSame = __not_::type, typename std::decay<_Tp2>::type>>; /** * Derives from @c unary_function or @c binary_function, or perhaps * nothing, depending on the number of arguments provided. The * primary template is the basis case, which derives nothing. */ template struct _Maybe_unary_or_binary_function { }; /// Derives from @c unary_function, as appropriate. template struct _Maybe_unary_or_binary_function<_Res, _T1> : std::unary_function<_T1, _Res> { }; /// Derives from @c binary_function, as appropriate. template struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> : std::binary_function<_T1, _T2, _Res> { }; template struct _Mem_fn_traits; template struct _Mem_fn_traits_base { using __result_type = _Res; using __class_type = _Class; using __arg_types = _Pack<_ArgTypes...>; using __maybe_type = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>; using __arity = integral_constant; }; #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \ template \ struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \ : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ { \ using __pmf_type = _Res (_Class::*)(_ArgTypes...) _CV _REF; \ using __lvalue = _LVAL; \ using __rvalue = _RVAL; \ using __vararg = false_type; \ }; \ template \ struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \ : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ { \ using __pmf_type = _Res (_Class::*)(_ArgTypes... ...) _CV _REF; \ using __lvalue = _LVAL; \ using __rvalue = _RVAL; \ using __vararg = true_type; \ }; #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \ _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \ _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \ _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \ _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL) _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type) _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type) _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) #undef _GLIBCXX_MEM_FN_TRAITS #undef _GLIBCXX_MEM_FN_TRAITS2 template::value> class _Mem_fn_base : public _Mem_fn_traits<_MemFunPtr>::__maybe_type { using _Traits = _Mem_fn_traits<_MemFunPtr>; using _Class = typename _Traits::__class_type; using _ArgTypes = typename _Traits::__arg_types; using _Pmf = typename _Traits::__pmf_type; using _Arity = typename _Traits::__arity; using _Varargs = typename _Traits::__vararg; template friend struct _Bind_check_arity; // for varargs functions we just check the number of arguments, // otherwise we also check they are convertible. template using _CheckArgs = typename conditional<_Varargs::value, __bool_constant<(_Args::value >= _ArgTypes::value)>, _AllConvertible<_Args, _ArgTypes> >::type; public: using result_type = typename _Traits::__result_type; explicit _Mem_fn_base(_Pmf __pmf) : _M_pmf(__pmf) { } // Handle objects template>>> result_type operator()(_Class& __object, _Args&&... __args) const { return (__object.*_M_pmf)(std::forward<_Args>(__args)...); } template>>> result_type operator()(_Class&& __object, _Args&&... __args) const { return (std::move(__object).*_M_pmf)(std::forward<_Args>(__args)...); } // Handle pointers template>>> result_type operator()(_Class* __object, _Args&&... __args) const { return (__object->*_M_pmf)(std::forward<_Args>(__args)...); } // Handle smart pointers, references and pointers to derived template, _NotSame<_Class*, _Tp>, _CheckArgs<_Pack<_Args...>>>> result_type operator()(_Tp&& __object, _Args&&... __args) const { return _M_call(std::forward<_Tp>(__object), &__object, std::forward<_Args>(__args)...); } // Handle reference wrappers template, typename _Traits::__lvalue, _CheckArgs<_Pack<_Args...>>>> result_type operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const { return operator()(__ref.get(), std::forward<_Args>(__args)...); } private: template result_type _M_call(_Tp&& __object, const volatile _Class *, _Args&&... __args) const { return (std::forward<_Tp>(__object).*_M_pmf) (std::forward<_Args>(__args)...); } template result_type _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const { return ((*__ptr).*_M_pmf)(std::forward<_Args>(__args)...); } _Pmf _M_pmf; }; // Partial specialization for member object pointers. template class _Mem_fn_base<_Res _Class::*, false> { using __pm_type = _Res _Class::*; // This bit of genius is due to Peter Dimov, improved slightly by // Douglas Gregor. // Made less elegant to support perfect forwarding and noexcept. template auto _M_call(_Tp&& __object, const _Class *) const noexcept -> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>()) { return std::forward<_Tp>(__object).*_M_pm; } template auto _M_call(_Tp&& __object, _Up * const *) const noexcept -> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>()) { return (*std::forward<_Tp>(__object)).*_M_pm; } template auto _M_call(_Tp&& __ptr, const volatile void*) const noexcept(noexcept((*__ptr).*std::declval<__pm_type&>())) -> decltype((*__ptr).*std::declval<__pm_type&>()) { return (*__ptr).*_M_pm; } using _Arity = integral_constant; using _Varargs = false_type; template friend struct _Bind_check_arity; public: explicit _Mem_fn_base(_Res _Class::*__pm) noexcept : _M_pm(__pm) { } // Handle objects _Res& operator()(_Class& __object) const noexcept { return __object.*_M_pm; } const _Res& operator()(const _Class& __object) const noexcept { return __object.*_M_pm; } _Res&& operator()(_Class&& __object) const noexcept { return std::forward<_Class>(__object).*_M_pm; } const _Res&& operator()(const _Class&& __object) const noexcept { return std::forward(__object).*_M_pm; } // Handle pointers _Res& operator()(_Class* __object) const noexcept { return __object->*_M_pm; } const _Res& operator()(const _Class* __object) const noexcept { return __object->*_M_pm; } // Handle smart pointers and derived template>> auto operator()(_Tp&& __unknown) const noexcept(noexcept(std::declval<_Mem_fn_base*>()->_M_call (std::forward<_Tp>(__unknown), &__unknown))) -> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown)) { return _M_call(std::forward<_Tp>(__unknown), &__unknown); } template>> auto operator()(reference_wrapper<_Tp> __ref) const noexcept(noexcept(std::declval<_Mem_fn_base&>()(__ref.get()))) -> decltype((*this)(__ref.get())) { return (*this)(__ref.get()); } private: _Res _Class::*_M_pm; }; template struct _Mem_fn<_Res _Class::*> : _Mem_fn_base<_Res _Class::*> { using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base; }; // _GLIBCXX_RESOLVE_LIB_DEFECTS // 2048. Unnecessary mem_fn overloads /** * @brief Returns a function object that forwards to the member * pointer @a pm. * @ingroup functors */ template inline _Mem_fn<_Tp _Class::*> mem_fn(_Tp _Class::* __pm) noexcept { return _Mem_fn<_Tp _Class::*>(__pm); } /** * @brief Determines if the given type _Tp is a function object * should be treated as a subexpression when evaluating calls to * function objects returned by bind(). [TR1 3.6.1] * @ingroup binders */ template struct is_bind_expression : public false_type { }; /** * @brief Determines if the given type _Tp is a placeholder in a * bind() expression and, if so, which placeholder it is. [TR1 3.6.2] * @ingroup binders */ template struct is_placeholder : public integral_constant { }; /** @brief The type of placeholder objects defined by libstdc++. * @ingroup binders */ template struct _Placeholder { }; _GLIBCXX_END_NAMESPACE_VERSION /** @namespace std::placeholders * @brief ISO C++11 entities sub-namespace for functional. * @ingroup binders */ namespace placeholders { _GLIBCXX_BEGIN_NAMESPACE_VERSION /* Define a large number of placeholders. There is no way to * simplify this with variadic templates, because we're introducing * unique names for each. */ extern const _Placeholder<1> _1; extern const _Placeholder<2> _2; extern const _Placeholder<3> _3; extern const _Placeholder<4> _4; extern const _Placeholder<5> _5; extern const _Placeholder<6> _6; extern const _Placeholder<7> _7; extern const _Placeholder<8> _8; extern const _Placeholder<9> _9; extern const _Placeholder<10> _10; extern const _Placeholder<11> _11; extern const _Placeholder<12> _12; extern const _Placeholder<13> _13; extern const _Placeholder<14> _14; extern const _Placeholder<15> _15; extern const _Placeholder<16> _16; extern const _Placeholder<17> _17; extern const _Placeholder<18> _18; extern const _Placeholder<19> _19; extern const _Placeholder<20> _20; extern const _Placeholder<21> _21; extern const _Placeholder<22> _22; extern const _Placeholder<23> _23; extern const _Placeholder<24> _24; extern const _Placeholder<25> _25; extern const _Placeholder<26> _26; extern const _Placeholder<27> _27; extern const _Placeholder<28> _28; extern const _Placeholder<29> _29; _GLIBCXX_END_NAMESPACE_VERSION } _GLIBCXX_BEGIN_NAMESPACE_VERSION /** * Partial specialization of is_placeholder that provides the placeholder * number for the placeholder objects defined by libstdc++. * @ingroup binders */ template struct is_placeholder<_Placeholder<_Num> > : public integral_constant { }; template struct is_placeholder > : public integral_constant { }; /** * Used by _Safe_tuple_element to indicate that there is no tuple * element at this position. */ struct _No_tuple_element; /** * Implementation helper for _Safe_tuple_element. This primary * template handles the case where it is safe to use @c * tuple_element. */ template struct _Safe_tuple_element_impl : tuple_element<__i, _Tuple> { }; /** * Implementation helper for _Safe_tuple_element. This partial * specialization handles the case where it is not safe to use @c * tuple_element. We just return @c _No_tuple_element. */ template struct _Safe_tuple_element_impl<__i, _Tuple, false> { typedef _No_tuple_element type; }; /** * Like tuple_element, but returns @c _No_tuple_element when * tuple_element would return an error. */ template struct _Safe_tuple_element : _Safe_tuple_element_impl<__i, _Tuple, (__i < tuple_size<_Tuple>::value)> { }; /** * Maps an argument to bind() into an actual argument to the bound * function object [TR1 3.6.3/5]. Only the first parameter should * be specified: the rest are used to determine among the various * implementations. Note that, although this class is a function * object, it isn't entirely normal because it takes only two * parameters regardless of the number of parameters passed to the * bind expression. The first parameter is the bound argument and * the second parameter is a tuple containing references to the * rest of the arguments. */ template::value, bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)> class _Mu; /** * If the argument is reference_wrapper<_Tp>, returns the * underlying reference. [TR1 3.6.3/5 bullet 1] */ template class _Mu, false, false> { public: typedef _Tp& result_type; /* Note: This won't actually work for const volatile * reference_wrappers, because reference_wrapper::get() is const * but not volatile-qualified. This might be a defect in the TR. */ template result_type operator()(_CVRef& __arg, _Tuple&) const volatile { return __arg.get(); } }; /** * If the argument is a bind expression, we invoke the underlying * function object with the same cv-qualifiers as we are given and * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2] */ template class _Mu<_Arg, true, false> { public: template auto operator()(_CVArg& __arg, tuple<_Args...>& __tuple) const volatile -> decltype(__arg(declval<_Args>()...)) { // Construct an index tuple and forward to __call typedef typename _Build_index_tuple::__type _Indexes; return this->__call(__arg, __tuple, _Indexes()); } private: // Invokes the underlying function object __arg by unpacking all // of the arguments in the tuple. template auto __call(_CVArg& __arg, tuple<_Args...>& __tuple, const _Index_tuple<_Indexes...>&) const volatile -> decltype(__arg(declval<_Args>()...)) { return __arg(std::forward<_Args>(std::get<_Indexes>(__tuple))...); } }; /** * If the argument is a placeholder for the Nth argument, returns * a reference to the Nth argument to the bind function object. * [TR1 3.6.3/5 bullet 3] */ template class _Mu<_Arg, false, true> { public: template class result; template class result<_CVMu(_CVArg, _Tuple)> { // Add a reference, if it hasn't already been done for us. // This allows us to be a little bit sloppy in constructing // the tuple that we pass to result_of<...>. typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value - 1), _Tuple>::type __base_type; public: typedef typename add_rvalue_reference<__base_type>::type type; }; template typename result<_Mu(_Arg, _Tuple)>::type operator()(const volatile _Arg&, _Tuple& __tuple) const volatile { return std::forward::type>( ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple)); } }; /** * If the argument is just a value, returns a reference to that * value. The cv-qualifiers on the reference are the same as the * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4] */ template class _Mu<_Arg, false, false> { public: template struct result; template struct result<_CVMu(_CVArg, _Tuple)> { typedef typename add_lvalue_reference<_CVArg>::type type; }; // Pick up the cv-qualifiers of the argument template _CVArg&& operator()(_CVArg&& __arg, _Tuple&) const volatile { return std::forward<_CVArg>(__arg); } }; /** * Maps member pointers into instances of _Mem_fn but leaves all * other function objects untouched. Used by std::bind(). The * primary template handles the non-member-pointer case. */ template struct _Maybe_wrap_member_pointer { typedef _Tp type; static const _Tp& __do_wrap(const _Tp& __x) { return __x; } static _Tp&& __do_wrap(_Tp&& __x) { return static_cast<_Tp&&>(__x); } }; /** * Maps member pointers into instances of _Mem_fn but leaves all * other function objects untouched. Used by std::bind(). This * partial specialization handles the member pointer case. */ template struct _Maybe_wrap_member_pointer<_Tp _Class::*> { typedef _Mem_fn<_Tp _Class::*> type; static type __do_wrap(_Tp _Class::* __pm) { return type(__pm); } }; // Specialization needed to prevent "forming reference to void" errors when // bind() is called, because argument deduction instantiates // _Maybe_wrap_member_pointer outside the immediate context where // SFINAE applies. template<> struct _Maybe_wrap_member_pointer { typedef void type; }; // std::get for volatile-qualified tuples template inline auto __volget(volatile tuple<_Tp...>& __tuple) -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile& { return std::get<_Ind>(const_cast&>(__tuple)); } // std::get for const-volatile-qualified tuples template inline auto __volget(const volatile tuple<_Tp...>& __tuple) -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile& { return std::get<_Ind>(const_cast&>(__tuple)); } /// Type of the function object returned from bind(). template struct _Bind; template class _Bind<_Functor(_Bound_args...)> : public _Weak_result_type<_Functor> { typedef _Bind __self_type; typedef typename _Build_index_tuple::__type _Bound_indexes; _Functor _M_f; tuple<_Bound_args...> _M_bound_args; // Call unqualified template _Result __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) { return _M_f(_Mu<_Bound_args>() (std::get<_Indexes>(_M_bound_args), __args)...); } // Call as const template _Result __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const { return _M_f(_Mu<_Bound_args>() (std::get<_Indexes>(_M_bound_args), __args)...); } // Call as volatile template _Result __call_v(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile { return _M_f(_Mu<_Bound_args>() (__volget<_Indexes>(_M_bound_args), __args)...); } // Call as const volatile template _Result __call_c_v(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const volatile { return _M_f(_Mu<_Bound_args>() (__volget<_Indexes>(_M_bound_args), __args)...); } public: template explicit _Bind(const _Functor& __f, _Args&&... __args) : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...) { } template explicit _Bind(_Functor&& __f, _Args&&... __args) : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...) { } _Bind(const _Bind&) = default; _Bind(_Bind&& __b) : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args)) { } // Call unqualified template()( _Mu<_Bound_args>()( std::declval<_Bound_args&>(), std::declval&>() )... ) )> _Result operator()(_Args&&... __args) { return this->__call<_Result>( std::forward_as_tuple(std::forward<_Args>(__args)...), _Bound_indexes()); } // Call as const template= 0), typename add_const<_Functor>::type&>::type>()( _Mu<_Bound_args>()( std::declval(), std::declval&>() )... ) )> _Result operator()(_Args&&... __args) const { return this->__call_c<_Result>( std::forward_as_tuple(std::forward<_Args>(__args)...), _Bound_indexes()); } // Call as volatile template= 0), typename add_volatile<_Functor>::type&>::type>()( _Mu<_Bound_args>()( std::declval(), std::declval&>() )... ) )> _Result operator()(_Args&&... __args) volatile { return this->__call_v<_Result>( std::forward_as_tuple(std::forward<_Args>(__args)...), _Bound_indexes()); } // Call as const volatile template= 0), typename add_cv<_Functor>::type&>::type>()( _Mu<_Bound_args>()( std::declval(), std::declval&>() )... ) )> _Result operator()(_Args&&... __args) const volatile { return this->__call_c_v<_Result>( std::forward_as_tuple(std::forward<_Args>(__args)...), _Bound_indexes()); } }; /// Type of the function object returned from bind(). template struct _Bind_result; template class _Bind_result<_Result, _Functor(_Bound_args...)> { typedef _Bind_result __self_type; typedef typename _Build_index_tuple::__type _Bound_indexes; _Functor _M_f; tuple<_Bound_args...> _M_bound_args; // sfinae types template struct __enable_if_void : enable_if::value, int> { }; template struct __disable_if_void : enable_if::value, int> { }; // Call unqualified template _Result __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, typename __disable_if_void<_Res>::type = 0) { return _M_f(_Mu<_Bound_args>() (std::get<_Indexes>(_M_bound_args), __args)...); } // Call unqualified, return void template void __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, typename __enable_if_void<_Res>::type = 0) { _M_f(_Mu<_Bound_args>() (std::get<_Indexes>(_M_bound_args), __args)...); } // Call as const template _Result __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, typename __disable_if_void<_Res>::type = 0) const { return _M_f(_Mu<_Bound_args>() (std::get<_Indexes>(_M_bound_args), __args)...); } // Call as const, return void template void __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, typename __enable_if_void<_Res>::type = 0) const { _M_f(_Mu<_Bound_args>() (std::get<_Indexes>(_M_bound_args), __args)...); } // Call as volatile template _Result __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, typename __disable_if_void<_Res>::type = 0) volatile { return _M_f(_Mu<_Bound_args>() (__volget<_Indexes>(_M_bound_args), __args)...); } // Call as volatile, return void template void __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, typename __enable_if_void<_Res>::type = 0) volatile { _M_f(_Mu<_Bound_args>() (__volget<_Indexes>(_M_bound_args), __args)...); } // Call as const volatile template _Result __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, typename __disable_if_void<_Res>::type = 0) const volatile { return _M_f(_Mu<_Bound_args>() (__volget<_Indexes>(_M_bound_args), __args)...); } // Call as const volatile, return void template void __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, typename __enable_if_void<_Res>::type = 0) const volatile { _M_f(_Mu<_Bound_args>() (__volget<_Indexes>(_M_bound_args), __args)...); } public: typedef _Result result_type; template explicit _Bind_result(const _Functor& __f, _Args&&... __args) : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...) { } template explicit _Bind_result(_Functor&& __f, _Args&&... __args) : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...) { } _Bind_result(const _Bind_result&) = default; _Bind_result(_Bind_result&& __b) : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args)) { } // Call unqualified template result_type operator()(_Args&&... __args) { return this->__call<_Result>( std::forward_as_tuple(std::forward<_Args>(__args)...), _Bound_indexes()); } // Call as const template result_type operator()(_Args&&... __args) const { return this->__call<_Result>( std::forward_as_tuple(std::forward<_Args>(__args)...), _Bound_indexes()); } // Call as volatile template result_type operator()(_Args&&... __args) volatile { return this->__call<_Result>( std::forward_as_tuple(std::forward<_Args>(__args)...), _Bound_indexes()); } // Call as const volatile template result_type operator()(_Args&&... __args) const volatile { return this->__call<_Result>( std::forward_as_tuple(std::forward<_Args>(__args)...), _Bound_indexes()); } }; /** * @brief Class template _Bind is always a bind expression. * @ingroup binders */ template struct is_bind_expression<_Bind<_Signature> > : public true_type { }; /** * @brief Class template _Bind is always a bind expression. * @ingroup binders */ template struct is_bind_expression > : public true_type { }; /** * @brief Class template _Bind is always a bind expression. * @ingroup binders */ template struct is_bind_expression > : public true_type { }; /** * @brief Class template _Bind is always a bind expression. * @ingroup binders */ template struct is_bind_expression> : public true_type { }; /** * @brief Class template _Bind_result is always a bind expression. * @ingroup binders */ template struct is_bind_expression<_Bind_result<_Result, _Signature>> : public true_type { }; /** * @brief Class template _Bind_result is always a bind expression. * @ingroup binders */ template struct is_bind_expression> : public true_type { }; /** * @brief Class template _Bind_result is always a bind expression. * @ingroup binders */ template struct is_bind_expression> : public true_type { }; /** * @brief Class template _Bind_result is always a bind expression. * @ingroup binders */ template struct is_bind_expression> : public true_type { }; template struct _Bind_check_arity { }; template struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...> { static_assert(sizeof...(_BoundArgs) == sizeof...(_Args), "Wrong number of arguments for function"); }; template struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...> { static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args), "Wrong number of arguments for function"); }; template struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...> { using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity; using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs; static_assert(_Varargs::value ? sizeof...(_BoundArgs) >= _Arity::value + 1 : sizeof...(_BoundArgs) == _Arity::value + 1, "Wrong number of arguments for pointer-to-member"); }; // Trait type used to remove std::bind() from overload set via SFINAE // when first argument has integer type, so that std::bind() will // not be a better match than ::bind() from the BSD Sockets API. template::type> using __is_socketlike = __or_, is_enum<_Tp2>>; template struct _Bind_helper : _Bind_check_arity::type, _BoundArgs...> { typedef _Maybe_wrap_member_pointer::type> __maybe_type; typedef typename __maybe_type::type __func_type; typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type; }; // Partial specialization for is_socketlike == true, does not define // nested type so std::bind() will not participate in overload resolution // when the first argument might be a socket file descriptor. template struct _Bind_helper { }; /** * @brief Function template for std::bind. * @ingroup binders */ template inline typename _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type bind(_Func&& __f, _BoundArgs&&... __args) { typedef _Bind_helper __helper_type; typedef typename __helper_type::__maybe_type __maybe_type; typedef typename __helper_type::type __result_type; return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)), std::forward<_BoundArgs>(__args)...); } template struct _Bindres_helper : _Bind_check_arity::type, _BoundArgs...> { typedef _Maybe_wrap_member_pointer::type> __maybe_type; typedef typename __maybe_type::type __functor_type; typedef _Bind_result<_Result, __functor_type(typename decay<_BoundArgs>::type...)> type; }; /** * @brief Function template for std::bind. * @ingroup binders */ template inline typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type bind(_Func&& __f, _BoundArgs&&... __args) { typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type; typedef typename __helper_type::__maybe_type __maybe_type; typedef typename __helper_type::type __result_type; return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)), std::forward<_BoundArgs>(__args)...); } template struct _Bind_simple; template struct _Bind_simple<_Callable(_Args...)> { typedef typename result_of<_Callable(_Args...)>::type result_type; template explicit _Bind_simple(_Tp&& __f, _Up&&... __args) : _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...) { } _Bind_simple(const _Bind_simple&) = default; _Bind_simple(_Bind_simple&&) = default; result_type operator()() { typedef typename _Build_index_tuple::__type _Indices; return _M_invoke(_Indices()); } private: template typename result_of<_Callable(_Args...)>::type _M_invoke(_Index_tuple<_Indices...>) { // std::bind always forwards bound arguments as lvalues, // but this type can call functions which only accept rvalues. return std::forward<_Callable>(std::get<0>(_M_bound))( std::forward<_Args>(std::get<_Indices+1>(_M_bound))...); } std::tuple<_Callable, _Args...> _M_bound; }; template struct _Bind_simple_helper : _Bind_check_arity::type, _BoundArgs...> { typedef _Maybe_wrap_member_pointer::type> __maybe_type; typedef typename __maybe_type::type __func_type; typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)> __type; }; // Simplified version of std::bind for internal use, without support for // unbound arguments, placeholders or nested bind expressions. template typename _Bind_simple_helper<_Callable, _Args...>::__type __bind_simple(_Callable&& __callable, _Args&&... __args) { typedef _Bind_simple_helper<_Callable, _Args...> __helper_type; typedef typename __helper_type::__maybe_type __maybe_type; typedef typename __helper_type::__type __result_type; return __result_type( __maybe_type::__do_wrap( std::forward<_Callable>(__callable)), std::forward<_Args>(__args)...); } /** * @brief Exception class thrown when class template function's * operator() is called with an empty target. * @ingroup exceptions */ class bad_function_call : public std::exception { public: virtual ~bad_function_call() noexcept; const char* what() const noexcept; }; /** * Trait identifying "location-invariant" types, meaning that the * address of the object (or any of its members) will not escape. * Trivially copyable types are location-invariant and users can * specialize this trait for other types. */ template struct __is_location_invariant : is_trivially_copyable<_Tp>::type { }; class _Undefined_class; union _Nocopy_types { void* _M_object; const void* _M_const_object; void (*_M_function_pointer)(); void (_Undefined_class::*_M_member_pointer)(); }; union _Any_data { void* _M_access() { return &_M_pod_data[0]; } const void* _M_access() const { return &_M_pod_data[0]; } template _Tp& _M_access() { return *static_cast<_Tp*>(_M_access()); } template const _Tp& _M_access() const { return *static_cast(_M_access()); } _Nocopy_types _M_unused; char _M_pod_data[sizeof(_Nocopy_types)]; }; enum _Manager_operation { __get_type_info, __get_functor_ptr, __clone_functor, __destroy_functor }; // Simple type wrapper that helps avoid annoying const problems // when casting between void pointers and pointers-to-pointers. template struct _Simple_type_wrapper { _Simple_type_wrapper(_Tp __value) : __value(__value) { } _Tp __value; }; template struct __is_location_invariant<_Simple_type_wrapper<_Tp> > : __is_location_invariant<_Tp> { }; // Converts a reference to a function object into a callable // function object. template inline _Functor& __callable_functor(_Functor& __f) { return __f; } template inline _Mem_fn<_Member _Class::*> __callable_functor(_Member _Class::* &__p) { return std::mem_fn(__p); } template inline _Mem_fn<_Member _Class::*> __callable_functor(_Member _Class::* const &__p) { return std::mem_fn(__p); } template inline _Mem_fn<_Member _Class::*> __callable_functor(_Member _Class::* volatile &__p) { return std::mem_fn(__p); } template inline _Mem_fn<_Member _Class::*> __callable_functor(_Member _Class::* const volatile &__p) { return std::mem_fn(__p); } template class function; /// Base class of all polymorphic function object wrappers. class _Function_base { public: static const std::size_t _M_max_size = sizeof(_Nocopy_types); static const std::size_t _M_max_align = __alignof__(_Nocopy_types); template class _Base_manager { protected: static const bool __stored_locally = (__is_location_invariant<_Functor>::value && sizeof(_Functor) <= _M_max_size && __alignof__(_Functor) <= _M_max_align && (_M_max_align % __alignof__(_Functor) == 0)); typedef integral_constant _Local_storage; // Retrieve a pointer to the function object static _Functor* _M_get_pointer(const _Any_data& __source) { const _Functor* __ptr = __stored_locally? std::__addressof(__source._M_access<_Functor>()) /* have stored a pointer */ : __source._M_access<_Functor*>(); return const_cast<_Functor*>(__ptr); } // Clone a location-invariant function object that fits within // an _Any_data structure. static void _M_clone(_Any_data& __dest, const _Any_data& __source, true_type) { new (__dest._M_access()) _Functor(__source._M_access<_Functor>()); } // Clone a function object that is not location-invariant or // that cannot fit into an _Any_data structure. static void _M_clone(_Any_data& __dest, const _Any_data& __source, false_type) { __dest._M_access<_Functor*>() = new _Functor(*__source._M_access<_Functor*>()); } // Destroying a location-invariant object may still require // destruction. static void _M_destroy(_Any_data& __victim, true_type) { __victim._M_access<_Functor>().~_Functor(); } // Destroying an object located on the heap. static void _M_destroy(_Any_data& __victim, false_type) { delete __victim._M_access<_Functor*>(); } public: static bool _M_manager(_Any_data& __dest, const _Any_data& __source, _Manager_operation __op) { switch (__op) { #if __cpp_rtti case __get_type_info: __dest._M_access() = &typeid(_Functor); break; #endif case __get_functor_ptr: __dest._M_access<_Functor*>() = _M_get_pointer(__source); break; case __clone_functor: _M_clone(__dest, __source, _Local_storage()); break; case __destroy_functor: _M_destroy(__dest, _Local_storage()); break; } return false; } static void _M_init_functor(_Any_data& __functor, _Functor&& __f) { _M_init_functor(__functor, std::move(__f), _Local_storage()); } template static bool _M_not_empty_function(const function<_Signature>& __f) { return static_cast(__f); } template static bool _M_not_empty_function(_Tp* const& __fp) { return __fp; } template static bool _M_not_empty_function(_Tp _Class::* const& __mp) { return __mp; } template static bool _M_not_empty_function(const _Tp&) { return true; } private: static void _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type) { new (__functor._M_access()) _Functor(std::move(__f)); } static void _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type) { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); } }; template class _Ref_manager : public _Base_manager<_Functor*> { typedef _Function_base::_Base_manager<_Functor*> _Base; public: static bool _M_manager(_Any_data& __dest, const _Any_data& __source, _Manager_operation __op) { switch (__op) { #if __cpp_rtti case __get_type_info: __dest._M_access() = &typeid(_Functor); break; #endif case __get_functor_ptr: __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source); return is_const<_Functor>::value; break; default: _Base::_M_manager(__dest, __source, __op); } return false; } static void _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f) { _Base::_M_init_functor(__functor, std::__addressof(__f.get())); } }; _Function_base() : _M_manager(nullptr) { } ~_Function_base() { if (_M_manager) _M_manager(_M_functor, _M_functor, __destroy_functor); } bool _M_empty() const { return !_M_manager; } typedef bool (*_Manager_type)(_Any_data&, const _Any_data&, _Manager_operation); _Any_data _M_functor; _Manager_type _M_manager; }; template class _Function_handler; template class _Function_handler<_Res(_ArgTypes...), _Functor> : public _Function_base::_Base_manager<_Functor> { typedef _Function_base::_Base_manager<_Functor> _Base; public: static _Res _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { return (*_Base::_M_get_pointer(__functor))( std::forward<_ArgTypes>(__args)...); } }; template class _Function_handler : public _Function_base::_Base_manager<_Functor> { typedef _Function_base::_Base_manager<_Functor> _Base; public: static void _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { (*_Base::_M_get_pointer(__functor))( std::forward<_ArgTypes>(__args)...); } }; template class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> > : public _Function_base::_Ref_manager<_Functor> { typedef _Function_base::_Ref_manager<_Functor> _Base; public: static _Res _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { return std::__callable_functor(**_Base::_M_get_pointer(__functor))( std::forward<_ArgTypes>(__args)...); } }; template class _Function_handler > : public _Function_base::_Ref_manager<_Functor> { typedef _Function_base::_Ref_manager<_Functor> _Base; public: static void _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { std::__callable_functor(**_Base::_M_get_pointer(__functor))( std::forward<_ArgTypes>(__args)...); } }; template class _Function_handler<_Res(_ArgTypes...), _Member _Class::*> : public _Function_handler { typedef _Function_handler _Base; public: static _Res _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)( std::forward<_ArgTypes>(__args)...); } }; template class _Function_handler : public _Function_base::_Base_manager< _Simple_type_wrapper< _Member _Class::* > > { typedef _Member _Class::* _Functor; typedef _Simple_type_wrapper<_Functor> _Wrapper; typedef _Function_base::_Base_manager<_Wrapper> _Base; public: static bool _M_manager(_Any_data& __dest, const _Any_data& __source, _Manager_operation __op) { switch (__op) { #if __cpp_rtti case __get_type_info: __dest._M_access() = &typeid(_Functor); break; #endif case __get_functor_ptr: __dest._M_access<_Functor*>() = &_Base::_M_get_pointer(__source)->__value; break; default: _Base::_M_manager(__dest, __source, __op); } return false; } static void _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { std::mem_fn(_Base::_M_get_pointer(__functor)->__value)( std::forward<_ArgTypes>(__args)...); } }; template using __check_func_return_type = __or_, is_convertible<_From, _To>>; /** * @brief Primary class template for std::function. * @ingroup functors * * Polymorphic function wrapper. */ template class function<_Res(_ArgTypes...)> : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>, private _Function_base { typedef _Res _Signature_type(_ArgTypes...); template::type> struct _Callable : __check_func_return_type<_Res2, _Res> { }; // Used so the return type convertibility checks aren't done when // performing overload resolution for copy construction/assignment. template struct _Callable : false_type { }; template using _Requires = typename enable_if<_Cond::value, _Tp>::type; public: typedef _Res result_type; // [3.7.2.1] construct/copy/destroy /** * @brief Default construct creates an empty function call wrapper. * @post @c !(bool)*this */ function() noexcept : _Function_base() { } /** * @brief Creates an empty function call wrapper. * @post @c !(bool)*this */ function(nullptr_t) noexcept : _Function_base() { } /** * @brief %Function copy constructor. * @param __x A %function object with identical call signature. * @post @c bool(*this) == bool(__x) * * The newly-created %function contains a copy of the target of @a * __x (if it has one). */ function(const function& __x); /** * @brief %Function move constructor. * @param __x A %function object rvalue with identical call signature. * * The newly-created %function contains the target of @a __x * (if it has one). */ function(function&& __x) : _Function_base() { __x.swap(*this); } // TODO: needs allocator_arg_t /** * @brief Builds a %function that targets a copy of the incoming * function object. * @param __f A %function object that is callable with parameters of * type @c T1, @c T2, ..., @c TN and returns a value convertible * to @c Res. * * The newly-created %function object will target a copy of * @a __f. If @a __f is @c reference_wrapper, then this function * object will contain a reference to the function object @c * __f.get(). If @a __f is a NULL function pointer or NULL * pointer-to-member, the newly-created object will be empty. * * If @a __f is a non-NULL function pointer or an object of type @c * reference_wrapper, this function will not throw. */ template>, void>, typename = _Requires<_Callable<_Functor>, void>> function(_Functor); /** * @brief %Function assignment operator. * @param __x A %function with identical call signature. * @post @c (bool)*this == (bool)x * @returns @c *this * * The target of @a __x is copied to @c *this. If @a __x has no * target, then @c *this will be empty. * * If @a __x targets a function pointer or a reference to a function * object, then this operation will not throw an %exception. */ function& operator=(const function& __x) { function(__x).swap(*this); return *this; } /** * @brief %Function move-assignment operator. * @param __x A %function rvalue with identical call signature. * @returns @c *this * * The target of @a __x is moved to @c *this. If @a __x has no * target, then @c *this will be empty. * * If @a __x targets a function pointer or a reference to a function * object, then this operation will not throw an %exception. */ function& operator=(function&& __x) { function(std::move(__x)).swap(*this); return *this; } /** * @brief %Function assignment to zero. * @post @c !(bool)*this * @returns @c *this * * The target of @c *this is deallocated, leaving it empty. */ function& operator=(nullptr_t) noexcept { if (_M_manager) { _M_manager(_M_functor, _M_functor, __destroy_functor); _M_manager = nullptr; _M_invoker = nullptr; } return *this; } /** * @brief %Function assignment to a new target. * @param __f A %function object that is callable with parameters of * type @c T1, @c T2, ..., @c TN and returns a value convertible * to @c Res. * @return @c *this * * This %function object wrapper will target a copy of @a * __f. If @a __f is @c reference_wrapper, then this function * object will contain a reference to the function object @c * __f.get(). If @a __f is a NULL function pointer or NULL * pointer-to-member, @c this object will be empty. * * If @a __f is a non-NULL function pointer or an object of type @c * reference_wrapper, this function will not throw. */ template _Requires<_Callable::type>, function&> operator=(_Functor&& __f) { function(std::forward<_Functor>(__f)).swap(*this); return *this; } /// @overload template function& operator=(reference_wrapper<_Functor> __f) noexcept { function(__f).swap(*this); return *this; } // [3.7.2.2] function modifiers /** * @brief Swap the targets of two %function objects. * @param __x A %function with identical call signature. * * Swap the targets of @c this function object and @a __f. This * function will not throw an %exception. */ void swap(function& __x) { std::swap(_M_functor, __x._M_functor); std::swap(_M_manager, __x._M_manager); std::swap(_M_invoker, __x._M_invoker); } // TODO: needs allocator_arg_t /* template void assign(_Functor&& __f, const _Alloc& __a) { function(allocator_arg, __a, std::forward<_Functor>(__f)).swap(*this); } */ // [3.7.2.3] function capacity /** * @brief Determine if the %function wrapper has a target. * * @return @c true when this %function object contains a target, * or @c false when it is empty. * * This function will not throw an %exception. */ explicit operator bool() const noexcept { return !_M_empty(); } // [3.7.2.4] function invocation /** * @brief Invokes the function targeted by @c *this. * @returns the result of the target. * @throws bad_function_call when @c !(bool)*this * * The function call operator invokes the target function object * stored by @c this. */ _Res operator()(_ArgTypes... __args) const; #if __cpp_rtti // [3.7.2.5] function target access /** * @brief Determine the type of the target of this function object * wrapper. * * @returns the type identifier of the target function object, or * @c typeid(void) if @c !(bool)*this. * * This function will not throw an %exception. */ const type_info& target_type() const noexcept; /** * @brief Access the stored target function object. * * @return Returns a pointer to the stored target function object, * if @c typeid(Functor).equals(target_type()); otherwise, a NULL * pointer. * * This function will not throw an %exception. */ template _Functor* target() noexcept; /// @overload template const _Functor* target() const noexcept; #endif private: using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...); _Invoker_type _M_invoker; }; // Out-of-line member definitions. template function<_Res(_ArgTypes...)>:: function(const function& __x) : _Function_base() { if (static_cast(__x)) { __x._M_manager(_M_functor, __x._M_functor, __clone_functor); _M_invoker = __x._M_invoker; _M_manager = __x._M_manager; } } template template function<_Res(_ArgTypes...)>:: function(_Functor __f) : _Function_base() { typedef _Function_handler<_Signature_type, _Functor> _My_handler; if (_My_handler::_M_not_empty_function(__f)) { _My_handler::_M_init_functor(_M_functor, std::move(__f)); _M_invoker = &_My_handler::_M_invoke; _M_manager = &_My_handler::_M_manager; } } template _Res function<_Res(_ArgTypes...)>:: operator()(_ArgTypes... __args) const { if (_M_empty()) __throw_bad_function_call(); return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...); } #if __cpp_rtti template const type_info& function<_Res(_ArgTypes...)>:: target_type() const noexcept { if (_M_manager) { _Any_data __typeinfo_result; _M_manager(__typeinfo_result, _M_functor, __get_type_info); return *__typeinfo_result._M_access(); } else return typeid(void); } template template _Functor* function<_Res(_ArgTypes...)>:: target() noexcept { if (typeid(_Functor) == target_type() && _M_manager) { _Any_data __ptr; if (_M_manager(__ptr, _M_functor, __get_functor_ptr) && !is_const<_Functor>::value) return 0; else return __ptr._M_access<_Functor*>(); } else return 0; } template template const _Functor* function<_Res(_ArgTypes...)>:: target() const noexcept { if (typeid(_Functor) == target_type() && _M_manager) { _Any_data __ptr; _M_manager(__ptr, _M_functor, __get_functor_ptr); return __ptr._M_access(); } else return 0; } #endif // [20.7.15.2.6] null pointer comparisons /** * @brief Compares a polymorphic function object wrapper against 0 * (the NULL pointer). * @returns @c true if the wrapper has no target, @c false otherwise * * This function will not throw an %exception. */ template inline bool operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept { return !static_cast(__f); } /// @overload template inline bool operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept { return !static_cast(__f); } /** * @brief Compares a polymorphic function object wrapper against 0 * (the NULL pointer). * @returns @c false if the wrapper has no target, @c true otherwise * * This function will not throw an %exception. */ template inline bool operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept { return static_cast(__f); } /// @overload template inline bool operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept { return static_cast(__f); } // [20.7.15.2.7] specialized algorithms /** * @brief Swap the targets of two polymorphic function object wrappers. * * This function will not throw an %exception. */ template inline void swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) { __x.swap(__y); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std #endif // C++11 #endif // _GLIBCXX_FUNCTIONAL