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