Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __functional / function.h
blob580dcf9aeeab6c278ab88631373dd536733444f6
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 <__assert>
14 #include <__config>
15 #include <__exception/exception.h>
16 #include <__functional/binary_function.h>
17 #include <__functional/invoke.h>
18 #include <__functional/unary_function.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__memory/addressof.h>
21 #include <__memory/allocator.h>
22 #include <__memory/allocator_destructor.h>
23 #include <__memory/allocator_traits.h>
24 #include <__memory/builtin_new_allocator.h>
25 #include <__memory/compressed_pair.h>
26 #include <__memory/unique_ptr.h>
27 #include <__type_traits/aligned_storage.h>
28 #include <__type_traits/decay.h>
29 #include <__type_traits/is_core_convertible.h>
30 #include <__type_traits/is_scalar.h>
31 #include <__type_traits/is_trivially_copy_constructible.h>
32 #include <__type_traits/is_trivially_destructible.h>
33 #include <__type_traits/is_void.h>
34 #include <__type_traits/strip_signature.h>
35 #include <__utility/forward.h>
36 #include <__utility/move.h>
37 #include <__utility/piecewise_construct.h>
38 #include <__utility/swap.h>
39 #include <__verbose_abort>
40 #include <new>
41 #include <tuple>
42 #include <typeinfo>
44 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45 # pragma GCC system_header
46 #endif
48 #ifndef _LIBCPP_CXX03_LANG
50 _LIBCPP_BEGIN_NAMESPACE_STD
52 // bad_function_call
54 _LIBCPP_DIAGNOSTIC_PUSH
55 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
56 class _LIBCPP_EXPORTED_FROM_ABI bad_function_call
57 : public exception
59 public:
60 _LIBCPP_HIDE_FROM_ABI bad_function_call() _NOEXCEPT = default;
61 _LIBCPP_HIDE_FROM_ABI bad_function_call(const bad_function_call&) _NOEXCEPT = default;
62 _LIBCPP_HIDE_FROM_ABI bad_function_call& operator=(const bad_function_call&) _NOEXCEPT = default;
63 // Note that when a key function is not used, every translation unit that uses
64 // bad_function_call will end up containing a weak definition of the vtable and
65 // typeinfo.
66 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
67 ~bad_function_call() _NOEXCEPT override;
68 #else
69 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_function_call() _NOEXCEPT override {}
70 #endif
72 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
73 const char* what() const _NOEXCEPT override;
74 #endif
76 _LIBCPP_DIAGNOSTIC_POP
78 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
79 void __throw_bad_function_call()
81 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
82 throw bad_function_call();
83 #else
84 _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");
85 #endif
88 template<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined
90 namespace __function
93 template<class _Rp>
94 struct __maybe_derive_from_unary_function
98 template<class _Rp, class _A1>
99 struct __maybe_derive_from_unary_function<_Rp(_A1)>
100 : public __unary_function<_A1, _Rp>
104 template<class _Rp>
105 struct __maybe_derive_from_binary_function
109 template<class _Rp, class _A1, class _A2>
110 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
111 : public __binary_function<_A1, _A2, _Rp>
115 template <class _Fp>
116 _LIBCPP_INLINE_VISIBILITY
117 bool __not_null(_Fp const&) { return true; }
119 template <class _Fp>
120 _LIBCPP_INLINE_VISIBILITY
121 bool __not_null(_Fp* __ptr) { return __ptr; }
123 template <class _Ret, class _Class>
124 _LIBCPP_INLINE_VISIBILITY
125 bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
127 template <class _Fp>
128 _LIBCPP_INLINE_VISIBILITY
129 bool __not_null(function<_Fp> const& __f) { return !!__f; }
131 #ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
132 template <class _Rp, class ..._Args>
133 _LIBCPP_INLINE_VISIBILITY
134 bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
135 #endif
137 } // namespace __function
139 namespace __function {
141 // __alloc_func holds a functor and an allocator.
143 template <class _Fp, class _Ap, class _FB> class __alloc_func;
144 template <class _Fp, class _FB>
145 class __default_alloc_func;
147 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
148 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
150 __compressed_pair<_Fp, _Ap> __f_;
152 public:
153 typedef _LIBCPP_NODEBUG _Fp _Target;
154 typedef _LIBCPP_NODEBUG _Ap _Alloc;
156 _LIBCPP_INLINE_VISIBILITY
157 const _Target& __target() const { return __f_.first(); }
159 // WIN32 APIs may define __allocator, so use __get_allocator instead.
160 _LIBCPP_INLINE_VISIBILITY
161 const _Alloc& __get_allocator() const { return __f_.second(); }
163 _LIBCPP_INLINE_VISIBILITY
164 explicit __alloc_func(_Target&& __f)
165 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
166 _VSTD::forward_as_tuple())
170 _LIBCPP_INLINE_VISIBILITY
171 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
172 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
173 _VSTD::forward_as_tuple(__a))
177 _LIBCPP_INLINE_VISIBILITY
178 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
179 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
180 _VSTD::forward_as_tuple(_VSTD::move(__a)))
184 _LIBCPP_INLINE_VISIBILITY
185 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
186 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
187 _VSTD::forward_as_tuple(_VSTD::move(__a)))
191 _LIBCPP_INLINE_VISIBILITY
192 _Rp operator()(_ArgTypes&&... __arg)
194 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
195 return _Invoker::__call(__f_.first(),
196 _VSTD::forward<_ArgTypes>(__arg)...);
199 _LIBCPP_INLINE_VISIBILITY
200 __alloc_func* __clone() const
202 typedef allocator_traits<_Alloc> __alloc_traits;
203 typedef __rebind_alloc<__alloc_traits, __alloc_func> _AA;
204 _AA __a(__f_.second());
205 typedef __allocator_destructor<_AA> _Dp;
206 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
207 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
208 return __hold.release();
211 _LIBCPP_INLINE_VISIBILITY
212 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
214 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__alloc_func* __f) {
215 typedef allocator_traits<_Alloc> __alloc_traits;
216 typedef __rebind_alloc<__alloc_traits, __alloc_func> _FunAlloc;
217 _FunAlloc __a(__f->__get_allocator());
218 __f->destroy();
219 __a.deallocate(__f, 1);
223 template <class _Fp, class _Rp, class... _ArgTypes>
224 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
225 _Fp __f_;
227 public:
228 typedef _LIBCPP_NODEBUG _Fp _Target;
230 _LIBCPP_INLINE_VISIBILITY
231 const _Target& __target() const { return __f_; }
233 _LIBCPP_INLINE_VISIBILITY
234 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
236 _LIBCPP_INLINE_VISIBILITY
237 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
239 _LIBCPP_INLINE_VISIBILITY
240 _Rp operator()(_ArgTypes&&... __arg) {
241 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
242 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
245 _LIBCPP_INLINE_VISIBILITY
246 __default_alloc_func* __clone() const {
247 __builtin_new_allocator::__holder_t __hold =
248 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
249 __default_alloc_func* __res =
250 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
251 (void)__hold.release();
252 return __res;
255 _LIBCPP_INLINE_VISIBILITY
256 void destroy() _NOEXCEPT { __f_.~_Target(); }
258 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__default_alloc_func* __f) {
259 __f->destroy();
260 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
264 // __base provides an abstract interface for copyable functors.
266 template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
268 template<class _Rp, class ..._ArgTypes>
269 class __base<_Rp(_ArgTypes...)>
271 __base(const __base&);
272 __base& operator=(const __base&);
273 public:
274 _LIBCPP_INLINE_VISIBILITY __base() {}
275 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__base() {}
276 virtual __base* __clone() const = 0;
277 virtual void __clone(__base*) const = 0;
278 virtual void destroy() _NOEXCEPT = 0;
279 virtual void destroy_deallocate() _NOEXCEPT = 0;
280 virtual _Rp operator()(_ArgTypes&& ...) = 0;
281 #ifndef _LIBCPP_HAS_NO_RTTI
282 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
283 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
284 #endif // _LIBCPP_HAS_NO_RTTI
287 // __func implements __base for a given functor type.
289 template<class _FD, class _Alloc, class _FB> class __func;
291 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
292 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
293 : public __base<_Rp(_ArgTypes...)>
295 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
296 public:
297 _LIBCPP_INLINE_VISIBILITY
298 explicit __func(_Fp&& __f)
299 : __f_(_VSTD::move(__f)) {}
301 _LIBCPP_INLINE_VISIBILITY
302 explicit __func(const _Fp& __f, const _Alloc& __a)
303 : __f_(__f, __a) {}
305 _LIBCPP_INLINE_VISIBILITY
306 explicit __func(const _Fp& __f, _Alloc&& __a)
307 : __f_(__f, _VSTD::move(__a)) {}
309 _LIBCPP_INLINE_VISIBILITY
310 explicit __func(_Fp&& __f, _Alloc&& __a)
311 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
313 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const;
314 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
315 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT;
316 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT;
317 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg);
318 #ifndef _LIBCPP_HAS_NO_RTTI
319 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(const type_info&) const _NOEXCEPT;
320 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT;
321 #endif // _LIBCPP_HAS_NO_RTTI
324 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
325 __base<_Rp(_ArgTypes...)>*
326 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
328 typedef allocator_traits<_Alloc> __alloc_traits;
329 typedef __rebind_alloc<__alloc_traits, __func> _Ap;
330 _Ap __a(__f_.__get_allocator());
331 typedef __allocator_destructor<_Ap> _Dp;
332 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
333 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
334 return __hold.release();
337 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
338 void
339 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
341 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
344 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
345 void
346 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
348 __f_.destroy();
351 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
352 void
353 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
355 typedef allocator_traits<_Alloc> __alloc_traits;
356 typedef __rebind_alloc<__alloc_traits, __func> _Ap;
357 _Ap __a(__f_.__get_allocator());
358 __f_.destroy();
359 __a.deallocate(this, 1);
362 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
364 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
366 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
369 #ifndef _LIBCPP_HAS_NO_RTTI
371 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
372 const void*
373 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
375 if (__ti == typeid(_Fp))
376 return _VSTD::addressof(__f_.__target());
377 return nullptr;
380 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
381 const std::type_info&
382 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
384 return typeid(_Fp);
387 #endif // _LIBCPP_HAS_NO_RTTI
389 // __value_func creates a value-type from a __func.
391 template <class _Fp> class __value_func;
393 template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
395 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
396 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
397 _LIBCPP_SUPPRESS_DEPRECATED_POP
399 typedef __base<_Rp(_ArgTypes...)> __func;
400 __func* __f_;
402 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI static __func* __as_base(void* __p)
404 return reinterpret_cast<__func*>(__p);
407 public:
408 _LIBCPP_INLINE_VISIBILITY
409 __value_func() _NOEXCEPT : __f_(nullptr) {}
411 template <class _Fp, class _Alloc>
412 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
413 : __f_(nullptr)
415 typedef allocator_traits<_Alloc> __alloc_traits;
416 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
417 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
419 if (__function::__not_null(__f))
421 _FunAlloc __af(__a);
422 if (sizeof(_Fun) <= sizeof(__buf_) &&
423 is_nothrow_copy_constructible<_Fp>::value &&
424 is_nothrow_copy_constructible<_FunAlloc>::value)
426 __f_ =
427 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
429 else
431 typedef __allocator_destructor<_FunAlloc> _Dp;
432 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
433 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
434 __f_ = __hold.release();
439 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __value_func>::value, int> = 0>
440 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
441 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
443 _LIBCPP_INLINE_VISIBILITY
444 __value_func(const __value_func& __f)
446 if (__f.__f_ == nullptr)
447 __f_ = nullptr;
448 else if ((void*)__f.__f_ == &__f.__buf_)
450 __f_ = __as_base(&__buf_);
451 __f.__f_->__clone(__f_);
453 else
454 __f_ = __f.__f_->__clone();
457 _LIBCPP_INLINE_VISIBILITY
458 __value_func(__value_func&& __f) _NOEXCEPT
460 if (__f.__f_ == nullptr)
461 __f_ = nullptr;
462 else if ((void*)__f.__f_ == &__f.__buf_)
464 __f_ = __as_base(&__buf_);
465 __f.__f_->__clone(__f_);
467 else
469 __f_ = __f.__f_;
470 __f.__f_ = nullptr;
474 _LIBCPP_INLINE_VISIBILITY
475 ~__value_func()
477 if ((void*)__f_ == &__buf_)
478 __f_->destroy();
479 else if (__f_)
480 __f_->destroy_deallocate();
483 _LIBCPP_INLINE_VISIBILITY
484 __value_func& operator=(__value_func&& __f)
486 *this = nullptr;
487 if (__f.__f_ == nullptr)
488 __f_ = nullptr;
489 else if ((void*)__f.__f_ == &__f.__buf_)
491 __f_ = __as_base(&__buf_);
492 __f.__f_->__clone(__f_);
494 else
496 __f_ = __f.__f_;
497 __f.__f_ = nullptr;
499 return *this;
502 _LIBCPP_INLINE_VISIBILITY
503 __value_func& operator=(nullptr_t)
505 __func* __f = __f_;
506 __f_ = nullptr;
507 if ((void*)__f == &__buf_)
508 __f->destroy();
509 else if (__f)
510 __f->destroy_deallocate();
511 return *this;
514 _LIBCPP_INLINE_VISIBILITY
515 _Rp operator()(_ArgTypes&&... __args) const
517 if (__f_ == nullptr)
518 __throw_bad_function_call();
519 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
522 _LIBCPP_INLINE_VISIBILITY
523 void swap(__value_func& __f) _NOEXCEPT
525 if (&__f == this)
526 return;
527 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
529 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
530 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
531 _LIBCPP_SUPPRESS_DEPRECATED_POP
532 __func* __t = __as_base(&__tempbuf);
533 __f_->__clone(__t);
534 __f_->destroy();
535 __f_ = nullptr;
536 __f.__f_->__clone(__as_base(&__buf_));
537 __f.__f_->destroy();
538 __f.__f_ = nullptr;
539 __f_ = __as_base(&__buf_);
540 __t->__clone(__as_base(&__f.__buf_));
541 __t->destroy();
542 __f.__f_ = __as_base(&__f.__buf_);
544 else if ((void*)__f_ == &__buf_)
546 __f_->__clone(__as_base(&__f.__buf_));
547 __f_->destroy();
548 __f_ = __f.__f_;
549 __f.__f_ = __as_base(&__f.__buf_);
551 else if ((void*)__f.__f_ == &__f.__buf_)
553 __f.__f_->__clone(__as_base(&__buf_));
554 __f.__f_->destroy();
555 __f.__f_ = __f_;
556 __f_ = __as_base(&__buf_);
558 else
559 _VSTD::swap(__f_, __f.__f_);
562 _LIBCPP_INLINE_VISIBILITY
563 explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
565 #ifndef _LIBCPP_HAS_NO_RTTI
566 _LIBCPP_INLINE_VISIBILITY
567 const std::type_info& target_type() const _NOEXCEPT
569 if (__f_ == nullptr)
570 return typeid(void);
571 return __f_->target_type();
574 template <typename _Tp>
575 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
577 if (__f_ == nullptr)
578 return nullptr;
579 return (const _Tp*)__f_->target(typeid(_Tp));
581 #endif // _LIBCPP_HAS_NO_RTTI
584 // Storage for a functor object, to be used with __policy to manage copy and
585 // destruction.
586 union __policy_storage
588 mutable char __small[sizeof(void*) * 2];
589 void* __large;
592 // True if _Fun can safely be held in __policy_storage.__small.
593 template <typename _Fun>
594 struct __use_small_storage
595 : public integral_constant<
596 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
597 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
598 is_trivially_copy_constructible<_Fun>::value &&
599 is_trivially_destructible<_Fun>::value> {};
601 // Policy contains information about how to copy, destroy, and move the
602 // underlying functor. You can think of it as a vtable of sorts.
603 struct __policy
605 // Used to copy or destroy __large values. null for trivial objects.
606 void* (*const __clone)(const void*);
607 void (*const __destroy)(void*);
609 // True if this is the null policy (no value).
610 const bool __is_null;
612 // The target type. May be null if RTTI is disabled.
613 const std::type_info* const __type_info;
615 // Returns a pointer to a static policy object suitable for the functor
616 // type.
617 template <typename _Fun>
618 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
620 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
623 _LIBCPP_INLINE_VISIBILITY
624 static const __policy* __create_empty()
626 static const _LIBCPP_CONSTEXPR __policy __policy = {nullptr, nullptr,
627 true,
628 #ifndef _LIBCPP_HAS_NO_RTTI
629 &typeid(void)
630 #else
631 nullptr
632 #endif
634 return &__policy;
637 private:
638 template <typename _Fun>
639 _LIBCPP_HIDE_FROM_ABI static void* __large_clone(const void* __s)
641 const _Fun* __f = static_cast<const _Fun*>(__s);
642 return __f->__clone();
645 template <typename _Fun>
646 _LIBCPP_HIDE_FROM_ABI static void __large_destroy(void* __s) {
647 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
650 template <typename _Fun>
651 _LIBCPP_INLINE_VISIBILITY static const __policy*
652 __choose_policy(/* is_small = */ false_type) {
653 static const _LIBCPP_CONSTEXPR __policy __policy = {
654 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
655 #ifndef _LIBCPP_HAS_NO_RTTI
656 &typeid(typename _Fun::_Target)
657 #else
658 nullptr
659 #endif
661 return &__policy;
664 template <typename _Fun>
665 _LIBCPP_INLINE_VISIBILITY static const __policy*
666 __choose_policy(/* is_small = */ true_type)
668 static const _LIBCPP_CONSTEXPR __policy __policy = {
669 nullptr, nullptr, false,
670 #ifndef _LIBCPP_HAS_NO_RTTI
671 &typeid(typename _Fun::_Target)
672 #else
673 nullptr
674 #endif
676 return &__policy;
680 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
681 // faster for types that can be passed in registers.
682 template <typename _Tp>
683 using __fast_forward = __conditional_t<is_scalar<_Tp>::value, _Tp, _Tp&&>;
685 // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
687 template <class _Fp> struct __policy_invoker;
689 template <class _Rp, class... _ArgTypes>
690 struct __policy_invoker<_Rp(_ArgTypes...)>
692 typedef _Rp (*__Call)(const __policy_storage*,
693 __fast_forward<_ArgTypes>...);
695 __Call __call_;
697 // Creates an invoker that throws bad_function_call.
698 _LIBCPP_INLINE_VISIBILITY
699 __policy_invoker() : __call_(&__call_empty) {}
701 // Creates an invoker that calls the given instance of __func.
702 template <typename _Fun>
703 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
705 return __policy_invoker(&__call_impl<_Fun>);
708 private:
709 _LIBCPP_INLINE_VISIBILITY
710 explicit __policy_invoker(__Call __c) : __call_(__c) {}
712 _LIBCPP_HIDE_FROM_ABI static _Rp __call_empty(const __policy_storage*,
713 __fast_forward<_ArgTypes>...)
715 __throw_bad_function_call();
718 template <typename _Fun>
719 _LIBCPP_HIDE_FROM_ABI static _Rp __call_impl(const __policy_storage* __buf,
720 __fast_forward<_ArgTypes>... __args)
722 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
723 ? &__buf->__small
724 : __buf->__large);
725 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
729 // __policy_func uses a __policy and __policy_invoker to create a type-erased,
730 // copyable functor.
732 template <class _Fp> class __policy_func;
734 template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
736 // Inline storage for small objects.
737 __policy_storage __buf_;
739 // Calls the value stored in __buf_. This could technically be part of
740 // policy, but storing it here eliminates a level of indirection inside
741 // operator().
742 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
743 __invoker __invoker_;
745 // The policy that describes how to move / copy / destroy __buf_. Never
746 // null, even if the function is empty.
747 const __policy* __policy_;
749 public:
750 _LIBCPP_INLINE_VISIBILITY
751 __policy_func() : __policy_(__policy::__create_empty()) {}
753 template <class _Fp, class _Alloc>
754 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
755 : __policy_(__policy::__create_empty())
757 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
758 typedef allocator_traits<_Alloc> __alloc_traits;
759 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
761 if (__function::__not_null(__f))
763 __invoker_ = __invoker::template __create<_Fun>();
764 __policy_ = __policy::__create<_Fun>();
766 _FunAlloc __af(__a);
767 if (__use_small_storage<_Fun>())
769 ::new ((void*)&__buf_.__small)
770 _Fun(_VSTD::move(__f), _Alloc(__af));
772 else
774 typedef __allocator_destructor<_FunAlloc> _Dp;
775 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
776 ::new ((void*)__hold.get())
777 _Fun(_VSTD::move(__f), _Alloc(__af));
778 __buf_.__large = __hold.release();
783 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __policy_func>::value, int> = 0>
784 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
785 : __policy_(__policy::__create_empty()) {
786 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
788 if (__function::__not_null(__f)) {
789 __invoker_ = __invoker::template __create<_Fun>();
790 __policy_ = __policy::__create<_Fun>();
791 if (__use_small_storage<_Fun>()) {
792 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
793 } else {
794 __builtin_new_allocator::__holder_t __hold =
795 __builtin_new_allocator::__allocate_type<_Fun>(1);
796 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
797 (void)__hold.release();
802 _LIBCPP_INLINE_VISIBILITY
803 __policy_func(const __policy_func& __f)
804 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
805 __policy_(__f.__policy_)
807 if (__policy_->__clone)
808 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
811 _LIBCPP_INLINE_VISIBILITY
812 __policy_func(__policy_func&& __f)
813 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
814 __policy_(__f.__policy_)
816 if (__policy_->__destroy)
818 __f.__policy_ = __policy::__create_empty();
819 __f.__invoker_ = __invoker();
823 _LIBCPP_INLINE_VISIBILITY
824 ~__policy_func()
826 if (__policy_->__destroy)
827 __policy_->__destroy(__buf_.__large);
830 _LIBCPP_INLINE_VISIBILITY
831 __policy_func& operator=(__policy_func&& __f)
833 *this = nullptr;
834 __buf_ = __f.__buf_;
835 __invoker_ = __f.__invoker_;
836 __policy_ = __f.__policy_;
837 __f.__policy_ = __policy::__create_empty();
838 __f.__invoker_ = __invoker();
839 return *this;
842 _LIBCPP_INLINE_VISIBILITY
843 __policy_func& operator=(nullptr_t)
845 const __policy* __p = __policy_;
846 __policy_ = __policy::__create_empty();
847 __invoker_ = __invoker();
848 if (__p->__destroy)
849 __p->__destroy(__buf_.__large);
850 return *this;
853 _LIBCPP_INLINE_VISIBILITY
854 _Rp operator()(_ArgTypes&&... __args) const
856 return __invoker_.__call_(_VSTD::addressof(__buf_),
857 _VSTD::forward<_ArgTypes>(__args)...);
860 _LIBCPP_INLINE_VISIBILITY
861 void swap(__policy_func& __f)
863 _VSTD::swap(__invoker_, __f.__invoker_);
864 _VSTD::swap(__policy_, __f.__policy_);
865 _VSTD::swap(__buf_, __f.__buf_);
868 _LIBCPP_INLINE_VISIBILITY
869 explicit operator bool() const _NOEXCEPT
871 return !__policy_->__is_null;
874 #ifndef _LIBCPP_HAS_NO_RTTI
875 _LIBCPP_INLINE_VISIBILITY
876 const std::type_info& target_type() const _NOEXCEPT
878 return *__policy_->__type_info;
881 template <typename _Tp>
882 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
884 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
885 return nullptr;
886 if (__policy_->__clone) // Out of line storage.
887 return reinterpret_cast<const _Tp*>(__buf_.__large);
888 else
889 return reinterpret_cast<const _Tp*>(&__buf_.__small);
891 #endif // _LIBCPP_HAS_NO_RTTI
894 #if defined(_LIBCPP_HAS_BLOCKS_RUNTIME)
896 extern "C" void *_Block_copy(const void *);
897 extern "C" void _Block_release(const void *);
899 template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
900 class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
901 : public __base<_Rp(_ArgTypes...)>
903 typedef _Rp1(^__block_type)(_ArgTypes1...);
904 __block_type __f_;
906 public:
907 _LIBCPP_INLINE_VISIBILITY
908 explicit __func(__block_type const& __f)
909 #ifdef _LIBCPP_HAS_OBJC_ARC
910 : __f_(__f)
911 #else
912 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
913 #endif
916 // [TODO] add && to save on a retain
918 _LIBCPP_INLINE_VISIBILITY
919 explicit __func(__block_type __f, const _Alloc& /* unused */)
920 #ifdef _LIBCPP_HAS_OBJC_ARC
921 : __f_(__f)
922 #else
923 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
924 #endif
927 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
928 _LIBCPP_ASSERT_INTERNAL(false,
929 "Block pointers are just pointers, so they should always fit into "
930 "std::function's small buffer optimization. This function should "
931 "never be invoked.");
932 return nullptr;
935 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
936 ::new ((void*)__p) __func(__f_);
939 virtual void destroy() _NOEXCEPT {
940 #ifndef _LIBCPP_HAS_OBJC_ARC
941 if (__f_)
942 _Block_release(__f_);
943 #endif
944 __f_ = 0;
947 virtual void destroy_deallocate() _NOEXCEPT {
948 _LIBCPP_ASSERT_INTERNAL(false,
949 "Block pointers are just pointers, so they should always fit into "
950 "std::function's small buffer optimization. This function should "
951 "never be invoked.");
954 virtual _Rp operator()(_ArgTypes&& ... __arg) {
955 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
958 #ifndef _LIBCPP_HAS_NO_RTTI
959 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
960 if (__ti == typeid(__func::__block_type))
961 return &__f_;
962 return (const void*)nullptr;
965 virtual const std::type_info& target_type() const _NOEXCEPT {
966 return typeid(__func::__block_type);
968 #endif // _LIBCPP_HAS_NO_RTTI
971 #endif // _LIBCPP_HAS_EXTENSION_BLOCKS
973 } // namespace __function
975 template<class _Rp, class ..._ArgTypes>
976 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
977 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
978 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
980 #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
981 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
982 #else
983 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
984 #endif
986 __func __f_;
988 template <class _Fp, bool = _And<
989 _IsNotSame<__remove_cvref_t<_Fp>, function>,
990 __invokable<_Fp, _ArgTypes...>
991 >::value>
992 struct __callable;
993 template <class _Fp>
994 struct __callable<_Fp, true>
996 static const bool value = is_void<_Rp>::value ||
997 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
998 _Rp>::value;
1000 template <class _Fp>
1001 struct __callable<_Fp, false>
1003 static const bool value = false;
1006 template <class _Fp>
1007 using _EnableIfLValueCallable = __enable_if_t<__callable<_Fp&>::value>;
1008 public:
1009 typedef _Rp result_type;
1011 // construct/copy/destroy:
1012 _LIBCPP_INLINE_VISIBILITY
1013 function() _NOEXCEPT { }
1014 _LIBCPP_INLINE_VISIBILITY
1015 _LIBCPP_HIDE_FROM_ABI function(nullptr_t) _NOEXCEPT {}
1016 _LIBCPP_HIDE_FROM_ABI function(const function&);
1017 _LIBCPP_HIDE_FROM_ABI function(function&&) _NOEXCEPT;
1018 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
1019 _LIBCPP_HIDE_FROM_ABI function(_Fp);
1021 #if _LIBCPP_STD_VER <= 14
1022 template<class _Alloc>
1023 _LIBCPP_INLINE_VISIBILITY
1024 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
1025 template<class _Alloc>
1026 _LIBCPP_INLINE_VISIBILITY
1027 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
1028 template<class _Alloc>
1029 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, const function&);
1030 template<class _Alloc>
1031 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, function&&);
1032 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
1033 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc& __a, _Fp __f);
1034 #endif
1036 _LIBCPP_HIDE_FROM_ABI function& operator=(const function&);
1037 _LIBCPP_HIDE_FROM_ABI function& operator=(function&&) _NOEXCEPT;
1038 _LIBCPP_HIDE_FROM_ABI function& operator=(nullptr_t) _NOEXCEPT;
1039 template<class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>>
1040 _LIBCPP_HIDE_FROM_ABI function& operator=(_Fp&&);
1042 _LIBCPP_HIDE_FROM_ABI ~function();
1044 // function modifiers:
1045 _LIBCPP_HIDE_FROM_ABI void swap(function&) _NOEXCEPT;
1047 #if _LIBCPP_STD_VER <= 14
1048 template<class _Fp, class _Alloc>
1049 _LIBCPP_INLINE_VISIBILITY
1050 void assign(_Fp&& __f, const _Alloc& __a)
1051 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
1052 #endif
1054 // function capacity:
1055 _LIBCPP_INLINE_VISIBILITY
1056 explicit operator bool() const _NOEXCEPT {
1057 return static_cast<bool>(__f_);
1060 // deleted overloads close possible hole in the type system
1061 template<class _R2, class... _ArgTypes2>
1062 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
1063 #if _LIBCPP_STD_VER <= 17
1064 template<class _R2, class... _ArgTypes2>
1065 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
1066 #endif
1067 public:
1068 // function invocation:
1069 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1071 #ifndef _LIBCPP_HAS_NO_RTTI
1072 // function target access:
1073 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT;
1074 template <typename _Tp>
1075 _LIBCPP_HIDE_FROM_ABI _Tp* target() _NOEXCEPT;
1076 template <typename _Tp>
1077 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT;
1078 #endif // _LIBCPP_HAS_NO_RTTI
1081 #if _LIBCPP_STD_VER >= 17
1082 template<class _Rp, class ..._Ap>
1083 function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
1085 template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1086 function(_Fp) -> function<_Stripped>;
1087 #endif // _LIBCPP_STD_VER >= 17
1089 template<class _Rp, class ..._ArgTypes>
1090 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
1092 #if _LIBCPP_STD_VER <= 14
1093 template<class _Rp, class ..._ArgTypes>
1094 template <class _Alloc>
1095 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1096 const function& __f) : __f_(__f.__f_) {}
1097 #endif
1099 template <class _Rp, class... _ArgTypes>
1100 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
1101 : __f_(_VSTD::move(__f.__f_)) {}
1103 #if _LIBCPP_STD_VER <= 14
1104 template<class _Rp, class ..._ArgTypes>
1105 template <class _Alloc>
1106 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1107 function&& __f)
1108 : __f_(_VSTD::move(__f.__f_)) {}
1109 #endif
1111 template <class _Rp, class... _ArgTypes>
1112 template <class _Fp, class>
1113 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
1115 #if _LIBCPP_STD_VER <= 14
1116 template <class _Rp, class... _ArgTypes>
1117 template <class _Fp, class _Alloc, class>
1118 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
1119 _Fp __f)
1120 : __f_(_VSTD::move(__f), __a) {}
1121 #endif
1123 template<class _Rp, class ..._ArgTypes>
1124 function<_Rp(_ArgTypes...)>&
1125 function<_Rp(_ArgTypes...)>::operator=(const function& __f)
1127 function(__f).swap(*this);
1128 return *this;
1131 template<class _Rp, class ..._ArgTypes>
1132 function<_Rp(_ArgTypes...)>&
1133 function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
1135 __f_ = _VSTD::move(__f.__f_);
1136 return *this;
1139 template<class _Rp, class ..._ArgTypes>
1140 function<_Rp(_ArgTypes...)>&
1141 function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
1143 __f_ = nullptr;
1144 return *this;
1147 template<class _Rp, class ..._ArgTypes>
1148 template <class _Fp, class>
1149 function<_Rp(_ArgTypes...)>&
1150 function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
1152 function(_VSTD::forward<_Fp>(__f)).swap(*this);
1153 return *this;
1156 template<class _Rp, class ..._ArgTypes>
1157 function<_Rp(_ArgTypes...)>::~function() {}
1159 template<class _Rp, class ..._ArgTypes>
1160 void
1161 function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
1163 __f_.swap(__f.__f_);
1166 template<class _Rp, class ..._ArgTypes>
1168 function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1170 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
1173 #ifndef _LIBCPP_HAS_NO_RTTI
1175 template<class _Rp, class ..._ArgTypes>
1176 const std::type_info&
1177 function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1179 return __f_.target_type();
1182 template<class _Rp, class ..._ArgTypes>
1183 template <typename _Tp>
1184 _Tp*
1185 function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
1187 return (_Tp*)(__f_.template target<_Tp>());
1190 template<class _Rp, class ..._ArgTypes>
1191 template <typename _Tp>
1192 const _Tp*
1193 function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
1195 return __f_.template target<_Tp>();
1198 #endif // _LIBCPP_HAS_NO_RTTI
1200 template <class _Rp, class... _ArgTypes>
1201 inline _LIBCPP_INLINE_VISIBILITY
1202 bool
1203 operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
1205 #if _LIBCPP_STD_VER <= 17
1207 template <class _Rp, class... _ArgTypes>
1208 inline _LIBCPP_INLINE_VISIBILITY
1209 bool
1210 operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
1212 template <class _Rp, class... _ArgTypes>
1213 inline _LIBCPP_INLINE_VISIBILITY
1214 bool
1215 operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
1217 template <class _Rp, class... _ArgTypes>
1218 inline _LIBCPP_INLINE_VISIBILITY
1219 bool
1220 operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
1222 #endif // _LIBCPP_STD_VER <= 17
1224 template <class _Rp, class... _ArgTypes>
1225 inline _LIBCPP_INLINE_VISIBILITY
1226 void
1227 swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
1228 {return __x.swap(__y);}
1230 _LIBCPP_END_NAMESPACE_STD
1232 #endif // _LIBCPP_CXX03_LANG
1234 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H