blob: b90676299b7d0fc1292c0a0807ed6d0790d72b1c [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 Hinnant324bb032010-08-22 00:02:43 +0000249#endif // _LIBCPP_NO_RTTI
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 Hinnant324bb032010-08-22 00:02:43 +0000268#endif // _LIBCPP_NO_RTTI
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 Hinnant324bb032010-08-22 00:02:43 +0000287#endif // _LIBCPP_NO_RTTI
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 Hinnant324bb032010-08-22 00:02:43 +0000306#endif // _LIBCPP_NO_RTTI
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 Hinnant324bb032010-08-22 00:02:43 +0000327#endif // _LIBCPP_NO_RTTI
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{
Howard Hinnant72552802010-08-20 19:36:46 +0000370 return __invoke(__f_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371}
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 Hinnant324bb032010-08-22 00:02:43 +0000391#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000392
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 Hinnant324bb032010-08-22 00:02:43 +0000409#endif // _LIBCPP_NO_RTTI
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 Hinnant324bb032010-08-22 00:02:43 +0000473#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000474
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 Hinnant324bb032010-08-22 00:02:43 +0000491#endif // _LIBCPP_NO_RTTI
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 Hinnant324bb032010-08-22 00:02:43 +0000555#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000556
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 Hinnant324bb032010-08-22 00:02:43 +0000573#endif // _LIBCPP_NO_RTTI
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 Hinnant324bb032010-08-22 00:02:43 +0000637#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000638
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
Howard Hinnant72552802010-08-20 19:36:46 +0000663 template<class _Alloc>
664 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
665 template<class _Alloc>
666 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
667 template<class _Alloc>
668 function(allocator_arg_t, const _Alloc&, const function&);
669 template<class _F, class _Alloc>
670 function(allocator_arg_t, const _Alloc& __a, _F __f,
671 typename enable_if<!is_integral<_F>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672
673 function& operator=(const function&);
674 function& operator=(nullptr_t);
675 template<class _F>
676 typename enable_if
677 <
678 !is_integral<_F>::value,
679 function&
680 >::type
681 operator=(_F);
682
683 ~function();
684
685 // 20.7.16.2.2, function modifiers:
686 void swap(function&);
Howard Hinnant72552802010-08-20 19:36:46 +0000687 template<class _F, class _Alloc>
688 void assign(_F __f, const _Alloc& __a)
689 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690
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 Hinnant324bb032010-08-22 00:02:43 +0000709#endif // _LIBCPP_NO_RTTI
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>
Howard Hinnant72552802010-08-20 19:36:46 +0000727template<class _Alloc>
728function<_R()>::function(allocator_arg_t, const _Alloc&, const function& __f)
729{
730 if (__f.__f_ == 0)
731 __f_ = 0;
732 else if (__f.__f_ == (const __base*)&__f.__buf_)
733 {
734 __f_ = (__base*)&__buf_;
735 __f.__f_->__clone(__f_);
736 }
737 else
738 __f_ = __f.__f_->__clone();
739}
740
741template<class _R>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742template <class _F>
743function<_R()>::function(_F __f,
744 typename enable_if<!is_integral<_F>::value>::type*)
745 : __f_(0)
746{
747 if (__not_null(__f))
748 {
749 typedef __function::__func<_F, allocator<_F>, _R()> _FF;
750 if (sizeof(_FF) <= sizeof(__buf_))
751 {
752 __f_ = (__base*)&__buf_;
753 ::new (__f_) _FF(__f);
754 }
755 else
756 {
757 typedef allocator<_FF> _A;
758 _A __a;
759 typedef __allocator_destructor<_A> _D;
760 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
761 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
762 __f_ = __hold.release();
763 }
764 }
765}
766
767template<class _R>
Howard Hinnant72552802010-08-20 19:36:46 +0000768template <class _F, class _Alloc>
769function<_R()>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
770 typename enable_if<!is_integral<_F>::value>::type*)
771 : __f_(0)
772{
773 typedef allocator_traits<_Alloc> __alloc_traits;
774 if (__not_null(__f))
775 {
776 typedef __function::__func<_F, _Alloc, _R()> _FF;
777 if (sizeof(_FF) <= sizeof(__buf_))
778 {
779 __f_ = (__base*)&__buf_;
780 ::new (__f_) _FF(__f);
781 }
782 else
783 {
784 typedef typename __alloc_traits::template
785#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
786 rebind_alloc<_FF>
787#else
788 rebind_alloc<_FF>::other
789#endif
790 _A;
791 _A __a(__a0);
792 typedef __allocator_destructor<_A> _D;
793 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
794 ::new (__hold.get()) _FF(__f, _Alloc(__a));
795 __f_ = __hold.release();
796 }
797 }
798}
799
800template<class _R>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000801function<_R()>&
802function<_R()>::operator=(const function& __f)
803{
804 function(__f).swap(*this);
805 return *this;
806}
807
808template<class _R>
809function<_R()>&
810function<_R()>::operator=(nullptr_t)
811{
812 if (__f_ == (__base*)&__buf_)
813 __f_->destroy();
814 else if (__f_)
815 __f_->destroy_deallocate();
816 __f_ = 0;
817}
818
819template<class _R>
820template <class _F>
821typename enable_if
822<
823 !is_integral<_F>::value,
824 function<_R()>&
825>::type
826function<_R()>::operator=(_F __f)
827{
828 function(_STD::move(__f)).swap(*this);
829 return *this;
830}
831
832template<class _R>
833function<_R()>::~function()
834{
835 if (__f_ == (__base*)&__buf_)
836 __f_->destroy();
837 else if (__f_)
838 __f_->destroy_deallocate();
839}
840
841template<class _R>
842void
843function<_R()>::swap(function& __f)
844{
845 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
846 {
847 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
848 __base* __t = (__base*)&__tempbuf;
849 __f_->__clone(__t);
850 __f_->destroy();
851 __f_ = 0;
852 __f.__f_->__clone((__base*)&__buf_);
853 __f.__f_->destroy();
854 __f.__f_ = 0;
855 __f_ = (__base*)&__buf_;
856 __t->__clone((__base*)&__f.__buf_);
857 __t->destroy();
858 __f.__f_ = (__base*)&__f.__buf_;
859 }
860 else if (__f_ == (__base*)&__buf_)
861 {
862 __f_->__clone((__base*)&__f.__buf_);
863 __f_->destroy();
864 __f_ = __f.__f_;
865 __f.__f_ = (__base*)&__f.__buf_;
866 }
867 else if (__f.__f_ == (__base*)&__f.__buf_)
868 {
869 __f.__f_->__clone((__base*)&__buf_);
870 __f.__f_->destroy();
871 __f.__f_ = __f_;
872 __f_ = (__base*)&__buf_;
873 }
874 else
875 _STD::swap(__f_, __f.__f_);
876}
877
878template<class _R>
879_R
880function<_R()>::operator()() const
881{
Howard Hinnantd4444702010-08-11 17:04:31 +0000882#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000883 if (__f_ == 0)
884 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +0000885#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000886 return (*__f_)();
887}
888
Howard Hinnantd4444702010-08-11 17:04:31 +0000889#ifndef _LIBCPP_NO_RTTI
890
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891template<class _R>
892const std::type_info&
893function<_R()>::target_type() const
894{
895 if (__f_ == 0)
896 return typeid(void);
897 return __f_->target_type();
898}
899
900template<class _R>
901template <typename _T>
902_T*
903function<_R()>::target()
904{
905 if (__f_ == 0)
906 return (_T*)0;
907 return (_T*)__f_->target(typeid(_T));
908}
909
910template<class _R>
911template <typename _T>
912const _T*
913function<_R()>::target() const
914{
915 if (__f_ == 0)
916 return (const _T*)0;
917 return (const _T*)__f_->target(typeid(_T));
918}
919
Howard Hinnant324bb032010-08-22 00:02:43 +0000920#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000921
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922template<class _R, class _A0>
923class function<_R(_A0)>
924 : public unary_function<_A0, _R>
925{
926 typedef __function::__base<_R(_A0)> __base;
927 aligned_storage<3*sizeof(void*)>::type __buf_;
928 __base* __f_;
929
930 template <class _F>
931 static bool __not_null(const _F&) {return true;}
932 template <class _R2, class _B0>
933 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
934 template <class _R2, class _C>
935 static bool __not_null(_R2 (_C::*__p)()) {return __p;}
936 template <class _R2, class _C>
937 static bool __not_null(_R2 (_C::*__p)() const) {return __p;}
938 template <class _R2, class _C>
939 static bool __not_null(_R2 (_C::*__p)() volatile) {return __p;}
940 template <class _R2, class _C>
941 static bool __not_null(_R2 (_C::*__p)() const volatile) {return __p;}
942 template <class _R2, class _B0>
943 static bool __not_null(const function<_R(_B0)>& __p) {return __p;}
944public:
945 typedef _R result_type;
946
947 // 20.7.16.2.1, construct/copy/destroy:
948 explicit function() : __f_(0) {}
949 function(nullptr_t) : __f_(0) {}
950 function(const function&);
951 template<class _F>
952 function(_F,
953 typename enable_if<!is_integral<_F>::value>::type* = 0);
954
Howard Hinnant72552802010-08-20 19:36:46 +0000955 template<class _Alloc>
956 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
957 template<class _Alloc>
958 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
959 template<class _Alloc>
960 function(allocator_arg_t, const _Alloc&, const function&);
961 template<class _F, class _Alloc>
962 function(allocator_arg_t, const _Alloc& __a, _F __f,
963 typename enable_if<!is_integral<_F>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964
965 function& operator=(const function&);
966 function& operator=(nullptr_t);
967 template<class _F>
968 typename enable_if
969 <
970 !is_integral<_F>::value,
971 function&
972 >::type
973 operator=(_F);
974
975 ~function();
976
977 // 20.7.16.2.2, function modifiers:
978 void swap(function&);
Howard Hinnant72552802010-08-20 19:36:46 +0000979 template<class _F, class _Alloc>
980 void assign(_F __f, const _Alloc& __a)
981 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982
983 // 20.7.16.2.3, function capacity:
984 operator bool() const {return __f_;}
985
986private:
987 // deleted overloads close possible hole in the type system
988 template<class _R2, class _B0>
989 bool operator==(const function<_R2(_B0)>&);// = delete;
990 template<class _R2, class _B0>
991 bool operator!=(const function<_R2(_B0)>&);// = delete;
992public:
993 // 20.7.16.2.4, function invocation:
994 _R operator()(_A0) const;
995
Howard Hinnantd4444702010-08-11 17:04:31 +0000996#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997 // 20.7.16.2.5, function target access:
998 const std::type_info& target_type() const;
999 template <typename _T> _T* target();
1000 template <typename _T> const _T* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001001#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002};
1003
1004template<class _R, class _A0>
1005function<_R(_A0)>::function(const function& __f)
1006{
1007 if (__f.__f_ == 0)
1008 __f_ = 0;
1009 else if (__f.__f_ == (const __base*)&__f.__buf_)
1010 {
1011 __f_ = (__base*)&__buf_;
1012 __f.__f_->__clone(__f_);
1013 }
1014 else
1015 __f_ = __f.__f_->__clone();
1016}
1017
1018template<class _R, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001019template<class _Alloc>
1020function<_R(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1021{
1022 if (__f.__f_ == 0)
1023 __f_ = 0;
1024 else if (__f.__f_ == (const __base*)&__f.__buf_)
1025 {
1026 __f_ = (__base*)&__buf_;
1027 __f.__f_->__clone(__f_);
1028 }
1029 else
1030 __f_ = __f.__f_->__clone();
1031}
1032
1033template<class _R, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001034template <class _F>
1035function<_R(_A0)>::function(_F __f,
1036 typename enable_if<!is_integral<_F>::value>::type*)
1037 : __f_(0)
1038{
1039 if (__not_null(__f))
1040 {
1041 typedef __function::__func<_F, allocator<_F>, _R(_A0)> _FF;
1042 if (sizeof(_FF) <= sizeof(__buf_))
1043 {
1044 __f_ = (__base*)&__buf_;
1045 ::new (__f_) _FF(__f);
1046 }
1047 else
1048 {
1049 typedef allocator<_FF> _A;
1050 _A __a;
1051 typedef __allocator_destructor<_A> _D;
1052 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1053 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1054 __f_ = __hold.release();
1055 }
1056 }
1057}
1058
1059template<class _R, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001060template <class _F, class _Alloc>
1061function<_R(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1062 typename enable_if<!is_integral<_F>::value>::type*)
1063 : __f_(0)
1064{
1065 typedef allocator_traits<_Alloc> __alloc_traits;
1066 if (__not_null(__f))
1067 {
1068 typedef __function::__func<_F, _Alloc, _R(_A0)> _FF;
1069 if (sizeof(_FF) <= sizeof(__buf_))
1070 {
1071 __f_ = (__base*)&__buf_;
1072 ::new (__f_) _FF(__f);
1073 }
1074 else
1075 {
1076 typedef typename __alloc_traits::template
1077#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1078 rebind_alloc<_FF>
1079#else
1080 rebind_alloc<_FF>::other
1081#endif
1082 _A;
1083 _A __a(__a0);
1084 typedef __allocator_destructor<_A> _D;
1085 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1086 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1087 __f_ = __hold.release();
1088 }
1089 }
1090}
1091
1092template<class _R, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093function<_R(_A0)>&
1094function<_R(_A0)>::operator=(const function& __f)
1095{
1096 function(__f).swap(*this);
1097 return *this;
1098}
1099
1100template<class _R, class _A0>
1101function<_R(_A0)>&
1102function<_R(_A0)>::operator=(nullptr_t)
1103{
1104 if (__f_ == (__base*)&__buf_)
1105 __f_->destroy();
1106 else if (__f_)
1107 __f_->destroy_deallocate();
1108 __f_ = 0;
1109}
1110
1111template<class _R, class _A0>
1112template <class _F>
1113typename enable_if
1114<
1115 !is_integral<_F>::value,
1116 function<_R(_A0)>&
1117>::type
1118function<_R(_A0)>::operator=(_F __f)
1119{
1120 function(_STD::move(__f)).swap(*this);
1121 return *this;
1122}
1123
1124template<class _R, class _A0>
1125function<_R(_A0)>::~function()
1126{
1127 if (__f_ == (__base*)&__buf_)
1128 __f_->destroy();
1129 else if (__f_)
1130 __f_->destroy_deallocate();
1131}
1132
1133template<class _R, class _A0>
1134void
1135function<_R(_A0)>::swap(function& __f)
1136{
1137 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1138 {
1139 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1140 __base* __t = (__base*)&__tempbuf;
1141 __f_->__clone(__t);
1142 __f_->destroy();
1143 __f_ = 0;
1144 __f.__f_->__clone((__base*)&__buf_);
1145 __f.__f_->destroy();
1146 __f.__f_ = 0;
1147 __f_ = (__base*)&__buf_;
1148 __t->__clone((__base*)&__f.__buf_);
1149 __t->destroy();
1150 __f.__f_ = (__base*)&__f.__buf_;
1151 }
1152 else if (__f_ == (__base*)&__buf_)
1153 {
1154 __f_->__clone((__base*)&__f.__buf_);
1155 __f_->destroy();
1156 __f_ = __f.__f_;
1157 __f.__f_ = (__base*)&__f.__buf_;
1158 }
1159 else if (__f.__f_ == (__base*)&__f.__buf_)
1160 {
1161 __f.__f_->__clone((__base*)&__buf_);
1162 __f.__f_->destroy();
1163 __f.__f_ = __f_;
1164 __f_ = (__base*)&__buf_;
1165 }
1166 else
1167 _STD::swap(__f_, __f.__f_);
1168}
1169
1170template<class _R, class _A0>
1171_R
1172function<_R(_A0)>::operator()(_A0 __a0) const
1173{
Howard Hinnantd4444702010-08-11 17:04:31 +00001174#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175 if (__f_ == 0)
1176 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001177#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178 return (*__f_)(__a0);
1179}
1180
Howard Hinnantd4444702010-08-11 17:04:31 +00001181#ifndef _LIBCPP_NO_RTTI
1182
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183template<class _R, class _A0>
1184const std::type_info&
1185function<_R(_A0)>::target_type() const
1186{
1187 if (__f_ == 0)
1188 return typeid(void);
1189 return __f_->target_type();
1190}
1191
1192template<class _R, class _A0>
1193template <typename _T>
1194_T*
1195function<_R(_A0)>::target()
1196{
1197 if (__f_ == 0)
1198 return (_T*)0;
1199 return (_T*)__f_->target(typeid(_T));
1200}
1201
1202template<class _R, class _A0>
1203template <typename _T>
1204const _T*
1205function<_R(_A0)>::target() const
1206{
1207 if (__f_ == 0)
1208 return (const _T*)0;
1209 return (const _T*)__f_->target(typeid(_T));
1210}
1211
Howard Hinnant324bb032010-08-22 00:02:43 +00001212#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001213
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214template<class _R, class _A0, class _A1>
1215class function<_R(_A0, _A1)>
1216 : public binary_function<_A0, _A1, _R>
1217{
1218 typedef __function::__base<_R(_A0, _A1)> __base;
1219 aligned_storage<3*sizeof(void*)>::type __buf_;
1220 __base* __f_;
1221
1222 template <class _F>
1223 static bool __not_null(const _F&) {return true;}
1224 template <class _R2, class _B0, class _B1>
1225 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
1226 template <class _R2, class _C, class _B1>
1227 static bool __not_null(_R2 (_C::*__p)(_B1)) {return __p;}
1228 template <class _R2, class _C, class _B1>
1229 static bool __not_null(_R2 (_C::*__p)(_B1) const) {return __p;}
1230 template <class _R2, class _C, class _B1>
1231 static bool __not_null(_R2 (_C::*__p)(_B1) volatile) {return __p;}
1232 template <class _R2, class _C, class _B1>
1233 static bool __not_null(_R2 (_C::*__p)(_B1) const volatile) {return __p;}
1234 template <class _R2, class _B0, class _B1>
1235 static bool __not_null(const function<_R(_B0, _B1)>& __p) {return __p;}
1236public:
1237 typedef _R result_type;
1238
1239 // 20.7.16.2.1, construct/copy/destroy:
1240 explicit function() : __f_(0) {}
1241 function(nullptr_t) : __f_(0) {}
1242 function(const function&);
1243 template<class _F>
1244 function(_F,
1245 typename enable_if<!is_integral<_F>::value>::type* = 0);
1246
Howard Hinnant72552802010-08-20 19:36:46 +00001247 template<class _Alloc>
1248 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1249 template<class _Alloc>
1250 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1251 template<class _Alloc>
1252 function(allocator_arg_t, const _Alloc&, const function&);
1253 template<class _F, class _Alloc>
1254 function(allocator_arg_t, const _Alloc& __a, _F __f,
1255 typename enable_if<!is_integral<_F>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001256
1257 function& operator=(const function&);
1258 function& operator=(nullptr_t);
1259 template<class _F>
1260 typename enable_if
1261 <
1262 !is_integral<_F>::value,
1263 function&
1264 >::type
1265 operator=(_F);
1266
1267 ~function();
1268
1269 // 20.7.16.2.2, function modifiers:
1270 void swap(function&);
Howard Hinnant72552802010-08-20 19:36:46 +00001271 template<class _F, class _Alloc>
1272 void assign(_F __f, const _Alloc& __a)
1273 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274
1275 // 20.7.16.2.3, function capacity:
1276 operator bool() const {return __f_;}
1277
1278private:
1279 // deleted overloads close possible hole in the type system
1280 template<class _R2, class _B0, class _B1>
1281 bool operator==(const function<_R2(_B0, _B1)>&);// = delete;
1282 template<class _R2, class _B0, class _B1>
1283 bool operator!=(const function<_R2(_B0, _B1)>&);// = delete;
1284public:
1285 // 20.7.16.2.4, function invocation:
1286 _R operator()(_A0, _A1) const;
1287
Howard Hinnantd4444702010-08-11 17:04:31 +00001288#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001289 // 20.7.16.2.5, function target access:
1290 const std::type_info& target_type() const;
1291 template <typename _T> _T* target();
1292 template <typename _T> const _T* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001293#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294};
1295
1296template<class _R, class _A0, class _A1>
1297function<_R(_A0, _A1)>::function(const function& __f)
1298{
1299 if (__f.__f_ == 0)
1300 __f_ = 0;
1301 else if (__f.__f_ == (const __base*)&__f.__buf_)
1302 {
1303 __f_ = (__base*)&__buf_;
1304 __f.__f_->__clone(__f_);
1305 }
1306 else
1307 __f_ = __f.__f_->__clone();
1308}
1309
1310template<class _R, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001311template<class _Alloc>
1312function<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1313{
1314 if (__f.__f_ == 0)
1315 __f_ = 0;
1316 else if (__f.__f_ == (const __base*)&__f.__buf_)
1317 {
1318 __f_ = (__base*)&__buf_;
1319 __f.__f_->__clone(__f_);
1320 }
1321 else
1322 __f_ = __f.__f_->__clone();
1323}
1324
1325template<class _R, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326template <class _F>
1327function<_R(_A0, _A1)>::function(_F __f,
Howard Hinnant72552802010-08-20 19:36:46 +00001328 typename enable_if<!is_integral<_F>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 : __f_(0)
1330{
1331 if (__not_null(__f))
1332 {
1333 typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1)> _FF;
1334 if (sizeof(_FF) <= sizeof(__buf_))
1335 {
1336 __f_ = (__base*)&__buf_;
1337 ::new (__f_) _FF(__f);
1338 }
1339 else
1340 {
1341 typedef allocator<_FF> _A;
1342 _A __a;
1343 typedef __allocator_destructor<_A> _D;
1344 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1345 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1346 __f_ = __hold.release();
1347 }
1348 }
1349}
1350
1351template<class _R, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001352template <class _F, class _Alloc>
1353function<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1354 typename enable_if<!is_integral<_F>::value>::type*)
1355 : __f_(0)
1356{
1357 typedef allocator_traits<_Alloc> __alloc_traits;
1358 if (__not_null(__f))
1359 {
1360 typedef __function::__func<_F, _Alloc, _R(_A0, _A1)> _FF;
1361 if (sizeof(_FF) <= sizeof(__buf_))
1362 {
1363 __f_ = (__base*)&__buf_;
1364 ::new (__f_) _FF(__f);
1365 }
1366 else
1367 {
1368 typedef typename __alloc_traits::template
1369#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1370 rebind_alloc<_FF>
1371#else
1372 rebind_alloc<_FF>::other
1373#endif
1374 _A;
1375 _A __a(__a0);
1376 typedef __allocator_destructor<_A> _D;
1377 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1378 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1379 __f_ = __hold.release();
1380 }
1381 }
1382}
1383
1384template<class _R, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385function<_R(_A0, _A1)>&
1386function<_R(_A0, _A1)>::operator=(const function& __f)
1387{
1388 function(__f).swap(*this);
1389 return *this;
1390}
1391
1392template<class _R, class _A0, class _A1>
1393function<_R(_A0, _A1)>&
1394function<_R(_A0, _A1)>::operator=(nullptr_t)
1395{
1396 if (__f_ == (__base*)&__buf_)
1397 __f_->destroy();
1398 else if (__f_)
1399 __f_->destroy_deallocate();
1400 __f_ = 0;
1401}
1402
1403template<class _R, class _A0, class _A1>
1404template <class _F>
1405typename enable_if
1406<
1407 !is_integral<_F>::value,
1408 function<_R(_A0, _A1)>&
1409>::type
1410function<_R(_A0, _A1)>::operator=(_F __f)
1411{
1412 function(_STD::move(__f)).swap(*this);
1413 return *this;
1414}
1415
1416template<class _R, class _A0, class _A1>
1417function<_R(_A0, _A1)>::~function()
1418{
1419 if (__f_ == (__base*)&__buf_)
1420 __f_->destroy();
1421 else if (__f_)
1422 __f_->destroy_deallocate();
1423}
1424
1425template<class _R, class _A0, class _A1>
1426void
1427function<_R(_A0, _A1)>::swap(function& __f)
1428{
1429 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1430 {
1431 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1432 __base* __t = (__base*)&__tempbuf;
1433 __f_->__clone(__t);
1434 __f_->destroy();
1435 __f_ = 0;
1436 __f.__f_->__clone((__base*)&__buf_);
1437 __f.__f_->destroy();
1438 __f.__f_ = 0;
1439 __f_ = (__base*)&__buf_;
1440 __t->__clone((__base*)&__f.__buf_);
1441 __t->destroy();
1442 __f.__f_ = (__base*)&__f.__buf_;
1443 }
1444 else if (__f_ == (__base*)&__buf_)
1445 {
1446 __f_->__clone((__base*)&__f.__buf_);
1447 __f_->destroy();
1448 __f_ = __f.__f_;
1449 __f.__f_ = (__base*)&__f.__buf_;
1450 }
1451 else if (__f.__f_ == (__base*)&__f.__buf_)
1452 {
1453 __f.__f_->__clone((__base*)&__buf_);
1454 __f.__f_->destroy();
1455 __f.__f_ = __f_;
1456 __f_ = (__base*)&__buf_;
1457 }
1458 else
1459 _STD::swap(__f_, __f.__f_);
1460}
1461
1462template<class _R, class _A0, class _A1>
1463_R
1464function<_R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1465{
Howard Hinnantd4444702010-08-11 17:04:31 +00001466#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467 if (__f_ == 0)
1468 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001469#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001470 return (*__f_)(__a0, __a1);
1471}
1472
Howard Hinnantd4444702010-08-11 17:04:31 +00001473#ifndef _LIBCPP_NO_RTTI
1474
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475template<class _R, class _A0, class _A1>
1476const std::type_info&
1477function<_R(_A0, _A1)>::target_type() const
1478{
1479 if (__f_ == 0)
1480 return typeid(void);
1481 return __f_->target_type();
1482}
1483
1484template<class _R, class _A0, class _A1>
1485template <typename _T>
1486_T*
1487function<_R(_A0, _A1)>::target()
1488{
1489 if (__f_ == 0)
1490 return (_T*)0;
1491 return (_T*)__f_->target(typeid(_T));
1492}
1493
1494template<class _R, class _A0, class _A1>
1495template <typename _T>
1496const _T*
1497function<_R(_A0, _A1)>::target() const
1498{
1499 if (__f_ == 0)
1500 return (const _T*)0;
1501 return (const _T*)__f_->target(typeid(_T));
1502}
1503
Howard Hinnant324bb032010-08-22 00:02:43 +00001504#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001505
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506template<class _R, class _A0, class _A1, class _A2>
1507class function<_R(_A0, _A1, _A2)>
1508{
1509 typedef __function::__base<_R(_A0, _A1, _A2)> __base;
1510 aligned_storage<3*sizeof(void*)>::type __buf_;
1511 __base* __f_;
1512
1513 template <class _F>
1514 static bool __not_null(const _F&) {return true;}
1515 template <class _R2, class _B0, class _B1, class _B2>
1516 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
1517 template <class _R2, class _C, class _B1, class _B2>
1518 static bool __not_null(_R2 (_C::*__p)(_B1, _B2)) {return __p;}
1519 template <class _R2, class _C, class _B1, class _B2>
1520 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const) {return __p;}
1521 template <class _R2, class _C, class _B1, class _B2>
1522 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) volatile) {return __p;}
1523 template <class _R2, class _C, class _B1, class _B2>
1524 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const volatile) {return __p;}
1525 template <class _R2, class _B0, class _B1, class _B2>
1526 static bool __not_null(const function<_R(_B0, _B1, _B2)>& __p) {return __p;}
1527public:
1528 typedef _R result_type;
1529
1530 // 20.7.16.2.1, construct/copy/destroy:
1531 explicit function() : __f_(0) {}
1532 function(nullptr_t) : __f_(0) {}
1533 function(const function&);
1534 template<class _F>
1535 function(_F,
1536 typename enable_if<!is_integral<_F>::value>::type* = 0);
1537
Howard Hinnant72552802010-08-20 19:36:46 +00001538 template<class _Alloc>
1539 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1540 template<class _Alloc>
1541 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1542 template<class _Alloc>
1543 function(allocator_arg_t, const _Alloc&, const function&);
1544 template<class _F, class _Alloc>
1545 function(allocator_arg_t, const _Alloc& __a, _F __f,
1546 typename enable_if<!is_integral<_F>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547
1548 function& operator=(const function&);
1549 function& operator=(nullptr_t);
1550 template<class _F>
1551 typename enable_if
1552 <
1553 !is_integral<_F>::value,
1554 function&
1555 >::type
1556 operator=(_F);
1557
1558 ~function();
1559
1560 // 20.7.16.2.2, function modifiers:
1561 void swap(function&);
Howard Hinnant72552802010-08-20 19:36:46 +00001562 template<class _F, class _Alloc>
1563 void assign(_F __f, const _Alloc& __a)
1564 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565
1566 // 20.7.16.2.3, function capacity:
1567 operator bool() const {return __f_;}
1568
1569private:
1570 // deleted overloads close possible hole in the type system
1571 template<class _R2, class _B0, class _B1, class _B2>
1572 bool operator==(const function<_R2(_B0, _B1, _B2)>&);// = delete;
1573 template<class _R2, class _B0, class _B1, class _B2>
1574 bool operator!=(const function<_R2(_B0, _B1, _B2)>&);// = delete;
1575public:
1576 // 20.7.16.2.4, function invocation:
1577 _R operator()(_A0, _A1, _A2) const;
1578
Howard Hinnantd4444702010-08-11 17:04:31 +00001579#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 // 20.7.16.2.5, function target access:
1581 const std::type_info& target_type() const;
1582 template <typename _T> _T* target();
1583 template <typename _T> const _T* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001584#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585};
1586
1587template<class _R, class _A0, class _A1, class _A2>
1588function<_R(_A0, _A1, _A2)>::function(const function& __f)
1589{
1590 if (__f.__f_ == 0)
1591 __f_ = 0;
1592 else if (__f.__f_ == (const __base*)&__f.__buf_)
1593 {
1594 __f_ = (__base*)&__buf_;
1595 __f.__f_->__clone(__f_);
1596 }
1597 else
1598 __f_ = __f.__f_->__clone();
1599}
1600
1601template<class _R, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001602template<class _Alloc>
1603function<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
1604 const function& __f)
1605{
1606 if (__f.__f_ == 0)
1607 __f_ = 0;
1608 else if (__f.__f_ == (const __base*)&__f.__buf_)
1609 {
1610 __f_ = (__base*)&__buf_;
1611 __f.__f_->__clone(__f_);
1612 }
1613 else
1614 __f_ = __f.__f_->__clone();
1615}
1616
1617template<class _R, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618template <class _F>
1619function<_R(_A0, _A1, _A2)>::function(_F __f,
1620 typename enable_if<!is_integral<_F>::value>::type*)
1621 : __f_(0)
1622{
1623 if (__not_null(__f))
1624 {
1625 typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1, _A2)> _FF;
1626 if (sizeof(_FF) <= sizeof(__buf_))
1627 {
1628 __f_ = (__base*)&__buf_;
1629 ::new (__f_) _FF(__f);
1630 }
1631 else
1632 {
1633 typedef allocator<_FF> _A;
1634 _A __a;
1635 typedef __allocator_destructor<_A> _D;
1636 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1637 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1638 __f_ = __hold.release();
1639 }
1640 }
1641}
1642
1643template<class _R, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001644template <class _F, class _Alloc>
1645function<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1646 typename enable_if<!is_integral<_F>::value>::type*)
1647 : __f_(0)
1648{
1649 typedef allocator_traits<_Alloc> __alloc_traits;
1650 if (__not_null(__f))
1651 {
1652 typedef __function::__func<_F, _Alloc, _R(_A0, _A1, _A2)> _FF;
1653 if (sizeof(_FF) <= sizeof(__buf_))
1654 {
1655 __f_ = (__base*)&__buf_;
1656 ::new (__f_) _FF(__f);
1657 }
1658 else
1659 {
1660 typedef typename __alloc_traits::template
1661#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1662 rebind_alloc<_FF>
1663#else
1664 rebind_alloc<_FF>::other
1665#endif
1666 _A;
1667 _A __a(__a0);
1668 typedef __allocator_destructor<_A> _D;
1669 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1670 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1671 __f_ = __hold.release();
1672 }
1673 }
1674}
1675
1676template<class _R, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677function<_R(_A0, _A1, _A2)>&
1678function<_R(_A0, _A1, _A2)>::operator=(const function& __f)
1679{
1680 function(__f).swap(*this);
1681 return *this;
1682}
1683
1684template<class _R, class _A0, class _A1, class _A2>
1685function<_R(_A0, _A1, _A2)>&
1686function<_R(_A0, _A1, _A2)>::operator=(nullptr_t)
1687{
1688 if (__f_ == (__base*)&__buf_)
1689 __f_->destroy();
1690 else if (__f_)
1691 __f_->destroy_deallocate();
1692 __f_ = 0;
1693}
1694
1695template<class _R, class _A0, class _A1, class _A2>
1696template <class _F>
1697typename enable_if
1698<
1699 !is_integral<_F>::value,
1700 function<_R(_A0, _A1, _A2)>&
1701>::type
1702function<_R(_A0, _A1, _A2)>::operator=(_F __f)
1703{
1704 function(_STD::move(__f)).swap(*this);
1705 return *this;
1706}
1707
1708template<class _R, class _A0, class _A1, class _A2>
1709function<_R(_A0, _A1, _A2)>::~function()
1710{
1711 if (__f_ == (__base*)&__buf_)
1712 __f_->destroy();
1713 else if (__f_)
1714 __f_->destroy_deallocate();
1715}
1716
1717template<class _R, class _A0, class _A1, class _A2>
1718void
1719function<_R(_A0, _A1, _A2)>::swap(function& __f)
1720{
1721 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1722 {
1723 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1724 __base* __t = (__base*)&__tempbuf;
1725 __f_->__clone(__t);
1726 __f_->destroy();
1727 __f_ = 0;
1728 __f.__f_->__clone((__base*)&__buf_);
1729 __f.__f_->destroy();
1730 __f.__f_ = 0;
1731 __f_ = (__base*)&__buf_;
1732 __t->__clone((__base*)&__f.__buf_);
1733 __t->destroy();
1734 __f.__f_ = (__base*)&__f.__buf_;
1735 }
1736 else if (__f_ == (__base*)&__buf_)
1737 {
1738 __f_->__clone((__base*)&__f.__buf_);
1739 __f_->destroy();
1740 __f_ = __f.__f_;
1741 __f.__f_ = (__base*)&__f.__buf_;
1742 }
1743 else if (__f.__f_ == (__base*)&__f.__buf_)
1744 {
1745 __f.__f_->__clone((__base*)&__buf_);
1746 __f.__f_->destroy();
1747 __f.__f_ = __f_;
1748 __f_ = (__base*)&__buf_;
1749 }
1750 else
1751 _STD::swap(__f_, __f.__f_);
1752}
1753
1754template<class _R, class _A0, class _A1, class _A2>
1755_R
1756function<_R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1757{
Howard Hinnantd4444702010-08-11 17:04:31 +00001758#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 if (__f_ == 0)
1760 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001761#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762 return (*__f_)(__a0, __a1, __a2);
1763}
1764
Howard Hinnantd4444702010-08-11 17:04:31 +00001765#ifndef _LIBCPP_NO_RTTI
1766
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001767template<class _R, class _A0, class _A1, class _A2>
1768const std::type_info&
1769function<_R(_A0, _A1, _A2)>::target_type() const
1770{
1771 if (__f_ == 0)
1772 return typeid(void);
1773 return __f_->target_type();
1774}
1775
1776template<class _R, class _A0, class _A1, class _A2>
1777template <typename _T>
1778_T*
1779function<_R(_A0, _A1, _A2)>::target()
1780{
1781 if (__f_ == 0)
1782 return (_T*)0;
1783 return (_T*)__f_->target(typeid(_T));
1784}
1785
1786template<class _R, class _A0, class _A1, class _A2>
1787template <typename _T>
1788const _T*
1789function<_R(_A0, _A1, _A2)>::target() const
1790{
1791 if (__f_ == 0)
1792 return (const _T*)0;
1793 return (const _T*)__f_->target(typeid(_T));
1794}
1795
Howard Hinnant324bb032010-08-22 00:02:43 +00001796#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001797
Howard Hinnant324bb032010-08-22 00:02:43 +00001798template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799inline _LIBCPP_INLINE_VISIBILITY
1800bool
1801operator==(const function<_F>& __f, nullptr_t) {return !__f;}
1802
Howard Hinnant324bb032010-08-22 00:02:43 +00001803template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804inline _LIBCPP_INLINE_VISIBILITY
1805bool
1806operator==(nullptr_t, const function<_F>& __f) {return !__f;}
1807
Howard Hinnant324bb032010-08-22 00:02:43 +00001808template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809inline _LIBCPP_INLINE_VISIBILITY
1810bool
1811operator!=(const function<_F>& __f, nullptr_t) {return (bool)__f;}
1812
Howard Hinnant324bb032010-08-22 00:02:43 +00001813template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001814inline _LIBCPP_INLINE_VISIBILITY
1815bool
1816operator!=(nullptr_t, const function<_F>& __f) {return (bool)__f;}
1817
Howard Hinnant324bb032010-08-22 00:02:43 +00001818template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819inline _LIBCPP_INLINE_VISIBILITY
1820void
1821swap(function<_F>& __x, function<_F>& __y)
1822{return __x.swap(__y);}
1823
1824template<class _Tp> struct __is_bind_expression : public false_type {};
1825template<class _Tp> struct is_bind_expression
1826 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1827
1828template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1829template<class _Tp> struct is_placeholder
1830 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1831
1832namespace placeholders
1833{
1834
1835template <int _N> struct __ph {};
1836
1837extern __ph<1> _1;
1838extern __ph<2> _2;
1839extern __ph<3> _3;
1840extern __ph<4> _4;
1841extern __ph<5> _5;
1842extern __ph<6> _6;
1843extern __ph<7> _7;
1844extern __ph<8> _8;
1845extern __ph<9> _9;
1846extern __ph<10> _10;
1847
1848} // placeholders
1849
1850template<int _N>
1851struct __is_placeholder<placeholders::__ph<_N> >
1852 : public integral_constant<int, _N> {};
1853
1854template <class _Tp, class _Uj>
1855inline _LIBCPP_INLINE_VISIBILITY
1856_Tp&
1857__mu(reference_wrapper<_Tp> __t, _Uj&)
1858{
1859 return __t.get();
1860}
1861/*
1862template <bool _IsBindExpr, class _Ti, class ..._Uj>
1863struct __mu_return1 {};
1864
1865template <class _Ti, class ..._Uj>
1866struct __mu_return1<true, _Ti, _Uj...>
1867{
1868 typedef typename result_of<_Ti(_Uj...)>::type type;
1869};
1870
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871template <class _Ti, class ..._Uj, size_t ..._Indx>
1872inline _LIBCPP_INLINE_VISIBILITY
1873typename __mu_return1<true, _Ti, _Uj...>::type
1874__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1875{
1876 __ti(_STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
1877}
1878
1879template <class _Ti, class ..._Uj>
1880inline _LIBCPP_INLINE_VISIBILITY
1881typename enable_if
1882<
1883 is_bind_expression<_Ti>::value,
1884 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1885>::type
1886__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1887{
1888 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1889 return __mu_expand(__ti, __uj, __indices());
1890}
1891
1892template <bool IsPh, class _Ti, class _Uj>
1893struct __mu_return2 {};
1894
1895template <class _Ti, class _Uj>
1896struct __mu_return2<true, _Ti, _Uj>
1897{
1898 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1899};
1900
1901template <class _Ti, class _Uj>
1902inline _LIBCPP_INLINE_VISIBILITY
1903typename enable_if
1904<
1905 0 < is_placeholder<_Ti>::value,
1906 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1907>::type
1908__mu(_Ti&, _Uj& __uj)
1909{
1910 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1911 // compiler bug workaround
1912 typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1913 return __t;
1914// return _STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
1915}
1916
1917template <class _Ti, class _Uj>
1918inline _LIBCPP_INLINE_VISIBILITY
1919typename enable_if
1920<
1921 !is_bind_expression<_Ti>::value &&
1922 is_placeholder<_Ti>::value == 0 &&
1923 !__is_reference_wrapper<_Ti>::value,
1924 _Ti&
1925>::type
1926__mu(_Ti& __ti, _Uj& __uj)
1927{
1928 return __ti;
1929}
1930
1931template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1932struct ____mu_return;
1933
1934template <class _Ti, class ..._Uj>
1935struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1936{
1937 typedef typename result_of<_Ti(_Uj...)>::type type;
1938};
1939
1940template <class _Ti, class _TupleUj>
1941struct ____mu_return<_Ti, false, true, _TupleUj>
1942{
1943 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1944 _TupleUj>::type&& type;
1945};
1946
1947template <class _Ti, class _TupleUj>
1948struct ____mu_return<_Ti, false, false, _TupleUj>
1949{
1950 typedef _Ti& type;
1951};
1952
1953template <class _Ti, class _TupleUj>
1954struct __mu_return
1955 : public ____mu_return<_Ti,
1956 is_bind_expression<_Ti>::value,
1957 0 < is_placeholder<_Ti>::value,
1958 _TupleUj>
1959{
1960};
1961
1962template <class _Ti, class _TupleUj>
1963struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1964{
1965 typedef _Ti& type;
1966};
1967
1968template <class _F, class _BoundArgs, class _TupleUj>
1969struct __bind_return;
1970
1971template <class _F, class ..._BoundArgs, class _TupleUj>
1972struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj>
1973{
1974 typedef typename __ref_return
1975 <
1976 _F&,
1977 typename __mu_return
1978 <
1979 _BoundArgs,
1980 _TupleUj
1981 >::type...
1982 >::type type;
1983};
1984
1985template <class _F, class ..._BoundArgs, class _TupleUj>
1986struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj>
1987{
1988 typedef typename __ref_return
1989 <
1990 _F&,
1991 typename __mu_return
1992 <
1993 const _BoundArgs,
1994 _TupleUj
1995 >::type...
1996 >::type type;
1997};
1998
1999template <class _F, class _BoundArgs, size_t ..._Indx, class _Args>
2000inline _LIBCPP_INLINE_VISIBILITY
2001typename __bind_return<_F, _BoundArgs, _Args>::type
2002__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
2003 _Args&& __args)
2004{
2005 return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
2006}
2007
Howard Hinnant324bb032010-08-22 00:02:43 +00002008template<class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002009class __bind
2010{
2011 _F __f_;
2012 tuple<_BoundArgs...> __bound_args_;
2013
2014 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2015public:
2016 template <class _G, class ..._BA>
2017 explicit __bind(_G&& __f, _BA&& ...__bound_args)
2018 : __f_(_STD::forward<_G>(__f)),
2019 __bound_args_(_STD::forward<_BA>(__bound_args)...) {}
2020
2021 template <class ..._Args>
2022 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2023 operator()(_Args&& ...__args)
2024 {
2025 // compiler bug workaround
Howard Hinnant324bb032010-08-22 00:02:43 +00002026 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027 tuple<_Args&&...>(__args...));
2028 }
2029
2030 template <class ..._Args>
2031 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2032 operator()(_Args&& ...__args) const
2033 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002034 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 tuple<_Args&&...>(__args...));
2036 }
2037};
2038
Howard Hinnant324bb032010-08-22 00:02:43 +00002039template<class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {};
2041
Howard Hinnant324bb032010-08-22 00:02:43 +00002042template<class _R, class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043class __bind_r
2044 : public __bind<_F, _BoundArgs...>
2045{
2046 typedef __bind<_F, _BoundArgs...> base;
2047public:
2048 typedef _R result_type;
2049
2050 template <class _G, class ..._BA>
2051 explicit __bind_r(_G&& __f, _BA&& ...__bound_args)
2052 : base(_STD::forward<_G>(__f),
2053 _STD::forward<_BA>(__bound_args)...) {}
2054
2055 template <class ..._Args>
2056 result_type
2057 operator()(_Args&& ...__args)
2058 {
2059 return base::operator()(_STD::forward<_Args>(__args)...);
2060 }
2061
2062 template <class ..._Args>
2063 result_type
2064 operator()(_Args&& ...__args) const
2065 {
2066 return base::operator()(_STD::forward<_Args>(__args)...);
2067 }
2068};
2069
Howard Hinnant324bb032010-08-22 00:02:43 +00002070template<class _R, class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {};
2072
Howard Hinnant324bb032010-08-22 00:02:43 +00002073template<class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074inline _LIBCPP_INLINE_VISIBILITY
2075__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...>
2076bind(_F&& __f, _BoundArgs&&... __bound_args)
2077{
2078 typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
2079 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
2080}
2081
Howard Hinnant324bb032010-08-22 00:02:43 +00002082template<class _R, class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083inline _LIBCPP_INLINE_VISIBILITY
2084__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...>
2085bind(_F&& __f, _BoundArgs&&... __bound_args)
2086{
2087 typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
2088 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
2089}
2090*/
2091
Howard Hinnant324bb032010-08-22 00:02:43 +00002092#endif // _LIBCPP_FUNCTIONAL_03