blob: fa318df162a3e4262158f7bc58aada44f5377156 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
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;
Howard Hinnantd4444702010-08-11 17:04:31 +0000246#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247 virtual const void* target(const type_info&) const = 0;
248 virtual const std::type_info& target_type() const = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000249#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250};
251
252template<class _R, class _A0>
253class __base<_R(_A0)>
254{
255 __base(const __base&);
256 __base& operator=(const __base&);
257public:
258 __base() {}
259 virtual ~__base() {}
260 virtual __base* __clone() const = 0;
261 virtual void __clone(__base*) const = 0;
262 virtual void destroy() = 0;
263 virtual void destroy_deallocate() = 0;
264 virtual _R operator()(_A0) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000265#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266 virtual const void* target(const type_info&) const = 0;
267 virtual const std::type_info& target_type() const = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000268#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269};
270
271template<class _R, class _A0, class _A1>
272class __base<_R(_A0, _A1)>
273{
274 __base(const __base&);
275 __base& operator=(const __base&);
276public:
277 __base() {}
278 virtual ~__base() {}
279 virtual __base* __clone() const = 0;
280 virtual void __clone(__base*) const = 0;
281 virtual void destroy() = 0;
282 virtual void destroy_deallocate() = 0;
283 virtual _R operator()(_A0, _A1) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000284#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285 virtual const void* target(const type_info&) const = 0;
286 virtual const std::type_info& target_type() const = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000287#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288};
289
290template<class _R, class _A0, class _A1, class _A2>
291class __base<_R(_A0, _A1, _A2)>
292{
293 __base(const __base&);
294 __base& operator=(const __base&);
295public:
296 __base() {}
297 virtual ~__base() {}
298 virtual __base* __clone() const = 0;
299 virtual void __clone(__base*) const = 0;
300 virtual void destroy() = 0;
301 virtual void destroy_deallocate() = 0;
302 virtual _R operator()(_A0, _A1, _A2) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000303#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000304 virtual const void* target(const type_info&) const = 0;
305 virtual const std::type_info& target_type() const = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000306#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000307};
308
309template<class _FD, class _Alloc, class _FB> class __func;
310
311template<class _F, class _Alloc, class _R>
312class __func<_F, _Alloc, _R()>
313 : public __base<_R()>
314{
315 __compressed_pair<_F, _Alloc> __f_;
316public:
317 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
318 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
319 virtual __base<_R()>* __clone() const;
320 virtual void __clone(__base<_R()>*) const;
321 virtual void destroy();
322 virtual void destroy_deallocate();
323 virtual _R operator()();
Howard Hinnantd4444702010-08-11 17:04:31 +0000324#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325 virtual const void* target(const type_info&) const;
326 virtual const std::type_info& target_type() const;
Howard Hinnantd4444702010-08-11 17:04:31 +0000327#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328};
329
330template<class _F, class _Alloc, class _R>
331__base<_R()>*
332__func<_F, _Alloc, _R()>::__clone() const
333{
334 typedef typename _Alloc::template rebind<__func>::other _A;
335 _A __a(__f_.second());
336 typedef __allocator_destructor<_A> _D;
337 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
338 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
339 return __hold.release();
340}
341
342template<class _F, class _Alloc, class _R>
343void
344__func<_F, _Alloc, _R()>::__clone(__base<_R()>* __p) const
345{
346 ::new (__p) __func(__f_.first(), __f_.second());
347}
348
349template<class _F, class _Alloc, class _R>
350void
351__func<_F, _Alloc, _R()>::destroy()
352{
353 __f_.~__compressed_pair<_F, _Alloc>();
354}
355
356template<class _F, class _Alloc, class _R>
357void
358__func<_F, _Alloc, _R()>::destroy_deallocate()
359{
360 typedef typename _Alloc::template rebind<__func>::other _A;
361 _A __a(__f_.second());
362 __f_.~__compressed_pair<_F, _Alloc>();
363 __a.deallocate(this, 1);
364}
365
366template<class _F, class _Alloc, class _R>
367_R
368__func<_F, _Alloc, _R()>::operator()()
369{
370 return __invoke<_R>(__f_.first());
371}
372
Howard Hinnantd4444702010-08-11 17:04:31 +0000373#ifndef _LIBCPP_NO_RTTI
374
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375template<class _F, class _Alloc, class _R>
376const void*
377__func<_F, _Alloc, _R()>::target(const type_info& __ti) const
378{
379 if (__ti == typeid(_F))
380 return &__f_.first();
381 return (const void*)0;
382}
383
384template<class _F, class _Alloc, class _R>
385const std::type_info&
386__func<_F, _Alloc, _R()>::target_type() const
387{
388 return typeid(_F);
389}
390
Howard Hinnantd4444702010-08-11 17:04:31 +0000391#endif
392
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393template<class _F, class _Alloc, class _R, class _A0>
394class __func<_F, _Alloc, _R(_A0)>
395 : public __base<_R(_A0)>
396{
397 __compressed_pair<_F, _Alloc> __f_;
398public:
399 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
400 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
401 virtual __base<_R(_A0)>* __clone() const;
402 virtual void __clone(__base<_R(_A0)>*) const;
403 virtual void destroy();
404 virtual void destroy_deallocate();
405 virtual _R operator()(_A0);
Howard Hinnantd4444702010-08-11 17:04:31 +0000406#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407 virtual const void* target(const type_info&) const;
408 virtual const std::type_info& target_type() const;
Howard Hinnantd4444702010-08-11 17:04:31 +0000409#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410};
411
412template<class _F, class _Alloc, class _R, class _A0>
413__base<_R(_A0)>*
414__func<_F, _Alloc, _R(_A0)>::__clone() const
415{
416 typedef typename _Alloc::template rebind<__func>::other _A;
417 _A __a(__f_.second());
418 typedef __allocator_destructor<_A> _D;
419 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
420 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
421 return __hold.release();
422}
423
424template<class _F, class _Alloc, class _R, class _A0>
425void
426__func<_F, _Alloc, _R(_A0)>::__clone(__base<_R(_A0)>* __p) const
427{
428 ::new (__p) __func(__f_.first(), __f_.second());
429}
430
431template<class _F, class _Alloc, class _R, class _A0>
432void
433__func<_F, _Alloc, _R(_A0)>::destroy()
434{
435 __f_.~__compressed_pair<_F, _Alloc>();
436}
437
438template<class _F, class _Alloc, class _R, class _A0>
439void
440__func<_F, _Alloc, _R(_A0)>::destroy_deallocate()
441{
442 typedef typename _Alloc::template rebind<__func>::other _A;
443 _A __a(__f_.second());
444 __f_.~__compressed_pair<_F, _Alloc>();
445 __a.deallocate(this, 1);
446}
447
448template<class _F, class _Alloc, class _R, class _A0>
449_R
450__func<_F, _Alloc, _R(_A0)>::operator()(_A0 __a0)
451{
452 return __invoke(__f_.first(), __a0);
453}
454
Howard Hinnantd4444702010-08-11 17:04:31 +0000455#ifndef _LIBCPP_NO_RTTI
456
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457template<class _F, class _Alloc, class _R, class _A0>
458const void*
459__func<_F, _Alloc, _R(_A0)>::target(const type_info& __ti) const
460{
461 if (__ti == typeid(_F))
462 return &__f_.first();
463 return (const void*)0;
464}
465
466template<class _F, class _Alloc, class _R, class _A0>
467const std::type_info&
468__func<_F, _Alloc, _R(_A0)>::target_type() const
469{
470 return typeid(_F);
471}
472
Howard Hinnantd4444702010-08-11 17:04:31 +0000473#endif
474
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475template<class _F, class _Alloc, class _R, class _A0, class _A1>
476class __func<_F, _Alloc, _R(_A0, _A1)>
477 : public __base<_R(_A0, _A1)>
478{
479 __compressed_pair<_F, _Alloc> __f_;
480public:
481 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
482 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
483 virtual __base<_R(_A0, _A1)>* __clone() const;
484 virtual void __clone(__base<_R(_A0, _A1)>*) const;
485 virtual void destroy();
486 virtual void destroy_deallocate();
487 virtual _R operator()(_A0, _A1);
Howard Hinnantd4444702010-08-11 17:04:31 +0000488#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 virtual const void* target(const type_info&) const;
490 virtual const std::type_info& target_type() const;
Howard Hinnantd4444702010-08-11 17:04:31 +0000491#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492};
493
494template<class _F, class _Alloc, class _R, class _A0, class _A1>
495__base<_R(_A0, _A1)>*
496__func<_F, _Alloc, _R(_A0, _A1)>::__clone() const
497{
498 typedef typename _Alloc::template rebind<__func>::other _A;
499 _A __a(__f_.second());
500 typedef __allocator_destructor<_A> _D;
501 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
502 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
503 return __hold.release();
504}
505
506template<class _F, class _Alloc, class _R, class _A0, class _A1>
507void
508__func<_F, _Alloc, _R(_A0, _A1)>::__clone(__base<_R(_A0, _A1)>* __p) const
509{
510 ::new (__p) __func(__f_.first(), __f_.second());
511}
512
513template<class _F, class _Alloc, class _R, class _A0, class _A1>
514void
515__func<_F, _Alloc, _R(_A0, _A1)>::destroy()
516{
517 __f_.~__compressed_pair<_F, _Alloc>();
518}
519
520template<class _F, class _Alloc, class _R, class _A0, class _A1>
521void
522__func<_F, _Alloc, _R(_A0, _A1)>::destroy_deallocate()
523{
524 typedef typename _Alloc::template rebind<__func>::other _A;
525 _A __a(__f_.second());
526 __f_.~__compressed_pair<_F, _Alloc>();
527 __a.deallocate(this, 1);
528}
529
530template<class _F, class _Alloc, class _R, class _A0, class _A1>
531_R
532__func<_F, _Alloc, _R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
533{
534 return __invoke(__f_.first(), __a0, __a1);
535}
536
Howard Hinnantd4444702010-08-11 17:04:31 +0000537#ifndef _LIBCPP_NO_RTTI
538
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539template<class _F, class _Alloc, class _R, class _A0, class _A1>
540const void*
541__func<_F, _Alloc, _R(_A0, _A1)>::target(const type_info& __ti) const
542{
543 if (__ti == typeid(_F))
544 return &__f_.first();
545 return (const void*)0;
546}
547
548template<class _F, class _Alloc, class _R, class _A0, class _A1>
549const std::type_info&
550__func<_F, _Alloc, _R(_A0, _A1)>::target_type() const
551{
552 return typeid(_F);
553}
554
Howard Hinnantd4444702010-08-11 17:04:31 +0000555#endif
556
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
558class __func<_F, _Alloc, _R(_A0, _A1, _A2)>
559 : public __base<_R(_A0, _A1, _A2)>
560{
561 __compressed_pair<_F, _Alloc> __f_;
562public:
563 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
564 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
565 virtual __base<_R(_A0, _A1, _A2)>* __clone() const;
566 virtual void __clone(__base<_R(_A0, _A1, _A2)>*) const;
567 virtual void destroy();
568 virtual void destroy_deallocate();
569 virtual _R operator()(_A0, _A1, _A2);
Howard Hinnantd4444702010-08-11 17:04:31 +0000570#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000571 virtual const void* target(const type_info&) const;
572 virtual const std::type_info& target_type() const;
Howard Hinnantd4444702010-08-11 17:04:31 +0000573#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574};
575
576template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
577__base<_R(_A0, _A1, _A2)>*
578__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone() const
579{
580 typedef typename _Alloc::template rebind<__func>::other _A;
581 _A __a(__f_.second());
582 typedef __allocator_destructor<_A> _D;
583 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
584 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
585 return __hold.release();
586}
587
588template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
589void
590__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone(__base<_R(_A0, _A1, _A2)>* __p) const
591{
592 ::new (__p) __func(__f_.first(), __f_.second());
593}
594
595template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
596void
597__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy()
598{
599 __f_.~__compressed_pair<_F, _Alloc>();
600}
601
602template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
603void
604__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy_deallocate()
605{
606 typedef typename _Alloc::template rebind<__func>::other _A;
607 _A __a(__f_.second());
608 __f_.~__compressed_pair<_F, _Alloc>();
609 __a.deallocate(this, 1);
610}
611
612template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
613_R
614__func<_F, _Alloc, _R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
615{
616 return __invoke(__f_.first(), __a0, __a1, __a2);
617}
618
Howard Hinnantd4444702010-08-11 17:04:31 +0000619#ifndef _LIBCPP_NO_RTTI
620
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
622const void*
623__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target(const type_info& __ti) const
624{
625 if (__ti == typeid(_F))
626 return &__f_.first();
627 return (const void*)0;
628}
629
630template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
631const std::type_info&
632__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target_type() const
633{
634 return typeid(_F);
635}
636
Howard Hinnantd4444702010-08-11 17:04:31 +0000637#endif
638
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639} // __function
640
641template<class _R>
642class function<_R()>
643{
644 typedef __function::__base<_R()> __base;
645 aligned_storage<3*sizeof(void*)>::type __buf_;
646 __base* __f_;
647
648 template <class _F>
649 static bool __not_null(const _F&) {return true;}
650 template <class _R2>
651 static bool __not_null(const function<_R()>& __p) {return __p;}
652public:
653 typedef _R result_type;
654
655 // 20.7.16.2.1, construct/copy/destroy:
656 explicit function() : __f_(0) {}
657 function(nullptr_t) : __f_(0) {}
658 function(const function&);
659 template<class _F>
660 function(_F,
661 typename enable_if<!is_integral<_F>::value>::type* = 0);
662
663// template<class _Alloc>
664// function(allocator_arg_t, const _Alloc&);
665// template<Allocator Alloc>
666// function(allocator_arg_t, const Alloc&, nullptr_t);
667// template<Allocator Alloc>
668// function(allocator_arg_t, const Alloc&, const function&);
669// template<Allocator Alloc>
670// function(allocator_arg_t, const Alloc&, function&&);
671// template<class F, Allocator Alloc>
672// function(allocator_arg_t, const Alloc&, F);
673
674 function& operator=(const function&);
675 function& operator=(nullptr_t);
676 template<class _F>
677 typename enable_if
678 <
679 !is_integral<_F>::value,
680 function&
681 >::type
682 operator=(_F);
683
684 ~function();
685
686 // 20.7.16.2.2, function modifiers:
687 void swap(function&);
688// template<class _F, class _Alloc>
689// void assign(_F, const _Alloc&);
690
691 // 20.7.16.2.3, function capacity:
692 operator bool() const {return __f_;}
693
694private:
695 // deleted overloads close possible hole in the type system
696 template<class _R2>
697 bool operator==(const function<_R2()>&);// = delete;
698 template<class _R2>
699 bool operator!=(const function<_R2()>&);// = delete;
700public:
701 // 20.7.16.2.4, function invocation:
702 _R operator()() const;
703
Howard Hinnantd4444702010-08-11 17:04:31 +0000704#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705 // 20.7.16.2.5, function target access:
706 const std::type_info& target_type() const;
707 template <typename _T> _T* target();
708 template <typename _T> const _T* target() const;
Howard Hinnantd4444702010-08-11 17:04:31 +0000709#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710};
711
712template<class _R>
713function<_R()>::function(const function& __f)
714{
715 if (__f.__f_ == 0)
716 __f_ = 0;
717 else if (__f.__f_ == (const __base*)&__f.__buf_)
718 {
719 __f_ = (__base*)&__buf_;
720 __f.__f_->__clone(__f_);
721 }
722 else
723 __f_ = __f.__f_->__clone();
724}
725
726template<class _R>
727template <class _F>
728function<_R()>::function(_F __f,
729 typename enable_if<!is_integral<_F>::value>::type*)
730 : __f_(0)
731{
732 if (__not_null(__f))
733 {
734 typedef __function::__func<_F, allocator<_F>, _R()> _FF;
735 if (sizeof(_FF) <= sizeof(__buf_))
736 {
737 __f_ = (__base*)&__buf_;
738 ::new (__f_) _FF(__f);
739 }
740 else
741 {
742 typedef allocator<_FF> _A;
743 _A __a;
744 typedef __allocator_destructor<_A> _D;
745 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
746 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
747 __f_ = __hold.release();
748 }
749 }
750}
751
752template<class _R>
753function<_R()>&
754function<_R()>::operator=(const function& __f)
755{
756 function(__f).swap(*this);
757 return *this;
758}
759
760template<class _R>
761function<_R()>&
762function<_R()>::operator=(nullptr_t)
763{
764 if (__f_ == (__base*)&__buf_)
765 __f_->destroy();
766 else if (__f_)
767 __f_->destroy_deallocate();
768 __f_ = 0;
769}
770
771template<class _R>
772template <class _F>
773typename enable_if
774<
775 !is_integral<_F>::value,
776 function<_R()>&
777>::type
778function<_R()>::operator=(_F __f)
779{
780 function(_STD::move(__f)).swap(*this);
781 return *this;
782}
783
784template<class _R>
785function<_R()>::~function()
786{
787 if (__f_ == (__base*)&__buf_)
788 __f_->destroy();
789 else if (__f_)
790 __f_->destroy_deallocate();
791}
792
793template<class _R>
794void
795function<_R()>::swap(function& __f)
796{
797 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
798 {
799 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
800 __base* __t = (__base*)&__tempbuf;
801 __f_->__clone(__t);
802 __f_->destroy();
803 __f_ = 0;
804 __f.__f_->__clone((__base*)&__buf_);
805 __f.__f_->destroy();
806 __f.__f_ = 0;
807 __f_ = (__base*)&__buf_;
808 __t->__clone((__base*)&__f.__buf_);
809 __t->destroy();
810 __f.__f_ = (__base*)&__f.__buf_;
811 }
812 else if (__f_ == (__base*)&__buf_)
813 {
814 __f_->__clone((__base*)&__f.__buf_);
815 __f_->destroy();
816 __f_ = __f.__f_;
817 __f.__f_ = (__base*)&__f.__buf_;
818 }
819 else if (__f.__f_ == (__base*)&__f.__buf_)
820 {
821 __f.__f_->__clone((__base*)&__buf_);
822 __f.__f_->destroy();
823 __f.__f_ = __f_;
824 __f_ = (__base*)&__buf_;
825 }
826 else
827 _STD::swap(__f_, __f.__f_);
828}
829
830template<class _R>
831_R
832function<_R()>::operator()() const
833{
Howard Hinnantd4444702010-08-11 17:04:31 +0000834#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000835 if (__f_ == 0)
836 throw bad_function_call();
Howard Hinnantd4444702010-08-11 17:04:31 +0000837#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838 return (*__f_)();
839}
840
Howard Hinnantd4444702010-08-11 17:04:31 +0000841#ifndef _LIBCPP_NO_RTTI
842
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843template<class _R>
844const std::type_info&
845function<_R()>::target_type() const
846{
847 if (__f_ == 0)
848 return typeid(void);
849 return __f_->target_type();
850}
851
852template<class _R>
853template <typename _T>
854_T*
855function<_R()>::target()
856{
857 if (__f_ == 0)
858 return (_T*)0;
859 return (_T*)__f_->target(typeid(_T));
860}
861
862template<class _R>
863template <typename _T>
864const _T*
865function<_R()>::target() const
866{
867 if (__f_ == 0)
868 return (const _T*)0;
869 return (const _T*)__f_->target(typeid(_T));
870}
871
Howard Hinnantd4444702010-08-11 17:04:31 +0000872#endif
873
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874template<class _R, class _A0>
875class function<_R(_A0)>
876 : public unary_function<_A0, _R>
877{
878 typedef __function::__base<_R(_A0)> __base;
879 aligned_storage<3*sizeof(void*)>::type __buf_;
880 __base* __f_;
881
882 template <class _F>
883 static bool __not_null(const _F&) {return true;}
884 template <class _R2, class _B0>
885 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
886 template <class _R2, class _C>
887 static bool __not_null(_R2 (_C::*__p)()) {return __p;}
888 template <class _R2, class _C>
889 static bool __not_null(_R2 (_C::*__p)() const) {return __p;}
890 template <class _R2, class _C>
891 static bool __not_null(_R2 (_C::*__p)() volatile) {return __p;}
892 template <class _R2, class _C>
893 static bool __not_null(_R2 (_C::*__p)() const volatile) {return __p;}
894 template <class _R2, class _B0>
895 static bool __not_null(const function<_R(_B0)>& __p) {return __p;}
896public:
897 typedef _R result_type;
898
899 // 20.7.16.2.1, construct/copy/destroy:
900 explicit function() : __f_(0) {}
901 function(nullptr_t) : __f_(0) {}
902 function(const function&);
903 template<class _F>
904 function(_F,
905 typename enable_if<!is_integral<_F>::value>::type* = 0);
906
907// template<class _Alloc>
908// function(allocator_arg_t, const _Alloc&);
909// template<Allocator Alloc>
910// function(allocator_arg_t, const Alloc&, nullptr_t);
911// template<Allocator Alloc>
912// function(allocator_arg_t, const Alloc&, const function&);
913// template<Allocator Alloc>
914// function(allocator_arg_t, const Alloc&, function&&);
915// template<class F, Allocator Alloc>
916// function(allocator_arg_t, const Alloc&, F);
917
918 function& operator=(const function&);
919 function& operator=(nullptr_t);
920 template<class _F>
921 typename enable_if
922 <
923 !is_integral<_F>::value,
924 function&
925 >::type
926 operator=(_F);
927
928 ~function();
929
930 // 20.7.16.2.2, function modifiers:
931 void swap(function&);
932// template<class _F, class _Alloc>
933// void assign(_F, const _Alloc&);
934
935 // 20.7.16.2.3, function capacity:
936 operator bool() const {return __f_;}
937
938private:
939 // deleted overloads close possible hole in the type system
940 template<class _R2, class _B0>
941 bool operator==(const function<_R2(_B0)>&);// = delete;
942 template<class _R2, class _B0>
943 bool operator!=(const function<_R2(_B0)>&);// = delete;
944public:
945 // 20.7.16.2.4, function invocation:
946 _R operator()(_A0) const;
947
Howard Hinnantd4444702010-08-11 17:04:31 +0000948#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949 // 20.7.16.2.5, function target access:
950 const std::type_info& target_type() const;
951 template <typename _T> _T* target();
952 template <typename _T> const _T* target() const;
Howard Hinnantd4444702010-08-11 17:04:31 +0000953#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954};
955
956template<class _R, class _A0>
957function<_R(_A0)>::function(const function& __f)
958{
959 if (__f.__f_ == 0)
960 __f_ = 0;
961 else if (__f.__f_ == (const __base*)&__f.__buf_)
962 {
963 __f_ = (__base*)&__buf_;
964 __f.__f_->__clone(__f_);
965 }
966 else
967 __f_ = __f.__f_->__clone();
968}
969
970template<class _R, class _A0>
971template <class _F>
972function<_R(_A0)>::function(_F __f,
973 typename enable_if<!is_integral<_F>::value>::type*)
974 : __f_(0)
975{
976 if (__not_null(__f))
977 {
978 typedef __function::__func<_F, allocator<_F>, _R(_A0)> _FF;
979 if (sizeof(_FF) <= sizeof(__buf_))
980 {
981 __f_ = (__base*)&__buf_;
982 ::new (__f_) _FF(__f);
983 }
984 else
985 {
986 typedef allocator<_FF> _A;
987 _A __a;
988 typedef __allocator_destructor<_A> _D;
989 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
990 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
991 __f_ = __hold.release();
992 }
993 }
994}
995
996template<class _R, class _A0>
997function<_R(_A0)>&
998function<_R(_A0)>::operator=(const function& __f)
999{
1000 function(__f).swap(*this);
1001 return *this;
1002}
1003
1004template<class _R, class _A0>
1005function<_R(_A0)>&
1006function<_R(_A0)>::operator=(nullptr_t)
1007{
1008 if (__f_ == (__base*)&__buf_)
1009 __f_->destroy();
1010 else if (__f_)
1011 __f_->destroy_deallocate();
1012 __f_ = 0;
1013}
1014
1015template<class _R, class _A0>
1016template <class _F>
1017typename enable_if
1018<
1019 !is_integral<_F>::value,
1020 function<_R(_A0)>&
1021>::type
1022function<_R(_A0)>::operator=(_F __f)
1023{
1024 function(_STD::move(__f)).swap(*this);
1025 return *this;
1026}
1027
1028template<class _R, class _A0>
1029function<_R(_A0)>::~function()
1030{
1031 if (__f_ == (__base*)&__buf_)
1032 __f_->destroy();
1033 else if (__f_)
1034 __f_->destroy_deallocate();
1035}
1036
1037template<class _R, class _A0>
1038void
1039function<_R(_A0)>::swap(function& __f)
1040{
1041 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1042 {
1043 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1044 __base* __t = (__base*)&__tempbuf;
1045 __f_->__clone(__t);
1046 __f_->destroy();
1047 __f_ = 0;
1048 __f.__f_->__clone((__base*)&__buf_);
1049 __f.__f_->destroy();
1050 __f.__f_ = 0;
1051 __f_ = (__base*)&__buf_;
1052 __t->__clone((__base*)&__f.__buf_);
1053 __t->destroy();
1054 __f.__f_ = (__base*)&__f.__buf_;
1055 }
1056 else if (__f_ == (__base*)&__buf_)
1057 {
1058 __f_->__clone((__base*)&__f.__buf_);
1059 __f_->destroy();
1060 __f_ = __f.__f_;
1061 __f.__f_ = (__base*)&__f.__buf_;
1062 }
1063 else if (__f.__f_ == (__base*)&__f.__buf_)
1064 {
1065 __f.__f_->__clone((__base*)&__buf_);
1066 __f.__f_->destroy();
1067 __f.__f_ = __f_;
1068 __f_ = (__base*)&__buf_;
1069 }
1070 else
1071 _STD::swap(__f_, __f.__f_);
1072}
1073
1074template<class _R, class _A0>
1075_R
1076function<_R(_A0)>::operator()(_A0 __a0) const
1077{
Howard Hinnantd4444702010-08-11 17:04:31 +00001078#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079 if (__f_ == 0)
1080 throw bad_function_call();
Howard Hinnantd4444702010-08-11 17:04:31 +00001081#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082 return (*__f_)(__a0);
1083}
1084
Howard Hinnantd4444702010-08-11 17:04:31 +00001085#ifndef _LIBCPP_NO_RTTI
1086
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001087template<class _R, class _A0>
1088const std::type_info&
1089function<_R(_A0)>::target_type() const
1090{
1091 if (__f_ == 0)
1092 return typeid(void);
1093 return __f_->target_type();
1094}
1095
1096template<class _R, class _A0>
1097template <typename _T>
1098_T*
1099function<_R(_A0)>::target()
1100{
1101 if (__f_ == 0)
1102 return (_T*)0;
1103 return (_T*)__f_->target(typeid(_T));
1104}
1105
1106template<class _R, class _A0>
1107template <typename _T>
1108const _T*
1109function<_R(_A0)>::target() const
1110{
1111 if (__f_ == 0)
1112 return (const _T*)0;
1113 return (const _T*)__f_->target(typeid(_T));
1114}
1115
Howard Hinnantd4444702010-08-11 17:04:31 +00001116#endif
1117
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118template<class _R, class _A0, class _A1>
1119class function<_R(_A0, _A1)>
1120 : public binary_function<_A0, _A1, _R>
1121{
1122 typedef __function::__base<_R(_A0, _A1)> __base;
1123 aligned_storage<3*sizeof(void*)>::type __buf_;
1124 __base* __f_;
1125
1126 template <class _F>
1127 static bool __not_null(const _F&) {return true;}
1128 template <class _R2, class _B0, class _B1>
1129 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
1130 template <class _R2, class _C, class _B1>
1131 static bool __not_null(_R2 (_C::*__p)(_B1)) {return __p;}
1132 template <class _R2, class _C, class _B1>
1133 static bool __not_null(_R2 (_C::*__p)(_B1) const) {return __p;}
1134 template <class _R2, class _C, class _B1>
1135 static bool __not_null(_R2 (_C::*__p)(_B1) volatile) {return __p;}
1136 template <class _R2, class _C, class _B1>
1137 static bool __not_null(_R2 (_C::*__p)(_B1) const volatile) {return __p;}
1138 template <class _R2, class _B0, class _B1>
1139 static bool __not_null(const function<_R(_B0, _B1)>& __p) {return __p;}
1140public:
1141 typedef _R result_type;
1142
1143 // 20.7.16.2.1, construct/copy/destroy:
1144 explicit function() : __f_(0) {}
1145 function(nullptr_t) : __f_(0) {}
1146 function(const function&);
1147 template<class _F>
1148 function(_F,
1149 typename enable_if<!is_integral<_F>::value>::type* = 0);
1150
1151// template<class _Alloc>
1152// function(allocator_arg_t, const _Alloc&);
1153// template<Allocator Alloc>
1154// function(allocator_arg_t, const Alloc&, nullptr_t);
1155// template<Allocator Alloc>
1156// function(allocator_arg_t, const Alloc&, const function&);
1157// template<Allocator Alloc>
1158// function(allocator_arg_t, const Alloc&, function&&);
1159// template<class F, Allocator Alloc>
1160// function(allocator_arg_t, const Alloc&, F);
1161
1162 function& operator=(const function&);
1163 function& operator=(nullptr_t);
1164 template<class _F>
1165 typename enable_if
1166 <
1167 !is_integral<_F>::value,
1168 function&
1169 >::type
1170 operator=(_F);
1171
1172 ~function();
1173
1174 // 20.7.16.2.2, function modifiers:
1175 void swap(function&);
1176// template<class _F, class _Alloc>
1177// void assign(_F, const _Alloc&);
1178
1179 // 20.7.16.2.3, function capacity:
1180 operator bool() const {return __f_;}
1181
1182private:
1183 // deleted overloads close possible hole in the type system
1184 template<class _R2, class _B0, class _B1>
1185 bool operator==(const function<_R2(_B0, _B1)>&);// = delete;
1186 template<class _R2, class _B0, class _B1>
1187 bool operator!=(const function<_R2(_B0, _B1)>&);// = delete;
1188public:
1189 // 20.7.16.2.4, function invocation:
1190 _R operator()(_A0, _A1) const;
1191
Howard Hinnantd4444702010-08-11 17:04:31 +00001192#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 // 20.7.16.2.5, function target access:
1194 const std::type_info& target_type() const;
1195 template <typename _T> _T* target();
1196 template <typename _T> const _T* target() const;
Howard Hinnantd4444702010-08-11 17:04:31 +00001197#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001198};
1199
1200template<class _R, class _A0, class _A1>
1201function<_R(_A0, _A1)>::function(const function& __f)
1202{
1203 if (__f.__f_ == 0)
1204 __f_ = 0;
1205 else if (__f.__f_ == (const __base*)&__f.__buf_)
1206 {
1207 __f_ = (__base*)&__buf_;
1208 __f.__f_->__clone(__f_);
1209 }
1210 else
1211 __f_ = __f.__f_->__clone();
1212}
1213
1214template<class _R, class _A0, class _A1>
1215template <class _F>
1216function<_R(_A0, _A1)>::function(_F __f,
1217 typename enable_if<!is_integral<_F>::value>::type*)
1218 : __f_(0)
1219{
1220 if (__not_null(__f))
1221 {
1222 typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1)> _FF;
1223 if (sizeof(_FF) <= sizeof(__buf_))
1224 {
1225 __f_ = (__base*)&__buf_;
1226 ::new (__f_) _FF(__f);
1227 }
1228 else
1229 {
1230 typedef allocator<_FF> _A;
1231 _A __a;
1232 typedef __allocator_destructor<_A> _D;
1233 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1234 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1235 __f_ = __hold.release();
1236 }
1237 }
1238}
1239
1240template<class _R, class _A0, class _A1>
1241function<_R(_A0, _A1)>&
1242function<_R(_A0, _A1)>::operator=(const function& __f)
1243{
1244 function(__f).swap(*this);
1245 return *this;
1246}
1247
1248template<class _R, class _A0, class _A1>
1249function<_R(_A0, _A1)>&
1250function<_R(_A0, _A1)>::operator=(nullptr_t)
1251{
1252 if (__f_ == (__base*)&__buf_)
1253 __f_->destroy();
1254 else if (__f_)
1255 __f_->destroy_deallocate();
1256 __f_ = 0;
1257}
1258
1259template<class _R, class _A0, class _A1>
1260template <class _F>
1261typename enable_if
1262<
1263 !is_integral<_F>::value,
1264 function<_R(_A0, _A1)>&
1265>::type
1266function<_R(_A0, _A1)>::operator=(_F __f)
1267{
1268 function(_STD::move(__f)).swap(*this);
1269 return *this;
1270}
1271
1272template<class _R, class _A0, class _A1>
1273function<_R(_A0, _A1)>::~function()
1274{
1275 if (__f_ == (__base*)&__buf_)
1276 __f_->destroy();
1277 else if (__f_)
1278 __f_->destroy_deallocate();
1279}
1280
1281template<class _R, class _A0, class _A1>
1282void
1283function<_R(_A0, _A1)>::swap(function& __f)
1284{
1285 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1286 {
1287 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1288 __base* __t = (__base*)&__tempbuf;
1289 __f_->__clone(__t);
1290 __f_->destroy();
1291 __f_ = 0;
1292 __f.__f_->__clone((__base*)&__buf_);
1293 __f.__f_->destroy();
1294 __f.__f_ = 0;
1295 __f_ = (__base*)&__buf_;
1296 __t->__clone((__base*)&__f.__buf_);
1297 __t->destroy();
1298 __f.__f_ = (__base*)&__f.__buf_;
1299 }
1300 else if (__f_ == (__base*)&__buf_)
1301 {
1302 __f_->__clone((__base*)&__f.__buf_);
1303 __f_->destroy();
1304 __f_ = __f.__f_;
1305 __f.__f_ = (__base*)&__f.__buf_;
1306 }
1307 else if (__f.__f_ == (__base*)&__f.__buf_)
1308 {
1309 __f.__f_->__clone((__base*)&__buf_);
1310 __f.__f_->destroy();
1311 __f.__f_ = __f_;
1312 __f_ = (__base*)&__buf_;
1313 }
1314 else
1315 _STD::swap(__f_, __f.__f_);
1316}
1317
1318template<class _R, class _A0, class _A1>
1319_R
1320function<_R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1321{
Howard Hinnantd4444702010-08-11 17:04:31 +00001322#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001323 if (__f_ == 0)
1324 throw bad_function_call();
Howard Hinnantd4444702010-08-11 17:04:31 +00001325#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 return (*__f_)(__a0, __a1);
1327}
1328
Howard Hinnantd4444702010-08-11 17:04:31 +00001329#ifndef _LIBCPP_NO_RTTI
1330
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331template<class _R, class _A0, class _A1>
1332const std::type_info&
1333function<_R(_A0, _A1)>::target_type() const
1334{
1335 if (__f_ == 0)
1336 return typeid(void);
1337 return __f_->target_type();
1338}
1339
1340template<class _R, class _A0, class _A1>
1341template <typename _T>
1342_T*
1343function<_R(_A0, _A1)>::target()
1344{
1345 if (__f_ == 0)
1346 return (_T*)0;
1347 return (_T*)__f_->target(typeid(_T));
1348}
1349
1350template<class _R, class _A0, class _A1>
1351template <typename _T>
1352const _T*
1353function<_R(_A0, _A1)>::target() const
1354{
1355 if (__f_ == 0)
1356 return (const _T*)0;
1357 return (const _T*)__f_->target(typeid(_T));
1358}
1359
Howard Hinnantd4444702010-08-11 17:04:31 +00001360#endif
1361
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362template<class _R, class _A0, class _A1, class _A2>
1363class function<_R(_A0, _A1, _A2)>
1364{
1365 typedef __function::__base<_R(_A0, _A1, _A2)> __base;
1366 aligned_storage<3*sizeof(void*)>::type __buf_;
1367 __base* __f_;
1368
1369 template <class _F>
1370 static bool __not_null(const _F&) {return true;}
1371 template <class _R2, class _B0, class _B1, class _B2>
1372 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
1373 template <class _R2, class _C, class _B1, class _B2>
1374 static bool __not_null(_R2 (_C::*__p)(_B1, _B2)) {return __p;}
1375 template <class _R2, class _C, class _B1, class _B2>
1376 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const) {return __p;}
1377 template <class _R2, class _C, class _B1, class _B2>
1378 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) volatile) {return __p;}
1379 template <class _R2, class _C, class _B1, class _B2>
1380 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const volatile) {return __p;}
1381 template <class _R2, class _B0, class _B1, class _B2>
1382 static bool __not_null(const function<_R(_B0, _B1, _B2)>& __p) {return __p;}
1383public:
1384 typedef _R result_type;
1385
1386 // 20.7.16.2.1, construct/copy/destroy:
1387 explicit function() : __f_(0) {}
1388 function(nullptr_t) : __f_(0) {}
1389 function(const function&);
1390 template<class _F>
1391 function(_F,
1392 typename enable_if<!is_integral<_F>::value>::type* = 0);
1393
1394// template<class _Alloc>
1395// function(allocator_arg_t, const _Alloc&);
1396// template<Allocator Alloc>
1397// function(allocator_arg_t, const Alloc&, nullptr_t);
1398// template<Allocator Alloc>
1399// function(allocator_arg_t, const Alloc&, const function&);
1400// template<Allocator Alloc>
1401// function(allocator_arg_t, const Alloc&, function&&);
1402// template<class F, Allocator Alloc>
1403// function(allocator_arg_t, const Alloc&, F);
1404
1405 function& operator=(const function&);
1406 function& operator=(nullptr_t);
1407 template<class _F>
1408 typename enable_if
1409 <
1410 !is_integral<_F>::value,
1411 function&
1412 >::type
1413 operator=(_F);
1414
1415 ~function();
1416
1417 // 20.7.16.2.2, function modifiers:
1418 void swap(function&);
1419// template<class _F, class _Alloc>
1420// void assign(_F, const _Alloc&);
1421
1422 // 20.7.16.2.3, function capacity:
1423 operator bool() const {return __f_;}
1424
1425private:
1426 // deleted overloads close possible hole in the type system
1427 template<class _R2, class _B0, class _B1, class _B2>
1428 bool operator==(const function<_R2(_B0, _B1, _B2)>&);// = delete;
1429 template<class _R2, class _B0, class _B1, class _B2>
1430 bool operator!=(const function<_R2(_B0, _B1, _B2)>&);// = delete;
1431public:
1432 // 20.7.16.2.4, function invocation:
1433 _R operator()(_A0, _A1, _A2) const;
1434
Howard Hinnantd4444702010-08-11 17:04:31 +00001435#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436 // 20.7.16.2.5, function target access:
1437 const std::type_info& target_type() const;
1438 template <typename _T> _T* target();
1439 template <typename _T> const _T* target() const;
Howard Hinnantd4444702010-08-11 17:04:31 +00001440#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441};
1442
1443template<class _R, class _A0, class _A1, class _A2>
1444function<_R(_A0, _A1, _A2)>::function(const function& __f)
1445{
1446 if (__f.__f_ == 0)
1447 __f_ = 0;
1448 else if (__f.__f_ == (const __base*)&__f.__buf_)
1449 {
1450 __f_ = (__base*)&__buf_;
1451 __f.__f_->__clone(__f_);
1452 }
1453 else
1454 __f_ = __f.__f_->__clone();
1455}
1456
1457template<class _R, class _A0, class _A1, class _A2>
1458template <class _F>
1459function<_R(_A0, _A1, _A2)>::function(_F __f,
1460 typename enable_if<!is_integral<_F>::value>::type*)
1461 : __f_(0)
1462{
1463 if (__not_null(__f))
1464 {
1465 typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1, _A2)> _FF;
1466 if (sizeof(_FF) <= sizeof(__buf_))
1467 {
1468 __f_ = (__base*)&__buf_;
1469 ::new (__f_) _FF(__f);
1470 }
1471 else
1472 {
1473 typedef allocator<_FF> _A;
1474 _A __a;
1475 typedef __allocator_destructor<_A> _D;
1476 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1477 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1478 __f_ = __hold.release();
1479 }
1480 }
1481}
1482
1483template<class _R, class _A0, class _A1, class _A2>
1484function<_R(_A0, _A1, _A2)>&
1485function<_R(_A0, _A1, _A2)>::operator=(const function& __f)
1486{
1487 function(__f).swap(*this);
1488 return *this;
1489}
1490
1491template<class _R, class _A0, class _A1, class _A2>
1492function<_R(_A0, _A1, _A2)>&
1493function<_R(_A0, _A1, _A2)>::operator=(nullptr_t)
1494{
1495 if (__f_ == (__base*)&__buf_)
1496 __f_->destroy();
1497 else if (__f_)
1498 __f_->destroy_deallocate();
1499 __f_ = 0;
1500}
1501
1502template<class _R, class _A0, class _A1, class _A2>
1503template <class _F>
1504typename enable_if
1505<
1506 !is_integral<_F>::value,
1507 function<_R(_A0, _A1, _A2)>&
1508>::type
1509function<_R(_A0, _A1, _A2)>::operator=(_F __f)
1510{
1511 function(_STD::move(__f)).swap(*this);
1512 return *this;
1513}
1514
1515template<class _R, class _A0, class _A1, class _A2>
1516function<_R(_A0, _A1, _A2)>::~function()
1517{
1518 if (__f_ == (__base*)&__buf_)
1519 __f_->destroy();
1520 else if (__f_)
1521 __f_->destroy_deallocate();
1522}
1523
1524template<class _R, class _A0, class _A1, class _A2>
1525void
1526function<_R(_A0, _A1, _A2)>::swap(function& __f)
1527{
1528 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1529 {
1530 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1531 __base* __t = (__base*)&__tempbuf;
1532 __f_->__clone(__t);
1533 __f_->destroy();
1534 __f_ = 0;
1535 __f.__f_->__clone((__base*)&__buf_);
1536 __f.__f_->destroy();
1537 __f.__f_ = 0;
1538 __f_ = (__base*)&__buf_;
1539 __t->__clone((__base*)&__f.__buf_);
1540 __t->destroy();
1541 __f.__f_ = (__base*)&__f.__buf_;
1542 }
1543 else if (__f_ == (__base*)&__buf_)
1544 {
1545 __f_->__clone((__base*)&__f.__buf_);
1546 __f_->destroy();
1547 __f_ = __f.__f_;
1548 __f.__f_ = (__base*)&__f.__buf_;
1549 }
1550 else if (__f.__f_ == (__base*)&__f.__buf_)
1551 {
1552 __f.__f_->__clone((__base*)&__buf_);
1553 __f.__f_->destroy();
1554 __f.__f_ = __f_;
1555 __f_ = (__base*)&__buf_;
1556 }
1557 else
1558 _STD::swap(__f_, __f.__f_);
1559}
1560
1561template<class _R, class _A0, class _A1, class _A2>
1562_R
1563function<_R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1564{
Howard Hinnantd4444702010-08-11 17:04:31 +00001565#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566 if (__f_ == 0)
1567 throw bad_function_call();
Howard Hinnantd4444702010-08-11 17:04:31 +00001568#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569 return (*__f_)(__a0, __a1, __a2);
1570}
1571
Howard Hinnantd4444702010-08-11 17:04:31 +00001572#ifndef _LIBCPP_NO_RTTI
1573
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574template<class _R, class _A0, class _A1, class _A2>
1575const std::type_info&
1576function<_R(_A0, _A1, _A2)>::target_type() const
1577{
1578 if (__f_ == 0)
1579 return typeid(void);
1580 return __f_->target_type();
1581}
1582
1583template<class _R, class _A0, class _A1, class _A2>
1584template <typename _T>
1585_T*
1586function<_R(_A0, _A1, _A2)>::target()
1587{
1588 if (__f_ == 0)
1589 return (_T*)0;
1590 return (_T*)__f_->target(typeid(_T));
1591}
1592
1593template<class _R, class _A0, class _A1, class _A2>
1594template <typename _T>
1595const _T*
1596function<_R(_A0, _A1, _A2)>::target() const
1597{
1598 if (__f_ == 0)
1599 return (const _T*)0;
1600 return (const _T*)__f_->target(typeid(_T));
1601}
1602
Howard Hinnantd4444702010-08-11 17:04:31 +00001603#endif
1604
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605template <class _F>
1606inline _LIBCPP_INLINE_VISIBILITY
1607bool
1608operator==(const function<_F>& __f, nullptr_t) {return !__f;}
1609
1610template <class _F>
1611inline _LIBCPP_INLINE_VISIBILITY
1612bool
1613operator==(nullptr_t, const function<_F>& __f) {return !__f;}
1614
1615template <class _F>
1616inline _LIBCPP_INLINE_VISIBILITY
1617bool
1618operator!=(const function<_F>& __f, nullptr_t) {return (bool)__f;}
1619
1620template <class _F>
1621inline _LIBCPP_INLINE_VISIBILITY
1622bool
1623operator!=(nullptr_t, const function<_F>& __f) {return (bool)__f;}
1624
1625template <class _F>
1626inline _LIBCPP_INLINE_VISIBILITY
1627void
1628swap(function<_F>& __x, function<_F>& __y)
1629{return __x.swap(__y);}
1630
1631template<class _Tp> struct __is_bind_expression : public false_type {};
1632template<class _Tp> struct is_bind_expression
1633 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1634
1635template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1636template<class _Tp> struct is_placeholder
1637 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1638
1639namespace placeholders
1640{
1641
1642template <int _N> struct __ph {};
1643
1644extern __ph<1> _1;
1645extern __ph<2> _2;
1646extern __ph<3> _3;
1647extern __ph<4> _4;
1648extern __ph<5> _5;
1649extern __ph<6> _6;
1650extern __ph<7> _7;
1651extern __ph<8> _8;
1652extern __ph<9> _9;
1653extern __ph<10> _10;
1654
1655} // placeholders
1656
1657template<int _N>
1658struct __is_placeholder<placeholders::__ph<_N> >
1659 : public integral_constant<int, _N> {};
1660
1661template <class _Tp, class _Uj>
1662inline _LIBCPP_INLINE_VISIBILITY
1663_Tp&
1664__mu(reference_wrapper<_Tp> __t, _Uj&)
1665{
1666 return __t.get();
1667}
1668/*
1669template <bool _IsBindExpr, class _Ti, class ..._Uj>
1670struct __mu_return1 {};
1671
1672template <class _Ti, class ..._Uj>
1673struct __mu_return1<true, _Ti, _Uj...>
1674{
1675 typedef typename result_of<_Ti(_Uj...)>::type type;
1676};
1677
1678
1679template <class _Ti, class ..._Uj, size_t ..._Indx>
1680inline _LIBCPP_INLINE_VISIBILITY
1681typename __mu_return1<true, _Ti, _Uj...>::type
1682__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1683{
1684 __ti(_STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
1685}
1686
1687template <class _Ti, class ..._Uj>
1688inline _LIBCPP_INLINE_VISIBILITY
1689typename enable_if
1690<
1691 is_bind_expression<_Ti>::value,
1692 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1693>::type
1694__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1695{
1696 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1697 return __mu_expand(__ti, __uj, __indices());
1698}
1699
1700template <bool IsPh, class _Ti, class _Uj>
1701struct __mu_return2 {};
1702
1703template <class _Ti, class _Uj>
1704struct __mu_return2<true, _Ti, _Uj>
1705{
1706 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1707};
1708
1709template <class _Ti, class _Uj>
1710inline _LIBCPP_INLINE_VISIBILITY
1711typename enable_if
1712<
1713 0 < is_placeholder<_Ti>::value,
1714 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1715>::type
1716__mu(_Ti&, _Uj& __uj)
1717{
1718 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1719 // compiler bug workaround
1720 typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1721 return __t;
1722// return _STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
1723}
1724
1725template <class _Ti, class _Uj>
1726inline _LIBCPP_INLINE_VISIBILITY
1727typename enable_if
1728<
1729 !is_bind_expression<_Ti>::value &&
1730 is_placeholder<_Ti>::value == 0 &&
1731 !__is_reference_wrapper<_Ti>::value,
1732 _Ti&
1733>::type
1734__mu(_Ti& __ti, _Uj& __uj)
1735{
1736 return __ti;
1737}
1738
1739template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1740struct ____mu_return;
1741
1742template <class _Ti, class ..._Uj>
1743struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1744{
1745 typedef typename result_of<_Ti(_Uj...)>::type type;
1746};
1747
1748template <class _Ti, class _TupleUj>
1749struct ____mu_return<_Ti, false, true, _TupleUj>
1750{
1751 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1752 _TupleUj>::type&& type;
1753};
1754
1755template <class _Ti, class _TupleUj>
1756struct ____mu_return<_Ti, false, false, _TupleUj>
1757{
1758 typedef _Ti& type;
1759};
1760
1761template <class _Ti, class _TupleUj>
1762struct __mu_return
1763 : public ____mu_return<_Ti,
1764 is_bind_expression<_Ti>::value,
1765 0 < is_placeholder<_Ti>::value,
1766 _TupleUj>
1767{
1768};
1769
1770template <class _Ti, class _TupleUj>
1771struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1772{
1773 typedef _Ti& type;
1774};
1775
1776template <class _F, class _BoundArgs, class _TupleUj>
1777struct __bind_return;
1778
1779template <class _F, class ..._BoundArgs, class _TupleUj>
1780struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj>
1781{
1782 typedef typename __ref_return
1783 <
1784 _F&,
1785 typename __mu_return
1786 <
1787 _BoundArgs,
1788 _TupleUj
1789 >::type...
1790 >::type type;
1791};
1792
1793template <class _F, class ..._BoundArgs, class _TupleUj>
1794struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj>
1795{
1796 typedef typename __ref_return
1797 <
1798 _F&,
1799 typename __mu_return
1800 <
1801 const _BoundArgs,
1802 _TupleUj
1803 >::type...
1804 >::type type;
1805};
1806
1807template <class _F, class _BoundArgs, size_t ..._Indx, class _Args>
1808inline _LIBCPP_INLINE_VISIBILITY
1809typename __bind_return<_F, _BoundArgs, _Args>::type
1810__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
1811 _Args&& __args)
1812{
1813 return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
1814}
1815
1816template<class _F, class ..._BoundArgs>
1817class __bind
1818{
1819 _F __f_;
1820 tuple<_BoundArgs...> __bound_args_;
1821
1822 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
1823public:
1824 template <class _G, class ..._BA>
1825 explicit __bind(_G&& __f, _BA&& ...__bound_args)
1826 : __f_(_STD::forward<_G>(__f)),
1827 __bound_args_(_STD::forward<_BA>(__bound_args)...) {}
1828
1829 template <class ..._Args>
1830 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1831 operator()(_Args&& ...__args)
1832 {
1833 // compiler bug workaround
1834 return __apply_functor(__f_, __bound_args_, __indices(),
1835 tuple<_Args&&...>(__args...));
1836 }
1837
1838 template <class ..._Args>
1839 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1840 operator()(_Args&& ...__args) const
1841 {
1842 return __apply_functor(__f_, __bound_args_, __indices(),
1843 tuple<_Args&&...>(__args...));
1844 }
1845};
1846
1847template<class _F, class ..._BoundArgs>
1848struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {};
1849
1850template<class _R, class _F, class ..._BoundArgs>
1851class __bind_r
1852 : public __bind<_F, _BoundArgs...>
1853{
1854 typedef __bind<_F, _BoundArgs...> base;
1855public:
1856 typedef _R result_type;
1857
1858 template <class _G, class ..._BA>
1859 explicit __bind_r(_G&& __f, _BA&& ...__bound_args)
1860 : base(_STD::forward<_G>(__f),
1861 _STD::forward<_BA>(__bound_args)...) {}
1862
1863 template <class ..._Args>
1864 result_type
1865 operator()(_Args&& ...__args)
1866 {
1867 return base::operator()(_STD::forward<_Args>(__args)...);
1868 }
1869
1870 template <class ..._Args>
1871 result_type
1872 operator()(_Args&& ...__args) const
1873 {
1874 return base::operator()(_STD::forward<_Args>(__args)...);
1875 }
1876};
1877
1878template<class _R, class _F, class ..._BoundArgs>
1879struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {};
1880
1881template<class _F, class ..._BoundArgs>
1882inline _LIBCPP_INLINE_VISIBILITY
1883__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1884bind(_F&& __f, _BoundArgs&&... __bound_args)
1885{
1886 typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1887 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1888}
1889
1890template<class _R, class _F, class ..._BoundArgs>
1891inline _LIBCPP_INLINE_VISIBILITY
1892__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1893bind(_F&& __f, _BoundArgs&&... __bound_args)
1894{
1895 typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1896 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1897}
1898*/
1899
1900#endif