blob: bf55e23482f12f57e604658bce99a2f7f5fb478f [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//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL_03
12#define _LIBCPP_FUNCTIONAL_03
13
14// manual variadic expansion for <functional>
15
Howard Hinnant08e17472011-10-17 20:05:10 +000016#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000017#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000018#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
20template <class _Tp>
21class __mem_fn
22 : public __weak_result_type<_Tp>
23{
24public:
25 // types
26 typedef _Tp type;
27private:
28 type __f_;
29
30public:
31 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
32
33 // invoke
34
35 typename __invoke_return<type>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000036 operator() () const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000037 {
38 return __invoke(__f_);
39 }
40
41 template <class _A0>
42 typename __invoke_return0<type, _A0>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000043 operator() (_A0& __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044 {
45 return __invoke(__f_, __a0);
46 }
47
48 template <class _A0, class _A1>
49 typename __invoke_return1<type, _A0, _A1>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000050 operator() (_A0& __a0, _A1& __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051 {
52 return __invoke(__f_, __a0, __a1);
53 }
54
55 template <class _A0, class _A1, class _A2>
56 typename __invoke_return2<type, _A0, _A1, _A2>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000057 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000058 {
59 return __invoke(__f_, __a0, __a1, __a2);
60 }
61};
62
Howard Hinnant99968442011-11-29 18:15:50 +000063template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000064inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000065__mem_fn<_Rp _Tp::*>
66mem_fn(_Rp _Tp::* __pm)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067{
Howard Hinnant99968442011-11-29 18:15:50 +000068 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069}
70
Howard Hinnant99968442011-11-29 18:15:50 +000071template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000073__mem_fn<_Rp (_Tp::*)()>
74mem_fn(_Rp (_Tp::* __pm)())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075{
Howard Hinnant99968442011-11-29 18:15:50 +000076 return __mem_fn<_Rp (_Tp::*)()>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077}
78
Howard Hinnant99968442011-11-29 18:15:50 +000079template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000081__mem_fn<_Rp (_Tp::*)(_A0)>
82mem_fn(_Rp (_Tp::* __pm)(_A0))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083{
Howard Hinnant99968442011-11-29 18:15:50 +000084 return __mem_fn<_Rp (_Tp::*)(_A0)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085}
86
Howard Hinnant99968442011-11-29 18:15:50 +000087template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000089__mem_fn<_Rp (_Tp::*)(_A0, _A1)>
90mem_fn(_Rp (_Tp::* __pm)(_A0, _A1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091{
Howard Hinnant99968442011-11-29 18:15:50 +000092 return __mem_fn<_Rp (_Tp::*)(_A0, _A1)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093}
94
Howard Hinnant99968442011-11-29 18:15:50 +000095template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000097__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>
98mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000099{
Howard Hinnant99968442011-11-29 18:15:50 +0000100 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101}
102
Howard Hinnant99968442011-11-29 18:15:50 +0000103template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000104inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000105__mem_fn<_Rp (_Tp::*)() const>
Howard Hinnant99968442011-11-29 18:15:50 +0000106mem_fn(_Rp (_Tp::* __pm)() const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107{
Richard Smith01fbfc22013-07-23 01:24:30 +0000108 return __mem_fn<_Rp (_Tp::*)() const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109}
110
Howard Hinnant99968442011-11-29 18:15:50 +0000111template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000113__mem_fn<_Rp (_Tp::*)(_A0) const>
Howard Hinnant99968442011-11-29 18:15:50 +0000114mem_fn(_Rp (_Tp::* __pm)(_A0) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115{
Richard Smith01fbfc22013-07-23 01:24:30 +0000116 return __mem_fn<_Rp (_Tp::*)(_A0) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117}
118
Howard Hinnant99968442011-11-29 18:15:50 +0000119template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000121__mem_fn<_Rp (_Tp::*)(_A0, _A1) const>
Howard Hinnant99968442011-11-29 18:15:50 +0000122mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123{
Richard Smith01fbfc22013-07-23 01:24:30 +0000124 return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000125}
126
Howard Hinnant99968442011-11-29 18:15:50 +0000127template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000129__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>
Howard Hinnant99968442011-11-29 18:15:50 +0000130mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131{
Richard Smith01fbfc22013-07-23 01:24:30 +0000132 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133}
134
Howard Hinnant99968442011-11-29 18:15:50 +0000135template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000137__mem_fn<_Rp (_Tp::*)() volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000138mem_fn(_Rp (_Tp::* __pm)() volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139{
Richard Smith01fbfc22013-07-23 01:24:30 +0000140 return __mem_fn<_Rp (_Tp::*)() volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141}
142
Howard Hinnant99968442011-11-29 18:15:50 +0000143template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000145__mem_fn<_Rp (_Tp::*)(_A0) volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000146mem_fn(_Rp (_Tp::* __pm)(_A0) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147{
Richard Smith01fbfc22013-07-23 01:24:30 +0000148 return __mem_fn<_Rp (_Tp::*)(_A0) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000149}
150
Howard Hinnant99968442011-11-29 18:15:50 +0000151template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000153__mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000154mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155{
Richard Smith01fbfc22013-07-23 01:24:30 +0000156 return __mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157}
158
Howard Hinnant99968442011-11-29 18:15:50 +0000159template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000161__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000162mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163{
Richard Smith01fbfc22013-07-23 01:24:30 +0000164 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165}
166
Howard Hinnant99968442011-11-29 18:15:50 +0000167template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000168inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000169__mem_fn<_Rp (_Tp::*)() const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000170mem_fn(_Rp (_Tp::* __pm)() const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171{
Richard Smith01fbfc22013-07-23 01:24:30 +0000172 return __mem_fn<_Rp (_Tp::*)() const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173}
174
Howard Hinnant99968442011-11-29 18:15:50 +0000175template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000177__mem_fn<_Rp (_Tp::*)(_A0) const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000178mem_fn(_Rp (_Tp::* __pm)(_A0) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179{
Richard Smith01fbfc22013-07-23 01:24:30 +0000180 return __mem_fn<_Rp (_Tp::*)(_A0) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000181}
182
Howard Hinnant99968442011-11-29 18:15:50 +0000183template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000185__mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000186mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187{
Richard Smith01fbfc22013-07-23 01:24:30 +0000188 return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189}
190
Howard Hinnant99968442011-11-29 18:15:50 +0000191template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000193__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000194mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195{
Richard Smith01fbfc22013-07-23 01:24:30 +0000196 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197}
198
Eric Fiselier45f63bc2015-07-22 04:14:38 +0000199namespace __function {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200
201template<class _Fp> class __base;
202
Howard Hinnant99968442011-11-29 18:15:50 +0000203template<class _Rp>
204class __base<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205{
206 __base(const __base&);
207 __base& operator=(const __base&);
208public:
209 __base() {}
210 virtual ~__base() {}
211 virtual __base* __clone() const = 0;
212 virtual void __clone(__base*) const = 0;
213 virtual void destroy() = 0;
214 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000215 virtual _Rp operator()() = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000216#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 virtual const void* target(const type_info&) const = 0;
218 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000219#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220};
221
Howard Hinnant99968442011-11-29 18:15:50 +0000222template<class _Rp, class _A0>
223class __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000224{
225 __base(const __base&);
226 __base& operator=(const __base&);
227public:
228 __base() {}
229 virtual ~__base() {}
230 virtual __base* __clone() const = 0;
231 virtual void __clone(__base*) const = 0;
232 virtual void destroy() = 0;
233 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000234 virtual _Rp operator()(_A0) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000235#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236 virtual const void* target(const type_info&) const = 0;
237 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000238#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000239};
240
Howard Hinnant99968442011-11-29 18:15:50 +0000241template<class _Rp, class _A0, class _A1>
242class __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243{
244 __base(const __base&);
245 __base& operator=(const __base&);
246public:
247 __base() {}
248 virtual ~__base() {}
249 virtual __base* __clone() const = 0;
250 virtual void __clone(__base*) const = 0;
251 virtual void destroy() = 0;
252 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000253 virtual _Rp operator()(_A0, _A1) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000254#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255 virtual const void* target(const type_info&) const = 0;
256 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000257#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258};
259
Howard Hinnant99968442011-11-29 18:15:50 +0000260template<class _Rp, class _A0, class _A1, class _A2>
261class __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262{
263 __base(const __base&);
264 __base& operator=(const __base&);
265public:
266 __base() {}
267 virtual ~__base() {}
268 virtual __base* __clone() const = 0;
269 virtual void __clone(__base*) const = 0;
270 virtual void destroy() = 0;
271 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000272 virtual _Rp operator()(_A0, _A1, _A2) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000273#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274 virtual const void* target(const type_info&) const = 0;
275 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000276#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277};
278
279template<class _FD, class _Alloc, class _FB> class __func;
280
Howard Hinnant99968442011-11-29 18:15:50 +0000281template<class _Fp, class _Alloc, class _Rp>
282class __func<_Fp, _Alloc, _Rp()>
283 : public __base<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284{
Howard Hinnant99968442011-11-29 18:15:50 +0000285 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286public:
Howard Hinnant99968442011-11-29 18:15:50 +0000287 explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
288 explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
289 virtual __base<_Rp()>* __clone() const;
290 virtual void __clone(__base<_Rp()>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291 virtual void destroy();
292 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000293 virtual _Rp operator()();
Howard Hinnantd4444702010-08-11 17:04:31 +0000294#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295 virtual const void* target(const type_info&) const;
296 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000297#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298};
299
Howard Hinnant99968442011-11-29 18:15:50 +0000300template<class _Fp, class _Alloc, class _Rp>
301__base<_Rp()>*
302__func<_Fp, _Alloc, _Rp()>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000304 typedef allocator_traits<_Alloc> __alloc_traits;
305 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000306 _Ap __a(__f_.second());
307 typedef __allocator_destructor<_Ap> _Dp;
308 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
310 return __hold.release();
311}
312
Howard Hinnant99968442011-11-29 18:15:50 +0000313template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000314void
Howard Hinnant99968442011-11-29 18:15:50 +0000315__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316{
317 ::new (__p) __func(__f_.first(), __f_.second());
318}
319
Howard Hinnant99968442011-11-29 18:15:50 +0000320template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321void
Howard Hinnant99968442011-11-29 18:15:50 +0000322__func<_Fp, _Alloc, _Rp()>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000323{
Howard Hinnant99968442011-11-29 18:15:50 +0000324 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325}
326
Howard Hinnant99968442011-11-29 18:15:50 +0000327template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328void
Howard Hinnant99968442011-11-29 18:15:50 +0000329__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000331 typedef allocator_traits<_Alloc> __alloc_traits;
332 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000333 _Ap __a(__f_.second());
334 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335 __a.deallocate(this, 1);
336}
337
Howard Hinnant99968442011-11-29 18:15:50 +0000338template<class _Fp, class _Alloc, class _Rp>
339_Rp
340__func<_Fp, _Alloc, _Rp()>::operator()()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000342 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
343 return _Invoker::__call(__f_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344}
345
Howard Hinnantd4444702010-08-11 17:04:31 +0000346#ifndef _LIBCPP_NO_RTTI
347
Howard Hinnant99968442011-11-29 18:15:50 +0000348template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000349const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000350__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351{
Howard Hinnant99968442011-11-29 18:15:50 +0000352 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353 return &__f_.first();
354 return (const void*)0;
355}
356
Howard Hinnant99968442011-11-29 18:15:50 +0000357template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000359__func<_Fp, _Alloc, _Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360{
Howard Hinnant99968442011-11-29 18:15:50 +0000361 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000362}
363
Howard Hinnant324bb032010-08-22 00:02:43 +0000364#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000365
Howard Hinnant99968442011-11-29 18:15:50 +0000366template<class _Fp, class _Alloc, class _Rp, class _A0>
367class __func<_Fp, _Alloc, _Rp(_A0)>
368 : public __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369{
Howard Hinnant99968442011-11-29 18:15:50 +0000370 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371public:
Howard Hinnant99968442011-11-29 18:15:50 +0000372 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
373 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000374 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000375 virtual __base<_Rp(_A0)>* __clone() const;
376 virtual void __clone(__base<_Rp(_A0)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 virtual void destroy();
378 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000379 virtual _Rp operator()(_A0);
Howard Hinnantd4444702010-08-11 17:04:31 +0000380#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 virtual const void* target(const type_info&) const;
382 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000383#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384};
385
Howard Hinnant99968442011-11-29 18:15:50 +0000386template<class _Fp, class _Alloc, class _Rp, class _A0>
387__base<_Rp(_A0)>*
388__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000390 typedef allocator_traits<_Alloc> __alloc_traits;
391 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000392 _Ap __a(__f_.second());
393 typedef __allocator_destructor<_Ap> _Dp;
394 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
396 return __hold.release();
397}
398
Howard Hinnant99968442011-11-29 18:15:50 +0000399template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000400void
Howard Hinnant99968442011-11-29 18:15:50 +0000401__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000402{
403 ::new (__p) __func(__f_.first(), __f_.second());
404}
405
Howard Hinnant99968442011-11-29 18:15:50 +0000406template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407void
Howard Hinnant99968442011-11-29 18:15:50 +0000408__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409{
Howard Hinnant99968442011-11-29 18:15:50 +0000410 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411}
412
Howard Hinnant99968442011-11-29 18:15:50 +0000413template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414void
Howard Hinnant99968442011-11-29 18:15:50 +0000415__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000417 typedef allocator_traits<_Alloc> __alloc_traits;
418 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000419 _Ap __a(__f_.second());
420 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421 __a.deallocate(this, 1);
422}
423
Howard Hinnant99968442011-11-29 18:15:50 +0000424template<class _Fp, class _Alloc, class _Rp, class _A0>
425_Rp
426__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000428 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
429 return _Invoker::__call(__f_.first(), __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430}
431
Howard Hinnantd4444702010-08-11 17:04:31 +0000432#ifndef _LIBCPP_NO_RTTI
433
Howard Hinnant99968442011-11-29 18:15:50 +0000434template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000436__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437{
Howard Hinnant99968442011-11-29 18:15:50 +0000438 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439 return &__f_.first();
440 return (const void*)0;
441}
442
Howard Hinnant99968442011-11-29 18:15:50 +0000443template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000445__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446{
Howard Hinnant99968442011-11-29 18:15:50 +0000447 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000448}
449
Howard Hinnant324bb032010-08-22 00:02:43 +0000450#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000451
Howard Hinnant99968442011-11-29 18:15:50 +0000452template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
453class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
454 : public __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455{
Howard Hinnant99968442011-11-29 18:15:50 +0000456 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457public:
Howard Hinnant99968442011-11-29 18:15:50 +0000458 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
459 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000460 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000461 virtual __base<_Rp(_A0, _A1)>* __clone() const;
462 virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463 virtual void destroy();
464 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000465 virtual _Rp operator()(_A0, _A1);
Howard Hinnantd4444702010-08-11 17:04:31 +0000466#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 virtual const void* target(const type_info&) const;
468 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000469#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470};
471
Howard Hinnant99968442011-11-29 18:15:50 +0000472template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
473__base<_Rp(_A0, _A1)>*
474__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000476 typedef allocator_traits<_Alloc> __alloc_traits;
477 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000478 _Ap __a(__f_.second());
479 typedef __allocator_destructor<_Ap> _Dp;
480 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
482 return __hold.release();
483}
484
Howard Hinnant99968442011-11-29 18:15:50 +0000485template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000486void
Howard Hinnant99968442011-11-29 18:15:50 +0000487__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488{
489 ::new (__p) __func(__f_.first(), __f_.second());
490}
491
Howard Hinnant99968442011-11-29 18:15:50 +0000492template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000493void
Howard Hinnant99968442011-11-29 18:15:50 +0000494__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495{
Howard Hinnant99968442011-11-29 18:15:50 +0000496 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497}
498
Howard Hinnant99968442011-11-29 18:15:50 +0000499template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000500void
Howard Hinnant99968442011-11-29 18:15:50 +0000501__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000503 typedef allocator_traits<_Alloc> __alloc_traits;
504 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000505 _Ap __a(__f_.second());
506 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507 __a.deallocate(this, 1);
508}
509
Howard Hinnant99968442011-11-29 18:15:50 +0000510template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
511_Rp
512__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000514 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
515 return _Invoker::__call(__f_.first(), __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516}
517
Howard Hinnantd4444702010-08-11 17:04:31 +0000518#ifndef _LIBCPP_NO_RTTI
519
Howard Hinnant99968442011-11-29 18:15:50 +0000520template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000522__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523{
Howard Hinnant99968442011-11-29 18:15:50 +0000524 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525 return &__f_.first();
526 return (const void*)0;
527}
528
Howard Hinnant99968442011-11-29 18:15:50 +0000529template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000531__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000532{
Howard Hinnant99968442011-11-29 18:15:50 +0000533 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534}
535
Howard Hinnant324bb032010-08-22 00:02:43 +0000536#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000537
Howard Hinnant99968442011-11-29 18:15:50 +0000538template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
539class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
540 : public __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541{
Howard Hinnant99968442011-11-29 18:15:50 +0000542 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543public:
Howard Hinnant99968442011-11-29 18:15:50 +0000544 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
545 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000546 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000547 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
548 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549 virtual void destroy();
550 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000551 virtual _Rp operator()(_A0, _A1, _A2);
Howard Hinnantd4444702010-08-11 17:04:31 +0000552#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553 virtual const void* target(const type_info&) const;
554 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000555#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556};
557
Howard Hinnant99968442011-11-29 18:15:50 +0000558template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
559__base<_Rp(_A0, _A1, _A2)>*
560__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000562 typedef allocator_traits<_Alloc> __alloc_traits;
563 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000564 _Ap __a(__f_.second());
565 typedef __allocator_destructor<_Ap> _Dp;
566 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
568 return __hold.release();
569}
570
Howard Hinnant99968442011-11-29 18:15:50 +0000571template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572void
Howard Hinnant99968442011-11-29 18:15:50 +0000573__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574{
575 ::new (__p) __func(__f_.first(), __f_.second());
576}
577
Howard Hinnant99968442011-11-29 18:15:50 +0000578template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579void
Howard Hinnant99968442011-11-29 18:15:50 +0000580__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581{
Howard Hinnant99968442011-11-29 18:15:50 +0000582 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583}
584
Howard Hinnant99968442011-11-29 18:15:50 +0000585template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000586void
Howard Hinnant99968442011-11-29 18:15:50 +0000587__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000589 typedef allocator_traits<_Alloc> __alloc_traits;
590 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000591 _Ap __a(__f_.second());
592 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593 __a.deallocate(this, 1);
594}
595
Howard Hinnant99968442011-11-29 18:15:50 +0000596template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
597_Rp
598__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000600 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
601 return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602}
603
Howard Hinnantd4444702010-08-11 17:04:31 +0000604#ifndef _LIBCPP_NO_RTTI
605
Howard Hinnant99968442011-11-29 18:15:50 +0000606template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000608__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609{
Howard Hinnant99968442011-11-29 18:15:50 +0000610 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611 return &__f_.first();
612 return (const void*)0;
613}
614
Howard Hinnant99968442011-11-29 18:15:50 +0000615template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000617__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618{
Howard Hinnant99968442011-11-29 18:15:50 +0000619 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620}
621
Howard Hinnant324bb032010-08-22 00:02:43 +0000622#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000623
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624} // __function
625
Howard Hinnant99968442011-11-29 18:15:50 +0000626template<class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000627class _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000628{
Howard Hinnant99968442011-11-29 18:15:50 +0000629 typedef __function::__base<_Rp()> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 aligned_storage<3*sizeof(void*)>::type __buf_;
631 __base* __f_;
632
Howard Hinnant99968442011-11-29 18:15:50 +0000633 template <class _Fp>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000635 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 template <class _R2>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000637 _LIBCPP_INLINE_VISIBILITY
638 static bool __not_null(_R2 (*__p)()) {return __p;}
639 template <class _R2>
640 _LIBCPP_INLINE_VISIBILITY
641 static bool __not_null(const function<_R2()>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642public:
Howard Hinnant99968442011-11-29 18:15:50 +0000643 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644
645 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000646 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
647 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000649 template<class _Fp>
650 function(_Fp,
651 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652
Howard Hinnant72552802010-08-20 19:36:46 +0000653 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000655 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
656 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000658 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
659 template<class _Alloc>
660 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000661 template<class _Fp, class _Alloc>
662 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
663 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664
665 function& operator=(const function&);
666 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000667 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668 typename enable_if
669 <
Howard Hinnant99968442011-11-29 18:15:50 +0000670 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671 function&
672 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000673 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674
675 ~function();
676
677 // 20.7.16.2.2, function modifiers:
678 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000679 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000681 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +0000682 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683
684 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +0000685 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000686
687private:
688 // deleted overloads close possible hole in the type system
689 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000690 bool operator==(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000692 bool operator!=(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693public:
694 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +0000695 _Rp operator()() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000696
Howard Hinnantd4444702010-08-11 17:04:31 +0000697#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698 // 20.7.16.2.5, function target access:
699 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +0000700 template <typename _Tp> _Tp* target();
701 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000702#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703};
704
Howard Hinnant99968442011-11-29 18:15:50 +0000705template<class _Rp>
706function<_Rp()>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707{
708 if (__f.__f_ == 0)
709 __f_ = 0;
710 else if (__f.__f_ == (const __base*)&__f.__buf_)
711 {
712 __f_ = (__base*)&__buf_;
713 __f.__f_->__clone(__f_);
714 }
715 else
716 __f_ = __f.__f_->__clone();
717}
718
Howard Hinnant99968442011-11-29 18:15:50 +0000719template<class _Rp>
Howard Hinnant72552802010-08-20 19:36:46 +0000720template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +0000721function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +0000722{
723 if (__f.__f_ == 0)
724 __f_ = 0;
725 else if (__f.__f_ == (const __base*)&__f.__buf_)
726 {
727 __f_ = (__base*)&__buf_;
728 __f.__f_->__clone(__f_);
729 }
730 else
731 __f_ = __f.__f_->__clone();
732}
733
Howard Hinnant99968442011-11-29 18:15:50 +0000734template<class _Rp>
735template <class _Fp>
736function<_Rp()>::function(_Fp __f,
737 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738 : __f_(0)
739{
740 if (__not_null(__f))
741 {
Howard Hinnant99968442011-11-29 18:15:50 +0000742 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743 if (sizeof(_FF) <= sizeof(__buf_))
744 {
745 __f_ = (__base*)&__buf_;
746 ::new (__f_) _FF(__f);
747 }
748 else
749 {
Howard Hinnant99968442011-11-29 18:15:50 +0000750 typedef allocator<_FF> _Ap;
751 _Ap __a;
752 typedef __allocator_destructor<_Ap> _Dp;
753 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
754 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755 __f_ = __hold.release();
756 }
757 }
758}
759
Howard Hinnant99968442011-11-29 18:15:50 +0000760template<class _Rp>
761template <class _Fp, class _Alloc>
762function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
763 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +0000764 : __f_(0)
765{
766 typedef allocator_traits<_Alloc> __alloc_traits;
767 if (__not_null(__f))
768 {
Howard Hinnant99968442011-11-29 18:15:50 +0000769 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +0000770 if (sizeof(_FF) <= sizeof(__buf_))
771 {
772 __f_ = (__base*)&__buf_;
Eric Fiselierb05f0592015-06-14 23:30:09 +0000773 ::new (__f_) _FF(__f, __a0);
Howard Hinnant72552802010-08-20 19:36:46 +0000774 }
775 else
776 {
Marshall Clow66302c62015-04-07 05:21:38 +0000777 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000778 _Ap __a(__a0);
779 typedef __allocator_destructor<_Ap> _Dp;
780 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +0000781 ::new (__hold.get()) _FF(__f, _Alloc(__a));
782 __f_ = __hold.release();
783 }
784 }
785}
786
Howard Hinnant99968442011-11-29 18:15:50 +0000787template<class _Rp>
788function<_Rp()>&
789function<_Rp()>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790{
791 function(__f).swap(*this);
792 return *this;
793}
794
Howard Hinnant99968442011-11-29 18:15:50 +0000795template<class _Rp>
796function<_Rp()>&
797function<_Rp()>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000798{
799 if (__f_ == (__base*)&__buf_)
800 __f_->destroy();
801 else if (__f_)
802 __f_->destroy_deallocate();
803 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +0000804 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000805}
806
Howard Hinnant99968442011-11-29 18:15:50 +0000807template<class _Rp>
808template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000809typename enable_if
810<
Howard Hinnant99968442011-11-29 18:15:50 +0000811 !is_integral<_Fp>::value,
812 function<_Rp()>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000813>::type
Howard Hinnant99968442011-11-29 18:15:50 +0000814function<_Rp()>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000815{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000816 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817 return *this;
818}
819
Howard Hinnant99968442011-11-29 18:15:50 +0000820template<class _Rp>
821function<_Rp()>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822{
823 if (__f_ == (__base*)&__buf_)
824 __f_->destroy();
825 else if (__f_)
826 __f_->destroy_deallocate();
827}
828
Howard Hinnant99968442011-11-29 18:15:50 +0000829template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000830void
Howard Hinnant99968442011-11-29 18:15:50 +0000831function<_Rp()>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000832{
833 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
834 {
835 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
836 __base* __t = (__base*)&__tempbuf;
837 __f_->__clone(__t);
838 __f_->destroy();
839 __f_ = 0;
840 __f.__f_->__clone((__base*)&__buf_);
841 __f.__f_->destroy();
842 __f.__f_ = 0;
843 __f_ = (__base*)&__buf_;
844 __t->__clone((__base*)&__f.__buf_);
845 __t->destroy();
846 __f.__f_ = (__base*)&__f.__buf_;
847 }
848 else if (__f_ == (__base*)&__buf_)
849 {
850 __f_->__clone((__base*)&__f.__buf_);
851 __f_->destroy();
852 __f_ = __f.__f_;
853 __f.__f_ = (__base*)&__f.__buf_;
854 }
855 else if (__f.__f_ == (__base*)&__f.__buf_)
856 {
857 __f.__f_->__clone((__base*)&__buf_);
858 __f.__f_->destroy();
859 __f.__f_ = __f_;
860 __f_ = (__base*)&__buf_;
861 }
862 else
Howard Hinnant0949eed2011-06-30 21:18:19 +0000863 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864}
865
Howard Hinnant99968442011-11-29 18:15:50 +0000866template<class _Rp>
867_Rp
868function<_Rp()>::operator()() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000869{
Howard Hinnantd4444702010-08-11 17:04:31 +0000870#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000871 if (__f_ == 0)
872 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +0000873#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874 return (*__f_)();
875}
876
Howard Hinnantd4444702010-08-11 17:04:31 +0000877#ifndef _LIBCPP_NO_RTTI
878
Howard Hinnant99968442011-11-29 18:15:50 +0000879template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000881function<_Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882{
883 if (__f_ == 0)
884 return typeid(void);
885 return __f_->target_type();
886}
887
Howard Hinnant99968442011-11-29 18:15:50 +0000888template<class _Rp>
889template <typename _Tp>
890_Tp*
891function<_Rp()>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892{
893 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000894 return (_Tp*)0;
895 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896}
897
Howard Hinnant99968442011-11-29 18:15:50 +0000898template<class _Rp>
899template <typename _Tp>
900const _Tp*
901function<_Rp()>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902{
903 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000904 return (const _Tp*)0;
905 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906}
907
Howard Hinnant324bb032010-08-22 00:02:43 +0000908#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000909
Howard Hinnant99968442011-11-29 18:15:50 +0000910template<class _Rp, class _A0>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000911class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
Howard Hinnant99968442011-11-29 18:15:50 +0000912 : public unary_function<_A0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913{
Howard Hinnant99968442011-11-29 18:15:50 +0000914 typedef __function::__base<_Rp(_A0)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915 aligned_storage<3*sizeof(void*)>::type __buf_;
916 __base* __f_;
917
Howard Hinnant99968442011-11-29 18:15:50 +0000918 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000920 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +0000924 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000926 static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
927 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000929 static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
930 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000932 static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
933 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000935 static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000937 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +0000938 static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939public:
Howard Hinnant99968442011-11-29 18:15:50 +0000940 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941
942 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000943 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
944 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000946 template<class _Fp>
947 function(_Fp,
948 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949
Howard Hinnant72552802010-08-20 19:36:46 +0000950 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000952 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
953 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000955 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
956 template<class _Alloc>
957 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000958 template<class _Fp, class _Alloc>
959 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
960 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961
962 function& operator=(const function&);
963 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000964 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 typename enable_if
966 <
Howard Hinnant99968442011-11-29 18:15:50 +0000967 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 function&
969 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000970 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971
972 ~function();
973
974 // 20.7.16.2.2, function modifiers:
975 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000976 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000978 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +0000979 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000980
981 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +0000982 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983
984private:
985 // deleted overloads close possible hole in the type system
986 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000987 bool operator==(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000989 bool operator!=(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990public:
991 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +0000992 _Rp operator()(_A0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993
Howard Hinnantd4444702010-08-11 17:04:31 +0000994#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995 // 20.7.16.2.5, function target access:
996 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +0000997 template <typename _Tp> _Tp* target();
998 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000999#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000};
1001
Howard Hinnant99968442011-11-29 18:15:50 +00001002template<class _Rp, class _A0>
1003function<_Rp(_A0)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004{
1005 if (__f.__f_ == 0)
1006 __f_ = 0;
1007 else if (__f.__f_ == (const __base*)&__f.__buf_)
1008 {
1009 __f_ = (__base*)&__buf_;
1010 __f.__f_->__clone(__f_);
1011 }
1012 else
1013 __f_ = __f.__f_->__clone();
1014}
1015
Howard Hinnant99968442011-11-29 18:15:50 +00001016template<class _Rp, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001017template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001018function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001019{
1020 if (__f.__f_ == 0)
1021 __f_ = 0;
1022 else if (__f.__f_ == (const __base*)&__f.__buf_)
1023 {
1024 __f_ = (__base*)&__buf_;
1025 __f.__f_->__clone(__f_);
1026 }
1027 else
1028 __f_ = __f.__f_->__clone();
1029}
1030
Howard Hinnant99968442011-11-29 18:15:50 +00001031template<class _Rp, class _A0>
1032template <class _Fp>
1033function<_Rp(_A0)>::function(_Fp __f,
1034 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035 : __f_(0)
1036{
1037 if (__not_null(__f))
1038 {
Howard Hinnant99968442011-11-29 18:15:50 +00001039 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 if (sizeof(_FF) <= sizeof(__buf_))
1041 {
1042 __f_ = (__base*)&__buf_;
1043 ::new (__f_) _FF(__f);
1044 }
1045 else
1046 {
Howard Hinnant99968442011-11-29 18:15:50 +00001047 typedef allocator<_FF> _Ap;
1048 _Ap __a;
1049 typedef __allocator_destructor<_Ap> _Dp;
1050 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1051 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052 __f_ = __hold.release();
1053 }
1054 }
1055}
1056
Howard Hinnant99968442011-11-29 18:15:50 +00001057template<class _Rp, class _A0>
1058template <class _Fp, class _Alloc>
1059function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1060 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001061 : __f_(0)
1062{
1063 typedef allocator_traits<_Alloc> __alloc_traits;
1064 if (__not_null(__f))
1065 {
Howard Hinnant99968442011-11-29 18:15:50 +00001066 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001067 if (sizeof(_FF) <= sizeof(__buf_))
1068 {
1069 __f_ = (__base*)&__buf_;
Eric Fiselierb05f0592015-06-14 23:30:09 +00001070 ::new (__f_) _FF(__f, __a0);
Howard Hinnant72552802010-08-20 19:36:46 +00001071 }
1072 else
1073 {
Marshall Clow66302c62015-04-07 05:21:38 +00001074 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001075 _Ap __a(__a0);
1076 typedef __allocator_destructor<_Ap> _Dp;
1077 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001078 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1079 __f_ = __hold.release();
1080 }
1081 }
1082}
1083
Howard Hinnant99968442011-11-29 18:15:50 +00001084template<class _Rp, class _A0>
1085function<_Rp(_A0)>&
1086function<_Rp(_A0)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001087{
1088 function(__f).swap(*this);
1089 return *this;
1090}
1091
Howard Hinnant99968442011-11-29 18:15:50 +00001092template<class _Rp, class _A0>
1093function<_Rp(_A0)>&
1094function<_Rp(_A0)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095{
1096 if (__f_ == (__base*)&__buf_)
1097 __f_->destroy();
1098 else if (__f_)
1099 __f_->destroy_deallocate();
1100 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +00001101 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102}
1103
Howard Hinnant99968442011-11-29 18:15:50 +00001104template<class _Rp, class _A0>
1105template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001106typename enable_if
1107<
Howard Hinnant99968442011-11-29 18:15:50 +00001108 !is_integral<_Fp>::value,
1109 function<_Rp(_A0)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001111function<_Rp(_A0)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001113 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114 return *this;
1115}
1116
Howard Hinnant99968442011-11-29 18:15:50 +00001117template<class _Rp, class _A0>
1118function<_Rp(_A0)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001119{
1120 if (__f_ == (__base*)&__buf_)
1121 __f_->destroy();
1122 else if (__f_)
1123 __f_->destroy_deallocate();
1124}
1125
Howard Hinnant99968442011-11-29 18:15:50 +00001126template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127void
Howard Hinnant99968442011-11-29 18:15:50 +00001128function<_Rp(_A0)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129{
1130 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1131 {
1132 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1133 __base* __t = (__base*)&__tempbuf;
1134 __f_->__clone(__t);
1135 __f_->destroy();
1136 __f_ = 0;
1137 __f.__f_->__clone((__base*)&__buf_);
1138 __f.__f_->destroy();
1139 __f.__f_ = 0;
1140 __f_ = (__base*)&__buf_;
1141 __t->__clone((__base*)&__f.__buf_);
1142 __t->destroy();
1143 __f.__f_ = (__base*)&__f.__buf_;
1144 }
1145 else if (__f_ == (__base*)&__buf_)
1146 {
1147 __f_->__clone((__base*)&__f.__buf_);
1148 __f_->destroy();
1149 __f_ = __f.__f_;
1150 __f.__f_ = (__base*)&__f.__buf_;
1151 }
1152 else if (__f.__f_ == (__base*)&__f.__buf_)
1153 {
1154 __f.__f_->__clone((__base*)&__buf_);
1155 __f.__f_->destroy();
1156 __f.__f_ = __f_;
1157 __f_ = (__base*)&__buf_;
1158 }
1159 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001160 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161}
1162
Howard Hinnant99968442011-11-29 18:15:50 +00001163template<class _Rp, class _A0>
1164_Rp
1165function<_Rp(_A0)>::operator()(_A0 __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001166{
Howard Hinnantd4444702010-08-11 17:04:31 +00001167#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 if (__f_ == 0)
1169 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001170#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171 return (*__f_)(__a0);
1172}
1173
Howard Hinnantd4444702010-08-11 17:04:31 +00001174#ifndef _LIBCPP_NO_RTTI
1175
Howard Hinnant99968442011-11-29 18:15:50 +00001176template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001177const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001178function<_Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001179{
1180 if (__f_ == 0)
1181 return typeid(void);
1182 return __f_->target_type();
1183}
1184
Howard Hinnant99968442011-11-29 18:15:50 +00001185template<class _Rp, class _A0>
1186template <typename _Tp>
1187_Tp*
1188function<_Rp(_A0)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189{
1190 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001191 return (_Tp*)0;
1192 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193}
1194
Howard Hinnant99968442011-11-29 18:15:50 +00001195template<class _Rp, class _A0>
1196template <typename _Tp>
1197const _Tp*
1198function<_Rp(_A0)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199{
1200 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001201 return (const _Tp*)0;
1202 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203}
1204
Howard Hinnant324bb032010-08-22 00:02:43 +00001205#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001206
Howard Hinnant99968442011-11-29 18:15:50 +00001207template<class _Rp, class _A0, class _A1>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001208class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
Howard Hinnant99968442011-11-29 18:15:50 +00001209 : public binary_function<_A0, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210{
Howard Hinnant99968442011-11-29 18:15:50 +00001211 typedef __function::__base<_Rp(_A0, _A1)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001212 aligned_storage<3*sizeof(void*)>::type __buf_;
1213 __base* __f_;
1214
Howard Hinnant99968442011-11-29 18:15:50 +00001215 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001217 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001218 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001221 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001223 static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1224 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001226 static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1227 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001229 static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1230 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001232 static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001234 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001235 static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236public:
Howard Hinnant99968442011-11-29 18:15:50 +00001237 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001238
1239 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001240 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1241 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001243 template<class _Fp>
1244 function(_Fp,
1245 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246
Howard Hinnant72552802010-08-20 19:36:46 +00001247 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001249 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1250 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001252 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1253 template<class _Alloc>
1254 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001255 template<class _Fp, class _Alloc>
1256 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1257 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258
1259 function& operator=(const function&);
1260 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001261 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262 typename enable_if
1263 <
Howard Hinnant99968442011-11-29 18:15:50 +00001264 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001265 function&
1266 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001267 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268
1269 ~function();
1270
1271 // 20.7.16.2.2, function modifiers:
1272 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001273 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001275 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001276 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277
1278 // 20.7.16.2.3, function capacity:
1279 operator bool() const {return __f_;}
1280
1281private:
1282 // deleted overloads close possible hole in the type system
1283 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001284 bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001286 bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287public:
1288 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001289 _Rp operator()(_A0, _A1) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290
Howard Hinnantd4444702010-08-11 17:04:31 +00001291#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292 // 20.7.16.2.5, function target access:
1293 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001294 template <typename _Tp> _Tp* target();
1295 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001296#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297};
1298
Howard Hinnant99968442011-11-29 18:15:50 +00001299template<class _Rp, class _A0, class _A1>
1300function<_Rp(_A0, _A1)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301{
1302 if (__f.__f_ == 0)
1303 __f_ = 0;
1304 else if (__f.__f_ == (const __base*)&__f.__buf_)
1305 {
1306 __f_ = (__base*)&__buf_;
1307 __f.__f_->__clone(__f_);
1308 }
1309 else
1310 __f_ = __f.__f_->__clone();
1311}
1312
Howard Hinnant99968442011-11-29 18:15:50 +00001313template<class _Rp, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001314template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001315function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001316{
1317 if (__f.__f_ == 0)
1318 __f_ = 0;
1319 else if (__f.__f_ == (const __base*)&__f.__buf_)
1320 {
1321 __f_ = (__base*)&__buf_;
1322 __f.__f_->__clone(__f_);
1323 }
1324 else
1325 __f_ = __f.__f_->__clone();
1326}
1327
Howard Hinnant99968442011-11-29 18:15:50 +00001328template<class _Rp, class _A0, class _A1>
1329template <class _Fp>
1330function<_Rp(_A0, _A1)>::function(_Fp __f,
1331 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 : __f_(0)
1333{
1334 if (__not_null(__f))
1335 {
Howard Hinnant99968442011-11-29 18:15:50 +00001336 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 if (sizeof(_FF) <= sizeof(__buf_))
1338 {
1339 __f_ = (__base*)&__buf_;
1340 ::new (__f_) _FF(__f);
1341 }
1342 else
1343 {
Howard Hinnant99968442011-11-29 18:15:50 +00001344 typedef allocator<_FF> _Ap;
1345 _Ap __a;
1346 typedef __allocator_destructor<_Ap> _Dp;
1347 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1348 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 __f_ = __hold.release();
1350 }
1351 }
1352}
1353
Howard Hinnant99968442011-11-29 18:15:50 +00001354template<class _Rp, class _A0, class _A1>
1355template <class _Fp, class _Alloc>
1356function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1357 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001358 : __f_(0)
1359{
1360 typedef allocator_traits<_Alloc> __alloc_traits;
1361 if (__not_null(__f))
1362 {
Howard Hinnant99968442011-11-29 18:15:50 +00001363 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001364 if (sizeof(_FF) <= sizeof(__buf_))
1365 {
1366 __f_ = (__base*)&__buf_;
Eric Fiselierb05f0592015-06-14 23:30:09 +00001367 ::new (__f_) _FF(__f, __a0);
Howard Hinnant72552802010-08-20 19:36:46 +00001368 }
1369 else
1370 {
Marshall Clow66302c62015-04-07 05:21:38 +00001371 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001372 _Ap __a(__a0);
1373 typedef __allocator_destructor<_Ap> _Dp;
1374 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001375 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1376 __f_ = __hold.release();
1377 }
1378 }
1379}
1380
Howard Hinnant99968442011-11-29 18:15:50 +00001381template<class _Rp, class _A0, class _A1>
1382function<_Rp(_A0, _A1)>&
1383function<_Rp(_A0, _A1)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001384{
1385 function(__f).swap(*this);
1386 return *this;
1387}
1388
Howard Hinnant99968442011-11-29 18:15:50 +00001389template<class _Rp, class _A0, class _A1>
1390function<_Rp(_A0, _A1)>&
1391function<_Rp(_A0, _A1)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392{
1393 if (__f_ == (__base*)&__buf_)
1394 __f_->destroy();
1395 else if (__f_)
1396 __f_->destroy_deallocate();
1397 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +00001398 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399}
1400
Howard Hinnant99968442011-11-29 18:15:50 +00001401template<class _Rp, class _A0, class _A1>
1402template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403typename enable_if
1404<
Howard Hinnant99968442011-11-29 18:15:50 +00001405 !is_integral<_Fp>::value,
1406 function<_Rp(_A0, _A1)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001408function<_Rp(_A0, _A1)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001410 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411 return *this;
1412}
1413
Howard Hinnant99968442011-11-29 18:15:50 +00001414template<class _Rp, class _A0, class _A1>
1415function<_Rp(_A0, _A1)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416{
1417 if (__f_ == (__base*)&__buf_)
1418 __f_->destroy();
1419 else if (__f_)
1420 __f_->destroy_deallocate();
1421}
1422
Howard Hinnant99968442011-11-29 18:15:50 +00001423template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424void
Howard Hinnant99968442011-11-29 18:15:50 +00001425function<_Rp(_A0, _A1)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426{
1427 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1428 {
1429 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1430 __base* __t = (__base*)&__tempbuf;
1431 __f_->__clone(__t);
1432 __f_->destroy();
1433 __f_ = 0;
1434 __f.__f_->__clone((__base*)&__buf_);
1435 __f.__f_->destroy();
1436 __f.__f_ = 0;
1437 __f_ = (__base*)&__buf_;
1438 __t->__clone((__base*)&__f.__buf_);
1439 __t->destroy();
1440 __f.__f_ = (__base*)&__f.__buf_;
1441 }
1442 else if (__f_ == (__base*)&__buf_)
1443 {
1444 __f_->__clone((__base*)&__f.__buf_);
1445 __f_->destroy();
1446 __f_ = __f.__f_;
1447 __f.__f_ = (__base*)&__f.__buf_;
1448 }
1449 else if (__f.__f_ == (__base*)&__f.__buf_)
1450 {
1451 __f.__f_->__clone((__base*)&__buf_);
1452 __f.__f_->destroy();
1453 __f.__f_ = __f_;
1454 __f_ = (__base*)&__buf_;
1455 }
1456 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001457 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458}
1459
Howard Hinnant99968442011-11-29 18:15:50 +00001460template<class _Rp, class _A0, class _A1>
1461_Rp
1462function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463{
Howard Hinnantd4444702010-08-11 17:04:31 +00001464#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465 if (__f_ == 0)
1466 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001467#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468 return (*__f_)(__a0, __a1);
1469}
1470
Howard Hinnantd4444702010-08-11 17:04:31 +00001471#ifndef _LIBCPP_NO_RTTI
1472
Howard Hinnant99968442011-11-29 18:15:50 +00001473template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001475function<_Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476{
1477 if (__f_ == 0)
1478 return typeid(void);
1479 return __f_->target_type();
1480}
1481
Howard Hinnant99968442011-11-29 18:15:50 +00001482template<class _Rp, class _A0, class _A1>
1483template <typename _Tp>
1484_Tp*
1485function<_Rp(_A0, _A1)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486{
1487 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001488 return (_Tp*)0;
1489 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490}
1491
Howard Hinnant99968442011-11-29 18:15:50 +00001492template<class _Rp, class _A0, class _A1>
1493template <typename _Tp>
1494const _Tp*
1495function<_Rp(_A0, _A1)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496{
1497 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001498 return (const _Tp*)0;
1499 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500}
1501
Howard Hinnant324bb032010-08-22 00:02:43 +00001502#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001503
Howard Hinnant99968442011-11-29 18:15:50 +00001504template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001505class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506{
Howard Hinnant99968442011-11-29 18:15:50 +00001507 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508 aligned_storage<3*sizeof(void*)>::type __buf_;
1509 __base* __f_;
1510
Howard Hinnant99968442011-11-29 18:15:50 +00001511 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001513 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001517 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001519 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1520 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001522 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1523 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001525 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1526 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001528 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001530 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001531 static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532public:
Howard Hinnant99968442011-11-29 18:15:50 +00001533 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534
1535 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001536 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1537 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001539 template<class _Fp>
1540 function(_Fp,
1541 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542
Howard Hinnant72552802010-08-20 19:36:46 +00001543 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001545 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1546 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001548 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1549 template<class _Alloc>
1550 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001551 template<class _Fp, class _Alloc>
1552 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1553 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554
1555 function& operator=(const function&);
1556 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001557 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 typename enable_if
1559 <
Howard Hinnant99968442011-11-29 18:15:50 +00001560 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 function&
1562 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001563 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564
1565 ~function();
1566
1567 // 20.7.16.2.2, function modifiers:
1568 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001569 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001571 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001572 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573
1574 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001575 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576
1577private:
1578 // deleted overloads close possible hole in the type system
1579 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001580 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001581 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001582 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001583public:
1584 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001585 _Rp operator()(_A0, _A1, _A2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586
Howard Hinnantd4444702010-08-11 17:04:31 +00001587#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588 // 20.7.16.2.5, function target access:
1589 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001590 template <typename _Tp> _Tp* target();
1591 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001592#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593};
1594
Howard Hinnant99968442011-11-29 18:15:50 +00001595template<class _Rp, class _A0, class _A1, class _A2>
1596function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597{
1598 if (__f.__f_ == 0)
1599 __f_ = 0;
1600 else if (__f.__f_ == (const __base*)&__f.__buf_)
1601 {
1602 __f_ = (__base*)&__buf_;
1603 __f.__f_->__clone(__f_);
1604 }
1605 else
1606 __f_ = __f.__f_->__clone();
1607}
1608
Howard Hinnant99968442011-11-29 18:15:50 +00001609template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001610template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001611function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnant72552802010-08-20 19:36:46 +00001612 const function& __f)
1613{
1614 if (__f.__f_ == 0)
1615 __f_ = 0;
1616 else if (__f.__f_ == (const __base*)&__f.__buf_)
1617 {
1618 __f_ = (__base*)&__buf_;
1619 __f.__f_->__clone(__f_);
1620 }
1621 else
1622 __f_ = __f.__f_->__clone();
1623}
1624
Howard Hinnant99968442011-11-29 18:15:50 +00001625template<class _Rp, class _A0, class _A1, class _A2>
1626template <class _Fp>
1627function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1628 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629 : __f_(0)
1630{
1631 if (__not_null(__f))
1632 {
Howard Hinnant99968442011-11-29 18:15:50 +00001633 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634 if (sizeof(_FF) <= sizeof(__buf_))
1635 {
1636 __f_ = (__base*)&__buf_;
1637 ::new (__f_) _FF(__f);
1638 }
1639 else
1640 {
Howard Hinnant99968442011-11-29 18:15:50 +00001641 typedef allocator<_FF> _Ap;
1642 _Ap __a;
1643 typedef __allocator_destructor<_Ap> _Dp;
1644 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1645 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646 __f_ = __hold.release();
1647 }
1648 }
1649}
1650
Howard Hinnant99968442011-11-29 18:15:50 +00001651template<class _Rp, class _A0, class _A1, class _A2>
1652template <class _Fp, class _Alloc>
1653function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1654 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001655 : __f_(0)
1656{
1657 typedef allocator_traits<_Alloc> __alloc_traits;
1658 if (__not_null(__f))
1659 {
Howard Hinnant99968442011-11-29 18:15:50 +00001660 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001661 if (sizeof(_FF) <= sizeof(__buf_))
1662 {
1663 __f_ = (__base*)&__buf_;
Eric Fiselierb05f0592015-06-14 23:30:09 +00001664 ::new (__f_) _FF(__f, __a0);
Howard Hinnant72552802010-08-20 19:36:46 +00001665 }
1666 else
1667 {
Marshall Clow66302c62015-04-07 05:21:38 +00001668 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001669 _Ap __a(__a0);
1670 typedef __allocator_destructor<_Ap> _Dp;
1671 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001672 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1673 __f_ = __hold.release();
1674 }
1675 }
1676}
1677
Howard Hinnant99968442011-11-29 18:15:50 +00001678template<class _Rp, class _A0, class _A1, class _A2>
1679function<_Rp(_A0, _A1, _A2)>&
1680function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681{
1682 function(__f).swap(*this);
1683 return *this;
1684}
1685
Howard Hinnant99968442011-11-29 18:15:50 +00001686template<class _Rp, class _A0, class _A1, class _A2>
1687function<_Rp(_A0, _A1, _A2)>&
1688function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689{
1690 if (__f_ == (__base*)&__buf_)
1691 __f_->destroy();
1692 else if (__f_)
1693 __f_->destroy_deallocate();
1694 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +00001695 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696}
1697
Howard Hinnant99968442011-11-29 18:15:50 +00001698template<class _Rp, class _A0, class _A1, class _A2>
1699template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700typename enable_if
1701<
Howard Hinnant99968442011-11-29 18:15:50 +00001702 !is_integral<_Fp>::value,
1703 function<_Rp(_A0, _A1, _A2)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001705function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001707 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708 return *this;
1709}
1710
Howard Hinnant99968442011-11-29 18:15:50 +00001711template<class _Rp, class _A0, class _A1, class _A2>
1712function<_Rp(_A0, _A1, _A2)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713{
1714 if (__f_ == (__base*)&__buf_)
1715 __f_->destroy();
1716 else if (__f_)
1717 __f_->destroy_deallocate();
1718}
1719
Howard Hinnant99968442011-11-29 18:15:50 +00001720template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721void
Howard Hinnant99968442011-11-29 18:15:50 +00001722function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723{
1724 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1725 {
1726 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1727 __base* __t = (__base*)&__tempbuf;
1728 __f_->__clone(__t);
1729 __f_->destroy();
1730 __f_ = 0;
1731 __f.__f_->__clone((__base*)&__buf_);
1732 __f.__f_->destroy();
1733 __f.__f_ = 0;
1734 __f_ = (__base*)&__buf_;
1735 __t->__clone((__base*)&__f.__buf_);
1736 __t->destroy();
1737 __f.__f_ = (__base*)&__f.__buf_;
1738 }
1739 else if (__f_ == (__base*)&__buf_)
1740 {
1741 __f_->__clone((__base*)&__f.__buf_);
1742 __f_->destroy();
1743 __f_ = __f.__f_;
1744 __f.__f_ = (__base*)&__f.__buf_;
1745 }
1746 else if (__f.__f_ == (__base*)&__f.__buf_)
1747 {
1748 __f.__f_->__clone((__base*)&__buf_);
1749 __f.__f_->destroy();
1750 __f.__f_ = __f_;
1751 __f_ = (__base*)&__buf_;
1752 }
1753 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001754 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755}
1756
Howard Hinnant99968442011-11-29 18:15:50 +00001757template<class _Rp, class _A0, class _A1, class _A2>
1758_Rp
1759function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760{
Howard Hinnantd4444702010-08-11 17:04:31 +00001761#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762 if (__f_ == 0)
1763 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001764#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 return (*__f_)(__a0, __a1, __a2);
1766}
1767
Howard Hinnantd4444702010-08-11 17:04:31 +00001768#ifndef _LIBCPP_NO_RTTI
1769
Howard Hinnant99968442011-11-29 18:15:50 +00001770template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001771const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001772function<_Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773{
1774 if (__f_ == 0)
1775 return typeid(void);
1776 return __f_->target_type();
1777}
1778
Howard Hinnant99968442011-11-29 18:15:50 +00001779template<class _Rp, class _A0, class _A1, class _A2>
1780template <typename _Tp>
1781_Tp*
1782function<_Rp(_A0, _A1, _A2)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783{
1784 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001785 return (_Tp*)0;
1786 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787}
1788
Howard Hinnant99968442011-11-29 18:15:50 +00001789template<class _Rp, class _A0, class _A1, class _A2>
1790template <typename _Tp>
1791const _Tp*
1792function<_Rp(_A0, _A1, _A2)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793{
1794 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001795 return (const _Tp*)0;
1796 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797}
1798
Howard Hinnant324bb032010-08-22 00:02:43 +00001799#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001800
Howard Hinnant99968442011-11-29 18:15:50 +00001801template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802inline _LIBCPP_INLINE_VISIBILITY
1803bool
Howard Hinnant99968442011-11-29 18:15:50 +00001804operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805
Howard Hinnant99968442011-11-29 18:15:50 +00001806template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807inline _LIBCPP_INLINE_VISIBILITY
1808bool
Howard Hinnant99968442011-11-29 18:15:50 +00001809operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001810
Howard Hinnant99968442011-11-29 18:15:50 +00001811template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001812inline _LIBCPP_INLINE_VISIBILITY
1813bool
Howard Hinnant99968442011-11-29 18:15:50 +00001814operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815
Howard Hinnant99968442011-11-29 18:15:50 +00001816template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817inline _LIBCPP_INLINE_VISIBILITY
1818bool
Howard Hinnant99968442011-11-29 18:15:50 +00001819operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820
Howard Hinnant99968442011-11-29 18:15:50 +00001821template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822inline _LIBCPP_INLINE_VISIBILITY
1823void
Howard Hinnant99968442011-11-29 18:15:50 +00001824swap(function<_Fp>& __x, function<_Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001825{return __x.swap(__y);}
1826
Howard Hinnant324bb032010-08-22 00:02:43 +00001827#endif // _LIBCPP_FUNCTIONAL_03