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")
40class _LIBCPP_EXCEPTION_ABI bad_function_call
41 : public exception
42{
43public:
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
60void __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
76template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
77
78namespace __function
79{
80
81template<class _Rp>
82struct __maybe_derive_from_unary_function
83{
84};
85
86template<class _Rp, class _A1>
87struct __maybe_derive_from_unary_function<_Rp(_A1)>
88 : public __unary_function<_A1, _Rp>
89{
90};
91
92template<class _Rp>
93struct __maybe_derive_from_binary_function
94{
95};
96
97template<class _Rp, class _A1, class _A2>
98struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
99 : public __binary_function<_A1, _A2, _Rp>
100{
101};
102
103template <class _Fp>
104_LIBCPP_INLINE_VISIBILITY
105bool __not_null(_Fp const&) { return true; }
106
107template <class _Fp>
108_LIBCPP_INLINE_VISIBILITY
109bool __not_null(_Fp* __ptr) { return __ptr; }
110
111template <class _Ret, class _Class>
112_LIBCPP_INLINE_VISIBILITY
113bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
114
115template <class _Fp>
116_LIBCPP_INLINE_VISIBILITY
117bool __not_null(function<_Fp> const& __f) { return !!__f; }
118
119#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
120template <class _Rp, class ..._Args>
121_LIBCPP_INLINE_VISIBILITY
122bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
123#endif
124
125} // namespace __function
126
127#ifndef _LIBCPP_CXX03_LANG
128
129namespace __function {
130
131// __alloc_func holds a functor and an allocator.
132
133template <class _Fp, class _Ap, class _FB> class __alloc_func;
134template <class _Fp, class _FB>
135class __default_alloc_func;
136
137template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
138class __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
216template <class _Fp, class _Rp, class... _ArgTypes>
217class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
218 _Fp __f_;
219
220public:
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
259template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
260
261template<class _Rp, class ..._ArgTypes>
262class __base<_Rp(_ArgTypes...)>
263{
264 __base(const __base&);
265 __base& operator=(const __base&);
266public:
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
282template<class _FD, class _Alloc, class _FB> class __func;
283
284template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
285class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
286 : public __base<_Rp(_ArgTypes...)>
287{
288 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
289public:
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
317template<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
330template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
331void
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
337template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
338void
339__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
340{
341 __f_.destroy();
342}
343
344template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
345void
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
355template<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
364template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
365const 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
373template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
374const 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
384template <class _Fp> class __value_func;
385
386template <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(p: &__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(p: &__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(p: &__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(p: &__tempbuf);
524 __f_->__clone(__t);
525 __f_->destroy();
526 __f_ = nullptr;
527 __f.__f_->__clone(__as_base(p: &__buf_));
528 __f.__f_->destroy();
529 __f.__f_ = nullptr;
530 __f_ = __as_base(p: &__buf_);
531 __t->__clone(__as_base(p: &__f.__buf_));
532 __t->destroy();
533 __f.__f_ = __as_base(p: &__f.__buf_);
534 }
535 else if ((void*)__f_ == &__buf_)
536 {
537 __f_->__clone(__as_base(p: &__f.__buf_));
538 __f_->destroy();
539 __f_ = __f.__f_;
540 __f.__f_ = __as_base(p: &__f.__buf_);
541 }
542 else if ((void*)__f.__f_ == &__f.__buf_)
543 {
544 __f.__f_->__clone(__as_base(p: &__buf_));
545 __f.__f_->destroy();
546 __f.__f_ = __f_;
547 __f_ = __as_base(p: &__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.
577union __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.
584template <typename _Fun>
585struct __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.
594struct __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.
672template <typename _Tp>
673using __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
678template <class _Fp> struct __policy_invoker;
679
680template <class _Rp, class... _ArgTypes>
681struct __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
723template <class _Fp> class __policy_func;
724
725template <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(x: __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) && !defined(_LIBCPP_HAS_OBJC_ARC)
887
888extern "C" void *_Block_copy(const void *);
889extern "C" void _Block_release(const void *);
890
891template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
892class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
893 : public __base<_Rp(_ArgTypes...)>
894{
895 typedef _Rp1(^__block_type)(_ArgTypes1...);
896 __block_type __f_;
897
898public:
899 _LIBCPP_INLINE_VISIBILITY
900 explicit __func(__block_type const& __f)
901 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
902 { }
903
904 // [TODO] add && to save on a retain
905
906 _LIBCPP_INLINE_VISIBILITY
907 explicit __func(__block_type __f, const _Alloc& /* unused */)
908 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
909 { }
910
911 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
912 _LIBCPP_ASSERT(false,
913 "Block pointers are just pointers, so they should always fit into "
914 "std::function's small buffer optimization. This function should "
915 "never be invoked.");
916 return nullptr;
917 }
918
919 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
920 ::new ((void*)__p) __func(__f_);
921 }
922
923 virtual void destroy() _NOEXCEPT {
924 if (__f_)
925 _Block_release(__f_);
926 __f_ = 0;
927 }
928
929 virtual void destroy_deallocate() _NOEXCEPT {
930 _LIBCPP_ASSERT(false,
931 "Block pointers are just pointers, so they should always fit into "
932 "std::function's small buffer optimization. This function should "
933 "never be invoked.");
934 }
935
936 virtual _Rp operator()(_ArgTypes&& ... __arg) {
937 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
938 }
939
940#ifndef _LIBCPP_NO_RTTI
941 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
942 if (__ti == typeid(__func::__block_type))
943 return &__f_;
944 return (const void*)nullptr;
945 }
946
947 virtual const std::type_info& target_type() const _NOEXCEPT {
948 return typeid(__func::__block_type);
949 }
950#endif // _LIBCPP_NO_RTTI
951};
952
953#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
954
955} // namespace __function
956
957template<class _Rp, class ..._ArgTypes>
958class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
959 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
960 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
961{
962#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
963 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
964#else
965 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
966#endif
967
968 __func __f_;
969
970 template <class _Fp, bool = _And<
971 _IsNotSame<__uncvref_t<_Fp>, function>,
972 __invokable<_Fp, _ArgTypes...>
973 >::value>
974 struct __callable;
975 template <class _Fp>
976 struct __callable<_Fp, true>
977 {
978 static const bool value = is_void<_Rp>::value ||
979 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
980 _Rp>::value;
981 };
982 template <class _Fp>
983 struct __callable<_Fp, false>
984 {
985 static const bool value = false;
986 };
987
988 template <class _Fp>
989 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
990public:
991 typedef _Rp result_type;
992
993 // construct/copy/destroy:
994 _LIBCPP_INLINE_VISIBILITY
995 function() _NOEXCEPT { }
996 _LIBCPP_INLINE_VISIBILITY
997 function(nullptr_t) _NOEXCEPT {}
998 function(const function&);
999 function(function&&) _NOEXCEPT;
1000 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
1001 function(_Fp);
1002
1003#if _LIBCPP_STD_VER <= 14
1004 template<class _Alloc>
1005 _LIBCPP_INLINE_VISIBILITY
1006 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
1007 template<class _Alloc>
1008 _LIBCPP_INLINE_VISIBILITY
1009 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
1010 template<class _Alloc>
1011 function(allocator_arg_t, const _Alloc&, const function&);
1012 template<class _Alloc>
1013 function(allocator_arg_t, const _Alloc&, function&&);
1014 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
1015 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
1016#endif
1017
1018 function& operator=(const function&);
1019 function& operator=(function&&) _NOEXCEPT;
1020 function& operator=(nullptr_t) _NOEXCEPT;
1021 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
1022 function& operator=(_Fp&&);
1023
1024 ~function();
1025
1026 // function modifiers:
1027 void swap(function&) _NOEXCEPT;
1028
1029#if _LIBCPP_STD_VER <= 14
1030 template<class _Fp, class _Alloc>
1031 _LIBCPP_INLINE_VISIBILITY
1032 void assign(_Fp&& __f, const _Alloc& __a)
1033 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
1034#endif
1035
1036 // function capacity:
1037 _LIBCPP_INLINE_VISIBILITY
1038 explicit operator bool() const _NOEXCEPT {
1039 return static_cast<bool>(__f_);
1040 }
1041
1042 // deleted overloads close possible hole in the type system
1043 template<class _R2, class... _ArgTypes2>
1044 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
1045 template<class _R2, class... _ArgTypes2>
1046 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
1047public:
1048 // function invocation:
1049 _Rp operator()(_ArgTypes...) const;
1050
1051#ifndef _LIBCPP_NO_RTTI
1052 // function target access:
1053 const std::type_info& target_type() const _NOEXCEPT;
1054 template <typename _Tp> _Tp* target() _NOEXCEPT;
1055 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
1056#endif // _LIBCPP_NO_RTTI
1057};
1058
1059#if _LIBCPP_STD_VER >= 17
1060template<class _Rp, class ..._Ap>
1061function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
1062
1063template<class _Fp>
1064struct __strip_signature;
1065
1066template<class _Rp, class _Gp, class ..._Ap>
1067struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
1068template<class _Rp, class _Gp, class ..._Ap>
1069struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
1070template<class _Rp, class _Gp, class ..._Ap>
1071struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
1072template<class _Rp, class _Gp, class ..._Ap>
1073struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
1074
1075template<class _Rp, class _Gp, class ..._Ap>
1076struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
1077template<class _Rp, class _Gp, class ..._Ap>
1078struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
1079template<class _Rp, class _Gp, class ..._Ap>
1080struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
1081template<class _Rp, class _Gp, class ..._Ap>
1082struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
1083
1084template<class _Rp, class _Gp, class ..._Ap>
1085struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
1086template<class _Rp, class _Gp, class ..._Ap>
1087struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
1088template<class _Rp, class _Gp, class ..._Ap>
1089struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
1090template<class _Rp, class _Gp, class ..._Ap>
1091struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
1092
1093template<class _Rp, class _Gp, class ..._Ap>
1094struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
1095template<class _Rp, class _Gp, class ..._Ap>
1096struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
1097template<class _Rp, class _Gp, class ..._Ap>
1098struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
1099template<class _Rp, class _Gp, class ..._Ap>
1100struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
1101
1102template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1103function(_Fp) -> function<_Stripped>;
1104#endif // _LIBCPP_STD_VER >= 17
1105
1106template<class _Rp, class ..._ArgTypes>
1107function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
1108
1109#if _LIBCPP_STD_VER <= 14
1110template<class _Rp, class ..._ArgTypes>
1111template <class _Alloc>
1112function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1113 const function& __f) : __f_(__f.__f_) {}
1114#endif
1115
1116template <class _Rp, class... _ArgTypes>
1117function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
1118 : __f_(_VSTD::move(__f.__f_)) {}
1119
1120#if _LIBCPP_STD_VER <= 14
1121template<class _Rp, class ..._ArgTypes>
1122template <class _Alloc>
1123function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1124 function&& __f)
1125 : __f_(_VSTD::move(__f.__f_)) {}
1126#endif
1127
1128template <class _Rp, class... _ArgTypes>
1129template <class _Fp, class>
1130function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
1131
1132#if _LIBCPP_STD_VER <= 14
1133template <class _Rp, class... _ArgTypes>
1134template <class _Fp, class _Alloc, class>
1135function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
1136 _Fp __f)
1137 : __f_(_VSTD::move(__f), __a) {}
1138#endif
1139
1140template<class _Rp, class ..._ArgTypes>
1141function<_Rp(_ArgTypes...)>&
1142function<_Rp(_ArgTypes...)>::operator=(const function& __f)
1143{
1144 function(__f).swap(*this);
1145 return *this;
1146}
1147
1148template<class _Rp, class ..._ArgTypes>
1149function<_Rp(_ArgTypes...)>&
1150function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
1151{
1152 __f_ = _VSTD::move(__f.__f_);
1153 return *this;
1154}
1155
1156template<class _Rp, class ..._ArgTypes>
1157function<_Rp(_ArgTypes...)>&
1158function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
1159{
1160 __f_ = nullptr;
1161 return *this;
1162}
1163
1164template<class _Rp, class ..._ArgTypes>
1165template <class _Fp, class>
1166function<_Rp(_ArgTypes...)>&
1167function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
1168{
1169 function(_VSTD::forward<_Fp>(__f)).swap(*this);
1170 return *this;
1171}
1172
1173template<class _Rp, class ..._ArgTypes>
1174function<_Rp(_ArgTypes...)>::~function() {}
1175
1176template<class _Rp, class ..._ArgTypes>
1177void
1178function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
1179{
1180 __f_.swap(__f.__f_);
1181}
1182
1183template<class _Rp, class ..._ArgTypes>
1184_Rp
1185function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1186{
1187 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
1188}
1189
1190#ifndef _LIBCPP_NO_RTTI
1191
1192template<class _Rp, class ..._ArgTypes>
1193const std::type_info&
1194function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1195{
1196 return __f_.target_type();
1197}
1198
1199template<class _Rp, class ..._ArgTypes>
1200template <typename _Tp>
1201_Tp*
1202function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
1203{
1204 return (_Tp*)(__f_.template target<_Tp>());
1205}
1206
1207template<class _Rp, class ..._ArgTypes>
1208template <typename _Tp>
1209const _Tp*
1210function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
1211{
1212 return __f_.template target<_Tp>();
1213}
1214
1215#endif // _LIBCPP_NO_RTTI
1216
1217template <class _Rp, class... _ArgTypes>
1218inline _LIBCPP_INLINE_VISIBILITY
1219bool
1220operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
1221
1222template <class _Rp, class... _ArgTypes>
1223inline _LIBCPP_INLINE_VISIBILITY
1224bool
1225operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
1226
1227template <class _Rp, class... _ArgTypes>
1228inline _LIBCPP_INLINE_VISIBILITY
1229bool
1230operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
1231
1232template <class _Rp, class... _ArgTypes>
1233inline _LIBCPP_INLINE_VISIBILITY
1234bool
1235operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
1236
1237template <class _Rp, class... _ArgTypes>
1238inline _LIBCPP_INLINE_VISIBILITY
1239void
1240swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
1241{return __x.swap(__y);}
1242
1243#elif defined(_LIBCPP_ENABLE_CXX03_FUNCTION)
1244
1245namespace __function {
1246
1247template<class _Fp> class __base;
1248
1249template<class _Rp>
1250class __base<_Rp()>
1251{
1252 __base(const __base&);
1253 __base& operator=(const __base&);
1254public:
1255 __base() {}
1256 virtual ~__base() {}
1257 virtual __base* __clone() const = 0;
1258 virtual void __clone(__base*) const = 0;
1259 virtual void destroy() = 0;
1260 virtual void destroy_deallocate() = 0;
1261 virtual _Rp operator()() = 0;
1262#ifndef _LIBCPP_NO_RTTI
1263 virtual const void* target(const type_info&) const = 0;
1264 virtual const std::type_info& target_type() const = 0;
1265#endif // _LIBCPP_NO_RTTI
1266};
1267
1268template<class _Rp, class _A0>
1269class __base<_Rp(_A0)>
1270{
1271 __base(const __base&);
1272 __base& operator=(const __base&);
1273public:
1274 __base() {}
1275 virtual ~__base() {}
1276 virtual __base* __clone() const = 0;
1277 virtual void __clone(__base*) const = 0;
1278 virtual void destroy() = 0;
1279 virtual void destroy_deallocate() = 0;
1280 virtual _Rp operator()(_A0) = 0;
1281#ifndef _LIBCPP_NO_RTTI
1282 virtual const void* target(const type_info&) const = 0;
1283 virtual const std::type_info& target_type() const = 0;
1284#endif // _LIBCPP_NO_RTTI
1285};
1286
1287template<class _Rp, class _A0, class _A1>
1288class __base<_Rp(_A0, _A1)>
1289{
1290 __base(const __base&);
1291 __base& operator=(const __base&);
1292public:
1293 __base() {}
1294 virtual ~__base() {}
1295 virtual __base* __clone() const = 0;
1296 virtual void __clone(__base*) const = 0;
1297 virtual void destroy() = 0;
1298 virtual void destroy_deallocate() = 0;
1299 virtual _Rp operator()(_A0, _A1) = 0;
1300#ifndef _LIBCPP_NO_RTTI
1301 virtual const void* target(const type_info&) const = 0;
1302 virtual const std::type_info& target_type() const = 0;
1303#endif // _LIBCPP_NO_RTTI
1304};
1305
1306template<class _Rp, class _A0, class _A1, class _A2>
1307class __base<_Rp(_A0, _A1, _A2)>
1308{
1309 __base(const __base&);
1310 __base& operator=(const __base&);
1311public:
1312 __base() {}
1313 virtual ~__base() {}
1314 virtual __base* __clone() const = 0;
1315 virtual void __clone(__base*) const = 0;
1316 virtual void destroy() = 0;
1317 virtual void destroy_deallocate() = 0;
1318 virtual _Rp operator()(_A0, _A1, _A2) = 0;
1319#ifndef _LIBCPP_NO_RTTI
1320 virtual const void* target(const type_info&) const = 0;
1321 virtual const std::type_info& target_type() const = 0;
1322#endif // _LIBCPP_NO_RTTI
1323};
1324
1325template<class _FD, class _Alloc, class _FB> class __func;
1326
1327template<class _Fp, class _Alloc, class _Rp>
1328class __func<_Fp, _Alloc, _Rp()>
1329 : public __base<_Rp()>
1330{
1331 __compressed_pair<_Fp, _Alloc> __f_;
1332public:
1333 explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1334 explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1335 virtual __base<_Rp()>* __clone() const;
1336 virtual void __clone(__base<_Rp()>*) const;
1337 virtual void destroy();
1338 virtual void destroy_deallocate();
1339 virtual _Rp operator()();
1340#ifndef _LIBCPP_NO_RTTI
1341 virtual const void* target(const type_info&) const;
1342 virtual const std::type_info& target_type() const;
1343#endif // _LIBCPP_NO_RTTI
1344};
1345
1346template<class _Fp, class _Alloc, class _Rp>
1347__base<_Rp()>*
1348__func<_Fp, _Alloc, _Rp()>::__clone() const
1349{
1350 typedef allocator_traits<_Alloc> __alloc_traits;
1351 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1352 _Ap __a(__f_.second());
1353 typedef __allocator_destructor<_Ap> _Dp;
1354 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1355 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1356 return __hold.release();
1357}
1358
1359template<class _Fp, class _Alloc, class _Rp>
1360void
1361__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
1362{
1363 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1364}
1365
1366template<class _Fp, class _Alloc, class _Rp>
1367void
1368__func<_Fp, _Alloc, _Rp()>::destroy()
1369{
1370 __f_.~__compressed_pair<_Fp, _Alloc>();
1371}
1372
1373template<class _Fp, class _Alloc, class _Rp>
1374void
1375__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
1376{
1377 typedef allocator_traits<_Alloc> __alloc_traits;
1378 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1379 _Ap __a(__f_.second());
1380 __f_.~__compressed_pair<_Fp, _Alloc>();
1381 __a.deallocate(this, 1);
1382}
1383
1384template<class _Fp, class _Alloc, class _Rp>
1385_Rp
1386__func<_Fp, _Alloc, _Rp()>::operator()()
1387{
1388 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1389 return _Invoker::__call(__f_.first());
1390}
1391
1392#ifndef _LIBCPP_NO_RTTI
1393
1394template<class _Fp, class _Alloc, class _Rp>
1395const void*
1396__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
1397{
1398 if (__ti == typeid(_Fp))
1399 return _VSTD::addressof(__f_.first());
1400 return (const void*)0;
1401}
1402
1403template<class _Fp, class _Alloc, class _Rp>
1404const std::type_info&
1405__func<_Fp, _Alloc, _Rp()>::target_type() const
1406{
1407 return typeid(_Fp);
1408}
1409
1410#endif // _LIBCPP_NO_RTTI
1411
1412template<class _Fp, class _Alloc, class _Rp, class _A0>
1413class __func<_Fp, _Alloc, _Rp(_A0)>
1414 : public __base<_Rp(_A0)>
1415{
1416 __compressed_pair<_Fp, _Alloc> __f_;
1417public:
1418 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1419 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1420 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1421 virtual __base<_Rp(_A0)>* __clone() const;
1422 virtual void __clone(__base<_Rp(_A0)>*) const;
1423 virtual void destroy();
1424 virtual void destroy_deallocate();
1425 virtual _Rp operator()(_A0);
1426#ifndef _LIBCPP_NO_RTTI
1427 virtual const void* target(const type_info&) const;
1428 virtual const std::type_info& target_type() const;
1429#endif // _LIBCPP_NO_RTTI
1430};
1431
1432template<class _Fp, class _Alloc, class _Rp, class _A0>
1433__base<_Rp(_A0)>*
1434__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
1435{
1436 typedef allocator_traits<_Alloc> __alloc_traits;
1437 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1438 _Ap __a(__f_.second());
1439 typedef __allocator_destructor<_Ap> _Dp;
1440 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1441 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1442 return __hold.release();
1443}
1444
1445template<class _Fp, class _Alloc, class _Rp, class _A0>
1446void
1447__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
1448{
1449 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1450}
1451
1452template<class _Fp, class _Alloc, class _Rp, class _A0>
1453void
1454__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
1455{
1456 __f_.~__compressed_pair<_Fp, _Alloc>();
1457}
1458
1459template<class _Fp, class _Alloc, class _Rp, class _A0>
1460void
1461__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
1462{
1463 typedef allocator_traits<_Alloc> __alloc_traits;
1464 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1465 _Ap __a(__f_.second());
1466 __f_.~__compressed_pair<_Fp, _Alloc>();
1467 __a.deallocate(this, 1);
1468}
1469
1470template<class _Fp, class _Alloc, class _Rp, class _A0>
1471_Rp
1472__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
1473{
1474 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1475 return _Invoker::__call(__f_.first(), __a0);
1476}
1477
1478#ifndef _LIBCPP_NO_RTTI
1479
1480template<class _Fp, class _Alloc, class _Rp, class _A0>
1481const void*
1482__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
1483{
1484 if (__ti == typeid(_Fp))
1485 return &__f_.first();
1486 return (const void*)0;
1487}
1488
1489template<class _Fp, class _Alloc, class _Rp, class _A0>
1490const std::type_info&
1491__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
1492{
1493 return typeid(_Fp);
1494}
1495
1496#endif // _LIBCPP_NO_RTTI
1497
1498template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1499class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
1500 : public __base<_Rp(_A0, _A1)>
1501{
1502 __compressed_pair<_Fp, _Alloc> __f_;
1503public:
1504 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1505 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1506 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1507 virtual __base<_Rp(_A0, _A1)>* __clone() const;
1508 virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
1509 virtual void destroy();
1510 virtual void destroy_deallocate();
1511 virtual _Rp operator()(_A0, _A1);
1512#ifndef _LIBCPP_NO_RTTI
1513 virtual const void* target(const type_info&) const;
1514 virtual const std::type_info& target_type() const;
1515#endif // _LIBCPP_NO_RTTI
1516};
1517
1518template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1519__base<_Rp(_A0, _A1)>*
1520__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
1521{
1522 typedef allocator_traits<_Alloc> __alloc_traits;
1523 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1524 _Ap __a(__f_.second());
1525 typedef __allocator_destructor<_Ap> _Dp;
1526 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1527 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1528 return __hold.release();
1529}
1530
1531template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1532void
1533__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
1534{
1535 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1536}
1537
1538template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1539void
1540__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
1541{
1542 __f_.~__compressed_pair<_Fp, _Alloc>();
1543}
1544
1545template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1546void
1547__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
1548{
1549 typedef allocator_traits<_Alloc> __alloc_traits;
1550 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1551 _Ap __a(__f_.second());
1552 __f_.~__compressed_pair<_Fp, _Alloc>();
1553 __a.deallocate(this, 1);
1554}
1555
1556template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1557_Rp
1558__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
1559{
1560 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1561 return _Invoker::__call(__f_.first(), __a0, __a1);
1562}
1563
1564#ifndef _LIBCPP_NO_RTTI
1565
1566template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1567const void*
1568__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
1569{
1570 if (__ti == typeid(_Fp))
1571 return &__f_.first();
1572 return (const void*)0;
1573}
1574
1575template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1576const std::type_info&
1577__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
1578{
1579 return typeid(_Fp);
1580}
1581
1582#endif // _LIBCPP_NO_RTTI
1583
1584template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1585class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
1586 : public __base<_Rp(_A0, _A1, _A2)>
1587{
1588 __compressed_pair<_Fp, _Alloc> __f_;
1589public:
1590 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1591 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1592 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1593 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
1594 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
1595 virtual void destroy();
1596 virtual void destroy_deallocate();
1597 virtual _Rp operator()(_A0, _A1, _A2);
1598#ifndef _LIBCPP_NO_RTTI
1599 virtual const void* target(const type_info&) const;
1600 virtual const std::type_info& target_type() const;
1601#endif // _LIBCPP_NO_RTTI
1602};
1603
1604template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1605__base<_Rp(_A0, _A1, _A2)>*
1606__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
1607{
1608 typedef allocator_traits<_Alloc> __alloc_traits;
1609 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1610 _Ap __a(__f_.second());
1611 typedef __allocator_destructor<_Ap> _Dp;
1612 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1613 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1614 return __hold.release();
1615}
1616
1617template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1618void
1619__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
1620{
1621 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1622}
1623
1624template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1625void
1626__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
1627{
1628 __f_.~__compressed_pair<_Fp, _Alloc>();
1629}
1630
1631template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1632void
1633__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
1634{
1635 typedef allocator_traits<_Alloc> __alloc_traits;
1636 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1637 _Ap __a(__f_.second());
1638 __f_.~__compressed_pair<_Fp, _Alloc>();
1639 __a.deallocate(this, 1);
1640}
1641
1642template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1643_Rp
1644__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
1645{
1646 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1647 return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
1648}
1649
1650#ifndef _LIBCPP_NO_RTTI
1651
1652template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1653const void*
1654__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
1655{
1656 if (__ti == typeid(_Fp))
1657 return &__f_.first();
1658 return (const void*)0;
1659}
1660
1661template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1662const std::type_info&
1663__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
1664{
1665 return typeid(_Fp);
1666}
1667
1668#endif // _LIBCPP_NO_RTTI
1669
1670} // namespace __function
1671
1672template<class _Rp>
1673class _LIBCPP_TEMPLATE_VIS function<_Rp()>
1674{
1675 typedef __function::__base<_Rp()> __base;
1676 aligned_storage<3*sizeof(void*)>::type __buf_;
1677 __base* __f_;
1678
1679public:
1680 typedef _Rp result_type;
1681
1682 // 20.7.16.2.1, construct/copy/destroy:
1683 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1684 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1685 function(const function&);
1686 template<class _Fp>
1687 function(_Fp,
1688 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1689
1690 template<class _Alloc>
1691 _LIBCPP_INLINE_VISIBILITY
1692 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1693 template<class _Alloc>
1694 _LIBCPP_INLINE_VISIBILITY
1695 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1696 template<class _Alloc>
1697 function(allocator_arg_t, const _Alloc&, const function&);
1698 template<class _Fp, class _Alloc>
1699 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1700 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1701
1702 function& operator=(const function&);
1703 function& operator=(nullptr_t);
1704 template<class _Fp>
1705 typename enable_if
1706 <
1707 !is_integral<_Fp>::value,
1708 function&
1709 >::type
1710 operator=(_Fp);
1711
1712 ~function();
1713
1714 // 20.7.16.2.2, function modifiers:
1715 void swap(function&);
1716 template<class _Fp, class _Alloc>
1717 _LIBCPP_INLINE_VISIBILITY
1718 void assign(_Fp __f, const _Alloc& __a)
1719 {function(allocator_arg, __a, __f).swap(*this);}
1720
1721 // 20.7.16.2.3, function capacity:
1722 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
1723
1724 template<class _R2>
1725 bool operator==(const function<_R2()>&) const = delete;
1726 template<class _R2>
1727 bool operator!=(const function<_R2()>&) const = delete;
1728
1729 // 20.7.16.2.4, function invocation:
1730 _Rp operator()() const;
1731
1732#ifndef _LIBCPP_NO_RTTI
1733 // 20.7.16.2.5, function target access:
1734 const std::type_info& target_type() const;
1735 template <typename _Tp> _Tp* target();
1736 template <typename _Tp> const _Tp* target() const;
1737#endif // _LIBCPP_NO_RTTI
1738};
1739
1740template<class _Rp>
1741function<_Rp()>::function(const function& __f)
1742{
1743 if (__f.__f_ == 0)
1744 __f_ = 0;
1745 else if (__f.__f_ == (const __base*)&__f.__buf_)
1746 {
1747 __f_ = (__base*)&__buf_;
1748 __f.__f_->__clone(__f_);
1749 }
1750 else
1751 __f_ = __f.__f_->__clone();
1752}
1753
1754template<class _Rp>
1755template<class _Alloc>
1756function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
1757{
1758 if (__f.__f_ == 0)
1759 __f_ = 0;
1760 else if (__f.__f_ == (const __base*)&__f.__buf_)
1761 {
1762 __f_ = (__base*)&__buf_;
1763 __f.__f_->__clone(__f_);
1764 }
1765 else
1766 __f_ = __f.__f_->__clone();
1767}
1768
1769template<class _Rp>
1770template <class _Fp>
1771function<_Rp()>::function(_Fp __f,
1772 typename enable_if<!is_integral<_Fp>::value>::type*)
1773 : __f_(0)
1774{
1775 if (__function::__not_null(__f))
1776 {
1777 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
1778 if (sizeof(_FF) <= sizeof(__buf_))
1779 {
1780 __f_ = (__base*)&__buf_;
1781 ::new ((void*)__f_) _FF(__f);
1782 }
1783 else
1784 {
1785 typedef allocator<_FF> _Ap;
1786 _Ap __a;
1787 typedef __allocator_destructor<_Ap> _Dp;
1788 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1789 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
1790 __f_ = __hold.release();
1791 }
1792 }
1793}
1794
1795template<class _Rp>
1796template <class _Fp, class _Alloc>
1797function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1798 typename enable_if<!is_integral<_Fp>::value>::type*)
1799 : __f_(0)
1800{
1801 typedef allocator_traits<_Alloc> __alloc_traits;
1802 if (__function::__not_null(__f))
1803 {
1804 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
1805 if (sizeof(_FF) <= sizeof(__buf_))
1806 {
1807 __f_ = (__base*)&__buf_;
1808 ::new ((void*)__f_) _FF(__f, __a0);
1809 }
1810 else
1811 {
1812 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1813 _Ap __a(__a0);
1814 typedef __allocator_destructor<_Ap> _Dp;
1815 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1816 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
1817 __f_ = __hold.release();
1818 }
1819 }
1820}
1821
1822template<class _Rp>
1823function<_Rp()>&
1824function<_Rp()>::operator=(const function& __f)
1825{
1826 if (__f)
1827 function(__f).swap(*this);
1828 else
1829 *this = nullptr;
1830 return *this;
1831}
1832
1833template<class _Rp>
1834function<_Rp()>&
1835function<_Rp()>::operator=(nullptr_t)
1836{
1837 __base* __t = __f_;
1838 __f_ = 0;
1839 if (__t == (__base*)&__buf_)
1840 __t->destroy();
1841 else if (__t)
1842 __t->destroy_deallocate();
1843 return *this;
1844}
1845
1846template<class _Rp>
1847template <class _Fp>
1848typename enable_if
1849<
1850 !is_integral<_Fp>::value,
1851 function<_Rp()>&
1852>::type
1853function<_Rp()>::operator=(_Fp __f)
1854{
1855 function(_VSTD::move(__f)).swap(*this);
1856 return *this;
1857}
1858
1859template<class _Rp>
1860function<_Rp()>::~function()
1861{
1862 if (__f_ == (__base*)&__buf_)
1863 __f_->destroy();
1864 else if (__f_)
1865 __f_->destroy_deallocate();
1866}
1867
1868template<class _Rp>
1869void
1870function<_Rp()>::swap(function& __f)
1871{
1872 if (_VSTD::addressof(__f) == this)
1873 return;
1874 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1875 {
1876 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1877 __base* __t = (__base*)&__tempbuf;
1878 __f_->__clone(__t);
1879 __f_->destroy();
1880 __f_ = 0;
1881 __f.__f_->__clone((__base*)&__buf_);
1882 __f.__f_->destroy();
1883 __f.__f_ = 0;
1884 __f_ = (__base*)&__buf_;
1885 __t->__clone((__base*)&__f.__buf_);
1886 __t->destroy();
1887 __f.__f_ = (__base*)&__f.__buf_;
1888 }
1889 else if (__f_ == (__base*)&__buf_)
1890 {
1891 __f_->__clone((__base*)&__f.__buf_);
1892 __f_->destroy();
1893 __f_ = __f.__f_;
1894 __f.__f_ = (__base*)&__f.__buf_;
1895 }
1896 else if (__f.__f_ == (__base*)&__f.__buf_)
1897 {
1898 __f.__f_->__clone((__base*)&__buf_);
1899 __f.__f_->destroy();
1900 __f.__f_ = __f_;
1901 __f_ = (__base*)&__buf_;
1902 }
1903 else
1904 _VSTD::swap(__f_, __f.__f_);
1905}
1906
1907template<class _Rp>
1908_Rp
1909function<_Rp()>::operator()() const
1910{
1911 if (__f_ == 0)
1912 __throw_bad_function_call();
1913 return (*__f_)();
1914}
1915
1916#ifndef _LIBCPP_NO_RTTI
1917
1918template<class _Rp>
1919const std::type_info&
1920function<_Rp()>::target_type() const
1921{
1922 if (__f_ == 0)
1923 return typeid(void);
1924 return __f_->target_type();
1925}
1926
1927template<class _Rp>
1928template <typename _Tp>
1929_Tp*
1930function<_Rp()>::target()
1931{
1932 if (__f_ == 0)
1933 return (_Tp*)0;
1934 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
1935}
1936
1937template<class _Rp>
1938template <typename _Tp>
1939const _Tp*
1940function<_Rp()>::target() const
1941{
1942 if (__f_ == 0)
1943 return (const _Tp*)0;
1944 return (const _Tp*)__f_->target(typeid(_Tp));
1945}
1946
1947#endif // _LIBCPP_NO_RTTI
1948
1949template<class _Rp, class _A0>
1950class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0)>
1951 : public unary_function<_A0, _Rp>
1952{
1953 typedef __function::__base<_Rp(_A0)> __base;
1954 aligned_storage<3*sizeof(void*)>::type __buf_;
1955 __base* __f_;
1956
1957public:
1958 typedef _Rp result_type;
1959
1960 // 20.7.16.2.1, construct/copy/destroy:
1961 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1962 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1963 function(const function&);
1964 template<class _Fp>
1965 function(_Fp,
1966 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1967
1968 template<class _Alloc>
1969 _LIBCPP_INLINE_VISIBILITY
1970 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1971 template<class _Alloc>
1972 _LIBCPP_INLINE_VISIBILITY
1973 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1974 template<class _Alloc>
1975 function(allocator_arg_t, const _Alloc&, const function&);
1976 template<class _Fp, class _Alloc>
1977 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1978 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1979
1980 function& operator=(const function&);
1981 function& operator=(nullptr_t);
1982 template<class _Fp>
1983 typename enable_if
1984 <
1985 !is_integral<_Fp>::value,
1986 function&
1987 >::type
1988 operator=(_Fp);
1989
1990 ~function();
1991
1992 // 20.7.16.2.2, function modifiers:
1993 void swap(function&);
1994 template<class _Fp, class _Alloc>
1995 _LIBCPP_INLINE_VISIBILITY
1996 void assign(_Fp __f, const _Alloc& __a)
1997 {function(allocator_arg, __a, __f).swap(*this);}
1998
1999 // 20.7.16.2.3, function capacity:
2000 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2001
2002 template<class _R2, class _B0>
2003 bool operator==(const function<_R2(_B0)>&) const = delete;
2004 template<class _R2, class _B0>
2005 bool operator!=(const function<_R2(_B0)>&) const = delete;
2006
2007 // 20.7.16.2.4, function invocation:
2008 _Rp operator()(_A0) const;
2009
2010#ifndef _LIBCPP_NO_RTTI
2011 // 20.7.16.2.5, function target access:
2012 const std::type_info& target_type() const;
2013 template <typename _Tp> _Tp* target();
2014 template <typename _Tp> const _Tp* target() const;
2015#endif // _LIBCPP_NO_RTTI
2016};
2017
2018template<class _Rp, class _A0>
2019function<_Rp(_A0)>::function(const function& __f)
2020{
2021 if (__f.__f_ == 0)
2022 __f_ = 0;
2023 else if (__f.__f_ == (const __base*)&__f.__buf_)
2024 {
2025 __f_ = (__base*)&__buf_;
2026 __f.__f_->__clone(__f_);
2027 }
2028 else
2029 __f_ = __f.__f_->__clone();
2030}
2031
2032template<class _Rp, class _A0>
2033template<class _Alloc>
2034function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
2035{
2036 if (__f.__f_ == 0)
2037 __f_ = 0;
2038 else if (__f.__f_ == (const __base*)&__f.__buf_)
2039 {
2040 __f_ = (__base*)&__buf_;
2041 __f.__f_->__clone(__f_);
2042 }
2043 else
2044 __f_ = __f.__f_->__clone();
2045}
2046
2047template<class _Rp, class _A0>
2048template <class _Fp>
2049function<_Rp(_A0)>::function(_Fp __f,
2050 typename enable_if<!is_integral<_Fp>::value>::type*)
2051 : __f_(0)
2052{
2053 if (__function::__not_null(__f))
2054 {
2055 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
2056 if (sizeof(_FF) <= sizeof(__buf_))
2057 {
2058 __f_ = (__base*)&__buf_;
2059 ::new ((void*)__f_) _FF(__f);
2060 }
2061 else
2062 {
2063 typedef allocator<_FF> _Ap;
2064 _Ap __a;
2065 typedef __allocator_destructor<_Ap> _Dp;
2066 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2067 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2068 __f_ = __hold.release();
2069 }
2070 }
2071}
2072
2073template<class _Rp, class _A0>
2074template <class _Fp, class _Alloc>
2075function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2076 typename enable_if<!is_integral<_Fp>::value>::type*)
2077 : __f_(0)
2078{
2079 typedef allocator_traits<_Alloc> __alloc_traits;
2080 if (__function::__not_null(__f))
2081 {
2082 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
2083 if (sizeof(_FF) <= sizeof(__buf_))
2084 {
2085 __f_ = (__base*)&__buf_;
2086 ::new ((void*)__f_) _FF(__f, __a0);
2087 }
2088 else
2089 {
2090 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2091 _Ap __a(__a0);
2092 typedef __allocator_destructor<_Ap> _Dp;
2093 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2094 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2095 __f_ = __hold.release();
2096 }
2097 }
2098}
2099
2100template<class _Rp, class _A0>
2101function<_Rp(_A0)>&
2102function<_Rp(_A0)>::operator=(const function& __f)
2103{
2104 if (__f)
2105 function(__f).swap(*this);
2106 else
2107 *this = nullptr;
2108 return *this;
2109}
2110
2111template<class _Rp, class _A0>
2112function<_Rp(_A0)>&
2113function<_Rp(_A0)>::operator=(nullptr_t)
2114{
2115 __base* __t = __f_;
2116 __f_ = 0;
2117 if (__t == (__base*)&__buf_)
2118 __t->destroy();
2119 else if (__t)
2120 __t->destroy_deallocate();
2121 return *this;
2122}
2123
2124template<class _Rp, class _A0>
2125template <class _Fp>
2126typename enable_if
2127<
2128 !is_integral<_Fp>::value,
2129 function<_Rp(_A0)>&
2130>::type
2131function<_Rp(_A0)>::operator=(_Fp __f)
2132{
2133 function(_VSTD::move(__f)).swap(*this);
2134 return *this;
2135}
2136
2137template<class _Rp, class _A0>
2138function<_Rp(_A0)>::~function()
2139{
2140 if (__f_ == (__base*)&__buf_)
2141 __f_->destroy();
2142 else if (__f_)
2143 __f_->destroy_deallocate();
2144}
2145
2146template<class _Rp, class _A0>
2147void
2148function<_Rp(_A0)>::swap(function& __f)
2149{
2150 if (_VSTD::addressof(__f) == this)
2151 return;
2152 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2153 {
2154 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2155 __base* __t = (__base*)&__tempbuf;
2156 __f_->__clone(__t);
2157 __f_->destroy();
2158 __f_ = 0;
2159 __f.__f_->__clone((__base*)&__buf_);
2160 __f.__f_->destroy();
2161 __f.__f_ = 0;
2162 __f_ = (__base*)&__buf_;
2163 __t->__clone((__base*)&__f.__buf_);
2164 __t->destroy();
2165 __f.__f_ = (__base*)&__f.__buf_;
2166 }
2167 else if (__f_ == (__base*)&__buf_)
2168 {
2169 __f_->__clone((__base*)&__f.__buf_);
2170 __f_->destroy();
2171 __f_ = __f.__f_;
2172 __f.__f_ = (__base*)&__f.__buf_;
2173 }
2174 else if (__f.__f_ == (__base*)&__f.__buf_)
2175 {
2176 __f.__f_->__clone((__base*)&__buf_);
2177 __f.__f_->destroy();
2178 __f.__f_ = __f_;
2179 __f_ = (__base*)&__buf_;
2180 }
2181 else
2182 _VSTD::swap(__f_, __f.__f_);
2183}
2184
2185template<class _Rp, class _A0>
2186_Rp
2187function<_Rp(_A0)>::operator()(_A0 __a0) const
2188{
2189 if (__f_ == 0)
2190 __throw_bad_function_call();
2191 return (*__f_)(__a0);
2192}
2193
2194#ifndef _LIBCPP_NO_RTTI
2195
2196template<class _Rp, class _A0>
2197const std::type_info&
2198function<_Rp(_A0)>::target_type() const
2199{
2200 if (__f_ == 0)
2201 return typeid(void);
2202 return __f_->target_type();
2203}
2204
2205template<class _Rp, class _A0>
2206template <typename _Tp>
2207_Tp*
2208function<_Rp(_A0)>::target()
2209{
2210 if (__f_ == 0)
2211 return (_Tp*)0;
2212 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2213}
2214
2215template<class _Rp, class _A0>
2216template <typename _Tp>
2217const _Tp*
2218function<_Rp(_A0)>::target() const
2219{
2220 if (__f_ == 0)
2221 return (const _Tp*)0;
2222 return (const _Tp*)__f_->target(typeid(_Tp));
2223}
2224
2225#endif // _LIBCPP_NO_RTTI
2226
2227template<class _Rp, class _A0, class _A1>
2228class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1)>
2229 : public binary_function<_A0, _A1, _Rp>
2230{
2231 typedef __function::__base<_Rp(_A0, _A1)> __base;
2232 aligned_storage<3*sizeof(void*)>::type __buf_;
2233 __base* __f_;
2234
2235public:
2236 typedef _Rp result_type;
2237
2238 // 20.7.16.2.1, construct/copy/destroy:
2239 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
2240 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
2241 function(const function&);
2242 template<class _Fp>
2243 function(_Fp,
2244 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2245
2246 template<class _Alloc>
2247 _LIBCPP_INLINE_VISIBILITY
2248 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
2249 template<class _Alloc>
2250 _LIBCPP_INLINE_VISIBILITY
2251 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
2252 template<class _Alloc>
2253 function(allocator_arg_t, const _Alloc&, const function&);
2254 template<class _Fp, class _Alloc>
2255 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
2256 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2257
2258 function& operator=(const function&);
2259 function& operator=(nullptr_t);
2260 template<class _Fp>
2261 typename enable_if
2262 <
2263 !is_integral<_Fp>::value,
2264 function&
2265 >::type
2266 operator=(_Fp);
2267
2268 ~function();
2269
2270 // 20.7.16.2.2, function modifiers:
2271 void swap(function&);
2272 template<class _Fp, class _Alloc>
2273 _LIBCPP_INLINE_VISIBILITY
2274 void assign(_Fp __f, const _Alloc& __a)
2275 {function(allocator_arg, __a, __f).swap(*this);}
2276
2277 // 20.7.16.2.3, function capacity:
2278 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2279
2280 template<class _R2, class _B0, class _B1>
2281 bool operator==(const function<_R2(_B0, _B1)>&) const = delete;
2282 template<class _R2, class _B0, class _B1>
2283 bool operator!=(const function<_R2(_B0, _B1)>&) const = delete;
2284
2285 // 20.7.16.2.4, function invocation:
2286 _Rp operator()(_A0, _A1) const;
2287
2288#ifndef _LIBCPP_NO_RTTI
2289 // 20.7.16.2.5, function target access:
2290 const std::type_info& target_type() const;
2291 template <typename _Tp> _Tp* target();
2292 template <typename _Tp> const _Tp* target() const;
2293#endif // _LIBCPP_NO_RTTI
2294};
2295
2296template<class _Rp, class _A0, class _A1>
2297function<_Rp(_A0, _A1)>::function(const function& __f)
2298{
2299 if (__f.__f_ == 0)
2300 __f_ = 0;
2301 else if (__f.__f_ == (const __base*)&__f.__buf_)
2302 {
2303 __f_ = (__base*)&__buf_;
2304 __f.__f_->__clone(__f_);
2305 }
2306 else
2307 __f_ = __f.__f_->__clone();
2308}
2309
2310template<class _Rp, class _A0, class _A1>
2311template<class _Alloc>
2312function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
2313{
2314 if (__f.__f_ == 0)
2315 __f_ = 0;
2316 else if (__f.__f_ == (const __base*)&__f.__buf_)
2317 {
2318 __f_ = (__base*)&__buf_;
2319 __f.__f_->__clone(__f_);
2320 }
2321 else
2322 __f_ = __f.__f_->__clone();
2323}
2324
2325template<class _Rp, class _A0, class _A1>
2326template <class _Fp>
2327function<_Rp(_A0, _A1)>::function(_Fp __f,
2328 typename enable_if<!is_integral<_Fp>::value>::type*)
2329 : __f_(0)
2330{
2331 if (__function::__not_null(__f))
2332 {
2333 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
2334 if (sizeof(_FF) <= sizeof(__buf_))
2335 {
2336 __f_ = (__base*)&__buf_;
2337 ::new ((void*)__f_) _FF(__f);
2338 }
2339 else
2340 {
2341 typedef allocator<_FF> _Ap;
2342 _Ap __a;
2343 typedef __allocator_destructor<_Ap> _Dp;
2344 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2345 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2346 __f_ = __hold.release();
2347 }
2348 }
2349}
2350
2351template<class _Rp, class _A0, class _A1>
2352template <class _Fp, class _Alloc>
2353function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2354 typename enable_if<!is_integral<_Fp>::value>::type*)
2355 : __f_(0)
2356{
2357 typedef allocator_traits<_Alloc> __alloc_traits;
2358 if (__function::__not_null(__f))
2359 {
2360 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
2361 if (sizeof(_FF) <= sizeof(__buf_))
2362 {
2363 __f_ = (__base*)&__buf_;
2364 ::new ((void*)__f_) _FF(__f, __a0);
2365 }
2366 else
2367 {
2368 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2369 _Ap __a(__a0);
2370 typedef __allocator_destructor<_Ap> _Dp;
2371 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2372 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2373 __f_ = __hold.release();
2374 }
2375 }
2376}
2377
2378template<class _Rp, class _A0, class _A1>
2379function<_Rp(_A0, _A1)>&
2380function<_Rp(_A0, _A1)>::operator=(const function& __f)
2381{
2382 if (__f)
2383 function(__f).swap(*this);
2384 else
2385 *this = nullptr;
2386 return *this;
2387}
2388
2389template<class _Rp, class _A0, class _A1>
2390function<_Rp(_A0, _A1)>&
2391function<_Rp(_A0, _A1)>::operator=(nullptr_t)
2392{
2393 __base* __t = __f_;
2394 __f_ = 0;
2395 if (__t == (__base*)&__buf_)
2396 __t->destroy();
2397 else if (__t)
2398 __t->destroy_deallocate();
2399 return *this;
2400}
2401
2402template<class _Rp, class _A0, class _A1>
2403template <class _Fp>
2404typename enable_if
2405<
2406 !is_integral<_Fp>::value,
2407 function<_Rp(_A0, _A1)>&
2408>::type
2409function<_Rp(_A0, _A1)>::operator=(_Fp __f)
2410{
2411 function(_VSTD::move(__f)).swap(*this);
2412 return *this;
2413}
2414
2415template<class _Rp, class _A0, class _A1>
2416function<_Rp(_A0, _A1)>::~function()
2417{
2418 if (__f_ == (__base*)&__buf_)
2419 __f_->destroy();
2420 else if (__f_)
2421 __f_->destroy_deallocate();
2422}
2423
2424template<class _Rp, class _A0, class _A1>
2425void
2426function<_Rp(_A0, _A1)>::swap(function& __f)
2427{
2428 if (_VSTD::addressof(__f) == this)
2429 return;
2430 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2431 {
2432 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2433 __base* __t = (__base*)&__tempbuf;
2434 __f_->__clone(__t);
2435 __f_->destroy();
2436 __f_ = 0;
2437 __f.__f_->__clone((__base*)&__buf_);
2438 __f.__f_->destroy();
2439 __f.__f_ = 0;
2440 __f_ = (__base*)&__buf_;
2441 __t->__clone((__base*)&__f.__buf_);
2442 __t->destroy();
2443 __f.__f_ = (__base*)&__f.__buf_;
2444 }
2445 else if (__f_ == (__base*)&__buf_)
2446 {
2447 __f_->__clone((__base*)&__f.__buf_);
2448 __f_->destroy();
2449 __f_ = __f.__f_;
2450 __f.__f_ = (__base*)&__f.__buf_;
2451 }
2452 else if (__f.__f_ == (__base*)&__f.__buf_)
2453 {
2454 __f.__f_->__clone((__base*)&__buf_);
2455 __f.__f_->destroy();
2456 __f.__f_ = __f_;
2457 __f_ = (__base*)&__buf_;
2458 }
2459 else
2460 _VSTD::swap(__f_, __f.__f_);
2461}
2462
2463template<class _Rp, class _A0, class _A1>
2464_Rp
2465function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
2466{
2467 if (__f_ == 0)
2468 __throw_bad_function_call();
2469 return (*__f_)(__a0, __a1);
2470}
2471
2472#ifndef _LIBCPP_NO_RTTI
2473
2474template<class _Rp, class _A0, class _A1>
2475const std::type_info&
2476function<_Rp(_A0, _A1)>::target_type() const
2477{
2478 if (__f_ == 0)
2479 return typeid(void);
2480 return __f_->target_type();
2481}
2482
2483template<class _Rp, class _A0, class _A1>
2484template <typename _Tp>
2485_Tp*
2486function<_Rp(_A0, _A1)>::target()
2487{
2488 if (__f_ == 0)
2489 return (_Tp*)0;
2490 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2491}
2492
2493template<class _Rp, class _A0, class _A1>
2494template <typename _Tp>
2495const _Tp*
2496function<_Rp(_A0, _A1)>::target() const
2497{
2498 if (__f_ == 0)
2499 return (const _Tp*)0;
2500 return (const _Tp*)__f_->target(typeid(_Tp));
2501}
2502
2503#endif // _LIBCPP_NO_RTTI
2504
2505template<class _Rp, class _A0, class _A1, class _A2>
2506class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1, _A2)>
2507{
2508 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
2509 aligned_storage<3*sizeof(void*)>::type __buf_;
2510 __base* __f_;
2511
2512public:
2513 typedef _Rp result_type;
2514
2515 // 20.7.16.2.1, construct/copy/destroy:
2516 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
2517 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
2518 function(const function&);
2519 template<class _Fp>
2520 function(_Fp,
2521 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2522
2523 template<class _Alloc>
2524 _LIBCPP_INLINE_VISIBILITY
2525 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
2526 template<class _Alloc>
2527 _LIBCPP_INLINE_VISIBILITY
2528 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
2529 template<class _Alloc>
2530 function(allocator_arg_t, const _Alloc&, const function&);
2531 template<class _Fp, class _Alloc>
2532 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
2533 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2534
2535 function& operator=(const function&);
2536 function& operator=(nullptr_t);
2537 template<class _Fp>
2538 typename enable_if
2539 <
2540 !is_integral<_Fp>::value,
2541 function&
2542 >::type
2543 operator=(_Fp);
2544
2545 ~function();
2546
2547 // 20.7.16.2.2, function modifiers:
2548 void swap(function&);
2549 template<class _Fp, class _Alloc>
2550 _LIBCPP_INLINE_VISIBILITY
2551 void assign(_Fp __f, const _Alloc& __a)
2552 {function(allocator_arg, __a, __f).swap(*this);}
2553
2554 // 20.7.16.2.3, function capacity:
2555 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2556
2557 template<class _R2, class _B0, class _B1, class _B2>
2558 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const = delete;
2559 template<class _R2, class _B0, class _B1, class _B2>
2560 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const = delete;
2561
2562 // 20.7.16.2.4, function invocation:
2563 _Rp operator()(_A0, _A1, _A2) const;
2564
2565#ifndef _LIBCPP_NO_RTTI
2566 // 20.7.16.2.5, function target access:
2567 const std::type_info& target_type() const;
2568 template <typename _Tp> _Tp* target();
2569 template <typename _Tp> const _Tp* target() const;
2570#endif // _LIBCPP_NO_RTTI
2571};
2572
2573template<class _Rp, class _A0, class _A1, class _A2>
2574function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
2575{
2576 if (__f.__f_ == 0)
2577 __f_ = 0;
2578 else if (__f.__f_ == (const __base*)&__f.__buf_)
2579 {
2580 __f_ = (__base*)&__buf_;
2581 __f.__f_->__clone(__f_);
2582 }
2583 else
2584 __f_ = __f.__f_->__clone();
2585}
2586
2587template<class _Rp, class _A0, class _A1, class _A2>
2588template<class _Alloc>
2589function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
2590 const function& __f)
2591{
2592 if (__f.__f_ == 0)
2593 __f_ = 0;
2594 else if (__f.__f_ == (const __base*)&__f.__buf_)
2595 {
2596 __f_ = (__base*)&__buf_;
2597 __f.__f_->__clone(__f_);
2598 }
2599 else
2600 __f_ = __f.__f_->__clone();
2601}
2602
2603template<class _Rp, class _A0, class _A1, class _A2>
2604template <class _Fp>
2605function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
2606 typename enable_if<!is_integral<_Fp>::value>::type*)
2607 : __f_(0)
2608{
2609 if (__function::__not_null(__f))
2610 {
2611 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
2612 if (sizeof(_FF) <= sizeof(__buf_))
2613 {
2614 __f_ = (__base*)&__buf_;
2615 ::new ((void*)__f_) _FF(__f);
2616 }
2617 else
2618 {
2619 typedef allocator<_FF> _Ap;
2620 _Ap __a;
2621 typedef __allocator_destructor<_Ap> _Dp;
2622 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2623 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2624 __f_ = __hold.release();
2625 }
2626 }
2627}
2628
2629template<class _Rp, class _A0, class _A1, class _A2>
2630template <class _Fp, class _Alloc>
2631function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2632 typename enable_if<!is_integral<_Fp>::value>::type*)
2633 : __f_(0)
2634{
2635 typedef allocator_traits<_Alloc> __alloc_traits;
2636 if (__function::__not_null(__f))
2637 {
2638 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
2639 if (sizeof(_FF) <= sizeof(__buf_))
2640 {
2641 __f_ = (__base*)&__buf_;
2642 ::new ((void*)__f_) _FF(__f, __a0);
2643 }
2644 else
2645 {
2646 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2647 _Ap __a(__a0);
2648 typedef __allocator_destructor<_Ap> _Dp;
2649 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2650 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2651 __f_ = __hold.release();
2652 }
2653 }
2654}
2655
2656template<class _Rp, class _A0, class _A1, class _A2>
2657function<_Rp(_A0, _A1, _A2)>&
2658function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
2659{
2660 if (__f)
2661 function(__f).swap(*this);
2662 else
2663 *this = nullptr;
2664 return *this;
2665}
2666
2667template<class _Rp, class _A0, class _A1, class _A2>
2668function<_Rp(_A0, _A1, _A2)>&
2669function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
2670{
2671 __base* __t = __f_;
2672 __f_ = 0;
2673 if (__t == (__base*)&__buf_)
2674 __t->destroy();
2675 else if (__t)
2676 __t->destroy_deallocate();
2677 return *this;
2678}
2679
2680template<class _Rp, class _A0, class _A1, class _A2>
2681template <class _Fp>
2682typename enable_if
2683<
2684 !is_integral<_Fp>::value,
2685 function<_Rp(_A0, _A1, _A2)>&
2686>::type
2687function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
2688{
2689 function(_VSTD::move(__f)).swap(*this);
2690 return *this;
2691}
2692
2693template<class _Rp, class _A0, class _A1, class _A2>
2694function<_Rp(_A0, _A1, _A2)>::~function()
2695{
2696 if (__f_ == (__base*)&__buf_)
2697 __f_->destroy();
2698 else if (__f_)
2699 __f_->destroy_deallocate();
2700}
2701
2702template<class _Rp, class _A0, class _A1, class _A2>
2703void
2704function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
2705{
2706 if (_VSTD::addressof(__f) == this)
2707 return;
2708 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2709 {
2710 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2711 __base* __t = (__base*)&__tempbuf;
2712 __f_->__clone(__t);
2713 __f_->destroy();
2714 __f_ = 0;
2715 __f.__f_->__clone((__base*)&__buf_);
2716 __f.__f_->destroy();
2717 __f.__f_ = 0;
2718 __f_ = (__base*)&__buf_;
2719 __t->__clone((__base*)&__f.__buf_);
2720 __t->destroy();
2721 __f.__f_ = (__base*)&__f.__buf_;
2722 }
2723 else if (__f_ == (__base*)&__buf_)
2724 {
2725 __f_->__clone((__base*)&__f.__buf_);
2726 __f_->destroy();
2727 __f_ = __f.__f_;
2728 __f.__f_ = (__base*)&__f.__buf_;
2729 }
2730 else if (__f.__f_ == (__base*)&__f.__buf_)
2731 {
2732 __f.__f_->__clone((__base*)&__buf_);
2733 __f.__f_->destroy();
2734 __f.__f_ = __f_;
2735 __f_ = (__base*)&__buf_;
2736 }
2737 else
2738 _VSTD::swap(__f_, __f.__f_);
2739}
2740
2741template<class _Rp, class _A0, class _A1, class _A2>
2742_Rp
2743function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
2744{
2745 if (__f_ == 0)
2746 __throw_bad_function_call();
2747 return (*__f_)(__a0, __a1, __a2);
2748}
2749
2750#ifndef _LIBCPP_NO_RTTI
2751
2752template<class _Rp, class _A0, class _A1, class _A2>
2753const std::type_info&
2754function<_Rp(_A0, _A1, _A2)>::target_type() const
2755{
2756 if (__f_ == 0)
2757 return typeid(void);
2758 return __f_->target_type();
2759}
2760
2761template<class _Rp, class _A0, class _A1, class _A2>
2762template <typename _Tp>
2763_Tp*
2764function<_Rp(_A0, _A1, _A2)>::target()
2765{
2766 if (__f_ == 0)
2767 return (_Tp*)0;
2768 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2769}
2770
2771template<class _Rp, class _A0, class _A1, class _A2>
2772template <typename _Tp>
2773const _Tp*
2774function<_Rp(_A0, _A1, _A2)>::target() const
2775{
2776 if (__f_ == 0)
2777 return (const _Tp*)0;
2778 return (const _Tp*)__f_->target(typeid(_Tp));
2779}
2780
2781#endif // _LIBCPP_NO_RTTI
2782
2783template <class _Fp>
2784inline _LIBCPP_INLINE_VISIBILITY
2785bool
2786operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
2787
2788template <class _Fp>
2789inline _LIBCPP_INLINE_VISIBILITY
2790bool
2791operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
2792
2793template <class _Fp>
2794inline _LIBCPP_INLINE_VISIBILITY
2795bool
2796operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
2797
2798template <class _Fp>
2799inline _LIBCPP_INLINE_VISIBILITY
2800bool
2801operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
2802
2803template <class _Fp>
2804inline _LIBCPP_INLINE_VISIBILITY
2805void
2806swap(function<_Fp>& __x, function<_Fp>& __y)
2807{return __x.swap(__y);}
2808
2809#endif // _LIBCPP_CXX03_LANG
2810
2811_LIBCPP_END_NAMESPACE_STD
2812
2813#endif // _LIBCPP___FUNCTIONAL_FUNCTION_H
2814

source code of flutter_engine/third_party/libcxx/include/__functional/function.h