Makefile.am: Add functional.cc, shared_ptr.cc.
[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 public:
1479 virtual ~bad_function_call() throw();
1480 };
1481
1482 /**
1483 * Trait identifying "location-invariant" types, meaning that the
1484 * address of the object (or any of its members) will not escape.
1485 * Also implies a trivial copy constructor and assignment operator.
1486 */
1487 template<typename _Tp>
1488 struct __is_location_invariant
1489 : integral_constant<bool, (is_pointer<_Tp>::value
1490 || is_member_pointer<_Tp>::value)>
1491 { };
1492
1493 class _Undefined_class;
1494
1495 union _Nocopy_types
1496 {
1497 void* _M_object;
1498 const void* _M_const_object;
1499 void (*_M_function_pointer)();
1500 void (_Undefined_class::*_M_member_pointer)();
1501 };
1502
1503 union _Any_data
1504 {
1505 void* _M_access() { return &_M_pod_data[0]; }
1506 const void* _M_access() const { return &_M_pod_data[0]; }
1507
1508 template<typename _Tp>
1509 _Tp&
1510 _M_access()
1511 { return *static_cast<_Tp*>(_M_access()); }
1512
1513 template<typename _Tp>
1514 const _Tp&
1515 _M_access() const
1516 { return *static_cast<const _Tp*>(_M_access()); }
1517
1518 _Nocopy_types _M_unused;
1519 char _M_pod_data[sizeof(_Nocopy_types)];
1520 };
1521
1522 enum _Manager_operation
1523 {
1524 __get_type_info,
1525 __get_functor_ptr,
1526 __clone_functor,
1527 __destroy_functor
1528 };
1529
1530 // Simple type wrapper that helps avoid annoying const problems
1531 // when casting between void pointers and pointers-to-pointers.
1532 template<typename _Tp>
1533 struct _Simple_type_wrapper
1534 {
1535 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1536
1537 _Tp __value;
1538 };
1539
1540 template<typename _Tp>
1541 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1542 : __is_location_invariant<_Tp>
1543 { };
1544
1545 // Converts a reference to a function object into a callable
1546 // function object.
1547 template<typename _Functor>
1548 inline _Functor&
1549 __callable_functor(_Functor& __f)
1550 { return __f; }
1551
1552 template<typename _Member, typename _Class>
1553 inline _Mem_fn<_Member _Class::*>
1554 __callable_functor(_Member _Class::* &__p)
1555 { return mem_fn(__p); }
1556
1557 template<typename _Member, typename _Class>
1558 inline _Mem_fn<_Member _Class::*>
1559 __callable_functor(_Member _Class::* const &__p)
1560 { return mem_fn(__p); }
1561
1562 template<typename _Signature>
1563 class function;
1564
1565 /// Base class of all polymorphic function object wrappers.
1566 class _Function_base
1567 {
1568 public:
1569 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1570 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1571
1572 template<typename _Functor>
1573 class _Base_manager
1574 {
1575 protected:
1576 static const bool __stored_locally =
1577 (__is_location_invariant<_Functor>::value
1578 && sizeof(_Functor) <= _M_max_size
1579 && __alignof__(_Functor) <= _M_max_align
1580 && (_M_max_align % __alignof__(_Functor) == 0));
1581
1582 typedef integral_constant<bool, __stored_locally> _Local_storage;
1583
1584 // Retrieve a pointer to the function object
1585 static _Functor*
1586 _M_get_pointer(const _Any_data& __source)
1587 {
1588 const _Functor* __ptr =
1589 __stored_locally? &__source._M_access<_Functor>()
1590 /* have stored a pointer */ : __source._M_access<_Functor*>();
1591 return const_cast<_Functor*>(__ptr);
1592 }
1593
1594 // Clone a location-invariant function object that fits within
1595 // an _Any_data structure.
1596 static void
1597 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1598 {
1599 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1600 }
1601
1602 // Clone a function object that is not location-invariant or
1603 // that cannot fit into an _Any_data structure.
1604 static void
1605 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1606 {
1607 __dest._M_access<_Functor*>() =
1608 new _Functor(*__source._M_access<_Functor*>());
1609 }
1610
1611 // Destroying a location-invariant object may still require
1612 // destruction.
1613 static void
1614 _M_destroy(_Any_data& __victim, true_type)
1615 {
1616 __victim._M_access<_Functor>().~_Functor();
1617 }
1618
1619 // Destroying an object located on the heap.
1620 static void
1621 _M_destroy(_Any_data& __victim, false_type)
1622 {
1623 delete __victim._M_access<_Functor*>();
1624 }
1625
1626 public:
1627 static bool
1628 _M_manager(_Any_data& __dest, const _Any_data& __source,
1629 _Manager_operation __op)
1630 {
1631 switch (__op)
1632 {
1633 #ifdef __GXX_RTTI
1634 case __get_type_info:
1635 __dest._M_access<const type_info*>() = &typeid(_Functor);
1636 break;
1637 #endif
1638 case __get_functor_ptr:
1639 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1640 break;
1641
1642 case __clone_functor:
1643 _M_clone(__dest, __source, _Local_storage());
1644 break;
1645
1646 case __destroy_functor:
1647 _M_destroy(__dest, _Local_storage());
1648 break;
1649 }
1650 return false;
1651 }
1652
1653 static void
1654 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1655 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1656
1657 template<typename _Signature>
1658 static bool
1659 _M_not_empty_function(const function<_Signature>& __f)
1660 { return static_cast<bool>(__f); }
1661
1662 template<typename _Tp>
1663 static bool
1664 _M_not_empty_function(const _Tp*& __fp)
1665 { return __fp; }
1666
1667 template<typename _Class, typename _Tp>
1668 static bool
1669 _M_not_empty_function(_Tp _Class::* const& __mp)
1670 { return __mp; }
1671
1672 template<typename _Tp>
1673 static bool
1674 _M_not_empty_function(const _Tp&)
1675 { return true; }
1676
1677 private:
1678 static void
1679 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1680 { new (__functor._M_access()) _Functor(std::move(__f)); }
1681
1682 static void
1683 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1684 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1685 };
1686
1687 template<typename _Functor>
1688 class _Ref_manager : public _Base_manager<_Functor*>
1689 {
1690 typedef _Function_base::_Base_manager<_Functor*> _Base;
1691
1692 public:
1693 static bool
1694 _M_manager(_Any_data& __dest, const _Any_data& __source,
1695 _Manager_operation __op)
1696 {
1697 switch (__op)
1698 {
1699 #ifdef __GXX_RTTI
1700 case __get_type_info:
1701 __dest._M_access<const type_info*>() = &typeid(_Functor);
1702 break;
1703 #endif
1704 case __get_functor_ptr:
1705 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1706 return is_const<_Functor>::value;
1707 break;
1708
1709 default:
1710 _Base::_M_manager(__dest, __source, __op);
1711 }
1712 return false;
1713 }
1714
1715 static void
1716 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1717 {
1718 // TBD: Use address_of function instead.
1719 _Base::_M_init_functor(__functor, &__f.get());
1720 }
1721 };
1722
1723 _Function_base() : _M_manager(0) { }
1724
1725 ~_Function_base()
1726 {
1727 if (_M_manager)
1728 _M_manager(_M_functor, _M_functor, __destroy_functor);
1729 }
1730
1731
1732 bool _M_empty() const { return !_M_manager; }
1733
1734 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1735 _Manager_operation);
1736
1737 _Any_data _M_functor;
1738 _Manager_type _M_manager;
1739 };
1740
1741 template<typename _Signature, typename _Functor>
1742 class _Function_handler;
1743
1744 template<typename _Res, typename _Functor, typename... _ArgTypes>
1745 class _Function_handler<_Res(_ArgTypes...), _Functor>
1746 : public _Function_base::_Base_manager<_Functor>
1747 {
1748 typedef _Function_base::_Base_manager<_Functor> _Base;
1749
1750 public:
1751 static _Res
1752 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1753 {
1754 return (*_Base::_M_get_pointer(__functor))(
1755 std::forward<_ArgTypes>(__args)...);
1756 }
1757 };
1758
1759 template<typename _Functor, typename... _ArgTypes>
1760 class _Function_handler<void(_ArgTypes...), _Functor>
1761 : public _Function_base::_Base_manager<_Functor>
1762 {
1763 typedef _Function_base::_Base_manager<_Functor> _Base;
1764
1765 public:
1766 static void
1767 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1768 {
1769 (*_Base::_M_get_pointer(__functor))(
1770 std::forward<_ArgTypes>(__args)...);
1771 }
1772 };
1773
1774 template<typename _Res, typename _Functor, typename... _ArgTypes>
1775 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1776 : public _Function_base::_Ref_manager<_Functor>
1777 {
1778 typedef _Function_base::_Ref_manager<_Functor> _Base;
1779
1780 public:
1781 static _Res
1782 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1783 {
1784 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1785 std::forward<_ArgTypes>(__args)...);
1786 }
1787 };
1788
1789 template<typename _Functor, typename... _ArgTypes>
1790 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1791 : public _Function_base::_Ref_manager<_Functor>
1792 {
1793 typedef _Function_base::_Ref_manager<_Functor> _Base;
1794
1795 public:
1796 static void
1797 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1798 {
1799 __callable_functor(**_Base::_M_get_pointer(__functor))(
1800 std::forward<_ArgTypes>(__args)...);
1801 }
1802 };
1803
1804 template<typename _Class, typename _Member, typename _Res,
1805 typename... _ArgTypes>
1806 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1807 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1808 {
1809 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1810 _Base;
1811
1812 public:
1813 static _Res
1814 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1815 {
1816 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1817 std::forward<_ArgTypes>(__args)...);
1818 }
1819 };
1820
1821 template<typename _Class, typename _Member, typename... _ArgTypes>
1822 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1823 : public _Function_base::_Base_manager<
1824 _Simple_type_wrapper< _Member _Class::* > >
1825 {
1826 typedef _Member _Class::* _Functor;
1827 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1828 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1829
1830 public:
1831 static bool
1832 _M_manager(_Any_data& __dest, const _Any_data& __source,
1833 _Manager_operation __op)
1834 {
1835 switch (__op)
1836 {
1837 #ifdef __GXX_RTTI
1838 case __get_type_info:
1839 __dest._M_access<const type_info*>() = &typeid(_Functor);
1840 break;
1841 #endif
1842 case __get_functor_ptr:
1843 __dest._M_access<_Functor*>() =
1844 &_Base::_M_get_pointer(__source)->__value;
1845 break;
1846
1847 default:
1848 _Base::_M_manager(__dest, __source, __op);
1849 }
1850 return false;
1851 }
1852
1853 static void
1854 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1855 {
1856 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1857 std::forward<_ArgTypes>(__args)...);
1858 }
1859 };
1860
1861 /**
1862 * @brief Primary class template for std::function.
1863 * @ingroup functors
1864 *
1865 * Polymorphic function wrapper.
1866 */
1867 template<typename _Res, typename... _ArgTypes>
1868 class function<_Res(_ArgTypes...)>
1869 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1870 private _Function_base
1871 {
1872 typedef _Res _Signature_type(_ArgTypes...);
1873
1874 struct _Useless { };
1875
1876 public:
1877 typedef _Res result_type;
1878
1879 // [3.7.2.1] construct/copy/destroy
1880
1881 /**
1882 * @brief Default construct creates an empty function call wrapper.
1883 * @post @c !(bool)*this
1884 */
1885 function() : _Function_base() { }
1886
1887 /**
1888 * @brief Creates an empty function call wrapper.
1889 * @post @c !(bool)*this
1890 */
1891 function(nullptr_t) : _Function_base() { }
1892
1893 /**
1894 * @brief %Function copy constructor.
1895 * @param x A %function object with identical call signature.
1896 * @post @c (bool)*this == (bool)x
1897 *
1898 * The newly-created %function contains a copy of the target of @a
1899 * x (if it has one).
1900 */
1901 function(const function& __x);
1902
1903 /**
1904 * @brief %Function move constructor.
1905 * @param x A %function object rvalue with identical call signature.
1906 *
1907 * The newly-created %function contains the target of @a x
1908 * (if it has one).
1909 */
1910 function(function&& __x) : _Function_base()
1911 {
1912 __x.swap(*this);
1913 }
1914
1915 // TODO: needs allocator_arg_t
1916
1917 /**
1918 * @brief Builds a %function that targets a copy of the incoming
1919 * function object.
1920 * @param f A %function object that is callable with parameters of
1921 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1922 * to @c Res.
1923 *
1924 * The newly-created %function object will target a copy of @a
1925 * f. If @a f is @c reference_wrapper<F>, then this function
1926 * object will contain a reference to the function object @c
1927 * f.get(). If @a f is a NULL function pointer or NULL
1928 * pointer-to-member, the newly-created object will be empty.
1929 *
1930 * If @a f is a non-NULL function pointer or an object of type @c
1931 * reference_wrapper<F>, this function will not throw.
1932 */
1933 template<typename _Functor>
1934 function(_Functor __f,
1935 typename enable_if<
1936 !is_integral<_Functor>::value, _Useless>::type
1937 = _Useless());
1938
1939 /**
1940 * @brief %Function assignment operator.
1941 * @param x A %function with identical call signature.
1942 * @post @c (bool)*this == (bool)x
1943 * @returns @c *this
1944 *
1945 * The target of @a x is copied to @c *this. If @a x has no
1946 * target, then @c *this will be empty.
1947 *
1948 * If @a x targets a function pointer or a reference to a function
1949 * object, then this operation will not throw an %exception.
1950 */
1951 function&
1952 operator=(const function& __x)
1953 {
1954 function(__x).swap(*this);
1955 return *this;
1956 }
1957
1958 /**
1959 * @brief %Function move-assignment operator.
1960 * @param x A %function rvalue with identical call signature.
1961 * @returns @c *this
1962 *
1963 * The target of @a x is moved to @c *this. If @a x has no
1964 * target, then @c *this will be empty.
1965 *
1966 * If @a x targets a function pointer or a reference to a function
1967 * object, then this operation will not throw an %exception.
1968 */
1969 function&
1970 operator=(function&& __x)
1971 {
1972 function(std::move(__x)).swap(*this);
1973 return *this;
1974 }
1975
1976 /**
1977 * @brief %Function assignment to zero.
1978 * @post @c !(bool)*this
1979 * @returns @c *this
1980 *
1981 * The target of @c *this is deallocated, leaving it empty.
1982 */
1983 function&
1984 operator=(nullptr_t)
1985 {
1986 if (_M_manager)
1987 {
1988 _M_manager(_M_functor, _M_functor, __destroy_functor);
1989 _M_manager = 0;
1990 _M_invoker = 0;
1991 }
1992 return *this;
1993 }
1994
1995 /**
1996 * @brief %Function assignment to a new target.
1997 * @param f A %function object that is callable with parameters of
1998 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1999 * to @c Res.
2000 * @return @c *this
2001 *
2002 * This %function object wrapper will target a copy of @a
2003 * f. If @a f is @c reference_wrapper<F>, then this function
2004 * object will contain a reference to the function object @c
2005 * f.get(). If @a f is a NULL function pointer or NULL
2006 * pointer-to-member, @c this object will be empty.
2007 *
2008 * If @a f is a non-NULL function pointer or an object of type @c
2009 * reference_wrapper<F>, this function will not throw.
2010 */
2011 template<typename _Functor>
2012 typename enable_if<!is_integral<_Functor>::value, function&>::type
2013 operator=(_Functor&& __f)
2014 {
2015 function(std::forward<_Functor>(__f)).swap(*this);
2016 return *this;
2017 }
2018
2019 /// @overload
2020 template<typename _Functor>
2021 typename enable_if<!is_integral<_Functor>::value, function&>::type
2022 operator=(reference_wrapper<_Functor> __f)
2023 {
2024 function(__f).swap(*this);
2025 return *this;
2026 }
2027
2028 // [3.7.2.2] function modifiers
2029
2030 /**
2031 * @brief Swap the targets of two %function objects.
2032 * @param f A %function with identical call signature.
2033 *
2034 * Swap the targets of @c this function object and @a f. This
2035 * function will not throw an %exception.
2036 */
2037 void swap(function& __x)
2038 {
2039 std::swap(_M_functor, __x._M_functor);
2040 std::swap(_M_manager, __x._M_manager);
2041 std::swap(_M_invoker, __x._M_invoker);
2042 }
2043
2044 // TODO: needs allocator_arg_t
2045 /*
2046 template<typename _Functor, typename _Alloc>
2047 void
2048 assign(_Functor&& __f, const _Alloc& __a)
2049 {
2050 function(allocator_arg, __a,
2051 std::forward<_Functor>(__f)).swap(*this);
2052 }
2053 */
2054
2055 // [3.7.2.3] function capacity
2056
2057 /**
2058 * @brief Determine if the %function wrapper has a target.
2059 *
2060 * @return @c true when this %function object contains a target,
2061 * or @c false when it is empty.
2062 *
2063 * This function will not throw an %exception.
2064 */
2065 explicit operator bool() const
2066 { return !_M_empty(); }
2067
2068 // [3.7.2.4] function invocation
2069
2070 /**
2071 * @brief Invokes the function targeted by @c *this.
2072 * @returns the result of the target.
2073 * @throws bad_function_call when @c !(bool)*this
2074 *
2075 * The function call operator invokes the target function object
2076 * stored by @c this.
2077 */
2078 _Res operator()(_ArgTypes... __args) const;
2079
2080 #ifdef __GXX_RTTI
2081 // [3.7.2.5] function target access
2082 /**
2083 * @brief Determine the type of the target of this function object
2084 * wrapper.
2085 *
2086 * @returns the type identifier of the target function object, or
2087 * @c typeid(void) if @c !(bool)*this.
2088 *
2089 * This function will not throw an %exception.
2090 */
2091 const type_info& target_type() const;
2092
2093 /**
2094 * @brief Access the stored target function object.
2095 *
2096 * @return Returns a pointer to the stored target function object,
2097 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2098 * pointer.
2099 *
2100 * This function will not throw an %exception.
2101 */
2102 template<typename _Functor> _Functor* target();
2103
2104 /// @overload
2105 template<typename _Functor> const _Functor* target() const;
2106 #endif
2107
2108 private:
2109 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2110 _Invoker_type _M_invoker;
2111 };
2112
2113 // Out-of-line member definitions.
2114 template<typename _Res, typename... _ArgTypes>
2115 function<_Res(_ArgTypes...)>::
2116 function(const function& __x)
2117 : _Function_base()
2118 {
2119 if (static_cast<bool>(__x))
2120 {
2121 _M_invoker = __x._M_invoker;
2122 _M_manager = __x._M_manager;
2123 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2124 }
2125 }
2126
2127 template<typename _Res, typename... _ArgTypes>
2128 template<typename _Functor>
2129 function<_Res(_ArgTypes...)>::
2130 function(_Functor __f,
2131 typename enable_if<
2132 !is_integral<_Functor>::value, _Useless>::type)
2133 : _Function_base()
2134 {
2135 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2136
2137 if (_My_handler::_M_not_empty_function(__f))
2138 {
2139 _M_invoker = &_My_handler::_M_invoke;
2140 _M_manager = &_My_handler::_M_manager;
2141 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2142 }
2143 }
2144
2145 template<typename _Res, typename... _ArgTypes>
2146 _Res
2147 function<_Res(_ArgTypes...)>::
2148 operator()(_ArgTypes... __args) const
2149 {
2150 if (_M_empty())
2151 __throw_bad_function_call();
2152 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2153 }
2154
2155 #ifdef __GXX_RTTI
2156 template<typename _Res, typename... _ArgTypes>
2157 const type_info&
2158 function<_Res(_ArgTypes...)>::
2159 target_type() const
2160 {
2161 if (_M_manager)
2162 {
2163 _Any_data __typeinfo_result;
2164 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2165 return *__typeinfo_result._M_access<const type_info*>();
2166 }
2167 else
2168 return typeid(void);
2169 }
2170
2171 template<typename _Res, typename... _ArgTypes>
2172 template<typename _Functor>
2173 _Functor*
2174 function<_Res(_ArgTypes...)>::
2175 target()
2176 {
2177 if (typeid(_Functor) == target_type() && _M_manager)
2178 {
2179 _Any_data __ptr;
2180 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2181 && !is_const<_Functor>::value)
2182 return 0;
2183 else
2184 return __ptr._M_access<_Functor*>();
2185 }
2186 else
2187 return 0;
2188 }
2189
2190 template<typename _Res, typename... _ArgTypes>
2191 template<typename _Functor>
2192 const _Functor*
2193 function<_Res(_ArgTypes...)>::
2194 target() const
2195 {
2196 if (typeid(_Functor) == target_type() && _M_manager)
2197 {
2198 _Any_data __ptr;
2199 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2200 return __ptr._M_access<const _Functor*>();
2201 }
2202 else
2203 return 0;
2204 }
2205 #endif
2206
2207 // [20.7.15.2.6] null pointer comparisons
2208
2209 /**
2210 * @brief Compares a polymorphic function object wrapper against 0
2211 * (the NULL pointer).
2212 * @returns @c true if the wrapper has no target, @c false otherwise
2213 *
2214 * This function will not throw an %exception.
2215 */
2216 template<typename _Res, typename... _Args>
2217 inline bool
2218 operator==(const function<_Res(_Args...)>& __f, nullptr_t)
2219 { return !static_cast<bool>(__f); }
2220
2221 /// @overload
2222 template<typename _Res, typename... _Args>
2223 inline bool
2224 operator==(nullptr_t, const function<_Res(_Args...)>& __f)
2225 { return !static_cast<bool>(__f); }
2226
2227 /**
2228 * @brief Compares a polymorphic function object wrapper against 0
2229 * (the NULL pointer).
2230 * @returns @c false if the wrapper has no target, @c true otherwise
2231 *
2232 * This function will not throw an %exception.
2233 */
2234 template<typename _Res, typename... _Args>
2235 inline bool
2236 operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
2237 { return static_cast<bool>(__f); }
2238
2239 /// @overload
2240 template<typename _Res, typename... _Args>
2241 inline bool
2242 operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
2243 { return static_cast<bool>(__f); }
2244
2245 // [20.7.15.2.7] specialized algorithms
2246
2247 /**
2248 * @brief Swap the targets of two polymorphic function object wrappers.
2249 *
2250 * This function will not throw an %exception.
2251 */
2252 template<typename _Res, typename... _Args>
2253 inline void
2254 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2255 { __x.swap(__y); }
2256
2257 _GLIBCXX_END_NAMESPACE_VERSION
2258 } // namespace std
2259
2260 #endif // __GXX_EXPERIMENTAL_CXX0X__
2261
2262 #endif // _GLIBCXX_FUNCTIONAL