blob: f048ea3243724394409733463ce7455d9c0114ae [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL_03
12#define _LIBCPP_FUNCTIONAL_03
13
14// manual variadic expansion for <functional>
15
Howard Hinnant08e17472011-10-17 20:05:10 +000016#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000017#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000018#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
20template <class _Tp>
21class __mem_fn
22 : public __weak_result_type<_Tp>
23{
24public:
25 // types
26 typedef _Tp type;
27private:
28 type __f_;
29
30public:
31 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
32
33 // invoke
34
35 typename __invoke_return<type>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000036 operator() () const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000037 {
38 return __invoke(__f_);
39 }
40
41 template <class _A0>
42 typename __invoke_return0<type, _A0>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000043 operator() (_A0& __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044 {
45 return __invoke(__f_, __a0);
46 }
47
48 template <class _A0, class _A1>
49 typename __invoke_return1<type, _A0, _A1>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000050 operator() (_A0& __a0, _A1& __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051 {
52 return __invoke(__f_, __a0, __a1);
53 }
54
55 template <class _A0, class _A1, class _A2>
56 typename __invoke_return2<type, _A0, _A1, _A2>::type
Peter Collingbournea4c0d872014-01-22 22:56:52 +000057 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000058 {
59 return __invoke(__f_, __a0, __a1, __a2);
60 }
61};
62
Howard Hinnant99968442011-11-29 18:15:50 +000063template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000064inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000065__mem_fn<_Rp _Tp::*>
66mem_fn(_Rp _Tp::* __pm)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067{
Howard Hinnant99968442011-11-29 18:15:50 +000068 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069}
70
Howard Hinnant99968442011-11-29 18:15:50 +000071template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000073__mem_fn<_Rp (_Tp::*)()>
74mem_fn(_Rp (_Tp::* __pm)())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075{
Howard Hinnant99968442011-11-29 18:15:50 +000076 return __mem_fn<_Rp (_Tp::*)()>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077}
78
Howard Hinnant99968442011-11-29 18:15:50 +000079template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000081__mem_fn<_Rp (_Tp::*)(_A0)>
82mem_fn(_Rp (_Tp::* __pm)(_A0))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083{
Howard Hinnant99968442011-11-29 18:15:50 +000084 return __mem_fn<_Rp (_Tp::*)(_A0)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085}
86
Howard Hinnant99968442011-11-29 18:15:50 +000087template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000089__mem_fn<_Rp (_Tp::*)(_A0, _A1)>
90mem_fn(_Rp (_Tp::* __pm)(_A0, _A1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091{
Howard Hinnant99968442011-11-29 18:15:50 +000092 return __mem_fn<_Rp (_Tp::*)(_A0, _A1)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093}
94
Howard Hinnant99968442011-11-29 18:15:50 +000095template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +000097__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>
98mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000099{
Howard Hinnant99968442011-11-29 18:15:50 +0000100 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101}
102
Howard Hinnant99968442011-11-29 18:15:50 +0000103template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000104inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000105__mem_fn<_Rp (_Tp::*)() const>
Howard Hinnant99968442011-11-29 18:15:50 +0000106mem_fn(_Rp (_Tp::* __pm)() const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107{
Richard Smith01fbfc22013-07-23 01:24:30 +0000108 return __mem_fn<_Rp (_Tp::*)() const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109}
110
Howard Hinnant99968442011-11-29 18:15:50 +0000111template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000113__mem_fn<_Rp (_Tp::*)(_A0) const>
Howard Hinnant99968442011-11-29 18:15:50 +0000114mem_fn(_Rp (_Tp::* __pm)(_A0) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115{
Richard Smith01fbfc22013-07-23 01:24:30 +0000116 return __mem_fn<_Rp (_Tp::*)(_A0) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117}
118
Howard Hinnant99968442011-11-29 18:15:50 +0000119template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000121__mem_fn<_Rp (_Tp::*)(_A0, _A1) const>
Howard Hinnant99968442011-11-29 18:15:50 +0000122mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123{
Richard Smith01fbfc22013-07-23 01:24:30 +0000124 return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000125}
126
Howard Hinnant99968442011-11-29 18:15:50 +0000127template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000129__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>
Howard Hinnant99968442011-11-29 18:15:50 +0000130mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131{
Richard Smith01fbfc22013-07-23 01:24:30 +0000132 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133}
134
Howard Hinnant99968442011-11-29 18:15:50 +0000135template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000137__mem_fn<_Rp (_Tp::*)() volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000138mem_fn(_Rp (_Tp::* __pm)() volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139{
Richard Smith01fbfc22013-07-23 01:24:30 +0000140 return __mem_fn<_Rp (_Tp::*)() volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141}
142
Howard Hinnant99968442011-11-29 18:15:50 +0000143template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000145__mem_fn<_Rp (_Tp::*)(_A0) volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000146mem_fn(_Rp (_Tp::* __pm)(_A0) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147{
Richard Smith01fbfc22013-07-23 01:24:30 +0000148 return __mem_fn<_Rp (_Tp::*)(_A0) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000149}
150
Howard Hinnant99968442011-11-29 18:15:50 +0000151template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000153__mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000154mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155{
Richard Smith01fbfc22013-07-23 01:24:30 +0000156 return __mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157}
158
Howard Hinnant99968442011-11-29 18:15:50 +0000159template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000161__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000162mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163{
Richard Smith01fbfc22013-07-23 01:24:30 +0000164 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165}
166
Howard Hinnant99968442011-11-29 18:15:50 +0000167template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000168inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000169__mem_fn<_Rp (_Tp::*)() const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000170mem_fn(_Rp (_Tp::* __pm)() const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171{
Richard Smith01fbfc22013-07-23 01:24:30 +0000172 return __mem_fn<_Rp (_Tp::*)() const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173}
174
Howard Hinnant99968442011-11-29 18:15:50 +0000175template<class _Rp, class _Tp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000177__mem_fn<_Rp (_Tp::*)(_A0) const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000178mem_fn(_Rp (_Tp::* __pm)(_A0) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179{
Richard Smith01fbfc22013-07-23 01:24:30 +0000180 return __mem_fn<_Rp (_Tp::*)(_A0) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000181}
182
Howard Hinnant99968442011-11-29 18:15:50 +0000183template<class _Rp, class _Tp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000185__mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000186mem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187{
Richard Smith01fbfc22013-07-23 01:24:30 +0000188 return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189}
190
Howard Hinnant99968442011-11-29 18:15:50 +0000191template<class _Rp, class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192inline _LIBCPP_INLINE_VISIBILITY
Richard Smith01fbfc22013-07-23 01:24:30 +0000193__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>
Howard Hinnant99968442011-11-29 18:15:50 +0000194mem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195{
Richard Smith01fbfc22013-07-23 01:24:30 +0000196 return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197}
198
199// bad_function_call
200
Howard Hinnant99acc502010-09-21 17:32:39 +0000201class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 : public exception
203{
204};
205
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000206template<class _Fp> class _LIBCPP_TYPE_VIS_ONLY function; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207
208namespace __function
209{
210
Howard Hinnant99968442011-11-29 18:15:50 +0000211template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212struct __maybe_derive_from_unary_function
213{
214};
215
Howard Hinnant99968442011-11-29 18:15:50 +0000216template<class _Rp, class _A1>
217struct __maybe_derive_from_unary_function<_Rp(_A1)>
218 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219{
220};
221
Howard Hinnant99968442011-11-29 18:15:50 +0000222template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223struct __maybe_derive_from_binary_function
224{
225};
226
Howard Hinnant99968442011-11-29 18:15:50 +0000227template<class _Rp, class _A1, class _A2>
228struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
229 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230{
231};
232
233template<class _Fp> class __base;
234
Howard Hinnant99968442011-11-29 18:15:50 +0000235template<class _Rp>
236class __base<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237{
238 __base(const __base&);
239 __base& operator=(const __base&);
240public:
241 __base() {}
242 virtual ~__base() {}
243 virtual __base* __clone() const = 0;
244 virtual void __clone(__base*) const = 0;
245 virtual void destroy() = 0;
246 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000247 virtual _Rp operator()() = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000248#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249 virtual const void* target(const type_info&) const = 0;
250 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000251#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252};
253
Howard Hinnant99968442011-11-29 18:15:50 +0000254template<class _Rp, class _A0>
255class __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256{
257 __base(const __base&);
258 __base& operator=(const __base&);
259public:
260 __base() {}
261 virtual ~__base() {}
262 virtual __base* __clone() const = 0;
263 virtual void __clone(__base*) const = 0;
264 virtual void destroy() = 0;
265 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000266 virtual _Rp operator()(_A0) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000267#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268 virtual const void* target(const type_info&) const = 0;
269 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000270#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271};
272
Howard Hinnant99968442011-11-29 18:15:50 +0000273template<class _Rp, class _A0, class _A1>
274class __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275{
276 __base(const __base&);
277 __base& operator=(const __base&);
278public:
279 __base() {}
280 virtual ~__base() {}
281 virtual __base* __clone() const = 0;
282 virtual void __clone(__base*) const = 0;
283 virtual void destroy() = 0;
284 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000285 virtual _Rp operator()(_A0, _A1) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000286#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287 virtual const void* target(const type_info&) const = 0;
288 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000289#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290};
291
Howard Hinnant99968442011-11-29 18:15:50 +0000292template<class _Rp, class _A0, class _A1, class _A2>
293class __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294{
295 __base(const __base&);
296 __base& operator=(const __base&);
297public:
298 __base() {}
299 virtual ~__base() {}
300 virtual __base* __clone() const = 0;
301 virtual void __clone(__base*) const = 0;
302 virtual void destroy() = 0;
303 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000304 virtual _Rp operator()(_A0, _A1, _A2) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000305#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 virtual const void* target(const type_info&) const = 0;
307 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000308#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309};
310
311template<class _FD, class _Alloc, class _FB> class __func;
312
Howard Hinnant99968442011-11-29 18:15:50 +0000313template<class _Fp, class _Alloc, class _Rp>
314class __func<_Fp, _Alloc, _Rp()>
315 : public __base<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316{
Howard Hinnant99968442011-11-29 18:15:50 +0000317 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000318public:
Howard Hinnant99968442011-11-29 18:15:50 +0000319 explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
320 explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
321 virtual __base<_Rp()>* __clone() const;
322 virtual void __clone(__base<_Rp()>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000323 virtual void destroy();
324 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000325 virtual _Rp operator()();
Howard Hinnantd4444702010-08-11 17:04:31 +0000326#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327 virtual const void* target(const type_info&) const;
328 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000329#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330};
331
Howard Hinnant99968442011-11-29 18:15:50 +0000332template<class _Fp, class _Alloc, class _Rp>
333__base<_Rp()>*
334__func<_Fp, _Alloc, _Rp()>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335{
Howard Hinnant99968442011-11-29 18:15:50 +0000336 typedef typename _Alloc::template rebind<__func>::other _Ap;
337 _Ap __a(__f_.second());
338 typedef __allocator_destructor<_Ap> _Dp;
339 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000340 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
341 return __hold.release();
342}
343
Howard Hinnant99968442011-11-29 18:15:50 +0000344template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345void
Howard Hinnant99968442011-11-29 18:15:50 +0000346__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347{
348 ::new (__p) __func(__f_.first(), __f_.second());
349}
350
Howard Hinnant99968442011-11-29 18:15:50 +0000351template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352void
Howard Hinnant99968442011-11-29 18:15:50 +0000353__func<_Fp, _Alloc, _Rp()>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354{
Howard Hinnant99968442011-11-29 18:15:50 +0000355 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356}
357
Howard Hinnant99968442011-11-29 18:15:50 +0000358template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000359void
Howard Hinnant99968442011-11-29 18:15:50 +0000360__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361{
Howard Hinnant99968442011-11-29 18:15:50 +0000362 typedef typename _Alloc::template rebind<__func>::other _Ap;
363 _Ap __a(__f_.second());
364 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365 __a.deallocate(this, 1);
366}
367
Howard Hinnant99968442011-11-29 18:15:50 +0000368template<class _Fp, class _Alloc, class _Rp>
369_Rp
370__func<_Fp, _Alloc, _Rp()>::operator()()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000372 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
373 return _Invoker::__call(__f_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374}
375
Howard Hinnantd4444702010-08-11 17:04:31 +0000376#ifndef _LIBCPP_NO_RTTI
377
Howard Hinnant99968442011-11-29 18:15:50 +0000378template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000380__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381{
Howard Hinnant99968442011-11-29 18:15:50 +0000382 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 return &__f_.first();
384 return (const void*)0;
385}
386
Howard Hinnant99968442011-11-29 18:15:50 +0000387template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000389__func<_Fp, _Alloc, _Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390{
Howard Hinnant99968442011-11-29 18:15:50 +0000391 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392}
393
Howard Hinnant324bb032010-08-22 00:02:43 +0000394#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000395
Howard Hinnant99968442011-11-29 18:15:50 +0000396template<class _Fp, class _Alloc, class _Rp, class _A0>
397class __func<_Fp, _Alloc, _Rp(_A0)>
398 : public __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399{
Howard Hinnant99968442011-11-29 18:15:50 +0000400 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401public:
Howard Hinnant99968442011-11-29 18:15:50 +0000402 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
403 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000404 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000405 virtual __base<_Rp(_A0)>* __clone() const;
406 virtual void __clone(__base<_Rp(_A0)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407 virtual void destroy();
408 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000409 virtual _Rp operator()(_A0);
Howard Hinnantd4444702010-08-11 17:04:31 +0000410#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 virtual const void* target(const type_info&) const;
412 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000413#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414};
415
Howard Hinnant99968442011-11-29 18:15:50 +0000416template<class _Fp, class _Alloc, class _Rp, class _A0>
417__base<_Rp(_A0)>*
418__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419{
Howard Hinnant99968442011-11-29 18:15:50 +0000420 typedef typename _Alloc::template rebind<__func>::other _Ap;
421 _Ap __a(__f_.second());
422 typedef __allocator_destructor<_Ap> _Dp;
423 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
425 return __hold.release();
426}
427
Howard Hinnant99968442011-11-29 18:15:50 +0000428template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429void
Howard Hinnant99968442011-11-29 18:15:50 +0000430__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000431{
432 ::new (__p) __func(__f_.first(), __f_.second());
433}
434
Howard Hinnant99968442011-11-29 18:15:50 +0000435template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436void
Howard Hinnant99968442011-11-29 18:15:50 +0000437__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438{
Howard Hinnant99968442011-11-29 18:15:50 +0000439 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440}
441
Howard Hinnant99968442011-11-29 18:15:50 +0000442template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443void
Howard Hinnant99968442011-11-29 18:15:50 +0000444__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445{
Howard Hinnant99968442011-11-29 18:15:50 +0000446 typedef typename _Alloc::template rebind<__func>::other _Ap;
447 _Ap __a(__f_.second());
448 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449 __a.deallocate(this, 1);
450}
451
Howard Hinnant99968442011-11-29 18:15:50 +0000452template<class _Fp, class _Alloc, class _Rp, class _A0>
453_Rp
454__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000456 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
457 return _Invoker::__call(__f_.first(), __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458}
459
Howard Hinnantd4444702010-08-11 17:04:31 +0000460#ifndef _LIBCPP_NO_RTTI
461
Howard Hinnant99968442011-11-29 18:15:50 +0000462template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000464__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465{
Howard Hinnant99968442011-11-29 18:15:50 +0000466 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 return &__f_.first();
468 return (const void*)0;
469}
470
Howard Hinnant99968442011-11-29 18:15:50 +0000471template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000473__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474{
Howard Hinnant99968442011-11-29 18:15:50 +0000475 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476}
477
Howard Hinnant324bb032010-08-22 00:02:43 +0000478#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000479
Howard Hinnant99968442011-11-29 18:15:50 +0000480template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
481class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
482 : public __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483{
Howard Hinnant99968442011-11-29 18:15:50 +0000484 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485public:
Howard Hinnant99968442011-11-29 18:15:50 +0000486 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
487 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000488 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000489 virtual __base<_Rp(_A0, _A1)>* __clone() const;
490 virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491 virtual void destroy();
492 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000493 virtual _Rp operator()(_A0, _A1);
Howard Hinnantd4444702010-08-11 17:04:31 +0000494#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495 virtual const void* target(const type_info&) const;
496 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000497#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498};
499
Howard Hinnant99968442011-11-29 18:15:50 +0000500template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
501__base<_Rp(_A0, _A1)>*
502__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503{
Howard Hinnant99968442011-11-29 18:15:50 +0000504 typedef typename _Alloc::template rebind<__func>::other _Ap;
505 _Ap __a(__f_.second());
506 typedef __allocator_destructor<_Ap> _Dp;
507 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
509 return __hold.release();
510}
511
Howard Hinnant99968442011-11-29 18:15:50 +0000512template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513void
Howard Hinnant99968442011-11-29 18:15:50 +0000514__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515{
516 ::new (__p) __func(__f_.first(), __f_.second());
517}
518
Howard Hinnant99968442011-11-29 18:15:50 +0000519template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520void
Howard Hinnant99968442011-11-29 18:15:50 +0000521__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522{
Howard Hinnant99968442011-11-29 18:15:50 +0000523 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524}
525
Howard Hinnant99968442011-11-29 18:15:50 +0000526template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527void
Howard Hinnant99968442011-11-29 18:15:50 +0000528__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529{
Howard Hinnant99968442011-11-29 18:15:50 +0000530 typedef typename _Alloc::template rebind<__func>::other _Ap;
531 _Ap __a(__f_.second());
532 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533 __a.deallocate(this, 1);
534}
535
Howard Hinnant99968442011-11-29 18:15:50 +0000536template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
537_Rp
538__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000540 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
541 return _Invoker::__call(__f_.first(), __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542}
543
Howard Hinnantd4444702010-08-11 17:04:31 +0000544#ifndef _LIBCPP_NO_RTTI
545
Howard Hinnant99968442011-11-29 18:15:50 +0000546template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000548__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549{
Howard Hinnant99968442011-11-29 18:15:50 +0000550 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000551 return &__f_.first();
552 return (const void*)0;
553}
554
Howard Hinnant99968442011-11-29 18:15:50 +0000555template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000557__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558{
Howard Hinnant99968442011-11-29 18:15:50 +0000559 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560}
561
Howard Hinnant324bb032010-08-22 00:02:43 +0000562#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000563
Howard Hinnant99968442011-11-29 18:15:50 +0000564template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
565class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
566 : public __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567{
Howard Hinnant99968442011-11-29 18:15:50 +0000568 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569public:
Howard Hinnant99968442011-11-29 18:15:50 +0000570 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
571 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000572 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000573 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
574 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575 virtual void destroy();
576 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000577 virtual _Rp operator()(_A0, _A1, _A2);
Howard Hinnantd4444702010-08-11 17:04:31 +0000578#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579 virtual const void* target(const type_info&) const;
580 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000581#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582};
583
Howard Hinnant99968442011-11-29 18:15:50 +0000584template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
585__base<_Rp(_A0, _A1, _A2)>*
586__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587{
Howard Hinnant99968442011-11-29 18:15:50 +0000588 typedef typename _Alloc::template rebind<__func>::other _Ap;
589 _Ap __a(__f_.second());
590 typedef __allocator_destructor<_Ap> _Dp;
591 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
593 return __hold.release();
594}
595
Howard Hinnant99968442011-11-29 18:15:50 +0000596template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597void
Howard Hinnant99968442011-11-29 18:15:50 +0000598__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599{
600 ::new (__p) __func(__f_.first(), __f_.second());
601}
602
Howard Hinnant99968442011-11-29 18:15:50 +0000603template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604void
Howard Hinnant99968442011-11-29 18:15:50 +0000605__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606{
Howard Hinnant99968442011-11-29 18:15:50 +0000607 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608}
609
Howard Hinnant99968442011-11-29 18:15:50 +0000610template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611void
Howard Hinnant99968442011-11-29 18:15:50 +0000612__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613{
Howard Hinnant99968442011-11-29 18:15:50 +0000614 typedef typename _Alloc::template rebind<__func>::other _Ap;
615 _Ap __a(__f_.second());
616 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617 __a.deallocate(this, 1);
618}
619
Howard Hinnant99968442011-11-29 18:15:50 +0000620template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
621_Rp
622__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623{
Eric Fiselierc3231d22015-02-10 16:48:45 +0000624 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
625 return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626}
627
Howard Hinnantd4444702010-08-11 17:04:31 +0000628#ifndef _LIBCPP_NO_RTTI
629
Howard Hinnant99968442011-11-29 18:15:50 +0000630template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000632__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633{
Howard Hinnant99968442011-11-29 18:15:50 +0000634 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635 return &__f_.first();
636 return (const void*)0;
637}
638
Howard Hinnant99968442011-11-29 18:15:50 +0000639template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000641__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642{
Howard Hinnant99968442011-11-29 18:15:50 +0000643 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644}
645
Howard Hinnant324bb032010-08-22 00:02:43 +0000646#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000647
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648} // __function
649
Howard Hinnant99968442011-11-29 18:15:50 +0000650template<class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000651class _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652{
Howard Hinnant99968442011-11-29 18:15:50 +0000653 typedef __function::__base<_Rp()> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 aligned_storage<3*sizeof(void*)>::type __buf_;
655 __base* __f_;
656
Howard Hinnant99968442011-11-29 18:15:50 +0000657 template <class _Fp>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000659 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660 template <class _R2>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000661 _LIBCPP_INLINE_VISIBILITY
662 static bool __not_null(_R2 (*__p)()) {return __p;}
663 template <class _R2>
664 _LIBCPP_INLINE_VISIBILITY
665 static bool __not_null(const function<_R2()>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666public:
Howard Hinnant99968442011-11-29 18:15:50 +0000667 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668
669 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000670 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
671 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000673 template<class _Fp>
674 function(_Fp,
675 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
Howard Hinnant72552802010-08-20 19:36:46 +0000677 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000679 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
680 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000682 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
683 template<class _Alloc>
684 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000685 template<class _Fp, class _Alloc>
686 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
687 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688
689 function& operator=(const function&);
690 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000691 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692 typename enable_if
693 <
Howard Hinnant99968442011-11-29 18:15:50 +0000694 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695 function&
696 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000697 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698
699 ~function();
700
701 // 20.7.16.2.2, function modifiers:
702 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000703 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000705 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +0000706 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707
708 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +0000709 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710
711private:
712 // deleted overloads close possible hole in the type system
713 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000714 bool operator==(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000716 bool operator!=(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717public:
718 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +0000719 _Rp operator()() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720
Howard Hinnantd4444702010-08-11 17:04:31 +0000721#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722 // 20.7.16.2.5, function target access:
723 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +0000724 template <typename _Tp> _Tp* target();
725 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000726#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727};
728
Howard Hinnant99968442011-11-29 18:15:50 +0000729template<class _Rp>
730function<_Rp()>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731{
732 if (__f.__f_ == 0)
733 __f_ = 0;
734 else if (__f.__f_ == (const __base*)&__f.__buf_)
735 {
736 __f_ = (__base*)&__buf_;
737 __f.__f_->__clone(__f_);
738 }
739 else
740 __f_ = __f.__f_->__clone();
741}
742
Howard Hinnant99968442011-11-29 18:15:50 +0000743template<class _Rp>
Howard Hinnant72552802010-08-20 19:36:46 +0000744template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +0000745function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +0000746{
747 if (__f.__f_ == 0)
748 __f_ = 0;
749 else if (__f.__f_ == (const __base*)&__f.__buf_)
750 {
751 __f_ = (__base*)&__buf_;
752 __f.__f_->__clone(__f_);
753 }
754 else
755 __f_ = __f.__f_->__clone();
756}
757
Howard Hinnant99968442011-11-29 18:15:50 +0000758template<class _Rp>
759template <class _Fp>
760function<_Rp()>::function(_Fp __f,
761 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000762 : __f_(0)
763{
764 if (__not_null(__f))
765 {
Howard Hinnant99968442011-11-29 18:15:50 +0000766 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767 if (sizeof(_FF) <= sizeof(__buf_))
768 {
769 __f_ = (__base*)&__buf_;
770 ::new (__f_) _FF(__f);
771 }
772 else
773 {
Howard Hinnant99968442011-11-29 18:15:50 +0000774 typedef allocator<_FF> _Ap;
775 _Ap __a;
776 typedef __allocator_destructor<_Ap> _Dp;
777 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
778 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 __f_ = __hold.release();
780 }
781 }
782}
783
Howard Hinnant99968442011-11-29 18:15:50 +0000784template<class _Rp>
785template <class _Fp, class _Alloc>
786function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
787 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +0000788 : __f_(0)
789{
790 typedef allocator_traits<_Alloc> __alloc_traits;
791 if (__not_null(__f))
792 {
Howard Hinnant99968442011-11-29 18:15:50 +0000793 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +0000794 if (sizeof(_FF) <= sizeof(__buf_))
795 {
796 __f_ = (__base*)&__buf_;
797 ::new (__f_) _FF(__f);
798 }
799 else
800 {
Marshall Clow66302c62015-04-07 05:21:38 +0000801 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +0000802 _Ap __a(__a0);
803 typedef __allocator_destructor<_Ap> _Dp;
804 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +0000805 ::new (__hold.get()) _FF(__f, _Alloc(__a));
806 __f_ = __hold.release();
807 }
808 }
809}
810
Howard Hinnant99968442011-11-29 18:15:50 +0000811template<class _Rp>
812function<_Rp()>&
813function<_Rp()>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814{
815 function(__f).swap(*this);
816 return *this;
817}
818
Howard Hinnant99968442011-11-29 18:15:50 +0000819template<class _Rp>
820function<_Rp()>&
821function<_Rp()>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822{
823 if (__f_ == (__base*)&__buf_)
824 __f_->destroy();
825 else if (__f_)
826 __f_->destroy_deallocate();
827 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +0000828 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000829}
830
Howard Hinnant99968442011-11-29 18:15:50 +0000831template<class _Rp>
832template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833typename enable_if
834<
Howard Hinnant99968442011-11-29 18:15:50 +0000835 !is_integral<_Fp>::value,
836 function<_Rp()>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837>::type
Howard Hinnant99968442011-11-29 18:15:50 +0000838function<_Rp()>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000840 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000841 return *this;
842}
843
Howard Hinnant99968442011-11-29 18:15:50 +0000844template<class _Rp>
845function<_Rp()>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000846{
847 if (__f_ == (__base*)&__buf_)
848 __f_->destroy();
849 else if (__f_)
850 __f_->destroy_deallocate();
851}
852
Howard Hinnant99968442011-11-29 18:15:50 +0000853template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000854void
Howard Hinnant99968442011-11-29 18:15:50 +0000855function<_Rp()>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000856{
857 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
858 {
859 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
860 __base* __t = (__base*)&__tempbuf;
861 __f_->__clone(__t);
862 __f_->destroy();
863 __f_ = 0;
864 __f.__f_->__clone((__base*)&__buf_);
865 __f.__f_->destroy();
866 __f.__f_ = 0;
867 __f_ = (__base*)&__buf_;
868 __t->__clone((__base*)&__f.__buf_);
869 __t->destroy();
870 __f.__f_ = (__base*)&__f.__buf_;
871 }
872 else if (__f_ == (__base*)&__buf_)
873 {
874 __f_->__clone((__base*)&__f.__buf_);
875 __f_->destroy();
876 __f_ = __f.__f_;
877 __f.__f_ = (__base*)&__f.__buf_;
878 }
879 else if (__f.__f_ == (__base*)&__f.__buf_)
880 {
881 __f.__f_->__clone((__base*)&__buf_);
882 __f.__f_->destroy();
883 __f.__f_ = __f_;
884 __f_ = (__base*)&__buf_;
885 }
886 else
Howard Hinnant0949eed2011-06-30 21:18:19 +0000887 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888}
889
Howard Hinnant99968442011-11-29 18:15:50 +0000890template<class _Rp>
891_Rp
892function<_Rp()>::operator()() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893{
Howard Hinnantd4444702010-08-11 17:04:31 +0000894#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895 if (__f_ == 0)
896 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +0000897#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898 return (*__f_)();
899}
900
Howard Hinnantd4444702010-08-11 17:04:31 +0000901#ifndef _LIBCPP_NO_RTTI
902
Howard Hinnant99968442011-11-29 18:15:50 +0000903template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000905function<_Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906{
907 if (__f_ == 0)
908 return typeid(void);
909 return __f_->target_type();
910}
911
Howard Hinnant99968442011-11-29 18:15:50 +0000912template<class _Rp>
913template <typename _Tp>
914_Tp*
915function<_Rp()>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916{
917 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000918 return (_Tp*)0;
919 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920}
921
Howard Hinnant99968442011-11-29 18:15:50 +0000922template<class _Rp>
923template <typename _Tp>
924const _Tp*
925function<_Rp()>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926{
927 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000928 return (const _Tp*)0;
929 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930}
931
Howard Hinnant324bb032010-08-22 00:02:43 +0000932#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000933
Howard Hinnant99968442011-11-29 18:15:50 +0000934template<class _Rp, class _A0>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000935class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
Howard Hinnant99968442011-11-29 18:15:50 +0000936 : public unary_function<_A0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937{
Howard Hinnant99968442011-11-29 18:15:50 +0000938 typedef __function::__base<_Rp(_A0)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939 aligned_storage<3*sizeof(void*)>::type __buf_;
940 __base* __f_;
941
Howard Hinnant99968442011-11-29 18:15:50 +0000942 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000944 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +0000948 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000950 static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
951 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000953 static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
954 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000956 static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
957 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000959 static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000961 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +0000962 static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000963public:
Howard Hinnant99968442011-11-29 18:15:50 +0000964 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965
966 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000967 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
968 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000969 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000970 template<class _Fp>
971 function(_Fp,
972 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973
Howard Hinnant72552802010-08-20 19:36:46 +0000974 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000976 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
977 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000979 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
980 template<class _Alloc>
981 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000982 template<class _Fp, class _Alloc>
983 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
984 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985
986 function& operator=(const function&);
987 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000988 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000989 typename enable_if
990 <
Howard Hinnant99968442011-11-29 18:15:50 +0000991 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000992 function&
993 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000994 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995
996 ~function();
997
998 // 20.7.16.2.2, function modifiers:
999 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001000 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001002 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001003 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004
1005 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001006 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007
1008private:
1009 // deleted overloads close possible hole in the type system
1010 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001011 bool operator==(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001013 bool operator!=(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001014public:
1015 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001016 _Rp operator()(_A0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017
Howard Hinnantd4444702010-08-11 17:04:31 +00001018#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 // 20.7.16.2.5, function target access:
1020 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001021 template <typename _Tp> _Tp* target();
1022 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001023#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024};
1025
Howard Hinnant99968442011-11-29 18:15:50 +00001026template<class _Rp, class _A0>
1027function<_Rp(_A0)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028{
1029 if (__f.__f_ == 0)
1030 __f_ = 0;
1031 else if (__f.__f_ == (const __base*)&__f.__buf_)
1032 {
1033 __f_ = (__base*)&__buf_;
1034 __f.__f_->__clone(__f_);
1035 }
1036 else
1037 __f_ = __f.__f_->__clone();
1038}
1039
Howard Hinnant99968442011-11-29 18:15:50 +00001040template<class _Rp, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001041template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001042function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001043{
1044 if (__f.__f_ == 0)
1045 __f_ = 0;
1046 else if (__f.__f_ == (const __base*)&__f.__buf_)
1047 {
1048 __f_ = (__base*)&__buf_;
1049 __f.__f_->__clone(__f_);
1050 }
1051 else
1052 __f_ = __f.__f_->__clone();
1053}
1054
Howard Hinnant99968442011-11-29 18:15:50 +00001055template<class _Rp, class _A0>
1056template <class _Fp>
1057function<_Rp(_A0)>::function(_Fp __f,
1058 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059 : __f_(0)
1060{
1061 if (__not_null(__f))
1062 {
Howard Hinnant99968442011-11-29 18:15:50 +00001063 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064 if (sizeof(_FF) <= sizeof(__buf_))
1065 {
1066 __f_ = (__base*)&__buf_;
1067 ::new (__f_) _FF(__f);
1068 }
1069 else
1070 {
Howard Hinnant99968442011-11-29 18:15:50 +00001071 typedef allocator<_FF> _Ap;
1072 _Ap __a;
1073 typedef __allocator_destructor<_Ap> _Dp;
1074 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1075 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076 __f_ = __hold.release();
1077 }
1078 }
1079}
1080
Howard Hinnant99968442011-11-29 18:15:50 +00001081template<class _Rp, class _A0>
1082template <class _Fp, class _Alloc>
1083function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1084 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001085 : __f_(0)
1086{
1087 typedef allocator_traits<_Alloc> __alloc_traits;
1088 if (__not_null(__f))
1089 {
Howard Hinnant99968442011-11-29 18:15:50 +00001090 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001091 if (sizeof(_FF) <= sizeof(__buf_))
1092 {
1093 __f_ = (__base*)&__buf_;
1094 ::new (__f_) _FF(__f);
1095 }
1096 else
1097 {
Marshall Clow66302c62015-04-07 05:21:38 +00001098 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001099 _Ap __a(__a0);
1100 typedef __allocator_destructor<_Ap> _Dp;
1101 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001102 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1103 __f_ = __hold.release();
1104 }
1105 }
1106}
1107
Howard Hinnant99968442011-11-29 18:15:50 +00001108template<class _Rp, class _A0>
1109function<_Rp(_A0)>&
1110function<_Rp(_A0)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111{
1112 function(__f).swap(*this);
1113 return *this;
1114}
1115
Howard Hinnant99968442011-11-29 18:15:50 +00001116template<class _Rp, class _A0>
1117function<_Rp(_A0)>&
1118function<_Rp(_A0)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001119{
1120 if (__f_ == (__base*)&__buf_)
1121 __f_->destroy();
1122 else if (__f_)
1123 __f_->destroy_deallocate();
1124 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +00001125 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126}
1127
Howard Hinnant99968442011-11-29 18:15:50 +00001128template<class _Rp, class _A0>
1129template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130typename enable_if
1131<
Howard Hinnant99968442011-11-29 18:15:50 +00001132 !is_integral<_Fp>::value,
1133 function<_Rp(_A0)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001135function<_Rp(_A0)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001137 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138 return *this;
1139}
1140
Howard Hinnant99968442011-11-29 18:15:50 +00001141template<class _Rp, class _A0>
1142function<_Rp(_A0)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001143{
1144 if (__f_ == (__base*)&__buf_)
1145 __f_->destroy();
1146 else if (__f_)
1147 __f_->destroy_deallocate();
1148}
1149
Howard Hinnant99968442011-11-29 18:15:50 +00001150template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151void
Howard Hinnant99968442011-11-29 18:15:50 +00001152function<_Rp(_A0)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153{
1154 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1155 {
1156 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1157 __base* __t = (__base*)&__tempbuf;
1158 __f_->__clone(__t);
1159 __f_->destroy();
1160 __f_ = 0;
1161 __f.__f_->__clone((__base*)&__buf_);
1162 __f.__f_->destroy();
1163 __f.__f_ = 0;
1164 __f_ = (__base*)&__buf_;
1165 __t->__clone((__base*)&__f.__buf_);
1166 __t->destroy();
1167 __f.__f_ = (__base*)&__f.__buf_;
1168 }
1169 else if (__f_ == (__base*)&__buf_)
1170 {
1171 __f_->__clone((__base*)&__f.__buf_);
1172 __f_->destroy();
1173 __f_ = __f.__f_;
1174 __f.__f_ = (__base*)&__f.__buf_;
1175 }
1176 else if (__f.__f_ == (__base*)&__f.__buf_)
1177 {
1178 __f.__f_->__clone((__base*)&__buf_);
1179 __f.__f_->destroy();
1180 __f.__f_ = __f_;
1181 __f_ = (__base*)&__buf_;
1182 }
1183 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001184 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001185}
1186
Howard Hinnant99968442011-11-29 18:15:50 +00001187template<class _Rp, class _A0>
1188_Rp
1189function<_Rp(_A0)>::operator()(_A0 __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190{
Howard Hinnantd4444702010-08-11 17:04:31 +00001191#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192 if (__f_ == 0)
1193 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001194#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195 return (*__f_)(__a0);
1196}
1197
Howard Hinnantd4444702010-08-11 17:04:31 +00001198#ifndef _LIBCPP_NO_RTTI
1199
Howard Hinnant99968442011-11-29 18:15:50 +00001200template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001201const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001202function<_Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203{
1204 if (__f_ == 0)
1205 return typeid(void);
1206 return __f_->target_type();
1207}
1208
Howard Hinnant99968442011-11-29 18:15:50 +00001209template<class _Rp, class _A0>
1210template <typename _Tp>
1211_Tp*
1212function<_Rp(_A0)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213{
1214 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001215 return (_Tp*)0;
1216 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217}
1218
Howard Hinnant99968442011-11-29 18:15:50 +00001219template<class _Rp, class _A0>
1220template <typename _Tp>
1221const _Tp*
1222function<_Rp(_A0)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001223{
1224 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001225 return (const _Tp*)0;
1226 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001227}
1228
Howard Hinnant324bb032010-08-22 00:02:43 +00001229#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001230
Howard Hinnant99968442011-11-29 18:15:50 +00001231template<class _Rp, class _A0, class _A1>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001232class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
Howard Hinnant99968442011-11-29 18:15:50 +00001233 : public binary_function<_A0, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234{
Howard Hinnant99968442011-11-29 18:15:50 +00001235 typedef __function::__base<_Rp(_A0, _A1)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236 aligned_storage<3*sizeof(void*)>::type __buf_;
1237 __base* __f_;
1238
Howard Hinnant99968442011-11-29 18:15:50 +00001239 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001241 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001245 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001247 static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1248 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001250 static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1251 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001253 static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1254 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001256 static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001257 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001258 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001259 static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260public:
Howard Hinnant99968442011-11-29 18:15:50 +00001261 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262
1263 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001264 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1265 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001267 template<class _Fp>
1268 function(_Fp,
1269 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270
Howard Hinnant72552802010-08-20 19:36:46 +00001271 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001273 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1274 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001276 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1277 template<class _Alloc>
1278 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001279 template<class _Fp, class _Alloc>
1280 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1281 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001282
1283 function& operator=(const function&);
1284 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001285 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286 typename enable_if
1287 <
Howard Hinnant99968442011-11-29 18:15:50 +00001288 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001289 function&
1290 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001291 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292
1293 ~function();
1294
1295 // 20.7.16.2.2, function modifiers:
1296 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001297 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001299 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001300 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301
1302 // 20.7.16.2.3, function capacity:
1303 operator bool() const {return __f_;}
1304
1305private:
1306 // deleted overloads close possible hole in the type system
1307 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001308 bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001310 bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311public:
1312 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001313 _Rp operator()(_A0, _A1) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314
Howard Hinnantd4444702010-08-11 17:04:31 +00001315#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316 // 20.7.16.2.5, function target access:
1317 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001318 template <typename _Tp> _Tp* target();
1319 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001320#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321};
1322
Howard Hinnant99968442011-11-29 18:15:50 +00001323template<class _Rp, class _A0, class _A1>
1324function<_Rp(_A0, _A1)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325{
1326 if (__f.__f_ == 0)
1327 __f_ = 0;
1328 else if (__f.__f_ == (const __base*)&__f.__buf_)
1329 {
1330 __f_ = (__base*)&__buf_;
1331 __f.__f_->__clone(__f_);
1332 }
1333 else
1334 __f_ = __f.__f_->__clone();
1335}
1336
Howard Hinnant99968442011-11-29 18:15:50 +00001337template<class _Rp, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001338template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001339function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001340{
1341 if (__f.__f_ == 0)
1342 __f_ = 0;
1343 else if (__f.__f_ == (const __base*)&__f.__buf_)
1344 {
1345 __f_ = (__base*)&__buf_;
1346 __f.__f_->__clone(__f_);
1347 }
1348 else
1349 __f_ = __f.__f_->__clone();
1350}
1351
Howard Hinnant99968442011-11-29 18:15:50 +00001352template<class _Rp, class _A0, class _A1>
1353template <class _Fp>
1354function<_Rp(_A0, _A1)>::function(_Fp __f,
1355 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356 : __f_(0)
1357{
1358 if (__not_null(__f))
1359 {
Howard Hinnant99968442011-11-29 18:15:50 +00001360 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361 if (sizeof(_FF) <= sizeof(__buf_))
1362 {
1363 __f_ = (__base*)&__buf_;
1364 ::new (__f_) _FF(__f);
1365 }
1366 else
1367 {
Howard Hinnant99968442011-11-29 18:15:50 +00001368 typedef allocator<_FF> _Ap;
1369 _Ap __a;
1370 typedef __allocator_destructor<_Ap> _Dp;
1371 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1372 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001373 __f_ = __hold.release();
1374 }
1375 }
1376}
1377
Howard Hinnant99968442011-11-29 18:15:50 +00001378template<class _Rp, class _A0, class _A1>
1379template <class _Fp, class _Alloc>
1380function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1381 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001382 : __f_(0)
1383{
1384 typedef allocator_traits<_Alloc> __alloc_traits;
1385 if (__not_null(__f))
1386 {
Howard Hinnant99968442011-11-29 18:15:50 +00001387 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001388 if (sizeof(_FF) <= sizeof(__buf_))
1389 {
1390 __f_ = (__base*)&__buf_;
1391 ::new (__f_) _FF(__f);
1392 }
1393 else
1394 {
Marshall Clow66302c62015-04-07 05:21:38 +00001395 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001396 _Ap __a(__a0);
1397 typedef __allocator_destructor<_Ap> _Dp;
1398 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001399 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1400 __f_ = __hold.release();
1401 }
1402 }
1403}
1404
Howard Hinnant99968442011-11-29 18:15:50 +00001405template<class _Rp, class _A0, class _A1>
1406function<_Rp(_A0, _A1)>&
1407function<_Rp(_A0, _A1)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408{
1409 function(__f).swap(*this);
1410 return *this;
1411}
1412
Howard Hinnant99968442011-11-29 18:15:50 +00001413template<class _Rp, class _A0, class _A1>
1414function<_Rp(_A0, _A1)>&
1415function<_Rp(_A0, _A1)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416{
1417 if (__f_ == (__base*)&__buf_)
1418 __f_->destroy();
1419 else if (__f_)
1420 __f_->destroy_deallocate();
1421 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +00001422 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423}
1424
Howard Hinnant99968442011-11-29 18:15:50 +00001425template<class _Rp, class _A0, class _A1>
1426template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427typename enable_if
1428<
Howard Hinnant99968442011-11-29 18:15:50 +00001429 !is_integral<_Fp>::value,
1430 function<_Rp(_A0, _A1)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001432function<_Rp(_A0, _A1)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001434 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435 return *this;
1436}
1437
Howard Hinnant99968442011-11-29 18:15:50 +00001438template<class _Rp, class _A0, class _A1>
1439function<_Rp(_A0, _A1)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440{
1441 if (__f_ == (__base*)&__buf_)
1442 __f_->destroy();
1443 else if (__f_)
1444 __f_->destroy_deallocate();
1445}
1446
Howard Hinnant99968442011-11-29 18:15:50 +00001447template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448void
Howard Hinnant99968442011-11-29 18:15:50 +00001449function<_Rp(_A0, _A1)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450{
1451 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1452 {
1453 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1454 __base* __t = (__base*)&__tempbuf;
1455 __f_->__clone(__t);
1456 __f_->destroy();
1457 __f_ = 0;
1458 __f.__f_->__clone((__base*)&__buf_);
1459 __f.__f_->destroy();
1460 __f.__f_ = 0;
1461 __f_ = (__base*)&__buf_;
1462 __t->__clone((__base*)&__f.__buf_);
1463 __t->destroy();
1464 __f.__f_ = (__base*)&__f.__buf_;
1465 }
1466 else if (__f_ == (__base*)&__buf_)
1467 {
1468 __f_->__clone((__base*)&__f.__buf_);
1469 __f_->destroy();
1470 __f_ = __f.__f_;
1471 __f.__f_ = (__base*)&__f.__buf_;
1472 }
1473 else if (__f.__f_ == (__base*)&__f.__buf_)
1474 {
1475 __f.__f_->__clone((__base*)&__buf_);
1476 __f.__f_->destroy();
1477 __f.__f_ = __f_;
1478 __f_ = (__base*)&__buf_;
1479 }
1480 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001481 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482}
1483
Howard Hinnant99968442011-11-29 18:15:50 +00001484template<class _Rp, class _A0, class _A1>
1485_Rp
1486function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487{
Howard Hinnantd4444702010-08-11 17:04:31 +00001488#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489 if (__f_ == 0)
1490 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001491#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492 return (*__f_)(__a0, __a1);
1493}
1494
Howard Hinnantd4444702010-08-11 17:04:31 +00001495#ifndef _LIBCPP_NO_RTTI
1496
Howard Hinnant99968442011-11-29 18:15:50 +00001497template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001499function<_Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500{
1501 if (__f_ == 0)
1502 return typeid(void);
1503 return __f_->target_type();
1504}
1505
Howard Hinnant99968442011-11-29 18:15:50 +00001506template<class _Rp, class _A0, class _A1>
1507template <typename _Tp>
1508_Tp*
1509function<_Rp(_A0, _A1)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510{
1511 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001512 return (_Tp*)0;
1513 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514}
1515
Howard Hinnant99968442011-11-29 18:15:50 +00001516template<class _Rp, class _A0, class _A1>
1517template <typename _Tp>
1518const _Tp*
1519function<_Rp(_A0, _A1)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520{
1521 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001522 return (const _Tp*)0;
1523 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524}
1525
Howard Hinnant324bb032010-08-22 00:02:43 +00001526#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001527
Howard Hinnant99968442011-11-29 18:15:50 +00001528template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001529class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530{
Howard Hinnant99968442011-11-29 18:15:50 +00001531 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532 aligned_storage<3*sizeof(void*)>::type __buf_;
1533 __base* __f_;
1534
Howard Hinnant99968442011-11-29 18:15:50 +00001535 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001537 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001541 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001543 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1544 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001546 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1547 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001549 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1550 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001552 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
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
Marshall Clow2f9e7142014-06-30 21:27:51 +00001555 static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556public:
Howard Hinnant99968442011-11-29 18:15:50 +00001557 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558
1559 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001560 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1561 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001563 template<class _Fp>
1564 function(_Fp,
1565 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566
Howard Hinnant72552802010-08-20 19:36:46 +00001567 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001569 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1570 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001572 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1573 template<class _Alloc>
1574 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001575 template<class _Fp, class _Alloc>
1576 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1577 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578
1579 function& operator=(const function&);
1580 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001581 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582 typename enable_if
1583 <
Howard Hinnant99968442011-11-29 18:15:50 +00001584 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585 function&
1586 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001587 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588
1589 ~function();
1590
1591 // 20.7.16.2.2, function modifiers:
1592 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001593 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001595 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001596 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597
1598 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001599 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600
1601private:
1602 // deleted overloads close possible hole in the type system
1603 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001604 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001606 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607public:
1608 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001609 _Rp operator()(_A0, _A1, _A2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610
Howard Hinnantd4444702010-08-11 17:04:31 +00001611#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001612 // 20.7.16.2.5, function target access:
1613 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001614 template <typename _Tp> _Tp* target();
1615 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001616#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617};
1618
Howard Hinnant99968442011-11-29 18:15:50 +00001619template<class _Rp, class _A0, class _A1, class _A2>
1620function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001621{
1622 if (__f.__f_ == 0)
1623 __f_ = 0;
1624 else if (__f.__f_ == (const __base*)&__f.__buf_)
1625 {
1626 __f_ = (__base*)&__buf_;
1627 __f.__f_->__clone(__f_);
1628 }
1629 else
1630 __f_ = __f.__f_->__clone();
1631}
1632
Howard Hinnant99968442011-11-29 18:15:50 +00001633template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001634template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001635function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnant72552802010-08-20 19:36:46 +00001636 const function& __f)
1637{
1638 if (__f.__f_ == 0)
1639 __f_ = 0;
1640 else if (__f.__f_ == (const __base*)&__f.__buf_)
1641 {
1642 __f_ = (__base*)&__buf_;
1643 __f.__f_->__clone(__f_);
1644 }
1645 else
1646 __f_ = __f.__f_->__clone();
1647}
1648
Howard Hinnant99968442011-11-29 18:15:50 +00001649template<class _Rp, class _A0, class _A1, class _A2>
1650template <class _Fp>
1651function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1652 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653 : __f_(0)
1654{
1655 if (__not_null(__f))
1656 {
Howard Hinnant99968442011-11-29 18:15:50 +00001657 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658 if (sizeof(_FF) <= sizeof(__buf_))
1659 {
1660 __f_ = (__base*)&__buf_;
1661 ::new (__f_) _FF(__f);
1662 }
1663 else
1664 {
Howard Hinnant99968442011-11-29 18:15:50 +00001665 typedef allocator<_FF> _Ap;
1666 _Ap __a;
1667 typedef __allocator_destructor<_Ap> _Dp;
1668 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1669 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670 __f_ = __hold.release();
1671 }
1672 }
1673}
1674
Howard Hinnant99968442011-11-29 18:15:50 +00001675template<class _Rp, class _A0, class _A1, class _A2>
1676template <class _Fp, class _Alloc>
1677function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1678 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001679 : __f_(0)
1680{
1681 typedef allocator_traits<_Alloc> __alloc_traits;
1682 if (__not_null(__f))
1683 {
Howard Hinnant99968442011-11-29 18:15:50 +00001684 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001685 if (sizeof(_FF) <= sizeof(__buf_))
1686 {
1687 __f_ = (__base*)&__buf_;
1688 ::new (__f_) _FF(__f);
1689 }
1690 else
1691 {
Marshall Clow66302c62015-04-07 05:21:38 +00001692 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001693 _Ap __a(__a0);
1694 typedef __allocator_destructor<_Ap> _Dp;
1695 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001696 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1697 __f_ = __hold.release();
1698 }
1699 }
1700}
1701
Howard Hinnant99968442011-11-29 18:15:50 +00001702template<class _Rp, class _A0, class _A1, class _A2>
1703function<_Rp(_A0, _A1, _A2)>&
1704function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705{
1706 function(__f).swap(*this);
1707 return *this;
1708}
1709
Howard Hinnant99968442011-11-29 18:15:50 +00001710template<class _Rp, class _A0, class _A1, class _A2>
1711function<_Rp(_A0, _A1, _A2)>&
1712function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713{
1714 if (__f_ == (__base*)&__buf_)
1715 __f_->destroy();
1716 else if (__f_)
1717 __f_->destroy_deallocate();
1718 __f_ = 0;
Eric Fiselierfa97c2e2015-06-02 01:31:33 +00001719 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720}
1721
Howard Hinnant99968442011-11-29 18:15:50 +00001722template<class _Rp, class _A0, class _A1, class _A2>
1723template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724typename enable_if
1725<
Howard Hinnant99968442011-11-29 18:15:50 +00001726 !is_integral<_Fp>::value,
1727 function<_Rp(_A0, _A1, _A2)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001729function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001731 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732 return *this;
1733}
1734
Howard Hinnant99968442011-11-29 18:15:50 +00001735template<class _Rp, class _A0, class _A1, class _A2>
1736function<_Rp(_A0, _A1, _A2)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001737{
1738 if (__f_ == (__base*)&__buf_)
1739 __f_->destroy();
1740 else if (__f_)
1741 __f_->destroy_deallocate();
1742}
1743
Howard Hinnant99968442011-11-29 18:15:50 +00001744template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745void
Howard Hinnant99968442011-11-29 18:15:50 +00001746function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001747{
1748 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1749 {
1750 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1751 __base* __t = (__base*)&__tempbuf;
1752 __f_->__clone(__t);
1753 __f_->destroy();
1754 __f_ = 0;
1755 __f.__f_->__clone((__base*)&__buf_);
1756 __f.__f_->destroy();
1757 __f.__f_ = 0;
1758 __f_ = (__base*)&__buf_;
1759 __t->__clone((__base*)&__f.__buf_);
1760 __t->destroy();
1761 __f.__f_ = (__base*)&__f.__buf_;
1762 }
1763 else if (__f_ == (__base*)&__buf_)
1764 {
1765 __f_->__clone((__base*)&__f.__buf_);
1766 __f_->destroy();
1767 __f_ = __f.__f_;
1768 __f.__f_ = (__base*)&__f.__buf_;
1769 }
1770 else if (__f.__f_ == (__base*)&__f.__buf_)
1771 {
1772 __f.__f_->__clone((__base*)&__buf_);
1773 __f.__f_->destroy();
1774 __f.__f_ = __f_;
1775 __f_ = (__base*)&__buf_;
1776 }
1777 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001778 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001779}
1780
Howard Hinnant99968442011-11-29 18:15:50 +00001781template<class _Rp, class _A0, class _A1, class _A2>
1782_Rp
1783function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784{
Howard Hinnantd4444702010-08-11 17:04:31 +00001785#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786 if (__f_ == 0)
1787 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001788#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789 return (*__f_)(__a0, __a1, __a2);
1790}
1791
Howard Hinnantd4444702010-08-11 17:04:31 +00001792#ifndef _LIBCPP_NO_RTTI
1793
Howard Hinnant99968442011-11-29 18:15:50 +00001794template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001796function<_Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797{
1798 if (__f_ == 0)
1799 return typeid(void);
1800 return __f_->target_type();
1801}
1802
Howard Hinnant99968442011-11-29 18:15:50 +00001803template<class _Rp, class _A0, class _A1, class _A2>
1804template <typename _Tp>
1805_Tp*
1806function<_Rp(_A0, _A1, _A2)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807{
1808 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001809 return (_Tp*)0;
1810 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811}
1812
Howard Hinnant99968442011-11-29 18:15:50 +00001813template<class _Rp, class _A0, class _A1, class _A2>
1814template <typename _Tp>
1815const _Tp*
1816function<_Rp(_A0, _A1, _A2)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817{
1818 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001819 return (const _Tp*)0;
1820 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821}
1822
Howard Hinnant324bb032010-08-22 00:02:43 +00001823#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001824
Howard Hinnant99968442011-11-29 18:15:50 +00001825template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826inline _LIBCPP_INLINE_VISIBILITY
1827bool
Howard Hinnant99968442011-11-29 18:15:50 +00001828operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829
Howard Hinnant99968442011-11-29 18:15:50 +00001830template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831inline _LIBCPP_INLINE_VISIBILITY
1832bool
Howard Hinnant99968442011-11-29 18:15:50 +00001833operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834
Howard Hinnant99968442011-11-29 18:15:50 +00001835template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836inline _LIBCPP_INLINE_VISIBILITY
1837bool
Howard Hinnant99968442011-11-29 18:15:50 +00001838operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839
Howard Hinnant99968442011-11-29 18:15:50 +00001840template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841inline _LIBCPP_INLINE_VISIBILITY
1842bool
Howard Hinnant99968442011-11-29 18:15:50 +00001843operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844
Howard Hinnant99968442011-11-29 18:15:50 +00001845template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001846inline _LIBCPP_INLINE_VISIBILITY
1847void
Howard Hinnant99968442011-11-29 18:15:50 +00001848swap(function<_Fp>& __x, function<_Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849{return __x.swap(__y);}
1850
1851template<class _Tp> struct __is_bind_expression : public false_type {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001852template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001853 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1854
1855template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001856template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1858
1859namespace placeholders
1860{
1861
Howard Hinnant99968442011-11-29 18:15:50 +00001862template <int _Np> struct __ph {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863
1864extern __ph<1> _1;
1865extern __ph<2> _2;
1866extern __ph<3> _3;
1867extern __ph<4> _4;
1868extern __ph<5> _5;
1869extern __ph<6> _6;
1870extern __ph<7> _7;
1871extern __ph<8> _8;
1872extern __ph<9> _9;
1873extern __ph<10> _10;
1874
1875} // placeholders
1876
Howard Hinnant99968442011-11-29 18:15:50 +00001877template<int _Np>
1878struct __is_placeholder<placeholders::__ph<_Np> >
1879 : public integral_constant<int, _Np> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880
1881template <class _Tp, class _Uj>
1882inline _LIBCPP_INLINE_VISIBILITY
1883_Tp&
1884__mu(reference_wrapper<_Tp> __t, _Uj&)
1885{
1886 return __t.get();
1887}
1888/*
1889template <bool _IsBindExpr, class _Ti, class ..._Uj>
1890struct __mu_return1 {};
1891
1892template <class _Ti, class ..._Uj>
1893struct __mu_return1<true, _Ti, _Uj...>
1894{
1895 typedef typename result_of<_Ti(_Uj...)>::type type;
1896};
1897
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898template <class _Ti, class ..._Uj, size_t ..._Indx>
1899inline _LIBCPP_INLINE_VISIBILITY
1900typename __mu_return1<true, _Ti, _Uj...>::type
1901__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1902{
Marshall Clowba6dbf42014-06-24 00:46:19 +00001903 __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904}
1905
1906template <class _Ti, class ..._Uj>
1907inline _LIBCPP_INLINE_VISIBILITY
1908typename enable_if
1909<
1910 is_bind_expression<_Ti>::value,
1911 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1912>::type
1913__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1914{
1915 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1916 return __mu_expand(__ti, __uj, __indices());
1917}
1918
1919template <bool IsPh, class _Ti, class _Uj>
1920struct __mu_return2 {};
1921
1922template <class _Ti, class _Uj>
1923struct __mu_return2<true, _Ti, _Uj>
1924{
1925 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1926};
1927
1928template <class _Ti, class _Uj>
1929inline _LIBCPP_INLINE_VISIBILITY
1930typename enable_if
1931<
1932 0 < is_placeholder<_Ti>::value,
1933 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1934>::type
1935__mu(_Ti&, _Uj& __uj)
1936{
1937 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1938 // compiler bug workaround
Marshall Clowba6dbf42014-06-24 00:46:19 +00001939 typename tuple_element<_Indx, _Uj>::type __t = _VSTD::get<_Indx>(__uj);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940 return __t;
Marshall Clowba6dbf42014-06-24 00:46:19 +00001941// return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942}
1943
1944template <class _Ti, class _Uj>
1945inline _LIBCPP_INLINE_VISIBILITY
1946typename enable_if
1947<
1948 !is_bind_expression<_Ti>::value &&
1949 is_placeholder<_Ti>::value == 0 &&
1950 !__is_reference_wrapper<_Ti>::value,
1951 _Ti&
1952>::type
1953__mu(_Ti& __ti, _Uj& __uj)
1954{
1955 return __ti;
1956}
1957
1958template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1959struct ____mu_return;
1960
1961template <class _Ti, class ..._Uj>
1962struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1963{
1964 typedef typename result_of<_Ti(_Uj...)>::type type;
1965};
1966
1967template <class _Ti, class _TupleUj>
1968struct ____mu_return<_Ti, false, true, _TupleUj>
1969{
1970 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1971 _TupleUj>::type&& type;
1972};
1973
1974template <class _Ti, class _TupleUj>
1975struct ____mu_return<_Ti, false, false, _TupleUj>
1976{
1977 typedef _Ti& type;
1978};
1979
1980template <class _Ti, class _TupleUj>
1981struct __mu_return
1982 : public ____mu_return<_Ti,
1983 is_bind_expression<_Ti>::value,
1984 0 < is_placeholder<_Ti>::value,
1985 _TupleUj>
1986{
1987};
1988
1989template <class _Ti, class _TupleUj>
1990struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1991{
1992 typedef _Ti& type;
1993};
1994
Howard Hinnant99968442011-11-29 18:15:50 +00001995template <class _Fp, class _BoundArgs, class _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996struct __bind_return;
1997
Howard Hinnant99968442011-11-29 18:15:50 +00001998template <class _Fp, class ..._BoundArgs, class _TupleUj>
1999struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000{
2001 typedef typename __ref_return
2002 <
Howard Hinnant99968442011-11-29 18:15:50 +00002003 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 typename __mu_return
2005 <
2006 _BoundArgs,
2007 _TupleUj
2008 >::type...
2009 >::type type;
2010};
2011
Howard Hinnant99968442011-11-29 18:15:50 +00002012template <class _Fp, class ..._BoundArgs, class _TupleUj>
2013struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014{
2015 typedef typename __ref_return
2016 <
Howard Hinnant99968442011-11-29 18:15:50 +00002017 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002018 typename __mu_return
2019 <
2020 const _BoundArgs,
2021 _TupleUj
2022 >::type...
2023 >::type type;
2024};
2025
Howard Hinnant99968442011-11-29 18:15:50 +00002026template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002028typename __bind_return<_Fp, _BoundArgs, _Args>::type
2029__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030 _Args&& __args)
2031{
Marshall Clowba6dbf42014-06-24 00:46:19 +00002032 return __invoke(__f, __mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033}
2034
Howard Hinnant99968442011-11-29 18:15:50 +00002035template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036class __bind
2037{
Howard Hinnant99968442011-11-29 18:15:50 +00002038 _Fp __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039 tuple<_BoundArgs...> __bound_args_;
2040
2041 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2042public:
Howard Hinnant99968442011-11-29 18:15:50 +00002043 template <class _Gp, class ..._BA>
2044 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2045 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002046 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047
2048 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002049 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 operator()(_Args&& ...__args)
2051 {
2052 // compiler bug workaround
Howard Hinnant324bb032010-08-22 00:02:43 +00002053 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054 tuple<_Args&&...>(__args...));
2055 }
2056
2057 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002058 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002059 operator()(_Args&& ...__args) const
2060 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002061 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062 tuple<_Args&&...>(__args...));
2063 }
2064};
2065
Howard Hinnant99968442011-11-29 18:15:50 +00002066template<class _Fp, class ..._BoundArgs>
2067struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068
Howard Hinnant99968442011-11-29 18:15:50 +00002069template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070class __bind_r
Howard Hinnant99968442011-11-29 18:15:50 +00002071 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072{
Howard Hinnant99968442011-11-29 18:15:50 +00002073 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074public:
Howard Hinnant99968442011-11-29 18:15:50 +00002075 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076
Howard Hinnant99968442011-11-29 18:15:50 +00002077 template <class _Gp, class ..._BA>
2078 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2079 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002080 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081
2082 template <class ..._Args>
2083 result_type
2084 operator()(_Args&& ...__args)
2085 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002086 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087 }
2088
2089 template <class ..._Args>
2090 result_type
2091 operator()(_Args&& ...__args) const
2092 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002093 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094 }
2095};
2096
Howard Hinnant99968442011-11-29 18:15:50 +00002097template<class _Rp, class _Fp, class ..._BoundArgs>
2098struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099
Howard Hinnant99968442011-11-29 18:15:50 +00002100template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002102__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2103bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104{
Howard Hinnant99968442011-11-29 18:15:50 +00002105 typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2106 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002107}
2108
Howard Hinnant99968442011-11-29 18:15:50 +00002109template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002111__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2112bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113{
Howard Hinnant99968442011-11-29 18:15:50 +00002114 typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2115 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116}
2117*/
2118
Howard Hinnant324bb032010-08-22 00:02:43 +00002119#endif // _LIBCPP_FUNCTIONAL_03