function.h 81 KB

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