function.h 81 KB

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