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