function.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. #ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H
  10. #define _LIBCPP___FUNCTIONAL_FUNCTION_H
  11. #include <__assert>
  12. #include <__config>
  13. #include <__exception/exception.h>
  14. #include <__functional/binary_function.h>
  15. #include <__functional/invoke.h>
  16. #include <__functional/unary_function.h>
  17. #include <__iterator/iterator_traits.h>
  18. #include <__memory/addressof.h>
  19. #include <__memory/allocator.h>
  20. #include <__memory/allocator_destructor.h>
  21. #include <__memory/allocator_traits.h>
  22. #include <__memory/builtin_new_allocator.h>
  23. #include <__memory/compressed_pair.h>
  24. #include <__memory/unique_ptr.h>
  25. #include <__type_traits/aligned_storage.h>
  26. #include <__type_traits/decay.h>
  27. #include <__type_traits/is_core_convertible.h>
  28. #include <__type_traits/is_scalar.h>
  29. #include <__type_traits/is_trivially_copy_constructible.h>
  30. #include <__type_traits/is_trivially_destructible.h>
  31. #include <__type_traits/is_void.h>
  32. #include <__type_traits/strip_signature.h>
  33. #include <__utility/forward.h>
  34. #include <__utility/move.h>
  35. #include <__utility/piecewise_construct.h>
  36. #include <__utility/swap.h>
  37. #include <__verbose_abort>
  38. #include <new>
  39. #include <tuple>
  40. #include <typeinfo>
  41. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  42. # pragma GCC system_header
  43. #endif
  44. #ifndef _LIBCPP_CXX03_LANG
  45. _LIBCPP_BEGIN_NAMESPACE_STD
  46. // bad_function_call
  47. _LIBCPP_DIAGNOSTIC_PUSH
  48. _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
  49. class _LIBCPP_EXPORTED_FROM_ABI bad_function_call
  50. : public exception
  51. {
  52. public:
  53. _LIBCPP_HIDE_FROM_ABI bad_function_call() _NOEXCEPT = default;
  54. _LIBCPP_HIDE_FROM_ABI bad_function_call(const bad_function_call&) _NOEXCEPT = default;
  55. _LIBCPP_HIDE_FROM_ABI bad_function_call& operator=(const bad_function_call&) _NOEXCEPT = default;
  56. // Note that when a key function is not used, every translation unit that uses
  57. // bad_function_call will end up containing a weak definition of the vtable and
  58. // typeinfo.
  59. #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
  60. ~bad_function_call() _NOEXCEPT override;
  61. #else
  62. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_function_call() _NOEXCEPT override {}
  63. #endif
  64. #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
  65. const char* what() const _NOEXCEPT override;
  66. #endif
  67. };
  68. _LIBCPP_DIAGNOSTIC_POP
  69. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  70. void __throw_bad_function_call()
  71. {
  72. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  73. throw bad_function_call();
  74. #else
  75. _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");
  76. #endif
  77. }
  78. template<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined
  79. namespace __function
  80. {
  81. template<class _Rp>
  82. struct __maybe_derive_from_unary_function
  83. {
  84. };
  85. template<class _Rp, class _A1>
  86. struct __maybe_derive_from_unary_function<_Rp(_A1)>
  87. : public __unary_function<_A1, _Rp>
  88. {
  89. };
  90. template<class _Rp>
  91. struct __maybe_derive_from_binary_function
  92. {
  93. };
  94. template<class _Rp, class _A1, class _A2>
  95. struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
  96. : public __binary_function<_A1, _A2, _Rp>
  97. {
  98. };
  99. template <class _Fp>
  100. _LIBCPP_INLINE_VISIBILITY
  101. bool __not_null(_Fp const&) { return true; }
  102. template <class _Fp>
  103. _LIBCPP_INLINE_VISIBILITY
  104. bool __not_null(_Fp* __ptr) { return __ptr; }
  105. template <class _Ret, class _Class>
  106. _LIBCPP_INLINE_VISIBILITY
  107. bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
  108. template <class _Fp>
  109. _LIBCPP_INLINE_VISIBILITY
  110. bool __not_null(function<_Fp> const& __f) { return !!__f; }
  111. #ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
  112. template <class _Rp, class ..._Args>
  113. _LIBCPP_INLINE_VISIBILITY
  114. bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
  115. #endif
  116. } // namespace __function
  117. namespace __function {
  118. // __alloc_func holds a functor and an allocator.
  119. template <class _Fp, class _Ap, class _FB> class __alloc_func;
  120. template <class _Fp, class _FB>
  121. class __default_alloc_func;
  122. template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
  123. class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
  124. {
  125. __compressed_pair<_Fp, _Ap> __f_;
  126. public:
  127. typedef _LIBCPP_NODEBUG _Fp _Target;
  128. typedef _LIBCPP_NODEBUG _Ap _Alloc;
  129. _LIBCPP_INLINE_VISIBILITY
  130. const _Target& __target() const { return __f_.first(); }
  131. // WIN32 APIs may define __allocator, so use __get_allocator instead.
  132. _LIBCPP_INLINE_VISIBILITY
  133. const _Alloc& __get_allocator() const { return __f_.second(); }
  134. _LIBCPP_INLINE_VISIBILITY
  135. explicit __alloc_func(_Target&& __f)
  136. : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
  137. _VSTD::forward_as_tuple())
  138. {
  139. }
  140. _LIBCPP_INLINE_VISIBILITY
  141. explicit __alloc_func(const _Target& __f, const _Alloc& __a)
  142. : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
  143. _VSTD::forward_as_tuple(__a))
  144. {
  145. }
  146. _LIBCPP_INLINE_VISIBILITY
  147. explicit __alloc_func(const _Target& __f, _Alloc&& __a)
  148. : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
  149. _VSTD::forward_as_tuple(_VSTD::move(__a)))
  150. {
  151. }
  152. _LIBCPP_INLINE_VISIBILITY
  153. explicit __alloc_func(_Target&& __f, _Alloc&& __a)
  154. : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
  155. _VSTD::forward_as_tuple(_VSTD::move(__a)))
  156. {
  157. }
  158. _LIBCPP_INLINE_VISIBILITY
  159. _Rp operator()(_ArgTypes&&... __arg)
  160. {
  161. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  162. return _Invoker::__call(__f_.first(),
  163. _VSTD::forward<_ArgTypes>(__arg)...);
  164. }
  165. _LIBCPP_INLINE_VISIBILITY
  166. __alloc_func* __clone() const
  167. {
  168. typedef allocator_traits<_Alloc> __alloc_traits;
  169. typedef __rebind_alloc<__alloc_traits, __alloc_func> _AA;
  170. _AA __a(__f_.second());
  171. typedef __allocator_destructor<_AA> _Dp;
  172. unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  173. ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
  174. return __hold.release();
  175. }
  176. _LIBCPP_INLINE_VISIBILITY
  177. void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
  178. _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__alloc_func* __f) {
  179. typedef allocator_traits<_Alloc> __alloc_traits;
  180. typedef __rebind_alloc<__alloc_traits, __alloc_func> _FunAlloc;
  181. _FunAlloc __a(__f->__get_allocator());
  182. __f->destroy();
  183. __a.deallocate(__f, 1);
  184. }
  185. };
  186. template <class _Fp, class _Rp, class... _ArgTypes>
  187. class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
  188. _Fp __f_;
  189. public:
  190. typedef _LIBCPP_NODEBUG _Fp _Target;
  191. _LIBCPP_INLINE_VISIBILITY
  192. const _Target& __target() const { return __f_; }
  193. _LIBCPP_INLINE_VISIBILITY
  194. explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
  195. _LIBCPP_INLINE_VISIBILITY
  196. explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
  197. _LIBCPP_INLINE_VISIBILITY
  198. _Rp operator()(_ArgTypes&&... __arg) {
  199. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  200. return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
  201. }
  202. _LIBCPP_INLINE_VISIBILITY
  203. __default_alloc_func* __clone() const {
  204. __builtin_new_allocator::__holder_t __hold =
  205. __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
  206. __default_alloc_func* __res =
  207. ::new ((void*)__hold.get()) __default_alloc_func(__f_);
  208. (void)__hold.release();
  209. return __res;
  210. }
  211. _LIBCPP_INLINE_VISIBILITY
  212. void destroy() _NOEXCEPT { __f_.~_Target(); }
  213. _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__default_alloc_func* __f) {
  214. __f->destroy();
  215. __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
  216. }
  217. };
  218. // __base provides an abstract interface for copyable functors.
  219. template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
  220. template<class _Rp, class ..._ArgTypes>
  221. class __base<_Rp(_ArgTypes...)>
  222. {
  223. __base(const __base&);
  224. __base& operator=(const __base&);
  225. public:
  226. _LIBCPP_INLINE_VISIBILITY __base() {}
  227. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__base() {}
  228. virtual __base* __clone() const = 0;
  229. virtual void __clone(__base*) const = 0;
  230. virtual void destroy() _NOEXCEPT = 0;
  231. virtual void destroy_deallocate() _NOEXCEPT = 0;
  232. virtual _Rp operator()(_ArgTypes&& ...) = 0;
  233. #ifndef _LIBCPP_HAS_NO_RTTI
  234. virtual const void* target(const type_info&) const _NOEXCEPT = 0;
  235. virtual const std::type_info& target_type() const _NOEXCEPT = 0;
  236. #endif // _LIBCPP_HAS_NO_RTTI
  237. };
  238. // __func implements __base for a given functor type.
  239. template<class _FD, class _Alloc, class _FB> class __func;
  240. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  241. class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
  242. : public __base<_Rp(_ArgTypes...)>
  243. {
  244. __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
  245. public:
  246. _LIBCPP_INLINE_VISIBILITY
  247. explicit __func(_Fp&& __f)
  248. : __f_(_VSTD::move(__f)) {}
  249. _LIBCPP_INLINE_VISIBILITY
  250. explicit __func(const _Fp& __f, const _Alloc& __a)
  251. : __f_(__f, __a) {}
  252. _LIBCPP_INLINE_VISIBILITY
  253. explicit __func(const _Fp& __f, _Alloc&& __a)
  254. : __f_(__f, _VSTD::move(__a)) {}
  255. _LIBCPP_INLINE_VISIBILITY
  256. explicit __func(_Fp&& __f, _Alloc&& __a)
  257. : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
  258. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const;
  259. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
  260. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT;
  261. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT;
  262. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg);
  263. #ifndef _LIBCPP_HAS_NO_RTTI
  264. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(const type_info&) const _NOEXCEPT;
  265. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT;
  266. #endif // _LIBCPP_HAS_NO_RTTI
  267. };
  268. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  269. __base<_Rp(_ArgTypes...)>*
  270. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
  271. {
  272. typedef allocator_traits<_Alloc> __alloc_traits;
  273. typedef __rebind_alloc<__alloc_traits, __func> _Ap;
  274. _Ap __a(__f_.__get_allocator());
  275. typedef __allocator_destructor<_Ap> _Dp;
  276. unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  277. ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
  278. return __hold.release();
  279. }
  280. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  281. void
  282. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
  283. {
  284. ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
  285. }
  286. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  287. void
  288. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
  289. {
  290. __f_.destroy();
  291. }
  292. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  293. void
  294. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
  295. {
  296. typedef allocator_traits<_Alloc> __alloc_traits;
  297. typedef __rebind_alloc<__alloc_traits, __func> _Ap;
  298. _Ap __a(__f_.__get_allocator());
  299. __f_.destroy();
  300. __a.deallocate(this, 1);
  301. }
  302. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  303. _Rp
  304. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
  305. {
  306. return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  307. }
  308. #ifndef _LIBCPP_HAS_NO_RTTI
  309. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  310. const void*
  311. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
  312. {
  313. if (__ti == typeid(_Fp))
  314. return _VSTD::addressof(__f_.__target());
  315. return nullptr;
  316. }
  317. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  318. const std::type_info&
  319. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
  320. {
  321. return typeid(_Fp);
  322. }
  323. #endif // _LIBCPP_HAS_NO_RTTI
  324. // __value_func creates a value-type from a __func.
  325. template <class _Fp> class __value_func;
  326. template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
  327. {
  328. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  329. typename aligned_storage<3 * sizeof(void*)>::type __buf_;
  330. _LIBCPP_SUPPRESS_DEPRECATED_POP
  331. typedef __base<_Rp(_ArgTypes...)> __func;
  332. __func* __f_;
  333. _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI static __func* __as_base(void* __p)
  334. {
  335. return reinterpret_cast<__func*>(__p);
  336. }
  337. public:
  338. _LIBCPP_INLINE_VISIBILITY
  339. __value_func() _NOEXCEPT : __f_(nullptr) {}
  340. template <class _Fp, class _Alloc>
  341. _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
  342. : __f_(nullptr)
  343. {
  344. typedef allocator_traits<_Alloc> __alloc_traits;
  345. typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
  346. typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
  347. if (__function::__not_null(__f))
  348. {
  349. _FunAlloc __af(__a);
  350. if (sizeof(_Fun) <= sizeof(__buf_) &&
  351. is_nothrow_copy_constructible<_Fp>::value &&
  352. is_nothrow_copy_constructible<_FunAlloc>::value)
  353. {
  354. __f_ =
  355. ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
  356. }
  357. else
  358. {
  359. typedef __allocator_destructor<_FunAlloc> _Dp;
  360. unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
  361. ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
  362. __f_ = __hold.release();
  363. }
  364. }
  365. }
  366. template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __value_func>::value, int> = 0>
  367. _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
  368. : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
  369. _LIBCPP_INLINE_VISIBILITY
  370. __value_func(const __value_func& __f)
  371. {
  372. if (__f.__f_ == nullptr)
  373. __f_ = nullptr;
  374. else if ((void*)__f.__f_ == &__f.__buf_)
  375. {
  376. __f_ = __as_base(&__buf_);
  377. __f.__f_->__clone(__f_);
  378. }
  379. else
  380. __f_ = __f.__f_->__clone();
  381. }
  382. _LIBCPP_INLINE_VISIBILITY
  383. __value_func(__value_func&& __f) _NOEXCEPT
  384. {
  385. if (__f.__f_ == nullptr)
  386. __f_ = nullptr;
  387. else if ((void*)__f.__f_ == &__f.__buf_)
  388. {
  389. __f_ = __as_base(&__buf_);
  390. __f.__f_->__clone(__f_);
  391. }
  392. else
  393. {
  394. __f_ = __f.__f_;
  395. __f.__f_ = nullptr;
  396. }
  397. }
  398. _LIBCPP_INLINE_VISIBILITY
  399. ~__value_func()
  400. {
  401. if ((void*)__f_ == &__buf_)
  402. __f_->destroy();
  403. else if (__f_)
  404. __f_->destroy_deallocate();
  405. }
  406. _LIBCPP_INLINE_VISIBILITY
  407. __value_func& operator=(__value_func&& __f)
  408. {
  409. *this = nullptr;
  410. if (__f.__f_ == nullptr)
  411. __f_ = nullptr;
  412. else if ((void*)__f.__f_ == &__f.__buf_)
  413. {
  414. __f_ = __as_base(&__buf_);
  415. __f.__f_->__clone(__f_);
  416. }
  417. else
  418. {
  419. __f_ = __f.__f_;
  420. __f.__f_ = nullptr;
  421. }
  422. return *this;
  423. }
  424. _LIBCPP_INLINE_VISIBILITY
  425. __value_func& operator=(nullptr_t)
  426. {
  427. __func* __f = __f_;
  428. __f_ = nullptr;
  429. if ((void*)__f == &__buf_)
  430. __f->destroy();
  431. else if (__f)
  432. __f->destroy_deallocate();
  433. return *this;
  434. }
  435. _LIBCPP_INLINE_VISIBILITY
  436. _Rp operator()(_ArgTypes&&... __args) const
  437. {
  438. if (__f_ == nullptr)
  439. __throw_bad_function_call();
  440. return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
  441. }
  442. _LIBCPP_INLINE_VISIBILITY
  443. void swap(__value_func& __f) _NOEXCEPT
  444. {
  445. if (&__f == this)
  446. return;
  447. if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
  448. {
  449. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  450. typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
  451. _LIBCPP_SUPPRESS_DEPRECATED_POP
  452. __func* __t = __as_base(&__tempbuf);
  453. __f_->__clone(__t);
  454. __f_->destroy();
  455. __f_ = nullptr;
  456. __f.__f_->__clone(__as_base(&__buf_));
  457. __f.__f_->destroy();
  458. __f.__f_ = nullptr;
  459. __f_ = __as_base(&__buf_);
  460. __t->__clone(__as_base(&__f.__buf_));
  461. __t->destroy();
  462. __f.__f_ = __as_base(&__f.__buf_);
  463. }
  464. else if ((void*)__f_ == &__buf_)
  465. {
  466. __f_->__clone(__as_base(&__f.__buf_));
  467. __f_->destroy();
  468. __f_ = __f.__f_;
  469. __f.__f_ = __as_base(&__f.__buf_);
  470. }
  471. else if ((void*)__f.__f_ == &__f.__buf_)
  472. {
  473. __f.__f_->__clone(__as_base(&__buf_));
  474. __f.__f_->destroy();
  475. __f.__f_ = __f_;
  476. __f_ = __as_base(&__buf_);
  477. }
  478. else
  479. _VSTD::swap(__f_, __f.__f_);
  480. }
  481. _LIBCPP_INLINE_VISIBILITY
  482. explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
  483. #ifndef _LIBCPP_HAS_NO_RTTI
  484. _LIBCPP_INLINE_VISIBILITY
  485. const std::type_info& target_type() const _NOEXCEPT
  486. {
  487. if (__f_ == nullptr)
  488. return typeid(void);
  489. return __f_->target_type();
  490. }
  491. template <typename _Tp>
  492. _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
  493. {
  494. if (__f_ == nullptr)
  495. return nullptr;
  496. return (const _Tp*)__f_->target(typeid(_Tp));
  497. }
  498. #endif // _LIBCPP_HAS_NO_RTTI
  499. };
  500. // Storage for a functor object, to be used with __policy to manage copy and
  501. // destruction.
  502. union __policy_storage
  503. {
  504. mutable char __small[sizeof(void*) * 2];
  505. void* __large;
  506. };
  507. // True if _Fun can safely be held in __policy_storage.__small.
  508. template <typename _Fun>
  509. struct __use_small_storage
  510. : public integral_constant<
  511. bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
  512. _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
  513. is_trivially_copy_constructible<_Fun>::value &&
  514. is_trivially_destructible<_Fun>::value> {};
  515. // Policy contains information about how to copy, destroy, and move the
  516. // underlying functor. You can think of it as a vtable of sorts.
  517. struct __policy
  518. {
  519. // Used to copy or destroy __large values. null for trivial objects.
  520. void* (*const __clone)(const void*);
  521. void (*const __destroy)(void*);
  522. // True if this is the null policy (no value).
  523. const bool __is_null;
  524. // The target type. May be null if RTTI is disabled.
  525. const std::type_info* const __type_info;
  526. // Returns a pointer to a static policy object suitable for the functor
  527. // type.
  528. template <typename _Fun>
  529. _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
  530. {
  531. return __choose_policy<_Fun>(__use_small_storage<_Fun>());
  532. }
  533. _LIBCPP_INLINE_VISIBILITY
  534. static const __policy* __create_empty()
  535. {
  536. static const _LIBCPP_CONSTEXPR __policy __policy = {nullptr, nullptr,
  537. true,
  538. #ifndef _LIBCPP_HAS_NO_RTTI
  539. &typeid(void)
  540. #else
  541. nullptr
  542. #endif
  543. };
  544. return &__policy;
  545. }
  546. private:
  547. template <typename _Fun>
  548. _LIBCPP_HIDE_FROM_ABI static void* __large_clone(const void* __s)
  549. {
  550. const _Fun* __f = static_cast<const _Fun*>(__s);
  551. return __f->__clone();
  552. }
  553. template <typename _Fun>
  554. _LIBCPP_HIDE_FROM_ABI static void __large_destroy(void* __s) {
  555. _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
  556. }
  557. template <typename _Fun>
  558. _LIBCPP_INLINE_VISIBILITY static const __policy*
  559. __choose_policy(/* is_small = */ false_type) {
  560. static const _LIBCPP_CONSTEXPR __policy __policy = {
  561. &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
  562. #ifndef _LIBCPP_HAS_NO_RTTI
  563. &typeid(typename _Fun::_Target)
  564. #else
  565. nullptr
  566. #endif
  567. };
  568. return &__policy;
  569. }
  570. template <typename _Fun>
  571. _LIBCPP_INLINE_VISIBILITY static const __policy*
  572. __choose_policy(/* is_small = */ true_type)
  573. {
  574. static const _LIBCPP_CONSTEXPR __policy __policy = {
  575. nullptr, nullptr, false,
  576. #ifndef _LIBCPP_HAS_NO_RTTI
  577. &typeid(typename _Fun::_Target)
  578. #else
  579. nullptr
  580. #endif
  581. };
  582. return &__policy;
  583. }
  584. };
  585. // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
  586. // faster for types that can be passed in registers.
  587. template <typename _Tp>
  588. using __fast_forward = __conditional_t<is_scalar<_Tp>::value, _Tp, _Tp&&>;
  589. // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
  590. template <class _Fp> struct __policy_invoker;
  591. template <class _Rp, class... _ArgTypes>
  592. struct __policy_invoker<_Rp(_ArgTypes...)>
  593. {
  594. typedef _Rp (*__Call)(const __policy_storage*,
  595. __fast_forward<_ArgTypes>...);
  596. __Call __call_;
  597. // Creates an invoker that throws bad_function_call.
  598. _LIBCPP_INLINE_VISIBILITY
  599. __policy_invoker() : __call_(&__call_empty) {}
  600. // Creates an invoker that calls the given instance of __func.
  601. template <typename _Fun>
  602. _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
  603. {
  604. return __policy_invoker(&__call_impl<_Fun>);
  605. }
  606. private:
  607. _LIBCPP_INLINE_VISIBILITY
  608. explicit __policy_invoker(__Call __c) : __call_(__c) {}
  609. _LIBCPP_HIDE_FROM_ABI static _Rp __call_empty(const __policy_storage*,
  610. __fast_forward<_ArgTypes>...)
  611. {
  612. __throw_bad_function_call();
  613. }
  614. template <typename _Fun>
  615. _LIBCPP_HIDE_FROM_ABI static _Rp __call_impl(const __policy_storage* __buf,
  616. __fast_forward<_ArgTypes>... __args)
  617. {
  618. _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
  619. ? &__buf->__small
  620. : __buf->__large);
  621. return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
  622. }
  623. };
  624. // __policy_func uses a __policy and __policy_invoker to create a type-erased,
  625. // copyable functor.
  626. template <class _Fp> class __policy_func;
  627. template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
  628. {
  629. // Inline storage for small objects.
  630. __policy_storage __buf_;
  631. // Calls the value stored in __buf_. This could technically be part of
  632. // policy, but storing it here eliminates a level of indirection inside
  633. // operator().
  634. typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
  635. __invoker __invoker_;
  636. // The policy that describes how to move / copy / destroy __buf_. Never
  637. // null, even if the function is empty.
  638. const __policy* __policy_;
  639. public:
  640. _LIBCPP_INLINE_VISIBILITY
  641. __policy_func() : __policy_(__policy::__create_empty()) {}
  642. template <class _Fp, class _Alloc>
  643. _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
  644. : __policy_(__policy::__create_empty())
  645. {
  646. typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
  647. typedef allocator_traits<_Alloc> __alloc_traits;
  648. typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
  649. if (__function::__not_null(__f))
  650. {
  651. __invoker_ = __invoker::template __create<_Fun>();
  652. __policy_ = __policy::__create<_Fun>();
  653. _FunAlloc __af(__a);
  654. if (__use_small_storage<_Fun>())
  655. {
  656. ::new ((void*)&__buf_.__small)
  657. _Fun(_VSTD::move(__f), _Alloc(__af));
  658. }
  659. else
  660. {
  661. typedef __allocator_destructor<_FunAlloc> _Dp;
  662. unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
  663. ::new ((void*)__hold.get())
  664. _Fun(_VSTD::move(__f), _Alloc(__af));
  665. __buf_.__large = __hold.release();
  666. }
  667. }
  668. }
  669. template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __policy_func>::value, int> = 0>
  670. _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
  671. : __policy_(__policy::__create_empty()) {
  672. typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
  673. if (__function::__not_null(__f)) {
  674. __invoker_ = __invoker::template __create<_Fun>();
  675. __policy_ = __policy::__create<_Fun>();
  676. if (__use_small_storage<_Fun>()) {
  677. ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
  678. } else {
  679. __builtin_new_allocator::__holder_t __hold =
  680. __builtin_new_allocator::__allocate_type<_Fun>(1);
  681. __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
  682. (void)__hold.release();
  683. }
  684. }
  685. }
  686. _LIBCPP_INLINE_VISIBILITY
  687. __policy_func(const __policy_func& __f)
  688. : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
  689. __policy_(__f.__policy_)
  690. {
  691. if (__policy_->__clone)
  692. __buf_.__large = __policy_->__clone(__f.__buf_.__large);
  693. }
  694. _LIBCPP_INLINE_VISIBILITY
  695. __policy_func(__policy_func&& __f)
  696. : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
  697. __policy_(__f.__policy_)
  698. {
  699. if (__policy_->__destroy)
  700. {
  701. __f.__policy_ = __policy::__create_empty();
  702. __f.__invoker_ = __invoker();
  703. }
  704. }
  705. _LIBCPP_INLINE_VISIBILITY
  706. ~__policy_func()
  707. {
  708. if (__policy_->__destroy)
  709. __policy_->__destroy(__buf_.__large);
  710. }
  711. _LIBCPP_INLINE_VISIBILITY
  712. __policy_func& operator=(__policy_func&& __f)
  713. {
  714. *this = nullptr;
  715. __buf_ = __f.__buf_;
  716. __invoker_ = __f.__invoker_;
  717. __policy_ = __f.__policy_;
  718. __f.__policy_ = __policy::__create_empty();
  719. __f.__invoker_ = __invoker();
  720. return *this;
  721. }
  722. _LIBCPP_INLINE_VISIBILITY
  723. __policy_func& operator=(nullptr_t)
  724. {
  725. const __policy* __p = __policy_;
  726. __policy_ = __policy::__create_empty();
  727. __invoker_ = __invoker();
  728. if (__p->__destroy)
  729. __p->__destroy(__buf_.__large);
  730. return *this;
  731. }
  732. _LIBCPP_INLINE_VISIBILITY
  733. _Rp operator()(_ArgTypes&&... __args) const
  734. {
  735. return __invoker_.__call_(_VSTD::addressof(__buf_),
  736. _VSTD::forward<_ArgTypes>(__args)...);
  737. }
  738. _LIBCPP_INLINE_VISIBILITY
  739. void swap(__policy_func& __f)
  740. {
  741. _VSTD::swap(__invoker_, __f.__invoker_);
  742. _VSTD::swap(__policy_, __f.__policy_);
  743. _VSTD::swap(__buf_, __f.__buf_);
  744. }
  745. _LIBCPP_INLINE_VISIBILITY
  746. explicit operator bool() const _NOEXCEPT
  747. {
  748. return !__policy_->__is_null;
  749. }
  750. #ifndef _LIBCPP_HAS_NO_RTTI
  751. _LIBCPP_INLINE_VISIBILITY
  752. const std::type_info& target_type() const _NOEXCEPT
  753. {
  754. return *__policy_->__type_info;
  755. }
  756. template <typename _Tp>
  757. _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
  758. {
  759. if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
  760. return nullptr;
  761. if (__policy_->__clone) // Out of line storage.
  762. return reinterpret_cast<const _Tp*>(__buf_.__large);
  763. else
  764. return reinterpret_cast<const _Tp*>(&__buf_.__small);
  765. }
  766. #endif // _LIBCPP_HAS_NO_RTTI
  767. };
  768. #if defined(_LIBCPP_HAS_BLOCKS_RUNTIME)
  769. extern "C" void *_Block_copy(const void *);
  770. extern "C" void _Block_release(const void *);
  771. template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
  772. class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
  773. : public __base<_Rp(_ArgTypes...)>
  774. {
  775. typedef _Rp1(^__block_type)(_ArgTypes1...);
  776. __block_type __f_;
  777. public:
  778. _LIBCPP_INLINE_VISIBILITY
  779. explicit __func(__block_type const& __f)
  780. #ifdef _LIBCPP_HAS_OBJC_ARC
  781. : __f_(__f)
  782. #else
  783. : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
  784. #endif
  785. { }
  786. // [TODO] add && to save on a retain
  787. _LIBCPP_INLINE_VISIBILITY
  788. explicit __func(__block_type __f, const _Alloc& /* unused */)
  789. #ifdef _LIBCPP_HAS_OBJC_ARC
  790. : __f_(__f)
  791. #else
  792. : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
  793. #endif
  794. { }
  795. virtual __base<_Rp(_ArgTypes...)>* __clone() const {
  796. _LIBCPP_ASSERT_INTERNAL(false,
  797. "Block pointers are just pointers, so they should always fit into "
  798. "std::function's small buffer optimization. This function should "
  799. "never be invoked.");
  800. return nullptr;
  801. }
  802. virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
  803. ::new ((void*)__p) __func(__f_);
  804. }
  805. virtual void destroy() _NOEXCEPT {
  806. #ifndef _LIBCPP_HAS_OBJC_ARC
  807. if (__f_)
  808. _Block_release(__f_);
  809. #endif
  810. __f_ = 0;
  811. }
  812. virtual void destroy_deallocate() _NOEXCEPT {
  813. _LIBCPP_ASSERT_INTERNAL(false,
  814. "Block pointers are just pointers, so they should always fit into "
  815. "std::function's small buffer optimization. This function should "
  816. "never be invoked.");
  817. }
  818. virtual _Rp operator()(_ArgTypes&& ... __arg) {
  819. return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
  820. }
  821. #ifndef _LIBCPP_HAS_NO_RTTI
  822. virtual const void* target(type_info const& __ti) const _NOEXCEPT {
  823. if (__ti == typeid(__func::__block_type))
  824. return &__f_;
  825. return (const void*)nullptr;
  826. }
  827. virtual const std::type_info& target_type() const _NOEXCEPT {
  828. return typeid(__func::__block_type);
  829. }
  830. #endif // _LIBCPP_HAS_NO_RTTI
  831. };
  832. #endif // _LIBCPP_HAS_EXTENSION_BLOCKS
  833. } // namespace __function
  834. template<class _Rp, class ..._ArgTypes>
  835. class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
  836. : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
  837. public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
  838. {
  839. #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
  840. typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
  841. #else
  842. typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
  843. #endif
  844. __func __f_;
  845. template <class _Fp, bool = _And<
  846. _IsNotSame<__remove_cvref_t<_Fp>, function>,
  847. __invokable<_Fp, _ArgTypes...>
  848. >::value>
  849. struct __callable;
  850. template <class _Fp>
  851. struct __callable<_Fp, true>
  852. {
  853. static const bool value = is_void<_Rp>::value ||
  854. __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
  855. _Rp>::value;
  856. };
  857. template <class _Fp>
  858. struct __callable<_Fp, false>
  859. {
  860. static const bool value = false;
  861. };
  862. template <class _Fp>
  863. using _EnableIfLValueCallable = __enable_if_t<__callable<_Fp&>::value>;
  864. public:
  865. typedef _Rp result_type;
  866. // construct/copy/destroy:
  867. _LIBCPP_INLINE_VISIBILITY
  868. function() _NOEXCEPT { }
  869. _LIBCPP_INLINE_VISIBILITY
  870. _LIBCPP_HIDE_FROM_ABI function(nullptr_t) _NOEXCEPT {}
  871. _LIBCPP_HIDE_FROM_ABI function(const function&);
  872. _LIBCPP_HIDE_FROM_ABI function(function&&) _NOEXCEPT;
  873. template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
  874. _LIBCPP_HIDE_FROM_ABI function(_Fp);
  875. #if _LIBCPP_STD_VER <= 14
  876. template<class _Alloc>
  877. _LIBCPP_INLINE_VISIBILITY
  878. function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
  879. template<class _Alloc>
  880. _LIBCPP_INLINE_VISIBILITY
  881. function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
  882. template<class _Alloc>
  883. _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, const function&);
  884. template<class _Alloc>
  885. _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, function&&);
  886. template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
  887. _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc& __a, _Fp __f);
  888. #endif
  889. _LIBCPP_HIDE_FROM_ABI function& operator=(const function&);
  890. _LIBCPP_HIDE_FROM_ABI function& operator=(function&&) _NOEXCEPT;
  891. _LIBCPP_HIDE_FROM_ABI function& operator=(nullptr_t) _NOEXCEPT;
  892. template<class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>>
  893. _LIBCPP_HIDE_FROM_ABI function& operator=(_Fp&&);
  894. _LIBCPP_HIDE_FROM_ABI ~function();
  895. // function modifiers:
  896. _LIBCPP_HIDE_FROM_ABI void swap(function&) _NOEXCEPT;
  897. #if _LIBCPP_STD_VER <= 14
  898. template<class _Fp, class _Alloc>
  899. _LIBCPP_INLINE_VISIBILITY
  900. void assign(_Fp&& __f, const _Alloc& __a)
  901. {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
  902. #endif
  903. // function capacity:
  904. _LIBCPP_INLINE_VISIBILITY
  905. explicit operator bool() const _NOEXCEPT {
  906. return static_cast<bool>(__f_);
  907. }
  908. // deleted overloads close possible hole in the type system
  909. template<class _R2, class... _ArgTypes2>
  910. bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
  911. #if _LIBCPP_STD_VER <= 17
  912. template<class _R2, class... _ArgTypes2>
  913. bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
  914. #endif
  915. public:
  916. // function invocation:
  917. _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
  918. #ifndef _LIBCPP_HAS_NO_RTTI
  919. // function target access:
  920. _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT;
  921. template <typename _Tp>
  922. _LIBCPP_HIDE_FROM_ABI _Tp* target() _NOEXCEPT;
  923. template <typename _Tp>
  924. _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT;
  925. #endif // _LIBCPP_HAS_NO_RTTI
  926. };
  927. #if _LIBCPP_STD_VER >= 17
  928. template<class _Rp, class ..._Ap>
  929. function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
  930. template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
  931. function(_Fp) -> function<_Stripped>;
  932. #endif // _LIBCPP_STD_VER >= 17
  933. template<class _Rp, class ..._ArgTypes>
  934. function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
  935. #if _LIBCPP_STD_VER <= 14
  936. template<class _Rp, class ..._ArgTypes>
  937. template <class _Alloc>
  938. function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
  939. const function& __f) : __f_(__f.__f_) {}
  940. #endif
  941. template <class _Rp, class... _ArgTypes>
  942. function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
  943. : __f_(_VSTD::move(__f.__f_)) {}
  944. #if _LIBCPP_STD_VER <= 14
  945. template<class _Rp, class ..._ArgTypes>
  946. template <class _Alloc>
  947. function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
  948. function&& __f)
  949. : __f_(_VSTD::move(__f.__f_)) {}
  950. #endif
  951. template <class _Rp, class... _ArgTypes>
  952. template <class _Fp, class>
  953. function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
  954. #if _LIBCPP_STD_VER <= 14
  955. template <class _Rp, class... _ArgTypes>
  956. template <class _Fp, class _Alloc, class>
  957. function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
  958. _Fp __f)
  959. : __f_(_VSTD::move(__f), __a) {}
  960. #endif
  961. template<class _Rp, class ..._ArgTypes>
  962. function<_Rp(_ArgTypes...)>&
  963. function<_Rp(_ArgTypes...)>::operator=(const function& __f)
  964. {
  965. function(__f).swap(*this);
  966. return *this;
  967. }
  968. template<class _Rp, class ..._ArgTypes>
  969. function<_Rp(_ArgTypes...)>&
  970. function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
  971. {
  972. __f_ = _VSTD::move(__f.__f_);
  973. return *this;
  974. }
  975. template<class _Rp, class ..._ArgTypes>
  976. function<_Rp(_ArgTypes...)>&
  977. function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
  978. {
  979. __f_ = nullptr;
  980. return *this;
  981. }
  982. template<class _Rp, class ..._ArgTypes>
  983. template <class _Fp, class>
  984. function<_Rp(_ArgTypes...)>&
  985. function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
  986. {
  987. function(_VSTD::forward<_Fp>(__f)).swap(*this);
  988. return *this;
  989. }
  990. template<class _Rp, class ..._ArgTypes>
  991. function<_Rp(_ArgTypes...)>::~function() {}
  992. template<class _Rp, class ..._ArgTypes>
  993. void
  994. function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
  995. {
  996. __f_.swap(__f.__f_);
  997. }
  998. template<class _Rp, class ..._ArgTypes>
  999. _Rp
  1000. function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
  1001. {
  1002. return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  1003. }
  1004. #ifndef _LIBCPP_HAS_NO_RTTI
  1005. template<class _Rp, class ..._ArgTypes>
  1006. const std::type_info&
  1007. function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
  1008. {
  1009. return __f_.target_type();
  1010. }
  1011. template<class _Rp, class ..._ArgTypes>
  1012. template <typename _Tp>
  1013. _Tp*
  1014. function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
  1015. {
  1016. return (_Tp*)(__f_.template target<_Tp>());
  1017. }
  1018. template<class _Rp, class ..._ArgTypes>
  1019. template <typename _Tp>
  1020. const _Tp*
  1021. function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
  1022. {
  1023. return __f_.template target<_Tp>();
  1024. }
  1025. #endif // _LIBCPP_HAS_NO_RTTI
  1026. template <class _Rp, class... _ArgTypes>
  1027. inline _LIBCPP_INLINE_VISIBILITY
  1028. bool
  1029. operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
  1030. #if _LIBCPP_STD_VER <= 17
  1031. template <class _Rp, class... _ArgTypes>
  1032. inline _LIBCPP_INLINE_VISIBILITY
  1033. bool
  1034. operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
  1035. template <class _Rp, class... _ArgTypes>
  1036. inline _LIBCPP_INLINE_VISIBILITY
  1037. bool
  1038. operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
  1039. template <class _Rp, class... _ArgTypes>
  1040. inline _LIBCPP_INLINE_VISIBILITY
  1041. bool
  1042. operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
  1043. #endif // _LIBCPP_STD_VER <= 17
  1044. template <class _Rp, class... _ArgTypes>
  1045. inline _LIBCPP_INLINE_VISIBILITY
  1046. void
  1047. swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
  1048. {return __x.swap(__y);}
  1049. _LIBCPP_END_NAMESPACE_STD
  1050. #endif // _LIBCPP_CXX03_LANG
  1051. #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H