blob: 2582601738e425040c33e7c79de832f2cf3fdce3 [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>
654 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000655 template <class _R2>
Howard Hinnant99968442011-11-29 18:15:50 +0000656 static bool __not_null(const function<_Rp()>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657public:
Howard Hinnant99968442011-11-29 18:15:50 +0000658 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659
660 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000661 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
662 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000664 template<class _Fp>
665 function(_Fp,
666 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667
Howard Hinnant72552802010-08-20 19:36:46 +0000668 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000670 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
671 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000673 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
674 template<class _Alloc>
675 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000676 template<class _Fp, class _Alloc>
677 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
678 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679
680 function& operator=(const function&);
681 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000682 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683 typename enable_if
684 <
Howard Hinnant99968442011-11-29 18:15:50 +0000685 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000686 function&
687 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000688 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689
690 ~function();
691
692 // 20.7.16.2.2, function modifiers:
693 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000694 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000696 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +0000697 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698
699 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +0000700 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701
702private:
703 // deleted overloads close possible hole in the type system
704 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000705 bool operator==(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000707 bool operator!=(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000708public:
709 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +0000710 _Rp operator()() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711
Howard Hinnantd4444702010-08-11 17:04:31 +0000712#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 // 20.7.16.2.5, function target access:
714 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +0000715 template <typename _Tp> _Tp* target();
716 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000717#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718};
719
Howard Hinnant99968442011-11-29 18:15:50 +0000720template<class _Rp>
721function<_Rp()>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722{
723 if (__f.__f_ == 0)
724 __f_ = 0;
725 else if (__f.__f_ == (const __base*)&__f.__buf_)
726 {
727 __f_ = (__base*)&__buf_;
728 __f.__f_->__clone(__f_);
729 }
730 else
731 __f_ = __f.__f_->__clone();
732}
733
Howard Hinnant99968442011-11-29 18:15:50 +0000734template<class _Rp>
Howard Hinnant72552802010-08-20 19:36:46 +0000735template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +0000736function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +0000737{
738 if (__f.__f_ == 0)
739 __f_ = 0;
740 else if (__f.__f_ == (const __base*)&__f.__buf_)
741 {
742 __f_ = (__base*)&__buf_;
743 __f.__f_->__clone(__f_);
744 }
745 else
746 __f_ = __f.__f_->__clone();
747}
748
Howard Hinnant99968442011-11-29 18:15:50 +0000749template<class _Rp>
750template <class _Fp>
751function<_Rp()>::function(_Fp __f,
752 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753 : __f_(0)
754{
755 if (__not_null(__f))
756 {
Howard Hinnant99968442011-11-29 18:15:50 +0000757 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758 if (sizeof(_FF) <= sizeof(__buf_))
759 {
760 __f_ = (__base*)&__buf_;
761 ::new (__f_) _FF(__f);
762 }
763 else
764 {
Howard Hinnant99968442011-11-29 18:15:50 +0000765 typedef allocator<_FF> _Ap;
766 _Ap __a;
767 typedef __allocator_destructor<_Ap> _Dp;
768 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
769 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000770 __f_ = __hold.release();
771 }
772 }
773}
774
Howard Hinnant99968442011-11-29 18:15:50 +0000775template<class _Rp>
776template <class _Fp, class _Alloc>
777function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
778 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +0000779 : __f_(0)
780{
781 typedef allocator_traits<_Alloc> __alloc_traits;
782 if (__not_null(__f))
783 {
Howard Hinnant99968442011-11-29 18:15:50 +0000784 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +0000785 if (sizeof(_FF) <= sizeof(__buf_))
786 {
787 __f_ = (__base*)&__buf_;
788 ::new (__f_) _FF(__f);
789 }
790 else
791 {
792 typedef typename __alloc_traits::template
793#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
794 rebind_alloc<_FF>
795#else
796 rebind_alloc<_FF>::other
797#endif
Howard Hinnant99968442011-11-29 18:15:50 +0000798 _Ap;
799 _Ap __a(__a0);
800 typedef __allocator_destructor<_Ap> _Dp;
801 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +0000802 ::new (__hold.get()) _FF(__f, _Alloc(__a));
803 __f_ = __hold.release();
804 }
805 }
806}
807
Howard Hinnant99968442011-11-29 18:15:50 +0000808template<class _Rp>
809function<_Rp()>&
810function<_Rp()>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000811{
812 function(__f).swap(*this);
813 return *this;
814}
815
Howard Hinnant99968442011-11-29 18:15:50 +0000816template<class _Rp>
817function<_Rp()>&
818function<_Rp()>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819{
820 if (__f_ == (__base*)&__buf_)
821 __f_->destroy();
822 else if (__f_)
823 __f_->destroy_deallocate();
824 __f_ = 0;
825}
826
Howard Hinnant99968442011-11-29 18:15:50 +0000827template<class _Rp>
828template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000829typename enable_if
830<
Howard Hinnant99968442011-11-29 18:15:50 +0000831 !is_integral<_Fp>::value,
832 function<_Rp()>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833>::type
Howard Hinnant99968442011-11-29 18:15:50 +0000834function<_Rp()>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000835{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000836 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837 return *this;
838}
839
Howard Hinnant99968442011-11-29 18:15:50 +0000840template<class _Rp>
841function<_Rp()>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842{
843 if (__f_ == (__base*)&__buf_)
844 __f_->destroy();
845 else if (__f_)
846 __f_->destroy_deallocate();
847}
848
Howard Hinnant99968442011-11-29 18:15:50 +0000849template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000850void
Howard Hinnant99968442011-11-29 18:15:50 +0000851function<_Rp()>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000852{
853 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
854 {
855 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
856 __base* __t = (__base*)&__tempbuf;
857 __f_->__clone(__t);
858 __f_->destroy();
859 __f_ = 0;
860 __f.__f_->__clone((__base*)&__buf_);
861 __f.__f_->destroy();
862 __f.__f_ = 0;
863 __f_ = (__base*)&__buf_;
864 __t->__clone((__base*)&__f.__buf_);
865 __t->destroy();
866 __f.__f_ = (__base*)&__f.__buf_;
867 }
868 else if (__f_ == (__base*)&__buf_)
869 {
870 __f_->__clone((__base*)&__f.__buf_);
871 __f_->destroy();
872 __f_ = __f.__f_;
873 __f.__f_ = (__base*)&__f.__buf_;
874 }
875 else if (__f.__f_ == (__base*)&__f.__buf_)
876 {
877 __f.__f_->__clone((__base*)&__buf_);
878 __f.__f_->destroy();
879 __f.__f_ = __f_;
880 __f_ = (__base*)&__buf_;
881 }
882 else
Howard Hinnant0949eed2011-06-30 21:18:19 +0000883 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884}
885
Howard Hinnant99968442011-11-29 18:15:50 +0000886template<class _Rp>
887_Rp
888function<_Rp()>::operator()() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889{
Howard Hinnantd4444702010-08-11 17:04:31 +0000890#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891 if (__f_ == 0)
892 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +0000893#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894 return (*__f_)();
895}
896
Howard Hinnantd4444702010-08-11 17:04:31 +0000897#ifndef _LIBCPP_NO_RTTI
898
Howard Hinnant99968442011-11-29 18:15:50 +0000899template<class _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +0000901function<_Rp()>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902{
903 if (__f_ == 0)
904 return typeid(void);
905 return __f_->target_type();
906}
907
Howard Hinnant99968442011-11-29 18:15:50 +0000908template<class _Rp>
909template <typename _Tp>
910_Tp*
911function<_Rp()>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912{
913 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000914 return (_Tp*)0;
915 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916}
917
Howard Hinnant99968442011-11-29 18:15:50 +0000918template<class _Rp>
919template <typename _Tp>
920const _Tp*
921function<_Rp()>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922{
923 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +0000924 return (const _Tp*)0;
925 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926}
927
Howard Hinnant324bb032010-08-22 00:02:43 +0000928#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000929
Howard Hinnant99968442011-11-29 18:15:50 +0000930template<class _Rp, class _A0>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000931class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
Howard Hinnant99968442011-11-29 18:15:50 +0000932 : public unary_function<_A0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000933{
Howard Hinnant99968442011-11-29 18:15:50 +0000934 typedef __function::__base<_Rp(_A0)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935 aligned_storage<3*sizeof(void*)>::type __buf_;
936 __base* __f_;
937
Howard Hinnant99968442011-11-29 18:15:50 +0000938 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000940 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +0000944 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000946 static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
947 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000949 static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
950 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000952 static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
953 template <class _R2, class _Cp>
Howard Hinnant99acc502010-09-21 17:32:39 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000955 static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000958 static bool __not_null(const function<_Rp(_B0)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959public:
Howard Hinnant99968442011-11-29 18:15:50 +0000960 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961
962 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000963 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
964 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000966 template<class _Fp>
967 function(_Fp,
968 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000969
Howard Hinnant72552802010-08-20 19:36:46 +0000970 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000972 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
973 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000975 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
976 template<class _Alloc>
977 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000978 template<class _Fp, class _Alloc>
979 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
980 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981
982 function& operator=(const function&);
983 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +0000984 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 typename enable_if
986 <
Howard Hinnant99968442011-11-29 18:15:50 +0000987 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 function&
989 >::type
Howard Hinnant99968442011-11-29 18:15:50 +0000990 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991
992 ~function();
993
994 // 20.7.16.2.2, function modifiers:
995 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +0000996 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000998 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +0000999 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000
1001 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001002 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003
1004private:
1005 // deleted overloads close possible hole in the type system
1006 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001007 bool operator==(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001009 bool operator!=(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010public:
1011 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001012 _Rp operator()(_A0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013
Howard Hinnantd4444702010-08-11 17:04:31 +00001014#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015 // 20.7.16.2.5, function target access:
1016 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001017 template <typename _Tp> _Tp* target();
1018 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001019#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020};
1021
Howard Hinnant99968442011-11-29 18:15:50 +00001022template<class _Rp, class _A0>
1023function<_Rp(_A0)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024{
1025 if (__f.__f_ == 0)
1026 __f_ = 0;
1027 else if (__f.__f_ == (const __base*)&__f.__buf_)
1028 {
1029 __f_ = (__base*)&__buf_;
1030 __f.__f_->__clone(__f_);
1031 }
1032 else
1033 __f_ = __f.__f_->__clone();
1034}
1035
Howard Hinnant99968442011-11-29 18:15:50 +00001036template<class _Rp, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001037template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001038function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001039{
1040 if (__f.__f_ == 0)
1041 __f_ = 0;
1042 else if (__f.__f_ == (const __base*)&__f.__buf_)
1043 {
1044 __f_ = (__base*)&__buf_;
1045 __f.__f_->__clone(__f_);
1046 }
1047 else
1048 __f_ = __f.__f_->__clone();
1049}
1050
Howard Hinnant99968442011-11-29 18:15:50 +00001051template<class _Rp, class _A0>
1052template <class _Fp>
1053function<_Rp(_A0)>::function(_Fp __f,
1054 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055 : __f_(0)
1056{
1057 if (__not_null(__f))
1058 {
Howard Hinnant99968442011-11-29 18:15:50 +00001059 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060 if (sizeof(_FF) <= sizeof(__buf_))
1061 {
1062 __f_ = (__base*)&__buf_;
1063 ::new (__f_) _FF(__f);
1064 }
1065 else
1066 {
Howard Hinnant99968442011-11-29 18:15:50 +00001067 typedef allocator<_FF> _Ap;
1068 _Ap __a;
1069 typedef __allocator_destructor<_Ap> _Dp;
1070 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1071 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072 __f_ = __hold.release();
1073 }
1074 }
1075}
1076
Howard Hinnant99968442011-11-29 18:15:50 +00001077template<class _Rp, class _A0>
1078template <class _Fp, class _Alloc>
1079function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1080 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001081 : __f_(0)
1082{
1083 typedef allocator_traits<_Alloc> __alloc_traits;
1084 if (__not_null(__f))
1085 {
Howard Hinnant99968442011-11-29 18:15:50 +00001086 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001087 if (sizeof(_FF) <= sizeof(__buf_))
1088 {
1089 __f_ = (__base*)&__buf_;
1090 ::new (__f_) _FF(__f);
1091 }
1092 else
1093 {
1094 typedef typename __alloc_traits::template
1095#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1096 rebind_alloc<_FF>
1097#else
1098 rebind_alloc<_FF>::other
1099#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001100 _Ap;
1101 _Ap __a(__a0);
1102 typedef __allocator_destructor<_Ap> _Dp;
1103 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001104 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1105 __f_ = __hold.release();
1106 }
1107 }
1108}
1109
Howard Hinnant99968442011-11-29 18:15:50 +00001110template<class _Rp, class _A0>
1111function<_Rp(_A0)>&
1112function<_Rp(_A0)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113{
1114 function(__f).swap(*this);
1115 return *this;
1116}
1117
Howard Hinnant99968442011-11-29 18:15:50 +00001118template<class _Rp, class _A0>
1119function<_Rp(_A0)>&
1120function<_Rp(_A0)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121{
1122 if (__f_ == (__base*)&__buf_)
1123 __f_->destroy();
1124 else if (__f_)
1125 __f_->destroy_deallocate();
1126 __f_ = 0;
1127}
1128
Howard Hinnant99968442011-11-29 18:15:50 +00001129template<class _Rp, class _A0>
1130template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001131typename enable_if
1132<
Howard Hinnant99968442011-11-29 18:15:50 +00001133 !is_integral<_Fp>::value,
1134 function<_Rp(_A0)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001135>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001136function<_Rp(_A0)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001137{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001138 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139 return *this;
1140}
1141
Howard Hinnant99968442011-11-29 18:15:50 +00001142template<class _Rp, class _A0>
1143function<_Rp(_A0)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144{
1145 if (__f_ == (__base*)&__buf_)
1146 __f_->destroy();
1147 else if (__f_)
1148 __f_->destroy_deallocate();
1149}
1150
Howard Hinnant99968442011-11-29 18:15:50 +00001151template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152void
Howard Hinnant99968442011-11-29 18:15:50 +00001153function<_Rp(_A0)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001154{
1155 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1156 {
1157 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1158 __base* __t = (__base*)&__tempbuf;
1159 __f_->__clone(__t);
1160 __f_->destroy();
1161 __f_ = 0;
1162 __f.__f_->__clone((__base*)&__buf_);
1163 __f.__f_->destroy();
1164 __f.__f_ = 0;
1165 __f_ = (__base*)&__buf_;
1166 __t->__clone((__base*)&__f.__buf_);
1167 __t->destroy();
1168 __f.__f_ = (__base*)&__f.__buf_;
1169 }
1170 else if (__f_ == (__base*)&__buf_)
1171 {
1172 __f_->__clone((__base*)&__f.__buf_);
1173 __f_->destroy();
1174 __f_ = __f.__f_;
1175 __f.__f_ = (__base*)&__f.__buf_;
1176 }
1177 else if (__f.__f_ == (__base*)&__f.__buf_)
1178 {
1179 __f.__f_->__clone((__base*)&__buf_);
1180 __f.__f_->destroy();
1181 __f.__f_ = __f_;
1182 __f_ = (__base*)&__buf_;
1183 }
1184 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001185 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001186}
1187
Howard Hinnant99968442011-11-29 18:15:50 +00001188template<class _Rp, class _A0>
1189_Rp
1190function<_Rp(_A0)>::operator()(_A0 __a0) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191{
Howard Hinnantd4444702010-08-11 17:04:31 +00001192#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 if (__f_ == 0)
1194 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001195#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196 return (*__f_)(__a0);
1197}
1198
Howard Hinnantd4444702010-08-11 17:04:31 +00001199#ifndef _LIBCPP_NO_RTTI
1200
Howard Hinnant99968442011-11-29 18:15:50 +00001201template<class _Rp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001203function<_Rp(_A0)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204{
1205 if (__f_ == 0)
1206 return typeid(void);
1207 return __f_->target_type();
1208}
1209
Howard Hinnant99968442011-11-29 18:15:50 +00001210template<class _Rp, class _A0>
1211template <typename _Tp>
1212_Tp*
1213function<_Rp(_A0)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214{
1215 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001216 return (_Tp*)0;
1217 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001218}
1219
Howard Hinnant99968442011-11-29 18:15:50 +00001220template<class _Rp, class _A0>
1221template <typename _Tp>
1222const _Tp*
1223function<_Rp(_A0)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224{
1225 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001226 return (const _Tp*)0;
1227 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228}
1229
Howard Hinnant324bb032010-08-22 00:02:43 +00001230#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001231
Howard Hinnant99968442011-11-29 18:15:50 +00001232template<class _Rp, class _A0, class _A1>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001233class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
Howard Hinnant99968442011-11-29 18:15:50 +00001234 : public binary_function<_A0, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235{
Howard Hinnant99968442011-11-29 18:15:50 +00001236 typedef __function::__base<_Rp(_A0, _A1)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237 aligned_storage<3*sizeof(void*)>::type __buf_;
1238 __base* __f_;
1239
Howard Hinnant99968442011-11-29 18:15:50 +00001240 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001242 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001243 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001246 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001248 static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1249 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001251 static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1252 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001254 static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1255 template <class _R2, class _Cp, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001257 static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001260 static bool __not_null(const function<_Rp(_B0, _B1)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261public:
Howard Hinnant99968442011-11-29 18:15:50 +00001262 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263
1264 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001265 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1266 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001268 template<class _Fp>
1269 function(_Fp,
1270 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271
Howard Hinnant72552802010-08-20 19:36:46 +00001272 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001274 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1275 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001277 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1278 template<class _Alloc>
1279 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001280 template<class _Fp, class _Alloc>
1281 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1282 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283
1284 function& operator=(const function&);
1285 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001286 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287 typename enable_if
1288 <
Howard Hinnant99968442011-11-29 18:15:50 +00001289 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290 function&
1291 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001292 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001293
1294 ~function();
1295
1296 // 20.7.16.2.2, function modifiers:
1297 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001298 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001300 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001301 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302
1303 // 20.7.16.2.3, function capacity:
1304 operator bool() const {return __f_;}
1305
1306private:
1307 // deleted overloads close possible hole in the type system
1308 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001309 bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001311 bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312public:
1313 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001314 _Rp operator()(_A0, _A1) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315
Howard Hinnantd4444702010-08-11 17:04:31 +00001316#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 // 20.7.16.2.5, function target access:
1318 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001319 template <typename _Tp> _Tp* target();
1320 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001321#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322};
1323
Howard Hinnant99968442011-11-29 18:15:50 +00001324template<class _Rp, class _A0, class _A1>
1325function<_Rp(_A0, _A1)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326{
1327 if (__f.__f_ == 0)
1328 __f_ = 0;
1329 else if (__f.__f_ == (const __base*)&__f.__buf_)
1330 {
1331 __f_ = (__base*)&__buf_;
1332 __f.__f_->__clone(__f_);
1333 }
1334 else
1335 __f_ = __f.__f_->__clone();
1336}
1337
Howard Hinnant99968442011-11-29 18:15:50 +00001338template<class _Rp, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001339template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001340function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
Howard Hinnant72552802010-08-20 19:36:46 +00001341{
1342 if (__f.__f_ == 0)
1343 __f_ = 0;
1344 else if (__f.__f_ == (const __base*)&__f.__buf_)
1345 {
1346 __f_ = (__base*)&__buf_;
1347 __f.__f_->__clone(__f_);
1348 }
1349 else
1350 __f_ = __f.__f_->__clone();
1351}
1352
Howard Hinnant99968442011-11-29 18:15:50 +00001353template<class _Rp, class _A0, class _A1>
1354template <class _Fp>
1355function<_Rp(_A0, _A1)>::function(_Fp __f,
1356 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 : __f_(0)
1358{
1359 if (__not_null(__f))
1360 {
Howard Hinnant99968442011-11-29 18:15:50 +00001361 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362 if (sizeof(_FF) <= sizeof(__buf_))
1363 {
1364 __f_ = (__base*)&__buf_;
1365 ::new (__f_) _FF(__f);
1366 }
1367 else
1368 {
Howard Hinnant99968442011-11-29 18:15:50 +00001369 typedef allocator<_FF> _Ap;
1370 _Ap __a;
1371 typedef __allocator_destructor<_Ap> _Dp;
1372 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1373 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001374 __f_ = __hold.release();
1375 }
1376 }
1377}
1378
Howard Hinnant99968442011-11-29 18:15:50 +00001379template<class _Rp, class _A0, class _A1>
1380template <class _Fp, class _Alloc>
1381function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1382 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001383 : __f_(0)
1384{
1385 typedef allocator_traits<_Alloc> __alloc_traits;
1386 if (__not_null(__f))
1387 {
Howard Hinnant99968442011-11-29 18:15:50 +00001388 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001389 if (sizeof(_FF) <= sizeof(__buf_))
1390 {
1391 __f_ = (__base*)&__buf_;
1392 ::new (__f_) _FF(__f);
1393 }
1394 else
1395 {
1396 typedef typename __alloc_traits::template
1397#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1398 rebind_alloc<_FF>
1399#else
1400 rebind_alloc<_FF>::other
1401#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001402 _Ap;
1403 _Ap __a(__a0);
1404 typedef __allocator_destructor<_Ap> _Dp;
1405 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001406 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1407 __f_ = __hold.release();
1408 }
1409 }
1410}
1411
Howard Hinnant99968442011-11-29 18:15:50 +00001412template<class _Rp, class _A0, class _A1>
1413function<_Rp(_A0, _A1)>&
1414function<_Rp(_A0, _A1)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001415{
1416 function(__f).swap(*this);
1417 return *this;
1418}
1419
Howard Hinnant99968442011-11-29 18:15:50 +00001420template<class _Rp, class _A0, class _A1>
1421function<_Rp(_A0, _A1)>&
1422function<_Rp(_A0, _A1)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423{
1424 if (__f_ == (__base*)&__buf_)
1425 __f_->destroy();
1426 else if (__f_)
1427 __f_->destroy_deallocate();
1428 __f_ = 0;
1429}
1430
Howard Hinnant99968442011-11-29 18:15:50 +00001431template<class _Rp, class _A0, class _A1>
1432template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433typename enable_if
1434<
Howard Hinnant99968442011-11-29 18:15:50 +00001435 !is_integral<_Fp>::value,
1436 function<_Rp(_A0, _A1)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001437>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001438function<_Rp(_A0, _A1)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001440 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 return *this;
1442}
1443
Howard Hinnant99968442011-11-29 18:15:50 +00001444template<class _Rp, class _A0, class _A1>
1445function<_Rp(_A0, _A1)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446{
1447 if (__f_ == (__base*)&__buf_)
1448 __f_->destroy();
1449 else if (__f_)
1450 __f_->destroy_deallocate();
1451}
1452
Howard Hinnant99968442011-11-29 18:15:50 +00001453template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454void
Howard Hinnant99968442011-11-29 18:15:50 +00001455function<_Rp(_A0, _A1)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001456{
1457 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1458 {
1459 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1460 __base* __t = (__base*)&__tempbuf;
1461 __f_->__clone(__t);
1462 __f_->destroy();
1463 __f_ = 0;
1464 __f.__f_->__clone((__base*)&__buf_);
1465 __f.__f_->destroy();
1466 __f.__f_ = 0;
1467 __f_ = (__base*)&__buf_;
1468 __t->__clone((__base*)&__f.__buf_);
1469 __t->destroy();
1470 __f.__f_ = (__base*)&__f.__buf_;
1471 }
1472 else if (__f_ == (__base*)&__buf_)
1473 {
1474 __f_->__clone((__base*)&__f.__buf_);
1475 __f_->destroy();
1476 __f_ = __f.__f_;
1477 __f.__f_ = (__base*)&__f.__buf_;
1478 }
1479 else if (__f.__f_ == (__base*)&__f.__buf_)
1480 {
1481 __f.__f_->__clone((__base*)&__buf_);
1482 __f.__f_->destroy();
1483 __f.__f_ = __f_;
1484 __f_ = (__base*)&__buf_;
1485 }
1486 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001487 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488}
1489
Howard Hinnant99968442011-11-29 18:15:50 +00001490template<class _Rp, class _A0, class _A1>
1491_Rp
1492function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493{
Howard Hinnantd4444702010-08-11 17:04:31 +00001494#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495 if (__f_ == 0)
1496 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001497#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 return (*__f_)(__a0, __a1);
1499}
1500
Howard Hinnantd4444702010-08-11 17:04:31 +00001501#ifndef _LIBCPP_NO_RTTI
1502
Howard Hinnant99968442011-11-29 18:15:50 +00001503template<class _Rp, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001505function<_Rp(_A0, _A1)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506{
1507 if (__f_ == 0)
1508 return typeid(void);
1509 return __f_->target_type();
1510}
1511
Howard Hinnant99968442011-11-29 18:15:50 +00001512template<class _Rp, class _A0, class _A1>
1513template <typename _Tp>
1514_Tp*
1515function<_Rp(_A0, _A1)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516{
1517 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001518 return (_Tp*)0;
1519 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520}
1521
Howard Hinnant99968442011-11-29 18:15:50 +00001522template<class _Rp, class _A0, class _A1>
1523template <typename _Tp>
1524const _Tp*
1525function<_Rp(_A0, _A1)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526{
1527 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001528 return (const _Tp*)0;
1529 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530}
1531
Howard Hinnant324bb032010-08-22 00:02:43 +00001532#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001533
Howard Hinnant99968442011-11-29 18:15:50 +00001534template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001535class _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536{
Howard Hinnant99968442011-11-29 18:15:50 +00001537 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538 aligned_storage<3*sizeof(void*)>::type __buf_;
1539 __base* __f_;
1540
Howard Hinnant99968442011-11-29 18:15:50 +00001541 template <class _Fp>
Howard Hinnant99acc502010-09-21 17:32:39 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001543 static bool __not_null(const _Fp&) {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
Howard Hinnant99968442011-11-29 18:15:50 +00001547 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001549 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1550 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001552 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1553 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001555 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1556 template <class _R2, class _Cp, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001558 static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001561 static bool __not_null(const function<_Rp(_B0, _B1, _B2)>& __p) {return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562public:
Howard Hinnant99968442011-11-29 18:15:50 +00001563 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564
1565 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001566 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1567 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568 function(const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001569 template<class _Fp>
1570 function(_Fp,
1571 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572
Howard Hinnant72552802010-08-20 19:36:46 +00001573 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001575 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1576 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001578 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1579 template<class _Alloc>
1580 function(allocator_arg_t, const _Alloc&, const function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001581 template<class _Fp, class _Alloc>
1582 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1583 typename enable_if<!is_integral<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584
1585 function& operator=(const function&);
1586 function& operator=(nullptr_t);
Howard Hinnant99968442011-11-29 18:15:50 +00001587 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588 typename enable_if
1589 <
Howard Hinnant99968442011-11-29 18:15:50 +00001590 !is_integral<_Fp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591 function&
1592 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001593 operator=(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594
1595 ~function();
1596
1597 // 20.7.16.2.2, function modifiers:
1598 void swap(function&);
Howard Hinnant99968442011-11-29 18:15:50 +00001599 template<class _Fp, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001601 void assign(_Fp __f, const _Alloc& __a)
Howard Hinnant72552802010-08-20 19:36:46 +00001602 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603
1604 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001605 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606
1607private:
1608 // deleted overloads close possible hole in the type system
1609 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001610 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001612 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613public:
1614 // 20.7.16.2.4, function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001615 _Rp operator()(_A0, _A1, _A2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616
Howard Hinnantd4444702010-08-11 17:04:31 +00001617#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 // 20.7.16.2.5, function target access:
1619 const std::type_info& target_type() const;
Howard Hinnant99968442011-11-29 18:15:50 +00001620 template <typename _Tp> _Tp* target();
1621 template <typename _Tp> const _Tp* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001622#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623};
1624
Howard Hinnant99968442011-11-29 18:15:50 +00001625template<class _Rp, class _A0, class _A1, class _A2>
1626function<_Rp(_A0, _A1, _A2)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627{
1628 if (__f.__f_ == 0)
1629 __f_ = 0;
1630 else if (__f.__f_ == (const __base*)&__f.__buf_)
1631 {
1632 __f_ = (__base*)&__buf_;
1633 __f.__f_->__clone(__f_);
1634 }
1635 else
1636 __f_ = __f.__f_->__clone();
1637}
1638
Howard Hinnant99968442011-11-29 18:15:50 +00001639template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001640template<class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001641function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnant72552802010-08-20 19:36:46 +00001642 const function& __f)
1643{
1644 if (__f.__f_ == 0)
1645 __f_ = 0;
1646 else if (__f.__f_ == (const __base*)&__f.__buf_)
1647 {
1648 __f_ = (__base*)&__buf_;
1649 __f.__f_->__clone(__f_);
1650 }
1651 else
1652 __f_ = __f.__f_->__clone();
1653}
1654
Howard Hinnant99968442011-11-29 18:15:50 +00001655template<class _Rp, class _A0, class _A1, class _A2>
1656template <class _Fp>
1657function<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1658 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 : __f_(0)
1660{
1661 if (__not_null(__f))
1662 {
Howard Hinnant99968442011-11-29 18:15:50 +00001663 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664 if (sizeof(_FF) <= sizeof(__buf_))
1665 {
1666 __f_ = (__base*)&__buf_;
1667 ::new (__f_) _FF(__f);
1668 }
1669 else
1670 {
Howard Hinnant99968442011-11-29 18:15:50 +00001671 typedef allocator<_FF> _Ap;
1672 _Ap __a;
1673 typedef __allocator_destructor<_Ap> _Dp;
1674 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1675 ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 __f_ = __hold.release();
1677 }
1678 }
1679}
1680
Howard Hinnant99968442011-11-29 18:15:50 +00001681template<class _Rp, class _A0, class _A1, class _A2>
1682template <class _Fp, class _Alloc>
1683function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1684 typename enable_if<!is_integral<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001685 : __f_(0)
1686{
1687 typedef allocator_traits<_Alloc> __alloc_traits;
1688 if (__not_null(__f))
1689 {
Howard Hinnant99968442011-11-29 18:15:50 +00001690 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
Howard Hinnant72552802010-08-20 19:36:46 +00001691 if (sizeof(_FF) <= sizeof(__buf_))
1692 {
1693 __f_ = (__base*)&__buf_;
1694 ::new (__f_) _FF(__f);
1695 }
1696 else
1697 {
1698 typedef typename __alloc_traits::template
1699#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1700 rebind_alloc<_FF>
1701#else
1702 rebind_alloc<_FF>::other
1703#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001704 _Ap;
1705 _Ap __a(__a0);
1706 typedef __allocator_destructor<_Ap> _Dp;
1707 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant72552802010-08-20 19:36:46 +00001708 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1709 __f_ = __hold.release();
1710 }
1711 }
1712}
1713
Howard Hinnant99968442011-11-29 18:15:50 +00001714template<class _Rp, class _A0, class _A1, class _A2>
1715function<_Rp(_A0, _A1, _A2)>&
1716function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001717{
1718 function(__f).swap(*this);
1719 return *this;
1720}
1721
Howard Hinnant99968442011-11-29 18:15:50 +00001722template<class _Rp, class _A0, class _A1, class _A2>
1723function<_Rp(_A0, _A1, _A2)>&
1724function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725{
1726 if (__f_ == (__base*)&__buf_)
1727 __f_->destroy();
1728 else if (__f_)
1729 __f_->destroy_deallocate();
1730 __f_ = 0;
1731}
1732
Howard Hinnant99968442011-11-29 18:15:50 +00001733template<class _Rp, class _A0, class _A1, class _A2>
1734template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735typename enable_if
1736<
Howard Hinnant99968442011-11-29 18:15:50 +00001737 !is_integral<_Fp>::value,
1738 function<_Rp(_A0, _A1, _A2)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001740function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001742 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 return *this;
1744}
1745
Howard Hinnant99968442011-11-29 18:15:50 +00001746template<class _Rp, class _A0, class _A1, class _A2>
1747function<_Rp(_A0, _A1, _A2)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748{
1749 if (__f_ == (__base*)&__buf_)
1750 __f_->destroy();
1751 else if (__f_)
1752 __f_->destroy_deallocate();
1753}
1754
Howard Hinnant99968442011-11-29 18:15:50 +00001755template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001756void
Howard Hinnant99968442011-11-29 18:15:50 +00001757function<_Rp(_A0, _A1, _A2)>::swap(function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758{
1759 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1760 {
1761 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1762 __base* __t = (__base*)&__tempbuf;
1763 __f_->__clone(__t);
1764 __f_->destroy();
1765 __f_ = 0;
1766 __f.__f_->__clone((__base*)&__buf_);
1767 __f.__f_->destroy();
1768 __f.__f_ = 0;
1769 __f_ = (__base*)&__buf_;
1770 __t->__clone((__base*)&__f.__buf_);
1771 __t->destroy();
1772 __f.__f_ = (__base*)&__f.__buf_;
1773 }
1774 else if (__f_ == (__base*)&__buf_)
1775 {
1776 __f_->__clone((__base*)&__f.__buf_);
1777 __f_->destroy();
1778 __f_ = __f.__f_;
1779 __f.__f_ = (__base*)&__f.__buf_;
1780 }
1781 else if (__f.__f_ == (__base*)&__f.__buf_)
1782 {
1783 __f.__f_->__clone((__base*)&__buf_);
1784 __f.__f_->destroy();
1785 __f.__f_ = __f_;
1786 __f_ = (__base*)&__buf_;
1787 }
1788 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001789 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790}
1791
Howard Hinnant99968442011-11-29 18:15:50 +00001792template<class _Rp, class _A0, class _A1, class _A2>
1793_Rp
1794function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795{
Howard Hinnantd4444702010-08-11 17:04:31 +00001796#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 if (__f_ == 0)
1798 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001799#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800 return (*__f_)(__a0, __a1, __a2);
1801}
1802
Howard Hinnantd4444702010-08-11 17:04:31 +00001803#ifndef _LIBCPP_NO_RTTI
1804
Howard Hinnant99968442011-11-29 18:15:50 +00001805template<class _Rp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001806const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001807function<_Rp(_A0, _A1, _A2)>::target_type() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001808{
1809 if (__f_ == 0)
1810 return typeid(void);
1811 return __f_->target_type();
1812}
1813
Howard Hinnant99968442011-11-29 18:15:50 +00001814template<class _Rp, class _A0, class _A1, class _A2>
1815template <typename _Tp>
1816_Tp*
1817function<_Rp(_A0, _A1, _A2)>::target()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818{
1819 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001820 return (_Tp*)0;
1821 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822}
1823
Howard Hinnant99968442011-11-29 18:15:50 +00001824template<class _Rp, class _A0, class _A1, class _A2>
1825template <typename _Tp>
1826const _Tp*
1827function<_Rp(_A0, _A1, _A2)>::target() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001828{
1829 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001830 return (const _Tp*)0;
1831 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832}
1833
Howard Hinnant324bb032010-08-22 00:02:43 +00001834#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001835
Howard Hinnant99968442011-11-29 18:15:50 +00001836template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837inline _LIBCPP_INLINE_VISIBILITY
1838bool
Howard Hinnant99968442011-11-29 18:15:50 +00001839operator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840
Howard Hinnant99968442011-11-29 18:15:50 +00001841template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842inline _LIBCPP_INLINE_VISIBILITY
1843bool
Howard Hinnant99968442011-11-29 18:15:50 +00001844operator==(nullptr_t, const function<_Fp>& __f) {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!=(const function<_Fp>& __f, nullptr_t) {return (bool)__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!=(nullptr_t, const function<_Fp>& __f) {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
1858void
Howard Hinnant99968442011-11-29 18:15:50 +00001859swap(function<_Fp>& __x, function<_Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860{return __x.swap(__y);}
1861
1862template<class _Tp> struct __is_bind_expression : public false_type {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001863template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1865
1866template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001867template<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1869
1870namespace placeholders
1871{
1872
Howard Hinnant99968442011-11-29 18:15:50 +00001873template <int _Np> struct __ph {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874
1875extern __ph<1> _1;
1876extern __ph<2> _2;
1877extern __ph<3> _3;
1878extern __ph<4> _4;
1879extern __ph<5> _5;
1880extern __ph<6> _6;
1881extern __ph<7> _7;
1882extern __ph<8> _8;
1883extern __ph<9> _9;
1884extern __ph<10> _10;
1885
1886} // placeholders
1887
Howard Hinnant99968442011-11-29 18:15:50 +00001888template<int _Np>
1889struct __is_placeholder<placeholders::__ph<_Np> >
1890 : public integral_constant<int, _Np> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891
1892template <class _Tp, class _Uj>
1893inline _LIBCPP_INLINE_VISIBILITY
1894_Tp&
1895__mu(reference_wrapper<_Tp> __t, _Uj&)
1896{
1897 return __t.get();
1898}
1899/*
1900template <bool _IsBindExpr, class _Ti, class ..._Uj>
1901struct __mu_return1 {};
1902
1903template <class _Ti, class ..._Uj>
1904struct __mu_return1<true, _Ti, _Uj...>
1905{
1906 typedef typename result_of<_Ti(_Uj...)>::type type;
1907};
1908
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909template <class _Ti, class ..._Uj, size_t ..._Indx>
1910inline _LIBCPP_INLINE_VISIBILITY
1911typename __mu_return1<true, _Ti, _Uj...>::type
1912__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1913{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001914 __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915}
1916
1917template <class _Ti, class ..._Uj>
1918inline _LIBCPP_INLINE_VISIBILITY
1919typename enable_if
1920<
1921 is_bind_expression<_Ti>::value,
1922 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1923>::type
1924__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1925{
1926 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1927 return __mu_expand(__ti, __uj, __indices());
1928}
1929
1930template <bool IsPh, class _Ti, class _Uj>
1931struct __mu_return2 {};
1932
1933template <class _Ti, class _Uj>
1934struct __mu_return2<true, _Ti, _Uj>
1935{
1936 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1937};
1938
1939template <class _Ti, class _Uj>
1940inline _LIBCPP_INLINE_VISIBILITY
1941typename enable_if
1942<
1943 0 < is_placeholder<_Ti>::value,
1944 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1945>::type
1946__mu(_Ti&, _Uj& __uj)
1947{
1948 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1949 // compiler bug workaround
1950 typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1951 return __t;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001952// return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953}
1954
1955template <class _Ti, class _Uj>
1956inline _LIBCPP_INLINE_VISIBILITY
1957typename enable_if
1958<
1959 !is_bind_expression<_Ti>::value &&
1960 is_placeholder<_Ti>::value == 0 &&
1961 !__is_reference_wrapper<_Ti>::value,
1962 _Ti&
1963>::type
1964__mu(_Ti& __ti, _Uj& __uj)
1965{
1966 return __ti;
1967}
1968
1969template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1970struct ____mu_return;
1971
1972template <class _Ti, class ..._Uj>
1973struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1974{
1975 typedef typename result_of<_Ti(_Uj...)>::type type;
1976};
1977
1978template <class _Ti, class _TupleUj>
1979struct ____mu_return<_Ti, false, true, _TupleUj>
1980{
1981 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1982 _TupleUj>::type&& type;
1983};
1984
1985template <class _Ti, class _TupleUj>
1986struct ____mu_return<_Ti, false, false, _TupleUj>
1987{
1988 typedef _Ti& type;
1989};
1990
1991template <class _Ti, class _TupleUj>
1992struct __mu_return
1993 : public ____mu_return<_Ti,
1994 is_bind_expression<_Ti>::value,
1995 0 < is_placeholder<_Ti>::value,
1996 _TupleUj>
1997{
1998};
1999
2000template <class _Ti, class _TupleUj>
2001struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
2002{
2003 typedef _Ti& type;
2004};
2005
Howard Hinnant99968442011-11-29 18:15:50 +00002006template <class _Fp, class _BoundArgs, class _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007struct __bind_return;
2008
Howard Hinnant99968442011-11-29 18:15:50 +00002009template <class _Fp, class ..._BoundArgs, class _TupleUj>
2010struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011{
2012 typedef typename __ref_return
2013 <
Howard Hinnant99968442011-11-29 18:15:50 +00002014 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002015 typename __mu_return
2016 <
2017 _BoundArgs,
2018 _TupleUj
2019 >::type...
2020 >::type type;
2021};
2022
Howard Hinnant99968442011-11-29 18:15:50 +00002023template <class _Fp, class ..._BoundArgs, class _TupleUj>
2024struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002025{
2026 typedef typename __ref_return
2027 <
Howard Hinnant99968442011-11-29 18:15:50 +00002028 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029 typename __mu_return
2030 <
2031 const _BoundArgs,
2032 _TupleUj
2033 >::type...
2034 >::type type;
2035};
2036
Howard Hinnant99968442011-11-29 18:15:50 +00002037template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002039typename __bind_return<_Fp, _BoundArgs, _Args>::type
2040__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 _Args&& __args)
2042{
2043 return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
2044}
2045
Howard Hinnant99968442011-11-29 18:15:50 +00002046template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047class __bind
2048{
Howard Hinnant99968442011-11-29 18:15:50 +00002049 _Fp __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 tuple<_BoundArgs...> __bound_args_;
2051
2052 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2053public:
Howard Hinnant99968442011-11-29 18:15:50 +00002054 template <class _Gp, class ..._BA>
2055 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2056 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002057 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058
2059 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002060 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061 operator()(_Args&& ...__args)
2062 {
2063 // compiler bug workaround
Howard Hinnant324bb032010-08-22 00:02:43 +00002064 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065 tuple<_Args&&...>(__args...));
2066 }
2067
2068 template <class ..._Args>
Howard Hinnant99968442011-11-29 18:15:50 +00002069 typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 operator()(_Args&& ...__args) const
2071 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002072 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073 tuple<_Args&&...>(__args...));
2074 }
2075};
2076
Howard Hinnant99968442011-11-29 18:15:50 +00002077template<class _Fp, class ..._BoundArgs>
2078struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079
Howard Hinnant99968442011-11-29 18:15:50 +00002080template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081class __bind_r
Howard Hinnant99968442011-11-29 18:15:50 +00002082 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083{
Howard Hinnant99968442011-11-29 18:15:50 +00002084 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002085public:
Howard Hinnant99968442011-11-29 18:15:50 +00002086 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087
Howard Hinnant99968442011-11-29 18:15:50 +00002088 template <class _Gp, class ..._BA>
2089 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2090 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002091 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092
2093 template <class ..._Args>
2094 result_type
2095 operator()(_Args&& ...__args)
2096 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002097 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098 }
2099
2100 template <class ..._Args>
2101 result_type
2102 operator()(_Args&& ...__args) const
2103 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002104 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105 }
2106};
2107
Howard Hinnant99968442011-11-29 18:15:50 +00002108template<class _Rp, class _Fp, class ..._BoundArgs>
2109struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110
Howard Hinnant99968442011-11-29 18:15:50 +00002111template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002113__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2114bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115{
Howard Hinnant99968442011-11-29 18:15:50 +00002116 typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2117 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118}
2119
Howard Hinnant99968442011-11-29 18:15:50 +00002120template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002122__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2123bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124{
Howard Hinnant99968442011-11-29 18:15:50 +00002125 typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2126 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127}
2128*/
2129
Howard Hinnant324bb032010-08-22 00:02:43 +00002130#endif // _LIBCPP_FUNCTIONAL_03