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