blob: d8a9f05fa124be869052fe345d7e54a26bdd9b36 [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{
Howard Hinnant72552802010-08-20 19:36:46 +0000372 return __invoke(__f_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373}
374
Howard Hinnantd4444702010-08-11 17:04:31 +0000375#ifndef _LIBCPP_NO_RTTI
376
Howard Hinnant99968442011-11-29 18:15:50 +0000377template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000379__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000380{
Howard Hinnant99968442011-11-29 18:15:50 +0000381 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000382 return &__f_.first();
383 return (const void*)0;
384}
385
Howard Hinnant99968442011-11-29 18:15:50 +0000386template<class _Fp, class _Alloc, class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000388__func<_Fp, _Alloc, _Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389{
Howard Hinnant99968442011-11-29 18:15:50 +0000390 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391}
392
Howard Hinnant324bb032010-08-22 00:02:43 +0000393#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000394
Howard Hinnant99968442011-11-29 18:15:50 +0000395template<class _Fp, class _Alloc, class _Rp, class _A0>
396class __func<_Fp, _Alloc, _Rp(_A0)>
397 : public __base<_Rp(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398{
Howard Hinnant99968442011-11-29 18:15:50 +0000399 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000400public:
Howard Hinnant99968442011-11-29 18:15:50 +0000401 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
402 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000403 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000404 virtual __base<_Rp(_A0)>* __clone() const;
405 virtual void __clone(__base<_Rp(_A0)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406 virtual void destroy();
407 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000408 virtual _Rp operator()(_A0);
Howard Hinnantd4444702010-08-11 17:04:31 +0000409#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 virtual const void* target(const type_info&) const;
411 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000412#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413};
414
Howard Hinnant99968442011-11-29 18:15:50 +0000415template<class _Fp, class _Alloc, class _Rp, class _A0>
416__base<_Rp(_A0)>*
417__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418{
Howard Hinnant99968442011-11-29 18:15:50 +0000419 typedef typename _Alloc::template rebind<__func>::other _Ap;
420 _Ap __a(__f_.second());
421 typedef __allocator_destructor<_Ap> _Dp;
422 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
424 return __hold.release();
425}
426
Howard Hinnant99968442011-11-29 18:15:50 +0000427template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428void
Howard Hinnant99968442011-11-29 18:15:50 +0000429__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430{
431 ::new (__p) __func(__f_.first(), __f_.second());
432}
433
Howard Hinnant99968442011-11-29 18:15:50 +0000434template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435void
Howard Hinnant99968442011-11-29 18:15:50 +0000436__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437{
Howard Hinnant99968442011-11-29 18:15:50 +0000438 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439}
440
Howard Hinnant99968442011-11-29 18:15:50 +0000441template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442void
Howard Hinnant99968442011-11-29 18:15:50 +0000443__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444{
Howard Hinnant99968442011-11-29 18:15:50 +0000445 typedef typename _Alloc::template rebind<__func>::other _Ap;
446 _Ap __a(__f_.second());
447 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000448 __a.deallocate(this, 1);
449}
450
Howard Hinnant99968442011-11-29 18:15:50 +0000451template<class _Fp, class _Alloc, class _Rp, class _A0>
452_Rp
453__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454{
455 return __invoke(__f_.first(), __a0);
456}
457
Howard Hinnantd4444702010-08-11 17:04:31 +0000458#ifndef _LIBCPP_NO_RTTI
459
Howard Hinnant99968442011-11-29 18:15:50 +0000460template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000461const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000462__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463{
Howard Hinnant99968442011-11-29 18:15:50 +0000464 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465 return &__f_.first();
466 return (const void*)0;
467}
468
Howard Hinnant99968442011-11-29 18:15:50 +0000469template<class _Fp, class _Alloc, class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000471__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472{
Howard Hinnant99968442011-11-29 18:15:50 +0000473 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474}
475
Howard Hinnant324bb032010-08-22 00:02:43 +0000476#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000477
Howard Hinnant99968442011-11-29 18:15:50 +0000478template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
479class __func<_Fp, _Alloc, _Rp(_A0, _A1)>
480 : public __base<_Rp(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481{
Howard Hinnant99968442011-11-29 18:15:50 +0000482 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483public:
Howard Hinnant99968442011-11-29 18:15:50 +0000484 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
485 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000486 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000487 virtual __base<_Rp(_A0, _A1)>* __clone() const;
488 virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 virtual void destroy();
490 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000491 virtual _Rp operator()(_A0, _A1);
Howard Hinnantd4444702010-08-11 17:04:31 +0000492#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000493 virtual const void* target(const type_info&) const;
494 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000495#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000496};
497
Howard Hinnant99968442011-11-29 18:15:50 +0000498template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
499__base<_Rp(_A0, _A1)>*
500__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501{
Howard Hinnant99968442011-11-29 18:15:50 +0000502 typedef typename _Alloc::template rebind<__func>::other _Ap;
503 _Ap __a(__f_.second());
504 typedef __allocator_destructor<_Ap> _Dp;
505 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
507 return __hold.release();
508}
509
Howard Hinnant99968442011-11-29 18:15:50 +0000510template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511void
Howard Hinnant99968442011-11-29 18:15:50 +0000512__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513{
514 ::new (__p) __func(__f_.first(), __f_.second());
515}
516
Howard Hinnant99968442011-11-29 18:15:50 +0000517template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518void
Howard Hinnant99968442011-11-29 18:15:50 +0000519__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520{
Howard Hinnant99968442011-11-29 18:15:50 +0000521 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522}
523
Howard Hinnant99968442011-11-29 18:15:50 +0000524template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525void
Howard Hinnant99968442011-11-29 18:15:50 +0000526__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527{
Howard Hinnant99968442011-11-29 18:15:50 +0000528 typedef typename _Alloc::template rebind<__func>::other _Ap;
529 _Ap __a(__f_.second());
530 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000531 __a.deallocate(this, 1);
532}
533
Howard Hinnant99968442011-11-29 18:15:50 +0000534template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
535_Rp
536__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000537{
538 return __invoke(__f_.first(), __a0, __a1);
539}
540
Howard Hinnantd4444702010-08-11 17:04:31 +0000541#ifndef _LIBCPP_NO_RTTI
542
Howard Hinnant99968442011-11-29 18:15:50 +0000543template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000545__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546{
Howard Hinnant99968442011-11-29 18:15:50 +0000547 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548 return &__f_.first();
549 return (const void*)0;
550}
551
Howard Hinnant99968442011-11-29 18:15:50 +0000552template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000554__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555{
Howard Hinnant99968442011-11-29 18:15:50 +0000556 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557}
558
Howard Hinnant324bb032010-08-22 00:02:43 +0000559#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000560
Howard Hinnant99968442011-11-29 18:15:50 +0000561template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
562class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
563 : public __base<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564{
Howard Hinnant99968442011-11-29 18:15:50 +0000565 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566public:
Howard Hinnant99968442011-11-29 18:15:50 +0000567 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
568 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000569 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000570 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
571 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572 virtual void destroy();
573 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +0000574 virtual _Rp operator()(_A0, _A1, _A2);
Howard Hinnantd4444702010-08-11 17:04:31 +0000575#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576 virtual const void* target(const type_info&) const;
577 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000578#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579};
580
Howard Hinnant99968442011-11-29 18:15:50 +0000581template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
582__base<_Rp(_A0, _A1, _A2)>*
583__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584{
Howard Hinnant99968442011-11-29 18:15:50 +0000585 typedef typename _Alloc::template rebind<__func>::other _Ap;
586 _Ap __a(__f_.second());
587 typedef __allocator_destructor<_Ap> _Dp;
588 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
590 return __hold.release();
591}
592
Howard Hinnant99968442011-11-29 18:15:50 +0000593template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594void
Howard Hinnant99968442011-11-29 18:15:50 +0000595__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000596{
597 ::new (__p) __func(__f_.first(), __f_.second());
598}
599
Howard Hinnant99968442011-11-29 18:15:50 +0000600template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601void
Howard Hinnant99968442011-11-29 18:15:50 +0000602__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603{
Howard Hinnant99968442011-11-29 18:15:50 +0000604 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605}
606
Howard Hinnant99968442011-11-29 18:15:50 +0000607template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608void
Howard Hinnant99968442011-11-29 18:15:50 +0000609__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610{
Howard Hinnant99968442011-11-29 18:15:50 +0000611 typedef typename _Alloc::template rebind<__func>::other _Ap;
612 _Ap __a(__f_.second());
613 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614 __a.deallocate(this, 1);
615}
616
Howard Hinnant99968442011-11-29 18:15:50 +0000617template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
618_Rp
619__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620{
621 return __invoke(__f_.first(), __a0, __a1, __a2);
622}
623
Howard Hinnantd4444702010-08-11 17:04:31 +0000624#ifndef _LIBCPP_NO_RTTI
625
Howard Hinnant99968442011-11-29 18:15:50 +0000626template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000627const void*
Howard Hinnant99968442011-11-29 18:15:50 +0000628__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629{
Howard Hinnant99968442011-11-29 18:15:50 +0000630 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631 return &__f_.first();
632 return (const void*)0;
633}
634
Howard Hinnant99968442011-11-29 18:15:50 +0000635template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000637__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638{
Howard Hinnant99968442011-11-29 18:15:50 +0000639 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640}
641
Howard Hinnant324bb032010-08-22 00:02:43 +0000642#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000643
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644} // __function
645
Howard Hinnant99968442011-11-29 18:15:50 +0000646template<class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000647class _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648{
Howard Hinnant99968442011-11-29 18:15:50 +0000649 typedef __function::__base<_Rp()> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650 aligned_storage<3*sizeof(void*)>::type __buf_;
651 __base* __f_;
652
Howard Hinnant99968442011-11-29 18:15:50 +0000653 template <class _Fp>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000655 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 template <class _R2>
Marshall Clow2f9e7142014-06-30 21:27:51 +0000657 _LIBCPP_INLINE_VISIBILITY
658 static bool __not_null(_R2 (*__p)()) {return __p;}
659 template <class _R2>
660 _LIBCPP_INLINE_VISIBILITY
661 static bool __not_null(const function<_R2()>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662public:
Howard Hinnant99968442011-11-29 18:15:50 +0000663 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664
665 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000666 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
667 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000669 template<class _Fp>
670 function(_Fp,
671 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672
Howard Hinnant72552802010-08-20 19:36:46 +0000673 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000675 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
676 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000678 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
679 template<class _Alloc>
680 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000681 template<class _Fp, class _Alloc>
682 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
683 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684
685 function& operator=(const function&);
686 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000687 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688 typename enable_if
689 <
Howard Hinnant99968442011-11-29 18:15:50 +0000690 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691 function&
692 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000693 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000694
695 ~function();
696
697 // 20.7.16.2.2, function modifiers:
698 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000699 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000701 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +0000702 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703
704 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +0000705 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706
707private:
708 // deleted overloads close possible hole in the type system
709 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000710 bool operator==(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000712 bool operator!=(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713public:
714 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +0000715 _Rp operator()() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716
Howard Hinnantd4444702010-08-11 17:04:31 +0000717#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718 // 20.7.16.2.5, function target access:
719 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +0000720 template <typename _Tp> _Tp* target();
721 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000722#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723};
724
Howard Hinnant99968442011-11-29 18:15:50 +0000725template<class _Rp>
726function<_Rp()>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727{
728 if (__f.__f_ == 0)
729 __f_ = 0;
730 else if (__f.__f_ == (const __base*)&__f.__buf_)
731 {
732 __f_ = (__base*)&__buf_;
733 __f.__f_->__clone(__f_);
734 }
735 else
736 __f_ = __f.__f_->__clone();
737}
738
Howard Hinnant99968442011-11-29 18:15:50 +0000739template<class _Rp>
Howard Hinnant72552802010-08-20 19:36:46 +0000740template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +0000741function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +0000742{
743 if (__f.__f_ == 0)
744 __f_ = 0;
745 else if (__f.__f_ == (const __base*)&__f.__buf_)
746 {
747 __f_ = (__base*)&__buf_;
748 __f.__f_->__clone(__f_);
749 }
750 else
751 __f_ = __f.__f_->__clone();
752}
753
Howard Hinnant99968442011-11-29 18:15:50 +0000754template<class _Rp>
755template <class _Fp>
756function<_Rp()>::function(_Fp __f,
757 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758 : __f_(0)
759{
760 if (__not_null(__f))
761 {
Howard Hinnant99968442011-11-29 18:15:50 +0000762 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763 if (sizeof(_FF) <= sizeof(__buf_))
764 {
765 __f_ = (__base*)&__buf_;
766 ::new (__f_) _FF(__f);
767 }
768 else
769 {
Howard Hinnant99968442011-11-29 18:15:50 +0000770 typedef allocator<_FF> _Ap;
771 _Ap __a;
772 typedef __allocator_destructor<_Ap> _Dp;
773 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
774 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000775 __f_ = __hold.release();
776 }
777 }
778}
779
Howard Hinnant99968442011-11-29 18:15:50 +0000780template<class _Rp>
781template <class _Fp, class _Alloc>
782function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
783 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +0000784 : __f_(0)
785{
786 typedef allocator_traits<_Alloc> __alloc_traits;
787 if (__not_null(__f))
788 {
Howard Hinnant99968442011-11-29 18:15:50 +0000789 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +0000790 if (sizeof(_FF) <= sizeof(__buf_))
791 {
792 __f_ = (__base*)&__buf_;
793 ::new (__f_) _FF(__f);
794 }
795 else
796 {
797 typedef typename __alloc_traits::template
798#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
799 rebind_alloc<_FF>
800#else
801 rebind_alloc<_FF>::other
802#endif
Howard Hinnant99968442011-11-29 18:15:50 +0000803 _Ap;
804 _Ap __a(__a0);
805 typedef __allocator_destructor<_Ap> _Dp;
806 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +0000807 ::new (__hold.get()) _FF(__f, _Alloc(__a));
808 __f_ = __hold.release();
809 }
810 }
811}
812
Howard Hinnant99968442011-11-29 18:15:50 +0000813template<class _Rp>
814function<_Rp()>&
815function<_Rp()>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816{
817 function(__f).swap(*this);
818 return *this;
819}
820
Howard Hinnant99968442011-11-29 18:15:50 +0000821template<class _Rp>
822function<_Rp()>&
823function<_Rp()>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824{
825 if (__f_ == (__base*)&__buf_)
826 __f_->destroy();
827 else if (__f_)
828 __f_->destroy_deallocate();
829 __f_ = 0;
830}
831
Howard Hinnant99968442011-11-29 18:15:50 +0000832template<class _Rp>
833template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834typename enable_if
835<
Howard Hinnant99968442011-11-29 18:15:50 +0000836 !is_integral<_Fp>::value,
837 function<_Rp()>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838>::type
Howard Hinnant99968442011-11-29 18:15:50 +0000839function<_Rp()>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000840{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000841 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842 return *this;
843}
844
Howard Hinnant99968442011-11-29 18:15:50 +0000845template<class _Rp>
846function<_Rp()>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847{
848 if (__f_ == (__base*)&__buf_)
849 __f_->destroy();
850 else if (__f_)
851 __f_->destroy_deallocate();
852}
853
Howard Hinnant99968442011-11-29 18:15:50 +0000854template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000855void
Howard Hinnant99968442011-11-29 18:15:50 +0000856function<_Rp()>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857{
858 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
859 {
860 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
861 __base* __t = (__base*)&__tempbuf;
862 __f_->__clone(__t);
863 __f_->destroy();
864 __f_ = 0;
865 __f.__f_->__clone((__base*)&__buf_);
866 __f.__f_->destroy();
867 __f.__f_ = 0;
868 __f_ = (__base*)&__buf_;
869 __t->__clone((__base*)&__f.__buf_);
870 __t->destroy();
871 __f.__f_ = (__base*)&__f.__buf_;
872 }
873 else if (__f_ == (__base*)&__buf_)
874 {
875 __f_->__clone((__base*)&__f.__buf_);
876 __f_->destroy();
877 __f_ = __f.__f_;
878 __f.__f_ = (__base*)&__f.__buf_;
879 }
880 else if (__f.__f_ == (__base*)&__f.__buf_)
881 {
882 __f.__f_->__clone((__base*)&__buf_);
883 __f.__f_->destroy();
884 __f.__f_ = __f_;
885 __f_ = (__base*)&__buf_;
886 }
887 else
Howard Hinnant0949eed2011-06-30 21:18:19 +0000888 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889}
890
Howard Hinnant99968442011-11-29 18:15:50 +0000891template<class _Rp>
892_Rp
893function<_Rp()>::operator()() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894{
Howard Hinnantd4444702010-08-11 17:04:31 +0000895#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896 if (__f_ == 0)
897 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +0000898#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899 return (*__f_)();
900}
901
Howard Hinnantd4444702010-08-11 17:04:31 +0000902#ifndef _LIBCPP_NO_RTTI
903
Howard Hinnant99968442011-11-29 18:15:50 +0000904template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000906function<_Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000907{
908 if (__f_ == 0)
909 return typeid(void);
910 return __f_->target_type();
911}
912
Howard Hinnant99968442011-11-29 18:15:50 +0000913template<class _Rp>
914template <typename _Tp>
915_Tp*
916function<_Rp()>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917{
918 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000919 return (_Tp*)0;
920 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921}
922
Howard Hinnant99968442011-11-29 18:15:50 +0000923template<class _Rp>
924template <typename _Tp>
925const _Tp*
926function<_Rp()>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000927{
928 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000929 return (const _Tp*)0;
930 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931}
932
Howard Hinnant324bb032010-08-22 00:02:43 +0000933#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000934
Howard Hinnant99968442011-11-29 18:15:50 +0000935template<class _Rp, class _A0>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000936class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
Howard Hinnant99968442011-11-29 18:15:50 +0000937 : public unary_function<_A0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938{
Howard Hinnant99968442011-11-29 18:15:50 +0000939 typedef __function::__base<_Rp(_A0)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 aligned_storage<3*sizeof(void*)>::type __buf_;
941 __base* __f_;
942
Howard Hinnant99968442011-11-29 18:15:50 +0000943 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000945 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +0000949 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000951 static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
952 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000954 static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
955 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000957 static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
958 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000960 static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000962 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +0000963 static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964public:
Howard Hinnant99968442011-11-29 18:15:50 +0000965 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000966
967 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000968 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
969 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000971 template<class _Fp>
972 function(_Fp,
973 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974
Howard Hinnant72552802010-08-20 19:36:46 +0000975 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000977 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
978 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000980 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
981 template<class _Alloc>
982 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000983 template<class _Fp, class _Alloc>
984 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
985 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000986
987 function& operator=(const function&);
988 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000989 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990 typename enable_if
991 <
Howard Hinnant99968442011-11-29 18:15:50 +0000992 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993 function&
994 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000995 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000996
997 ~function();
998
999 // 20.7.16.2.2, function modifiers:
1000 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001001 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001003 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001004 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005
1006 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001007 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008
1009private:
1010 // deleted overloads close possible hole in the type system
1011 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001012 bool operator==(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001014 bool operator!=(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015public:
1016 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001017 _Rp operator()(_A0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018
Howard Hinnantd4444702010-08-11 17:04:31 +00001019#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020 // 20.7.16.2.5, function target access:
1021 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001022 template <typename _Tp> _Tp* target();
1023 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001024#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025};
1026
Howard Hinnant99968442011-11-29 18:15:50 +00001027template<class _Rp, class _A0>
1028function<_Rp(_A0)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001029{
1030 if (__f.__f_ == 0)
1031 __f_ = 0;
1032 else if (__f.__f_ == (const __base*)&__f.__buf_)
1033 {
1034 __f_ = (__base*)&__buf_;
1035 __f.__f_->__clone(__f_);
1036 }
1037 else
1038 __f_ = __f.__f_->__clone();
1039}
1040
Howard Hinnant99968442011-11-29 18:15:50 +00001041template<class _Rp, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001042template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001043function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001044{
1045 if (__f.__f_ == 0)
1046 __f_ = 0;
1047 else if (__f.__f_ == (const __base*)&__f.__buf_)
1048 {
1049 __f_ = (__base*)&__buf_;
1050 __f.__f_->__clone(__f_);
1051 }
1052 else
1053 __f_ = __f.__f_->__clone();
1054}
1055
Howard Hinnant99968442011-11-29 18:15:50 +00001056template<class _Rp, class _A0>
1057template <class _Fp>
1058function<_Rp(_A0)>::function(_Fp __f,
1059 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060 : __f_(0)
1061{
1062 if (__not_null(__f))
1063 {
Howard Hinnant99968442011-11-29 18:15:50 +00001064 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065 if (sizeof(_FF) <= sizeof(__buf_))
1066 {
1067 __f_ = (__base*)&__buf_;
1068 ::new (__f_) _FF(__f);
1069 }
1070 else
1071 {
Howard Hinnant99968442011-11-29 18:15:50 +00001072 typedef allocator<_FF> _Ap;
1073 _Ap __a;
1074 typedef __allocator_destructor<_Ap> _Dp;
1075 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1076 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077 __f_ = __hold.release();
1078 }
1079 }
1080}
1081
Howard Hinnant99968442011-11-29 18:15:50 +00001082template<class _Rp, class _A0>
1083template <class _Fp, class _Alloc>
1084function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1085 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001086 : __f_(0)
1087{
1088 typedef allocator_traits<_Alloc> __alloc_traits;
1089 if (__not_null(__f))
1090 {
Howard Hinnant99968442011-11-29 18:15:50 +00001091 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001092 if (sizeof(_FF) <= sizeof(__buf_))
1093 {
1094 __f_ = (__base*)&__buf_;
1095 ::new (__f_) _FF(__f);
1096 }
1097 else
1098 {
1099 typedef typename __alloc_traits::template
1100#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1101 rebind_alloc<_FF>
1102#else
1103 rebind_alloc<_FF>::other
1104#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001105 _Ap;
1106 _Ap __a(__a0);
1107 typedef __allocator_destructor<_Ap> _Dp;
1108 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001109 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1110 __f_ = __hold.release();
1111 }
1112 }
1113}
1114
Howard Hinnant99968442011-11-29 18:15:50 +00001115template<class _Rp, class _A0>
1116function<_Rp(_A0)>&
1117function<_Rp(_A0)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118{
1119 function(__f).swap(*this);
1120 return *this;
1121}
1122
Howard Hinnant99968442011-11-29 18:15:50 +00001123template<class _Rp, class _A0>
1124function<_Rp(_A0)>&
1125function<_Rp(_A0)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126{
1127 if (__f_ == (__base*)&__buf_)
1128 __f_->destroy();
1129 else if (__f_)
1130 __f_->destroy_deallocate();
1131 __f_ = 0;
1132}
1133
Howard Hinnant99968442011-11-29 18:15:50 +00001134template<class _Rp, class _A0>
1135template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136typename enable_if
1137<
Howard Hinnant99968442011-11-29 18:15:50 +00001138 !is_integral<_Fp>::value,
1139 function<_Rp(_A0)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001141function<_Rp(_A0)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001143 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144 return *this;
1145}
1146
Howard Hinnant99968442011-11-29 18:15:50 +00001147template<class _Rp, class _A0>
1148function<_Rp(_A0)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149{
1150 if (__f_ == (__base*)&__buf_)
1151 __f_->destroy();
1152 else if (__f_)
1153 __f_->destroy_deallocate();
1154}
1155
Howard Hinnant99968442011-11-29 18:15:50 +00001156template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001157void
Howard Hinnant99968442011-11-29 18:15:50 +00001158function<_Rp(_A0)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159{
1160 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1161 {
1162 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1163 __base* __t = (__base*)&__tempbuf;
1164 __f_->__clone(__t);
1165 __f_->destroy();
1166 __f_ = 0;
1167 __f.__f_->__clone((__base*)&__buf_);
1168 __f.__f_->destroy();
1169 __f.__f_ = 0;
1170 __f_ = (__base*)&__buf_;
1171 __t->__clone((__base*)&__f.__buf_);
1172 __t->destroy();
1173 __f.__f_ = (__base*)&__f.__buf_;
1174 }
1175 else if (__f_ == (__base*)&__buf_)
1176 {
1177 __f_->__clone((__base*)&__f.__buf_);
1178 __f_->destroy();
1179 __f_ = __f.__f_;
1180 __f.__f_ = (__base*)&__f.__buf_;
1181 }
1182 else if (__f.__f_ == (__base*)&__f.__buf_)
1183 {
1184 __f.__f_->__clone((__base*)&__buf_);
1185 __f.__f_->destroy();
1186 __f.__f_ = __f_;
1187 __f_ = (__base*)&__buf_;
1188 }
1189 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001190 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191}
1192
Howard Hinnant99968442011-11-29 18:15:50 +00001193template<class _Rp, class _A0>
1194_Rp
1195function<_Rp(_A0)>::operator()(_A0 __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196{
Howard Hinnantd4444702010-08-11 17:04:31 +00001197#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001198 if (__f_ == 0)
1199 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001200#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001201 return (*__f_)(__a0);
1202}
1203
Howard Hinnantd4444702010-08-11 17:04:31 +00001204#ifndef _LIBCPP_NO_RTTI
1205
Howard Hinnant99968442011-11-29 18:15:50 +00001206template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001208function<_Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209{
1210 if (__f_ == 0)
1211 return typeid(void);
1212 return __f_->target_type();
1213}
1214
Howard Hinnant99968442011-11-29 18:15:50 +00001215template<class _Rp, class _A0>
1216template <typename _Tp>
1217_Tp*
1218function<_Rp(_A0)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219{
1220 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001221 return (_Tp*)0;
1222 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001223}
1224
Howard Hinnant99968442011-11-29 18:15:50 +00001225template<class _Rp, class _A0>
1226template <typename _Tp>
1227const _Tp*
1228function<_Rp(_A0)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229{
1230 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001231 return (const _Tp*)0;
1232 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233}
1234
Howard Hinnant324bb032010-08-22 00:02:43 +00001235#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001236
Howard Hinnant99968442011-11-29 18:15:50 +00001237template<class _Rp, class _A0, class _A1>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001238class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
Howard Hinnant99968442011-11-29 18:15:50 +00001239 : public binary_function<_A0, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240{
Howard Hinnant99968442011-11-29 18:15:50 +00001241 typedef __function::__base<_Rp(_A0, _A1)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242 aligned_storage<3*sizeof(void*)>::type __buf_;
1243 __base* __f_;
1244
Howard Hinnant99968442011-11-29 18:15:50 +00001245 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001247 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001250 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001251 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)) {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) {return __p;}
1257 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001259 static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1260 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001262 static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001264 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001265 static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266public:
Howard Hinnant99968442011-11-29 18:15:50 +00001267 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268
1269 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001270 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1271 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001273 template<class _Fp>
1274 function(_Fp,
1275 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276
Howard Hinnant72552802010-08-20 19:36:46 +00001277 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001279 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1280 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001282 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1283 template<class _Alloc>
1284 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001285 template<class _Fp, class _Alloc>
1286 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1287 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288
1289 function& operator=(const function&);
1290 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001291 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292 typename enable_if
1293 <
Howard Hinnant99968442011-11-29 18:15:50 +00001294 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295 function&
1296 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001297 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298
1299 ~function();
1300
1301 // 20.7.16.2.2, function modifiers:
1302 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001303 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001305 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001306 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307
1308 // 20.7.16.2.3, function capacity:
1309 operator bool() const {return __f_;}
1310
1311private:
1312 // deleted overloads close possible hole in the type system
1313 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001314 bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001316 bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317public:
1318 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001319 _Rp operator()(_A0, _A1) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320
Howard Hinnantd4444702010-08-11 17:04:31 +00001321#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322 // 20.7.16.2.5, function target access:
1323 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001324 template <typename _Tp> _Tp* target();
1325 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001326#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327};
1328
Howard Hinnant99968442011-11-29 18:15:50 +00001329template<class _Rp, class _A0, class _A1>
1330function<_Rp(_A0, _A1)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331{
1332 if (__f.__f_ == 0)
1333 __f_ = 0;
1334 else if (__f.__f_ == (const __base*)&__f.__buf_)
1335 {
1336 __f_ = (__base*)&__buf_;
1337 __f.__f_->__clone(__f_);
1338 }
1339 else
1340 __f_ = __f.__f_->__clone();
1341}
1342
Howard Hinnant99968442011-11-29 18:15:50 +00001343template<class _Rp, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001344template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001345function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001346{
1347 if (__f.__f_ == 0)
1348 __f_ = 0;
1349 else if (__f.__f_ == (const __base*)&__f.__buf_)
1350 {
1351 __f_ = (__base*)&__buf_;
1352 __f.__f_->__clone(__f_);
1353 }
1354 else
1355 __f_ = __f.__f_->__clone();
1356}
1357
Howard Hinnant99968442011-11-29 18:15:50 +00001358template<class _Rp, class _A0, class _A1>
1359template <class _Fp>
1360function<_Rp(_A0, _A1)>::function(_Fp __f,
1361 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362 : __f_(0)
1363{
1364 if (__not_null(__f))
1365 {
Howard Hinnant99968442011-11-29 18:15:50 +00001366 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367 if (sizeof(_FF) <= sizeof(__buf_))
1368 {
1369 __f_ = (__base*)&__buf_;
1370 ::new (__f_) _FF(__f);
1371 }
1372 else
1373 {
Howard Hinnant99968442011-11-29 18:15:50 +00001374 typedef allocator<_FF> _Ap;
1375 _Ap __a;
1376 typedef __allocator_destructor<_Ap> _Dp;
1377 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1378 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 __f_ = __hold.release();
1380 }
1381 }
1382}
1383
Howard Hinnant99968442011-11-29 18:15:50 +00001384template<class _Rp, class _A0, class _A1>
1385template <class _Fp, class _Alloc>
1386function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1387 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001388 : __f_(0)
1389{
1390 typedef allocator_traits<_Alloc> __alloc_traits;
1391 if (__not_null(__f))
1392 {
Howard Hinnant99968442011-11-29 18:15:50 +00001393 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001394 if (sizeof(_FF) <= sizeof(__buf_))
1395 {
1396 __f_ = (__base*)&__buf_;
1397 ::new (__f_) _FF(__f);
1398 }
1399 else
1400 {
1401 typedef typename __alloc_traits::template
1402#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1403 rebind_alloc<_FF>
1404#else
1405 rebind_alloc<_FF>::other
1406#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001407 _Ap;
1408 _Ap __a(__a0);
1409 typedef __allocator_destructor<_Ap> _Dp;
1410 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001411 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1412 __f_ = __hold.release();
1413 }
1414 }
1415}
1416
Howard Hinnant99968442011-11-29 18:15:50 +00001417template<class _Rp, class _A0, class _A1>
1418function<_Rp(_A0, _A1)>&
1419function<_Rp(_A0, _A1)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420{
1421 function(__f).swap(*this);
1422 return *this;
1423}
1424
Howard Hinnant99968442011-11-29 18:15:50 +00001425template<class _Rp, class _A0, class _A1>
1426function<_Rp(_A0, _A1)>&
1427function<_Rp(_A0, _A1)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428{
1429 if (__f_ == (__base*)&__buf_)
1430 __f_->destroy();
1431 else if (__f_)
1432 __f_->destroy_deallocate();
1433 __f_ = 0;
1434}
1435
Howard Hinnant99968442011-11-29 18:15:50 +00001436template<class _Rp, class _A0, class _A1>
1437template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001438typename enable_if
1439<
Howard Hinnant99968442011-11-29 18:15:50 +00001440 !is_integral<_Fp>::value,
1441 function<_Rp(_A0, _A1)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001443function<_Rp(_A0, _A1)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001445 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 return *this;
1447}
1448
Howard Hinnant99968442011-11-29 18:15:50 +00001449template<class _Rp, class _A0, class _A1>
1450function<_Rp(_A0, _A1)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451{
1452 if (__f_ == (__base*)&__buf_)
1453 __f_->destroy();
1454 else if (__f_)
1455 __f_->destroy_deallocate();
1456}
1457
Howard Hinnant99968442011-11-29 18:15:50 +00001458template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459void
Howard Hinnant99968442011-11-29 18:15:50 +00001460function<_Rp(_A0, _A1)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461{
1462 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1463 {
1464 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1465 __base* __t = (__base*)&__tempbuf;
1466 __f_->__clone(__t);
1467 __f_->destroy();
1468 __f_ = 0;
1469 __f.__f_->__clone((__base*)&__buf_);
1470 __f.__f_->destroy();
1471 __f.__f_ = 0;
1472 __f_ = (__base*)&__buf_;
1473 __t->__clone((__base*)&__f.__buf_);
1474 __t->destroy();
1475 __f.__f_ = (__base*)&__f.__buf_;
1476 }
1477 else if (__f_ == (__base*)&__buf_)
1478 {
1479 __f_->__clone((__base*)&__f.__buf_);
1480 __f_->destroy();
1481 __f_ = __f.__f_;
1482 __f.__f_ = (__base*)&__f.__buf_;
1483 }
1484 else if (__f.__f_ == (__base*)&__f.__buf_)
1485 {
1486 __f.__f_->__clone((__base*)&__buf_);
1487 __f.__f_->destroy();
1488 __f.__f_ = __f_;
1489 __f_ = (__base*)&__buf_;
1490 }
1491 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001492 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493}
1494
Howard Hinnant99968442011-11-29 18:15:50 +00001495template<class _Rp, class _A0, class _A1>
1496_Rp
1497function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498{
Howard Hinnantd4444702010-08-11 17:04:31 +00001499#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500 if (__f_ == 0)
1501 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001502#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503 return (*__f_)(__a0, __a1);
1504}
1505
Howard Hinnantd4444702010-08-11 17:04:31 +00001506#ifndef _LIBCPP_NO_RTTI
1507
Howard Hinnant99968442011-11-29 18:15:50 +00001508template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001510function<_Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511{
1512 if (__f_ == 0)
1513 return typeid(void);
1514 return __f_->target_type();
1515}
1516
Howard Hinnant99968442011-11-29 18:15:50 +00001517template<class _Rp, class _A0, class _A1>
1518template <typename _Tp>
1519_Tp*
1520function<_Rp(_A0, _A1)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521{
1522 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001523 return (_Tp*)0;
1524 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525}
1526
Howard Hinnant99968442011-11-29 18:15:50 +00001527template<class _Rp, class _A0, class _A1>
1528template <typename _Tp>
1529const _Tp*
1530function<_Rp(_A0, _A1)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001531{
1532 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001533 return (const _Tp*)0;
1534 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535}
1536
Howard Hinnant324bb032010-08-22 00:02:43 +00001537#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001538
Howard Hinnant99968442011-11-29 18:15:50 +00001539template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001540class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541{
Howard Hinnant99968442011-11-29 18:15:50 +00001542 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 aligned_storage<3*sizeof(void*)>::type __buf_;
1544 __base* __f_;
1545
Howard Hinnant99968442011-11-29 18:15:50 +00001546 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001548 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001549 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001552 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001554 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1555 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001557 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1558 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001560 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1561 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001563 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001565 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2f9e7142014-06-30 21:27:51 +00001566 static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567public:
Howard Hinnant99968442011-11-29 18:15:50 +00001568 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569
1570 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001571 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1572 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001574 template<class _Fp>
1575 function(_Fp,
1576 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577
Howard Hinnant72552802010-08-20 19:36:46 +00001578 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001580 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1581 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001583 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1584 template<class _Alloc>
1585 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001586 template<class _Fp, class _Alloc>
1587 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1588 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589
1590 function& operator=(const function&);
1591 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001592 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593 typename enable_if
1594 <
Howard Hinnant99968442011-11-29 18:15:50 +00001595 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596 function&
1597 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001598 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599
1600 ~function();
1601
1602 // 20.7.16.2.2, function modifiers:
1603 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001604 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001606 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001607 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608
1609 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001610 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611
1612private:
1613 // deleted overloads close possible hole in the type system
1614 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001615 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001617 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618public:
1619 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001620 _Rp operator()(_A0, _A1, _A2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001621
Howard Hinnantd4444702010-08-11 17:04:31 +00001622#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623 // 20.7.16.2.5, function target access:
1624 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001625 template <typename _Tp> _Tp* target();
1626 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001627#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628};
1629
Howard Hinnant99968442011-11-29 18:15:50 +00001630template<class _Rp, class _A0, class _A1, class _A2>
1631function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632{
1633 if (__f.__f_ == 0)
1634 __f_ = 0;
1635 else if (__f.__f_ == (const __base*)&__f.__buf_)
1636 {
1637 __f_ = (__base*)&__buf_;
1638 __f.__f_->__clone(__f_);
1639 }
1640 else
1641 __f_ = __f.__f_->__clone();
1642}
1643
Howard Hinnant99968442011-11-29 18:15:50 +00001644template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001645template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001646function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnant72552802010-08-20 19:36:46 +00001647 const function& __f)
1648{
1649 if (__f.__f_ == 0)
1650 __f_ = 0;
1651 else if (__f.__f_ == (const __base*)&__f.__buf_)
1652 {
1653 __f_ = (__base*)&__buf_;
1654 __f.__f_->__clone(__f_);
1655 }
1656 else
1657 __f_ = __f.__f_->__clone();
1658}
1659
Howard Hinnant99968442011-11-29 18:15:50 +00001660template<class _Rp, class _A0, class _A1, class _A2>
1661template <class _Fp>
1662function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1663 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664 : __f_(0)
1665{
1666 if (__not_null(__f))
1667 {
Howard Hinnant99968442011-11-29 18:15:50 +00001668 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001669 if (sizeof(_FF) <= sizeof(__buf_))
1670 {
1671 __f_ = (__base*)&__buf_;
1672 ::new (__f_) _FF(__f);
1673 }
1674 else
1675 {
Howard Hinnant99968442011-11-29 18:15:50 +00001676 typedef allocator<_FF> _Ap;
1677 _Ap __a;
1678 typedef __allocator_destructor<_Ap> _Dp;
1679 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1680 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681 __f_ = __hold.release();
1682 }
1683 }
1684}
1685
Howard Hinnant99968442011-11-29 18:15:50 +00001686template<class _Rp, class _A0, class _A1, class _A2>
1687template <class _Fp, class _Alloc>
1688function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1689 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001690 : __f_(0)
1691{
1692 typedef allocator_traits<_Alloc> __alloc_traits;
1693 if (__not_null(__f))
1694 {
Howard Hinnant99968442011-11-29 18:15:50 +00001695 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001696 if (sizeof(_FF) <= sizeof(__buf_))
1697 {
1698 __f_ = (__base*)&__buf_;
1699 ::new (__f_) _FF(__f);
1700 }
1701 else
1702 {
1703 typedef typename __alloc_traits::template
1704#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1705 rebind_alloc<_FF>
1706#else
1707 rebind_alloc<_FF>::other
1708#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001709 _Ap;
1710 _Ap __a(__a0);
1711 typedef __allocator_destructor<_Ap> _Dp;
1712 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001713 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1714 __f_ = __hold.release();
1715 }
1716 }
1717}
1718
Howard Hinnant99968442011-11-29 18:15:50 +00001719template<class _Rp, class _A0, class _A1, class _A2>
1720function<_Rp(_A0, _A1, _A2)>&
1721function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722{
1723 function(__f).swap(*this);
1724 return *this;
1725}
1726
Howard Hinnant99968442011-11-29 18:15:50 +00001727template<class _Rp, class _A0, class _A1, class _A2>
1728function<_Rp(_A0, _A1, _A2)>&
1729function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730{
1731 if (__f_ == (__base*)&__buf_)
1732 __f_->destroy();
1733 else if (__f_)
1734 __f_->destroy_deallocate();
1735 __f_ = 0;
1736}
1737
Howard Hinnant99968442011-11-29 18:15:50 +00001738template<class _Rp, class _A0, class _A1, class _A2>
1739template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740typename enable_if
1741<
Howard Hinnant99968442011-11-29 18:15:50 +00001742 !is_integral<_Fp>::value,
1743 function<_Rp(_A0, _A1, _A2)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001745function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001746{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001747 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748 return *this;
1749}
1750
Howard Hinnant99968442011-11-29 18:15:50 +00001751template<class _Rp, class _A0, class _A1, class _A2>
1752function<_Rp(_A0, _A1, _A2)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753{
1754 if (__f_ == (__base*)&__buf_)
1755 __f_->destroy();
1756 else if (__f_)
1757 __f_->destroy_deallocate();
1758}
1759
Howard Hinnant99968442011-11-29 18:15:50 +00001760template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761void
Howard Hinnant99968442011-11-29 18:15:50 +00001762function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763{
1764 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1765 {
1766 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1767 __base* __t = (__base*)&__tempbuf;
1768 __f_->__clone(__t);
1769 __f_->destroy();
1770 __f_ = 0;
1771 __f.__f_->__clone((__base*)&__buf_);
1772 __f.__f_->destroy();
1773 __f.__f_ = 0;
1774 __f_ = (__base*)&__buf_;
1775 __t->__clone((__base*)&__f.__buf_);
1776 __t->destroy();
1777 __f.__f_ = (__base*)&__f.__buf_;
1778 }
1779 else if (__f_ == (__base*)&__buf_)
1780 {
1781 __f_->__clone((__base*)&__f.__buf_);
1782 __f_->destroy();
1783 __f_ = __f.__f_;
1784 __f.__f_ = (__base*)&__f.__buf_;
1785 }
1786 else if (__f.__f_ == (__base*)&__f.__buf_)
1787 {
1788 __f.__f_->__clone((__base*)&__buf_);
1789 __f.__f_->destroy();
1790 __f.__f_ = __f_;
1791 __f_ = (__base*)&__buf_;
1792 }
1793 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001794 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795}
1796
Howard Hinnant99968442011-11-29 18:15:50 +00001797template<class _Rp, class _A0, class _A1, class _A2>
1798_Rp
1799function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800{
Howard Hinnantd4444702010-08-11 17:04:31 +00001801#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 if (__f_ == 0)
1803 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001804#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 return (*__f_)(__a0, __a1, __a2);
1806}
1807
Howard Hinnantd4444702010-08-11 17:04:31 +00001808#ifndef _LIBCPP_NO_RTTI
1809
Howard Hinnant99968442011-11-29 18:15:50 +00001810template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001812function<_Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813{
1814 if (__f_ == 0)
1815 return typeid(void);
1816 return __f_->target_type();
1817}
1818
Howard Hinnant99968442011-11-29 18:15:50 +00001819template<class _Rp, class _A0, class _A1, class _A2>
1820template <typename _Tp>
1821_Tp*
1822function<_Rp(_A0, _A1, _A2)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823{
1824 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001825 return (_Tp*)0;
1826 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827}
1828
Howard Hinnant99968442011-11-29 18:15:50 +00001829template<class _Rp, class _A0, class _A1, class _A2>
1830template <typename _Tp>
1831const _Tp*
1832function<_Rp(_A0, _A1, _A2)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833{
1834 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001835 return (const _Tp*)0;
1836 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837}
1838
Howard Hinnant324bb032010-08-22 00:02:43 +00001839#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001840
Howard Hinnant99968442011-11-29 18:15:50 +00001841template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842inline _LIBCPP_INLINE_VISIBILITY
1843bool
Howard Hinnant99968442011-11-29 18:15:50 +00001844operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845
Howard Hinnant99968442011-11-29 18:15:50 +00001846template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847inline _LIBCPP_INLINE_VISIBILITY
1848bool
Howard Hinnant99968442011-11-29 18:15:50 +00001849operator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850
Howard Hinnant99968442011-11-29 18:15:50 +00001851template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852inline _LIBCPP_INLINE_VISIBILITY
1853bool
Howard Hinnant99968442011-11-29 18:15:50 +00001854operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855
Howard Hinnant99968442011-11-29 18:15:50 +00001856template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857inline _LIBCPP_INLINE_VISIBILITY
1858bool
Howard Hinnant99968442011-11-29 18:15:50 +00001859operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860
Howard Hinnant99968442011-11-29 18:15:50 +00001861template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862inline _LIBCPP_INLINE_VISIBILITY
1863void
Howard Hinnant99968442011-11-29 18:15:50 +00001864swap(function<_Fp>& __x, function<_Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865{return __x.swap(__y);}
1866
1867template<class _Tp> struct __is_bind_expression : public false_type {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001868template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1870
1871template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001872template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001873 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1874
1875namespace placeholders
1876{
1877
Howard Hinnant99968442011-11-29 18:15:50 +00001878template <int _Np> struct __ph {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879
1880extern __ph<1> _1;
1881extern __ph<2> _2;
1882extern __ph<3> _3;
1883extern __ph<4> _4;
1884extern __ph<5> _5;
1885extern __ph<6> _6;
1886extern __ph<7> _7;
1887extern __ph<8> _8;
1888extern __ph<9> _9;
1889extern __ph<10> _10;
1890
1891} // placeholders
1892
Howard Hinnant99968442011-11-29 18:15:50 +00001893template<int _Np>
1894struct __is_placeholder<placeholders::__ph<_Np> >
1895 : public integral_constant<int, _Np> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896
1897template <class _Tp, class _Uj>
1898inline _LIBCPP_INLINE_VISIBILITY
1899_Tp&
1900__mu(reference_wrapper<_Tp> __t, _Uj&)
1901{
1902 return __t.get();
1903}
1904/*
1905template <bool _IsBindExpr, class _Ti, class ..._Uj>
1906struct __mu_return1 {};
1907
1908template <class _Ti, class ..._Uj>
1909struct __mu_return1<true, _Ti, _Uj...>
1910{
1911 typedef typename result_of<_Ti(_Uj...)>::type type;
1912};
1913
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914template <class _Ti, class ..._Uj, size_t ..._Indx>
1915inline _LIBCPP_INLINE_VISIBILITY
1916typename __mu_return1<true, _Ti, _Uj...>::type
1917__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1918{
Marshall Clowba6dbf42014-06-24 00:46:19 +00001919 __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920}
1921
1922template <class _Ti, class ..._Uj>
1923inline _LIBCPP_INLINE_VISIBILITY
1924typename enable_if
1925<
1926 is_bind_expression<_Ti>::value,
1927 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1928>::type
1929__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1930{
1931 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1932 return __mu_expand(__ti, __uj, __indices());
1933}
1934
1935template <bool IsPh, class _Ti, class _Uj>
1936struct __mu_return2 {};
1937
1938template <class _Ti, class _Uj>
1939struct __mu_return2<true, _Ti, _Uj>
1940{
1941 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1942};
1943
1944template <class _Ti, class _Uj>
1945inline _LIBCPP_INLINE_VISIBILITY
1946typename enable_if
1947<
1948 0 < is_placeholder<_Ti>::value,
1949 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1950>::type
1951__mu(_Ti&, _Uj& __uj)
1952{
1953 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1954 // compiler bug workaround
Marshall Clowba6dbf42014-06-24 00:46:19 +00001955 typename tuple_element<_Indx, _Uj>::type __t = _VSTD::get<_Indx>(__uj);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 return __t;
Marshall Clowba6dbf42014-06-24 00:46:19 +00001957// return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001958}
1959
1960template <class _Ti, class _Uj>
1961inline _LIBCPP_INLINE_VISIBILITY
1962typename enable_if
1963<
1964 !is_bind_expression<_Ti>::value &&
1965 is_placeholder<_Ti>::value == 0 &&
1966 !__is_reference_wrapper<_Ti>::value,
1967 _Ti&
1968>::type
1969__mu(_Ti& __ti, _Uj& __uj)
1970{
1971 return __ti;
1972}
1973
1974template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1975struct ____mu_return;
1976
1977template <class _Ti, class ..._Uj>
1978struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1979{
1980 typedef typename result_of<_Ti(_Uj...)>::type type;
1981};
1982
1983template <class _Ti, class _TupleUj>
1984struct ____mu_return<_Ti, false, true, _TupleUj>
1985{
1986 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1987 _TupleUj>::type&& type;
1988};
1989
1990template <class _Ti, class _TupleUj>
1991struct ____mu_return<_Ti, false, false, _TupleUj>
1992{
1993 typedef _Ti& type;
1994};
1995
1996template <class _Ti, class _TupleUj>
1997struct __mu_return
1998 : public ____mu_return<_Ti,
1999 is_bind_expression<_Ti>::value,
2000 0 < is_placeholder<_Ti>::value,
2001 _TupleUj>
2002{
2003};
2004
2005template <class _Ti, class _TupleUj>
2006struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
2007{
2008 typedef _Ti& type;
2009};
2010
Howard Hinnant99968442011-11-29 18:15:50 +00002011template <class _Fp, class _BoundArgs, class _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002012struct __bind_return;
2013
Howard Hinnant99968442011-11-29 18:15:50 +00002014template <class _Fp, class ..._BoundArgs, class _TupleUj>
2015struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016{
2017 typedef typename __ref_return
2018 <
Howard Hinnant99968442011-11-29 18:15:50 +00002019 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020 typename __mu_return
2021 <
2022 _BoundArgs,
2023 _TupleUj
2024 >::type...
2025 >::type type;
2026};
2027
Howard Hinnant99968442011-11-29 18:15:50 +00002028template <class _Fp, class ..._BoundArgs, class _TupleUj>
2029struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030{
2031 typedef typename __ref_return
2032 <
Howard Hinnant99968442011-11-29 18:15:50 +00002033 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 typename __mu_return
2035 <
2036 const _BoundArgs,
2037 _TupleUj
2038 >::type...
2039 >::type type;
2040};
2041
Howard Hinnant99968442011-11-29 18:15:50 +00002042template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002044typename __bind_return<_Fp, _BoundArgs, _Args>::type
2045__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046 _Args&& __args)
2047{
Marshall Clowba6dbf42014-06-24 00:46:19 +00002048 return __invoke(__f, __mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002049}
2050
Howard Hinnant99968442011-11-29 18:15:50 +00002051template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052class __bind
2053{
Howard Hinnant99968442011-11-29 18:15:50 +00002054 _Fp __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055 tuple<_BoundArgs...> __bound_args_;
2056
2057 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2058public:
Howard Hinnant99968442011-11-29 18:15:50 +00002059 template <class _Gp, class ..._BA>
2060 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2061 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002062 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063
2064 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002065 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002066 operator()(_Args&& ...__args)
2067 {
2068 // compiler bug workaround
Howard Hinnant324bb032010-08-22 00:02:43 +00002069 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 tuple<_Args&&...>(__args...));
2071 }
2072
2073 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002074 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075 operator()(_Args&& ...__args) const
2076 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002077 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078 tuple<_Args&&...>(__args...));
2079 }
2080};
2081
Howard Hinnant99968442011-11-29 18:15:50 +00002082template<class _Fp, class ..._BoundArgs>
2083struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084
Howard Hinnant99968442011-11-29 18:15:50 +00002085template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086class __bind_r
Howard Hinnant99968442011-11-29 18:15:50 +00002087 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088{
Howard Hinnant99968442011-11-29 18:15:50 +00002089 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090public:
Howard Hinnant99968442011-11-29 18:15:50 +00002091 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092
Howard Hinnant99968442011-11-29 18:15:50 +00002093 template <class _Gp, class ..._BA>
2094 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2095 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002096 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097
2098 template <class ..._Args>
2099 result_type
2100 operator()(_Args&& ...__args)
2101 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002102 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002103 }
2104
2105 template <class ..._Args>
2106 result_type
2107 operator()(_Args&& ...__args) const
2108 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002109 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110 }
2111};
2112
Howard Hinnant99968442011-11-29 18:15:50 +00002113template<class _Rp, class _Fp, class ..._BoundArgs>
2114struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115
Howard Hinnant99968442011-11-29 18:15:50 +00002116template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002118__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2119bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120{
Howard Hinnant99968442011-11-29 18:15:50 +00002121 typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2122 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123}
2124
Howard Hinnant99968442011-11-29 18:15:50 +00002125template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002127__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2128bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129{
Howard Hinnant99968442011-11-29 18:15:50 +00002130 typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2131 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132}
2133*/
2134
Howard Hinnant324bb032010-08-22 00:02:43 +00002135#endif // _LIBCPP_FUNCTIONAL_03