function.h 35 KB

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