functional 69 KB

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