You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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