function.h 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824
  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 <__config>
  12. #include <__debug>
  13. #include <__functional/binary_function.h>
  14. #include <__functional/invoke.h>
  15. #include <__functional/unary_function.h>
  16. #include <__iterator/iterator_traits.h>
  17. #include <__memory/allocator_traits.h>
  18. #include <__memory/compressed_pair.h>
  19. #include <__memory/shared_ptr.h>
  20. #include <exception>
  21. #include <memory> // TODO: replace with <__memory/__builtin_new_allocator.h>
  22. #include <type_traits>
  23. #include <utility>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. #pragma GCC system_header
  26. #endif
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. // bad_function_call
  29. class _LIBCPP_EXCEPTION_ABI bad_function_call
  30. : public exception
  31. {
  32. public:
  33. // Note that when a key function is not used, every translation unit that uses
  34. // bad_function_call will end up containing a weak definition of the vtable and
  35. // typeinfo.
  36. #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
  37. virtual ~bad_function_call() _NOEXCEPT;
  38. #else
  39. virtual ~bad_function_call() _NOEXCEPT {}
  40. #endif
  41. #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
  42. virtual const char* what() const _NOEXCEPT;
  43. #endif
  44. };
  45. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  46. void __throw_bad_function_call()
  47. {
  48. #ifndef _LIBCPP_NO_EXCEPTIONS
  49. throw bad_function_call();
  50. #else
  51. _VSTD::abort();
  52. #endif
  53. }
  54. #if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
  55. # define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
  56. __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
  57. #else
  58. # define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
  59. #endif
  60. template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
  61. namespace __function
  62. {
  63. template<class _Rp>
  64. struct __maybe_derive_from_unary_function
  65. {
  66. };
  67. template<class _Rp, class _A1>
  68. struct __maybe_derive_from_unary_function<_Rp(_A1)>
  69. : public unary_function<_A1, _Rp>
  70. {
  71. };
  72. template<class _Rp>
  73. struct __maybe_derive_from_binary_function
  74. {
  75. };
  76. template<class _Rp, class _A1, class _A2>
  77. struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
  78. : public binary_function<_A1, _A2, _Rp>
  79. {
  80. };
  81. template <class _Fp>
  82. _LIBCPP_INLINE_VISIBILITY
  83. bool __not_null(_Fp const&) { return true; }
  84. template <class _Fp>
  85. _LIBCPP_INLINE_VISIBILITY
  86. bool __not_null(_Fp* __ptr) { return __ptr; }
  87. template <class _Ret, class _Class>
  88. _LIBCPP_INLINE_VISIBILITY
  89. bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
  90. template <class _Fp>
  91. _LIBCPP_INLINE_VISIBILITY
  92. bool __not_null(function<_Fp> const& __f) { return !!__f; }
  93. #ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
  94. template <class _Rp, class ..._Args>
  95. _LIBCPP_INLINE_VISIBILITY
  96. bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
  97. #endif
  98. } // namespace __function
  99. #ifndef _LIBCPP_CXX03_LANG
  100. namespace __function {
  101. // __alloc_func holds a functor and an allocator.
  102. template <class _Fp, class _Ap, class _FB> class __alloc_func;
  103. template <class _Fp, class _FB>
  104. class __default_alloc_func;
  105. template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
  106. class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
  107. {
  108. __compressed_pair<_Fp, _Ap> __f_;
  109. public:
  110. typedef _LIBCPP_NODEBUG _Fp _Target;
  111. typedef _LIBCPP_NODEBUG _Ap _Alloc;
  112. _LIBCPP_INLINE_VISIBILITY
  113. const _Target& __target() const { return __f_.first(); }
  114. // WIN32 APIs may define __allocator, so use __get_allocator instead.
  115. _LIBCPP_INLINE_VISIBILITY
  116. const _Alloc& __get_allocator() const { return __f_.second(); }
  117. _LIBCPP_INLINE_VISIBILITY
  118. explicit __alloc_func(_Target&& __f)
  119. : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
  120. _VSTD::forward_as_tuple())
  121. {
  122. }
  123. _LIBCPP_INLINE_VISIBILITY
  124. explicit __alloc_func(const _Target& __f, const _Alloc& __a)
  125. : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
  126. _VSTD::forward_as_tuple(__a))
  127. {
  128. }
  129. _LIBCPP_INLINE_VISIBILITY
  130. explicit __alloc_func(const _Target& __f, _Alloc&& __a)
  131. : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
  132. _VSTD::forward_as_tuple(_VSTD::move(__a)))
  133. {
  134. }
  135. _LIBCPP_INLINE_VISIBILITY
  136. explicit __alloc_func(_Target&& __f, _Alloc&& __a)
  137. : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
  138. _VSTD::forward_as_tuple(_VSTD::move(__a)))
  139. {
  140. }
  141. _LIBCPP_INLINE_VISIBILITY
  142. _Rp operator()(_ArgTypes&&... __arg)
  143. {
  144. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  145. return _Invoker::__call(__f_.first(),
  146. _VSTD::forward<_ArgTypes>(__arg)...);
  147. }
  148. _LIBCPP_INLINE_VISIBILITY
  149. __alloc_func* __clone() const
  150. {
  151. typedef allocator_traits<_Alloc> __alloc_traits;
  152. typedef
  153. typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
  154. _AA;
  155. _AA __a(__f_.second());
  156. typedef __allocator_destructor<_AA> _Dp;
  157. unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  158. ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
  159. return __hold.release();
  160. }
  161. _LIBCPP_INLINE_VISIBILITY
  162. void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
  163. static void __destroy_and_delete(__alloc_func* __f) {
  164. typedef allocator_traits<_Alloc> __alloc_traits;
  165. typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
  166. _FunAlloc;
  167. _FunAlloc __a(__f->__get_allocator());
  168. __f->destroy();
  169. __a.deallocate(__f, 1);
  170. }
  171. };
  172. template <class _Fp, class _Rp, class... _ArgTypes>
  173. class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
  174. _Fp __f_;
  175. public:
  176. typedef _LIBCPP_NODEBUG _Fp _Target;
  177. _LIBCPP_INLINE_VISIBILITY
  178. const _Target& __target() const { return __f_; }
  179. _LIBCPP_INLINE_VISIBILITY
  180. explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
  181. _LIBCPP_INLINE_VISIBILITY
  182. explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
  183. _LIBCPP_INLINE_VISIBILITY
  184. _Rp operator()(_ArgTypes&&... __arg) {
  185. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  186. return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
  187. }
  188. _LIBCPP_INLINE_VISIBILITY
  189. __default_alloc_func* __clone() const {
  190. __builtin_new_allocator::__holder_t __hold =
  191. __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
  192. __default_alloc_func* __res =
  193. ::new ((void*)__hold.get()) __default_alloc_func(__f_);
  194. (void)__hold.release();
  195. return __res;
  196. }
  197. _LIBCPP_INLINE_VISIBILITY
  198. void destroy() _NOEXCEPT { __f_.~_Target(); }
  199. static void __destroy_and_delete(__default_alloc_func* __f) {
  200. __f->destroy();
  201. __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
  202. }
  203. };
  204. // __base provides an abstract interface for copyable functors.
  205. template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
  206. template<class _Rp, class ..._ArgTypes>
  207. class __base<_Rp(_ArgTypes...)>
  208. {
  209. __base(const __base&);
  210. __base& operator=(const __base&);
  211. public:
  212. _LIBCPP_INLINE_VISIBILITY __base() {}
  213. _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
  214. virtual __base* __clone() const = 0;
  215. virtual void __clone(__base*) const = 0;
  216. virtual void destroy() _NOEXCEPT = 0;
  217. virtual void destroy_deallocate() _NOEXCEPT = 0;
  218. virtual _Rp operator()(_ArgTypes&& ...) = 0;
  219. #ifndef _LIBCPP_NO_RTTI
  220. virtual const void* target(const type_info&) const _NOEXCEPT = 0;
  221. virtual const std::type_info& target_type() const _NOEXCEPT = 0;
  222. #endif // _LIBCPP_NO_RTTI
  223. };
  224. // __func implements __base for a given functor type.
  225. template<class _FD, class _Alloc, class _FB> class __func;
  226. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  227. class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
  228. : public __base<_Rp(_ArgTypes...)>
  229. {
  230. __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
  231. public:
  232. _LIBCPP_INLINE_VISIBILITY
  233. explicit __func(_Fp&& __f)
  234. : __f_(_VSTD::move(__f)) {}
  235. _LIBCPP_INLINE_VISIBILITY
  236. explicit __func(const _Fp& __f, const _Alloc& __a)
  237. : __f_(__f, __a) {}
  238. _LIBCPP_INLINE_VISIBILITY
  239. explicit __func(const _Fp& __f, _Alloc&& __a)
  240. : __f_(__f, _VSTD::move(__a)) {}
  241. _LIBCPP_INLINE_VISIBILITY
  242. explicit __func(_Fp&& __f, _Alloc&& __a)
  243. : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
  244. virtual __base<_Rp(_ArgTypes...)>* __clone() const;
  245. virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
  246. virtual void destroy() _NOEXCEPT;
  247. virtual void destroy_deallocate() _NOEXCEPT;
  248. virtual _Rp operator()(_ArgTypes&&... __arg);
  249. #ifndef _LIBCPP_NO_RTTI
  250. virtual const void* target(const type_info&) const _NOEXCEPT;
  251. virtual const std::type_info& target_type() const _NOEXCEPT;
  252. #endif // _LIBCPP_NO_RTTI
  253. };
  254. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  255. __base<_Rp(_ArgTypes...)>*
  256. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
  257. {
  258. typedef allocator_traits<_Alloc> __alloc_traits;
  259. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  260. _Ap __a(__f_.__get_allocator());
  261. typedef __allocator_destructor<_Ap> _Dp;
  262. unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  263. ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
  264. return __hold.release();
  265. }
  266. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  267. void
  268. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
  269. {
  270. ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
  271. }
  272. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  273. void
  274. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
  275. {
  276. __f_.destroy();
  277. }
  278. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  279. void
  280. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
  281. {
  282. typedef allocator_traits<_Alloc> __alloc_traits;
  283. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  284. _Ap __a(__f_.__get_allocator());
  285. __f_.destroy();
  286. __a.deallocate(this, 1);
  287. }
  288. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  289. _Rp
  290. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
  291. {
  292. return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  293. }
  294. #ifndef _LIBCPP_NO_RTTI
  295. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  296. const void*
  297. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
  298. {
  299. if (__ti == typeid(_Fp))
  300. return &__f_.__target();
  301. return nullptr;
  302. }
  303. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  304. const std::type_info&
  305. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
  306. {
  307. return typeid(_Fp);
  308. }
  309. #endif // _LIBCPP_NO_RTTI
  310. // __value_func creates a value-type from a __func.
  311. template <class _Fp> class __value_func;
  312. template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
  313. {
  314. typename aligned_storage<3 * sizeof(void*)>::type __buf_;
  315. typedef __base<_Rp(_ArgTypes...)> __func;
  316. __func* __f_;
  317. _LIBCPP_NO_CFI static __func* __as_base(void* p)
  318. {
  319. return reinterpret_cast<__func*>(p);
  320. }
  321. public:
  322. _LIBCPP_INLINE_VISIBILITY
  323. __value_func() _NOEXCEPT : __f_(nullptr) {}
  324. template <class _Fp, class _Alloc>
  325. _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
  326. : __f_(nullptr)
  327. {
  328. typedef allocator_traits<_Alloc> __alloc_traits;
  329. typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
  330. typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
  331. _FunAlloc;
  332. if (__function::__not_null(__f))
  333. {
  334. _FunAlloc __af(__a);
  335. if (sizeof(_Fun) <= sizeof(__buf_) &&
  336. is_nothrow_copy_constructible<_Fp>::value &&
  337. is_nothrow_copy_constructible<_FunAlloc>::value)
  338. {
  339. __f_ =
  340. ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
  341. }
  342. else
  343. {
  344. typedef __allocator_destructor<_FunAlloc> _Dp;
  345. unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
  346. ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
  347. __f_ = __hold.release();
  348. }
  349. }
  350. }
  351. template <class _Fp,
  352. class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
  353. _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
  354. : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
  355. _LIBCPP_INLINE_VISIBILITY
  356. __value_func(const __value_func& __f)
  357. {
  358. if (__f.__f_ == nullptr)
  359. __f_ = nullptr;
  360. else if ((void*)__f.__f_ == &__f.__buf_)
  361. {
  362. __f_ = __as_base(&__buf_);
  363. __f.__f_->__clone(__f_);
  364. }
  365. else
  366. __f_ = __f.__f_->__clone();
  367. }
  368. _LIBCPP_INLINE_VISIBILITY
  369. __value_func(__value_func&& __f) _NOEXCEPT
  370. {
  371. if (__f.__f_ == nullptr)
  372. __f_ = nullptr;
  373. else if ((void*)__f.__f_ == &__f.__buf_)
  374. {
  375. __f_ = __as_base(&__buf_);
  376. __f.__f_->__clone(__f_);
  377. }
  378. else
  379. {
  380. __f_ = __f.__f_;
  381. __f.__f_ = nullptr;
  382. }
  383. }
  384. _LIBCPP_INLINE_VISIBILITY
  385. ~__value_func()
  386. {
  387. if ((void*)__f_ == &__buf_)
  388. __f_->destroy();
  389. else if (__f_)
  390. __f_->destroy_deallocate();
  391. }
  392. _LIBCPP_INLINE_VISIBILITY
  393. __value_func& operator=(__value_func&& __f)
  394. {
  395. *this = nullptr;
  396. if (__f.__f_ == nullptr)
  397. __f_ = nullptr;
  398. else if ((void*)__f.__f_ == &__f.__buf_)
  399. {
  400. __f_ = __as_base(&__buf_);
  401. __f.__f_->__clone(__f_);
  402. }
  403. else
  404. {
  405. __f_ = __f.__f_;
  406. __f.__f_ = nullptr;
  407. }
  408. return *this;
  409. }
  410. _LIBCPP_INLINE_VISIBILITY
  411. __value_func& operator=(nullptr_t)
  412. {
  413. __func* __f = __f_;
  414. __f_ = nullptr;
  415. if ((void*)__f == &__buf_)
  416. __f->destroy();
  417. else if (__f)
  418. __f->destroy_deallocate();
  419. return *this;
  420. }
  421. _LIBCPP_INLINE_VISIBILITY
  422. _Rp operator()(_ArgTypes&&... __args) const
  423. {
  424. if (__f_ == nullptr)
  425. __throw_bad_function_call();
  426. return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
  427. }
  428. _LIBCPP_INLINE_VISIBILITY
  429. void swap(__value_func& __f) _NOEXCEPT
  430. {
  431. if (&__f == this)
  432. return;
  433. if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
  434. {
  435. typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
  436. __func* __t = __as_base(&__tempbuf);
  437. __f_->__clone(__t);
  438. __f_->destroy();
  439. __f_ = nullptr;
  440. __f.__f_->__clone(__as_base(&__buf_));
  441. __f.__f_->destroy();
  442. __f.__f_ = nullptr;
  443. __f_ = __as_base(&__buf_);
  444. __t->__clone(__as_base(&__f.__buf_));
  445. __t->destroy();
  446. __f.__f_ = __as_base(&__f.__buf_);
  447. }
  448. else if ((void*)__f_ == &__buf_)
  449. {
  450. __f_->__clone(__as_base(&__f.__buf_));
  451. __f_->destroy();
  452. __f_ = __f.__f_;
  453. __f.__f_ = __as_base(&__f.__buf_);
  454. }
  455. else if ((void*)__f.__f_ == &__f.__buf_)
  456. {
  457. __f.__f_->__clone(__as_base(&__buf_));
  458. __f.__f_->destroy();
  459. __f.__f_ = __f_;
  460. __f_ = __as_base(&__buf_);
  461. }
  462. else
  463. _VSTD::swap(__f_, __f.__f_);
  464. }
  465. _LIBCPP_INLINE_VISIBILITY
  466. explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
  467. #ifndef _LIBCPP_NO_RTTI
  468. _LIBCPP_INLINE_VISIBILITY
  469. const std::type_info& target_type() const _NOEXCEPT
  470. {
  471. if (__f_ == nullptr)
  472. return typeid(void);
  473. return __f_->target_type();
  474. }
  475. template <typename _Tp>
  476. _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
  477. {
  478. if (__f_ == nullptr)
  479. return nullptr;
  480. return (const _Tp*)__f_->target(typeid(_Tp));
  481. }
  482. #endif // _LIBCPP_NO_RTTI
  483. };
  484. // Storage for a functor object, to be used with __policy to manage copy and
  485. // destruction.
  486. union __policy_storage
  487. {
  488. mutable char __small[sizeof(void*) * 2];
  489. void* __large;
  490. };
  491. // True if _Fun can safely be held in __policy_storage.__small.
  492. template <typename _Fun>
  493. struct __use_small_storage
  494. : public integral_constant<
  495. bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
  496. _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
  497. is_trivially_copy_constructible<_Fun>::value &&
  498. is_trivially_destructible<_Fun>::value> {};
  499. // Policy contains information about how to copy, destroy, and move the
  500. // underlying functor. You can think of it as a vtable of sorts.
  501. struct __policy
  502. {
  503. // Used to copy or destroy __large values. null for trivial objects.
  504. void* (*const __clone)(const void*);
  505. void (*const __destroy)(void*);
  506. // True if this is the null policy (no value).
  507. const bool __is_null;
  508. // The target type. May be null if RTTI is disabled.
  509. const std::type_info* const __type_info;
  510. // Returns a pointer to a static policy object suitable for the functor
  511. // type.
  512. template <typename _Fun>
  513. _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
  514. {
  515. return __choose_policy<_Fun>(__use_small_storage<_Fun>());
  516. }
  517. _LIBCPP_INLINE_VISIBILITY
  518. static const __policy* __create_empty()
  519. {
  520. static const /*_LIBCPP_CONSTEXPR*/ __policy __policy_ = {nullptr, nullptr,
  521. true,
  522. #ifndef _LIBCPP_NO_RTTI
  523. &typeid(void)
  524. #else
  525. nullptr
  526. #endif
  527. };
  528. return &__policy_;
  529. }
  530. private:
  531. template <typename _Fun> static void* __large_clone(const void* __s)
  532. {
  533. const _Fun* __f = static_cast<const _Fun*>(__s);
  534. return __f->__clone();
  535. }
  536. template <typename _Fun>
  537. static void __large_destroy(void* __s) {
  538. _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
  539. }
  540. template <typename _Fun>
  541. _LIBCPP_INLINE_VISIBILITY static const __policy*
  542. __choose_policy(/* is_small = */ false_type) {
  543. static const _LIBCPP_CONSTEXPR __policy __policy_ = {
  544. &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
  545. #ifndef _LIBCPP_NO_RTTI
  546. &typeid(typename _Fun::_Target)
  547. #else
  548. nullptr
  549. #endif
  550. };
  551. return &__policy_;
  552. }
  553. template <typename _Fun>
  554. _LIBCPP_INLINE_VISIBILITY static const __policy*
  555. __choose_policy(/* is_small = */ true_type)
  556. {
  557. static const _LIBCPP_CONSTEXPR __policy __policy_ = {
  558. nullptr, nullptr, false,
  559. #ifndef _LIBCPP_NO_RTTI
  560. &typeid(typename _Fun::_Target)
  561. #else
  562. nullptr
  563. #endif
  564. };
  565. return &__policy_;
  566. }
  567. };
  568. // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
  569. // faster for types that can be passed in registers.
  570. template <typename _Tp>
  571. using __fast_forward =
  572. typename conditional<is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
  573. // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
  574. template <class _Fp> struct __policy_invoker;
  575. template <class _Rp, class... _ArgTypes>
  576. struct __policy_invoker<_Rp(_ArgTypes...)>
  577. {
  578. typedef _Rp (*__Call)(const __policy_storage*,
  579. __fast_forward<_ArgTypes>...);
  580. __Call __call_;
  581. // Creates an invoker that throws bad_function_call.
  582. _LIBCPP_INLINE_VISIBILITY
  583. __policy_invoker() : __call_(&__call_empty) {}
  584. // Creates an invoker that calls the given instance of __func.
  585. template <typename _Fun>
  586. _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
  587. {
  588. return __policy_invoker(&__call_impl<_Fun>);
  589. }
  590. private:
  591. _LIBCPP_INLINE_VISIBILITY
  592. explicit __policy_invoker(__Call __c) : __call_(__c) {}
  593. static _Rp __call_empty(const __policy_storage*,
  594. __fast_forward<_ArgTypes>...)
  595. {
  596. __throw_bad_function_call();
  597. }
  598. template <typename _Fun>
  599. static _Rp __call_impl(const __policy_storage* __buf,
  600. __fast_forward<_ArgTypes>... __args)
  601. {
  602. _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
  603. ? &__buf->__small
  604. : __buf->__large);
  605. return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
  606. }
  607. };
  608. // __policy_func uses a __policy and __policy_invoker to create a type-erased,
  609. // copyable functor.
  610. template <class _Fp> class __policy_func;
  611. template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
  612. {
  613. // Inline storage for small objects.
  614. __policy_storage __buf_;
  615. // Calls the value stored in __buf_. This could technically be part of
  616. // policy, but storing it here eliminates a level of indirection inside
  617. // operator().
  618. typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
  619. __invoker __invoker_;
  620. // The policy that describes how to move / copy / destroy __buf_. Never
  621. // null, even if the function is empty.
  622. const __policy* __policy_;
  623. public:
  624. _LIBCPP_INLINE_VISIBILITY
  625. __policy_func() : __policy_(__policy::__create_empty()) {}
  626. template <class _Fp, class _Alloc>
  627. _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
  628. : __policy_(__policy::__create_empty())
  629. {
  630. typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
  631. typedef allocator_traits<_Alloc> __alloc_traits;
  632. typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
  633. _FunAlloc;
  634. if (__function::__not_null(__f))
  635. {
  636. __invoker_ = __invoker::template __create<_Fun>();
  637. __policy_ = __policy::__create<_Fun>();
  638. _FunAlloc __af(__a);
  639. if (__use_small_storage<_Fun>())
  640. {
  641. ::new ((void*)&__buf_.__small)
  642. _Fun(_VSTD::move(__f), _Alloc(__af));
  643. }
  644. else
  645. {
  646. typedef __allocator_destructor<_FunAlloc> _Dp;
  647. unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
  648. ::new ((void*)__hold.get())
  649. _Fun(_VSTD::move(__f), _Alloc(__af));
  650. __buf_.__large = __hold.release();
  651. }
  652. }
  653. }
  654. template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
  655. _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
  656. : __policy_(__policy::__create_empty()) {
  657. typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
  658. if (__function::__not_null(__f)) {
  659. __invoker_ = __invoker::template __create<_Fun>();
  660. __policy_ = __policy::__create<_Fun>();
  661. if (__use_small_storage<_Fun>()) {
  662. ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
  663. } else {
  664. __builtin_new_allocator::__holder_t __hold =
  665. __builtin_new_allocator::__allocate_type<_Fun>(1);
  666. __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
  667. (void)__hold.release();
  668. }
  669. }
  670. }
  671. _LIBCPP_INLINE_VISIBILITY
  672. __policy_func(const __policy_func& __f)
  673. : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
  674. __policy_(__f.__policy_)
  675. {
  676. if (__policy_->__clone)
  677. __buf_.__large = __policy_->__clone(__f.__buf_.__large);
  678. }
  679. _LIBCPP_INLINE_VISIBILITY
  680. __policy_func(__policy_func&& __f)
  681. : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
  682. __policy_(__f.__policy_)
  683. {
  684. if (__policy_->__destroy)
  685. {
  686. __f.__policy_ = __policy::__create_empty();
  687. __f.__invoker_ = __invoker();
  688. }
  689. }
  690. _LIBCPP_INLINE_VISIBILITY
  691. ~__policy_func()
  692. {
  693. if (__policy_->__destroy)
  694. __policy_->__destroy(__buf_.__large);
  695. }
  696. _LIBCPP_INLINE_VISIBILITY
  697. __policy_func& operator=(__policy_func&& __f)
  698. {
  699. *this = nullptr;
  700. __buf_ = __f.__buf_;
  701. __invoker_ = __f.__invoker_;
  702. __policy_ = __f.__policy_;
  703. __f.__policy_ = __policy::__create_empty();
  704. __f.__invoker_ = __invoker();
  705. return *this;
  706. }
  707. _LIBCPP_INLINE_VISIBILITY
  708. __policy_func& operator=(nullptr_t)
  709. {
  710. const __policy* __p = __policy_;
  711. __policy_ = __policy::__create_empty();
  712. __invoker_ = __invoker();
  713. if (__p->__destroy)
  714. __p->__destroy(__buf_.__large);
  715. return *this;
  716. }
  717. _LIBCPP_INLINE_VISIBILITY
  718. _Rp operator()(_ArgTypes&&... __args) const
  719. {
  720. return __invoker_.__call_(_VSTD::addressof(__buf_),
  721. _VSTD::forward<_ArgTypes>(__args)...);
  722. }
  723. _LIBCPP_INLINE_VISIBILITY
  724. void swap(__policy_func& __f)
  725. {
  726. _VSTD::swap(__invoker_, __f.__invoker_);
  727. _VSTD::swap(__policy_, __f.__policy_);
  728. _VSTD::swap(__buf_, __f.__buf_);
  729. }
  730. _LIBCPP_INLINE_VISIBILITY
  731. explicit operator bool() const _NOEXCEPT
  732. {
  733. return !__policy_->__is_null;
  734. }
  735. #ifndef _LIBCPP_NO_RTTI
  736. _LIBCPP_INLINE_VISIBILITY
  737. const std::type_info& target_type() const _NOEXCEPT
  738. {
  739. return *__policy_->__type_info;
  740. }
  741. template <typename _Tp>
  742. _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
  743. {
  744. if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
  745. return nullptr;
  746. if (__policy_->__clone) // Out of line storage.
  747. return reinterpret_cast<const _Tp*>(__buf_.__large);
  748. else
  749. return reinterpret_cast<const _Tp*>(&__buf_.__small);
  750. }
  751. #endif // _LIBCPP_NO_RTTI
  752. };
  753. #if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
  754. extern "C" void *_Block_copy(const void *);
  755. extern "C" void _Block_release(const void *);
  756. template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
  757. class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
  758. : public __base<_Rp(_ArgTypes...)>
  759. {
  760. typedef _Rp1(^__block_type)(_ArgTypes1...);
  761. __block_type __f_;
  762. public:
  763. _LIBCPP_INLINE_VISIBILITY
  764. explicit __func(__block_type const& __f)
  765. : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
  766. { }
  767. // [TODO] add && to save on a retain
  768. _LIBCPP_INLINE_VISIBILITY
  769. explicit __func(__block_type __f, const _Alloc& /* unused */)
  770. : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
  771. { }
  772. virtual __base<_Rp(_ArgTypes...)>* __clone() const {
  773. _LIBCPP_ASSERT(false,
  774. "Block pointers are just pointers, so they should always fit into "
  775. "std::function's small buffer optimization. This function should "
  776. "never be invoked.");
  777. return nullptr;
  778. }
  779. virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
  780. ::new ((void*)__p) __func(__f_);
  781. }
  782. virtual void destroy() _NOEXCEPT {
  783. if (__f_)
  784. _Block_release(__f_);
  785. __f_ = 0;
  786. }
  787. virtual void destroy_deallocate() _NOEXCEPT {
  788. _LIBCPP_ASSERT(false,
  789. "Block pointers are just pointers, so they should always fit into "
  790. "std::function's small buffer optimization. This function should "
  791. "never be invoked.");
  792. }
  793. virtual _Rp operator()(_ArgTypes&& ... __arg) {
  794. return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
  795. }
  796. #ifndef _LIBCPP_NO_RTTI
  797. virtual const void* target(type_info const& __ti) const _NOEXCEPT {
  798. if (__ti == typeid(__func::__block_type))
  799. return &__f_;
  800. return (const void*)nullptr;
  801. }
  802. virtual const std::type_info& target_type() const _NOEXCEPT {
  803. return typeid(__func::__block_type);
  804. }
  805. #endif // _LIBCPP_NO_RTTI
  806. };
  807. #endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
  808. } // __function
  809. template<class _Rp, class ..._ArgTypes>
  810. class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
  811. #if _LIBCPP_STD_VER <= 17 || !defined(_LIBCPP_ABI_NO_BINDER_BASES)
  812. : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
  813. public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
  814. #endif
  815. {
  816. #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
  817. typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
  818. #else
  819. typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
  820. #endif
  821. __func __f_;
  822. #ifdef _LIBCPP_COMPILER_MSVC
  823. #pragma warning ( push )
  824. #pragma warning ( disable : 4348 )
  825. #endif
  826. template <class _Fp, bool = _And<
  827. _IsNotSame<__uncvref_t<_Fp>, function>,
  828. __invokable<_Fp, _ArgTypes...>
  829. >::value>
  830. struct __callable;
  831. #ifdef _LIBCPP_COMPILER_MSVC
  832. #pragma warning ( pop )
  833. #endif
  834. template <class _Fp>
  835. struct __callable<_Fp, true>
  836. {
  837. static const bool value = is_void<_Rp>::value ||
  838. __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
  839. _Rp>::value;
  840. };
  841. template <class _Fp>
  842. struct __callable<_Fp, false>
  843. {
  844. static const bool value = false;
  845. };
  846. template <class _Fp>
  847. using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
  848. public:
  849. typedef _Rp result_type;
  850. // construct/copy/destroy:
  851. _LIBCPP_INLINE_VISIBILITY
  852. function() _NOEXCEPT { }
  853. _LIBCPP_INLINE_VISIBILITY
  854. function(nullptr_t) _NOEXCEPT {}
  855. function(const function&);
  856. function(function&&) _NOEXCEPT;
  857. template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
  858. function(_Fp);
  859. #if _LIBCPP_STD_VER <= 14
  860. template<class _Alloc>
  861. _LIBCPP_INLINE_VISIBILITY
  862. function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
  863. template<class _Alloc>
  864. _LIBCPP_INLINE_VISIBILITY
  865. function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
  866. template<class _Alloc>
  867. function(allocator_arg_t, const _Alloc&, const function&);
  868. template<class _Alloc>
  869. function(allocator_arg_t, const _Alloc&, function&&);
  870. template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
  871. function(allocator_arg_t, const _Alloc& __a, _Fp __f);
  872. #endif
  873. function& operator=(const function&);
  874. function& operator=(function&&) _NOEXCEPT;
  875. function& operator=(nullptr_t) _NOEXCEPT;
  876. template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
  877. function& operator=(_Fp&&);
  878. ~function();
  879. // function modifiers:
  880. void swap(function&) _NOEXCEPT;
  881. #if _LIBCPP_STD_VER <= 14
  882. template<class _Fp, class _Alloc>
  883. _LIBCPP_INLINE_VISIBILITY
  884. void assign(_Fp&& __f, const _Alloc& __a)
  885. {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
  886. #endif
  887. // function capacity:
  888. _LIBCPP_INLINE_VISIBILITY
  889. explicit operator bool() const _NOEXCEPT {
  890. return static_cast<bool>(__f_);
  891. }
  892. // deleted overloads close possible hole in the type system
  893. template<class _R2, class... _ArgTypes2>
  894. bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
  895. template<class _R2, class... _ArgTypes2>
  896. bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
  897. public:
  898. // function invocation:
  899. _Rp operator()(_ArgTypes...) const;
  900. #ifndef _LIBCPP_NO_RTTI
  901. // function target access:
  902. const std::type_info& target_type() const _NOEXCEPT;
  903. template <typename _Tp> _Tp* target() _NOEXCEPT;
  904. template <typename _Tp> const _Tp* target() const _NOEXCEPT;
  905. #endif // _LIBCPP_NO_RTTI
  906. };
  907. #if _LIBCPP_STD_VER >= 17
  908. template<class _Rp, class ..._Ap>
  909. function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
  910. template<class _Fp>
  911. struct __strip_signature;
  912. template<class _Rp, class _Gp, class ..._Ap>
  913. struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
  914. template<class _Rp, class _Gp, class ..._Ap>
  915. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
  916. template<class _Rp, class _Gp, class ..._Ap>
  917. struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
  918. template<class _Rp, class _Gp, class ..._Ap>
  919. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
  920. template<class _Rp, class _Gp, class ..._Ap>
  921. struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
  922. template<class _Rp, class _Gp, class ..._Ap>
  923. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
  924. template<class _Rp, class _Gp, class ..._Ap>
  925. struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
  926. template<class _Rp, class _Gp, class ..._Ap>
  927. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
  928. template<class _Rp, class _Gp, class ..._Ap>
  929. struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
  930. template<class _Rp, class _Gp, class ..._Ap>
  931. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
  932. template<class _Rp, class _Gp, class ..._Ap>
  933. struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
  934. template<class _Rp, class _Gp, class ..._Ap>
  935. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
  936. template<class _Rp, class _Gp, class ..._Ap>
  937. struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
  938. template<class _Rp, class _Gp, class ..._Ap>
  939. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
  940. template<class _Rp, class _Gp, class ..._Ap>
  941. struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
  942. template<class _Rp, class _Gp, class ..._Ap>
  943. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
  944. template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
  945. function(_Fp) -> function<_Stripped>;
  946. #endif // _LIBCPP_STD_VER >= 17
  947. template<class _Rp, class ..._ArgTypes>
  948. function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
  949. #if _LIBCPP_STD_VER <= 14
  950. template<class _Rp, class ..._ArgTypes>
  951. template <class _Alloc>
  952. function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
  953. const function& __f) : __f_(__f.__f_) {}
  954. #endif
  955. template <class _Rp, class... _ArgTypes>
  956. function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
  957. : __f_(_VSTD::move(__f.__f_)) {}
  958. #if _LIBCPP_STD_VER <= 14
  959. template<class _Rp, class ..._ArgTypes>
  960. template <class _Alloc>
  961. function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
  962. function&& __f)
  963. : __f_(_VSTD::move(__f.__f_)) {}
  964. #endif
  965. template <class _Rp, class... _ArgTypes>
  966. template <class _Fp, class>
  967. function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
  968. #if _LIBCPP_STD_VER <= 14
  969. template <class _Rp, class... _ArgTypes>
  970. template <class _Fp, class _Alloc, class>
  971. function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
  972. _Fp __f)
  973. : __f_(_VSTD::move(__f), __a) {}
  974. #endif
  975. template<class _Rp, class ..._ArgTypes>
  976. function<_Rp(_ArgTypes...)>&
  977. function<_Rp(_ArgTypes...)>::operator=(const function& __f)
  978. {
  979. function(__f).swap(*this);
  980. return *this;
  981. }
  982. template<class _Rp, class ..._ArgTypes>
  983. function<_Rp(_ArgTypes...)>&
  984. function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
  985. {
  986. __f_ = _VSTD::move(__f.__f_);
  987. return *this;
  988. }
  989. template<class _Rp, class ..._ArgTypes>
  990. function<_Rp(_ArgTypes...)>&
  991. function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
  992. {
  993. __f_ = nullptr;
  994. return *this;
  995. }
  996. template<class _Rp, class ..._ArgTypes>
  997. template <class _Fp, class>
  998. function<_Rp(_ArgTypes...)>&
  999. function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
  1000. {
  1001. function(_VSTD::forward<_Fp>(__f)).swap(*this);
  1002. return *this;
  1003. }
  1004. template<class _Rp, class ..._ArgTypes>
  1005. function<_Rp(_ArgTypes...)>::~function() {}
  1006. template<class _Rp, class ..._ArgTypes>
  1007. void
  1008. function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
  1009. {
  1010. __f_.swap(__f.__f_);
  1011. }
  1012. template<class _Rp, class ..._ArgTypes>
  1013. _Rp
  1014. function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
  1015. {
  1016. return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  1017. }
  1018. #ifndef _LIBCPP_NO_RTTI
  1019. template<class _Rp, class ..._ArgTypes>
  1020. const std::type_info&
  1021. function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
  1022. {
  1023. return __f_.target_type();
  1024. }
  1025. template<class _Rp, class ..._ArgTypes>
  1026. template <typename _Tp>
  1027. _Tp*
  1028. function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
  1029. {
  1030. return (_Tp*)(__f_.template target<_Tp>());
  1031. }
  1032. template<class _Rp, class ..._ArgTypes>
  1033. template <typename _Tp>
  1034. const _Tp*
  1035. function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
  1036. {
  1037. return __f_.template target<_Tp>();
  1038. }
  1039. #endif // _LIBCPP_NO_RTTI
  1040. template <class _Rp, class... _ArgTypes>
  1041. inline _LIBCPP_INLINE_VISIBILITY
  1042. bool
  1043. operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
  1044. template <class _Rp, class... _ArgTypes>
  1045. inline _LIBCPP_INLINE_VISIBILITY
  1046. bool
  1047. operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
  1048. template <class _Rp, class... _ArgTypes>
  1049. inline _LIBCPP_INLINE_VISIBILITY
  1050. bool
  1051. operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
  1052. template <class _Rp, class... _ArgTypes>
  1053. inline _LIBCPP_INLINE_VISIBILITY
  1054. bool
  1055. operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
  1056. template <class _Rp, class... _ArgTypes>
  1057. inline _LIBCPP_INLINE_VISIBILITY
  1058. void
  1059. swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
  1060. {return __x.swap(__y);}
  1061. #else // _LIBCPP_CXX03_LANG
  1062. namespace __function {
  1063. template<class _Fp> class __base;
  1064. template<class _Rp>
  1065. class __base<_Rp()>
  1066. {
  1067. __base(const __base&);
  1068. __base& operator=(const __base&);
  1069. public:
  1070. __base() {}
  1071. virtual ~__base() {}
  1072. virtual __base* __clone() const = 0;
  1073. virtual void __clone(__base*) const = 0;
  1074. virtual void destroy() = 0;
  1075. virtual void destroy_deallocate() = 0;
  1076. virtual _Rp operator()() = 0;
  1077. #ifndef _LIBCPP_NO_RTTI
  1078. virtual const void* target(const type_info&) const = 0;
  1079. virtual const std::type_info& target_type() const = 0;
  1080. #endif // _LIBCPP_NO_RTTI
  1081. };
  1082. template<class _Rp, class _A0>
  1083. class __base<_Rp(_A0)>
  1084. {
  1085. __base(const __base&);
  1086. __base& operator=(const __base&);
  1087. public:
  1088. __base() {}
  1089. virtual ~__base() {}
  1090. virtual __base* __clone() const = 0;
  1091. virtual void __clone(__base*) const = 0;
  1092. virtual void destroy() = 0;
  1093. virtual void destroy_deallocate() = 0;
  1094. virtual _Rp operator()(_A0) = 0;
  1095. #ifndef _LIBCPP_NO_RTTI
  1096. virtual const void* target(const type_info&) const = 0;
  1097. virtual const std::type_info& target_type() const = 0;
  1098. #endif // _LIBCPP_NO_RTTI
  1099. };
  1100. template<class _Rp, class _A0, class _A1>
  1101. class __base<_Rp(_A0, _A1)>
  1102. {
  1103. __base(const __base&);
  1104. __base& operator=(const __base&);
  1105. public:
  1106. __base() {}
  1107. virtual ~__base() {}
  1108. virtual __base* __clone() const = 0;
  1109. virtual void __clone(__base*) const = 0;
  1110. virtual void destroy() = 0;
  1111. virtual void destroy_deallocate() = 0;
  1112. virtual _Rp operator()(_A0, _A1) = 0;
  1113. #ifndef _LIBCPP_NO_RTTI
  1114. virtual const void* target(const type_info&) const = 0;
  1115. virtual const std::type_info& target_type() const = 0;
  1116. #endif // _LIBCPP_NO_RTTI
  1117. };
  1118. template<class _Rp, class _A0, class _A1, class _A2>
  1119. class __base<_Rp(_A0, _A1, _A2)>
  1120. {
  1121. __base(const __base&);
  1122. __base& operator=(const __base&);
  1123. public:
  1124. __base() {}
  1125. virtual ~__base() {}
  1126. virtual __base* __clone() const = 0;
  1127. virtual void __clone(__base*) const = 0;
  1128. virtual void destroy() = 0;
  1129. virtual void destroy_deallocate() = 0;
  1130. virtual _Rp operator()(_A0, _A1, _A2) = 0;
  1131. #ifndef _LIBCPP_NO_RTTI
  1132. virtual const void* target(const type_info&) const = 0;
  1133. virtual const std::type_info& target_type() const = 0;
  1134. #endif // _LIBCPP_NO_RTTI
  1135. };
  1136. template<class _FD, class _Alloc, class _FB> class __func;
  1137. template<class _Fp, class _Alloc, class _Rp>
  1138. class __func<_Fp, _Alloc, _Rp()>
  1139. : public __base<_Rp()>
  1140. {
  1141. __compressed_pair<_Fp, _Alloc> __f_;
  1142. public:
  1143. explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
  1144. explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
  1145. virtual __base<_Rp()>* __clone() const;
  1146. virtual void __clone(__base<_Rp()>*) const;
  1147. virtual void destroy();
  1148. virtual void destroy_deallocate();
  1149. virtual _Rp operator()();
  1150. #ifndef _LIBCPP_NO_RTTI
  1151. virtual const void* target(const type_info&) const;
  1152. virtual const std::type_info& target_type() const;
  1153. #endif // _LIBCPP_NO_RTTI
  1154. };
  1155. template<class _Fp, class _Alloc, class _Rp>
  1156. __base<_Rp()>*
  1157. __func<_Fp, _Alloc, _Rp()>::__clone() const
  1158. {
  1159. typedef allocator_traits<_Alloc> __alloc_traits;
  1160. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1161. _Ap __a(__f_.second());
  1162. typedef __allocator_destructor<_Ap> _Dp;
  1163. unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1164. ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
  1165. return __hold.release();
  1166. }
  1167. template<class _Fp, class _Alloc, class _Rp>
  1168. void
  1169. __func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
  1170. {
  1171. ::new ((void*)__p) __func(__f_.first(), __f_.second());
  1172. }
  1173. template<class _Fp, class _Alloc, class _Rp>
  1174. void
  1175. __func<_Fp, _Alloc, _Rp()>::destroy()
  1176. {
  1177. __f_.~__compressed_pair<_Fp, _Alloc>();
  1178. }
  1179. template<class _Fp, class _Alloc, class _Rp>
  1180. void
  1181. __func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
  1182. {
  1183. typedef allocator_traits<_Alloc> __alloc_traits;
  1184. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1185. _Ap __a(__f_.second());
  1186. __f_.~__compressed_pair<_Fp, _Alloc>();
  1187. __a.deallocate(this, 1);
  1188. }
  1189. template<class _Fp, class _Alloc, class _Rp>
  1190. _Rp
  1191. __func<_Fp, _Alloc, _Rp()>::operator()()
  1192. {
  1193. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  1194. return _Invoker::__call(__f_.first());
  1195. }
  1196. #ifndef _LIBCPP_NO_RTTI
  1197. template<class _Fp, class _Alloc, class _Rp>
  1198. const void*
  1199. __func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
  1200. {
  1201. if (__ti == typeid(_Fp))
  1202. return &__f_.first();
  1203. return (const void*)0;
  1204. }
  1205. template<class _Fp, class _Alloc, class _Rp>
  1206. const std::type_info&
  1207. __func<_Fp, _Alloc, _Rp()>::target_type() const
  1208. {
  1209. return typeid(_Fp);
  1210. }
  1211. #endif // _LIBCPP_NO_RTTI
  1212. template<class _Fp, class _Alloc, class _Rp, class _A0>
  1213. class __func<_Fp, _Alloc, _Rp(_A0)>
  1214. : public __base<_Rp(_A0)>
  1215. {
  1216. __compressed_pair<_Fp, _Alloc> __f_;
  1217. public:
  1218. _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
  1219. _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
  1220. : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
  1221. virtual __base<_Rp(_A0)>* __clone() const;
  1222. virtual void __clone(__base<_Rp(_A0)>*) const;
  1223. virtual void destroy();
  1224. virtual void destroy_deallocate();
  1225. virtual _Rp operator()(_A0);
  1226. #ifndef _LIBCPP_NO_RTTI
  1227. virtual const void* target(const type_info&) const;
  1228. virtual const std::type_info& target_type() const;
  1229. #endif // _LIBCPP_NO_RTTI
  1230. };
  1231. template<class _Fp, class _Alloc, class _Rp, class _A0>
  1232. __base<_Rp(_A0)>*
  1233. __func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
  1234. {
  1235. typedef allocator_traits<_Alloc> __alloc_traits;
  1236. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1237. _Ap __a(__f_.second());
  1238. typedef __allocator_destructor<_Ap> _Dp;
  1239. unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1240. ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
  1241. return __hold.release();
  1242. }
  1243. template<class _Fp, class _Alloc, class _Rp, class _A0>
  1244. void
  1245. __func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
  1246. {
  1247. ::new ((void*)__p) __func(__f_.first(), __f_.second());
  1248. }
  1249. template<class _Fp, class _Alloc, class _Rp, class _A0>
  1250. void
  1251. __func<_Fp, _Alloc, _Rp(_A0)>::destroy()
  1252. {
  1253. __f_.~__compressed_pair<_Fp, _Alloc>();
  1254. }
  1255. template<class _Fp, class _Alloc, class _Rp, class _A0>
  1256. void
  1257. __func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
  1258. {
  1259. typedef allocator_traits<_Alloc> __alloc_traits;
  1260. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1261. _Ap __a(__f_.second());
  1262. __f_.~__compressed_pair<_Fp, _Alloc>();
  1263. __a.deallocate(this, 1);
  1264. }
  1265. template<class _Fp, class _Alloc, class _Rp, class _A0>
  1266. _Rp
  1267. __func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
  1268. {
  1269. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  1270. return _Invoker::__call(__f_.first(), __a0);
  1271. }
  1272. #ifndef _LIBCPP_NO_RTTI
  1273. template<class _Fp, class _Alloc, class _Rp, class _A0>
  1274. const void*
  1275. __func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
  1276. {
  1277. if (__ti == typeid(_Fp))
  1278. return &__f_.first();
  1279. return (const void*)0;
  1280. }
  1281. template<class _Fp, class _Alloc, class _Rp, class _A0>
  1282. const std::type_info&
  1283. __func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
  1284. {
  1285. return typeid(_Fp);
  1286. }
  1287. #endif // _LIBCPP_NO_RTTI
  1288. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
  1289. class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
  1290. : public __base<_Rp(_A0, _A1)>
  1291. {
  1292. __compressed_pair<_Fp, _Alloc> __f_;
  1293. public:
  1294. _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
  1295. _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
  1296. : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
  1297. virtual __base<_Rp(_A0, _A1)>* __clone() const;
  1298. virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
  1299. virtual void destroy();
  1300. virtual void destroy_deallocate();
  1301. virtual _Rp operator()(_A0, _A1);
  1302. #ifndef _LIBCPP_NO_RTTI
  1303. virtual const void* target(const type_info&) const;
  1304. virtual const std::type_info& target_type() const;
  1305. #endif // _LIBCPP_NO_RTTI
  1306. };
  1307. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
  1308. __base<_Rp(_A0, _A1)>*
  1309. __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
  1310. {
  1311. typedef allocator_traits<_Alloc> __alloc_traits;
  1312. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1313. _Ap __a(__f_.second());
  1314. typedef __allocator_destructor<_Ap> _Dp;
  1315. unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1316. ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
  1317. return __hold.release();
  1318. }
  1319. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
  1320. void
  1321. __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
  1322. {
  1323. ::new ((void*)__p) __func(__f_.first(), __f_.second());
  1324. }
  1325. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
  1326. void
  1327. __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
  1328. {
  1329. __f_.~__compressed_pair<_Fp, _Alloc>();
  1330. }
  1331. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
  1332. void
  1333. __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
  1334. {
  1335. typedef allocator_traits<_Alloc> __alloc_traits;
  1336. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1337. _Ap __a(__f_.second());
  1338. __f_.~__compressed_pair<_Fp, _Alloc>();
  1339. __a.deallocate(this, 1);
  1340. }
  1341. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
  1342. _Rp
  1343. __func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
  1344. {
  1345. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  1346. return _Invoker::__call(__f_.first(), __a0, __a1);
  1347. }
  1348. #ifndef _LIBCPP_NO_RTTI
  1349. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
  1350. const void*
  1351. __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
  1352. {
  1353. if (__ti == typeid(_Fp))
  1354. return &__f_.first();
  1355. return (const void*)0;
  1356. }
  1357. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
  1358. const std::type_info&
  1359. __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
  1360. {
  1361. return typeid(_Fp);
  1362. }
  1363. #endif // _LIBCPP_NO_RTTI
  1364. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
  1365. class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
  1366. : public __base<_Rp(_A0, _A1, _A2)>
  1367. {
  1368. __compressed_pair<_Fp, _Alloc> __f_;
  1369. public:
  1370. _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
  1371. _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
  1372. : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
  1373. virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
  1374. virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
  1375. virtual void destroy();
  1376. virtual void destroy_deallocate();
  1377. virtual _Rp operator()(_A0, _A1, _A2);
  1378. #ifndef _LIBCPP_NO_RTTI
  1379. virtual const void* target(const type_info&) const;
  1380. virtual const std::type_info& target_type() const;
  1381. #endif // _LIBCPP_NO_RTTI
  1382. };
  1383. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
  1384. __base<_Rp(_A0, _A1, _A2)>*
  1385. __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
  1386. {
  1387. typedef allocator_traits<_Alloc> __alloc_traits;
  1388. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1389. _Ap __a(__f_.second());
  1390. typedef __allocator_destructor<_Ap> _Dp;
  1391. unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1392. ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
  1393. return __hold.release();
  1394. }
  1395. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
  1396. void
  1397. __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
  1398. {
  1399. ::new ((void*)__p) __func(__f_.first(), __f_.second());
  1400. }
  1401. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
  1402. void
  1403. __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
  1404. {
  1405. __f_.~__compressed_pair<_Fp, _Alloc>();
  1406. }
  1407. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
  1408. void
  1409. __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
  1410. {
  1411. typedef allocator_traits<_Alloc> __alloc_traits;
  1412. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1413. _Ap __a(__f_.second());
  1414. __f_.~__compressed_pair<_Fp, _Alloc>();
  1415. __a.deallocate(this, 1);
  1416. }
  1417. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
  1418. _Rp
  1419. __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
  1420. {
  1421. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  1422. return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
  1423. }
  1424. #ifndef _LIBCPP_NO_RTTI
  1425. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
  1426. const void*
  1427. __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
  1428. {
  1429. if (__ti == typeid(_Fp))
  1430. return &__f_.first();
  1431. return (const void*)0;
  1432. }
  1433. template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
  1434. const std::type_info&
  1435. __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
  1436. {
  1437. return typeid(_Fp);
  1438. }
  1439. #endif // _LIBCPP_NO_RTTI
  1440. } // __function
  1441. template<class _Rp>
  1442. class _LIBCPP_TEMPLATE_VIS function<_Rp()>
  1443. {
  1444. typedef __function::__base<_Rp()> __base;
  1445. aligned_storage<3*sizeof(void*)>::type __buf_;
  1446. __base* __f_;
  1447. public:
  1448. typedef _Rp result_type;
  1449. // 20.7.16.2.1, construct/copy/destroy:
  1450. _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
  1451. _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
  1452. function(const function&);
  1453. template<class _Fp>
  1454. function(_Fp,
  1455. typename enable_if<!is_integral<_Fp>::value>::type* = 0);
  1456. template<class _Alloc>
  1457. _LIBCPP_INLINE_VISIBILITY
  1458. function(allocator_arg_t, const _Alloc&) : __f_(0) {}
  1459. template<class _Alloc>
  1460. _LIBCPP_INLINE_VISIBILITY
  1461. function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
  1462. template<class _Alloc>
  1463. function(allocator_arg_t, const _Alloc&, const function&);
  1464. template<class _Fp, class _Alloc>
  1465. function(allocator_arg_t, const _Alloc& __a, _Fp __f,
  1466. typename enable_if<!is_integral<_Fp>::value>::type* = 0);
  1467. function& operator=(const function&);
  1468. function& operator=(nullptr_t);
  1469. template<class _Fp>
  1470. typename enable_if
  1471. <
  1472. !is_integral<_Fp>::value,
  1473. function&
  1474. >::type
  1475. operator=(_Fp);
  1476. ~function();
  1477. // 20.7.16.2.2, function modifiers:
  1478. void swap(function&);
  1479. template<class _Fp, class _Alloc>
  1480. _LIBCPP_INLINE_VISIBILITY
  1481. void assign(_Fp __f, const _Alloc& __a)
  1482. {function(allocator_arg, __a, __f).swap(*this);}
  1483. // 20.7.16.2.3, function capacity:
  1484. _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
  1485. private:
  1486. // deleted overloads close possible hole in the type system
  1487. template<class _R2>
  1488. bool operator==(const function<_R2()>&) const;// = delete;
  1489. template<class _R2>
  1490. bool operator!=(const function<_R2()>&) const;// = delete;
  1491. public:
  1492. // 20.7.16.2.4, function invocation:
  1493. _Rp operator()() const;
  1494. #ifndef _LIBCPP_NO_RTTI
  1495. // 20.7.16.2.5, function target access:
  1496. const std::type_info& target_type() const;
  1497. template <typename _Tp> _Tp* target();
  1498. template <typename _Tp> const _Tp* target() const;
  1499. #endif // _LIBCPP_NO_RTTI
  1500. };
  1501. template<class _Rp>
  1502. function<_Rp()>::function(const function& __f)
  1503. {
  1504. if (__f.__f_ == 0)
  1505. __f_ = 0;
  1506. else if (__f.__f_ == (const __base*)&__f.__buf_)
  1507. {
  1508. __f_ = (__base*)&__buf_;
  1509. __f.__f_->__clone(__f_);
  1510. }
  1511. else
  1512. __f_ = __f.__f_->__clone();
  1513. }
  1514. template<class _Rp>
  1515. template<class _Alloc>
  1516. function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
  1517. {
  1518. if (__f.__f_ == 0)
  1519. __f_ = 0;
  1520. else if (__f.__f_ == (const __base*)&__f.__buf_)
  1521. {
  1522. __f_ = (__base*)&__buf_;
  1523. __f.__f_->__clone(__f_);
  1524. }
  1525. else
  1526. __f_ = __f.__f_->__clone();
  1527. }
  1528. template<class _Rp>
  1529. template <class _Fp>
  1530. function<_Rp()>::function(_Fp __f,
  1531. typename enable_if<!is_integral<_Fp>::value>::type*)
  1532. : __f_(0)
  1533. {
  1534. if (__function::__not_null(__f))
  1535. {
  1536. typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
  1537. if (sizeof(_FF) <= sizeof(__buf_))
  1538. {
  1539. __f_ = (__base*)&__buf_;
  1540. ::new ((void*)__f_) _FF(__f);
  1541. }
  1542. else
  1543. {
  1544. typedef allocator<_FF> _Ap;
  1545. _Ap __a;
  1546. typedef __allocator_destructor<_Ap> _Dp;
  1547. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1548. ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
  1549. __f_ = __hold.release();
  1550. }
  1551. }
  1552. }
  1553. template<class _Rp>
  1554. template <class _Fp, class _Alloc>
  1555. function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
  1556. typename enable_if<!is_integral<_Fp>::value>::type*)
  1557. : __f_(0)
  1558. {
  1559. typedef allocator_traits<_Alloc> __alloc_traits;
  1560. if (__function::__not_null(__f))
  1561. {
  1562. typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
  1563. if (sizeof(_FF) <= sizeof(__buf_))
  1564. {
  1565. __f_ = (__base*)&__buf_;
  1566. ::new ((void*)__f_) _FF(__f, __a0);
  1567. }
  1568. else
  1569. {
  1570. typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
  1571. _Ap __a(__a0);
  1572. typedef __allocator_destructor<_Ap> _Dp;
  1573. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1574. ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
  1575. __f_ = __hold.release();
  1576. }
  1577. }
  1578. }
  1579. template<class _Rp>
  1580. function<_Rp()>&
  1581. function<_Rp()>::operator=(const function& __f)
  1582. {
  1583. if (__f)
  1584. function(__f).swap(*this);
  1585. else
  1586. *this = nullptr;
  1587. return *this;
  1588. }
  1589. template<class _Rp>
  1590. function<_Rp()>&
  1591. function<_Rp()>::operator=(nullptr_t)
  1592. {
  1593. __base* __t = __f_;
  1594. __f_ = 0;
  1595. if (__t == (__base*)&__buf_)
  1596. __t->destroy();
  1597. else if (__t)
  1598. __t->destroy_deallocate();
  1599. return *this;
  1600. }
  1601. template<class _Rp>
  1602. template <class _Fp>
  1603. typename enable_if
  1604. <
  1605. !is_integral<_Fp>::value,
  1606. function<_Rp()>&
  1607. >::type
  1608. function<_Rp()>::operator=(_Fp __f)
  1609. {
  1610. function(_VSTD::move(__f)).swap(*this);
  1611. return *this;
  1612. }
  1613. template<class _Rp>
  1614. function<_Rp()>::~function()
  1615. {
  1616. if (__f_ == (__base*)&__buf_)
  1617. __f_->destroy();
  1618. else if (__f_)
  1619. __f_->destroy_deallocate();
  1620. }
  1621. template<class _Rp>
  1622. void
  1623. function<_Rp()>::swap(function& __f)
  1624. {
  1625. if (_VSTD::addressof(__f) == this)
  1626. return;
  1627. if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
  1628. {
  1629. typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
  1630. __base* __t = (__base*)&__tempbuf;
  1631. __f_->__clone(__t);
  1632. __f_->destroy();
  1633. __f_ = 0;
  1634. __f.__f_->__clone((__base*)&__buf_);
  1635. __f.__f_->destroy();
  1636. __f.__f_ = 0;
  1637. __f_ = (__base*)&__buf_;
  1638. __t->__clone((__base*)&__f.__buf_);
  1639. __t->destroy();
  1640. __f.__f_ = (__base*)&__f.__buf_;
  1641. }
  1642. else if (__f_ == (__base*)&__buf_)
  1643. {
  1644. __f_->__clone((__base*)&__f.__buf_);
  1645. __f_->destroy();
  1646. __f_ = __f.__f_;
  1647. __f.__f_ = (__base*)&__f.__buf_;
  1648. }
  1649. else if (__f.__f_ == (__base*)&__f.__buf_)
  1650. {
  1651. __f.__f_->__clone((__base*)&__buf_);
  1652. __f.__f_->destroy();
  1653. __f.__f_ = __f_;
  1654. __f_ = (__base*)&__buf_;
  1655. }
  1656. else
  1657. _VSTD::swap(__f_, __f.__f_);
  1658. }
  1659. template<class _Rp>
  1660. _Rp
  1661. function<_Rp()>::operator()() const
  1662. {
  1663. if (__f_ == 0)
  1664. __throw_bad_function_call();
  1665. return (*__f_)();
  1666. }
  1667. #ifndef _LIBCPP_NO_RTTI
  1668. template<class _Rp>
  1669. const std::type_info&
  1670. function<_Rp()>::target_type() const
  1671. {
  1672. if (__f_ == 0)
  1673. return typeid(void);
  1674. return __f_->target_type();
  1675. }
  1676. template<class _Rp>
  1677. template <typename _Tp>
  1678. _Tp*
  1679. function<_Rp()>::target()
  1680. {
  1681. if (__f_ == 0)
  1682. return (_Tp*)0;
  1683. return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
  1684. }
  1685. template<class _Rp>
  1686. template <typename _Tp>
  1687. const _Tp*
  1688. function<_Rp()>::target() const
  1689. {
  1690. if (__f_ == 0)
  1691. return (const _Tp*)0;
  1692. return (const _Tp*)__f_->target(typeid(_Tp));
  1693. }
  1694. #endif // _LIBCPP_NO_RTTI
  1695. template<class _Rp, class _A0>
  1696. class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0)>
  1697. : public unary_function<_A0, _Rp>
  1698. {
  1699. typedef __function::__base<_Rp(_A0)> __base;
  1700. aligned_storage<3*sizeof(void*)>::type __buf_;
  1701. __base* __f_;
  1702. public:
  1703. typedef _Rp result_type;
  1704. // 20.7.16.2.1, construct/copy/destroy:
  1705. _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
  1706. _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
  1707. function(const function&);
  1708. template<class _Fp>
  1709. function(_Fp,
  1710. typename enable_if<!is_integral<_Fp>::value>::type* = 0);
  1711. template<class _Alloc>
  1712. _LIBCPP_INLINE_VISIBILITY
  1713. function(allocator_arg_t, const _Alloc&) : __f_(0) {}
  1714. template<class _Alloc>
  1715. _LIBCPP_INLINE_VISIBILITY
  1716. function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
  1717. template<class _Alloc>
  1718. function(allocator_arg_t, const _Alloc&, const function&);
  1719. template<class _Fp, class _Alloc>
  1720. function(allocator_arg_t, const _Alloc& __a, _Fp __f,
  1721. typename enable_if<!is_integral<_Fp>::value>::type* = 0);
  1722. function& operator=(const function&);
  1723. function& operator=(nullptr_t);
  1724. template<class _Fp>
  1725. typename enable_if
  1726. <
  1727. !is_integral<_Fp>::value,
  1728. function&
  1729. >::type
  1730. operator=(_Fp);
  1731. ~function();
  1732. // 20.7.16.2.2, function modifiers:
  1733. void swap(function&);
  1734. template<class _Fp, class _Alloc>
  1735. _LIBCPP_INLINE_VISIBILITY
  1736. void assign(_Fp __f, const _Alloc& __a)
  1737. {function(allocator_arg, __a, __f).swap(*this);}
  1738. // 20.7.16.2.3, function capacity:
  1739. _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
  1740. private:
  1741. // deleted overloads close possible hole in the type system
  1742. template<class _R2, class _B0>
  1743. bool operator==(const function<_R2(_B0)>&) const;// = delete;
  1744. template<class _R2, class _B0>
  1745. bool operator!=(const function<_R2(_B0)>&) const;// = delete;
  1746. public:
  1747. // 20.7.16.2.4, function invocation:
  1748. _Rp operator()(_A0) const;
  1749. #ifndef _LIBCPP_NO_RTTI
  1750. // 20.7.16.2.5, function target access:
  1751. const std::type_info& target_type() const;
  1752. template <typename _Tp> _Tp* target();
  1753. template <typename _Tp> const _Tp* target() const;
  1754. #endif // _LIBCPP_NO_RTTI
  1755. };
  1756. template<class _Rp, class _A0>
  1757. function<_Rp(_A0)>::function(const function& __f)
  1758. {
  1759. if (__f.__f_ == 0)
  1760. __f_ = 0;
  1761. else if (__f.__f_ == (const __base*)&__f.__buf_)
  1762. {
  1763. __f_ = (__base*)&__buf_;
  1764. __f.__f_->__clone(__f_);
  1765. }
  1766. else
  1767. __f_ = __f.__f_->__clone();
  1768. }
  1769. template<class _Rp, class _A0>
  1770. template<class _Alloc>
  1771. function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
  1772. {
  1773. if (__f.__f_ == 0)
  1774. __f_ = 0;
  1775. else if (__f.__f_ == (const __base*)&__f.__buf_)
  1776. {
  1777. __f_ = (__base*)&__buf_;
  1778. __f.__f_->__clone(__f_);
  1779. }
  1780. else
  1781. __f_ = __f.__f_->__clone();
  1782. }
  1783. template<class _Rp, class _A0>
  1784. template <class _Fp>
  1785. function<_Rp(_A0)>::function(_Fp __f,
  1786. typename enable_if<!is_integral<_Fp>::value>::type*)
  1787. : __f_(0)
  1788. {
  1789. if (__function::__not_null(__f))
  1790. {
  1791. typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
  1792. if (sizeof(_FF) <= sizeof(__buf_))
  1793. {
  1794. __f_ = (__base*)&__buf_;
  1795. ::new ((void*)__f_) _FF(__f);
  1796. }
  1797. else
  1798. {
  1799. typedef allocator<_FF> _Ap;
  1800. _Ap __a;
  1801. typedef __allocator_destructor<_Ap> _Dp;
  1802. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1803. ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
  1804. __f_ = __hold.release();
  1805. }
  1806. }
  1807. }
  1808. template<class _Rp, class _A0>
  1809. template <class _Fp, class _Alloc>
  1810. function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
  1811. typename enable_if<!is_integral<_Fp>::value>::type*)
  1812. : __f_(0)
  1813. {
  1814. typedef allocator_traits<_Alloc> __alloc_traits;
  1815. if (__function::__not_null(__f))
  1816. {
  1817. typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
  1818. if (sizeof(_FF) <= sizeof(__buf_))
  1819. {
  1820. __f_ = (__base*)&__buf_;
  1821. ::new ((void*)__f_) _FF(__f, __a0);
  1822. }
  1823. else
  1824. {
  1825. typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
  1826. _Ap __a(__a0);
  1827. typedef __allocator_destructor<_Ap> _Dp;
  1828. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1829. ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
  1830. __f_ = __hold.release();
  1831. }
  1832. }
  1833. }
  1834. template<class _Rp, class _A0>
  1835. function<_Rp(_A0)>&
  1836. function<_Rp(_A0)>::operator=(const function& __f)
  1837. {
  1838. if (__f)
  1839. function(__f).swap(*this);
  1840. else
  1841. *this = nullptr;
  1842. return *this;
  1843. }
  1844. template<class _Rp, class _A0>
  1845. function<_Rp(_A0)>&
  1846. function<_Rp(_A0)>::operator=(nullptr_t)
  1847. {
  1848. __base* __t = __f_;
  1849. __f_ = 0;
  1850. if (__t == (__base*)&__buf_)
  1851. __t->destroy();
  1852. else if (__t)
  1853. __t->destroy_deallocate();
  1854. return *this;
  1855. }
  1856. template<class _Rp, class _A0>
  1857. template <class _Fp>
  1858. typename enable_if
  1859. <
  1860. !is_integral<_Fp>::value,
  1861. function<_Rp(_A0)>&
  1862. >::type
  1863. function<_Rp(_A0)>::operator=(_Fp __f)
  1864. {
  1865. function(_VSTD::move(__f)).swap(*this);
  1866. return *this;
  1867. }
  1868. template<class _Rp, class _A0>
  1869. function<_Rp(_A0)>::~function()
  1870. {
  1871. if (__f_ == (__base*)&__buf_)
  1872. __f_->destroy();
  1873. else if (__f_)
  1874. __f_->destroy_deallocate();
  1875. }
  1876. template<class _Rp, class _A0>
  1877. void
  1878. function<_Rp(_A0)>::swap(function& __f)
  1879. {
  1880. if (_VSTD::addressof(__f) == this)
  1881. return;
  1882. if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
  1883. {
  1884. typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
  1885. __base* __t = (__base*)&__tempbuf;
  1886. __f_->__clone(__t);
  1887. __f_->destroy();
  1888. __f_ = 0;
  1889. __f.__f_->__clone((__base*)&__buf_);
  1890. __f.__f_->destroy();
  1891. __f.__f_ = 0;
  1892. __f_ = (__base*)&__buf_;
  1893. __t->__clone((__base*)&__f.__buf_);
  1894. __t->destroy();
  1895. __f.__f_ = (__base*)&__f.__buf_;
  1896. }
  1897. else if (__f_ == (__base*)&__buf_)
  1898. {
  1899. __f_->__clone((__base*)&__f.__buf_);
  1900. __f_->destroy();
  1901. __f_ = __f.__f_;
  1902. __f.__f_ = (__base*)&__f.__buf_;
  1903. }
  1904. else if (__f.__f_ == (__base*)&__f.__buf_)
  1905. {
  1906. __f.__f_->__clone((__base*)&__buf_);
  1907. __f.__f_->destroy();
  1908. __f.__f_ = __f_;
  1909. __f_ = (__base*)&__buf_;
  1910. }
  1911. else
  1912. _VSTD::swap(__f_, __f.__f_);
  1913. }
  1914. template<class _Rp, class _A0>
  1915. _Rp
  1916. function<_Rp(_A0)>::operator()(_A0 __a0) const
  1917. {
  1918. if (__f_ == 0)
  1919. __throw_bad_function_call();
  1920. return (*__f_)(__a0);
  1921. }
  1922. #ifndef _LIBCPP_NO_RTTI
  1923. template<class _Rp, class _A0>
  1924. const std::type_info&
  1925. function<_Rp(_A0)>::target_type() const
  1926. {
  1927. if (__f_ == 0)
  1928. return typeid(void);
  1929. return __f_->target_type();
  1930. }
  1931. template<class _Rp, class _A0>
  1932. template <typename _Tp>
  1933. _Tp*
  1934. function<_Rp(_A0)>::target()
  1935. {
  1936. if (__f_ == 0)
  1937. return (_Tp*)0;
  1938. return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
  1939. }
  1940. template<class _Rp, class _A0>
  1941. template <typename _Tp>
  1942. const _Tp*
  1943. function<_Rp(_A0)>::target() const
  1944. {
  1945. if (__f_ == 0)
  1946. return (const _Tp*)0;
  1947. return (const _Tp*)__f_->target(typeid(_Tp));
  1948. }
  1949. #endif // _LIBCPP_NO_RTTI
  1950. template<class _Rp, class _A0, class _A1>
  1951. class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1)>
  1952. : public binary_function<_A0, _A1, _Rp>
  1953. {
  1954. typedef __function::__base<_Rp(_A0, _A1)> __base;
  1955. aligned_storage<3*sizeof(void*)>::type __buf_;
  1956. __base* __f_;
  1957. public:
  1958. typedef _Rp result_type;
  1959. // 20.7.16.2.1, construct/copy/destroy:
  1960. _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
  1961. _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
  1962. function(const function&);
  1963. template<class _Fp>
  1964. function(_Fp,
  1965. typename enable_if<!is_integral<_Fp>::value>::type* = 0);
  1966. template<class _Alloc>
  1967. _LIBCPP_INLINE_VISIBILITY
  1968. function(allocator_arg_t, const _Alloc&) : __f_(0) {}
  1969. template<class _Alloc>
  1970. _LIBCPP_INLINE_VISIBILITY
  1971. function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
  1972. template<class _Alloc>
  1973. function(allocator_arg_t, const _Alloc&, const function&);
  1974. template<class _Fp, class _Alloc>
  1975. function(allocator_arg_t, const _Alloc& __a, _Fp __f,
  1976. typename enable_if<!is_integral<_Fp>::value>::type* = 0);
  1977. function& operator=(const function&);
  1978. function& operator=(nullptr_t);
  1979. template<class _Fp>
  1980. typename enable_if
  1981. <
  1982. !is_integral<_Fp>::value,
  1983. function&
  1984. >::type
  1985. operator=(_Fp);
  1986. ~function();
  1987. // 20.7.16.2.2, function modifiers:
  1988. void swap(function&);
  1989. template<class _Fp, class _Alloc>
  1990. _LIBCPP_INLINE_VISIBILITY
  1991. void assign(_Fp __f, const _Alloc& __a)
  1992. {function(allocator_arg, __a, __f).swap(*this);}
  1993. // 20.7.16.2.3, function capacity:
  1994. _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
  1995. private:
  1996. // deleted overloads close possible hole in the type system
  1997. template<class _R2, class _B0, class _B1>
  1998. bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
  1999. template<class _R2, class _B0, class _B1>
  2000. bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
  2001. public:
  2002. // 20.7.16.2.4, function invocation:
  2003. _Rp operator()(_A0, _A1) const;
  2004. #ifndef _LIBCPP_NO_RTTI
  2005. // 20.7.16.2.5, function target access:
  2006. const std::type_info& target_type() const;
  2007. template <typename _Tp> _Tp* target();
  2008. template <typename _Tp> const _Tp* target() const;
  2009. #endif // _LIBCPP_NO_RTTI
  2010. };
  2011. template<class _Rp, class _A0, class _A1>
  2012. function<_Rp(_A0, _A1)>::function(const function& __f)
  2013. {
  2014. if (__f.__f_ == 0)
  2015. __f_ = 0;
  2016. else if (__f.__f_ == (const __base*)&__f.__buf_)
  2017. {
  2018. __f_ = (__base*)&__buf_;
  2019. __f.__f_->__clone(__f_);
  2020. }
  2021. else
  2022. __f_ = __f.__f_->__clone();
  2023. }
  2024. template<class _Rp, class _A0, class _A1>
  2025. template<class _Alloc>
  2026. function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
  2027. {
  2028. if (__f.__f_ == 0)
  2029. __f_ = 0;
  2030. else if (__f.__f_ == (const __base*)&__f.__buf_)
  2031. {
  2032. __f_ = (__base*)&__buf_;
  2033. __f.__f_->__clone(__f_);
  2034. }
  2035. else
  2036. __f_ = __f.__f_->__clone();
  2037. }
  2038. template<class _Rp, class _A0, class _A1>
  2039. template <class _Fp>
  2040. function<_Rp(_A0, _A1)>::function(_Fp __f,
  2041. typename enable_if<!is_integral<_Fp>::value>::type*)
  2042. : __f_(0)
  2043. {
  2044. if (__function::__not_null(__f))
  2045. {
  2046. typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
  2047. if (sizeof(_FF) <= sizeof(__buf_))
  2048. {
  2049. __f_ = (__base*)&__buf_;
  2050. ::new ((void*)__f_) _FF(__f);
  2051. }
  2052. else
  2053. {
  2054. typedef allocator<_FF> _Ap;
  2055. _Ap __a;
  2056. typedef __allocator_destructor<_Ap> _Dp;
  2057. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  2058. ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
  2059. __f_ = __hold.release();
  2060. }
  2061. }
  2062. }
  2063. template<class _Rp, class _A0, class _A1>
  2064. template <class _Fp, class _Alloc>
  2065. function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
  2066. typename enable_if<!is_integral<_Fp>::value>::type*)
  2067. : __f_(0)
  2068. {
  2069. typedef allocator_traits<_Alloc> __alloc_traits;
  2070. if (__function::__not_null(__f))
  2071. {
  2072. typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
  2073. if (sizeof(_FF) <= sizeof(__buf_))
  2074. {
  2075. __f_ = (__base*)&__buf_;
  2076. ::new ((void*)__f_) _FF(__f, __a0);
  2077. }
  2078. else
  2079. {
  2080. typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
  2081. _Ap __a(__a0);
  2082. typedef __allocator_destructor<_Ap> _Dp;
  2083. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  2084. ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
  2085. __f_ = __hold.release();
  2086. }
  2087. }
  2088. }
  2089. template<class _Rp, class _A0, class _A1>
  2090. function<_Rp(_A0, _A1)>&
  2091. function<_Rp(_A0, _A1)>::operator=(const function& __f)
  2092. {
  2093. if (__f)
  2094. function(__f).swap(*this);
  2095. else
  2096. *this = nullptr;
  2097. return *this;
  2098. }
  2099. template<class _Rp, class _A0, class _A1>
  2100. function<_Rp(_A0, _A1)>&
  2101. function<_Rp(_A0, _A1)>::operator=(nullptr_t)
  2102. {
  2103. __base* __t = __f_;
  2104. __f_ = 0;
  2105. if (__t == (__base*)&__buf_)
  2106. __t->destroy();
  2107. else if (__t)
  2108. __t->destroy_deallocate();
  2109. return *this;
  2110. }
  2111. template<class _Rp, class _A0, class _A1>
  2112. template <class _Fp>
  2113. typename enable_if
  2114. <
  2115. !is_integral<_Fp>::value,
  2116. function<_Rp(_A0, _A1)>&
  2117. >::type
  2118. function<_Rp(_A0, _A1)>::operator=(_Fp __f)
  2119. {
  2120. function(_VSTD::move(__f)).swap(*this);
  2121. return *this;
  2122. }
  2123. template<class _Rp, class _A0, class _A1>
  2124. function<_Rp(_A0, _A1)>::~function()
  2125. {
  2126. if (__f_ == (__base*)&__buf_)
  2127. __f_->destroy();
  2128. else if (__f_)
  2129. __f_->destroy_deallocate();
  2130. }
  2131. template<class _Rp, class _A0, class _A1>
  2132. void
  2133. function<_Rp(_A0, _A1)>::swap(function& __f)
  2134. {
  2135. if (_VSTD::addressof(__f) == this)
  2136. return;
  2137. if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
  2138. {
  2139. typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
  2140. __base* __t = (__base*)&__tempbuf;
  2141. __f_->__clone(__t);
  2142. __f_->destroy();
  2143. __f_ = 0;
  2144. __f.__f_->__clone((__base*)&__buf_);
  2145. __f.__f_->destroy();
  2146. __f.__f_ = 0;
  2147. __f_ = (__base*)&__buf_;
  2148. __t->__clone((__base*)&__f.__buf_);
  2149. __t->destroy();
  2150. __f.__f_ = (__base*)&__f.__buf_;
  2151. }
  2152. else if (__f_ == (__base*)&__buf_)
  2153. {
  2154. __f_->__clone((__base*)&__f.__buf_);
  2155. __f_->destroy();
  2156. __f_ = __f.__f_;
  2157. __f.__f_ = (__base*)&__f.__buf_;
  2158. }
  2159. else if (__f.__f_ == (__base*)&__f.__buf_)
  2160. {
  2161. __f.__f_->__clone((__base*)&__buf_);
  2162. __f.__f_->destroy();
  2163. __f.__f_ = __f_;
  2164. __f_ = (__base*)&__buf_;
  2165. }
  2166. else
  2167. _VSTD::swap(__f_, __f.__f_);
  2168. }
  2169. template<class _Rp, class _A0, class _A1>
  2170. _Rp
  2171. function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
  2172. {
  2173. if (__f_ == 0)
  2174. __throw_bad_function_call();
  2175. return (*__f_)(__a0, __a1);
  2176. }
  2177. #ifndef _LIBCPP_NO_RTTI
  2178. template<class _Rp, class _A0, class _A1>
  2179. const std::type_info&
  2180. function<_Rp(_A0, _A1)>::target_type() const
  2181. {
  2182. if (__f_ == 0)
  2183. return typeid(void);
  2184. return __f_->target_type();
  2185. }
  2186. template<class _Rp, class _A0, class _A1>
  2187. template <typename _Tp>
  2188. _Tp*
  2189. function<_Rp(_A0, _A1)>::target()
  2190. {
  2191. if (__f_ == 0)
  2192. return (_Tp*)0;
  2193. return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
  2194. }
  2195. template<class _Rp, class _A0, class _A1>
  2196. template <typename _Tp>
  2197. const _Tp*
  2198. function<_Rp(_A0, _A1)>::target() const
  2199. {
  2200. if (__f_ == 0)
  2201. return (const _Tp*)0;
  2202. return (const _Tp*)__f_->target(typeid(_Tp));
  2203. }
  2204. #endif // _LIBCPP_NO_RTTI
  2205. template<class _Rp, class _A0, class _A1, class _A2>
  2206. class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1, _A2)>
  2207. {
  2208. typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
  2209. aligned_storage<3*sizeof(void*)>::type __buf_;
  2210. __base* __f_;
  2211. public:
  2212. typedef _Rp result_type;
  2213. // 20.7.16.2.1, construct/copy/destroy:
  2214. _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
  2215. _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
  2216. function(const function&);
  2217. template<class _Fp>
  2218. function(_Fp,
  2219. typename enable_if<!is_integral<_Fp>::value>::type* = 0);
  2220. template<class _Alloc>
  2221. _LIBCPP_INLINE_VISIBILITY
  2222. function(allocator_arg_t, const _Alloc&) : __f_(0) {}
  2223. template<class _Alloc>
  2224. _LIBCPP_INLINE_VISIBILITY
  2225. function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
  2226. template<class _Alloc>
  2227. function(allocator_arg_t, const _Alloc&, const function&);
  2228. template<class _Fp, class _Alloc>
  2229. function(allocator_arg_t, const _Alloc& __a, _Fp __f,
  2230. typename enable_if<!is_integral<_Fp>::value>::type* = 0);
  2231. function& operator=(const function&);
  2232. function& operator=(nullptr_t);
  2233. template<class _Fp>
  2234. typename enable_if
  2235. <
  2236. !is_integral<_Fp>::value,
  2237. function&
  2238. >::type
  2239. operator=(_Fp);
  2240. ~function();
  2241. // 20.7.16.2.2, function modifiers:
  2242. void swap(function&);
  2243. template<class _Fp, class _Alloc>
  2244. _LIBCPP_INLINE_VISIBILITY
  2245. void assign(_Fp __f, const _Alloc& __a)
  2246. {function(allocator_arg, __a, __f).swap(*this);}
  2247. // 20.7.16.2.3, function capacity:
  2248. _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
  2249. private:
  2250. // deleted overloads close possible hole in the type system
  2251. template<class _R2, class _B0, class _B1, class _B2>
  2252. bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
  2253. template<class _R2, class _B0, class _B1, class _B2>
  2254. bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
  2255. public:
  2256. // 20.7.16.2.4, function invocation:
  2257. _Rp operator()(_A0, _A1, _A2) const;
  2258. #ifndef _LIBCPP_NO_RTTI
  2259. // 20.7.16.2.5, function target access:
  2260. const std::type_info& target_type() const;
  2261. template <typename _Tp> _Tp* target();
  2262. template <typename _Tp> const _Tp* target() const;
  2263. #endif // _LIBCPP_NO_RTTI
  2264. };
  2265. template<class _Rp, class _A0, class _A1, class _A2>
  2266. function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
  2267. {
  2268. if (__f.__f_ == 0)
  2269. __f_ = 0;
  2270. else if (__f.__f_ == (const __base*)&__f.__buf_)
  2271. {
  2272. __f_ = (__base*)&__buf_;
  2273. __f.__f_->__clone(__f_);
  2274. }
  2275. else
  2276. __f_ = __f.__f_->__clone();
  2277. }
  2278. template<class _Rp, class _A0, class _A1, class _A2>
  2279. template<class _Alloc>
  2280. function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
  2281. const function& __f)
  2282. {
  2283. if (__f.__f_ == 0)
  2284. __f_ = 0;
  2285. else if (__f.__f_ == (const __base*)&__f.__buf_)
  2286. {
  2287. __f_ = (__base*)&__buf_;
  2288. __f.__f_->__clone(__f_);
  2289. }
  2290. else
  2291. __f_ = __f.__f_->__clone();
  2292. }
  2293. template<class _Rp, class _A0, class _A1, class _A2>
  2294. template <class _Fp>
  2295. function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
  2296. typename enable_if<!is_integral<_Fp>::value>::type*)
  2297. : __f_(0)
  2298. {
  2299. if (__function::__not_null(__f))
  2300. {
  2301. typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
  2302. if (sizeof(_FF) <= sizeof(__buf_))
  2303. {
  2304. __f_ = (__base*)&__buf_;
  2305. ::new ((void*)__f_) _FF(__f);
  2306. }
  2307. else
  2308. {
  2309. typedef allocator<_FF> _Ap;
  2310. _Ap __a;
  2311. typedef __allocator_destructor<_Ap> _Dp;
  2312. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  2313. ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
  2314. __f_ = __hold.release();
  2315. }
  2316. }
  2317. }
  2318. template<class _Rp, class _A0, class _A1, class _A2>
  2319. template <class _Fp, class _Alloc>
  2320. function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
  2321. typename enable_if<!is_integral<_Fp>::value>::type*)
  2322. : __f_(0)
  2323. {
  2324. typedef allocator_traits<_Alloc> __alloc_traits;
  2325. if (__function::__not_null(__f))
  2326. {
  2327. typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
  2328. if (sizeof(_FF) <= sizeof(__buf_))
  2329. {
  2330. __f_ = (__base*)&__buf_;
  2331. ::new ((void*)__f_) _FF(__f, __a0);
  2332. }
  2333. else
  2334. {
  2335. typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
  2336. _Ap __a(__a0);
  2337. typedef __allocator_destructor<_Ap> _Dp;
  2338. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  2339. ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
  2340. __f_ = __hold.release();
  2341. }
  2342. }
  2343. }
  2344. template<class _Rp, class _A0, class _A1, class _A2>
  2345. function<_Rp(_A0, _A1, _A2)>&
  2346. function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
  2347. {
  2348. if (__f)
  2349. function(__f).swap(*this);
  2350. else
  2351. *this = nullptr;
  2352. return *this;
  2353. }
  2354. template<class _Rp, class _A0, class _A1, class _A2>
  2355. function<_Rp(_A0, _A1, _A2)>&
  2356. function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
  2357. {
  2358. __base* __t = __f_;
  2359. __f_ = 0;
  2360. if (__t == (__base*)&__buf_)
  2361. __t->destroy();
  2362. else if (__t)
  2363. __t->destroy_deallocate();
  2364. return *this;
  2365. }
  2366. template<class _Rp, class _A0, class _A1, class _A2>
  2367. template <class _Fp>
  2368. typename enable_if
  2369. <
  2370. !is_integral<_Fp>::value,
  2371. function<_Rp(_A0, _A1, _A2)>&
  2372. >::type
  2373. function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
  2374. {
  2375. function(_VSTD::move(__f)).swap(*this);
  2376. return *this;
  2377. }
  2378. template<class _Rp, class _A0, class _A1, class _A2>
  2379. function<_Rp(_A0, _A1, _A2)>::~function()
  2380. {
  2381. if (__f_ == (__base*)&__buf_)
  2382. __f_->destroy();
  2383. else if (__f_)
  2384. __f_->destroy_deallocate();
  2385. }
  2386. template<class _Rp, class _A0, class _A1, class _A2>
  2387. void
  2388. function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
  2389. {
  2390. if (_VSTD::addressof(__f) == this)
  2391. return;
  2392. if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
  2393. {
  2394. typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
  2395. __base* __t = (__base*)&__tempbuf;
  2396. __f_->__clone(__t);
  2397. __f_->destroy();
  2398. __f_ = 0;
  2399. __f.__f_->__clone((__base*)&__buf_);
  2400. __f.__f_->destroy();
  2401. __f.__f_ = 0;
  2402. __f_ = (__base*)&__buf_;
  2403. __t->__clone((__base*)&__f.__buf_);
  2404. __t->destroy();
  2405. __f.__f_ = (__base*)&__f.__buf_;
  2406. }
  2407. else if (__f_ == (__base*)&__buf_)
  2408. {
  2409. __f_->__clone((__base*)&__f.__buf_);
  2410. __f_->destroy();
  2411. __f_ = __f.__f_;
  2412. __f.__f_ = (__base*)&__f.__buf_;
  2413. }
  2414. else if (__f.__f_ == (__base*)&__f.__buf_)
  2415. {
  2416. __f.__f_->__clone((__base*)&__buf_);
  2417. __f.__f_->destroy();
  2418. __f.__f_ = __f_;
  2419. __f_ = (__base*)&__buf_;
  2420. }
  2421. else
  2422. _VSTD::swap(__f_, __f.__f_);
  2423. }
  2424. template<class _Rp, class _A0, class _A1, class _A2>
  2425. _Rp
  2426. function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
  2427. {
  2428. if (__f_ == 0)
  2429. __throw_bad_function_call();
  2430. return (*__f_)(__a0, __a1, __a2);
  2431. }
  2432. #ifndef _LIBCPP_NO_RTTI
  2433. template<class _Rp, class _A0, class _A1, class _A2>
  2434. const std::type_info&
  2435. function<_Rp(_A0, _A1, _A2)>::target_type() const
  2436. {
  2437. if (__f_ == 0)
  2438. return typeid(void);
  2439. return __f_->target_type();
  2440. }
  2441. template<class _Rp, class _A0, class _A1, class _A2>
  2442. template <typename _Tp>
  2443. _Tp*
  2444. function<_Rp(_A0, _A1, _A2)>::target()
  2445. {
  2446. if (__f_ == 0)
  2447. return (_Tp*)0;
  2448. return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
  2449. }
  2450. template<class _Rp, class _A0, class _A1, class _A2>
  2451. template <typename _Tp>
  2452. const _Tp*
  2453. function<_Rp(_A0, _A1, _A2)>::target() const
  2454. {
  2455. if (__f_ == 0)
  2456. return (const _Tp*)0;
  2457. return (const _Tp*)__f_->target(typeid(_Tp));
  2458. }
  2459. #endif // _LIBCPP_NO_RTTI
  2460. template <class _Fp>
  2461. inline _LIBCPP_INLINE_VISIBILITY
  2462. bool
  2463. operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
  2464. template <class _Fp>
  2465. inline _LIBCPP_INLINE_VISIBILITY
  2466. bool
  2467. operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
  2468. template <class _Fp>
  2469. inline _LIBCPP_INLINE_VISIBILITY
  2470. bool
  2471. operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
  2472. template <class _Fp>
  2473. inline _LIBCPP_INLINE_VISIBILITY
  2474. bool
  2475. operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
  2476. template <class _Fp>
  2477. inline _LIBCPP_INLINE_VISIBILITY
  2478. void
  2479. swap(function<_Fp>& __x, function<_Fp>& __y)
  2480. {return __x.swap(__y);}
  2481. #endif
  2482. _LIBCPP_END_NAMESPACE_STD
  2483. #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H