xref: /netbsd-src/external/apache2/llvm/dist/libcxx/include/__functional_03 (revision 4d6fc14bc9b0c5bf3e30be318c143ee82cadd108)
1*4d6fc14bSjoerg// -*- C++ -*-
2*4d6fc14bSjoerg//===----------------------------------------------------------------------===//
3*4d6fc14bSjoerg//
4*4d6fc14bSjoerg// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*4d6fc14bSjoerg// See https://llvm.org/LICENSE.txt for license information.
6*4d6fc14bSjoerg// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*4d6fc14bSjoerg//
8*4d6fc14bSjoerg//===----------------------------------------------------------------------===//
9*4d6fc14bSjoerg
10*4d6fc14bSjoerg#ifndef _LIBCPP_FUNCTIONAL_03
11*4d6fc14bSjoerg#define _LIBCPP_FUNCTIONAL_03
12*4d6fc14bSjoerg
13*4d6fc14bSjoerg// manual variadic expansion for <functional>
14*4d6fc14bSjoerg
15*4d6fc14bSjoerg#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16*4d6fc14bSjoerg#pragma GCC system_header
17*4d6fc14bSjoerg#endif
18*4d6fc14bSjoerg
19*4d6fc14bSjoergnamespace __function {
20*4d6fc14bSjoerg
21*4d6fc14bSjoergtemplate<class _Fp> class __base;
22*4d6fc14bSjoerg
23*4d6fc14bSjoergtemplate<class _Rp>
24*4d6fc14bSjoergclass __base<_Rp()>
25*4d6fc14bSjoerg{
26*4d6fc14bSjoerg    __base(const __base&);
27*4d6fc14bSjoerg    __base& operator=(const __base&);
28*4d6fc14bSjoergpublic:
29*4d6fc14bSjoerg    __base() {}
30*4d6fc14bSjoerg    virtual ~__base() {}
31*4d6fc14bSjoerg    virtual __base* __clone() const = 0;
32*4d6fc14bSjoerg    virtual void __clone(__base*) const = 0;
33*4d6fc14bSjoerg    virtual void destroy() = 0;
34*4d6fc14bSjoerg    virtual void destroy_deallocate() = 0;
35*4d6fc14bSjoerg    virtual _Rp operator()() = 0;
36*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
37*4d6fc14bSjoerg    virtual const void* target(const type_info&) const = 0;
38*4d6fc14bSjoerg    virtual const std::type_info& target_type() const = 0;
39*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
40*4d6fc14bSjoerg};
41*4d6fc14bSjoerg
42*4d6fc14bSjoergtemplate<class _Rp, class _A0>
43*4d6fc14bSjoergclass __base<_Rp(_A0)>
44*4d6fc14bSjoerg{
45*4d6fc14bSjoerg    __base(const __base&);
46*4d6fc14bSjoerg    __base& operator=(const __base&);
47*4d6fc14bSjoergpublic:
48*4d6fc14bSjoerg    __base() {}
49*4d6fc14bSjoerg    virtual ~__base() {}
50*4d6fc14bSjoerg    virtual __base* __clone() const = 0;
51*4d6fc14bSjoerg    virtual void __clone(__base*) const = 0;
52*4d6fc14bSjoerg    virtual void destroy() = 0;
53*4d6fc14bSjoerg    virtual void destroy_deallocate() = 0;
54*4d6fc14bSjoerg    virtual _Rp operator()(_A0) = 0;
55*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
56*4d6fc14bSjoerg    virtual const void* target(const type_info&) const = 0;
57*4d6fc14bSjoerg    virtual const std::type_info& target_type() const = 0;
58*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
59*4d6fc14bSjoerg};
60*4d6fc14bSjoerg
61*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
62*4d6fc14bSjoergclass __base<_Rp(_A0, _A1)>
63*4d6fc14bSjoerg{
64*4d6fc14bSjoerg    __base(const __base&);
65*4d6fc14bSjoerg    __base& operator=(const __base&);
66*4d6fc14bSjoergpublic:
67*4d6fc14bSjoerg    __base() {}
68*4d6fc14bSjoerg    virtual ~__base() {}
69*4d6fc14bSjoerg    virtual __base* __clone() const = 0;
70*4d6fc14bSjoerg    virtual void __clone(__base*) const = 0;
71*4d6fc14bSjoerg    virtual void destroy() = 0;
72*4d6fc14bSjoerg    virtual void destroy_deallocate() = 0;
73*4d6fc14bSjoerg    virtual _Rp operator()(_A0, _A1) = 0;
74*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
75*4d6fc14bSjoerg    virtual const void* target(const type_info&) const = 0;
76*4d6fc14bSjoerg    virtual const std::type_info& target_type() const = 0;
77*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
78*4d6fc14bSjoerg};
79*4d6fc14bSjoerg
80*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
81*4d6fc14bSjoergclass __base<_Rp(_A0, _A1, _A2)>
82*4d6fc14bSjoerg{
83*4d6fc14bSjoerg    __base(const __base&);
84*4d6fc14bSjoerg    __base& operator=(const __base&);
85*4d6fc14bSjoergpublic:
86*4d6fc14bSjoerg    __base() {}
87*4d6fc14bSjoerg    virtual ~__base() {}
88*4d6fc14bSjoerg    virtual __base* __clone() const = 0;
89*4d6fc14bSjoerg    virtual void __clone(__base*) const = 0;
90*4d6fc14bSjoerg    virtual void destroy() = 0;
91*4d6fc14bSjoerg    virtual void destroy_deallocate() = 0;
92*4d6fc14bSjoerg    virtual _Rp operator()(_A0, _A1, _A2) = 0;
93*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
94*4d6fc14bSjoerg    virtual const void* target(const type_info&) const = 0;
95*4d6fc14bSjoerg    virtual const std::type_info& target_type() const = 0;
96*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
97*4d6fc14bSjoerg};
98*4d6fc14bSjoerg
99*4d6fc14bSjoergtemplate<class _FD, class _Alloc, class _FB> class __func;
100*4d6fc14bSjoerg
101*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp>
102*4d6fc14bSjoergclass __func<_Fp, _Alloc, _Rp()>
103*4d6fc14bSjoerg    : public  __base<_Rp()>
104*4d6fc14bSjoerg{
105*4d6fc14bSjoerg    __compressed_pair<_Fp, _Alloc> __f_;
106*4d6fc14bSjoergpublic:
107*4d6fc14bSjoerg    explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
108*4d6fc14bSjoerg    explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
109*4d6fc14bSjoerg    virtual __base<_Rp()>* __clone() const;
110*4d6fc14bSjoerg    virtual void __clone(__base<_Rp()>*) const;
111*4d6fc14bSjoerg    virtual void destroy();
112*4d6fc14bSjoerg    virtual void destroy_deallocate();
113*4d6fc14bSjoerg    virtual _Rp operator()();
114*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
115*4d6fc14bSjoerg    virtual const void* target(const type_info&) const;
116*4d6fc14bSjoerg    virtual const std::type_info& target_type() const;
117*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
118*4d6fc14bSjoerg};
119*4d6fc14bSjoerg
120*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp>
121*4d6fc14bSjoerg__base<_Rp()>*
122*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp()>::__clone() const
123*4d6fc14bSjoerg{
124*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
125*4d6fc14bSjoerg    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
126*4d6fc14bSjoerg    _Ap __a(__f_.second());
127*4d6fc14bSjoerg    typedef __allocator_destructor<_Ap> _Dp;
128*4d6fc14bSjoerg    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
129*4d6fc14bSjoerg    ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
130*4d6fc14bSjoerg    return __hold.release();
131*4d6fc14bSjoerg}
132*4d6fc14bSjoerg
133*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp>
134*4d6fc14bSjoergvoid
135*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
136*4d6fc14bSjoerg{
137*4d6fc14bSjoerg    ::new ((void*)__p) __func(__f_.first(), __f_.second());
138*4d6fc14bSjoerg}
139*4d6fc14bSjoerg
140*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp>
141*4d6fc14bSjoergvoid
142*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp()>::destroy()
143*4d6fc14bSjoerg{
144*4d6fc14bSjoerg    __f_.~__compressed_pair<_Fp, _Alloc>();
145*4d6fc14bSjoerg}
146*4d6fc14bSjoerg
147*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp>
148*4d6fc14bSjoergvoid
149*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
150*4d6fc14bSjoerg{
151*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
152*4d6fc14bSjoerg    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
153*4d6fc14bSjoerg    _Ap __a(__f_.second());
154*4d6fc14bSjoerg    __f_.~__compressed_pair<_Fp, _Alloc>();
155*4d6fc14bSjoerg    __a.deallocate(this, 1);
156*4d6fc14bSjoerg}
157*4d6fc14bSjoerg
158*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp>
159*4d6fc14bSjoerg_Rp
160*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp()>::operator()()
161*4d6fc14bSjoerg{
162*4d6fc14bSjoerg    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
163*4d6fc14bSjoerg    return _Invoker::__call(__f_.first());
164*4d6fc14bSjoerg}
165*4d6fc14bSjoerg
166*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
167*4d6fc14bSjoerg
168*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp>
169*4d6fc14bSjoergconst void*
170*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
171*4d6fc14bSjoerg{
172*4d6fc14bSjoerg    if (__ti == typeid(_Fp))
173*4d6fc14bSjoerg        return &__f_.first();
174*4d6fc14bSjoerg    return (const void*)0;
175*4d6fc14bSjoerg}
176*4d6fc14bSjoerg
177*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp>
178*4d6fc14bSjoergconst std::type_info&
179*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp()>::target_type() const
180*4d6fc14bSjoerg{
181*4d6fc14bSjoerg    return typeid(_Fp);
182*4d6fc14bSjoerg}
183*4d6fc14bSjoerg
184*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
185*4d6fc14bSjoerg
186*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0>
187*4d6fc14bSjoergclass __func<_Fp, _Alloc, _Rp(_A0)>
188*4d6fc14bSjoerg    : public  __base<_Rp(_A0)>
189*4d6fc14bSjoerg{
190*4d6fc14bSjoerg    __compressed_pair<_Fp, _Alloc> __f_;
191*4d6fc14bSjoergpublic:
192*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
193*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
194*4d6fc14bSjoerg        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
195*4d6fc14bSjoerg    virtual __base<_Rp(_A0)>* __clone() const;
196*4d6fc14bSjoerg    virtual void __clone(__base<_Rp(_A0)>*) const;
197*4d6fc14bSjoerg    virtual void destroy();
198*4d6fc14bSjoerg    virtual void destroy_deallocate();
199*4d6fc14bSjoerg    virtual _Rp operator()(_A0);
200*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
201*4d6fc14bSjoerg    virtual const void* target(const type_info&) const;
202*4d6fc14bSjoerg    virtual const std::type_info& target_type() const;
203*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
204*4d6fc14bSjoerg};
205*4d6fc14bSjoerg
206*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0>
207*4d6fc14bSjoerg__base<_Rp(_A0)>*
208*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
209*4d6fc14bSjoerg{
210*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
211*4d6fc14bSjoerg    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
212*4d6fc14bSjoerg    _Ap __a(__f_.second());
213*4d6fc14bSjoerg    typedef __allocator_destructor<_Ap> _Dp;
214*4d6fc14bSjoerg    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
215*4d6fc14bSjoerg    ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
216*4d6fc14bSjoerg    return __hold.release();
217*4d6fc14bSjoerg}
218*4d6fc14bSjoerg
219*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0>
220*4d6fc14bSjoergvoid
221*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
222*4d6fc14bSjoerg{
223*4d6fc14bSjoerg    ::new ((void*)__p) __func(__f_.first(), __f_.second());
224*4d6fc14bSjoerg}
225*4d6fc14bSjoerg
226*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0>
227*4d6fc14bSjoergvoid
228*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
229*4d6fc14bSjoerg{
230*4d6fc14bSjoerg    __f_.~__compressed_pair<_Fp, _Alloc>();
231*4d6fc14bSjoerg}
232*4d6fc14bSjoerg
233*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0>
234*4d6fc14bSjoergvoid
235*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
236*4d6fc14bSjoerg{
237*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
238*4d6fc14bSjoerg    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
239*4d6fc14bSjoerg    _Ap __a(__f_.second());
240*4d6fc14bSjoerg    __f_.~__compressed_pair<_Fp, _Alloc>();
241*4d6fc14bSjoerg    __a.deallocate(this, 1);
242*4d6fc14bSjoerg}
243*4d6fc14bSjoerg
244*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0>
245*4d6fc14bSjoerg_Rp
246*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
247*4d6fc14bSjoerg{
248*4d6fc14bSjoerg    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
249*4d6fc14bSjoerg    return _Invoker::__call(__f_.first(), __a0);
250*4d6fc14bSjoerg}
251*4d6fc14bSjoerg
252*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
253*4d6fc14bSjoerg
254*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0>
255*4d6fc14bSjoergconst void*
256*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
257*4d6fc14bSjoerg{
258*4d6fc14bSjoerg    if (__ti == typeid(_Fp))
259*4d6fc14bSjoerg        return &__f_.first();
260*4d6fc14bSjoerg    return (const void*)0;
261*4d6fc14bSjoerg}
262*4d6fc14bSjoerg
263*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0>
264*4d6fc14bSjoergconst std::type_info&
265*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
266*4d6fc14bSjoerg{
267*4d6fc14bSjoerg    return typeid(_Fp);
268*4d6fc14bSjoerg}
269*4d6fc14bSjoerg
270*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
271*4d6fc14bSjoerg
272*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
273*4d6fc14bSjoergclass __func<_Fp, _Alloc, _Rp(_A0, _A1)>
274*4d6fc14bSjoerg    : public  __base<_Rp(_A0, _A1)>
275*4d6fc14bSjoerg{
276*4d6fc14bSjoerg    __compressed_pair<_Fp, _Alloc> __f_;
277*4d6fc14bSjoergpublic:
278*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
279*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
280*4d6fc14bSjoerg        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
281*4d6fc14bSjoerg    virtual __base<_Rp(_A0, _A1)>* __clone() const;
282*4d6fc14bSjoerg    virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
283*4d6fc14bSjoerg    virtual void destroy();
284*4d6fc14bSjoerg    virtual void destroy_deallocate();
285*4d6fc14bSjoerg    virtual _Rp operator()(_A0, _A1);
286*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
287*4d6fc14bSjoerg    virtual const void* target(const type_info&) const;
288*4d6fc14bSjoerg    virtual const std::type_info& target_type() const;
289*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
290*4d6fc14bSjoerg};
291*4d6fc14bSjoerg
292*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
293*4d6fc14bSjoerg__base<_Rp(_A0, _A1)>*
294*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
295*4d6fc14bSjoerg{
296*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
297*4d6fc14bSjoerg    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
298*4d6fc14bSjoerg    _Ap __a(__f_.second());
299*4d6fc14bSjoerg    typedef __allocator_destructor<_Ap> _Dp;
300*4d6fc14bSjoerg    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
301*4d6fc14bSjoerg    ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
302*4d6fc14bSjoerg    return __hold.release();
303*4d6fc14bSjoerg}
304*4d6fc14bSjoerg
305*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
306*4d6fc14bSjoergvoid
307*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
308*4d6fc14bSjoerg{
309*4d6fc14bSjoerg    ::new ((void*)__p) __func(__f_.first(), __f_.second());
310*4d6fc14bSjoerg}
311*4d6fc14bSjoerg
312*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
313*4d6fc14bSjoergvoid
314*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
315*4d6fc14bSjoerg{
316*4d6fc14bSjoerg    __f_.~__compressed_pair<_Fp, _Alloc>();
317*4d6fc14bSjoerg}
318*4d6fc14bSjoerg
319*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
320*4d6fc14bSjoergvoid
321*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
322*4d6fc14bSjoerg{
323*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
324*4d6fc14bSjoerg    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
325*4d6fc14bSjoerg    _Ap __a(__f_.second());
326*4d6fc14bSjoerg    __f_.~__compressed_pair<_Fp, _Alloc>();
327*4d6fc14bSjoerg    __a.deallocate(this, 1);
328*4d6fc14bSjoerg}
329*4d6fc14bSjoerg
330*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
331*4d6fc14bSjoerg_Rp
332*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
333*4d6fc14bSjoerg{
334*4d6fc14bSjoerg    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
335*4d6fc14bSjoerg    return _Invoker::__call(__f_.first(), __a0, __a1);
336*4d6fc14bSjoerg}
337*4d6fc14bSjoerg
338*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
339*4d6fc14bSjoerg
340*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
341*4d6fc14bSjoergconst void*
342*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
343*4d6fc14bSjoerg{
344*4d6fc14bSjoerg    if (__ti == typeid(_Fp))
345*4d6fc14bSjoerg        return &__f_.first();
346*4d6fc14bSjoerg    return (const void*)0;
347*4d6fc14bSjoerg}
348*4d6fc14bSjoerg
349*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
350*4d6fc14bSjoergconst std::type_info&
351*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
352*4d6fc14bSjoerg{
353*4d6fc14bSjoerg    return typeid(_Fp);
354*4d6fc14bSjoerg}
355*4d6fc14bSjoerg
356*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
357*4d6fc14bSjoerg
358*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
359*4d6fc14bSjoergclass __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
360*4d6fc14bSjoerg    : public  __base<_Rp(_A0, _A1, _A2)>
361*4d6fc14bSjoerg{
362*4d6fc14bSjoerg    __compressed_pair<_Fp, _Alloc> __f_;
363*4d6fc14bSjoergpublic:
364*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
365*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
366*4d6fc14bSjoerg        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
367*4d6fc14bSjoerg    virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
368*4d6fc14bSjoerg    virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
369*4d6fc14bSjoerg    virtual void destroy();
370*4d6fc14bSjoerg    virtual void destroy_deallocate();
371*4d6fc14bSjoerg    virtual _Rp operator()(_A0, _A1, _A2);
372*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
373*4d6fc14bSjoerg    virtual const void* target(const type_info&) const;
374*4d6fc14bSjoerg    virtual const std::type_info& target_type() const;
375*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
376*4d6fc14bSjoerg};
377*4d6fc14bSjoerg
378*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
379*4d6fc14bSjoerg__base<_Rp(_A0, _A1, _A2)>*
380*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
381*4d6fc14bSjoerg{
382*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
383*4d6fc14bSjoerg    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
384*4d6fc14bSjoerg    _Ap __a(__f_.second());
385*4d6fc14bSjoerg    typedef __allocator_destructor<_Ap> _Dp;
386*4d6fc14bSjoerg    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
387*4d6fc14bSjoerg    ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a));
388*4d6fc14bSjoerg    return __hold.release();
389*4d6fc14bSjoerg}
390*4d6fc14bSjoerg
391*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
392*4d6fc14bSjoergvoid
393*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
394*4d6fc14bSjoerg{
395*4d6fc14bSjoerg    ::new ((void*)__p) __func(__f_.first(), __f_.second());
396*4d6fc14bSjoerg}
397*4d6fc14bSjoerg
398*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
399*4d6fc14bSjoergvoid
400*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
401*4d6fc14bSjoerg{
402*4d6fc14bSjoerg    __f_.~__compressed_pair<_Fp, _Alloc>();
403*4d6fc14bSjoerg}
404*4d6fc14bSjoerg
405*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
406*4d6fc14bSjoergvoid
407*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
408*4d6fc14bSjoerg{
409*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
410*4d6fc14bSjoerg    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
411*4d6fc14bSjoerg    _Ap __a(__f_.second());
412*4d6fc14bSjoerg    __f_.~__compressed_pair<_Fp, _Alloc>();
413*4d6fc14bSjoerg    __a.deallocate(this, 1);
414*4d6fc14bSjoerg}
415*4d6fc14bSjoerg
416*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
417*4d6fc14bSjoerg_Rp
418*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
419*4d6fc14bSjoerg{
420*4d6fc14bSjoerg    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
421*4d6fc14bSjoerg    return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
422*4d6fc14bSjoerg}
423*4d6fc14bSjoerg
424*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
425*4d6fc14bSjoerg
426*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
427*4d6fc14bSjoergconst void*
428*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
429*4d6fc14bSjoerg{
430*4d6fc14bSjoerg    if (__ti == typeid(_Fp))
431*4d6fc14bSjoerg        return &__f_.first();
432*4d6fc14bSjoerg    return (const void*)0;
433*4d6fc14bSjoerg}
434*4d6fc14bSjoerg
435*4d6fc14bSjoergtemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
436*4d6fc14bSjoergconst std::type_info&
437*4d6fc14bSjoerg__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
438*4d6fc14bSjoerg{
439*4d6fc14bSjoerg    return typeid(_Fp);
440*4d6fc14bSjoerg}
441*4d6fc14bSjoerg
442*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
443*4d6fc14bSjoerg
444*4d6fc14bSjoerg}  // __function
445*4d6fc14bSjoerg
446*4d6fc14bSjoergtemplate<class _Rp>
447*4d6fc14bSjoergclass _LIBCPP_TEMPLATE_VIS function<_Rp()>
448*4d6fc14bSjoerg{
449*4d6fc14bSjoerg    typedef __function::__base<_Rp()> __base;
450*4d6fc14bSjoerg    aligned_storage<3*sizeof(void*)>::type __buf_;
451*4d6fc14bSjoerg    __base* __f_;
452*4d6fc14bSjoerg
453*4d6fc14bSjoergpublic:
454*4d6fc14bSjoerg    typedef _Rp result_type;
455*4d6fc14bSjoerg
456*4d6fc14bSjoerg    // 20.7.16.2.1, construct/copy/destroy:
457*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
458*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
459*4d6fc14bSjoerg    function(const function&);
460*4d6fc14bSjoerg    template<class _Fp>
461*4d6fc14bSjoerg      function(_Fp,
462*4d6fc14bSjoerg               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
463*4d6fc14bSjoerg
464*4d6fc14bSjoerg    template<class _Alloc>
465*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
466*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
467*4d6fc14bSjoerg    template<class _Alloc>
468*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
469*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
470*4d6fc14bSjoerg    template<class _Alloc>
471*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&, const function&);
472*4d6fc14bSjoerg    template<class _Fp, class _Alloc>
473*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
474*4d6fc14bSjoerg               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
475*4d6fc14bSjoerg
476*4d6fc14bSjoerg    function& operator=(const function&);
477*4d6fc14bSjoerg    function& operator=(nullptr_t);
478*4d6fc14bSjoerg    template<class _Fp>
479*4d6fc14bSjoerg      typename enable_if
480*4d6fc14bSjoerg      <
481*4d6fc14bSjoerg        !is_integral<_Fp>::value,
482*4d6fc14bSjoerg        function&
483*4d6fc14bSjoerg      >::type
484*4d6fc14bSjoerg      operator=(_Fp);
485*4d6fc14bSjoerg
486*4d6fc14bSjoerg    ~function();
487*4d6fc14bSjoerg
488*4d6fc14bSjoerg    // 20.7.16.2.2, function modifiers:
489*4d6fc14bSjoerg    void swap(function&);
490*4d6fc14bSjoerg    template<class _Fp, class _Alloc>
491*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
492*4d6fc14bSjoerg      void assign(_Fp __f, const _Alloc& __a)
493*4d6fc14bSjoerg        {function(allocator_arg, __a, __f).swap(*this);}
494*4d6fc14bSjoerg
495*4d6fc14bSjoerg    // 20.7.16.2.3, function capacity:
496*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
497*4d6fc14bSjoerg
498*4d6fc14bSjoergprivate:
499*4d6fc14bSjoerg    // deleted overloads close possible hole in the type system
500*4d6fc14bSjoerg    template<class _R2>
501*4d6fc14bSjoerg      bool operator==(const function<_R2()>&) const;// = delete;
502*4d6fc14bSjoerg    template<class _R2>
503*4d6fc14bSjoerg      bool operator!=(const function<_R2()>&) const;// = delete;
504*4d6fc14bSjoergpublic:
505*4d6fc14bSjoerg    // 20.7.16.2.4, function invocation:
506*4d6fc14bSjoerg    _Rp operator()() const;
507*4d6fc14bSjoerg
508*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
509*4d6fc14bSjoerg    // 20.7.16.2.5, function target access:
510*4d6fc14bSjoerg    const std::type_info& target_type() const;
511*4d6fc14bSjoerg    template <typename _Tp> _Tp* target();
512*4d6fc14bSjoerg    template <typename _Tp> const _Tp* target() const;
513*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
514*4d6fc14bSjoerg};
515*4d6fc14bSjoerg
516*4d6fc14bSjoergtemplate<class _Rp>
517*4d6fc14bSjoergfunction<_Rp()>::function(const function& __f)
518*4d6fc14bSjoerg{
519*4d6fc14bSjoerg    if (__f.__f_ == 0)
520*4d6fc14bSjoerg        __f_ = 0;
521*4d6fc14bSjoerg    else if (__f.__f_ == (const __base*)&__f.__buf_)
522*4d6fc14bSjoerg    {
523*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
524*4d6fc14bSjoerg        __f.__f_->__clone(__f_);
525*4d6fc14bSjoerg    }
526*4d6fc14bSjoerg    else
527*4d6fc14bSjoerg        __f_ = __f.__f_->__clone();
528*4d6fc14bSjoerg}
529*4d6fc14bSjoerg
530*4d6fc14bSjoergtemplate<class _Rp>
531*4d6fc14bSjoergtemplate<class _Alloc>
532*4d6fc14bSjoergfunction<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
533*4d6fc14bSjoerg{
534*4d6fc14bSjoerg    if (__f.__f_ == 0)
535*4d6fc14bSjoerg        __f_ = 0;
536*4d6fc14bSjoerg    else if (__f.__f_ == (const __base*)&__f.__buf_)
537*4d6fc14bSjoerg    {
538*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
539*4d6fc14bSjoerg        __f.__f_->__clone(__f_);
540*4d6fc14bSjoerg    }
541*4d6fc14bSjoerg    else
542*4d6fc14bSjoerg        __f_ = __f.__f_->__clone();
543*4d6fc14bSjoerg}
544*4d6fc14bSjoerg
545*4d6fc14bSjoergtemplate<class _Rp>
546*4d6fc14bSjoergtemplate <class _Fp>
547*4d6fc14bSjoergfunction<_Rp()>::function(_Fp __f,
548*4d6fc14bSjoerg                                     typename enable_if<!is_integral<_Fp>::value>::type*)
549*4d6fc14bSjoerg    : __f_(0)
550*4d6fc14bSjoerg{
551*4d6fc14bSjoerg    if (__function::__not_null(__f))
552*4d6fc14bSjoerg    {
553*4d6fc14bSjoerg        typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
554*4d6fc14bSjoerg        if (sizeof(_FF) <= sizeof(__buf_))
555*4d6fc14bSjoerg        {
556*4d6fc14bSjoerg            __f_ = (__base*)&__buf_;
557*4d6fc14bSjoerg            ::new ((void*)__f_) _FF(__f);
558*4d6fc14bSjoerg        }
559*4d6fc14bSjoerg        else
560*4d6fc14bSjoerg        {
561*4d6fc14bSjoerg            typedef allocator<_FF> _Ap;
562*4d6fc14bSjoerg            _Ap __a;
563*4d6fc14bSjoerg            typedef __allocator_destructor<_Ap> _Dp;
564*4d6fc14bSjoerg            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
565*4d6fc14bSjoerg            ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
566*4d6fc14bSjoerg            __f_ = __hold.release();
567*4d6fc14bSjoerg        }
568*4d6fc14bSjoerg    }
569*4d6fc14bSjoerg}
570*4d6fc14bSjoerg
571*4d6fc14bSjoergtemplate<class _Rp>
572*4d6fc14bSjoergtemplate <class _Fp, class _Alloc>
573*4d6fc14bSjoergfunction<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
574*4d6fc14bSjoerg                                     typename enable_if<!is_integral<_Fp>::value>::type*)
575*4d6fc14bSjoerg    : __f_(0)
576*4d6fc14bSjoerg{
577*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
578*4d6fc14bSjoerg    if (__function::__not_null(__f))
579*4d6fc14bSjoerg    {
580*4d6fc14bSjoerg        typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
581*4d6fc14bSjoerg        if (sizeof(_FF) <= sizeof(__buf_))
582*4d6fc14bSjoerg        {
583*4d6fc14bSjoerg            __f_ = (__base*)&__buf_;
584*4d6fc14bSjoerg            ::new ((void*)__f_) _FF(__f, __a0);
585*4d6fc14bSjoerg        }
586*4d6fc14bSjoerg        else
587*4d6fc14bSjoerg        {
588*4d6fc14bSjoerg            typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
589*4d6fc14bSjoerg            _Ap __a(__a0);
590*4d6fc14bSjoerg            typedef __allocator_destructor<_Ap> _Dp;
591*4d6fc14bSjoerg            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
592*4d6fc14bSjoerg            ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
593*4d6fc14bSjoerg            __f_ = __hold.release();
594*4d6fc14bSjoerg        }
595*4d6fc14bSjoerg    }
596*4d6fc14bSjoerg}
597*4d6fc14bSjoerg
598*4d6fc14bSjoergtemplate<class _Rp>
599*4d6fc14bSjoergfunction<_Rp()>&
600*4d6fc14bSjoergfunction<_Rp()>::operator=(const function& __f)
601*4d6fc14bSjoerg{
602*4d6fc14bSjoerg    if (__f)
603*4d6fc14bSjoerg        function(__f).swap(*this);
604*4d6fc14bSjoerg    else
605*4d6fc14bSjoerg        *this = nullptr;
606*4d6fc14bSjoerg    return *this;
607*4d6fc14bSjoerg}
608*4d6fc14bSjoerg
609*4d6fc14bSjoergtemplate<class _Rp>
610*4d6fc14bSjoergfunction<_Rp()>&
611*4d6fc14bSjoergfunction<_Rp()>::operator=(nullptr_t)
612*4d6fc14bSjoerg{
613*4d6fc14bSjoerg    __base* __t = __f_;
614*4d6fc14bSjoerg    __f_ = 0;
615*4d6fc14bSjoerg    if (__t == (__base*)&__buf_)
616*4d6fc14bSjoerg        __t->destroy();
617*4d6fc14bSjoerg    else if (__t)
618*4d6fc14bSjoerg        __t->destroy_deallocate();
619*4d6fc14bSjoerg    return *this;
620*4d6fc14bSjoerg}
621*4d6fc14bSjoerg
622*4d6fc14bSjoergtemplate<class _Rp>
623*4d6fc14bSjoergtemplate <class _Fp>
624*4d6fc14bSjoergtypename enable_if
625*4d6fc14bSjoerg<
626*4d6fc14bSjoerg    !is_integral<_Fp>::value,
627*4d6fc14bSjoerg    function<_Rp()>&
628*4d6fc14bSjoerg>::type
629*4d6fc14bSjoergfunction<_Rp()>::operator=(_Fp __f)
630*4d6fc14bSjoerg{
631*4d6fc14bSjoerg    function(_VSTD::move(__f)).swap(*this);
632*4d6fc14bSjoerg    return *this;
633*4d6fc14bSjoerg}
634*4d6fc14bSjoerg
635*4d6fc14bSjoergtemplate<class _Rp>
636*4d6fc14bSjoergfunction<_Rp()>::~function()
637*4d6fc14bSjoerg{
638*4d6fc14bSjoerg    if (__f_ == (__base*)&__buf_)
639*4d6fc14bSjoerg        __f_->destroy();
640*4d6fc14bSjoerg    else if (__f_)
641*4d6fc14bSjoerg        __f_->destroy_deallocate();
642*4d6fc14bSjoerg}
643*4d6fc14bSjoerg
644*4d6fc14bSjoergtemplate<class _Rp>
645*4d6fc14bSjoergvoid
646*4d6fc14bSjoergfunction<_Rp()>::swap(function& __f)
647*4d6fc14bSjoerg{
648*4d6fc14bSjoerg    if (_VSTD::addressof(__f) == this)
649*4d6fc14bSjoerg      return;
650*4d6fc14bSjoerg    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
651*4d6fc14bSjoerg    {
652*4d6fc14bSjoerg        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
653*4d6fc14bSjoerg        __base* __t = (__base*)&__tempbuf;
654*4d6fc14bSjoerg        __f_->__clone(__t);
655*4d6fc14bSjoerg        __f_->destroy();
656*4d6fc14bSjoerg        __f_ = 0;
657*4d6fc14bSjoerg        __f.__f_->__clone((__base*)&__buf_);
658*4d6fc14bSjoerg        __f.__f_->destroy();
659*4d6fc14bSjoerg        __f.__f_ = 0;
660*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
661*4d6fc14bSjoerg        __t->__clone((__base*)&__f.__buf_);
662*4d6fc14bSjoerg        __t->destroy();
663*4d6fc14bSjoerg        __f.__f_ = (__base*)&__f.__buf_;
664*4d6fc14bSjoerg    }
665*4d6fc14bSjoerg    else if (__f_ == (__base*)&__buf_)
666*4d6fc14bSjoerg    {
667*4d6fc14bSjoerg        __f_->__clone((__base*)&__f.__buf_);
668*4d6fc14bSjoerg        __f_->destroy();
669*4d6fc14bSjoerg        __f_ = __f.__f_;
670*4d6fc14bSjoerg        __f.__f_ = (__base*)&__f.__buf_;
671*4d6fc14bSjoerg    }
672*4d6fc14bSjoerg    else if (__f.__f_ == (__base*)&__f.__buf_)
673*4d6fc14bSjoerg    {
674*4d6fc14bSjoerg        __f.__f_->__clone((__base*)&__buf_);
675*4d6fc14bSjoerg        __f.__f_->destroy();
676*4d6fc14bSjoerg        __f.__f_ = __f_;
677*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
678*4d6fc14bSjoerg    }
679*4d6fc14bSjoerg    else
680*4d6fc14bSjoerg        _VSTD::swap(__f_, __f.__f_);
681*4d6fc14bSjoerg}
682*4d6fc14bSjoerg
683*4d6fc14bSjoergtemplate<class _Rp>
684*4d6fc14bSjoerg_Rp
685*4d6fc14bSjoergfunction<_Rp()>::operator()() const
686*4d6fc14bSjoerg{
687*4d6fc14bSjoerg    if (__f_ == 0)
688*4d6fc14bSjoerg        __throw_bad_function_call();
689*4d6fc14bSjoerg    return (*__f_)();
690*4d6fc14bSjoerg}
691*4d6fc14bSjoerg
692*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
693*4d6fc14bSjoerg
694*4d6fc14bSjoergtemplate<class _Rp>
695*4d6fc14bSjoergconst std::type_info&
696*4d6fc14bSjoergfunction<_Rp()>::target_type() const
697*4d6fc14bSjoerg{
698*4d6fc14bSjoerg    if (__f_ == 0)
699*4d6fc14bSjoerg        return typeid(void);
700*4d6fc14bSjoerg    return __f_->target_type();
701*4d6fc14bSjoerg}
702*4d6fc14bSjoerg
703*4d6fc14bSjoergtemplate<class _Rp>
704*4d6fc14bSjoergtemplate <typename _Tp>
705*4d6fc14bSjoerg_Tp*
706*4d6fc14bSjoergfunction<_Rp()>::target()
707*4d6fc14bSjoerg{
708*4d6fc14bSjoerg    if (__f_ == 0)
709*4d6fc14bSjoerg        return (_Tp*)0;
710*4d6fc14bSjoerg    return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
711*4d6fc14bSjoerg}
712*4d6fc14bSjoerg
713*4d6fc14bSjoergtemplate<class _Rp>
714*4d6fc14bSjoergtemplate <typename _Tp>
715*4d6fc14bSjoergconst _Tp*
716*4d6fc14bSjoergfunction<_Rp()>::target() const
717*4d6fc14bSjoerg{
718*4d6fc14bSjoerg    if (__f_ == 0)
719*4d6fc14bSjoerg        return (const _Tp*)0;
720*4d6fc14bSjoerg    return (const _Tp*)__f_->target(typeid(_Tp));
721*4d6fc14bSjoerg}
722*4d6fc14bSjoerg
723*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
724*4d6fc14bSjoerg
725*4d6fc14bSjoergtemplate<class _Rp, class _A0>
726*4d6fc14bSjoergclass _LIBCPP_TEMPLATE_VIS function<_Rp(_A0)>
727*4d6fc14bSjoerg    : public unary_function<_A0, _Rp>
728*4d6fc14bSjoerg{
729*4d6fc14bSjoerg    typedef __function::__base<_Rp(_A0)> __base;
730*4d6fc14bSjoerg    aligned_storage<3*sizeof(void*)>::type __buf_;
731*4d6fc14bSjoerg    __base* __f_;
732*4d6fc14bSjoerg
733*4d6fc14bSjoergpublic:
734*4d6fc14bSjoerg    typedef _Rp result_type;
735*4d6fc14bSjoerg
736*4d6fc14bSjoerg    // 20.7.16.2.1, construct/copy/destroy:
737*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
738*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
739*4d6fc14bSjoerg    function(const function&);
740*4d6fc14bSjoerg    template<class _Fp>
741*4d6fc14bSjoerg      function(_Fp,
742*4d6fc14bSjoerg               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
743*4d6fc14bSjoerg
744*4d6fc14bSjoerg    template<class _Alloc>
745*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
746*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
747*4d6fc14bSjoerg    template<class _Alloc>
748*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
749*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
750*4d6fc14bSjoerg    template<class _Alloc>
751*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&, const function&);
752*4d6fc14bSjoerg    template<class _Fp, class _Alloc>
753*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
754*4d6fc14bSjoerg               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
755*4d6fc14bSjoerg
756*4d6fc14bSjoerg    function& operator=(const function&);
757*4d6fc14bSjoerg    function& operator=(nullptr_t);
758*4d6fc14bSjoerg    template<class _Fp>
759*4d6fc14bSjoerg      typename enable_if
760*4d6fc14bSjoerg      <
761*4d6fc14bSjoerg        !is_integral<_Fp>::value,
762*4d6fc14bSjoerg        function&
763*4d6fc14bSjoerg      >::type
764*4d6fc14bSjoerg      operator=(_Fp);
765*4d6fc14bSjoerg
766*4d6fc14bSjoerg    ~function();
767*4d6fc14bSjoerg
768*4d6fc14bSjoerg    // 20.7.16.2.2, function modifiers:
769*4d6fc14bSjoerg    void swap(function&);
770*4d6fc14bSjoerg    template<class _Fp, class _Alloc>
771*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
772*4d6fc14bSjoerg      void assign(_Fp __f, const _Alloc& __a)
773*4d6fc14bSjoerg        {function(allocator_arg, __a, __f).swap(*this);}
774*4d6fc14bSjoerg
775*4d6fc14bSjoerg    // 20.7.16.2.3, function capacity:
776*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
777*4d6fc14bSjoerg
778*4d6fc14bSjoergprivate:
779*4d6fc14bSjoerg    // deleted overloads close possible hole in the type system
780*4d6fc14bSjoerg    template<class _R2, class _B0>
781*4d6fc14bSjoerg      bool operator==(const function<_R2(_B0)>&) const;// = delete;
782*4d6fc14bSjoerg    template<class _R2, class _B0>
783*4d6fc14bSjoerg      bool operator!=(const function<_R2(_B0)>&) const;// = delete;
784*4d6fc14bSjoergpublic:
785*4d6fc14bSjoerg    // 20.7.16.2.4, function invocation:
786*4d6fc14bSjoerg    _Rp operator()(_A0) const;
787*4d6fc14bSjoerg
788*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
789*4d6fc14bSjoerg    // 20.7.16.2.5, function target access:
790*4d6fc14bSjoerg    const std::type_info& target_type() const;
791*4d6fc14bSjoerg    template <typename _Tp> _Tp* target();
792*4d6fc14bSjoerg    template <typename _Tp> const _Tp* target() const;
793*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
794*4d6fc14bSjoerg};
795*4d6fc14bSjoerg
796*4d6fc14bSjoergtemplate<class _Rp, class _A0>
797*4d6fc14bSjoergfunction<_Rp(_A0)>::function(const function& __f)
798*4d6fc14bSjoerg{
799*4d6fc14bSjoerg    if (__f.__f_ == 0)
800*4d6fc14bSjoerg        __f_ = 0;
801*4d6fc14bSjoerg    else if (__f.__f_ == (const __base*)&__f.__buf_)
802*4d6fc14bSjoerg    {
803*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
804*4d6fc14bSjoerg        __f.__f_->__clone(__f_);
805*4d6fc14bSjoerg    }
806*4d6fc14bSjoerg    else
807*4d6fc14bSjoerg        __f_ = __f.__f_->__clone();
808*4d6fc14bSjoerg}
809*4d6fc14bSjoerg
810*4d6fc14bSjoergtemplate<class _Rp, class _A0>
811*4d6fc14bSjoergtemplate<class _Alloc>
812*4d6fc14bSjoergfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
813*4d6fc14bSjoerg{
814*4d6fc14bSjoerg    if (__f.__f_ == 0)
815*4d6fc14bSjoerg        __f_ = 0;
816*4d6fc14bSjoerg    else if (__f.__f_ == (const __base*)&__f.__buf_)
817*4d6fc14bSjoerg    {
818*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
819*4d6fc14bSjoerg        __f.__f_->__clone(__f_);
820*4d6fc14bSjoerg    }
821*4d6fc14bSjoerg    else
822*4d6fc14bSjoerg        __f_ = __f.__f_->__clone();
823*4d6fc14bSjoerg}
824*4d6fc14bSjoerg
825*4d6fc14bSjoergtemplate<class _Rp, class _A0>
826*4d6fc14bSjoergtemplate <class _Fp>
827*4d6fc14bSjoergfunction<_Rp(_A0)>::function(_Fp __f,
828*4d6fc14bSjoerg                                     typename enable_if<!is_integral<_Fp>::value>::type*)
829*4d6fc14bSjoerg    : __f_(0)
830*4d6fc14bSjoerg{
831*4d6fc14bSjoerg    if (__function::__not_null(__f))
832*4d6fc14bSjoerg    {
833*4d6fc14bSjoerg        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
834*4d6fc14bSjoerg        if (sizeof(_FF) <= sizeof(__buf_))
835*4d6fc14bSjoerg        {
836*4d6fc14bSjoerg            __f_ = (__base*)&__buf_;
837*4d6fc14bSjoerg            ::new ((void*)__f_) _FF(__f);
838*4d6fc14bSjoerg        }
839*4d6fc14bSjoerg        else
840*4d6fc14bSjoerg        {
841*4d6fc14bSjoerg            typedef allocator<_FF> _Ap;
842*4d6fc14bSjoerg            _Ap __a;
843*4d6fc14bSjoerg            typedef __allocator_destructor<_Ap> _Dp;
844*4d6fc14bSjoerg            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
845*4d6fc14bSjoerg            ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
846*4d6fc14bSjoerg            __f_ = __hold.release();
847*4d6fc14bSjoerg        }
848*4d6fc14bSjoerg    }
849*4d6fc14bSjoerg}
850*4d6fc14bSjoerg
851*4d6fc14bSjoergtemplate<class _Rp, class _A0>
852*4d6fc14bSjoergtemplate <class _Fp, class _Alloc>
853*4d6fc14bSjoergfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
854*4d6fc14bSjoerg                                     typename enable_if<!is_integral<_Fp>::value>::type*)
855*4d6fc14bSjoerg    : __f_(0)
856*4d6fc14bSjoerg{
857*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
858*4d6fc14bSjoerg    if (__function::__not_null(__f))
859*4d6fc14bSjoerg    {
860*4d6fc14bSjoerg        typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
861*4d6fc14bSjoerg        if (sizeof(_FF) <= sizeof(__buf_))
862*4d6fc14bSjoerg        {
863*4d6fc14bSjoerg            __f_ = (__base*)&__buf_;
864*4d6fc14bSjoerg            ::new ((void*)__f_) _FF(__f, __a0);
865*4d6fc14bSjoerg        }
866*4d6fc14bSjoerg        else
867*4d6fc14bSjoerg        {
868*4d6fc14bSjoerg            typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
869*4d6fc14bSjoerg            _Ap __a(__a0);
870*4d6fc14bSjoerg            typedef __allocator_destructor<_Ap> _Dp;
871*4d6fc14bSjoerg            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
872*4d6fc14bSjoerg            ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
873*4d6fc14bSjoerg            __f_ = __hold.release();
874*4d6fc14bSjoerg        }
875*4d6fc14bSjoerg    }
876*4d6fc14bSjoerg}
877*4d6fc14bSjoerg
878*4d6fc14bSjoergtemplate<class _Rp, class _A0>
879*4d6fc14bSjoergfunction<_Rp(_A0)>&
880*4d6fc14bSjoergfunction<_Rp(_A0)>::operator=(const function& __f)
881*4d6fc14bSjoerg{
882*4d6fc14bSjoerg    if (__f)
883*4d6fc14bSjoerg        function(__f).swap(*this);
884*4d6fc14bSjoerg    else
885*4d6fc14bSjoerg        *this = nullptr;
886*4d6fc14bSjoerg    return *this;
887*4d6fc14bSjoerg}
888*4d6fc14bSjoerg
889*4d6fc14bSjoergtemplate<class _Rp, class _A0>
890*4d6fc14bSjoergfunction<_Rp(_A0)>&
891*4d6fc14bSjoergfunction<_Rp(_A0)>::operator=(nullptr_t)
892*4d6fc14bSjoerg{
893*4d6fc14bSjoerg    __base* __t = __f_;
894*4d6fc14bSjoerg    __f_ = 0;
895*4d6fc14bSjoerg    if (__t == (__base*)&__buf_)
896*4d6fc14bSjoerg        __t->destroy();
897*4d6fc14bSjoerg    else if (__t)
898*4d6fc14bSjoerg        __t->destroy_deallocate();
899*4d6fc14bSjoerg    return *this;
900*4d6fc14bSjoerg}
901*4d6fc14bSjoerg
902*4d6fc14bSjoergtemplate<class _Rp, class _A0>
903*4d6fc14bSjoergtemplate <class _Fp>
904*4d6fc14bSjoergtypename enable_if
905*4d6fc14bSjoerg<
906*4d6fc14bSjoerg    !is_integral<_Fp>::value,
907*4d6fc14bSjoerg    function<_Rp(_A0)>&
908*4d6fc14bSjoerg>::type
909*4d6fc14bSjoergfunction<_Rp(_A0)>::operator=(_Fp __f)
910*4d6fc14bSjoerg{
911*4d6fc14bSjoerg    function(_VSTD::move(__f)).swap(*this);
912*4d6fc14bSjoerg    return *this;
913*4d6fc14bSjoerg}
914*4d6fc14bSjoerg
915*4d6fc14bSjoergtemplate<class _Rp, class _A0>
916*4d6fc14bSjoergfunction<_Rp(_A0)>::~function()
917*4d6fc14bSjoerg{
918*4d6fc14bSjoerg    if (__f_ == (__base*)&__buf_)
919*4d6fc14bSjoerg        __f_->destroy();
920*4d6fc14bSjoerg    else if (__f_)
921*4d6fc14bSjoerg        __f_->destroy_deallocate();
922*4d6fc14bSjoerg}
923*4d6fc14bSjoerg
924*4d6fc14bSjoergtemplate<class _Rp, class _A0>
925*4d6fc14bSjoergvoid
926*4d6fc14bSjoergfunction<_Rp(_A0)>::swap(function& __f)
927*4d6fc14bSjoerg{
928*4d6fc14bSjoerg    if (_VSTD::addressof(__f) == this)
929*4d6fc14bSjoerg      return;
930*4d6fc14bSjoerg    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
931*4d6fc14bSjoerg    {
932*4d6fc14bSjoerg        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
933*4d6fc14bSjoerg        __base* __t = (__base*)&__tempbuf;
934*4d6fc14bSjoerg        __f_->__clone(__t);
935*4d6fc14bSjoerg        __f_->destroy();
936*4d6fc14bSjoerg        __f_ = 0;
937*4d6fc14bSjoerg        __f.__f_->__clone((__base*)&__buf_);
938*4d6fc14bSjoerg        __f.__f_->destroy();
939*4d6fc14bSjoerg        __f.__f_ = 0;
940*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
941*4d6fc14bSjoerg        __t->__clone((__base*)&__f.__buf_);
942*4d6fc14bSjoerg        __t->destroy();
943*4d6fc14bSjoerg        __f.__f_ = (__base*)&__f.__buf_;
944*4d6fc14bSjoerg    }
945*4d6fc14bSjoerg    else if (__f_ == (__base*)&__buf_)
946*4d6fc14bSjoerg    {
947*4d6fc14bSjoerg        __f_->__clone((__base*)&__f.__buf_);
948*4d6fc14bSjoerg        __f_->destroy();
949*4d6fc14bSjoerg        __f_ = __f.__f_;
950*4d6fc14bSjoerg        __f.__f_ = (__base*)&__f.__buf_;
951*4d6fc14bSjoerg    }
952*4d6fc14bSjoerg    else if (__f.__f_ == (__base*)&__f.__buf_)
953*4d6fc14bSjoerg    {
954*4d6fc14bSjoerg        __f.__f_->__clone((__base*)&__buf_);
955*4d6fc14bSjoerg        __f.__f_->destroy();
956*4d6fc14bSjoerg        __f.__f_ = __f_;
957*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
958*4d6fc14bSjoerg    }
959*4d6fc14bSjoerg    else
960*4d6fc14bSjoerg        _VSTD::swap(__f_, __f.__f_);
961*4d6fc14bSjoerg}
962*4d6fc14bSjoerg
963*4d6fc14bSjoergtemplate<class _Rp, class _A0>
964*4d6fc14bSjoerg_Rp
965*4d6fc14bSjoergfunction<_Rp(_A0)>::operator()(_A0 __a0) const
966*4d6fc14bSjoerg{
967*4d6fc14bSjoerg    if (__f_ == 0)
968*4d6fc14bSjoerg        __throw_bad_function_call();
969*4d6fc14bSjoerg    return (*__f_)(__a0);
970*4d6fc14bSjoerg}
971*4d6fc14bSjoerg
972*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
973*4d6fc14bSjoerg
974*4d6fc14bSjoergtemplate<class _Rp, class _A0>
975*4d6fc14bSjoergconst std::type_info&
976*4d6fc14bSjoergfunction<_Rp(_A0)>::target_type() const
977*4d6fc14bSjoerg{
978*4d6fc14bSjoerg    if (__f_ == 0)
979*4d6fc14bSjoerg        return typeid(void);
980*4d6fc14bSjoerg    return __f_->target_type();
981*4d6fc14bSjoerg}
982*4d6fc14bSjoerg
983*4d6fc14bSjoergtemplate<class _Rp, class _A0>
984*4d6fc14bSjoergtemplate <typename _Tp>
985*4d6fc14bSjoerg_Tp*
986*4d6fc14bSjoergfunction<_Rp(_A0)>::target()
987*4d6fc14bSjoerg{
988*4d6fc14bSjoerg    if (__f_ == 0)
989*4d6fc14bSjoerg        return (_Tp*)0;
990*4d6fc14bSjoerg    return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
991*4d6fc14bSjoerg}
992*4d6fc14bSjoerg
993*4d6fc14bSjoergtemplate<class _Rp, class _A0>
994*4d6fc14bSjoergtemplate <typename _Tp>
995*4d6fc14bSjoergconst _Tp*
996*4d6fc14bSjoergfunction<_Rp(_A0)>::target() const
997*4d6fc14bSjoerg{
998*4d6fc14bSjoerg    if (__f_ == 0)
999*4d6fc14bSjoerg        return (const _Tp*)0;
1000*4d6fc14bSjoerg    return (const _Tp*)__f_->target(typeid(_Tp));
1001*4d6fc14bSjoerg}
1002*4d6fc14bSjoerg
1003*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
1004*4d6fc14bSjoerg
1005*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1006*4d6fc14bSjoergclass _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1)>
1007*4d6fc14bSjoerg    : public binary_function<_A0, _A1, _Rp>
1008*4d6fc14bSjoerg{
1009*4d6fc14bSjoerg    typedef __function::__base<_Rp(_A0, _A1)> __base;
1010*4d6fc14bSjoerg    aligned_storage<3*sizeof(void*)>::type __buf_;
1011*4d6fc14bSjoerg    __base* __f_;
1012*4d6fc14bSjoerg
1013*4d6fc14bSjoergpublic:
1014*4d6fc14bSjoerg    typedef _Rp result_type;
1015*4d6fc14bSjoerg
1016*4d6fc14bSjoerg    // 20.7.16.2.1, construct/copy/destroy:
1017*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1018*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1019*4d6fc14bSjoerg    function(const function&);
1020*4d6fc14bSjoerg    template<class _Fp>
1021*4d6fc14bSjoerg      function(_Fp,
1022*4d6fc14bSjoerg               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1023*4d6fc14bSjoerg
1024*4d6fc14bSjoerg    template<class _Alloc>
1025*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
1026*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1027*4d6fc14bSjoerg    template<class _Alloc>
1028*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
1029*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1030*4d6fc14bSjoerg    template<class _Alloc>
1031*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&, const function&);
1032*4d6fc14bSjoerg    template<class _Fp, class _Alloc>
1033*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1034*4d6fc14bSjoerg               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1035*4d6fc14bSjoerg
1036*4d6fc14bSjoerg    function& operator=(const function&);
1037*4d6fc14bSjoerg    function& operator=(nullptr_t);
1038*4d6fc14bSjoerg    template<class _Fp>
1039*4d6fc14bSjoerg      typename enable_if
1040*4d6fc14bSjoerg      <
1041*4d6fc14bSjoerg        !is_integral<_Fp>::value,
1042*4d6fc14bSjoerg        function&
1043*4d6fc14bSjoerg      >::type
1044*4d6fc14bSjoerg      operator=(_Fp);
1045*4d6fc14bSjoerg
1046*4d6fc14bSjoerg    ~function();
1047*4d6fc14bSjoerg
1048*4d6fc14bSjoerg    // 20.7.16.2.2, function modifiers:
1049*4d6fc14bSjoerg    void swap(function&);
1050*4d6fc14bSjoerg    template<class _Fp, class _Alloc>
1051*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
1052*4d6fc14bSjoerg      void assign(_Fp __f, const _Alloc& __a)
1053*4d6fc14bSjoerg        {function(allocator_arg, __a, __f).swap(*this);}
1054*4d6fc14bSjoerg
1055*4d6fc14bSjoerg    // 20.7.16.2.3, function capacity:
1056*4d6fc14bSjoerg    operator bool() const {return __f_;}
1057*4d6fc14bSjoerg
1058*4d6fc14bSjoergprivate:
1059*4d6fc14bSjoerg    // deleted overloads close possible hole in the type system
1060*4d6fc14bSjoerg    template<class _R2, class _B0, class _B1>
1061*4d6fc14bSjoerg      bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
1062*4d6fc14bSjoerg    template<class _R2, class _B0, class _B1>
1063*4d6fc14bSjoerg      bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
1064*4d6fc14bSjoergpublic:
1065*4d6fc14bSjoerg    // 20.7.16.2.4, function invocation:
1066*4d6fc14bSjoerg    _Rp operator()(_A0, _A1) const;
1067*4d6fc14bSjoerg
1068*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
1069*4d6fc14bSjoerg    // 20.7.16.2.5, function target access:
1070*4d6fc14bSjoerg    const std::type_info& target_type() const;
1071*4d6fc14bSjoerg    template <typename _Tp> _Tp* target();
1072*4d6fc14bSjoerg    template <typename _Tp> const _Tp* target() const;
1073*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
1074*4d6fc14bSjoerg};
1075*4d6fc14bSjoerg
1076*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1077*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::function(const function& __f)
1078*4d6fc14bSjoerg{
1079*4d6fc14bSjoerg    if (__f.__f_ == 0)
1080*4d6fc14bSjoerg        __f_ = 0;
1081*4d6fc14bSjoerg    else if (__f.__f_ == (const __base*)&__f.__buf_)
1082*4d6fc14bSjoerg    {
1083*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
1084*4d6fc14bSjoerg        __f.__f_->__clone(__f_);
1085*4d6fc14bSjoerg    }
1086*4d6fc14bSjoerg    else
1087*4d6fc14bSjoerg        __f_ = __f.__f_->__clone();
1088*4d6fc14bSjoerg}
1089*4d6fc14bSjoerg
1090*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1091*4d6fc14bSjoergtemplate<class _Alloc>
1092*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1093*4d6fc14bSjoerg{
1094*4d6fc14bSjoerg    if (__f.__f_ == 0)
1095*4d6fc14bSjoerg        __f_ = 0;
1096*4d6fc14bSjoerg    else if (__f.__f_ == (const __base*)&__f.__buf_)
1097*4d6fc14bSjoerg    {
1098*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
1099*4d6fc14bSjoerg        __f.__f_->__clone(__f_);
1100*4d6fc14bSjoerg    }
1101*4d6fc14bSjoerg    else
1102*4d6fc14bSjoerg        __f_ = __f.__f_->__clone();
1103*4d6fc14bSjoerg}
1104*4d6fc14bSjoerg
1105*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1106*4d6fc14bSjoergtemplate <class _Fp>
1107*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::function(_Fp __f,
1108*4d6fc14bSjoerg                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1109*4d6fc14bSjoerg    : __f_(0)
1110*4d6fc14bSjoerg{
1111*4d6fc14bSjoerg    if (__function::__not_null(__f))
1112*4d6fc14bSjoerg    {
1113*4d6fc14bSjoerg        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
1114*4d6fc14bSjoerg        if (sizeof(_FF) <= sizeof(__buf_))
1115*4d6fc14bSjoerg        {
1116*4d6fc14bSjoerg            __f_ = (__base*)&__buf_;
1117*4d6fc14bSjoerg            ::new ((void*)__f_) _FF(__f);
1118*4d6fc14bSjoerg        }
1119*4d6fc14bSjoerg        else
1120*4d6fc14bSjoerg        {
1121*4d6fc14bSjoerg            typedef allocator<_FF> _Ap;
1122*4d6fc14bSjoerg            _Ap __a;
1123*4d6fc14bSjoerg            typedef __allocator_destructor<_Ap> _Dp;
1124*4d6fc14bSjoerg            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1125*4d6fc14bSjoerg            ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
1126*4d6fc14bSjoerg            __f_ = __hold.release();
1127*4d6fc14bSjoerg        }
1128*4d6fc14bSjoerg    }
1129*4d6fc14bSjoerg}
1130*4d6fc14bSjoerg
1131*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1132*4d6fc14bSjoergtemplate <class _Fp, class _Alloc>
1133*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1134*4d6fc14bSjoerg                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1135*4d6fc14bSjoerg    : __f_(0)
1136*4d6fc14bSjoerg{
1137*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
1138*4d6fc14bSjoerg    if (__function::__not_null(__f))
1139*4d6fc14bSjoerg    {
1140*4d6fc14bSjoerg        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
1141*4d6fc14bSjoerg        if (sizeof(_FF) <= sizeof(__buf_))
1142*4d6fc14bSjoerg        {
1143*4d6fc14bSjoerg            __f_ = (__base*)&__buf_;
1144*4d6fc14bSjoerg            ::new ((void*)__f_) _FF(__f, __a0);
1145*4d6fc14bSjoerg        }
1146*4d6fc14bSjoerg        else
1147*4d6fc14bSjoerg        {
1148*4d6fc14bSjoerg            typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1149*4d6fc14bSjoerg            _Ap __a(__a0);
1150*4d6fc14bSjoerg            typedef __allocator_destructor<_Ap> _Dp;
1151*4d6fc14bSjoerg            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1152*4d6fc14bSjoerg            ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
1153*4d6fc14bSjoerg            __f_ = __hold.release();
1154*4d6fc14bSjoerg        }
1155*4d6fc14bSjoerg    }
1156*4d6fc14bSjoerg}
1157*4d6fc14bSjoerg
1158*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1159*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>&
1160*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::operator=(const function& __f)
1161*4d6fc14bSjoerg{
1162*4d6fc14bSjoerg    if (__f)
1163*4d6fc14bSjoerg        function(__f).swap(*this);
1164*4d6fc14bSjoerg    else
1165*4d6fc14bSjoerg        *this = nullptr;
1166*4d6fc14bSjoerg    return *this;
1167*4d6fc14bSjoerg}
1168*4d6fc14bSjoerg
1169*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1170*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>&
1171*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::operator=(nullptr_t)
1172*4d6fc14bSjoerg{
1173*4d6fc14bSjoerg    __base* __t = __f_;
1174*4d6fc14bSjoerg    __f_ = 0;
1175*4d6fc14bSjoerg    if (__t == (__base*)&__buf_)
1176*4d6fc14bSjoerg        __t->destroy();
1177*4d6fc14bSjoerg    else if (__t)
1178*4d6fc14bSjoerg        __t->destroy_deallocate();
1179*4d6fc14bSjoerg    return *this;
1180*4d6fc14bSjoerg}
1181*4d6fc14bSjoerg
1182*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1183*4d6fc14bSjoergtemplate <class _Fp>
1184*4d6fc14bSjoergtypename enable_if
1185*4d6fc14bSjoerg<
1186*4d6fc14bSjoerg    !is_integral<_Fp>::value,
1187*4d6fc14bSjoerg    function<_Rp(_A0, _A1)>&
1188*4d6fc14bSjoerg>::type
1189*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::operator=(_Fp __f)
1190*4d6fc14bSjoerg{
1191*4d6fc14bSjoerg    function(_VSTD::move(__f)).swap(*this);
1192*4d6fc14bSjoerg    return *this;
1193*4d6fc14bSjoerg}
1194*4d6fc14bSjoerg
1195*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1196*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::~function()
1197*4d6fc14bSjoerg{
1198*4d6fc14bSjoerg    if (__f_ == (__base*)&__buf_)
1199*4d6fc14bSjoerg        __f_->destroy();
1200*4d6fc14bSjoerg    else if (__f_)
1201*4d6fc14bSjoerg        __f_->destroy_deallocate();
1202*4d6fc14bSjoerg}
1203*4d6fc14bSjoerg
1204*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1205*4d6fc14bSjoergvoid
1206*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::swap(function& __f)
1207*4d6fc14bSjoerg{
1208*4d6fc14bSjoerg    if (_VSTD::addressof(__f) == this)
1209*4d6fc14bSjoerg      return;
1210*4d6fc14bSjoerg    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1211*4d6fc14bSjoerg    {
1212*4d6fc14bSjoerg        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1213*4d6fc14bSjoerg        __base* __t = (__base*)&__tempbuf;
1214*4d6fc14bSjoerg        __f_->__clone(__t);
1215*4d6fc14bSjoerg        __f_->destroy();
1216*4d6fc14bSjoerg        __f_ = 0;
1217*4d6fc14bSjoerg        __f.__f_->__clone((__base*)&__buf_);
1218*4d6fc14bSjoerg        __f.__f_->destroy();
1219*4d6fc14bSjoerg        __f.__f_ = 0;
1220*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
1221*4d6fc14bSjoerg        __t->__clone((__base*)&__f.__buf_);
1222*4d6fc14bSjoerg        __t->destroy();
1223*4d6fc14bSjoerg        __f.__f_ = (__base*)&__f.__buf_;
1224*4d6fc14bSjoerg    }
1225*4d6fc14bSjoerg    else if (__f_ == (__base*)&__buf_)
1226*4d6fc14bSjoerg    {
1227*4d6fc14bSjoerg        __f_->__clone((__base*)&__f.__buf_);
1228*4d6fc14bSjoerg        __f_->destroy();
1229*4d6fc14bSjoerg        __f_ = __f.__f_;
1230*4d6fc14bSjoerg        __f.__f_ = (__base*)&__f.__buf_;
1231*4d6fc14bSjoerg    }
1232*4d6fc14bSjoerg    else if (__f.__f_ == (__base*)&__f.__buf_)
1233*4d6fc14bSjoerg    {
1234*4d6fc14bSjoerg        __f.__f_->__clone((__base*)&__buf_);
1235*4d6fc14bSjoerg        __f.__f_->destroy();
1236*4d6fc14bSjoerg        __f.__f_ = __f_;
1237*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
1238*4d6fc14bSjoerg    }
1239*4d6fc14bSjoerg    else
1240*4d6fc14bSjoerg        _VSTD::swap(__f_, __f.__f_);
1241*4d6fc14bSjoerg}
1242*4d6fc14bSjoerg
1243*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1244*4d6fc14bSjoerg_Rp
1245*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1246*4d6fc14bSjoerg{
1247*4d6fc14bSjoerg    if (__f_ == 0)
1248*4d6fc14bSjoerg        __throw_bad_function_call();
1249*4d6fc14bSjoerg    return (*__f_)(__a0, __a1);
1250*4d6fc14bSjoerg}
1251*4d6fc14bSjoerg
1252*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
1253*4d6fc14bSjoerg
1254*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1255*4d6fc14bSjoergconst std::type_info&
1256*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::target_type() const
1257*4d6fc14bSjoerg{
1258*4d6fc14bSjoerg    if (__f_ == 0)
1259*4d6fc14bSjoerg        return typeid(void);
1260*4d6fc14bSjoerg    return __f_->target_type();
1261*4d6fc14bSjoerg}
1262*4d6fc14bSjoerg
1263*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1264*4d6fc14bSjoergtemplate <typename _Tp>
1265*4d6fc14bSjoerg_Tp*
1266*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::target()
1267*4d6fc14bSjoerg{
1268*4d6fc14bSjoerg    if (__f_ == 0)
1269*4d6fc14bSjoerg        return (_Tp*)0;
1270*4d6fc14bSjoerg    return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
1271*4d6fc14bSjoerg}
1272*4d6fc14bSjoerg
1273*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1>
1274*4d6fc14bSjoergtemplate <typename _Tp>
1275*4d6fc14bSjoergconst _Tp*
1276*4d6fc14bSjoergfunction<_Rp(_A0, _A1)>::target() const
1277*4d6fc14bSjoerg{
1278*4d6fc14bSjoerg    if (__f_ == 0)
1279*4d6fc14bSjoerg        return (const _Tp*)0;
1280*4d6fc14bSjoerg    return (const _Tp*)__f_->target(typeid(_Tp));
1281*4d6fc14bSjoerg}
1282*4d6fc14bSjoerg
1283*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
1284*4d6fc14bSjoerg
1285*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1286*4d6fc14bSjoergclass _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1, _A2)>
1287*4d6fc14bSjoerg{
1288*4d6fc14bSjoerg    typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
1289*4d6fc14bSjoerg    aligned_storage<3*sizeof(void*)>::type __buf_;
1290*4d6fc14bSjoerg    __base* __f_;
1291*4d6fc14bSjoerg
1292*4d6fc14bSjoergpublic:
1293*4d6fc14bSjoerg    typedef _Rp result_type;
1294*4d6fc14bSjoerg
1295*4d6fc14bSjoerg    // 20.7.16.2.1, construct/copy/destroy:
1296*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1297*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1298*4d6fc14bSjoerg    function(const function&);
1299*4d6fc14bSjoerg    template<class _Fp>
1300*4d6fc14bSjoerg      function(_Fp,
1301*4d6fc14bSjoerg               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1302*4d6fc14bSjoerg
1303*4d6fc14bSjoerg    template<class _Alloc>
1304*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
1305*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1306*4d6fc14bSjoerg    template<class _Alloc>
1307*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
1308*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1309*4d6fc14bSjoerg    template<class _Alloc>
1310*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc&, const function&);
1311*4d6fc14bSjoerg    template<class _Fp, class _Alloc>
1312*4d6fc14bSjoerg      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1313*4d6fc14bSjoerg               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1314*4d6fc14bSjoerg
1315*4d6fc14bSjoerg    function& operator=(const function&);
1316*4d6fc14bSjoerg    function& operator=(nullptr_t);
1317*4d6fc14bSjoerg    template<class _Fp>
1318*4d6fc14bSjoerg      typename enable_if
1319*4d6fc14bSjoerg      <
1320*4d6fc14bSjoerg        !is_integral<_Fp>::value,
1321*4d6fc14bSjoerg        function&
1322*4d6fc14bSjoerg      >::type
1323*4d6fc14bSjoerg      operator=(_Fp);
1324*4d6fc14bSjoerg
1325*4d6fc14bSjoerg    ~function();
1326*4d6fc14bSjoerg
1327*4d6fc14bSjoerg    // 20.7.16.2.2, function modifiers:
1328*4d6fc14bSjoerg    void swap(function&);
1329*4d6fc14bSjoerg    template<class _Fp, class _Alloc>
1330*4d6fc14bSjoerg      _LIBCPP_INLINE_VISIBILITY
1331*4d6fc14bSjoerg      void assign(_Fp __f, const _Alloc& __a)
1332*4d6fc14bSjoerg        {function(allocator_arg, __a, __f).swap(*this);}
1333*4d6fc14bSjoerg
1334*4d6fc14bSjoerg    // 20.7.16.2.3, function capacity:
1335*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1336*4d6fc14bSjoerg
1337*4d6fc14bSjoergprivate:
1338*4d6fc14bSjoerg    // deleted overloads close possible hole in the type system
1339*4d6fc14bSjoerg    template<class _R2, class _B0, class _B1, class _B2>
1340*4d6fc14bSjoerg      bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1341*4d6fc14bSjoerg    template<class _R2, class _B0, class _B1, class _B2>
1342*4d6fc14bSjoerg      bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1343*4d6fc14bSjoergpublic:
1344*4d6fc14bSjoerg    // 20.7.16.2.4, function invocation:
1345*4d6fc14bSjoerg    _Rp operator()(_A0, _A1, _A2) const;
1346*4d6fc14bSjoerg
1347*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
1348*4d6fc14bSjoerg    // 20.7.16.2.5, function target access:
1349*4d6fc14bSjoerg    const std::type_info& target_type() const;
1350*4d6fc14bSjoerg    template <typename _Tp> _Tp* target();
1351*4d6fc14bSjoerg    template <typename _Tp> const _Tp* target() const;
1352*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
1353*4d6fc14bSjoerg};
1354*4d6fc14bSjoerg
1355*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1356*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::function(const function& __f)
1357*4d6fc14bSjoerg{
1358*4d6fc14bSjoerg    if (__f.__f_ == 0)
1359*4d6fc14bSjoerg        __f_ = 0;
1360*4d6fc14bSjoerg    else if (__f.__f_ == (const __base*)&__f.__buf_)
1361*4d6fc14bSjoerg    {
1362*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
1363*4d6fc14bSjoerg        __f.__f_->__clone(__f_);
1364*4d6fc14bSjoerg    }
1365*4d6fc14bSjoerg    else
1366*4d6fc14bSjoerg        __f_ = __f.__f_->__clone();
1367*4d6fc14bSjoerg}
1368*4d6fc14bSjoerg
1369*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1370*4d6fc14bSjoergtemplate<class _Alloc>
1371*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
1372*4d6fc14bSjoerg                                      const function& __f)
1373*4d6fc14bSjoerg{
1374*4d6fc14bSjoerg    if (__f.__f_ == 0)
1375*4d6fc14bSjoerg        __f_ = 0;
1376*4d6fc14bSjoerg    else if (__f.__f_ == (const __base*)&__f.__buf_)
1377*4d6fc14bSjoerg    {
1378*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
1379*4d6fc14bSjoerg        __f.__f_->__clone(__f_);
1380*4d6fc14bSjoerg    }
1381*4d6fc14bSjoerg    else
1382*4d6fc14bSjoerg        __f_ = __f.__f_->__clone();
1383*4d6fc14bSjoerg}
1384*4d6fc14bSjoerg
1385*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1386*4d6fc14bSjoergtemplate <class _Fp>
1387*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1388*4d6fc14bSjoerg                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1389*4d6fc14bSjoerg    : __f_(0)
1390*4d6fc14bSjoerg{
1391*4d6fc14bSjoerg    if (__function::__not_null(__f))
1392*4d6fc14bSjoerg    {
1393*4d6fc14bSjoerg        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
1394*4d6fc14bSjoerg        if (sizeof(_FF) <= sizeof(__buf_))
1395*4d6fc14bSjoerg        {
1396*4d6fc14bSjoerg            __f_ = (__base*)&__buf_;
1397*4d6fc14bSjoerg            ::new ((void*)__f_) _FF(__f);
1398*4d6fc14bSjoerg        }
1399*4d6fc14bSjoerg        else
1400*4d6fc14bSjoerg        {
1401*4d6fc14bSjoerg            typedef allocator<_FF> _Ap;
1402*4d6fc14bSjoerg            _Ap __a;
1403*4d6fc14bSjoerg            typedef __allocator_destructor<_Ap> _Dp;
1404*4d6fc14bSjoerg            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1405*4d6fc14bSjoerg            ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a));
1406*4d6fc14bSjoerg            __f_ = __hold.release();
1407*4d6fc14bSjoerg        }
1408*4d6fc14bSjoerg    }
1409*4d6fc14bSjoerg}
1410*4d6fc14bSjoerg
1411*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1412*4d6fc14bSjoergtemplate <class _Fp, class _Alloc>
1413*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1414*4d6fc14bSjoerg                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1415*4d6fc14bSjoerg    : __f_(0)
1416*4d6fc14bSjoerg{
1417*4d6fc14bSjoerg    typedef allocator_traits<_Alloc> __alloc_traits;
1418*4d6fc14bSjoerg    if (__function::__not_null(__f))
1419*4d6fc14bSjoerg    {
1420*4d6fc14bSjoerg        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
1421*4d6fc14bSjoerg        if (sizeof(_FF) <= sizeof(__buf_))
1422*4d6fc14bSjoerg        {
1423*4d6fc14bSjoerg            __f_ = (__base*)&__buf_;
1424*4d6fc14bSjoerg            ::new ((void*)__f_) _FF(__f, __a0);
1425*4d6fc14bSjoerg        }
1426*4d6fc14bSjoerg        else
1427*4d6fc14bSjoerg        {
1428*4d6fc14bSjoerg            typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1429*4d6fc14bSjoerg            _Ap __a(__a0);
1430*4d6fc14bSjoerg            typedef __allocator_destructor<_Ap> _Dp;
1431*4d6fc14bSjoerg            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1432*4d6fc14bSjoerg            ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a));
1433*4d6fc14bSjoerg            __f_ = __hold.release();
1434*4d6fc14bSjoerg        }
1435*4d6fc14bSjoerg    }
1436*4d6fc14bSjoerg}
1437*4d6fc14bSjoerg
1438*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1439*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>&
1440*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
1441*4d6fc14bSjoerg{
1442*4d6fc14bSjoerg    if (__f)
1443*4d6fc14bSjoerg        function(__f).swap(*this);
1444*4d6fc14bSjoerg    else
1445*4d6fc14bSjoerg        *this = nullptr;
1446*4d6fc14bSjoerg    return *this;
1447*4d6fc14bSjoerg}
1448*4d6fc14bSjoerg
1449*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1450*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>&
1451*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
1452*4d6fc14bSjoerg{
1453*4d6fc14bSjoerg    __base* __t = __f_;
1454*4d6fc14bSjoerg    __f_ = 0;
1455*4d6fc14bSjoerg    if (__t == (__base*)&__buf_)
1456*4d6fc14bSjoerg        __t->destroy();
1457*4d6fc14bSjoerg    else if (__t)
1458*4d6fc14bSjoerg        __t->destroy_deallocate();
1459*4d6fc14bSjoerg    return *this;
1460*4d6fc14bSjoerg}
1461*4d6fc14bSjoerg
1462*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1463*4d6fc14bSjoergtemplate <class _Fp>
1464*4d6fc14bSjoergtypename enable_if
1465*4d6fc14bSjoerg<
1466*4d6fc14bSjoerg    !is_integral<_Fp>::value,
1467*4d6fc14bSjoerg    function<_Rp(_A0, _A1, _A2)>&
1468*4d6fc14bSjoerg>::type
1469*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
1470*4d6fc14bSjoerg{
1471*4d6fc14bSjoerg    function(_VSTD::move(__f)).swap(*this);
1472*4d6fc14bSjoerg    return *this;
1473*4d6fc14bSjoerg}
1474*4d6fc14bSjoerg
1475*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1476*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::~function()
1477*4d6fc14bSjoerg{
1478*4d6fc14bSjoerg    if (__f_ == (__base*)&__buf_)
1479*4d6fc14bSjoerg        __f_->destroy();
1480*4d6fc14bSjoerg    else if (__f_)
1481*4d6fc14bSjoerg        __f_->destroy_deallocate();
1482*4d6fc14bSjoerg}
1483*4d6fc14bSjoerg
1484*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1485*4d6fc14bSjoergvoid
1486*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::swap(function& __f)
1487*4d6fc14bSjoerg{
1488*4d6fc14bSjoerg    if (_VSTD::addressof(__f) == this)
1489*4d6fc14bSjoerg      return;
1490*4d6fc14bSjoerg    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1491*4d6fc14bSjoerg    {
1492*4d6fc14bSjoerg        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1493*4d6fc14bSjoerg        __base* __t = (__base*)&__tempbuf;
1494*4d6fc14bSjoerg        __f_->__clone(__t);
1495*4d6fc14bSjoerg        __f_->destroy();
1496*4d6fc14bSjoerg        __f_ = 0;
1497*4d6fc14bSjoerg        __f.__f_->__clone((__base*)&__buf_);
1498*4d6fc14bSjoerg        __f.__f_->destroy();
1499*4d6fc14bSjoerg        __f.__f_ = 0;
1500*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
1501*4d6fc14bSjoerg        __t->__clone((__base*)&__f.__buf_);
1502*4d6fc14bSjoerg        __t->destroy();
1503*4d6fc14bSjoerg        __f.__f_ = (__base*)&__f.__buf_;
1504*4d6fc14bSjoerg    }
1505*4d6fc14bSjoerg    else if (__f_ == (__base*)&__buf_)
1506*4d6fc14bSjoerg    {
1507*4d6fc14bSjoerg        __f_->__clone((__base*)&__f.__buf_);
1508*4d6fc14bSjoerg        __f_->destroy();
1509*4d6fc14bSjoerg        __f_ = __f.__f_;
1510*4d6fc14bSjoerg        __f.__f_ = (__base*)&__f.__buf_;
1511*4d6fc14bSjoerg    }
1512*4d6fc14bSjoerg    else if (__f.__f_ == (__base*)&__f.__buf_)
1513*4d6fc14bSjoerg    {
1514*4d6fc14bSjoerg        __f.__f_->__clone((__base*)&__buf_);
1515*4d6fc14bSjoerg        __f.__f_->destroy();
1516*4d6fc14bSjoerg        __f.__f_ = __f_;
1517*4d6fc14bSjoerg        __f_ = (__base*)&__buf_;
1518*4d6fc14bSjoerg    }
1519*4d6fc14bSjoerg    else
1520*4d6fc14bSjoerg        _VSTD::swap(__f_, __f.__f_);
1521*4d6fc14bSjoerg}
1522*4d6fc14bSjoerg
1523*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1524*4d6fc14bSjoerg_Rp
1525*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1526*4d6fc14bSjoerg{
1527*4d6fc14bSjoerg    if (__f_ == 0)
1528*4d6fc14bSjoerg        __throw_bad_function_call();
1529*4d6fc14bSjoerg    return (*__f_)(__a0, __a1, __a2);
1530*4d6fc14bSjoerg}
1531*4d6fc14bSjoerg
1532*4d6fc14bSjoerg#ifndef _LIBCPP_NO_RTTI
1533*4d6fc14bSjoerg
1534*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1535*4d6fc14bSjoergconst std::type_info&
1536*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::target_type() const
1537*4d6fc14bSjoerg{
1538*4d6fc14bSjoerg    if (__f_ == 0)
1539*4d6fc14bSjoerg        return typeid(void);
1540*4d6fc14bSjoerg    return __f_->target_type();
1541*4d6fc14bSjoerg}
1542*4d6fc14bSjoerg
1543*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1544*4d6fc14bSjoergtemplate <typename _Tp>
1545*4d6fc14bSjoerg_Tp*
1546*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::target()
1547*4d6fc14bSjoerg{
1548*4d6fc14bSjoerg    if (__f_ == 0)
1549*4d6fc14bSjoerg        return (_Tp*)0;
1550*4d6fc14bSjoerg    return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
1551*4d6fc14bSjoerg}
1552*4d6fc14bSjoerg
1553*4d6fc14bSjoergtemplate<class _Rp, class _A0, class _A1, class _A2>
1554*4d6fc14bSjoergtemplate <typename _Tp>
1555*4d6fc14bSjoergconst _Tp*
1556*4d6fc14bSjoergfunction<_Rp(_A0, _A1, _A2)>::target() const
1557*4d6fc14bSjoerg{
1558*4d6fc14bSjoerg    if (__f_ == 0)
1559*4d6fc14bSjoerg        return (const _Tp*)0;
1560*4d6fc14bSjoerg    return (const _Tp*)__f_->target(typeid(_Tp));
1561*4d6fc14bSjoerg}
1562*4d6fc14bSjoerg
1563*4d6fc14bSjoerg#endif // _LIBCPP_NO_RTTI
1564*4d6fc14bSjoerg
1565*4d6fc14bSjoergtemplate <class _Fp>
1566*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1567*4d6fc14bSjoergbool
1568*4d6fc14bSjoergoperator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
1569*4d6fc14bSjoerg
1570*4d6fc14bSjoergtemplate <class _Fp>
1571*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1572*4d6fc14bSjoergbool
1573*4d6fc14bSjoergoperator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
1574*4d6fc14bSjoerg
1575*4d6fc14bSjoergtemplate <class _Fp>
1576*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1577*4d6fc14bSjoergbool
1578*4d6fc14bSjoergoperator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
1579*4d6fc14bSjoerg
1580*4d6fc14bSjoergtemplate <class _Fp>
1581*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1582*4d6fc14bSjoergbool
1583*4d6fc14bSjoergoperator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
1584*4d6fc14bSjoerg
1585*4d6fc14bSjoergtemplate <class _Fp>
1586*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1587*4d6fc14bSjoergvoid
1588*4d6fc14bSjoergswap(function<_Fp>& __x, function<_Fp>& __y)
1589*4d6fc14bSjoerg{return __x.swap(__y);}
1590*4d6fc14bSjoerg
1591*4d6fc14bSjoerg#endif // _LIBCPP_FUNCTIONAL_03
1592