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