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