1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H
11 #define _LIBCPP___FUNCTIONAL_FUNCTION_H
12
13 #include <__assert>
14 #include <__config>
15 #include <__functional/binary_function.h>
16 #include <__functional/invoke.h>
17 #include <__functional/unary_function.h>
18 #include <__iterator/iterator_traits.h>
19 #include <__memory/addressof.h>
20 #include <__memory/allocator_traits.h>
21 #include <__memory/compressed_pair.h>
22 #include <__memory/shared_ptr.h>
23 #include <__utility/forward.h>
24 #include <__utility/move.h>
25 #include <__utility/swap.h>
26 #include <exception>
27 #include <memory> // TODO: replace with <__memory/__builtin_new_allocator.h>
28 #include <type_traits>
29
30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31 # pragma GCC system_header
32 #endif
33
34 _LIBCPP_BEGIN_NAMESPACE_STD
35
36 // bad_function_call
37
38 _LIBCPP_DIAGNOSTIC_PUSH
39 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
40 class _LIBCPP_EXCEPTION_ABI bad_function_call
41 : public exception
42 {
43 public:
44 // Note that when a key function is not used, every translation unit that uses
45 // bad_function_call will end up containing a weak definition of the vtable and
46 // typeinfo.
47 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
48 virtual ~bad_function_call() _NOEXCEPT;
49 #else
50 virtual ~bad_function_call() _NOEXCEPT {}
51 #endif
52
53 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
54 virtual const char* what() const _NOEXCEPT;
55 #endif
56 };
57 _LIBCPP_DIAGNOSTIC_POP
58
59 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
__throw_bad_function_call()60 void __throw_bad_function_call()
61 {
62 #ifndef _LIBCPP_NO_EXCEPTIONS
63 throw bad_function_call();
64 #else
65 _VSTD::abort();
66 #endif
67 }
68
69 #if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
70 # define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
71 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
72 #else
73 # define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
74 #endif
75
76 template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
77
78 namespace __function
79 {
80
81 template<class _Rp>
82 struct __maybe_derive_from_unary_function
83 {
84 };
85
86 template<class _Rp, class _A1>
87 struct __maybe_derive_from_unary_function<_Rp(_A1)>
88 : public __unary_function<_A1, _Rp>
89 {
90 };
91
92 template<class _Rp>
93 struct __maybe_derive_from_binary_function
94 {
95 };
96
97 template<class _Rp, class _A1, class _A2>
98 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
99 : public __binary_function<_A1, _A2, _Rp>
100 {
101 };
102
103 template <class _Fp>
104 _LIBCPP_INLINE_VISIBILITY
105 bool __not_null(_Fp const&) { return true; }
106
107 template <class _Fp>
108 _LIBCPP_INLINE_VISIBILITY
109 bool __not_null(_Fp* __ptr) { return __ptr; }
110
111 template <class _Ret, class _Class>
112 _LIBCPP_INLINE_VISIBILITY
113 bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
114
115 template <class _Fp>
116 _LIBCPP_INLINE_VISIBILITY
117 bool __not_null(function<_Fp> const& __f) { return !!__f; }
118
119 #ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
120 template <class _Rp, class ..._Args>
121 _LIBCPP_INLINE_VISIBILITY
122 bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
123 #endif
124
125 } // namespace __function
126
127 #ifndef _LIBCPP_CXX03_LANG
128
129 namespace __function {
130
131 // __alloc_func holds a functor and an allocator.
132
133 template <class _Fp, class _Ap, class _FB> class __alloc_func;
134 template <class _Fp, class _FB>
135 class __default_alloc_func;
136
137 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
138 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
139 {
140 __compressed_pair<_Fp, _Ap> __f_;
141
142 public:
143 typedef _LIBCPP_NODEBUG _Fp _Target;
144 typedef _LIBCPP_NODEBUG _Ap _Alloc;
145
146 _LIBCPP_INLINE_VISIBILITY
147 const _Target& __target() const { return __f_.first(); }
148
149 // WIN32 APIs may define __allocator, so use __get_allocator instead.
150 _LIBCPP_INLINE_VISIBILITY
151 const _Alloc& __get_allocator() const { return __f_.second(); }
152
153 _LIBCPP_INLINE_VISIBILITY
154 explicit __alloc_func(_Target&& __f)
155 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
156 _VSTD::forward_as_tuple())
157 {
158 }
159
160 _LIBCPP_INLINE_VISIBILITY
161 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
162 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
163 _VSTD::forward_as_tuple(__a))
164 {
165 }
166
167 _LIBCPP_INLINE_VISIBILITY
168 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
169 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
170 _VSTD::forward_as_tuple(_VSTD::move(__a)))
171 {
172 }
173
174 _LIBCPP_INLINE_VISIBILITY
175 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
176 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
177 _VSTD::forward_as_tuple(_VSTD::move(__a)))
178 {
179 }
180
181 _LIBCPP_INLINE_VISIBILITY
182 _Rp operator()(_ArgTypes&&... __arg)
183 {
184 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
185 return _Invoker::__call(__f_.first(),
186 _VSTD::forward<_ArgTypes>(__arg)...);
187 }
188
189 _LIBCPP_INLINE_VISIBILITY
190 __alloc_func* __clone() const
191 {
192 typedef allocator_traits<_Alloc> __alloc_traits;
193 typedef
194 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
195 _AA;
196 _AA __a(__f_.second());
197 typedef __allocator_destructor<_AA> _Dp;
198 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
199 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
200 return __hold.release();
201 }
202
203 _LIBCPP_INLINE_VISIBILITY
204 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
205
206 static void __destroy_and_delete(__alloc_func* __f) {
207 typedef allocator_traits<_Alloc> __alloc_traits;
208 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
209 _FunAlloc;
210 _FunAlloc __a(__f->__get_allocator());
211 __f->destroy();
212 __a.deallocate(__f, 1);
213 }
214 };
215
216 template <class _Fp, class _Rp, class... _ArgTypes>
217 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
218 _Fp __f_;
219
220 public:
221 typedef _LIBCPP_NODEBUG _Fp _Target;
222
223 _LIBCPP_INLINE_VISIBILITY
224 const _Target& __target() const { return __f_; }
225
226 _LIBCPP_INLINE_VISIBILITY
227 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
228
229 _LIBCPP_INLINE_VISIBILITY
230 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
231
232 _LIBCPP_INLINE_VISIBILITY
233 _Rp operator()(_ArgTypes&&... __arg) {
234 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
235 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
236 }
237
238 _LIBCPP_INLINE_VISIBILITY
239 __default_alloc_func* __clone() const {
240 __builtin_new_allocator::__holder_t __hold =
241 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
242 __default_alloc_func* __res =
243 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
244 (void)__hold.release();
245 return __res;
246 }
247
248 _LIBCPP_INLINE_VISIBILITY
249 void destroy() _NOEXCEPT { __f_.~_Target(); }
250
251 static void __destroy_and_delete(__default_alloc_func* __f) {
252 __f->destroy();
253 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
254 }
255 };
256
257 // __base provides an abstract interface for copyable functors.
258
259 template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
260
261 template<class _Rp, class ..._ArgTypes>
262 class __base<_Rp(_ArgTypes...)>
263 {
264 __base(const __base&);
265 __base& operator=(const __base&);
266 public:
267 _LIBCPP_INLINE_VISIBILITY __base() {}
268 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
269 virtual __base* __clone() const = 0;
270 virtual void __clone(__base*) const = 0;
271 virtual void destroy() _NOEXCEPT = 0;
272 virtual void destroy_deallocate() _NOEXCEPT = 0;
273 virtual _Rp operator()(_ArgTypes&& ...) = 0;
274 #ifndef _LIBCPP_NO_RTTI
275 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
276 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
277 #endif // _LIBCPP_NO_RTTI
278 };
279
280 // __func implements __base for a given functor type.
281
282 template<class _FD, class _Alloc, class _FB> class __func;
283
284 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
285 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
286 : public __base<_Rp(_ArgTypes...)>
287 {
288 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
289 public:
290 _LIBCPP_INLINE_VISIBILITY
291 explicit __func(_Fp&& __f)
292 : __f_(_VSTD::move(__f)) {}
293
294 _LIBCPP_INLINE_VISIBILITY
295 explicit __func(const _Fp& __f, const _Alloc& __a)
296 : __f_(__f, __a) {}
297
298 _LIBCPP_INLINE_VISIBILITY
299 explicit __func(const _Fp& __f, _Alloc&& __a)
300 : __f_(__f, _VSTD::move(__a)) {}
301
302 _LIBCPP_INLINE_VISIBILITY
303 explicit __func(_Fp&& __f, _Alloc&& __a)
304 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
305
306 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
307 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
308 virtual void destroy() _NOEXCEPT;
309 virtual void destroy_deallocate() _NOEXCEPT;
310 virtual _Rp operator()(_ArgTypes&&... __arg);
311 #ifndef _LIBCPP_NO_RTTI
312 virtual const void* target(const type_info&) const _NOEXCEPT;
313 virtual const std::type_info& target_type() const _NOEXCEPT;
314 #endif // _LIBCPP_NO_RTTI
315 };
316
317 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
318 __base<_Rp(_ArgTypes...)>*
319 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
320 {
321 typedef allocator_traits<_Alloc> __alloc_traits;
322 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
323 _Ap __a(__f_.__get_allocator());
324 typedef __allocator_destructor<_Ap> _Dp;
325 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
326 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
327 return __hold.release();
328 }
329
330 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
331 void
332 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
333 {
334 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
335 }
336
337 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
338 void
339 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
340 {
341 __f_.destroy();
342 }
343
344 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
345 void
346 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
347 {
348 typedef allocator_traits<_Alloc> __alloc_traits;
349 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
350 _Ap __a(__f_.__get_allocator());
351 __f_.destroy();
352 __a.deallocate(this, 1);
353 }
354
355 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
356 _Rp
357 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
358 {
359 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
360 }
361
362 #ifndef _LIBCPP_NO_RTTI
363
364 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
365 const void*
366 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
367 {
368 if (__ti == typeid(_Fp))
369 return _VSTD::addressof(__f_.__target());
370 return nullptr;
371 }
372
373 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
374 const std::type_info&
375 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
376 {
377 return typeid(_Fp);
378 }
379
380 #endif // _LIBCPP_NO_RTTI
381
382 // __value_func creates a value-type from a __func.
383
384 template <class _Fp> class __value_func;
385
386 template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
387 {
388 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
389
390 typedef __base<_Rp(_ArgTypes...)> __func;
391 __func* __f_;
392
393 _LIBCPP_NO_CFI static __func* __as_base(void* __p)
394 {
395 return reinterpret_cast<__func*>(__p);
396 }
397
398 public:
399 _LIBCPP_INLINE_VISIBILITY
400 __value_func() _NOEXCEPT : __f_(nullptr) {}
401
402 template <class _Fp, class _Alloc>
403 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
404 : __f_(nullptr)
405 {
406 typedef allocator_traits<_Alloc> __alloc_traits;
407 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
408 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
409 _FunAlloc;
410
411 if (__function::__not_null(__f))
412 {
413 _FunAlloc __af(__a);
414 if (sizeof(_Fun) <= sizeof(__buf_) &&
415 is_nothrow_copy_constructible<_Fp>::value &&
416 is_nothrow_copy_constructible<_FunAlloc>::value)
417 {
418 __f_ =
419 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
420 }
421 else
422 {
423 typedef __allocator_destructor<_FunAlloc> _Dp;
424 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
425 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
426 __f_ = __hold.release();
427 }
428 }
429 }
430
431 template <class _Fp,
432 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
433 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
434 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
435
436 _LIBCPP_INLINE_VISIBILITY
437 __value_func(const __value_func& __f)
438 {
439 if (__f.__f_ == nullptr)
440 __f_ = nullptr;
441 else if ((void*)__f.__f_ == &__f.__buf_)
442 {
443 __f_ = __as_base(&__buf_);
444 __f.__f_->__clone(__f_);
445 }
446 else
447 __f_ = __f.__f_->__clone();
448 }
449
450 _LIBCPP_INLINE_VISIBILITY
451 __value_func(__value_func&& __f) _NOEXCEPT
452 {
453 if (__f.__f_ == nullptr)
454 __f_ = nullptr;
455 else if ((void*)__f.__f_ == &__f.__buf_)
456 {
457 __f_ = __as_base(&__buf_);
458 __f.__f_->__clone(__f_);
459 }
460 else
461 {
462 __f_ = __f.__f_;
463 __f.__f_ = nullptr;
464 }
465 }
466
467 _LIBCPP_INLINE_VISIBILITY
468 ~__value_func()
469 {
470 if ((void*)__f_ == &__buf_)
471 __f_->destroy();
472 else if (__f_)
473 __f_->destroy_deallocate();
474 }
475
476 _LIBCPP_INLINE_VISIBILITY
477 __value_func& operator=(__value_func&& __f)
478 {
479 *this = nullptr;
480 if (__f.__f_ == nullptr)
481 __f_ = nullptr;
482 else if ((void*)__f.__f_ == &__f.__buf_)
483 {
484 __f_ = __as_base(&__buf_);
485 __f.__f_->__clone(__f_);
486 }
487 else
488 {
489 __f_ = __f.__f_;
490 __f.__f_ = nullptr;
491 }
492 return *this;
493 }
494
495 _LIBCPP_INLINE_VISIBILITY
496 __value_func& operator=(nullptr_t)
497 {
498 __func* __f = __f_;
499 __f_ = nullptr;
500 if ((void*)__f == &__buf_)
501 __f->destroy();
502 else if (__f)
503 __f->destroy_deallocate();
504 return *this;
505 }
506
507 _LIBCPP_INLINE_VISIBILITY
508 _Rp operator()(_ArgTypes&&... __args) const
509 {
510 if (__f_ == nullptr)
511 __throw_bad_function_call();
512 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
513 }
514
515 _LIBCPP_INLINE_VISIBILITY
516 void swap(__value_func& __f) _NOEXCEPT
517 {
518 if (&__f == this)
519 return;
520 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
521 {
522 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
523 __func* __t = __as_base(&__tempbuf);
524 __f_->__clone(__t);
525 __f_->destroy();
526 __f_ = nullptr;
527 __f.__f_->__clone(__as_base(&__buf_));
528 __f.__f_->destroy();
529 __f.__f_ = nullptr;
530 __f_ = __as_base(&__buf_);
531 __t->__clone(__as_base(&__f.__buf_));
532 __t->destroy();
533 __f.__f_ = __as_base(&__f.__buf_);
534 }
535 else if ((void*)__f_ == &__buf_)
536 {
537 __f_->__clone(__as_base(&__f.__buf_));
538 __f_->destroy();
539 __f_ = __f.__f_;
540 __f.__f_ = __as_base(&__f.__buf_);
541 }
542 else if ((void*)__f.__f_ == &__f.__buf_)
543 {
544 __f.__f_->__clone(__as_base(&__buf_));
545 __f.__f_->destroy();
546 __f.__f_ = __f_;
547 __f_ = __as_base(&__buf_);
548 }
549 else
550 _VSTD::swap(__f_, __f.__f_);
551 }
552
553 _LIBCPP_INLINE_VISIBILITY
554 explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
555
556 #ifndef _LIBCPP_NO_RTTI
557 _LIBCPP_INLINE_VISIBILITY
558 const std::type_info& target_type() const _NOEXCEPT
559 {
560 if (__f_ == nullptr)
561 return typeid(void);
562 return __f_->target_type();
563 }
564
565 template <typename _Tp>
566 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
567 {
568 if (__f_ == nullptr)
569 return nullptr;
570 return (const _Tp*)__f_->target(typeid(_Tp));
571 }
572 #endif // _LIBCPP_NO_RTTI
573 };
574
575 // Storage for a functor object, to be used with __policy to manage copy and
576 // destruction.
577 union __policy_storage
578 {
579 mutable char __small[sizeof(void*) * 2];
580 void* __large;
581 };
582
583 // True if _Fun can safely be held in __policy_storage.__small.
584 template <typename _Fun>
585 struct __use_small_storage
586 : public integral_constant<
587 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
588 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
589 is_trivially_copy_constructible<_Fun>::value &&
590 is_trivially_destructible<_Fun>::value> {};
591
592 // Policy contains information about how to copy, destroy, and move the
593 // underlying functor. You can think of it as a vtable of sorts.
594 struct __policy
595 {
596 // Used to copy or destroy __large values. null for trivial objects.
597 void* (*const __clone)(const void*);
598 void (*const __destroy)(void*);
599
600 // True if this is the null policy (no value).
601 const bool __is_null;
602
603 // The target type. May be null if RTTI is disabled.
604 const std::type_info* const __type_info;
605
606 // Returns a pointer to a static policy object suitable for the functor
607 // type.
608 template <typename _Fun>
609 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
610 {
611 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
612 }
613
614 _LIBCPP_INLINE_VISIBILITY
615 static const __policy* __create_empty()
616 {
617 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
618 true,
619 #ifndef _LIBCPP_NO_RTTI
620 &typeid(void)
621 #else
622 nullptr
623 #endif
624 };
625 return &__policy_;
626 }
627
628 private:
629 template <typename _Fun> static void* __large_clone(const void* __s)
630 {
631 const _Fun* __f = static_cast<const _Fun*>(__s);
632 return __f->__clone();
633 }
634
635 template <typename _Fun>
636 static void __large_destroy(void* __s) {
637 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
638 }
639
640 template <typename _Fun>
641 _LIBCPP_INLINE_VISIBILITY static const __policy*
642 __choose_policy(/* is_small = */ false_type) {
643 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
644 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
645 #ifndef _LIBCPP_NO_RTTI
646 &typeid(typename _Fun::_Target)
647 #else
648 nullptr
649 #endif
650 };
651 return &__policy_;
652 }
653
654 template <typename _Fun>
655 _LIBCPP_INLINE_VISIBILITY static const __policy*
656 __choose_policy(/* is_small = */ true_type)
657 {
658 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
659 nullptr, nullptr, false,
660 #ifndef _LIBCPP_NO_RTTI
661 &typeid(typename _Fun::_Target)
662 #else
663 nullptr
664 #endif
665 };
666 return &__policy_;
667 }
668 };
669
670 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
671 // faster for types that can be passed in registers.
672 template <typename _Tp>
673 using __fast_forward =
674 typename conditional<is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
675
676 // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
677
678 template <class _Fp> struct __policy_invoker;
679
680 template <class _Rp, class... _ArgTypes>
681 struct __policy_invoker<_Rp(_ArgTypes...)>
682 {
683 typedef _Rp (*__Call)(const __policy_storage*,
684 __fast_forward<_ArgTypes>...);
685
686 __Call __call_;
687
688 // Creates an invoker that throws bad_function_call.
689 _LIBCPP_INLINE_VISIBILITY
690 __policy_invoker() : __call_(&__call_empty) {}
691
692 // Creates an invoker that calls the given instance of __func.
693 template <typename _Fun>
694 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
695 {
696 return __policy_invoker(&__call_impl<_Fun>);
697 }
698
699 private:
700 _LIBCPP_INLINE_VISIBILITY
701 explicit __policy_invoker(__Call __c) : __call_(__c) {}
702
703 static _Rp __call_empty(const __policy_storage*,
704 __fast_forward<_ArgTypes>...)
705 {
706 __throw_bad_function_call();
707 }
708
709 template <typename _Fun>
710 static _Rp __call_impl(const __policy_storage* __buf,
711 __fast_forward<_ArgTypes>... __args)
712 {
713 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
714 ? &__buf->__small
715 : __buf->__large);
716 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
717 }
718 };
719
720 // __policy_func uses a __policy and __policy_invoker to create a type-erased,
721 // copyable functor.
722
723 template <class _Fp> class __policy_func;
724
725 template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
726 {
727 // Inline storage for small objects.
728 __policy_storage __buf_;
729
730 // Calls the value stored in __buf_. This could technically be part of
731 // policy, but storing it here eliminates a level of indirection inside
732 // operator().
733 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
734 __invoker __invoker_;
735
736 // The policy that describes how to move / copy / destroy __buf_. Never
737 // null, even if the function is empty.
738 const __policy* __policy_;
739
740 public:
741 _LIBCPP_INLINE_VISIBILITY
742 __policy_func() : __policy_(__policy::__create_empty()) {}
743
744 template <class _Fp, class _Alloc>
745 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
746 : __policy_(__policy::__create_empty())
747 {
748 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
749 typedef allocator_traits<_Alloc> __alloc_traits;
750 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
751 _FunAlloc;
752
753 if (__function::__not_null(__f))
754 {
755 __invoker_ = __invoker::template __create<_Fun>();
756 __policy_ = __policy::__create<_Fun>();
757
758 _FunAlloc __af(__a);
759 if (__use_small_storage<_Fun>())
760 {
761 ::new ((void*)&__buf_.__small)
762 _Fun(_VSTD::move(__f), _Alloc(__af));
763 }
764 else
765 {
766 typedef __allocator_destructor<_FunAlloc> _Dp;
767 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
768 ::new ((void*)__hold.get())
769 _Fun(_VSTD::move(__f), _Alloc(__af));
770 __buf_.__large = __hold.release();
771 }
772 }
773 }
774
775 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
776 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
777 : __policy_(__policy::__create_empty()) {
778 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
779
780 if (__function::__not_null(__f)) {
781 __invoker_ = __invoker::template __create<_Fun>();
782 __policy_ = __policy::__create<_Fun>();
783 if (__use_small_storage<_Fun>()) {
784 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
785 } else {
786 __builtin_new_allocator::__holder_t __hold =
787 __builtin_new_allocator::__allocate_type<_Fun>(1);
788 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
789 (void)__hold.release();
790 }
791 }
792 }
793
794 _LIBCPP_INLINE_VISIBILITY
795 __policy_func(const __policy_func& __f)
796 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
797 __policy_(__f.__policy_)
798 {
799 if (__policy_->__clone)
800 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
801 }
802
803 _LIBCPP_INLINE_VISIBILITY
804 __policy_func(__policy_func&& __f)
805 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
806 __policy_(__f.__policy_)
807 {
808 if (__policy_->__destroy)
809 {
810 __f.__policy_ = __policy::__create_empty();
811 __f.__invoker_ = __invoker();
812 }
813 }
814
815 _LIBCPP_INLINE_VISIBILITY
816 ~__policy_func()
817 {
818 if (__policy_->__destroy)
819 __policy_->__destroy(__buf_.__large);
820 }
821
822 _LIBCPP_INLINE_VISIBILITY
823 __policy_func& operator=(__policy_func&& __f)
824 {
825 *this = nullptr;
826 __buf_ = __f.__buf_;
827 __invoker_ = __f.__invoker_;
828 __policy_ = __f.__policy_;
829 __f.__policy_ = __policy::__create_empty();
830 __f.__invoker_ = __invoker();
831 return *this;
832 }
833
834 _LIBCPP_INLINE_VISIBILITY
835 __policy_func& operator=(nullptr_t)
836 {
837 const __policy* __p = __policy_;
838 __policy_ = __policy::__create_empty();
839 __invoker_ = __invoker();
840 if (__p->__destroy)
841 __p->__destroy(__buf_.__large);
842 return *this;
843 }
844
845 _LIBCPP_INLINE_VISIBILITY
846 _Rp operator()(_ArgTypes&&... __args) const
847 {
848 return __invoker_.__call_(_VSTD::addressof(__buf_),
849 _VSTD::forward<_ArgTypes>(__args)...);
850 }
851
852 _LIBCPP_INLINE_VISIBILITY
853 void swap(__policy_func& __f)
854 {
855 _VSTD::swap(__invoker_, __f.__invoker_);
856 _VSTD::swap(__policy_, __f.__policy_);
857 _VSTD::swap(__buf_, __f.__buf_);
858 }
859
860 _LIBCPP_INLINE_VISIBILITY
861 explicit operator bool() const _NOEXCEPT
862 {
863 return !__policy_->__is_null;
864 }
865
866 #ifndef _LIBCPP_NO_RTTI
867 _LIBCPP_INLINE_VISIBILITY
868 const std::type_info& target_type() const _NOEXCEPT
869 {
870 return *__policy_->__type_info;
871 }
872
873 template <typename _Tp>
874 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
875 {
876 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
877 return nullptr;
878 if (__policy_->__clone) // Out of line storage.
879 return reinterpret_cast<const _Tp*>(__buf_.__large);
880 else
881 return reinterpret_cast<const _Tp*>(&__buf_.__small);
882 }
883 #endif // _LIBCPP_NO_RTTI
884 };
885
886 #if defined(_LIBCPP_HAS_BLOCKS_RUNTIME)
887
888 extern "C" void *_Block_copy(const void *);
889 extern "C" void _Block_release(const void *);
890
891 template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
892 class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
893 : public __base<_Rp(_ArgTypes...)>
894 {
895 typedef _Rp1(^__block_type)(_ArgTypes1...);
896 __block_type __f_;
897
898 public:
899 _LIBCPP_INLINE_VISIBILITY
900 explicit __func(__block_type const& __f)
901 #ifdef _LIBCPP_HAS_OBJC_ARC
902 : __f_(__f)
903 #else
904 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
905 #endif
906 { }
907
908 // [TODO] add && to save on a retain
909
910 _LIBCPP_INLINE_VISIBILITY
911 explicit __func(__block_type __f, const _Alloc& /* unused */)
912 #ifdef _LIBCPP_HAS_OBJC_ARC
913 : __f_(__f)
914 #else
915 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
916 #endif
917 { }
918
919 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
920 _LIBCPP_ASSERT(false,
921 "Block pointers are just pointers, so they should always fit into "
922 "std::function's small buffer optimization. This function should "
923 "never be invoked.");
924 return nullptr;
925 }
926
927 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
928 ::new ((void*)__p) __func(__f_);
929 }
930
931 virtual void destroy() _NOEXCEPT {
932 #ifndef _LIBCPP_HAS_OBJC_ARC
933 if (__f_)
934 _Block_release(__f_);
935 #endif
936 __f_ = 0;
937 }
938
939 virtual void destroy_deallocate() _NOEXCEPT {
940 _LIBCPP_ASSERT(false,
941 "Block pointers are just pointers, so they should always fit into "
942 "std::function's small buffer optimization. This function should "
943 "never be invoked.");
944 }
945
946 virtual _Rp operator()(_ArgTypes&& ... __arg) {
947 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
948 }
949
950 #ifndef _LIBCPP_NO_RTTI
951 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
952 if (__ti == typeid(__func::__block_type))
953 return &__f_;
954 return (const void*)nullptr;
955 }
956
957 virtual const std::type_info& target_type() const _NOEXCEPT {
958 return typeid(__func::__block_type);
959 }
960 #endif // _LIBCPP_NO_RTTI
961 };
962
963 #endif // _LIBCPP_HAS_EXTENSION_BLOCKS
964
965 } // namespace __function
966
967 template<class _Rp, class ..._ArgTypes>
968 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
969 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
970 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
971 {
972 #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
973 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
974 #else
975 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
976 #endif
977
978 __func __f_;
979
980 template <class _Fp, bool = _And<
981 _IsNotSame<__uncvref_t<_Fp>, function>,
982 __invokable<_Fp, _ArgTypes...>
983 >::value>
984 struct __callable;
985 template <class _Fp>
986 struct __callable<_Fp, true>
987 {
988 static const bool value = is_void<_Rp>::value ||
989 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
990 _Rp>::value;
991 };
992 template <class _Fp>
993 struct __callable<_Fp, false>
994 {
995 static const bool value = false;
996 };
997
998 template <class _Fp>
999 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
1000 public:
1001 typedef _Rp result_type;
1002
1003 // construct/copy/destroy:
1004 _LIBCPP_INLINE_VISIBILITY
1005 function() _NOEXCEPT { }
1006 _LIBCPP_INLINE_VISIBILITY
1007 function(nullptr_t) _NOEXCEPT {}
1008 function(const function&);
1009 function(function&&) _NOEXCEPT;
1010 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
1011 function(_Fp);
1012
1013 #if _LIBCPP_STD_VER <= 14
1014 template<class _Alloc>
1015 _LIBCPP_INLINE_VISIBILITY
1016 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
1017 template<class _Alloc>
1018 _LIBCPP_INLINE_VISIBILITY
1019 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
1020 template<class _Alloc>
1021 function(allocator_arg_t, const _Alloc&, const function&);
1022 template<class _Alloc>
1023 function(allocator_arg_t, const _Alloc&, function&&);
1024 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
1025 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
1026 #endif
1027
1028 function& operator=(const function&);
1029 function& operator=(function&&) _NOEXCEPT;
1030 function& operator=(nullptr_t) _NOEXCEPT;
1031 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
1032 function& operator=(_Fp&&);
1033
1034 ~function();
1035
1036 // function modifiers:
1037 void swap(function&) _NOEXCEPT;
1038
1039 #if _LIBCPP_STD_VER <= 14
1040 template<class _Fp, class _Alloc>
1041 _LIBCPP_INLINE_VISIBILITY
1042 void assign(_Fp&& __f, const _Alloc& __a)
1043 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
1044 #endif
1045
1046 // function capacity:
1047 _LIBCPP_INLINE_VISIBILITY
1048 explicit operator bool() const _NOEXCEPT {
1049 return static_cast<bool>(__f_);
1050 }
1051
1052 // deleted overloads close possible hole in the type system
1053 template<class _R2, class... _ArgTypes2>
1054 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
1055 template<class _R2, class... _ArgTypes2>
1056 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
1057 public:
1058 // function invocation:
1059 _Rp operator()(_ArgTypes...) const;
1060
1061 #ifndef _LIBCPP_NO_RTTI
1062 // function target access:
1063 const std::type_info& target_type() const _NOEXCEPT;
1064 template <typename _Tp> _Tp* target() _NOEXCEPT;
1065 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
1066 #endif // _LIBCPP_NO_RTTI
1067 };
1068
1069 #if _LIBCPP_STD_VER >= 17
1070 template<class _Rp, class ..._Ap>
1071 function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
1072
1073 template<class _Fp>
1074 struct __strip_signature;
1075
1076 template<class _Rp, class _Gp, class ..._Ap>
1077 struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
1078 template<class _Rp, class _Gp, class ..._Ap>
1079 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
1080 template<class _Rp, class _Gp, class ..._Ap>
1081 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
1082 template<class _Rp, class _Gp, class ..._Ap>
1083 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
1084
1085 template<class _Rp, class _Gp, class ..._Ap>
1086 struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
1087 template<class _Rp, class _Gp, class ..._Ap>
1088 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
1089 template<class _Rp, class _Gp, class ..._Ap>
1090 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
1091 template<class _Rp, class _Gp, class ..._Ap>
1092 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
1093
1094 template<class _Rp, class _Gp, class ..._Ap>
1095 struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
1096 template<class _Rp, class _Gp, class ..._Ap>
1097 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
1098 template<class _Rp, class _Gp, class ..._Ap>
1099 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
1100 template<class _Rp, class _Gp, class ..._Ap>
1101 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
1102
1103 template<class _Rp, class _Gp, class ..._Ap>
1104 struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
1105 template<class _Rp, class _Gp, class ..._Ap>
1106 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
1107 template<class _Rp, class _Gp, class ..._Ap>
1108 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
1109 template<class _Rp, class _Gp, class ..._Ap>
1110 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
1111
1112 template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1113 function(_Fp) -> function<_Stripped>;
1114 #endif // _LIBCPP_STD_VER >= 17
1115
1116 template<class _Rp, class ..._ArgTypes>
1117 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
1118
1119 #if _LIBCPP_STD_VER <= 14
1120 template<class _Rp, class ..._ArgTypes>
1121 template <class _Alloc>
1122 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1123 const function& __f) : __f_(__f.__f_) {}
1124 #endif
1125
1126 template <class _Rp, class... _ArgTypes>
1127 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
1128 : __f_(_VSTD::move(__f.__f_)) {}
1129
1130 #if _LIBCPP_STD_VER <= 14
1131 template<class _Rp, class ..._ArgTypes>
1132 template <class _Alloc>
1133 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1134 function&& __f)
1135 : __f_(_VSTD::move(__f.__f_)) {}
1136 #endif
1137
1138 template <class _Rp, class... _ArgTypes>
1139 template <class _Fp, class>
1140 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
1141
1142 #if _LIBCPP_STD_VER <= 14
1143 template <class _Rp, class... _ArgTypes>
1144 template <class _Fp, class _Alloc, class>
1145 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
1146 _Fp __f)
1147 : __f_(_VSTD::move(__f), __a) {}
1148 #endif
1149
1150 template<class _Rp, class ..._ArgTypes>
1151 function<_Rp(_ArgTypes...)>&
1152 function<_Rp(_ArgTypes...)>::operator=(const function& __f)
1153 {
1154 function(__f).swap(*this);
1155 return *this;
1156 }
1157
1158 template<class _Rp, class ..._ArgTypes>
1159 function<_Rp(_ArgTypes...)>&
1160 function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
1161 {
1162 __f_ = _VSTD::move(__f.__f_);
1163 return *this;
1164 }
1165
1166 template<class _Rp, class ..._ArgTypes>
1167 function<_Rp(_ArgTypes...)>&
1168 function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
1169 {
1170 __f_ = nullptr;
1171 return *this;
1172 }
1173
1174 template<class _Rp, class ..._ArgTypes>
1175 template <class _Fp, class>
1176 function<_Rp(_ArgTypes...)>&
1177 function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
1178 {
1179 function(_VSTD::forward<_Fp>(__f)).swap(*this);
1180 return *this;
1181 }
1182
1183 template<class _Rp, class ..._ArgTypes>
1184 function<_Rp(_ArgTypes...)>::~function() {}
1185
1186 template<class _Rp, class ..._ArgTypes>
1187 void
1188 function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
1189 {
1190 __f_.swap(__f.__f_);
1191 }
1192
1193 template<class _Rp, class ..._ArgTypes>
1194 _Rp
1195 function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1196 {
1197 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
1198 }
1199
1200 #ifndef _LIBCPP_NO_RTTI
1201
1202 template<class _Rp, class ..._ArgTypes>
1203 const std::type_info&
1204 function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1205 {
1206 return __f_.target_type();
1207 }
1208
1209 template<class _Rp, class ..._ArgTypes>
1210 template <typename _Tp>
1211 _Tp*
1212 function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
1213 {
1214 return (_Tp*)(__f_.template target<_Tp>());
1215 }
1216
1217 template<class _Rp, class ..._ArgTypes>
1218 template <typename _Tp>
1219 const _Tp*
1220 function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
1221 {
1222 return __f_.template target<_Tp>();
1223 }
1224
1225 #endif // _LIBCPP_NO_RTTI
1226
1227 template <class _Rp, class... _ArgTypes>
1228 inline _LIBCPP_INLINE_VISIBILITY
1229 bool
1230 operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
1231
1232 template <class _Rp, class... _ArgTypes>
1233 inline _LIBCPP_INLINE_VISIBILITY
1234 bool
1235 operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
1236
1237 template <class _Rp, class... _ArgTypes>
1238 inline _LIBCPP_INLINE_VISIBILITY
1239 bool
1240 operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
1241
1242 template <class _Rp, class... _ArgTypes>
1243 inline _LIBCPP_INLINE_VISIBILITY
1244 bool
1245 operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
1246
1247 template <class _Rp, class... _ArgTypes>
1248 inline _LIBCPP_INLINE_VISIBILITY
1249 void
1250 swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
1251 {return __x.swap(__y);}
1252
1253 #elif defined(_LIBCPP_ENABLE_CXX03_FUNCTION)
1254
1255 namespace __function {
1256
1257 template<class _Fp> class __base;
1258
1259 template<class _Rp>
1260 class __base<_Rp()>
1261 {
1262 __base(const __base&);
1263 __base& operator=(const __base&);
1264 public:
1265 __base() {}
1266 virtual ~__base() {}
1267 virtual __base* __clone() const = 0;
1268 virtual void __clone(__base*) const = 0;
1269 virtual void destroy() = 0;
1270 virtual void destroy_deallocate() = 0;
1271 virtual _Rp operator()() = 0;
1272 #ifndef _LIBCPP_NO_RTTI
1273 virtual const void* target(const type_info&) const = 0;
1274 virtual const std::type_info& target_type() const = 0;
1275 #endif // _LIBCPP_NO_RTTI
1276 };
1277
1278 template<class _Rp, class _A0>
1279 class __base<_Rp(_A0)>
1280 {
1281 __base(const __base&);
1282 __base& operator=(const __base&);
1283 public:
1284 __base() {}
1285 virtual ~__base() {}
1286 virtual __base* __clone() const = 0;
1287 virtual void __clone(__base*) const = 0;
1288 virtual void destroy() = 0;
1289 virtual void destroy_deallocate() = 0;
1290 virtual _Rp operator()(_A0) = 0;
1291 #ifndef _LIBCPP_NO_RTTI
1292 virtual const void* target(const type_info&) const = 0;
1293 virtual const std::type_info& target_type() const = 0;
1294 #endif // _LIBCPP_NO_RTTI
1295 };
1296
1297 template<class _Rp, class _A0, class _A1>
1298 class __base<_Rp(_A0, _A1)>
1299 {
1300 __base(const __base&);
1301 __base& operator=(const __base&);
1302 public:
1303 __base() {}
1304 virtual ~__base() {}
1305 virtual __base* __clone() const = 0;
1306 virtual void __clone(__base*) const = 0;
1307 virtual void destroy() = 0;
1308 virtual void destroy_deallocate() = 0;
1309 virtual _Rp operator()(_A0, _A1) = 0;
1310 #ifndef _LIBCPP_NO_RTTI
1311 virtual const void* target(const type_info&) const = 0;
1312 virtual const std::type_info& target_type() const = 0;
1313 #endif // _LIBCPP_NO_RTTI
1314 };
1315
1316 template<class _Rp, class _A0, class _A1, class _A2>
1317 class __base<_Rp(_A0, _A1, _A2)>
1318 {
1319 __base(const __base&);
1320 __base& operator=(const __base&);
1321 public:
1322 __base() {}
1323 virtual ~__base() {}
1324 virtual __base* __clone() const = 0;
1325 virtual void __clone(__base*) const = 0;
1326 virtual void destroy() = 0;
1327 virtual void destroy_deallocate() = 0;
1328 virtual _Rp operator()(_A0, _A1, _A2) = 0;
1329 #ifndef _LIBCPP_NO_RTTI
1330 virtual const void* target(const type_info&) const = 0;
1331 virtual const std::type_info& target_type() const = 0;
1332 #endif // _LIBCPP_NO_RTTI
1333 };
1334
1335 template<class _FD, class _Alloc, class _FB> class __func;
1336
1337 template<class _Fp, class _Alloc, class _Rp>
1338 class __func<_Fp, _Alloc, _Rp()>
1339 : public __base<_Rp()>
1340 {
1341 __compressed_pair<_Fp, _Alloc> __f_;
1342 public:
1343 explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1344 explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1345 virtual __base<_Rp()>* __clone() const;
1346 virtual void __clone(__base<_Rp()>*) const;
1347 virtual void destroy();
1348 virtual void destroy_deallocate();
1349 virtual _Rp operator()();
1350 #ifndef _LIBCPP_NO_RTTI
1351 virtual const void* target(const type_info&) const;
1352 virtual const std::type_info& target_type() const;
1353 #endif // _LIBCPP_NO_RTTI
1354 };
1355
1356 template<class _Fp, class _Alloc, class _Rp>
1357 __base<_Rp()>*
1358 __func<_Fp, _Alloc, _Rp()>::__clone() const
1359 {
1360 typedef allocator_traits<_Alloc> __alloc_traits;
1361 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1362 _Ap __a(__f_.second());
1363 typedef __allocator_destructor<_Ap> _Dp;
1364 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1365 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1366 return __hold.release();
1367 }
1368
1369 template<class _Fp, class _Alloc, class _Rp>
1370 void
1371 __func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
1372 {
1373 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1374 }
1375
1376 template<class _Fp, class _Alloc, class _Rp>
1377 void
1378 __func<_Fp, _Alloc, _Rp()>::destroy()
1379 {
1380 __f_.~__compressed_pair<_Fp, _Alloc>();
1381 }
1382
1383 template<class _Fp, class _Alloc, class _Rp>
1384 void
1385 __func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
1386 {
1387 typedef allocator_traits<_Alloc> __alloc_traits;
1388 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1389 _Ap __a(__f_.second());
1390 __f_.~__compressed_pair<_Fp, _Alloc>();
1391 __a.deallocate(this, 1);
1392 }
1393
1394 template<class _Fp, class _Alloc, class _Rp>
1395 _Rp
1396 __func<_Fp, _Alloc, _Rp()>::operator()()
1397 {
1398 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1399 return _Invoker::__call(__f_.first());
1400 }
1401
1402 #ifndef _LIBCPP_NO_RTTI
1403
1404 template<class _Fp, class _Alloc, class _Rp>
1405 const void*
1406 __func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
1407 {
1408 if (__ti == typeid(_Fp))
1409 return _VSTD::addressof(__f_.first());
1410 return (const void*)0;
1411 }
1412
1413 template<class _Fp, class _Alloc, class _Rp>
1414 const std::type_info&
1415 __func<_Fp, _Alloc, _Rp()>::target_type() const
1416 {
1417 return typeid(_Fp);
1418 }
1419
1420 #endif // _LIBCPP_NO_RTTI
1421
1422 template<class _Fp, class _Alloc, class _Rp, class _A0>
1423 class __func<_Fp, _Alloc, _Rp(_A0)>
1424 : public __base<_Rp(_A0)>
1425 {
1426 __compressed_pair<_Fp, _Alloc> __f_;
1427 public:
1428 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1429 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1430 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1431 virtual __base<_Rp(_A0)>* __clone() const;
1432 virtual void __clone(__base<_Rp(_A0)>*) const;
1433 virtual void destroy();
1434 virtual void destroy_deallocate();
1435 virtual _Rp operator()(_A0);
1436 #ifndef _LIBCPP_NO_RTTI
1437 virtual const void* target(const type_info&) const;
1438 virtual const std::type_info& target_type() const;
1439 #endif // _LIBCPP_NO_RTTI
1440 };
1441
1442 template<class _Fp, class _Alloc, class _Rp, class _A0>
1443 __base<_Rp(_A0)>*
1444 __func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
1445 {
1446 typedef allocator_traits<_Alloc> __alloc_traits;
1447 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1448 _Ap __a(__f_.second());
1449 typedef __allocator_destructor<_Ap> _Dp;
1450 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1451 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1452 return __hold.release();
1453 }
1454
1455 template<class _Fp, class _Alloc, class _Rp, class _A0>
1456 void
1457 __func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
1458 {
1459 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1460 }
1461
1462 template<class _Fp, class _Alloc, class _Rp, class _A0>
1463 void
1464 __func<_Fp, _Alloc, _Rp(_A0)>::destroy()
1465 {
1466 __f_.~__compressed_pair<_Fp, _Alloc>();
1467 }
1468
1469 template<class _Fp, class _Alloc, class _Rp, class _A0>
1470 void
1471 __func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
1472 {
1473 typedef allocator_traits<_Alloc> __alloc_traits;
1474 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1475 _Ap __a(__f_.second());
1476 __f_.~__compressed_pair<_Fp, _Alloc>();
1477 __a.deallocate(this, 1);
1478 }
1479
1480 template<class _Fp, class _Alloc, class _Rp, class _A0>
1481 _Rp
1482 __func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
1483 {
1484 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1485 return _Invoker::__call(__f_.first(), __a0);
1486 }
1487
1488 #ifndef _LIBCPP_NO_RTTI
1489
1490 template<class _Fp, class _Alloc, class _Rp, class _A0>
1491 const void*
1492 __func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
1493 {
1494 if (__ti == typeid(_Fp))
1495 return &__f_.first();
1496 return (const void*)0;
1497 }
1498
1499 template<class _Fp, class _Alloc, class _Rp, class _A0>
1500 const std::type_info&
1501 __func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
1502 {
1503 return typeid(_Fp);
1504 }
1505
1506 #endif // _LIBCPP_NO_RTTI
1507
1508 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1509 class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
1510 : public __base<_Rp(_A0, _A1)>
1511 {
1512 __compressed_pair<_Fp, _Alloc> __f_;
1513 public:
1514 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1515 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1516 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1517 virtual __base<_Rp(_A0, _A1)>* __clone() const;
1518 virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
1519 virtual void destroy();
1520 virtual void destroy_deallocate();
1521 virtual _Rp operator()(_A0, _A1);
1522 #ifndef _LIBCPP_NO_RTTI
1523 virtual const void* target(const type_info&) const;
1524 virtual const std::type_info& target_type() const;
1525 #endif // _LIBCPP_NO_RTTI
1526 };
1527
1528 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1529 __base<_Rp(_A0, _A1)>*
1530 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
1531 {
1532 typedef allocator_traits<_Alloc> __alloc_traits;
1533 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1534 _Ap __a(__f_.second());
1535 typedef __allocator_destructor<_Ap> _Dp;
1536 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1537 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1538 return __hold.release();
1539 }
1540
1541 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1542 void
1543 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
1544 {
1545 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1546 }
1547
1548 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1549 void
1550 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
1551 {
1552 __f_.~__compressed_pair<_Fp, _Alloc>();
1553 }
1554
1555 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1556 void
1557 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
1558 {
1559 typedef allocator_traits<_Alloc> __alloc_traits;
1560 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1561 _Ap __a(__f_.second());
1562 __f_.~__compressed_pair<_Fp, _Alloc>();
1563 __a.deallocate(this, 1);
1564 }
1565
1566 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1567 _Rp
1568 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
1569 {
1570 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1571 return _Invoker::__call(__f_.first(), __a0, __a1);
1572 }
1573
1574 #ifndef _LIBCPP_NO_RTTI
1575
1576 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1577 const void*
1578 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
1579 {
1580 if (__ti == typeid(_Fp))
1581 return &__f_.first();
1582 return (const void*)0;
1583 }
1584
1585 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1586 const std::type_info&
1587 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
1588 {
1589 return typeid(_Fp);
1590 }
1591
1592 #endif // _LIBCPP_NO_RTTI
1593
1594 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1595 class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
1596 : public __base<_Rp(_A0, _A1, _A2)>
1597 {
1598 __compressed_pair<_Fp, _Alloc> __f_;
1599 public:
1600 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1601 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1602 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1603 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
1604 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
1605 virtual void destroy();
1606 virtual void destroy_deallocate();
1607 virtual _Rp operator()(_A0, _A1, _A2);
1608 #ifndef _LIBCPP_NO_RTTI
1609 virtual const void* target(const type_info&) const;
1610 virtual const std::type_info& target_type() const;
1611 #endif // _LIBCPP_NO_RTTI
1612 };
1613
1614 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1615 __base<_Rp(_A0, _A1, _A2)>*
1616 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
1617 {
1618 typedef allocator_traits<_Alloc> __alloc_traits;
1619 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1620 _Ap __a(__f_.second());
1621 typedef __allocator_destructor<_Ap> _Dp;
1622 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1623 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1624 return __hold.release();
1625 }
1626
1627 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1628 void
1629 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
1630 {
1631 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1632 }
1633
1634 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1635 void
1636 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
1637 {
1638 __f_.~__compressed_pair<_Fp, _Alloc>();
1639 }
1640
1641 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1642 void
1643 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
1644 {
1645 typedef allocator_traits<_Alloc> __alloc_traits;
1646 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1647 _Ap __a(__f_.second());
1648 __f_.~__compressed_pair<_Fp, _Alloc>();
1649 __a.deallocate(this, 1);
1650 }
1651
1652 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1653 _Rp
1654 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
1655 {
1656 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1657 return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
1658 }
1659
1660 #ifndef _LIBCPP_NO_RTTI
1661
1662 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1663 const void*
1664 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
1665 {
1666 if (__ti == typeid(_Fp))
1667 return &__f_.first();
1668 return (const void*)0;
1669 }
1670
1671 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1672 const std::type_info&
1673 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
1674 {
1675 return typeid(_Fp);
1676 }
1677
1678 #endif // _LIBCPP_NO_RTTI
1679
1680 } // namespace __function
1681
1682 template<class _Rp>
1683 class _LIBCPP_TEMPLATE_VIS function<_Rp()>
1684 {
1685 typedef __function::__base<_Rp()> __base;
1686 aligned_storage<3*sizeof(void*)>::type __buf_;
1687 __base* __f_;
1688
1689 public:
1690 typedef _Rp result_type;
1691
1692 // 20.7.16.2.1, construct/copy/destroy:
1693 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1694 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1695 function(const function&);
1696 template<class _Fp>
1697 function(_Fp,
1698 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1699
1700 template<class _Alloc>
1701 _LIBCPP_INLINE_VISIBILITY
1702 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1703 template<class _Alloc>
1704 _LIBCPP_INLINE_VISIBILITY
1705 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1706 template<class _Alloc>
1707 function(allocator_arg_t, const _Alloc&, const function&);
1708 template<class _Fp, class _Alloc>
1709 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1710 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1711
1712 function& operator=(const function&);
1713 function& operator=(nullptr_t);
1714 template<class _Fp>
1715 typename enable_if
1716 <
1717 !is_integral<_Fp>::value,
1718 function&
1719 >::type
1720 operator=(_Fp);
1721
1722 ~function();
1723
1724 // 20.7.16.2.2, function modifiers:
1725 void swap(function&);
1726 template<class _Fp, class _Alloc>
1727 _LIBCPP_INLINE_VISIBILITY
1728 void assign(_Fp __f, const _Alloc& __a)
1729 {function(allocator_arg, __a, __f).swap(*this);}
1730
1731 // 20.7.16.2.3, function capacity:
1732 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
1733
1734 template<class _R2>
1735 bool operator==(const function<_R2()>&) const = delete;
1736 template<class _R2>
1737 bool operator!=(const function<_R2()>&) const = delete;
1738
1739 // 20.7.16.2.4, function invocation:
1740 _Rp operator()() const;
1741
1742 #ifndef _LIBCPP_NO_RTTI
1743 // 20.7.16.2.5, function target access:
1744 const std::type_info& target_type() const;
1745 template <typename _Tp> _Tp* target();
1746 template <typename _Tp> const _Tp* target() const;
1747 #endif // _LIBCPP_NO_RTTI
1748 };
1749
1750 template<class _Rp>
1751 function<_Rp()>::function(const function& __f)
1752 {
1753 if (__f.__f_ == 0)
1754 __f_ = 0;
1755 else if (__f.__f_ == (const __base*)&__f.__buf_)
1756 {
1757 __f_ = (__base*)&__buf_;
1758 __f.__f_->__clone(__f_);
1759 }
1760 else
1761 __f_ = __f.__f_->__clone();
1762 }
1763
1764 template<class _Rp>
1765 template<class _Alloc>
1766 function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
1767 {
1768 if (__f.__f_ == 0)
1769 __f_ = 0;
1770 else if (__f.__f_ == (const __base*)&__f.__buf_)
1771 {
1772 __f_ = (__base*)&__buf_;
1773 __f.__f_->__clone(__f_);
1774 }
1775 else
1776 __f_ = __f.__f_->__clone();
1777 }
1778
1779 template<class _Rp>
1780 template <class _Fp>
1781 function<_Rp()>::function(_Fp __f,
1782 typename enable_if<!is_integral<_Fp>::value>::type*)
1783 : __f_(0)
1784 {
1785 if (__function::__not_null(__f))
1786 {
1787 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
1788 if (sizeof(_FF) <= sizeof(__buf_))
1789 {
1790 __f_ = (__base*)&__buf_;
1791 ::new ((void*)__f_) _FF(__f);
1792 }
1793 else
1794 {
1795 typedef allocator<_FF> _Ap;
1796 _Ap __a;
1797 typedef __allocator_destructor<_Ap> _Dp;
1798 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1799 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
1800 __f_ = __hold.release();
1801 }
1802 }
1803 }
1804
1805 template<class _Rp>
1806 template <class _Fp, class _Alloc>
1807 function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1808 typename enable_if<!is_integral<_Fp>::value>::type*)
1809 : __f_(0)
1810 {
1811 typedef allocator_traits<_Alloc> __alloc_traits;
1812 if (__function::__not_null(__f))
1813 {
1814 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
1815 if (sizeof(_FF) <= sizeof(__buf_))
1816 {
1817 __f_ = (__base*)&__buf_;
1818 ::new ((void*)__f_) _FF(__f, __a0);
1819 }
1820 else
1821 {
1822 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1823 _Ap __a(__a0);
1824 typedef __allocator_destructor<_Ap> _Dp;
1825 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1826 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
1827 __f_ = __hold.release();
1828 }
1829 }
1830 }
1831
1832 template<class _Rp>
1833 function<_Rp()>&
1834 function<_Rp()>::operator=(const function& __f)
1835 {
1836 if (__f)
1837 function(__f).swap(*this);
1838 else
1839 *this = nullptr;
1840 return *this;
1841 }
1842
1843 template<class _Rp>
1844 function<_Rp()>&
1845 function<_Rp()>::operator=(nullptr_t)
1846 {
1847 __base* __t = __f_;
1848 __f_ = 0;
1849 if (__t == (__base*)&__buf_)
1850 __t->destroy();
1851 else if (__t)
1852 __t->destroy_deallocate();
1853 return *this;
1854 }
1855
1856 template<class _Rp>
1857 template <class _Fp>
1858 typename enable_if
1859 <
1860 !is_integral<_Fp>::value,
1861 function<_Rp()>&
1862 >::type
1863 function<_Rp()>::operator=(_Fp __f)
1864 {
1865 function(_VSTD::move(__f)).swap(*this);
1866 return *this;
1867 }
1868
1869 template<class _Rp>
1870 function<_Rp()>::~function()
1871 {
1872 if (__f_ == (__base*)&__buf_)
1873 __f_->destroy();
1874 else if (__f_)
1875 __f_->destroy_deallocate();
1876 }
1877
1878 template<class _Rp>
1879 void
1880 function<_Rp()>::swap(function& __f)
1881 {
1882 if (_VSTD::addressof(__f) == this)
1883 return;
1884 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1885 {
1886 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1887 __base* __t = (__base*)&__tempbuf;
1888 __f_->__clone(__t);
1889 __f_->destroy();
1890 __f_ = 0;
1891 __f.__f_->__clone((__base*)&__buf_);
1892 __f.__f_->destroy();
1893 __f.__f_ = 0;
1894 __f_ = (__base*)&__buf_;
1895 __t->__clone((__base*)&__f.__buf_);
1896 __t->destroy();
1897 __f.__f_ = (__base*)&__f.__buf_;
1898 }
1899 else if (__f_ == (__base*)&__buf_)
1900 {
1901 __f_->__clone((__base*)&__f.__buf_);
1902 __f_->destroy();
1903 __f_ = __f.__f_;
1904 __f.__f_ = (__base*)&__f.__buf_;
1905 }
1906 else if (__f.__f_ == (__base*)&__f.__buf_)
1907 {
1908 __f.__f_->__clone((__base*)&__buf_);
1909 __f.__f_->destroy();
1910 __f.__f_ = __f_;
1911 __f_ = (__base*)&__buf_;
1912 }
1913 else
1914 _VSTD::swap(__f_, __f.__f_);
1915 }
1916
1917 template<class _Rp>
1918 _Rp
1919 function<_Rp()>::operator()() const
1920 {
1921 if (__f_ == 0)
1922 __throw_bad_function_call();
1923 return (*__f_)();
1924 }
1925
1926 #ifndef _LIBCPP_NO_RTTI
1927
1928 template<class _Rp>
1929 const std::type_info&
1930 function<_Rp()>::target_type() const
1931 {
1932 if (__f_ == 0)
1933 return typeid(void);
1934 return __f_->target_type();
1935 }
1936
1937 template<class _Rp>
1938 template <typename _Tp>
1939 _Tp*
1940 function<_Rp()>::target()
1941 {
1942 if (__f_ == 0)
1943 return (_Tp*)0;
1944 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
1945 }
1946
1947 template<class _Rp>
1948 template <typename _Tp>
1949 const _Tp*
1950 function<_Rp()>::target() const
1951 {
1952 if (__f_ == 0)
1953 return (const _Tp*)0;
1954 return (const _Tp*)__f_->target(typeid(_Tp));
1955 }
1956
1957 #endif // _LIBCPP_NO_RTTI
1958
1959 template<class _Rp, class _A0>
1960 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0)>
1961 : public unary_function<_A0, _Rp>
1962 {
1963 typedef __function::__base<_Rp(_A0)> __base;
1964 aligned_storage<3*sizeof(void*)>::type __buf_;
1965 __base* __f_;
1966
1967 public:
1968 typedef _Rp result_type;
1969
1970 // 20.7.16.2.1, construct/copy/destroy:
1971 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1972 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1973 function(const function&);
1974 template<class _Fp>
1975 function(_Fp,
1976 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1977
1978 template<class _Alloc>
1979 _LIBCPP_INLINE_VISIBILITY
1980 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1981 template<class _Alloc>
1982 _LIBCPP_INLINE_VISIBILITY
1983 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1984 template<class _Alloc>
1985 function(allocator_arg_t, const _Alloc&, const function&);
1986 template<class _Fp, class _Alloc>
1987 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1988 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1989
1990 function& operator=(const function&);
1991 function& operator=(nullptr_t);
1992 template<class _Fp>
1993 typename enable_if
1994 <
1995 !is_integral<_Fp>::value,
1996 function&
1997 >::type
1998 operator=(_Fp);
1999
2000 ~function();
2001
2002 // 20.7.16.2.2, function modifiers:
2003 void swap(function&);
2004 template<class _Fp, class _Alloc>
2005 _LIBCPP_INLINE_VISIBILITY
2006 void assign(_Fp __f, const _Alloc& __a)
2007 {function(allocator_arg, __a, __f).swap(*this);}
2008
2009 // 20.7.16.2.3, function capacity:
2010 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2011
2012 template<class _R2, class _B0>
2013 bool operator==(const function<_R2(_B0)>&) const = delete;
2014 template<class _R2, class _B0>
2015 bool operator!=(const function<_R2(_B0)>&) const = delete;
2016
2017 // 20.7.16.2.4, function invocation:
2018 _Rp operator()(_A0) const;
2019
2020 #ifndef _LIBCPP_NO_RTTI
2021 // 20.7.16.2.5, function target access:
2022 const std::type_info& target_type() const;
2023 template <typename _Tp> _Tp* target();
2024 template <typename _Tp> const _Tp* target() const;
2025 #endif // _LIBCPP_NO_RTTI
2026 };
2027
2028 template<class _Rp, class _A0>
2029 function<_Rp(_A0)>::function(const function& __f)
2030 {
2031 if (__f.__f_ == 0)
2032 __f_ = 0;
2033 else if (__f.__f_ == (const __base*)&__f.__buf_)
2034 {
2035 __f_ = (__base*)&__buf_;
2036 __f.__f_->__clone(__f_);
2037 }
2038 else
2039 __f_ = __f.__f_->__clone();
2040 }
2041
2042 template<class _Rp, class _A0>
2043 template<class _Alloc>
2044 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
2045 {
2046 if (__f.__f_ == 0)
2047 __f_ = 0;
2048 else if (__f.__f_ == (const __base*)&__f.__buf_)
2049 {
2050 __f_ = (__base*)&__buf_;
2051 __f.__f_->__clone(__f_);
2052 }
2053 else
2054 __f_ = __f.__f_->__clone();
2055 }
2056
2057 template<class _Rp, class _A0>
2058 template <class _Fp>
2059 function<_Rp(_A0)>::function(_Fp __f,
2060 typename enable_if<!is_integral<_Fp>::value>::type*)
2061 : __f_(0)
2062 {
2063 if (__function::__not_null(__f))
2064 {
2065 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
2066 if (sizeof(_FF) <= sizeof(__buf_))
2067 {
2068 __f_ = (__base*)&__buf_;
2069 ::new ((void*)__f_) _FF(__f);
2070 }
2071 else
2072 {
2073 typedef allocator<_FF> _Ap;
2074 _Ap __a;
2075 typedef __allocator_destructor<_Ap> _Dp;
2076 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2077 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2078 __f_ = __hold.release();
2079 }
2080 }
2081 }
2082
2083 template<class _Rp, class _A0>
2084 template <class _Fp, class _Alloc>
2085 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2086 typename enable_if<!is_integral<_Fp>::value>::type*)
2087 : __f_(0)
2088 {
2089 typedef allocator_traits<_Alloc> __alloc_traits;
2090 if (__function::__not_null(__f))
2091 {
2092 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
2093 if (sizeof(_FF) <= sizeof(__buf_))
2094 {
2095 __f_ = (__base*)&__buf_;
2096 ::new ((void*)__f_) _FF(__f, __a0);
2097 }
2098 else
2099 {
2100 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2101 _Ap __a(__a0);
2102 typedef __allocator_destructor<_Ap> _Dp;
2103 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2104 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2105 __f_ = __hold.release();
2106 }
2107 }
2108 }
2109
2110 template<class _Rp, class _A0>
2111 function<_Rp(_A0)>&
2112 function<_Rp(_A0)>::operator=(const function& __f)
2113 {
2114 if (__f)
2115 function(__f).swap(*this);
2116 else
2117 *this = nullptr;
2118 return *this;
2119 }
2120
2121 template<class _Rp, class _A0>
2122 function<_Rp(_A0)>&
2123 function<_Rp(_A0)>::operator=(nullptr_t)
2124 {
2125 __base* __t = __f_;
2126 __f_ = 0;
2127 if (__t == (__base*)&__buf_)
2128 __t->destroy();
2129 else if (__t)
2130 __t->destroy_deallocate();
2131 return *this;
2132 }
2133
2134 template<class _Rp, class _A0>
2135 template <class _Fp>
2136 typename enable_if
2137 <
2138 !is_integral<_Fp>::value,
2139 function<_Rp(_A0)>&
2140 >::type
2141 function<_Rp(_A0)>::operator=(_Fp __f)
2142 {
2143 function(_VSTD::move(__f)).swap(*this);
2144 return *this;
2145 }
2146
2147 template<class _Rp, class _A0>
2148 function<_Rp(_A0)>::~function()
2149 {
2150 if (__f_ == (__base*)&__buf_)
2151 __f_->destroy();
2152 else if (__f_)
2153 __f_->destroy_deallocate();
2154 }
2155
2156 template<class _Rp, class _A0>
2157 void
2158 function<_Rp(_A0)>::swap(function& __f)
2159 {
2160 if (_VSTD::addressof(__f) == this)
2161 return;
2162 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2163 {
2164 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2165 __base* __t = (__base*)&__tempbuf;
2166 __f_->__clone(__t);
2167 __f_->destroy();
2168 __f_ = 0;
2169 __f.__f_->__clone((__base*)&__buf_);
2170 __f.__f_->destroy();
2171 __f.__f_ = 0;
2172 __f_ = (__base*)&__buf_;
2173 __t->__clone((__base*)&__f.__buf_);
2174 __t->destroy();
2175 __f.__f_ = (__base*)&__f.__buf_;
2176 }
2177 else if (__f_ == (__base*)&__buf_)
2178 {
2179 __f_->__clone((__base*)&__f.__buf_);
2180 __f_->destroy();
2181 __f_ = __f.__f_;
2182 __f.__f_ = (__base*)&__f.__buf_;
2183 }
2184 else if (__f.__f_ == (__base*)&__f.__buf_)
2185 {
2186 __f.__f_->__clone((__base*)&__buf_);
2187 __f.__f_->destroy();
2188 __f.__f_ = __f_;
2189 __f_ = (__base*)&__buf_;
2190 }
2191 else
2192 _VSTD::swap(__f_, __f.__f_);
2193 }
2194
2195 template<class _Rp, class _A0>
2196 _Rp
2197 function<_Rp(_A0)>::operator()(_A0 __a0) const
2198 {
2199 if (__f_ == 0)
2200 __throw_bad_function_call();
2201 return (*__f_)(__a0);
2202 }
2203
2204 #ifndef _LIBCPP_NO_RTTI
2205
2206 template<class _Rp, class _A0>
2207 const std::type_info&
2208 function<_Rp(_A0)>::target_type() const
2209 {
2210 if (__f_ == 0)
2211 return typeid(void);
2212 return __f_->target_type();
2213 }
2214
2215 template<class _Rp, class _A0>
2216 template <typename _Tp>
2217 _Tp*
2218 function<_Rp(_A0)>::target()
2219 {
2220 if (__f_ == 0)
2221 return (_Tp*)0;
2222 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2223 }
2224
2225 template<class _Rp, class _A0>
2226 template <typename _Tp>
2227 const _Tp*
2228 function<_Rp(_A0)>::target() const
2229 {
2230 if (__f_ == 0)
2231 return (const _Tp*)0;
2232 return (const _Tp*)__f_->target(typeid(_Tp));
2233 }
2234
2235 #endif // _LIBCPP_NO_RTTI
2236
2237 template<class _Rp, class _A0, class _A1>
2238 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1)>
2239 : public binary_function<_A0, _A1, _Rp>
2240 {
2241 typedef __function::__base<_Rp(_A0, _A1)> __base;
2242 aligned_storage<3*sizeof(void*)>::type __buf_;
2243 __base* __f_;
2244
2245 public:
2246 typedef _Rp result_type;
2247
2248 // 20.7.16.2.1, construct/copy/destroy:
2249 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
2250 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
2251 function(const function&);
2252 template<class _Fp>
2253 function(_Fp,
2254 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2255
2256 template<class _Alloc>
2257 _LIBCPP_INLINE_VISIBILITY
2258 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
2259 template<class _Alloc>
2260 _LIBCPP_INLINE_VISIBILITY
2261 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
2262 template<class _Alloc>
2263 function(allocator_arg_t, const _Alloc&, const function&);
2264 template<class _Fp, class _Alloc>
2265 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
2266 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2267
2268 function& operator=(const function&);
2269 function& operator=(nullptr_t);
2270 template<class _Fp>
2271 typename enable_if
2272 <
2273 !is_integral<_Fp>::value,
2274 function&
2275 >::type
2276 operator=(_Fp);
2277
2278 ~function();
2279
2280 // 20.7.16.2.2, function modifiers:
2281 void swap(function&);
2282 template<class _Fp, class _Alloc>
2283 _LIBCPP_INLINE_VISIBILITY
2284 void assign(_Fp __f, const _Alloc& __a)
2285 {function(allocator_arg, __a, __f).swap(*this);}
2286
2287 // 20.7.16.2.3, function capacity:
2288 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2289
2290 template<class _R2, class _B0, class _B1>
2291 bool operator==(const function<_R2(_B0, _B1)>&) const = delete;
2292 template<class _R2, class _B0, class _B1>
2293 bool operator!=(const function<_R2(_B0, _B1)>&) const = delete;
2294
2295 // 20.7.16.2.4, function invocation:
2296 _Rp operator()(_A0, _A1) const;
2297
2298 #ifndef _LIBCPP_NO_RTTI
2299 // 20.7.16.2.5, function target access:
2300 const std::type_info& target_type() const;
2301 template <typename _Tp> _Tp* target();
2302 template <typename _Tp> const _Tp* target() const;
2303 #endif // _LIBCPP_NO_RTTI
2304 };
2305
2306 template<class _Rp, class _A0, class _A1>
2307 function<_Rp(_A0, _A1)>::function(const function& __f)
2308 {
2309 if (__f.__f_ == 0)
2310 __f_ = 0;
2311 else if (__f.__f_ == (const __base*)&__f.__buf_)
2312 {
2313 __f_ = (__base*)&__buf_;
2314 __f.__f_->__clone(__f_);
2315 }
2316 else
2317 __f_ = __f.__f_->__clone();
2318 }
2319
2320 template<class _Rp, class _A0, class _A1>
2321 template<class _Alloc>
2322 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
2323 {
2324 if (__f.__f_ == 0)
2325 __f_ = 0;
2326 else if (__f.__f_ == (const __base*)&__f.__buf_)
2327 {
2328 __f_ = (__base*)&__buf_;
2329 __f.__f_->__clone(__f_);
2330 }
2331 else
2332 __f_ = __f.__f_->__clone();
2333 }
2334
2335 template<class _Rp, class _A0, class _A1>
2336 template <class _Fp>
2337 function<_Rp(_A0, _A1)>::function(_Fp __f,
2338 typename enable_if<!is_integral<_Fp>::value>::type*)
2339 : __f_(0)
2340 {
2341 if (__function::__not_null(__f))
2342 {
2343 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
2344 if (sizeof(_FF) <= sizeof(__buf_))
2345 {
2346 __f_ = (__base*)&__buf_;
2347 ::new ((void*)__f_) _FF(__f);
2348 }
2349 else
2350 {
2351 typedef allocator<_FF> _Ap;
2352 _Ap __a;
2353 typedef __allocator_destructor<_Ap> _Dp;
2354 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2355 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2356 __f_ = __hold.release();
2357 }
2358 }
2359 }
2360
2361 template<class _Rp, class _A0, class _A1>
2362 template <class _Fp, class _Alloc>
2363 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2364 typename enable_if<!is_integral<_Fp>::value>::type*)
2365 : __f_(0)
2366 {
2367 typedef allocator_traits<_Alloc> __alloc_traits;
2368 if (__function::__not_null(__f))
2369 {
2370 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
2371 if (sizeof(_FF) <= sizeof(__buf_))
2372 {
2373 __f_ = (__base*)&__buf_;
2374 ::new ((void*)__f_) _FF(__f, __a0);
2375 }
2376 else
2377 {
2378 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2379 _Ap __a(__a0);
2380 typedef __allocator_destructor<_Ap> _Dp;
2381 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2382 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2383 __f_ = __hold.release();
2384 }
2385 }
2386 }
2387
2388 template<class _Rp, class _A0, class _A1>
2389 function<_Rp(_A0, _A1)>&
2390 function<_Rp(_A0, _A1)>::operator=(const function& __f)
2391 {
2392 if (__f)
2393 function(__f).swap(*this);
2394 else
2395 *this = nullptr;
2396 return *this;
2397 }
2398
2399 template<class _Rp, class _A0, class _A1>
2400 function<_Rp(_A0, _A1)>&
2401 function<_Rp(_A0, _A1)>::operator=(nullptr_t)
2402 {
2403 __base* __t = __f_;
2404 __f_ = 0;
2405 if (__t == (__base*)&__buf_)
2406 __t->destroy();
2407 else if (__t)
2408 __t->destroy_deallocate();
2409 return *this;
2410 }
2411
2412 template<class _Rp, class _A0, class _A1>
2413 template <class _Fp>
2414 typename enable_if
2415 <
2416 !is_integral<_Fp>::value,
2417 function<_Rp(_A0, _A1)>&
2418 >::type
2419 function<_Rp(_A0, _A1)>::operator=(_Fp __f)
2420 {
2421 function(_VSTD::move(__f)).swap(*this);
2422 return *this;
2423 }
2424
2425 template<class _Rp, class _A0, class _A1>
2426 function<_Rp(_A0, _A1)>::~function()
2427 {
2428 if (__f_ == (__base*)&__buf_)
2429 __f_->destroy();
2430 else if (__f_)
2431 __f_->destroy_deallocate();
2432 }
2433
2434 template<class _Rp, class _A0, class _A1>
2435 void
2436 function<_Rp(_A0, _A1)>::swap(function& __f)
2437 {
2438 if (_VSTD::addressof(__f) == this)
2439 return;
2440 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2441 {
2442 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2443 __base* __t = (__base*)&__tempbuf;
2444 __f_->__clone(__t);
2445 __f_->destroy();
2446 __f_ = 0;
2447 __f.__f_->__clone((__base*)&__buf_);
2448 __f.__f_->destroy();
2449 __f.__f_ = 0;
2450 __f_ = (__base*)&__buf_;
2451 __t->__clone((__base*)&__f.__buf_);
2452 __t->destroy();
2453 __f.__f_ = (__base*)&__f.__buf_;
2454 }
2455 else if (__f_ == (__base*)&__buf_)
2456 {
2457 __f_->__clone((__base*)&__f.__buf_);
2458 __f_->destroy();
2459 __f_ = __f.__f_;
2460 __f.__f_ = (__base*)&__f.__buf_;
2461 }
2462 else if (__f.__f_ == (__base*)&__f.__buf_)
2463 {
2464 __f.__f_->__clone((__base*)&__buf_);
2465 __f.__f_->destroy();
2466 __f.__f_ = __f_;
2467 __f_ = (__base*)&__buf_;
2468 }
2469 else
2470 _VSTD::swap(__f_, __f.__f_);
2471 }
2472
2473 template<class _Rp, class _A0, class _A1>
2474 _Rp
2475 function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
2476 {
2477 if (__f_ == 0)
2478 __throw_bad_function_call();
2479 return (*__f_)(__a0, __a1);
2480 }
2481
2482 #ifndef _LIBCPP_NO_RTTI
2483
2484 template<class _Rp, class _A0, class _A1>
2485 const std::type_info&
2486 function<_Rp(_A0, _A1)>::target_type() const
2487 {
2488 if (__f_ == 0)
2489 return typeid(void);
2490 return __f_->target_type();
2491 }
2492
2493 template<class _Rp, class _A0, class _A1>
2494 template <typename _Tp>
2495 _Tp*
2496 function<_Rp(_A0, _A1)>::target()
2497 {
2498 if (__f_ == 0)
2499 return (_Tp*)0;
2500 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2501 }
2502
2503 template<class _Rp, class _A0, class _A1>
2504 template <typename _Tp>
2505 const _Tp*
2506 function<_Rp(_A0, _A1)>::target() const
2507 {
2508 if (__f_ == 0)
2509 return (const _Tp*)0;
2510 return (const _Tp*)__f_->target(typeid(_Tp));
2511 }
2512
2513 #endif // _LIBCPP_NO_RTTI
2514
2515 template<class _Rp, class _A0, class _A1, class _A2>
2516 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1, _A2)>
2517 {
2518 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
2519 aligned_storage<3*sizeof(void*)>::type __buf_;
2520 __base* __f_;
2521
2522 public:
2523 typedef _Rp result_type;
2524
2525 // 20.7.16.2.1, construct/copy/destroy:
2526 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
2527 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
2528 function(const function&);
2529 template<class _Fp>
2530 function(_Fp,
2531 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2532
2533 template<class _Alloc>
2534 _LIBCPP_INLINE_VISIBILITY
2535 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
2536 template<class _Alloc>
2537 _LIBCPP_INLINE_VISIBILITY
2538 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
2539 template<class _Alloc>
2540 function(allocator_arg_t, const _Alloc&, const function&);
2541 template<class _Fp, class _Alloc>
2542 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
2543 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2544
2545 function& operator=(const function&);
2546 function& operator=(nullptr_t);
2547 template<class _Fp>
2548 typename enable_if
2549 <
2550 !is_integral<_Fp>::value,
2551 function&
2552 >::type
2553 operator=(_Fp);
2554
2555 ~function();
2556
2557 // 20.7.16.2.2, function modifiers:
2558 void swap(function&);
2559 template<class _Fp, class _Alloc>
2560 _LIBCPP_INLINE_VISIBILITY
2561 void assign(_Fp __f, const _Alloc& __a)
2562 {function(allocator_arg, __a, __f).swap(*this);}
2563
2564 // 20.7.16.2.3, function capacity:
2565 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2566
2567 template<class _R2, class _B0, class _B1, class _B2>
2568 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const = delete;
2569 template<class _R2, class _B0, class _B1, class _B2>
2570 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const = delete;
2571
2572 // 20.7.16.2.4, function invocation:
2573 _Rp operator()(_A0, _A1, _A2) const;
2574
2575 #ifndef _LIBCPP_NO_RTTI
2576 // 20.7.16.2.5, function target access:
2577 const std::type_info& target_type() const;
2578 template <typename _Tp> _Tp* target();
2579 template <typename _Tp> const _Tp* target() const;
2580 #endif // _LIBCPP_NO_RTTI
2581 };
2582
2583 template<class _Rp, class _A0, class _A1, class _A2>
2584 function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
2585 {
2586 if (__f.__f_ == 0)
2587 __f_ = 0;
2588 else if (__f.__f_ == (const __base*)&__f.__buf_)
2589 {
2590 __f_ = (__base*)&__buf_;
2591 __f.__f_->__clone(__f_);
2592 }
2593 else
2594 __f_ = __f.__f_->__clone();
2595 }
2596
2597 template<class _Rp, class _A0, class _A1, class _A2>
2598 template<class _Alloc>
2599 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
2600 const function& __f)
2601 {
2602 if (__f.__f_ == 0)
2603 __f_ = 0;
2604 else if (__f.__f_ == (const __base*)&__f.__buf_)
2605 {
2606 __f_ = (__base*)&__buf_;
2607 __f.__f_->__clone(__f_);
2608 }
2609 else
2610 __f_ = __f.__f_->__clone();
2611 }
2612
2613 template<class _Rp, class _A0, class _A1, class _A2>
2614 template <class _Fp>
2615 function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
2616 typename enable_if<!is_integral<_Fp>::value>::type*)
2617 : __f_(0)
2618 {
2619 if (__function::__not_null(__f))
2620 {
2621 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
2622 if (sizeof(_FF) <= sizeof(__buf_))
2623 {
2624 __f_ = (__base*)&__buf_;
2625 ::new ((void*)__f_) _FF(__f);
2626 }
2627 else
2628 {
2629 typedef allocator<_FF> _Ap;
2630 _Ap __a;
2631 typedef __allocator_destructor<_Ap> _Dp;
2632 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2633 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2634 __f_ = __hold.release();
2635 }
2636 }
2637 }
2638
2639 template<class _Rp, class _A0, class _A1, class _A2>
2640 template <class _Fp, class _Alloc>
2641 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2642 typename enable_if<!is_integral<_Fp>::value>::type*)
2643 : __f_(0)
2644 {
2645 typedef allocator_traits<_Alloc> __alloc_traits;
2646 if (__function::__not_null(__f))
2647 {
2648 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
2649 if (sizeof(_FF) <= sizeof(__buf_))
2650 {
2651 __f_ = (__base*)&__buf_;
2652 ::new ((void*)__f_) _FF(__f, __a0);
2653 }
2654 else
2655 {
2656 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2657 _Ap __a(__a0);
2658 typedef __allocator_destructor<_Ap> _Dp;
2659 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2660 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2661 __f_ = __hold.release();
2662 }
2663 }
2664 }
2665
2666 template<class _Rp, class _A0, class _A1, class _A2>
2667 function<_Rp(_A0, _A1, _A2)>&
2668 function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
2669 {
2670 if (__f)
2671 function(__f).swap(*this);
2672 else
2673 *this = nullptr;
2674 return *this;
2675 }
2676
2677 template<class _Rp, class _A0, class _A1, class _A2>
2678 function<_Rp(_A0, _A1, _A2)>&
2679 function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
2680 {
2681 __base* __t = __f_;
2682 __f_ = 0;
2683 if (__t == (__base*)&__buf_)
2684 __t->destroy();
2685 else if (__t)
2686 __t->destroy_deallocate();
2687 return *this;
2688 }
2689
2690 template<class _Rp, class _A0, class _A1, class _A2>
2691 template <class _Fp>
2692 typename enable_if
2693 <
2694 !is_integral<_Fp>::value,
2695 function<_Rp(_A0, _A1, _A2)>&
2696 >::type
2697 function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
2698 {
2699 function(_VSTD::move(__f)).swap(*this);
2700 return *this;
2701 }
2702
2703 template<class _Rp, class _A0, class _A1, class _A2>
2704 function<_Rp(_A0, _A1, _A2)>::~function()
2705 {
2706 if (__f_ == (__base*)&__buf_)
2707 __f_->destroy();
2708 else if (__f_)
2709 __f_->destroy_deallocate();
2710 }
2711
2712 template<class _Rp, class _A0, class _A1, class _A2>
2713 void
2714 function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
2715 {
2716 if (_VSTD::addressof(__f) == this)
2717 return;
2718 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2719 {
2720 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2721 __base* __t = (__base*)&__tempbuf;
2722 __f_->__clone(__t);
2723 __f_->destroy();
2724 __f_ = 0;
2725 __f.__f_->__clone((__base*)&__buf_);
2726 __f.__f_->destroy();
2727 __f.__f_ = 0;
2728 __f_ = (__base*)&__buf_;
2729 __t->__clone((__base*)&__f.__buf_);
2730 __t->destroy();
2731 __f.__f_ = (__base*)&__f.__buf_;
2732 }
2733 else if (__f_ == (__base*)&__buf_)
2734 {
2735 __f_->__clone((__base*)&__f.__buf_);
2736 __f_->destroy();
2737 __f_ = __f.__f_;
2738 __f.__f_ = (__base*)&__f.__buf_;
2739 }
2740 else if (__f.__f_ == (__base*)&__f.__buf_)
2741 {
2742 __f.__f_->__clone((__base*)&__buf_);
2743 __f.__f_->destroy();
2744 __f.__f_ = __f_;
2745 __f_ = (__base*)&__buf_;
2746 }
2747 else
2748 _VSTD::swap(__f_, __f.__f_);
2749 }
2750
2751 template<class _Rp, class _A0, class _A1, class _A2>
2752 _Rp
2753 function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
2754 {
2755 if (__f_ == 0)
2756 __throw_bad_function_call();
2757 return (*__f_)(__a0, __a1, __a2);
2758 }
2759
2760 #ifndef _LIBCPP_NO_RTTI
2761
2762 template<class _Rp, class _A0, class _A1, class _A2>
2763 const std::type_info&
2764 function<_Rp(_A0, _A1, _A2)>::target_type() const
2765 {
2766 if (__f_ == 0)
2767 return typeid(void);
2768 return __f_->target_type();
2769 }
2770
2771 template<class _Rp, class _A0, class _A1, class _A2>
2772 template <typename _Tp>
2773 _Tp*
2774 function<_Rp(_A0, _A1, _A2)>::target()
2775 {
2776 if (__f_ == 0)
2777 return (_Tp*)0;
2778 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2779 }
2780
2781 template<class _Rp, class _A0, class _A1, class _A2>
2782 template <typename _Tp>
2783 const _Tp*
2784 function<_Rp(_A0, _A1, _A2)>::target() const
2785 {
2786 if (__f_ == 0)
2787 return (const _Tp*)0;
2788 return (const _Tp*)__f_->target(typeid(_Tp));
2789 }
2790
2791 #endif // _LIBCPP_NO_RTTI
2792
2793 template <class _Fp>
2794 inline _LIBCPP_INLINE_VISIBILITY
2795 bool
2796 operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
2797
2798 template <class _Fp>
2799 inline _LIBCPP_INLINE_VISIBILITY
2800 bool
2801 operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
2802
2803 template <class _Fp>
2804 inline _LIBCPP_INLINE_VISIBILITY
2805 bool
2806 operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
2807
2808 template <class _Fp>
2809 inline _LIBCPP_INLINE_VISIBILITY
2810 bool
2811 operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
2812
2813 template <class _Fp>
2814 inline _LIBCPP_INLINE_VISIBILITY
2815 void
2816 swap(function<_Fp>& __x, function<_Fp>& __y)
2817 {return __x.swap(__y);}
2818
2819 #endif // _LIBCPP_CXX03_LANG
2820
2821 _LIBCPP_END_NAMESPACE_STD
2822
2823 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H
2824