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