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