functional 69 KB

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