blob: 157d6bf893256c63294959f24d5270bcd8b1e9b7 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL_03
12#define _LIBCPP_FUNCTIONAL_03
13
14// manual variadic expansion for <functional>
15
Howard Hinnant08e17472011-10-17 20:05:10 +000016#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000017#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000018#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
20template <class _Tp>
21class __mem_fn
22 : public __weak_result_type<_Tp>
23{
24public:
25 // types
26 typedef _Tp type;
27private:
28 type __f_;
29
30public:
31 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
32
33 // invoke
34
35 typename __invoke_return<type>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000036 operator() () const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000037 {
38 return __invoke(__f_);
39 }
40
41 template <class _A0>
42 typename __invoke_return0<type, _A0>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000043 operator() (_A0& __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044 {
45 return __invoke(__f_, __a0);
46 }
47
48 template <class _A0, class _A1>
49 typename __invoke_return1<type, _A0, _A1>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000050 operator() (_A0& __a0, _A1& __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051 {
52 return __invoke(__f_, __a0, __a1);
53 }
54
55 template <class _A0, class _A1, class _A2>
56 typename __invoke_return2<type, _A0, _A1, _A2>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000057 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000058 {
59 return __invoke(__f_, __a0, __a1, __a2);
60 }
61};
62
Howard Hinnant99968442011-11-29 18:15:50 +000063template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000064inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000065__mem_fn<_Rp _Tp::*>
66mem_fn(_Rp _Tp::* __pm)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067{
Howard Hinnant99968442011-11-29 18:15:50 +000068 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069}
70
Howard Hinnant99968442011-11-29 18:15:50 +000071template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000073__mem_fn<_Rp (_Tp::*)()>
74mem_fn(_Rp (_Tp::* __pm)())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075{
Howard Hinnant99968442011-11-29 18:15:50 +000076 return __mem_fn<_Rp (_Tp::*)()>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077}
78
Howard Hinnant99968442011-11-29 18:15:50 +000079template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000081__mem_fn<_Rp (_Tp::*)(_A0)>
82mem_fn(_Rp (_Tp::* __pm)(_A0))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083{
Howard Hinnant99968442011-11-29 18:15:50 +000084 return __mem_fn<_Rp (_Tp::*)(_A0)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085}
86
Howard Hinnant99968442011-11-29 18:15:50 +000087template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000089__mem_fn<_Rp (_Tp::*)(_A0, _A1)>
90mem_fn(_Rp (_Tp::* __pm)(_A0, _A1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091{
Howard Hinnant99968442011-11-29 18:15:50 +000092 return __mem_fn<_Rp (_Tp::*)(_A0, _A1)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093}
94
Howard Hinnant99968442011-11-29 18:15:50 +000095template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000097__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>
98mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000099{
Howard Hinnant99968442011-11-29 18:15:50 +0000100 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101}
102
Howard Hinnant99968442011-11-29 18:15:50 +0000103template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000104inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000105__mem_fn<_Rp (_Tp::*)() const>
Howard Hinnant99968442011-11-29 18:15:50 +0000106mem_fn(_Rp (_Tp::* __pm)() const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107{
Richard Smith01fbfc22013-07-23 01:24:30 +0000108 return __mem_fn<_Rp (_Tp::*)() const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109}
110
Howard Hinnant99968442011-11-29 18:15:50 +0000111template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000113__mem_fn<_Rp (_Tp::*)(_A0) const>
Howard Hinnant99968442011-11-29 18:15:50 +0000114mem_fn(_Rp (_Tp::* __pm)(_A0) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115{
Richard Smith01fbfc22013-07-23 01:24:30 +0000116 return __mem_fn<_Rp (_Tp::*)(_A0) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117}
118
Howard Hinnant99968442011-11-29 18:15:50 +0000119template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000121__mem_fn<_Rp (_Tp::*)(_A0, _A1) const>
Howard Hinnant99968442011-11-29 18:15:50 +0000122mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123{
Richard Smith01fbfc22013-07-23 01:24:30 +0000124 return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000125}
126
Howard Hinnant99968442011-11-29 18:15:50 +0000127template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000129__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>
Howard Hinnant99968442011-11-29 18:15:50 +0000130mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131{
Richard Smith01fbfc22013-07-23 01:24:30 +0000132 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133}
134
Howard Hinnant99968442011-11-29 18:15:50 +0000135template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000137__mem_fn<_Rp (_Tp::*)() volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000138mem_fn(_Rp (_Tp::* __pm)() volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139{
Richard Smith01fbfc22013-07-23 01:24:30 +0000140 return __mem_fn<_Rp (_Tp::*)() volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141}
142
Howard Hinnant99968442011-11-29 18:15:50 +0000143template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000145__mem_fn<_Rp (_Tp::*)(_A0) volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000146mem_fn(_Rp (_Tp::* __pm)(_A0) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147{
Richard Smith01fbfc22013-07-23 01:24:30 +0000148 return __mem_fn<_Rp (_Tp::*)(_A0) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000149}
150
Howard Hinnant99968442011-11-29 18:15:50 +0000151template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000153__mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000154mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155{
Richard Smith01fbfc22013-07-23 01:24:30 +0000156 return __mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157}
158
Howard Hinnant99968442011-11-29 18:15:50 +0000159template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000161__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000162mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163{
Richard Smith01fbfc22013-07-23 01:24:30 +0000164 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165}
166
Howard Hinnant99968442011-11-29 18:15:50 +0000167template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000168inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000169__mem_fn<_Rp (_Tp::*)() const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000170mem_fn(_Rp (_Tp::* __pm)() const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171{
Richard Smith01fbfc22013-07-23 01:24:30 +0000172 return __mem_fn<_Rp (_Tp::*)() const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173}
174
Howard Hinnant99968442011-11-29 18:15:50 +0000175template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000177__mem_fn<_Rp (_Tp::*)(_A0) const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000178mem_fn(_Rp (_Tp::* __pm)(_A0) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179{
Richard Smith01fbfc22013-07-23 01:24:30 +0000180 return __mem_fn<_Rp (_Tp::*)(_A0) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000181}
182
Howard Hinnant99968442011-11-29 18:15:50 +0000183template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000185__mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000186mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187{
Richard Smith01fbfc22013-07-23 01:24:30 +0000188 return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189}
190
Howard Hinnant99968442011-11-29 18:15:50 +0000191template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000193__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000194mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195{
Richard Smith01fbfc22013-07-23 01:24:30 +0000196 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197}
198
199// bad_function_call
200
Howard Hinnant99acc502010-09-21 17:32:39 +0000201class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 : public exception
203{
204};
205
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000206template<class _Fp> class _LIBCPP_TYPE_VIS_ONLY function; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207
208namespace __function
209{
210
Howard Hinnant99968442011-11-29 18:15:50 +0000211template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212struct __maybe_derive_from_unary_function
213{
214};
215
Howard Hinnant99968442011-11-29 18:15:50 +0000216template<class _Rp, class _A1>
217struct __maybe_derive_from_unary_function<_Rp(_A1)>
218 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219{
220};
221
Howard Hinnant99968442011-11-29 18:15:50 +0000222template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223struct __maybe_derive_from_binary_function
224{
225};
226
Howard Hinnant99968442011-11-29 18:15:50 +0000227template<class _Rp, class _A1, class _A2>
228struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
229 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230{
231};
232
233template<class _Fp> class __base;
234
Howard Hinnant99968442011-11-29 18:15:50 +0000235template<class _Rp>
236class __base<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237{
238 __base(const __base&);
239 __base& operator=(const __base&);
240public:
241 __base() {}
242 virtual ~__base() {}
243 virtual __base* __clone() const = 0;
244 virtual void __clone(__base*) const = 0;
245 virtual void destroy() = 0;
246 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000247 virtual _Rp operator()() = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000248#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249 virtual const void* target(const type_info&) const = 0;
250 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000251#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252};
253
Howard Hinnant99968442011-11-29 18:15:50 +0000254template<class _Rp, class _A0>
255class __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256{
257 __base(const __base&);
258 __base& operator=(const __base&);
259public:
260 __base() {}
261 virtual ~__base() {}
262 virtual __base* __clone() const = 0;
263 virtual void __clone(__base*) const = 0;
264 virtual void destroy() = 0;
265 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000266 virtual _Rp operator()(_A0) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000267#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268 virtual const void* target(const type_info&) const = 0;
269 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000270#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271};
272
Howard Hinnant99968442011-11-29 18:15:50 +0000273template<class _Rp, class _A0, class _A1>
274class __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275{
276 __base(const __base&);
277 __base& operator=(const __base&);
278public:
279 __base() {}
280 virtual ~__base() {}
281 virtual __base* __clone() const = 0;
282 virtual void __clone(__base*) const = 0;
283 virtual void destroy() = 0;
284 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000285 virtual _Rp operator()(_A0, _A1) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000286#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287 virtual const void* target(const type_info&) const = 0;
288 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000289#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290};
291
Howard Hinnant99968442011-11-29 18:15:50 +0000292template<class _Rp, class _A0, class _A1, class _A2>
293class __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294{
295 __base(const __base&);
296 __base& operator=(const __base&);
297public:
298 __base() {}
299 virtual ~__base() {}
300 virtual __base* __clone() const = 0;
301 virtual void __clone(__base*) const = 0;
302 virtual void destroy() = 0;
303 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000304 virtual _Rp operator()(_A0, _A1, _A2) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000305#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 virtual const void* target(const type_info&) const = 0;
307 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000308#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309};
310
311template<class _FD, class _Alloc, class _FB> class __func;
312
Howard Hinnant99968442011-11-29 18:15:50 +0000313template<class _Fp, class _Alloc, class _Rp>
314class __func<_Fp, _Alloc, _Rp()>
315 : public __base<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316{
Howard Hinnant99968442011-11-29 18:15:50 +0000317 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000318public:
Howard Hinnant99968442011-11-29 18:15:50 +0000319 explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
320 explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
321 virtual __base<_Rp()>* __clone() const;
322 virtual void __clone(__base<_Rp()>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000323 virtual void destroy();
324 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000325 virtual _Rp operator()();
Howard Hinnantd4444702010-08-11 17:04:31 +0000326#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327 virtual const void* target(const type_info&) const;
328 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000329#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330};
331
Howard Hinnant99968442011-11-29 18:15:50 +0000332template<class _Fp, class _Alloc, class _Rp>
333__base<_Rp()>*
334__func<_Fp, _Alloc, _Rp()>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335{
Howard Hinnant99968442011-11-29 18:15:50 +0000336 typedef typename _Alloc::template rebind<__func>::other _Ap;
337 _Ap __a(__f_.second());
338 typedef __allocator_destructor<_Ap> _Dp;
339 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000340 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
341 return __hold.release();
342}
343
Howard Hinnant99968442011-11-29 18:15:50 +0000344template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345void
Howard Hinnant99968442011-11-29 18:15:50 +0000346__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347{
348 ::new (__p) __func(__f_.first(), __f_.second());
349}
350
Howard Hinnant99968442011-11-29 18:15:50 +0000351template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352void
Howard Hinnant99968442011-11-29 18:15:50 +0000353__func<_Fp, _Alloc, _Rp()>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354{
Howard Hinnant99968442011-11-29 18:15:50 +0000355 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356}
357
Howard Hinnant99968442011-11-29 18:15:50 +0000358template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000359void
Howard Hinnant99968442011-11-29 18:15:50 +0000360__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361{
Howard Hinnant99968442011-11-29 18:15:50 +0000362 typedef typename _Alloc::template rebind<__func>::other _Ap;
363 _Ap __a(__f_.second());
364 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365 __a.deallocate(this, 1);
366}
367
Howard Hinnant99968442011-11-29 18:15:50 +0000368template<class _Fp, class _Alloc, class _Rp>
369_Rp
370__func<_Fp, _Alloc, _Rp()>::operator()()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000372 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
373 return _Invoker::__call(__f_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374}
375
Howard Hinnantd4444702010-08-11 17:04:31 +0000376#ifndef _LIBCPP_NO_RTTI
377
Howard Hinnant99968442011-11-29 18:15:50 +0000378template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000380__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381{
Howard Hinnant99968442011-11-29 18:15:50 +0000382 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 return &__f_.first();
384 return (const void*)0;
385}
386
Howard Hinnant99968442011-11-29 18:15:50 +0000387template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000389__func<_Fp, _Alloc, _Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390{
Howard Hinnant99968442011-11-29 18:15:50 +0000391 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392}
393
Howard Hinnant324bb032010-08-22 00:02:43 +0000394#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000395
Howard Hinnant99968442011-11-29 18:15:50 +0000396template<class _Fp, class _Alloc, class _Rp, class _A0>
397class __func<_Fp, _Alloc, _Rp(_A0)>
398 : public __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399{
Howard Hinnant99968442011-11-29 18:15:50 +0000400 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401public:
Howard Hinnant99968442011-11-29 18:15:50 +0000402 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
403 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000404 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000405 virtual __base<_Rp(_A0)>* __clone() const;
406 virtual void __clone(__base<_Rp(_A0)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407 virtual void destroy();
408 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000409 virtual _Rp operator()(_A0);
Howard Hinnantd4444702010-08-11 17:04:31 +0000410#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 virtual const void* target(const type_info&) const;
412 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000413#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414};
415
Howard Hinnant99968442011-11-29 18:15:50 +0000416template<class _Fp, class _Alloc, class _Rp, class _A0>
417__base<_Rp(_A0)>*
418__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419{
Howard Hinnant99968442011-11-29 18:15:50 +0000420 typedef typename _Alloc::template rebind<__func>::other _Ap;
421 _Ap __a(__f_.second());
422 typedef __allocator_destructor<_Ap> _Dp;
423 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
425 return __hold.release();
426}
427
Howard Hinnant99968442011-11-29 18:15:50 +0000428template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429void
Howard Hinnant99968442011-11-29 18:15:50 +0000430__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000431{
432 ::new (__p) __func(__f_.first(), __f_.second());
433}
434
Howard Hinnant99968442011-11-29 18:15:50 +0000435template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436void
Howard Hinnant99968442011-11-29 18:15:50 +0000437__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438{
Howard Hinnant99968442011-11-29 18:15:50 +0000439 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440}
441
Howard Hinnant99968442011-11-29 18:15:50 +0000442template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443void
Howard Hinnant99968442011-11-29 18:15:50 +0000444__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445{
Howard Hinnant99968442011-11-29 18:15:50 +0000446 typedef typename _Alloc::template rebind<__func>::other _Ap;
447 _Ap __a(__f_.second());
448 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449 __a.deallocate(this, 1);
450}
451
Howard Hinnant99968442011-11-29 18:15:50 +0000452template<class _Fp, class _Alloc, class _Rp, class _A0>
453_Rp
454__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000456 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
457 return _Invoker::__call(__f_.first(), __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458}
459
Howard Hinnantd4444702010-08-11 17:04:31 +0000460#ifndef _LIBCPP_NO_RTTI
461
Howard Hinnant99968442011-11-29 18:15:50 +0000462template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000464__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465{
Howard Hinnant99968442011-11-29 18:15:50 +0000466 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 return &__f_.first();
468 return (const void*)0;
469}
470
Howard Hinnant99968442011-11-29 18:15:50 +0000471template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000473__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474{
Howard Hinnant99968442011-11-29 18:15:50 +0000475 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476}
477
Howard Hinnant324bb032010-08-22 00:02:43 +0000478#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000479
Howard Hinnant99968442011-11-29 18:15:50 +0000480template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
481class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
482 : public __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483{
Howard Hinnant99968442011-11-29 18:15:50 +0000484 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485public:
Howard Hinnant99968442011-11-29 18:15:50 +0000486 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
487 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000488 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000489 virtual __base<_Rp(_A0, _A1)>* __clone() const;
490 virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491 virtual void destroy();
492 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000493 virtual _Rp operator()(_A0, _A1);
Howard Hinnantd4444702010-08-11 17:04:31 +0000494#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495 virtual const void* target(const type_info&) const;
496 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000497#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498};
499
Howard Hinnant99968442011-11-29 18:15:50 +0000500template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
501__base<_Rp(_A0, _A1)>*
502__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503{
Howard Hinnant99968442011-11-29 18:15:50 +0000504 typedef typename _Alloc::template rebind<__func>::other _Ap;
505 _Ap __a(__f_.second());
506 typedef __allocator_destructor<_Ap> _Dp;
507 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
509 return __hold.release();
510}
511
Howard Hinnant99968442011-11-29 18:15:50 +0000512template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513void
Howard Hinnant99968442011-11-29 18:15:50 +0000514__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515{
516 ::new (__p) __func(__f_.first(), __f_.second());
517}
518
Howard Hinnant99968442011-11-29 18:15:50 +0000519template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520void
Howard Hinnant99968442011-11-29 18:15:50 +0000521__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522{
Howard Hinnant99968442011-11-29 18:15:50 +0000523 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524}
525
Howard Hinnant99968442011-11-29 18:15:50 +0000526template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527void
Howard Hinnant99968442011-11-29 18:15:50 +0000528__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529{
Howard Hinnant99968442011-11-29 18:15:50 +0000530 typedef typename _Alloc::template rebind<__func>::other _Ap;
531 _Ap __a(__f_.second());
532 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533 __a.deallocate(this, 1);
534}
535
Howard Hinnant99968442011-11-29 18:15:50 +0000536template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
537_Rp
538__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000540 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
541 return _Invoker::__call(__f_.first(), __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542}
543
Howard Hinnantd4444702010-08-11 17:04:31 +0000544#ifndef _LIBCPP_NO_RTTI
545
Howard Hinnant99968442011-11-29 18:15:50 +0000546template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000548__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549{
Howard Hinnant99968442011-11-29 18:15:50 +0000550 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000551 return &__f_.first();
552 return (const void*)0;
553}
554
Howard Hinnant99968442011-11-29 18:15:50 +0000555template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000557__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558{
Howard Hinnant99968442011-11-29 18:15:50 +0000559 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560}
561
Howard Hinnant324bb032010-08-22 00:02:43 +0000562#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000563
Howard Hinnant99968442011-11-29 18:15:50 +0000564template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
565class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
566 : public __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567{
Howard Hinnant99968442011-11-29 18:15:50 +0000568 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569public:
Howard Hinnant99968442011-11-29 18:15:50 +0000570 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
571 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000572 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000573 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
574 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575 virtual void destroy();
576 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000577 virtual _Rp operator()(_A0, _A1, _A2);
Howard Hinnantd4444702010-08-11 17:04:31 +0000578#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579 virtual const void* target(const type_info&) const;
580 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000581#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582};
583
Howard Hinnant99968442011-11-29 18:15:50 +0000584template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
585__base<_Rp(_A0, _A1, _A2)>*
586__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587{
Howard Hinnant99968442011-11-29 18:15:50 +0000588 typedef typename _Alloc::template rebind<__func>::other _Ap;
589 _Ap __a(__f_.second());
590 typedef __allocator_destructor<_Ap> _Dp;
591 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
593 return __hold.release();
594}
595
Howard Hinnant99968442011-11-29 18:15:50 +0000596template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597void
Howard Hinnant99968442011-11-29 18:15:50 +0000598__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599{
600 ::new (__p) __func(__f_.first(), __f_.second());
601}
602
Howard Hinnant99968442011-11-29 18:15:50 +0000603template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604void
Howard Hinnant99968442011-11-29 18:15:50 +0000605__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606{
Howard Hinnant99968442011-11-29 18:15:50 +0000607 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608}
609
Howard Hinnant99968442011-11-29 18:15:50 +0000610template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611void
Howard Hinnant99968442011-11-29 18:15:50 +0000612__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613{
Howard Hinnant99968442011-11-29 18:15:50 +0000614 typedef typename _Alloc::template rebind<__func>::other _Ap;
615 _Ap __a(__f_.second());
616 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617 __a.deallocate(this, 1);
618}
619
Howard Hinnant99968442011-11-29 18:15:50 +0000620template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
621_Rp
622__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000624 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
625 return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626}
627
Howard Hinnantd4444702010-08-11 17:04:31 +0000628#ifndef _LIBCPP_NO_RTTI
629
Howard Hinnant99968442011-11-29 18:15:50 +0000630template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000632__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633{
Howard Hinnant99968442011-11-29 18:15:50 +0000634 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635 return &__f_.first();
636 return (const void*)0;
637}
638
Howard Hinnant99968442011-11-29 18:15:50 +0000639template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000641__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642{
Howard Hinnant99968442011-11-29 18:15:50 +0000643 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644}
645
Howard Hinnant324bb032010-08-22 00:02:43 +0000646#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000647
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648} // __function
649
Howard Hinnant99968442011-11-29 18:15:50 +0000650template<class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000651class _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652{
Howard Hinnant99968442011-11-29 18:15:50 +0000653 typedef __function::__base<_Rp()> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 aligned_storage<3*sizeof(void*)>::type __buf_;
655 __base* __f_;
656
Howard Hinnant99968442011-11-29 18:15:50 +0000657 template <class _Fp>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000659 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660 template <class _R2>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000661 _LIBCPP_INLINE_VISIBILITY
662 static bool __not_null(_R2 (*__p)()) {return __p;}
663 template <class _R2>
664 _LIBCPP_INLINE_VISIBILITY
665 static bool __not_null(const function<_R2()>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666public:
Howard Hinnant99968442011-11-29 18:15:50 +0000667 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668
669 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000670 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
671 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000673 template<class _Fp>
674 function(_Fp,
675 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
Howard Hinnant72552802010-08-20 19:36:46 +0000677 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000679 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
680 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000682 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
683 template<class _Alloc>
684 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000685 template<class _Fp, class _Alloc>
686 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
687 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688
689 function& operator=(const function&);
690 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000691 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692 typename enable_if
693 <
Howard Hinnant99968442011-11-29 18:15:50 +0000694 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695 function&
696 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000697 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698
699 ~function();
700
701 // 20.7.16.2.2, function modifiers:
702 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000703 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000705 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +0000706 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707
708 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +0000709 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710
711private:
712 // deleted overloads close possible hole in the type system
713 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000714 bool operator==(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000716 bool operator!=(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717public:
718 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +0000719 _Rp operator()() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720
Howard Hinnantd4444702010-08-11 17:04:31 +0000721#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722 // 20.7.16.2.5, function target access:
723 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +0000724 template <typename _Tp> _Tp* target();
725 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000726#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727};
728
Howard Hinnant99968442011-11-29 18:15:50 +0000729template<class _Rp>
730function<_Rp()>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731{
732 if (__f.__f_ == 0)
733 __f_ = 0;
734 else if (__f.__f_ == (const __base*)&__f.__buf_)
735 {
736 __f_ = (__base*)&__buf_;
737 __f.__f_->__clone(__f_);
738 }
739 else
740 __f_ = __f.__f_->__clone();
741}
742
Howard Hinnant99968442011-11-29 18:15:50 +0000743template<class _Rp>
Howard Hinnant72552802010-08-20 19:36:46 +0000744template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +0000745function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +0000746{
747 if (__f.__f_ == 0)
748 __f_ = 0;
749 else if (__f.__f_ == (const __base*)&__f.__buf_)
750 {
751 __f_ = (__base*)&__buf_;
752 __f.__f_->__clone(__f_);
753 }
754 else
755 __f_ = __f.__f_->__clone();
756}
757
Howard Hinnant99968442011-11-29 18:15:50 +0000758template<class _Rp>
759template <class _Fp>
760function<_Rp()>::function(_Fp __f,
761 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000762 : __f_(0)
763{
764 if (__not_null(__f))
765 {
Howard Hinnant99968442011-11-29 18:15:50 +0000766 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767 if (sizeof(_FF) <= sizeof(__buf_))
768 {
769 __f_ = (__base*)&__buf_;
770 ::new (__f_) _FF(__f);
771 }
772 else
773 {
Howard Hinnant99968442011-11-29 18:15:50 +0000774 typedef allocator<_FF> _Ap;
775 _Ap __a;
776 typedef __allocator_destructor<_Ap> _Dp;
777 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
778 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 __f_ = __hold.release();
780 }
781 }
782}
783
Howard Hinnant99968442011-11-29 18:15:50 +0000784template<class _Rp>
785template <class _Fp, class _Alloc>
786function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
787 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +0000788 : __f_(0)
789{
790 typedef allocator_traits<_Alloc> __alloc_traits;
791 if (__not_null(__f))
792 {
Howard Hinnant99968442011-11-29 18:15:50 +0000793 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +0000794 if (sizeof(_FF) <= sizeof(__buf_))
795 {
796 __f_ = (__base*)&__buf_;
797 ::new (__f_) _FF(__f);
798 }
799 else
800 {
801 typedef typename __alloc_traits::template
802#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
803 rebind_alloc<_FF>
804#else
805 rebind_alloc<_FF>::other
806#endif
Howard Hinnant99968442011-11-29 18:15:50 +0000807 _Ap;
808 _Ap __a(__a0);
809 typedef __allocator_destructor<_Ap> _Dp;
810 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +0000811 ::new (__hold.get()) _FF(__f, _Alloc(__a));
812 __f_ = __hold.release();
813 }
814 }
815}
816
Howard Hinnant99968442011-11-29 18:15:50 +0000817template<class _Rp>
818function<_Rp()>&
819function<_Rp()>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000820{
821 function(__f).swap(*this);
822 return *this;
823}
824
Howard Hinnant99968442011-11-29 18:15:50 +0000825template<class _Rp>
826function<_Rp()>&
827function<_Rp()>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828{
829 if (__f_ == (__base*)&__buf_)
830 __f_->destroy();
831 else if (__f_)
832 __f_->destroy_deallocate();
833 __f_ = 0;
834}
835
Howard Hinnant99968442011-11-29 18:15:50 +0000836template<class _Rp>
837template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838typename enable_if
839<
Howard Hinnant99968442011-11-29 18:15:50 +0000840 !is_integral<_Fp>::value,
841 function<_Rp()>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842>::type
Howard Hinnant99968442011-11-29 18:15:50 +0000843function<_Rp()>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000844{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000845 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000846 return *this;
847}
848
Howard Hinnant99968442011-11-29 18:15:50 +0000849template<class _Rp>
850function<_Rp()>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000851{
852 if (__f_ == (__base*)&__buf_)
853 __f_->destroy();
854 else if (__f_)
855 __f_->destroy_deallocate();
856}
857
Howard Hinnant99968442011-11-29 18:15:50 +0000858template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000859void
Howard Hinnant99968442011-11-29 18:15:50 +0000860function<_Rp()>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861{
862 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
863 {
864 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
865 __base* __t = (__base*)&__tempbuf;
866 __f_->__clone(__t);
867 __f_->destroy();
868 __f_ = 0;
869 __f.__f_->__clone((__base*)&__buf_);
870 __f.__f_->destroy();
871 __f.__f_ = 0;
872 __f_ = (__base*)&__buf_;
873 __t->__clone((__base*)&__f.__buf_);
874 __t->destroy();
875 __f.__f_ = (__base*)&__f.__buf_;
876 }
877 else if (__f_ == (__base*)&__buf_)
878 {
879 __f_->__clone((__base*)&__f.__buf_);
880 __f_->destroy();
881 __f_ = __f.__f_;
882 __f.__f_ = (__base*)&__f.__buf_;
883 }
884 else if (__f.__f_ == (__base*)&__f.__buf_)
885 {
886 __f.__f_->__clone((__base*)&__buf_);
887 __f.__f_->destroy();
888 __f.__f_ = __f_;
889 __f_ = (__base*)&__buf_;
890 }
891 else
Howard Hinnant0949eed2011-06-30 21:18:19 +0000892 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893}
894
Howard Hinnant99968442011-11-29 18:15:50 +0000895template<class _Rp>
896_Rp
897function<_Rp()>::operator()() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898{
Howard Hinnantd4444702010-08-11 17:04:31 +0000899#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900 if (__f_ == 0)
901 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +0000902#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903 return (*__f_)();
904}
905
Howard Hinnantd4444702010-08-11 17:04:31 +0000906#ifndef _LIBCPP_NO_RTTI
907
Howard Hinnant99968442011-11-29 18:15:50 +0000908template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000910function<_Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911{
912 if (__f_ == 0)
913 return typeid(void);
914 return __f_->target_type();
915}
916
Howard Hinnant99968442011-11-29 18:15:50 +0000917template<class _Rp>
918template <typename _Tp>
919_Tp*
920function<_Rp()>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921{
922 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000923 return (_Tp*)0;
924 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925}
926
Howard Hinnant99968442011-11-29 18:15:50 +0000927template<class _Rp>
928template <typename _Tp>
929const _Tp*
930function<_Rp()>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931{
932 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000933 return (const _Tp*)0;
934 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935}
936
Howard Hinnant324bb032010-08-22 00:02:43 +0000937#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000938
Howard Hinnant99968442011-11-29 18:15:50 +0000939template<class _Rp, class _A0>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000940class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
Howard Hinnant99968442011-11-29 18:15:50 +0000941 : public unary_function<_A0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942{
Howard Hinnant99968442011-11-29 18:15:50 +0000943 typedef __function::__base<_Rp(_A0)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944 aligned_storage<3*sizeof(void*)>::type __buf_;
945 __base* __f_;
946
Howard Hinnant99968442011-11-29 18:15:50 +0000947 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000949 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000952 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +0000953 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000955 static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
956 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000958 static bool __not_null(_R2 (_Cp::*__p)() const) {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)() volatile) {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)() const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000966 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +0000967 static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968public:
Howard Hinnant99968442011-11-29 18:15:50 +0000969 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970
971 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000972 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
973 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000975 template<class _Fp>
976 function(_Fp,
977 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978
Howard Hinnant72552802010-08-20 19:36:46 +0000979 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000981 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
982 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&, nullptr_t) : __f_(0) {}
985 template<class _Alloc>
986 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000987 template<class _Fp, class _Alloc>
988 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
989 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990
991 function& operator=(const function&);
992 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000993 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994 typename enable_if
995 <
Howard Hinnant99968442011-11-29 18:15:50 +0000996 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997 function&
998 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000999 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000
1001 ~function();
1002
1003 // 20.7.16.2.2, function modifiers:
1004 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001005 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001007 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001008 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009
1010 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001011 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012
1013private:
1014 // deleted overloads close possible hole in the type system
1015 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001016 bool operator==(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001018 bool operator!=(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019public:
1020 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001021 _Rp operator()(_A0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022
Howard Hinnantd4444702010-08-11 17:04:31 +00001023#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024 // 20.7.16.2.5, function target access:
1025 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001026 template <typename _Tp> _Tp* target();
1027 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001028#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001029};
1030
Howard Hinnant99968442011-11-29 18:15:50 +00001031template<class _Rp, class _A0>
1032function<_Rp(_A0)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033{
1034 if (__f.__f_ == 0)
1035 __f_ = 0;
1036 else if (__f.__f_ == (const __base*)&__f.__buf_)
1037 {
1038 __f_ = (__base*)&__buf_;
1039 __f.__f_->__clone(__f_);
1040 }
1041 else
1042 __f_ = __f.__f_->__clone();
1043}
1044
Howard Hinnant99968442011-11-29 18:15:50 +00001045template<class _Rp, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001046template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001047function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001048{
1049 if (__f.__f_ == 0)
1050 __f_ = 0;
1051 else if (__f.__f_ == (const __base*)&__f.__buf_)
1052 {
1053 __f_ = (__base*)&__buf_;
1054 __f.__f_->__clone(__f_);
1055 }
1056 else
1057 __f_ = __f.__f_->__clone();
1058}
1059
Howard Hinnant99968442011-11-29 18:15:50 +00001060template<class _Rp, class _A0>
1061template <class _Fp>
1062function<_Rp(_A0)>::function(_Fp __f,
1063 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064 : __f_(0)
1065{
1066 if (__not_null(__f))
1067 {
Howard Hinnant99968442011-11-29 18:15:50 +00001068 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 if (sizeof(_FF) <= sizeof(__buf_))
1070 {
1071 __f_ = (__base*)&__buf_;
1072 ::new (__f_) _FF(__f);
1073 }
1074 else
1075 {
Howard Hinnant99968442011-11-29 18:15:50 +00001076 typedef allocator<_FF> _Ap;
1077 _Ap __a;
1078 typedef __allocator_destructor<_Ap> _Dp;
1079 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1080 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081 __f_ = __hold.release();
1082 }
1083 }
1084}
1085
Howard Hinnant99968442011-11-29 18:15:50 +00001086template<class _Rp, class _A0>
1087template <class _Fp, class _Alloc>
1088function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1089 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001090 : __f_(0)
1091{
1092 typedef allocator_traits<_Alloc> __alloc_traits;
1093 if (__not_null(__f))
1094 {
Howard Hinnant99968442011-11-29 18:15:50 +00001095 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001096 if (sizeof(_FF) <= sizeof(__buf_))
1097 {
1098 __f_ = (__base*)&__buf_;
1099 ::new (__f_) _FF(__f);
1100 }
1101 else
1102 {
1103 typedef typename __alloc_traits::template
1104#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1105 rebind_alloc<_FF>
1106#else
1107 rebind_alloc<_FF>::other
1108#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001109 _Ap;
1110 _Ap __a(__a0);
1111 typedef __allocator_destructor<_Ap> _Dp;
1112 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001113 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1114 __f_ = __hold.release();
1115 }
1116 }
1117}
1118
Howard Hinnant99968442011-11-29 18:15:50 +00001119template<class _Rp, class _A0>
1120function<_Rp(_A0)>&
1121function<_Rp(_A0)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122{
1123 function(__f).swap(*this);
1124 return *this;
1125}
1126
Howard Hinnant99968442011-11-29 18:15:50 +00001127template<class _Rp, class _A0>
1128function<_Rp(_A0)>&
1129function<_Rp(_A0)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130{
1131 if (__f_ == (__base*)&__buf_)
1132 __f_->destroy();
1133 else if (__f_)
1134 __f_->destroy_deallocate();
1135 __f_ = 0;
1136}
1137
Howard Hinnant99968442011-11-29 18:15:50 +00001138template<class _Rp, class _A0>
1139template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140typename enable_if
1141<
Howard Hinnant99968442011-11-29 18:15:50 +00001142 !is_integral<_Fp>::value,
1143 function<_Rp(_A0)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001145function<_Rp(_A0)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001147 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148 return *this;
1149}
1150
Howard Hinnant99968442011-11-29 18:15:50 +00001151template<class _Rp, class _A0>
1152function<_Rp(_A0)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153{
1154 if (__f_ == (__base*)&__buf_)
1155 __f_->destroy();
1156 else if (__f_)
1157 __f_->destroy_deallocate();
1158}
1159
Howard Hinnant99968442011-11-29 18:15:50 +00001160template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161void
Howard Hinnant99968442011-11-29 18:15:50 +00001162function<_Rp(_A0)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163{
1164 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1165 {
1166 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1167 __base* __t = (__base*)&__tempbuf;
1168 __f_->__clone(__t);
1169 __f_->destroy();
1170 __f_ = 0;
1171 __f.__f_->__clone((__base*)&__buf_);
1172 __f.__f_->destroy();
1173 __f.__f_ = 0;
1174 __f_ = (__base*)&__buf_;
1175 __t->__clone((__base*)&__f.__buf_);
1176 __t->destroy();
1177 __f.__f_ = (__base*)&__f.__buf_;
1178 }
1179 else if (__f_ == (__base*)&__buf_)
1180 {
1181 __f_->__clone((__base*)&__f.__buf_);
1182 __f_->destroy();
1183 __f_ = __f.__f_;
1184 __f.__f_ = (__base*)&__f.__buf_;
1185 }
1186 else if (__f.__f_ == (__base*)&__f.__buf_)
1187 {
1188 __f.__f_->__clone((__base*)&__buf_);
1189 __f.__f_->destroy();
1190 __f.__f_ = __f_;
1191 __f_ = (__base*)&__buf_;
1192 }
1193 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001194 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195}
1196
Howard Hinnant99968442011-11-29 18:15:50 +00001197template<class _Rp, class _A0>
1198_Rp
1199function<_Rp(_A0)>::operator()(_A0 __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200{
Howard Hinnantd4444702010-08-11 17:04:31 +00001201#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202 if (__f_ == 0)
1203 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001204#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001205 return (*__f_)(__a0);
1206}
1207
Howard Hinnantd4444702010-08-11 17:04:31 +00001208#ifndef _LIBCPP_NO_RTTI
1209
Howard Hinnant99968442011-11-29 18:15:50 +00001210template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001211const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001212function<_Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213{
1214 if (__f_ == 0)
1215 return typeid(void);
1216 return __f_->target_type();
1217}
1218
Howard Hinnant99968442011-11-29 18:15:50 +00001219template<class _Rp, class _A0>
1220template <typename _Tp>
1221_Tp*
1222function<_Rp(_A0)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001223{
1224 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001225 return (_Tp*)0;
1226 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001227}
1228
Howard Hinnant99968442011-11-29 18:15:50 +00001229template<class _Rp, class _A0>
1230template <typename _Tp>
1231const _Tp*
1232function<_Rp(_A0)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233{
1234 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001235 return (const _Tp*)0;
1236 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237}
1238
Howard Hinnant324bb032010-08-22 00:02:43 +00001239#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001240
Howard Hinnant99968442011-11-29 18:15:50 +00001241template<class _Rp, class _A0, class _A1>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001242class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
Howard Hinnant99968442011-11-29 18:15:50 +00001243 : public binary_function<_A0, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244{
Howard Hinnant99968442011-11-29 18:15:50 +00001245 typedef __function::__base<_Rp(_A0, _A1)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246 aligned_storage<3*sizeof(void*)>::type __buf_;
1247 __base* __f_;
1248
Howard Hinnant99968442011-11-29 18:15:50 +00001249 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001251 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001254 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001255 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001257 static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1258 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001260 static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1261 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001263 static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1264 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001266 static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001268 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001269 static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270public:
Howard Hinnant99968442011-11-29 18:15:50 +00001271 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272
1273 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001274 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1275 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001277 template<class _Fp>
1278 function(_Fp,
1279 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280
Howard Hinnant72552802010-08-20 19:36:46 +00001281 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001283 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1284 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001286 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1287 template<class _Alloc>
1288 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001289 template<class _Fp, class _Alloc>
1290 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1291 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292
1293 function& operator=(const function&);
1294 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001295 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296 typename enable_if
1297 <
Howard Hinnant99968442011-11-29 18:15:50 +00001298 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299 function&
1300 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001301 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302
1303 ~function();
1304
1305 // 20.7.16.2.2, function modifiers:
1306 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001307 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001309 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001310 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311
1312 // 20.7.16.2.3, function capacity:
1313 operator bool() const {return __f_;}
1314
1315private:
1316 // deleted overloads close possible hole in the type system
1317 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 +00001319 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001320 bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321public:
1322 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001323 _Rp operator()(_A0, _A1) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324
Howard Hinnantd4444702010-08-11 17:04:31 +00001325#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 // 20.7.16.2.5, function target access:
1327 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001328 template <typename _Tp> _Tp* target();
1329 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001330#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331};
1332
Howard Hinnant99968442011-11-29 18:15:50 +00001333template<class _Rp, class _A0, class _A1>
1334function<_Rp(_A0, _A1)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335{
1336 if (__f.__f_ == 0)
1337 __f_ = 0;
1338 else if (__f.__f_ == (const __base*)&__f.__buf_)
1339 {
1340 __f_ = (__base*)&__buf_;
1341 __f.__f_->__clone(__f_);
1342 }
1343 else
1344 __f_ = __f.__f_->__clone();
1345}
1346
Howard Hinnant99968442011-11-29 18:15:50 +00001347template<class _Rp, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001348template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001349function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001350{
1351 if (__f.__f_ == 0)
1352 __f_ = 0;
1353 else if (__f.__f_ == (const __base*)&__f.__buf_)
1354 {
1355 __f_ = (__base*)&__buf_;
1356 __f.__f_->__clone(__f_);
1357 }
1358 else
1359 __f_ = __f.__f_->__clone();
1360}
1361
Howard Hinnant99968442011-11-29 18:15:50 +00001362template<class _Rp, class _A0, class _A1>
1363template <class _Fp>
1364function<_Rp(_A0, _A1)>::function(_Fp __f,
1365 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366 : __f_(0)
1367{
1368 if (__not_null(__f))
1369 {
Howard Hinnant99968442011-11-29 18:15:50 +00001370 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371 if (sizeof(_FF) <= sizeof(__buf_))
1372 {
1373 __f_ = (__base*)&__buf_;
1374 ::new (__f_) _FF(__f);
1375 }
1376 else
1377 {
Howard Hinnant99968442011-11-29 18:15:50 +00001378 typedef allocator<_FF> _Ap;
1379 _Ap __a;
1380 typedef __allocator_destructor<_Ap> _Dp;
1381 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1382 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383 __f_ = __hold.release();
1384 }
1385 }
1386}
1387
Howard Hinnant99968442011-11-29 18:15:50 +00001388template<class _Rp, class _A0, class _A1>
1389template <class _Fp, class _Alloc>
1390function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1391 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001392 : __f_(0)
1393{
1394 typedef allocator_traits<_Alloc> __alloc_traits;
1395 if (__not_null(__f))
1396 {
Howard Hinnant99968442011-11-29 18:15:50 +00001397 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001398 if (sizeof(_FF) <= sizeof(__buf_))
1399 {
1400 __f_ = (__base*)&__buf_;
1401 ::new (__f_) _FF(__f);
1402 }
1403 else
1404 {
1405 typedef typename __alloc_traits::template
1406#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1407 rebind_alloc<_FF>
1408#else
1409 rebind_alloc<_FF>::other
1410#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001411 _Ap;
1412 _Ap __a(__a0);
1413 typedef __allocator_destructor<_Ap> _Dp;
1414 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001415 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1416 __f_ = __hold.release();
1417 }
1418 }
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=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424{
1425 function(__f).swap(*this);
1426 return *this;
1427}
1428
Howard Hinnant99968442011-11-29 18:15:50 +00001429template<class _Rp, class _A0, class _A1>
1430function<_Rp(_A0, _A1)>&
1431function<_Rp(_A0, _A1)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432{
1433 if (__f_ == (__base*)&__buf_)
1434 __f_->destroy();
1435 else if (__f_)
1436 __f_->destroy_deallocate();
1437 __f_ = 0;
1438}
1439
Howard Hinnant99968442011-11-29 18:15:50 +00001440template<class _Rp, class _A0, class _A1>
1441template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442typename enable_if
1443<
Howard Hinnant99968442011-11-29 18:15:50 +00001444 !is_integral<_Fp>::value,
1445 function<_Rp(_A0, _A1)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001447function<_Rp(_A0, _A1)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001449 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450 return *this;
1451}
1452
Howard Hinnant99968442011-11-29 18:15:50 +00001453template<class _Rp, class _A0, class _A1>
1454function<_Rp(_A0, _A1)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455{
1456 if (__f_ == (__base*)&__buf_)
1457 __f_->destroy();
1458 else if (__f_)
1459 __f_->destroy_deallocate();
1460}
1461
Howard Hinnant99968442011-11-29 18:15:50 +00001462template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463void
Howard Hinnant99968442011-11-29 18:15:50 +00001464function<_Rp(_A0, _A1)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465{
1466 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1467 {
1468 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1469 __base* __t = (__base*)&__tempbuf;
1470 __f_->__clone(__t);
1471 __f_->destroy();
1472 __f_ = 0;
1473 __f.__f_->__clone((__base*)&__buf_);
1474 __f.__f_->destroy();
1475 __f.__f_ = 0;
1476 __f_ = (__base*)&__buf_;
1477 __t->__clone((__base*)&__f.__buf_);
1478 __t->destroy();
1479 __f.__f_ = (__base*)&__f.__buf_;
1480 }
1481 else if (__f_ == (__base*)&__buf_)
1482 {
1483 __f_->__clone((__base*)&__f.__buf_);
1484 __f_->destroy();
1485 __f_ = __f.__f_;
1486 __f.__f_ = (__base*)&__f.__buf_;
1487 }
1488 else if (__f.__f_ == (__base*)&__f.__buf_)
1489 {
1490 __f.__f_->__clone((__base*)&__buf_);
1491 __f.__f_->destroy();
1492 __f.__f_ = __f_;
1493 __f_ = (__base*)&__buf_;
1494 }
1495 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001496 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497}
1498
Howard Hinnant99968442011-11-29 18:15:50 +00001499template<class _Rp, class _A0, class _A1>
1500_Rp
1501function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502{
Howard Hinnantd4444702010-08-11 17:04:31 +00001503#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 if (__f_ == 0)
1505 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001506#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507 return (*__f_)(__a0, __a1);
1508}
1509
Howard Hinnantd4444702010-08-11 17:04:31 +00001510#ifndef _LIBCPP_NO_RTTI
1511
Howard Hinnant99968442011-11-29 18:15:50 +00001512template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001514function<_Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515{
1516 if (__f_ == 0)
1517 return typeid(void);
1518 return __f_->target_type();
1519}
1520
Howard Hinnant99968442011-11-29 18:15:50 +00001521template<class _Rp, class _A0, class _A1>
1522template <typename _Tp>
1523_Tp*
1524function<_Rp(_A0, _A1)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525{
1526 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001527 return (_Tp*)0;
1528 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529}
1530
Howard Hinnant99968442011-11-29 18:15:50 +00001531template<class _Rp, class _A0, class _A1>
1532template <typename _Tp>
1533const _Tp*
1534function<_Rp(_A0, _A1)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535{
1536 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001537 return (const _Tp*)0;
1538 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539}
1540
Howard Hinnant324bb032010-08-22 00:02:43 +00001541#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001542
Howard Hinnant99968442011-11-29 18:15:50 +00001543template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001544class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545{
Howard Hinnant99968442011-11-29 18:15:50 +00001546 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547 aligned_storage<3*sizeof(void*)>::type __buf_;
1548 __base* __f_;
1549
Howard Hinnant99968442011-11-29 18:15:50 +00001550 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001552 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001556 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001558 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1559 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001561 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1562 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001564 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1565 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001567 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001569 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001570 static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571public:
Howard Hinnant99968442011-11-29 18:15:50 +00001572 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573
1574 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001575 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1576 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001578 template<class _Fp>
1579 function(_Fp,
1580 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001581
Howard Hinnant72552802010-08-20 19:36:46 +00001582 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001584 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1585 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001587 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1588 template<class _Alloc>
1589 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001590 template<class _Fp, class _Alloc>
1591 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1592 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593
1594 function& operator=(const function&);
1595 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001596 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 typename enable_if
1598 <
Howard Hinnant99968442011-11-29 18:15:50 +00001599 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 function&
1601 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001602 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603
1604 ~function();
1605
1606 // 20.7.16.2.2, function modifiers:
1607 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001608 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001610 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001611 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001612
1613 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001614 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615
1616private:
1617 // deleted overloads close possible hole in the type system
1618 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001619 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001621 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622public:
1623 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001624 _Rp operator()(_A0, _A1, _A2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625
Howard Hinnantd4444702010-08-11 17:04:31 +00001626#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627 // 20.7.16.2.5, function target access:
1628 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001629 template <typename _Tp> _Tp* target();
1630 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001631#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632};
1633
Howard Hinnant99968442011-11-29 18:15:50 +00001634template<class _Rp, class _A0, class _A1, class _A2>
1635function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636{
1637 if (__f.__f_ == 0)
1638 __f_ = 0;
1639 else if (__f.__f_ == (const __base*)&__f.__buf_)
1640 {
1641 __f_ = (__base*)&__buf_;
1642 __f.__f_->__clone(__f_);
1643 }
1644 else
1645 __f_ = __f.__f_->__clone();
1646}
1647
Howard Hinnant99968442011-11-29 18:15:50 +00001648template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001649template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001650function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnant72552802010-08-20 19:36:46 +00001651 const function& __f)
1652{
1653 if (__f.__f_ == 0)
1654 __f_ = 0;
1655 else if (__f.__f_ == (const __base*)&__f.__buf_)
1656 {
1657 __f_ = (__base*)&__buf_;
1658 __f.__f_->__clone(__f_);
1659 }
1660 else
1661 __f_ = __f.__f_->__clone();
1662}
1663
Howard Hinnant99968442011-11-29 18:15:50 +00001664template<class _Rp, class _A0, class _A1, class _A2>
1665template <class _Fp>
1666function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1667 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668 : __f_(0)
1669{
1670 if (__not_null(__f))
1671 {
Howard Hinnant99968442011-11-29 18:15:50 +00001672 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673 if (sizeof(_FF) <= sizeof(__buf_))
1674 {
1675 __f_ = (__base*)&__buf_;
1676 ::new (__f_) _FF(__f);
1677 }
1678 else
1679 {
Howard Hinnant99968442011-11-29 18:15:50 +00001680 typedef allocator<_FF> _Ap;
1681 _Ap __a;
1682 typedef __allocator_destructor<_Ap> _Dp;
1683 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1684 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685 __f_ = __hold.release();
1686 }
1687 }
1688}
1689
Howard Hinnant99968442011-11-29 18:15:50 +00001690template<class _Rp, class _A0, class _A1, class _A2>
1691template <class _Fp, class _Alloc>
1692function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1693 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001694 : __f_(0)
1695{
1696 typedef allocator_traits<_Alloc> __alloc_traits;
1697 if (__not_null(__f))
1698 {
Howard Hinnant99968442011-11-29 18:15:50 +00001699 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001700 if (sizeof(_FF) <= sizeof(__buf_))
1701 {
1702 __f_ = (__base*)&__buf_;
1703 ::new (__f_) _FF(__f);
1704 }
1705 else
1706 {
1707 typedef typename __alloc_traits::template
1708#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1709 rebind_alloc<_FF>
1710#else
1711 rebind_alloc<_FF>::other
1712#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001713 _Ap;
1714 _Ap __a(__a0);
1715 typedef __allocator_destructor<_Ap> _Dp;
1716 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001717 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1718 __f_ = __hold.release();
1719 }
1720 }
1721}
1722
Howard Hinnant99968442011-11-29 18:15:50 +00001723template<class _Rp, class _A0, class _A1, class _A2>
1724function<_Rp(_A0, _A1, _A2)>&
1725function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726{
1727 function(__f).swap(*this);
1728 return *this;
1729}
1730
Howard Hinnant99968442011-11-29 18:15:50 +00001731template<class _Rp, class _A0, class _A1, class _A2>
1732function<_Rp(_A0, _A1, _A2)>&
1733function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734{
1735 if (__f_ == (__base*)&__buf_)
1736 __f_->destroy();
1737 else if (__f_)
1738 __f_->destroy_deallocate();
1739 __f_ = 0;
1740}
1741
Howard Hinnant99968442011-11-29 18:15:50 +00001742template<class _Rp, class _A0, class _A1, class _A2>
1743template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744typename enable_if
1745<
Howard Hinnant99968442011-11-29 18:15:50 +00001746 !is_integral<_Fp>::value,
1747 function<_Rp(_A0, _A1, _A2)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001749function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001750{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001751 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752 return *this;
1753}
1754
Howard Hinnant99968442011-11-29 18:15:50 +00001755template<class _Rp, class _A0, class _A1, class _A2>
1756function<_Rp(_A0, _A1, _A2)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757{
1758 if (__f_ == (__base*)&__buf_)
1759 __f_->destroy();
1760 else if (__f_)
1761 __f_->destroy_deallocate();
1762}
1763
Howard Hinnant99968442011-11-29 18:15:50 +00001764template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765void
Howard Hinnant99968442011-11-29 18:15:50 +00001766function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001767{
1768 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1769 {
1770 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1771 __base* __t = (__base*)&__tempbuf;
1772 __f_->__clone(__t);
1773 __f_->destroy();
1774 __f_ = 0;
1775 __f.__f_->__clone((__base*)&__buf_);
1776 __f.__f_->destroy();
1777 __f.__f_ = 0;
1778 __f_ = (__base*)&__buf_;
1779 __t->__clone((__base*)&__f.__buf_);
1780 __t->destroy();
1781 __f.__f_ = (__base*)&__f.__buf_;
1782 }
1783 else if (__f_ == (__base*)&__buf_)
1784 {
1785 __f_->__clone((__base*)&__f.__buf_);
1786 __f_->destroy();
1787 __f_ = __f.__f_;
1788 __f.__f_ = (__base*)&__f.__buf_;
1789 }
1790 else if (__f.__f_ == (__base*)&__f.__buf_)
1791 {
1792 __f.__f_->__clone((__base*)&__buf_);
1793 __f.__f_->destroy();
1794 __f.__f_ = __f_;
1795 __f_ = (__base*)&__buf_;
1796 }
1797 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001798 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799}
1800
Howard Hinnant99968442011-11-29 18:15:50 +00001801template<class _Rp, class _A0, class _A1, class _A2>
1802_Rp
1803function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804{
Howard Hinnantd4444702010-08-11 17:04:31 +00001805#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001806 if (__f_ == 0)
1807 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001808#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 return (*__f_)(__a0, __a1, __a2);
1810}
1811
Howard Hinnantd4444702010-08-11 17:04:31 +00001812#ifndef _LIBCPP_NO_RTTI
1813
Howard Hinnant99968442011-11-29 18:15:50 +00001814template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001816function<_Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817{
1818 if (__f_ == 0)
1819 return typeid(void);
1820 return __f_->target_type();
1821}
1822
Howard Hinnant99968442011-11-29 18:15:50 +00001823template<class _Rp, class _A0, class _A1, class _A2>
1824template <typename _Tp>
1825_Tp*
1826function<_Rp(_A0, _A1, _A2)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827{
1828 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001829 return (_Tp*)0;
1830 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831}
1832
Howard Hinnant99968442011-11-29 18:15:50 +00001833template<class _Rp, class _A0, class _A1, class _A2>
1834template <typename _Tp>
1835const _Tp*
1836function<_Rp(_A0, _A1, _A2)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837{
1838 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001839 return (const _Tp*)0;
1840 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841}
1842
Howard Hinnant324bb032010-08-22 00:02:43 +00001843#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001844
Howard Hinnant99968442011-11-29 18:15:50 +00001845template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001846inline _LIBCPP_INLINE_VISIBILITY
1847bool
Howard Hinnant99968442011-11-29 18:15:50 +00001848operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849
Howard Hinnant99968442011-11-29 18:15:50 +00001850template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001851inline _LIBCPP_INLINE_VISIBILITY
1852bool
Howard Hinnant99968442011-11-29 18:15:50 +00001853operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854
Howard Hinnant99968442011-11-29 18:15:50 +00001855template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856inline _LIBCPP_INLINE_VISIBILITY
1857bool
Howard Hinnant99968442011-11-29 18:15:50 +00001858operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859
Howard Hinnant99968442011-11-29 18:15:50 +00001860template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861inline _LIBCPP_INLINE_VISIBILITY
1862bool
Howard Hinnant99968442011-11-29 18:15:50 +00001863operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864
Howard Hinnant99968442011-11-29 18:15:50 +00001865template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866inline _LIBCPP_INLINE_VISIBILITY
1867void
Howard Hinnant99968442011-11-29 18:15:50 +00001868swap(function<_Fp>& __x, function<_Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869{return __x.swap(__y);}
1870
1871template<class _Tp> struct __is_bind_expression : public false_type {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001872template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001873 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1874
1875template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001876template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1878
1879namespace placeholders
1880{
1881
Howard Hinnant99968442011-11-29 18:15:50 +00001882template <int _Np> struct __ph {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883
1884extern __ph<1> _1;
1885extern __ph<2> _2;
1886extern __ph<3> _3;
1887extern __ph<4> _4;
1888extern __ph<5> _5;
1889extern __ph<6> _6;
1890extern __ph<7> _7;
1891extern __ph<8> _8;
1892extern __ph<9> _9;
1893extern __ph<10> _10;
1894
1895} // placeholders
1896
Howard Hinnant99968442011-11-29 18:15:50 +00001897template<int _Np>
1898struct __is_placeholder<placeholders::__ph<_Np> >
1899 : public integral_constant<int, _Np> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900
1901template <class _Tp, class _Uj>
1902inline _LIBCPP_INLINE_VISIBILITY
1903_Tp&
1904__mu(reference_wrapper<_Tp> __t, _Uj&)
1905{
1906 return __t.get();
1907}
1908/*
1909template <bool _IsBindExpr, class _Ti, class ..._Uj>
1910struct __mu_return1 {};
1911
1912template <class _Ti, class ..._Uj>
1913struct __mu_return1<true, _Ti, _Uj...>
1914{
1915 typedef typename result_of<_Ti(_Uj...)>::type type;
1916};
1917
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001918template <class _Ti, class ..._Uj, size_t ..._Indx>
1919inline _LIBCPP_INLINE_VISIBILITY
1920typename __mu_return1<true, _Ti, _Uj...>::type
1921__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1922{
Marshall Clowba6dbf42014-06-24 00:46:19 +00001923 __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924}
1925
1926template <class _Ti, class ..._Uj>
1927inline _LIBCPP_INLINE_VISIBILITY
1928typename enable_if
1929<
1930 is_bind_expression<_Ti>::value,
1931 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1932>::type
1933__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1934{
1935 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1936 return __mu_expand(__ti, __uj, __indices());
1937}
1938
1939template <bool IsPh, class _Ti, class _Uj>
1940struct __mu_return2 {};
1941
1942template <class _Ti, class _Uj>
1943struct __mu_return2<true, _Ti, _Uj>
1944{
1945 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1946};
1947
1948template <class _Ti, class _Uj>
1949inline _LIBCPP_INLINE_VISIBILITY
1950typename enable_if
1951<
1952 0 < is_placeholder<_Ti>::value,
1953 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1954>::type
1955__mu(_Ti&, _Uj& __uj)
1956{
1957 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1958 // compiler bug workaround
Marshall Clowba6dbf42014-06-24 00:46:19 +00001959 typename tuple_element<_Indx, _Uj>::type __t = _VSTD::get<_Indx>(__uj);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001960 return __t;
Marshall Clowba6dbf42014-06-24 00:46:19 +00001961// return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001962}
1963
1964template <class _Ti, class _Uj>
1965inline _LIBCPP_INLINE_VISIBILITY
1966typename enable_if
1967<
1968 !is_bind_expression<_Ti>::value &&
1969 is_placeholder<_Ti>::value == 0 &&
1970 !__is_reference_wrapper<_Ti>::value,
1971 _Ti&
1972>::type
1973__mu(_Ti& __ti, _Uj& __uj)
1974{
1975 return __ti;
1976}
1977
1978template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1979struct ____mu_return;
1980
1981template <class _Ti, class ..._Uj>
1982struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1983{
1984 typedef typename result_of<_Ti(_Uj...)>::type type;
1985};
1986
1987template <class _Ti, class _TupleUj>
1988struct ____mu_return<_Ti, false, true, _TupleUj>
1989{
1990 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1991 _TupleUj>::type&& type;
1992};
1993
1994template <class _Ti, class _TupleUj>
1995struct ____mu_return<_Ti, false, false, _TupleUj>
1996{
1997 typedef _Ti& type;
1998};
1999
2000template <class _Ti, class _TupleUj>
2001struct __mu_return
2002 : public ____mu_return<_Ti,
2003 is_bind_expression<_Ti>::value,
2004 0 < is_placeholder<_Ti>::value,
2005 _TupleUj>
2006{
2007};
2008
2009template <class _Ti, class _TupleUj>
2010struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
2011{
2012 typedef _Ti& type;
2013};
2014
Howard Hinnant99968442011-11-29 18:15:50 +00002015template <class _Fp, class _BoundArgs, class _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016struct __bind_return;
2017
Howard Hinnant99968442011-11-29 18:15:50 +00002018template <class _Fp, class ..._BoundArgs, class _TupleUj>
2019struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020{
2021 typedef typename __ref_return
2022 <
Howard Hinnant99968442011-11-29 18:15:50 +00002023 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 typename __mu_return
2025 <
2026 _BoundArgs,
2027 _TupleUj
2028 >::type...
2029 >::type type;
2030};
2031
Howard Hinnant99968442011-11-29 18:15:50 +00002032template <class _Fp, class ..._BoundArgs, class _TupleUj>
2033struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034{
2035 typedef typename __ref_return
2036 <
Howard Hinnant99968442011-11-29 18:15:50 +00002037 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038 typename __mu_return
2039 <
2040 const _BoundArgs,
2041 _TupleUj
2042 >::type...
2043 >::type type;
2044};
2045
Howard Hinnant99968442011-11-29 18:15:50 +00002046template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002048typename __bind_return<_Fp, _BoundArgs, _Args>::type
2049__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 _Args&& __args)
2051{
Marshall Clowba6dbf42014-06-24 00:46:19 +00002052 return __invoke(__f, __mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002053}
2054
Howard Hinnant99968442011-11-29 18:15:50 +00002055template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056class __bind
2057{
Howard Hinnant99968442011-11-29 18:15:50 +00002058 _Fp __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002059 tuple<_BoundArgs...> __bound_args_;
2060
2061 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2062public:
Howard Hinnant99968442011-11-29 18:15:50 +00002063 template <class _Gp, class ..._BA>
2064 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2065 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002066 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067
2068 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002069 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 operator()(_Args&& ...__args)
2071 {
2072 // compiler bug workaround
Howard Hinnant324bb032010-08-22 00:02:43 +00002073 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074 tuple<_Args&&...>(__args...));
2075 }
2076
2077 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002078 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079 operator()(_Args&& ...__args) const
2080 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002081 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002082 tuple<_Args&&...>(__args...));
2083 }
2084};
2085
Howard Hinnant99968442011-11-29 18:15:50 +00002086template<class _Fp, class ..._BoundArgs>
2087struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088
Howard Hinnant99968442011-11-29 18:15:50 +00002089template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090class __bind_r
Howard Hinnant99968442011-11-29 18:15:50 +00002091 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092{
Howard Hinnant99968442011-11-29 18:15:50 +00002093 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094public:
Howard Hinnant99968442011-11-29 18:15:50 +00002095 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096
Howard Hinnant99968442011-11-29 18:15:50 +00002097 template <class _Gp, class ..._BA>
2098 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2099 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002100 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002101
2102 template <class ..._Args>
2103 result_type
2104 operator()(_Args&& ...__args)
2105 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002106 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002107 }
2108
2109 template <class ..._Args>
2110 result_type
2111 operator()(_Args&& ...__args) const
2112 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002113 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114 }
2115};
2116
Howard Hinnant99968442011-11-29 18:15:50 +00002117template<class _Rp, class _Fp, class ..._BoundArgs>
2118struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119
Howard Hinnant99968442011-11-29 18:15:50 +00002120template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002122__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2123bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124{
Howard Hinnant99968442011-11-29 18:15:50 +00002125 typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2126 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127}
2128
Howard Hinnant99968442011-11-29 18:15:50 +00002129template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002131__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2132bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133{
Howard Hinnant99968442011-11-29 18:15:50 +00002134 typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2135 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136}
2137*/
2138
Howard Hinnant324bb032010-08-22 00:02:43 +00002139#endif // _LIBCPP_FUNCTIONAL_03