1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H
11#define _LIBCPP___FUNCTIONAL_FUNCTION_H
12
13#include <__assert>
14#include <__config>
15#include <__cstddef/nullptr_t.h>
16#include <__exception/exception.h>
17#include <__functional/binary_function.h>
18#include <__functional/unary_function.h>
19#include <__memory/addressof.h>
20#include <__new/placement_new_delete.h>
21#include <__type_traits/aligned_storage.h>
22#include <__type_traits/conditional.h>
23#include <__type_traits/conjunction.h>
24#include <__type_traits/decay.h>
25#include <__type_traits/enable_if.h>
26#include <__type_traits/invoke.h>
27#include <__type_traits/is_nothrow_constructible.h>
28#include <__type_traits/is_same.h>
29#include <__type_traits/is_scalar.h>
30#include <__type_traits/is_trivially_constructible.h>
31#include <__type_traits/is_trivially_destructible.h>
32#include <__type_traits/remove_cvref.h>
33#include <__type_traits/strip_signature.h>
34#include <__utility/forward.h>
35#include <__utility/move.h>
36#include <__utility/swap.h>
37#include <__utility/unreachable.h>
38#include <typeinfo>
39
40#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
41# pragma GCC system_header
42#endif
43
44_LIBCPP_PUSH_MACROS
45#include <__undef_macros>
46
47#ifndef _LIBCPP_CXX03_LANG
48
49_LIBCPP_BEGIN_NAMESPACE_STD
50
51// bad_function_call
52
53_LIBCPP_DIAGNOSTIC_PUSH
54_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
55# if !_LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
56_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
57# endif
58class _LIBCPP_EXPORTED_FROM_ABI bad_function_call : public exception {
59public:
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# if _LIBCPP_AVAILABILITY_HAS_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
71
72# if _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
73 const char* what() const _NOEXCEPT override;
74# endif
75};
76_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
77_LIBCPP_DIAGNOSTIC_POP
78
79[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_function_call() {
80# if _LIBCPP_HAS_EXCEPTIONS
81 throw bad_function_call();
82# else
83 _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");
84# endif
85}
86
87template <class _Fp>
88class function; // undefined
89
90namespace __function {
91
92template <class _Rp>
93struct __maybe_derive_from_unary_function {};
94
95template <class _Rp, class _A1>
96struct __maybe_derive_from_unary_function<_Rp(_A1)> : public __unary_function<_A1, _Rp> {};
97
98template <class _Rp>
99struct __maybe_derive_from_binary_function {};
100
101template <class _Rp, class _A1, class _A2>
102struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)> : public __binary_function<_A1, _A2, _Rp> {};
103
104template <class _Fp>
105_LIBCPP_HIDE_FROM_ABI bool __is_null(_Fp const&) {
106 return false;
107}
108
109template <class _Fp>
110_LIBCPP_HIDE_FROM_ABI bool __is_null(_Fp* __ptr) {
111 return !__ptr;
112}
113
114template <class _Ret, class _Class>
115_LIBCPP_HIDE_FROM_ABI bool __is_null(_Ret _Class::* __ptr) {
116 return !__ptr;
117}
118
119template <class _Fp>
120_LIBCPP_HIDE_FROM_ABI bool __is_null(function<_Fp> const& __f) {
121 return !__f;
122}
123
124# if __has_extension(blocks)
125template <class _Rp, class... _Args>
126_LIBCPP_HIDE_FROM_ABI bool __is_null(_Rp (^__p)(_Args...)) {
127 return !__p;
128}
129# endif
130
131} // namespace __function
132
133namespace __function {
134
135// __base provides an abstract interface for copyable functors.
136
137template <class _Fp>
138class __base;
139
140_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
141template <class _Rp, class... _ArgTypes>
142class __base<_Rp(_ArgTypes...)> {
143public:
144 __base(const __base&) = delete;
145 __base& operator=(const __base&) = delete;
146
147 _LIBCPP_HIDE_FROM_ABI __base() {}
148 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__base() {}
149 virtual __base* __clone() const = 0;
150 virtual void __clone(__base*) const = 0;
151 virtual void destroy() _NOEXCEPT = 0;
152 virtual void destroy_deallocate() _NOEXCEPT = 0;
153 virtual _Rp operator()(_ArgTypes&&...) = 0;
154 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(const type_info&) const _NOEXCEPT {
155 _LIBCPP_ASSERT_NON_NULL(false, "Trying to access type_info of std::function created in -fno-rtti mode!");
156 std::__libcpp_unreachable();
157 }
158 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT {
159 _LIBCPP_ASSERT_NON_NULL(false, "Trying to access type_info of std::function created in -fno-rtti mode!");
160 std::__libcpp_unreachable();
161 }
162};
163_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
164
165// __func implements __base for a given functor type.
166
167template <class _FD, class _FB>
168class __func;
169
170template <class _Fp, class _Rp, class... _ArgTypes>
171class __func<_Fp, _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {
172 _Fp __func_;
173
174public:
175 _LIBCPP_HIDE_FROM_ABI explicit __func(_Fp&& __f) : __func_(std::move(__f)) {}
176 _LIBCPP_HIDE_FROM_ABI explicit __func(const _Fp& __f) : __func_(__f) {}
177
178 _LIBCPP_HIDE_FROM_ABI_VIRTUAL __base<_Rp(_ArgTypes...)>* __clone() const override { return new __func(__func_); }
179
180 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __clone(__base<_Rp(_ArgTypes...)>* __p) const override {
181 ::new ((void*)__p) __func(__func_);
182 }
183
184 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void destroy() _NOEXCEPT override { __func_.~_Fp(); }
185 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void destroy_deallocate() _NOEXCEPT override { delete this; }
186 _LIBCPP_HIDE_FROM_ABI_VIRTUAL _Rp operator()(_ArgTypes&&... __arg) override {
187 return std::__invoke_r<_Rp>(__func_, std::forward<_ArgTypes>(__arg)...);
188 }
189# if _LIBCPP_HAS_RTTI
190 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const void* target(const type_info& __ti) const _NOEXCEPT override {
191 if (__ti == typeid(_Fp))
192 return std::addressof(__func_);
193 return nullptr;
194 }
195 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const std::type_info& target_type() const _NOEXCEPT override { return typeid(_Fp); }
196# endif // _LIBCPP_HAS_RTTI
197};
198
199// __value_func creates a value-type from a __func.
200
201template <class _Fp>
202class __value_func;
203
204template <class _Rp, class... _ArgTypes>
205class __value_func<_Rp(_ArgTypes...)> {
206 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
207 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
208 _LIBCPP_SUPPRESS_DEPRECATED_POP
209
210 typedef __base<_Rp(_ArgTypes...)> __func;
211 __func* __f_;
212
213 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI static __func* __as_base(void* __p) { return reinterpret_cast<__func*>(__p); }
214
215public:
216 _LIBCPP_HIDE_FROM_ABI __value_func() _NOEXCEPT : __f_(nullptr) {}
217
218 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __value_func>::value, int> = 0>
219 _LIBCPP_HIDE_FROM_ABI explicit __value_func(_Fp&& __f) : __f_(nullptr) {
220 typedef __function::__func<_Fp, _Rp(_ArgTypes...)> _Fun;
221
222 if (__function::__is_null(__f))
223 return;
224
225 if (sizeof(_Fun) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value) {
226 __f_ = ::new (std::addressof(x&: __buf_)) _Fun(std::move(__f));
227 } else {
228 __f_ = new _Fun(std::move(__f));
229 }
230 }
231
232 _LIBCPP_HIDE_FROM_ABI __value_func(const __value_func& __f) {
233 if (__f.__f_ == nullptr)
234 __f_ = nullptr;
235 else if ((void*)__f.__f_ == &__f.__buf_) {
236 __f_ = __as_base(p: &__buf_);
237 __f.__f_->__clone(__f_);
238 } else
239 __f_ = __f.__f_->__clone();
240 }
241
242 _LIBCPP_HIDE_FROM_ABI __value_func(__value_func&& __f) _NOEXCEPT {
243 if (__f.__f_ == nullptr)
244 __f_ = nullptr;
245 else if ((void*)__f.__f_ == &__f.__buf_) {
246 __f_ = __as_base(p: &__buf_);
247 __f.__f_->__clone(__f_);
248 } else {
249 __f_ = __f.__f_;
250 __f.__f_ = nullptr;
251 }
252 }
253
254 _LIBCPP_HIDE_FROM_ABI ~__value_func() {
255 if ((void*)__f_ == &__buf_)
256 __f_->destroy();
257 else if (__f_)
258 __f_->destroy_deallocate();
259 }
260
261 _LIBCPP_HIDE_FROM_ABI __value_func& operator=(__value_func&& __f) {
262 *this = nullptr;
263 if (__f.__f_ == nullptr)
264 __f_ = nullptr;
265 else if ((void*)__f.__f_ == &__f.__buf_) {
266 __f_ = __as_base(p: &__buf_);
267 __f.__f_->__clone(__f_);
268 } else {
269 __f_ = __f.__f_;
270 __f.__f_ = nullptr;
271 }
272 return *this;
273 }
274
275 _LIBCPP_HIDE_FROM_ABI __value_func& operator=(nullptr_t) {
276 __func* __f = __f_;
277 __f_ = nullptr;
278 if ((void*)__f == &__buf_)
279 __f->destroy();
280 else if (__f)
281 __f->destroy_deallocate();
282 return *this;
283 }
284
285 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {
286 if (__f_ == nullptr)
287 std::__throw_bad_function_call();
288 return (*__f_)(std::forward<_ArgTypes>(__args)...);
289 }
290
291 _LIBCPP_HIDE_FROM_ABI void swap(__value_func& __f) _NOEXCEPT {
292 if (std::addressof(__f) == this)
293 return;
294 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_) {
295 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
296 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
297 _LIBCPP_SUPPRESS_DEPRECATED_POP
298 __func* __t = __as_base(p: &__tempbuf);
299 __f_->__clone(__t);
300 __f_->destroy();
301 __f_ = nullptr;
302 __f.__f_->__clone(__as_base(p: &__buf_));
303 __f.__f_->destroy();
304 __f.__f_ = nullptr;
305 __f_ = __as_base(p: &__buf_);
306 __t->__clone(__as_base(p: &__f.__buf_));
307 __t->destroy();
308 __f.__f_ = __as_base(p: &__f.__buf_);
309 } else if ((void*)__f_ == &__buf_) {
310 __f_->__clone(__as_base(p: &__f.__buf_));
311 __f_->destroy();
312 __f_ = __f.__f_;
313 __f.__f_ = __as_base(p: &__f.__buf_);
314 } else if ((void*)__f.__f_ == &__f.__buf_) {
315 __f.__f_->__clone(__as_base(p: &__buf_));
316 __f.__f_->destroy();
317 __f.__f_ = __f_;
318 __f_ = __as_base(p: &__buf_);
319 } else
320 std::swap(__f_, __f.__f_);
321 }
322
323 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
324
325# if _LIBCPP_HAS_RTTI
326 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT {
327 if (__f_ == nullptr)
328 return typeid(void);
329 return __f_->target_type();
330 }
331
332 template <typename _Tp>
333 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {
334 if (__f_ == nullptr)
335 return nullptr;
336 return (const _Tp*)__f_->target(typeid(_Tp));
337 }
338# endif // _LIBCPP_HAS_RTTI
339};
340
341// Storage for a functor object, to be used with __policy to manage copy and
342// destruction.
343union __policy_storage {
344 mutable char __small[sizeof(void*) * 2];
345 void* __large;
346};
347
348// True if _Fun can safely be held in __policy_storage.__small.
349template <typename _Fun>
350struct __use_small_storage
351 : public integral_constant<
352 bool,
353 sizeof(_Fun) <= sizeof(__policy_storage) && _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
354 is_trivially_copy_constructible<_Fun>::value && is_trivially_destructible<_Fun>::value> {};
355
356// Policy contains information about how to copy, destroy, and move the
357// underlying functor. You can think of it as a vtable of sorts.
358struct __policy {
359 // Used to copy or destroy __large values. null for trivial objects.
360 void* (*const __clone)(const void*);
361 void (*const __destroy)(void*);
362
363 // True if this is the null policy (no value).
364 const bool __is_null;
365
366 // The target type. May be null if RTTI is disabled.
367 const std::type_info* const __type_info;
368
369 // Returns a pointer to a static policy object suitable for the functor
370 // type.
371 template <typename _Fun>
372 _LIBCPP_HIDE_FROM_ABI static const __policy* __create() {
373 if constexpr (__use_small_storage<_Fun>::value) {
374 static constexpr __policy __policy = {
375 nullptr,
376 nullptr,
377 false,
378# if _LIBCPP_HAS_RTTI
379 &typeid(_Fun)
380# else
381 nullptr
382# endif
383 };
384 return &__policy;
385 } else {
386 static constexpr __policy __policy = {
387 std::addressof(__large_clone<_Fun>),
388 std::addressof(__large_destroy<_Fun>),
389 false,
390# if _LIBCPP_HAS_RTTI
391 &typeid(_Fun)
392# else
393 nullptr
394# endif
395 };
396 return &__policy;
397 }
398 }
399
400 _LIBCPP_HIDE_FROM_ABI static const __policy* __create_empty() {
401 static constexpr __policy __policy = {
402 nullptr,
403 nullptr,
404 true,
405# if _LIBCPP_HAS_RTTI
406 &typeid(void)
407# else
408 nullptr
409# endif
410 };
411 return &__policy;
412 }
413
414private:
415 template <typename _Fun>
416 _LIBCPP_HIDE_FROM_ABI static void* __large_clone(const void* __s) {
417 const _Fun* __f = static_cast<const _Fun*>(__s);
418 return new _Fun(*__f);
419 }
420
421 template <typename _Fun>
422 _LIBCPP_HIDE_FROM_ABI static void __large_destroy(void* __s) {
423 delete static_cast<_Fun*>(__s);
424 }
425};
426
427// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
428// faster for types that can be passed in registers.
429template <typename _Tp>
430using __fast_forward _LIBCPP_NODEBUG = __conditional_t<is_scalar<_Tp>::value, _Tp, _Tp&&>;
431
432// __policy_func uses a __policy to create a type-erased, copyable functor.
433
434template <class _Fp>
435class __policy_func;
436
437template <class _Rp, class... _ArgTypes>
438class __policy_func<_Rp(_ArgTypes...)> {
439 // Inline storage for small objects.
440 __policy_storage __buf_;
441
442 using _ErasedFunc _LIBCPP_NODEBUG = _Rp(const __policy_storage*, __fast_forward<_ArgTypes>...);
443
444 _ErasedFunc* __func_;
445
446 // The policy that describes how to move / copy / destroy __buf_. Never
447 // null, even if the function is empty.
448 const __policy* __policy_;
449
450 _LIBCPP_HIDE_FROM_ABI static _Rp __empty_func(const __policy_storage*, __fast_forward<_ArgTypes>...) {
451 std::__throw_bad_function_call();
452 }
453
454 template <class _Fun>
455 _LIBCPP_HIDE_FROM_ABI static _Rp __call_func(const __policy_storage* __buf, __fast_forward<_ArgTypes>... __args) {
456 _Fun* __func = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value ? &__buf->__small : __buf->__large);
457
458 return std::__invoke_r<_Rp>(*__func, std::forward<_ArgTypes>(__args)...);
459 }
460
461public:
462 _LIBCPP_HIDE_FROM_ABI __policy_func() : __func_(__empty_func), __policy_(__policy::__create_empty()) {}
463
464 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __policy_func>::value, int> = 0>
465 _LIBCPP_HIDE_FROM_ABI explicit __policy_func(_Fp&& __f) : __policy_(__policy::__create_empty()) {
466 if (__function::__is_null(__f))
467 return;
468
469 __func_ = __call_func<_Fp>;
470 __policy_ = __policy::__create<_Fp>();
471 if (__use_small_storage<_Fp>()) {
472 ::new ((void*)&__buf_.__small) _Fp(std::move(__f));
473 } else {
474 __buf_.__large = ::new _Fp(std::move(__f));
475 }
476 }
477
478 _LIBCPP_HIDE_FROM_ABI __policy_func(const __policy_func& __f)
479 : __buf_(__f.__buf_), __func_(__f.__func_), __policy_(__f.__policy_) {
480 if (__policy_->__clone)
481 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
482 }
483
484 _LIBCPP_HIDE_FROM_ABI __policy_func(__policy_func&& __f)
485 : __buf_(__f.__buf_), __func_(__f.__func_), __policy_(__f.__policy_) {
486 if (__policy_->__destroy) {
487 __f.__policy_ = __policy::__create_empty();
488 __f.__func_ = {};
489 }
490 }
491
492 _LIBCPP_HIDE_FROM_ABI ~__policy_func() {
493 if (__policy_->__destroy)
494 __policy_->__destroy(__buf_.__large);
495 }
496
497 _LIBCPP_HIDE_FROM_ABI __policy_func& operator=(__policy_func&& __f) {
498 *this = nullptr;
499 __buf_ = __f.__buf_;
500 __func_ = __f.__func_;
501 __policy_ = __f.__policy_;
502 __f.__policy_ = __policy::__create_empty();
503 __f.__func_ = {};
504 return *this;
505 }
506
507 _LIBCPP_HIDE_FROM_ABI __policy_func& operator=(nullptr_t) {
508 const __policy* __p = __policy_;
509 __policy_ = __policy::__create_empty();
510 __func_ = {};
511 if (__p->__destroy)
512 __p->__destroy(__buf_.__large);
513 return *this;
514 }
515
516 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {
517 return __func_(std::addressof(x: __buf_), std::forward<_ArgTypes>(__args)...);
518 }
519
520 _LIBCPP_HIDE_FROM_ABI void swap(__policy_func& __f) {
521 std::swap(__func_, __f.__func_);
522 std::swap(x&: __policy_, y&: __f.__policy_);
523 std::swap(x&: __buf_, y&: __f.__buf_);
524 }
525
526 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return !__policy_->__is_null; }
527
528# if _LIBCPP_HAS_RTTI
529 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT {
530 _LIBCPP_ASSERT_NON_NULL(
531 __policy_->__type_info, "Trying to access type_info of std::functions created in -fno-rtti mode!");
532 return *__policy_->__type_info;
533 }
534
535 template <typename _Tp>
536 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {
537 if (__policy_->__is_null)
538 return nullptr;
539 _LIBCPP_ASSERT_NON_NULL(
540 __policy_->__type_info, "Trying to access type_info of std::function created in -fno-rtti mode!");
541 if (typeid(_Tp) != *__policy_->__type_info)
542 return nullptr;
543 if (__policy_->__clone) // Out of line storage.
544 return reinterpret_cast<const _Tp*>(__buf_.__large);
545 else
546 return reinterpret_cast<const _Tp*>(&__buf_.__small);
547 }
548# endif // _LIBCPP_HAS_RTTI
549};
550
551# if _LIBCPP_HAS_BLOCKS_RUNTIME
552
553_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
554
555extern "C" void* _Block_copy(const void*);
556extern "C" void _Block_release(const void*);
557
558_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
559
560template <class _Rp1, class... _ArgTypes1, class _Rp, class... _ArgTypes>
561class __func<_Rp1 (^)(_ArgTypes1...), _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {
562 typedef _Rp1 (^__block_type)(_ArgTypes1...);
563 __block_type __f_;
564
565public:
566 _LIBCPP_HIDE_FROM_ABI explicit __func(__block_type const& __f)
567# if __has_feature(objc_arc)
568 : __f_(__f)
569# else
570 : __f_(reinterpret_cast<__block_type>(__f ? __function::_Block_copy(__f) : nullptr))
571# endif
572 {
573 }
574
575 // [TODO] add && to save on a retain
576
577 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const {
578 _LIBCPP_ASSERT_INTERNAL(
579 false,
580 "Block pointers are just pointers, so they should always fit into "
581 "std::function's small buffer optimization. This function should "
582 "never be invoked.");
583 return nullptr;
584 }
585
586 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
587 ::new ((void*)__p) __func(__f_);
588 }
589
590 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT {
591# if !__has_feature(objc_arc)
592 if (__f_)
593 __function::_Block_release(__f_);
594# endif
595 __f_ = 0;
596 }
597
598 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT {
599 _LIBCPP_ASSERT_INTERNAL(
600 false,
601 "Block pointers are just pointers, so they should always fit into "
602 "std::function's small buffer optimization. This function should "
603 "never be invoked.");
604 }
605
606 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg) {
607 return std::__invoke(__f_, std::forward<_ArgTypes>(__arg)...);
608 }
609
610# if _LIBCPP_HAS_RTTI
611 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(type_info const& __ti) const _NOEXCEPT {
612 if (__ti == typeid(__func::__block_type))
613 return &__f_;
614 return (const void*)nullptr;
615 }
616
617 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT {
618 return typeid(__func::__block_type);
619 }
620# endif // _LIBCPP_HAS_RTTI
621};
622
623# endif // _LIBCPP_HAS_BLOCKS_RUNTIME
624
625} // namespace __function
626
627template <class _Rp, class... _ArgTypes>
628class function<_Rp(_ArgTypes...)>
629 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
630 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)> {
631# ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
632 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
633# else
634 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
635# endif
636
637 __func __f_;
638
639 template <class _Fp>
640 using _EnableIfLValueCallable _LIBCPP_NODEBUG = __enable_if_t<
641 _And<_IsNotSame<__remove_cvref_t<_Fp>, function>, __is_invocable_r<_Rp, _Fp&, _ArgTypes...>>::value>;
642
643public:
644 typedef _Rp result_type;
645
646 // construct/copy/destroy:
647 _LIBCPP_HIDE_FROM_ABI function() _NOEXCEPT {}
648 _LIBCPP_HIDE_FROM_ABI function(nullptr_t) _NOEXCEPT {}
649 _LIBCPP_HIDE_FROM_ABI function(const function&) = default;
650 _LIBCPP_HIDE_FROM_ABI function(function&&) _NOEXCEPT = default;
651 template <class _Fp, class = _EnableIfLValueCallable<_Fp>>
652 _LIBCPP_HIDE_FROM_ABI function(_Fp __f) : __f_(std::move(__f)) {}
653
654# if _LIBCPP_STD_VER <= 14
655 template <class _Alloc>
656 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
657 template <class _Alloc>
658 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
659 template <class _Alloc>
660 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, const function& __f) : __f_(__f.__f_) {}
661 template <class _Alloc>
662 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, function&& __f) : __f_(std::move(__f.__f_)) {}
663 template <class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
664 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, _Fp __f) : __f_(std::move(__f)) {}
665# endif
666
667 _LIBCPP_HIDE_FROM_ABI function& operator=(const function& __f) {
668 function(__f).swap(f&: *this);
669 return *this;
670 }
671
672 _LIBCPP_HIDE_FROM_ABI function& operator=(function&& __f) _NOEXCEPT = default;
673
674 _LIBCPP_HIDE_FROM_ABI function& operator=(nullptr_t) _NOEXCEPT {
675 __f_ = nullptr;
676 return *this;
677 }
678
679 template <class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>>
680 _LIBCPP_HIDE_FROM_ABI function& operator=(_Fp&& __f) {
681 function(std::forward<_Fp>(__f)).swap(f&: *this);
682 return *this;
683 }
684
685 _LIBCPP_HIDE_FROM_ABI ~function() = default;
686
687 // function modifiers:
688 _LIBCPP_HIDE_FROM_ABI void swap(function& __f) _NOEXCEPT { __f_.swap(__f.__f_); }
689
690# if _LIBCPP_STD_VER <= 14
691 template <class _Fp, class _Alloc>
692 _LIBCPP_HIDE_FROM_ABI void assign(_Fp&& __f, const _Alloc& __a) {
693 function(allocator_arg, __a, std::forward<_Fp>(__f)).swap(*this);
694 }
695# endif
696
697 // function capacity:
698 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return static_cast<bool>(__f_); }
699
700 // deleted overloads close possible hole in the type system
701 template <class _R2, class... _ArgTypes2>
702 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
703# if _LIBCPP_STD_VER <= 17
704 template <class _R2, class... _ArgTypes2>
705 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
706# endif
707
708public:
709 // function invocation:
710 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes... __args) const { return __f_(std::forward<_ArgTypes>(__args)...); }
711
712# if _LIBCPP_HAS_RTTI
713 // function target access:
714 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT {
715 return __f_.target_type();
716 }
717
718 template <typename _Tp>
719 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _Tp* target() _NOEXCEPT {
720 return (_Tp*)(__f_.template target<_Tp>());
721 }
722
723 template <typename _Tp>
724 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {
725 return __f_.template target<_Tp>();
726 }
727# endif // _LIBCPP_HAS_RTTI
728};
729
730# if _LIBCPP_STD_VER >= 17
731template <class _Rp, class... _Ap>
732function(_Rp (*)(_Ap...)) -> function<_Rp(_Ap...)>;
733
734template <class _Fp, class _Stripped = __strip_signature_t<decltype(&_Fp::operator())>>
735function(_Fp) -> function<_Stripped>;
736# endif // _LIBCPP_STD_VER >= 17
737
738template <class _Rp, class... _ArgTypes>
739inline _LIBCPP_HIDE_FROM_ABI bool operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {
740 return !__f;
741}
742
743# if _LIBCPP_STD_VER <= 17
744
745template <class _Rp, class... _ArgTypes>
746inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {
747 return !__f;
748}
749
750template <class _Rp, class... _ArgTypes>
751inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {
752 return (bool)__f;
753}
754
755template <class _Rp, class... _ArgTypes>
756inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {
757 return (bool)__f;
758}
759
760# endif // _LIBCPP_STD_VER <= 17
761
762template <class _Rp, class... _ArgTypes>
763inline _LIBCPP_HIDE_FROM_ABI void swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
764 return __x.swap(__y);
765}
766
767_LIBCPP_END_NAMESPACE_STD
768
769#endif // _LIBCPP_CXX03_LANG
770
771_LIBCPP_POP_MACROS
772
773#endif // _LIBCPP___FUNCTIONAL_FUNCTION_H
774