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