libstdc++
|
00001 // <functional> -*- C++ -*- 00002 00003 // Copyright (C) 2001-2013 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * Copyright (c) 1997 00027 * Silicon Graphics Computer Systems, Inc. 00028 * 00029 * Permission to use, copy, modify, distribute and sell this software 00030 * and its documentation for any purpose is hereby granted without fee, 00031 * provided that the above copyright notice appear in all copies and 00032 * that both that copyright notice and this permission notice appear 00033 * in supporting documentation. Silicon Graphics makes no 00034 * representations about the suitability of this software for any 00035 * purpose. It is provided "as is" without express or implied warranty. 00036 * 00037 */ 00038 00039 /** @file include/functional 00040 * This is a Standard C++ Library header. 00041 */ 00042 00043 #ifndef _GLIBCXX_FUNCTIONAL 00044 #define _GLIBCXX_FUNCTIONAL 1 00045 00046 #pragma GCC system_header 00047 00048 #include <bits/c++config.h> 00049 #include <bits/stl_function.h> 00050 00051 #if __cplusplus >= 201103L 00052 00053 #include <typeinfo> 00054 #include <new> 00055 #include <tuple> 00056 #include <type_traits> 00057 #include <bits/functexcept.h> 00058 #include <bits/functional_hash.h> 00059 00060 namespace std _GLIBCXX_VISIBILITY(default) 00061 { 00062 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00063 00064 template<typename _MemberPointer> 00065 class _Mem_fn; 00066 template<typename _Tp, typename _Class> 00067 _Mem_fn<_Tp _Class::*> 00068 mem_fn(_Tp _Class::*) noexcept; 00069 00070 _GLIBCXX_HAS_NESTED_TYPE(result_type) 00071 00072 /// If we have found a result_type, extract it. 00073 template<bool _Has_result_type, typename _Functor> 00074 struct _Maybe_get_result_type 00075 { }; 00076 00077 template<typename _Functor> 00078 struct _Maybe_get_result_type<true, _Functor> 00079 { typedef typename _Functor::result_type result_type; }; 00080 00081 /** 00082 * Base class for any function object that has a weak result type, as 00083 * defined in 3.3/3 of TR1. 00084 */ 00085 template<typename _Functor> 00086 struct _Weak_result_type_impl 00087 : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor> 00088 { }; 00089 00090 /// Retrieve the result type for a function type. 00091 template<typename _Res, typename... _ArgTypes> 00092 struct _Weak_result_type_impl<_Res(_ArgTypes...)> 00093 { typedef _Res result_type; }; 00094 00095 template<typename _Res, typename... _ArgTypes> 00096 struct _Weak_result_type_impl<_Res(_ArgTypes......)> 00097 { typedef _Res result_type; }; 00098 00099 template<typename _Res, typename... _ArgTypes> 00100 struct _Weak_result_type_impl<_Res(_ArgTypes...) const> 00101 { typedef _Res result_type; }; 00102 00103 template<typename _Res, typename... _ArgTypes> 00104 struct _Weak_result_type_impl<_Res(_ArgTypes......) const> 00105 { typedef _Res result_type; }; 00106 00107 template<typename _Res, typename... _ArgTypes> 00108 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile> 00109 { typedef _Res result_type; }; 00110 00111 template<typename _Res, typename... _ArgTypes> 00112 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile> 00113 { typedef _Res result_type; }; 00114 00115 template<typename _Res, typename... _ArgTypes> 00116 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile> 00117 { typedef _Res result_type; }; 00118 00119 template<typename _Res, typename... _ArgTypes> 00120 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile> 00121 { typedef _Res result_type; }; 00122 00123 /// Retrieve the result type for a function reference. 00124 template<typename _Res, typename... _ArgTypes> 00125 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)> 00126 { typedef _Res result_type; }; 00127 00128 template<typename _Res, typename... _ArgTypes> 00129 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)> 00130 { typedef _Res result_type; }; 00131 00132 /// Retrieve the result type for a function pointer. 00133 template<typename _Res, typename... _ArgTypes> 00134 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)> 00135 { typedef _Res result_type; }; 00136 00137 template<typename _Res, typename... _ArgTypes> 00138 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)> 00139 { typedef _Res result_type; }; 00140 00141 /// Retrieve result type for a member function pointer. 00142 template<typename _Res, typename _Class, typename... _ArgTypes> 00143 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)> 00144 { typedef _Res result_type; }; 00145 00146 template<typename _Res, typename _Class, typename... _ArgTypes> 00147 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)> 00148 { typedef _Res result_type; }; 00149 00150 /// Retrieve result type for a const member function pointer. 00151 template<typename _Res, typename _Class, typename... _ArgTypes> 00152 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const> 00153 { typedef _Res result_type; }; 00154 00155 template<typename _Res, typename _Class, typename... _ArgTypes> 00156 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const> 00157 { typedef _Res result_type; }; 00158 00159 /// Retrieve result type for a volatile member function pointer. 00160 template<typename _Res, typename _Class, typename... _ArgTypes> 00161 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile> 00162 { typedef _Res result_type; }; 00163 00164 template<typename _Res, typename _Class, typename... _ArgTypes> 00165 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile> 00166 { typedef _Res result_type; }; 00167 00168 /// Retrieve result type for a const volatile member function pointer. 00169 template<typename _Res, typename _Class, typename... _ArgTypes> 00170 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) 00171 const volatile> 00172 { typedef _Res result_type; }; 00173 00174 template<typename _Res, typename _Class, typename... _ArgTypes> 00175 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) 00176 const volatile> 00177 { typedef _Res result_type; }; 00178 00179 /** 00180 * Strip top-level cv-qualifiers from the function object and let 00181 * _Weak_result_type_impl perform the real work. 00182 */ 00183 template<typename _Functor> 00184 struct _Weak_result_type 00185 : _Weak_result_type_impl<typename remove_cv<_Functor>::type> 00186 { }; 00187 00188 /// Determines if the type _Tp derives from unary_function. 00189 template<typename _Tp> 00190 struct _Derives_from_unary_function : __sfinae_types 00191 { 00192 private: 00193 template<typename _T1, typename _Res> 00194 static __one __test(const volatile unary_function<_T1, _Res>*); 00195 00196 // It's tempting to change "..." to const volatile void*, but 00197 // that fails when _Tp is a function type. 00198 static __two __test(...); 00199 00200 public: 00201 static const bool value = sizeof(__test((_Tp*)0)) == 1; 00202 }; 00203 00204 /// Determines if the type _Tp derives from binary_function. 00205 template<typename _Tp> 00206 struct _Derives_from_binary_function : __sfinae_types 00207 { 00208 private: 00209 template<typename _T1, typename _T2, typename _Res> 00210 static __one __test(const volatile binary_function<_T1, _T2, _Res>*); 00211 00212 // It's tempting to change "..." to const volatile void*, but 00213 // that fails when _Tp is a function type. 00214 static __two __test(...); 00215 00216 public: 00217 static const bool value = sizeof(__test((_Tp*)0)) == 1; 00218 }; 00219 00220 /** 00221 * Invoke a function object, which may be either a member pointer or a 00222 * function object. The first parameter will tell which. 00223 */ 00224 template<typename _Functor, typename... _Args> 00225 inline 00226 typename enable_if< 00227 (!is_member_pointer<_Functor>::value 00228 && !is_function<_Functor>::value 00229 && !is_function<typename remove_pointer<_Functor>::type>::value), 00230 typename result_of<_Functor&(_Args&&...)>::type 00231 >::type 00232 __invoke(_Functor& __f, _Args&&... __args) 00233 { 00234 return __f(std::forward<_Args>(__args)...); 00235 } 00236 00237 template<typename _Functor, typename... _Args> 00238 inline 00239 typename enable_if< 00240 (is_member_pointer<_Functor>::value 00241 && !is_function<_Functor>::value 00242 && !is_function<typename remove_pointer<_Functor>::type>::value), 00243 typename result_of<_Functor(_Args&&...)>::type 00244 >::type 00245 __invoke(_Functor& __f, _Args&&... __args) 00246 { 00247 return std::mem_fn(__f)(std::forward<_Args>(__args)...); 00248 } 00249 00250 // To pick up function references (that will become function pointers) 00251 template<typename _Functor, typename... _Args> 00252 inline 00253 typename enable_if< 00254 (is_pointer<_Functor>::value 00255 && is_function<typename remove_pointer<_Functor>::type>::value), 00256 typename result_of<_Functor(_Args&&...)>::type 00257 >::type 00258 __invoke(_Functor __f, _Args&&... __args) 00259 { 00260 return __f(std::forward<_Args>(__args)...); 00261 } 00262 00263 /** 00264 * Knowing which of unary_function and binary_function _Tp derives 00265 * from, derives from the same and ensures that reference_wrapper 00266 * will have a weak result type. See cases below. 00267 */ 00268 template<bool _Unary, bool _Binary, typename _Tp> 00269 struct _Reference_wrapper_base_impl; 00270 00271 // None of the nested argument types. 00272 template<typename _Tp> 00273 struct _Reference_wrapper_base_impl<false, false, _Tp> 00274 : _Weak_result_type<_Tp> 00275 { }; 00276 00277 // Nested argument_type only. 00278 template<typename _Tp> 00279 struct _Reference_wrapper_base_impl<true, false, _Tp> 00280 : _Weak_result_type<_Tp> 00281 { 00282 typedef typename _Tp::argument_type argument_type; 00283 }; 00284 00285 // Nested first_argument_type and second_argument_type only. 00286 template<typename _Tp> 00287 struct _Reference_wrapper_base_impl<false, true, _Tp> 00288 : _Weak_result_type<_Tp> 00289 { 00290 typedef typename _Tp::first_argument_type first_argument_type; 00291 typedef typename _Tp::second_argument_type second_argument_type; 00292 }; 00293 00294 // All the nested argument types. 00295 template<typename _Tp> 00296 struct _Reference_wrapper_base_impl<true, true, _Tp> 00297 : _Weak_result_type<_Tp> 00298 { 00299 typedef typename _Tp::argument_type argument_type; 00300 typedef typename _Tp::first_argument_type first_argument_type; 00301 typedef typename _Tp::second_argument_type second_argument_type; 00302 }; 00303 00304 _GLIBCXX_HAS_NESTED_TYPE(argument_type) 00305 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type) 00306 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type) 00307 00308 /** 00309 * Derives from unary_function or binary_function when it 00310 * can. Specializations handle all of the easy cases. The primary 00311 * template determines what to do with a class type, which may 00312 * derive from both unary_function and binary_function. 00313 */ 00314 template<typename _Tp> 00315 struct _Reference_wrapper_base 00316 : _Reference_wrapper_base_impl< 00317 __has_argument_type<_Tp>::value, 00318 __has_first_argument_type<_Tp>::value 00319 && __has_second_argument_type<_Tp>::value, 00320 _Tp> 00321 { }; 00322 00323 // - a function type (unary) 00324 template<typename _Res, typename _T1> 00325 struct _Reference_wrapper_base<_Res(_T1)> 00326 : unary_function<_T1, _Res> 00327 { }; 00328 00329 template<typename _Res, typename _T1> 00330 struct _Reference_wrapper_base<_Res(_T1) const> 00331 : unary_function<_T1, _Res> 00332 { }; 00333 00334 template<typename _Res, typename _T1> 00335 struct _Reference_wrapper_base<_Res(_T1) volatile> 00336 : unary_function<_T1, _Res> 00337 { }; 00338 00339 template<typename _Res, typename _T1> 00340 struct _Reference_wrapper_base<_Res(_T1) const volatile> 00341 : unary_function<_T1, _Res> 00342 { }; 00343 00344 // - a function type (binary) 00345 template<typename _Res, typename _T1, typename _T2> 00346 struct _Reference_wrapper_base<_Res(_T1, _T2)> 00347 : binary_function<_T1, _T2, _Res> 00348 { }; 00349 00350 template<typename _Res, typename _T1, typename _T2> 00351 struct _Reference_wrapper_base<_Res(_T1, _T2) const> 00352 : binary_function<_T1, _T2, _Res> 00353 { }; 00354 00355 template<typename _Res, typename _T1, typename _T2> 00356 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile> 00357 : binary_function<_T1, _T2, _Res> 00358 { }; 00359 00360 template<typename _Res, typename _T1, typename _T2> 00361 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile> 00362 : binary_function<_T1, _T2, _Res> 00363 { }; 00364 00365 // - a function pointer type (unary) 00366 template<typename _Res, typename _T1> 00367 struct _Reference_wrapper_base<_Res(*)(_T1)> 00368 : unary_function<_T1, _Res> 00369 { }; 00370 00371 // - a function pointer type (binary) 00372 template<typename _Res, typename _T1, typename _T2> 00373 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)> 00374 : binary_function<_T1, _T2, _Res> 00375 { }; 00376 00377 // - a pointer to member function type (unary, no qualifiers) 00378 template<typename _Res, typename _T1> 00379 struct _Reference_wrapper_base<_Res (_T1::*)()> 00380 : unary_function<_T1*, _Res> 00381 { }; 00382 00383 // - a pointer to member function type (binary, no qualifiers) 00384 template<typename _Res, typename _T1, typename _T2> 00385 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)> 00386 : binary_function<_T1*, _T2, _Res> 00387 { }; 00388 00389 // - a pointer to member function type (unary, const) 00390 template<typename _Res, typename _T1> 00391 struct _Reference_wrapper_base<_Res (_T1::*)() const> 00392 : unary_function<const _T1*, _Res> 00393 { }; 00394 00395 // - a pointer to member function type (binary, const) 00396 template<typename _Res, typename _T1, typename _T2> 00397 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const> 00398 : binary_function<const _T1*, _T2, _Res> 00399 { }; 00400 00401 // - a pointer to member function type (unary, volatile) 00402 template<typename _Res, typename _T1> 00403 struct _Reference_wrapper_base<_Res (_T1::*)() volatile> 00404 : unary_function<volatile _T1*, _Res> 00405 { }; 00406 00407 // - a pointer to member function type (binary, volatile) 00408 template<typename _Res, typename _T1, typename _T2> 00409 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile> 00410 : binary_function<volatile _T1*, _T2, _Res> 00411 { }; 00412 00413 // - a pointer to member function type (unary, const volatile) 00414 template<typename _Res, typename _T1> 00415 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile> 00416 : unary_function<const volatile _T1*, _Res> 00417 { }; 00418 00419 // - a pointer to member function type (binary, const volatile) 00420 template<typename _Res, typename _T1, typename _T2> 00421 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile> 00422 : binary_function<const volatile _T1*, _T2, _Res> 00423 { }; 00424 00425 /** 00426 * @brief Primary class template for reference_wrapper. 00427 * @ingroup functors 00428 * @{ 00429 */ 00430 template<typename _Tp> 00431 class reference_wrapper 00432 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type> 00433 { 00434 _Tp* _M_data; 00435 00436 public: 00437 typedef _Tp type; 00438 00439 reference_wrapper(_Tp& __indata) noexcept 00440 : _M_data(std::__addressof(__indata)) 00441 { } 00442 00443 reference_wrapper(_Tp&&) = delete; 00444 00445 reference_wrapper(const reference_wrapper<_Tp>& __inref) noexcept 00446 : _M_data(__inref._M_data) 00447 { } 00448 00449 reference_wrapper& 00450 operator=(const reference_wrapper<_Tp>& __inref) noexcept 00451 { 00452 _M_data = __inref._M_data; 00453 return *this; 00454 } 00455 00456 operator _Tp&() const noexcept 00457 { return this->get(); } 00458 00459 _Tp& 00460 get() const noexcept 00461 { return *_M_data; } 00462 00463 template<typename... _Args> 00464 typename result_of<_Tp&(_Args&&...)>::type 00465 operator()(_Args&&... __args) const 00466 { 00467 return __invoke(get(), std::forward<_Args>(__args)...); 00468 } 00469 }; 00470 00471 00472 /// Denotes a reference should be taken to a variable. 00473 template<typename _Tp> 00474 inline reference_wrapper<_Tp> 00475 ref(_Tp& __t) noexcept 00476 { return reference_wrapper<_Tp>(__t); } 00477 00478 /// Denotes a const reference should be taken to a variable. 00479 template<typename _Tp> 00480 inline reference_wrapper<const _Tp> 00481 cref(const _Tp& __t) noexcept 00482 { return reference_wrapper<const _Tp>(__t); } 00483 00484 template<typename _Tp> 00485 void ref(const _Tp&&) = delete; 00486 00487 template<typename _Tp> 00488 void cref(const _Tp&&) = delete; 00489 00490 /// Partial specialization. 00491 template<typename _Tp> 00492 inline reference_wrapper<_Tp> 00493 ref(reference_wrapper<_Tp> __t) noexcept 00494 { return ref(__t.get()); } 00495 00496 /// Partial specialization. 00497 template<typename _Tp> 00498 inline reference_wrapper<const _Tp> 00499 cref(reference_wrapper<_Tp> __t) noexcept 00500 { return cref(__t.get()); } 00501 00502 // @} group functors 00503 00504 template<typename... _Types> 00505 struct _Pack : integral_constant<size_t, sizeof...(_Types)> 00506 { }; 00507 00508 template<typename _From, typename _To, bool = _From::value == _To::value> 00509 struct _AllConvertible : false_type 00510 { }; 00511 00512 template<typename... _From, typename... _To> 00513 struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true> 00514 : __and_<is_convertible<_From, _To>...> 00515 { }; 00516 00517 template<typename _Tp1, typename _Tp2> 00518 using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type, 00519 typename std::decay<_Tp2>::type>>; 00520 00521 /** 00522 * Derives from @c unary_function or @c binary_function, or perhaps 00523 * nothing, depending on the number of arguments provided. The 00524 * primary template is the basis case, which derives nothing. 00525 */ 00526 template<typename _Res, typename... _ArgTypes> 00527 struct _Maybe_unary_or_binary_function { }; 00528 00529 /// Derives from @c unary_function, as appropriate. 00530 template<typename _Res, typename _T1> 00531 struct _Maybe_unary_or_binary_function<_Res, _T1> 00532 : std::unary_function<_T1, _Res> { }; 00533 00534 /// Derives from @c binary_function, as appropriate. 00535 template<typename _Res, typename _T1, typename _T2> 00536 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> 00537 : std::binary_function<_T1, _T2, _Res> { }; 00538 00539 /// Implementation of @c mem_fn for member function pointers. 00540 template<typename _Res, typename _Class, typename... _ArgTypes> 00541 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)> 00542 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...> 00543 { 00544 typedef _Res (_Class::*_Functor)(_ArgTypes...); 00545 00546 template<typename _Tp, typename... _Args> 00547 _Res 00548 _M_call(_Tp&& __object, const volatile _Class *, 00549 _Args&&... __args) const 00550 { 00551 return (std::forward<_Tp>(__object).*__pmf) 00552 (std::forward<_Args>(__args)...); 00553 } 00554 00555 template<typename _Tp, typename... _Args> 00556 _Res 00557 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const 00558 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); } 00559 00560 // Require each _Args to be convertible to corresponding _ArgTypes 00561 template<typename... _Args> 00562 using _RequireValidArgs 00563 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00564 00565 // Require each _Args to be convertible to corresponding _ArgTypes 00566 // and require _Tp is not _Class, _Class& or _Class* 00567 template<typename _Tp, typename... _Args> 00568 using _RequireValidArgs2 00569 = _Require<_NotSame<_Class, _Tp>, _NotSame<_Class*, _Tp>, 00570 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00571 00572 // Require each _Args to be convertible to corresponding _ArgTypes 00573 // and require _Tp is _Class or derived from _Class 00574 template<typename _Tp, typename... _Args> 00575 using _RequireValidArgs3 00576 = _Require<is_base_of<_Class, _Tp>, 00577 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00578 00579 public: 00580 typedef _Res result_type; 00581 00582 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 00583 00584 // Handle objects 00585 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00586 _Res 00587 operator()(_Class& __object, _Args&&... __args) const 00588 { return (__object.*__pmf)(std::forward<_Args>(__args)...); } 00589 00590 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00591 _Res 00592 operator()(_Class&& __object, _Args&&... __args) const 00593 { 00594 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...); 00595 } 00596 00597 // Handle pointers 00598 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00599 _Res 00600 operator()(_Class* __object, _Args&&... __args) const 00601 { return (__object->*__pmf)(std::forward<_Args>(__args)...); } 00602 00603 // Handle smart pointers, references and pointers to derived 00604 template<typename _Tp, typename... _Args, 00605 typename _Req = _RequireValidArgs2<_Tp, _Args...>> 00606 _Res 00607 operator()(_Tp&& __object, _Args&&... __args) const 00608 { 00609 return _M_call(std::forward<_Tp>(__object), &__object, 00610 std::forward<_Args>(__args)...); 00611 } 00612 00613 template<typename _Tp, typename... _Args, 00614 typename _Req = _RequireValidArgs3<_Tp, _Args...>> 00615 _Res 00616 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const 00617 { return operator()(__ref.get(), std::forward<_Args>(__args)...); } 00618 00619 private: 00620 _Functor __pmf; 00621 }; 00622 00623 /// Implementation of @c mem_fn for const member function pointers. 00624 template<typename _Res, typename _Class, typename... _ArgTypes> 00625 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const> 00626 : public _Maybe_unary_or_binary_function<_Res, const _Class*, 00627 _ArgTypes...> 00628 { 00629 typedef _Res (_Class::*_Functor)(_ArgTypes...) const; 00630 00631 template<typename _Tp, typename... _Args> 00632 _Res 00633 _M_call(_Tp&& __object, const volatile _Class *, 00634 _Args&&... __args) const 00635 { 00636 return (std::forward<_Tp>(__object).*__pmf) 00637 (std::forward<_Args>(__args)...); 00638 } 00639 00640 template<typename _Tp, typename... _Args> 00641 _Res 00642 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const 00643 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); } 00644 00645 template<typename... _Args> 00646 using _RequireValidArgs 00647 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00648 00649 template<typename _Tp, typename... _Args> 00650 using _RequireValidArgs2 00651 = _Require<_NotSame<_Class, _Tp>, _NotSame<const _Class*, _Tp>, 00652 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00653 00654 template<typename _Tp, typename... _Args> 00655 using _RequireValidArgs3 00656 = _Require<is_base_of<_Class, _Tp>, 00657 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00658 00659 public: 00660 typedef _Res result_type; 00661 00662 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 00663 00664 // Handle objects 00665 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00666 _Res 00667 operator()(const _Class& __object, _Args&&... __args) const 00668 { return (__object.*__pmf)(std::forward<_Args>(__args)...); } 00669 00670 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00671 _Res 00672 operator()(const _Class&& __object, _Args&&... __args) const 00673 { 00674 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...); 00675 } 00676 00677 // Handle pointers 00678 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00679 _Res 00680 operator()(const _Class* __object, _Args&&... __args) const 00681 { return (__object->*__pmf)(std::forward<_Args>(__args)...); } 00682 00683 // Handle smart pointers, references and pointers to derived 00684 template<typename _Tp, typename... _Args, 00685 typename _Req = _RequireValidArgs2<_Tp, _Args...>> 00686 _Res operator()(_Tp&& __object, _Args&&... __args) const 00687 { 00688 return _M_call(std::forward<_Tp>(__object), &__object, 00689 std::forward<_Args>(__args)...); 00690 } 00691 00692 template<typename _Tp, typename... _Args, 00693 typename _Req = _RequireValidArgs3<_Tp, _Args...>> 00694 _Res 00695 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const 00696 { return operator()(__ref.get(), std::forward<_Args>(__args)...); } 00697 00698 private: 00699 _Functor __pmf; 00700 }; 00701 00702 /// Implementation of @c mem_fn for volatile member function pointers. 00703 template<typename _Res, typename _Class, typename... _ArgTypes> 00704 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile> 00705 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*, 00706 _ArgTypes...> 00707 { 00708 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile; 00709 00710 template<typename _Tp, typename... _Args> 00711 _Res 00712 _M_call(_Tp&& __object, const volatile _Class *, 00713 _Args&&... __args) const 00714 { 00715 return (std::forward<_Tp>(__object).*__pmf) 00716 (std::forward<_Args>(__args)...); 00717 } 00718 00719 template<typename _Tp, typename... _Args> 00720 _Res 00721 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const 00722 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); } 00723 00724 template<typename... _Args> 00725 using _RequireValidArgs 00726 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00727 00728 template<typename _Tp, typename... _Args> 00729 using _RequireValidArgs2 00730 = _Require<_NotSame<_Class, _Tp>, _NotSame<volatile _Class*, _Tp>, 00731 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00732 00733 template<typename _Tp, typename... _Args> 00734 using _RequireValidArgs3 00735 = _Require<is_base_of<_Class, _Tp>, 00736 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00737 00738 public: 00739 typedef _Res result_type; 00740 00741 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 00742 00743 // Handle objects 00744 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00745 _Res 00746 operator()(volatile _Class& __object, _Args&&... __args) const 00747 { return (__object.*__pmf)(std::forward<_Args>(__args)...); } 00748 00749 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00750 _Res 00751 operator()(volatile _Class&& __object, _Args&&... __args) const 00752 { 00753 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...); 00754 } 00755 00756 // Handle pointers 00757 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00758 _Res 00759 operator()(volatile _Class* __object, _Args&&... __args) const 00760 { return (__object->*__pmf)(std::forward<_Args>(__args)...); } 00761 00762 // Handle smart pointers, references and pointers to derived 00763 template<typename _Tp, typename... _Args, 00764 typename _Req = _RequireValidArgs2<_Tp, _Args...>> 00765 _Res 00766 operator()(_Tp&& __object, _Args&&... __args) const 00767 { 00768 return _M_call(std::forward<_Tp>(__object), &__object, 00769 std::forward<_Args>(__args)...); 00770 } 00771 00772 template<typename _Tp, typename... _Args, 00773 typename _Req = _RequireValidArgs3<_Tp, _Args...>> 00774 _Res 00775 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const 00776 { return operator()(__ref.get(), std::forward<_Args>(__args)...); } 00777 00778 private: 00779 _Functor __pmf; 00780 }; 00781 00782 /// Implementation of @c mem_fn for const volatile member function pointers. 00783 template<typename _Res, typename _Class, typename... _ArgTypes> 00784 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile> 00785 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*, 00786 _ArgTypes...> 00787 { 00788 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile; 00789 00790 template<typename _Tp, typename... _Args> 00791 _Res 00792 _M_call(_Tp&& __object, const volatile _Class *, 00793 _Args&&... __args) const 00794 { 00795 return (std::forward<_Tp>(__object).*__pmf) 00796 (std::forward<_Args>(__args)...); 00797 } 00798 00799 template<typename _Tp, typename... _Args> 00800 _Res 00801 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const 00802 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); } 00803 00804 template<typename... _Args> 00805 using _RequireValidArgs 00806 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00807 00808 template<typename _Tp, typename... _Args> 00809 using _RequireValidArgs2 00810 = _Require<_NotSame<_Class, _Tp>, 00811 _NotSame<const volatile _Class*, _Tp>, 00812 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00813 00814 template<typename _Tp, typename... _Args> 00815 using _RequireValidArgs3 00816 = _Require<is_base_of<_Class, _Tp>, 00817 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 00818 00819 public: 00820 typedef _Res result_type; 00821 00822 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 00823 00824 // Handle objects 00825 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00826 _Res 00827 operator()(const volatile _Class& __object, _Args&&... __args) const 00828 { return (__object.*__pmf)(std::forward<_Args>(__args)...); } 00829 00830 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00831 _Res 00832 operator()(const volatile _Class&& __object, _Args&&... __args) const 00833 { 00834 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...); 00835 } 00836 00837 // Handle pointers 00838 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 00839 _Res 00840 operator()(const volatile _Class* __object, _Args&&... __args) const 00841 { return (__object->*__pmf)(std::forward<_Args>(__args)...); } 00842 00843 // Handle smart pointers, references and pointers to derived 00844 template<typename _Tp, typename... _Args, 00845 typename _Req = _RequireValidArgs2<_Tp, _Args...>> 00846 _Res operator()(_Tp&& __object, _Args&&... __args) const 00847 { 00848 return _M_call(std::forward<_Tp>(__object), &__object, 00849 std::forward<_Args>(__args)...); 00850 } 00851 00852 template<typename _Tp, typename... _Args, 00853 typename _Req = _RequireValidArgs3<_Tp, _Args...>> 00854 _Res 00855 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const 00856 { return operator()(__ref.get(), std::forward<_Args>(__args)...); } 00857 00858 private: 00859 _Functor __pmf; 00860 }; 00861 00862 00863 template<typename _Tp, bool> 00864 struct _Mem_fn_const_or_non 00865 { 00866 typedef const _Tp& type; 00867 }; 00868 00869 template<typename _Tp> 00870 struct _Mem_fn_const_or_non<_Tp, false> 00871 { 00872 typedef _Tp& type; 00873 }; 00874 00875 template<typename _Res, typename _Class> 00876 class _Mem_fn<_Res _Class::*> 00877 { 00878 using __pm_type = _Res _Class::*; 00879 00880 // This bit of genius is due to Peter Dimov, improved slightly by 00881 // Douglas Gregor. 00882 // Made less elegant to support perfect forwarding and noexcept. 00883 template<typename _Tp> 00884 auto 00885 _M_call(_Tp&& __object, const _Class *) const noexcept 00886 -> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>()) 00887 { return std::forward<_Tp>(__object).*__pm; } 00888 00889 template<typename _Tp, typename _Up> 00890 auto 00891 _M_call(_Tp&& __object, _Up * const *) const noexcept 00892 -> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>()) 00893 { return (*std::forward<_Tp>(__object)).*__pm; } 00894 00895 template<typename _Tp> 00896 auto 00897 _M_call(_Tp&& __ptr, const volatile void*) const 00898 noexcept(noexcept((*__ptr).*std::declval<__pm_type&>())) 00899 -> decltype((*__ptr).*std::declval<__pm_type&>()) 00900 { return (*__ptr).*__pm; } 00901 00902 public: 00903 explicit 00904 _Mem_fn(_Res _Class::*__pm) noexcept : __pm(__pm) { } 00905 00906 // Handle objects 00907 _Res& 00908 operator()(_Class& __object) const noexcept 00909 { return __object.*__pm; } 00910 00911 const _Res& 00912 operator()(const _Class& __object) const noexcept 00913 { return __object.*__pm; } 00914 00915 _Res&& 00916 operator()(_Class&& __object) const noexcept 00917 { return std::forward<_Class>(__object).*__pm; } 00918 00919 const _Res&& 00920 operator()(const _Class&& __object) const noexcept 00921 { return std::forward<const _Class>(__object).*__pm; } 00922 00923 // Handle pointers 00924 _Res& 00925 operator()(_Class* __object) const noexcept 00926 { return __object->*__pm; } 00927 00928 const _Res& 00929 operator()(const _Class* __object) const noexcept 00930 { return __object->*__pm; } 00931 00932 // Handle smart pointers and derived 00933 template<typename _Tp, typename _Req = _Require<_NotSame<_Class*, _Tp>>> 00934 auto 00935 operator()(_Tp&& __unknown) const 00936 noexcept(noexcept(std::declval<_Mem_fn*>()->_M_call 00937 (std::forward<_Tp>(__unknown), &__unknown))) 00938 -> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown)) 00939 { return _M_call(std::forward<_Tp>(__unknown), &__unknown); } 00940 00941 template<typename _Tp, typename _Req = _Require<is_base_of<_Class, _Tp>>> 00942 auto 00943 operator()(reference_wrapper<_Tp> __ref) const 00944 noexcept(noexcept(std::declval<_Mem_fn&>()(__ref.get()))) 00945 -> decltype((*this)(__ref.get())) 00946 { return (*this)(__ref.get()); } 00947 00948 private: 00949 _Res _Class::*__pm; 00950 }; 00951 00952 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00953 // 2048. Unnecessary mem_fn overloads 00954 /** 00955 * @brief Returns a function object that forwards to the member 00956 * pointer @a pm. 00957 * @ingroup functors 00958 */ 00959 template<typename _Tp, typename _Class> 00960 inline _Mem_fn<_Tp _Class::*> 00961 mem_fn(_Tp _Class::* __pm) noexcept 00962 { 00963 return _Mem_fn<_Tp _Class::*>(__pm); 00964 } 00965 00966 /** 00967 * @brief Determines if the given type _Tp is a function object 00968 * should be treated as a subexpression when evaluating calls to 00969 * function objects returned by bind(). [TR1 3.6.1] 00970 * @ingroup binders 00971 */ 00972 template<typename _Tp> 00973 struct is_bind_expression 00974 : public false_type { }; 00975 00976 /** 00977 * @brief Determines if the given type _Tp is a placeholder in a 00978 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2] 00979 * @ingroup binders 00980 */ 00981 template<typename _Tp> 00982 struct is_placeholder 00983 : public integral_constant<int, 0> 00984 { }; 00985 00986 /** @brief The type of placeholder objects defined by libstdc++. 00987 * @ingroup binders 00988 */ 00989 template<int _Num> struct _Placeholder { }; 00990 00991 _GLIBCXX_END_NAMESPACE_VERSION 00992 00993 /** @namespace std::placeholders 00994 * @brief ISO C++11 entities sub-namespace for functional. 00995 * @ingroup binders 00996 */ 00997 namespace placeholders 00998 { 00999 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01000 /* Define a large number of placeholders. There is no way to 01001 * simplify this with variadic templates, because we're introducing 01002 * unique names for each. 01003 */ 01004 extern const _Placeholder<1> _1; 01005 extern const _Placeholder<2> _2; 01006 extern const _Placeholder<3> _3; 01007 extern const _Placeholder<4> _4; 01008 extern const _Placeholder<5> _5; 01009 extern const _Placeholder<6> _6; 01010 extern const _Placeholder<7> _7; 01011 extern const _Placeholder<8> _8; 01012 extern const _Placeholder<9> _9; 01013 extern const _Placeholder<10> _10; 01014 extern const _Placeholder<11> _11; 01015 extern const _Placeholder<12> _12; 01016 extern const _Placeholder<13> _13; 01017 extern const _Placeholder<14> _14; 01018 extern const _Placeholder<15> _15; 01019 extern const _Placeholder<16> _16; 01020 extern const _Placeholder<17> _17; 01021 extern const _Placeholder<18> _18; 01022 extern const _Placeholder<19> _19; 01023 extern const _Placeholder<20> _20; 01024 extern const _Placeholder<21> _21; 01025 extern const _Placeholder<22> _22; 01026 extern const _Placeholder<23> _23; 01027 extern const _Placeholder<24> _24; 01028 extern const _Placeholder<25> _25; 01029 extern const _Placeholder<26> _26; 01030 extern const _Placeholder<27> _27; 01031 extern const _Placeholder<28> _28; 01032 extern const _Placeholder<29> _29; 01033 _GLIBCXX_END_NAMESPACE_VERSION 01034 } 01035 01036 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01037 01038 /** 01039 * Partial specialization of is_placeholder that provides the placeholder 01040 * number for the placeholder objects defined by libstdc++. 01041 * @ingroup binders 01042 */ 01043 template<int _Num> 01044 struct is_placeholder<_Placeholder<_Num> > 01045 : public integral_constant<int, _Num> 01046 { }; 01047 01048 template<int _Num> 01049 struct is_placeholder<const _Placeholder<_Num> > 01050 : public integral_constant<int, _Num> 01051 { }; 01052 01053 /** 01054 * Used by _Safe_tuple_element to indicate that there is no tuple 01055 * element at this position. 01056 */ 01057 struct _No_tuple_element; 01058 01059 /** 01060 * Implementation helper for _Safe_tuple_element. This primary 01061 * template handles the case where it is safe to use @c 01062 * tuple_element. 01063 */ 01064 template<std::size_t __i, typename _Tuple, bool _IsSafe> 01065 struct _Safe_tuple_element_impl 01066 : tuple_element<__i, _Tuple> { }; 01067 01068 /** 01069 * Implementation helper for _Safe_tuple_element. This partial 01070 * specialization handles the case where it is not safe to use @c 01071 * tuple_element. We just return @c _No_tuple_element. 01072 */ 01073 template<std::size_t __i, typename _Tuple> 01074 struct _Safe_tuple_element_impl<__i, _Tuple, false> 01075 { 01076 typedef _No_tuple_element type; 01077 }; 01078 01079 /** 01080 * Like tuple_element, but returns @c _No_tuple_element when 01081 * tuple_element would return an error. 01082 */ 01083 template<std::size_t __i, typename _Tuple> 01084 struct _Safe_tuple_element 01085 : _Safe_tuple_element_impl<__i, _Tuple, 01086 (__i < tuple_size<_Tuple>::value)> 01087 { }; 01088 01089 /** 01090 * Maps an argument to bind() into an actual argument to the bound 01091 * function object [TR1 3.6.3/5]. Only the first parameter should 01092 * be specified: the rest are used to determine among the various 01093 * implementations. Note that, although this class is a function 01094 * object, it isn't entirely normal because it takes only two 01095 * parameters regardless of the number of parameters passed to the 01096 * bind expression. The first parameter is the bound argument and 01097 * the second parameter is a tuple containing references to the 01098 * rest of the arguments. 01099 */ 01100 template<typename _Arg, 01101 bool _IsBindExp = is_bind_expression<_Arg>::value, 01102 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)> 01103 class _Mu; 01104 01105 /** 01106 * If the argument is reference_wrapper<_Tp>, returns the 01107 * underlying reference. [TR1 3.6.3/5 bullet 1] 01108 */ 01109 template<typename _Tp> 01110 class _Mu<reference_wrapper<_Tp>, false, false> 01111 { 01112 public: 01113 typedef _Tp& result_type; 01114 01115 /* Note: This won't actually work for const volatile 01116 * reference_wrappers, because reference_wrapper::get() is const 01117 * but not volatile-qualified. This might be a defect in the TR. 01118 */ 01119 template<typename _CVRef, typename _Tuple> 01120 result_type 01121 operator()(_CVRef& __arg, _Tuple&) const volatile 01122 { return __arg.get(); } 01123 }; 01124 01125 /** 01126 * If the argument is a bind expression, we invoke the underlying 01127 * function object with the same cv-qualifiers as we are given and 01128 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2] 01129 */ 01130 template<typename _Arg> 01131 class _Mu<_Arg, true, false> 01132 { 01133 public: 01134 template<typename _CVArg, typename... _Args> 01135 auto 01136 operator()(_CVArg& __arg, 01137 tuple<_Args...>& __tuple) const volatile 01138 -> decltype(__arg(declval<_Args>()...)) 01139 { 01140 // Construct an index tuple and forward to __call 01141 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type 01142 _Indexes; 01143 return this->__call(__arg, __tuple, _Indexes()); 01144 } 01145 01146 private: 01147 // Invokes the underlying function object __arg by unpacking all 01148 // of the arguments in the tuple. 01149 template<typename _CVArg, typename... _Args, std::size_t... _Indexes> 01150 auto 01151 __call(_CVArg& __arg, tuple<_Args...>& __tuple, 01152 const _Index_tuple<_Indexes...>&) const volatile 01153 -> decltype(__arg(declval<_Args>()...)) 01154 { 01155 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...); 01156 } 01157 }; 01158 01159 /** 01160 * If the argument is a placeholder for the Nth argument, returns 01161 * a reference to the Nth argument to the bind function object. 01162 * [TR1 3.6.3/5 bullet 3] 01163 */ 01164 template<typename _Arg> 01165 class _Mu<_Arg, false, true> 01166 { 01167 public: 01168 template<typename _Signature> class result; 01169 01170 template<typename _CVMu, typename _CVArg, typename _Tuple> 01171 class result<_CVMu(_CVArg, _Tuple)> 01172 { 01173 // Add a reference, if it hasn't already been done for us. 01174 // This allows us to be a little bit sloppy in constructing 01175 // the tuple that we pass to result_of<...>. 01176 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value 01177 - 1), _Tuple>::type 01178 __base_type; 01179 01180 public: 01181 typedef typename add_rvalue_reference<__base_type>::type type; 01182 }; 01183 01184 template<typename _Tuple> 01185 typename result<_Mu(_Arg, _Tuple)>::type 01186 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile 01187 { 01188 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>( 01189 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple)); 01190 } 01191 }; 01192 01193 /** 01194 * If the argument is just a value, returns a reference to that 01195 * value. The cv-qualifiers on the reference are the same as the 01196 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4] 01197 */ 01198 template<typename _Arg> 01199 class _Mu<_Arg, false, false> 01200 { 01201 public: 01202 template<typename _Signature> struct result; 01203 01204 template<typename _CVMu, typename _CVArg, typename _Tuple> 01205 struct result<_CVMu(_CVArg, _Tuple)> 01206 { 01207 typedef typename add_lvalue_reference<_CVArg>::type type; 01208 }; 01209 01210 // Pick up the cv-qualifiers of the argument 01211 template<typename _CVArg, typename _Tuple> 01212 _CVArg&& 01213 operator()(_CVArg&& __arg, _Tuple&) const volatile 01214 { return std::forward<_CVArg>(__arg); } 01215 }; 01216 01217 /** 01218 * Maps member pointers into instances of _Mem_fn but leaves all 01219 * other function objects untouched. Used by tr1::bind(). The 01220 * primary template handles the non--member-pointer case. 01221 */ 01222 template<typename _Tp> 01223 struct _Maybe_wrap_member_pointer 01224 { 01225 typedef _Tp type; 01226 01227 static const _Tp& 01228 __do_wrap(const _Tp& __x) 01229 { return __x; } 01230 01231 static _Tp&& 01232 __do_wrap(_Tp&& __x) 01233 { return static_cast<_Tp&&>(__x); } 01234 }; 01235 01236 /** 01237 * Maps member pointers into instances of _Mem_fn but leaves all 01238 * other function objects untouched. Used by tr1::bind(). This 01239 * partial specialization handles the member pointer case. 01240 */ 01241 template<typename _Tp, typename _Class> 01242 struct _Maybe_wrap_member_pointer<_Tp _Class::*> 01243 { 01244 typedef _Mem_fn<_Tp _Class::*> type; 01245 01246 static type 01247 __do_wrap(_Tp _Class::* __pm) 01248 { return type(__pm); } 01249 }; 01250 01251 // Specialization needed to prevent "forming reference to void" errors when 01252 // bind<void>() is called, because argument deduction instantiates 01253 // _Maybe_wrap_member_pointer<void> outside the immediate context where 01254 // SFINAE applies. 01255 template<> 01256 struct _Maybe_wrap_member_pointer<void> 01257 { 01258 typedef void type; 01259 }; 01260 01261 // std::get<I> for volatile-qualified tuples 01262 template<std::size_t _Ind, typename... _Tp> 01263 inline auto 01264 __volget(volatile tuple<_Tp...>& __tuple) 01265 -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile& 01266 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); } 01267 01268 // std::get<I> for const-volatile-qualified tuples 01269 template<std::size_t _Ind, typename... _Tp> 01270 inline auto 01271 __volget(const volatile tuple<_Tp...>& __tuple) 01272 -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile& 01273 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); } 01274 01275 /// Type of the function object returned from bind(). 01276 template<typename _Signature> 01277 struct _Bind; 01278 01279 template<typename _Functor, typename... _Bound_args> 01280 class _Bind<_Functor(_Bound_args...)> 01281 : public _Weak_result_type<_Functor> 01282 { 01283 typedef _Bind __self_type; 01284 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 01285 _Bound_indexes; 01286 01287 _Functor _M_f; 01288 tuple<_Bound_args...> _M_bound_args; 01289 01290 // Call unqualified 01291 template<typename _Result, typename... _Args, std::size_t... _Indexes> 01292 _Result 01293 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) 01294 { 01295 return _M_f(_Mu<_Bound_args>() 01296 (get<_Indexes>(_M_bound_args), __args)...); 01297 } 01298 01299 // Call as const 01300 template<typename _Result, typename... _Args, std::size_t... _Indexes> 01301 _Result 01302 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const 01303 { 01304 return _M_f(_Mu<_Bound_args>() 01305 (get<_Indexes>(_M_bound_args), __args)...); 01306 } 01307 01308 // Call as volatile 01309 template<typename _Result, typename... _Args, std::size_t... _Indexes> 01310 _Result 01311 __call_v(tuple<_Args...>&& __args, 01312 _Index_tuple<_Indexes...>) volatile 01313 { 01314 return _M_f(_Mu<_Bound_args>() 01315 (__volget<_Indexes>(_M_bound_args), __args)...); 01316 } 01317 01318 // Call as const volatile 01319 template<typename _Result, typename... _Args, std::size_t... _Indexes> 01320 _Result 01321 __call_c_v(tuple<_Args...>&& __args, 01322 _Index_tuple<_Indexes...>) const volatile 01323 { 01324 return _M_f(_Mu<_Bound_args>() 01325 (__volget<_Indexes>(_M_bound_args), __args)...); 01326 } 01327 01328 public: 01329 template<typename... _Args> 01330 explicit _Bind(const _Functor& __f, _Args&&... __args) 01331 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...) 01332 { } 01333 01334 template<typename... _Args> 01335 explicit _Bind(_Functor&& __f, _Args&&... __args) 01336 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...) 01337 { } 01338 01339 _Bind(const _Bind&) = default; 01340 01341 _Bind(_Bind&& __b) 01342 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args)) 01343 { } 01344 01345 // Call unqualified 01346 template<typename... _Args, typename _Result 01347 = decltype( std::declval<_Functor>()( 01348 _Mu<_Bound_args>()( std::declval<_Bound_args&>(), 01349 std::declval<tuple<_Args...>&>() )... ) )> 01350 _Result 01351 operator()(_Args&&... __args) 01352 { 01353 return this->__call<_Result>( 01354 std::forward_as_tuple(std::forward<_Args>(__args)...), 01355 _Bound_indexes()); 01356 } 01357 01358 // Call as const 01359 template<typename... _Args, typename _Result 01360 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0), 01361 typename add_const<_Functor>::type>::type>()( 01362 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(), 01363 std::declval<tuple<_Args...>&>() )... ) )> 01364 _Result 01365 operator()(_Args&&... __args) const 01366 { 01367 return this->__call_c<_Result>( 01368 std::forward_as_tuple(std::forward<_Args>(__args)...), 01369 _Bound_indexes()); 01370 } 01371 01372 // Call as volatile 01373 template<typename... _Args, typename _Result 01374 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0), 01375 typename add_volatile<_Functor>::type>::type>()( 01376 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(), 01377 std::declval<tuple<_Args...>&>() )... ) )> 01378 _Result 01379 operator()(_Args&&... __args) volatile 01380 { 01381 return this->__call_v<_Result>( 01382 std::forward_as_tuple(std::forward<_Args>(__args)...), 01383 _Bound_indexes()); 01384 } 01385 01386 // Call as const volatile 01387 template<typename... _Args, typename _Result 01388 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0), 01389 typename add_cv<_Functor>::type>::type>()( 01390 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(), 01391 std::declval<tuple<_Args...>&>() )... ) )> 01392 _Result 01393 operator()(_Args&&... __args) const volatile 01394 { 01395 return this->__call_c_v<_Result>( 01396 std::forward_as_tuple(std::forward<_Args>(__args)...), 01397 _Bound_indexes()); 01398 } 01399 }; 01400 01401 /// Type of the function object returned from bind<R>(). 01402 template<typename _Result, typename _Signature> 01403 struct _Bind_result; 01404 01405 template<typename _Result, typename _Functor, typename... _Bound_args> 01406 class _Bind_result<_Result, _Functor(_Bound_args...)> 01407 { 01408 typedef _Bind_result __self_type; 01409 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 01410 _Bound_indexes; 01411 01412 _Functor _M_f; 01413 tuple<_Bound_args...> _M_bound_args; 01414 01415 // sfinae types 01416 template<typename _Res> 01417 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { }; 01418 template<typename _Res> 01419 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { }; 01420 01421 // Call unqualified 01422 template<typename _Res, typename... _Args, std::size_t... _Indexes> 01423 _Result 01424 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, 01425 typename __disable_if_void<_Res>::type = 0) 01426 { 01427 return _M_f(_Mu<_Bound_args>() 01428 (get<_Indexes>(_M_bound_args), __args)...); 01429 } 01430 01431 // Call unqualified, return void 01432 template<typename _Res, typename... _Args, std::size_t... _Indexes> 01433 void 01434 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, 01435 typename __enable_if_void<_Res>::type = 0) 01436 { 01437 _M_f(_Mu<_Bound_args>() 01438 (get<_Indexes>(_M_bound_args), __args)...); 01439 } 01440 01441 // Call as const 01442 template<typename _Res, typename... _Args, std::size_t... _Indexes> 01443 _Result 01444 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, 01445 typename __disable_if_void<_Res>::type = 0) const 01446 { 01447 return _M_f(_Mu<_Bound_args>() 01448 (get<_Indexes>(_M_bound_args), __args)...); 01449 } 01450 01451 // Call as const, return void 01452 template<typename _Res, typename... _Args, std::size_t... _Indexes> 01453 void 01454 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, 01455 typename __enable_if_void<_Res>::type = 0) const 01456 { 01457 _M_f(_Mu<_Bound_args>() 01458 (get<_Indexes>(_M_bound_args), __args)...); 01459 } 01460 01461 // Call as volatile 01462 template<typename _Res, typename... _Args, std::size_t... _Indexes> 01463 _Result 01464 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, 01465 typename __disable_if_void<_Res>::type = 0) volatile 01466 { 01467 return _M_f(_Mu<_Bound_args>() 01468 (__volget<_Indexes>(_M_bound_args), __args)...); 01469 } 01470 01471 // Call as volatile, return void 01472 template<typename _Res, typename... _Args, std::size_t... _Indexes> 01473 void 01474 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, 01475 typename __enable_if_void<_Res>::type = 0) volatile 01476 { 01477 _M_f(_Mu<_Bound_args>() 01478 (__volget<_Indexes>(_M_bound_args), __args)...); 01479 } 01480 01481 // Call as const volatile 01482 template<typename _Res, typename... _Args, std::size_t... _Indexes> 01483 _Result 01484 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>, 01485 typename __disable_if_void<_Res>::type = 0) const volatile 01486 { 01487 return _M_f(_Mu<_Bound_args>() 01488 (__volget<_Indexes>(_M_bound_args), __args)...); 01489 } 01490 01491 // Call as const volatile, return void 01492 template<typename _Res, typename... _Args, std::size_t... _Indexes> 01493 void 01494 __call(tuple<_Args...>&& __args, 01495 _Index_tuple<_Indexes...>, 01496 typename __enable_if_void<_Res>::type = 0) const volatile 01497 { 01498 _M_f(_Mu<_Bound_args>() 01499 (__volget<_Indexes>(_M_bound_args), __args)...); 01500 } 01501 01502 public: 01503 typedef _Result result_type; 01504 01505 template<typename... _Args> 01506 explicit _Bind_result(const _Functor& __f, _Args&&... __args) 01507 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...) 01508 { } 01509 01510 template<typename... _Args> 01511 explicit _Bind_result(_Functor&& __f, _Args&&... __args) 01512 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...) 01513 { } 01514 01515 _Bind_result(const _Bind_result&) = default; 01516 01517 _Bind_result(_Bind_result&& __b) 01518 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args)) 01519 { } 01520 01521 // Call unqualified 01522 template<typename... _Args> 01523 result_type 01524 operator()(_Args&&... __args) 01525 { 01526 return this->__call<_Result>( 01527 std::forward_as_tuple(std::forward<_Args>(__args)...), 01528 _Bound_indexes()); 01529 } 01530 01531 // Call as const 01532 template<typename... _Args> 01533 result_type 01534 operator()(_Args&&... __args) const 01535 { 01536 return this->__call<_Result>( 01537 std::forward_as_tuple(std::forward<_Args>(__args)...), 01538 _Bound_indexes()); 01539 } 01540 01541 // Call as volatile 01542 template<typename... _Args> 01543 result_type 01544 operator()(_Args&&... __args) volatile 01545 { 01546 return this->__call<_Result>( 01547 std::forward_as_tuple(std::forward<_Args>(__args)...), 01548 _Bound_indexes()); 01549 } 01550 01551 // Call as const volatile 01552 template<typename... _Args> 01553 result_type 01554 operator()(_Args&&... __args) const volatile 01555 { 01556 return this->__call<_Result>( 01557 std::forward_as_tuple(std::forward<_Args>(__args)...), 01558 _Bound_indexes()); 01559 } 01560 }; 01561 01562 /** 01563 * @brief Class template _Bind is always a bind expression. 01564 * @ingroup binders 01565 */ 01566 template<typename _Signature> 01567 struct is_bind_expression<_Bind<_Signature> > 01568 : public true_type { }; 01569 01570 /** 01571 * @brief Class template _Bind is always a bind expression. 01572 * @ingroup binders 01573 */ 01574 template<typename _Signature> 01575 struct is_bind_expression<const _Bind<_Signature> > 01576 : public true_type { }; 01577 01578 /** 01579 * @brief Class template _Bind is always a bind expression. 01580 * @ingroup binders 01581 */ 01582 template<typename _Signature> 01583 struct is_bind_expression<volatile _Bind<_Signature> > 01584 : public true_type { }; 01585 01586 /** 01587 * @brief Class template _Bind is always a bind expression. 01588 * @ingroup binders 01589 */ 01590 template<typename _Signature> 01591 struct is_bind_expression<const volatile _Bind<_Signature>> 01592 : public true_type { }; 01593 01594 /** 01595 * @brief Class template _Bind_result is always a bind expression. 01596 * @ingroup binders 01597 */ 01598 template<typename _Result, typename _Signature> 01599 struct is_bind_expression<_Bind_result<_Result, _Signature>> 01600 : public true_type { }; 01601 01602 /** 01603 * @brief Class template _Bind_result is always a bind expression. 01604 * @ingroup binders 01605 */ 01606 template<typename _Result, typename _Signature> 01607 struct is_bind_expression<const _Bind_result<_Result, _Signature>> 01608 : public true_type { }; 01609 01610 /** 01611 * @brief Class template _Bind_result is always a bind expression. 01612 * @ingroup binders 01613 */ 01614 template<typename _Result, typename _Signature> 01615 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>> 01616 : public true_type { }; 01617 01618 /** 01619 * @brief Class template _Bind_result is always a bind expression. 01620 * @ingroup binders 01621 */ 01622 template<typename _Result, typename _Signature> 01623 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>> 01624 : public true_type { }; 01625 01626 // Trait type used to remove std::bind() from overload set via SFINAE 01627 // when first argument has integer type, so that std::bind() will 01628 // not be a better match than ::bind() from the BSD Sockets API. 01629 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type> 01630 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>; 01631 01632 template<bool _SocketLike, typename _Func, typename... _BoundArgs> 01633 struct _Bind_helper 01634 { 01635 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type> 01636 __maybe_type; 01637 typedef typename __maybe_type::type __func_type; 01638 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type; 01639 }; 01640 01641 // Partial specialization for is_socketlike == true, does not define 01642 // nested type so std::bind() will not participate in overload resolution 01643 // when the first argument might be a socket file descriptor. 01644 template<typename _Func, typename... _BoundArgs> 01645 struct _Bind_helper<true, _Func, _BoundArgs...> 01646 { }; 01647 01648 /** 01649 * @brief Function template for std::bind. 01650 * @ingroup binders 01651 */ 01652 template<typename _Func, typename... _BoundArgs> 01653 inline typename 01654 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type 01655 bind(_Func&& __f, _BoundArgs&&... __args) 01656 { 01657 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type; 01658 typedef typename __helper_type::__maybe_type __maybe_type; 01659 typedef typename __helper_type::type __result_type; 01660 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)), 01661 std::forward<_BoundArgs>(__args)...); 01662 } 01663 01664 template<typename _Result, typename _Func, typename... _BoundArgs> 01665 struct _Bindres_helper 01666 { 01667 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type> 01668 __maybe_type; 01669 typedef typename __maybe_type::type __functor_type; 01670 typedef _Bind_result<_Result, 01671 __functor_type(typename decay<_BoundArgs>::type...)> 01672 type; 01673 }; 01674 01675 /** 01676 * @brief Function template for std::bind<R>. 01677 * @ingroup binders 01678 */ 01679 template<typename _Result, typename _Func, typename... _BoundArgs> 01680 inline 01681 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type 01682 bind(_Func&& __f, _BoundArgs&&... __args) 01683 { 01684 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type; 01685 typedef typename __helper_type::__maybe_type __maybe_type; 01686 typedef typename __helper_type::type __result_type; 01687 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)), 01688 std::forward<_BoundArgs>(__args)...); 01689 } 01690 01691 template<typename _Signature> 01692 struct _Bind_simple; 01693 01694 template<typename _Callable, typename... _Args> 01695 struct _Bind_simple<_Callable(_Args...)> 01696 { 01697 typedef typename result_of<_Callable(_Args...)>::type result_type; 01698 01699 template<typename... _Args2, typename = typename 01700 enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type> 01701 explicit 01702 _Bind_simple(const _Callable& __callable, _Args2&&... __args) 01703 : _M_bound(__callable, std::forward<_Args2>(__args)...) 01704 { } 01705 01706 template<typename... _Args2, typename = typename 01707 enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type> 01708 explicit 01709 _Bind_simple(_Callable&& __callable, _Args2&&... __args) 01710 : _M_bound(std::move(__callable), std::forward<_Args2>(__args)...) 01711 { } 01712 01713 _Bind_simple(const _Bind_simple&) = default; 01714 _Bind_simple(_Bind_simple&&) = default; 01715 01716 result_type 01717 operator()() 01718 { 01719 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices; 01720 return _M_invoke(_Indices()); 01721 } 01722 01723 private: 01724 01725 template<std::size_t... _Indices> 01726 typename result_of<_Callable(_Args...)>::type 01727 _M_invoke(_Index_tuple<_Indices...>) 01728 { 01729 // std::bind always forwards bound arguments as lvalues, 01730 // but this type can call functions which only accept rvalues. 01731 return std::forward<_Callable>(std::get<0>(_M_bound))( 01732 std::forward<_Args>(std::get<_Indices+1>(_M_bound))...); 01733 } 01734 01735 std::tuple<_Callable, _Args...> _M_bound; 01736 }; 01737 01738 template<typename _Func, typename... _BoundArgs> 01739 struct _Bind_simple_helper 01740 { 01741 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type> 01742 __maybe_type; 01743 typedef typename __maybe_type::type __func_type; 01744 typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)> 01745 __type; 01746 }; 01747 01748 // Simplified version of std::bind for internal use, without support for 01749 // unbound arguments, placeholders or nested bind expressions. 01750 template<typename _Callable, typename... _Args> 01751 typename _Bind_simple_helper<_Callable, _Args...>::__type 01752 __bind_simple(_Callable&& __callable, _Args&&... __args) 01753 { 01754 typedef _Bind_simple_helper<_Callable, _Args...> __helper_type; 01755 typedef typename __helper_type::__maybe_type __maybe_type; 01756 typedef typename __helper_type::__type __result_type; 01757 return __result_type( 01758 __maybe_type::__do_wrap( std::forward<_Callable>(__callable)), 01759 std::forward<_Args>(__args)...); 01760 } 01761 01762 /** 01763 * @brief Exception class thrown when class template function's 01764 * operator() is called with an empty target. 01765 * @ingroup exceptions 01766 */ 01767 class bad_function_call : public std::exception 01768 { 01769 public: 01770 virtual ~bad_function_call() noexcept; 01771 01772 const char* what() const noexcept; 01773 }; 01774 01775 /** 01776 * Trait identifying "location-invariant" types, meaning that the 01777 * address of the object (or any of its members) will not escape. 01778 * Also implies a trivial copy constructor and assignment operator. 01779 */ 01780 template<typename _Tp> 01781 struct __is_location_invariant 01782 : integral_constant<bool, (is_pointer<_Tp>::value 01783 || is_member_pointer<_Tp>::value)> 01784 { }; 01785 01786 class _Undefined_class; 01787 01788 union _Nocopy_types 01789 { 01790 void* _M_object; 01791 const void* _M_const_object; 01792 void (*_M_function_pointer)(); 01793 void (_Undefined_class::*_M_member_pointer)(); 01794 }; 01795 01796 union _Any_data 01797 { 01798 void* _M_access() { return &_M_pod_data[0]; } 01799 const void* _M_access() const { return &_M_pod_data[0]; } 01800 01801 template<typename _Tp> 01802 _Tp& 01803 _M_access() 01804 { return *static_cast<_Tp*>(_M_access()); } 01805 01806 template<typename _Tp> 01807 const _Tp& 01808 _M_access() const 01809 { return *static_cast<const _Tp*>(_M_access()); } 01810 01811 _Nocopy_types _M_unused; 01812 char _M_pod_data[sizeof(_Nocopy_types)]; 01813 }; 01814 01815 enum _Manager_operation 01816 { 01817 __get_type_info, 01818 __get_functor_ptr, 01819 __clone_functor, 01820 __destroy_functor 01821 }; 01822 01823 // Simple type wrapper that helps avoid annoying const problems 01824 // when casting between void pointers and pointers-to-pointers. 01825 template<typename _Tp> 01826 struct _Simple_type_wrapper 01827 { 01828 _Simple_type_wrapper(_Tp __value) : __value(__value) { } 01829 01830 _Tp __value; 01831 }; 01832 01833 template<typename _Tp> 01834 struct __is_location_invariant<_Simple_type_wrapper<_Tp> > 01835 : __is_location_invariant<_Tp> 01836 { }; 01837 01838 // Converts a reference to a function object into a callable 01839 // function object. 01840 template<typename _Functor> 01841 inline _Functor& 01842 __callable_functor(_Functor& __f) 01843 { return __f; } 01844 01845 template<typename _Member, typename _Class> 01846 inline _Mem_fn<_Member _Class::*> 01847 __callable_functor(_Member _Class::* &__p) 01848 { return std::mem_fn(__p); } 01849 01850 template<typename _Member, typename _Class> 01851 inline _Mem_fn<_Member _Class::*> 01852 __callable_functor(_Member _Class::* const &__p) 01853 { return std::mem_fn(__p); } 01854 01855 template<typename _Member, typename _Class> 01856 inline _Mem_fn<_Member _Class::*> 01857 __callable_functor(_Member _Class::* volatile &__p) 01858 { return std::mem_fn(__p); } 01859 01860 template<typename _Member, typename _Class> 01861 inline _Mem_fn<_Member _Class::*> 01862 __callable_functor(_Member _Class::* const volatile &__p) 01863 { return std::mem_fn(__p); } 01864 01865 template<typename _Signature> 01866 class function; 01867 01868 /// Base class of all polymorphic function object wrappers. 01869 class _Function_base 01870 { 01871 public: 01872 static const std::size_t _M_max_size = sizeof(_Nocopy_types); 01873 static const std::size_t _M_max_align = __alignof__(_Nocopy_types); 01874 01875 template<typename _Functor> 01876 class _Base_manager 01877 { 01878 protected: 01879 static const bool __stored_locally = 01880 (__is_location_invariant<_Functor>::value 01881 && sizeof(_Functor) <= _M_max_size 01882 && __alignof__(_Functor) <= _M_max_align 01883 && (_M_max_align % __alignof__(_Functor) == 0)); 01884 01885 typedef integral_constant<bool, __stored_locally> _Local_storage; 01886 01887 // Retrieve a pointer to the function object 01888 static _Functor* 01889 _M_get_pointer(const _Any_data& __source) 01890 { 01891 const _Functor* __ptr = 01892 __stored_locally? std::__addressof(__source._M_access<_Functor>()) 01893 /* have stored a pointer */ : __source._M_access<_Functor*>(); 01894 return const_cast<_Functor*>(__ptr); 01895 } 01896 01897 // Clone a location-invariant function object that fits within 01898 // an _Any_data structure. 01899 static void 01900 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type) 01901 { 01902 new (__dest._M_access()) _Functor(__source._M_access<_Functor>()); 01903 } 01904 01905 // Clone a function object that is not location-invariant or 01906 // that cannot fit into an _Any_data structure. 01907 static void 01908 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type) 01909 { 01910 __dest._M_access<_Functor*>() = 01911 new _Functor(*__source._M_access<_Functor*>()); 01912 } 01913 01914 // Destroying a location-invariant object may still require 01915 // destruction. 01916 static void 01917 _M_destroy(_Any_data& __victim, true_type) 01918 { 01919 __victim._M_access<_Functor>().~_Functor(); 01920 } 01921 01922 // Destroying an object located on the heap. 01923 static void 01924 _M_destroy(_Any_data& __victim, false_type) 01925 { 01926 delete __victim._M_access<_Functor*>(); 01927 } 01928 01929 public: 01930 static bool 01931 _M_manager(_Any_data& __dest, const _Any_data& __source, 01932 _Manager_operation __op) 01933 { 01934 switch (__op) 01935 { 01936 #ifdef __GXX_RTTI 01937 case __get_type_info: 01938 __dest._M_access<const type_info*>() = &typeid(_Functor); 01939 break; 01940 #endif 01941 case __get_functor_ptr: 01942 __dest._M_access<_Functor*>() = _M_get_pointer(__source); 01943 break; 01944 01945 case __clone_functor: 01946 _M_clone(__dest, __source, _Local_storage()); 01947 break; 01948 01949 case __destroy_functor: 01950 _M_destroy(__dest, _Local_storage()); 01951 break; 01952 } 01953 return false; 01954 } 01955 01956 static void 01957 _M_init_functor(_Any_data& __functor, _Functor&& __f) 01958 { _M_init_functor(__functor, std::move(__f), _Local_storage()); } 01959 01960 template<typename _Signature> 01961 static bool 01962 _M_not_empty_function(const function<_Signature>& __f) 01963 { return static_cast<bool>(__f); } 01964 01965 template<typename _Tp> 01966 static bool 01967 _M_not_empty_function(const _Tp*& __fp) 01968 { return __fp; } 01969 01970 template<typename _Class, typename _Tp> 01971 static bool 01972 _M_not_empty_function(_Tp _Class::* const& __mp) 01973 { return __mp; } 01974 01975 template<typename _Tp> 01976 static bool 01977 _M_not_empty_function(const _Tp&) 01978 { return true; } 01979 01980 private: 01981 static void 01982 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type) 01983 { new (__functor._M_access()) _Functor(std::move(__f)); } 01984 01985 static void 01986 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type) 01987 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); } 01988 }; 01989 01990 template<typename _Functor> 01991 class _Ref_manager : public _Base_manager<_Functor*> 01992 { 01993 typedef _Function_base::_Base_manager<_Functor*> _Base; 01994 01995 public: 01996 static bool 01997 _M_manager(_Any_data& __dest, const _Any_data& __source, 01998 _Manager_operation __op) 01999 { 02000 switch (__op) 02001 { 02002 #ifdef __GXX_RTTI 02003 case __get_type_info: 02004 __dest._M_access<const type_info*>() = &typeid(_Functor); 02005 break; 02006 #endif 02007 case __get_functor_ptr: 02008 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source); 02009 return is_const<_Functor>::value; 02010 break; 02011 02012 default: 02013 _Base::_M_manager(__dest, __source, __op); 02014 } 02015 return false; 02016 } 02017 02018 static void 02019 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f) 02020 { 02021 _Base::_M_init_functor(__functor, std::__addressof(__f.get())); 02022 } 02023 }; 02024 02025 _Function_base() : _M_manager(0) { } 02026 02027 ~_Function_base() 02028 { 02029 if (_M_manager) 02030 _M_manager(_M_functor, _M_functor, __destroy_functor); 02031 } 02032 02033 02034 bool _M_empty() const { return !_M_manager; } 02035 02036 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&, 02037 _Manager_operation); 02038 02039 _Any_data _M_functor; 02040 _Manager_type _M_manager; 02041 }; 02042 02043 template<typename _Signature, typename _Functor> 02044 class _Function_handler; 02045 02046 template<typename _Res, typename _Functor, typename... _ArgTypes> 02047 class _Function_handler<_Res(_ArgTypes...), _Functor> 02048 : public _Function_base::_Base_manager<_Functor> 02049 { 02050 typedef _Function_base::_Base_manager<_Functor> _Base; 02051 02052 public: 02053 static _Res 02054 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 02055 { 02056 return (*_Base::_M_get_pointer(__functor))( 02057 std::forward<_ArgTypes>(__args)...); 02058 } 02059 }; 02060 02061 template<typename _Functor, typename... _ArgTypes> 02062 class _Function_handler<void(_ArgTypes...), _Functor> 02063 : public _Function_base::_Base_manager<_Functor> 02064 { 02065 typedef _Function_base::_Base_manager<_Functor> _Base; 02066 02067 public: 02068 static void 02069 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 02070 { 02071 (*_Base::_M_get_pointer(__functor))( 02072 std::forward<_ArgTypes>(__args)...); 02073 } 02074 }; 02075 02076 template<typename _Res, typename _Functor, typename... _ArgTypes> 02077 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> > 02078 : public _Function_base::_Ref_manager<_Functor> 02079 { 02080 typedef _Function_base::_Ref_manager<_Functor> _Base; 02081 02082 public: 02083 static _Res 02084 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 02085 { 02086 return __callable_functor(**_Base::_M_get_pointer(__functor))( 02087 std::forward<_ArgTypes>(__args)...); 02088 } 02089 }; 02090 02091 template<typename _Functor, typename... _ArgTypes> 02092 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> > 02093 : public _Function_base::_Ref_manager<_Functor> 02094 { 02095 typedef _Function_base::_Ref_manager<_Functor> _Base; 02096 02097 public: 02098 static void 02099 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 02100 { 02101 __callable_functor(**_Base::_M_get_pointer(__functor))( 02102 std::forward<_ArgTypes>(__args)...); 02103 } 02104 }; 02105 02106 template<typename _Class, typename _Member, typename _Res, 02107 typename... _ArgTypes> 02108 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*> 02109 : public _Function_handler<void(_ArgTypes...), _Member _Class::*> 02110 { 02111 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*> 02112 _Base; 02113 02114 public: 02115 static _Res 02116 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 02117 { 02118 return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)( 02119 std::forward<_ArgTypes>(__args)...); 02120 } 02121 }; 02122 02123 template<typename _Class, typename _Member, typename... _ArgTypes> 02124 class _Function_handler<void(_ArgTypes...), _Member _Class::*> 02125 : public _Function_base::_Base_manager< 02126 _Simple_type_wrapper< _Member _Class::* > > 02127 { 02128 typedef _Member _Class::* _Functor; 02129 typedef _Simple_type_wrapper<_Functor> _Wrapper; 02130 typedef _Function_base::_Base_manager<_Wrapper> _Base; 02131 02132 public: 02133 static bool 02134 _M_manager(_Any_data& __dest, const _Any_data& __source, 02135 _Manager_operation __op) 02136 { 02137 switch (__op) 02138 { 02139 #ifdef __GXX_RTTI 02140 case __get_type_info: 02141 __dest._M_access<const type_info*>() = &typeid(_Functor); 02142 break; 02143 #endif 02144 case __get_functor_ptr: 02145 __dest._M_access<_Functor*>() = 02146 &_Base::_M_get_pointer(__source)->__value; 02147 break; 02148 02149 default: 02150 _Base::_M_manager(__dest, __source, __op); 02151 } 02152 return false; 02153 } 02154 02155 static void 02156 _M_invoke(const _Any_data& __functor, _ArgTypes... __args) 02157 { 02158 std::mem_fn(_Base::_M_get_pointer(__functor)->__value)( 02159 std::forward<_ArgTypes>(__args)...); 02160 } 02161 }; 02162 02163 /** 02164 * @brief Primary class template for std::function. 02165 * @ingroup functors 02166 * 02167 * Polymorphic function wrapper. 02168 */ 02169 template<typename _Res, typename... _ArgTypes> 02170 class function<_Res(_ArgTypes...)> 02171 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>, 02172 private _Function_base 02173 { 02174 typedef _Res _Signature_type(_ArgTypes...); 02175 02176 template<typename _Functor> 02177 using _Invoke = decltype(__callable_functor(std::declval<_Functor&>()) 02178 (std::declval<_ArgTypes>()...) ); 02179 02180 template<typename _CallRes, typename _Res1> 02181 struct _CheckResult 02182 : is_convertible<_CallRes, _Res1> { }; 02183 02184 template<typename _CallRes> 02185 struct _CheckResult<_CallRes, void> 02186 : true_type { }; 02187 02188 template<typename _Functor> 02189 using _Callable = _CheckResult<_Invoke<_Functor>, _Res>; 02190 02191 template<typename _Cond, typename _Tp> 02192 using _Requires = typename enable_if<_Cond::value, _Tp>::type; 02193 02194 public: 02195 typedef _Res result_type; 02196 02197 // [3.7.2.1] construct/copy/destroy 02198 02199 /** 02200 * @brief Default construct creates an empty function call wrapper. 02201 * @post @c !(bool)*this 02202 */ 02203 function() noexcept 02204 : _Function_base() { } 02205 02206 /** 02207 * @brief Creates an empty function call wrapper. 02208 * @post @c !(bool)*this 02209 */ 02210 function(nullptr_t) noexcept 02211 : _Function_base() { } 02212 02213 /** 02214 * @brief %Function copy constructor. 02215 * @param __x A %function object with identical call signature. 02216 * @post @c bool(*this) == bool(__x) 02217 * 02218 * The newly-created %function contains a copy of the target of @a 02219 * __x (if it has one). 02220 */ 02221 function(const function& __x); 02222 02223 /** 02224 * @brief %Function move constructor. 02225 * @param __x A %function object rvalue with identical call signature. 02226 * 02227 * The newly-created %function contains the target of @a __x 02228 * (if it has one). 02229 */ 02230 function(function&& __x) : _Function_base() 02231 { 02232 __x.swap(*this); 02233 } 02234 02235 // TODO: needs allocator_arg_t 02236 02237 /** 02238 * @brief Builds a %function that targets a copy of the incoming 02239 * function object. 02240 * @param __f A %function object that is callable with parameters of 02241 * type @c T1, @c T2, ..., @c TN and returns a value convertible 02242 * to @c Res. 02243 * 02244 * The newly-created %function object will target a copy of 02245 * @a __f. If @a __f is @c reference_wrapper<F>, then this function 02246 * object will contain a reference to the function object @c 02247 * __f.get(). If @a __f is a NULL function pointer or NULL 02248 * pointer-to-member, the newly-created object will be empty. 02249 * 02250 * If @a __f is a non-NULL function pointer or an object of type @c 02251 * reference_wrapper<F>, this function will not throw. 02252 */ 02253 template<typename _Functor, 02254 typename = _Requires<_Callable<_Functor>, void>> 02255 function(_Functor); 02256 02257 /** 02258 * @brief %Function assignment operator. 02259 * @param __x A %function with identical call signature. 02260 * @post @c (bool)*this == (bool)x 02261 * @returns @c *this 02262 * 02263 * The target of @a __x is copied to @c *this. If @a __x has no 02264 * target, then @c *this will be empty. 02265 * 02266 * If @a __x targets a function pointer or a reference to a function 02267 * object, then this operation will not throw an %exception. 02268 */ 02269 function& 02270 operator=(const function& __x) 02271 { 02272 function(__x).swap(*this); 02273 return *this; 02274 } 02275 02276 /** 02277 * @brief %Function move-assignment operator. 02278 * @param __x A %function rvalue with identical call signature. 02279 * @returns @c *this 02280 * 02281 * The target of @a __x is moved to @c *this. If @a __x has no 02282 * target, then @c *this will be empty. 02283 * 02284 * If @a __x targets a function pointer or a reference to a function 02285 * object, then this operation will not throw an %exception. 02286 */ 02287 function& 02288 operator=(function&& __x) 02289 { 02290 function(std::move(__x)).swap(*this); 02291 return *this; 02292 } 02293 02294 /** 02295 * @brief %Function assignment to zero. 02296 * @post @c !(bool)*this 02297 * @returns @c *this 02298 * 02299 * The target of @c *this is deallocated, leaving it empty. 02300 */ 02301 function& 02302 operator=(nullptr_t) 02303 { 02304 if (_M_manager) 02305 { 02306 _M_manager(_M_functor, _M_functor, __destroy_functor); 02307 _M_manager = 0; 02308 _M_invoker = 0; 02309 } 02310 return *this; 02311 } 02312 02313 /** 02314 * @brief %Function assignment to a new target. 02315 * @param __f A %function object that is callable with parameters of 02316 * type @c T1, @c T2, ..., @c TN and returns a value convertible 02317 * to @c Res. 02318 * @return @c *this 02319 * 02320 * This %function object wrapper will target a copy of @a 02321 * __f. If @a __f is @c reference_wrapper<F>, then this function 02322 * object will contain a reference to the function object @c 02323 * __f.get(). If @a __f is a NULL function pointer or NULL 02324 * pointer-to-member, @c this object will be empty. 02325 * 02326 * If @a __f is a non-NULL function pointer or an object of type @c 02327 * reference_wrapper<F>, this function will not throw. 02328 */ 02329 template<typename _Functor> 02330 _Requires<_Callable<_Functor>, function&> 02331 operator=(_Functor&& __f) 02332 { 02333 function(std::forward<_Functor>(__f)).swap(*this); 02334 return *this; 02335 } 02336 02337 /// @overload 02338 template<typename _Functor> 02339 function& 02340 operator=(reference_wrapper<_Functor> __f) noexcept 02341 { 02342 function(__f).swap(*this); 02343 return *this; 02344 } 02345 02346 // [3.7.2.2] function modifiers 02347 02348 /** 02349 * @brief Swap the targets of two %function objects. 02350 * @param __x A %function with identical call signature. 02351 * 02352 * Swap the targets of @c this function object and @a __f. This 02353 * function will not throw an %exception. 02354 */ 02355 void swap(function& __x) 02356 { 02357 std::swap(_M_functor, __x._M_functor); 02358 std::swap(_M_manager, __x._M_manager); 02359 std::swap(_M_invoker, __x._M_invoker); 02360 } 02361 02362 // TODO: needs allocator_arg_t 02363 /* 02364 template<typename _Functor, typename _Alloc> 02365 void 02366 assign(_Functor&& __f, const _Alloc& __a) 02367 { 02368 function(allocator_arg, __a, 02369 std::forward<_Functor>(__f)).swap(*this); 02370 } 02371 */ 02372 02373 // [3.7.2.3] function capacity 02374 02375 /** 02376 * @brief Determine if the %function wrapper has a target. 02377 * 02378 * @return @c true when this %function object contains a target, 02379 * or @c false when it is empty. 02380 * 02381 * This function will not throw an %exception. 02382 */ 02383 explicit operator bool() const noexcept 02384 { return !_M_empty(); } 02385 02386 // [3.7.2.4] function invocation 02387 02388 /** 02389 * @brief Invokes the function targeted by @c *this. 02390 * @returns the result of the target. 02391 * @throws bad_function_call when @c !(bool)*this 02392 * 02393 * The function call operator invokes the target function object 02394 * stored by @c this. 02395 */ 02396 _Res operator()(_ArgTypes... __args) const; 02397 02398 #ifdef __GXX_RTTI 02399 // [3.7.2.5] function target access 02400 /** 02401 * @brief Determine the type of the target of this function object 02402 * wrapper. 02403 * 02404 * @returns the type identifier of the target function object, or 02405 * @c typeid(void) if @c !(bool)*this. 02406 * 02407 * This function will not throw an %exception. 02408 */ 02409 const type_info& target_type() const noexcept; 02410 02411 /** 02412 * @brief Access the stored target function object. 02413 * 02414 * @return Returns a pointer to the stored target function object, 02415 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL 02416 * pointer. 02417 * 02418 * This function will not throw an %exception. 02419 */ 02420 template<typename _Functor> _Functor* target() noexcept; 02421 02422 /// @overload 02423 template<typename _Functor> const _Functor* target() const noexcept; 02424 #endif 02425 02426 private: 02427 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...); 02428 _Invoker_type _M_invoker; 02429 }; 02430 02431 // Out-of-line member definitions. 02432 template<typename _Res, typename... _ArgTypes> 02433 function<_Res(_ArgTypes...)>:: 02434 function(const function& __x) 02435 : _Function_base() 02436 { 02437 if (static_cast<bool>(__x)) 02438 { 02439 _M_invoker = __x._M_invoker; 02440 _M_manager = __x._M_manager; 02441 __x._M_manager(_M_functor, __x._M_functor, __clone_functor); 02442 } 02443 } 02444 02445 template<typename _Res, typename... _ArgTypes> 02446 template<typename _Functor, typename> 02447 function<_Res(_ArgTypes...)>:: 02448 function(_Functor __f) 02449 : _Function_base() 02450 { 02451 typedef _Function_handler<_Signature_type, _Functor> _My_handler; 02452 02453 if (_My_handler::_M_not_empty_function(__f)) 02454 { 02455 _My_handler::_M_init_functor(_M_functor, std::move(__f)); 02456 _M_invoker = &_My_handler::_M_invoke; 02457 _M_manager = &_My_handler::_M_manager; 02458 } 02459 } 02460 02461 template<typename _Res, typename... _ArgTypes> 02462 _Res 02463 function<_Res(_ArgTypes...)>:: 02464 operator()(_ArgTypes... __args) const 02465 { 02466 if (_M_empty()) 02467 __throw_bad_function_call(); 02468 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...); 02469 } 02470 02471 #ifdef __GXX_RTTI 02472 template<typename _Res, typename... _ArgTypes> 02473 const type_info& 02474 function<_Res(_ArgTypes...)>:: 02475 target_type() const noexcept 02476 { 02477 if (_M_manager) 02478 { 02479 _Any_data __typeinfo_result; 02480 _M_manager(__typeinfo_result, _M_functor, __get_type_info); 02481 return *__typeinfo_result._M_access<const type_info*>(); 02482 } 02483 else 02484 return typeid(void); 02485 } 02486 02487 template<typename _Res, typename... _ArgTypes> 02488 template<typename _Functor> 02489 _Functor* 02490 function<_Res(_ArgTypes...)>:: 02491 target() noexcept 02492 { 02493 if (typeid(_Functor) == target_type() && _M_manager) 02494 { 02495 _Any_data __ptr; 02496 if (_M_manager(__ptr, _M_functor, __get_functor_ptr) 02497 && !is_const<_Functor>::value) 02498 return 0; 02499 else 02500 return __ptr._M_access<_Functor*>(); 02501 } 02502 else 02503 return 0; 02504 } 02505 02506 template<typename _Res, typename... _ArgTypes> 02507 template<typename _Functor> 02508 const _Functor* 02509 function<_Res(_ArgTypes...)>:: 02510 target() const noexcept 02511 { 02512 if (typeid(_Functor) == target_type() && _M_manager) 02513 { 02514 _Any_data __ptr; 02515 _M_manager(__ptr, _M_functor, __get_functor_ptr); 02516 return __ptr._M_access<const _Functor*>(); 02517 } 02518 else 02519 return 0; 02520 } 02521 #endif 02522 02523 // [20.7.15.2.6] null pointer comparisons 02524 02525 /** 02526 * @brief Compares a polymorphic function object wrapper against 0 02527 * (the NULL pointer). 02528 * @returns @c true if the wrapper has no target, @c false otherwise 02529 * 02530 * This function will not throw an %exception. 02531 */ 02532 template<typename _Res, typename... _Args> 02533 inline bool 02534 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept 02535 { return !static_cast<bool>(__f); } 02536 02537 /// @overload 02538 template<typename _Res, typename... _Args> 02539 inline bool 02540 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept 02541 { return !static_cast<bool>(__f); } 02542 02543 /** 02544 * @brief Compares a polymorphic function object wrapper against 0 02545 * (the NULL pointer). 02546 * @returns @c false if the wrapper has no target, @c true otherwise 02547 * 02548 * This function will not throw an %exception. 02549 */ 02550 template<typename _Res, typename... _Args> 02551 inline bool 02552 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept 02553 { return static_cast<bool>(__f); } 02554 02555 /// @overload 02556 template<typename _Res, typename... _Args> 02557 inline bool 02558 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept 02559 { return static_cast<bool>(__f); } 02560 02561 // [20.7.15.2.7] specialized algorithms 02562 02563 /** 02564 * @brief Swap the targets of two polymorphic function object wrappers. 02565 * 02566 * This function will not throw an %exception. 02567 */ 02568 template<typename _Res, typename... _Args> 02569 inline void 02570 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) 02571 { __x.swap(__y); } 02572 02573 _GLIBCXX_END_NAMESPACE_VERSION 02574 } // namespace std 02575 02576 #endif // C++11 02577 02578 #endif // _GLIBCXX_FUNCTIONAL