blob: 406385167294e94b3be65da26e394e3340ce325a [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL_03
12#define _LIBCPP_FUNCTIONAL_03
13
14// manual variadic expansion for <functional>
15
16#pragma GCC system_header
17
18template <class _Tp>
19class __mem_fn
20 : public __weak_result_type<_Tp>
21{
22public:
23 // types
24 typedef _Tp type;
25private:
26 type __f_;
27
28public:
29 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
30
31 // invoke
32
33 typename __invoke_return<type>::type
34 operator() ()
35 {
36 return __invoke(__f_);
37 }
38
39 template <class _A0>
40 typename __invoke_return0<type, _A0>::type
41 operator() (_A0& __a0)
42 {
43 return __invoke(__f_, __a0);
44 }
45
46 template <class _A0, class _A1>
47 typename __invoke_return1<type, _A0, _A1>::type
48 operator() (_A0& __a0, _A1& __a1)
49 {
50 return __invoke(__f_, __a0, __a1);
51 }
52
53 template <class _A0, class _A1, class _A2>
54 typename __invoke_return2<type, _A0, _A1, _A2>::type
55 operator() (_A0& __a0, _A1& __a1, _A2& __a2)
56 {
57 return __invoke(__f_, __a0, __a1, __a2);
58 }
59};
60
61template<class _R, class _T>
62inline _LIBCPP_INLINE_VISIBILITY
63__mem_fn<_R _T::*>
64mem_fn(_R _T::* __pm)
65{
66 return __mem_fn<_R _T::*>(__pm);
67}
68
69template<class _R, class _T>
70inline _LIBCPP_INLINE_VISIBILITY
71__mem_fn<_R (_T::*)()>
72mem_fn(_R (_T::* __pm)())
73{
74 return __mem_fn<_R (_T::*)()>(__pm);
75}
76
77template<class _R, class _T, class _A0>
78inline _LIBCPP_INLINE_VISIBILITY
79__mem_fn<_R (_T::*)(_A0)>
80mem_fn(_R (_T::* __pm)(_A0))
81{
82 return __mem_fn<_R (_T::*)(_A0)>(__pm);
83}
84
85template<class _R, class _T, class _A0, class _A1>
86inline _LIBCPP_INLINE_VISIBILITY
87__mem_fn<_R (_T::*)(_A0, _A1)>
88mem_fn(_R (_T::* __pm)(_A0, _A1))
89{
90 return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
91}
92
93template<class _R, class _T, class _A0, class _A1, class _A2>
94inline _LIBCPP_INLINE_VISIBILITY
95__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
96mem_fn(_R (_T::* __pm)(_A0, _A1, _A2))
97{
98 return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
99}
100
101template<class _R, class _T>
102inline _LIBCPP_INLINE_VISIBILITY
103__mem_fn<_R (_T::*)()>
104mem_fn(_R (_T::* __pm)() const)
105{
106 return __mem_fn<_R (_T::*)()>(__pm);
107}
108
109template<class _R, class _T, class _A0>
110inline _LIBCPP_INLINE_VISIBILITY
111__mem_fn<_R (_T::*)(_A0)>
112mem_fn(_R (_T::* __pm)(_A0) const)
113{
114 return __mem_fn<_R (_T::*)(_A0)>(__pm);
115}
116
117template<class _R, class _T, class _A0, class _A1>
118inline _LIBCPP_INLINE_VISIBILITY
119__mem_fn<_R (_T::*)(_A0, _A1)>
120mem_fn(_R (_T::* __pm)(_A0, _A1) const)
121{
122 return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
123}
124
125template<class _R, class _T, class _A0, class _A1, class _A2>
126inline _LIBCPP_INLINE_VISIBILITY
127__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
128mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const)
129{
130 return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
131}
132
133template<class _R, class _T>
134inline _LIBCPP_INLINE_VISIBILITY
135__mem_fn<_R (_T::*)()>
136mem_fn(_R (_T::* __pm)() volatile)
137{
138 return __mem_fn<_R (_T::*)()>(__pm);
139}
140
141template<class _R, class _T, class _A0>
142inline _LIBCPP_INLINE_VISIBILITY
143__mem_fn<_R (_T::*)(_A0)>
144mem_fn(_R (_T::* __pm)(_A0) volatile)
145{
146 return __mem_fn<_R (_T::*)(_A0)>(__pm);
147}
148
149template<class _R, class _T, class _A0, class _A1>
150inline _LIBCPP_INLINE_VISIBILITY
151__mem_fn<_R (_T::*)(_A0, _A1)>
152mem_fn(_R (_T::* __pm)(_A0, _A1) volatile)
153{
154 return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
155}
156
157template<class _R, class _T, class _A0, class _A1, class _A2>
158inline _LIBCPP_INLINE_VISIBILITY
159__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
160mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) volatile)
161{
162 return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
163}
164
165template<class _R, class _T>
166inline _LIBCPP_INLINE_VISIBILITY
167__mem_fn<_R (_T::*)()>
168mem_fn(_R (_T::* __pm)() const volatile)
169{
170 return __mem_fn<_R (_T::*)()>(__pm);
171}
172
173template<class _R, class _T, class _A0>
174inline _LIBCPP_INLINE_VISIBILITY
175__mem_fn<_R (_T::*)(_A0)>
176mem_fn(_R (_T::* __pm)(_A0) const volatile)
177{
178 return __mem_fn<_R (_T::*)(_A0)>(__pm);
179}
180
181template<class _R, class _T, class _A0, class _A1>
182inline _LIBCPP_INLINE_VISIBILITY
183__mem_fn<_R (_T::*)(_A0, _A1)>
184mem_fn(_R (_T::* __pm)(_A0, _A1) const volatile)
185{
186 return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
187}
188
189template<class _R, class _T, class _A0, class _A1, class _A2>
190inline _LIBCPP_INLINE_VISIBILITY
191__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
192mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const volatile)
193{
194 return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
195}
196
197// bad_function_call
198
199class bad_function_call
200 : public exception
201{
202};
203
204template<class _Fp> class function; // undefined
205
206namespace __function
207{
208
209template<class _F>
210struct __maybe_derive_from_unary_function
211{
212};
213
214template<class _R, class _A1>
215struct __maybe_derive_from_unary_function<_R(_A1)>
216 : public unary_function<_A1, _R>
217{
218};
219
220template<class _F>
221struct __maybe_derive_from_binary_function
222{
223};
224
225template<class _R, class _A1, class _A2>
226struct __maybe_derive_from_binary_function<_R(_A1, _A2)>
227 : public binary_function<_A1, _A2, _R>
228{
229};
230
231template<class _Fp> class __base;
232
233template<class _R>
234class __base<_R()>
235{
236 __base(const __base&);
237 __base& operator=(const __base&);
238public:
239 __base() {}
240 virtual ~__base() {}
241 virtual __base* __clone() const = 0;
242 virtual void __clone(__base*) const = 0;
243 virtual void destroy() = 0;
244 virtual void destroy_deallocate() = 0;
245 virtual _R operator()() = 0;
246 virtual const void* target(const type_info&) const = 0;
247 virtual const std::type_info& target_type() const = 0;
248};
249
250template<class _R, class _A0>
251class __base<_R(_A0)>
252{
253 __base(const __base&);
254 __base& operator=(const __base&);
255public:
256 __base() {}
257 virtual ~__base() {}
258 virtual __base* __clone() const = 0;
259 virtual void __clone(__base*) const = 0;
260 virtual void destroy() = 0;
261 virtual void destroy_deallocate() = 0;
262 virtual _R operator()(_A0) = 0;
263 virtual const void* target(const type_info&) const = 0;
264 virtual const std::type_info& target_type() const = 0;
265};
266
267template<class _R, class _A0, class _A1>
268class __base<_R(_A0, _A1)>
269{
270 __base(const __base&);
271 __base& operator=(const __base&);
272public:
273 __base() {}
274 virtual ~__base() {}
275 virtual __base* __clone() const = 0;
276 virtual void __clone(__base*) const = 0;
277 virtual void destroy() = 0;
278 virtual void destroy_deallocate() = 0;
279 virtual _R operator()(_A0, _A1) = 0;
280 virtual const void* target(const type_info&) const = 0;
281 virtual const std::type_info& target_type() const = 0;
282};
283
284template<class _R, class _A0, class _A1, class _A2>
285class __base<_R(_A0, _A1, _A2)>
286{
287 __base(const __base&);
288 __base& operator=(const __base&);
289public:
290 __base() {}
291 virtual ~__base() {}
292 virtual __base* __clone() const = 0;
293 virtual void __clone(__base*) const = 0;
294 virtual void destroy() = 0;
295 virtual void destroy_deallocate() = 0;
296 virtual _R operator()(_A0, _A1, _A2) = 0;
297 virtual const void* target(const type_info&) const = 0;
298 virtual const std::type_info& target_type() const = 0;
299};
300
301template<class _FD, class _Alloc, class _FB> class __func;
302
303template<class _F, class _Alloc, class _R>
304class __func<_F, _Alloc, _R()>
305 : public __base<_R()>
306{
307 __compressed_pair<_F, _Alloc> __f_;
308public:
309 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
310 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
311 virtual __base<_R()>* __clone() const;
312 virtual void __clone(__base<_R()>*) const;
313 virtual void destroy();
314 virtual void destroy_deallocate();
315 virtual _R operator()();
316 virtual const void* target(const type_info&) const;
317 virtual const std::type_info& target_type() const;
318};
319
320template<class _F, class _Alloc, class _R>
321__base<_R()>*
322__func<_F, _Alloc, _R()>::__clone() const
323{
324 typedef typename _Alloc::template rebind<__func>::other _A;
325 _A __a(__f_.second());
326 typedef __allocator_destructor<_A> _D;
327 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
328 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
329 return __hold.release();
330}
331
332template<class _F, class _Alloc, class _R>
333void
334__func<_F, _Alloc, _R()>::__clone(__base<_R()>* __p) const
335{
336 ::new (__p) __func(__f_.first(), __f_.second());
337}
338
339template<class _F, class _Alloc, class _R>
340void
341__func<_F, _Alloc, _R()>::destroy()
342{
343 __f_.~__compressed_pair<_F, _Alloc>();
344}
345
346template<class _F, class _Alloc, class _R>
347void
348__func<_F, _Alloc, _R()>::destroy_deallocate()
349{
350 typedef typename _Alloc::template rebind<__func>::other _A;
351 _A __a(__f_.second());
352 __f_.~__compressed_pair<_F, _Alloc>();
353 __a.deallocate(this, 1);
354}
355
356template<class _F, class _Alloc, class _R>
357_R
358__func<_F, _Alloc, _R()>::operator()()
359{
360 return __invoke<_R>(__f_.first());
361}
362
363template<class _F, class _Alloc, class _R>
364const void*
365__func<_F, _Alloc, _R()>::target(const type_info& __ti) const
366{
367 if (__ti == typeid(_F))
368 return &__f_.first();
369 return (const void*)0;
370}
371
372template<class _F, class _Alloc, class _R>
373const std::type_info&
374__func<_F, _Alloc, _R()>::target_type() const
375{
376 return typeid(_F);
377}
378
379template<class _F, class _Alloc, class _R, class _A0>
380class __func<_F, _Alloc, _R(_A0)>
381 : public __base<_R(_A0)>
382{
383 __compressed_pair<_F, _Alloc> __f_;
384public:
385 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
386 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
387 virtual __base<_R(_A0)>* __clone() const;
388 virtual void __clone(__base<_R(_A0)>*) const;
389 virtual void destroy();
390 virtual void destroy_deallocate();
391 virtual _R operator()(_A0);
392 virtual const void* target(const type_info&) const;
393 virtual const std::type_info& target_type() const;
394};
395
396template<class _F, class _Alloc, class _R, class _A0>
397__base<_R(_A0)>*
398__func<_F, _Alloc, _R(_A0)>::__clone() const
399{
400 typedef typename _Alloc::template rebind<__func>::other _A;
401 _A __a(__f_.second());
402 typedef __allocator_destructor<_A> _D;
403 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
404 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
405 return __hold.release();
406}
407
408template<class _F, class _Alloc, class _R, class _A0>
409void
410__func<_F, _Alloc, _R(_A0)>::__clone(__base<_R(_A0)>* __p) const
411{
412 ::new (__p) __func(__f_.first(), __f_.second());
413}
414
415template<class _F, class _Alloc, class _R, class _A0>
416void
417__func<_F, _Alloc, _R(_A0)>::destroy()
418{
419 __f_.~__compressed_pair<_F, _Alloc>();
420}
421
422template<class _F, class _Alloc, class _R, class _A0>
423void
424__func<_F, _Alloc, _R(_A0)>::destroy_deallocate()
425{
426 typedef typename _Alloc::template rebind<__func>::other _A;
427 _A __a(__f_.second());
428 __f_.~__compressed_pair<_F, _Alloc>();
429 __a.deallocate(this, 1);
430}
431
432template<class _F, class _Alloc, class _R, class _A0>
433_R
434__func<_F, _Alloc, _R(_A0)>::operator()(_A0 __a0)
435{
436 return __invoke(__f_.first(), __a0);
437}
438
439template<class _F, class _Alloc, class _R, class _A0>
440const void*
441__func<_F, _Alloc, _R(_A0)>::target(const type_info& __ti) const
442{
443 if (__ti == typeid(_F))
444 return &__f_.first();
445 return (const void*)0;
446}
447
448template<class _F, class _Alloc, class _R, class _A0>
449const std::type_info&
450__func<_F, _Alloc, _R(_A0)>::target_type() const
451{
452 return typeid(_F);
453}
454
455template<class _F, class _Alloc, class _R, class _A0, class _A1>
456class __func<_F, _Alloc, _R(_A0, _A1)>
457 : public __base<_R(_A0, _A1)>
458{
459 __compressed_pair<_F, _Alloc> __f_;
460public:
461 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
462 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
463 virtual __base<_R(_A0, _A1)>* __clone() const;
464 virtual void __clone(__base<_R(_A0, _A1)>*) const;
465 virtual void destroy();
466 virtual void destroy_deallocate();
467 virtual _R operator()(_A0, _A1);
468 virtual const void* target(const type_info&) const;
469 virtual const std::type_info& target_type() const;
470};
471
472template<class _F, class _Alloc, class _R, class _A0, class _A1>
473__base<_R(_A0, _A1)>*
474__func<_F, _Alloc, _R(_A0, _A1)>::__clone() const
475{
476 typedef typename _Alloc::template rebind<__func>::other _A;
477 _A __a(__f_.second());
478 typedef __allocator_destructor<_A> _D;
479 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
480 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
481 return __hold.release();
482}
483
484template<class _F, class _Alloc, class _R, class _A0, class _A1>
485void
486__func<_F, _Alloc, _R(_A0, _A1)>::__clone(__base<_R(_A0, _A1)>* __p) const
487{
488 ::new (__p) __func(__f_.first(), __f_.second());
489}
490
491template<class _F, class _Alloc, class _R, class _A0, class _A1>
492void
493__func<_F, _Alloc, _R(_A0, _A1)>::destroy()
494{
495 __f_.~__compressed_pair<_F, _Alloc>();
496}
497
498template<class _F, class _Alloc, class _R, class _A0, class _A1>
499void
500__func<_F, _Alloc, _R(_A0, _A1)>::destroy_deallocate()
501{
502 typedef typename _Alloc::template rebind<__func>::other _A;
503 _A __a(__f_.second());
504 __f_.~__compressed_pair<_F, _Alloc>();
505 __a.deallocate(this, 1);
506}
507
508template<class _F, class _Alloc, class _R, class _A0, class _A1>
509_R
510__func<_F, _Alloc, _R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
511{
512 return __invoke(__f_.first(), __a0, __a1);
513}
514
515template<class _F, class _Alloc, class _R, class _A0, class _A1>
516const void*
517__func<_F, _Alloc, _R(_A0, _A1)>::target(const type_info& __ti) const
518{
519 if (__ti == typeid(_F))
520 return &__f_.first();
521 return (const void*)0;
522}
523
524template<class _F, class _Alloc, class _R, class _A0, class _A1>
525const std::type_info&
526__func<_F, _Alloc, _R(_A0, _A1)>::target_type() const
527{
528 return typeid(_F);
529}
530
531template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
532class __func<_F, _Alloc, _R(_A0, _A1, _A2)>
533 : public __base<_R(_A0, _A1, _A2)>
534{
535 __compressed_pair<_F, _Alloc> __f_;
536public:
537 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
538 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
539 virtual __base<_R(_A0, _A1, _A2)>* __clone() const;
540 virtual void __clone(__base<_R(_A0, _A1, _A2)>*) const;
541 virtual void destroy();
542 virtual void destroy_deallocate();
543 virtual _R operator()(_A0, _A1, _A2);
544 virtual const void* target(const type_info&) const;
545 virtual const std::type_info& target_type() const;
546};
547
548template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
549__base<_R(_A0, _A1, _A2)>*
550__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone() const
551{
552 typedef typename _Alloc::template rebind<__func>::other _A;
553 _A __a(__f_.second());
554 typedef __allocator_destructor<_A> _D;
555 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
556 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
557 return __hold.release();
558}
559
560template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
561void
562__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone(__base<_R(_A0, _A1, _A2)>* __p) const
563{
564 ::new (__p) __func(__f_.first(), __f_.second());
565}
566
567template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
568void
569__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy()
570{
571 __f_.~__compressed_pair<_F, _Alloc>();
572}
573
574template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
575void
576__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy_deallocate()
577{
578 typedef typename _Alloc::template rebind<__func>::other _A;
579 _A __a(__f_.second());
580 __f_.~__compressed_pair<_F, _Alloc>();
581 __a.deallocate(this, 1);
582}
583
584template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
585_R
586__func<_F, _Alloc, _R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
587{
588 return __invoke(__f_.first(), __a0, __a1, __a2);
589}
590
591template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
592const void*
593__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target(const type_info& __ti) const
594{
595 if (__ti == typeid(_F))
596 return &__f_.first();
597 return (const void*)0;
598}
599
600template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
601const std::type_info&
602__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target_type() const
603{
604 return typeid(_F);
605}
606
607} // __function
608
609template<class _R>
610class function<_R()>
611{
612 typedef __function::__base<_R()> __base;
613 aligned_storage<3*sizeof(void*)>::type __buf_;
614 __base* __f_;
615
616 template <class _F>
617 static bool __not_null(const _F&) {return true;}
618 template <class _R2>
619 static bool __not_null(const function<_R()>& __p) {return __p;}
620public:
621 typedef _R result_type;
622
623 // 20.7.16.2.1, construct/copy/destroy:
624 explicit function() : __f_(0) {}
625 function(nullptr_t) : __f_(0) {}
626 function(const function&);
627 template<class _F>
628 function(_F,
629 typename enable_if<!is_integral<_F>::value>::type* = 0);
630
631// template<class _Alloc>
632// function(allocator_arg_t, const _Alloc&);
633// template<Allocator Alloc>
634// function(allocator_arg_t, const Alloc&, nullptr_t);
635// template<Allocator Alloc>
636// function(allocator_arg_t, const Alloc&, const function&);
637// template<Allocator Alloc>
638// function(allocator_arg_t, const Alloc&, function&&);
639// template<class F, Allocator Alloc>
640// function(allocator_arg_t, const Alloc&, F);
641
642 function& operator=(const function&);
643 function& operator=(nullptr_t);
644 template<class _F>
645 typename enable_if
646 <
647 !is_integral<_F>::value,
648 function&
649 >::type
650 operator=(_F);
651
652 ~function();
653
654 // 20.7.16.2.2, function modifiers:
655 void swap(function&);
656// template<class _F, class _Alloc>
657// void assign(_F, const _Alloc&);
658
659 // 20.7.16.2.3, function capacity:
660 operator bool() const {return __f_;}
661
662private:
663 // deleted overloads close possible hole in the type system
664 template<class _R2>
665 bool operator==(const function<_R2()>&);// = delete;
666 template<class _R2>
667 bool operator!=(const function<_R2()>&);// = delete;
668public:
669 // 20.7.16.2.4, function invocation:
670 _R operator()() const;
671
672 // 20.7.16.2.5, function target access:
673 const std::type_info& target_type() const;
674 template <typename _T> _T* target();
675 template <typename _T> const _T* target() const;
676};
677
678template<class _R>
679function<_R()>::function(const function& __f)
680{
681 if (__f.__f_ == 0)
682 __f_ = 0;
683 else if (__f.__f_ == (const __base*)&__f.__buf_)
684 {
685 __f_ = (__base*)&__buf_;
686 __f.__f_->__clone(__f_);
687 }
688 else
689 __f_ = __f.__f_->__clone();
690}
691
692template<class _R>
693template <class _F>
694function<_R()>::function(_F __f,
695 typename enable_if<!is_integral<_F>::value>::type*)
696 : __f_(0)
697{
698 if (__not_null(__f))
699 {
700 typedef __function::__func<_F, allocator<_F>, _R()> _FF;
701 if (sizeof(_FF) <= sizeof(__buf_))
702 {
703 __f_ = (__base*)&__buf_;
704 ::new (__f_) _FF(__f);
705 }
706 else
707 {
708 typedef allocator<_FF> _A;
709 _A __a;
710 typedef __allocator_destructor<_A> _D;
711 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
712 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
713 __f_ = __hold.release();
714 }
715 }
716}
717
718template<class _R>
719function<_R()>&
720function<_R()>::operator=(const function& __f)
721{
722 function(__f).swap(*this);
723 return *this;
724}
725
726template<class _R>
727function<_R()>&
728function<_R()>::operator=(nullptr_t)
729{
730 if (__f_ == (__base*)&__buf_)
731 __f_->destroy();
732 else if (__f_)
733 __f_->destroy_deallocate();
734 __f_ = 0;
735}
736
737template<class _R>
738template <class _F>
739typename enable_if
740<
741 !is_integral<_F>::value,
742 function<_R()>&
743>::type
744function<_R()>::operator=(_F __f)
745{
746 function(_STD::move(__f)).swap(*this);
747 return *this;
748}
749
750template<class _R>
751function<_R()>::~function()
752{
753 if (__f_ == (__base*)&__buf_)
754 __f_->destroy();
755 else if (__f_)
756 __f_->destroy_deallocate();
757}
758
759template<class _R>
760void
761function<_R()>::swap(function& __f)
762{
763 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
764 {
765 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
766 __base* __t = (__base*)&__tempbuf;
767 __f_->__clone(__t);
768 __f_->destroy();
769 __f_ = 0;
770 __f.__f_->__clone((__base*)&__buf_);
771 __f.__f_->destroy();
772 __f.__f_ = 0;
773 __f_ = (__base*)&__buf_;
774 __t->__clone((__base*)&__f.__buf_);
775 __t->destroy();
776 __f.__f_ = (__base*)&__f.__buf_;
777 }
778 else if (__f_ == (__base*)&__buf_)
779 {
780 __f_->__clone((__base*)&__f.__buf_);
781 __f_->destroy();
782 __f_ = __f.__f_;
783 __f.__f_ = (__base*)&__f.__buf_;
784 }
785 else if (__f.__f_ == (__base*)&__f.__buf_)
786 {
787 __f.__f_->__clone((__base*)&__buf_);
788 __f.__f_->destroy();
789 __f.__f_ = __f_;
790 __f_ = (__base*)&__buf_;
791 }
792 else
793 _STD::swap(__f_, __f.__f_);
794}
795
796template<class _R>
797_R
798function<_R()>::operator()() const
799{
800 if (__f_ == 0)
801 throw bad_function_call();
802 return (*__f_)();
803}
804
805template<class _R>
806const std::type_info&
807function<_R()>::target_type() const
808{
809 if (__f_ == 0)
810 return typeid(void);
811 return __f_->target_type();
812}
813
814template<class _R>
815template <typename _T>
816_T*
817function<_R()>::target()
818{
819 if (__f_ == 0)
820 return (_T*)0;
821 return (_T*)__f_->target(typeid(_T));
822}
823
824template<class _R>
825template <typename _T>
826const _T*
827function<_R()>::target() const
828{
829 if (__f_ == 0)
830 return (const _T*)0;
831 return (const _T*)__f_->target(typeid(_T));
832}
833
834template<class _R, class _A0>
835class function<_R(_A0)>
836 : public unary_function<_A0, _R>
837{
838 typedef __function::__base<_R(_A0)> __base;
839 aligned_storage<3*sizeof(void*)>::type __buf_;
840 __base* __f_;
841
842 template <class _F>
843 static bool __not_null(const _F&) {return true;}
844 template <class _R2, class _B0>
845 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
846 template <class _R2, class _C>
847 static bool __not_null(_R2 (_C::*__p)()) {return __p;}
848 template <class _R2, class _C>
849 static bool __not_null(_R2 (_C::*__p)() const) {return __p;}
850 template <class _R2, class _C>
851 static bool __not_null(_R2 (_C::*__p)() volatile) {return __p;}
852 template <class _R2, class _C>
853 static bool __not_null(_R2 (_C::*__p)() const volatile) {return __p;}
854 template <class _R2, class _B0>
855 static bool __not_null(const function<_R(_B0)>& __p) {return __p;}
856public:
857 typedef _R result_type;
858
859 // 20.7.16.2.1, construct/copy/destroy:
860 explicit function() : __f_(0) {}
861 function(nullptr_t) : __f_(0) {}
862 function(const function&);
863 template<class _F>
864 function(_F,
865 typename enable_if<!is_integral<_F>::value>::type* = 0);
866
867// template<class _Alloc>
868// function(allocator_arg_t, const _Alloc&);
869// template<Allocator Alloc>
870// function(allocator_arg_t, const Alloc&, nullptr_t);
871// template<Allocator Alloc>
872// function(allocator_arg_t, const Alloc&, const function&);
873// template<Allocator Alloc>
874// function(allocator_arg_t, const Alloc&, function&&);
875// template<class F, Allocator Alloc>
876// function(allocator_arg_t, const Alloc&, F);
877
878 function& operator=(const function&);
879 function& operator=(nullptr_t);
880 template<class _F>
881 typename enable_if
882 <
883 !is_integral<_F>::value,
884 function&
885 >::type
886 operator=(_F);
887
888 ~function();
889
890 // 20.7.16.2.2, function modifiers:
891 void swap(function&);
892// template<class _F, class _Alloc>
893// void assign(_F, const _Alloc&);
894
895 // 20.7.16.2.3, function capacity:
896 operator bool() const {return __f_;}
897
898private:
899 // deleted overloads close possible hole in the type system
900 template<class _R2, class _B0>
901 bool operator==(const function<_R2(_B0)>&);// = delete;
902 template<class _R2, class _B0>
903 bool operator!=(const function<_R2(_B0)>&);// = delete;
904public:
905 // 20.7.16.2.4, function invocation:
906 _R operator()(_A0) const;
907
908 // 20.7.16.2.5, function target access:
909 const std::type_info& target_type() const;
910 template <typename _T> _T* target();
911 template <typename _T> const _T* target() const;
912};
913
914template<class _R, class _A0>
915function<_R(_A0)>::function(const function& __f)
916{
917 if (__f.__f_ == 0)
918 __f_ = 0;
919 else if (__f.__f_ == (const __base*)&__f.__buf_)
920 {
921 __f_ = (__base*)&__buf_;
922 __f.__f_->__clone(__f_);
923 }
924 else
925 __f_ = __f.__f_->__clone();
926}
927
928template<class _R, class _A0>
929template <class _F>
930function<_R(_A0)>::function(_F __f,
931 typename enable_if<!is_integral<_F>::value>::type*)
932 : __f_(0)
933{
934 if (__not_null(__f))
935 {
936 typedef __function::__func<_F, allocator<_F>, _R(_A0)> _FF;
937 if (sizeof(_FF) <= sizeof(__buf_))
938 {
939 __f_ = (__base*)&__buf_;
940 ::new (__f_) _FF(__f);
941 }
942 else
943 {
944 typedef allocator<_FF> _A;
945 _A __a;
946 typedef __allocator_destructor<_A> _D;
947 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
948 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
949 __f_ = __hold.release();
950 }
951 }
952}
953
954template<class _R, class _A0>
955function<_R(_A0)>&
956function<_R(_A0)>::operator=(const function& __f)
957{
958 function(__f).swap(*this);
959 return *this;
960}
961
962template<class _R, class _A0>
963function<_R(_A0)>&
964function<_R(_A0)>::operator=(nullptr_t)
965{
966 if (__f_ == (__base*)&__buf_)
967 __f_->destroy();
968 else if (__f_)
969 __f_->destroy_deallocate();
970 __f_ = 0;
971}
972
973template<class _R, class _A0>
974template <class _F>
975typename enable_if
976<
977 !is_integral<_F>::value,
978 function<_R(_A0)>&
979>::type
980function<_R(_A0)>::operator=(_F __f)
981{
982 function(_STD::move(__f)).swap(*this);
983 return *this;
984}
985
986template<class _R, class _A0>
987function<_R(_A0)>::~function()
988{
989 if (__f_ == (__base*)&__buf_)
990 __f_->destroy();
991 else if (__f_)
992 __f_->destroy_deallocate();
993}
994
995template<class _R, class _A0>
996void
997function<_R(_A0)>::swap(function& __f)
998{
999 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1000 {
1001 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1002 __base* __t = (__base*)&__tempbuf;
1003 __f_->__clone(__t);
1004 __f_->destroy();
1005 __f_ = 0;
1006 __f.__f_->__clone((__base*)&__buf_);
1007 __f.__f_->destroy();
1008 __f.__f_ = 0;
1009 __f_ = (__base*)&__buf_;
1010 __t->__clone((__base*)&__f.__buf_);
1011 __t->destroy();
1012 __f.__f_ = (__base*)&__f.__buf_;
1013 }
1014 else if (__f_ == (__base*)&__buf_)
1015 {
1016 __f_->__clone((__base*)&__f.__buf_);
1017 __f_->destroy();
1018 __f_ = __f.__f_;
1019 __f.__f_ = (__base*)&__f.__buf_;
1020 }
1021 else if (__f.__f_ == (__base*)&__f.__buf_)
1022 {
1023 __f.__f_->__clone((__base*)&__buf_);
1024 __f.__f_->destroy();
1025 __f.__f_ = __f_;
1026 __f_ = (__base*)&__buf_;
1027 }
1028 else
1029 _STD::swap(__f_, __f.__f_);
1030}
1031
1032template<class _R, class _A0>
1033_R
1034function<_R(_A0)>::operator()(_A0 __a0) const
1035{
1036 if (__f_ == 0)
1037 throw bad_function_call();
1038 return (*__f_)(__a0);
1039}
1040
1041template<class _R, class _A0>
1042const std::type_info&
1043function<_R(_A0)>::target_type() const
1044{
1045 if (__f_ == 0)
1046 return typeid(void);
1047 return __f_->target_type();
1048}
1049
1050template<class _R, class _A0>
1051template <typename _T>
1052_T*
1053function<_R(_A0)>::target()
1054{
1055 if (__f_ == 0)
1056 return (_T*)0;
1057 return (_T*)__f_->target(typeid(_T));
1058}
1059
1060template<class _R, class _A0>
1061template <typename _T>
1062const _T*
1063function<_R(_A0)>::target() const
1064{
1065 if (__f_ == 0)
1066 return (const _T*)0;
1067 return (const _T*)__f_->target(typeid(_T));
1068}
1069
1070template<class _R, class _A0, class _A1>
1071class function<_R(_A0, _A1)>
1072 : public binary_function<_A0, _A1, _R>
1073{
1074 typedef __function::__base<_R(_A0, _A1)> __base;
1075 aligned_storage<3*sizeof(void*)>::type __buf_;
1076 __base* __f_;
1077
1078 template <class _F>
1079 static bool __not_null(const _F&) {return true;}
1080 template <class _R2, class _B0, class _B1>
1081 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
1082 template <class _R2, class _C, class _B1>
1083 static bool __not_null(_R2 (_C::*__p)(_B1)) {return __p;}
1084 template <class _R2, class _C, class _B1>
1085 static bool __not_null(_R2 (_C::*__p)(_B1) const) {return __p;}
1086 template <class _R2, class _C, class _B1>
1087 static bool __not_null(_R2 (_C::*__p)(_B1) volatile) {return __p;}
1088 template <class _R2, class _C, class _B1>
1089 static bool __not_null(_R2 (_C::*__p)(_B1) const volatile) {return __p;}
1090 template <class _R2, class _B0, class _B1>
1091 static bool __not_null(const function<_R(_B0, _B1)>& __p) {return __p;}
1092public:
1093 typedef _R result_type;
1094
1095 // 20.7.16.2.1, construct/copy/destroy:
1096 explicit function() : __f_(0) {}
1097 function(nullptr_t) : __f_(0) {}
1098 function(const function&);
1099 template<class _F>
1100 function(_F,
1101 typename enable_if<!is_integral<_F>::value>::type* = 0);
1102
1103// template<class _Alloc>
1104// function(allocator_arg_t, const _Alloc&);
1105// template<Allocator Alloc>
1106// function(allocator_arg_t, const Alloc&, nullptr_t);
1107// template<Allocator Alloc>
1108// function(allocator_arg_t, const Alloc&, const function&);
1109// template<Allocator Alloc>
1110// function(allocator_arg_t, const Alloc&, function&&);
1111// template<class F, Allocator Alloc>
1112// function(allocator_arg_t, const Alloc&, F);
1113
1114 function& operator=(const function&);
1115 function& operator=(nullptr_t);
1116 template<class _F>
1117 typename enable_if
1118 <
1119 !is_integral<_F>::value,
1120 function&
1121 >::type
1122 operator=(_F);
1123
1124 ~function();
1125
1126 // 20.7.16.2.2, function modifiers:
1127 void swap(function&);
1128// template<class _F, class _Alloc>
1129// void assign(_F, const _Alloc&);
1130
1131 // 20.7.16.2.3, function capacity:
1132 operator bool() const {return __f_;}
1133
1134private:
1135 // deleted overloads close possible hole in the type system
1136 template<class _R2, class _B0, class _B1>
1137 bool operator==(const function<_R2(_B0, _B1)>&);// = delete;
1138 template<class _R2, class _B0, class _B1>
1139 bool operator!=(const function<_R2(_B0, _B1)>&);// = delete;
1140public:
1141 // 20.7.16.2.4, function invocation:
1142 _R operator()(_A0, _A1) const;
1143
1144 // 20.7.16.2.5, function target access:
1145 const std::type_info& target_type() const;
1146 template <typename _T> _T* target();
1147 template <typename _T> const _T* target() const;
1148};
1149
1150template<class _R, class _A0, class _A1>
1151function<_R(_A0, _A1)>::function(const function& __f)
1152{
1153 if (__f.__f_ == 0)
1154 __f_ = 0;
1155 else if (__f.__f_ == (const __base*)&__f.__buf_)
1156 {
1157 __f_ = (__base*)&__buf_;
1158 __f.__f_->__clone(__f_);
1159 }
1160 else
1161 __f_ = __f.__f_->__clone();
1162}
1163
1164template<class _R, class _A0, class _A1>
1165template <class _F>
1166function<_R(_A0, _A1)>::function(_F __f,
1167 typename enable_if<!is_integral<_F>::value>::type*)
1168 : __f_(0)
1169{
1170 if (__not_null(__f))
1171 {
1172 typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1)> _FF;
1173 if (sizeof(_FF) <= sizeof(__buf_))
1174 {
1175 __f_ = (__base*)&__buf_;
1176 ::new (__f_) _FF(__f);
1177 }
1178 else
1179 {
1180 typedef allocator<_FF> _A;
1181 _A __a;
1182 typedef __allocator_destructor<_A> _D;
1183 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1184 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1185 __f_ = __hold.release();
1186 }
1187 }
1188}
1189
1190template<class _R, class _A0, class _A1>
1191function<_R(_A0, _A1)>&
1192function<_R(_A0, _A1)>::operator=(const function& __f)
1193{
1194 function(__f).swap(*this);
1195 return *this;
1196}
1197
1198template<class _R, class _A0, class _A1>
1199function<_R(_A0, _A1)>&
1200function<_R(_A0, _A1)>::operator=(nullptr_t)
1201{
1202 if (__f_ == (__base*)&__buf_)
1203 __f_->destroy();
1204 else if (__f_)
1205 __f_->destroy_deallocate();
1206 __f_ = 0;
1207}
1208
1209template<class _R, class _A0, class _A1>
1210template <class _F>
1211typename enable_if
1212<
1213 !is_integral<_F>::value,
1214 function<_R(_A0, _A1)>&
1215>::type
1216function<_R(_A0, _A1)>::operator=(_F __f)
1217{
1218 function(_STD::move(__f)).swap(*this);
1219 return *this;
1220}
1221
1222template<class _R, class _A0, class _A1>
1223function<_R(_A0, _A1)>::~function()
1224{
1225 if (__f_ == (__base*)&__buf_)
1226 __f_->destroy();
1227 else if (__f_)
1228 __f_->destroy_deallocate();
1229}
1230
1231template<class _R, class _A0, class _A1>
1232void
1233function<_R(_A0, _A1)>::swap(function& __f)
1234{
1235 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1236 {
1237 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1238 __base* __t = (__base*)&__tempbuf;
1239 __f_->__clone(__t);
1240 __f_->destroy();
1241 __f_ = 0;
1242 __f.__f_->__clone((__base*)&__buf_);
1243 __f.__f_->destroy();
1244 __f.__f_ = 0;
1245 __f_ = (__base*)&__buf_;
1246 __t->__clone((__base*)&__f.__buf_);
1247 __t->destroy();
1248 __f.__f_ = (__base*)&__f.__buf_;
1249 }
1250 else if (__f_ == (__base*)&__buf_)
1251 {
1252 __f_->__clone((__base*)&__f.__buf_);
1253 __f_->destroy();
1254 __f_ = __f.__f_;
1255 __f.__f_ = (__base*)&__f.__buf_;
1256 }
1257 else if (__f.__f_ == (__base*)&__f.__buf_)
1258 {
1259 __f.__f_->__clone((__base*)&__buf_);
1260 __f.__f_->destroy();
1261 __f.__f_ = __f_;
1262 __f_ = (__base*)&__buf_;
1263 }
1264 else
1265 _STD::swap(__f_, __f.__f_);
1266}
1267
1268template<class _R, class _A0, class _A1>
1269_R
1270function<_R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1271{
1272 if (__f_ == 0)
1273 throw bad_function_call();
1274 return (*__f_)(__a0, __a1);
1275}
1276
1277template<class _R, class _A0, class _A1>
1278const std::type_info&
1279function<_R(_A0, _A1)>::target_type() const
1280{
1281 if (__f_ == 0)
1282 return typeid(void);
1283 return __f_->target_type();
1284}
1285
1286template<class _R, class _A0, class _A1>
1287template <typename _T>
1288_T*
1289function<_R(_A0, _A1)>::target()
1290{
1291 if (__f_ == 0)
1292 return (_T*)0;
1293 return (_T*)__f_->target(typeid(_T));
1294}
1295
1296template<class _R, class _A0, class _A1>
1297template <typename _T>
1298const _T*
1299function<_R(_A0, _A1)>::target() const
1300{
1301 if (__f_ == 0)
1302 return (const _T*)0;
1303 return (const _T*)__f_->target(typeid(_T));
1304}
1305
1306template<class _R, class _A0, class _A1, class _A2>
1307class function<_R(_A0, _A1, _A2)>
1308{
1309 typedef __function::__base<_R(_A0, _A1, _A2)> __base;
1310 aligned_storage<3*sizeof(void*)>::type __buf_;
1311 __base* __f_;
1312
1313 template <class _F>
1314 static bool __not_null(const _F&) {return true;}
1315 template <class _R2, class _B0, class _B1, class _B2>
1316 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
1317 template <class _R2, class _C, class _B1, class _B2>
1318 static bool __not_null(_R2 (_C::*__p)(_B1, _B2)) {return __p;}
1319 template <class _R2, class _C, class _B1, class _B2>
1320 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const) {return __p;}
1321 template <class _R2, class _C, class _B1, class _B2>
1322 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) volatile) {return __p;}
1323 template <class _R2, class _C, class _B1, class _B2>
1324 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const volatile) {return __p;}
1325 template <class _R2, class _B0, class _B1, class _B2>
1326 static bool __not_null(const function<_R(_B0, _B1, _B2)>& __p) {return __p;}
1327public:
1328 typedef _R result_type;
1329
1330 // 20.7.16.2.1, construct/copy/destroy:
1331 explicit function() : __f_(0) {}
1332 function(nullptr_t) : __f_(0) {}
1333 function(const function&);
1334 template<class _F>
1335 function(_F,
1336 typename enable_if<!is_integral<_F>::value>::type* = 0);
1337
1338// template<class _Alloc>
1339// function(allocator_arg_t, const _Alloc&);
1340// template<Allocator Alloc>
1341// function(allocator_arg_t, const Alloc&, nullptr_t);
1342// template<Allocator Alloc>
1343// function(allocator_arg_t, const Alloc&, const function&);
1344// template<Allocator Alloc>
1345// function(allocator_arg_t, const Alloc&, function&&);
1346// template<class F, Allocator Alloc>
1347// function(allocator_arg_t, const Alloc&, F);
1348
1349 function& operator=(const function&);
1350 function& operator=(nullptr_t);
1351 template<class _F>
1352 typename enable_if
1353 <
1354 !is_integral<_F>::value,
1355 function&
1356 >::type
1357 operator=(_F);
1358
1359 ~function();
1360
1361 // 20.7.16.2.2, function modifiers:
1362 void swap(function&);
1363// template<class _F, class _Alloc>
1364// void assign(_F, const _Alloc&);
1365
1366 // 20.7.16.2.3, function capacity:
1367 operator bool() const {return __f_;}
1368
1369private:
1370 // deleted overloads close possible hole in the type system
1371 template<class _R2, class _B0, class _B1, class _B2>
1372 bool operator==(const function<_R2(_B0, _B1, _B2)>&);// = delete;
1373 template<class _R2, class _B0, class _B1, class _B2>
1374 bool operator!=(const function<_R2(_B0, _B1, _B2)>&);// = delete;
1375public:
1376 // 20.7.16.2.4, function invocation:
1377 _R operator()(_A0, _A1, _A2) const;
1378
1379 // 20.7.16.2.5, function target access:
1380 const std::type_info& target_type() const;
1381 template <typename _T> _T* target();
1382 template <typename _T> const _T* target() const;
1383};
1384
1385template<class _R, class _A0, class _A1, class _A2>
1386function<_R(_A0, _A1, _A2)>::function(const function& __f)
1387{
1388 if (__f.__f_ == 0)
1389 __f_ = 0;
1390 else if (__f.__f_ == (const __base*)&__f.__buf_)
1391 {
1392 __f_ = (__base*)&__buf_;
1393 __f.__f_->__clone(__f_);
1394 }
1395 else
1396 __f_ = __f.__f_->__clone();
1397}
1398
1399template<class _R, class _A0, class _A1, class _A2>
1400template <class _F>
1401function<_R(_A0, _A1, _A2)>::function(_F __f,
1402 typename enable_if<!is_integral<_F>::value>::type*)
1403 : __f_(0)
1404{
1405 if (__not_null(__f))
1406 {
1407 typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1, _A2)> _FF;
1408 if (sizeof(_FF) <= sizeof(__buf_))
1409 {
1410 __f_ = (__base*)&__buf_;
1411 ::new (__f_) _FF(__f);
1412 }
1413 else
1414 {
1415 typedef allocator<_FF> _A;
1416 _A __a;
1417 typedef __allocator_destructor<_A> _D;
1418 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1419 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1420 __f_ = __hold.release();
1421 }
1422 }
1423}
1424
1425template<class _R, class _A0, class _A1, class _A2>
1426function<_R(_A0, _A1, _A2)>&
1427function<_R(_A0, _A1, _A2)>::operator=(const function& __f)
1428{
1429 function(__f).swap(*this);
1430 return *this;
1431}
1432
1433template<class _R, class _A0, class _A1, class _A2>
1434function<_R(_A0, _A1, _A2)>&
1435function<_R(_A0, _A1, _A2)>::operator=(nullptr_t)
1436{
1437 if (__f_ == (__base*)&__buf_)
1438 __f_->destroy();
1439 else if (__f_)
1440 __f_->destroy_deallocate();
1441 __f_ = 0;
1442}
1443
1444template<class _R, class _A0, class _A1, class _A2>
1445template <class _F>
1446typename enable_if
1447<
1448 !is_integral<_F>::value,
1449 function<_R(_A0, _A1, _A2)>&
1450>::type
1451function<_R(_A0, _A1, _A2)>::operator=(_F __f)
1452{
1453 function(_STD::move(__f)).swap(*this);
1454 return *this;
1455}
1456
1457template<class _R, class _A0, class _A1, class _A2>
1458function<_R(_A0, _A1, _A2)>::~function()
1459{
1460 if (__f_ == (__base*)&__buf_)
1461 __f_->destroy();
1462 else if (__f_)
1463 __f_->destroy_deallocate();
1464}
1465
1466template<class _R, class _A0, class _A1, class _A2>
1467void
1468function<_R(_A0, _A1, _A2)>::swap(function& __f)
1469{
1470 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1471 {
1472 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1473 __base* __t = (__base*)&__tempbuf;
1474 __f_->__clone(__t);
1475 __f_->destroy();
1476 __f_ = 0;
1477 __f.__f_->__clone((__base*)&__buf_);
1478 __f.__f_->destroy();
1479 __f.__f_ = 0;
1480 __f_ = (__base*)&__buf_;
1481 __t->__clone((__base*)&__f.__buf_);
1482 __t->destroy();
1483 __f.__f_ = (__base*)&__f.__buf_;
1484 }
1485 else if (__f_ == (__base*)&__buf_)
1486 {
1487 __f_->__clone((__base*)&__f.__buf_);
1488 __f_->destroy();
1489 __f_ = __f.__f_;
1490 __f.__f_ = (__base*)&__f.__buf_;
1491 }
1492 else if (__f.__f_ == (__base*)&__f.__buf_)
1493 {
1494 __f.__f_->__clone((__base*)&__buf_);
1495 __f.__f_->destroy();
1496 __f.__f_ = __f_;
1497 __f_ = (__base*)&__buf_;
1498 }
1499 else
1500 _STD::swap(__f_, __f.__f_);
1501}
1502
1503template<class _R, class _A0, class _A1, class _A2>
1504_R
1505function<_R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1506{
1507 if (__f_ == 0)
1508 throw bad_function_call();
1509 return (*__f_)(__a0, __a1, __a2);
1510}
1511
1512template<class _R, class _A0, class _A1, class _A2>
1513const std::type_info&
1514function<_R(_A0, _A1, _A2)>::target_type() const
1515{
1516 if (__f_ == 0)
1517 return typeid(void);
1518 return __f_->target_type();
1519}
1520
1521template<class _R, class _A0, class _A1, class _A2>
1522template <typename _T>
1523_T*
1524function<_R(_A0, _A1, _A2)>::target()
1525{
1526 if (__f_ == 0)
1527 return (_T*)0;
1528 return (_T*)__f_->target(typeid(_T));
1529}
1530
1531template<class _R, class _A0, class _A1, class _A2>
1532template <typename _T>
1533const _T*
1534function<_R(_A0, _A1, _A2)>::target() const
1535{
1536 if (__f_ == 0)
1537 return (const _T*)0;
1538 return (const _T*)__f_->target(typeid(_T));
1539}
1540
1541template <class _F>
1542inline _LIBCPP_INLINE_VISIBILITY
1543bool
1544operator==(const function<_F>& __f, nullptr_t) {return !__f;}
1545
1546template <class _F>
1547inline _LIBCPP_INLINE_VISIBILITY
1548bool
1549operator==(nullptr_t, const function<_F>& __f) {return !__f;}
1550
1551template <class _F>
1552inline _LIBCPP_INLINE_VISIBILITY
1553bool
1554operator!=(const function<_F>& __f, nullptr_t) {return (bool)__f;}
1555
1556template <class _F>
1557inline _LIBCPP_INLINE_VISIBILITY
1558bool
1559operator!=(nullptr_t, const function<_F>& __f) {return (bool)__f;}
1560
1561template <class _F>
1562inline _LIBCPP_INLINE_VISIBILITY
1563void
1564swap(function<_F>& __x, function<_F>& __y)
1565{return __x.swap(__y);}
1566
1567template<class _Tp> struct __is_bind_expression : public false_type {};
1568template<class _Tp> struct is_bind_expression
1569 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1570
1571template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1572template<class _Tp> struct is_placeholder
1573 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1574
1575namespace placeholders
1576{
1577
1578template <int _N> struct __ph {};
1579
1580extern __ph<1> _1;
1581extern __ph<2> _2;
1582extern __ph<3> _3;
1583extern __ph<4> _4;
1584extern __ph<5> _5;
1585extern __ph<6> _6;
1586extern __ph<7> _7;
1587extern __ph<8> _8;
1588extern __ph<9> _9;
1589extern __ph<10> _10;
1590
1591} // placeholders
1592
1593template<int _N>
1594struct __is_placeholder<placeholders::__ph<_N> >
1595 : public integral_constant<int, _N> {};
1596
1597template <class _Tp, class _Uj>
1598inline _LIBCPP_INLINE_VISIBILITY
1599_Tp&
1600__mu(reference_wrapper<_Tp> __t, _Uj&)
1601{
1602 return __t.get();
1603}
1604/*
1605template <bool _IsBindExpr, class _Ti, class ..._Uj>
1606struct __mu_return1 {};
1607
1608template <class _Ti, class ..._Uj>
1609struct __mu_return1<true, _Ti, _Uj...>
1610{
1611 typedef typename result_of<_Ti(_Uj...)>::type type;
1612};
1613
1614
1615template <class _Ti, class ..._Uj, size_t ..._Indx>
1616inline _LIBCPP_INLINE_VISIBILITY
1617typename __mu_return1<true, _Ti, _Uj...>::type
1618__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1619{
1620 __ti(_STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
1621}
1622
1623template <class _Ti, class ..._Uj>
1624inline _LIBCPP_INLINE_VISIBILITY
1625typename enable_if
1626<
1627 is_bind_expression<_Ti>::value,
1628 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1629>::type
1630__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1631{
1632 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1633 return __mu_expand(__ti, __uj, __indices());
1634}
1635
1636template <bool IsPh, class _Ti, class _Uj>
1637struct __mu_return2 {};
1638
1639template <class _Ti, class _Uj>
1640struct __mu_return2<true, _Ti, _Uj>
1641{
1642 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1643};
1644
1645template <class _Ti, class _Uj>
1646inline _LIBCPP_INLINE_VISIBILITY
1647typename enable_if
1648<
1649 0 < is_placeholder<_Ti>::value,
1650 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1651>::type
1652__mu(_Ti&, _Uj& __uj)
1653{
1654 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1655 // compiler bug workaround
1656 typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1657 return __t;
1658// return _STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
1659}
1660
1661template <class _Ti, class _Uj>
1662inline _LIBCPP_INLINE_VISIBILITY
1663typename enable_if
1664<
1665 !is_bind_expression<_Ti>::value &&
1666 is_placeholder<_Ti>::value == 0 &&
1667 !__is_reference_wrapper<_Ti>::value,
1668 _Ti&
1669>::type
1670__mu(_Ti& __ti, _Uj& __uj)
1671{
1672 return __ti;
1673}
1674
1675template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1676struct ____mu_return;
1677
1678template <class _Ti, class ..._Uj>
1679struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1680{
1681 typedef typename result_of<_Ti(_Uj...)>::type type;
1682};
1683
1684template <class _Ti, class _TupleUj>
1685struct ____mu_return<_Ti, false, true, _TupleUj>
1686{
1687 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1688 _TupleUj>::type&& type;
1689};
1690
1691template <class _Ti, class _TupleUj>
1692struct ____mu_return<_Ti, false, false, _TupleUj>
1693{
1694 typedef _Ti& type;
1695};
1696
1697template <class _Ti, class _TupleUj>
1698struct __mu_return
1699 : public ____mu_return<_Ti,
1700 is_bind_expression<_Ti>::value,
1701 0 < is_placeholder<_Ti>::value,
1702 _TupleUj>
1703{
1704};
1705
1706template <class _Ti, class _TupleUj>
1707struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1708{
1709 typedef _Ti& type;
1710};
1711
1712template <class _F, class _BoundArgs, class _TupleUj>
1713struct __bind_return;
1714
1715template <class _F, class ..._BoundArgs, class _TupleUj>
1716struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj>
1717{
1718 typedef typename __ref_return
1719 <
1720 _F&,
1721 typename __mu_return
1722 <
1723 _BoundArgs,
1724 _TupleUj
1725 >::type...
1726 >::type type;
1727};
1728
1729template <class _F, class ..._BoundArgs, class _TupleUj>
1730struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj>
1731{
1732 typedef typename __ref_return
1733 <
1734 _F&,
1735 typename __mu_return
1736 <
1737 const _BoundArgs,
1738 _TupleUj
1739 >::type...
1740 >::type type;
1741};
1742
1743template <class _F, class _BoundArgs, size_t ..._Indx, class _Args>
1744inline _LIBCPP_INLINE_VISIBILITY
1745typename __bind_return<_F, _BoundArgs, _Args>::type
1746__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
1747 _Args&& __args)
1748{
1749 return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
1750}
1751
1752template<class _F, class ..._BoundArgs>
1753class __bind
1754{
1755 _F __f_;
1756 tuple<_BoundArgs...> __bound_args_;
1757
1758 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
1759public:
1760 template <class _G, class ..._BA>
1761 explicit __bind(_G&& __f, _BA&& ...__bound_args)
1762 : __f_(_STD::forward<_G>(__f)),
1763 __bound_args_(_STD::forward<_BA>(__bound_args)...) {}
1764
1765 template <class ..._Args>
1766 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1767 operator()(_Args&& ...__args)
1768 {
1769 // compiler bug workaround
1770 return __apply_functor(__f_, __bound_args_, __indices(),
1771 tuple<_Args&&...>(__args...));
1772 }
1773
1774 template <class ..._Args>
1775 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1776 operator()(_Args&& ...__args) const
1777 {
1778 return __apply_functor(__f_, __bound_args_, __indices(),
1779 tuple<_Args&&...>(__args...));
1780 }
1781};
1782
1783template<class _F, class ..._BoundArgs>
1784struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {};
1785
1786template<class _R, class _F, class ..._BoundArgs>
1787class __bind_r
1788 : public __bind<_F, _BoundArgs...>
1789{
1790 typedef __bind<_F, _BoundArgs...> base;
1791public:
1792 typedef _R result_type;
1793
1794 template <class _G, class ..._BA>
1795 explicit __bind_r(_G&& __f, _BA&& ...__bound_args)
1796 : base(_STD::forward<_G>(__f),
1797 _STD::forward<_BA>(__bound_args)...) {}
1798
1799 template <class ..._Args>
1800 result_type
1801 operator()(_Args&& ...__args)
1802 {
1803 return base::operator()(_STD::forward<_Args>(__args)...);
1804 }
1805
1806 template <class ..._Args>
1807 result_type
1808 operator()(_Args&& ...__args) const
1809 {
1810 return base::operator()(_STD::forward<_Args>(__args)...);
1811 }
1812};
1813
1814template<class _R, class _F, class ..._BoundArgs>
1815struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {};
1816
1817template<class _F, class ..._BoundArgs>
1818inline _LIBCPP_INLINE_VISIBILITY
1819__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1820bind(_F&& __f, _BoundArgs&&... __bound_args)
1821{
1822 typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1823 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1824}
1825
1826template<class _R, class _F, class ..._BoundArgs>
1827inline _LIBCPP_INLINE_VISIBILITY
1828__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1829bind(_F&& __f, _BoundArgs&&... __bound_args)
1830{
1831 typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1832 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1833}
1834*/
1835
1836#endif