blob: 785a2758c05836d787b0f949d56dec59a153c14a [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
199// bad_function_call
200
Howard Hinnant99acc502010-09-21 17:32:39 +0000201class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 : public exception
203{
204};
205
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000206template<class _Fp> class _LIBCPP_TYPE_VIS_ONLY function; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207
208namespace __function
209{
210
Howard Hinnant99968442011-11-29 18:15:50 +0000211template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212struct __maybe_derive_from_unary_function
213{
214};
215
Howard Hinnant99968442011-11-29 18:15:50 +0000216template<class _Rp, class _A1>
217struct __maybe_derive_from_unary_function<_Rp(_A1)>
218 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219{
220};
221
Howard Hinnant99968442011-11-29 18:15:50 +0000222template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223struct __maybe_derive_from_binary_function
224{
225};
226
Howard Hinnant99968442011-11-29 18:15:50 +0000227template<class _Rp, class _A1, class _A2>
228struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
229 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230{
231};
232
233template<class _Fp> class __base;
234
Howard Hinnant99968442011-11-29 18:15:50 +0000235template<class _Rp>
236class __base<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237{
238 __base(const __base&);
239 __base& operator=(const __base&);
240public:
241 __base() {}
242 virtual ~__base() {}
243 virtual __base* __clone() const = 0;
244 virtual void __clone(__base*) const = 0;
245 virtual void destroy() = 0;
246 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000247 virtual _Rp operator()() = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000248#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249 virtual const void* target(const type_info&) const = 0;
250 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000251#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252};
253
Howard Hinnant99968442011-11-29 18:15:50 +0000254template<class _Rp, class _A0>
255class __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256{
257 __base(const __base&);
258 __base& operator=(const __base&);
259public:
260 __base() {}
261 virtual ~__base() {}
262 virtual __base* __clone() const = 0;
263 virtual void __clone(__base*) const = 0;
264 virtual void destroy() = 0;
265 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000266 virtual _Rp operator()(_A0) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000267#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268 virtual const void* target(const type_info&) const = 0;
269 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000270#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271};
272
Howard Hinnant99968442011-11-29 18:15:50 +0000273template<class _Rp, class _A0, class _A1>
274class __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275{
276 __base(const __base&);
277 __base& operator=(const __base&);
278public:
279 __base() {}
280 virtual ~__base() {}
281 virtual __base* __clone() const = 0;
282 virtual void __clone(__base*) const = 0;
283 virtual void destroy() = 0;
284 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000285 virtual _Rp operator()(_A0, _A1) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000286#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287 virtual const void* target(const type_info&) const = 0;
288 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000289#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290};
291
Howard Hinnant99968442011-11-29 18:15:50 +0000292template<class _Rp, class _A0, class _A1, class _A2>
293class __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294{
295 __base(const __base&);
296 __base& operator=(const __base&);
297public:
298 __base() {}
299 virtual ~__base() {}
300 virtual __base* __clone() const = 0;
301 virtual void __clone(__base*) const = 0;
302 virtual void destroy() = 0;
303 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000304 virtual _Rp operator()(_A0, _A1, _A2) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000305#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 virtual const void* target(const type_info&) const = 0;
307 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000308#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309};
310
311template<class _FD, class _Alloc, class _FB> class __func;
312
Howard Hinnant99968442011-11-29 18:15:50 +0000313template<class _Fp, class _Alloc, class _Rp>
314class __func<_Fp, _Alloc, _Rp()>
315 : public __base<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316{
Howard Hinnant99968442011-11-29 18:15:50 +0000317 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000318public:
Howard Hinnant99968442011-11-29 18:15:50 +0000319 explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
320 explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
321 virtual __base<_Rp()>* __clone() const;
322 virtual void __clone(__base<_Rp()>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000323 virtual void destroy();
324 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000325 virtual _Rp operator()();
Howard Hinnantd4444702010-08-11 17:04:31 +0000326#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327 virtual const void* target(const type_info&) const;
328 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000329#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330};
331
Howard Hinnant99968442011-11-29 18:15:50 +0000332template<class _Fp, class _Alloc, class _Rp>
333__base<_Rp()>*
334__func<_Fp, _Alloc, _Rp()>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335{
Howard Hinnant99968442011-11-29 18:15:50 +0000336 typedef typename _Alloc::template rebind<__func>::other _Ap;
337 _Ap __a(__f_.second());
338 typedef __allocator_destructor<_Ap> _Dp;
339 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000340 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
341 return __hold.release();
342}
343
Howard Hinnant99968442011-11-29 18:15:50 +0000344template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345void
Howard Hinnant99968442011-11-29 18:15:50 +0000346__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347{
348 ::new (__p) __func(__f_.first(), __f_.second());
349}
350
Howard Hinnant99968442011-11-29 18:15:50 +0000351template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352void
Howard Hinnant99968442011-11-29 18:15:50 +0000353__func<_Fp, _Alloc, _Rp()>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354{
Howard Hinnant99968442011-11-29 18:15:50 +0000355 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356}
357
Howard Hinnant99968442011-11-29 18:15:50 +0000358template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000359void
Howard Hinnant99968442011-11-29 18:15:50 +0000360__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361{
Howard Hinnant99968442011-11-29 18:15:50 +0000362 typedef typename _Alloc::template rebind<__func>::other _Ap;
363 _Ap __a(__f_.second());
364 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365 __a.deallocate(this, 1);
366}
367
Howard Hinnant99968442011-11-29 18:15:50 +0000368template<class _Fp, class _Alloc, class _Rp>
369_Rp
370__func<_Fp, _Alloc, _Rp()>::operator()()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000372 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
373 return _Invoker::__call(__f_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374}
375
Howard Hinnantd4444702010-08-11 17:04:31 +0000376#ifndef _LIBCPP_NO_RTTI
377
Howard Hinnant99968442011-11-29 18:15:50 +0000378template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000380__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381{
Howard Hinnant99968442011-11-29 18:15:50 +0000382 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 return &__f_.first();
384 return (const void*)0;
385}
386
Howard Hinnant99968442011-11-29 18:15:50 +0000387template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000389__func<_Fp, _Alloc, _Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390{
Howard Hinnant99968442011-11-29 18:15:50 +0000391 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392}
393
Howard Hinnant324bb032010-08-22 00:02:43 +0000394#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000395
Howard Hinnant99968442011-11-29 18:15:50 +0000396template<class _Fp, class _Alloc, class _Rp, class _A0>
397class __func<_Fp, _Alloc, _Rp(_A0)>
398 : public __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399{
Howard Hinnant99968442011-11-29 18:15:50 +0000400 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401public:
Howard Hinnant99968442011-11-29 18:15:50 +0000402 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
403 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000404 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000405 virtual __base<_Rp(_A0)>* __clone() const;
406 virtual void __clone(__base<_Rp(_A0)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407 virtual void destroy();
408 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000409 virtual _Rp operator()(_A0);
Howard Hinnantd4444702010-08-11 17:04:31 +0000410#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 virtual const void* target(const type_info&) const;
412 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000413#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414};
415
Howard Hinnant99968442011-11-29 18:15:50 +0000416template<class _Fp, class _Alloc, class _Rp, class _A0>
417__base<_Rp(_A0)>*
418__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419{
Howard Hinnant99968442011-11-29 18:15:50 +0000420 typedef typename _Alloc::template rebind<__func>::other _Ap;
421 _Ap __a(__f_.second());
422 typedef __allocator_destructor<_Ap> _Dp;
423 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
425 return __hold.release();
426}
427
Howard Hinnant99968442011-11-29 18:15:50 +0000428template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429void
Howard Hinnant99968442011-11-29 18:15:50 +0000430__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000431{
432 ::new (__p) __func(__f_.first(), __f_.second());
433}
434
Howard Hinnant99968442011-11-29 18:15:50 +0000435template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436void
Howard Hinnant99968442011-11-29 18:15:50 +0000437__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438{
Howard Hinnant99968442011-11-29 18:15:50 +0000439 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440}
441
Howard Hinnant99968442011-11-29 18:15:50 +0000442template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443void
Howard Hinnant99968442011-11-29 18:15:50 +0000444__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445{
Howard Hinnant99968442011-11-29 18:15:50 +0000446 typedef typename _Alloc::template rebind<__func>::other _Ap;
447 _Ap __a(__f_.second());
448 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449 __a.deallocate(this, 1);
450}
451
Howard Hinnant99968442011-11-29 18:15:50 +0000452template<class _Fp, class _Alloc, class _Rp, class _A0>
453_Rp
454__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000456 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
457 return _Invoker::__call(__f_.first(), __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458}
459
Howard Hinnantd4444702010-08-11 17:04:31 +0000460#ifndef _LIBCPP_NO_RTTI
461
Howard Hinnant99968442011-11-29 18:15:50 +0000462template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000464__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465{
Howard Hinnant99968442011-11-29 18:15:50 +0000466 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 return &__f_.first();
468 return (const void*)0;
469}
470
Howard Hinnant99968442011-11-29 18:15:50 +0000471template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000473__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474{
Howard Hinnant99968442011-11-29 18:15:50 +0000475 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476}
477
Howard Hinnant324bb032010-08-22 00:02:43 +0000478#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000479
Howard Hinnant99968442011-11-29 18:15:50 +0000480template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
481class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
482 : public __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483{
Howard Hinnant99968442011-11-29 18:15:50 +0000484 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485public:
Howard Hinnant99968442011-11-29 18:15:50 +0000486 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
487 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000488 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000489 virtual __base<_Rp(_A0, _A1)>* __clone() const;
490 virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491 virtual void destroy();
492 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000493 virtual _Rp operator()(_A0, _A1);
Howard Hinnantd4444702010-08-11 17:04:31 +0000494#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495 virtual const void* target(const type_info&) const;
496 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000497#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498};
499
Howard Hinnant99968442011-11-29 18:15:50 +0000500template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
501__base<_Rp(_A0, _A1)>*
502__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503{
Howard Hinnant99968442011-11-29 18:15:50 +0000504 typedef typename _Alloc::template rebind<__func>::other _Ap;
505 _Ap __a(__f_.second());
506 typedef __allocator_destructor<_Ap> _Dp;
507 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
509 return __hold.release();
510}
511
Howard Hinnant99968442011-11-29 18:15:50 +0000512template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513void
Howard Hinnant99968442011-11-29 18:15:50 +0000514__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515{
516 ::new (__p) __func(__f_.first(), __f_.second());
517}
518
Howard Hinnant99968442011-11-29 18:15:50 +0000519template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520void
Howard Hinnant99968442011-11-29 18:15:50 +0000521__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522{
Howard Hinnant99968442011-11-29 18:15:50 +0000523 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524}
525
Howard Hinnant99968442011-11-29 18:15:50 +0000526template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527void
Howard Hinnant99968442011-11-29 18:15:50 +0000528__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529{
Howard Hinnant99968442011-11-29 18:15:50 +0000530 typedef typename _Alloc::template rebind<__func>::other _Ap;
531 _Ap __a(__f_.second());
532 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533 __a.deallocate(this, 1);
534}
535
Howard Hinnant99968442011-11-29 18:15:50 +0000536template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
537_Rp
538__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000540 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
541 return _Invoker::__call(__f_.first(), __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542}
543
Howard Hinnantd4444702010-08-11 17:04:31 +0000544#ifndef _LIBCPP_NO_RTTI
545
Howard Hinnant99968442011-11-29 18:15:50 +0000546template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000548__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549{
Howard Hinnant99968442011-11-29 18:15:50 +0000550 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000551 return &__f_.first();
552 return (const void*)0;
553}
554
Howard Hinnant99968442011-11-29 18:15:50 +0000555template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000557__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558{
Howard Hinnant99968442011-11-29 18:15:50 +0000559 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560}
561
Howard Hinnant324bb032010-08-22 00:02:43 +0000562#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000563
Howard Hinnant99968442011-11-29 18:15:50 +0000564template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
565class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
566 : public __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567{
Howard Hinnant99968442011-11-29 18:15:50 +0000568 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569public:
Howard Hinnant99968442011-11-29 18:15:50 +0000570 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
571 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000572 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000573 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
574 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575 virtual void destroy();
576 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000577 virtual _Rp operator()(_A0, _A1, _A2);
Howard Hinnantd4444702010-08-11 17:04:31 +0000578#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579 virtual const void* target(const type_info&) const;
580 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000581#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582};
583
Howard Hinnant99968442011-11-29 18:15:50 +0000584template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
585__base<_Rp(_A0, _A1, _A2)>*
586__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587{
Howard Hinnant99968442011-11-29 18:15:50 +0000588 typedef typename _Alloc::template rebind<__func>::other _Ap;
589 _Ap __a(__f_.second());
590 typedef __allocator_destructor<_Ap> _Dp;
591 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
593 return __hold.release();
594}
595
Howard Hinnant99968442011-11-29 18:15:50 +0000596template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597void
Howard Hinnant99968442011-11-29 18:15:50 +0000598__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599{
600 ::new (__p) __func(__f_.first(), __f_.second());
601}
602
Howard Hinnant99968442011-11-29 18:15:50 +0000603template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604void
Howard Hinnant99968442011-11-29 18:15:50 +0000605__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606{
Howard Hinnant99968442011-11-29 18:15:50 +0000607 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608}
609
Howard Hinnant99968442011-11-29 18:15:50 +0000610template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611void
Howard Hinnant99968442011-11-29 18:15:50 +0000612__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613{
Howard Hinnant99968442011-11-29 18:15:50 +0000614 typedef typename _Alloc::template rebind<__func>::other _Ap;
615 _Ap __a(__f_.second());
616 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617 __a.deallocate(this, 1);
618}
619
Howard Hinnant99968442011-11-29 18:15:50 +0000620template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
621_Rp
622__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000624 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
625 return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626}
627
Howard Hinnantd4444702010-08-11 17:04:31 +0000628#ifndef _LIBCPP_NO_RTTI
629
Howard Hinnant99968442011-11-29 18:15:50 +0000630template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000632__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633{
Howard Hinnant99968442011-11-29 18:15:50 +0000634 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635 return &__f_.first();
636 return (const void*)0;
637}
638
Howard Hinnant99968442011-11-29 18:15:50 +0000639template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000641__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642{
Howard Hinnant99968442011-11-29 18:15:50 +0000643 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644}
645
Howard Hinnant324bb032010-08-22 00:02:43 +0000646#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000647
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648} // __function
649
Howard Hinnant99968442011-11-29 18:15:50 +0000650template<class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000651class _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652{
Howard Hinnant99968442011-11-29 18:15:50 +0000653 typedef __function::__base<_Rp()> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 aligned_storage<3*sizeof(void*)>::type __buf_;
655 __base* __f_;
656
Howard Hinnant99968442011-11-29 18:15:50 +0000657 template <class _Fp>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000659 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660 template <class _R2>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000661 _LIBCPP_INLINE_VISIBILITY
662 static bool __not_null(_R2 (*__p)()) {return __p;}
663 template <class _R2>
664 _LIBCPP_INLINE_VISIBILITY
665 static bool __not_null(const function<_R2()>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666public:
Howard Hinnant99968442011-11-29 18:15:50 +0000667 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668
669 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000670 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
671 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000673 template<class _Fp>
674 function(_Fp,
675 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
Howard Hinnant72552802010-08-20 19:36:46 +0000677 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000679 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
680 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000682 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
683 template<class _Alloc>
684 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000685 template<class _Fp, class _Alloc>
686 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
687 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688
689 function& operator=(const function&);
690 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000691 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692 typename enable_if
693 <
Howard Hinnant99968442011-11-29 18:15:50 +0000694 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695 function&
696 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000697 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698
699 ~function();
700
701 // 20.7.16.2.2, function modifiers:
702 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000703 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000705 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +0000706 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707
708 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +0000709 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710
711private:
712 // deleted overloads close possible hole in the type system
713 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000714 bool operator==(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000716 bool operator!=(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717public:
718 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +0000719 _Rp operator()() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720
Howard Hinnantd4444702010-08-11 17:04:31 +0000721#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722 // 20.7.16.2.5, function target access:
723 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +0000724 template <typename _Tp> _Tp* target();
725 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000726#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727};
728
Howard Hinnant99968442011-11-29 18:15:50 +0000729template<class _Rp>
730function<_Rp()>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731{
732 if (__f.__f_ == 0)
733 __f_ = 0;
734 else if (__f.__f_ == (const __base*)&__f.__buf_)
735 {
736 __f_ = (__base*)&__buf_;
737 __f.__f_->__clone(__f_);
738 }
739 else
740 __f_ = __f.__f_->__clone();
741}
742
Howard Hinnant99968442011-11-29 18:15:50 +0000743template<class _Rp>
Howard Hinnant72552802010-08-20 19:36:46 +0000744template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +0000745function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +0000746{
747 if (__f.__f_ == 0)
748 __f_ = 0;
749 else if (__f.__f_ == (const __base*)&__f.__buf_)
750 {
751 __f_ = (__base*)&__buf_;
752 __f.__f_->__clone(__f_);
753 }
754 else
755 __f_ = __f.__f_->__clone();
756}
757
Howard Hinnant99968442011-11-29 18:15:50 +0000758template<class _Rp>
759template <class _Fp>
760function<_Rp()>::function(_Fp __f,
761 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000762 : __f_(0)
763{
764 if (__not_null(__f))
765 {
Howard Hinnant99968442011-11-29 18:15:50 +0000766 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767 if (sizeof(_FF) <= sizeof(__buf_))
768 {
769 __f_ = (__base*)&__buf_;
770 ::new (__f_) _FF(__f);
771 }
772 else
773 {
Howard Hinnant99968442011-11-29 18:15:50 +0000774 typedef allocator<_FF> _Ap;
775 _Ap __a;
776 typedef __allocator_destructor<_Ap> _Dp;
777 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
778 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 __f_ = __hold.release();
780 }
781 }
782}
783
Howard Hinnant99968442011-11-29 18:15:50 +0000784template<class _Rp>
785template <class _Fp, class _Alloc>
786function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
787 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +0000788 : __f_(0)
789{
790 typedef allocator_traits<_Alloc> __alloc_traits;
791 if (__not_null(__f))
792 {
Howard Hinnant99968442011-11-29 18:15:50 +0000793 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +0000794 if (sizeof(_FF) <= sizeof(__buf_))
795 {
796 __f_ = (__base*)&__buf_;
797 ::new (__f_) _FF(__f);
798 }
799 else
800 {
Marshall Clow66302c62015-04-07 05:21:38 +0000801 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000802 _Ap __a(__a0);
803 typedef __allocator_destructor<_Ap> _Dp;
804 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +0000805 ::new (__hold.get()) _FF(__f, _Alloc(__a));
806 __f_ = __hold.release();
807 }
808 }
809}
810
Howard Hinnant99968442011-11-29 18:15:50 +0000811template<class _Rp>
812function<_Rp()>&
813function<_Rp()>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814{
815 function(__f).swap(*this);
816 return *this;
817}
818
Howard Hinnant99968442011-11-29 18:15:50 +0000819template<class _Rp>
820function<_Rp()>&
821function<_Rp()>::operator=(nullptr_t)
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 __f_ = 0;
828}
829
Howard Hinnant99968442011-11-29 18:15:50 +0000830template<class _Rp>
831template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000832typename enable_if
833<
Howard Hinnant99968442011-11-29 18:15:50 +0000834 !is_integral<_Fp>::value,
835 function<_Rp()>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836>::type
Howard Hinnant99968442011-11-29 18:15:50 +0000837function<_Rp()>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000839 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000840 return *this;
841}
842
Howard Hinnant99968442011-11-29 18:15:50 +0000843template<class _Rp>
844function<_Rp()>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000845{
846 if (__f_ == (__base*)&__buf_)
847 __f_->destroy();
848 else if (__f_)
849 __f_->destroy_deallocate();
850}
851
Howard Hinnant99968442011-11-29 18:15:50 +0000852template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853void
Howard Hinnant99968442011-11-29 18:15:50 +0000854function<_Rp()>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000855{
856 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
857 {
858 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
859 __base* __t = (__base*)&__tempbuf;
860 __f_->__clone(__t);
861 __f_->destroy();
862 __f_ = 0;
863 __f.__f_->__clone((__base*)&__buf_);
864 __f.__f_->destroy();
865 __f.__f_ = 0;
866 __f_ = (__base*)&__buf_;
867 __t->__clone((__base*)&__f.__buf_);
868 __t->destroy();
869 __f.__f_ = (__base*)&__f.__buf_;
870 }
871 else if (__f_ == (__base*)&__buf_)
872 {
873 __f_->__clone((__base*)&__f.__buf_);
874 __f_->destroy();
875 __f_ = __f.__f_;
876 __f.__f_ = (__base*)&__f.__buf_;
877 }
878 else if (__f.__f_ == (__base*)&__f.__buf_)
879 {
880 __f.__f_->__clone((__base*)&__buf_);
881 __f.__f_->destroy();
882 __f.__f_ = __f_;
883 __f_ = (__base*)&__buf_;
884 }
885 else
Howard Hinnant0949eed2011-06-30 21:18:19 +0000886 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887}
888
Howard Hinnant99968442011-11-29 18:15:50 +0000889template<class _Rp>
890_Rp
891function<_Rp()>::operator()() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892{
Howard Hinnantd4444702010-08-11 17:04:31 +0000893#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894 if (__f_ == 0)
895 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +0000896#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000897 return (*__f_)();
898}
899
Howard Hinnantd4444702010-08-11 17:04:31 +0000900#ifndef _LIBCPP_NO_RTTI
901
Howard Hinnant99968442011-11-29 18:15:50 +0000902template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000904function<_Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905{
906 if (__f_ == 0)
907 return typeid(void);
908 return __f_->target_type();
909}
910
Howard Hinnant99968442011-11-29 18:15:50 +0000911template<class _Rp>
912template <typename _Tp>
913_Tp*
914function<_Rp()>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915{
916 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000917 return (_Tp*)0;
918 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919}
920
Howard Hinnant99968442011-11-29 18:15:50 +0000921template<class _Rp>
922template <typename _Tp>
923const _Tp*
924function<_Rp()>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925{
926 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000927 return (const _Tp*)0;
928 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929}
930
Howard Hinnant324bb032010-08-22 00:02:43 +0000931#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000932
Howard Hinnant99968442011-11-29 18:15:50 +0000933template<class _Rp, class _A0>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000934class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
Howard Hinnant99968442011-11-29 18:15:50 +0000935 : public unary_function<_A0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936{
Howard Hinnant99968442011-11-29 18:15:50 +0000937 typedef __function::__base<_Rp(_A0)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938 aligned_storage<3*sizeof(void*)>::type __buf_;
939 __base* __f_;
940
Howard Hinnant99968442011-11-29 18:15:50 +0000941 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000943 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +0000947 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000949 static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
950 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000952 static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
953 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000955 static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
956 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000958 static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000960 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +0000961 static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962public:
Howard Hinnant99968442011-11-29 18:15:50 +0000963 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964
965 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000966 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
967 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000969 template<class _Fp>
970 function(_Fp,
971 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972
Howard Hinnant72552802010-08-20 19:36:46 +0000973 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000975 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
976 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000978 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
979 template<class _Alloc>
980 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000981 template<class _Fp, class _Alloc>
982 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
983 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984
985 function& operator=(const function&);
986 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000987 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 typename enable_if
989 <
Howard Hinnant99968442011-11-29 18:15:50 +0000990 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991 function&
992 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000993 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994
995 ~function();
996
997 // 20.7.16.2.2, function modifiers:
998 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000999 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001001 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001002 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003
1004 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001005 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006
1007private:
1008 // deleted overloads close possible hole in the type system
1009 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001010 bool operator==(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001011 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001012 bool operator!=(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013public:
1014 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001015 _Rp operator()(_A0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016
Howard Hinnantd4444702010-08-11 17:04:31 +00001017#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018 // 20.7.16.2.5, function target access:
1019 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001020 template <typename _Tp> _Tp* target();
1021 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001022#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023};
1024
Howard Hinnant99968442011-11-29 18:15:50 +00001025template<class _Rp, class _A0>
1026function<_Rp(_A0)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027{
1028 if (__f.__f_ == 0)
1029 __f_ = 0;
1030 else if (__f.__f_ == (const __base*)&__f.__buf_)
1031 {
1032 __f_ = (__base*)&__buf_;
1033 __f.__f_->__clone(__f_);
1034 }
1035 else
1036 __f_ = __f.__f_->__clone();
1037}
1038
Howard Hinnant99968442011-11-29 18:15:50 +00001039template<class _Rp, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001040template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001041function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001042{
1043 if (__f.__f_ == 0)
1044 __f_ = 0;
1045 else if (__f.__f_ == (const __base*)&__f.__buf_)
1046 {
1047 __f_ = (__base*)&__buf_;
1048 __f.__f_->__clone(__f_);
1049 }
1050 else
1051 __f_ = __f.__f_->__clone();
1052}
1053
Howard Hinnant99968442011-11-29 18:15:50 +00001054template<class _Rp, class _A0>
1055template <class _Fp>
1056function<_Rp(_A0)>::function(_Fp __f,
1057 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058 : __f_(0)
1059{
1060 if (__not_null(__f))
1061 {
Howard Hinnant99968442011-11-29 18:15:50 +00001062 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 if (sizeof(_FF) <= sizeof(__buf_))
1064 {
1065 __f_ = (__base*)&__buf_;
1066 ::new (__f_) _FF(__f);
1067 }
1068 else
1069 {
Howard Hinnant99968442011-11-29 18:15:50 +00001070 typedef allocator<_FF> _Ap;
1071 _Ap __a;
1072 typedef __allocator_destructor<_Ap> _Dp;
1073 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1074 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075 __f_ = __hold.release();
1076 }
1077 }
1078}
1079
Howard Hinnant99968442011-11-29 18:15:50 +00001080template<class _Rp, class _A0>
1081template <class _Fp, class _Alloc>
1082function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1083 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001084 : __f_(0)
1085{
1086 typedef allocator_traits<_Alloc> __alloc_traits;
1087 if (__not_null(__f))
1088 {
Howard Hinnant99968442011-11-29 18:15:50 +00001089 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001090 if (sizeof(_FF) <= sizeof(__buf_))
1091 {
1092 __f_ = (__base*)&__buf_;
1093 ::new (__f_) _FF(__f);
1094 }
1095 else
1096 {
Marshall Clow66302c62015-04-07 05:21:38 +00001097 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001098 _Ap __a(__a0);
1099 typedef __allocator_destructor<_Ap> _Dp;
1100 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001101 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1102 __f_ = __hold.release();
1103 }
1104 }
1105}
1106
Howard Hinnant99968442011-11-29 18:15:50 +00001107template<class _Rp, class _A0>
1108function<_Rp(_A0)>&
1109function<_Rp(_A0)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110{
1111 function(__f).swap(*this);
1112 return *this;
1113}
1114
Howard Hinnant99968442011-11-29 18:15:50 +00001115template<class _Rp, class _A0>
1116function<_Rp(_A0)>&
1117function<_Rp(_A0)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118{
1119 if (__f_ == (__base*)&__buf_)
1120 __f_->destroy();
1121 else if (__f_)
1122 __f_->destroy_deallocate();
1123 __f_ = 0;
1124}
1125
Howard Hinnant99968442011-11-29 18:15:50 +00001126template<class _Rp, class _A0>
1127template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001128typename enable_if
1129<
Howard Hinnant99968442011-11-29 18:15:50 +00001130 !is_integral<_Fp>::value,
1131 function<_Rp(_A0)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001132>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001133function<_Rp(_A0)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001135 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136 return *this;
1137}
1138
Howard Hinnant99968442011-11-29 18:15:50 +00001139template<class _Rp, class _A0>
1140function<_Rp(_A0)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001141{
1142 if (__f_ == (__base*)&__buf_)
1143 __f_->destroy();
1144 else if (__f_)
1145 __f_->destroy_deallocate();
1146}
1147
Howard Hinnant99968442011-11-29 18:15:50 +00001148template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149void
Howard Hinnant99968442011-11-29 18:15:50 +00001150function<_Rp(_A0)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151{
1152 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1153 {
1154 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1155 __base* __t = (__base*)&__tempbuf;
1156 __f_->__clone(__t);
1157 __f_->destroy();
1158 __f_ = 0;
1159 __f.__f_->__clone((__base*)&__buf_);
1160 __f.__f_->destroy();
1161 __f.__f_ = 0;
1162 __f_ = (__base*)&__buf_;
1163 __t->__clone((__base*)&__f.__buf_);
1164 __t->destroy();
1165 __f.__f_ = (__base*)&__f.__buf_;
1166 }
1167 else if (__f_ == (__base*)&__buf_)
1168 {
1169 __f_->__clone((__base*)&__f.__buf_);
1170 __f_->destroy();
1171 __f_ = __f.__f_;
1172 __f.__f_ = (__base*)&__f.__buf_;
1173 }
1174 else if (__f.__f_ == (__base*)&__f.__buf_)
1175 {
1176 __f.__f_->__clone((__base*)&__buf_);
1177 __f.__f_->destroy();
1178 __f.__f_ = __f_;
1179 __f_ = (__base*)&__buf_;
1180 }
1181 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001182 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183}
1184
Howard Hinnant99968442011-11-29 18:15:50 +00001185template<class _Rp, class _A0>
1186_Rp
1187function<_Rp(_A0)>::operator()(_A0 __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188{
Howard Hinnantd4444702010-08-11 17:04:31 +00001189#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190 if (__f_ == 0)
1191 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001192#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 return (*__f_)(__a0);
1194}
1195
Howard Hinnantd4444702010-08-11 17:04:31 +00001196#ifndef _LIBCPP_NO_RTTI
1197
Howard Hinnant99968442011-11-29 18:15:50 +00001198template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001200function<_Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001201{
1202 if (__f_ == 0)
1203 return typeid(void);
1204 return __f_->target_type();
1205}
1206
Howard Hinnant99968442011-11-29 18:15:50 +00001207template<class _Rp, class _A0>
1208template <typename _Tp>
1209_Tp*
1210function<_Rp(_A0)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001211{
1212 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001213 return (_Tp*)0;
1214 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001215}
1216
Howard Hinnant99968442011-11-29 18:15:50 +00001217template<class _Rp, class _A0>
1218template <typename _Tp>
1219const _Tp*
1220function<_Rp(_A0)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221{
1222 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001223 return (const _Tp*)0;
1224 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001225}
1226
Howard Hinnant324bb032010-08-22 00:02:43 +00001227#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001228
Howard Hinnant99968442011-11-29 18:15:50 +00001229template<class _Rp, class _A0, class _A1>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001230class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
Howard Hinnant99968442011-11-29 18:15:50 +00001231 : public binary_function<_A0, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001232{
Howard Hinnant99968442011-11-29 18:15:50 +00001233 typedef __function::__base<_Rp(_A0, _A1)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234 aligned_storage<3*sizeof(void*)>::type __buf_;
1235 __base* __f_;
1236
Howard Hinnant99968442011-11-29 18:15:50 +00001237 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001239 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001243 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001245 static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1246 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001248 static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1249 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001251 static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1252 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001254 static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001256 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001257 static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258public:
Howard Hinnant99968442011-11-29 18:15:50 +00001259 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260
1261 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001262 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1263 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001265 template<class _Fp>
1266 function(_Fp,
1267 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268
Howard Hinnant72552802010-08-20 19:36:46 +00001269 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001271 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1272 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001274 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1275 template<class _Alloc>
1276 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001277 template<class _Fp, class _Alloc>
1278 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1279 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280
1281 function& operator=(const function&);
1282 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001283 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 typename enable_if
1285 <
Howard Hinnant99968442011-11-29 18:15:50 +00001286 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287 function&
1288 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001289 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290
1291 ~function();
1292
1293 // 20.7.16.2.2, function modifiers:
1294 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001295 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001297 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001298 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299
1300 // 20.7.16.2.3, function capacity:
1301 operator bool() const {return __f_;}
1302
1303private:
1304 // deleted overloads close possible hole in the type system
1305 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001306 bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001308 bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309public:
1310 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001311 _Rp operator()(_A0, _A1) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312
Howard Hinnantd4444702010-08-11 17:04:31 +00001313#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314 // 20.7.16.2.5, function target access:
1315 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001316 template <typename _Tp> _Tp* target();
1317 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001318#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001319};
1320
Howard Hinnant99968442011-11-29 18:15:50 +00001321template<class _Rp, class _A0, class _A1>
1322function<_Rp(_A0, _A1)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001323{
1324 if (__f.__f_ == 0)
1325 __f_ = 0;
1326 else if (__f.__f_ == (const __base*)&__f.__buf_)
1327 {
1328 __f_ = (__base*)&__buf_;
1329 __f.__f_->__clone(__f_);
1330 }
1331 else
1332 __f_ = __f.__f_->__clone();
1333}
1334
Howard Hinnant99968442011-11-29 18:15:50 +00001335template<class _Rp, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001336template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001337function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001338{
1339 if (__f.__f_ == 0)
1340 __f_ = 0;
1341 else if (__f.__f_ == (const __base*)&__f.__buf_)
1342 {
1343 __f_ = (__base*)&__buf_;
1344 __f.__f_->__clone(__f_);
1345 }
1346 else
1347 __f_ = __f.__f_->__clone();
1348}
1349
Howard Hinnant99968442011-11-29 18:15:50 +00001350template<class _Rp, class _A0, class _A1>
1351template <class _Fp>
1352function<_Rp(_A0, _A1)>::function(_Fp __f,
1353 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354 : __f_(0)
1355{
1356 if (__not_null(__f))
1357 {
Howard Hinnant99968442011-11-29 18:15:50 +00001358 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359 if (sizeof(_FF) <= sizeof(__buf_))
1360 {
1361 __f_ = (__base*)&__buf_;
1362 ::new (__f_) _FF(__f);
1363 }
1364 else
1365 {
Howard Hinnant99968442011-11-29 18:15:50 +00001366 typedef allocator<_FF> _Ap;
1367 _Ap __a;
1368 typedef __allocator_destructor<_Ap> _Dp;
1369 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1370 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371 __f_ = __hold.release();
1372 }
1373 }
1374}
1375
Howard Hinnant99968442011-11-29 18:15:50 +00001376template<class _Rp, class _A0, class _A1>
1377template <class _Fp, class _Alloc>
1378function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1379 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001380 : __f_(0)
1381{
1382 typedef allocator_traits<_Alloc> __alloc_traits;
1383 if (__not_null(__f))
1384 {
Howard Hinnant99968442011-11-29 18:15:50 +00001385 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001386 if (sizeof(_FF) <= sizeof(__buf_))
1387 {
1388 __f_ = (__base*)&__buf_;
1389 ::new (__f_) _FF(__f);
1390 }
1391 else
1392 {
Marshall Clow66302c62015-04-07 05:21:38 +00001393 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001394 _Ap __a(__a0);
1395 typedef __allocator_destructor<_Ap> _Dp;
1396 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001397 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1398 __f_ = __hold.release();
1399 }
1400 }
1401}
1402
Howard Hinnant99968442011-11-29 18:15:50 +00001403template<class _Rp, class _A0, class _A1>
1404function<_Rp(_A0, _A1)>&
1405function<_Rp(_A0, _A1)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001406{
1407 function(__f).swap(*this);
1408 return *this;
1409}
1410
Howard Hinnant99968442011-11-29 18:15:50 +00001411template<class _Rp, class _A0, class _A1>
1412function<_Rp(_A0, _A1)>&
1413function<_Rp(_A0, _A1)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414{
1415 if (__f_ == (__base*)&__buf_)
1416 __f_->destroy();
1417 else if (__f_)
1418 __f_->destroy_deallocate();
1419 __f_ = 0;
1420}
1421
Howard Hinnant99968442011-11-29 18:15:50 +00001422template<class _Rp, class _A0, class _A1>
1423template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424typename enable_if
1425<
Howard Hinnant99968442011-11-29 18:15:50 +00001426 !is_integral<_Fp>::value,
1427 function<_Rp(_A0, _A1)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001429function<_Rp(_A0, _A1)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001431 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 return *this;
1433}
1434
Howard Hinnant99968442011-11-29 18:15:50 +00001435template<class _Rp, class _A0, class _A1>
1436function<_Rp(_A0, _A1)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001437{
1438 if (__f_ == (__base*)&__buf_)
1439 __f_->destroy();
1440 else if (__f_)
1441 __f_->destroy_deallocate();
1442}
1443
Howard Hinnant99968442011-11-29 18:15:50 +00001444template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445void
Howard Hinnant99968442011-11-29 18:15:50 +00001446function<_Rp(_A0, _A1)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447{
1448 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1449 {
1450 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1451 __base* __t = (__base*)&__tempbuf;
1452 __f_->__clone(__t);
1453 __f_->destroy();
1454 __f_ = 0;
1455 __f.__f_->__clone((__base*)&__buf_);
1456 __f.__f_->destroy();
1457 __f.__f_ = 0;
1458 __f_ = (__base*)&__buf_;
1459 __t->__clone((__base*)&__f.__buf_);
1460 __t->destroy();
1461 __f.__f_ = (__base*)&__f.__buf_;
1462 }
1463 else if (__f_ == (__base*)&__buf_)
1464 {
1465 __f_->__clone((__base*)&__f.__buf_);
1466 __f_->destroy();
1467 __f_ = __f.__f_;
1468 __f.__f_ = (__base*)&__f.__buf_;
1469 }
1470 else if (__f.__f_ == (__base*)&__f.__buf_)
1471 {
1472 __f.__f_->__clone((__base*)&__buf_);
1473 __f.__f_->destroy();
1474 __f.__f_ = __f_;
1475 __f_ = (__base*)&__buf_;
1476 }
1477 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001478 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479}
1480
Howard Hinnant99968442011-11-29 18:15:50 +00001481template<class _Rp, class _A0, class _A1>
1482_Rp
1483function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484{
Howard Hinnantd4444702010-08-11 17:04:31 +00001485#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 if (__f_ == 0)
1487 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001488#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489 return (*__f_)(__a0, __a1);
1490}
1491
Howard Hinnantd4444702010-08-11 17:04:31 +00001492#ifndef _LIBCPP_NO_RTTI
1493
Howard Hinnant99968442011-11-29 18:15:50 +00001494template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001496function<_Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497{
1498 if (__f_ == 0)
1499 return typeid(void);
1500 return __f_->target_type();
1501}
1502
Howard Hinnant99968442011-11-29 18:15:50 +00001503template<class _Rp, class _A0, class _A1>
1504template <typename _Tp>
1505_Tp*
1506function<_Rp(_A0, _A1)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507{
1508 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001509 return (_Tp*)0;
1510 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511}
1512
Howard Hinnant99968442011-11-29 18:15:50 +00001513template<class _Rp, class _A0, class _A1>
1514template <typename _Tp>
1515const _Tp*
1516function<_Rp(_A0, _A1)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517{
1518 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001519 return (const _Tp*)0;
1520 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521}
1522
Howard Hinnant324bb032010-08-22 00:02:43 +00001523#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001524
Howard Hinnant99968442011-11-29 18:15:50 +00001525template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001526class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527{
Howard Hinnant99968442011-11-29 18:15:50 +00001528 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529 aligned_storage<3*sizeof(void*)>::type __buf_;
1530 __base* __f_;
1531
Howard Hinnant99968442011-11-29 18:15:50 +00001532 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001534 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001538 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001540 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1541 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001543 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1544 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001546 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1547 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001549 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001551 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001552 static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553public:
Howard Hinnant99968442011-11-29 18:15:50 +00001554 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555
1556 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001557 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1558 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001560 template<class _Fp>
1561 function(_Fp,
1562 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563
Howard Hinnant72552802010-08-20 19:36:46 +00001564 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001566 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1567 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001569 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1570 template<class _Alloc>
1571 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001572 template<class _Fp, class _Alloc>
1573 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1574 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001575
1576 function& operator=(const function&);
1577 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001578 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579 typename enable_if
1580 <
Howard Hinnant99968442011-11-29 18:15:50 +00001581 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582 function&
1583 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001584 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585
1586 ~function();
1587
1588 // 20.7.16.2.2, function modifiers:
1589 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001590 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001592 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001593 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594
1595 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001596 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597
1598private:
1599 // deleted overloads close possible hole in the type system
1600 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001601 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001603 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604public:
1605 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001606 _Rp operator()(_A0, _A1, _A2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607
Howard Hinnantd4444702010-08-11 17:04:31 +00001608#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 // 20.7.16.2.5, function target access:
1610 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001611 template <typename _Tp> _Tp* target();
1612 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001613#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614};
1615
Howard Hinnant99968442011-11-29 18:15:50 +00001616template<class _Rp, class _A0, class _A1, class _A2>
1617function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618{
1619 if (__f.__f_ == 0)
1620 __f_ = 0;
1621 else if (__f.__f_ == (const __base*)&__f.__buf_)
1622 {
1623 __f_ = (__base*)&__buf_;
1624 __f.__f_->__clone(__f_);
1625 }
1626 else
1627 __f_ = __f.__f_->__clone();
1628}
1629
Howard Hinnant99968442011-11-29 18:15:50 +00001630template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001631template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001632function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnant72552802010-08-20 19:36:46 +00001633 const function& __f)
1634{
1635 if (__f.__f_ == 0)
1636 __f_ = 0;
1637 else if (__f.__f_ == (const __base*)&__f.__buf_)
1638 {
1639 __f_ = (__base*)&__buf_;
1640 __f.__f_->__clone(__f_);
1641 }
1642 else
1643 __f_ = __f.__f_->__clone();
1644}
1645
Howard Hinnant99968442011-11-29 18:15:50 +00001646template<class _Rp, class _A0, class _A1, class _A2>
1647template <class _Fp>
1648function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1649 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 : __f_(0)
1651{
1652 if (__not_null(__f))
1653 {
Howard Hinnant99968442011-11-29 18:15:50 +00001654 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655 if (sizeof(_FF) <= sizeof(__buf_))
1656 {
1657 __f_ = (__base*)&__buf_;
1658 ::new (__f_) _FF(__f);
1659 }
1660 else
1661 {
Howard Hinnant99968442011-11-29 18:15:50 +00001662 typedef allocator<_FF> _Ap;
1663 _Ap __a;
1664 typedef __allocator_destructor<_Ap> _Dp;
1665 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1666 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 __f_ = __hold.release();
1668 }
1669 }
1670}
1671
Howard Hinnant99968442011-11-29 18:15:50 +00001672template<class _Rp, class _A0, class _A1, class _A2>
1673template <class _Fp, class _Alloc>
1674function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1675 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001676 : __f_(0)
1677{
1678 typedef allocator_traits<_Alloc> __alloc_traits;
1679 if (__not_null(__f))
1680 {
Howard Hinnant99968442011-11-29 18:15:50 +00001681 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001682 if (sizeof(_FF) <= sizeof(__buf_))
1683 {
1684 __f_ = (__base*)&__buf_;
1685 ::new (__f_) _FF(__f);
1686 }
1687 else
1688 {
Marshall Clow66302c62015-04-07 05:21:38 +00001689 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001690 _Ap __a(__a0);
1691 typedef __allocator_destructor<_Ap> _Dp;
1692 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001693 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1694 __f_ = __hold.release();
1695 }
1696 }
1697}
1698
Howard Hinnant99968442011-11-29 18:15:50 +00001699template<class _Rp, class _A0, class _A1, class _A2>
1700function<_Rp(_A0, _A1, _A2)>&
1701function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001702{
1703 function(__f).swap(*this);
1704 return *this;
1705}
1706
Howard Hinnant99968442011-11-29 18:15:50 +00001707template<class _Rp, class _A0, class _A1, class _A2>
1708function<_Rp(_A0, _A1, _A2)>&
1709function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710{
1711 if (__f_ == (__base*)&__buf_)
1712 __f_->destroy();
1713 else if (__f_)
1714 __f_->destroy_deallocate();
1715 __f_ = 0;
1716}
1717
Howard Hinnant99968442011-11-29 18:15:50 +00001718template<class _Rp, class _A0, class _A1, class _A2>
1719template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720typename enable_if
1721<
Howard Hinnant99968442011-11-29 18:15:50 +00001722 !is_integral<_Fp>::value,
1723 function<_Rp(_A0, _A1, _A2)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001725function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001727 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728 return *this;
1729}
1730
Howard Hinnant99968442011-11-29 18:15:50 +00001731template<class _Rp, class _A0, class _A1, class _A2>
1732function<_Rp(_A0, _A1, _A2)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733{
1734 if (__f_ == (__base*)&__buf_)
1735 __f_->destroy();
1736 else if (__f_)
1737 __f_->destroy_deallocate();
1738}
1739
Howard Hinnant99968442011-11-29 18:15:50 +00001740template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741void
Howard Hinnant99968442011-11-29 18:15:50 +00001742function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743{
1744 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1745 {
1746 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1747 __base* __t = (__base*)&__tempbuf;
1748 __f_->__clone(__t);
1749 __f_->destroy();
1750 __f_ = 0;
1751 __f.__f_->__clone((__base*)&__buf_);
1752 __f.__f_->destroy();
1753 __f.__f_ = 0;
1754 __f_ = (__base*)&__buf_;
1755 __t->__clone((__base*)&__f.__buf_);
1756 __t->destroy();
1757 __f.__f_ = (__base*)&__f.__buf_;
1758 }
1759 else if (__f_ == (__base*)&__buf_)
1760 {
1761 __f_->__clone((__base*)&__f.__buf_);
1762 __f_->destroy();
1763 __f_ = __f.__f_;
1764 __f.__f_ = (__base*)&__f.__buf_;
1765 }
1766 else if (__f.__f_ == (__base*)&__f.__buf_)
1767 {
1768 __f.__f_->__clone((__base*)&__buf_);
1769 __f.__f_->destroy();
1770 __f.__f_ = __f_;
1771 __f_ = (__base*)&__buf_;
1772 }
1773 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001774 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775}
1776
Howard Hinnant99968442011-11-29 18:15:50 +00001777template<class _Rp, class _A0, class _A1, class _A2>
1778_Rp
1779function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780{
Howard Hinnantd4444702010-08-11 17:04:31 +00001781#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782 if (__f_ == 0)
1783 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001784#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001785 return (*__f_)(__a0, __a1, __a2);
1786}
1787
Howard Hinnantd4444702010-08-11 17:04:31 +00001788#ifndef _LIBCPP_NO_RTTI
1789
Howard Hinnant99968442011-11-29 18:15:50 +00001790template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001792function<_Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793{
1794 if (__f_ == 0)
1795 return typeid(void);
1796 return __f_->target_type();
1797}
1798
Howard Hinnant99968442011-11-29 18:15:50 +00001799template<class _Rp, class _A0, class _A1, class _A2>
1800template <typename _Tp>
1801_Tp*
1802function<_Rp(_A0, _A1, _A2)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803{
1804 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001805 return (_Tp*)0;
1806 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807}
1808
Howard Hinnant99968442011-11-29 18:15:50 +00001809template<class _Rp, class _A0, class _A1, class _A2>
1810template <typename _Tp>
1811const _Tp*
1812function<_Rp(_A0, _A1, _A2)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813{
1814 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001815 return (const _Tp*)0;
1816 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817}
1818
Howard Hinnant324bb032010-08-22 00:02:43 +00001819#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001820
Howard Hinnant99968442011-11-29 18:15:50 +00001821template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822inline _LIBCPP_INLINE_VISIBILITY
1823bool
Howard Hinnant99968442011-11-29 18:15:50 +00001824operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001825
Howard Hinnant99968442011-11-29 18:15:50 +00001826template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827inline _LIBCPP_INLINE_VISIBILITY
1828bool
Howard Hinnant99968442011-11-29 18:15:50 +00001829operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001830
Howard Hinnant99968442011-11-29 18:15:50 +00001831template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832inline _LIBCPP_INLINE_VISIBILITY
1833bool
Howard Hinnant99968442011-11-29 18:15:50 +00001834operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001835
Howard Hinnant99968442011-11-29 18:15:50 +00001836template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837inline _LIBCPP_INLINE_VISIBILITY
1838bool
Howard Hinnant99968442011-11-29 18:15:50 +00001839operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840
Howard Hinnant99968442011-11-29 18:15:50 +00001841template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842inline _LIBCPP_INLINE_VISIBILITY
1843void
Howard Hinnant99968442011-11-29 18:15:50 +00001844swap(function<_Fp>& __x, function<_Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845{return __x.swap(__y);}
1846
1847template<class _Tp> struct __is_bind_expression : public false_type {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001848template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1850
1851template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001852template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001853 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1854
1855namespace placeholders
1856{
1857
Howard Hinnant99968442011-11-29 18:15:50 +00001858template <int _Np> struct __ph {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859
1860extern __ph<1> _1;
1861extern __ph<2> _2;
1862extern __ph<3> _3;
1863extern __ph<4> _4;
1864extern __ph<5> _5;
1865extern __ph<6> _6;
1866extern __ph<7> _7;
1867extern __ph<8> _8;
1868extern __ph<9> _9;
1869extern __ph<10> _10;
1870
1871} // placeholders
1872
Howard Hinnant99968442011-11-29 18:15:50 +00001873template<int _Np>
1874struct __is_placeholder<placeholders::__ph<_Np> >
1875 : public integral_constant<int, _Np> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876
1877template <class _Tp, class _Uj>
1878inline _LIBCPP_INLINE_VISIBILITY
1879_Tp&
1880__mu(reference_wrapper<_Tp> __t, _Uj&)
1881{
1882 return __t.get();
1883}
1884/*
1885template <bool _IsBindExpr, class _Ti, class ..._Uj>
1886struct __mu_return1 {};
1887
1888template <class _Ti, class ..._Uj>
1889struct __mu_return1<true, _Ti, _Uj...>
1890{
1891 typedef typename result_of<_Ti(_Uj...)>::type type;
1892};
1893
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894template <class _Ti, class ..._Uj, size_t ..._Indx>
1895inline _LIBCPP_INLINE_VISIBILITY
1896typename __mu_return1<true, _Ti, _Uj...>::type
1897__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1898{
Marshall Clowba6dbf42014-06-24 00:46:19 +00001899 __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900}
1901
1902template <class _Ti, class ..._Uj>
1903inline _LIBCPP_INLINE_VISIBILITY
1904typename enable_if
1905<
1906 is_bind_expression<_Ti>::value,
1907 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1908>::type
1909__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1910{
1911 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1912 return __mu_expand(__ti, __uj, __indices());
1913}
1914
1915template <bool IsPh, class _Ti, class _Uj>
1916struct __mu_return2 {};
1917
1918template <class _Ti, class _Uj>
1919struct __mu_return2<true, _Ti, _Uj>
1920{
1921 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1922};
1923
1924template <class _Ti, class _Uj>
1925inline _LIBCPP_INLINE_VISIBILITY
1926typename enable_if
1927<
1928 0 < is_placeholder<_Ti>::value,
1929 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1930>::type
1931__mu(_Ti&, _Uj& __uj)
1932{
1933 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1934 // compiler bug workaround
Marshall Clowba6dbf42014-06-24 00:46:19 +00001935 typename tuple_element<_Indx, _Uj>::type __t = _VSTD::get<_Indx>(__uj);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936 return __t;
Marshall Clowba6dbf42014-06-24 00:46:19 +00001937// return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938}
1939
1940template <class _Ti, class _Uj>
1941inline _LIBCPP_INLINE_VISIBILITY
1942typename enable_if
1943<
1944 !is_bind_expression<_Ti>::value &&
1945 is_placeholder<_Ti>::value == 0 &&
1946 !__is_reference_wrapper<_Ti>::value,
1947 _Ti&
1948>::type
1949__mu(_Ti& __ti, _Uj& __uj)
1950{
1951 return __ti;
1952}
1953
1954template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1955struct ____mu_return;
1956
1957template <class _Ti, class ..._Uj>
1958struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1959{
1960 typedef typename result_of<_Ti(_Uj...)>::type type;
1961};
1962
1963template <class _Ti, class _TupleUj>
1964struct ____mu_return<_Ti, false, true, _TupleUj>
1965{
1966 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1967 _TupleUj>::type&& type;
1968};
1969
1970template <class _Ti, class _TupleUj>
1971struct ____mu_return<_Ti, false, false, _TupleUj>
1972{
1973 typedef _Ti& type;
1974};
1975
1976template <class _Ti, class _TupleUj>
1977struct __mu_return
1978 : public ____mu_return<_Ti,
1979 is_bind_expression<_Ti>::value,
1980 0 < is_placeholder<_Ti>::value,
1981 _TupleUj>
1982{
1983};
1984
1985template <class _Ti, class _TupleUj>
1986struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1987{
1988 typedef _Ti& type;
1989};
1990
Howard Hinnant99968442011-11-29 18:15:50 +00001991template <class _Fp, class _BoundArgs, class _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001992struct __bind_return;
1993
Howard Hinnant99968442011-11-29 18:15:50 +00001994template <class _Fp, class ..._BoundArgs, class _TupleUj>
1995struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996{
1997 typedef typename __ref_return
1998 <
Howard Hinnant99968442011-11-29 18:15:50 +00001999 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000 typename __mu_return
2001 <
2002 _BoundArgs,
2003 _TupleUj
2004 >::type...
2005 >::type type;
2006};
2007
Howard Hinnant99968442011-11-29 18:15:50 +00002008template <class _Fp, class ..._BoundArgs, class _TupleUj>
2009struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002010{
2011 typedef typename __ref_return
2012 <
Howard Hinnant99968442011-11-29 18:15:50 +00002013 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014 typename __mu_return
2015 <
2016 const _BoundArgs,
2017 _TupleUj
2018 >::type...
2019 >::type type;
2020};
2021
Howard Hinnant99968442011-11-29 18:15:50 +00002022template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002024typename __bind_return<_Fp, _BoundArgs, _Args>::type
2025__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026 _Args&& __args)
2027{
Marshall Clowba6dbf42014-06-24 00:46:19 +00002028 return __invoke(__f, __mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029}
2030
Howard Hinnant99968442011-11-29 18:15:50 +00002031template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032class __bind
2033{
Howard Hinnant99968442011-11-29 18:15:50 +00002034 _Fp __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 tuple<_BoundArgs...> __bound_args_;
2036
2037 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2038public:
Howard Hinnant99968442011-11-29 18:15:50 +00002039 template <class _Gp, class ..._BA>
2040 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2041 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002042 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043
2044 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002045 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046 operator()(_Args&& ...__args)
2047 {
2048 // compiler bug workaround
Howard Hinnant324bb032010-08-22 00:02:43 +00002049 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 tuple<_Args&&...>(__args...));
2051 }
2052
2053 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002054 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055 operator()(_Args&& ...__args) const
2056 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002057 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058 tuple<_Args&&...>(__args...));
2059 }
2060};
2061
Howard Hinnant99968442011-11-29 18:15:50 +00002062template<class _Fp, class ..._BoundArgs>
2063struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064
Howard Hinnant99968442011-11-29 18:15:50 +00002065template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002066class __bind_r
Howard Hinnant99968442011-11-29 18:15:50 +00002067 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068{
Howard Hinnant99968442011-11-29 18:15:50 +00002069 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070public:
Howard Hinnant99968442011-11-29 18:15:50 +00002071 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072
Howard Hinnant99968442011-11-29 18:15:50 +00002073 template <class _Gp, class ..._BA>
2074 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2075 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002076 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002077
2078 template <class ..._Args>
2079 result_type
2080 operator()(_Args&& ...__args)
2081 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002082 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083 }
2084
2085 template <class ..._Args>
2086 result_type
2087 operator()(_Args&& ...__args) const
2088 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002089 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090 }
2091};
2092
Howard Hinnant99968442011-11-29 18:15:50 +00002093template<class _Rp, class _Fp, class ..._BoundArgs>
2094struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095
Howard Hinnant99968442011-11-29 18:15:50 +00002096template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002098__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2099bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100{
Howard Hinnant99968442011-11-29 18:15:50 +00002101 typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2102 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002103}
2104
Howard Hinnant99968442011-11-29 18:15:50 +00002105template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002107__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2108bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109{
Howard Hinnant99968442011-11-29 18:15:50 +00002110 typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2111 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112}
2113*/
2114
Howard Hinnant324bb032010-08-22 00:02:43 +00002115#endif // _LIBCPP_FUNCTIONAL_03