[rtsan] Remove mkfifoat interceptor (#116997)
[llvm-project.git] / libcxx / include / __functional / function.h
blob543be05f10f3305da86ee2f1780235bebf1e1844
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 <__cstddef/nullptr_t.h>
16 #include <__exception/exception.h>
17 #include <__functional/binary_function.h>
18 #include <__functional/invoke.h>
19 #include <__functional/unary_function.h>
20 #include <__iterator/iterator_traits.h>
21 #include <__memory/addressof.h>
22 #include <__memory/allocator.h>
23 #include <__memory/allocator_destructor.h>
24 #include <__memory/allocator_traits.h>
25 #include <__memory/builtin_new_allocator.h>
26 #include <__memory/compressed_pair.h>
27 #include <__memory/unique_ptr.h>
28 #include <__type_traits/aligned_storage.h>
29 #include <__type_traits/decay.h>
30 #include <__type_traits/is_core_convertible.h>
31 #include <__type_traits/is_scalar.h>
32 #include <__type_traits/is_trivially_constructible.h>
33 #include <__type_traits/is_trivially_destructible.h>
34 #include <__type_traits/is_void.h>
35 #include <__type_traits/strip_signature.h>
36 #include <__utility/forward.h>
37 #include <__utility/move.h>
38 #include <__utility/piecewise_construct.h>
39 #include <__utility/swap.h>
40 #include <__verbose_abort>
41 #include <new>
42 #include <tuple>
43 #include <typeinfo>
45 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
46 # pragma GCC system_header
47 #endif
49 _LIBCPP_PUSH_MACROS
50 #include <__undef_macros>
52 #ifndef _LIBCPP_CXX03_LANG
54 _LIBCPP_BEGIN_NAMESPACE_STD
56 // bad_function_call
58 _LIBCPP_DIAGNOSTIC_PUSH
59 # if !_LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
60 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
61 # endif
62 class _LIBCPP_EXPORTED_FROM_ABI bad_function_call : public exception {
63 public:
64 _LIBCPP_HIDE_FROM_ABI bad_function_call() _NOEXCEPT = default;
65 _LIBCPP_HIDE_FROM_ABI bad_function_call(const bad_function_call&) _NOEXCEPT = default;
66 _LIBCPP_HIDE_FROM_ABI bad_function_call& operator=(const bad_function_call&) _NOEXCEPT = default;
67 // Note that when a key function is not used, every translation unit that uses
68 // bad_function_call will end up containing a weak definition of the vtable and
69 // typeinfo.
70 # if _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
71 ~bad_function_call() _NOEXCEPT override;
72 # else
73 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_function_call() _NOEXCEPT override {}
74 # endif
76 # ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
77 const char* what() const _NOEXCEPT override;
78 # endif
80 _LIBCPP_DIAGNOSTIC_POP
82 [[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_function_call() {
83 # if _LIBCPP_HAS_EXCEPTIONS
84 throw bad_function_call();
85 # else
86 _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");
87 # endif
90 template <class _Fp>
91 class _LIBCPP_TEMPLATE_VIS function; // undefined
93 namespace __function {
95 template <class _Rp>
96 struct __maybe_derive_from_unary_function {};
98 template <class _Rp, class _A1>
99 struct __maybe_derive_from_unary_function<_Rp(_A1)> : public __unary_function<_A1, _Rp> {};
101 template <class _Rp>
102 struct __maybe_derive_from_binary_function {};
104 template <class _Rp, class _A1, class _A2>
105 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)> : public __binary_function<_A1, _A2, _Rp> {};
107 template <class _Fp>
108 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Fp const&) {
109 return true;
112 template <class _Fp>
113 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Fp* __ptr) {
114 return __ptr;
117 template <class _Ret, class _Class>
118 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Ret _Class::*__ptr) {
119 return __ptr;
122 template <class _Fp>
123 _LIBCPP_HIDE_FROM_ABI bool __not_null(function<_Fp> const& __f) {
124 return !!__f;
127 # if _LIBCPP_HAS_EXTENSION_BLOCKS
128 template <class _Rp, class... _Args>
129 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Rp (^__p)(_Args...)) {
130 return __p;
132 # endif
134 } // namespace __function
136 namespace __function {
138 // __alloc_func holds a functor and an allocator.
140 template <class _Fp, class _Ap, class _FB>
141 class __alloc_func;
142 template <class _Fp, class _FB>
143 class __default_alloc_func;
145 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
146 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)> {
147 _LIBCPP_COMPRESSED_PAIR(_Fp, __func_, _Ap, __alloc_);
149 public:
150 typedef _LIBCPP_NODEBUG _Fp _Target;
151 typedef _LIBCPP_NODEBUG _Ap _Alloc;
153 _LIBCPP_HIDE_FROM_ABI const _Target& __target() const { return __func_; }
155 // WIN32 APIs may define __allocator, so use __get_allocator instead.
156 _LIBCPP_HIDE_FROM_ABI const _Alloc& __get_allocator() const { return __alloc_; }
158 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(_Target&& __f) : __func_(std::move(__f)), __alloc_() {}
160 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(const _Target& __f, const _Alloc& __a) : __func_(__f), __alloc_(__a) {}
162 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(const _Target& __f, _Alloc&& __a)
163 : __func_(__f), __alloc_(std::move(__a)) {}
165 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(_Target&& __f, _Alloc&& __a)
166 : __func_(std::move(__f)), __alloc_(std::move(__a)) {}
168 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __arg) {
169 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
170 return _Invoker::__call(__func_, std::forward<_ArgTypes>(__arg)...);
173 _LIBCPP_HIDE_FROM_ABI __alloc_func* __clone() const {
174 typedef allocator_traits<_Alloc> __alloc_traits;
175 typedef __rebind_alloc<__alloc_traits, __alloc_func> _AA;
176 _AA __a(__alloc_);
177 typedef __allocator_destructor<_AA> _Dp;
178 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
179 ::new ((void*)__hold.get()) __alloc_func(__func_, _Alloc(__a));
180 return __hold.release();
183 _LIBCPP_HIDE_FROM_ABI void destroy() _NOEXCEPT {
184 __func_.~_Fp();
185 __alloc_.~_Alloc();
188 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__alloc_func* __f) {
189 typedef allocator_traits<_Alloc> __alloc_traits;
190 typedef __rebind_alloc<__alloc_traits, __alloc_func> _FunAlloc;
191 _FunAlloc __a(__f->__get_allocator());
192 __f->destroy();
193 __a.deallocate(__f, 1);
197 template <class _Fp, class _Rp, class... _ArgTypes>
198 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
199 _Fp __f_;
201 public:
202 typedef _LIBCPP_NODEBUG _Fp _Target;
204 _LIBCPP_HIDE_FROM_ABI const _Target& __target() const { return __f_; }
206 _LIBCPP_HIDE_FROM_ABI explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}
208 _LIBCPP_HIDE_FROM_ABI explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
210 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __arg) {
211 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
212 return _Invoker::__call(__f_, std::forward<_ArgTypes>(__arg)...);
215 _LIBCPP_HIDE_FROM_ABI __default_alloc_func* __clone() const {
216 __builtin_new_allocator::__holder_t __hold = __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
217 __default_alloc_func* __res = ::new ((void*)__hold.get()) __default_alloc_func(__f_);
218 (void)__hold.release();
219 return __res;
222 _LIBCPP_HIDE_FROM_ABI void destroy() _NOEXCEPT { __f_.~_Target(); }
224 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__default_alloc_func* __f) {
225 __f->destroy();
226 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
230 // __base provides an abstract interface for copyable functors.
232 template <class _Fp>
233 class _LIBCPP_TEMPLATE_VIS __base;
235 template <class _Rp, class... _ArgTypes>
236 class __base<_Rp(_ArgTypes...)> {
237 public:
238 __base(const __base&) = delete;
239 __base& operator=(const __base&) = delete;
241 _LIBCPP_HIDE_FROM_ABI __base() {}
242 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__base() {}
243 virtual __base* __clone() const = 0;
244 virtual void __clone(__base*) const = 0;
245 virtual void destroy() _NOEXCEPT = 0;
246 virtual void destroy_deallocate() _NOEXCEPT = 0;
247 virtual _Rp operator()(_ArgTypes&&...) = 0;
248 # if _LIBCPP_HAS_RTTI
249 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
250 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
251 # endif // _LIBCPP_HAS_RTTI
254 // __func implements __base for a given functor type.
256 template <class _FD, class _Alloc, class _FB>
257 class __func;
259 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
260 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {
261 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
263 public:
264 _LIBCPP_HIDE_FROM_ABI explicit __func(_Fp&& __f) : __f_(std::move(__f)) {}
266 _LIBCPP_HIDE_FROM_ABI explicit __func(const _Fp& __f, const _Alloc& __a) : __f_(__f, __a) {}
268 _LIBCPP_HIDE_FROM_ABI explicit __func(const _Fp& __f, _Alloc&& __a) : __f_(__f, std::move(__a)) {}
270 _LIBCPP_HIDE_FROM_ABI explicit __func(_Fp&& __f, _Alloc&& __a) : __f_(std::move(__f), std::move(__a)) {}
272 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const;
273 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
274 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT;
275 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT;
276 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg);
277 # if _LIBCPP_HAS_RTTI
278 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(const type_info&) const _NOEXCEPT;
279 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT;
280 # endif // _LIBCPP_HAS_RTTI
283 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
284 __base<_Rp(_ArgTypes...)>* __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const {
285 typedef allocator_traits<_Alloc> __alloc_traits;
286 typedef __rebind_alloc<__alloc_traits, __func> _Ap;
287 _Ap __a(__f_.__get_allocator());
288 typedef __allocator_destructor<_Ap> _Dp;
289 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
290 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
291 return __hold.release();
294 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
295 void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const {
296 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
299 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
300 void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT {
301 __f_.destroy();
304 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
305 void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT {
306 typedef allocator_traits<_Alloc> __alloc_traits;
307 typedef __rebind_alloc<__alloc_traits, __func> _Ap;
308 _Ap __a(__f_.__get_allocator());
309 __f_.destroy();
310 __a.deallocate(this, 1);
313 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
314 _Rp __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
315 return __f_(std::forward<_ArgTypes>(__arg)...);
318 # if _LIBCPP_HAS_RTTI
320 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
321 const void* __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT {
322 if (__ti == typeid(_Fp))
323 return std::addressof(__f_.__target());
324 return nullptr;
327 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
328 const std::type_info& __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT {
329 return typeid(_Fp);
332 # endif // _LIBCPP_HAS_RTTI
334 // __value_func creates a value-type from a __func.
336 template <class _Fp>
337 class __value_func;
339 template <class _Rp, class... _ArgTypes>
340 class __value_func<_Rp(_ArgTypes...)> {
341 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
342 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
343 _LIBCPP_SUPPRESS_DEPRECATED_POP
345 typedef __base<_Rp(_ArgTypes...)> __func;
346 __func* __f_;
348 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI static __func* __as_base(void* __p) { return reinterpret_cast<__func*>(__p); }
350 public:
351 _LIBCPP_HIDE_FROM_ABI __value_func() _NOEXCEPT : __f_(nullptr) {}
353 template <class _Fp, class _Alloc>
354 _LIBCPP_HIDE_FROM_ABI __value_func(_Fp&& __f, const _Alloc& __a) : __f_(nullptr) {
355 typedef allocator_traits<_Alloc> __alloc_traits;
356 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
357 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
359 if (__function::__not_null(__f)) {
360 _FunAlloc __af(__a);
361 if (sizeof(_Fun) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value &&
362 is_nothrow_copy_constructible<_FunAlloc>::value) {
363 __f_ = ::new ((void*)&__buf_) _Fun(std::move(__f), _Alloc(__af));
364 } else {
365 typedef __allocator_destructor<_FunAlloc> _Dp;
366 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
367 ::new ((void*)__hold.get()) _Fun(std::move(__f), _Alloc(__a));
368 __f_ = __hold.release();
373 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __value_func>::value, int> = 0>
374 _LIBCPP_HIDE_FROM_ABI explicit __value_func(_Fp&& __f) : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}
376 _LIBCPP_HIDE_FROM_ABI __value_func(const __value_func& __f) {
377 if (__f.__f_ == nullptr)
378 __f_ = nullptr;
379 else if ((void*)__f.__f_ == &__f.__buf_) {
380 __f_ = __as_base(&__buf_);
381 __f.__f_->__clone(__f_);
382 } else
383 __f_ = __f.__f_->__clone();
386 _LIBCPP_HIDE_FROM_ABI __value_func(__value_func&& __f) _NOEXCEPT {
387 if (__f.__f_ == nullptr)
388 __f_ = nullptr;
389 else if ((void*)__f.__f_ == &__f.__buf_) {
390 __f_ = __as_base(&__buf_);
391 __f.__f_->__clone(__f_);
392 } else {
393 __f_ = __f.__f_;
394 __f.__f_ = nullptr;
398 _LIBCPP_HIDE_FROM_ABI ~__value_func() {
399 if ((void*)__f_ == &__buf_)
400 __f_->destroy();
401 else if (__f_)
402 __f_->destroy_deallocate();
405 _LIBCPP_HIDE_FROM_ABI __value_func& operator=(__value_func&& __f) {
406 *this = nullptr;
407 if (__f.__f_ == nullptr)
408 __f_ = nullptr;
409 else if ((void*)__f.__f_ == &__f.__buf_) {
410 __f_ = __as_base(&__buf_);
411 __f.__f_->__clone(__f_);
412 } else {
413 __f_ = __f.__f_;
414 __f.__f_ = nullptr;
416 return *this;
419 _LIBCPP_HIDE_FROM_ABI __value_func& operator=(nullptr_t) {
420 __func* __f = __f_;
421 __f_ = nullptr;
422 if ((void*)__f == &__buf_)
423 __f->destroy();
424 else if (__f)
425 __f->destroy_deallocate();
426 return *this;
429 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {
430 if (__f_ == nullptr)
431 __throw_bad_function_call();
432 return (*__f_)(std::forward<_ArgTypes>(__args)...);
435 _LIBCPP_HIDE_FROM_ABI void swap(__value_func& __f) _NOEXCEPT {
436 if (&__f == this)
437 return;
438 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_) {
439 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
440 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
441 _LIBCPP_SUPPRESS_DEPRECATED_POP
442 __func* __t = __as_base(&__tempbuf);
443 __f_->__clone(__t);
444 __f_->destroy();
445 __f_ = nullptr;
446 __f.__f_->__clone(__as_base(&__buf_));
447 __f.__f_->destroy();
448 __f.__f_ = nullptr;
449 __f_ = __as_base(&__buf_);
450 __t->__clone(__as_base(&__f.__buf_));
451 __t->destroy();
452 __f.__f_ = __as_base(&__f.__buf_);
453 } else if ((void*)__f_ == &__buf_) {
454 __f_->__clone(__as_base(&__f.__buf_));
455 __f_->destroy();
456 __f_ = __f.__f_;
457 __f.__f_ = __as_base(&__f.__buf_);
458 } else if ((void*)__f.__f_ == &__f.__buf_) {
459 __f.__f_->__clone(__as_base(&__buf_));
460 __f.__f_->destroy();
461 __f.__f_ = __f_;
462 __f_ = __as_base(&__buf_);
463 } else
464 std::swap(__f_, __f.__f_);
467 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
469 # if _LIBCPP_HAS_RTTI
470 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT {
471 if (__f_ == nullptr)
472 return typeid(void);
473 return __f_->target_type();
476 template <typename _Tp>
477 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {
478 if (__f_ == nullptr)
479 return nullptr;
480 return (const _Tp*)__f_->target(typeid(_Tp));
482 # endif // _LIBCPP_HAS_RTTI
485 // Storage for a functor object, to be used with __policy to manage copy and
486 // destruction.
487 union __policy_storage {
488 mutable char __small[sizeof(void*) * 2];
489 void* __large;
492 // True if _Fun can safely be held in __policy_storage.__small.
493 template <typename _Fun>
494 struct __use_small_storage
495 : public integral_constant<
496 bool,
497 sizeof(_Fun) <= sizeof(__policy_storage)&& _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
498 is_trivially_copy_constructible<_Fun>::value && is_trivially_destructible<_Fun>::value> {};
500 // Policy contains information about how to copy, destroy, and move the
501 // underlying functor. You can think of it as a vtable of sorts.
502 struct __policy {
503 // Used to copy or destroy __large values. null for trivial objects.
504 void* (*const __clone)(const void*);
505 void (*const __destroy)(void*);
507 // True if this is the null policy (no value).
508 const bool __is_null;
510 // The target type. May be null if RTTI is disabled.
511 const std::type_info* const __type_info;
513 // Returns a pointer to a static policy object suitable for the functor
514 // type.
515 template <typename _Fun>
516 _LIBCPP_HIDE_FROM_ABI static const __policy* __create() {
517 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
520 _LIBCPP_HIDE_FROM_ABI static const __policy* __create_empty() {
521 static constexpr __policy __policy = {
522 nullptr,
523 nullptr,
524 true,
525 # if _LIBCPP_HAS_RTTI
526 &typeid(void)
527 # else
528 nullptr
529 # endif
531 return &__policy;
534 private:
535 template <typename _Fun>
536 _LIBCPP_HIDE_FROM_ABI static void* __large_clone(const void* __s) {
537 const _Fun* __f = static_cast<const _Fun*>(__s);
538 return __f->__clone();
541 template <typename _Fun>
542 _LIBCPP_HIDE_FROM_ABI static void __large_destroy(void* __s) {
543 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
546 template <typename _Fun>
547 _LIBCPP_HIDE_FROM_ABI static const __policy* __choose_policy(/* is_small = */ false_type) {
548 static constexpr __policy __policy = {
549 &__large_clone<_Fun>,
550 &__large_destroy<_Fun>,
551 false,
552 # if _LIBCPP_HAS_RTTI
553 &typeid(typename _Fun::_Target)
554 # else
555 nullptr
556 # endif
558 return &__policy;
561 template <typename _Fun>
562 _LIBCPP_HIDE_FROM_ABI static const __policy* __choose_policy(/* is_small = */ true_type) {
563 static constexpr __policy __policy = {
564 nullptr,
565 nullptr,
566 false,
567 # if _LIBCPP_HAS_RTTI
568 &typeid(typename _Fun::_Target)
569 # else
570 nullptr
571 # endif
573 return &__policy;
577 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
578 // faster for types that can be passed in registers.
579 template <typename _Tp>
580 using __fast_forward = __conditional_t<is_scalar<_Tp>::value, _Tp, _Tp&&>;
582 // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
584 template <class _Fp>
585 struct __policy_invoker;
587 template <class _Rp, class... _ArgTypes>
588 struct __policy_invoker<_Rp(_ArgTypes...)> {
589 typedef _Rp (*__Call)(const __policy_storage*, __fast_forward<_ArgTypes>...);
591 __Call __call_;
593 // Creates an invoker that throws bad_function_call.
594 _LIBCPP_HIDE_FROM_ABI __policy_invoker() : __call_(&__call_empty) {}
596 // Creates an invoker that calls the given instance of __func.
597 template <typename _Fun>
598 _LIBCPP_HIDE_FROM_ABI static __policy_invoker __create() {
599 return __policy_invoker(&__call_impl<_Fun>);
602 private:
603 _LIBCPP_HIDE_FROM_ABI explicit __policy_invoker(__Call __c) : __call_(__c) {}
605 _LIBCPP_HIDE_FROM_ABI static _Rp __call_empty(const __policy_storage*, __fast_forward<_ArgTypes>...) {
606 __throw_bad_function_call();
609 template <typename _Fun>
610 _LIBCPP_HIDE_FROM_ABI static _Rp __call_impl(const __policy_storage* __buf, __fast_forward<_ArgTypes>... __args) {
611 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value ? &__buf->__small : __buf->__large);
612 return (*__f)(std::forward<_ArgTypes>(__args)...);
616 // __policy_func uses a __policy and __policy_invoker to create a type-erased,
617 // copyable functor.
619 template <class _Fp>
620 class __policy_func;
622 template <class _Rp, class... _ArgTypes>
623 class __policy_func<_Rp(_ArgTypes...)> {
624 // Inline storage for small objects.
625 __policy_storage __buf_;
627 // Calls the value stored in __buf_. This could technically be part of
628 // policy, but storing it here eliminates a level of indirection inside
629 // operator().
630 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
631 __invoker __invoker_;
633 // The policy that describes how to move / copy / destroy __buf_. Never
634 // null, even if the function is empty.
635 const __policy* __policy_;
637 public:
638 _LIBCPP_HIDE_FROM_ABI __policy_func() : __policy_(__policy::__create_empty()) {}
640 template <class _Fp, class _Alloc>
641 _LIBCPP_HIDE_FROM_ABI __policy_func(_Fp&& __f, const _Alloc& __a) : __policy_(__policy::__create_empty()) {
642 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
643 typedef allocator_traits<_Alloc> __alloc_traits;
644 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
646 if (__function::__not_null(__f)) {
647 __invoker_ = __invoker::template __create<_Fun>();
648 __policy_ = __policy::__create<_Fun>();
650 _FunAlloc __af(__a);
651 if (__use_small_storage<_Fun>()) {
652 ::new ((void*)&__buf_.__small) _Fun(std::move(__f), _Alloc(__af));
653 } else {
654 typedef __allocator_destructor<_FunAlloc> _Dp;
655 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
656 ::new ((void*)__hold.get()) _Fun(std::move(__f), _Alloc(__af));
657 __buf_.__large = __hold.release();
662 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __policy_func>::value, int> = 0>
663 _LIBCPP_HIDE_FROM_ABI explicit __policy_func(_Fp&& __f) : __policy_(__policy::__create_empty()) {
664 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
666 if (__function::__not_null(__f)) {
667 __invoker_ = __invoker::template __create<_Fun>();
668 __policy_ = __policy::__create<_Fun>();
669 if (__use_small_storage<_Fun>()) {
670 ::new ((void*)&__buf_.__small) _Fun(std::move(__f));
671 } else {
672 __builtin_new_allocator::__holder_t __hold = __builtin_new_allocator::__allocate_type<_Fun>(1);
673 __buf_.__large = ::new ((void*)__hold.get()) _Fun(std::move(__f));
674 (void)__hold.release();
679 _LIBCPP_HIDE_FROM_ABI __policy_func(const __policy_func& __f)
680 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), __policy_(__f.__policy_) {
681 if (__policy_->__clone)
682 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
685 _LIBCPP_HIDE_FROM_ABI __policy_func(__policy_func&& __f)
686 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), __policy_(__f.__policy_) {
687 if (__policy_->__destroy) {
688 __f.__policy_ = __policy::__create_empty();
689 __f.__invoker_ = __invoker();
693 _LIBCPP_HIDE_FROM_ABI ~__policy_func() {
694 if (__policy_->__destroy)
695 __policy_->__destroy(__buf_.__large);
698 _LIBCPP_HIDE_FROM_ABI __policy_func& operator=(__policy_func&& __f) {
699 *this = nullptr;
700 __buf_ = __f.__buf_;
701 __invoker_ = __f.__invoker_;
702 __policy_ = __f.__policy_;
703 __f.__policy_ = __policy::__create_empty();
704 __f.__invoker_ = __invoker();
705 return *this;
708 _LIBCPP_HIDE_FROM_ABI __policy_func& operator=(nullptr_t) {
709 const __policy* __p = __policy_;
710 __policy_ = __policy::__create_empty();
711 __invoker_ = __invoker();
712 if (__p->__destroy)
713 __p->__destroy(__buf_.__large);
714 return *this;
717 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {
718 return __invoker_.__call_(std::addressof(__buf_), std::forward<_ArgTypes>(__args)...);
721 _LIBCPP_HIDE_FROM_ABI void swap(__policy_func& __f) {
722 std::swap(__invoker_, __f.__invoker_);
723 std::swap(__policy_, __f.__policy_);
724 std::swap(__buf_, __f.__buf_);
727 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return !__policy_->__is_null; }
729 # if _LIBCPP_HAS_RTTI
730 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT { return *__policy_->__type_info; }
732 template <typename _Tp>
733 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {
734 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
735 return nullptr;
736 if (__policy_->__clone) // Out of line storage.
737 return reinterpret_cast<const _Tp*>(__buf_.__large);
738 else
739 return reinterpret_cast<const _Tp*>(&__buf_.__small);
741 # endif // _LIBCPP_HAS_RTTI
744 # if _LIBCPP_HAS_BLOCKS_RUNTIME
746 extern "C" void* _Block_copy(const void*);
747 extern "C" void _Block_release(const void*);
749 template <class _Rp1, class... _ArgTypes1, class _Alloc, class _Rp, class... _ArgTypes>
750 class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {
751 typedef _Rp1 (^__block_type)(_ArgTypes1...);
752 __block_type __f_;
754 public:
755 _LIBCPP_HIDE_FROM_ABI explicit __func(__block_type const& __f)
756 # if _LIBCPP_HAS_OBJC_ARC
757 : __f_(__f)
758 # else
759 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
760 # endif
764 // [TODO] add && to save on a retain
766 _LIBCPP_HIDE_FROM_ABI explicit __func(__block_type __f, const _Alloc& /* unused */)
767 # if _LIBCPP_HAS_OBJC_ARC
768 : __f_(__f)
769 # else
770 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
771 # endif
775 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const {
776 _LIBCPP_ASSERT_INTERNAL(
777 false,
778 "Block pointers are just pointers, so they should always fit into "
779 "std::function's small buffer optimization. This function should "
780 "never be invoked.");
781 return nullptr;
784 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
785 ::new ((void*)__p) __func(__f_);
788 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT {
789 # if !_LIBCPP_HAS_OBJC_ARC
790 if (__f_)
791 _Block_release(__f_);
792 # endif
793 __f_ = 0;
796 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT {
797 _LIBCPP_ASSERT_INTERNAL(
798 false,
799 "Block pointers are just pointers, so they should always fit into "
800 "std::function's small buffer optimization. This function should "
801 "never be invoked.");
804 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg) {
805 return std::__invoke(__f_, std::forward<_ArgTypes>(__arg)...);
808 # if _LIBCPP_HAS_RTTI
809 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(type_info const& __ti) const _NOEXCEPT {
810 if (__ti == typeid(__func::__block_type))
811 return &__f_;
812 return (const void*)nullptr;
815 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT {
816 return typeid(__func::__block_type);
818 # endif // _LIBCPP_HAS_RTTI
821 # endif // _LIBCPP_HAS_EXTENSION_BLOCKS
823 } // namespace __function
825 template <class _Rp, class... _ArgTypes>
826 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
827 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
828 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)> {
829 # ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
830 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
831 # else
832 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
833 # endif
835 __func __f_;
837 template <class _Fp,
838 bool = _And< _IsNotSame<__remove_cvref_t<_Fp>, function>, __invokable<_Fp, _ArgTypes...> >::value>
839 struct __callable;
840 template <class _Fp>
841 struct __callable<_Fp, true> {
842 static const bool value =
843 is_void<_Rp>::value || __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type, _Rp>::value;
845 template <class _Fp>
846 struct __callable<_Fp, false> {
847 static const bool value = false;
850 template <class _Fp>
851 using _EnableIfLValueCallable = __enable_if_t<__callable<_Fp&>::value>;
853 public:
854 typedef _Rp result_type;
856 // construct/copy/destroy:
857 _LIBCPP_HIDE_FROM_ABI function() _NOEXCEPT {}
858 _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI function(nullptr_t) _NOEXCEPT {}
859 _LIBCPP_HIDE_FROM_ABI function(const function&);
860 _LIBCPP_HIDE_FROM_ABI function(function&&) _NOEXCEPT;
861 template <class _Fp, class = _EnableIfLValueCallable<_Fp>>
862 _LIBCPP_HIDE_FROM_ABI function(_Fp);
864 # if _LIBCPP_STD_VER <= 14
865 template <class _Alloc>
866 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
867 template <class _Alloc>
868 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
869 template <class _Alloc>
870 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, const function&);
871 template <class _Alloc>
872 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, function&&);
873 template <class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
874 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc& __a, _Fp __f);
875 # endif
877 _LIBCPP_HIDE_FROM_ABI function& operator=(const function&);
878 _LIBCPP_HIDE_FROM_ABI function& operator=(function&&) _NOEXCEPT;
879 _LIBCPP_HIDE_FROM_ABI function& operator=(nullptr_t) _NOEXCEPT;
880 template <class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>>
881 _LIBCPP_HIDE_FROM_ABI function& operator=(_Fp&&);
883 _LIBCPP_HIDE_FROM_ABI ~function();
885 // function modifiers:
886 _LIBCPP_HIDE_FROM_ABI void swap(function&) _NOEXCEPT;
888 # if _LIBCPP_STD_VER <= 14
889 template <class _Fp, class _Alloc>
890 _LIBCPP_HIDE_FROM_ABI void assign(_Fp&& __f, const _Alloc& __a) {
891 function(allocator_arg, __a, std::forward<_Fp>(__f)).swap(*this);
893 # endif
895 // function capacity:
896 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return static_cast<bool>(__f_); }
898 // deleted overloads close possible hole in the type system
899 template <class _R2, class... _ArgTypes2>
900 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
901 # if _LIBCPP_STD_VER <= 17
902 template <class _R2, class... _ArgTypes2>
903 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
904 # endif
906 public:
907 // function invocation:
908 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
910 # if _LIBCPP_HAS_RTTI
911 // function target access:
912 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT;
913 template <typename _Tp>
914 _LIBCPP_HIDE_FROM_ABI _Tp* target() _NOEXCEPT;
915 template <typename _Tp>
916 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT;
917 # endif // _LIBCPP_HAS_RTTI
920 # if _LIBCPP_STD_VER >= 17
921 template <class _Rp, class... _Ap>
922 function(_Rp (*)(_Ap...)) -> function<_Rp(_Ap...)>;
924 template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
925 function(_Fp) -> function<_Stripped>;
926 # endif // _LIBCPP_STD_VER >= 17
928 template <class _Rp, class... _ArgTypes>
929 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
931 # if _LIBCPP_STD_VER <= 14
932 template <class _Rp, class... _ArgTypes>
933 template <class _Alloc>
934 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, const function& __f) : __f_(__f.__f_) {}
935 # endif
937 template <class _Rp, class... _ArgTypes>
938 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT : __f_(std::move(__f.__f_)) {}
940 # if _LIBCPP_STD_VER <= 14
941 template <class _Rp, class... _ArgTypes>
942 template <class _Alloc>
943 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, function&& __f) : __f_(std::move(__f.__f_)) {}
944 # endif
946 template <class _Rp, class... _ArgTypes>
947 template <class _Fp, class>
948 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(std::move(__f)) {}
950 # if _LIBCPP_STD_VER <= 14
951 template <class _Rp, class... _ArgTypes>
952 template <class _Fp, class _Alloc, class>
953 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a, _Fp __f) : __f_(std::move(__f), __a) {}
954 # endif
956 template <class _Rp, class... _ArgTypes>
957 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(const function& __f) {
958 function(__f).swap(*this);
959 return *this;
962 template <class _Rp, class... _ArgTypes>
963 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT {
964 __f_ = std::move(__f.__f_);
965 return *this;
968 template <class _Rp, class... _ArgTypes>
969 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT {
970 __f_ = nullptr;
971 return *this;
974 template <class _Rp, class... _ArgTypes>
975 template <class _Fp, class>
976 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f) {
977 function(std::forward<_Fp>(__f)).swap(*this);
978 return *this;
981 template <class _Rp, class... _ArgTypes>
982 function<_Rp(_ArgTypes...)>::~function() {}
984 template <class _Rp, class... _ArgTypes>
985 void function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT {
986 __f_.swap(__f.__f_);
989 template <class _Rp, class... _ArgTypes>
990 _Rp function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
991 return __f_(std::forward<_ArgTypes>(__arg)...);
994 # if _LIBCPP_HAS_RTTI
996 template <class _Rp, class... _ArgTypes>
997 const std::type_info& function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT {
998 return __f_.target_type();
1001 template <class _Rp, class... _ArgTypes>
1002 template <typename _Tp>
1003 _Tp* function<_Rp(_ArgTypes...)>::target() _NOEXCEPT {
1004 return (_Tp*)(__f_.template target<_Tp>());
1007 template <class _Rp, class... _ArgTypes>
1008 template <typename _Tp>
1009 const _Tp* function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT {
1010 return __f_.template target<_Tp>();
1013 # endif // _LIBCPP_HAS_RTTI
1015 template <class _Rp, class... _ArgTypes>
1016 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {
1017 return !__f;
1020 # if _LIBCPP_STD_VER <= 17
1022 template <class _Rp, class... _ArgTypes>
1023 inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {
1024 return !__f;
1027 template <class _Rp, class... _ArgTypes>
1028 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {
1029 return (bool)__f;
1032 template <class _Rp, class... _ArgTypes>
1033 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {
1034 return (bool)__f;
1037 # endif // _LIBCPP_STD_VER <= 17
1039 template <class _Rp, class... _ArgTypes>
1040 inline _LIBCPP_HIDE_FROM_ABI void swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1041 return __x.swap(__y);
1044 _LIBCPP_END_NAMESPACE_STD
1046 #endif // _LIBCPP_CXX03_LANG
1048 _LIBCPP_POP_MACROS
1050 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H