__functional_03 revision 261272
1227825Stheraven// -*- C++ -*-
2227825Stheraven//===----------------------------------------------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_FUNCTIONAL_03
12227825Stheraven#define _LIBCPP_FUNCTIONAL_03
13227825Stheraven
14227825Stheraven// manual variadic expansion for <functional>
15227825Stheraven
16227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17227825Stheraven#pragma GCC system_header
18227825Stheraven#endif
19227825Stheraven
20227825Stheraventemplate <class _Tp>
21227825Stheravenclass __mem_fn
22227825Stheraven    : public __weak_result_type<_Tp>
23227825Stheraven{
24227825Stheravenpublic:
25227825Stheraven    // types
26227825Stheraven    typedef _Tp type;
27227825Stheravenprivate:
28227825Stheraven    type __f_;
29227825Stheraven
30227825Stheravenpublic:
31227825Stheraven    _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
32227825Stheraven
33227825Stheraven    // invoke
34227825Stheraven
35227825Stheraven    typename __invoke_return<type>::type
36227825Stheraven       operator() ()
37227825Stheraven       {
38227825Stheraven           return __invoke(__f_);
39227825Stheraven       }
40227825Stheraven
41227825Stheraven    template <class _A0>
42227825Stheraven       typename __invoke_return0<type, _A0>::type
43227825Stheraven          operator() (_A0& __a0)
44227825Stheraven          {
45227825Stheraven              return __invoke(__f_, __a0);
46227825Stheraven          }
47227825Stheraven
48227825Stheraven    template <class _A0, class _A1>
49227825Stheraven       typename __invoke_return1<type, _A0, _A1>::type
50227825Stheraven          operator() (_A0& __a0, _A1& __a1)
51227825Stheraven          {
52227825Stheraven              return __invoke(__f_, __a0, __a1);
53227825Stheraven          }
54227825Stheraven
55227825Stheraven    template <class _A0, class _A1, class _A2>
56227825Stheraven       typename __invoke_return2<type, _A0, _A1, _A2>::type
57227825Stheraven          operator() (_A0& __a0, _A1& __a1, _A2& __a2)
58227825Stheraven          {
59227825Stheraven              return __invoke(__f_, __a0, __a1, __a2);
60227825Stheraven          }
61227825Stheraven};
62227825Stheraven
63232924Stheraventemplate<class _Rp, class _Tp>
64227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
65232924Stheraven__mem_fn<_Rp _Tp::*>
66232924Stheravenmem_fn(_Rp _Tp::* __pm)
67227825Stheraven{
68232924Stheraven    return __mem_fn<_Rp _Tp::*>(__pm);
69227825Stheraven}
70227825Stheraven
71232924Stheraventemplate<class _Rp, class _Tp>
72227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
73232924Stheraven__mem_fn<_Rp (_Tp::*)()>
74232924Stheravenmem_fn(_Rp (_Tp::* __pm)())
75227825Stheraven{
76232924Stheraven    return __mem_fn<_Rp (_Tp::*)()>(__pm);
77227825Stheraven}
78227825Stheraven
79232924Stheraventemplate<class _Rp, class _Tp, class _A0>
80227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
81232924Stheraven__mem_fn<_Rp (_Tp::*)(_A0)>
82232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0))
83227825Stheraven{
84232924Stheraven    return __mem_fn<_Rp (_Tp::*)(_A0)>(__pm);
85227825Stheraven}
86227825Stheraven
87232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
88227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
89232924Stheraven__mem_fn<_Rp (_Tp::*)(_A0, _A1)>
90232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1))
91227825Stheraven{
92232924Stheraven    return __mem_fn<_Rp (_Tp::*)(_A0, _A1)>(__pm);
93227825Stheraven}
94227825Stheraven
95232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
96227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
97232924Stheraven__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>
98232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2))
99227825Stheraven{
100232924Stheraven    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>(__pm);
101227825Stheraven}
102227825Stheraven
103232924Stheraventemplate<class _Rp, class _Tp>
104227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
105261272Sdim__mem_fn<_Rp (_Tp::*)() const>
106232924Stheravenmem_fn(_Rp (_Tp::* __pm)() const)
107227825Stheraven{
108261272Sdim    return __mem_fn<_Rp (_Tp::*)() const>(__pm);
109227825Stheraven}
110227825Stheraven
111232924Stheraventemplate<class _Rp, class _Tp, class _A0>
112227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
113261272Sdim__mem_fn<_Rp (_Tp::*)(_A0) const>
114232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0) const)
115227825Stheraven{
116261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0) const>(__pm);
117227825Stheraven}
118227825Stheraven
119232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
120227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
121261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1) const>
122232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const)
123227825Stheraven{
124261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const>(__pm);
125227825Stheraven}
126227825Stheraven
127232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
128227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
129261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>
130232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const)
131227825Stheraven{
132261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>(__pm);
133227825Stheraven}
134227825Stheraven
135232924Stheraventemplate<class _Rp, class _Tp>
136227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
137261272Sdim__mem_fn<_Rp (_Tp::*)() volatile>
138232924Stheravenmem_fn(_Rp (_Tp::* __pm)() volatile)
139227825Stheraven{
140261272Sdim    return __mem_fn<_Rp (_Tp::*)() volatile>(__pm);
141227825Stheraven}
142227825Stheraven
143232924Stheraventemplate<class _Rp, class _Tp, class _A0>
144227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
145261272Sdim__mem_fn<_Rp (_Tp::*)(_A0) volatile>
146232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0) volatile)
147227825Stheraven{
148261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0) volatile>(__pm);
149227825Stheraven}
150227825Stheraven
151232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
152227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
153261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>
154232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1) volatile)
155227825Stheraven{
156261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>(__pm);
157227825Stheraven}
158227825Stheraven
159232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
160227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
161261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>
162232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) volatile)
163227825Stheraven{
164261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>(__pm);
165227825Stheraven}
166227825Stheraven
167232924Stheraventemplate<class _Rp, class _Tp>
168227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
169261272Sdim__mem_fn<_Rp (_Tp::*)() const volatile>
170232924Stheravenmem_fn(_Rp (_Tp::* __pm)() const volatile)
171227825Stheraven{
172261272Sdim    return __mem_fn<_Rp (_Tp::*)() const volatile>(__pm);
173227825Stheraven}
174227825Stheraven
175232924Stheraventemplate<class _Rp, class _Tp, class _A0>
176227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
177261272Sdim__mem_fn<_Rp (_Tp::*)(_A0) const volatile>
178232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0) const volatile)
179227825Stheraven{
180261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0) const volatile>(__pm);
181227825Stheraven}
182227825Stheraven
183232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
184227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
185261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>
186232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const volatile)
187227825Stheraven{
188261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>(__pm);
189227825Stheraven}
190227825Stheraven
191232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
192227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
193261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>
194232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const volatile)
195227825Stheraven{
196261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>(__pm);
197227825Stheraven}
198227825Stheraven
199227825Stheraven// bad_function_call
200227825Stheraven
201227825Stheravenclass _LIBCPP_EXCEPTION_ABI bad_function_call
202227825Stheraven    : public exception
203227825Stheraven{
204227825Stheraven};
205227825Stheraven
206261272Sdimtemplate<class _Fp> class _LIBCPP_TYPE_VIS_ONLY function; // undefined
207227825Stheraven
208227825Stheravennamespace __function
209227825Stheraven{
210227825Stheraven
211232924Stheraventemplate<class _Fp>
212227825Stheravenstruct __maybe_derive_from_unary_function
213227825Stheraven{
214227825Stheraven};
215227825Stheraven
216232924Stheraventemplate<class _Rp, class _A1>
217232924Stheravenstruct __maybe_derive_from_unary_function<_Rp(_A1)>
218232924Stheraven    : public unary_function<_A1, _Rp>
219227825Stheraven{
220227825Stheraven};
221227825Stheraven
222232924Stheraventemplate<class _Fp>
223227825Stheravenstruct __maybe_derive_from_binary_function
224227825Stheraven{
225227825Stheraven};
226227825Stheraven
227232924Stheraventemplate<class _Rp, class _A1, class _A2>
228232924Stheravenstruct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
229232924Stheraven    : public binary_function<_A1, _A2, _Rp>
230227825Stheraven{
231227825Stheraven};
232227825Stheraven
233227825Stheraventemplate<class _Fp> class __base;
234227825Stheraven
235232924Stheraventemplate<class _Rp>
236232924Stheravenclass __base<_Rp()>
237227825Stheraven{
238227825Stheraven    __base(const __base&);
239227825Stheraven    __base& operator=(const __base&);
240227825Stheravenpublic:
241227825Stheraven    __base() {}
242227825Stheraven    virtual ~__base() {}
243227825Stheraven    virtual __base* __clone() const = 0;
244227825Stheraven    virtual void __clone(__base*) const = 0;
245227825Stheraven    virtual void destroy() = 0;
246227825Stheraven    virtual void destroy_deallocate() = 0;
247232924Stheraven    virtual _Rp operator()() = 0;
248227825Stheraven#ifndef _LIBCPP_NO_RTTI
249227825Stheraven    virtual const void* target(const type_info&) const = 0;
250227825Stheraven    virtual const std::type_info& target_type() const = 0;
251227825Stheraven#endif  // _LIBCPP_NO_RTTI
252227825Stheraven};
253227825Stheraven
254232924Stheraventemplate<class _Rp, class _A0>
255232924Stheravenclass __base<_Rp(_A0)>
256227825Stheraven{
257227825Stheraven    __base(const __base&);
258227825Stheraven    __base& operator=(const __base&);
259227825Stheravenpublic:
260227825Stheraven    __base() {}
261227825Stheraven    virtual ~__base() {}
262227825Stheraven    virtual __base* __clone() const = 0;
263227825Stheraven    virtual void __clone(__base*) const = 0;
264227825Stheraven    virtual void destroy() = 0;
265227825Stheraven    virtual void destroy_deallocate() = 0;
266232924Stheraven    virtual _Rp operator()(_A0) = 0;
267227825Stheraven#ifndef _LIBCPP_NO_RTTI
268227825Stheraven    virtual const void* target(const type_info&) const = 0;
269227825Stheraven    virtual const std::type_info& target_type() const = 0;
270227825Stheraven#endif  // _LIBCPP_NO_RTTI
271227825Stheraven};
272227825Stheraven
273232924Stheraventemplate<class _Rp, class _A0, class _A1>
274232924Stheravenclass __base<_Rp(_A0, _A1)>
275227825Stheraven{
276227825Stheraven    __base(const __base&);
277227825Stheraven    __base& operator=(const __base&);
278227825Stheravenpublic:
279227825Stheraven    __base() {}
280227825Stheraven    virtual ~__base() {}
281227825Stheraven    virtual __base* __clone() const = 0;
282227825Stheraven    virtual void __clone(__base*) const = 0;
283227825Stheraven    virtual void destroy() = 0;
284227825Stheraven    virtual void destroy_deallocate() = 0;
285232924Stheraven    virtual _Rp operator()(_A0, _A1) = 0;
286227825Stheraven#ifndef _LIBCPP_NO_RTTI
287227825Stheraven    virtual const void* target(const type_info&) const = 0;
288227825Stheraven    virtual const std::type_info& target_type() const = 0;
289227825Stheraven#endif  // _LIBCPP_NO_RTTI
290227825Stheraven};
291227825Stheraven
292232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
293232924Stheravenclass __base<_Rp(_A0, _A1, _A2)>
294227825Stheraven{
295227825Stheraven    __base(const __base&);
296227825Stheraven    __base& operator=(const __base&);
297227825Stheravenpublic:
298227825Stheraven    __base() {}
299227825Stheraven    virtual ~__base() {}
300227825Stheraven    virtual __base* __clone() const = 0;
301227825Stheraven    virtual void __clone(__base*) const = 0;
302227825Stheraven    virtual void destroy() = 0;
303227825Stheraven    virtual void destroy_deallocate() = 0;
304232924Stheraven    virtual _Rp operator()(_A0, _A1, _A2) = 0;
305227825Stheraven#ifndef _LIBCPP_NO_RTTI
306227825Stheraven    virtual const void* target(const type_info&) const = 0;
307227825Stheraven    virtual const std::type_info& target_type() const = 0;
308227825Stheraven#endif  // _LIBCPP_NO_RTTI
309227825Stheraven};
310227825Stheraven
311227825Stheraventemplate<class _FD, class _Alloc, class _FB> class __func;
312227825Stheraven
313232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
314232924Stheravenclass __func<_Fp, _Alloc, _Rp()>
315232924Stheraven    : public  __base<_Rp()>
316227825Stheraven{
317232924Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
318227825Stheravenpublic:
319232924Stheraven    explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
320232924Stheraven    explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
321232924Stheraven    virtual __base<_Rp()>* __clone() const;
322232924Stheraven    virtual void __clone(__base<_Rp()>*) const;
323227825Stheraven    virtual void destroy();
324227825Stheraven    virtual void destroy_deallocate();
325232924Stheraven    virtual _Rp operator()();
326227825Stheraven#ifndef _LIBCPP_NO_RTTI
327227825Stheraven    virtual const void* target(const type_info&) const;
328227825Stheraven    virtual const std::type_info& target_type() const;
329227825Stheraven#endif  // _LIBCPP_NO_RTTI
330227825Stheraven};
331227825Stheraven
332232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
333232924Stheraven__base<_Rp()>*
334232924Stheraven__func<_Fp, _Alloc, _Rp()>::__clone() const
335227825Stheraven{
336232924Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
337232924Stheraven    _Ap __a(__f_.second());
338232924Stheraven    typedef __allocator_destructor<_Ap> _Dp;
339232924Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
340227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
341227825Stheraven    return __hold.release();
342227825Stheraven}
343227825Stheraven
344232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
345227825Stheravenvoid
346232924Stheraven__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
347227825Stheraven{
348227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
349227825Stheraven}
350227825Stheraven
351232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
352227825Stheravenvoid
353232924Stheraven__func<_Fp, _Alloc, _Rp()>::destroy()
354227825Stheraven{
355232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
356227825Stheraven}
357227825Stheraven
358232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
359227825Stheravenvoid
360232924Stheraven__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
361227825Stheraven{
362232924Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
363232924Stheraven    _Ap __a(__f_.second());
364232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
365227825Stheraven    __a.deallocate(this, 1);
366227825Stheraven}
367227825Stheraven
368232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
369232924Stheraven_Rp
370232924Stheraven__func<_Fp, _Alloc, _Rp()>::operator()()
371227825Stheraven{
372227825Stheraven    return __invoke(__f_.first());
373227825Stheraven}
374227825Stheraven
375227825Stheraven#ifndef _LIBCPP_NO_RTTI
376227825Stheraven
377232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
378227825Stheravenconst void*
379232924Stheraven__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
380227825Stheraven{
381232924Stheraven    if (__ti == typeid(_Fp))
382227825Stheraven        return &__f_.first();
383227825Stheraven    return (const void*)0;
384227825Stheraven}
385227825Stheraven
386232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
387227825Stheravenconst std::type_info&
388232924Stheraven__func<_Fp, _Alloc, _Rp()>::target_type() const
389227825Stheraven{
390232924Stheraven    return typeid(_Fp);
391227825Stheraven}
392227825Stheraven
393227825Stheraven#endif  // _LIBCPP_NO_RTTI
394227825Stheraven
395232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
396232924Stheravenclass __func<_Fp, _Alloc, _Rp(_A0)>
397232924Stheraven    : public  __base<_Rp(_A0)>
398227825Stheraven{
399232924Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
400227825Stheravenpublic:
401232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
402232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
403227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
404232924Stheraven    virtual __base<_Rp(_A0)>* __clone() const;
405232924Stheraven    virtual void __clone(__base<_Rp(_A0)>*) const;
406227825Stheraven    virtual void destroy();
407227825Stheraven    virtual void destroy_deallocate();
408232924Stheraven    virtual _Rp operator()(_A0);
409227825Stheraven#ifndef _LIBCPP_NO_RTTI
410227825Stheraven    virtual const void* target(const type_info&) const;
411227825Stheraven    virtual const std::type_info& target_type() const;
412227825Stheraven#endif  // _LIBCPP_NO_RTTI
413227825Stheraven};
414227825Stheraven
415232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
416232924Stheraven__base<_Rp(_A0)>*
417232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
418227825Stheraven{
419232924Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
420232924Stheraven    _Ap __a(__f_.second());
421232924Stheraven    typedef __allocator_destructor<_Ap> _Dp;
422232924Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
423227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
424227825Stheraven    return __hold.release();
425227825Stheraven}
426227825Stheraven
427232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
428227825Stheravenvoid
429232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
430227825Stheraven{
431227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
432227825Stheraven}
433227825Stheraven
434232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
435227825Stheravenvoid
436232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
437227825Stheraven{
438232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
439227825Stheraven}
440227825Stheraven
441232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
442227825Stheravenvoid
443232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
444227825Stheraven{
445232924Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
446232924Stheraven    _Ap __a(__f_.second());
447232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
448227825Stheraven    __a.deallocate(this, 1);
449227825Stheraven}
450227825Stheraven
451232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
452232924Stheraven_Rp
453232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
454227825Stheraven{
455227825Stheraven    return __invoke(__f_.first(), __a0);
456227825Stheraven}
457227825Stheraven
458227825Stheraven#ifndef _LIBCPP_NO_RTTI
459227825Stheraven
460232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
461227825Stheravenconst void*
462232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
463227825Stheraven{
464232924Stheraven    if (__ti == typeid(_Fp))
465227825Stheraven        return &__f_.first();
466227825Stheraven    return (const void*)0;
467227825Stheraven}
468227825Stheraven
469232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
470227825Stheravenconst std::type_info&
471232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
472227825Stheraven{
473232924Stheraven    return typeid(_Fp);
474227825Stheraven}
475227825Stheraven
476227825Stheraven#endif  // _LIBCPP_NO_RTTI
477227825Stheraven
478232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
479232924Stheravenclass __func<_Fp, _Alloc, _Rp(_A0, _A1)>
480232924Stheraven    : public  __base<_Rp(_A0, _A1)>
481227825Stheraven{
482232924Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
483227825Stheravenpublic:
484232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
485232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
486227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
487232924Stheraven    virtual __base<_Rp(_A0, _A1)>* __clone() const;
488232924Stheraven    virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
489227825Stheraven    virtual void destroy();
490227825Stheraven    virtual void destroy_deallocate();
491232924Stheraven    virtual _Rp operator()(_A0, _A1);
492227825Stheraven#ifndef _LIBCPP_NO_RTTI
493227825Stheraven    virtual const void* target(const type_info&) const;
494227825Stheraven    virtual const std::type_info& target_type() const;
495227825Stheraven#endif  // _LIBCPP_NO_RTTI
496227825Stheraven};
497227825Stheraven
498232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
499232924Stheraven__base<_Rp(_A0, _A1)>*
500232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
501227825Stheraven{
502232924Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
503232924Stheraven    _Ap __a(__f_.second());
504232924Stheraven    typedef __allocator_destructor<_Ap> _Dp;
505232924Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
506227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
507227825Stheraven    return __hold.release();
508227825Stheraven}
509227825Stheraven
510232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
511227825Stheravenvoid
512232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
513227825Stheraven{
514227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
515227825Stheraven}
516227825Stheraven
517232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
518227825Stheravenvoid
519232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
520227825Stheraven{
521232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
522227825Stheraven}
523227825Stheraven
524232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
525227825Stheravenvoid
526232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
527227825Stheraven{
528232924Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
529232924Stheraven    _Ap __a(__f_.second());
530232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
531227825Stheraven    __a.deallocate(this, 1);
532227825Stheraven}
533227825Stheraven
534232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
535232924Stheraven_Rp
536232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
537227825Stheraven{
538227825Stheraven    return __invoke(__f_.first(), __a0, __a1);
539227825Stheraven}
540227825Stheraven
541227825Stheraven#ifndef _LIBCPP_NO_RTTI
542227825Stheraven
543232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
544227825Stheravenconst void*
545232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
546227825Stheraven{
547232924Stheraven    if (__ti == typeid(_Fp))
548227825Stheraven        return &__f_.first();
549227825Stheraven    return (const void*)0;
550227825Stheraven}
551227825Stheraven
552232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
553227825Stheravenconst std::type_info&
554232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
555227825Stheraven{
556232924Stheraven    return typeid(_Fp);
557227825Stheraven}
558227825Stheraven
559227825Stheraven#endif  // _LIBCPP_NO_RTTI
560227825Stheraven
561232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
562232924Stheravenclass __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
563232924Stheraven    : public  __base<_Rp(_A0, _A1, _A2)>
564227825Stheraven{
565232924Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
566227825Stheravenpublic:
567232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
568232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
569227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
570232924Stheraven    virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
571232924Stheraven    virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
572227825Stheraven    virtual void destroy();
573227825Stheraven    virtual void destroy_deallocate();
574232924Stheraven    virtual _Rp operator()(_A0, _A1, _A2);
575227825Stheraven#ifndef _LIBCPP_NO_RTTI
576227825Stheraven    virtual const void* target(const type_info&) const;
577227825Stheraven    virtual const std::type_info& target_type() const;
578227825Stheraven#endif  // _LIBCPP_NO_RTTI
579227825Stheraven};
580227825Stheraven
581232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
582232924Stheraven__base<_Rp(_A0, _A1, _A2)>*
583232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
584227825Stheraven{
585232924Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
586232924Stheraven    _Ap __a(__f_.second());
587232924Stheraven    typedef __allocator_destructor<_Ap> _Dp;
588232924Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
589227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
590227825Stheraven    return __hold.release();
591227825Stheraven}
592227825Stheraven
593232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
594227825Stheravenvoid
595232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
596227825Stheraven{
597227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
598227825Stheraven}
599227825Stheraven
600232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
601227825Stheravenvoid
602232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
603227825Stheraven{
604232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
605227825Stheraven}
606227825Stheraven
607232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
608227825Stheravenvoid
609232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
610227825Stheraven{
611232924Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
612232924Stheraven    _Ap __a(__f_.second());
613232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
614227825Stheraven    __a.deallocate(this, 1);
615227825Stheraven}
616227825Stheraven
617232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
618232924Stheraven_Rp
619232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
620227825Stheraven{
621227825Stheraven    return __invoke(__f_.first(), __a0, __a1, __a2);
622227825Stheraven}
623227825Stheraven
624227825Stheraven#ifndef _LIBCPP_NO_RTTI
625227825Stheraven
626232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
627227825Stheravenconst void*
628232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
629227825Stheraven{
630232924Stheraven    if (__ti == typeid(_Fp))
631227825Stheraven        return &__f_.first();
632227825Stheraven    return (const void*)0;
633227825Stheraven}
634227825Stheraven
635232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
636227825Stheravenconst std::type_info&
637232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
638227825Stheraven{
639232924Stheraven    return typeid(_Fp);
640227825Stheraven}
641227825Stheraven
642227825Stheraven#endif  // _LIBCPP_NO_RTTI
643227825Stheraven
644227825Stheraven}  // __function
645227825Stheraven
646232924Stheraventemplate<class _Rp>
647261272Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
648227825Stheraven{
649232924Stheraven    typedef __function::__base<_Rp()> __base;
650227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
651227825Stheraven    __base* __f_;
652227825Stheraven
653232924Stheraven    template <class _Fp>
654232924Stheraven        static bool __not_null(const _Fp&) {return true;}
655227825Stheraven    template <class _R2>
656232924Stheraven        static bool __not_null(const function<_Rp()>& __p) {return __p;}
657227825Stheravenpublic:
658232924Stheraven    typedef _Rp result_type;
659227825Stheraven
660227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
661227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
662227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
663227825Stheraven    function(const function&);
664232924Stheraven    template<class _Fp>
665232924Stheraven      function(_Fp,
666232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
667227825Stheraven
668227825Stheraven    template<class _Alloc>
669227825Stheraven      _LIBCPP_INLINE_VISIBILITY
670227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
671227825Stheraven    template<class _Alloc>
672227825Stheraven      _LIBCPP_INLINE_VISIBILITY
673227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
674227825Stheraven    template<class _Alloc>
675227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
676232924Stheraven    template<class _Fp, class _Alloc>
677232924Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
678232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
679227825Stheraven
680227825Stheraven    function& operator=(const function&);
681227825Stheraven    function& operator=(nullptr_t);
682232924Stheraven    template<class _Fp>
683227825Stheraven      typename enable_if
684227825Stheraven      <
685232924Stheraven        !is_integral<_Fp>::value,
686227825Stheraven        function&
687227825Stheraven      >::type
688232924Stheraven      operator=(_Fp);
689227825Stheraven
690227825Stheraven    ~function();
691227825Stheraven
692227825Stheraven    // 20.7.16.2.2, function modifiers:
693227825Stheraven    void swap(function&);
694232924Stheraven    template<class _Fp, class _Alloc>
695227825Stheraven      _LIBCPP_INLINE_VISIBILITY
696232924Stheraven      void assign(_Fp __f, const _Alloc& __a)
697227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
698227825Stheraven
699227825Stheraven    // 20.7.16.2.3, function capacity:
700227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
701227825Stheraven
702227825Stheravenprivate:
703227825Stheraven    // deleted overloads close possible hole in the type system
704227825Stheraven    template<class _R2>
705227825Stheraven      bool operator==(const function<_R2()>&) const;// = delete;
706227825Stheraven    template<class _R2>
707227825Stheraven      bool operator!=(const function<_R2()>&) const;// = delete;
708227825Stheravenpublic:
709227825Stheraven    // 20.7.16.2.4, function invocation:
710232924Stheraven    _Rp operator()() const;
711227825Stheraven
712227825Stheraven#ifndef _LIBCPP_NO_RTTI
713227825Stheraven    // 20.7.16.2.5, function target access:
714227825Stheraven    const std::type_info& target_type() const;
715232924Stheraven    template <typename _Tp> _Tp* target();
716232924Stheraven    template <typename _Tp> const _Tp* target() const;
717227825Stheraven#endif  // _LIBCPP_NO_RTTI
718227825Stheraven};
719227825Stheraven
720232924Stheraventemplate<class _Rp>
721232924Stheravenfunction<_Rp()>::function(const function& __f)
722227825Stheraven{
723227825Stheraven    if (__f.__f_ == 0)
724227825Stheraven        __f_ = 0;
725227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
726227825Stheraven    {
727227825Stheraven        __f_ = (__base*)&__buf_;
728227825Stheraven        __f.__f_->__clone(__f_);
729227825Stheraven    }
730227825Stheraven    else
731227825Stheraven        __f_ = __f.__f_->__clone();
732227825Stheraven}
733227825Stheraven
734232924Stheraventemplate<class _Rp>
735227825Stheraventemplate<class _Alloc>
736232924Stheravenfunction<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
737227825Stheraven{
738227825Stheraven    if (__f.__f_ == 0)
739227825Stheraven        __f_ = 0;
740227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
741227825Stheraven    {
742227825Stheraven        __f_ = (__base*)&__buf_;
743227825Stheraven        __f.__f_->__clone(__f_);
744227825Stheraven    }
745227825Stheraven    else
746227825Stheraven        __f_ = __f.__f_->__clone();
747227825Stheraven}
748227825Stheraven
749232924Stheraventemplate<class _Rp>
750232924Stheraventemplate <class _Fp>
751232924Stheravenfunction<_Rp()>::function(_Fp __f,
752232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
753227825Stheraven    : __f_(0)
754227825Stheraven{
755227825Stheraven    if (__not_null(__f))
756227825Stheraven    {
757232924Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
758227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
759227825Stheraven        {
760227825Stheraven            __f_ = (__base*)&__buf_;
761227825Stheraven            ::new (__f_) _FF(__f);
762227825Stheraven        }
763227825Stheraven        else
764227825Stheraven        {
765232924Stheraven            typedef allocator<_FF> _Ap;
766232924Stheraven            _Ap __a;
767232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
768232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
769232924Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
770227825Stheraven            __f_ = __hold.release();
771227825Stheraven        }
772227825Stheraven    }
773227825Stheraven}
774227825Stheraven
775232924Stheraventemplate<class _Rp>
776232924Stheraventemplate <class _Fp, class _Alloc>
777232924Stheravenfunction<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
778232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
779227825Stheraven    : __f_(0)
780227825Stheraven{
781227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
782227825Stheraven    if (__not_null(__f))
783227825Stheraven    {
784232924Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
785227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
786227825Stheraven        {
787227825Stheraven            __f_ = (__base*)&__buf_;
788227825Stheraven            ::new (__f_) _FF(__f);
789227825Stheraven        }
790227825Stheraven        else
791227825Stheraven        {
792227825Stheraven            typedef typename __alloc_traits::template
793227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
794227825Stheraven                rebind_alloc<_FF>
795227825Stheraven#else
796227825Stheraven                rebind_alloc<_FF>::other
797227825Stheraven#endif
798232924Stheraven                                                         _Ap;
799232924Stheraven            _Ap __a(__a0);
800232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
801232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
802227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
803227825Stheraven            __f_ = __hold.release();
804227825Stheraven        }
805227825Stheraven    }
806227825Stheraven}
807227825Stheraven
808232924Stheraventemplate<class _Rp>
809232924Stheravenfunction<_Rp()>&
810232924Stheravenfunction<_Rp()>::operator=(const function& __f)
811227825Stheraven{
812227825Stheraven    function(__f).swap(*this);
813227825Stheraven    return *this;
814227825Stheraven}
815227825Stheraven
816232924Stheraventemplate<class _Rp>
817232924Stheravenfunction<_Rp()>&
818232924Stheravenfunction<_Rp()>::operator=(nullptr_t)
819227825Stheraven{
820227825Stheraven    if (__f_ == (__base*)&__buf_)
821227825Stheraven        __f_->destroy();
822227825Stheraven    else if (__f_)
823227825Stheraven        __f_->destroy_deallocate();
824227825Stheraven    __f_ = 0;
825227825Stheraven}
826227825Stheraven
827232924Stheraventemplate<class _Rp>
828232924Stheraventemplate <class _Fp>
829227825Stheraventypename enable_if
830227825Stheraven<
831232924Stheraven    !is_integral<_Fp>::value,
832232924Stheraven    function<_Rp()>&
833227825Stheraven>::type
834232924Stheravenfunction<_Rp()>::operator=(_Fp __f)
835227825Stheraven{
836227825Stheraven    function(_VSTD::move(__f)).swap(*this);
837227825Stheraven    return *this;
838227825Stheraven}
839227825Stheraven
840232924Stheraventemplate<class _Rp>
841232924Stheravenfunction<_Rp()>::~function()
842227825Stheraven{
843227825Stheraven    if (__f_ == (__base*)&__buf_)
844227825Stheraven        __f_->destroy();
845227825Stheraven    else if (__f_)
846227825Stheraven        __f_->destroy_deallocate();
847227825Stheraven}
848227825Stheraven
849232924Stheraventemplate<class _Rp>
850227825Stheravenvoid
851232924Stheravenfunction<_Rp()>::swap(function& __f)
852227825Stheraven{
853227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
854227825Stheraven    {
855227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
856227825Stheraven        __base* __t = (__base*)&__tempbuf;
857227825Stheraven        __f_->__clone(__t);
858227825Stheraven        __f_->destroy();
859227825Stheraven        __f_ = 0;
860227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
861227825Stheraven        __f.__f_->destroy();
862227825Stheraven        __f.__f_ = 0;
863227825Stheraven        __f_ = (__base*)&__buf_;
864227825Stheraven        __t->__clone((__base*)&__f.__buf_);
865227825Stheraven        __t->destroy();
866227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
867227825Stheraven    }
868227825Stheraven    else if (__f_ == (__base*)&__buf_)
869227825Stheraven    {
870227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
871227825Stheraven        __f_->destroy();
872227825Stheraven        __f_ = __f.__f_;
873227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
874227825Stheraven    }
875227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
876227825Stheraven    {
877227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
878227825Stheraven        __f.__f_->destroy();
879227825Stheraven        __f.__f_ = __f_;
880227825Stheraven        __f_ = (__base*)&__buf_;
881227825Stheraven    }
882227825Stheraven    else
883227825Stheraven        _VSTD::swap(__f_, __f.__f_);
884227825Stheraven}
885227825Stheraven
886232924Stheraventemplate<class _Rp>
887232924Stheraven_Rp
888232924Stheravenfunction<_Rp()>::operator()() const
889227825Stheraven{
890227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
891227825Stheraven    if (__f_ == 0)
892227825Stheraven        throw bad_function_call();
893227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
894227825Stheraven    return (*__f_)();
895227825Stheraven}
896227825Stheraven
897227825Stheraven#ifndef _LIBCPP_NO_RTTI
898227825Stheraven
899232924Stheraventemplate<class _Rp>
900227825Stheravenconst std::type_info&
901232924Stheravenfunction<_Rp()>::target_type() const
902227825Stheraven{
903227825Stheraven    if (__f_ == 0)
904227825Stheraven        return typeid(void);
905227825Stheraven    return __f_->target_type();
906227825Stheraven}
907227825Stheraven
908232924Stheraventemplate<class _Rp>
909232924Stheraventemplate <typename _Tp>
910232924Stheraven_Tp*
911232924Stheravenfunction<_Rp()>::target()
912227825Stheraven{
913227825Stheraven    if (__f_ == 0)
914232924Stheraven        return (_Tp*)0;
915232924Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
916227825Stheraven}
917227825Stheraven
918232924Stheraventemplate<class _Rp>
919232924Stheraventemplate <typename _Tp>
920232924Stheravenconst _Tp*
921232924Stheravenfunction<_Rp()>::target() const
922227825Stheraven{
923227825Stheraven    if (__f_ == 0)
924232924Stheraven        return (const _Tp*)0;
925232924Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
926227825Stheraven}
927227825Stheraven
928227825Stheraven#endif  // _LIBCPP_NO_RTTI
929227825Stheraven
930232924Stheraventemplate<class _Rp, class _A0>
931261272Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
932232924Stheraven    : public unary_function<_A0, _Rp>
933227825Stheraven{
934232924Stheraven    typedef __function::__base<_Rp(_A0)> __base;
935227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
936227825Stheraven    __base* __f_;
937227825Stheraven
938232924Stheraven    template <class _Fp>
939227825Stheraven        _LIBCPP_INLINE_VISIBILITY
940232924Stheraven        static bool __not_null(const _Fp&) {return true;}
941227825Stheraven    template <class _R2, class _B0>
942227825Stheraven        _LIBCPP_INLINE_VISIBILITY
943227825Stheraven        static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
944232924Stheraven    template <class _R2, class _Cp>
945227825Stheraven        _LIBCPP_INLINE_VISIBILITY
946232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
947232924Stheraven    template <class _R2, class _Cp>
948227825Stheraven        _LIBCPP_INLINE_VISIBILITY
949232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
950232924Stheraven    template <class _R2, class _Cp>
951227825Stheraven        _LIBCPP_INLINE_VISIBILITY
952232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
953232924Stheraven    template <class _R2, class _Cp>
954227825Stheraven        _LIBCPP_INLINE_VISIBILITY
955232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
956227825Stheraven    template <class _R2, class _B0>
957227825Stheraven        _LIBCPP_INLINE_VISIBILITY
958232924Stheraven        static bool __not_null(const function<_Rp(_B0)>& __p) {return __p;}
959227825Stheravenpublic:
960232924Stheraven    typedef _Rp result_type;
961227825Stheraven
962227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
963227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
964227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
965227825Stheraven    function(const function&);
966232924Stheraven    template<class _Fp>
967232924Stheraven      function(_Fp,
968232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
969227825Stheraven
970227825Stheraven    template<class _Alloc>
971227825Stheraven      _LIBCPP_INLINE_VISIBILITY
972227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
973227825Stheraven    template<class _Alloc>
974227825Stheraven      _LIBCPP_INLINE_VISIBILITY
975227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
976227825Stheraven    template<class _Alloc>
977227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
978232924Stheraven    template<class _Fp, class _Alloc>
979232924Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
980232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
981227825Stheraven
982227825Stheraven    function& operator=(const function&);
983227825Stheraven    function& operator=(nullptr_t);
984232924Stheraven    template<class _Fp>
985227825Stheraven      typename enable_if
986227825Stheraven      <
987232924Stheraven        !is_integral<_Fp>::value,
988227825Stheraven        function&
989227825Stheraven      >::type
990232924Stheraven      operator=(_Fp);
991227825Stheraven
992227825Stheraven    ~function();
993227825Stheraven
994227825Stheraven    // 20.7.16.2.2, function modifiers:
995227825Stheraven    void swap(function&);
996232924Stheraven    template<class _Fp, class _Alloc>
997227825Stheraven      _LIBCPP_INLINE_VISIBILITY
998232924Stheraven      void assign(_Fp __f, const _Alloc& __a)
999227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1000227825Stheraven
1001227825Stheraven    // 20.7.16.2.3, function capacity:
1002227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1003227825Stheraven
1004227825Stheravenprivate:
1005227825Stheraven    // deleted overloads close possible hole in the type system
1006227825Stheraven    template<class _R2, class _B0>
1007227825Stheraven      bool operator==(const function<_R2(_B0)>&) const;// = delete;
1008227825Stheraven    template<class _R2, class _B0>
1009227825Stheraven      bool operator!=(const function<_R2(_B0)>&) const;// = delete;
1010227825Stheravenpublic:
1011227825Stheraven    // 20.7.16.2.4, function invocation:
1012232924Stheraven    _Rp operator()(_A0) const;
1013227825Stheraven
1014227825Stheraven#ifndef _LIBCPP_NO_RTTI
1015227825Stheraven    // 20.7.16.2.5, function target access:
1016227825Stheraven    const std::type_info& target_type() const;
1017232924Stheraven    template <typename _Tp> _Tp* target();
1018232924Stheraven    template <typename _Tp> const _Tp* target() const;
1019227825Stheraven#endif  // _LIBCPP_NO_RTTI
1020227825Stheraven};
1021227825Stheraven
1022232924Stheraventemplate<class _Rp, class _A0>
1023232924Stheravenfunction<_Rp(_A0)>::function(const function& __f)
1024227825Stheraven{
1025227825Stheraven    if (__f.__f_ == 0)
1026227825Stheraven        __f_ = 0;
1027227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1028227825Stheraven    {
1029227825Stheraven        __f_ = (__base*)&__buf_;
1030227825Stheraven        __f.__f_->__clone(__f_);
1031227825Stheraven    }
1032227825Stheraven    else
1033227825Stheraven        __f_ = __f.__f_->__clone();
1034227825Stheraven}
1035227825Stheraven
1036232924Stheraventemplate<class _Rp, class _A0>
1037227825Stheraventemplate<class _Alloc>
1038232924Stheravenfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1039227825Stheraven{
1040227825Stheraven    if (__f.__f_ == 0)
1041227825Stheraven        __f_ = 0;
1042227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1043227825Stheraven    {
1044227825Stheraven        __f_ = (__base*)&__buf_;
1045227825Stheraven        __f.__f_->__clone(__f_);
1046227825Stheraven    }
1047227825Stheraven    else
1048227825Stheraven        __f_ = __f.__f_->__clone();
1049227825Stheraven}
1050227825Stheraven
1051232924Stheraventemplate<class _Rp, class _A0>
1052232924Stheraventemplate <class _Fp>
1053232924Stheravenfunction<_Rp(_A0)>::function(_Fp __f,
1054232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1055227825Stheraven    : __f_(0)
1056227825Stheraven{
1057227825Stheraven    if (__not_null(__f))
1058227825Stheraven    {
1059232924Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
1060227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1061227825Stheraven        {
1062227825Stheraven            __f_ = (__base*)&__buf_;
1063227825Stheraven            ::new (__f_) _FF(__f);
1064227825Stheraven        }
1065227825Stheraven        else
1066227825Stheraven        {
1067232924Stheraven            typedef allocator<_FF> _Ap;
1068232924Stheraven            _Ap __a;
1069232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1070232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1071232924Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1072227825Stheraven            __f_ = __hold.release();
1073227825Stheraven        }
1074227825Stheraven    }
1075227825Stheraven}
1076227825Stheraven
1077232924Stheraventemplate<class _Rp, class _A0>
1078232924Stheraventemplate <class _Fp, class _Alloc>
1079232924Stheravenfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1080232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1081227825Stheraven    : __f_(0)
1082227825Stheraven{
1083227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1084227825Stheraven    if (__not_null(__f))
1085227825Stheraven    {
1086232924Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
1087227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1088227825Stheraven        {
1089227825Stheraven            __f_ = (__base*)&__buf_;
1090227825Stheraven            ::new (__f_) _FF(__f);
1091227825Stheraven        }
1092227825Stheraven        else
1093227825Stheraven        {
1094227825Stheraven            typedef typename __alloc_traits::template
1095227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1096227825Stheraven                rebind_alloc<_FF>
1097227825Stheraven#else
1098227825Stheraven                rebind_alloc<_FF>::other
1099227825Stheraven#endif
1100232924Stheraven                                                         _Ap;
1101232924Stheraven            _Ap __a(__a0);
1102232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1103232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1104227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1105227825Stheraven            __f_ = __hold.release();
1106227825Stheraven        }
1107227825Stheraven    }
1108227825Stheraven}
1109227825Stheraven
1110232924Stheraventemplate<class _Rp, class _A0>
1111232924Stheravenfunction<_Rp(_A0)>&
1112232924Stheravenfunction<_Rp(_A0)>::operator=(const function& __f)
1113227825Stheraven{
1114227825Stheraven    function(__f).swap(*this);
1115227825Stheraven    return *this;
1116227825Stheraven}
1117227825Stheraven
1118232924Stheraventemplate<class _Rp, class _A0>
1119232924Stheravenfunction<_Rp(_A0)>&
1120232924Stheravenfunction<_Rp(_A0)>::operator=(nullptr_t)
1121227825Stheraven{
1122227825Stheraven    if (__f_ == (__base*)&__buf_)
1123227825Stheraven        __f_->destroy();
1124227825Stheraven    else if (__f_)
1125227825Stheraven        __f_->destroy_deallocate();
1126227825Stheraven    __f_ = 0;
1127227825Stheraven}
1128227825Stheraven
1129232924Stheraventemplate<class _Rp, class _A0>
1130232924Stheraventemplate <class _Fp>
1131227825Stheraventypename enable_if
1132227825Stheraven<
1133232924Stheraven    !is_integral<_Fp>::value,
1134232924Stheraven    function<_Rp(_A0)>&
1135227825Stheraven>::type
1136232924Stheravenfunction<_Rp(_A0)>::operator=(_Fp __f)
1137227825Stheraven{
1138227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1139227825Stheraven    return *this;
1140227825Stheraven}
1141227825Stheraven
1142232924Stheraventemplate<class _Rp, class _A0>
1143232924Stheravenfunction<_Rp(_A0)>::~function()
1144227825Stheraven{
1145227825Stheraven    if (__f_ == (__base*)&__buf_)
1146227825Stheraven        __f_->destroy();
1147227825Stheraven    else if (__f_)
1148227825Stheraven        __f_->destroy_deallocate();
1149227825Stheraven}
1150227825Stheraven
1151232924Stheraventemplate<class _Rp, class _A0>
1152227825Stheravenvoid
1153232924Stheravenfunction<_Rp(_A0)>::swap(function& __f)
1154227825Stheraven{
1155227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1156227825Stheraven    {
1157227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1158227825Stheraven        __base* __t = (__base*)&__tempbuf;
1159227825Stheraven        __f_->__clone(__t);
1160227825Stheraven        __f_->destroy();
1161227825Stheraven        __f_ = 0;
1162227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1163227825Stheraven        __f.__f_->destroy();
1164227825Stheraven        __f.__f_ = 0;
1165227825Stheraven        __f_ = (__base*)&__buf_;
1166227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1167227825Stheraven        __t->destroy();
1168227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1169227825Stheraven    }
1170227825Stheraven    else if (__f_ == (__base*)&__buf_)
1171227825Stheraven    {
1172227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1173227825Stheraven        __f_->destroy();
1174227825Stheraven        __f_ = __f.__f_;
1175227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1176227825Stheraven    }
1177227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1178227825Stheraven    {
1179227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1180227825Stheraven        __f.__f_->destroy();
1181227825Stheraven        __f.__f_ = __f_;
1182227825Stheraven        __f_ = (__base*)&__buf_;
1183227825Stheraven    }
1184227825Stheraven    else
1185227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1186227825Stheraven}
1187227825Stheraven
1188232924Stheraventemplate<class _Rp, class _A0>
1189232924Stheraven_Rp
1190232924Stheravenfunction<_Rp(_A0)>::operator()(_A0 __a0) const
1191227825Stheraven{
1192227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1193227825Stheraven    if (__f_ == 0)
1194227825Stheraven        throw bad_function_call();
1195227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1196227825Stheraven    return (*__f_)(__a0);
1197227825Stheraven}
1198227825Stheraven
1199227825Stheraven#ifndef _LIBCPP_NO_RTTI
1200227825Stheraven
1201232924Stheraventemplate<class _Rp, class _A0>
1202227825Stheravenconst std::type_info&
1203232924Stheravenfunction<_Rp(_A0)>::target_type() const
1204227825Stheraven{
1205227825Stheraven    if (__f_ == 0)
1206227825Stheraven        return typeid(void);
1207227825Stheraven    return __f_->target_type();
1208227825Stheraven}
1209227825Stheraven
1210232924Stheraventemplate<class _Rp, class _A0>
1211232924Stheraventemplate <typename _Tp>
1212232924Stheraven_Tp*
1213232924Stheravenfunction<_Rp(_A0)>::target()
1214227825Stheraven{
1215227825Stheraven    if (__f_ == 0)
1216232924Stheraven        return (_Tp*)0;
1217232924Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1218227825Stheraven}
1219227825Stheraven
1220232924Stheraventemplate<class _Rp, class _A0>
1221232924Stheraventemplate <typename _Tp>
1222232924Stheravenconst _Tp*
1223232924Stheravenfunction<_Rp(_A0)>::target() const
1224227825Stheraven{
1225227825Stheraven    if (__f_ == 0)
1226232924Stheraven        return (const _Tp*)0;
1227232924Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1228227825Stheraven}
1229227825Stheraven
1230227825Stheraven#endif  // _LIBCPP_NO_RTTI
1231227825Stheraven
1232232924Stheraventemplate<class _Rp, class _A0, class _A1>
1233261272Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
1234232924Stheraven    : public binary_function<_A0, _A1, _Rp>
1235227825Stheraven{
1236232924Stheraven    typedef __function::__base<_Rp(_A0, _A1)> __base;
1237227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1238227825Stheraven    __base* __f_;
1239227825Stheraven
1240232924Stheraven    template <class _Fp>
1241227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1242232924Stheraven        static bool __not_null(const _Fp&) {return true;}
1243227825Stheraven    template <class _R2, class _B0, class _B1>
1244227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1245227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
1246232924Stheraven    template <class _R2, class _Cp, class _B1>
1247227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1248232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1249232924Stheraven    template <class _R2, class _Cp, class _B1>
1250227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1251232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1252232924Stheraven    template <class _R2, class _Cp, class _B1>
1253227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1254232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1255232924Stheraven    template <class _R2, class _Cp, class _B1>
1256227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1257232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
1258227825Stheraven    template <class _R2, class _B0, class _B1>
1259227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1260232924Stheraven        static bool __not_null(const function<_Rp(_B0, _B1)>& __p) {return __p;}
1261227825Stheravenpublic:
1262232924Stheraven    typedef _Rp result_type;
1263227825Stheraven
1264227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1265227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1266227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1267227825Stheraven    function(const function&);
1268232924Stheraven    template<class _Fp>
1269232924Stheraven      function(_Fp,
1270232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1271227825Stheraven
1272227825Stheraven    template<class _Alloc>
1273227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1274227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1275227825Stheraven    template<class _Alloc>
1276227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1277227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1278227825Stheraven    template<class _Alloc>
1279227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1280232924Stheraven    template<class _Fp, class _Alloc>
1281232924Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1282232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1283227825Stheraven
1284227825Stheraven    function& operator=(const function&);
1285227825Stheraven    function& operator=(nullptr_t);
1286232924Stheraven    template<class _Fp>
1287227825Stheraven      typename enable_if
1288227825Stheraven      <
1289232924Stheraven        !is_integral<_Fp>::value,
1290227825Stheraven        function&
1291227825Stheraven      >::type
1292232924Stheraven      operator=(_Fp);
1293227825Stheraven
1294227825Stheraven    ~function();
1295227825Stheraven
1296227825Stheraven    // 20.7.16.2.2, function modifiers:
1297227825Stheraven    void swap(function&);
1298232924Stheraven    template<class _Fp, class _Alloc>
1299227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1300232924Stheraven      void assign(_Fp __f, const _Alloc& __a)
1301227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1302227825Stheraven
1303227825Stheraven    // 20.7.16.2.3, function capacity:
1304227825Stheraven    operator bool() const {return __f_;}
1305227825Stheraven
1306227825Stheravenprivate:
1307227825Stheraven    // deleted overloads close possible hole in the type system
1308227825Stheraven    template<class _R2, class _B0, class _B1>
1309227825Stheraven      bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
1310227825Stheraven    template<class _R2, class _B0, class _B1>
1311227825Stheraven      bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
1312227825Stheravenpublic:
1313227825Stheraven    // 20.7.16.2.4, function invocation:
1314232924Stheraven    _Rp operator()(_A0, _A1) const;
1315227825Stheraven
1316227825Stheraven#ifndef _LIBCPP_NO_RTTI
1317227825Stheraven    // 20.7.16.2.5, function target access:
1318227825Stheraven    const std::type_info& target_type() const;
1319232924Stheraven    template <typename _Tp> _Tp* target();
1320232924Stheraven    template <typename _Tp> const _Tp* target() const;
1321227825Stheraven#endif  // _LIBCPP_NO_RTTI
1322227825Stheraven};
1323227825Stheraven
1324232924Stheraventemplate<class _Rp, class _A0, class _A1>
1325232924Stheravenfunction<_Rp(_A0, _A1)>::function(const function& __f)
1326227825Stheraven{
1327227825Stheraven    if (__f.__f_ == 0)
1328227825Stheraven        __f_ = 0;
1329227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1330227825Stheraven    {
1331227825Stheraven        __f_ = (__base*)&__buf_;
1332227825Stheraven        __f.__f_->__clone(__f_);
1333227825Stheraven    }
1334227825Stheraven    else
1335227825Stheraven        __f_ = __f.__f_->__clone();
1336227825Stheraven}
1337227825Stheraven
1338232924Stheraventemplate<class _Rp, class _A0, class _A1>
1339227825Stheraventemplate<class _Alloc>
1340232924Stheravenfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1341227825Stheraven{
1342227825Stheraven    if (__f.__f_ == 0)
1343227825Stheraven        __f_ = 0;
1344227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1345227825Stheraven    {
1346227825Stheraven        __f_ = (__base*)&__buf_;
1347227825Stheraven        __f.__f_->__clone(__f_);
1348227825Stheraven    }
1349227825Stheraven    else
1350227825Stheraven        __f_ = __f.__f_->__clone();
1351227825Stheraven}
1352227825Stheraven
1353232924Stheraventemplate<class _Rp, class _A0, class _A1>
1354232924Stheraventemplate <class _Fp>
1355232924Stheravenfunction<_Rp(_A0, _A1)>::function(_Fp __f,
1356232924Stheraven                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1357227825Stheraven    : __f_(0)
1358227825Stheraven{
1359227825Stheraven    if (__not_null(__f))
1360227825Stheraven    {
1361232924Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
1362227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1363227825Stheraven        {
1364227825Stheraven            __f_ = (__base*)&__buf_;
1365227825Stheraven            ::new (__f_) _FF(__f);
1366227825Stheraven        }
1367227825Stheraven        else
1368227825Stheraven        {
1369232924Stheraven            typedef allocator<_FF> _Ap;
1370232924Stheraven            _Ap __a;
1371232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1372232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1373232924Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1374227825Stheraven            __f_ = __hold.release();
1375227825Stheraven        }
1376227825Stheraven    }
1377227825Stheraven}
1378227825Stheraven
1379232924Stheraventemplate<class _Rp, class _A0, class _A1>
1380232924Stheraventemplate <class _Fp, class _Alloc>
1381232924Stheravenfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1382232924Stheraven                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1383227825Stheraven    : __f_(0)
1384227825Stheraven{
1385227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1386227825Stheraven    if (__not_null(__f))
1387227825Stheraven    {
1388232924Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
1389227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1390227825Stheraven        {
1391227825Stheraven            __f_ = (__base*)&__buf_;
1392227825Stheraven            ::new (__f_) _FF(__f);
1393227825Stheraven        }
1394227825Stheraven        else
1395227825Stheraven        {
1396227825Stheraven            typedef typename __alloc_traits::template
1397227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1398227825Stheraven                rebind_alloc<_FF>
1399227825Stheraven#else
1400227825Stheraven                rebind_alloc<_FF>::other
1401227825Stheraven#endif
1402232924Stheraven                                                         _Ap;
1403232924Stheraven            _Ap __a(__a0);
1404232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1405232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1406227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1407227825Stheraven            __f_ = __hold.release();
1408227825Stheraven        }
1409227825Stheraven    }
1410227825Stheraven}
1411227825Stheraven
1412232924Stheraventemplate<class _Rp, class _A0, class _A1>
1413232924Stheravenfunction<_Rp(_A0, _A1)>&
1414232924Stheravenfunction<_Rp(_A0, _A1)>::operator=(const function& __f)
1415227825Stheraven{
1416227825Stheraven    function(__f).swap(*this);
1417227825Stheraven    return *this;
1418227825Stheraven}
1419227825Stheraven
1420232924Stheraventemplate<class _Rp, class _A0, class _A1>
1421232924Stheravenfunction<_Rp(_A0, _A1)>&
1422232924Stheravenfunction<_Rp(_A0, _A1)>::operator=(nullptr_t)
1423227825Stheraven{
1424227825Stheraven    if (__f_ == (__base*)&__buf_)
1425227825Stheraven        __f_->destroy();
1426227825Stheraven    else if (__f_)
1427227825Stheraven        __f_->destroy_deallocate();
1428227825Stheraven    __f_ = 0;
1429227825Stheraven}
1430227825Stheraven
1431232924Stheraventemplate<class _Rp, class _A0, class _A1>
1432232924Stheraventemplate <class _Fp>
1433227825Stheraventypename enable_if
1434227825Stheraven<
1435232924Stheraven    !is_integral<_Fp>::value,
1436232924Stheraven    function<_Rp(_A0, _A1)>&
1437227825Stheraven>::type
1438232924Stheravenfunction<_Rp(_A0, _A1)>::operator=(_Fp __f)
1439227825Stheraven{
1440227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1441227825Stheraven    return *this;
1442227825Stheraven}
1443227825Stheraven
1444232924Stheraventemplate<class _Rp, class _A0, class _A1>
1445232924Stheravenfunction<_Rp(_A0, _A1)>::~function()
1446227825Stheraven{
1447227825Stheraven    if (__f_ == (__base*)&__buf_)
1448227825Stheraven        __f_->destroy();
1449227825Stheraven    else if (__f_)
1450227825Stheraven        __f_->destroy_deallocate();
1451227825Stheraven}
1452227825Stheraven
1453232924Stheraventemplate<class _Rp, class _A0, class _A1>
1454227825Stheravenvoid
1455232924Stheravenfunction<_Rp(_A0, _A1)>::swap(function& __f)
1456227825Stheraven{
1457227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1458227825Stheraven    {
1459227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1460227825Stheraven        __base* __t = (__base*)&__tempbuf;
1461227825Stheraven        __f_->__clone(__t);
1462227825Stheraven        __f_->destroy();
1463227825Stheraven        __f_ = 0;
1464227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1465227825Stheraven        __f.__f_->destroy();
1466227825Stheraven        __f.__f_ = 0;
1467227825Stheraven        __f_ = (__base*)&__buf_;
1468227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1469227825Stheraven        __t->destroy();
1470227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1471227825Stheraven    }
1472227825Stheraven    else if (__f_ == (__base*)&__buf_)
1473227825Stheraven    {
1474227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1475227825Stheraven        __f_->destroy();
1476227825Stheraven        __f_ = __f.__f_;
1477227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1478227825Stheraven    }
1479227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1480227825Stheraven    {
1481227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1482227825Stheraven        __f.__f_->destroy();
1483227825Stheraven        __f.__f_ = __f_;
1484227825Stheraven        __f_ = (__base*)&__buf_;
1485227825Stheraven    }
1486227825Stheraven    else
1487227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1488227825Stheraven}
1489227825Stheraven
1490232924Stheraventemplate<class _Rp, class _A0, class _A1>
1491232924Stheraven_Rp
1492232924Stheravenfunction<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1493227825Stheraven{
1494227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1495227825Stheraven    if (__f_ == 0)
1496227825Stheraven        throw bad_function_call();
1497227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1498227825Stheraven    return (*__f_)(__a0, __a1);
1499227825Stheraven}
1500227825Stheraven
1501227825Stheraven#ifndef _LIBCPP_NO_RTTI
1502227825Stheraven
1503232924Stheraventemplate<class _Rp, class _A0, class _A1>
1504227825Stheravenconst std::type_info&
1505232924Stheravenfunction<_Rp(_A0, _A1)>::target_type() const
1506227825Stheraven{
1507227825Stheraven    if (__f_ == 0)
1508227825Stheraven        return typeid(void);
1509227825Stheraven    return __f_->target_type();
1510227825Stheraven}
1511227825Stheraven
1512232924Stheraventemplate<class _Rp, class _A0, class _A1>
1513232924Stheraventemplate <typename _Tp>
1514232924Stheraven_Tp*
1515232924Stheravenfunction<_Rp(_A0, _A1)>::target()
1516227825Stheraven{
1517227825Stheraven    if (__f_ == 0)
1518232924Stheraven        return (_Tp*)0;
1519232924Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1520227825Stheraven}
1521227825Stheraven
1522232924Stheraventemplate<class _Rp, class _A0, class _A1>
1523232924Stheraventemplate <typename _Tp>
1524232924Stheravenconst _Tp*
1525232924Stheravenfunction<_Rp(_A0, _A1)>::target() const
1526227825Stheraven{
1527227825Stheraven    if (__f_ == 0)
1528232924Stheraven        return (const _Tp*)0;
1529232924Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1530227825Stheraven}
1531227825Stheraven
1532227825Stheraven#endif  // _LIBCPP_NO_RTTI
1533227825Stheraven
1534232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1535261272Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
1536227825Stheraven{
1537232924Stheraven    typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
1538227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1539227825Stheraven    __base* __f_;
1540227825Stheraven
1541232924Stheraven    template <class _Fp>
1542227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1543232924Stheraven        static bool __not_null(const _Fp&) {return true;}
1544227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1545227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1546227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
1547232924Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1548227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1549232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1550232924Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1551227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1552232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1553232924Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1554227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1555232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1556232924Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1557227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1558232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
1559227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1560227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1561232924Stheraven        static bool __not_null(const function<_Rp(_B0, _B1, _B2)>& __p) {return __p;}
1562227825Stheravenpublic:
1563232924Stheraven    typedef _Rp result_type;
1564227825Stheraven
1565227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1566227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1567227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1568227825Stheraven    function(const function&);
1569232924Stheraven    template<class _Fp>
1570232924Stheraven      function(_Fp,
1571232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1572227825Stheraven
1573227825Stheraven    template<class _Alloc>
1574227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1575227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1576227825Stheraven    template<class _Alloc>
1577227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1578227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1579227825Stheraven    template<class _Alloc>
1580227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1581232924Stheraven    template<class _Fp, class _Alloc>
1582232924Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1583232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1584227825Stheraven
1585227825Stheraven    function& operator=(const function&);
1586227825Stheraven    function& operator=(nullptr_t);
1587232924Stheraven    template<class _Fp>
1588227825Stheraven      typename enable_if
1589227825Stheraven      <
1590232924Stheraven        !is_integral<_Fp>::value,
1591227825Stheraven        function&
1592227825Stheraven      >::type
1593232924Stheraven      operator=(_Fp);
1594227825Stheraven
1595227825Stheraven    ~function();
1596227825Stheraven
1597227825Stheraven    // 20.7.16.2.2, function modifiers:
1598227825Stheraven    void swap(function&);
1599232924Stheraven    template<class _Fp, class _Alloc>
1600227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1601232924Stheraven      void assign(_Fp __f, const _Alloc& __a)
1602227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1603227825Stheraven
1604227825Stheraven    // 20.7.16.2.3, function capacity:
1605227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1606227825Stheraven
1607227825Stheravenprivate:
1608227825Stheraven    // deleted overloads close possible hole in the type system
1609227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1610227825Stheraven      bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1611227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1612227825Stheraven      bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1613227825Stheravenpublic:
1614227825Stheraven    // 20.7.16.2.4, function invocation:
1615232924Stheraven    _Rp operator()(_A0, _A1, _A2) const;
1616227825Stheraven
1617227825Stheraven#ifndef _LIBCPP_NO_RTTI
1618227825Stheraven    // 20.7.16.2.5, function target access:
1619227825Stheraven    const std::type_info& target_type() const;
1620232924Stheraven    template <typename _Tp> _Tp* target();
1621232924Stheraven    template <typename _Tp> const _Tp* target() const;
1622227825Stheraven#endif  // _LIBCPP_NO_RTTI
1623227825Stheraven};
1624227825Stheraven
1625232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1626232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(const function& __f)
1627227825Stheraven{
1628227825Stheraven    if (__f.__f_ == 0)
1629227825Stheraven        __f_ = 0;
1630227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1631227825Stheraven    {
1632227825Stheraven        __f_ = (__base*)&__buf_;
1633227825Stheraven        __f.__f_->__clone(__f_);
1634227825Stheraven    }
1635227825Stheraven    else
1636227825Stheraven        __f_ = __f.__f_->__clone();
1637227825Stheraven}
1638227825Stheraven
1639232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1640227825Stheraventemplate<class _Alloc>
1641232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
1642227825Stheraven                                      const function& __f)
1643227825Stheraven{
1644227825Stheraven    if (__f.__f_ == 0)
1645227825Stheraven        __f_ = 0;
1646227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1647227825Stheraven    {
1648227825Stheraven        __f_ = (__base*)&__buf_;
1649227825Stheraven        __f.__f_->__clone(__f_);
1650227825Stheraven    }
1651227825Stheraven    else
1652227825Stheraven        __f_ = __f.__f_->__clone();
1653227825Stheraven}
1654227825Stheraven
1655232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1656232924Stheraventemplate <class _Fp>
1657232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1658232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1659227825Stheraven    : __f_(0)
1660227825Stheraven{
1661227825Stheraven    if (__not_null(__f))
1662227825Stheraven    {
1663232924Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
1664227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1665227825Stheraven        {
1666227825Stheraven            __f_ = (__base*)&__buf_;
1667227825Stheraven            ::new (__f_) _FF(__f);
1668227825Stheraven        }
1669227825Stheraven        else
1670227825Stheraven        {
1671232924Stheraven            typedef allocator<_FF> _Ap;
1672232924Stheraven            _Ap __a;
1673232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1674232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1675232924Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1676227825Stheraven            __f_ = __hold.release();
1677227825Stheraven        }
1678227825Stheraven    }
1679227825Stheraven}
1680227825Stheraven
1681232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1682232924Stheraventemplate <class _Fp, class _Alloc>
1683232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1684232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1685227825Stheraven    : __f_(0)
1686227825Stheraven{
1687227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1688227825Stheraven    if (__not_null(__f))
1689227825Stheraven    {
1690232924Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
1691227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1692227825Stheraven        {
1693227825Stheraven            __f_ = (__base*)&__buf_;
1694227825Stheraven            ::new (__f_) _FF(__f);
1695227825Stheraven        }
1696227825Stheraven        else
1697227825Stheraven        {
1698227825Stheraven            typedef typename __alloc_traits::template
1699227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1700227825Stheraven                rebind_alloc<_FF>
1701227825Stheraven#else
1702227825Stheraven                rebind_alloc<_FF>::other
1703227825Stheraven#endif
1704232924Stheraven                                                         _Ap;
1705232924Stheraven            _Ap __a(__a0);
1706232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1707232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1708227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1709227825Stheraven            __f_ = __hold.release();
1710227825Stheraven        }
1711227825Stheraven    }
1712227825Stheraven}
1713227825Stheraven
1714232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1715232924Stheravenfunction<_Rp(_A0, _A1, _A2)>&
1716232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
1717227825Stheraven{
1718227825Stheraven    function(__f).swap(*this);
1719227825Stheraven    return *this;
1720227825Stheraven}
1721227825Stheraven
1722232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1723232924Stheravenfunction<_Rp(_A0, _A1, _A2)>&
1724232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
1725227825Stheraven{
1726227825Stheraven    if (__f_ == (__base*)&__buf_)
1727227825Stheraven        __f_->destroy();
1728227825Stheraven    else if (__f_)
1729227825Stheraven        __f_->destroy_deallocate();
1730227825Stheraven    __f_ = 0;
1731227825Stheraven}
1732227825Stheraven
1733232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1734232924Stheraventemplate <class _Fp>
1735227825Stheraventypename enable_if
1736227825Stheraven<
1737232924Stheraven    !is_integral<_Fp>::value,
1738232924Stheraven    function<_Rp(_A0, _A1, _A2)>&
1739227825Stheraven>::type
1740232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
1741227825Stheraven{
1742227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1743227825Stheraven    return *this;
1744227825Stheraven}
1745227825Stheraven
1746232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1747232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::~function()
1748227825Stheraven{
1749227825Stheraven    if (__f_ == (__base*)&__buf_)
1750227825Stheraven        __f_->destroy();
1751227825Stheraven    else if (__f_)
1752227825Stheraven        __f_->destroy_deallocate();
1753227825Stheraven}
1754227825Stheraven
1755232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1756227825Stheravenvoid
1757232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::swap(function& __f)
1758227825Stheraven{
1759227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1760227825Stheraven    {
1761227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1762227825Stheraven        __base* __t = (__base*)&__tempbuf;
1763227825Stheraven        __f_->__clone(__t);
1764227825Stheraven        __f_->destroy();
1765227825Stheraven        __f_ = 0;
1766227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1767227825Stheraven        __f.__f_->destroy();
1768227825Stheraven        __f.__f_ = 0;
1769227825Stheraven        __f_ = (__base*)&__buf_;
1770227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1771227825Stheraven        __t->destroy();
1772227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1773227825Stheraven    }
1774227825Stheraven    else if (__f_ == (__base*)&__buf_)
1775227825Stheraven    {
1776227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1777227825Stheraven        __f_->destroy();
1778227825Stheraven        __f_ = __f.__f_;
1779227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1780227825Stheraven    }
1781227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1782227825Stheraven    {
1783227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1784227825Stheraven        __f.__f_->destroy();
1785227825Stheraven        __f.__f_ = __f_;
1786227825Stheraven        __f_ = (__base*)&__buf_;
1787227825Stheraven    }
1788227825Stheraven    else
1789227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1790227825Stheraven}
1791227825Stheraven
1792232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1793232924Stheraven_Rp
1794232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1795227825Stheraven{
1796227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1797227825Stheraven    if (__f_ == 0)
1798227825Stheraven        throw bad_function_call();
1799227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1800227825Stheraven    return (*__f_)(__a0, __a1, __a2);
1801227825Stheraven}
1802227825Stheraven
1803227825Stheraven#ifndef _LIBCPP_NO_RTTI
1804227825Stheraven
1805232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1806227825Stheravenconst std::type_info&
1807232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::target_type() const
1808227825Stheraven{
1809227825Stheraven    if (__f_ == 0)
1810227825Stheraven        return typeid(void);
1811227825Stheraven    return __f_->target_type();
1812227825Stheraven}
1813227825Stheraven
1814232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1815232924Stheraventemplate <typename _Tp>
1816232924Stheraven_Tp*
1817232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::target()
1818227825Stheraven{
1819227825Stheraven    if (__f_ == 0)
1820232924Stheraven        return (_Tp*)0;
1821232924Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1822227825Stheraven}
1823227825Stheraven
1824232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1825232924Stheraventemplate <typename _Tp>
1826232924Stheravenconst _Tp*
1827232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::target() const
1828227825Stheraven{
1829227825Stheraven    if (__f_ == 0)
1830232924Stheraven        return (const _Tp*)0;
1831232924Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1832227825Stheraven}
1833227825Stheraven
1834227825Stheraven#endif  // _LIBCPP_NO_RTTI
1835227825Stheraven
1836232924Stheraventemplate <class _Fp>
1837227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1838227825Stheravenbool
1839232924Stheravenoperator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
1840227825Stheraven
1841232924Stheraventemplate <class _Fp>
1842227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1843227825Stheravenbool
1844232924Stheravenoperator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
1845227825Stheraven
1846232924Stheraventemplate <class _Fp>
1847227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1848227825Stheravenbool
1849232924Stheravenoperator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
1850227825Stheraven
1851232924Stheraventemplate <class _Fp>
1852227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1853227825Stheravenbool
1854232924Stheravenoperator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
1855227825Stheraven
1856232924Stheraventemplate <class _Fp>
1857227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1858227825Stheravenvoid
1859232924Stheravenswap(function<_Fp>& __x, function<_Fp>& __y)
1860227825Stheraven{return __x.swap(__y);}
1861227825Stheraven
1862227825Stheraventemplate<class _Tp> struct __is_bind_expression : public false_type {};
1863261272Sdimtemplate<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
1864227825Stheraven    : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1865227825Stheraven
1866227825Stheraventemplate<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1867261272Sdimtemplate<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
1868227825Stheraven    : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1869227825Stheraven
1870227825Stheravennamespace placeholders
1871227825Stheraven{
1872227825Stheraven
1873232924Stheraventemplate <int _Np> struct __ph {};
1874227825Stheraven
1875227825Stheravenextern __ph<1>   _1;
1876227825Stheravenextern __ph<2>   _2;
1877227825Stheravenextern __ph<3>   _3;
1878227825Stheravenextern __ph<4>   _4;
1879227825Stheravenextern __ph<5>   _5;
1880227825Stheravenextern __ph<6>   _6;
1881227825Stheravenextern __ph<7>   _7;
1882227825Stheravenextern __ph<8>   _8;
1883227825Stheravenextern __ph<9>   _9;
1884227825Stheravenextern __ph<10> _10;
1885227825Stheraven
1886227825Stheraven}  // placeholders
1887227825Stheraven
1888232924Stheraventemplate<int _Np>
1889232924Stheravenstruct __is_placeholder<placeholders::__ph<_Np> >
1890232924Stheraven    : public integral_constant<int, _Np> {};
1891227825Stheraven
1892227825Stheraventemplate <class _Tp, class _Uj>
1893227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1894227825Stheraven_Tp&
1895227825Stheraven__mu(reference_wrapper<_Tp> __t, _Uj&)
1896227825Stheraven{
1897227825Stheraven    return __t.get();
1898227825Stheraven}
1899227825Stheraven/*
1900227825Stheraventemplate <bool _IsBindExpr, class _Ti, class ..._Uj>
1901227825Stheravenstruct __mu_return1 {};
1902227825Stheraven
1903227825Stheraventemplate <class _Ti, class ..._Uj>
1904227825Stheravenstruct __mu_return1<true, _Ti, _Uj...>
1905227825Stheraven{
1906227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1907227825Stheraven};
1908227825Stheraven
1909227825Stheraventemplate <class _Ti, class ..._Uj, size_t ..._Indx>
1910227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1911227825Stheraventypename __mu_return1<true, _Ti, _Uj...>::type
1912227825Stheraven__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1913227825Stheraven{
1914227825Stheraven    __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
1915227825Stheraven}
1916227825Stheraven
1917227825Stheraventemplate <class _Ti, class ..._Uj>
1918227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1919227825Stheraventypename enable_if
1920227825Stheraven<
1921227825Stheraven    is_bind_expression<_Ti>::value,
1922227825Stheraven    typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1923227825Stheraven>::type
1924227825Stheraven__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1925227825Stheraven{
1926227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1927227825Stheraven    return  __mu_expand(__ti, __uj, __indices());
1928227825Stheraven}
1929227825Stheraven
1930227825Stheraventemplate <bool IsPh, class _Ti, class _Uj>
1931227825Stheravenstruct __mu_return2 {};
1932227825Stheraven
1933227825Stheraventemplate <class _Ti, class _Uj>
1934227825Stheravenstruct __mu_return2<true, _Ti, _Uj>
1935227825Stheraven{
1936227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1937227825Stheraven};
1938227825Stheraven
1939227825Stheraventemplate <class _Ti, class _Uj>
1940227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1941227825Stheraventypename enable_if
1942227825Stheraven<
1943227825Stheraven    0 < is_placeholder<_Ti>::value,
1944227825Stheraven    typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1945227825Stheraven>::type
1946227825Stheraven__mu(_Ti&, _Uj& __uj)
1947227825Stheraven{
1948227825Stheraven    const size_t _Indx = is_placeholder<_Ti>::value - 1;
1949227825Stheraven    // compiler bug workaround
1950227825Stheraven    typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1951227825Stheraven    return __t;
1952227825Stheraven//    return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
1953227825Stheraven}
1954227825Stheraven
1955227825Stheraventemplate <class _Ti, class _Uj>
1956227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1957227825Stheraventypename enable_if
1958227825Stheraven<
1959227825Stheraven    !is_bind_expression<_Ti>::value &&
1960227825Stheraven    is_placeholder<_Ti>::value == 0 &&
1961227825Stheraven    !__is_reference_wrapper<_Ti>::value,
1962227825Stheraven    _Ti&
1963227825Stheraven>::type
1964227825Stheraven__mu(_Ti& __ti, _Uj& __uj)
1965227825Stheraven{
1966227825Stheraven    return __ti;
1967227825Stheraven}
1968227825Stheraven
1969227825Stheraventemplate <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1970227825Stheravenstruct ____mu_return;
1971227825Stheraven
1972227825Stheraventemplate <class _Ti, class ..._Uj>
1973227825Stheravenstruct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1974227825Stheraven{
1975227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1976227825Stheraven};
1977227825Stheraven
1978227825Stheraventemplate <class _Ti, class _TupleUj>
1979227825Stheravenstruct ____mu_return<_Ti, false, true, _TupleUj>
1980227825Stheraven{
1981227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1982227825Stheraven                                   _TupleUj>::type&& type;
1983227825Stheraven};
1984227825Stheraven
1985227825Stheraventemplate <class _Ti, class _TupleUj>
1986227825Stheravenstruct ____mu_return<_Ti, false, false, _TupleUj>
1987227825Stheraven{
1988227825Stheraven    typedef _Ti& type;
1989227825Stheraven};
1990227825Stheraven
1991227825Stheraventemplate <class _Ti, class _TupleUj>
1992227825Stheravenstruct __mu_return
1993227825Stheraven    : public ____mu_return<_Ti,
1994227825Stheraven                           is_bind_expression<_Ti>::value,
1995227825Stheraven                           0 < is_placeholder<_Ti>::value,
1996227825Stheraven                           _TupleUj>
1997227825Stheraven{
1998227825Stheraven};
1999227825Stheraven
2000227825Stheraventemplate <class _Ti, class _TupleUj>
2001227825Stheravenstruct __mu_return<reference_wrapper<_Ti>, _TupleUj>
2002227825Stheraven{
2003227825Stheraven    typedef _Ti& type;
2004227825Stheraven};
2005227825Stheraven
2006232924Stheraventemplate <class _Fp, class _BoundArgs, class _TupleUj>
2007227825Stheravenstruct __bind_return;
2008227825Stheraven
2009232924Stheraventemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
2010232924Stheravenstruct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
2011227825Stheraven{
2012227825Stheraven    typedef typename __ref_return
2013227825Stheraven    <
2014232924Stheraven        _Fp&,
2015227825Stheraven        typename __mu_return
2016227825Stheraven        <
2017227825Stheraven            _BoundArgs,
2018227825Stheraven            _TupleUj
2019227825Stheraven        >::type...
2020227825Stheraven    >::type type;
2021227825Stheraven};
2022227825Stheraven
2023232924Stheraventemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
2024232924Stheravenstruct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
2025227825Stheraven{
2026227825Stheraven    typedef typename __ref_return
2027227825Stheraven    <
2028232924Stheraven        _Fp&,
2029227825Stheraven        typename __mu_return
2030227825Stheraven        <
2031227825Stheraven            const _BoundArgs,
2032227825Stheraven            _TupleUj
2033227825Stheraven        >::type...
2034227825Stheraven    >::type type;
2035227825Stheraven};
2036227825Stheraven
2037232924Stheraventemplate <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
2038227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2039232924Stheraventypename __bind_return<_Fp, _BoundArgs, _Args>::type
2040232924Stheraven__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
2041227825Stheraven                _Args&& __args)
2042227825Stheraven{
2043227825Stheraven    return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
2044227825Stheraven}
2045227825Stheraven
2046232924Stheraventemplate<class _Fp, class ..._BoundArgs>
2047227825Stheravenclass __bind
2048227825Stheraven{
2049232924Stheraven    _Fp __f_;
2050227825Stheraven    tuple<_BoundArgs...> __bound_args_;
2051227825Stheraven
2052227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2053227825Stheravenpublic:
2054232924Stheraven    template <class _Gp, class ..._BA>
2055232924Stheraven      explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2056232924Stheraven        : __f_(_VSTD::forward<_Gp>(__f)),
2057227825Stheraven          __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
2058227825Stheraven
2059227825Stheraven    template <class ..._Args>
2060232924Stheraven        typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2061227825Stheraven        operator()(_Args&& ...__args)
2062227825Stheraven        {
2063227825Stheraven            // compiler bug workaround
2064227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2065227825Stheraven                                  tuple<_Args&&...>(__args...));
2066227825Stheraven        }
2067227825Stheraven
2068227825Stheraven    template <class ..._Args>
2069232924Stheraven        typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2070227825Stheraven        operator()(_Args&& ...__args) const
2071227825Stheraven        {
2072227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2073227825Stheraven                                   tuple<_Args&&...>(__args...));
2074227825Stheraven        }
2075227825Stheraven};
2076227825Stheraven
2077232924Stheraventemplate<class _Fp, class ..._BoundArgs>
2078232924Stheravenstruct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
2079227825Stheraven
2080232924Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2081227825Stheravenclass __bind_r
2082232924Stheraven    : public __bind<_Fp, _BoundArgs...>
2083227825Stheraven{
2084232924Stheraven    typedef __bind<_Fp, _BoundArgs...> base;
2085227825Stheravenpublic:
2086232924Stheraven    typedef _Rp result_type;
2087227825Stheraven
2088232924Stheraven    template <class _Gp, class ..._BA>
2089232924Stheraven      explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2090232924Stheraven        : base(_VSTD::forward<_Gp>(__f),
2091227825Stheraven               _VSTD::forward<_BA>(__bound_args)...) {}
2092227825Stheraven
2093227825Stheraven    template <class ..._Args>
2094227825Stheraven        result_type
2095227825Stheraven        operator()(_Args&& ...__args)
2096227825Stheraven        {
2097227825Stheraven            return base::operator()(_VSTD::forward<_Args>(__args)...);
2098227825Stheraven        }
2099227825Stheraven
2100227825Stheraven    template <class ..._Args>
2101227825Stheraven        result_type
2102227825Stheraven        operator()(_Args&& ...__args) const
2103227825Stheraven        {
2104227825Stheraven            return base::operator()(_VSTD::forward<_Args>(__args)...);
2105227825Stheraven        }
2106227825Stheraven};
2107227825Stheraven
2108232924Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2109232924Stheravenstruct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
2110227825Stheraven
2111232924Stheraventemplate<class _Fp, class ..._BoundArgs>
2112227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2113232924Stheraven__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2114232924Stheravenbind(_Fp&& __f, _BoundArgs&&... __bound_args)
2115227825Stheraven{
2116232924Stheraven    typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2117232924Stheraven    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2118227825Stheraven}
2119227825Stheraven
2120232924Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2121227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2122232924Stheraven__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2123232924Stheravenbind(_Fp&& __f, _BoundArgs&&... __bound_args)
2124227825Stheraven{
2125232924Stheraven    typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2126232924Stheraven    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2127227825Stheraven}
2128227825Stheraven*/
2129227825Stheraven
2130227825Stheraven#endif  // _LIBCPP_FUNCTIONAL_03
2131