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