functional 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381
  1. // <functional> -*- C++ -*-
  2. // Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. * Copyright (c) 1997
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. *
  32. */
  33. /** @file include/functional
  34. * This is a Standard C++ Library header.
  35. */
  36. #ifndef _GLIBCXX_FUNCTIONAL
  37. #define _GLIBCXX_FUNCTIONAL 1
  38. #pragma GCC system_header
  39. #include <bits/c++config.h>
  40. #include <bits/stl_function.h>
  41. #if __cplusplus >= 201103L
  42. #include <typeinfo>
  43. #include <new>
  44. #include <tuple>
  45. #include <type_traits>
  46. #include <bits/functexcept.h>
  47. #include <bits/functional_hash.h>
  48. namespace std _GLIBCXX_VISIBILITY(default)
  49. {
  50. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  51. template<typename _MemberPointer>
  52. class _Mem_fn;
  53. template<typename _Tp, typename _Class>
  54. _Mem_fn<_Tp _Class::*>
  55. mem_fn(_Tp _Class::*) noexcept;
  56. /// If we have found a result_type, extract it.
  57. template<typename _Functor, typename = __void_t<>>
  58. struct _Maybe_get_result_type
  59. { };
  60. template<typename _Functor>
  61. struct _Maybe_get_result_type<_Functor,
  62. __void_t<typename _Functor::result_type>>
  63. { typedef typename _Functor::result_type result_type; };
  64. /**
  65. * Base class for any function object that has a weak result type, as
  66. * defined in 20.8.2 [func.require] of C++11.
  67. */
  68. template<typename _Functor>
  69. struct _Weak_result_type_impl
  70. : _Maybe_get_result_type<_Functor>
  71. { };
  72. /// Retrieve the result type for a function type.
  73. template<typename _Res, typename... _ArgTypes>
  74. struct _Weak_result_type_impl<_Res(_ArgTypes...)>
  75. { typedef _Res result_type; };
  76. template<typename _Res, typename... _ArgTypes>
  77. struct _Weak_result_type_impl<_Res(_ArgTypes......)>
  78. { typedef _Res result_type; };
  79. template<typename _Res, typename... _ArgTypes>
  80. struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
  81. { typedef _Res result_type; };
  82. template<typename _Res, typename... _ArgTypes>
  83. struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
  84. { typedef _Res result_type; };
  85. template<typename _Res, typename... _ArgTypes>
  86. struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
  87. { typedef _Res result_type; };
  88. template<typename _Res, typename... _ArgTypes>
  89. struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
  90. { typedef _Res result_type; };
  91. template<typename _Res, typename... _ArgTypes>
  92. struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
  93. { typedef _Res result_type; };
  94. template<typename _Res, typename... _ArgTypes>
  95. struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
  96. { typedef _Res result_type; };
  97. /// Retrieve the result type for a function reference.
  98. template<typename _Res, typename... _ArgTypes>
  99. struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
  100. { typedef _Res result_type; };
  101. template<typename _Res, typename... _ArgTypes>
  102. struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
  103. { typedef _Res result_type; };
  104. /// Retrieve the result type for a function pointer.
  105. template<typename _Res, typename... _ArgTypes>
  106. struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
  107. { typedef _Res result_type; };
  108. template<typename _Res, typename... _ArgTypes>
  109. struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
  110. { typedef _Res result_type; };
  111. /// Retrieve result type for a member function pointer.
  112. template<typename _Res, typename _Class, typename... _ArgTypes>
  113. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
  114. { typedef _Res result_type; };
  115. template<typename _Res, typename _Class, typename... _ArgTypes>
  116. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
  117. { typedef _Res result_type; };
  118. /// Retrieve result type for a const member function pointer.
  119. template<typename _Res, typename _Class, typename... _ArgTypes>
  120. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
  121. { typedef _Res result_type; };
  122. template<typename _Res, typename _Class, typename... _ArgTypes>
  123. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
  124. { typedef _Res result_type; };
  125. /// Retrieve result type for a volatile member function pointer.
  126. template<typename _Res, typename _Class, typename... _ArgTypes>
  127. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
  128. { typedef _Res result_type; };
  129. template<typename _Res, typename _Class, typename... _ArgTypes>
  130. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
  131. { typedef _Res result_type; };
  132. /// Retrieve result type for a const volatile member function pointer.
  133. template<typename _Res, typename _Class, typename... _ArgTypes>
  134. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
  135. const volatile>
  136. { typedef _Res result_type; };
  137. template<typename _Res, typename _Class, typename... _ArgTypes>
  138. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
  139. const volatile>
  140. { typedef _Res result_type; };
  141. /**
  142. * Strip top-level cv-qualifiers from the function object and let
  143. * _Weak_result_type_impl perform the real work.
  144. */
  145. template<typename _Functor>
  146. struct _Weak_result_type
  147. : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
  148. { };
  149. /**
  150. * Invoke a function object, which may be either a member pointer or a
  151. * function object. The first parameter will tell which.
  152. */
  153. template<typename _Functor, typename... _Args>
  154. inline
  155. typename enable_if<
  156. (!is_member_pointer<_Functor>::value
  157. && !is_function<_Functor>::value
  158. && !is_function<typename remove_pointer<_Functor>::type>::value),
  159. typename result_of<_Functor&(_Args&&...)>::type
  160. >::type
  161. __invoke(_Functor& __f, _Args&&... __args)
  162. {
  163. return __f(std::forward<_Args>(__args)...);
  164. }
  165. template<typename _Functor, typename... _Args>
  166. inline
  167. typename enable_if<
  168. (is_member_pointer<_Functor>::value
  169. && !is_function<_Functor>::value
  170. && !is_function<typename remove_pointer<_Functor>::type>::value),
  171. typename result_of<_Functor(_Args&&...)>::type
  172. >::type
  173. __invoke(_Functor& __f, _Args&&... __args)
  174. {
  175. return std::mem_fn(__f)(std::forward<_Args>(__args)...);
  176. }
  177. // To pick up function references (that will become function pointers)
  178. template<typename _Functor, typename... _Args>
  179. inline
  180. typename enable_if<
  181. (is_pointer<_Functor>::value
  182. && is_function<typename remove_pointer<_Functor>::type>::value),
  183. typename result_of<_Functor(_Args&&...)>::type
  184. >::type
  185. __invoke(_Functor __f, _Args&&... __args)
  186. {
  187. return __f(std::forward<_Args>(__args)...);
  188. }
  189. /**
  190. * Knowing which of unary_function and binary_function _Tp derives
  191. * from, derives from the same and ensures that reference_wrapper
  192. * will have a weak result type. See cases below.
  193. */
  194. template<bool _Unary, bool _Binary, typename _Tp>
  195. struct _Reference_wrapper_base_impl;
  196. // None of the nested argument types.
  197. template<typename _Tp>
  198. struct _Reference_wrapper_base_impl<false, false, _Tp>
  199. : _Weak_result_type<_Tp>
  200. { };
  201. // Nested argument_type only.
  202. template<typename _Tp>
  203. struct _Reference_wrapper_base_impl<true, false, _Tp>
  204. : _Weak_result_type<_Tp>
  205. {
  206. typedef typename _Tp::argument_type argument_type;
  207. };
  208. // Nested first_argument_type and second_argument_type only.
  209. template<typename _Tp>
  210. struct _Reference_wrapper_base_impl<false, true, _Tp>
  211. : _Weak_result_type<_Tp>
  212. {
  213. typedef typename _Tp::first_argument_type first_argument_type;
  214. typedef typename _Tp::second_argument_type second_argument_type;
  215. };
  216. // All the nested argument types.
  217. template<typename _Tp>
  218. struct _Reference_wrapper_base_impl<true, true, _Tp>
  219. : _Weak_result_type<_Tp>
  220. {
  221. typedef typename _Tp::argument_type argument_type;
  222. typedef typename _Tp::first_argument_type first_argument_type;
  223. typedef typename _Tp::second_argument_type second_argument_type;
  224. };
  225. _GLIBCXX_HAS_NESTED_TYPE(argument_type)
  226. _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
  227. _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
  228. /**
  229. * Derives from unary_function or binary_function when it
  230. * can. Specializations handle all of the easy cases. The primary
  231. * template determines what to do with a class type, which may
  232. * derive from both unary_function and binary_function.
  233. */
  234. template<typename _Tp>
  235. struct _Reference_wrapper_base
  236. : _Reference_wrapper_base_impl<
  237. __has_argument_type<_Tp>::value,
  238. __has_first_argument_type<_Tp>::value
  239. && __has_second_argument_type<_Tp>::value,
  240. _Tp>
  241. { };
  242. // - a function type (unary)
  243. template<typename _Res, typename _T1>
  244. struct _Reference_wrapper_base<_Res(_T1)>
  245. : unary_function<_T1, _Res>
  246. { };
  247. template<typename _Res, typename _T1>
  248. struct _Reference_wrapper_base<_Res(_T1) const>
  249. : unary_function<_T1, _Res>
  250. { };
  251. template<typename _Res, typename _T1>
  252. struct _Reference_wrapper_base<_Res(_T1) volatile>
  253. : unary_function<_T1, _Res>
  254. { };
  255. template<typename _Res, typename _T1>
  256. struct _Reference_wrapper_base<_Res(_T1) const volatile>
  257. : unary_function<_T1, _Res>
  258. { };
  259. // - a function type (binary)
  260. template<typename _Res, typename _T1, typename _T2>
  261. struct _Reference_wrapper_base<_Res(_T1, _T2)>
  262. : binary_function<_T1, _T2, _Res>
  263. { };
  264. template<typename _Res, typename _T1, typename _T2>
  265. struct _Reference_wrapper_base<_Res(_T1, _T2) const>
  266. : binary_function<_T1, _T2, _Res>
  267. { };
  268. template<typename _Res, typename _T1, typename _T2>
  269. struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
  270. : binary_function<_T1, _T2, _Res>
  271. { };
  272. template<typename _Res, typename _T1, typename _T2>
  273. struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
  274. : binary_function<_T1, _T2, _Res>
  275. { };
  276. // - a function pointer type (unary)
  277. template<typename _Res, typename _T1>
  278. struct _Reference_wrapper_base<_Res(*)(_T1)>
  279. : unary_function<_T1, _Res>
  280. { };
  281. // - a function pointer type (binary)
  282. template<typename _Res, typename _T1, typename _T2>
  283. struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
  284. : binary_function<_T1, _T2, _Res>
  285. { };
  286. // - a pointer to member function type (unary, no qualifiers)
  287. template<typename _Res, typename _T1>
  288. struct _Reference_wrapper_base<_Res (_T1::*)()>
  289. : unary_function<_T1*, _Res>
  290. { };
  291. // - a pointer to member function type (binary, no qualifiers)
  292. template<typename _Res, typename _T1, typename _T2>
  293. struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
  294. : binary_function<_T1*, _T2, _Res>
  295. { };
  296. // - a pointer to member function type (unary, const)
  297. template<typename _Res, typename _T1>
  298. struct _Reference_wrapper_base<_Res (_T1::*)() const>
  299. : unary_function<const _T1*, _Res>
  300. { };
  301. // - a pointer to member function type (binary, const)
  302. template<typename _Res, typename _T1, typename _T2>
  303. struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
  304. : binary_function<const _T1*, _T2, _Res>
  305. { };
  306. // - a pointer to member function type (unary, volatile)
  307. template<typename _Res, typename _T1>
  308. struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
  309. : unary_function<volatile _T1*, _Res>
  310. { };
  311. // - a pointer to member function type (binary, volatile)
  312. template<typename _Res, typename _T1, typename _T2>
  313. struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
  314. : binary_function<volatile _T1*, _T2, _Res>
  315. { };
  316. // - a pointer to member function type (unary, const volatile)
  317. template<typename _Res, typename _T1>
  318. struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
  319. : unary_function<const volatile _T1*, _Res>
  320. { };
  321. // - a pointer to member function type (binary, const volatile)
  322. template<typename _Res, typename _T1, typename _T2>
  323. struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
  324. : binary_function<const volatile _T1*, _T2, _Res>
  325. { };
  326. /**
  327. * @brief Primary class template for reference_wrapper.
  328. * @ingroup functors
  329. * @{
  330. */
  331. template<typename _Tp>
  332. class reference_wrapper
  333. : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
  334. {
  335. _Tp* _M_data;
  336. public:
  337. typedef _Tp type;
  338. reference_wrapper(_Tp& __indata) noexcept
  339. : _M_data(std::__addressof(__indata))
  340. { }
  341. reference_wrapper(_Tp&&) = delete;
  342. reference_wrapper(const reference_wrapper&) = default;
  343. reference_wrapper&
  344. operator=(const reference_wrapper&) = default;
  345. operator _Tp&() const noexcept
  346. { return this->get(); }
  347. _Tp&
  348. get() const noexcept
  349. { return *_M_data; }
  350. template<typename... _Args>
  351. typename result_of<_Tp&(_Args&&...)>::type
  352. operator()(_Args&&... __args) const
  353. {
  354. return __invoke(get(), std::forward<_Args>(__args)...);
  355. }
  356. };
  357. /// Denotes a reference should be taken to a variable.
  358. template<typename _Tp>
  359. inline reference_wrapper<_Tp>
  360. ref(_Tp& __t) noexcept
  361. { return reference_wrapper<_Tp>(__t); }
  362. /// Denotes a const reference should be taken to a variable.
  363. template<typename _Tp>
  364. inline reference_wrapper<const _Tp>
  365. cref(const _Tp& __t) noexcept
  366. { return reference_wrapper<const _Tp>(__t); }
  367. template<typename _Tp>
  368. void ref(const _Tp&&) = delete;
  369. template<typename _Tp>
  370. void cref(const _Tp&&) = delete;
  371. /// Partial specialization.
  372. template<typename _Tp>
  373. inline reference_wrapper<_Tp>
  374. ref(reference_wrapper<_Tp> __t) noexcept
  375. { return ref(__t.get()); }
  376. /// Partial specialization.
  377. template<typename _Tp>
  378. inline reference_wrapper<const _Tp>
  379. cref(reference_wrapper<_Tp> __t) noexcept
  380. { return cref(__t.get()); }
  381. // @} group functors
  382. template<typename... _Types>
  383. struct _Pack : integral_constant<size_t, sizeof...(_Types)>
  384. { };
  385. template<typename _From, typename _To, bool = _From::value == _To::value>
  386. struct _AllConvertible : false_type
  387. { };
  388. template<typename... _From, typename... _To>
  389. struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
  390. : __and_<is_convertible<_From, _To>...>
  391. { };
  392. template<typename _Tp1, typename _Tp2>
  393. using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
  394. typename std::decay<_Tp2>::type>>;
  395. /**
  396. * Derives from @c unary_function or @c binary_function, or perhaps
  397. * nothing, depending on the number of arguments provided. The
  398. * primary template is the basis case, which derives nothing.
  399. */
  400. template<typename _Res, typename... _ArgTypes>
  401. struct _Maybe_unary_or_binary_function { };
  402. /// Derives from @c unary_function, as appropriate.
  403. template<typename _Res, typename _T1>
  404. struct _Maybe_unary_or_binary_function<_Res, _T1>
  405. : std::unary_function<_T1, _Res> { };
  406. /// Derives from @c binary_function, as appropriate.
  407. template<typename _Res, typename _T1, typename _T2>
  408. struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
  409. : std::binary_function<_T1, _T2, _Res> { };
  410. template<typename _Signature>
  411. struct _Mem_fn_traits;
  412. template<typename _Res, typename _Class, typename... _ArgTypes>
  413. struct _Mem_fn_traits_base
  414. {
  415. using __result_type = _Res;
  416. using __class_type = _Class;
  417. using __arg_types = _Pack<_ArgTypes...>;
  418. using __maybe_type
  419. = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
  420. using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
  421. };
  422. #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \
  423. template<typename _Res, typename _Class, typename... _ArgTypes> \
  424. struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \
  425. : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
  426. { \
  427. using __pmf_type = _Res (_Class::*)(_ArgTypes...) _CV _REF; \
  428. using __lvalue = _LVAL; \
  429. using __rvalue = _RVAL; \
  430. using __vararg = false_type; \
  431. }; \
  432. template<typename _Res, typename _Class, typename... _ArgTypes> \
  433. struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \
  434. : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
  435. { \
  436. using __pmf_type = _Res (_Class::*)(_ArgTypes... ...) _CV _REF; \
  437. using __lvalue = _LVAL; \
  438. using __rvalue = _RVAL; \
  439. using __vararg = true_type; \
  440. };
  441. #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \
  442. _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \
  443. _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \
  444. _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \
  445. _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
  446. _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
  447. _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
  448. _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
  449. #undef _GLIBCXX_MEM_FN_TRAITS
  450. #undef _GLIBCXX_MEM_FN_TRAITS2
  451. template<typename _MemFunPtr,
  452. bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
  453. class _Mem_fn_base
  454. : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
  455. {
  456. using _Traits = _Mem_fn_traits<_MemFunPtr>;
  457. using _Class = typename _Traits::__class_type;
  458. using _ArgTypes = typename _Traits::__arg_types;
  459. using _Pmf = typename _Traits::__pmf_type;
  460. using _Arity = typename _Traits::__arity;
  461. using _Varargs = typename _Traits::__vararg;
  462. template<typename _Func, typename... _BoundArgs>
  463. friend struct _Bind_check_arity;
  464. // for varargs functions we just check the number of arguments,
  465. // otherwise we also check they are convertible.
  466. template<typename _Args>
  467. using _CheckArgs = typename conditional<_Varargs::value,
  468. __bool_constant<(_Args::value >= _ArgTypes::value)>,
  469. _AllConvertible<_Args, _ArgTypes>
  470. >::type;
  471. public:
  472. using result_type = typename _Traits::__result_type;
  473. explicit _Mem_fn_base(_Pmf __pmf) : _M_pmf(__pmf) { }
  474. // Handle objects
  475. template<typename... _Args, typename _Req
  476. = _Require<typename _Traits::__lvalue,
  477. _CheckArgs<_Pack<_Args...>>>>
  478. result_type
  479. operator()(_Class& __object, _Args&&... __args) const
  480. { return (__object.*_M_pmf)(std::forward<_Args>(__args)...); }
  481. template<typename... _Args, typename _Req
  482. = _Require<typename _Traits::__rvalue,
  483. _CheckArgs<_Pack<_Args...>>>>
  484. result_type
  485. operator()(_Class&& __object, _Args&&... __args) const
  486. {
  487. return (std::move(__object).*_M_pmf)(std::forward<_Args>(__args)...);
  488. }
  489. // Handle pointers
  490. template<typename... _Args, typename _Req
  491. = _Require<typename _Traits::__lvalue,
  492. _CheckArgs<_Pack<_Args...>>>>
  493. result_type
  494. operator()(_Class* __object, _Args&&... __args) const
  495. { return (__object->*_M_pmf)(std::forward<_Args>(__args)...); }
  496. // Handle smart pointers, references and pointers to derived
  497. template<typename _Tp, typename... _Args, typename _Req
  498. = _Require<_NotSame<_Class, _Tp>, _NotSame<_Class*, _Tp>,
  499. _CheckArgs<_Pack<_Args...>>>>
  500. result_type
  501. operator()(_Tp&& __object, _Args&&... __args) const
  502. {
  503. return _M_call(std::forward<_Tp>(__object), &__object,
  504. std::forward<_Args>(__args)...);
  505. }
  506. // Handle reference wrappers
  507. template<typename _Tp, typename... _Args, typename _Req
  508. = _Require<is_base_of<_Class, _Tp>, typename _Traits::__lvalue,
  509. _CheckArgs<_Pack<_Args...>>>>
  510. result_type
  511. operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
  512. { return operator()(__ref.get(), std::forward<_Args>(__args)...); }
  513. private:
  514. template<typename _Tp, typename... _Args>
  515. result_type
  516. _M_call(_Tp&& __object, const volatile _Class *,
  517. _Args&&... __args) const
  518. {
  519. return (std::forward<_Tp>(__object).*_M_pmf)
  520. (std::forward<_Args>(__args)...);
  521. }
  522. template<typename _Tp, typename... _Args>
  523. result_type
  524. _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
  525. { return ((*__ptr).*_M_pmf)(std::forward<_Args>(__args)...); }
  526. _Pmf _M_pmf;
  527. };
  528. // Partial specialization for member object pointers.
  529. template<typename _Res, typename _Class>
  530. class _Mem_fn_base<_Res _Class::*, false>
  531. {
  532. using __pm_type = _Res _Class::*;
  533. // This bit of genius is due to Peter Dimov, improved slightly by
  534. // Douglas Gregor.
  535. // Made less elegant to support perfect forwarding and noexcept.
  536. template<typename _Tp>
  537. auto
  538. _M_call(_Tp&& __object, const _Class *) const noexcept
  539. -> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>())
  540. { return std::forward<_Tp>(__object).*_M_pm; }
  541. template<typename _Tp, typename _Up>
  542. auto
  543. _M_call(_Tp&& __object, _Up * const *) const noexcept
  544. -> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>())
  545. { return (*std::forward<_Tp>(__object)).*_M_pm; }
  546. template<typename _Tp>
  547. auto
  548. _M_call(_Tp&& __ptr, const volatile void*) const
  549. noexcept(noexcept((*__ptr).*std::declval<__pm_type&>()))
  550. -> decltype((*__ptr).*std::declval<__pm_type&>())
  551. { return (*__ptr).*_M_pm; }
  552. using _Arity = integral_constant<size_t, 0>;
  553. using _Varargs = false_type;
  554. template<typename _Func, typename... _BoundArgs>
  555. friend struct _Bind_check_arity;
  556. public:
  557. explicit
  558. _Mem_fn_base(_Res _Class::*__pm) noexcept : _M_pm(__pm) { }
  559. // Handle objects
  560. _Res&
  561. operator()(_Class& __object) const noexcept
  562. { return __object.*_M_pm; }
  563. const _Res&
  564. operator()(const _Class& __object) const noexcept
  565. { return __object.*_M_pm; }
  566. _Res&&
  567. operator()(_Class&& __object) const noexcept
  568. { return std::forward<_Class>(__object).*_M_pm; }
  569. const _Res&&
  570. operator()(const _Class&& __object) const noexcept
  571. { return std::forward<const _Class>(__object).*_M_pm; }
  572. // Handle pointers
  573. _Res&
  574. operator()(_Class* __object) const noexcept
  575. { return __object->*_M_pm; }
  576. const _Res&
  577. operator()(const _Class* __object) const noexcept
  578. { return __object->*_M_pm; }
  579. // Handle smart pointers and derived
  580. template<typename _Tp, typename _Req = _Require<_NotSame<_Class*, _Tp>>>
  581. auto
  582. operator()(_Tp&& __unknown) const
  583. noexcept(noexcept(std::declval<_Mem_fn_base*>()->_M_call
  584. (std::forward<_Tp>(__unknown), &__unknown)))
  585. -> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown))
  586. { return _M_call(std::forward<_Tp>(__unknown), &__unknown); }
  587. template<typename _Tp, typename _Req = _Require<is_base_of<_Class, _Tp>>>
  588. auto
  589. operator()(reference_wrapper<_Tp> __ref) const
  590. noexcept(noexcept(std::declval<_Mem_fn_base&>()(__ref.get())))
  591. -> decltype((*this)(__ref.get()))
  592. { return (*this)(__ref.get()); }
  593. private:
  594. _Res _Class::*_M_pm;
  595. };
  596. template<typename _Res, typename _Class>
  597. struct _Mem_fn<_Res _Class::*>
  598. : _Mem_fn_base<_Res _Class::*>
  599. {
  600. using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
  601. };
  602. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  603. // 2048. Unnecessary mem_fn overloads
  604. /**
  605. * @brief Returns a function object that forwards to the member
  606. * pointer @a pm.
  607. * @ingroup functors
  608. */
  609. template<typename _Tp, typename _Class>
  610. inline _Mem_fn<_Tp _Class::*>
  611. mem_fn(_Tp _Class::* __pm) noexcept
  612. {
  613. return _Mem_fn<_Tp _Class::*>(__pm);
  614. }
  615. /**
  616. * @brief Determines if the given type _Tp is a function object
  617. * should be treated as a subexpression when evaluating calls to
  618. * function objects returned by bind(). [TR1 3.6.1]
  619. * @ingroup binders
  620. */
  621. template<typename _Tp>
  622. struct is_bind_expression
  623. : public false_type { };
  624. /**
  625. * @brief Determines if the given type _Tp is a placeholder in a
  626. * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
  627. * @ingroup binders
  628. */
  629. template<typename _Tp>
  630. struct is_placeholder
  631. : public integral_constant<int, 0>
  632. { };
  633. /** @brief The type of placeholder objects defined by libstdc++.
  634. * @ingroup binders
  635. */
  636. template<int _Num> struct _Placeholder { };
  637. _GLIBCXX_END_NAMESPACE_VERSION
  638. /** @namespace std::placeholders
  639. * @brief ISO C++11 entities sub-namespace for functional.
  640. * @ingroup binders
  641. */
  642. namespace placeholders
  643. {
  644. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  645. /* Define a large number of placeholders. There is no way to
  646. * simplify this with variadic templates, because we're introducing
  647. * unique names for each.
  648. */
  649. extern const _Placeholder<1> _1;
  650. extern const _Placeholder<2> _2;
  651. extern const _Placeholder<3> _3;
  652. extern const _Placeholder<4> _4;
  653. extern const _Placeholder<5> _5;
  654. extern const _Placeholder<6> _6;
  655. extern const _Placeholder<7> _7;
  656. extern const _Placeholder<8> _8;
  657. extern const _Placeholder<9> _9;
  658. extern const _Placeholder<10> _10;
  659. extern const _Placeholder<11> _11;
  660. extern const _Placeholder<12> _12;
  661. extern const _Placeholder<13> _13;
  662. extern const _Placeholder<14> _14;
  663. extern const _Placeholder<15> _15;
  664. extern const _Placeholder<16> _16;
  665. extern const _Placeholder<17> _17;
  666. extern const _Placeholder<18> _18;
  667. extern const _Placeholder<19> _19;
  668. extern const _Placeholder<20> _20;
  669. extern const _Placeholder<21> _21;
  670. extern const _Placeholder<22> _22;
  671. extern const _Placeholder<23> _23;
  672. extern const _Placeholder<24> _24;
  673. extern const _Placeholder<25> _25;
  674. extern const _Placeholder<26> _26;
  675. extern const _Placeholder<27> _27;
  676. extern const _Placeholder<28> _28;
  677. extern const _Placeholder<29> _29;
  678. _GLIBCXX_END_NAMESPACE_VERSION
  679. }
  680. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  681. /**
  682. * Partial specialization of is_placeholder that provides the placeholder
  683. * number for the placeholder objects defined by libstdc++.
  684. * @ingroup binders
  685. */
  686. template<int _Num>
  687. struct is_placeholder<_Placeholder<_Num> >
  688. : public integral_constant<int, _Num>
  689. { };
  690. template<int _Num>
  691. struct is_placeholder<const _Placeholder<_Num> >
  692. : public integral_constant<int, _Num>
  693. { };
  694. /**
  695. * Used by _Safe_tuple_element to indicate that there is no tuple
  696. * element at this position.
  697. */
  698. struct _No_tuple_element;
  699. /**
  700. * Implementation helper for _Safe_tuple_element. This primary
  701. * template handles the case where it is safe to use @c
  702. * tuple_element.
  703. */
  704. template<std::size_t __i, typename _Tuple, bool _IsSafe>
  705. struct _Safe_tuple_element_impl
  706. : tuple_element<__i, _Tuple> { };
  707. /**
  708. * Implementation helper for _Safe_tuple_element. This partial
  709. * specialization handles the case where it is not safe to use @c
  710. * tuple_element. We just return @c _No_tuple_element.
  711. */
  712. template<std::size_t __i, typename _Tuple>
  713. struct _Safe_tuple_element_impl<__i, _Tuple, false>
  714. {
  715. typedef _No_tuple_element type;
  716. };
  717. /**
  718. * Like tuple_element, but returns @c _No_tuple_element when
  719. * tuple_element would return an error.
  720. */
  721. template<std::size_t __i, typename _Tuple>
  722. struct _Safe_tuple_element
  723. : _Safe_tuple_element_impl<__i, _Tuple,
  724. (__i < tuple_size<_Tuple>::value)>
  725. { };
  726. /**
  727. * Maps an argument to bind() into an actual argument to the bound
  728. * function object [TR1 3.6.3/5]. Only the first parameter should
  729. * be specified: the rest are used to determine among the various
  730. * implementations. Note that, although this class is a function
  731. * object, it isn't entirely normal because it takes only two
  732. * parameters regardless of the number of parameters passed to the
  733. * bind expression. The first parameter is the bound argument and
  734. * the second parameter is a tuple containing references to the
  735. * rest of the arguments.
  736. */
  737. template<typename _Arg,
  738. bool _IsBindExp = is_bind_expression<_Arg>::value,
  739. bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
  740. class _Mu;
  741. /**
  742. * If the argument is reference_wrapper<_Tp>, returns the
  743. * underlying reference. [TR1 3.6.3/5 bullet 1]
  744. */
  745. template<typename _Tp>
  746. class _Mu<reference_wrapper<_Tp>, false, false>
  747. {
  748. public:
  749. typedef _Tp& result_type;
  750. /* Note: This won't actually work for const volatile
  751. * reference_wrappers, because reference_wrapper::get() is const
  752. * but not volatile-qualified. This might be a defect in the TR.
  753. */
  754. template<typename _CVRef, typename _Tuple>
  755. result_type
  756. operator()(_CVRef& __arg, _Tuple&) const volatile
  757. { return __arg.get(); }
  758. };
  759. /**
  760. * If the argument is a bind expression, we invoke the underlying
  761. * function object with the same cv-qualifiers as we are given and
  762. * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
  763. */
  764. template<typename _Arg>
  765. class _Mu<_Arg, true, false>
  766. {
  767. public:
  768. template<typename _CVArg, typename... _Args>
  769. auto
  770. operator()(_CVArg& __arg,
  771. tuple<_Args...>& __tuple) const volatile
  772. -> decltype(__arg(declval<_Args>()...))
  773. {
  774. // Construct an index tuple and forward to __call
  775. typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
  776. _Indexes;
  777. return this->__call(__arg, __tuple, _Indexes());
  778. }
  779. private:
  780. // Invokes the underlying function object __arg by unpacking all
  781. // of the arguments in the tuple.
  782. template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
  783. auto
  784. __call(_CVArg& __arg, tuple<_Args...>& __tuple,
  785. const _Index_tuple<_Indexes...>&) const volatile
  786. -> decltype(__arg(declval<_Args>()...))
  787. {
  788. return __arg(std::forward<_Args>(std::get<_Indexes>(__tuple))...);
  789. }
  790. };
  791. /**
  792. * If the argument is a placeholder for the Nth argument, returns
  793. * a reference to the Nth argument to the bind function object.
  794. * [TR1 3.6.3/5 bullet 3]
  795. */
  796. template<typename _Arg>
  797. class _Mu<_Arg, false, true>
  798. {
  799. public:
  800. template<typename _Signature> class result;
  801. template<typename _CVMu, typename _CVArg, typename _Tuple>
  802. class result<_CVMu(_CVArg, _Tuple)>
  803. {
  804. // Add a reference, if it hasn't already been done for us.
  805. // This allows us to be a little bit sloppy in constructing
  806. // the tuple that we pass to result_of<...>.
  807. typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
  808. - 1), _Tuple>::type
  809. __base_type;
  810. public:
  811. typedef typename add_rvalue_reference<__base_type>::type type;
  812. };
  813. template<typename _Tuple>
  814. typename result<_Mu(_Arg, _Tuple)>::type
  815. operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
  816. {
  817. return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
  818. ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
  819. }
  820. };
  821. /**
  822. * If the argument is just a value, returns a reference to that
  823. * value. The cv-qualifiers on the reference are the same as the
  824. * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
  825. */
  826. template<typename _Arg>
  827. class _Mu<_Arg, false, false>
  828. {
  829. public:
  830. template<typename _Signature> struct result;
  831. template<typename _CVMu, typename _CVArg, typename _Tuple>
  832. struct result<_CVMu(_CVArg, _Tuple)>
  833. {
  834. typedef typename add_lvalue_reference<_CVArg>::type type;
  835. };
  836. // Pick up the cv-qualifiers of the argument
  837. template<typename _CVArg, typename _Tuple>
  838. _CVArg&&
  839. operator()(_CVArg&& __arg, _Tuple&) const volatile
  840. { return std::forward<_CVArg>(__arg); }
  841. };
  842. /**
  843. * Maps member pointers into instances of _Mem_fn but leaves all
  844. * other function objects untouched. Used by std::bind(). The
  845. * primary template handles the non-member-pointer case.
  846. */
  847. template<typename _Tp>
  848. struct _Maybe_wrap_member_pointer
  849. {
  850. typedef _Tp type;
  851. static const _Tp&
  852. __do_wrap(const _Tp& __x)
  853. { return __x; }
  854. static _Tp&&
  855. __do_wrap(_Tp&& __x)
  856. { return static_cast<_Tp&&>(__x); }
  857. };
  858. /**
  859. * Maps member pointers into instances of _Mem_fn but leaves all
  860. * other function objects untouched. Used by std::bind(). This
  861. * partial specialization handles the member pointer case.
  862. */
  863. template<typename _Tp, typename _Class>
  864. struct _Maybe_wrap_member_pointer<_Tp _Class::*>
  865. {
  866. typedef _Mem_fn<_Tp _Class::*> type;
  867. static type
  868. __do_wrap(_Tp _Class::* __pm)
  869. { return type(__pm); }
  870. };
  871. // Specialization needed to prevent "forming reference to void" errors when
  872. // bind<void>() is called, because argument deduction instantiates
  873. // _Maybe_wrap_member_pointer<void> outside the immediate context where
  874. // SFINAE applies.
  875. template<>
  876. struct _Maybe_wrap_member_pointer<void>
  877. {
  878. typedef void type;
  879. };
  880. // std::get<I> for volatile-qualified tuples
  881. template<std::size_t _Ind, typename... _Tp>
  882. inline auto
  883. __volget(volatile tuple<_Tp...>& __tuple)
  884. -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
  885. { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
  886. // std::get<I> for const-volatile-qualified tuples
  887. template<std::size_t _Ind, typename... _Tp>
  888. inline auto
  889. __volget(const volatile tuple<_Tp...>& __tuple)
  890. -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
  891. { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
  892. /// Type of the function object returned from bind().
  893. template<typename _Signature>
  894. struct _Bind;
  895. template<typename _Functor, typename... _Bound_args>
  896. class _Bind<_Functor(_Bound_args...)>
  897. : public _Weak_result_type<_Functor>
  898. {
  899. typedef _Bind __self_type;
  900. typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
  901. _Bound_indexes;
  902. _Functor _M_f;
  903. tuple<_Bound_args...> _M_bound_args;
  904. // Call unqualified
  905. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  906. _Result
  907. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
  908. {
  909. return _M_f(_Mu<_Bound_args>()
  910. (std::get<_Indexes>(_M_bound_args), __args)...);
  911. }
  912. // Call as const
  913. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  914. _Result
  915. __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
  916. {
  917. return _M_f(_Mu<_Bound_args>()
  918. (std::get<_Indexes>(_M_bound_args), __args)...);
  919. }
  920. // Call as volatile
  921. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  922. _Result
  923. __call_v(tuple<_Args...>&& __args,
  924. _Index_tuple<_Indexes...>) volatile
  925. {
  926. return _M_f(_Mu<_Bound_args>()
  927. (__volget<_Indexes>(_M_bound_args), __args)...);
  928. }
  929. // Call as const volatile
  930. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  931. _Result
  932. __call_c_v(tuple<_Args...>&& __args,
  933. _Index_tuple<_Indexes...>) const volatile
  934. {
  935. return _M_f(_Mu<_Bound_args>()
  936. (__volget<_Indexes>(_M_bound_args), __args)...);
  937. }
  938. public:
  939. template<typename... _Args>
  940. explicit _Bind(const _Functor& __f, _Args&&... __args)
  941. : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
  942. { }
  943. template<typename... _Args>
  944. explicit _Bind(_Functor&& __f, _Args&&... __args)
  945. : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
  946. { }
  947. _Bind(const _Bind&) = default;
  948. _Bind(_Bind&& __b)
  949. : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
  950. { }
  951. // Call unqualified
  952. template<typename... _Args, typename _Result
  953. = decltype( std::declval<_Functor>()(
  954. _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
  955. std::declval<tuple<_Args...>&>() )... ) )>
  956. _Result
  957. operator()(_Args&&... __args)
  958. {
  959. return this->__call<_Result>(
  960. std::forward_as_tuple(std::forward<_Args>(__args)...),
  961. _Bound_indexes());
  962. }
  963. // Call as const
  964. template<typename... _Args, typename _Result
  965. = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
  966. typename add_const<_Functor>::type>::type>()(
  967. _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
  968. std::declval<tuple<_Args...>&>() )... ) )>
  969. _Result
  970. operator()(_Args&&... __args) const
  971. {
  972. return this->__call_c<_Result>(
  973. std::forward_as_tuple(std::forward<_Args>(__args)...),
  974. _Bound_indexes());
  975. }
  976. // Call as volatile
  977. template<typename... _Args, typename _Result
  978. = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
  979. typename add_volatile<_Functor>::type>::type>()(
  980. _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
  981. std::declval<tuple<_Args...>&>() )... ) )>
  982. _Result
  983. operator()(_Args&&... __args) volatile
  984. {
  985. return this->__call_v<_Result>(
  986. std::forward_as_tuple(std::forward<_Args>(__args)...),
  987. _Bound_indexes());
  988. }
  989. // Call as const volatile
  990. template<typename... _Args, typename _Result
  991. = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
  992. typename add_cv<_Functor>::type>::type>()(
  993. _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
  994. std::declval<tuple<_Args...>&>() )... ) )>
  995. _Result
  996. operator()(_Args&&... __args) const volatile
  997. {
  998. return this->__call_c_v<_Result>(
  999. std::forward_as_tuple(std::forward<_Args>(__args)...),
  1000. _Bound_indexes());
  1001. }
  1002. };
  1003. /// Type of the function object returned from bind<R>().
  1004. template<typename _Result, typename _Signature>
  1005. struct _Bind_result;
  1006. template<typename _Result, typename _Functor, typename... _Bound_args>
  1007. class _Bind_result<_Result, _Functor(_Bound_args...)>
  1008. {
  1009. typedef _Bind_result __self_type;
  1010. typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
  1011. _Bound_indexes;
  1012. _Functor _M_f;
  1013. tuple<_Bound_args...> _M_bound_args;
  1014. // sfinae types
  1015. template<typename _Res>
  1016. struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
  1017. template<typename _Res>
  1018. struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
  1019. // Call unqualified
  1020. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  1021. _Result
  1022. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  1023. typename __disable_if_void<_Res>::type = 0)
  1024. {
  1025. return _M_f(_Mu<_Bound_args>()
  1026. (std::get<_Indexes>(_M_bound_args), __args)...);
  1027. }
  1028. // Call unqualified, return void
  1029. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  1030. void
  1031. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  1032. typename __enable_if_void<_Res>::type = 0)
  1033. {
  1034. _M_f(_Mu<_Bound_args>()
  1035. (std::get<_Indexes>(_M_bound_args), __args)...);
  1036. }
  1037. // Call as const
  1038. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  1039. _Result
  1040. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  1041. typename __disable_if_void<_Res>::type = 0) const
  1042. {
  1043. return _M_f(_Mu<_Bound_args>()
  1044. (std::get<_Indexes>(_M_bound_args), __args)...);
  1045. }
  1046. // Call as const, return void
  1047. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  1048. void
  1049. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  1050. typename __enable_if_void<_Res>::type = 0) const
  1051. {
  1052. _M_f(_Mu<_Bound_args>()
  1053. (std::get<_Indexes>(_M_bound_args), __args)...);
  1054. }
  1055. // Call as volatile
  1056. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  1057. _Result
  1058. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  1059. typename __disable_if_void<_Res>::type = 0) volatile
  1060. {
  1061. return _M_f(_Mu<_Bound_args>()
  1062. (__volget<_Indexes>(_M_bound_args), __args)...);
  1063. }
  1064. // Call as volatile, return void
  1065. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  1066. void
  1067. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  1068. typename __enable_if_void<_Res>::type = 0) volatile
  1069. {
  1070. _M_f(_Mu<_Bound_args>()
  1071. (__volget<_Indexes>(_M_bound_args), __args)...);
  1072. }
  1073. // Call as const volatile
  1074. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  1075. _Result
  1076. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  1077. typename __disable_if_void<_Res>::type = 0) const volatile
  1078. {
  1079. return _M_f(_Mu<_Bound_args>()
  1080. (__volget<_Indexes>(_M_bound_args), __args)...);
  1081. }
  1082. // Call as const volatile, return void
  1083. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  1084. void
  1085. __call(tuple<_Args...>&& __args,
  1086. _Index_tuple<_Indexes...>,
  1087. typename __enable_if_void<_Res>::type = 0) const volatile
  1088. {
  1089. _M_f(_Mu<_Bound_args>()
  1090. (__volget<_Indexes>(_M_bound_args), __args)...);
  1091. }
  1092. public:
  1093. typedef _Result result_type;
  1094. template<typename... _Args>
  1095. explicit _Bind_result(const _Functor& __f, _Args&&... __args)
  1096. : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
  1097. { }
  1098. template<typename... _Args>
  1099. explicit _Bind_result(_Functor&& __f, _Args&&... __args)
  1100. : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
  1101. { }
  1102. _Bind_result(const _Bind_result&) = default;
  1103. _Bind_result(_Bind_result&& __b)
  1104. : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
  1105. { }
  1106. // Call unqualified
  1107. template<typename... _Args>
  1108. result_type
  1109. operator()(_Args&&... __args)
  1110. {
  1111. return this->__call<_Result>(
  1112. std::forward_as_tuple(std::forward<_Args>(__args)...),
  1113. _Bound_indexes());
  1114. }
  1115. // Call as const
  1116. template<typename... _Args>
  1117. result_type
  1118. operator()(_Args&&... __args) const
  1119. {
  1120. return this->__call<_Result>(
  1121. std::forward_as_tuple(std::forward<_Args>(__args)...),
  1122. _Bound_indexes());
  1123. }
  1124. // Call as volatile
  1125. template<typename... _Args>
  1126. result_type
  1127. operator()(_Args&&... __args) volatile
  1128. {
  1129. return this->__call<_Result>(
  1130. std::forward_as_tuple(std::forward<_Args>(__args)...),
  1131. _Bound_indexes());
  1132. }
  1133. // Call as const volatile
  1134. template<typename... _Args>
  1135. result_type
  1136. operator()(_Args&&... __args) const volatile
  1137. {
  1138. return this->__call<_Result>(
  1139. std::forward_as_tuple(std::forward<_Args>(__args)...),
  1140. _Bound_indexes());
  1141. }
  1142. };
  1143. /**
  1144. * @brief Class template _Bind is always a bind expression.
  1145. * @ingroup binders
  1146. */
  1147. template<typename _Signature>
  1148. struct is_bind_expression<_Bind<_Signature> >
  1149. : public true_type { };
  1150. /**
  1151. * @brief Class template _Bind is always a bind expression.
  1152. * @ingroup binders
  1153. */
  1154. template<typename _Signature>
  1155. struct is_bind_expression<const _Bind<_Signature> >
  1156. : public true_type { };
  1157. /**
  1158. * @brief Class template _Bind is always a bind expression.
  1159. * @ingroup binders
  1160. */
  1161. template<typename _Signature>
  1162. struct is_bind_expression<volatile _Bind<_Signature> >
  1163. : public true_type { };
  1164. /**
  1165. * @brief Class template _Bind is always a bind expression.
  1166. * @ingroup binders
  1167. */
  1168. template<typename _Signature>
  1169. struct is_bind_expression<const volatile _Bind<_Signature>>
  1170. : public true_type { };
  1171. /**
  1172. * @brief Class template _Bind_result is always a bind expression.
  1173. * @ingroup binders
  1174. */
  1175. template<typename _Result, typename _Signature>
  1176. struct is_bind_expression<_Bind_result<_Result, _Signature>>
  1177. : public true_type { };
  1178. /**
  1179. * @brief Class template _Bind_result is always a bind expression.
  1180. * @ingroup binders
  1181. */
  1182. template<typename _Result, typename _Signature>
  1183. struct is_bind_expression<const _Bind_result<_Result, _Signature>>
  1184. : public true_type { };
  1185. /**
  1186. * @brief Class template _Bind_result is always a bind expression.
  1187. * @ingroup binders
  1188. */
  1189. template<typename _Result, typename _Signature>
  1190. struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
  1191. : public true_type { };
  1192. /**
  1193. * @brief Class template _Bind_result is always a bind expression.
  1194. * @ingroup binders
  1195. */
  1196. template<typename _Result, typename _Signature>
  1197. struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
  1198. : public true_type { };
  1199. template<typename _Func, typename... _BoundArgs>
  1200. struct _Bind_check_arity { };
  1201. template<typename _Ret, typename... _Args, typename... _BoundArgs>
  1202. struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
  1203. {
  1204. static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
  1205. "Wrong number of arguments for function");
  1206. };
  1207. template<typename _Ret, typename... _Args, typename... _BoundArgs>
  1208. struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
  1209. {
  1210. static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
  1211. "Wrong number of arguments for function");
  1212. };
  1213. template<typename _Tp, typename _Class, typename... _BoundArgs>
  1214. struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
  1215. {
  1216. using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
  1217. using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
  1218. static_assert(_Varargs::value
  1219. ? sizeof...(_BoundArgs) >= _Arity::value + 1
  1220. : sizeof...(_BoundArgs) == _Arity::value + 1,
  1221. "Wrong number of arguments for pointer-to-member");
  1222. };
  1223. // Trait type used to remove std::bind() from overload set via SFINAE
  1224. // when first argument has integer type, so that std::bind() will
  1225. // not be a better match than ::bind() from the BSD Sockets API.
  1226. template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
  1227. using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
  1228. template<bool _SocketLike, typename _Func, typename... _BoundArgs>
  1229. struct _Bind_helper
  1230. : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
  1231. {
  1232. typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
  1233. __maybe_type;
  1234. typedef typename __maybe_type::type __func_type;
  1235. typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
  1236. };
  1237. // Partial specialization for is_socketlike == true, does not define
  1238. // nested type so std::bind() will not participate in overload resolution
  1239. // when the first argument might be a socket file descriptor.
  1240. template<typename _Func, typename... _BoundArgs>
  1241. struct _Bind_helper<true, _Func, _BoundArgs...>
  1242. { };
  1243. /**
  1244. * @brief Function template for std::bind.
  1245. * @ingroup binders
  1246. */
  1247. template<typename _Func, typename... _BoundArgs>
  1248. inline typename
  1249. _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
  1250. bind(_Func&& __f, _BoundArgs&&... __args)
  1251. {
  1252. typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
  1253. typedef typename __helper_type::__maybe_type __maybe_type;
  1254. typedef typename __helper_type::type __result_type;
  1255. return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
  1256. std::forward<_BoundArgs>(__args)...);
  1257. }
  1258. template<typename _Result, typename _Func, typename... _BoundArgs>
  1259. struct _Bindres_helper
  1260. : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
  1261. {
  1262. typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
  1263. __maybe_type;
  1264. typedef typename __maybe_type::type __functor_type;
  1265. typedef _Bind_result<_Result,
  1266. __functor_type(typename decay<_BoundArgs>::type...)>
  1267. type;
  1268. };
  1269. /**
  1270. * @brief Function template for std::bind<R>.
  1271. * @ingroup binders
  1272. */
  1273. template<typename _Result, typename _Func, typename... _BoundArgs>
  1274. inline
  1275. typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
  1276. bind(_Func&& __f, _BoundArgs&&... __args)
  1277. {
  1278. typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
  1279. typedef typename __helper_type::__maybe_type __maybe_type;
  1280. typedef typename __helper_type::type __result_type;
  1281. return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
  1282. std::forward<_BoundArgs>(__args)...);
  1283. }
  1284. template<typename _Signature>
  1285. struct _Bind_simple;
  1286. template<typename _Callable, typename... _Args>
  1287. struct _Bind_simple<_Callable(_Args...)>
  1288. {
  1289. typedef typename result_of<_Callable(_Args...)>::type result_type;
  1290. template<typename _Tp, typename... _Up>
  1291. explicit
  1292. _Bind_simple(_Tp&& __f, _Up&&... __args)
  1293. : _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...)
  1294. { }
  1295. _Bind_simple(const _Bind_simple&) = default;
  1296. _Bind_simple(_Bind_simple&&) = default;
  1297. result_type
  1298. operator()()
  1299. {
  1300. typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
  1301. return _M_invoke(_Indices());
  1302. }
  1303. private:
  1304. template<std::size_t... _Indices>
  1305. typename result_of<_Callable(_Args...)>::type
  1306. _M_invoke(_Index_tuple<_Indices...>)
  1307. {
  1308. // std::bind always forwards bound arguments as lvalues,
  1309. // but this type can call functions which only accept rvalues.
  1310. return std::forward<_Callable>(std::get<0>(_M_bound))(
  1311. std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
  1312. }
  1313. std::tuple<_Callable, _Args...> _M_bound;
  1314. };
  1315. template<typename _Func, typename... _BoundArgs>
  1316. struct _Bind_simple_helper
  1317. : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
  1318. {
  1319. typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
  1320. __maybe_type;
  1321. typedef typename __maybe_type::type __func_type;
  1322. typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
  1323. __type;
  1324. };
  1325. // Simplified version of std::bind for internal use, without support for
  1326. // unbound arguments, placeholders or nested bind expressions.
  1327. template<typename _Callable, typename... _Args>
  1328. typename _Bind_simple_helper<_Callable, _Args...>::__type
  1329. __bind_simple(_Callable&& __callable, _Args&&... __args)
  1330. {
  1331. typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
  1332. typedef typename __helper_type::__maybe_type __maybe_type;
  1333. typedef typename __helper_type::__type __result_type;
  1334. return __result_type(
  1335. __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
  1336. std::forward<_Args>(__args)...);
  1337. }
  1338. /**
  1339. * @brief Exception class thrown when class template function's
  1340. * operator() is called with an empty target.
  1341. * @ingroup exceptions
  1342. */
  1343. class bad_function_call : public std::exception
  1344. {
  1345. public:
  1346. virtual ~bad_function_call() noexcept;
  1347. const char* what() const noexcept;
  1348. };
  1349. /**
  1350. * Trait identifying "location-invariant" types, meaning that the
  1351. * address of the object (or any of its members) will not escape.
  1352. * Trivially copyable types are location-invariant and users can
  1353. * specialize this trait for other types.
  1354. */
  1355. template<typename _Tp>
  1356. struct __is_location_invariant
  1357. : is_trivially_copyable<_Tp>::type
  1358. { };
  1359. class _Undefined_class;
  1360. union _Nocopy_types
  1361. {
  1362. void* _M_object;
  1363. const void* _M_const_object;
  1364. void (*_M_function_pointer)();
  1365. void (_Undefined_class::*_M_member_pointer)();
  1366. };
  1367. union _Any_data
  1368. {
  1369. void* _M_access() { return &_M_pod_data[0]; }
  1370. const void* _M_access() const { return &_M_pod_data[0]; }
  1371. template<typename _Tp>
  1372. _Tp&
  1373. _M_access()
  1374. { return *static_cast<_Tp*>(_M_access()); }
  1375. template<typename _Tp>
  1376. const _Tp&
  1377. _M_access() const
  1378. { return *static_cast<const _Tp*>(_M_access()); }
  1379. _Nocopy_types _M_unused;
  1380. char _M_pod_data[sizeof(_Nocopy_types)];
  1381. };
  1382. enum _Manager_operation
  1383. {
  1384. __get_type_info,
  1385. __get_functor_ptr,
  1386. __clone_functor,
  1387. __destroy_functor
  1388. };
  1389. // Simple type wrapper that helps avoid annoying const problems
  1390. // when casting between void pointers and pointers-to-pointers.
  1391. template<typename _Tp>
  1392. struct _Simple_type_wrapper
  1393. {
  1394. _Simple_type_wrapper(_Tp __value) : __value(__value) { }
  1395. _Tp __value;
  1396. };
  1397. template<typename _Tp>
  1398. struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
  1399. : __is_location_invariant<_Tp>
  1400. { };
  1401. // Converts a reference to a function object into a callable
  1402. // function object.
  1403. template<typename _Functor>
  1404. inline _Functor&
  1405. __callable_functor(_Functor& __f)
  1406. { return __f; }
  1407. template<typename _Member, typename _Class>
  1408. inline _Mem_fn<_Member _Class::*>
  1409. __callable_functor(_Member _Class::* &__p)
  1410. { return std::mem_fn(__p); }
  1411. template<typename _Member, typename _Class>
  1412. inline _Mem_fn<_Member _Class::*>
  1413. __callable_functor(_Member _Class::* const &__p)
  1414. { return std::mem_fn(__p); }
  1415. template<typename _Member, typename _Class>
  1416. inline _Mem_fn<_Member _Class::*>
  1417. __callable_functor(_Member _Class::* volatile &__p)
  1418. { return std::mem_fn(__p); }
  1419. template<typename _Member, typename _Class>
  1420. inline _Mem_fn<_Member _Class::*>
  1421. __callable_functor(_Member _Class::* const volatile &__p)
  1422. { return std::mem_fn(__p); }
  1423. template<typename _Signature>
  1424. class function;
  1425. /// Base class of all polymorphic function object wrappers.
  1426. class _Function_base
  1427. {
  1428. public:
  1429. static const std::size_t _M_max_size = sizeof(_Nocopy_types);
  1430. static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
  1431. template<typename _Functor>
  1432. class _Base_manager
  1433. {
  1434. protected:
  1435. static const bool __stored_locally =
  1436. (__is_location_invariant<_Functor>::value
  1437. && sizeof(_Functor) <= _M_max_size
  1438. && __alignof__(_Functor) <= _M_max_align
  1439. && (_M_max_align % __alignof__(_Functor) == 0));
  1440. typedef integral_constant<bool, __stored_locally> _Local_storage;
  1441. // Retrieve a pointer to the function object
  1442. static _Functor*
  1443. _M_get_pointer(const _Any_data& __source)
  1444. {
  1445. const _Functor* __ptr =
  1446. __stored_locally? std::__addressof(__source._M_access<_Functor>())
  1447. /* have stored a pointer */ : __source._M_access<_Functor*>();
  1448. return const_cast<_Functor*>(__ptr);
  1449. }
  1450. // Clone a location-invariant function object that fits within
  1451. // an _Any_data structure.
  1452. static void
  1453. _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
  1454. {
  1455. new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
  1456. }
  1457. // Clone a function object that is not location-invariant or
  1458. // that cannot fit into an _Any_data structure.
  1459. static void
  1460. _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
  1461. {
  1462. __dest._M_access<_Functor*>() =
  1463. new _Functor(*__source._M_access<_Functor*>());
  1464. }
  1465. // Destroying a location-invariant object may still require
  1466. // destruction.
  1467. static void
  1468. _M_destroy(_Any_data& __victim, true_type)
  1469. {
  1470. __victim._M_access<_Functor>().~_Functor();
  1471. }
  1472. // Destroying an object located on the heap.
  1473. static void
  1474. _M_destroy(_Any_data& __victim, false_type)
  1475. {
  1476. delete __victim._M_access<_Functor*>();
  1477. }
  1478. public:
  1479. static bool
  1480. _M_manager(_Any_data& __dest, const _Any_data& __source,
  1481. _Manager_operation __op)
  1482. {
  1483. switch (__op)
  1484. {
  1485. #if __cpp_rtti
  1486. case __get_type_info:
  1487. __dest._M_access<const type_info*>() = &typeid(_Functor);
  1488. break;
  1489. #endif
  1490. case __get_functor_ptr:
  1491. __dest._M_access<_Functor*>() = _M_get_pointer(__source);
  1492. break;
  1493. case __clone_functor:
  1494. _M_clone(__dest, __source, _Local_storage());
  1495. break;
  1496. case __destroy_functor:
  1497. _M_destroy(__dest, _Local_storage());
  1498. break;
  1499. }
  1500. return false;
  1501. }
  1502. static void
  1503. _M_init_functor(_Any_data& __functor, _Functor&& __f)
  1504. { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
  1505. template<typename _Signature>
  1506. static bool
  1507. _M_not_empty_function(const function<_Signature>& __f)
  1508. { return static_cast<bool>(__f); }
  1509. template<typename _Tp>
  1510. static bool
  1511. _M_not_empty_function(_Tp* const& __fp)
  1512. { return __fp; }
  1513. template<typename _Class, typename _Tp>
  1514. static bool
  1515. _M_not_empty_function(_Tp _Class::* const& __mp)
  1516. { return __mp; }
  1517. template<typename _Tp>
  1518. static bool
  1519. _M_not_empty_function(const _Tp&)
  1520. { return true; }
  1521. private:
  1522. static void
  1523. _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
  1524. { new (__functor._M_access()) _Functor(std::move(__f)); }
  1525. static void
  1526. _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
  1527. { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
  1528. };
  1529. template<typename _Functor>
  1530. class _Ref_manager : public _Base_manager<_Functor*>
  1531. {
  1532. typedef _Function_base::_Base_manager<_Functor*> _Base;
  1533. public:
  1534. static bool
  1535. _M_manager(_Any_data& __dest, const _Any_data& __source,
  1536. _Manager_operation __op)
  1537. {
  1538. switch (__op)
  1539. {
  1540. #if __cpp_rtti
  1541. case __get_type_info:
  1542. __dest._M_access<const type_info*>() = &typeid(_Functor);
  1543. break;
  1544. #endif
  1545. case __get_functor_ptr:
  1546. __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
  1547. return is_const<_Functor>::value;
  1548. break;
  1549. default:
  1550. _Base::_M_manager(__dest, __source, __op);
  1551. }
  1552. return false;
  1553. }
  1554. static void
  1555. _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
  1556. {
  1557. _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
  1558. }
  1559. };
  1560. _Function_base() : _M_manager(nullptr) { }
  1561. ~_Function_base()
  1562. {
  1563. if (_M_manager)
  1564. _M_manager(_M_functor, _M_functor, __destroy_functor);
  1565. }
  1566. bool _M_empty() const { return !_M_manager; }
  1567. typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
  1568. _Manager_operation);
  1569. _Any_data _M_functor;
  1570. _Manager_type _M_manager;
  1571. };
  1572. template<typename _Signature, typename _Functor>
  1573. class _Function_handler;
  1574. template<typename _Res, typename _Functor, typename... _ArgTypes>
  1575. class _Function_handler<_Res(_ArgTypes...), _Functor>
  1576. : public _Function_base::_Base_manager<_Functor>
  1577. {
  1578. typedef _Function_base::_Base_manager<_Functor> _Base;
  1579. public:
  1580. static _Res
  1581. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1582. {
  1583. return (*_Base::_M_get_pointer(__functor))(
  1584. std::forward<_ArgTypes>(__args)...);
  1585. }
  1586. };
  1587. template<typename _Functor, typename... _ArgTypes>
  1588. class _Function_handler<void(_ArgTypes...), _Functor>
  1589. : public _Function_base::_Base_manager<_Functor>
  1590. {
  1591. typedef _Function_base::_Base_manager<_Functor> _Base;
  1592. public:
  1593. static void
  1594. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1595. {
  1596. (*_Base::_M_get_pointer(__functor))(
  1597. std::forward<_ArgTypes>(__args)...);
  1598. }
  1599. };
  1600. template<typename _Res, typename _Functor, typename... _ArgTypes>
  1601. class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
  1602. : public _Function_base::_Ref_manager<_Functor>
  1603. {
  1604. typedef _Function_base::_Ref_manager<_Functor> _Base;
  1605. public:
  1606. static _Res
  1607. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1608. {
  1609. return __callable_functor(**_Base::_M_get_pointer(__functor))(
  1610. std::forward<_ArgTypes>(__args)...);
  1611. }
  1612. };
  1613. template<typename _Functor, typename... _ArgTypes>
  1614. class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
  1615. : public _Function_base::_Ref_manager<_Functor>
  1616. {
  1617. typedef _Function_base::_Ref_manager<_Functor> _Base;
  1618. public:
  1619. static void
  1620. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1621. {
  1622. __callable_functor(**_Base::_M_get_pointer(__functor))(
  1623. std::forward<_ArgTypes>(__args)...);
  1624. }
  1625. };
  1626. template<typename _Class, typename _Member, typename _Res,
  1627. typename... _ArgTypes>
  1628. class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
  1629. : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
  1630. {
  1631. typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
  1632. _Base;
  1633. public:
  1634. static _Res
  1635. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1636. {
  1637. return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
  1638. std::forward<_ArgTypes>(__args)...);
  1639. }
  1640. };
  1641. template<typename _Class, typename _Member, typename... _ArgTypes>
  1642. class _Function_handler<void(_ArgTypes...), _Member _Class::*>
  1643. : public _Function_base::_Base_manager<
  1644. _Simple_type_wrapper< _Member _Class::* > >
  1645. {
  1646. typedef _Member _Class::* _Functor;
  1647. typedef _Simple_type_wrapper<_Functor> _Wrapper;
  1648. typedef _Function_base::_Base_manager<_Wrapper> _Base;
  1649. public:
  1650. static bool
  1651. _M_manager(_Any_data& __dest, const _Any_data& __source,
  1652. _Manager_operation __op)
  1653. {
  1654. switch (__op)
  1655. {
  1656. #if __cpp_rtti
  1657. case __get_type_info:
  1658. __dest._M_access<const type_info*>() = &typeid(_Functor);
  1659. break;
  1660. #endif
  1661. case __get_functor_ptr:
  1662. __dest._M_access<_Functor*>() =
  1663. &_Base::_M_get_pointer(__source)->__value;
  1664. break;
  1665. default:
  1666. _Base::_M_manager(__dest, __source, __op);
  1667. }
  1668. return false;
  1669. }
  1670. static void
  1671. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1672. {
  1673. std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
  1674. std::forward<_ArgTypes>(__args)...);
  1675. }
  1676. };
  1677. template<typename _From, typename _To>
  1678. using __check_func_return_type
  1679. = __or_<is_void<_To>, is_convertible<_From, _To>>;
  1680. /**
  1681. * @brief Primary class template for std::function.
  1682. * @ingroup functors
  1683. *
  1684. * Polymorphic function wrapper.
  1685. */
  1686. template<typename _Res, typename... _ArgTypes>
  1687. class function<_Res(_ArgTypes...)>
  1688. : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
  1689. private _Function_base
  1690. {
  1691. typedef _Res _Signature_type(_ArgTypes...);
  1692. template<typename _Functor>
  1693. using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
  1694. (std::declval<_ArgTypes>()...) );
  1695. // Used so the return type convertibility checks aren't done when
  1696. // performing overload resolution for copy construction/assignment.
  1697. template<typename _Tp>
  1698. using _NotSelf = __not_<is_same<_Tp, function>>;
  1699. template<typename _Functor>
  1700. using _Callable
  1701. = __and_<_NotSelf<_Functor>,
  1702. __check_func_return_type<_Invoke<_Functor>, _Res>>;
  1703. template<typename _Cond, typename _Tp>
  1704. using _Requires = typename enable_if<_Cond::value, _Tp>::type;
  1705. public:
  1706. typedef _Res result_type;
  1707. // [3.7.2.1] construct/copy/destroy
  1708. /**
  1709. * @brief Default construct creates an empty function call wrapper.
  1710. * @post @c !(bool)*this
  1711. */
  1712. function() noexcept
  1713. : _Function_base() { }
  1714. /**
  1715. * @brief Creates an empty function call wrapper.
  1716. * @post @c !(bool)*this
  1717. */
  1718. function(nullptr_t) noexcept
  1719. : _Function_base() { }
  1720. /**
  1721. * @brief %Function copy constructor.
  1722. * @param __x A %function object with identical call signature.
  1723. * @post @c bool(*this) == bool(__x)
  1724. *
  1725. * The newly-created %function contains a copy of the target of @a
  1726. * __x (if it has one).
  1727. */
  1728. function(const function& __x);
  1729. /**
  1730. * @brief %Function move constructor.
  1731. * @param __x A %function object rvalue with identical call signature.
  1732. *
  1733. * The newly-created %function contains the target of @a __x
  1734. * (if it has one).
  1735. */
  1736. function(function&& __x) : _Function_base()
  1737. {
  1738. __x.swap(*this);
  1739. }
  1740. // TODO: needs allocator_arg_t
  1741. /**
  1742. * @brief Builds a %function that targets a copy of the incoming
  1743. * function object.
  1744. * @param __f A %function object that is callable with parameters of
  1745. * type @c T1, @c T2, ..., @c TN and returns a value convertible
  1746. * to @c Res.
  1747. *
  1748. * The newly-created %function object will target a copy of
  1749. * @a __f. If @a __f is @c reference_wrapper<F>, then this function
  1750. * object will contain a reference to the function object @c
  1751. * __f.get(). If @a __f is a NULL function pointer or NULL
  1752. * pointer-to-member, the newly-created object will be empty.
  1753. *
  1754. * If @a __f is a non-NULL function pointer or an object of type @c
  1755. * reference_wrapper<F>, this function will not throw.
  1756. */
  1757. template<typename _Functor,
  1758. typename = _Requires<_Callable<_Functor>, void>>
  1759. function(_Functor);
  1760. /**
  1761. * @brief %Function assignment operator.
  1762. * @param __x A %function with identical call signature.
  1763. * @post @c (bool)*this == (bool)x
  1764. * @returns @c *this
  1765. *
  1766. * The target of @a __x is copied to @c *this. If @a __x has no
  1767. * target, then @c *this will be empty.
  1768. *
  1769. * If @a __x targets a function pointer or a reference to a function
  1770. * object, then this operation will not throw an %exception.
  1771. */
  1772. function&
  1773. operator=(const function& __x)
  1774. {
  1775. function(__x).swap(*this);
  1776. return *this;
  1777. }
  1778. /**
  1779. * @brief %Function move-assignment operator.
  1780. * @param __x A %function rvalue with identical call signature.
  1781. * @returns @c *this
  1782. *
  1783. * The target of @a __x is moved to @c *this. If @a __x has no
  1784. * target, then @c *this will be empty.
  1785. *
  1786. * If @a __x targets a function pointer or a reference to a function
  1787. * object, then this operation will not throw an %exception.
  1788. */
  1789. function&
  1790. operator=(function&& __x)
  1791. {
  1792. function(std::move(__x)).swap(*this);
  1793. return *this;
  1794. }
  1795. /**
  1796. * @brief %Function assignment to zero.
  1797. * @post @c !(bool)*this
  1798. * @returns @c *this
  1799. *
  1800. * The target of @c *this is deallocated, leaving it empty.
  1801. */
  1802. function&
  1803. operator=(nullptr_t) noexcept
  1804. {
  1805. if (_M_manager)
  1806. {
  1807. _M_manager(_M_functor, _M_functor, __destroy_functor);
  1808. _M_manager = nullptr;
  1809. _M_invoker = nullptr;
  1810. }
  1811. return *this;
  1812. }
  1813. /**
  1814. * @brief %Function assignment to a new target.
  1815. * @param __f A %function object that is callable with parameters of
  1816. * type @c T1, @c T2, ..., @c TN and returns a value convertible
  1817. * to @c Res.
  1818. * @return @c *this
  1819. *
  1820. * This %function object wrapper will target a copy of @a
  1821. * __f. If @a __f is @c reference_wrapper<F>, then this function
  1822. * object will contain a reference to the function object @c
  1823. * __f.get(). If @a __f is a NULL function pointer or NULL
  1824. * pointer-to-member, @c this object will be empty.
  1825. *
  1826. * If @a __f is a non-NULL function pointer or an object of type @c
  1827. * reference_wrapper<F>, this function will not throw.
  1828. */
  1829. template<typename _Functor>
  1830. _Requires<_Callable<typename decay<_Functor>::type>, function&>
  1831. operator=(_Functor&& __f)
  1832. {
  1833. function(std::forward<_Functor>(__f)).swap(*this);
  1834. return *this;
  1835. }
  1836. /// @overload
  1837. template<typename _Functor>
  1838. function&
  1839. operator=(reference_wrapper<_Functor> __f) noexcept
  1840. {
  1841. function(__f).swap(*this);
  1842. return *this;
  1843. }
  1844. // [3.7.2.2] function modifiers
  1845. /**
  1846. * @brief Swap the targets of two %function objects.
  1847. * @param __x A %function with identical call signature.
  1848. *
  1849. * Swap the targets of @c this function object and @a __f. This
  1850. * function will not throw an %exception.
  1851. */
  1852. void swap(function& __x)
  1853. {
  1854. std::swap(_M_functor, __x._M_functor);
  1855. std::swap(_M_manager, __x._M_manager);
  1856. std::swap(_M_invoker, __x._M_invoker);
  1857. }
  1858. // TODO: needs allocator_arg_t
  1859. /*
  1860. template<typename _Functor, typename _Alloc>
  1861. void
  1862. assign(_Functor&& __f, const _Alloc& __a)
  1863. {
  1864. function(allocator_arg, __a,
  1865. std::forward<_Functor>(__f)).swap(*this);
  1866. }
  1867. */
  1868. // [3.7.2.3] function capacity
  1869. /**
  1870. * @brief Determine if the %function wrapper has a target.
  1871. *
  1872. * @return @c true when this %function object contains a target,
  1873. * or @c false when it is empty.
  1874. *
  1875. * This function will not throw an %exception.
  1876. */
  1877. explicit operator bool() const noexcept
  1878. { return !_M_empty(); }
  1879. // [3.7.2.4] function invocation
  1880. /**
  1881. * @brief Invokes the function targeted by @c *this.
  1882. * @returns the result of the target.
  1883. * @throws bad_function_call when @c !(bool)*this
  1884. *
  1885. * The function call operator invokes the target function object
  1886. * stored by @c this.
  1887. */
  1888. _Res operator()(_ArgTypes... __args) const;
  1889. #if __cpp_rtti
  1890. // [3.7.2.5] function target access
  1891. /**
  1892. * @brief Determine the type of the target of this function object
  1893. * wrapper.
  1894. *
  1895. * @returns the type identifier of the target function object, or
  1896. * @c typeid(void) if @c !(bool)*this.
  1897. *
  1898. * This function will not throw an %exception.
  1899. */
  1900. const type_info& target_type() const noexcept;
  1901. /**
  1902. * @brief Access the stored target function object.
  1903. *
  1904. * @return Returns a pointer to the stored target function object,
  1905. * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
  1906. * pointer.
  1907. *
  1908. * This function will not throw an %exception.
  1909. */
  1910. template<typename _Functor> _Functor* target() noexcept;
  1911. /// @overload
  1912. template<typename _Functor> const _Functor* target() const noexcept;
  1913. #endif
  1914. private:
  1915. using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
  1916. _Invoker_type _M_invoker;
  1917. };
  1918. // Out-of-line member definitions.
  1919. template<typename _Res, typename... _ArgTypes>
  1920. function<_Res(_ArgTypes...)>::
  1921. function(const function& __x)
  1922. : _Function_base()
  1923. {
  1924. if (static_cast<bool>(__x))
  1925. {
  1926. __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
  1927. _M_invoker = __x._M_invoker;
  1928. _M_manager = __x._M_manager;
  1929. }
  1930. }
  1931. template<typename _Res, typename... _ArgTypes>
  1932. template<typename _Functor, typename>
  1933. function<_Res(_ArgTypes...)>::
  1934. function(_Functor __f)
  1935. : _Function_base()
  1936. {
  1937. typedef _Function_handler<_Signature_type, _Functor> _My_handler;
  1938. if (_My_handler::_M_not_empty_function(__f))
  1939. {
  1940. _My_handler::_M_init_functor(_M_functor, std::move(__f));
  1941. _M_invoker = &_My_handler::_M_invoke;
  1942. _M_manager = &_My_handler::_M_manager;
  1943. }
  1944. }
  1945. template<typename _Res, typename... _ArgTypes>
  1946. _Res
  1947. function<_Res(_ArgTypes...)>::
  1948. operator()(_ArgTypes... __args) const
  1949. {
  1950. if (_M_empty())
  1951. __throw_bad_function_call();
  1952. return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
  1953. }
  1954. #if __cpp_rtti
  1955. template<typename _Res, typename... _ArgTypes>
  1956. const type_info&
  1957. function<_Res(_ArgTypes...)>::
  1958. target_type() const noexcept
  1959. {
  1960. if (_M_manager)
  1961. {
  1962. _Any_data __typeinfo_result;
  1963. _M_manager(__typeinfo_result, _M_functor, __get_type_info);
  1964. return *__typeinfo_result._M_access<const type_info*>();
  1965. }
  1966. else
  1967. return typeid(void);
  1968. }
  1969. template<typename _Res, typename... _ArgTypes>
  1970. template<typename _Functor>
  1971. _Functor*
  1972. function<_Res(_ArgTypes...)>::
  1973. target() noexcept
  1974. {
  1975. if (typeid(_Functor) == target_type() && _M_manager)
  1976. {
  1977. _Any_data __ptr;
  1978. if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
  1979. && !is_const<_Functor>::value)
  1980. return 0;
  1981. else
  1982. return __ptr._M_access<_Functor*>();
  1983. }
  1984. else
  1985. return 0;
  1986. }
  1987. template<typename _Res, typename... _ArgTypes>
  1988. template<typename _Functor>
  1989. const _Functor*
  1990. function<_Res(_ArgTypes...)>::
  1991. target() const noexcept
  1992. {
  1993. if (typeid(_Functor) == target_type() && _M_manager)
  1994. {
  1995. _Any_data __ptr;
  1996. _M_manager(__ptr, _M_functor, __get_functor_ptr);
  1997. return __ptr._M_access<const _Functor*>();
  1998. }
  1999. else
  2000. return 0;
  2001. }
  2002. #endif
  2003. // [20.7.15.2.6] null pointer comparisons
  2004. /**
  2005. * @brief Compares a polymorphic function object wrapper against 0
  2006. * (the NULL pointer).
  2007. * @returns @c true if the wrapper has no target, @c false otherwise
  2008. *
  2009. * This function will not throw an %exception.
  2010. */
  2011. template<typename _Res, typename... _Args>
  2012. inline bool
  2013. operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
  2014. { return !static_cast<bool>(__f); }
  2015. /// @overload
  2016. template<typename _Res, typename... _Args>
  2017. inline bool
  2018. operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
  2019. { return !static_cast<bool>(__f); }
  2020. /**
  2021. * @brief Compares a polymorphic function object wrapper against 0
  2022. * (the NULL pointer).
  2023. * @returns @c false if the wrapper has no target, @c true otherwise
  2024. *
  2025. * This function will not throw an %exception.
  2026. */
  2027. template<typename _Res, typename... _Args>
  2028. inline bool
  2029. operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
  2030. { return static_cast<bool>(__f); }
  2031. /// @overload
  2032. template<typename _Res, typename... _Args>
  2033. inline bool
  2034. operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
  2035. { return static_cast<bool>(__f); }
  2036. // [20.7.15.2.7] specialized algorithms
  2037. /**
  2038. * @brief Swap the targets of two polymorphic function object wrappers.
  2039. *
  2040. * This function will not throw an %exception.
  2041. */
  2042. template<typename _Res, typename... _Args>
  2043. inline void
  2044. swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
  2045. { __x.swap(__y); }
  2046. _GLIBCXX_END_NAMESPACE_VERSION
  2047. } // namespace std
  2048. #endif // C++11
  2049. #endif // _GLIBCXX_FUNCTIONAL