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