[mlir][py] Enable loading only specified dialects during creation. (#121421)
[llvm-project.git] / libcxx / include / __functional / function.h
bloba421a3ef4f5f99109b3a5636fbe69829f22c7161
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 <tuple>
42 #include <typeinfo>
44 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45 # pragma GCC system_header
46 #endif
48 _LIBCPP_PUSH_MACROS
49 #include <__undef_macros>
51 #ifndef _LIBCPP_CXX03_LANG
53 _LIBCPP_BEGIN_NAMESPACE_STD
55 // bad_function_call
57 _LIBCPP_DIAGNOSTIC_PUSH
58 # if !_LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
59 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
60 # endif
61 class _LIBCPP_EXPORTED_FROM_ABI bad_function_call : public exception {
62 public:
63 _LIBCPP_HIDE_FROM_ABI bad_function_call() _NOEXCEPT = default;
64 _LIBCPP_HIDE_FROM_ABI bad_function_call(const bad_function_call&) _NOEXCEPT = default;
65 _LIBCPP_HIDE_FROM_ABI bad_function_call& operator=(const bad_function_call&) _NOEXCEPT = default;
66 // Note that when a key function is not used, every translation unit that uses
67 // bad_function_call will end up containing a weak definition of the vtable and
68 // typeinfo.
69 # if _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
70 ~bad_function_call() _NOEXCEPT override;
71 # else
72 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_function_call() _NOEXCEPT override {}
73 # endif
75 # ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
76 const char* what() const _NOEXCEPT override;
77 # endif
79 _LIBCPP_DIAGNOSTIC_POP
81 [[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_function_call() {
82 # if _LIBCPP_HAS_EXCEPTIONS
83 throw bad_function_call();
84 # else
85 _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");
86 # endif
89 template <class _Fp>
90 class _LIBCPP_TEMPLATE_VIS function; // undefined
92 namespace __function {
94 template <class _Rp>
95 struct __maybe_derive_from_unary_function {};
97 template <class _Rp, class _A1>
98 struct __maybe_derive_from_unary_function<_Rp(_A1)> : public __unary_function<_A1, _Rp> {};
100 template <class _Rp>
101 struct __maybe_derive_from_binary_function {};
103 template <class _Rp, class _A1, class _A2>
104 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)> : public __binary_function<_A1, _A2, _Rp> {};
106 template <class _Fp>
107 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Fp const&) {
108 return true;
111 template <class _Fp>
112 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Fp* __ptr) {
113 return __ptr;
116 template <class _Ret, class _Class>
117 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Ret _Class::*__ptr) {
118 return __ptr;
121 template <class _Fp>
122 _LIBCPP_HIDE_FROM_ABI bool __not_null(function<_Fp> const& __f) {
123 return !!__f;
126 # if _LIBCPP_HAS_EXTENSION_BLOCKS
127 template <class _Rp, class... _Args>
128 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Rp (^__p)(_Args...)) {
129 return __p;
131 # endif
133 } // namespace __function
135 namespace __function {
137 // __alloc_func holds a functor and an allocator.
139 template <class _Fp, class _Ap, class _FB>
140 class __alloc_func;
141 template <class _Fp, class _FB>
142 class __default_alloc_func;
144 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
145 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)> {
146 _LIBCPP_COMPRESSED_PAIR(_Fp, __func_, _Ap, __alloc_);
148 public:
149 using _Target _LIBCPP_NODEBUG = _Fp;
150 using _Alloc _LIBCPP_NODEBUG = _Ap;
152 _LIBCPP_HIDE_FROM_ABI const _Target& __target() const { return __func_; }
154 // WIN32 APIs may define __allocator, so use __get_allocator instead.
155 _LIBCPP_HIDE_FROM_ABI const _Alloc& __get_allocator() const { return __alloc_; }
157 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(_Target&& __f) : __func_(std::move(__f)), __alloc_() {}
159 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(const _Target& __f, const _Alloc& __a) : __func_(__f), __alloc_(__a) {}
161 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(const _Target& __f, _Alloc&& __a)
162 : __func_(__f), __alloc_(std::move(__a)) {}
164 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(_Target&& __f, _Alloc&& __a)
165 : __func_(std::move(__f)), __alloc_(std::move(__a)) {}
167 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __arg) {
168 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
169 return _Invoker::__call(__func_, std::forward<_ArgTypes>(__arg)...);
172 _LIBCPP_HIDE_FROM_ABI __alloc_func* __clone() const {
173 typedef allocator_traits<_Alloc> __alloc_traits;
174 typedef __rebind_alloc<__alloc_traits, __alloc_func> _AA;
175 _AA __a(__alloc_);
176 typedef __allocator_destructor<_AA> _Dp;
177 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
178 ::new ((void*)__hold.get()) __alloc_func(__func_, _Alloc(__a));
179 return __hold.release();
182 _LIBCPP_HIDE_FROM_ABI void destroy() _NOEXCEPT {
183 __func_.~_Fp();
184 __alloc_.~_Alloc();
187 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__alloc_func* __f) {
188 typedef allocator_traits<_Alloc> __alloc_traits;
189 typedef __rebind_alloc<__alloc_traits, __alloc_func> _FunAlloc;
190 _FunAlloc __a(__f->__get_allocator());
191 __f->destroy();
192 __a.deallocate(__f, 1);
196 template <class _Fp, class _Rp, class... _ArgTypes>
197 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
198 _Fp __f_;
200 public:
201 using _Target _LIBCPP_NODEBUG = _Fp;
203 _LIBCPP_HIDE_FROM_ABI const _Target& __target() const { return __f_; }
205 _LIBCPP_HIDE_FROM_ABI explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}
207 _LIBCPP_HIDE_FROM_ABI explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
209 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __arg) {
210 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
211 return _Invoker::__call(__f_, std::forward<_ArgTypes>(__arg)...);
214 _LIBCPP_HIDE_FROM_ABI __default_alloc_func* __clone() const {
215 __builtin_new_allocator::__holder_t __hold = __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
216 __default_alloc_func* __res = ::new ((void*)__hold.get()) __default_alloc_func(__f_);
217 (void)__hold.release();
218 return __res;
221 _LIBCPP_HIDE_FROM_ABI void destroy() _NOEXCEPT { __f_.~_Target(); }
223 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__default_alloc_func* __f) {
224 __f->destroy();
225 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
229 // __base provides an abstract interface for copyable functors.
231 template <class _Fp>
232 class _LIBCPP_TEMPLATE_VIS __base;
234 template <class _Rp, class... _ArgTypes>
235 class __base<_Rp(_ArgTypes...)> {
236 public:
237 __base(const __base&) = delete;
238 __base& operator=(const __base&) = delete;
240 _LIBCPP_HIDE_FROM_ABI __base() {}
241 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__base() {}
242 virtual __base* __clone() const = 0;
243 virtual void __clone(__base*) const = 0;
244 virtual void destroy() _NOEXCEPT = 0;
245 virtual void destroy_deallocate() _NOEXCEPT = 0;
246 virtual _Rp operator()(_ArgTypes&&...) = 0;
247 # if _LIBCPP_HAS_RTTI
248 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
249 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
250 # endif // _LIBCPP_HAS_RTTI
253 // __func implements __base for a given functor type.
255 template <class _FD, class _Alloc, class _FB>
256 class __func;
258 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
259 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {
260 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
262 public:
263 _LIBCPP_HIDE_FROM_ABI explicit __func(_Fp&& __f) : __f_(std::move(__f)) {}
265 _LIBCPP_HIDE_FROM_ABI explicit __func(const _Fp& __f, const _Alloc& __a) : __f_(__f, __a) {}
267 _LIBCPP_HIDE_FROM_ABI explicit __func(const _Fp& __f, _Alloc&& __a) : __f_(__f, std::move(__a)) {}
269 _LIBCPP_HIDE_FROM_ABI explicit __func(_Fp&& __f, _Alloc&& __a) : __f_(std::move(__f), std::move(__a)) {}
271 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const;
272 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
273 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT;
274 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT;
275 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg);
276 # if _LIBCPP_HAS_RTTI
277 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(const type_info&) const _NOEXCEPT;
278 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT;
279 # endif // _LIBCPP_HAS_RTTI
282 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
283 __base<_Rp(_ArgTypes...)>* __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const {
284 typedef allocator_traits<_Alloc> __alloc_traits;
285 typedef __rebind_alloc<__alloc_traits, __func> _Ap;
286 _Ap __a(__f_.__get_allocator());
287 typedef __allocator_destructor<_Ap> _Dp;
288 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
289 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
290 return __hold.release();
293 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
294 void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const {
295 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
298 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
299 void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT {
300 __f_.destroy();
303 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
304 void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT {
305 typedef allocator_traits<_Alloc> __alloc_traits;
306 typedef __rebind_alloc<__alloc_traits, __func> _Ap;
307 _Ap __a(__f_.__get_allocator());
308 __f_.destroy();
309 __a.deallocate(this, 1);
312 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
313 _Rp __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
314 return __f_(std::forward<_ArgTypes>(__arg)...);
317 # if _LIBCPP_HAS_RTTI
319 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
320 const void* __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT {
321 if (__ti == typeid(_Fp))
322 return std::addressof(__f_.__target());
323 return nullptr;
326 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
327 const std::type_info& __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT {
328 return typeid(_Fp);
331 # endif // _LIBCPP_HAS_RTTI
333 // __value_func creates a value-type from a __func.
335 template <class _Fp>
336 class __value_func;
338 template <class _Rp, class... _ArgTypes>
339 class __value_func<_Rp(_ArgTypes...)> {
340 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
341 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
342 _LIBCPP_SUPPRESS_DEPRECATED_POP
344 typedef __base<_Rp(_ArgTypes...)> __func;
345 __func* __f_;
347 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI static __func* __as_base(void* __p) { return reinterpret_cast<__func*>(__p); }
349 public:
350 _LIBCPP_HIDE_FROM_ABI __value_func() _NOEXCEPT : __f_(nullptr) {}
352 template <class _Fp, class _Alloc>
353 _LIBCPP_HIDE_FROM_ABI __value_func(_Fp&& __f, const _Alloc& __a) : __f_(nullptr) {
354 typedef allocator_traits<_Alloc> __alloc_traits;
355 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
356 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
358 if (__function::__not_null(__f)) {
359 _FunAlloc __af(__a);
360 if (sizeof(_Fun) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value &&
361 is_nothrow_copy_constructible<_FunAlloc>::value) {
362 __f_ = ::new ((void*)&__buf_) _Fun(std::move(__f), _Alloc(__af));
363 } else {
364 typedef __allocator_destructor<_FunAlloc> _Dp;
365 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
366 ::new ((void*)__hold.get()) _Fun(std::move(__f), _Alloc(__a));
367 __f_ = __hold.release();
372 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __value_func>::value, int> = 0>
373 _LIBCPP_HIDE_FROM_ABI explicit __value_func(_Fp&& __f) : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}
375 _LIBCPP_HIDE_FROM_ABI __value_func(const __value_func& __f) {
376 if (__f.__f_ == nullptr)
377 __f_ = nullptr;
378 else if ((void*)__f.__f_ == &__f.__buf_) {
379 __f_ = __as_base(&__buf_);
380 __f.__f_->__clone(__f_);
381 } else
382 __f_ = __f.__f_->__clone();
385 _LIBCPP_HIDE_FROM_ABI __value_func(__value_func&& __f) _NOEXCEPT {
386 if (__f.__f_ == nullptr)
387 __f_ = nullptr;
388 else if ((void*)__f.__f_ == &__f.__buf_) {
389 __f_ = __as_base(&__buf_);
390 __f.__f_->__clone(__f_);
391 } else {
392 __f_ = __f.__f_;
393 __f.__f_ = nullptr;
397 _LIBCPP_HIDE_FROM_ABI ~__value_func() {
398 if ((void*)__f_ == &__buf_)
399 __f_->destroy();
400 else if (__f_)
401 __f_->destroy_deallocate();
404 _LIBCPP_HIDE_FROM_ABI __value_func& operator=(__value_func&& __f) {
405 *this = nullptr;
406 if (__f.__f_ == nullptr)
407 __f_ = nullptr;
408 else if ((void*)__f.__f_ == &__f.__buf_) {
409 __f_ = __as_base(&__buf_);
410 __f.__f_->__clone(__f_);
411 } else {
412 __f_ = __f.__f_;
413 __f.__f_ = nullptr;
415 return *this;
418 _LIBCPP_HIDE_FROM_ABI __value_func& operator=(nullptr_t) {
419 __func* __f = __f_;
420 __f_ = nullptr;
421 if ((void*)__f == &__buf_)
422 __f->destroy();
423 else if (__f)
424 __f->destroy_deallocate();
425 return *this;
428 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {
429 if (__f_ == nullptr)
430 __throw_bad_function_call();
431 return (*__f_)(std::forward<_ArgTypes>(__args)...);
434 _LIBCPP_HIDE_FROM_ABI void swap(__value_func& __f) _NOEXCEPT {
435 if (&__f == this)
436 return;
437 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_) {
438 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
439 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
440 _LIBCPP_SUPPRESS_DEPRECATED_POP
441 __func* __t = __as_base(&__tempbuf);
442 __f_->__clone(__t);
443 __f_->destroy();
444 __f_ = nullptr;
445 __f.__f_->__clone(__as_base(&__buf_));
446 __f.__f_->destroy();
447 __f.__f_ = nullptr;
448 __f_ = __as_base(&__buf_);
449 __t->__clone(__as_base(&__f.__buf_));
450 __t->destroy();
451 __f.__f_ = __as_base(&__f.__buf_);
452 } else if ((void*)__f_ == &__buf_) {
453 __f_->__clone(__as_base(&__f.__buf_));
454 __f_->destroy();
455 __f_ = __f.__f_;
456 __f.__f_ = __as_base(&__f.__buf_);
457 } else if ((void*)__f.__f_ == &__f.__buf_) {
458 __f.__f_->__clone(__as_base(&__buf_));
459 __f.__f_->destroy();
460 __f.__f_ = __f_;
461 __f_ = __as_base(&__buf_);
462 } else
463 std::swap(__f_, __f.__f_);
466 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
468 # if _LIBCPP_HAS_RTTI
469 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT {
470 if (__f_ == nullptr)
471 return typeid(void);
472 return __f_->target_type();
475 template <typename _Tp>
476 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {
477 if (__f_ == nullptr)
478 return nullptr;
479 return (const _Tp*)__f_->target(typeid(_Tp));
481 # endif // _LIBCPP_HAS_RTTI
484 // Storage for a functor object, to be used with __policy to manage copy and
485 // destruction.
486 union __policy_storage {
487 mutable char __small[sizeof(void*) * 2];
488 void* __large;
491 // True if _Fun can safely be held in __policy_storage.__small.
492 template <typename _Fun>
493 struct __use_small_storage
494 : public integral_constant<
495 bool,
496 sizeof(_Fun) <= sizeof(__policy_storage)&& _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
497 is_trivially_copy_constructible<_Fun>::value && is_trivially_destructible<_Fun>::value> {};
499 // Policy contains information about how to copy, destroy, and move the
500 // underlying functor. You can think of it as a vtable of sorts.
501 struct __policy {
502 // Used to copy or destroy __large values. null for trivial objects.
503 void* (*const __clone)(const void*);
504 void (*const __destroy)(void*);
506 // True if this is the null policy (no value).
507 const bool __is_null;
509 // The target type. May be null if RTTI is disabled.
510 const std::type_info* const __type_info;
512 // Returns a pointer to a static policy object suitable for the functor
513 // type.
514 template <typename _Fun>
515 _LIBCPP_HIDE_FROM_ABI static const __policy* __create() {
516 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
519 _LIBCPP_HIDE_FROM_ABI static const __policy* __create_empty() {
520 static constexpr __policy __policy = {
521 nullptr,
522 nullptr,
523 true,
524 # if _LIBCPP_HAS_RTTI
525 &typeid(void)
526 # else
527 nullptr
528 # endif
530 return &__policy;
533 private:
534 template <typename _Fun>
535 _LIBCPP_HIDE_FROM_ABI static void* __large_clone(const void* __s) {
536 const _Fun* __f = static_cast<const _Fun*>(__s);
537 return __f->__clone();
540 template <typename _Fun>
541 _LIBCPP_HIDE_FROM_ABI static void __large_destroy(void* __s) {
542 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
545 template <typename _Fun>
546 _LIBCPP_HIDE_FROM_ABI static const __policy* __choose_policy(/* is_small = */ false_type) {
547 static constexpr __policy __policy = {
548 &__large_clone<_Fun>,
549 &__large_destroy<_Fun>,
550 false,
551 # if _LIBCPP_HAS_RTTI
552 &typeid(typename _Fun::_Target)
553 # else
554 nullptr
555 # endif
557 return &__policy;
560 template <typename _Fun>
561 _LIBCPP_HIDE_FROM_ABI static const __policy* __choose_policy(/* is_small = */ true_type) {
562 static constexpr __policy __policy = {
563 nullptr,
564 nullptr,
565 false,
566 # if _LIBCPP_HAS_RTTI
567 &typeid(typename _Fun::_Target)
568 # else
569 nullptr
570 # endif
572 return &__policy;
576 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
577 // faster for types that can be passed in registers.
578 template <typename _Tp>
579 using __fast_forward = __conditional_t<is_scalar<_Tp>::value, _Tp, _Tp&&>;
581 // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
583 template <class _Fp>
584 struct __policy_invoker;
586 template <class _Rp, class... _ArgTypes>
587 struct __policy_invoker<_Rp(_ArgTypes...)> {
588 typedef _Rp (*__Call)(const __policy_storage*, __fast_forward<_ArgTypes>...);
590 __Call __call_;
592 // Creates an invoker that throws bad_function_call.
593 _LIBCPP_HIDE_FROM_ABI __policy_invoker() : __call_(&__call_empty) {}
595 // Creates an invoker that calls the given instance of __func.
596 template <typename _Fun>
597 _LIBCPP_HIDE_FROM_ABI static __policy_invoker __create() {
598 return __policy_invoker(&__call_impl<_Fun>);
601 private:
602 _LIBCPP_HIDE_FROM_ABI explicit __policy_invoker(__Call __c) : __call_(__c) {}
604 _LIBCPP_HIDE_FROM_ABI static _Rp __call_empty(const __policy_storage*, __fast_forward<_ArgTypes>...) {
605 __throw_bad_function_call();
608 template <typename _Fun>
609 _LIBCPP_HIDE_FROM_ABI static _Rp __call_impl(const __policy_storage* __buf, __fast_forward<_ArgTypes>... __args) {
610 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value ? &__buf->__small : __buf->__large);
611 return (*__f)(std::forward<_ArgTypes>(__args)...);
615 // __policy_func uses a __policy and __policy_invoker to create a type-erased,
616 // copyable functor.
618 template <class _Fp>
619 class __policy_func;
621 template <class _Rp, class... _ArgTypes>
622 class __policy_func<_Rp(_ArgTypes...)> {
623 // Inline storage for small objects.
624 __policy_storage __buf_;
626 // Calls the value stored in __buf_. This could technically be part of
627 // policy, but storing it here eliminates a level of indirection inside
628 // operator().
629 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
630 __invoker __invoker_;
632 // The policy that describes how to move / copy / destroy __buf_. Never
633 // null, even if the function is empty.
634 const __policy* __policy_;
636 public:
637 _LIBCPP_HIDE_FROM_ABI __policy_func() : __policy_(__policy::__create_empty()) {}
639 template <class _Fp, class _Alloc>
640 _LIBCPP_HIDE_FROM_ABI __policy_func(_Fp&& __f, const _Alloc& __a) : __policy_(__policy::__create_empty()) {
641 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
642 typedef allocator_traits<_Alloc> __alloc_traits;
643 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
645 if (__function::__not_null(__f)) {
646 __invoker_ = __invoker::template __create<_Fun>();
647 __policy_ = __policy::__create<_Fun>();
649 _FunAlloc __af(__a);
650 if (__use_small_storage<_Fun>()) {
651 ::new ((void*)&__buf_.__small) _Fun(std::move(__f), _Alloc(__af));
652 } else {
653 typedef __allocator_destructor<_FunAlloc> _Dp;
654 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
655 ::new ((void*)__hold.get()) _Fun(std::move(__f), _Alloc(__af));
656 __buf_.__large = __hold.release();
661 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __policy_func>::value, int> = 0>
662 _LIBCPP_HIDE_FROM_ABI explicit __policy_func(_Fp&& __f) : __policy_(__policy::__create_empty()) {
663 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
665 if (__function::__not_null(__f)) {
666 __invoker_ = __invoker::template __create<_Fun>();
667 __policy_ = __policy::__create<_Fun>();
668 if (__use_small_storage<_Fun>()) {
669 ::new ((void*)&__buf_.__small) _Fun(std::move(__f));
670 } else {
671 __builtin_new_allocator::__holder_t __hold = __builtin_new_allocator::__allocate_type<_Fun>(1);
672 __buf_.__large = ::new ((void*)__hold.get()) _Fun(std::move(__f));
673 (void)__hold.release();
678 _LIBCPP_HIDE_FROM_ABI __policy_func(const __policy_func& __f)
679 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), __policy_(__f.__policy_) {
680 if (__policy_->__clone)
681 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
684 _LIBCPP_HIDE_FROM_ABI __policy_func(__policy_func&& __f)
685 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), __policy_(__f.__policy_) {
686 if (__policy_->__destroy) {
687 __f.__policy_ = __policy::__create_empty();
688 __f.__invoker_ = __invoker();
692 _LIBCPP_HIDE_FROM_ABI ~__policy_func() {
693 if (__policy_->__destroy)
694 __policy_->__destroy(__buf_.__large);
697 _LIBCPP_HIDE_FROM_ABI __policy_func& operator=(__policy_func&& __f) {
698 *this = nullptr;
699 __buf_ = __f.__buf_;
700 __invoker_ = __f.__invoker_;
701 __policy_ = __f.__policy_;
702 __f.__policy_ = __policy::__create_empty();
703 __f.__invoker_ = __invoker();
704 return *this;
707 _LIBCPP_HIDE_FROM_ABI __policy_func& operator=(nullptr_t) {
708 const __policy* __p = __policy_;
709 __policy_ = __policy::__create_empty();
710 __invoker_ = __invoker();
711 if (__p->__destroy)
712 __p->__destroy(__buf_.__large);
713 return *this;
716 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {
717 return __invoker_.__call_(std::addressof(__buf_), std::forward<_ArgTypes>(__args)...);
720 _LIBCPP_HIDE_FROM_ABI void swap(__policy_func& __f) {
721 std::swap(__invoker_, __f.__invoker_);
722 std::swap(__policy_, __f.__policy_);
723 std::swap(__buf_, __f.__buf_);
726 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return !__policy_->__is_null; }
728 # if _LIBCPP_HAS_RTTI
729 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT { return *__policy_->__type_info; }
731 template <typename _Tp>
732 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {
733 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
734 return nullptr;
735 if (__policy_->__clone) // Out of line storage.
736 return reinterpret_cast<const _Tp*>(__buf_.__large);
737 else
738 return reinterpret_cast<const _Tp*>(&__buf_.__small);
740 # endif // _LIBCPP_HAS_RTTI
743 # if _LIBCPP_HAS_BLOCKS_RUNTIME
745 extern "C" void* _Block_copy(const void*);
746 extern "C" void _Block_release(const void*);
748 template <class _Rp1, class... _ArgTypes1, class _Alloc, class _Rp, class... _ArgTypes>
749 class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {
750 typedef _Rp1 (^__block_type)(_ArgTypes1...);
751 __block_type __f_;
753 public:
754 _LIBCPP_HIDE_FROM_ABI explicit __func(__block_type const& __f)
755 # if _LIBCPP_HAS_OBJC_ARC
756 : __f_(__f)
757 # else
758 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
759 # endif
763 // [TODO] add && to save on a retain
765 _LIBCPP_HIDE_FROM_ABI explicit __func(__block_type __f, const _Alloc& /* unused */)
766 # if _LIBCPP_HAS_OBJC_ARC
767 : __f_(__f)
768 # else
769 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
770 # endif
774 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const {
775 _LIBCPP_ASSERT_INTERNAL(
776 false,
777 "Block pointers are just pointers, so they should always fit into "
778 "std::function's small buffer optimization. This function should "
779 "never be invoked.");
780 return nullptr;
783 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
784 ::new ((void*)__p) __func(__f_);
787 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT {
788 # if !_LIBCPP_HAS_OBJC_ARC
789 if (__f_)
790 _Block_release(__f_);
791 # endif
792 __f_ = 0;
795 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT {
796 _LIBCPP_ASSERT_INTERNAL(
797 false,
798 "Block pointers are just pointers, so they should always fit into "
799 "std::function's small buffer optimization. This function should "
800 "never be invoked.");
803 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg) {
804 return std::__invoke(__f_, std::forward<_ArgTypes>(__arg)...);
807 # if _LIBCPP_HAS_RTTI
808 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(type_info const& __ti) const _NOEXCEPT {
809 if (__ti == typeid(__func::__block_type))
810 return &__f_;
811 return (const void*)nullptr;
814 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT {
815 return typeid(__func::__block_type);
817 # endif // _LIBCPP_HAS_RTTI
820 # endif // _LIBCPP_HAS_EXTENSION_BLOCKS
822 } // namespace __function
824 template <class _Rp, class... _ArgTypes>
825 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
826 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
827 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)> {
828 # ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
829 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
830 # else
831 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
832 # endif
834 __func __f_;
836 template <class _Fp,
837 bool = _And< _IsNotSame<__remove_cvref_t<_Fp>, function>, __invokable<_Fp, _ArgTypes...> >::value>
838 struct __callable;
839 template <class _Fp>
840 struct __callable<_Fp, true> {
841 static const bool value =
842 is_void<_Rp>::value || __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type, _Rp>::value;
844 template <class _Fp>
845 struct __callable<_Fp, false> {
846 static const bool value = false;
849 template <class _Fp>
850 using _EnableIfLValueCallable = __enable_if_t<__callable<_Fp&>::value>;
852 public:
853 typedef _Rp result_type;
855 // construct/copy/destroy:
856 _LIBCPP_HIDE_FROM_ABI function() _NOEXCEPT {}
857 _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI function(nullptr_t) _NOEXCEPT {}
858 _LIBCPP_HIDE_FROM_ABI function(const function&);
859 _LIBCPP_HIDE_FROM_ABI function(function&&) _NOEXCEPT;
860 template <class _Fp, class = _EnableIfLValueCallable<_Fp>>
861 _LIBCPP_HIDE_FROM_ABI function(_Fp);
863 # if _LIBCPP_STD_VER <= 14
864 template <class _Alloc>
865 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
866 template <class _Alloc>
867 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
868 template <class _Alloc>
869 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, const function&);
870 template <class _Alloc>
871 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, function&&);
872 template <class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
873 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc& __a, _Fp __f);
874 # endif
876 _LIBCPP_HIDE_FROM_ABI function& operator=(const function&);
877 _LIBCPP_HIDE_FROM_ABI function& operator=(function&&) _NOEXCEPT;
878 _LIBCPP_HIDE_FROM_ABI function& operator=(nullptr_t) _NOEXCEPT;
879 template <class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>>
880 _LIBCPP_HIDE_FROM_ABI function& operator=(_Fp&&);
882 _LIBCPP_HIDE_FROM_ABI ~function();
884 // function modifiers:
885 _LIBCPP_HIDE_FROM_ABI void swap(function&) _NOEXCEPT;
887 # if _LIBCPP_STD_VER <= 14
888 template <class _Fp, class _Alloc>
889 _LIBCPP_HIDE_FROM_ABI void assign(_Fp&& __f, const _Alloc& __a) {
890 function(allocator_arg, __a, std::forward<_Fp>(__f)).swap(*this);
892 # endif
894 // function capacity:
895 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return static_cast<bool>(__f_); }
897 // deleted overloads close possible hole in the type system
898 template <class _R2, class... _ArgTypes2>
899 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
900 # if _LIBCPP_STD_VER <= 17
901 template <class _R2, class... _ArgTypes2>
902 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
903 # endif
905 public:
906 // function invocation:
907 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
909 # if _LIBCPP_HAS_RTTI
910 // function target access:
911 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT;
912 template <typename _Tp>
913 _LIBCPP_HIDE_FROM_ABI _Tp* target() _NOEXCEPT;
914 template <typename _Tp>
915 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT;
916 # endif // _LIBCPP_HAS_RTTI
919 # if _LIBCPP_STD_VER >= 17
920 template <class _Rp, class... _Ap>
921 function(_Rp (*)(_Ap...)) -> function<_Rp(_Ap...)>;
923 template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
924 function(_Fp) -> function<_Stripped>;
925 # endif // _LIBCPP_STD_VER >= 17
927 template <class _Rp, class... _ArgTypes>
928 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
930 # if _LIBCPP_STD_VER <= 14
931 template <class _Rp, class... _ArgTypes>
932 template <class _Alloc>
933 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, const function& __f) : __f_(__f.__f_) {}
934 # endif
936 template <class _Rp, class... _ArgTypes>
937 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT : __f_(std::move(__f.__f_)) {}
939 # if _LIBCPP_STD_VER <= 14
940 template <class _Rp, class... _ArgTypes>
941 template <class _Alloc>
942 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, function&& __f) : __f_(std::move(__f.__f_)) {}
943 # endif
945 template <class _Rp, class... _ArgTypes>
946 template <class _Fp, class>
947 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(std::move(__f)) {}
949 # if _LIBCPP_STD_VER <= 14
950 template <class _Rp, class... _ArgTypes>
951 template <class _Fp, class _Alloc, class>
952 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a, _Fp __f) : __f_(std::move(__f), __a) {}
953 # endif
955 template <class _Rp, class... _ArgTypes>
956 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(const function& __f) {
957 function(__f).swap(*this);
958 return *this;
961 template <class _Rp, class... _ArgTypes>
962 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT {
963 __f_ = std::move(__f.__f_);
964 return *this;
967 template <class _Rp, class... _ArgTypes>
968 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT {
969 __f_ = nullptr;
970 return *this;
973 template <class _Rp, class... _ArgTypes>
974 template <class _Fp, class>
975 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f) {
976 function(std::forward<_Fp>(__f)).swap(*this);
977 return *this;
980 template <class _Rp, class... _ArgTypes>
981 function<_Rp(_ArgTypes...)>::~function() {}
983 template <class _Rp, class... _ArgTypes>
984 void function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT {
985 __f_.swap(__f.__f_);
988 template <class _Rp, class... _ArgTypes>
989 _Rp function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
990 return __f_(std::forward<_ArgTypes>(__arg)...);
993 # if _LIBCPP_HAS_RTTI
995 template <class _Rp, class... _ArgTypes>
996 const std::type_info& function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT {
997 return __f_.target_type();
1000 template <class _Rp, class... _ArgTypes>
1001 template <typename _Tp>
1002 _Tp* function<_Rp(_ArgTypes...)>::target() _NOEXCEPT {
1003 return (_Tp*)(__f_.template target<_Tp>());
1006 template <class _Rp, class... _ArgTypes>
1007 template <typename _Tp>
1008 const _Tp* function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT {
1009 return __f_.template target<_Tp>();
1012 # endif // _LIBCPP_HAS_RTTI
1014 template <class _Rp, class... _ArgTypes>
1015 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {
1016 return !__f;
1019 # if _LIBCPP_STD_VER <= 17
1021 template <class _Rp, class... _ArgTypes>
1022 inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {
1023 return !__f;
1026 template <class _Rp, class... _ArgTypes>
1027 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {
1028 return (bool)__f;
1031 template <class _Rp, class... _ArgTypes>
1032 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {
1033 return (bool)__f;
1036 # endif // _LIBCPP_STD_VER <= 17
1038 template <class _Rp, class... _ArgTypes>
1039 inline _LIBCPP_HIDE_FROM_ABI void swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1040 return __x.swap(__y);
1043 _LIBCPP_END_NAMESPACE_STD
1045 #endif // _LIBCPP_CXX03_LANG
1047 _LIBCPP_POP_MACROS
1049 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H