[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / libcxx / include / __functional / function.h
blobb6d383ce84591c611cdac352b2ab257481ffb2bf
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H
11 #define _LIBCPP___FUNCTIONAL_FUNCTION_H
13 #include <__config>
14 #include <__debug>
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 <exception>
24 #include <memory> // TODO: replace with <__memory/__builtin_new_allocator.h>
25 #include <type_traits>
26 #include <utility>
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 #pragma GCC system_header
30 #endif
32 _LIBCPP_BEGIN_NAMESPACE_STD
34 // bad_function_call
36 class _LIBCPP_EXCEPTION_ABI bad_function_call
37 : public exception
39 public:
40 // Note that when a key function is not used, every translation unit that uses
41 // bad_function_call will end up containing a weak definition of the vtable and
42 // typeinfo.
43 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
44 virtual ~bad_function_call() _NOEXCEPT;
45 #else
46 virtual ~bad_function_call() _NOEXCEPT {}
47 #endif
49 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
50 virtual const char* what() const _NOEXCEPT;
51 #endif
54 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
55 void __throw_bad_function_call()
57 #ifndef _LIBCPP_NO_EXCEPTIONS
58 throw bad_function_call();
59 #else
60 _VSTD::abort();
61 #endif
64 #if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
65 # define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
66 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
67 #else
68 # define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
69 #endif
71 template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
73 namespace __function
76 template<class _Rp>
77 struct __maybe_derive_from_unary_function
81 template<class _Rp, class _A1>
82 struct __maybe_derive_from_unary_function<_Rp(_A1)>
83 : public unary_function<_A1, _Rp>
87 template<class _Rp>
88 struct __maybe_derive_from_binary_function
92 template<class _Rp, class _A1, class _A2>
93 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
94 : public binary_function<_A1, _A2, _Rp>
98 template <class _Fp>
99 _LIBCPP_INLINE_VISIBILITY
100 bool __not_null(_Fp const&) { return true; }
102 template <class _Fp>
103 _LIBCPP_INLINE_VISIBILITY
104 bool __not_null(_Fp* __ptr) { return __ptr; }
106 template <class _Ret, class _Class>
107 _LIBCPP_INLINE_VISIBILITY
108 bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
110 template <class _Fp>
111 _LIBCPP_INLINE_VISIBILITY
112 bool __not_null(function<_Fp> const& __f) { return !!__f; }
114 #ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
115 template <class _Rp, class ..._Args>
116 _LIBCPP_INLINE_VISIBILITY
117 bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
118 #endif
120 } // namespace __function
122 #ifndef _LIBCPP_CXX03_LANG
124 namespace __function {
126 // __alloc_func holds a functor and an allocator.
128 template <class _Fp, class _Ap, class _FB> class __alloc_func;
129 template <class _Fp, class _FB>
130 class __default_alloc_func;
132 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
133 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
135 __compressed_pair<_Fp, _Ap> __f_;
137 public:
138 typedef _LIBCPP_NODEBUG _Fp _Target;
139 typedef _LIBCPP_NODEBUG _Ap _Alloc;
141 _LIBCPP_INLINE_VISIBILITY
142 const _Target& __target() const { return __f_.first(); }
144 // WIN32 APIs may define __allocator, so use __get_allocator instead.
145 _LIBCPP_INLINE_VISIBILITY
146 const _Alloc& __get_allocator() const { return __f_.second(); }
148 _LIBCPP_INLINE_VISIBILITY
149 explicit __alloc_func(_Target&& __f)
150 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
151 _VSTD::forward_as_tuple())
155 _LIBCPP_INLINE_VISIBILITY
156 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
157 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
158 _VSTD::forward_as_tuple(__a))
162 _LIBCPP_INLINE_VISIBILITY
163 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
164 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
165 _VSTD::forward_as_tuple(_VSTD::move(__a)))
169 _LIBCPP_INLINE_VISIBILITY
170 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
171 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
172 _VSTD::forward_as_tuple(_VSTD::move(__a)))
176 _LIBCPP_INLINE_VISIBILITY
177 _Rp operator()(_ArgTypes&&... __arg)
179 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
180 return _Invoker::__call(__f_.first(),
181 _VSTD::forward<_ArgTypes>(__arg)...);
184 _LIBCPP_INLINE_VISIBILITY
185 __alloc_func* __clone() const
187 typedef allocator_traits<_Alloc> __alloc_traits;
188 typedef
189 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
190 _AA;
191 _AA __a(__f_.second());
192 typedef __allocator_destructor<_AA> _Dp;
193 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
194 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
195 return __hold.release();
198 _LIBCPP_INLINE_VISIBILITY
199 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
201 static void __destroy_and_delete(__alloc_func* __f) {
202 typedef allocator_traits<_Alloc> __alloc_traits;
203 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
204 _FunAlloc;
205 _FunAlloc __a(__f->__get_allocator());
206 __f->destroy();
207 __a.deallocate(__f, 1);
211 template <class _Fp, class _Rp, class... _ArgTypes>
212 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
213 _Fp __f_;
215 public:
216 typedef _LIBCPP_NODEBUG _Fp _Target;
218 _LIBCPP_INLINE_VISIBILITY
219 const _Target& __target() const { return __f_; }
221 _LIBCPP_INLINE_VISIBILITY
222 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
224 _LIBCPP_INLINE_VISIBILITY
225 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
227 _LIBCPP_INLINE_VISIBILITY
228 _Rp operator()(_ArgTypes&&... __arg) {
229 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
230 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
233 _LIBCPP_INLINE_VISIBILITY
234 __default_alloc_func* __clone() const {
235 __builtin_new_allocator::__holder_t __hold =
236 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
237 __default_alloc_func* __res =
238 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
239 (void)__hold.release();
240 return __res;
243 _LIBCPP_INLINE_VISIBILITY
244 void destroy() _NOEXCEPT { __f_.~_Target(); }
246 static void __destroy_and_delete(__default_alloc_func* __f) {
247 __f->destroy();
248 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
252 // __base provides an abstract interface for copyable functors.
254 template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
256 template<class _Rp, class ..._ArgTypes>
257 class __base<_Rp(_ArgTypes...)>
259 __base(const __base&);
260 __base& operator=(const __base&);
261 public:
262 _LIBCPP_INLINE_VISIBILITY __base() {}
263 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
264 virtual __base* __clone() const = 0;
265 virtual void __clone(__base*) const = 0;
266 virtual void destroy() _NOEXCEPT = 0;
267 virtual void destroy_deallocate() _NOEXCEPT = 0;
268 virtual _Rp operator()(_ArgTypes&& ...) = 0;
269 #ifndef _LIBCPP_NO_RTTI
270 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
271 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
272 #endif // _LIBCPP_NO_RTTI
275 // __func implements __base for a given functor type.
277 template<class _FD, class _Alloc, class _FB> class __func;
279 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
280 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
281 : public __base<_Rp(_ArgTypes...)>
283 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
284 public:
285 _LIBCPP_INLINE_VISIBILITY
286 explicit __func(_Fp&& __f)
287 : __f_(_VSTD::move(__f)) {}
289 _LIBCPP_INLINE_VISIBILITY
290 explicit __func(const _Fp& __f, const _Alloc& __a)
291 : __f_(__f, __a) {}
293 _LIBCPP_INLINE_VISIBILITY
294 explicit __func(const _Fp& __f, _Alloc&& __a)
295 : __f_(__f, _VSTD::move(__a)) {}
297 _LIBCPP_INLINE_VISIBILITY
298 explicit __func(_Fp&& __f, _Alloc&& __a)
299 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
301 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
302 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
303 virtual void destroy() _NOEXCEPT;
304 virtual void destroy_deallocate() _NOEXCEPT;
305 virtual _Rp operator()(_ArgTypes&&... __arg);
306 #ifndef _LIBCPP_NO_RTTI
307 virtual const void* target(const type_info&) const _NOEXCEPT;
308 virtual const std::type_info& target_type() const _NOEXCEPT;
309 #endif // _LIBCPP_NO_RTTI
312 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
313 __base<_Rp(_ArgTypes...)>*
314 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
316 typedef allocator_traits<_Alloc> __alloc_traits;
317 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
318 _Ap __a(__f_.__get_allocator());
319 typedef __allocator_destructor<_Ap> _Dp;
320 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
321 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
322 return __hold.release();
325 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
326 void
327 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
329 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
332 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
333 void
334 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
336 __f_.destroy();
339 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
340 void
341 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
343 typedef allocator_traits<_Alloc> __alloc_traits;
344 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
345 _Ap __a(__f_.__get_allocator());
346 __f_.destroy();
347 __a.deallocate(this, 1);
350 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
352 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
354 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
357 #ifndef _LIBCPP_NO_RTTI
359 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
360 const void*
361 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
363 if (__ti == typeid(_Fp))
364 return _VSTD::addressof(__f_.__target());
365 return nullptr;
368 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
369 const std::type_info&
370 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
372 return typeid(_Fp);
375 #endif // _LIBCPP_NO_RTTI
377 // __value_func creates a value-type from a __func.
379 template <class _Fp> class __value_func;
381 template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
383 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
385 typedef __base<_Rp(_ArgTypes...)> __func;
386 __func* __f_;
388 _LIBCPP_NO_CFI static __func* __as_base(void* p)
390 return reinterpret_cast<__func*>(p);
393 public:
394 _LIBCPP_INLINE_VISIBILITY
395 __value_func() _NOEXCEPT : __f_(nullptr) {}
397 template <class _Fp, class _Alloc>
398 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
399 : __f_(nullptr)
401 typedef allocator_traits<_Alloc> __alloc_traits;
402 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
403 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
404 _FunAlloc;
406 if (__function::__not_null(__f))
408 _FunAlloc __af(__a);
409 if (sizeof(_Fun) <= sizeof(__buf_) &&
410 is_nothrow_copy_constructible<_Fp>::value &&
411 is_nothrow_copy_constructible<_FunAlloc>::value)
413 __f_ =
414 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
416 else
418 typedef __allocator_destructor<_FunAlloc> _Dp;
419 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
420 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
421 __f_ = __hold.release();
426 template <class _Fp,
427 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
428 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
429 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
431 _LIBCPP_INLINE_VISIBILITY
432 __value_func(const __value_func& __f)
434 if (__f.__f_ == nullptr)
435 __f_ = nullptr;
436 else if ((void*)__f.__f_ == &__f.__buf_)
438 __f_ = __as_base(&__buf_);
439 __f.__f_->__clone(__f_);
441 else
442 __f_ = __f.__f_->__clone();
445 _LIBCPP_INLINE_VISIBILITY
446 __value_func(__value_func&& __f) _NOEXCEPT
448 if (__f.__f_ == nullptr)
449 __f_ = nullptr;
450 else if ((void*)__f.__f_ == &__f.__buf_)
452 __f_ = __as_base(&__buf_);
453 __f.__f_->__clone(__f_);
455 else
457 __f_ = __f.__f_;
458 __f.__f_ = nullptr;
462 _LIBCPP_INLINE_VISIBILITY
463 ~__value_func()
465 if ((void*)__f_ == &__buf_)
466 __f_->destroy();
467 else if (__f_)
468 __f_->destroy_deallocate();
471 _LIBCPP_INLINE_VISIBILITY
472 __value_func& operator=(__value_func&& __f)
474 *this = nullptr;
475 if (__f.__f_ == nullptr)
476 __f_ = nullptr;
477 else if ((void*)__f.__f_ == &__f.__buf_)
479 __f_ = __as_base(&__buf_);
480 __f.__f_->__clone(__f_);
482 else
484 __f_ = __f.__f_;
485 __f.__f_ = nullptr;
487 return *this;
490 _LIBCPP_INLINE_VISIBILITY
491 __value_func& operator=(nullptr_t)
493 __func* __f = __f_;
494 __f_ = nullptr;
495 if ((void*)__f == &__buf_)
496 __f->destroy();
497 else if (__f)
498 __f->destroy_deallocate();
499 return *this;
502 _LIBCPP_INLINE_VISIBILITY
503 _Rp operator()(_ArgTypes&&... __args) const
505 if (__f_ == nullptr)
506 __throw_bad_function_call();
507 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
510 _LIBCPP_INLINE_VISIBILITY
511 void swap(__value_func& __f) _NOEXCEPT
513 if (&__f == this)
514 return;
515 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
517 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
518 __func* __t = __as_base(&__tempbuf);
519 __f_->__clone(__t);
520 __f_->destroy();
521 __f_ = nullptr;
522 __f.__f_->__clone(__as_base(&__buf_));
523 __f.__f_->destroy();
524 __f.__f_ = nullptr;
525 __f_ = __as_base(&__buf_);
526 __t->__clone(__as_base(&__f.__buf_));
527 __t->destroy();
528 __f.__f_ = __as_base(&__f.__buf_);
530 else if ((void*)__f_ == &__buf_)
532 __f_->__clone(__as_base(&__f.__buf_));
533 __f_->destroy();
534 __f_ = __f.__f_;
535 __f.__f_ = __as_base(&__f.__buf_);
537 else if ((void*)__f.__f_ == &__f.__buf_)
539 __f.__f_->__clone(__as_base(&__buf_));
540 __f.__f_->destroy();
541 __f.__f_ = __f_;
542 __f_ = __as_base(&__buf_);
544 else
545 _VSTD::swap(__f_, __f.__f_);
548 _LIBCPP_INLINE_VISIBILITY
549 explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
551 #ifndef _LIBCPP_NO_RTTI
552 _LIBCPP_INLINE_VISIBILITY
553 const std::type_info& target_type() const _NOEXCEPT
555 if (__f_ == nullptr)
556 return typeid(void);
557 return __f_->target_type();
560 template <typename _Tp>
561 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
563 if (__f_ == nullptr)
564 return nullptr;
565 return (const _Tp*)__f_->target(typeid(_Tp));
567 #endif // _LIBCPP_NO_RTTI
570 // Storage for a functor object, to be used with __policy to manage copy and
571 // destruction.
572 union __policy_storage
574 mutable char __small[sizeof(void*) * 2];
575 void* __large;
578 // True if _Fun can safely be held in __policy_storage.__small.
579 template <typename _Fun>
580 struct __use_small_storage
581 : public integral_constant<
582 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
583 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
584 is_trivially_copy_constructible<_Fun>::value &&
585 is_trivially_destructible<_Fun>::value> {};
587 // Policy contains information about how to copy, destroy, and move the
588 // underlying functor. You can think of it as a vtable of sorts.
589 struct __policy
591 // Used to copy or destroy __large values. null for trivial objects.
592 void* (*const __clone)(const void*);
593 void (*const __destroy)(void*);
595 // True if this is the null policy (no value).
596 const bool __is_null;
598 // The target type. May be null if RTTI is disabled.
599 const std::type_info* const __type_info;
601 // Returns a pointer to a static policy object suitable for the functor
602 // type.
603 template <typename _Fun>
604 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
606 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
609 _LIBCPP_INLINE_VISIBILITY
610 static const __policy* __create_empty()
612 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
613 true,
614 #ifndef _LIBCPP_NO_RTTI
615 &typeid(void)
616 #else
617 nullptr
618 #endif
620 return &__policy_;
623 private:
624 template <typename _Fun> static void* __large_clone(const void* __s)
626 const _Fun* __f = static_cast<const _Fun*>(__s);
627 return __f->__clone();
630 template <typename _Fun>
631 static void __large_destroy(void* __s) {
632 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
635 template <typename _Fun>
636 _LIBCPP_INLINE_VISIBILITY static const __policy*
637 __choose_policy(/* is_small = */ false_type) {
638 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
639 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
640 #ifndef _LIBCPP_NO_RTTI
641 &typeid(typename _Fun::_Target)
642 #else
643 nullptr
644 #endif
646 return &__policy_;
649 template <typename _Fun>
650 _LIBCPP_INLINE_VISIBILITY static const __policy*
651 __choose_policy(/* is_small = */ true_type)
653 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
654 nullptr, nullptr, false,
655 #ifndef _LIBCPP_NO_RTTI
656 &typeid(typename _Fun::_Target)
657 #else
658 nullptr
659 #endif
661 return &__policy_;
665 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
666 // faster for types that can be passed in registers.
667 template <typename _Tp>
668 using __fast_forward =
669 typename conditional<is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
671 // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
673 template <class _Fp> struct __policy_invoker;
675 template <class _Rp, class... _ArgTypes>
676 struct __policy_invoker<_Rp(_ArgTypes...)>
678 typedef _Rp (*__Call)(const __policy_storage*,
679 __fast_forward<_ArgTypes>...);
681 __Call __call_;
683 // Creates an invoker that throws bad_function_call.
684 _LIBCPP_INLINE_VISIBILITY
685 __policy_invoker() : __call_(&__call_empty) {}
687 // Creates an invoker that calls the given instance of __func.
688 template <typename _Fun>
689 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
691 return __policy_invoker(&__call_impl<_Fun>);
694 private:
695 _LIBCPP_INLINE_VISIBILITY
696 explicit __policy_invoker(__Call __c) : __call_(__c) {}
698 static _Rp __call_empty(const __policy_storage*,
699 __fast_forward<_ArgTypes>...)
701 __throw_bad_function_call();
704 template <typename _Fun>
705 static _Rp __call_impl(const __policy_storage* __buf,
706 __fast_forward<_ArgTypes>... __args)
708 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
709 ? &__buf->__small
710 : __buf->__large);
711 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
715 // __policy_func uses a __policy and __policy_invoker to create a type-erased,
716 // copyable functor.
718 template <class _Fp> class __policy_func;
720 template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
722 // Inline storage for small objects.
723 __policy_storage __buf_;
725 // Calls the value stored in __buf_. This could technically be part of
726 // policy, but storing it here eliminates a level of indirection inside
727 // operator().
728 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
729 __invoker __invoker_;
731 // The policy that describes how to move / copy / destroy __buf_. Never
732 // null, even if the function is empty.
733 const __policy* __policy_;
735 public:
736 _LIBCPP_INLINE_VISIBILITY
737 __policy_func() : __policy_(__policy::__create_empty()) {}
739 template <class _Fp, class _Alloc>
740 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
741 : __policy_(__policy::__create_empty())
743 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
744 typedef allocator_traits<_Alloc> __alloc_traits;
745 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
746 _FunAlloc;
748 if (__function::__not_null(__f))
750 __invoker_ = __invoker::template __create<_Fun>();
751 __policy_ = __policy::__create<_Fun>();
753 _FunAlloc __af(__a);
754 if (__use_small_storage<_Fun>())
756 ::new ((void*)&__buf_.__small)
757 _Fun(_VSTD::move(__f), _Alloc(__af));
759 else
761 typedef __allocator_destructor<_FunAlloc> _Dp;
762 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
763 ::new ((void*)__hold.get())
764 _Fun(_VSTD::move(__f), _Alloc(__af));
765 __buf_.__large = __hold.release();
770 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
771 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
772 : __policy_(__policy::__create_empty()) {
773 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
775 if (__function::__not_null(__f)) {
776 __invoker_ = __invoker::template __create<_Fun>();
777 __policy_ = __policy::__create<_Fun>();
778 if (__use_small_storage<_Fun>()) {
779 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
780 } else {
781 __builtin_new_allocator::__holder_t __hold =
782 __builtin_new_allocator::__allocate_type<_Fun>(1);
783 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
784 (void)__hold.release();
789 _LIBCPP_INLINE_VISIBILITY
790 __policy_func(const __policy_func& __f)
791 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
792 __policy_(__f.__policy_)
794 if (__policy_->__clone)
795 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
798 _LIBCPP_INLINE_VISIBILITY
799 __policy_func(__policy_func&& __f)
800 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
801 __policy_(__f.__policy_)
803 if (__policy_->__destroy)
805 __f.__policy_ = __policy::__create_empty();
806 __f.__invoker_ = __invoker();
810 _LIBCPP_INLINE_VISIBILITY
811 ~__policy_func()
813 if (__policy_->__destroy)
814 __policy_->__destroy(__buf_.__large);
817 _LIBCPP_INLINE_VISIBILITY
818 __policy_func& operator=(__policy_func&& __f)
820 *this = nullptr;
821 __buf_ = __f.__buf_;
822 __invoker_ = __f.__invoker_;
823 __policy_ = __f.__policy_;
824 __f.__policy_ = __policy::__create_empty();
825 __f.__invoker_ = __invoker();
826 return *this;
829 _LIBCPP_INLINE_VISIBILITY
830 __policy_func& operator=(nullptr_t)
832 const __policy* __p = __policy_;
833 __policy_ = __policy::__create_empty();
834 __invoker_ = __invoker();
835 if (__p->__destroy)
836 __p->__destroy(__buf_.__large);
837 return *this;
840 _LIBCPP_INLINE_VISIBILITY
841 _Rp operator()(_ArgTypes&&... __args) const
843 return __invoker_.__call_(_VSTD::addressof(__buf_),
844 _VSTD::forward<_ArgTypes>(__args)...);
847 _LIBCPP_INLINE_VISIBILITY
848 void swap(__policy_func& __f)
850 _VSTD::swap(__invoker_, __f.__invoker_);
851 _VSTD::swap(__policy_, __f.__policy_);
852 _VSTD::swap(__buf_, __f.__buf_);
855 _LIBCPP_INLINE_VISIBILITY
856 explicit operator bool() const _NOEXCEPT
858 return !__policy_->__is_null;
861 #ifndef _LIBCPP_NO_RTTI
862 _LIBCPP_INLINE_VISIBILITY
863 const std::type_info& target_type() const _NOEXCEPT
865 return *__policy_->__type_info;
868 template <typename _Tp>
869 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
871 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
872 return nullptr;
873 if (__policy_->__clone) // Out of line storage.
874 return reinterpret_cast<const _Tp*>(__buf_.__large);
875 else
876 return reinterpret_cast<const _Tp*>(&__buf_.__small);
878 #endif // _LIBCPP_NO_RTTI
881 #if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
883 extern "C" void *_Block_copy(const void *);
884 extern "C" void _Block_release(const void *);
886 template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
887 class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
888 : public __base<_Rp(_ArgTypes...)>
890 typedef _Rp1(^__block_type)(_ArgTypes1...);
891 __block_type __f_;
893 public:
894 _LIBCPP_INLINE_VISIBILITY
895 explicit __func(__block_type const& __f)
896 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
899 // [TODO] add && to save on a retain
901 _LIBCPP_INLINE_VISIBILITY
902 explicit __func(__block_type __f, const _Alloc& /* unused */)
903 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
906 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
907 _LIBCPP_ASSERT(false,
908 "Block pointers are just pointers, so they should always fit into "
909 "std::function's small buffer optimization. This function should "
910 "never be invoked.");
911 return nullptr;
914 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
915 ::new ((void*)__p) __func(__f_);
918 virtual void destroy() _NOEXCEPT {
919 if (__f_)
920 _Block_release(__f_);
921 __f_ = 0;
924 virtual void destroy_deallocate() _NOEXCEPT {
925 _LIBCPP_ASSERT(false,
926 "Block pointers are just pointers, so they should always fit into "
927 "std::function's small buffer optimization. This function should "
928 "never be invoked.");
931 virtual _Rp operator()(_ArgTypes&& ... __arg) {
932 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
935 #ifndef _LIBCPP_NO_RTTI
936 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
937 if (__ti == typeid(__func::__block_type))
938 return &__f_;
939 return (const void*)nullptr;
942 virtual const std::type_info& target_type() const _NOEXCEPT {
943 return typeid(__func::__block_type);
945 #endif // _LIBCPP_NO_RTTI
948 #endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
950 } // namespace __function
952 template<class _Rp, class ..._ArgTypes>
953 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
954 #if _LIBCPP_STD_VER <= 17 || !defined(_LIBCPP_ABI_NO_BINDER_BASES)
955 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
956 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
957 #endif
959 #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
960 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
961 #else
962 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
963 #endif
965 __func __f_;
967 template <class _Fp, bool = _And<
968 _IsNotSame<__uncvref_t<_Fp>, function>,
969 __invokable<_Fp, _ArgTypes...>
970 >::value>
971 struct __callable;
972 template <class _Fp>
973 struct __callable<_Fp, true>
975 static const bool value = is_void<_Rp>::value ||
976 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
977 _Rp>::value;
979 template <class _Fp>
980 struct __callable<_Fp, false>
982 static const bool value = false;
985 template <class _Fp>
986 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
987 public:
988 typedef _Rp result_type;
990 // construct/copy/destroy:
991 _LIBCPP_INLINE_VISIBILITY
992 function() _NOEXCEPT { }
993 _LIBCPP_INLINE_VISIBILITY
994 function(nullptr_t) _NOEXCEPT {}
995 function(const function&);
996 function(function&&) _NOEXCEPT;
997 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
998 function(_Fp);
1000 #if _LIBCPP_STD_VER <= 14
1001 template<class _Alloc>
1002 _LIBCPP_INLINE_VISIBILITY
1003 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
1004 template<class _Alloc>
1005 _LIBCPP_INLINE_VISIBILITY
1006 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
1007 template<class _Alloc>
1008 function(allocator_arg_t, const _Alloc&, const function&);
1009 template<class _Alloc>
1010 function(allocator_arg_t, const _Alloc&, function&&);
1011 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
1012 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
1013 #endif
1015 function& operator=(const function&);
1016 function& operator=(function&&) _NOEXCEPT;
1017 function& operator=(nullptr_t) _NOEXCEPT;
1018 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
1019 function& operator=(_Fp&&);
1021 ~function();
1023 // function modifiers:
1024 void swap(function&) _NOEXCEPT;
1026 #if _LIBCPP_STD_VER <= 14
1027 template<class _Fp, class _Alloc>
1028 _LIBCPP_INLINE_VISIBILITY
1029 void assign(_Fp&& __f, const _Alloc& __a)
1030 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
1031 #endif
1033 // function capacity:
1034 _LIBCPP_INLINE_VISIBILITY
1035 explicit operator bool() const _NOEXCEPT {
1036 return static_cast<bool>(__f_);
1039 // deleted overloads close possible hole in the type system
1040 template<class _R2, class... _ArgTypes2>
1041 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
1042 template<class _R2, class... _ArgTypes2>
1043 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
1044 public:
1045 // function invocation:
1046 _Rp operator()(_ArgTypes...) const;
1048 #ifndef _LIBCPP_NO_RTTI
1049 // function target access:
1050 const std::type_info& target_type() const _NOEXCEPT;
1051 template <typename _Tp> _Tp* target() _NOEXCEPT;
1052 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
1053 #endif // _LIBCPP_NO_RTTI
1056 #if _LIBCPP_STD_VER >= 17
1057 template<class _Rp, class ..._Ap>
1058 function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
1060 template<class _Fp>
1061 struct __strip_signature;
1063 template<class _Rp, class _Gp, class ..._Ap>
1064 struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
1065 template<class _Rp, class _Gp, class ..._Ap>
1066 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
1067 template<class _Rp, class _Gp, class ..._Ap>
1068 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
1069 template<class _Rp, class _Gp, class ..._Ap>
1070 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
1072 template<class _Rp, class _Gp, class ..._Ap>
1073 struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
1074 template<class _Rp, class _Gp, class ..._Ap>
1075 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
1076 template<class _Rp, class _Gp, class ..._Ap>
1077 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
1078 template<class _Rp, class _Gp, class ..._Ap>
1079 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
1081 template<class _Rp, class _Gp, class ..._Ap>
1082 struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
1083 template<class _Rp, class _Gp, class ..._Ap>
1084 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
1085 template<class _Rp, class _Gp, class ..._Ap>
1086 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
1087 template<class _Rp, class _Gp, class ..._Ap>
1088 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
1090 template<class _Rp, class _Gp, class ..._Ap>
1091 struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
1092 template<class _Rp, class _Gp, class ..._Ap>
1093 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
1094 template<class _Rp, class _Gp, class ..._Ap>
1095 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
1096 template<class _Rp, class _Gp, class ..._Ap>
1097 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
1099 template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1100 function(_Fp) -> function<_Stripped>;
1101 #endif // _LIBCPP_STD_VER >= 17
1103 template<class _Rp, class ..._ArgTypes>
1104 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
1106 #if _LIBCPP_STD_VER <= 14
1107 template<class _Rp, class ..._ArgTypes>
1108 template <class _Alloc>
1109 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1110 const function& __f) : __f_(__f.__f_) {}
1111 #endif
1113 template <class _Rp, class... _ArgTypes>
1114 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
1115 : __f_(_VSTD::move(__f.__f_)) {}
1117 #if _LIBCPP_STD_VER <= 14
1118 template<class _Rp, class ..._ArgTypes>
1119 template <class _Alloc>
1120 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1121 function&& __f)
1122 : __f_(_VSTD::move(__f.__f_)) {}
1123 #endif
1125 template <class _Rp, class... _ArgTypes>
1126 template <class _Fp, class>
1127 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
1129 #if _LIBCPP_STD_VER <= 14
1130 template <class _Rp, class... _ArgTypes>
1131 template <class _Fp, class _Alloc, class>
1132 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
1133 _Fp __f)
1134 : __f_(_VSTD::move(__f), __a) {}
1135 #endif
1137 template<class _Rp, class ..._ArgTypes>
1138 function<_Rp(_ArgTypes...)>&
1139 function<_Rp(_ArgTypes...)>::operator=(const function& __f)
1141 function(__f).swap(*this);
1142 return *this;
1145 template<class _Rp, class ..._ArgTypes>
1146 function<_Rp(_ArgTypes...)>&
1147 function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
1149 __f_ = _VSTD::move(__f.__f_);
1150 return *this;
1153 template<class _Rp, class ..._ArgTypes>
1154 function<_Rp(_ArgTypes...)>&
1155 function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
1157 __f_ = nullptr;
1158 return *this;
1161 template<class _Rp, class ..._ArgTypes>
1162 template <class _Fp, class>
1163 function<_Rp(_ArgTypes...)>&
1164 function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
1166 function(_VSTD::forward<_Fp>(__f)).swap(*this);
1167 return *this;
1170 template<class _Rp, class ..._ArgTypes>
1171 function<_Rp(_ArgTypes...)>::~function() {}
1173 template<class _Rp, class ..._ArgTypes>
1174 void
1175 function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
1177 __f_.swap(__f.__f_);
1180 template<class _Rp, class ..._ArgTypes>
1182 function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1184 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
1187 #ifndef _LIBCPP_NO_RTTI
1189 template<class _Rp, class ..._ArgTypes>
1190 const std::type_info&
1191 function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1193 return __f_.target_type();
1196 template<class _Rp, class ..._ArgTypes>
1197 template <typename _Tp>
1198 _Tp*
1199 function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
1201 return (_Tp*)(__f_.template target<_Tp>());
1204 template<class _Rp, class ..._ArgTypes>
1205 template <typename _Tp>
1206 const _Tp*
1207 function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
1209 return __f_.template target<_Tp>();
1212 #endif // _LIBCPP_NO_RTTI
1214 template <class _Rp, class... _ArgTypes>
1215 inline _LIBCPP_INLINE_VISIBILITY
1216 bool
1217 operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
1219 template <class _Rp, class... _ArgTypes>
1220 inline _LIBCPP_INLINE_VISIBILITY
1221 bool
1222 operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
1224 template <class _Rp, class... _ArgTypes>
1225 inline _LIBCPP_INLINE_VISIBILITY
1226 bool
1227 operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
1229 template <class _Rp, class... _ArgTypes>
1230 inline _LIBCPP_INLINE_VISIBILITY
1231 bool
1232 operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
1234 template <class _Rp, class... _ArgTypes>
1235 inline _LIBCPP_INLINE_VISIBILITY
1236 void
1237 swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
1238 {return __x.swap(__y);}
1240 #else // _LIBCPP_CXX03_LANG
1242 namespace __function {
1244 template<class _Fp> class __base;
1246 template<class _Rp>
1247 class __base<_Rp()>
1249 __base(const __base&);
1250 __base& operator=(const __base&);
1251 public:
1252 __base() {}
1253 virtual ~__base() {}
1254 virtual __base* __clone() const = 0;
1255 virtual void __clone(__base*) const = 0;
1256 virtual void destroy() = 0;
1257 virtual void destroy_deallocate() = 0;
1258 virtual _Rp operator()() = 0;
1259 #ifndef _LIBCPP_NO_RTTI
1260 virtual const void* target(const type_info&) const = 0;
1261 virtual const std::type_info& target_type() const = 0;
1262 #endif // _LIBCPP_NO_RTTI
1265 template<class _Rp, class _A0>
1266 class __base<_Rp(_A0)>
1268 __base(const __base&);
1269 __base& operator=(const __base&);
1270 public:
1271 __base() {}
1272 virtual ~__base() {}
1273 virtual __base* __clone() const = 0;
1274 virtual void __clone(__base*) const = 0;
1275 virtual void destroy() = 0;
1276 virtual void destroy_deallocate() = 0;
1277 virtual _Rp operator()(_A0) = 0;
1278 #ifndef _LIBCPP_NO_RTTI
1279 virtual const void* target(const type_info&) const = 0;
1280 virtual const std::type_info& target_type() const = 0;
1281 #endif // _LIBCPP_NO_RTTI
1284 template<class _Rp, class _A0, class _A1>
1285 class __base<_Rp(_A0, _A1)>
1287 __base(const __base&);
1288 __base& operator=(const __base&);
1289 public:
1290 __base() {}
1291 virtual ~__base() {}
1292 virtual __base* __clone() const = 0;
1293 virtual void __clone(__base*) const = 0;
1294 virtual void destroy() = 0;
1295 virtual void destroy_deallocate() = 0;
1296 virtual _Rp operator()(_A0, _A1) = 0;
1297 #ifndef _LIBCPP_NO_RTTI
1298 virtual const void* target(const type_info&) const = 0;
1299 virtual const std::type_info& target_type() const = 0;
1300 #endif // _LIBCPP_NO_RTTI
1303 template<class _Rp, class _A0, class _A1, class _A2>
1304 class __base<_Rp(_A0, _A1, _A2)>
1306 __base(const __base&);
1307 __base& operator=(const __base&);
1308 public:
1309 __base() {}
1310 virtual ~__base() {}
1311 virtual __base* __clone() const = 0;
1312 virtual void __clone(__base*) const = 0;
1313 virtual void destroy() = 0;
1314 virtual void destroy_deallocate() = 0;
1315 virtual _Rp operator()(_A0, _A1, _A2) = 0;
1316 #ifndef _LIBCPP_NO_RTTI
1317 virtual const void* target(const type_info&) const = 0;
1318 virtual const std::type_info& target_type() const = 0;
1319 #endif // _LIBCPP_NO_RTTI
1322 template<class _FD, class _Alloc, class _FB> class __func;
1324 template<class _Fp, class _Alloc, class _Rp>
1325 class __func<_Fp, _Alloc, _Rp()>
1326 : public __base<_Rp()>
1328 __compressed_pair<_Fp, _Alloc> __f_;
1329 public:
1330 explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1331 explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1332 virtual __base<_Rp()>* __clone() const;
1333 virtual void __clone(__base<_Rp()>*) const;
1334 virtual void destroy();
1335 virtual void destroy_deallocate();
1336 virtual _Rp operator()();
1337 #ifndef _LIBCPP_NO_RTTI
1338 virtual const void* target(const type_info&) const;
1339 virtual const std::type_info& target_type() const;
1340 #endif // _LIBCPP_NO_RTTI
1343 template<class _Fp, class _Alloc, class _Rp>
1344 __base<_Rp()>*
1345 __func<_Fp, _Alloc, _Rp()>::__clone() const
1347 typedef allocator_traits<_Alloc> __alloc_traits;
1348 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1349 _Ap __a(__f_.second());
1350 typedef __allocator_destructor<_Ap> _Dp;
1351 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1352 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1353 return __hold.release();
1356 template<class _Fp, class _Alloc, class _Rp>
1357 void
1358 __func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
1360 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1363 template<class _Fp, class _Alloc, class _Rp>
1364 void
1365 __func<_Fp, _Alloc, _Rp()>::destroy()
1367 __f_.~__compressed_pair<_Fp, _Alloc>();
1370 template<class _Fp, class _Alloc, class _Rp>
1371 void
1372 __func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
1374 typedef allocator_traits<_Alloc> __alloc_traits;
1375 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1376 _Ap __a(__f_.second());
1377 __f_.~__compressed_pair<_Fp, _Alloc>();
1378 __a.deallocate(this, 1);
1381 template<class _Fp, class _Alloc, class _Rp>
1383 __func<_Fp, _Alloc, _Rp()>::operator()()
1385 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1386 return _Invoker::__call(__f_.first());
1389 #ifndef _LIBCPP_NO_RTTI
1391 template<class _Fp, class _Alloc, class _Rp>
1392 const void*
1393 __func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
1395 if (__ti == typeid(_Fp))
1396 return _VSTD::addressof(__f_.first());
1397 return (const void*)0;
1400 template<class _Fp, class _Alloc, class _Rp>
1401 const std::type_info&
1402 __func<_Fp, _Alloc, _Rp()>::target_type() const
1404 return typeid(_Fp);
1407 #endif // _LIBCPP_NO_RTTI
1409 template<class _Fp, class _Alloc, class _Rp, class _A0>
1410 class __func<_Fp, _Alloc, _Rp(_A0)>
1411 : public __base<_Rp(_A0)>
1413 __compressed_pair<_Fp, _Alloc> __f_;
1414 public:
1415 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1416 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1417 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1418 virtual __base<_Rp(_A0)>* __clone() const;
1419 virtual void __clone(__base<_Rp(_A0)>*) const;
1420 virtual void destroy();
1421 virtual void destroy_deallocate();
1422 virtual _Rp operator()(_A0);
1423 #ifndef _LIBCPP_NO_RTTI
1424 virtual const void* target(const type_info&) const;
1425 virtual const std::type_info& target_type() const;
1426 #endif // _LIBCPP_NO_RTTI
1429 template<class _Fp, class _Alloc, class _Rp, class _A0>
1430 __base<_Rp(_A0)>*
1431 __func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
1433 typedef allocator_traits<_Alloc> __alloc_traits;
1434 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1435 _Ap __a(__f_.second());
1436 typedef __allocator_destructor<_Ap> _Dp;
1437 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1438 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1439 return __hold.release();
1442 template<class _Fp, class _Alloc, class _Rp, class _A0>
1443 void
1444 __func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
1446 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1449 template<class _Fp, class _Alloc, class _Rp, class _A0>
1450 void
1451 __func<_Fp, _Alloc, _Rp(_A0)>::destroy()
1453 __f_.~__compressed_pair<_Fp, _Alloc>();
1456 template<class _Fp, class _Alloc, class _Rp, class _A0>
1457 void
1458 __func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
1460 typedef allocator_traits<_Alloc> __alloc_traits;
1461 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1462 _Ap __a(__f_.second());
1463 __f_.~__compressed_pair<_Fp, _Alloc>();
1464 __a.deallocate(this, 1);
1467 template<class _Fp, class _Alloc, class _Rp, class _A0>
1469 __func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
1471 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1472 return _Invoker::__call(__f_.first(), __a0);
1475 #ifndef _LIBCPP_NO_RTTI
1477 template<class _Fp, class _Alloc, class _Rp, class _A0>
1478 const void*
1479 __func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
1481 if (__ti == typeid(_Fp))
1482 return &__f_.first();
1483 return (const void*)0;
1486 template<class _Fp, class _Alloc, class _Rp, class _A0>
1487 const std::type_info&
1488 __func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
1490 return typeid(_Fp);
1493 #endif // _LIBCPP_NO_RTTI
1495 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1496 class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
1497 : public __base<_Rp(_A0, _A1)>
1499 __compressed_pair<_Fp, _Alloc> __f_;
1500 public:
1501 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1502 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1503 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1504 virtual __base<_Rp(_A0, _A1)>* __clone() const;
1505 virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
1506 virtual void destroy();
1507 virtual void destroy_deallocate();
1508 virtual _Rp operator()(_A0, _A1);
1509 #ifndef _LIBCPP_NO_RTTI
1510 virtual const void* target(const type_info&) const;
1511 virtual const std::type_info& target_type() const;
1512 #endif // _LIBCPP_NO_RTTI
1515 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1516 __base<_Rp(_A0, _A1)>*
1517 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
1519 typedef allocator_traits<_Alloc> __alloc_traits;
1520 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1521 _Ap __a(__f_.second());
1522 typedef __allocator_destructor<_Ap> _Dp;
1523 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1524 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1525 return __hold.release();
1528 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1529 void
1530 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
1532 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1535 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1536 void
1537 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
1539 __f_.~__compressed_pair<_Fp, _Alloc>();
1542 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1543 void
1544 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
1546 typedef allocator_traits<_Alloc> __alloc_traits;
1547 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1548 _Ap __a(__f_.second());
1549 __f_.~__compressed_pair<_Fp, _Alloc>();
1550 __a.deallocate(this, 1);
1553 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1555 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
1557 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1558 return _Invoker::__call(__f_.first(), __a0, __a1);
1561 #ifndef _LIBCPP_NO_RTTI
1563 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1564 const void*
1565 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
1567 if (__ti == typeid(_Fp))
1568 return &__f_.first();
1569 return (const void*)0;
1572 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
1573 const std::type_info&
1574 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
1576 return typeid(_Fp);
1579 #endif // _LIBCPP_NO_RTTI
1581 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1582 class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
1583 : public __base<_Rp(_A0, _A1, _A2)>
1585 __compressed_pair<_Fp, _Alloc> __f_;
1586 public:
1587 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1588 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
1589 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1590 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
1591 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
1592 virtual void destroy();
1593 virtual void destroy_deallocate();
1594 virtual _Rp operator()(_A0, _A1, _A2);
1595 #ifndef _LIBCPP_NO_RTTI
1596 virtual const void* target(const type_info&) const;
1597 virtual const std::type_info& target_type() const;
1598 #endif // _LIBCPP_NO_RTTI
1601 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1602 __base<_Rp(_A0, _A1, _A2)>*
1603 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
1605 typedef allocator_traits<_Alloc> __alloc_traits;
1606 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1607 _Ap __a(__f_.second());
1608 typedef __allocator_destructor<_Ap> _Dp;
1609 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1610 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
1611 return __hold.release();
1614 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1615 void
1616 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
1618 ::new ((void*)__p) __func(__f_.first(), __f_.second());
1621 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1622 void
1623 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
1625 __f_.~__compressed_pair<_Fp, _Alloc>();
1628 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1629 void
1630 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
1632 typedef allocator_traits<_Alloc> __alloc_traits;
1633 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
1634 _Ap __a(__f_.second());
1635 __f_.~__compressed_pair<_Fp, _Alloc>();
1636 __a.deallocate(this, 1);
1639 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1641 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
1643 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1644 return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
1647 #ifndef _LIBCPP_NO_RTTI
1649 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1650 const void*
1651 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
1653 if (__ti == typeid(_Fp))
1654 return &__f_.first();
1655 return (const void*)0;
1658 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
1659 const std::type_info&
1660 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
1662 return typeid(_Fp);
1665 #endif // _LIBCPP_NO_RTTI
1667 } // __function
1669 template<class _Rp>
1670 class _LIBCPP_TEMPLATE_VIS function<_Rp()>
1672 typedef __function::__base<_Rp()> __base;
1673 aligned_storage<3*sizeof(void*)>::type __buf_;
1674 __base* __f_;
1676 public:
1677 typedef _Rp result_type;
1679 // 20.7.16.2.1, construct/copy/destroy:
1680 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1681 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1682 function(const function&);
1683 template<class _Fp>
1684 function(_Fp,
1685 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1687 template<class _Alloc>
1688 _LIBCPP_INLINE_VISIBILITY
1689 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1690 template<class _Alloc>
1691 _LIBCPP_INLINE_VISIBILITY
1692 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1693 template<class _Alloc>
1694 function(allocator_arg_t, const _Alloc&, const function&);
1695 template<class _Fp, class _Alloc>
1696 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1697 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1699 function& operator=(const function&);
1700 function& operator=(nullptr_t);
1701 template<class _Fp>
1702 typename enable_if
1704 !is_integral<_Fp>::value,
1705 function&
1706 >::type
1707 operator=(_Fp);
1709 ~function();
1711 // 20.7.16.2.2, function modifiers:
1712 void swap(function&);
1713 template<class _Fp, class _Alloc>
1714 _LIBCPP_INLINE_VISIBILITY
1715 void assign(_Fp __f, const _Alloc& __a)
1716 {function(allocator_arg, __a, __f).swap(*this);}
1718 // 20.7.16.2.3, function capacity:
1719 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
1721 template<class _R2>
1722 bool operator==(const function<_R2()>&) const = delete;
1723 template<class _R2>
1724 bool operator!=(const function<_R2()>&) const = delete;
1726 // 20.7.16.2.4, function invocation:
1727 _Rp operator()() const;
1729 #ifndef _LIBCPP_NO_RTTI
1730 // 20.7.16.2.5, function target access:
1731 const std::type_info& target_type() const;
1732 template <typename _Tp> _Tp* target();
1733 template <typename _Tp> const _Tp* target() const;
1734 #endif // _LIBCPP_NO_RTTI
1737 template<class _Rp>
1738 function<_Rp()>::function(const function& __f)
1740 if (__f.__f_ == 0)
1741 __f_ = 0;
1742 else if (__f.__f_ == (const __base*)&__f.__buf_)
1744 __f_ = (__base*)&__buf_;
1745 __f.__f_->__clone(__f_);
1747 else
1748 __f_ = __f.__f_->__clone();
1751 template<class _Rp>
1752 template<class _Alloc>
1753 function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
1755 if (__f.__f_ == 0)
1756 __f_ = 0;
1757 else if (__f.__f_ == (const __base*)&__f.__buf_)
1759 __f_ = (__base*)&__buf_;
1760 __f.__f_->__clone(__f_);
1762 else
1763 __f_ = __f.__f_->__clone();
1766 template<class _Rp>
1767 template <class _Fp>
1768 function<_Rp()>::function(_Fp __f,
1769 typename enable_if<!is_integral<_Fp>::value>::type*)
1770 : __f_(0)
1772 if (__function::__not_null(__f))
1774 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
1775 if (sizeof(_FF) <= sizeof(__buf_))
1777 __f_ = (__base*)&__buf_;
1778 ::new ((void*)__f_) _FF(__f);
1780 else
1782 typedef allocator<_FF> _Ap;
1783 _Ap __a;
1784 typedef __allocator_destructor<_Ap> _Dp;
1785 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1786 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
1787 __f_ = __hold.release();
1792 template<class _Rp>
1793 template <class _Fp, class _Alloc>
1794 function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1795 typename enable_if<!is_integral<_Fp>::value>::type*)
1796 : __f_(0)
1798 typedef allocator_traits<_Alloc> __alloc_traits;
1799 if (__function::__not_null(__f))
1801 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
1802 if (sizeof(_FF) <= sizeof(__buf_))
1804 __f_ = (__base*)&__buf_;
1805 ::new ((void*)__f_) _FF(__f, __a0);
1807 else
1809 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1810 _Ap __a(__a0);
1811 typedef __allocator_destructor<_Ap> _Dp;
1812 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1813 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
1814 __f_ = __hold.release();
1819 template<class _Rp>
1820 function<_Rp()>&
1821 function<_Rp()>::operator=(const function& __f)
1823 if (__f)
1824 function(__f).swap(*this);
1825 else
1826 *this = nullptr;
1827 return *this;
1830 template<class _Rp>
1831 function<_Rp()>&
1832 function<_Rp()>::operator=(nullptr_t)
1834 __base* __t = __f_;
1835 __f_ = 0;
1836 if (__t == (__base*)&__buf_)
1837 __t->destroy();
1838 else if (__t)
1839 __t->destroy_deallocate();
1840 return *this;
1843 template<class _Rp>
1844 template <class _Fp>
1845 typename enable_if
1847 !is_integral<_Fp>::value,
1848 function<_Rp()>&
1849 >::type
1850 function<_Rp()>::operator=(_Fp __f)
1852 function(_VSTD::move(__f)).swap(*this);
1853 return *this;
1856 template<class _Rp>
1857 function<_Rp()>::~function()
1859 if (__f_ == (__base*)&__buf_)
1860 __f_->destroy();
1861 else if (__f_)
1862 __f_->destroy_deallocate();
1865 template<class _Rp>
1866 void
1867 function<_Rp()>::swap(function& __f)
1869 if (_VSTD::addressof(__f) == this)
1870 return;
1871 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1873 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1874 __base* __t = (__base*)&__tempbuf;
1875 __f_->__clone(__t);
1876 __f_->destroy();
1877 __f_ = 0;
1878 __f.__f_->__clone((__base*)&__buf_);
1879 __f.__f_->destroy();
1880 __f.__f_ = 0;
1881 __f_ = (__base*)&__buf_;
1882 __t->__clone((__base*)&__f.__buf_);
1883 __t->destroy();
1884 __f.__f_ = (__base*)&__f.__buf_;
1886 else if (__f_ == (__base*)&__buf_)
1888 __f_->__clone((__base*)&__f.__buf_);
1889 __f_->destroy();
1890 __f_ = __f.__f_;
1891 __f.__f_ = (__base*)&__f.__buf_;
1893 else if (__f.__f_ == (__base*)&__f.__buf_)
1895 __f.__f_->__clone((__base*)&__buf_);
1896 __f.__f_->destroy();
1897 __f.__f_ = __f_;
1898 __f_ = (__base*)&__buf_;
1900 else
1901 _VSTD::swap(__f_, __f.__f_);
1904 template<class _Rp>
1906 function<_Rp()>::operator()() const
1908 if (__f_ == 0)
1909 __throw_bad_function_call();
1910 return (*__f_)();
1913 #ifndef _LIBCPP_NO_RTTI
1915 template<class _Rp>
1916 const std::type_info&
1917 function<_Rp()>::target_type() const
1919 if (__f_ == 0)
1920 return typeid(void);
1921 return __f_->target_type();
1924 template<class _Rp>
1925 template <typename _Tp>
1926 _Tp*
1927 function<_Rp()>::target()
1929 if (__f_ == 0)
1930 return (_Tp*)0;
1931 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
1934 template<class _Rp>
1935 template <typename _Tp>
1936 const _Tp*
1937 function<_Rp()>::target() const
1939 if (__f_ == 0)
1940 return (const _Tp*)0;
1941 return (const _Tp*)__f_->target(typeid(_Tp));
1944 #endif // _LIBCPP_NO_RTTI
1946 template<class _Rp, class _A0>
1947 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0)>
1948 : public unary_function<_A0, _Rp>
1950 typedef __function::__base<_Rp(_A0)> __base;
1951 aligned_storage<3*sizeof(void*)>::type __buf_;
1952 __base* __f_;
1954 public:
1955 typedef _Rp result_type;
1957 // 20.7.16.2.1, construct/copy/destroy:
1958 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1959 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1960 function(const function&);
1961 template<class _Fp>
1962 function(_Fp,
1963 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1965 template<class _Alloc>
1966 _LIBCPP_INLINE_VISIBILITY
1967 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1968 template<class _Alloc>
1969 _LIBCPP_INLINE_VISIBILITY
1970 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1971 template<class _Alloc>
1972 function(allocator_arg_t, const _Alloc&, const function&);
1973 template<class _Fp, class _Alloc>
1974 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1975 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1977 function& operator=(const function&);
1978 function& operator=(nullptr_t);
1979 template<class _Fp>
1980 typename enable_if
1982 !is_integral<_Fp>::value,
1983 function&
1984 >::type
1985 operator=(_Fp);
1987 ~function();
1989 // 20.7.16.2.2, function modifiers:
1990 void swap(function&);
1991 template<class _Fp, class _Alloc>
1992 _LIBCPP_INLINE_VISIBILITY
1993 void assign(_Fp __f, const _Alloc& __a)
1994 {function(allocator_arg, __a, __f).swap(*this);}
1996 // 20.7.16.2.3, function capacity:
1997 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
1999 template<class _R2, class _B0>
2000 bool operator==(const function<_R2(_B0)>&) const = delete;
2001 template<class _R2, class _B0>
2002 bool operator!=(const function<_R2(_B0)>&) const = delete;
2004 // 20.7.16.2.4, function invocation:
2005 _Rp operator()(_A0) const;
2007 #ifndef _LIBCPP_NO_RTTI
2008 // 20.7.16.2.5, function target access:
2009 const std::type_info& target_type() const;
2010 template <typename _Tp> _Tp* target();
2011 template <typename _Tp> const _Tp* target() const;
2012 #endif // _LIBCPP_NO_RTTI
2015 template<class _Rp, class _A0>
2016 function<_Rp(_A0)>::function(const function& __f)
2018 if (__f.__f_ == 0)
2019 __f_ = 0;
2020 else if (__f.__f_ == (const __base*)&__f.__buf_)
2022 __f_ = (__base*)&__buf_;
2023 __f.__f_->__clone(__f_);
2025 else
2026 __f_ = __f.__f_->__clone();
2029 template<class _Rp, class _A0>
2030 template<class _Alloc>
2031 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
2033 if (__f.__f_ == 0)
2034 __f_ = 0;
2035 else if (__f.__f_ == (const __base*)&__f.__buf_)
2037 __f_ = (__base*)&__buf_;
2038 __f.__f_->__clone(__f_);
2040 else
2041 __f_ = __f.__f_->__clone();
2044 template<class _Rp, class _A0>
2045 template <class _Fp>
2046 function<_Rp(_A0)>::function(_Fp __f,
2047 typename enable_if<!is_integral<_Fp>::value>::type*)
2048 : __f_(0)
2050 if (__function::__not_null(__f))
2052 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
2053 if (sizeof(_FF) <= sizeof(__buf_))
2055 __f_ = (__base*)&__buf_;
2056 ::new ((void*)__f_) _FF(__f);
2058 else
2060 typedef allocator<_FF> _Ap;
2061 _Ap __a;
2062 typedef __allocator_destructor<_Ap> _Dp;
2063 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2064 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2065 __f_ = __hold.release();
2070 template<class _Rp, class _A0>
2071 template <class _Fp, class _Alloc>
2072 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2073 typename enable_if<!is_integral<_Fp>::value>::type*)
2074 : __f_(0)
2076 typedef allocator_traits<_Alloc> __alloc_traits;
2077 if (__function::__not_null(__f))
2079 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
2080 if (sizeof(_FF) <= sizeof(__buf_))
2082 __f_ = (__base*)&__buf_;
2083 ::new ((void*)__f_) _FF(__f, __a0);
2085 else
2087 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2088 _Ap __a(__a0);
2089 typedef __allocator_destructor<_Ap> _Dp;
2090 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2091 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2092 __f_ = __hold.release();
2097 template<class _Rp, class _A0>
2098 function<_Rp(_A0)>&
2099 function<_Rp(_A0)>::operator=(const function& __f)
2101 if (__f)
2102 function(__f).swap(*this);
2103 else
2104 *this = nullptr;
2105 return *this;
2108 template<class _Rp, class _A0>
2109 function<_Rp(_A0)>&
2110 function<_Rp(_A0)>::operator=(nullptr_t)
2112 __base* __t = __f_;
2113 __f_ = 0;
2114 if (__t == (__base*)&__buf_)
2115 __t->destroy();
2116 else if (__t)
2117 __t->destroy_deallocate();
2118 return *this;
2121 template<class _Rp, class _A0>
2122 template <class _Fp>
2123 typename enable_if
2125 !is_integral<_Fp>::value,
2126 function<_Rp(_A0)>&
2127 >::type
2128 function<_Rp(_A0)>::operator=(_Fp __f)
2130 function(_VSTD::move(__f)).swap(*this);
2131 return *this;
2134 template<class _Rp, class _A0>
2135 function<_Rp(_A0)>::~function()
2137 if (__f_ == (__base*)&__buf_)
2138 __f_->destroy();
2139 else if (__f_)
2140 __f_->destroy_deallocate();
2143 template<class _Rp, class _A0>
2144 void
2145 function<_Rp(_A0)>::swap(function& __f)
2147 if (_VSTD::addressof(__f) == this)
2148 return;
2149 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2151 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2152 __base* __t = (__base*)&__tempbuf;
2153 __f_->__clone(__t);
2154 __f_->destroy();
2155 __f_ = 0;
2156 __f.__f_->__clone((__base*)&__buf_);
2157 __f.__f_->destroy();
2158 __f.__f_ = 0;
2159 __f_ = (__base*)&__buf_;
2160 __t->__clone((__base*)&__f.__buf_);
2161 __t->destroy();
2162 __f.__f_ = (__base*)&__f.__buf_;
2164 else if (__f_ == (__base*)&__buf_)
2166 __f_->__clone((__base*)&__f.__buf_);
2167 __f_->destroy();
2168 __f_ = __f.__f_;
2169 __f.__f_ = (__base*)&__f.__buf_;
2171 else if (__f.__f_ == (__base*)&__f.__buf_)
2173 __f.__f_->__clone((__base*)&__buf_);
2174 __f.__f_->destroy();
2175 __f.__f_ = __f_;
2176 __f_ = (__base*)&__buf_;
2178 else
2179 _VSTD::swap(__f_, __f.__f_);
2182 template<class _Rp, class _A0>
2184 function<_Rp(_A0)>::operator()(_A0 __a0) const
2186 if (__f_ == 0)
2187 __throw_bad_function_call();
2188 return (*__f_)(__a0);
2191 #ifndef _LIBCPP_NO_RTTI
2193 template<class _Rp, class _A0>
2194 const std::type_info&
2195 function<_Rp(_A0)>::target_type() const
2197 if (__f_ == 0)
2198 return typeid(void);
2199 return __f_->target_type();
2202 template<class _Rp, class _A0>
2203 template <typename _Tp>
2204 _Tp*
2205 function<_Rp(_A0)>::target()
2207 if (__f_ == 0)
2208 return (_Tp*)0;
2209 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2212 template<class _Rp, class _A0>
2213 template <typename _Tp>
2214 const _Tp*
2215 function<_Rp(_A0)>::target() const
2217 if (__f_ == 0)
2218 return (const _Tp*)0;
2219 return (const _Tp*)__f_->target(typeid(_Tp));
2222 #endif // _LIBCPP_NO_RTTI
2224 template<class _Rp, class _A0, class _A1>
2225 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1)>
2226 : public binary_function<_A0, _A1, _Rp>
2228 typedef __function::__base<_Rp(_A0, _A1)> __base;
2229 aligned_storage<3*sizeof(void*)>::type __buf_;
2230 __base* __f_;
2232 public:
2233 typedef _Rp result_type;
2235 // 20.7.16.2.1, construct/copy/destroy:
2236 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
2237 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
2238 function(const function&);
2239 template<class _Fp>
2240 function(_Fp,
2241 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2243 template<class _Alloc>
2244 _LIBCPP_INLINE_VISIBILITY
2245 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
2246 template<class _Alloc>
2247 _LIBCPP_INLINE_VISIBILITY
2248 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
2249 template<class _Alloc>
2250 function(allocator_arg_t, const _Alloc&, const function&);
2251 template<class _Fp, class _Alloc>
2252 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
2253 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2255 function& operator=(const function&);
2256 function& operator=(nullptr_t);
2257 template<class _Fp>
2258 typename enable_if
2260 !is_integral<_Fp>::value,
2261 function&
2262 >::type
2263 operator=(_Fp);
2265 ~function();
2267 // 20.7.16.2.2, function modifiers:
2268 void swap(function&);
2269 template<class _Fp, class _Alloc>
2270 _LIBCPP_INLINE_VISIBILITY
2271 void assign(_Fp __f, const _Alloc& __a)
2272 {function(allocator_arg, __a, __f).swap(*this);}
2274 // 20.7.16.2.3, function capacity:
2275 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2277 template<class _R2, class _B0, class _B1>
2278 bool operator==(const function<_R2(_B0, _B1)>&) const = delete;
2279 template<class _R2, class _B0, class _B1>
2280 bool operator!=(const function<_R2(_B0, _B1)>&) const = delete;
2282 // 20.7.16.2.4, function invocation:
2283 _Rp operator()(_A0, _A1) const;
2285 #ifndef _LIBCPP_NO_RTTI
2286 // 20.7.16.2.5, function target access:
2287 const std::type_info& target_type() const;
2288 template <typename _Tp> _Tp* target();
2289 template <typename _Tp> const _Tp* target() const;
2290 #endif // _LIBCPP_NO_RTTI
2293 template<class _Rp, class _A0, class _A1>
2294 function<_Rp(_A0, _A1)>::function(const function& __f)
2296 if (__f.__f_ == 0)
2297 __f_ = 0;
2298 else if (__f.__f_ == (const __base*)&__f.__buf_)
2300 __f_ = (__base*)&__buf_;
2301 __f.__f_->__clone(__f_);
2303 else
2304 __f_ = __f.__f_->__clone();
2307 template<class _Rp, class _A0, class _A1>
2308 template<class _Alloc>
2309 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
2311 if (__f.__f_ == 0)
2312 __f_ = 0;
2313 else if (__f.__f_ == (const __base*)&__f.__buf_)
2315 __f_ = (__base*)&__buf_;
2316 __f.__f_->__clone(__f_);
2318 else
2319 __f_ = __f.__f_->__clone();
2322 template<class _Rp, class _A0, class _A1>
2323 template <class _Fp>
2324 function<_Rp(_A0, _A1)>::function(_Fp __f,
2325 typename enable_if<!is_integral<_Fp>::value>::type*)
2326 : __f_(0)
2328 if (__function::__not_null(__f))
2330 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
2331 if (sizeof(_FF) <= sizeof(__buf_))
2333 __f_ = (__base*)&__buf_;
2334 ::new ((void*)__f_) _FF(__f);
2336 else
2338 typedef allocator<_FF> _Ap;
2339 _Ap __a;
2340 typedef __allocator_destructor<_Ap> _Dp;
2341 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2342 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2343 __f_ = __hold.release();
2348 template<class _Rp, class _A0, class _A1>
2349 template <class _Fp, class _Alloc>
2350 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2351 typename enable_if<!is_integral<_Fp>::value>::type*)
2352 : __f_(0)
2354 typedef allocator_traits<_Alloc> __alloc_traits;
2355 if (__function::__not_null(__f))
2357 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
2358 if (sizeof(_FF) <= sizeof(__buf_))
2360 __f_ = (__base*)&__buf_;
2361 ::new ((void*)__f_) _FF(__f, __a0);
2363 else
2365 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2366 _Ap __a(__a0);
2367 typedef __allocator_destructor<_Ap> _Dp;
2368 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2369 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2370 __f_ = __hold.release();
2375 template<class _Rp, class _A0, class _A1>
2376 function<_Rp(_A0, _A1)>&
2377 function<_Rp(_A0, _A1)>::operator=(const function& __f)
2379 if (__f)
2380 function(__f).swap(*this);
2381 else
2382 *this = nullptr;
2383 return *this;
2386 template<class _Rp, class _A0, class _A1>
2387 function<_Rp(_A0, _A1)>&
2388 function<_Rp(_A0, _A1)>::operator=(nullptr_t)
2390 __base* __t = __f_;
2391 __f_ = 0;
2392 if (__t == (__base*)&__buf_)
2393 __t->destroy();
2394 else if (__t)
2395 __t->destroy_deallocate();
2396 return *this;
2399 template<class _Rp, class _A0, class _A1>
2400 template <class _Fp>
2401 typename enable_if
2403 !is_integral<_Fp>::value,
2404 function<_Rp(_A0, _A1)>&
2405 >::type
2406 function<_Rp(_A0, _A1)>::operator=(_Fp __f)
2408 function(_VSTD::move(__f)).swap(*this);
2409 return *this;
2412 template<class _Rp, class _A0, class _A1>
2413 function<_Rp(_A0, _A1)>::~function()
2415 if (__f_ == (__base*)&__buf_)
2416 __f_->destroy();
2417 else if (__f_)
2418 __f_->destroy_deallocate();
2421 template<class _Rp, class _A0, class _A1>
2422 void
2423 function<_Rp(_A0, _A1)>::swap(function& __f)
2425 if (_VSTD::addressof(__f) == this)
2426 return;
2427 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2429 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2430 __base* __t = (__base*)&__tempbuf;
2431 __f_->__clone(__t);
2432 __f_->destroy();
2433 __f_ = 0;
2434 __f.__f_->__clone((__base*)&__buf_);
2435 __f.__f_->destroy();
2436 __f.__f_ = 0;
2437 __f_ = (__base*)&__buf_;
2438 __t->__clone((__base*)&__f.__buf_);
2439 __t->destroy();
2440 __f.__f_ = (__base*)&__f.__buf_;
2442 else if (__f_ == (__base*)&__buf_)
2444 __f_->__clone((__base*)&__f.__buf_);
2445 __f_->destroy();
2446 __f_ = __f.__f_;
2447 __f.__f_ = (__base*)&__f.__buf_;
2449 else if (__f.__f_ == (__base*)&__f.__buf_)
2451 __f.__f_->__clone((__base*)&__buf_);
2452 __f.__f_->destroy();
2453 __f.__f_ = __f_;
2454 __f_ = (__base*)&__buf_;
2456 else
2457 _VSTD::swap(__f_, __f.__f_);
2460 template<class _Rp, class _A0, class _A1>
2462 function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
2464 if (__f_ == 0)
2465 __throw_bad_function_call();
2466 return (*__f_)(__a0, __a1);
2469 #ifndef _LIBCPP_NO_RTTI
2471 template<class _Rp, class _A0, class _A1>
2472 const std::type_info&
2473 function<_Rp(_A0, _A1)>::target_type() const
2475 if (__f_ == 0)
2476 return typeid(void);
2477 return __f_->target_type();
2480 template<class _Rp, class _A0, class _A1>
2481 template <typename _Tp>
2482 _Tp*
2483 function<_Rp(_A0, _A1)>::target()
2485 if (__f_ == 0)
2486 return (_Tp*)0;
2487 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2490 template<class _Rp, class _A0, class _A1>
2491 template <typename _Tp>
2492 const _Tp*
2493 function<_Rp(_A0, _A1)>::target() const
2495 if (__f_ == 0)
2496 return (const _Tp*)0;
2497 return (const _Tp*)__f_->target(typeid(_Tp));
2500 #endif // _LIBCPP_NO_RTTI
2502 template<class _Rp, class _A0, class _A1, class _A2>
2503 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1, _A2)>
2505 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
2506 aligned_storage<3*sizeof(void*)>::type __buf_;
2507 __base* __f_;
2509 public:
2510 typedef _Rp result_type;
2512 // 20.7.16.2.1, construct/copy/destroy:
2513 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
2514 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
2515 function(const function&);
2516 template<class _Fp>
2517 function(_Fp,
2518 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2520 template<class _Alloc>
2521 _LIBCPP_INLINE_VISIBILITY
2522 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
2523 template<class _Alloc>
2524 _LIBCPP_INLINE_VISIBILITY
2525 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
2526 template<class _Alloc>
2527 function(allocator_arg_t, const _Alloc&, const function&);
2528 template<class _Fp, class _Alloc>
2529 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
2530 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
2532 function& operator=(const function&);
2533 function& operator=(nullptr_t);
2534 template<class _Fp>
2535 typename enable_if
2537 !is_integral<_Fp>::value,
2538 function&
2539 >::type
2540 operator=(_Fp);
2542 ~function();
2544 // 20.7.16.2.2, function modifiers:
2545 void swap(function&);
2546 template<class _Fp, class _Alloc>
2547 _LIBCPP_INLINE_VISIBILITY
2548 void assign(_Fp __f, const _Alloc& __a)
2549 {function(allocator_arg, __a, __f).swap(*this);}
2551 // 20.7.16.2.3, function capacity:
2552 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
2554 template<class _R2, class _B0, class _B1, class _B2>
2555 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const = delete;
2556 template<class _R2, class _B0, class _B1, class _B2>
2557 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const = delete;
2559 // 20.7.16.2.4, function invocation:
2560 _Rp operator()(_A0, _A1, _A2) const;
2562 #ifndef _LIBCPP_NO_RTTI
2563 // 20.7.16.2.5, function target access:
2564 const std::type_info& target_type() const;
2565 template <typename _Tp> _Tp* target();
2566 template <typename _Tp> const _Tp* target() const;
2567 #endif // _LIBCPP_NO_RTTI
2570 template<class _Rp, class _A0, class _A1, class _A2>
2571 function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
2573 if (__f.__f_ == 0)
2574 __f_ = 0;
2575 else if (__f.__f_ == (const __base*)&__f.__buf_)
2577 __f_ = (__base*)&__buf_;
2578 __f.__f_->__clone(__f_);
2580 else
2581 __f_ = __f.__f_->__clone();
2584 template<class _Rp, class _A0, class _A1, class _A2>
2585 template<class _Alloc>
2586 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
2587 const function& __f)
2589 if (__f.__f_ == 0)
2590 __f_ = 0;
2591 else if (__f.__f_ == (const __base*)&__f.__buf_)
2593 __f_ = (__base*)&__buf_;
2594 __f.__f_->__clone(__f_);
2596 else
2597 __f_ = __f.__f_->__clone();
2600 template<class _Rp, class _A0, class _A1, class _A2>
2601 template <class _Fp>
2602 function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
2603 typename enable_if<!is_integral<_Fp>::value>::type*)
2604 : __f_(0)
2606 if (__function::__not_null(__f))
2608 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
2609 if (sizeof(_FF) <= sizeof(__buf_))
2611 __f_ = (__base*)&__buf_;
2612 ::new ((void*)__f_) _FF(__f);
2614 else
2616 typedef allocator<_FF> _Ap;
2617 _Ap __a;
2618 typedef __allocator_destructor<_Ap> _Dp;
2619 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2620 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
2621 __f_ = __hold.release();
2626 template<class _Rp, class _A0, class _A1, class _A2>
2627 template <class _Fp, class _Alloc>
2628 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
2629 typename enable_if<!is_integral<_Fp>::value>::type*)
2630 : __f_(0)
2632 typedef allocator_traits<_Alloc> __alloc_traits;
2633 if (__function::__not_null(__f))
2635 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
2636 if (sizeof(_FF) <= sizeof(__buf_))
2638 __f_ = (__base*)&__buf_;
2639 ::new ((void*)__f_) _FF(__f, __a0);
2641 else
2643 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
2644 _Ap __a(__a0);
2645 typedef __allocator_destructor<_Ap> _Dp;
2646 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
2647 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
2648 __f_ = __hold.release();
2653 template<class _Rp, class _A0, class _A1, class _A2>
2654 function<_Rp(_A0, _A1, _A2)>&
2655 function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
2657 if (__f)
2658 function(__f).swap(*this);
2659 else
2660 *this = nullptr;
2661 return *this;
2664 template<class _Rp, class _A0, class _A1, class _A2>
2665 function<_Rp(_A0, _A1, _A2)>&
2666 function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
2668 __base* __t = __f_;
2669 __f_ = 0;
2670 if (__t == (__base*)&__buf_)
2671 __t->destroy();
2672 else if (__t)
2673 __t->destroy_deallocate();
2674 return *this;
2677 template<class _Rp, class _A0, class _A1, class _A2>
2678 template <class _Fp>
2679 typename enable_if
2681 !is_integral<_Fp>::value,
2682 function<_Rp(_A0, _A1, _A2)>&
2683 >::type
2684 function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
2686 function(_VSTD::move(__f)).swap(*this);
2687 return *this;
2690 template<class _Rp, class _A0, class _A1, class _A2>
2691 function<_Rp(_A0, _A1, _A2)>::~function()
2693 if (__f_ == (__base*)&__buf_)
2694 __f_->destroy();
2695 else if (__f_)
2696 __f_->destroy_deallocate();
2699 template<class _Rp, class _A0, class _A1, class _A2>
2700 void
2701 function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
2703 if (_VSTD::addressof(__f) == this)
2704 return;
2705 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
2707 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
2708 __base* __t = (__base*)&__tempbuf;
2709 __f_->__clone(__t);
2710 __f_->destroy();
2711 __f_ = 0;
2712 __f.__f_->__clone((__base*)&__buf_);
2713 __f.__f_->destroy();
2714 __f.__f_ = 0;
2715 __f_ = (__base*)&__buf_;
2716 __t->__clone((__base*)&__f.__buf_);
2717 __t->destroy();
2718 __f.__f_ = (__base*)&__f.__buf_;
2720 else if (__f_ == (__base*)&__buf_)
2722 __f_->__clone((__base*)&__f.__buf_);
2723 __f_->destroy();
2724 __f_ = __f.__f_;
2725 __f.__f_ = (__base*)&__f.__buf_;
2727 else if (__f.__f_ == (__base*)&__f.__buf_)
2729 __f.__f_->__clone((__base*)&__buf_);
2730 __f.__f_->destroy();
2731 __f.__f_ = __f_;
2732 __f_ = (__base*)&__buf_;
2734 else
2735 _VSTD::swap(__f_, __f.__f_);
2738 template<class _Rp, class _A0, class _A1, class _A2>
2740 function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
2742 if (__f_ == 0)
2743 __throw_bad_function_call();
2744 return (*__f_)(__a0, __a1, __a2);
2747 #ifndef _LIBCPP_NO_RTTI
2749 template<class _Rp, class _A0, class _A1, class _A2>
2750 const std::type_info&
2751 function<_Rp(_A0, _A1, _A2)>::target_type() const
2753 if (__f_ == 0)
2754 return typeid(void);
2755 return __f_->target_type();
2758 template<class _Rp, class _A0, class _A1, class _A2>
2759 template <typename _Tp>
2760 _Tp*
2761 function<_Rp(_A0, _A1, _A2)>::target()
2763 if (__f_ == 0)
2764 return (_Tp*)0;
2765 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
2768 template<class _Rp, class _A0, class _A1, class _A2>
2769 template <typename _Tp>
2770 const _Tp*
2771 function<_Rp(_A0, _A1, _A2)>::target() const
2773 if (__f_ == 0)
2774 return (const _Tp*)0;
2775 return (const _Tp*)__f_->target(typeid(_Tp));
2778 #endif // _LIBCPP_NO_RTTI
2780 template <class _Fp>
2781 inline _LIBCPP_INLINE_VISIBILITY
2782 bool
2783 operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
2785 template <class _Fp>
2786 inline _LIBCPP_INLINE_VISIBILITY
2787 bool
2788 operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
2790 template <class _Fp>
2791 inline _LIBCPP_INLINE_VISIBILITY
2792 bool
2793 operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
2795 template <class _Fp>
2796 inline _LIBCPP_INLINE_VISIBILITY
2797 bool
2798 operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
2800 template <class _Fp>
2801 inline _LIBCPP_INLINE_VISIBILITY
2802 void
2803 swap(function<_Fp>& __x, function<_Fp>& __y)
2804 {return __x.swap(__y);}
2806 #endif
2808 _LIBCPP_END_NAMESPACE_STD
2810 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H