blob: 99354834234a24737cda6c998efa492098b18cda [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{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000336 typedef allocator_traits<_Alloc> __alloc_traits;
337 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000338 _Ap __a(__f_.second());
339 typedef __allocator_destructor<_Ap> _Dp;
340 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
342 return __hold.release();
343}
344
Howard Hinnant99968442011-11-29 18:15:50 +0000345template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346void
Howard Hinnant99968442011-11-29 18:15:50 +0000347__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000348{
349 ::new (__p) __func(__f_.first(), __f_.second());
350}
351
Howard Hinnant99968442011-11-29 18:15:50 +0000352template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353void
Howard Hinnant99968442011-11-29 18:15:50 +0000354__func<_Fp, _Alloc, _Rp()>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355{
Howard Hinnant99968442011-11-29 18:15:50 +0000356 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357}
358
Howard Hinnant99968442011-11-29 18:15:50 +0000359template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360void
Howard Hinnant99968442011-11-29 18:15:50 +0000361__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000362{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000363 typedef allocator_traits<_Alloc> __alloc_traits;
364 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000365 _Ap __a(__f_.second());
366 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367 __a.deallocate(this, 1);
368}
369
Howard Hinnant99968442011-11-29 18:15:50 +0000370template<class _Fp, class _Alloc, class _Rp>
371_Rp
372__func<_Fp, _Alloc, _Rp()>::operator()()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000374 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
375 return _Invoker::__call(__f_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376}
377
Howard Hinnantd4444702010-08-11 17:04:31 +0000378#ifndef _LIBCPP_NO_RTTI
379
Howard Hinnant99968442011-11-29 18:15:50 +0000380template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000382__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383{
Howard Hinnant99968442011-11-29 18:15:50 +0000384 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385 return &__f_.first();
386 return (const void*)0;
387}
388
Howard Hinnant99968442011-11-29 18:15:50 +0000389template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000391__func<_Fp, _Alloc, _Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392{
Howard Hinnant99968442011-11-29 18:15:50 +0000393 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394}
395
Howard Hinnant324bb032010-08-22 00:02:43 +0000396#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000397
Howard Hinnant99968442011-11-29 18:15:50 +0000398template<class _Fp, class _Alloc, class _Rp, class _A0>
399class __func<_Fp, _Alloc, _Rp(_A0)>
400 : public __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401{
Howard Hinnant99968442011-11-29 18:15:50 +0000402 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403public:
Howard Hinnant99968442011-11-29 18:15:50 +0000404 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
405 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000406 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000407 virtual __base<_Rp(_A0)>* __clone() const;
408 virtual void __clone(__base<_Rp(_A0)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 virtual void destroy();
410 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000411 virtual _Rp operator()(_A0);
Howard Hinnantd4444702010-08-11 17:04:31 +0000412#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413 virtual const void* target(const type_info&) const;
414 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000415#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416};
417
Howard Hinnant99968442011-11-29 18:15:50 +0000418template<class _Fp, class _Alloc, class _Rp, class _A0>
419__base<_Rp(_A0)>*
420__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000422 typedef allocator_traits<_Alloc> __alloc_traits;
423 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000424 _Ap __a(__f_.second());
425 typedef __allocator_destructor<_Ap> _Dp;
426 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
428 return __hold.release();
429}
430
Howard Hinnant99968442011-11-29 18:15:50 +0000431template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432void
Howard Hinnant99968442011-11-29 18:15:50 +0000433__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434{
435 ::new (__p) __func(__f_.first(), __f_.second());
436}
437
Howard Hinnant99968442011-11-29 18:15:50 +0000438template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439void
Howard Hinnant99968442011-11-29 18:15:50 +0000440__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000441{
Howard Hinnant99968442011-11-29 18:15:50 +0000442 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443}
444
Howard Hinnant99968442011-11-29 18:15:50 +0000445template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446void
Howard Hinnant99968442011-11-29 18:15:50 +0000447__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000448{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000449 typedef allocator_traits<_Alloc> __alloc_traits;
450 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000451 _Ap __a(__f_.second());
452 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453 __a.deallocate(this, 1);
454}
455
Howard Hinnant99968442011-11-29 18:15:50 +0000456template<class _Fp, class _Alloc, class _Rp, class _A0>
457_Rp
458__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000460 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
461 return _Invoker::__call(__f_.first(), __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462}
463
Howard Hinnantd4444702010-08-11 17:04:31 +0000464#ifndef _LIBCPP_NO_RTTI
465
Howard Hinnant99968442011-11-29 18:15:50 +0000466template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000468__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000469{
Howard Hinnant99968442011-11-29 18:15:50 +0000470 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471 return &__f_.first();
472 return (const void*)0;
473}
474
Howard Hinnant99968442011-11-29 18:15:50 +0000475template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000477__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478{
Howard Hinnant99968442011-11-29 18:15:50 +0000479 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000480}
481
Howard Hinnant324bb032010-08-22 00:02:43 +0000482#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000483
Howard Hinnant99968442011-11-29 18:15:50 +0000484template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
485class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
486 : public __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487{
Howard Hinnant99968442011-11-29 18:15:50 +0000488 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489public:
Howard Hinnant99968442011-11-29 18:15:50 +0000490 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
491 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000492 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000493 virtual __base<_Rp(_A0, _A1)>* __clone() const;
494 virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495 virtual void destroy();
496 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000497 virtual _Rp operator()(_A0, _A1);
Howard Hinnantd4444702010-08-11 17:04:31 +0000498#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499 virtual const void* target(const type_info&) const;
500 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000501#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502};
503
Howard Hinnant99968442011-11-29 18:15:50 +0000504template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
505__base<_Rp(_A0, _A1)>*
506__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000508 typedef allocator_traits<_Alloc> __alloc_traits;
509 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000510 _Ap __a(__f_.second());
511 typedef __allocator_destructor<_Ap> _Dp;
512 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
514 return __hold.release();
515}
516
Howard Hinnant99968442011-11-29 18:15:50 +0000517template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518void
Howard Hinnant99968442011-11-29 18:15:50 +0000519__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520{
521 ::new (__p) __func(__f_.first(), __f_.second());
522}
523
Howard Hinnant99968442011-11-29 18:15:50 +0000524template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525void
Howard Hinnant99968442011-11-29 18:15:50 +0000526__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527{
Howard Hinnant99968442011-11-29 18:15:50 +0000528 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529}
530
Howard Hinnant99968442011-11-29 18:15:50 +0000531template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000532void
Howard Hinnant99968442011-11-29 18:15:50 +0000533__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000535 typedef allocator_traits<_Alloc> __alloc_traits;
536 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000537 _Ap __a(__f_.second());
538 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539 __a.deallocate(this, 1);
540}
541
Howard Hinnant99968442011-11-29 18:15:50 +0000542template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
543_Rp
544__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000545{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000546 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
547 return _Invoker::__call(__f_.first(), __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548}
549
Howard Hinnantd4444702010-08-11 17:04:31 +0000550#ifndef _LIBCPP_NO_RTTI
551
Howard Hinnant99968442011-11-29 18:15:50 +0000552template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000554__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555{
Howard Hinnant99968442011-11-29 18:15:50 +0000556 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557 return &__f_.first();
558 return (const void*)0;
559}
560
Howard Hinnant99968442011-11-29 18:15:50 +0000561template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000563__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564{
Howard Hinnant99968442011-11-29 18:15:50 +0000565 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566}
567
Howard Hinnant324bb032010-08-22 00:02:43 +0000568#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000569
Howard Hinnant99968442011-11-29 18:15:50 +0000570template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
571class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
572 : public __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573{
Howard Hinnant99968442011-11-29 18:15:50 +0000574 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575public:
Howard Hinnant99968442011-11-29 18:15:50 +0000576 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
577 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000578 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000579 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
580 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581 virtual void destroy();
582 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000583 virtual _Rp operator()(_A0, _A1, _A2);
Howard Hinnantd4444702010-08-11 17:04:31 +0000584#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585 virtual const void* target(const type_info&) const;
586 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000587#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588};
589
Howard Hinnant99968442011-11-29 18:15:50 +0000590template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
591__base<_Rp(_A0, _A1, _A2)>*
592__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000594 typedef allocator_traits<_Alloc> __alloc_traits;
595 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000596 _Ap __a(__f_.second());
597 typedef __allocator_destructor<_Ap> _Dp;
598 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
600 return __hold.release();
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)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606{
607 ::new (__p) __func(__f_.first(), __f_.second());
608}
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()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613{
Howard Hinnant99968442011-11-29 18:15:50 +0000614 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000615}
616
Howard Hinnant99968442011-11-29 18:15:50 +0000617template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618void
Howard Hinnant99968442011-11-29 18:15:50 +0000619__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620{
Eric Fiselierb05f0592015-06-14 23:30:09 +0000621 typedef allocator_traits<_Alloc> __alloc_traits;
622 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000623 _Ap __a(__f_.second());
624 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625 __a.deallocate(this, 1);
626}
627
Howard Hinnant99968442011-11-29 18:15:50 +0000628template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
629_Rp
630__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000632 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
633 return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634}
635
Howard Hinnantd4444702010-08-11 17:04:31 +0000636#ifndef _LIBCPP_NO_RTTI
637
Howard Hinnant99968442011-11-29 18:15:50 +0000638template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000640__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641{
Howard Hinnant99968442011-11-29 18:15:50 +0000642 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643 return &__f_.first();
644 return (const void*)0;
645}
646
Howard Hinnant99968442011-11-29 18:15:50 +0000647template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000649__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650{
Howard Hinnant99968442011-11-29 18:15:50 +0000651 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652}
653
Howard Hinnant324bb032010-08-22 00:02:43 +0000654#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000655
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656} // __function
657
Howard Hinnant99968442011-11-29 18:15:50 +0000658template<class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000659class _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660{
Howard Hinnant99968442011-11-29 18:15:50 +0000661 typedef __function::__base<_Rp()> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662 aligned_storage<3*sizeof(void*)>::type __buf_;
663 __base* __f_;
664
Howard Hinnant99968442011-11-29 18:15:50 +0000665 template <class _Fp>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000667 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668 template <class _R2>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000669 _LIBCPP_INLINE_VISIBILITY
670 static bool __not_null(_R2 (*__p)()) {return __p;}
671 template <class _R2>
672 _LIBCPP_INLINE_VISIBILITY
673 static bool __not_null(const function<_R2()>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674public:
Howard Hinnant99968442011-11-29 18:15:50 +0000675 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
677 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000678 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
679 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000681 template<class _Fp>
682 function(_Fp,
683 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684
Howard Hinnant72552802010-08-20 19:36:46 +0000685 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000687 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
688 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000690 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
691 template<class _Alloc>
692 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000693 template<class _Fp, class _Alloc>
694 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
695 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000696
697 function& operator=(const function&);
698 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000699 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000700 typename enable_if
701 <
Howard Hinnant99968442011-11-29 18:15:50 +0000702 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703 function&
704 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000705 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706
707 ~function();
708
709 // 20.7.16.2.2, function modifiers:
710 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000711 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000713 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +0000714 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715
716 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +0000717 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718
719private:
720 // deleted overloads close possible hole in the type system
721 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000722 bool operator==(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000724 bool operator!=(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725public:
726 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +0000727 _Rp operator()() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728
Howard Hinnantd4444702010-08-11 17:04:31 +0000729#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730 // 20.7.16.2.5, function target access:
731 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +0000732 template <typename _Tp> _Tp* target();
733 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000734#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000735};
736
Howard Hinnant99968442011-11-29 18:15:50 +0000737template<class _Rp>
738function<_Rp()>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739{
740 if (__f.__f_ == 0)
741 __f_ = 0;
742 else if (__f.__f_ == (const __base*)&__f.__buf_)
743 {
744 __f_ = (__base*)&__buf_;
745 __f.__f_->__clone(__f_);
746 }
747 else
748 __f_ = __f.__f_->__clone();
749}
750
Howard Hinnant99968442011-11-29 18:15:50 +0000751template<class _Rp>
Howard Hinnant72552802010-08-20 19:36:46 +0000752template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +0000753function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +0000754{
755 if (__f.__f_ == 0)
756 __f_ = 0;
757 else if (__f.__f_ == (const __base*)&__f.__buf_)
758 {
759 __f_ = (__base*)&__buf_;
760 __f.__f_->__clone(__f_);
761 }
762 else
763 __f_ = __f.__f_->__clone();
764}
765
Howard Hinnant99968442011-11-29 18:15:50 +0000766template<class _Rp>
767template <class _Fp>
768function<_Rp()>::function(_Fp __f,
769 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000770 : __f_(0)
771{
772 if (__not_null(__f))
773 {
Howard Hinnant99968442011-11-29 18:15:50 +0000774 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000775 if (sizeof(_FF) <= sizeof(__buf_))
776 {
777 __f_ = (__base*)&__buf_;
778 ::new (__f_) _FF(__f);
779 }
780 else
781 {
Howard Hinnant99968442011-11-29 18:15:50 +0000782 typedef allocator<_FF> _Ap;
783 _Ap __a;
784 typedef __allocator_destructor<_Ap> _Dp;
785 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
786 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787 __f_ = __hold.release();
788 }
789 }
790}
791
Howard Hinnant99968442011-11-29 18:15:50 +0000792template<class _Rp>
793template <class _Fp, class _Alloc>
794function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
795 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +0000796 : __f_(0)
797{
798 typedef allocator_traits<_Alloc> __alloc_traits;
799 if (__not_null(__f))
800 {
Howard Hinnant99968442011-11-29 18:15:50 +0000801 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +0000802 if (sizeof(_FF) <= sizeof(__buf_))
803 {
804 __f_ = (__base*)&__buf_;
Eric Fiselierb05f0592015-06-14 23:30:09 +0000805 ::new (__f_) _FF(__f, __a0);
Howard Hinnant72552802010-08-20 19:36:46 +0000806 }
807 else
808 {
Marshall Clow66302c62015-04-07 05:21:38 +0000809 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000810 _Ap __a(__a0);
811 typedef __allocator_destructor<_Ap> _Dp;
812 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +0000813 ::new (__hold.get()) _FF(__f, _Alloc(__a));
814 __f_ = __hold.release();
815 }
816 }
817}
818
Howard Hinnant99968442011-11-29 18:15:50 +0000819template<class _Rp>
820function<_Rp()>&
821function<_Rp()>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822{
823 function(__f).swap(*this);
824 return *this;
825}
826
Howard Hinnant99968442011-11-29 18:15:50 +0000827template<class _Rp>
828function<_Rp()>&
829function<_Rp()>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000830{
831 if (__f_ == (__base*)&__buf_)
832 __f_->destroy();
833 else if (__f_)
834 __f_->destroy_deallocate();
835 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +0000836 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837}
838
Howard Hinnant99968442011-11-29 18:15:50 +0000839template<class _Rp>
840template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000841typename enable_if
842<
Howard Hinnant99968442011-11-29 18:15:50 +0000843 !is_integral<_Fp>::value,
844 function<_Rp()>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000845>::type
Howard Hinnant99968442011-11-29 18:15:50 +0000846function<_Rp()>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000848 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000849 return *this;
850}
851
Howard Hinnant99968442011-11-29 18:15:50 +0000852template<class _Rp>
853function<_Rp()>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000854{
855 if (__f_ == (__base*)&__buf_)
856 __f_->destroy();
857 else if (__f_)
858 __f_->destroy_deallocate();
859}
860
Howard Hinnant99968442011-11-29 18:15:50 +0000861template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862void
Howard Hinnant99968442011-11-29 18:15:50 +0000863function<_Rp()>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864{
865 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
866 {
867 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
868 __base* __t = (__base*)&__tempbuf;
869 __f_->__clone(__t);
870 __f_->destroy();
871 __f_ = 0;
872 __f.__f_->__clone((__base*)&__buf_);
873 __f.__f_->destroy();
874 __f.__f_ = 0;
875 __f_ = (__base*)&__buf_;
876 __t->__clone((__base*)&__f.__buf_);
877 __t->destroy();
878 __f.__f_ = (__base*)&__f.__buf_;
879 }
880 else if (__f_ == (__base*)&__buf_)
881 {
882 __f_->__clone((__base*)&__f.__buf_);
883 __f_->destroy();
884 __f_ = __f.__f_;
885 __f.__f_ = (__base*)&__f.__buf_;
886 }
887 else if (__f.__f_ == (__base*)&__f.__buf_)
888 {
889 __f.__f_->__clone((__base*)&__buf_);
890 __f.__f_->destroy();
891 __f.__f_ = __f_;
892 __f_ = (__base*)&__buf_;
893 }
894 else
Howard Hinnant0949eed2011-06-30 21:18:19 +0000895 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896}
897
Howard Hinnant99968442011-11-29 18:15:50 +0000898template<class _Rp>
899_Rp
900function<_Rp()>::operator()() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901{
Howard Hinnantd4444702010-08-11 17:04:31 +0000902#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903 if (__f_ == 0)
904 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +0000905#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906 return (*__f_)();
907}
908
Howard Hinnantd4444702010-08-11 17:04:31 +0000909#ifndef _LIBCPP_NO_RTTI
910
Howard Hinnant99968442011-11-29 18:15:50 +0000911template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000913function<_Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914{
915 if (__f_ == 0)
916 return typeid(void);
917 return __f_->target_type();
918}
919
Howard Hinnant99968442011-11-29 18:15:50 +0000920template<class _Rp>
921template <typename _Tp>
922_Tp*
923function<_Rp()>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924{
925 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000926 return (_Tp*)0;
927 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000928}
929
Howard Hinnant99968442011-11-29 18:15:50 +0000930template<class _Rp>
931template <typename _Tp>
932const _Tp*
933function<_Rp()>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934{
935 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000936 return (const _Tp*)0;
937 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938}
939
Howard Hinnant324bb032010-08-22 00:02:43 +0000940#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000941
Howard Hinnant99968442011-11-29 18:15:50 +0000942template<class _Rp, class _A0>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000943class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
Howard Hinnant99968442011-11-29 18:15:50 +0000944 : public unary_function<_A0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945{
Howard Hinnant99968442011-11-29 18:15:50 +0000946 typedef __function::__base<_Rp(_A0)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947 aligned_storage<3*sizeof(void*)>::type __buf_;
948 __base* __f_;
949
Howard Hinnant99968442011-11-29 18:15:50 +0000950 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000952 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +0000956 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)()) {return __p;}
959 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000961 static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
962 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000964 static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
965 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000967 static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000969 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +0000970 static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971public:
Howard Hinnant99968442011-11-29 18:15:50 +0000972 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973
974 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000975 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
976 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000978 template<class _Fp>
979 function(_Fp,
980 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981
Howard Hinnant72552802010-08-20 19:36:46 +0000982 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000984 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
985 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000987 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
988 template<class _Alloc>
989 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000990 template<class _Fp, class _Alloc>
991 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
992 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993
994 function& operator=(const function&);
995 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000996 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997 typename enable_if
998 <
Howard Hinnant99968442011-11-29 18:15:50 +0000999 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000 function&
1001 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001002 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003
1004 ~function();
1005
1006 // 20.7.16.2.2, function modifiers:
1007 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001008 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001010 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001011 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012
1013 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001014 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015
1016private:
1017 // deleted overloads close possible hole in the type system
1018 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001019 bool operator==(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001021 bool operator!=(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022public:
1023 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001024 _Rp operator()(_A0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025
Howard Hinnantd4444702010-08-11 17:04:31 +00001026#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027 // 20.7.16.2.5, function target access:
1028 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001029 template <typename _Tp> _Tp* target();
1030 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001031#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001032};
1033
Howard Hinnant99968442011-11-29 18:15:50 +00001034template<class _Rp, class _A0>
1035function<_Rp(_A0)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001036{
1037 if (__f.__f_ == 0)
1038 __f_ = 0;
1039 else if (__f.__f_ == (const __base*)&__f.__buf_)
1040 {
1041 __f_ = (__base*)&__buf_;
1042 __f.__f_->__clone(__f_);
1043 }
1044 else
1045 __f_ = __f.__f_->__clone();
1046}
1047
Howard Hinnant99968442011-11-29 18:15:50 +00001048template<class _Rp, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001049template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001050function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001051{
1052 if (__f.__f_ == 0)
1053 __f_ = 0;
1054 else if (__f.__f_ == (const __base*)&__f.__buf_)
1055 {
1056 __f_ = (__base*)&__buf_;
1057 __f.__f_->__clone(__f_);
1058 }
1059 else
1060 __f_ = __f.__f_->__clone();
1061}
1062
Howard Hinnant99968442011-11-29 18:15:50 +00001063template<class _Rp, class _A0>
1064template <class _Fp>
1065function<_Rp(_A0)>::function(_Fp __f,
1066 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067 : __f_(0)
1068{
1069 if (__not_null(__f))
1070 {
Howard Hinnant99968442011-11-29 18:15:50 +00001071 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072 if (sizeof(_FF) <= sizeof(__buf_))
1073 {
1074 __f_ = (__base*)&__buf_;
1075 ::new (__f_) _FF(__f);
1076 }
1077 else
1078 {
Howard Hinnant99968442011-11-29 18:15:50 +00001079 typedef allocator<_FF> _Ap;
1080 _Ap __a;
1081 typedef __allocator_destructor<_Ap> _Dp;
1082 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1083 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084 __f_ = __hold.release();
1085 }
1086 }
1087}
1088
Howard Hinnant99968442011-11-29 18:15:50 +00001089template<class _Rp, class _A0>
1090template <class _Fp, class _Alloc>
1091function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1092 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001093 : __f_(0)
1094{
1095 typedef allocator_traits<_Alloc> __alloc_traits;
1096 if (__not_null(__f))
1097 {
Howard Hinnant99968442011-11-29 18:15:50 +00001098 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001099 if (sizeof(_FF) <= sizeof(__buf_))
1100 {
1101 __f_ = (__base*)&__buf_;
Eric Fiselierb05f0592015-06-14 23:30:09 +00001102 ::new (__f_) _FF(__f, __a0);
Howard Hinnant72552802010-08-20 19:36:46 +00001103 }
1104 else
1105 {
Marshall Clow66302c62015-04-07 05:21:38 +00001106 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001107 _Ap __a(__a0);
1108 typedef __allocator_destructor<_Ap> _Dp;
1109 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001110 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1111 __f_ = __hold.release();
1112 }
1113 }
1114}
1115
Howard Hinnant99968442011-11-29 18:15:50 +00001116template<class _Rp, class _A0>
1117function<_Rp(_A0)>&
1118function<_Rp(_A0)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001119{
1120 function(__f).swap(*this);
1121 return *this;
1122}
1123
Howard Hinnant99968442011-11-29 18:15:50 +00001124template<class _Rp, class _A0>
1125function<_Rp(_A0)>&
1126function<_Rp(_A0)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127{
1128 if (__f_ == (__base*)&__buf_)
1129 __f_->destroy();
1130 else if (__f_)
1131 __f_->destroy_deallocate();
1132 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +00001133 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134}
1135
Howard Hinnant99968442011-11-29 18:15:50 +00001136template<class _Rp, class _A0>
1137template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138typename enable_if
1139<
Howard Hinnant99968442011-11-29 18:15:50 +00001140 !is_integral<_Fp>::value,
1141 function<_Rp(_A0)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001143function<_Rp(_A0)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001145 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 return *this;
1147}
1148
Howard Hinnant99968442011-11-29 18:15:50 +00001149template<class _Rp, class _A0>
1150function<_Rp(_A0)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151{
1152 if (__f_ == (__base*)&__buf_)
1153 __f_->destroy();
1154 else if (__f_)
1155 __f_->destroy_deallocate();
1156}
1157
Howard Hinnant99968442011-11-29 18:15:50 +00001158template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159void
Howard Hinnant99968442011-11-29 18:15:50 +00001160function<_Rp(_A0)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161{
1162 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1163 {
1164 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1165 __base* __t = (__base*)&__tempbuf;
1166 __f_->__clone(__t);
1167 __f_->destroy();
1168 __f_ = 0;
1169 __f.__f_->__clone((__base*)&__buf_);
1170 __f.__f_->destroy();
1171 __f.__f_ = 0;
1172 __f_ = (__base*)&__buf_;
1173 __t->__clone((__base*)&__f.__buf_);
1174 __t->destroy();
1175 __f.__f_ = (__base*)&__f.__buf_;
1176 }
1177 else if (__f_ == (__base*)&__buf_)
1178 {
1179 __f_->__clone((__base*)&__f.__buf_);
1180 __f_->destroy();
1181 __f_ = __f.__f_;
1182 __f.__f_ = (__base*)&__f.__buf_;
1183 }
1184 else if (__f.__f_ == (__base*)&__f.__buf_)
1185 {
1186 __f.__f_->__clone((__base*)&__buf_);
1187 __f.__f_->destroy();
1188 __f.__f_ = __f_;
1189 __f_ = (__base*)&__buf_;
1190 }
1191 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001192 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193}
1194
Howard Hinnant99968442011-11-29 18:15:50 +00001195template<class _Rp, class _A0>
1196_Rp
1197function<_Rp(_A0)>::operator()(_A0 __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001198{
Howard Hinnantd4444702010-08-11 17:04:31 +00001199#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200 if (__f_ == 0)
1201 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001202#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203 return (*__f_)(__a0);
1204}
1205
Howard Hinnantd4444702010-08-11 17:04:31 +00001206#ifndef _LIBCPP_NO_RTTI
1207
Howard Hinnant99968442011-11-29 18:15:50 +00001208template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001210function<_Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001211{
1212 if (__f_ == 0)
1213 return typeid(void);
1214 return __f_->target_type();
1215}
1216
Howard Hinnant99968442011-11-29 18:15:50 +00001217template<class _Rp, class _A0>
1218template <typename _Tp>
1219_Tp*
1220function<_Rp(_A0)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221{
1222 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001223 return (_Tp*)0;
1224 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001225}
1226
Howard Hinnant99968442011-11-29 18:15:50 +00001227template<class _Rp, class _A0>
1228template <typename _Tp>
1229const _Tp*
1230function<_Rp(_A0)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231{
1232 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001233 return (const _Tp*)0;
1234 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235}
1236
Howard Hinnant324bb032010-08-22 00:02:43 +00001237#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001238
Howard Hinnant99968442011-11-29 18:15:50 +00001239template<class _Rp, class _A0, class _A1>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001240class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
Howard Hinnant99968442011-11-29 18:15:50 +00001241 : public binary_function<_A0, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242{
Howard Hinnant99968442011-11-29 18:15:50 +00001243 typedef __function::__base<_Rp(_A0, _A1)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244 aligned_storage<3*sizeof(void*)>::type __buf_;
1245 __base* __f_;
1246
Howard Hinnant99968442011-11-29 18:15:50 +00001247 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001249 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001250 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001253 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001255 static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1256 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001258 static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1259 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001261 static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1262 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001264 static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001265 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001266 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001267 static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268public:
Howard Hinnant99968442011-11-29 18:15:50 +00001269 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270
1271 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001272 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1273 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001275 template<class _Fp>
1276 function(_Fp,
1277 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278
Howard Hinnant72552802010-08-20 19:36:46 +00001279 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001281 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1282 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001284 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1285 template<class _Alloc>
1286 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001287 template<class _Fp, class _Alloc>
1288 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1289 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290
1291 function& operator=(const function&);
1292 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001293 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294 typename enable_if
1295 <
Howard Hinnant99968442011-11-29 18:15:50 +00001296 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297 function&
1298 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001299 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300
1301 ~function();
1302
1303 // 20.7.16.2.2, function modifiers:
1304 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001305 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001307 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001308 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309
1310 // 20.7.16.2.3, function capacity:
1311 operator bool() const {return __f_;}
1312
1313private:
1314 // deleted overloads close possible hole in the type system
1315 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001316 bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001318 bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001319public:
1320 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001321 _Rp operator()(_A0, _A1) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322
Howard Hinnantd4444702010-08-11 17:04:31 +00001323#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324 // 20.7.16.2.5, function target access:
1325 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001326 template <typename _Tp> _Tp* target();
1327 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001328#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329};
1330
Howard Hinnant99968442011-11-29 18:15:50 +00001331template<class _Rp, class _A0, class _A1>
1332function<_Rp(_A0, _A1)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333{
1334 if (__f.__f_ == 0)
1335 __f_ = 0;
1336 else if (__f.__f_ == (const __base*)&__f.__buf_)
1337 {
1338 __f_ = (__base*)&__buf_;
1339 __f.__f_->__clone(__f_);
1340 }
1341 else
1342 __f_ = __f.__f_->__clone();
1343}
1344
Howard Hinnant99968442011-11-29 18:15:50 +00001345template<class _Rp, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001346template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001347function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001348{
1349 if (__f.__f_ == 0)
1350 __f_ = 0;
1351 else if (__f.__f_ == (const __base*)&__f.__buf_)
1352 {
1353 __f_ = (__base*)&__buf_;
1354 __f.__f_->__clone(__f_);
1355 }
1356 else
1357 __f_ = __f.__f_->__clone();
1358}
1359
Howard Hinnant99968442011-11-29 18:15:50 +00001360template<class _Rp, class _A0, class _A1>
1361template <class _Fp>
1362function<_Rp(_A0, _A1)>::function(_Fp __f,
1363 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364 : __f_(0)
1365{
1366 if (__not_null(__f))
1367 {
Howard Hinnant99968442011-11-29 18:15:50 +00001368 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369 if (sizeof(_FF) <= sizeof(__buf_))
1370 {
1371 __f_ = (__base*)&__buf_;
1372 ::new (__f_) _FF(__f);
1373 }
1374 else
1375 {
Howard Hinnant99968442011-11-29 18:15:50 +00001376 typedef allocator<_FF> _Ap;
1377 _Ap __a;
1378 typedef __allocator_destructor<_Ap> _Dp;
1379 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1380 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381 __f_ = __hold.release();
1382 }
1383 }
1384}
1385
Howard Hinnant99968442011-11-29 18:15:50 +00001386template<class _Rp, class _A0, class _A1>
1387template <class _Fp, class _Alloc>
1388function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1389 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001390 : __f_(0)
1391{
1392 typedef allocator_traits<_Alloc> __alloc_traits;
1393 if (__not_null(__f))
1394 {
Howard Hinnant99968442011-11-29 18:15:50 +00001395 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001396 if (sizeof(_FF) <= sizeof(__buf_))
1397 {
1398 __f_ = (__base*)&__buf_;
Eric Fiselierb05f0592015-06-14 23:30:09 +00001399 ::new (__f_) _FF(__f, __a0);
Howard Hinnant72552802010-08-20 19:36:46 +00001400 }
1401 else
1402 {
Marshall Clow66302c62015-04-07 05:21:38 +00001403 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001404 _Ap __a(__a0);
1405 typedef __allocator_destructor<_Ap> _Dp;
1406 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001407 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1408 __f_ = __hold.release();
1409 }
1410 }
1411}
1412
Howard Hinnant99968442011-11-29 18:15:50 +00001413template<class _Rp, class _A0, class _A1>
1414function<_Rp(_A0, _A1)>&
1415function<_Rp(_A0, _A1)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416{
1417 function(__f).swap(*this);
1418 return *this;
1419}
1420
Howard Hinnant99968442011-11-29 18:15:50 +00001421template<class _Rp, class _A0, class _A1>
1422function<_Rp(_A0, _A1)>&
1423function<_Rp(_A0, _A1)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424{
1425 if (__f_ == (__base*)&__buf_)
1426 __f_->destroy();
1427 else if (__f_)
1428 __f_->destroy_deallocate();
1429 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +00001430 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431}
1432
Howard Hinnant99968442011-11-29 18:15:50 +00001433template<class _Rp, class _A0, class _A1>
1434template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435typename enable_if
1436<
Howard Hinnant99968442011-11-29 18:15:50 +00001437 !is_integral<_Fp>::value,
1438 function<_Rp(_A0, _A1)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001440function<_Rp(_A0, _A1)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001442 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443 return *this;
1444}
1445
Howard Hinnant99968442011-11-29 18:15:50 +00001446template<class _Rp, class _A0, class _A1>
1447function<_Rp(_A0, _A1)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448{
1449 if (__f_ == (__base*)&__buf_)
1450 __f_->destroy();
1451 else if (__f_)
1452 __f_->destroy_deallocate();
1453}
1454
Howard Hinnant99968442011-11-29 18:15:50 +00001455template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001456void
Howard Hinnant99968442011-11-29 18:15:50 +00001457function<_Rp(_A0, _A1)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458{
1459 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1460 {
1461 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1462 __base* __t = (__base*)&__tempbuf;
1463 __f_->__clone(__t);
1464 __f_->destroy();
1465 __f_ = 0;
1466 __f.__f_->__clone((__base*)&__buf_);
1467 __f.__f_->destroy();
1468 __f.__f_ = 0;
1469 __f_ = (__base*)&__buf_;
1470 __t->__clone((__base*)&__f.__buf_);
1471 __t->destroy();
1472 __f.__f_ = (__base*)&__f.__buf_;
1473 }
1474 else if (__f_ == (__base*)&__buf_)
1475 {
1476 __f_->__clone((__base*)&__f.__buf_);
1477 __f_->destroy();
1478 __f_ = __f.__f_;
1479 __f.__f_ = (__base*)&__f.__buf_;
1480 }
1481 else if (__f.__f_ == (__base*)&__f.__buf_)
1482 {
1483 __f.__f_->__clone((__base*)&__buf_);
1484 __f.__f_->destroy();
1485 __f.__f_ = __f_;
1486 __f_ = (__base*)&__buf_;
1487 }
1488 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001489 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490}
1491
Howard Hinnant99968442011-11-29 18:15:50 +00001492template<class _Rp, class _A0, class _A1>
1493_Rp
1494function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495{
Howard Hinnantd4444702010-08-11 17:04:31 +00001496#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497 if (__f_ == 0)
1498 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001499#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500 return (*__f_)(__a0, __a1);
1501}
1502
Howard Hinnantd4444702010-08-11 17:04:31 +00001503#ifndef _LIBCPP_NO_RTTI
1504
Howard Hinnant99968442011-11-29 18:15:50 +00001505template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001507function<_Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508{
1509 if (__f_ == 0)
1510 return typeid(void);
1511 return __f_->target_type();
1512}
1513
Howard Hinnant99968442011-11-29 18:15:50 +00001514template<class _Rp, class _A0, class _A1>
1515template <typename _Tp>
1516_Tp*
1517function<_Rp(_A0, _A1)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001518{
1519 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001520 return (_Tp*)0;
1521 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001522}
1523
Howard Hinnant99968442011-11-29 18:15:50 +00001524template<class _Rp, class _A0, class _A1>
1525template <typename _Tp>
1526const _Tp*
1527function<_Rp(_A0, _A1)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001528{
1529 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001530 return (const _Tp*)0;
1531 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532}
1533
Howard Hinnant324bb032010-08-22 00:02:43 +00001534#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001535
Howard Hinnant99968442011-11-29 18:15:50 +00001536template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001537class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538{
Howard Hinnant99968442011-11-29 18:15:50 +00001539 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 aligned_storage<3*sizeof(void*)>::type __buf_;
1541 __base* __f_;
1542
Howard Hinnant99968442011-11-29 18:15:50 +00001543 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001545 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001548 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001549 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001551 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1552 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001554 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1555 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001557 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1558 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001560 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001562 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001563 static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564public:
Howard Hinnant99968442011-11-29 18:15:50 +00001565 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566
1567 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001568 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1569 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001571 template<class _Fp>
1572 function(_Fp,
1573 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574
Howard Hinnant72552802010-08-20 19:36:46 +00001575 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001577 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1578 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001580 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1581 template<class _Alloc>
1582 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001583 template<class _Fp, class _Alloc>
1584 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1585 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586
1587 function& operator=(const function&);
1588 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001589 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 typename enable_if
1591 <
Howard Hinnant99968442011-11-29 18:15:50 +00001592 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593 function&
1594 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001595 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596
1597 ~function();
1598
1599 // 20.7.16.2.2, function modifiers:
1600 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001601 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001603 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001604 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605
1606 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001607 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608
1609private:
1610 // deleted overloads close possible hole in the type system
1611 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001612 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001614 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615public:
1616 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001617 _Rp operator()(_A0, _A1, _A2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618
Howard Hinnantd4444702010-08-11 17:04:31 +00001619#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 // 20.7.16.2.5, function target access:
1621 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001622 template <typename _Tp> _Tp* target();
1623 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001624#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625};
1626
Howard Hinnant99968442011-11-29 18:15:50 +00001627template<class _Rp, class _A0, class _A1, class _A2>
1628function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629{
1630 if (__f.__f_ == 0)
1631 __f_ = 0;
1632 else if (__f.__f_ == (const __base*)&__f.__buf_)
1633 {
1634 __f_ = (__base*)&__buf_;
1635 __f.__f_->__clone(__f_);
1636 }
1637 else
1638 __f_ = __f.__f_->__clone();
1639}
1640
Howard Hinnant99968442011-11-29 18:15:50 +00001641template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001642template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001643function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnant72552802010-08-20 19:36:46 +00001644 const function& __f)
1645{
1646 if (__f.__f_ == 0)
1647 __f_ = 0;
1648 else if (__f.__f_ == (const __base*)&__f.__buf_)
1649 {
1650 __f_ = (__base*)&__buf_;
1651 __f.__f_->__clone(__f_);
1652 }
1653 else
1654 __f_ = __f.__f_->__clone();
1655}
1656
Howard Hinnant99968442011-11-29 18:15:50 +00001657template<class _Rp, class _A0, class _A1, class _A2>
1658template <class _Fp>
1659function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1660 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661 : __f_(0)
1662{
1663 if (__not_null(__f))
1664 {
Howard Hinnant99968442011-11-29 18:15:50 +00001665 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666 if (sizeof(_FF) <= sizeof(__buf_))
1667 {
1668 __f_ = (__base*)&__buf_;
1669 ::new (__f_) _FF(__f);
1670 }
1671 else
1672 {
Howard Hinnant99968442011-11-29 18:15:50 +00001673 typedef allocator<_FF> _Ap;
1674 _Ap __a;
1675 typedef __allocator_destructor<_Ap> _Dp;
1676 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1677 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678 __f_ = __hold.release();
1679 }
1680 }
1681}
1682
Howard Hinnant99968442011-11-29 18:15:50 +00001683template<class _Rp, class _A0, class _A1, class _A2>
1684template <class _Fp, class _Alloc>
1685function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1686 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001687 : __f_(0)
1688{
1689 typedef allocator_traits<_Alloc> __alloc_traits;
1690 if (__not_null(__f))
1691 {
Howard Hinnant99968442011-11-29 18:15:50 +00001692 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001693 if (sizeof(_FF) <= sizeof(__buf_))
1694 {
1695 __f_ = (__base*)&__buf_;
Eric Fiselierb05f0592015-06-14 23:30:09 +00001696 ::new (__f_) _FF(__f, __a0);
Howard Hinnant72552802010-08-20 19:36:46 +00001697 }
1698 else
1699 {
Marshall Clow66302c62015-04-07 05:21:38 +00001700 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001701 _Ap __a(__a0);
1702 typedef __allocator_destructor<_Ap> _Dp;
1703 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001704 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1705 __f_ = __hold.release();
1706 }
1707 }
1708}
1709
Howard Hinnant99968442011-11-29 18:15:50 +00001710template<class _Rp, class _A0, class _A1, class _A2>
1711function<_Rp(_A0, _A1, _A2)>&
1712function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713{
1714 function(__f).swap(*this);
1715 return *this;
1716}
1717
Howard Hinnant99968442011-11-29 18:15:50 +00001718template<class _Rp, class _A0, class _A1, class _A2>
1719function<_Rp(_A0, _A1, _A2)>&
1720function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721{
1722 if (__f_ == (__base*)&__buf_)
1723 __f_->destroy();
1724 else if (__f_)
1725 __f_->destroy_deallocate();
1726 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +00001727 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728}
1729
Howard Hinnant99968442011-11-29 18:15:50 +00001730template<class _Rp, class _A0, class _A1, class _A2>
1731template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732typename enable_if
1733<
Howard Hinnant99968442011-11-29 18:15:50 +00001734 !is_integral<_Fp>::value,
1735 function<_Rp(_A0, _A1, _A2)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001737function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001738{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001739 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 return *this;
1741}
1742
Howard Hinnant99968442011-11-29 18:15:50 +00001743template<class _Rp, class _A0, class _A1, class _A2>
1744function<_Rp(_A0, _A1, _A2)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745{
1746 if (__f_ == (__base*)&__buf_)
1747 __f_->destroy();
1748 else if (__f_)
1749 __f_->destroy_deallocate();
1750}
1751
Howard Hinnant99968442011-11-29 18:15:50 +00001752template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753void
Howard Hinnant99968442011-11-29 18:15:50 +00001754function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755{
1756 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1757 {
1758 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1759 __base* __t = (__base*)&__tempbuf;
1760 __f_->__clone(__t);
1761 __f_->destroy();
1762 __f_ = 0;
1763 __f.__f_->__clone((__base*)&__buf_);
1764 __f.__f_->destroy();
1765 __f.__f_ = 0;
1766 __f_ = (__base*)&__buf_;
1767 __t->__clone((__base*)&__f.__buf_);
1768 __t->destroy();
1769 __f.__f_ = (__base*)&__f.__buf_;
1770 }
1771 else if (__f_ == (__base*)&__buf_)
1772 {
1773 __f_->__clone((__base*)&__f.__buf_);
1774 __f_->destroy();
1775 __f_ = __f.__f_;
1776 __f.__f_ = (__base*)&__f.__buf_;
1777 }
1778 else if (__f.__f_ == (__base*)&__f.__buf_)
1779 {
1780 __f.__f_->__clone((__base*)&__buf_);
1781 __f.__f_->destroy();
1782 __f.__f_ = __f_;
1783 __f_ = (__base*)&__buf_;
1784 }
1785 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001786 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787}
1788
Howard Hinnant99968442011-11-29 18:15:50 +00001789template<class _Rp, class _A0, class _A1, class _A2>
1790_Rp
1791function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001792{
Howard Hinnantd4444702010-08-11 17:04:31 +00001793#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 if (__f_ == 0)
1795 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001796#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 return (*__f_)(__a0, __a1, __a2);
1798}
1799
Howard Hinnantd4444702010-08-11 17:04:31 +00001800#ifndef _LIBCPP_NO_RTTI
1801
Howard Hinnant99968442011-11-29 18:15:50 +00001802template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001804function<_Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805{
1806 if (__f_ == 0)
1807 return typeid(void);
1808 return __f_->target_type();
1809}
1810
Howard Hinnant99968442011-11-29 18:15:50 +00001811template<class _Rp, class _A0, class _A1, class _A2>
1812template <typename _Tp>
1813_Tp*
1814function<_Rp(_A0, _A1, _A2)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815{
1816 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001817 return (_Tp*)0;
1818 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819}
1820
Howard Hinnant99968442011-11-29 18:15:50 +00001821template<class _Rp, class _A0, class _A1, class _A2>
1822template <typename _Tp>
1823const _Tp*
1824function<_Rp(_A0, _A1, _A2)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001825{
1826 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001827 return (const _Tp*)0;
1828 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829}
1830
Howard Hinnant324bb032010-08-22 00:02:43 +00001831#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001832
Howard Hinnant99968442011-11-29 18:15:50 +00001833template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834inline _LIBCPP_INLINE_VISIBILITY
1835bool
Howard Hinnant99968442011-11-29 18:15:50 +00001836operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837
Howard Hinnant99968442011-11-29 18:15:50 +00001838template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839inline _LIBCPP_INLINE_VISIBILITY
1840bool
Howard Hinnant99968442011-11-29 18:15:50 +00001841operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842
Howard Hinnant99968442011-11-29 18:15:50 +00001843template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844inline _LIBCPP_INLINE_VISIBILITY
1845bool
Howard Hinnant99968442011-11-29 18:15:50 +00001846operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847
Howard Hinnant99968442011-11-29 18:15:50 +00001848template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849inline _LIBCPP_INLINE_VISIBILITY
1850bool
Howard Hinnant99968442011-11-29 18:15:50 +00001851operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852
Howard Hinnant99968442011-11-29 18:15:50 +00001853template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854inline _LIBCPP_INLINE_VISIBILITY
1855void
Howard Hinnant99968442011-11-29 18:15:50 +00001856swap(function<_Fp>& __x, function<_Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857{return __x.swap(__y);}
1858
1859template<class _Tp> struct __is_bind_expression : public false_type {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001860template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1862
1863template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001864template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1866
1867namespace placeholders
1868{
1869
Howard Hinnant99968442011-11-29 18:15:50 +00001870template <int _Np> struct __ph {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871
1872extern __ph<1> _1;
1873extern __ph<2> _2;
1874extern __ph<3> _3;
1875extern __ph<4> _4;
1876extern __ph<5> _5;
1877extern __ph<6> _6;
1878extern __ph<7> _7;
1879extern __ph<8> _8;
1880extern __ph<9> _9;
1881extern __ph<10> _10;
1882
1883} // placeholders
1884
Howard Hinnant99968442011-11-29 18:15:50 +00001885template<int _Np>
1886struct __is_placeholder<placeholders::__ph<_Np> >
1887 : public integral_constant<int, _Np> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888
1889template <class _Tp, class _Uj>
1890inline _LIBCPP_INLINE_VISIBILITY
1891_Tp&
1892__mu(reference_wrapper<_Tp> __t, _Uj&)
1893{
1894 return __t.get();
1895}
1896/*
1897template <bool _IsBindExpr, class _Ti, class ..._Uj>
1898struct __mu_return1 {};
1899
1900template <class _Ti, class ..._Uj>
1901struct __mu_return1<true, _Ti, _Uj...>
1902{
1903 typedef typename result_of<_Ti(_Uj...)>::type type;
1904};
1905
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906template <class _Ti, class ..._Uj, size_t ..._Indx>
1907inline _LIBCPP_INLINE_VISIBILITY
1908typename __mu_return1<true, _Ti, _Uj...>::type
1909__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1910{
Marshall Clowba6dbf42014-06-24 00:46:19 +00001911 __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912}
1913
1914template <class _Ti, class ..._Uj>
1915inline _LIBCPP_INLINE_VISIBILITY
1916typename enable_if
1917<
1918 is_bind_expression<_Ti>::value,
1919 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1920>::type
1921__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1922{
1923 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1924 return __mu_expand(__ti, __uj, __indices());
1925}
1926
1927template <bool IsPh, class _Ti, class _Uj>
1928struct __mu_return2 {};
1929
1930template <class _Ti, class _Uj>
1931struct __mu_return2<true, _Ti, _Uj>
1932{
1933 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1934};
1935
1936template <class _Ti, class _Uj>
1937inline _LIBCPP_INLINE_VISIBILITY
1938typename enable_if
1939<
1940 0 < is_placeholder<_Ti>::value,
1941 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1942>::type
1943__mu(_Ti&, _Uj& __uj)
1944{
1945 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1946 // compiler bug workaround
Marshall Clowba6dbf42014-06-24 00:46:19 +00001947 typename tuple_element<_Indx, _Uj>::type __t = _VSTD::get<_Indx>(__uj);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948 return __t;
Marshall Clowba6dbf42014-06-24 00:46:19 +00001949// return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950}
1951
1952template <class _Ti, class _Uj>
1953inline _LIBCPP_INLINE_VISIBILITY
1954typename enable_if
1955<
1956 !is_bind_expression<_Ti>::value &&
1957 is_placeholder<_Ti>::value == 0 &&
1958 !__is_reference_wrapper<_Ti>::value,
1959 _Ti&
1960>::type
1961__mu(_Ti& __ti, _Uj& __uj)
1962{
1963 return __ti;
1964}
1965
1966template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1967struct ____mu_return;
1968
1969template <class _Ti, class ..._Uj>
1970struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1971{
1972 typedef typename result_of<_Ti(_Uj...)>::type type;
1973};
1974
1975template <class _Ti, class _TupleUj>
1976struct ____mu_return<_Ti, false, true, _TupleUj>
1977{
1978 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1979 _TupleUj>::type&& type;
1980};
1981
1982template <class _Ti, class _TupleUj>
1983struct ____mu_return<_Ti, false, false, _TupleUj>
1984{
1985 typedef _Ti& type;
1986};
1987
1988template <class _Ti, class _TupleUj>
1989struct __mu_return
1990 : public ____mu_return<_Ti,
1991 is_bind_expression<_Ti>::value,
1992 0 < is_placeholder<_Ti>::value,
1993 _TupleUj>
1994{
1995};
1996
1997template <class _Ti, class _TupleUj>
1998struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1999{
2000 typedef _Ti& type;
2001};
2002
Howard Hinnant99968442011-11-29 18:15:50 +00002003template <class _Fp, class _BoundArgs, class _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004struct __bind_return;
2005
Howard Hinnant99968442011-11-29 18:15:50 +00002006template <class _Fp, class ..._BoundArgs, class _TupleUj>
2007struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002008{
2009 typedef typename __ref_return
2010 <
Howard Hinnant99968442011-11-29 18:15:50 +00002011 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002012 typename __mu_return
2013 <
2014 _BoundArgs,
2015 _TupleUj
2016 >::type...
2017 >::type type;
2018};
2019
Howard Hinnant99968442011-11-29 18:15:50 +00002020template <class _Fp, class ..._BoundArgs, class _TupleUj>
2021struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022{
2023 typedef typename __ref_return
2024 <
Howard Hinnant99968442011-11-29 18:15:50 +00002025 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026 typename __mu_return
2027 <
2028 const _BoundArgs,
2029 _TupleUj
2030 >::type...
2031 >::type type;
2032};
2033
Howard Hinnant99968442011-11-29 18:15:50 +00002034template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002036typename __bind_return<_Fp, _BoundArgs, _Args>::type
2037__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038 _Args&& __args)
2039{
Marshall Clowba6dbf42014-06-24 00:46:19 +00002040 return __invoke(__f, __mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041}
2042
Howard Hinnant99968442011-11-29 18:15:50 +00002043template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002044class __bind
2045{
Howard Hinnant99968442011-11-29 18:15:50 +00002046 _Fp __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047 tuple<_BoundArgs...> __bound_args_;
2048
2049 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2050public:
Howard Hinnant99968442011-11-29 18:15:50 +00002051 template <class _Gp, class ..._BA>
2052 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2053 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002054 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055
2056 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002057 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058 operator()(_Args&& ...__args)
2059 {
2060 // compiler bug workaround
Howard Hinnant324bb032010-08-22 00:02:43 +00002061 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062 tuple<_Args&&...>(__args...));
2063 }
2064
2065 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002066 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067 operator()(_Args&& ...__args) const
2068 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002069 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 tuple<_Args&&...>(__args...));
2071 }
2072};
2073
Howard Hinnant99968442011-11-29 18:15:50 +00002074template<class _Fp, class ..._BoundArgs>
2075struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076
Howard Hinnant99968442011-11-29 18:15:50 +00002077template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078class __bind_r
Howard Hinnant99968442011-11-29 18:15:50 +00002079 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002080{
Howard Hinnant99968442011-11-29 18:15:50 +00002081 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002082public:
Howard Hinnant99968442011-11-29 18:15:50 +00002083 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084
Howard Hinnant99968442011-11-29 18:15:50 +00002085 template <class _Gp, class ..._BA>
2086 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2087 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002088 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089
2090 template <class ..._Args>
2091 result_type
2092 operator()(_Args&& ...__args)
2093 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002094 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095 }
2096
2097 template <class ..._Args>
2098 result_type
2099 operator()(_Args&& ...__args) const
2100 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002101 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 }
2103};
2104
Howard Hinnant99968442011-11-29 18:15:50 +00002105template<class _Rp, class _Fp, class ..._BoundArgs>
2106struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002107
Howard Hinnant99968442011-11-29 18:15:50 +00002108template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002110__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2111bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112{
Howard Hinnant99968442011-11-29 18:15:50 +00002113 typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2114 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115}
2116
Howard Hinnant99968442011-11-29 18:15:50 +00002117template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002119__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2120bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121{
Howard Hinnant99968442011-11-29 18:15:50 +00002122 typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2123 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124}
2125*/
2126
Howard Hinnant324bb032010-08-22 00:02:43 +00002127#endif // _LIBCPP_FUNCTIONAL_03