functional 69 KB

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