Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame^] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_FUNCTIONAL_03 |
| 12 | #define _LIBCPP_FUNCTIONAL_03 |
| 13 | |
| 14 | // manual variadic expansion for <functional> |
| 15 | |
| 16 | #pragma GCC system_header |
| 17 | |
| 18 | template <class _Tp> |
| 19 | class __mem_fn |
| 20 | : public __weak_result_type<_Tp> |
| 21 | { |
| 22 | public: |
| 23 | // types |
| 24 | typedef _Tp type; |
| 25 | private: |
| 26 | type __f_; |
| 27 | |
| 28 | public: |
| 29 | _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {} |
| 30 | |
| 31 | // invoke |
| 32 | |
| 33 | typename __invoke_return<type>::type |
| 34 | operator() () |
| 35 | { |
| 36 | return __invoke(__f_); |
| 37 | } |
| 38 | |
| 39 | template <class _A0> |
| 40 | typename __invoke_return0<type, _A0>::type |
| 41 | operator() (_A0& __a0) |
| 42 | { |
| 43 | return __invoke(__f_, __a0); |
| 44 | } |
| 45 | |
| 46 | template <class _A0, class _A1> |
| 47 | typename __invoke_return1<type, _A0, _A1>::type |
| 48 | operator() (_A0& __a0, _A1& __a1) |
| 49 | { |
| 50 | return __invoke(__f_, __a0, __a1); |
| 51 | } |
| 52 | |
| 53 | template <class _A0, class _A1, class _A2> |
| 54 | typename __invoke_return2<type, _A0, _A1, _A2>::type |
| 55 | operator() (_A0& __a0, _A1& __a1, _A2& __a2) |
| 56 | { |
| 57 | return __invoke(__f_, __a0, __a1, __a2); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | template<class _R, class _T> |
| 62 | inline _LIBCPP_INLINE_VISIBILITY |
| 63 | __mem_fn<_R _T::*> |
| 64 | mem_fn(_R _T::* __pm) |
| 65 | { |
| 66 | return __mem_fn<_R _T::*>(__pm); |
| 67 | } |
| 68 | |
| 69 | template<class _R, class _T> |
| 70 | inline _LIBCPP_INLINE_VISIBILITY |
| 71 | __mem_fn<_R (_T::*)()> |
| 72 | mem_fn(_R (_T::* __pm)()) |
| 73 | { |
| 74 | return __mem_fn<_R (_T::*)()>(__pm); |
| 75 | } |
| 76 | |
| 77 | template<class _R, class _T, class _A0> |
| 78 | inline _LIBCPP_INLINE_VISIBILITY |
| 79 | __mem_fn<_R (_T::*)(_A0)> |
| 80 | mem_fn(_R (_T::* __pm)(_A0)) |
| 81 | { |
| 82 | return __mem_fn<_R (_T::*)(_A0)>(__pm); |
| 83 | } |
| 84 | |
| 85 | template<class _R, class _T, class _A0, class _A1> |
| 86 | inline _LIBCPP_INLINE_VISIBILITY |
| 87 | __mem_fn<_R (_T::*)(_A0, _A1)> |
| 88 | mem_fn(_R (_T::* __pm)(_A0, _A1)) |
| 89 | { |
| 90 | return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm); |
| 91 | } |
| 92 | |
| 93 | template<class _R, class _T, class _A0, class _A1, class _A2> |
| 94 | inline _LIBCPP_INLINE_VISIBILITY |
| 95 | __mem_fn<_R (_T::*)(_A0, _A1, _A2)> |
| 96 | mem_fn(_R (_T::* __pm)(_A0, _A1, _A2)) |
| 97 | { |
| 98 | return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm); |
| 99 | } |
| 100 | |
| 101 | template<class _R, class _T> |
| 102 | inline _LIBCPP_INLINE_VISIBILITY |
| 103 | __mem_fn<_R (_T::*)()> |
| 104 | mem_fn(_R (_T::* __pm)() const) |
| 105 | { |
| 106 | return __mem_fn<_R (_T::*)()>(__pm); |
| 107 | } |
| 108 | |
| 109 | template<class _R, class _T, class _A0> |
| 110 | inline _LIBCPP_INLINE_VISIBILITY |
| 111 | __mem_fn<_R (_T::*)(_A0)> |
| 112 | mem_fn(_R (_T::* __pm)(_A0) const) |
| 113 | { |
| 114 | return __mem_fn<_R (_T::*)(_A0)>(__pm); |
| 115 | } |
| 116 | |
| 117 | template<class _R, class _T, class _A0, class _A1> |
| 118 | inline _LIBCPP_INLINE_VISIBILITY |
| 119 | __mem_fn<_R (_T::*)(_A0, _A1)> |
| 120 | mem_fn(_R (_T::* __pm)(_A0, _A1) const) |
| 121 | { |
| 122 | return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm); |
| 123 | } |
| 124 | |
| 125 | template<class _R, class _T, class _A0, class _A1, class _A2> |
| 126 | inline _LIBCPP_INLINE_VISIBILITY |
| 127 | __mem_fn<_R (_T::*)(_A0, _A1, _A2)> |
| 128 | mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const) |
| 129 | { |
| 130 | return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm); |
| 131 | } |
| 132 | |
| 133 | template<class _R, class _T> |
| 134 | inline _LIBCPP_INLINE_VISIBILITY |
| 135 | __mem_fn<_R (_T::*)()> |
| 136 | mem_fn(_R (_T::* __pm)() volatile) |
| 137 | { |
| 138 | return __mem_fn<_R (_T::*)()>(__pm); |
| 139 | } |
| 140 | |
| 141 | template<class _R, class _T, class _A0> |
| 142 | inline _LIBCPP_INLINE_VISIBILITY |
| 143 | __mem_fn<_R (_T::*)(_A0)> |
| 144 | mem_fn(_R (_T::* __pm)(_A0) volatile) |
| 145 | { |
| 146 | return __mem_fn<_R (_T::*)(_A0)>(__pm); |
| 147 | } |
| 148 | |
| 149 | template<class _R, class _T, class _A0, class _A1> |
| 150 | inline _LIBCPP_INLINE_VISIBILITY |
| 151 | __mem_fn<_R (_T::*)(_A0, _A1)> |
| 152 | mem_fn(_R (_T::* __pm)(_A0, _A1) volatile) |
| 153 | { |
| 154 | return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm); |
| 155 | } |
| 156 | |
| 157 | template<class _R, class _T, class _A0, class _A1, class _A2> |
| 158 | inline _LIBCPP_INLINE_VISIBILITY |
| 159 | __mem_fn<_R (_T::*)(_A0, _A1, _A2)> |
| 160 | mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) volatile) |
| 161 | { |
| 162 | return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm); |
| 163 | } |
| 164 | |
| 165 | template<class _R, class _T> |
| 166 | inline _LIBCPP_INLINE_VISIBILITY |
| 167 | __mem_fn<_R (_T::*)()> |
| 168 | mem_fn(_R (_T::* __pm)() const volatile) |
| 169 | { |
| 170 | return __mem_fn<_R (_T::*)()>(__pm); |
| 171 | } |
| 172 | |
| 173 | template<class _R, class _T, class _A0> |
| 174 | inline _LIBCPP_INLINE_VISIBILITY |
| 175 | __mem_fn<_R (_T::*)(_A0)> |
| 176 | mem_fn(_R (_T::* __pm)(_A0) const volatile) |
| 177 | { |
| 178 | return __mem_fn<_R (_T::*)(_A0)>(__pm); |
| 179 | } |
| 180 | |
| 181 | template<class _R, class _T, class _A0, class _A1> |
| 182 | inline _LIBCPP_INLINE_VISIBILITY |
| 183 | __mem_fn<_R (_T::*)(_A0, _A1)> |
| 184 | mem_fn(_R (_T::* __pm)(_A0, _A1) const volatile) |
| 185 | { |
| 186 | return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm); |
| 187 | } |
| 188 | |
| 189 | template<class _R, class _T, class _A0, class _A1, class _A2> |
| 190 | inline _LIBCPP_INLINE_VISIBILITY |
| 191 | __mem_fn<_R (_T::*)(_A0, _A1, _A2)> |
| 192 | mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const volatile) |
| 193 | { |
| 194 | return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm); |
| 195 | } |
| 196 | |
| 197 | // bad_function_call |
| 198 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 199 | class _LIBCPP_EXCEPTION_ABI bad_function_call |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 200 | : public exception |
| 201 | { |
| 202 | }; |
| 203 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 204 | template<class _Fp> class _LIBCPP_VISIBLE function; // undefined |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 205 | |
| 206 | namespace __function |
| 207 | { |
| 208 | |
| 209 | template<class _F> |
| 210 | struct __maybe_derive_from_unary_function |
| 211 | { |
| 212 | }; |
| 213 | |
| 214 | template<class _R, class _A1> |
| 215 | struct __maybe_derive_from_unary_function<_R(_A1)> |
| 216 | : public unary_function<_A1, _R> |
| 217 | { |
| 218 | }; |
| 219 | |
| 220 | template<class _F> |
| 221 | struct __maybe_derive_from_binary_function |
| 222 | { |
| 223 | }; |
| 224 | |
| 225 | template<class _R, class _A1, class _A2> |
| 226 | struct __maybe_derive_from_binary_function<_R(_A1, _A2)> |
| 227 | : public binary_function<_A1, _A2, _R> |
| 228 | { |
| 229 | }; |
| 230 | |
| 231 | template<class _Fp> class __base; |
| 232 | |
| 233 | template<class _R> |
| 234 | class __base<_R()> |
| 235 | { |
| 236 | __base(const __base&); |
| 237 | __base& operator=(const __base&); |
| 238 | public: |
| 239 | __base() {} |
| 240 | virtual ~__base() {} |
| 241 | virtual __base* __clone() const = 0; |
| 242 | virtual void __clone(__base*) const = 0; |
| 243 | virtual void destroy() = 0; |
| 244 | virtual void destroy_deallocate() = 0; |
| 245 | virtual _R operator()() = 0; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 246 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 247 | virtual const void* target(const type_info&) const = 0; |
| 248 | virtual const std::type_info& target_type() const = 0; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 249 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | template<class _R, class _A0> |
| 253 | class __base<_R(_A0)> |
| 254 | { |
| 255 | __base(const __base&); |
| 256 | __base& operator=(const __base&); |
| 257 | public: |
| 258 | __base() {} |
| 259 | virtual ~__base() {} |
| 260 | virtual __base* __clone() const = 0; |
| 261 | virtual void __clone(__base*) const = 0; |
| 262 | virtual void destroy() = 0; |
| 263 | virtual void destroy_deallocate() = 0; |
| 264 | virtual _R operator()(_A0) = 0; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 265 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 266 | virtual const void* target(const type_info&) const = 0; |
| 267 | virtual const std::type_info& target_type() const = 0; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 268 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | template<class _R, class _A0, class _A1> |
| 272 | class __base<_R(_A0, _A1)> |
| 273 | { |
| 274 | __base(const __base&); |
| 275 | __base& operator=(const __base&); |
| 276 | public: |
| 277 | __base() {} |
| 278 | virtual ~__base() {} |
| 279 | virtual __base* __clone() const = 0; |
| 280 | virtual void __clone(__base*) const = 0; |
| 281 | virtual void destroy() = 0; |
| 282 | virtual void destroy_deallocate() = 0; |
| 283 | virtual _R operator()(_A0, _A1) = 0; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 284 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 285 | virtual const void* target(const type_info&) const = 0; |
| 286 | virtual const std::type_info& target_type() const = 0; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 287 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | template<class _R, class _A0, class _A1, class _A2> |
| 291 | class __base<_R(_A0, _A1, _A2)> |
| 292 | { |
| 293 | __base(const __base&); |
| 294 | __base& operator=(const __base&); |
| 295 | public: |
| 296 | __base() {} |
| 297 | virtual ~__base() {} |
| 298 | virtual __base* __clone() const = 0; |
| 299 | virtual void __clone(__base*) const = 0; |
| 300 | virtual void destroy() = 0; |
| 301 | virtual void destroy_deallocate() = 0; |
| 302 | virtual _R operator()(_A0, _A1, _A2) = 0; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 303 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 304 | virtual const void* target(const type_info&) const = 0; |
| 305 | virtual const std::type_info& target_type() const = 0; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 306 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 307 | }; |
| 308 | |
| 309 | template<class _FD, class _Alloc, class _FB> class __func; |
| 310 | |
| 311 | template<class _F, class _Alloc, class _R> |
| 312 | class __func<_F, _Alloc, _R()> |
| 313 | : public __base<_R()> |
| 314 | { |
| 315 | __compressed_pair<_F, _Alloc> __f_; |
| 316 | public: |
| 317 | explicit __func(_F __f) : __f_(_STD::move(__f)) {} |
| 318 | explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {} |
| 319 | virtual __base<_R()>* __clone() const; |
| 320 | virtual void __clone(__base<_R()>*) const; |
| 321 | virtual void destroy(); |
| 322 | virtual void destroy_deallocate(); |
| 323 | virtual _R operator()(); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 324 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 325 | virtual const void* target(const type_info&) const; |
| 326 | virtual const std::type_info& target_type() const; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 327 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | template<class _F, class _Alloc, class _R> |
| 331 | __base<_R()>* |
| 332 | __func<_F, _Alloc, _R()>::__clone() const |
| 333 | { |
| 334 | typedef typename _Alloc::template rebind<__func>::other _A; |
| 335 | _A __a(__f_.second()); |
| 336 | typedef __allocator_destructor<_A> _D; |
| 337 | unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 338 | ::new (__hold.get()) __func(__f_.first(), _Alloc(__a)); |
| 339 | return __hold.release(); |
| 340 | } |
| 341 | |
| 342 | template<class _F, class _Alloc, class _R> |
| 343 | void |
| 344 | __func<_F, _Alloc, _R()>::__clone(__base<_R()>* __p) const |
| 345 | { |
| 346 | ::new (__p) __func(__f_.first(), __f_.second()); |
| 347 | } |
| 348 | |
| 349 | template<class _F, class _Alloc, class _R> |
| 350 | void |
| 351 | __func<_F, _Alloc, _R()>::destroy() |
| 352 | { |
| 353 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 354 | } |
| 355 | |
| 356 | template<class _F, class _Alloc, class _R> |
| 357 | void |
| 358 | __func<_F, _Alloc, _R()>::destroy_deallocate() |
| 359 | { |
| 360 | typedef typename _Alloc::template rebind<__func>::other _A; |
| 361 | _A __a(__f_.second()); |
| 362 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 363 | __a.deallocate(this, 1); |
| 364 | } |
| 365 | |
| 366 | template<class _F, class _Alloc, class _R> |
| 367 | _R |
| 368 | __func<_F, _Alloc, _R()>::operator()() |
| 369 | { |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 370 | return __invoke(__f_.first()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 373 | #ifndef _LIBCPP_NO_RTTI |
| 374 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 375 | template<class _F, class _Alloc, class _R> |
| 376 | const void* |
| 377 | __func<_F, _Alloc, _R()>::target(const type_info& __ti) const |
| 378 | { |
| 379 | if (__ti == typeid(_F)) |
| 380 | return &__f_.first(); |
| 381 | return (const void*)0; |
| 382 | } |
| 383 | |
| 384 | template<class _F, class _Alloc, class _R> |
| 385 | const std::type_info& |
| 386 | __func<_F, _Alloc, _R()>::target_type() const |
| 387 | { |
| 388 | return typeid(_F); |
| 389 | } |
| 390 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 391 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 392 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 393 | template<class _F, class _Alloc, class _R, class _A0> |
| 394 | class __func<_F, _Alloc, _R(_A0)> |
| 395 | : public __base<_R(_A0)> |
| 396 | { |
| 397 | __compressed_pair<_F, _Alloc> __f_; |
| 398 | public: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 399 | _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_STD::move(__f)) {} |
| 400 | _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a) |
| 401 | : __f_(_STD::move(__f), _STD::move(__a)) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 402 | virtual __base<_R(_A0)>* __clone() const; |
| 403 | virtual void __clone(__base<_R(_A0)>*) const; |
| 404 | virtual void destroy(); |
| 405 | virtual void destroy_deallocate(); |
| 406 | virtual _R operator()(_A0); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 407 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 408 | virtual const void* target(const type_info&) const; |
| 409 | virtual const std::type_info& target_type() const; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 410 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 411 | }; |
| 412 | |
| 413 | template<class _F, class _Alloc, class _R, class _A0> |
| 414 | __base<_R(_A0)>* |
| 415 | __func<_F, _Alloc, _R(_A0)>::__clone() const |
| 416 | { |
| 417 | typedef typename _Alloc::template rebind<__func>::other _A; |
| 418 | _A __a(__f_.second()); |
| 419 | typedef __allocator_destructor<_A> _D; |
| 420 | unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 421 | ::new (__hold.get()) __func(__f_.first(), _Alloc(__a)); |
| 422 | return __hold.release(); |
| 423 | } |
| 424 | |
| 425 | template<class _F, class _Alloc, class _R, class _A0> |
| 426 | void |
| 427 | __func<_F, _Alloc, _R(_A0)>::__clone(__base<_R(_A0)>* __p) const |
| 428 | { |
| 429 | ::new (__p) __func(__f_.first(), __f_.second()); |
| 430 | } |
| 431 | |
| 432 | template<class _F, class _Alloc, class _R, class _A0> |
| 433 | void |
| 434 | __func<_F, _Alloc, _R(_A0)>::destroy() |
| 435 | { |
| 436 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 437 | } |
| 438 | |
| 439 | template<class _F, class _Alloc, class _R, class _A0> |
| 440 | void |
| 441 | __func<_F, _Alloc, _R(_A0)>::destroy_deallocate() |
| 442 | { |
| 443 | typedef typename _Alloc::template rebind<__func>::other _A; |
| 444 | _A __a(__f_.second()); |
| 445 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 446 | __a.deallocate(this, 1); |
| 447 | } |
| 448 | |
| 449 | template<class _F, class _Alloc, class _R, class _A0> |
| 450 | _R |
| 451 | __func<_F, _Alloc, _R(_A0)>::operator()(_A0 __a0) |
| 452 | { |
| 453 | return __invoke(__f_.first(), __a0); |
| 454 | } |
| 455 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 456 | #ifndef _LIBCPP_NO_RTTI |
| 457 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 458 | template<class _F, class _Alloc, class _R, class _A0> |
| 459 | const void* |
| 460 | __func<_F, _Alloc, _R(_A0)>::target(const type_info& __ti) const |
| 461 | { |
| 462 | if (__ti == typeid(_F)) |
| 463 | return &__f_.first(); |
| 464 | return (const void*)0; |
| 465 | } |
| 466 | |
| 467 | template<class _F, class _Alloc, class _R, class _A0> |
| 468 | const std::type_info& |
| 469 | __func<_F, _Alloc, _R(_A0)>::target_type() const |
| 470 | { |
| 471 | return typeid(_F); |
| 472 | } |
| 473 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 474 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 475 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 476 | template<class _F, class _Alloc, class _R, class _A0, class _A1> |
| 477 | class __func<_F, _Alloc, _R(_A0, _A1)> |
| 478 | : public __base<_R(_A0, _A1)> |
| 479 | { |
| 480 | __compressed_pair<_F, _Alloc> __f_; |
| 481 | public: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 482 | _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_STD::move(__f)) {} |
| 483 | _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a) |
| 484 | : __f_(_STD::move(__f), _STD::move(__a)) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 485 | virtual __base<_R(_A0, _A1)>* __clone() const; |
| 486 | virtual void __clone(__base<_R(_A0, _A1)>*) const; |
| 487 | virtual void destroy(); |
| 488 | virtual void destroy_deallocate(); |
| 489 | virtual _R operator()(_A0, _A1); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 490 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 491 | virtual const void* target(const type_info&) const; |
| 492 | virtual const std::type_info& target_type() const; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 493 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | template<class _F, class _Alloc, class _R, class _A0, class _A1> |
| 497 | __base<_R(_A0, _A1)>* |
| 498 | __func<_F, _Alloc, _R(_A0, _A1)>::__clone() const |
| 499 | { |
| 500 | typedef typename _Alloc::template rebind<__func>::other _A; |
| 501 | _A __a(__f_.second()); |
| 502 | typedef __allocator_destructor<_A> _D; |
| 503 | unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 504 | ::new (__hold.get()) __func(__f_.first(), _Alloc(__a)); |
| 505 | return __hold.release(); |
| 506 | } |
| 507 | |
| 508 | template<class _F, class _Alloc, class _R, class _A0, class _A1> |
| 509 | void |
| 510 | __func<_F, _Alloc, _R(_A0, _A1)>::__clone(__base<_R(_A0, _A1)>* __p) const |
| 511 | { |
| 512 | ::new (__p) __func(__f_.first(), __f_.second()); |
| 513 | } |
| 514 | |
| 515 | template<class _F, class _Alloc, class _R, class _A0, class _A1> |
| 516 | void |
| 517 | __func<_F, _Alloc, _R(_A0, _A1)>::destroy() |
| 518 | { |
| 519 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 520 | } |
| 521 | |
| 522 | template<class _F, class _Alloc, class _R, class _A0, class _A1> |
| 523 | void |
| 524 | __func<_F, _Alloc, _R(_A0, _A1)>::destroy_deallocate() |
| 525 | { |
| 526 | typedef typename _Alloc::template rebind<__func>::other _A; |
| 527 | _A __a(__f_.second()); |
| 528 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 529 | __a.deallocate(this, 1); |
| 530 | } |
| 531 | |
| 532 | template<class _F, class _Alloc, class _R, class _A0, class _A1> |
| 533 | _R |
| 534 | __func<_F, _Alloc, _R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) |
| 535 | { |
| 536 | return __invoke(__f_.first(), __a0, __a1); |
| 537 | } |
| 538 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 539 | #ifndef _LIBCPP_NO_RTTI |
| 540 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 541 | template<class _F, class _Alloc, class _R, class _A0, class _A1> |
| 542 | const void* |
| 543 | __func<_F, _Alloc, _R(_A0, _A1)>::target(const type_info& __ti) const |
| 544 | { |
| 545 | if (__ti == typeid(_F)) |
| 546 | return &__f_.first(); |
| 547 | return (const void*)0; |
| 548 | } |
| 549 | |
| 550 | template<class _F, class _Alloc, class _R, class _A0, class _A1> |
| 551 | const std::type_info& |
| 552 | __func<_F, _Alloc, _R(_A0, _A1)>::target_type() const |
| 553 | { |
| 554 | return typeid(_F); |
| 555 | } |
| 556 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 557 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 558 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 559 | template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> |
| 560 | class __func<_F, _Alloc, _R(_A0, _A1, _A2)> |
| 561 | : public __base<_R(_A0, _A1, _A2)> |
| 562 | { |
| 563 | __compressed_pair<_F, _Alloc> __f_; |
| 564 | public: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 565 | _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_STD::move(__f)) {} |
| 566 | _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a) |
| 567 | : __f_(_STD::move(__f), _STD::move(__a)) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 568 | virtual __base<_R(_A0, _A1, _A2)>* __clone() const; |
| 569 | virtual void __clone(__base<_R(_A0, _A1, _A2)>*) const; |
| 570 | virtual void destroy(); |
| 571 | virtual void destroy_deallocate(); |
| 572 | virtual _R operator()(_A0, _A1, _A2); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 573 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 574 | virtual const void* target(const type_info&) const; |
| 575 | virtual const std::type_info& target_type() const; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 576 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> |
| 580 | __base<_R(_A0, _A1, _A2)>* |
| 581 | __func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone() const |
| 582 | { |
| 583 | typedef typename _Alloc::template rebind<__func>::other _A; |
| 584 | _A __a(__f_.second()); |
| 585 | typedef __allocator_destructor<_A> _D; |
| 586 | unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 587 | ::new (__hold.get()) __func(__f_.first(), _Alloc(__a)); |
| 588 | return __hold.release(); |
| 589 | } |
| 590 | |
| 591 | template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> |
| 592 | void |
| 593 | __func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone(__base<_R(_A0, _A1, _A2)>* __p) const |
| 594 | { |
| 595 | ::new (__p) __func(__f_.first(), __f_.second()); |
| 596 | } |
| 597 | |
| 598 | template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> |
| 599 | void |
| 600 | __func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy() |
| 601 | { |
| 602 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 603 | } |
| 604 | |
| 605 | template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> |
| 606 | void |
| 607 | __func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy_deallocate() |
| 608 | { |
| 609 | typedef typename _Alloc::template rebind<__func>::other _A; |
| 610 | _A __a(__f_.second()); |
| 611 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 612 | __a.deallocate(this, 1); |
| 613 | } |
| 614 | |
| 615 | template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> |
| 616 | _R |
| 617 | __func<_F, _Alloc, _R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) |
| 618 | { |
| 619 | return __invoke(__f_.first(), __a0, __a1, __a2); |
| 620 | } |
| 621 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 622 | #ifndef _LIBCPP_NO_RTTI |
| 623 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 624 | template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> |
| 625 | const void* |
| 626 | __func<_F, _Alloc, _R(_A0, _A1, _A2)>::target(const type_info& __ti) const |
| 627 | { |
| 628 | if (__ti == typeid(_F)) |
| 629 | return &__f_.first(); |
| 630 | return (const void*)0; |
| 631 | } |
| 632 | |
| 633 | template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> |
| 634 | const std::type_info& |
| 635 | __func<_F, _Alloc, _R(_A0, _A1, _A2)>::target_type() const |
| 636 | { |
| 637 | return typeid(_F); |
| 638 | } |
| 639 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 640 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 641 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 642 | } // __function |
| 643 | |
| 644 | template<class _R> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 645 | class _LIBCPP_VISIBLE function<_R()> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 646 | { |
| 647 | typedef __function::__base<_R()> __base; |
| 648 | aligned_storage<3*sizeof(void*)>::type __buf_; |
| 649 | __base* __f_; |
| 650 | |
| 651 | template <class _F> |
| 652 | static bool __not_null(const _F&) {return true;} |
| 653 | template <class _R2> |
| 654 | static bool __not_null(const function<_R()>& __p) {return __p;} |
| 655 | public: |
| 656 | typedef _R result_type; |
| 657 | |
| 658 | // 20.7.16.2.1, construct/copy/destroy: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 659 | _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} |
| 660 | _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 661 | function(const function&); |
| 662 | template<class _F> |
| 663 | function(_F, |
| 664 | typename enable_if<!is_integral<_F>::value>::type* = 0); |
| 665 | |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 666 | template<class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 667 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 668 | function(allocator_arg_t, const _Alloc&) : __f_(0) {} |
| 669 | template<class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 670 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 671 | function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} |
| 672 | template<class _Alloc> |
| 673 | function(allocator_arg_t, const _Alloc&, const function&); |
| 674 | template<class _F, class _Alloc> |
| 675 | function(allocator_arg_t, const _Alloc& __a, _F __f, |
| 676 | typename enable_if<!is_integral<_F>::value>::type* = 0); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 677 | |
| 678 | function& operator=(const function&); |
| 679 | function& operator=(nullptr_t); |
| 680 | template<class _F> |
| 681 | typename enable_if |
| 682 | < |
| 683 | !is_integral<_F>::value, |
| 684 | function& |
| 685 | >::type |
| 686 | operator=(_F); |
| 687 | |
| 688 | ~function(); |
| 689 | |
| 690 | // 20.7.16.2.2, function modifiers: |
| 691 | void swap(function&); |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 692 | template<class _F, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 693 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 694 | void assign(_F __f, const _Alloc& __a) |
| 695 | {function(allocator_arg, __a, __f).swap(*this);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 696 | |
| 697 | // 20.7.16.2.3, function capacity: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 698 | _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 699 | |
| 700 | private: |
| 701 | // deleted overloads close possible hole in the type system |
| 702 | template<class _R2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 703 | bool operator==(const function<_R2()>&) const;// = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 704 | template<class _R2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 705 | bool operator!=(const function<_R2()>&) const;// = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 706 | public: |
| 707 | // 20.7.16.2.4, function invocation: |
| 708 | _R operator()() const; |
| 709 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 710 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 711 | // 20.7.16.2.5, function target access: |
| 712 | const std::type_info& target_type() const; |
| 713 | template <typename _T> _T* target(); |
| 714 | template <typename _T> const _T* target() const; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 715 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 716 | }; |
| 717 | |
| 718 | template<class _R> |
| 719 | function<_R()>::function(const function& __f) |
| 720 | { |
| 721 | if (__f.__f_ == 0) |
| 722 | __f_ = 0; |
| 723 | else if (__f.__f_ == (const __base*)&__f.__buf_) |
| 724 | { |
| 725 | __f_ = (__base*)&__buf_; |
| 726 | __f.__f_->__clone(__f_); |
| 727 | } |
| 728 | else |
| 729 | __f_ = __f.__f_->__clone(); |
| 730 | } |
| 731 | |
| 732 | template<class _R> |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 733 | template<class _Alloc> |
| 734 | function<_R()>::function(allocator_arg_t, const _Alloc&, const function& __f) |
| 735 | { |
| 736 | if (__f.__f_ == 0) |
| 737 | __f_ = 0; |
| 738 | else if (__f.__f_ == (const __base*)&__f.__buf_) |
| 739 | { |
| 740 | __f_ = (__base*)&__buf_; |
| 741 | __f.__f_->__clone(__f_); |
| 742 | } |
| 743 | else |
| 744 | __f_ = __f.__f_->__clone(); |
| 745 | } |
| 746 | |
| 747 | template<class _R> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 748 | template <class _F> |
| 749 | function<_R()>::function(_F __f, |
| 750 | typename enable_if<!is_integral<_F>::value>::type*) |
| 751 | : __f_(0) |
| 752 | { |
| 753 | if (__not_null(__f)) |
| 754 | { |
| 755 | typedef __function::__func<_F, allocator<_F>, _R()> _FF; |
| 756 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 757 | { |
| 758 | __f_ = (__base*)&__buf_; |
| 759 | ::new (__f_) _FF(__f); |
| 760 | } |
| 761 | else |
| 762 | { |
| 763 | typedef allocator<_FF> _A; |
| 764 | _A __a; |
| 765 | typedef __allocator_destructor<_A> _D; |
| 766 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 767 | ::new (__hold.get()) _FF(__f, allocator<_F>(__a)); |
| 768 | __f_ = __hold.release(); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | template<class _R> |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 774 | template <class _F, class _Alloc> |
| 775 | function<_R()>::function(allocator_arg_t, const _Alloc& __a0, _F __f, |
| 776 | typename enable_if<!is_integral<_F>::value>::type*) |
| 777 | : __f_(0) |
| 778 | { |
| 779 | typedef allocator_traits<_Alloc> __alloc_traits; |
| 780 | if (__not_null(__f)) |
| 781 | { |
| 782 | typedef __function::__func<_F, _Alloc, _R()> _FF; |
| 783 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 784 | { |
| 785 | __f_ = (__base*)&__buf_; |
| 786 | ::new (__f_) _FF(__f); |
| 787 | } |
| 788 | else |
| 789 | { |
| 790 | typedef typename __alloc_traits::template |
| 791 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 792 | rebind_alloc<_FF> |
| 793 | #else |
| 794 | rebind_alloc<_FF>::other |
| 795 | #endif |
| 796 | _A; |
| 797 | _A __a(__a0); |
| 798 | typedef __allocator_destructor<_A> _D; |
| 799 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 800 | ::new (__hold.get()) _FF(__f, _Alloc(__a)); |
| 801 | __f_ = __hold.release(); |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | template<class _R> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 807 | function<_R()>& |
| 808 | function<_R()>::operator=(const function& __f) |
| 809 | { |
| 810 | function(__f).swap(*this); |
| 811 | return *this; |
| 812 | } |
| 813 | |
| 814 | template<class _R> |
| 815 | function<_R()>& |
| 816 | function<_R()>::operator=(nullptr_t) |
| 817 | { |
| 818 | if (__f_ == (__base*)&__buf_) |
| 819 | __f_->destroy(); |
| 820 | else if (__f_) |
| 821 | __f_->destroy_deallocate(); |
| 822 | __f_ = 0; |
| 823 | } |
| 824 | |
| 825 | template<class _R> |
| 826 | template <class _F> |
| 827 | typename enable_if |
| 828 | < |
| 829 | !is_integral<_F>::value, |
| 830 | function<_R()>& |
| 831 | >::type |
| 832 | function<_R()>::operator=(_F __f) |
| 833 | { |
| 834 | function(_STD::move(__f)).swap(*this); |
| 835 | return *this; |
| 836 | } |
| 837 | |
| 838 | template<class _R> |
| 839 | function<_R()>::~function() |
| 840 | { |
| 841 | if (__f_ == (__base*)&__buf_) |
| 842 | __f_->destroy(); |
| 843 | else if (__f_) |
| 844 | __f_->destroy_deallocate(); |
| 845 | } |
| 846 | |
| 847 | template<class _R> |
| 848 | void |
| 849 | function<_R()>::swap(function& __f) |
| 850 | { |
| 851 | if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) |
| 852 | { |
| 853 | typename aligned_storage<sizeof(__buf_)>::type __tempbuf; |
| 854 | __base* __t = (__base*)&__tempbuf; |
| 855 | __f_->__clone(__t); |
| 856 | __f_->destroy(); |
| 857 | __f_ = 0; |
| 858 | __f.__f_->__clone((__base*)&__buf_); |
| 859 | __f.__f_->destroy(); |
| 860 | __f.__f_ = 0; |
| 861 | __f_ = (__base*)&__buf_; |
| 862 | __t->__clone((__base*)&__f.__buf_); |
| 863 | __t->destroy(); |
| 864 | __f.__f_ = (__base*)&__f.__buf_; |
| 865 | } |
| 866 | else if (__f_ == (__base*)&__buf_) |
| 867 | { |
| 868 | __f_->__clone((__base*)&__f.__buf_); |
| 869 | __f_->destroy(); |
| 870 | __f_ = __f.__f_; |
| 871 | __f.__f_ = (__base*)&__f.__buf_; |
| 872 | } |
| 873 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 874 | { |
| 875 | __f.__f_->__clone((__base*)&__buf_); |
| 876 | __f.__f_->destroy(); |
| 877 | __f.__f_ = __f_; |
| 878 | __f_ = (__base*)&__buf_; |
| 879 | } |
| 880 | else |
| 881 | _STD::swap(__f_, __f.__f_); |
| 882 | } |
| 883 | |
| 884 | template<class _R> |
| 885 | _R |
| 886 | function<_R()>::operator()() const |
| 887 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 888 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 889 | if (__f_ == 0) |
| 890 | throw bad_function_call(); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 891 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 892 | return (*__f_)(); |
| 893 | } |
| 894 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 895 | #ifndef _LIBCPP_NO_RTTI |
| 896 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 897 | template<class _R> |
| 898 | const std::type_info& |
| 899 | function<_R()>::target_type() const |
| 900 | { |
| 901 | if (__f_ == 0) |
| 902 | return typeid(void); |
| 903 | return __f_->target_type(); |
| 904 | } |
| 905 | |
| 906 | template<class _R> |
| 907 | template <typename _T> |
| 908 | _T* |
| 909 | function<_R()>::target() |
| 910 | { |
| 911 | if (__f_ == 0) |
| 912 | return (_T*)0; |
| 913 | return (_T*)__f_->target(typeid(_T)); |
| 914 | } |
| 915 | |
| 916 | template<class _R> |
| 917 | template <typename _T> |
| 918 | const _T* |
| 919 | function<_R()>::target() const |
| 920 | { |
| 921 | if (__f_ == 0) |
| 922 | return (const _T*)0; |
| 923 | return (const _T*)__f_->target(typeid(_T)); |
| 924 | } |
| 925 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 926 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 927 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 928 | template<class _R, class _A0> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 929 | class _LIBCPP_VISIBLE function<_R(_A0)> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 930 | : public unary_function<_A0, _R> |
| 931 | { |
| 932 | typedef __function::__base<_R(_A0)> __base; |
| 933 | aligned_storage<3*sizeof(void*)>::type __buf_; |
| 934 | __base* __f_; |
| 935 | |
| 936 | template <class _F> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 937 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 938 | static bool __not_null(const _F&) {return true;} |
| 939 | template <class _R2, class _B0> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 940 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 941 | static bool __not_null(_R2 (*__p)(_B0)) {return __p;} |
| 942 | template <class _R2, class _C> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 943 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 944 | static bool __not_null(_R2 (_C::*__p)()) {return __p;} |
| 945 | template <class _R2, class _C> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 946 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 947 | static bool __not_null(_R2 (_C::*__p)() const) {return __p;} |
| 948 | template <class _R2, class _C> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 949 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 950 | static bool __not_null(_R2 (_C::*__p)() volatile) {return __p;} |
| 951 | template <class _R2, class _C> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 952 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 953 | static bool __not_null(_R2 (_C::*__p)() const volatile) {return __p;} |
| 954 | template <class _R2, class _B0> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 955 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 956 | static bool __not_null(const function<_R(_B0)>& __p) {return __p;} |
| 957 | public: |
| 958 | typedef _R result_type; |
| 959 | |
| 960 | // 20.7.16.2.1, construct/copy/destroy: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 961 | _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} |
| 962 | _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 963 | function(const function&); |
| 964 | template<class _F> |
| 965 | function(_F, |
| 966 | typename enable_if<!is_integral<_F>::value>::type* = 0); |
| 967 | |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 968 | template<class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 969 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 970 | function(allocator_arg_t, const _Alloc&) : __f_(0) {} |
| 971 | template<class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 972 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 973 | function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} |
| 974 | template<class _Alloc> |
| 975 | function(allocator_arg_t, const _Alloc&, const function&); |
| 976 | template<class _F, class _Alloc> |
| 977 | function(allocator_arg_t, const _Alloc& __a, _F __f, |
| 978 | typename enable_if<!is_integral<_F>::value>::type* = 0); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 979 | |
| 980 | function& operator=(const function&); |
| 981 | function& operator=(nullptr_t); |
| 982 | template<class _F> |
| 983 | typename enable_if |
| 984 | < |
| 985 | !is_integral<_F>::value, |
| 986 | function& |
| 987 | >::type |
| 988 | operator=(_F); |
| 989 | |
| 990 | ~function(); |
| 991 | |
| 992 | // 20.7.16.2.2, function modifiers: |
| 993 | void swap(function&); |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 994 | template<class _F, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 995 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 996 | void assign(_F __f, const _Alloc& __a) |
| 997 | {function(allocator_arg, __a, __f).swap(*this);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 998 | |
| 999 | // 20.7.16.2.3, function capacity: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1000 | _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1001 | |
| 1002 | private: |
| 1003 | // deleted overloads close possible hole in the type system |
| 1004 | template<class _R2, class _B0> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1005 | bool operator==(const function<_R2(_B0)>&) const;// = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1006 | template<class _R2, class _B0> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1007 | bool operator!=(const function<_R2(_B0)>&) const;// = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1008 | public: |
| 1009 | // 20.7.16.2.4, function invocation: |
| 1010 | _R operator()(_A0) const; |
| 1011 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1012 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1013 | // 20.7.16.2.5, function target access: |
| 1014 | const std::type_info& target_type() const; |
| 1015 | template <typename _T> _T* target(); |
| 1016 | template <typename _T> const _T* target() const; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1017 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1018 | }; |
| 1019 | |
| 1020 | template<class _R, class _A0> |
| 1021 | function<_R(_A0)>::function(const function& __f) |
| 1022 | { |
| 1023 | if (__f.__f_ == 0) |
| 1024 | __f_ = 0; |
| 1025 | else if (__f.__f_ == (const __base*)&__f.__buf_) |
| 1026 | { |
| 1027 | __f_ = (__base*)&__buf_; |
| 1028 | __f.__f_->__clone(__f_); |
| 1029 | } |
| 1030 | else |
| 1031 | __f_ = __f.__f_->__clone(); |
| 1032 | } |
| 1033 | |
| 1034 | template<class _R, class _A0> |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1035 | template<class _Alloc> |
| 1036 | function<_R(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f) |
| 1037 | { |
| 1038 | if (__f.__f_ == 0) |
| 1039 | __f_ = 0; |
| 1040 | else if (__f.__f_ == (const __base*)&__f.__buf_) |
| 1041 | { |
| 1042 | __f_ = (__base*)&__buf_; |
| 1043 | __f.__f_->__clone(__f_); |
| 1044 | } |
| 1045 | else |
| 1046 | __f_ = __f.__f_->__clone(); |
| 1047 | } |
| 1048 | |
| 1049 | template<class _R, class _A0> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1050 | template <class _F> |
| 1051 | function<_R(_A0)>::function(_F __f, |
| 1052 | typename enable_if<!is_integral<_F>::value>::type*) |
| 1053 | : __f_(0) |
| 1054 | { |
| 1055 | if (__not_null(__f)) |
| 1056 | { |
| 1057 | typedef __function::__func<_F, allocator<_F>, _R(_A0)> _FF; |
| 1058 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1059 | { |
| 1060 | __f_ = (__base*)&__buf_; |
| 1061 | ::new (__f_) _FF(__f); |
| 1062 | } |
| 1063 | else |
| 1064 | { |
| 1065 | typedef allocator<_FF> _A; |
| 1066 | _A __a; |
| 1067 | typedef __allocator_destructor<_A> _D; |
| 1068 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 1069 | ::new (__hold.get()) _FF(__f, allocator<_F>(__a)); |
| 1070 | __f_ = __hold.release(); |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | template<class _R, class _A0> |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1076 | template <class _F, class _Alloc> |
| 1077 | function<_R(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _F __f, |
| 1078 | typename enable_if<!is_integral<_F>::value>::type*) |
| 1079 | : __f_(0) |
| 1080 | { |
| 1081 | typedef allocator_traits<_Alloc> __alloc_traits; |
| 1082 | if (__not_null(__f)) |
| 1083 | { |
| 1084 | typedef __function::__func<_F, _Alloc, _R(_A0)> _FF; |
| 1085 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1086 | { |
| 1087 | __f_ = (__base*)&__buf_; |
| 1088 | ::new (__f_) _FF(__f); |
| 1089 | } |
| 1090 | else |
| 1091 | { |
| 1092 | typedef typename __alloc_traits::template |
| 1093 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 1094 | rebind_alloc<_FF> |
| 1095 | #else |
| 1096 | rebind_alloc<_FF>::other |
| 1097 | #endif |
| 1098 | _A; |
| 1099 | _A __a(__a0); |
| 1100 | typedef __allocator_destructor<_A> _D; |
| 1101 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 1102 | ::new (__hold.get()) _FF(__f, _Alloc(__a)); |
| 1103 | __f_ = __hold.release(); |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | template<class _R, class _A0> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1109 | function<_R(_A0)>& |
| 1110 | function<_R(_A0)>::operator=(const function& __f) |
| 1111 | { |
| 1112 | function(__f).swap(*this); |
| 1113 | return *this; |
| 1114 | } |
| 1115 | |
| 1116 | template<class _R, class _A0> |
| 1117 | function<_R(_A0)>& |
| 1118 | function<_R(_A0)>::operator=(nullptr_t) |
| 1119 | { |
| 1120 | if (__f_ == (__base*)&__buf_) |
| 1121 | __f_->destroy(); |
| 1122 | else if (__f_) |
| 1123 | __f_->destroy_deallocate(); |
| 1124 | __f_ = 0; |
| 1125 | } |
| 1126 | |
| 1127 | template<class _R, class _A0> |
| 1128 | template <class _F> |
| 1129 | typename enable_if |
| 1130 | < |
| 1131 | !is_integral<_F>::value, |
| 1132 | function<_R(_A0)>& |
| 1133 | >::type |
| 1134 | function<_R(_A0)>::operator=(_F __f) |
| 1135 | { |
| 1136 | function(_STD::move(__f)).swap(*this); |
| 1137 | return *this; |
| 1138 | } |
| 1139 | |
| 1140 | template<class _R, class _A0> |
| 1141 | function<_R(_A0)>::~function() |
| 1142 | { |
| 1143 | if (__f_ == (__base*)&__buf_) |
| 1144 | __f_->destroy(); |
| 1145 | else if (__f_) |
| 1146 | __f_->destroy_deallocate(); |
| 1147 | } |
| 1148 | |
| 1149 | template<class _R, class _A0> |
| 1150 | void |
| 1151 | function<_R(_A0)>::swap(function& __f) |
| 1152 | { |
| 1153 | if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) |
| 1154 | { |
| 1155 | typename aligned_storage<sizeof(__buf_)>::type __tempbuf; |
| 1156 | __base* __t = (__base*)&__tempbuf; |
| 1157 | __f_->__clone(__t); |
| 1158 | __f_->destroy(); |
| 1159 | __f_ = 0; |
| 1160 | __f.__f_->__clone((__base*)&__buf_); |
| 1161 | __f.__f_->destroy(); |
| 1162 | __f.__f_ = 0; |
| 1163 | __f_ = (__base*)&__buf_; |
| 1164 | __t->__clone((__base*)&__f.__buf_); |
| 1165 | __t->destroy(); |
| 1166 | __f.__f_ = (__base*)&__f.__buf_; |
| 1167 | } |
| 1168 | else if (__f_ == (__base*)&__buf_) |
| 1169 | { |
| 1170 | __f_->__clone((__base*)&__f.__buf_); |
| 1171 | __f_->destroy(); |
| 1172 | __f_ = __f.__f_; |
| 1173 | __f.__f_ = (__base*)&__f.__buf_; |
| 1174 | } |
| 1175 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1176 | { |
| 1177 | __f.__f_->__clone((__base*)&__buf_); |
| 1178 | __f.__f_->destroy(); |
| 1179 | __f.__f_ = __f_; |
| 1180 | __f_ = (__base*)&__buf_; |
| 1181 | } |
| 1182 | else |
| 1183 | _STD::swap(__f_, __f.__f_); |
| 1184 | } |
| 1185 | |
| 1186 | template<class _R, class _A0> |
| 1187 | _R |
| 1188 | function<_R(_A0)>::operator()(_A0 __a0) const |
| 1189 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1190 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1191 | if (__f_ == 0) |
| 1192 | throw bad_function_call(); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1193 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1194 | return (*__f_)(__a0); |
| 1195 | } |
| 1196 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1197 | #ifndef _LIBCPP_NO_RTTI |
| 1198 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1199 | template<class _R, class _A0> |
| 1200 | const std::type_info& |
| 1201 | function<_R(_A0)>::target_type() const |
| 1202 | { |
| 1203 | if (__f_ == 0) |
| 1204 | return typeid(void); |
| 1205 | return __f_->target_type(); |
| 1206 | } |
| 1207 | |
| 1208 | template<class _R, class _A0> |
| 1209 | template <typename _T> |
| 1210 | _T* |
| 1211 | function<_R(_A0)>::target() |
| 1212 | { |
| 1213 | if (__f_ == 0) |
| 1214 | return (_T*)0; |
| 1215 | return (_T*)__f_->target(typeid(_T)); |
| 1216 | } |
| 1217 | |
| 1218 | template<class _R, class _A0> |
| 1219 | template <typename _T> |
| 1220 | const _T* |
| 1221 | function<_R(_A0)>::target() const |
| 1222 | { |
| 1223 | if (__f_ == 0) |
| 1224 | return (const _T*)0; |
| 1225 | return (const _T*)__f_->target(typeid(_T)); |
| 1226 | } |
| 1227 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1228 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1229 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1230 | template<class _R, class _A0, class _A1> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1231 | class _LIBCPP_VISIBLE function<_R(_A0, _A1)> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1232 | : public binary_function<_A0, _A1, _R> |
| 1233 | { |
| 1234 | typedef __function::__base<_R(_A0, _A1)> __base; |
| 1235 | aligned_storage<3*sizeof(void*)>::type __buf_; |
| 1236 | __base* __f_; |
| 1237 | |
| 1238 | template <class _F> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1239 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1240 | static bool __not_null(const _F&) {return true;} |
| 1241 | template <class _R2, class _B0, class _B1> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1242 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1243 | static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;} |
| 1244 | template <class _R2, class _C, class _B1> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1245 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1246 | static bool __not_null(_R2 (_C::*__p)(_B1)) {return __p;} |
| 1247 | template <class _R2, class _C, class _B1> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1248 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1249 | static bool __not_null(_R2 (_C::*__p)(_B1) const) {return __p;} |
| 1250 | template <class _R2, class _C, class _B1> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1251 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1252 | static bool __not_null(_R2 (_C::*__p)(_B1) volatile) {return __p;} |
| 1253 | template <class _R2, class _C, class _B1> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1254 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1255 | static bool __not_null(_R2 (_C::*__p)(_B1) const volatile) {return __p;} |
| 1256 | template <class _R2, class _B0, class _B1> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1257 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1258 | static bool __not_null(const function<_R(_B0, _B1)>& __p) {return __p;} |
| 1259 | public: |
| 1260 | typedef _R result_type; |
| 1261 | |
| 1262 | // 20.7.16.2.1, construct/copy/destroy: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1263 | _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} |
| 1264 | _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1265 | function(const function&); |
| 1266 | template<class _F> |
| 1267 | function(_F, |
| 1268 | typename enable_if<!is_integral<_F>::value>::type* = 0); |
| 1269 | |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1270 | template<class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1271 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1272 | function(allocator_arg_t, const _Alloc&) : __f_(0) {} |
| 1273 | template<class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1274 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1275 | function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} |
| 1276 | template<class _Alloc> |
| 1277 | function(allocator_arg_t, const _Alloc&, const function&); |
| 1278 | template<class _F, class _Alloc> |
| 1279 | function(allocator_arg_t, const _Alloc& __a, _F __f, |
| 1280 | typename enable_if<!is_integral<_F>::value>::type* = 0); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1281 | |
| 1282 | function& operator=(const function&); |
| 1283 | function& operator=(nullptr_t); |
| 1284 | template<class _F> |
| 1285 | typename enable_if |
| 1286 | < |
| 1287 | !is_integral<_F>::value, |
| 1288 | function& |
| 1289 | >::type |
| 1290 | operator=(_F); |
| 1291 | |
| 1292 | ~function(); |
| 1293 | |
| 1294 | // 20.7.16.2.2, function modifiers: |
| 1295 | void swap(function&); |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1296 | template<class _F, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1297 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1298 | void assign(_F __f, const _Alloc& __a) |
| 1299 | {function(allocator_arg, __a, __f).swap(*this);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1300 | |
| 1301 | // 20.7.16.2.3, function capacity: |
| 1302 | operator bool() const {return __f_;} |
| 1303 | |
| 1304 | private: |
| 1305 | // deleted overloads close possible hole in the type system |
| 1306 | template<class _R2, class _B0, class _B1> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1307 | bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1308 | template<class _R2, class _B0, class _B1> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1309 | bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1310 | public: |
| 1311 | // 20.7.16.2.4, function invocation: |
| 1312 | _R operator()(_A0, _A1) const; |
| 1313 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1314 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1315 | // 20.7.16.2.5, function target access: |
| 1316 | const std::type_info& target_type() const; |
| 1317 | template <typename _T> _T* target(); |
| 1318 | template <typename _T> const _T* target() const; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1319 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1320 | }; |
| 1321 | |
| 1322 | template<class _R, class _A0, class _A1> |
| 1323 | function<_R(_A0, _A1)>::function(const function& __f) |
| 1324 | { |
| 1325 | if (__f.__f_ == 0) |
| 1326 | __f_ = 0; |
| 1327 | else if (__f.__f_ == (const __base*)&__f.__buf_) |
| 1328 | { |
| 1329 | __f_ = (__base*)&__buf_; |
| 1330 | __f.__f_->__clone(__f_); |
| 1331 | } |
| 1332 | else |
| 1333 | __f_ = __f.__f_->__clone(); |
| 1334 | } |
| 1335 | |
| 1336 | template<class _R, class _A0, class _A1> |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1337 | template<class _Alloc> |
| 1338 | function<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f) |
| 1339 | { |
| 1340 | if (__f.__f_ == 0) |
| 1341 | __f_ = 0; |
| 1342 | else if (__f.__f_ == (const __base*)&__f.__buf_) |
| 1343 | { |
| 1344 | __f_ = (__base*)&__buf_; |
| 1345 | __f.__f_->__clone(__f_); |
| 1346 | } |
| 1347 | else |
| 1348 | __f_ = __f.__f_->__clone(); |
| 1349 | } |
| 1350 | |
| 1351 | template<class _R, class _A0, class _A1> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1352 | template <class _F> |
| 1353 | function<_R(_A0, _A1)>::function(_F __f, |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1354 | typename enable_if<!is_integral<_F>::value>::type*) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1355 | : __f_(0) |
| 1356 | { |
| 1357 | if (__not_null(__f)) |
| 1358 | { |
| 1359 | typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1)> _FF; |
| 1360 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1361 | { |
| 1362 | __f_ = (__base*)&__buf_; |
| 1363 | ::new (__f_) _FF(__f); |
| 1364 | } |
| 1365 | else |
| 1366 | { |
| 1367 | typedef allocator<_FF> _A; |
| 1368 | _A __a; |
| 1369 | typedef __allocator_destructor<_A> _D; |
| 1370 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 1371 | ::new (__hold.get()) _FF(__f, allocator<_F>(__a)); |
| 1372 | __f_ = __hold.release(); |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | template<class _R, class _A0, class _A1> |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1378 | template <class _F, class _Alloc> |
| 1379 | function<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _F __f, |
| 1380 | typename enable_if<!is_integral<_F>::value>::type*) |
| 1381 | : __f_(0) |
| 1382 | { |
| 1383 | typedef allocator_traits<_Alloc> __alloc_traits; |
| 1384 | if (__not_null(__f)) |
| 1385 | { |
| 1386 | typedef __function::__func<_F, _Alloc, _R(_A0, _A1)> _FF; |
| 1387 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1388 | { |
| 1389 | __f_ = (__base*)&__buf_; |
| 1390 | ::new (__f_) _FF(__f); |
| 1391 | } |
| 1392 | else |
| 1393 | { |
| 1394 | typedef typename __alloc_traits::template |
| 1395 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 1396 | rebind_alloc<_FF> |
| 1397 | #else |
| 1398 | rebind_alloc<_FF>::other |
| 1399 | #endif |
| 1400 | _A; |
| 1401 | _A __a(__a0); |
| 1402 | typedef __allocator_destructor<_A> _D; |
| 1403 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 1404 | ::new (__hold.get()) _FF(__f, _Alloc(__a)); |
| 1405 | __f_ = __hold.release(); |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | template<class _R, class _A0, class _A1> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1411 | function<_R(_A0, _A1)>& |
| 1412 | function<_R(_A0, _A1)>::operator=(const function& __f) |
| 1413 | { |
| 1414 | function(__f).swap(*this); |
| 1415 | return *this; |
| 1416 | } |
| 1417 | |
| 1418 | template<class _R, class _A0, class _A1> |
| 1419 | function<_R(_A0, _A1)>& |
| 1420 | function<_R(_A0, _A1)>::operator=(nullptr_t) |
| 1421 | { |
| 1422 | if (__f_ == (__base*)&__buf_) |
| 1423 | __f_->destroy(); |
| 1424 | else if (__f_) |
| 1425 | __f_->destroy_deallocate(); |
| 1426 | __f_ = 0; |
| 1427 | } |
| 1428 | |
| 1429 | template<class _R, class _A0, class _A1> |
| 1430 | template <class _F> |
| 1431 | typename enable_if |
| 1432 | < |
| 1433 | !is_integral<_F>::value, |
| 1434 | function<_R(_A0, _A1)>& |
| 1435 | >::type |
| 1436 | function<_R(_A0, _A1)>::operator=(_F __f) |
| 1437 | { |
| 1438 | function(_STD::move(__f)).swap(*this); |
| 1439 | return *this; |
| 1440 | } |
| 1441 | |
| 1442 | template<class _R, class _A0, class _A1> |
| 1443 | function<_R(_A0, _A1)>::~function() |
| 1444 | { |
| 1445 | if (__f_ == (__base*)&__buf_) |
| 1446 | __f_->destroy(); |
| 1447 | else if (__f_) |
| 1448 | __f_->destroy_deallocate(); |
| 1449 | } |
| 1450 | |
| 1451 | template<class _R, class _A0, class _A1> |
| 1452 | void |
| 1453 | function<_R(_A0, _A1)>::swap(function& __f) |
| 1454 | { |
| 1455 | if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) |
| 1456 | { |
| 1457 | typename aligned_storage<sizeof(__buf_)>::type __tempbuf; |
| 1458 | __base* __t = (__base*)&__tempbuf; |
| 1459 | __f_->__clone(__t); |
| 1460 | __f_->destroy(); |
| 1461 | __f_ = 0; |
| 1462 | __f.__f_->__clone((__base*)&__buf_); |
| 1463 | __f.__f_->destroy(); |
| 1464 | __f.__f_ = 0; |
| 1465 | __f_ = (__base*)&__buf_; |
| 1466 | __t->__clone((__base*)&__f.__buf_); |
| 1467 | __t->destroy(); |
| 1468 | __f.__f_ = (__base*)&__f.__buf_; |
| 1469 | } |
| 1470 | else if (__f_ == (__base*)&__buf_) |
| 1471 | { |
| 1472 | __f_->__clone((__base*)&__f.__buf_); |
| 1473 | __f_->destroy(); |
| 1474 | __f_ = __f.__f_; |
| 1475 | __f.__f_ = (__base*)&__f.__buf_; |
| 1476 | } |
| 1477 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1478 | { |
| 1479 | __f.__f_->__clone((__base*)&__buf_); |
| 1480 | __f.__f_->destroy(); |
| 1481 | __f.__f_ = __f_; |
| 1482 | __f_ = (__base*)&__buf_; |
| 1483 | } |
| 1484 | else |
| 1485 | _STD::swap(__f_, __f.__f_); |
| 1486 | } |
| 1487 | |
| 1488 | template<class _R, class _A0, class _A1> |
| 1489 | _R |
| 1490 | function<_R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const |
| 1491 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1492 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1493 | if (__f_ == 0) |
| 1494 | throw bad_function_call(); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1495 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1496 | return (*__f_)(__a0, __a1); |
| 1497 | } |
| 1498 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1499 | #ifndef _LIBCPP_NO_RTTI |
| 1500 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1501 | template<class _R, class _A0, class _A1> |
| 1502 | const std::type_info& |
| 1503 | function<_R(_A0, _A1)>::target_type() const |
| 1504 | { |
| 1505 | if (__f_ == 0) |
| 1506 | return typeid(void); |
| 1507 | return __f_->target_type(); |
| 1508 | } |
| 1509 | |
| 1510 | template<class _R, class _A0, class _A1> |
| 1511 | template <typename _T> |
| 1512 | _T* |
| 1513 | function<_R(_A0, _A1)>::target() |
| 1514 | { |
| 1515 | if (__f_ == 0) |
| 1516 | return (_T*)0; |
| 1517 | return (_T*)__f_->target(typeid(_T)); |
| 1518 | } |
| 1519 | |
| 1520 | template<class _R, class _A0, class _A1> |
| 1521 | template <typename _T> |
| 1522 | const _T* |
| 1523 | function<_R(_A0, _A1)>::target() const |
| 1524 | { |
| 1525 | if (__f_ == 0) |
| 1526 | return (const _T*)0; |
| 1527 | return (const _T*)__f_->target(typeid(_T)); |
| 1528 | } |
| 1529 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1530 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1531 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1532 | template<class _R, class _A0, class _A1, class _A2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1533 | class _LIBCPP_VISIBLE function<_R(_A0, _A1, _A2)> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1534 | { |
| 1535 | typedef __function::__base<_R(_A0, _A1, _A2)> __base; |
| 1536 | aligned_storage<3*sizeof(void*)>::type __buf_; |
| 1537 | __base* __f_; |
| 1538 | |
| 1539 | template <class _F> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1540 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1541 | static bool __not_null(const _F&) {return true;} |
| 1542 | template <class _R2, class _B0, class _B1, class _B2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1543 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1544 | static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;} |
| 1545 | template <class _R2, class _C, class _B1, class _B2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1546 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1547 | static bool __not_null(_R2 (_C::*__p)(_B1, _B2)) {return __p;} |
| 1548 | template <class _R2, class _C, class _B1, class _B2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1549 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1550 | static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const) {return __p;} |
| 1551 | template <class _R2, class _C, class _B1, class _B2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1552 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1553 | static bool __not_null(_R2 (_C::*__p)(_B1, _B2) volatile) {return __p;} |
| 1554 | template <class _R2, class _C, class _B1, class _B2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1555 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1556 | static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const volatile) {return __p;} |
| 1557 | template <class _R2, class _B0, class _B1, class _B2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1558 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1559 | static bool __not_null(const function<_R(_B0, _B1, _B2)>& __p) {return __p;} |
| 1560 | public: |
| 1561 | typedef _R result_type; |
| 1562 | |
| 1563 | // 20.7.16.2.1, construct/copy/destroy: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1564 | _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} |
| 1565 | _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1566 | function(const function&); |
| 1567 | template<class _F> |
| 1568 | function(_F, |
| 1569 | typename enable_if<!is_integral<_F>::value>::type* = 0); |
| 1570 | |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1571 | template<class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1572 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1573 | function(allocator_arg_t, const _Alloc&) : __f_(0) {} |
| 1574 | template<class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1575 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1576 | function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} |
| 1577 | template<class _Alloc> |
| 1578 | function(allocator_arg_t, const _Alloc&, const function&); |
| 1579 | template<class _F, class _Alloc> |
| 1580 | function(allocator_arg_t, const _Alloc& __a, _F __f, |
| 1581 | typename enable_if<!is_integral<_F>::value>::type* = 0); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1582 | |
| 1583 | function& operator=(const function&); |
| 1584 | function& operator=(nullptr_t); |
| 1585 | template<class _F> |
| 1586 | typename enable_if |
| 1587 | < |
| 1588 | !is_integral<_F>::value, |
| 1589 | function& |
| 1590 | >::type |
| 1591 | operator=(_F); |
| 1592 | |
| 1593 | ~function(); |
| 1594 | |
| 1595 | // 20.7.16.2.2, function modifiers: |
| 1596 | void swap(function&); |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1597 | template<class _F, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1598 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1599 | void assign(_F __f, const _Alloc& __a) |
| 1600 | {function(allocator_arg, __a, __f).swap(*this);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1601 | |
| 1602 | // 20.7.16.2.3, function capacity: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1603 | _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1604 | |
| 1605 | private: |
| 1606 | // deleted overloads close possible hole in the type system |
| 1607 | template<class _R2, class _B0, class _B1, class _B2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1608 | bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1609 | template<class _R2, class _B0, class _B1, class _B2> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1610 | bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1611 | public: |
| 1612 | // 20.7.16.2.4, function invocation: |
| 1613 | _R operator()(_A0, _A1, _A2) const; |
| 1614 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1615 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1616 | // 20.7.16.2.5, function target access: |
| 1617 | const std::type_info& target_type() const; |
| 1618 | template <typename _T> _T* target(); |
| 1619 | template <typename _T> const _T* target() const; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1620 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1621 | }; |
| 1622 | |
| 1623 | template<class _R, class _A0, class _A1, class _A2> |
| 1624 | function<_R(_A0, _A1, _A2)>::function(const function& __f) |
| 1625 | { |
| 1626 | if (__f.__f_ == 0) |
| 1627 | __f_ = 0; |
| 1628 | else if (__f.__f_ == (const __base*)&__f.__buf_) |
| 1629 | { |
| 1630 | __f_ = (__base*)&__buf_; |
| 1631 | __f.__f_->__clone(__f_); |
| 1632 | } |
| 1633 | else |
| 1634 | __f_ = __f.__f_->__clone(); |
| 1635 | } |
| 1636 | |
| 1637 | template<class _R, class _A0, class _A1, class _A2> |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1638 | template<class _Alloc> |
| 1639 | function<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&, |
| 1640 | const function& __f) |
| 1641 | { |
| 1642 | if (__f.__f_ == 0) |
| 1643 | __f_ = 0; |
| 1644 | else if (__f.__f_ == (const __base*)&__f.__buf_) |
| 1645 | { |
| 1646 | __f_ = (__base*)&__buf_; |
| 1647 | __f.__f_->__clone(__f_); |
| 1648 | } |
| 1649 | else |
| 1650 | __f_ = __f.__f_->__clone(); |
| 1651 | } |
| 1652 | |
| 1653 | template<class _R, class _A0, class _A1, class _A2> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1654 | template <class _F> |
| 1655 | function<_R(_A0, _A1, _A2)>::function(_F __f, |
| 1656 | typename enable_if<!is_integral<_F>::value>::type*) |
| 1657 | : __f_(0) |
| 1658 | { |
| 1659 | if (__not_null(__f)) |
| 1660 | { |
| 1661 | typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1, _A2)> _FF; |
| 1662 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1663 | { |
| 1664 | __f_ = (__base*)&__buf_; |
| 1665 | ::new (__f_) _FF(__f); |
| 1666 | } |
| 1667 | else |
| 1668 | { |
| 1669 | typedef allocator<_FF> _A; |
| 1670 | _A __a; |
| 1671 | typedef __allocator_destructor<_A> _D; |
| 1672 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 1673 | ::new (__hold.get()) _FF(__f, allocator<_F>(__a)); |
| 1674 | __f_ = __hold.release(); |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | template<class _R, class _A0, class _A1, class _A2> |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 1680 | template <class _F, class _Alloc> |
| 1681 | function<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _F __f, |
| 1682 | typename enable_if<!is_integral<_F>::value>::type*) |
| 1683 | : __f_(0) |
| 1684 | { |
| 1685 | typedef allocator_traits<_Alloc> __alloc_traits; |
| 1686 | if (__not_null(__f)) |
| 1687 | { |
| 1688 | typedef __function::__func<_F, _Alloc, _R(_A0, _A1, _A2)> _FF; |
| 1689 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1690 | { |
| 1691 | __f_ = (__base*)&__buf_; |
| 1692 | ::new (__f_) _FF(__f); |
| 1693 | } |
| 1694 | else |
| 1695 | { |
| 1696 | typedef typename __alloc_traits::template |
| 1697 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 1698 | rebind_alloc<_FF> |
| 1699 | #else |
| 1700 | rebind_alloc<_FF>::other |
| 1701 | #endif |
| 1702 | _A; |
| 1703 | _A __a(__a0); |
| 1704 | typedef __allocator_destructor<_A> _D; |
| 1705 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 1706 | ::new (__hold.get()) _FF(__f, _Alloc(__a)); |
| 1707 | __f_ = __hold.release(); |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | template<class _R, class _A0, class _A1, class _A2> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1713 | function<_R(_A0, _A1, _A2)>& |
| 1714 | function<_R(_A0, _A1, _A2)>::operator=(const function& __f) |
| 1715 | { |
| 1716 | function(__f).swap(*this); |
| 1717 | return *this; |
| 1718 | } |
| 1719 | |
| 1720 | template<class _R, class _A0, class _A1, class _A2> |
| 1721 | function<_R(_A0, _A1, _A2)>& |
| 1722 | function<_R(_A0, _A1, _A2)>::operator=(nullptr_t) |
| 1723 | { |
| 1724 | if (__f_ == (__base*)&__buf_) |
| 1725 | __f_->destroy(); |
| 1726 | else if (__f_) |
| 1727 | __f_->destroy_deallocate(); |
| 1728 | __f_ = 0; |
| 1729 | } |
| 1730 | |
| 1731 | template<class _R, class _A0, class _A1, class _A2> |
| 1732 | template <class _F> |
| 1733 | typename enable_if |
| 1734 | < |
| 1735 | !is_integral<_F>::value, |
| 1736 | function<_R(_A0, _A1, _A2)>& |
| 1737 | >::type |
| 1738 | function<_R(_A0, _A1, _A2)>::operator=(_F __f) |
| 1739 | { |
| 1740 | function(_STD::move(__f)).swap(*this); |
| 1741 | return *this; |
| 1742 | } |
| 1743 | |
| 1744 | template<class _R, class _A0, class _A1, class _A2> |
| 1745 | function<_R(_A0, _A1, _A2)>::~function() |
| 1746 | { |
| 1747 | if (__f_ == (__base*)&__buf_) |
| 1748 | __f_->destroy(); |
| 1749 | else if (__f_) |
| 1750 | __f_->destroy_deallocate(); |
| 1751 | } |
| 1752 | |
| 1753 | template<class _R, class _A0, class _A1, class _A2> |
| 1754 | void |
| 1755 | function<_R(_A0, _A1, _A2)>::swap(function& __f) |
| 1756 | { |
| 1757 | if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) |
| 1758 | { |
| 1759 | typename aligned_storage<sizeof(__buf_)>::type __tempbuf; |
| 1760 | __base* __t = (__base*)&__tempbuf; |
| 1761 | __f_->__clone(__t); |
| 1762 | __f_->destroy(); |
| 1763 | __f_ = 0; |
| 1764 | __f.__f_->__clone((__base*)&__buf_); |
| 1765 | __f.__f_->destroy(); |
| 1766 | __f.__f_ = 0; |
| 1767 | __f_ = (__base*)&__buf_; |
| 1768 | __t->__clone((__base*)&__f.__buf_); |
| 1769 | __t->destroy(); |
| 1770 | __f.__f_ = (__base*)&__f.__buf_; |
| 1771 | } |
| 1772 | else if (__f_ == (__base*)&__buf_) |
| 1773 | { |
| 1774 | __f_->__clone((__base*)&__f.__buf_); |
| 1775 | __f_->destroy(); |
| 1776 | __f_ = __f.__f_; |
| 1777 | __f.__f_ = (__base*)&__f.__buf_; |
| 1778 | } |
| 1779 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1780 | { |
| 1781 | __f.__f_->__clone((__base*)&__buf_); |
| 1782 | __f.__f_->destroy(); |
| 1783 | __f.__f_ = __f_; |
| 1784 | __f_ = (__base*)&__buf_; |
| 1785 | } |
| 1786 | else |
| 1787 | _STD::swap(__f_, __f.__f_); |
| 1788 | } |
| 1789 | |
| 1790 | template<class _R, class _A0, class _A1, class _A2> |
| 1791 | _R |
| 1792 | function<_R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const |
| 1793 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1794 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1795 | if (__f_ == 0) |
| 1796 | throw bad_function_call(); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1797 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1798 | return (*__f_)(__a0, __a1, __a2); |
| 1799 | } |
| 1800 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1801 | #ifndef _LIBCPP_NO_RTTI |
| 1802 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1803 | template<class _R, class _A0, class _A1, class _A2> |
| 1804 | const std::type_info& |
| 1805 | function<_R(_A0, _A1, _A2)>::target_type() const |
| 1806 | { |
| 1807 | if (__f_ == 0) |
| 1808 | return typeid(void); |
| 1809 | return __f_->target_type(); |
| 1810 | } |
| 1811 | |
| 1812 | template<class _R, class _A0, class _A1, class _A2> |
| 1813 | template <typename _T> |
| 1814 | _T* |
| 1815 | function<_R(_A0, _A1, _A2)>::target() |
| 1816 | { |
| 1817 | if (__f_ == 0) |
| 1818 | return (_T*)0; |
| 1819 | return (_T*)__f_->target(typeid(_T)); |
| 1820 | } |
| 1821 | |
| 1822 | template<class _R, class _A0, class _A1, class _A2> |
| 1823 | template <typename _T> |
| 1824 | const _T* |
| 1825 | function<_R(_A0, _A1, _A2)>::target() const |
| 1826 | { |
| 1827 | if (__f_ == 0) |
| 1828 | return (const _T*)0; |
| 1829 | return (const _T*)__f_->target(typeid(_T)); |
| 1830 | } |
| 1831 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1832 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1833 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1834 | template <class _F> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1835 | inline _LIBCPP_INLINE_VISIBILITY |
| 1836 | bool |
| 1837 | operator==(const function<_F>& __f, nullptr_t) {return !__f;} |
| 1838 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1839 | template <class _F> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1840 | inline _LIBCPP_INLINE_VISIBILITY |
| 1841 | bool |
| 1842 | operator==(nullptr_t, const function<_F>& __f) {return !__f;} |
| 1843 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1844 | template <class _F> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1845 | inline _LIBCPP_INLINE_VISIBILITY |
| 1846 | bool |
| 1847 | operator!=(const function<_F>& __f, nullptr_t) {return (bool)__f;} |
| 1848 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1849 | template <class _F> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1850 | inline _LIBCPP_INLINE_VISIBILITY |
| 1851 | bool |
| 1852 | operator!=(nullptr_t, const function<_F>& __f) {return (bool)__f;} |
| 1853 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1854 | template <class _F> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1855 | inline _LIBCPP_INLINE_VISIBILITY |
| 1856 | void |
| 1857 | swap(function<_F>& __x, function<_F>& __y) |
| 1858 | {return __x.swap(__y);} |
| 1859 | |
| 1860 | template<class _Tp> struct __is_bind_expression : public false_type {}; |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1861 | template<class _Tp> struct _LIBCPP_VISIBLE is_bind_expression |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1862 | : public __is_bind_expression<typename remove_cv<_Tp>::type> {}; |
| 1863 | |
| 1864 | template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {}; |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1865 | template<class _Tp> struct _LIBCPP_VISIBLE is_placeholder |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1866 | : public __is_placeholder<typename remove_cv<_Tp>::type> {}; |
| 1867 | |
| 1868 | namespace placeholders |
| 1869 | { |
| 1870 | |
| 1871 | template <int _N> struct __ph {}; |
| 1872 | |
| 1873 | extern __ph<1> _1; |
| 1874 | extern __ph<2> _2; |
| 1875 | extern __ph<3> _3; |
| 1876 | extern __ph<4> _4; |
| 1877 | extern __ph<5> _5; |
| 1878 | extern __ph<6> _6; |
| 1879 | extern __ph<7> _7; |
| 1880 | extern __ph<8> _8; |
| 1881 | extern __ph<9> _9; |
| 1882 | extern __ph<10> _10; |
| 1883 | |
| 1884 | } // placeholders |
| 1885 | |
| 1886 | template<int _N> |
| 1887 | struct __is_placeholder<placeholders::__ph<_N> > |
| 1888 | : public integral_constant<int, _N> {}; |
| 1889 | |
| 1890 | template <class _Tp, class _Uj> |
| 1891 | inline _LIBCPP_INLINE_VISIBILITY |
| 1892 | _Tp& |
| 1893 | __mu(reference_wrapper<_Tp> __t, _Uj&) |
| 1894 | { |
| 1895 | return __t.get(); |
| 1896 | } |
| 1897 | /* |
| 1898 | template <bool _IsBindExpr, class _Ti, class ..._Uj> |
| 1899 | struct __mu_return1 {}; |
| 1900 | |
| 1901 | template <class _Ti, class ..._Uj> |
| 1902 | struct __mu_return1<true, _Ti, _Uj...> |
| 1903 | { |
| 1904 | typedef typename result_of<_Ti(_Uj...)>::type type; |
| 1905 | }; |
| 1906 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1907 | template <class _Ti, class ..._Uj, size_t ..._Indx> |
| 1908 | inline _LIBCPP_INLINE_VISIBILITY |
| 1909 | typename __mu_return1<true, _Ti, _Uj...>::type |
| 1910 | __mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>) |
| 1911 | { |
| 1912 | __ti(_STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...); |
| 1913 | } |
| 1914 | |
| 1915 | template <class _Ti, class ..._Uj> |
| 1916 | inline _LIBCPP_INLINE_VISIBILITY |
| 1917 | typename enable_if |
| 1918 | < |
| 1919 | is_bind_expression<_Ti>::value, |
| 1920 | typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type |
| 1921 | >::type |
| 1922 | __mu(_Ti& __ti, tuple<_Uj...>& __uj) |
| 1923 | { |
| 1924 | typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices; |
| 1925 | return __mu_expand(__ti, __uj, __indices()); |
| 1926 | } |
| 1927 | |
| 1928 | template <bool IsPh, class _Ti, class _Uj> |
| 1929 | struct __mu_return2 {}; |
| 1930 | |
| 1931 | template <class _Ti, class _Uj> |
| 1932 | struct __mu_return2<true, _Ti, _Uj> |
| 1933 | { |
| 1934 | typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type; |
| 1935 | }; |
| 1936 | |
| 1937 | template <class _Ti, class _Uj> |
| 1938 | inline _LIBCPP_INLINE_VISIBILITY |
| 1939 | typename enable_if |
| 1940 | < |
| 1941 | 0 < is_placeholder<_Ti>::value, |
| 1942 | typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type |
| 1943 | >::type |
| 1944 | __mu(_Ti&, _Uj& __uj) |
| 1945 | { |
| 1946 | const size_t _Indx = is_placeholder<_Ti>::value - 1; |
| 1947 | // compiler bug workaround |
| 1948 | typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj); |
| 1949 | return __t; |
| 1950 | // return _STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj)); |
| 1951 | } |
| 1952 | |
| 1953 | template <class _Ti, class _Uj> |
| 1954 | inline _LIBCPP_INLINE_VISIBILITY |
| 1955 | typename enable_if |
| 1956 | < |
| 1957 | !is_bind_expression<_Ti>::value && |
| 1958 | is_placeholder<_Ti>::value == 0 && |
| 1959 | !__is_reference_wrapper<_Ti>::value, |
| 1960 | _Ti& |
| 1961 | >::type |
| 1962 | __mu(_Ti& __ti, _Uj& __uj) |
| 1963 | { |
| 1964 | return __ti; |
| 1965 | } |
| 1966 | |
| 1967 | template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj> |
| 1968 | struct ____mu_return; |
| 1969 | |
| 1970 | template <class _Ti, class ..._Uj> |
| 1971 | struct ____mu_return<_Ti, true, false, tuple<_Uj...> > |
| 1972 | { |
| 1973 | typedef typename result_of<_Ti(_Uj...)>::type type; |
| 1974 | }; |
| 1975 | |
| 1976 | template <class _Ti, class _TupleUj> |
| 1977 | struct ____mu_return<_Ti, false, true, _TupleUj> |
| 1978 | { |
| 1979 | typedef typename tuple_element<is_placeholder<_Ti>::value - 1, |
| 1980 | _TupleUj>::type&& type; |
| 1981 | }; |
| 1982 | |
| 1983 | template <class _Ti, class _TupleUj> |
| 1984 | struct ____mu_return<_Ti, false, false, _TupleUj> |
| 1985 | { |
| 1986 | typedef _Ti& type; |
| 1987 | }; |
| 1988 | |
| 1989 | template <class _Ti, class _TupleUj> |
| 1990 | struct __mu_return |
| 1991 | : public ____mu_return<_Ti, |
| 1992 | is_bind_expression<_Ti>::value, |
| 1993 | 0 < is_placeholder<_Ti>::value, |
| 1994 | _TupleUj> |
| 1995 | { |
| 1996 | }; |
| 1997 | |
| 1998 | template <class _Ti, class _TupleUj> |
| 1999 | struct __mu_return<reference_wrapper<_Ti>, _TupleUj> |
| 2000 | { |
| 2001 | typedef _Ti& type; |
| 2002 | }; |
| 2003 | |
| 2004 | template <class _F, class _BoundArgs, class _TupleUj> |
| 2005 | struct __bind_return; |
| 2006 | |
| 2007 | template <class _F, class ..._BoundArgs, class _TupleUj> |
| 2008 | struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj> |
| 2009 | { |
| 2010 | typedef typename __ref_return |
| 2011 | < |
| 2012 | _F&, |
| 2013 | typename __mu_return |
| 2014 | < |
| 2015 | _BoundArgs, |
| 2016 | _TupleUj |
| 2017 | >::type... |
| 2018 | >::type type; |
| 2019 | }; |
| 2020 | |
| 2021 | template <class _F, class ..._BoundArgs, class _TupleUj> |
| 2022 | struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj> |
| 2023 | { |
| 2024 | typedef typename __ref_return |
| 2025 | < |
| 2026 | _F&, |
| 2027 | typename __mu_return |
| 2028 | < |
| 2029 | const _BoundArgs, |
| 2030 | _TupleUj |
| 2031 | >::type... |
| 2032 | >::type type; |
| 2033 | }; |
| 2034 | |
| 2035 | template <class _F, class _BoundArgs, size_t ..._Indx, class _Args> |
| 2036 | inline _LIBCPP_INLINE_VISIBILITY |
| 2037 | typename __bind_return<_F, _BoundArgs, _Args>::type |
| 2038 | __apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>, |
| 2039 | _Args&& __args) |
| 2040 | { |
| 2041 | return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...); |
| 2042 | } |
| 2043 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2044 | template<class _F, class ..._BoundArgs> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2045 | class __bind |
| 2046 | { |
| 2047 | _F __f_; |
| 2048 | tuple<_BoundArgs...> __bound_args_; |
| 2049 | |
| 2050 | typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices; |
| 2051 | public: |
| 2052 | template <class _G, class ..._BA> |
| 2053 | explicit __bind(_G&& __f, _BA&& ...__bound_args) |
| 2054 | : __f_(_STD::forward<_G>(__f)), |
| 2055 | __bound_args_(_STD::forward<_BA>(__bound_args)...) {} |
| 2056 | |
| 2057 | template <class ..._Args> |
| 2058 | typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type |
| 2059 | operator()(_Args&& ...__args) |
| 2060 | { |
| 2061 | // compiler bug workaround |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2062 | return __apply_functor(__f_, __bound_args_, __indices(), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2063 | tuple<_Args&&...>(__args...)); |
| 2064 | } |
| 2065 | |
| 2066 | template <class ..._Args> |
| 2067 | typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type |
| 2068 | operator()(_Args&& ...__args) const |
| 2069 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2070 | return __apply_functor(__f_, __bound_args_, __indices(), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2071 | tuple<_Args&&...>(__args...)); |
| 2072 | } |
| 2073 | }; |
| 2074 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2075 | template<class _F, class ..._BoundArgs> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2076 | struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {}; |
| 2077 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2078 | template<class _R, class _F, class ..._BoundArgs> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2079 | class __bind_r |
| 2080 | : public __bind<_F, _BoundArgs...> |
| 2081 | { |
| 2082 | typedef __bind<_F, _BoundArgs...> base; |
| 2083 | public: |
| 2084 | typedef _R result_type; |
| 2085 | |
| 2086 | template <class _G, class ..._BA> |
| 2087 | explicit __bind_r(_G&& __f, _BA&& ...__bound_args) |
| 2088 | : base(_STD::forward<_G>(__f), |
| 2089 | _STD::forward<_BA>(__bound_args)...) {} |
| 2090 | |
| 2091 | template <class ..._Args> |
| 2092 | result_type |
| 2093 | operator()(_Args&& ...__args) |
| 2094 | { |
| 2095 | return base::operator()(_STD::forward<_Args>(__args)...); |
| 2096 | } |
| 2097 | |
| 2098 | template <class ..._Args> |
| 2099 | result_type |
| 2100 | operator()(_Args&& ...__args) const |
| 2101 | { |
| 2102 | return base::operator()(_STD::forward<_Args>(__args)...); |
| 2103 | } |
| 2104 | }; |
| 2105 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2106 | template<class _R, class _F, class ..._BoundArgs> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2107 | struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {}; |
| 2108 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2109 | template<class _F, class ..._BoundArgs> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2110 | inline _LIBCPP_INLINE_VISIBILITY |
| 2111 | __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> |
| 2112 | bind(_F&& __f, _BoundArgs&&... __bound_args) |
| 2113 | { |
| 2114 | typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type; |
| 2115 | return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...); |
| 2116 | } |
| 2117 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2118 | template<class _R, class _F, class ..._BoundArgs> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2119 | inline _LIBCPP_INLINE_VISIBILITY |
| 2120 | __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> |
| 2121 | bind(_F&& __f, _BoundArgs&&... __bound_args) |
| 2122 | { |
| 2123 | typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type; |
| 2124 | return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...); |
| 2125 | } |
| 2126 | */ |
| 2127 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2128 | #endif // _LIBCPP_FUNCTIONAL_03 |