__functional_03 revision 278724
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
36262956Sdim       operator() () const
37227825Stheraven       {
38227825Stheraven           return __invoke(__f_);
39227825Stheraven       }
40227825Stheraven
41227825Stheraven    template <class _A0>
42227825Stheraven       typename __invoke_return0<type, _A0>::type
43262956Sdim          operator() (_A0& __a0) const
44227825Stheraven          {
45227825Stheraven              return __invoke(__f_, __a0);
46227825Stheraven          }
47227825Stheraven
48227825Stheraven    template <class _A0, class _A1>
49227825Stheraven       typename __invoke_return1<type, _A0, _A1>::type
50262956Sdim          operator() (_A0& __a0, _A1& __a1) const
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
57262956Sdim          operator() (_A0& __a0, _A1& __a1, _A2& __a2) const
58227825Stheraven          {
59227825Stheraven              return __invoke(__f_, __a0, __a1, __a2);
60227825Stheraven          }
61227825Stheraven};
62227825Stheraven
63232950Stheraventemplate<class _Rp, class _Tp>
64227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
65232950Stheraven__mem_fn<_Rp _Tp::*>
66232950Stheravenmem_fn(_Rp _Tp::* __pm)
67227825Stheraven{
68232950Stheraven    return __mem_fn<_Rp _Tp::*>(__pm);
69227825Stheraven}
70227825Stheraven
71232950Stheraventemplate<class _Rp, class _Tp>
72227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
73232950Stheraven__mem_fn<_Rp (_Tp::*)()>
74232950Stheravenmem_fn(_Rp (_Tp::* __pm)())
75227825Stheraven{
76232950Stheraven    return __mem_fn<_Rp (_Tp::*)()>(__pm);
77227825Stheraven}
78227825Stheraven
79232950Stheraventemplate<class _Rp, class _Tp, class _A0>
80227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
81232950Stheraven__mem_fn<_Rp (_Tp::*)(_A0)>
82232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0))
83227825Stheraven{
84232950Stheraven    return __mem_fn<_Rp (_Tp::*)(_A0)>(__pm);
85227825Stheraven}
86227825Stheraven
87232950Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
88227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
89232950Stheraven__mem_fn<_Rp (_Tp::*)(_A0, _A1)>
90232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1))
91227825Stheraven{
92232950Stheraven    return __mem_fn<_Rp (_Tp::*)(_A0, _A1)>(__pm);
93227825Stheraven}
94227825Stheraven
95232950Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
96227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
97232950Stheraven__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>
98232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2))
99227825Stheraven{
100232950Stheraven    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>(__pm);
101227825Stheraven}
102227825Stheraven
103232950Stheraventemplate<class _Rp, class _Tp>
104227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
105262801Sdim__mem_fn<_Rp (_Tp::*)() const>
106232950Stheravenmem_fn(_Rp (_Tp::* __pm)() const)
107227825Stheraven{
108262801Sdim    return __mem_fn<_Rp (_Tp::*)() const>(__pm);
109227825Stheraven}
110227825Stheraven
111232950Stheraventemplate<class _Rp, class _Tp, class _A0>
112227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
113262801Sdim__mem_fn<_Rp (_Tp::*)(_A0) const>
114232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0) const)
115227825Stheraven{
116262801Sdim    return __mem_fn<_Rp (_Tp::*)(_A0) const>(__pm);
117227825Stheraven}
118227825Stheraven
119232950Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
120227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
121262801Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1) const>
122232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const)
123227825Stheraven{
124262801Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const>(__pm);
125227825Stheraven}
126227825Stheraven
127232950Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
128227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
129262801Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>
130232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const)
131227825Stheraven{
132262801Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>(__pm);
133227825Stheraven}
134227825Stheraven
135232950Stheraventemplate<class _Rp, class _Tp>
136227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
137262801Sdim__mem_fn<_Rp (_Tp::*)() volatile>
138232950Stheravenmem_fn(_Rp (_Tp::* __pm)() volatile)
139227825Stheraven{
140262801Sdim    return __mem_fn<_Rp (_Tp::*)() volatile>(__pm);
141227825Stheraven}
142227825Stheraven
143232950Stheraventemplate<class _Rp, class _Tp, class _A0>
144227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
145262801Sdim__mem_fn<_Rp (_Tp::*)(_A0) volatile>
146232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0) volatile)
147227825Stheraven{
148262801Sdim    return __mem_fn<_Rp (_Tp::*)(_A0) volatile>(__pm);
149227825Stheraven}
150227825Stheraven
151232950Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
152227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
153262801Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>
154232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1) volatile)
155227825Stheraven{
156262801Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>(__pm);
157227825Stheraven}
158227825Stheraven
159232950Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
160227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
161262801Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>
162232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) volatile)
163227825Stheraven{
164262801Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>(__pm);
165227825Stheraven}
166227825Stheraven
167232950Stheraventemplate<class _Rp, class _Tp>
168227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
169262801Sdim__mem_fn<_Rp (_Tp::*)() const volatile>
170232950Stheravenmem_fn(_Rp (_Tp::* __pm)() const volatile)
171227825Stheraven{
172262801Sdim    return __mem_fn<_Rp (_Tp::*)() const volatile>(__pm);
173227825Stheraven}
174227825Stheraven
175232950Stheraventemplate<class _Rp, class _Tp, class _A0>
176227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
177262801Sdim__mem_fn<_Rp (_Tp::*)(_A0) const volatile>
178232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0) const volatile)
179227825Stheraven{
180262801Sdim    return __mem_fn<_Rp (_Tp::*)(_A0) const volatile>(__pm);
181227825Stheraven}
182227825Stheraven
183232950Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
184227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
185262801Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>
186232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const volatile)
187227825Stheraven{
188262801Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>(__pm);
189227825Stheraven}
190227825Stheraven
191232950Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
192227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
193262801Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>
194232950Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const volatile)
195227825Stheraven{
196262801Sdim    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
206262801Sdimtemplate<class _Fp> class _LIBCPP_TYPE_VIS_ONLY function; // undefined
207227825Stheraven
208227825Stheravennamespace __function
209227825Stheraven{
210227825Stheraven
211232950Stheraventemplate<class _Fp>
212227825Stheravenstruct __maybe_derive_from_unary_function
213227825Stheraven{
214227825Stheraven};
215227825Stheraven
216232950Stheraventemplate<class _Rp, class _A1>
217232950Stheravenstruct __maybe_derive_from_unary_function<_Rp(_A1)>
218232950Stheraven    : public unary_function<_A1, _Rp>
219227825Stheraven{
220227825Stheraven};
221227825Stheraven
222232950Stheraventemplate<class _Fp>
223227825Stheravenstruct __maybe_derive_from_binary_function
224227825Stheraven{
225227825Stheraven};
226227825Stheraven
227232950Stheraventemplate<class _Rp, class _A1, class _A2>
228232950Stheravenstruct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
229232950Stheraven    : public binary_function<_A1, _A2, _Rp>
230227825Stheraven{
231227825Stheraven};
232227825Stheraven
233227825Stheraventemplate<class _Fp> class __base;
234227825Stheraven
235232950Stheraventemplate<class _Rp>
236232950Stheravenclass __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;
247232950Stheraven    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
254232950Stheraventemplate<class _Rp, class _A0>
255232950Stheravenclass __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;
266232950Stheraven    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
273232950Stheraventemplate<class _Rp, class _A0, class _A1>
274232950Stheravenclass __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;
285232950Stheraven    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
292232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
293232950Stheravenclass __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;
304232950Stheraven    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
313232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
314232950Stheravenclass __func<_Fp, _Alloc, _Rp()>
315232950Stheraven    : public  __base<_Rp()>
316227825Stheraven{
317232950Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
318227825Stheravenpublic:
319232950Stheraven    explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
320232950Stheraven    explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
321232950Stheraven    virtual __base<_Rp()>* __clone() const;
322232950Stheraven    virtual void __clone(__base<_Rp()>*) const;
323227825Stheraven    virtual void destroy();
324227825Stheraven    virtual void destroy_deallocate();
325232950Stheraven    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
332232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
333232950Stheraven__base<_Rp()>*
334232950Stheraven__func<_Fp, _Alloc, _Rp()>::__clone() const
335227825Stheraven{
336232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
337232950Stheraven    _Ap __a(__f_.second());
338232950Stheraven    typedef __allocator_destructor<_Ap> _Dp;
339232950Stheraven    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
344232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
345227825Stheravenvoid
346232950Stheraven__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
347227825Stheraven{
348227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
349227825Stheraven}
350227825Stheraven
351232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
352227825Stheravenvoid
353232950Stheraven__func<_Fp, _Alloc, _Rp()>::destroy()
354227825Stheraven{
355232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
356227825Stheraven}
357227825Stheraven
358232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
359227825Stheravenvoid
360232950Stheraven__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
361227825Stheraven{
362232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
363232950Stheraven    _Ap __a(__f_.second());
364232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
365227825Stheraven    __a.deallocate(this, 1);
366227825Stheraven}
367227825Stheraven
368232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
369232950Stheraven_Rp
370232950Stheraven__func<_Fp, _Alloc, _Rp()>::operator()()
371227825Stheraven{
372227825Stheraven    return __invoke(__f_.first());
373227825Stheraven}
374227825Stheraven
375227825Stheraven#ifndef _LIBCPP_NO_RTTI
376227825Stheraven
377232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
378227825Stheravenconst void*
379232950Stheraven__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
380227825Stheraven{
381232950Stheraven    if (__ti == typeid(_Fp))
382227825Stheraven        return &__f_.first();
383227825Stheraven    return (const void*)0;
384227825Stheraven}
385227825Stheraven
386232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
387227825Stheravenconst std::type_info&
388232950Stheraven__func<_Fp, _Alloc, _Rp()>::target_type() const
389227825Stheraven{
390232950Stheraven    return typeid(_Fp);
391227825Stheraven}
392227825Stheraven
393227825Stheraven#endif  // _LIBCPP_NO_RTTI
394227825Stheraven
395232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
396232950Stheravenclass __func<_Fp, _Alloc, _Rp(_A0)>
397232950Stheraven    : public  __base<_Rp(_A0)>
398227825Stheraven{
399232950Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
400227825Stheravenpublic:
401232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
402232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
403227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
404232950Stheraven    virtual __base<_Rp(_A0)>* __clone() const;
405232950Stheraven    virtual void __clone(__base<_Rp(_A0)>*) const;
406227825Stheraven    virtual void destroy();
407227825Stheraven    virtual void destroy_deallocate();
408232950Stheraven    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
415232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
416232950Stheraven__base<_Rp(_A0)>*
417232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
418227825Stheraven{
419232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
420232950Stheraven    _Ap __a(__f_.second());
421232950Stheraven    typedef __allocator_destructor<_Ap> _Dp;
422232950Stheraven    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
427232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
428227825Stheravenvoid
429232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
430227825Stheraven{
431227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
432227825Stheraven}
433227825Stheraven
434232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
435227825Stheravenvoid
436232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
437227825Stheraven{
438232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
439227825Stheraven}
440227825Stheraven
441232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
442227825Stheravenvoid
443232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
444227825Stheraven{
445232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
446232950Stheraven    _Ap __a(__f_.second());
447232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
448227825Stheraven    __a.deallocate(this, 1);
449227825Stheraven}
450227825Stheraven
451232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
452232950Stheraven_Rp
453232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
454227825Stheraven{
455227825Stheraven    return __invoke(__f_.first(), __a0);
456227825Stheraven}
457227825Stheraven
458227825Stheraven#ifndef _LIBCPP_NO_RTTI
459227825Stheraven
460232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
461227825Stheravenconst void*
462232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
463227825Stheraven{
464232950Stheraven    if (__ti == typeid(_Fp))
465227825Stheraven        return &__f_.first();
466227825Stheraven    return (const void*)0;
467227825Stheraven}
468227825Stheraven
469232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
470227825Stheravenconst std::type_info&
471232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
472227825Stheraven{
473232950Stheraven    return typeid(_Fp);
474227825Stheraven}
475227825Stheraven
476227825Stheraven#endif  // _LIBCPP_NO_RTTI
477227825Stheraven
478232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
479232950Stheravenclass __func<_Fp, _Alloc, _Rp(_A0, _A1)>
480232950Stheraven    : public  __base<_Rp(_A0, _A1)>
481227825Stheraven{
482232950Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
483227825Stheravenpublic:
484232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
485232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
486227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
487232950Stheraven    virtual __base<_Rp(_A0, _A1)>* __clone() const;
488232950Stheraven    virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
489227825Stheraven    virtual void destroy();
490227825Stheraven    virtual void destroy_deallocate();
491232950Stheraven    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
498232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
499232950Stheraven__base<_Rp(_A0, _A1)>*
500232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
501227825Stheraven{
502232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
503232950Stheraven    _Ap __a(__f_.second());
504232950Stheraven    typedef __allocator_destructor<_Ap> _Dp;
505232950Stheraven    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
510232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
511227825Stheravenvoid
512232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
513227825Stheraven{
514227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
515227825Stheraven}
516227825Stheraven
517232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
518227825Stheravenvoid
519232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
520227825Stheraven{
521232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
522227825Stheraven}
523227825Stheraven
524232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
525227825Stheravenvoid
526232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
527227825Stheraven{
528232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
529232950Stheraven    _Ap __a(__f_.second());
530232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
531227825Stheraven    __a.deallocate(this, 1);
532227825Stheraven}
533227825Stheraven
534232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
535232950Stheraven_Rp
536232950Stheraven__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
543232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
544227825Stheravenconst void*
545232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
546227825Stheraven{
547232950Stheraven    if (__ti == typeid(_Fp))
548227825Stheraven        return &__f_.first();
549227825Stheraven    return (const void*)0;
550227825Stheraven}
551227825Stheraven
552232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
553227825Stheravenconst std::type_info&
554232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
555227825Stheraven{
556232950Stheraven    return typeid(_Fp);
557227825Stheraven}
558227825Stheraven
559227825Stheraven#endif  // _LIBCPP_NO_RTTI
560227825Stheraven
561232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
562232950Stheravenclass __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
563232950Stheraven    : public  __base<_Rp(_A0, _A1, _A2)>
564227825Stheraven{
565232950Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
566227825Stheravenpublic:
567232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
568232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
569227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
570232950Stheraven    virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
571232950Stheraven    virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
572227825Stheraven    virtual void destroy();
573227825Stheraven    virtual void destroy_deallocate();
574232950Stheraven    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
581232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
582232950Stheraven__base<_Rp(_A0, _A1, _A2)>*
583232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
584227825Stheraven{
585232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
586232950Stheraven    _Ap __a(__f_.second());
587232950Stheraven    typedef __allocator_destructor<_Ap> _Dp;
588232950Stheraven    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
593232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
594227825Stheravenvoid
595232950Stheraven__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
600232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
601227825Stheravenvoid
602232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
603227825Stheraven{
604232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
605227825Stheraven}
606227825Stheraven
607232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
608227825Stheravenvoid
609232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
610227825Stheraven{
611232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
612232950Stheraven    _Ap __a(__f_.second());
613232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
614227825Stheraven    __a.deallocate(this, 1);
615227825Stheraven}
616227825Stheraven
617232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
618232950Stheraven_Rp
619232950Stheraven__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
626232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
627227825Stheravenconst void*
628232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
629227825Stheraven{
630232950Stheraven    if (__ti == typeid(_Fp))
631227825Stheraven        return &__f_.first();
632227825Stheraven    return (const void*)0;
633227825Stheraven}
634227825Stheraven
635232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
636227825Stheravenconst std::type_info&
637232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
638227825Stheraven{
639232950Stheraven    return typeid(_Fp);
640227825Stheraven}
641227825Stheraven
642227825Stheraven#endif  // _LIBCPP_NO_RTTI
643227825Stheraven
644227825Stheraven}  // __function
645227825Stheraven
646232950Stheraventemplate<class _Rp>
647262801Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
648227825Stheraven{
649232950Stheraven    typedef __function::__base<_Rp()> __base;
650227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
651227825Stheraven    __base* __f_;
652227825Stheraven
653232950Stheraven    template <class _Fp>
654278724Sdim        _LIBCPP_INLINE_VISIBILITY
655232950Stheraven        static bool __not_null(const _Fp&) {return true;}
656227825Stheraven    template <class _R2>
657278724Sdim        _LIBCPP_INLINE_VISIBILITY
658278724Sdim        static bool __not_null(_R2 (*__p)()) {return __p;}
659278724Sdim    template <class _R2>
660278724Sdim        _LIBCPP_INLINE_VISIBILITY
661278724Sdim        static bool __not_null(const function<_R2()>& __p) {return __p;}
662227825Stheravenpublic:
663232950Stheraven    typedef _Rp result_type;
664227825Stheraven
665227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
666227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
667227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
668227825Stheraven    function(const function&);
669232950Stheraven    template<class _Fp>
670232950Stheraven      function(_Fp,
671232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
672227825Stheraven
673227825Stheraven    template<class _Alloc>
674227825Stheraven      _LIBCPP_INLINE_VISIBILITY
675227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
676227825Stheraven    template<class _Alloc>
677227825Stheraven      _LIBCPP_INLINE_VISIBILITY
678227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
679227825Stheraven    template<class _Alloc>
680227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
681232950Stheraven    template<class _Fp, class _Alloc>
682232950Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
683232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
684227825Stheraven
685227825Stheraven    function& operator=(const function&);
686227825Stheraven    function& operator=(nullptr_t);
687232950Stheraven    template<class _Fp>
688227825Stheraven      typename enable_if
689227825Stheraven      <
690232950Stheraven        !is_integral<_Fp>::value,
691227825Stheraven        function&
692227825Stheraven      >::type
693232950Stheraven      operator=(_Fp);
694227825Stheraven
695227825Stheraven    ~function();
696227825Stheraven
697227825Stheraven    // 20.7.16.2.2, function modifiers:
698227825Stheraven    void swap(function&);
699232950Stheraven    template<class _Fp, class _Alloc>
700227825Stheraven      _LIBCPP_INLINE_VISIBILITY
701232950Stheraven      void assign(_Fp __f, const _Alloc& __a)
702227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
703227825Stheraven
704227825Stheraven    // 20.7.16.2.3, function capacity:
705227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
706227825Stheraven
707227825Stheravenprivate:
708227825Stheraven    // deleted overloads close possible hole in the type system
709227825Stheraven    template<class _R2>
710227825Stheraven      bool operator==(const function<_R2()>&) const;// = delete;
711227825Stheraven    template<class _R2>
712227825Stheraven      bool operator!=(const function<_R2()>&) const;// = delete;
713227825Stheravenpublic:
714227825Stheraven    // 20.7.16.2.4, function invocation:
715232950Stheraven    _Rp operator()() const;
716227825Stheraven
717227825Stheraven#ifndef _LIBCPP_NO_RTTI
718227825Stheraven    // 20.7.16.2.5, function target access:
719227825Stheraven    const std::type_info& target_type() const;
720232950Stheraven    template <typename _Tp> _Tp* target();
721232950Stheraven    template <typename _Tp> const _Tp* target() const;
722227825Stheraven#endif  // _LIBCPP_NO_RTTI
723227825Stheraven};
724227825Stheraven
725232950Stheraventemplate<class _Rp>
726232950Stheravenfunction<_Rp()>::function(const function& __f)
727227825Stheraven{
728227825Stheraven    if (__f.__f_ == 0)
729227825Stheraven        __f_ = 0;
730227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
731227825Stheraven    {
732227825Stheraven        __f_ = (__base*)&__buf_;
733227825Stheraven        __f.__f_->__clone(__f_);
734227825Stheraven    }
735227825Stheraven    else
736227825Stheraven        __f_ = __f.__f_->__clone();
737227825Stheraven}
738227825Stheraven
739232950Stheraventemplate<class _Rp>
740227825Stheraventemplate<class _Alloc>
741232950Stheravenfunction<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
742227825Stheraven{
743227825Stheraven    if (__f.__f_ == 0)
744227825Stheraven        __f_ = 0;
745227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
746227825Stheraven    {
747227825Stheraven        __f_ = (__base*)&__buf_;
748227825Stheraven        __f.__f_->__clone(__f_);
749227825Stheraven    }
750227825Stheraven    else
751227825Stheraven        __f_ = __f.__f_->__clone();
752227825Stheraven}
753227825Stheraven
754232950Stheraventemplate<class _Rp>
755232950Stheraventemplate <class _Fp>
756232950Stheravenfunction<_Rp()>::function(_Fp __f,
757232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
758227825Stheraven    : __f_(0)
759227825Stheraven{
760227825Stheraven    if (__not_null(__f))
761227825Stheraven    {
762232950Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
763227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
764227825Stheraven        {
765227825Stheraven            __f_ = (__base*)&__buf_;
766227825Stheraven            ::new (__f_) _FF(__f);
767227825Stheraven        }
768227825Stheraven        else
769227825Stheraven        {
770232950Stheraven            typedef allocator<_FF> _Ap;
771232950Stheraven            _Ap __a;
772232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
773232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
774232950Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
775227825Stheraven            __f_ = __hold.release();
776227825Stheraven        }
777227825Stheraven    }
778227825Stheraven}
779227825Stheraven
780232950Stheraventemplate<class _Rp>
781232950Stheraventemplate <class _Fp, class _Alloc>
782232950Stheravenfunction<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
783232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
784227825Stheraven    : __f_(0)
785227825Stheraven{
786227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
787227825Stheraven    if (__not_null(__f))
788227825Stheraven    {
789232950Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
790227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
791227825Stheraven        {
792227825Stheraven            __f_ = (__base*)&__buf_;
793227825Stheraven            ::new (__f_) _FF(__f);
794227825Stheraven        }
795227825Stheraven        else
796227825Stheraven        {
797227825Stheraven            typedef typename __alloc_traits::template
798227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
799227825Stheraven                rebind_alloc<_FF>
800227825Stheraven#else
801227825Stheraven                rebind_alloc<_FF>::other
802227825Stheraven#endif
803232950Stheraven                                                         _Ap;
804232950Stheraven            _Ap __a(__a0);
805232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
806232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
807227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
808227825Stheraven            __f_ = __hold.release();
809227825Stheraven        }
810227825Stheraven    }
811227825Stheraven}
812227825Stheraven
813232950Stheraventemplate<class _Rp>
814232950Stheravenfunction<_Rp()>&
815232950Stheravenfunction<_Rp()>::operator=(const function& __f)
816227825Stheraven{
817227825Stheraven    function(__f).swap(*this);
818227825Stheraven    return *this;
819227825Stheraven}
820227825Stheraven
821232950Stheraventemplate<class _Rp>
822232950Stheravenfunction<_Rp()>&
823232950Stheravenfunction<_Rp()>::operator=(nullptr_t)
824227825Stheraven{
825227825Stheraven    if (__f_ == (__base*)&__buf_)
826227825Stheraven        __f_->destroy();
827227825Stheraven    else if (__f_)
828227825Stheraven        __f_->destroy_deallocate();
829227825Stheraven    __f_ = 0;
830227825Stheraven}
831227825Stheraven
832232950Stheraventemplate<class _Rp>
833232950Stheraventemplate <class _Fp>
834227825Stheraventypename enable_if
835227825Stheraven<
836232950Stheraven    !is_integral<_Fp>::value,
837232950Stheraven    function<_Rp()>&
838227825Stheraven>::type
839232950Stheravenfunction<_Rp()>::operator=(_Fp __f)
840227825Stheraven{
841227825Stheraven    function(_VSTD::move(__f)).swap(*this);
842227825Stheraven    return *this;
843227825Stheraven}
844227825Stheraven
845232950Stheraventemplate<class _Rp>
846232950Stheravenfunction<_Rp()>::~function()
847227825Stheraven{
848227825Stheraven    if (__f_ == (__base*)&__buf_)
849227825Stheraven        __f_->destroy();
850227825Stheraven    else if (__f_)
851227825Stheraven        __f_->destroy_deallocate();
852227825Stheraven}
853227825Stheraven
854232950Stheraventemplate<class _Rp>
855227825Stheravenvoid
856232950Stheravenfunction<_Rp()>::swap(function& __f)
857227825Stheraven{
858227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
859227825Stheraven    {
860227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
861227825Stheraven        __base* __t = (__base*)&__tempbuf;
862227825Stheraven        __f_->__clone(__t);
863227825Stheraven        __f_->destroy();
864227825Stheraven        __f_ = 0;
865227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
866227825Stheraven        __f.__f_->destroy();
867227825Stheraven        __f.__f_ = 0;
868227825Stheraven        __f_ = (__base*)&__buf_;
869227825Stheraven        __t->__clone((__base*)&__f.__buf_);
870227825Stheraven        __t->destroy();
871227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
872227825Stheraven    }
873227825Stheraven    else if (__f_ == (__base*)&__buf_)
874227825Stheraven    {
875227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
876227825Stheraven        __f_->destroy();
877227825Stheraven        __f_ = __f.__f_;
878227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
879227825Stheraven    }
880227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
881227825Stheraven    {
882227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
883227825Stheraven        __f.__f_->destroy();
884227825Stheraven        __f.__f_ = __f_;
885227825Stheraven        __f_ = (__base*)&__buf_;
886227825Stheraven    }
887227825Stheraven    else
888227825Stheraven        _VSTD::swap(__f_, __f.__f_);
889227825Stheraven}
890227825Stheraven
891232950Stheraventemplate<class _Rp>
892232950Stheraven_Rp
893232950Stheravenfunction<_Rp()>::operator()() const
894227825Stheraven{
895227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
896227825Stheraven    if (__f_ == 0)
897227825Stheraven        throw bad_function_call();
898227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
899227825Stheraven    return (*__f_)();
900227825Stheraven}
901227825Stheraven
902227825Stheraven#ifndef _LIBCPP_NO_RTTI
903227825Stheraven
904232950Stheraventemplate<class _Rp>
905227825Stheravenconst std::type_info&
906232950Stheravenfunction<_Rp()>::target_type() const
907227825Stheraven{
908227825Stheraven    if (__f_ == 0)
909227825Stheraven        return typeid(void);
910227825Stheraven    return __f_->target_type();
911227825Stheraven}
912227825Stheraven
913232950Stheraventemplate<class _Rp>
914232950Stheraventemplate <typename _Tp>
915232950Stheraven_Tp*
916232950Stheravenfunction<_Rp()>::target()
917227825Stheraven{
918227825Stheraven    if (__f_ == 0)
919232950Stheraven        return (_Tp*)0;
920232950Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
921227825Stheraven}
922227825Stheraven
923232950Stheraventemplate<class _Rp>
924232950Stheraventemplate <typename _Tp>
925232950Stheravenconst _Tp*
926232950Stheravenfunction<_Rp()>::target() const
927227825Stheraven{
928227825Stheraven    if (__f_ == 0)
929232950Stheraven        return (const _Tp*)0;
930232950Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
931227825Stheraven}
932227825Stheraven
933227825Stheraven#endif  // _LIBCPP_NO_RTTI
934227825Stheraven
935232950Stheraventemplate<class _Rp, class _A0>
936262801Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
937232950Stheraven    : public unary_function<_A0, _Rp>
938227825Stheraven{
939232950Stheraven    typedef __function::__base<_Rp(_A0)> __base;
940227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
941227825Stheraven    __base* __f_;
942227825Stheraven
943232950Stheraven    template <class _Fp>
944227825Stheraven        _LIBCPP_INLINE_VISIBILITY
945232950Stheraven        static bool __not_null(const _Fp&) {return true;}
946227825Stheraven    template <class _R2, class _B0>
947227825Stheraven        _LIBCPP_INLINE_VISIBILITY
948227825Stheraven        static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
949232950Stheraven    template <class _R2, class _Cp>
950227825Stheraven        _LIBCPP_INLINE_VISIBILITY
951232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
952232950Stheraven    template <class _R2, class _Cp>
953227825Stheraven        _LIBCPP_INLINE_VISIBILITY
954232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
955232950Stheraven    template <class _R2, class _Cp>
956227825Stheraven        _LIBCPP_INLINE_VISIBILITY
957232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
958232950Stheraven    template <class _R2, class _Cp>
959227825Stheraven        _LIBCPP_INLINE_VISIBILITY
960232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
961227825Stheraven    template <class _R2, class _B0>
962227825Stheraven        _LIBCPP_INLINE_VISIBILITY
963278724Sdim        static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
964227825Stheravenpublic:
965232950Stheraven    typedef _Rp result_type;
966227825Stheraven
967227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
968227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
969227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
970227825Stheraven    function(const function&);
971232950Stheraven    template<class _Fp>
972232950Stheraven      function(_Fp,
973232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
974227825Stheraven
975227825Stheraven    template<class _Alloc>
976227825Stheraven      _LIBCPP_INLINE_VISIBILITY
977227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
978227825Stheraven    template<class _Alloc>
979227825Stheraven      _LIBCPP_INLINE_VISIBILITY
980227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
981227825Stheraven    template<class _Alloc>
982227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
983232950Stheraven    template<class _Fp, class _Alloc>
984232950Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
985232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
986227825Stheraven
987227825Stheraven    function& operator=(const function&);
988227825Stheraven    function& operator=(nullptr_t);
989232950Stheraven    template<class _Fp>
990227825Stheraven      typename enable_if
991227825Stheraven      <
992232950Stheraven        !is_integral<_Fp>::value,
993227825Stheraven        function&
994227825Stheraven      >::type
995232950Stheraven      operator=(_Fp);
996227825Stheraven
997227825Stheraven    ~function();
998227825Stheraven
999227825Stheraven    // 20.7.16.2.2, function modifiers:
1000227825Stheraven    void swap(function&);
1001232950Stheraven    template<class _Fp, class _Alloc>
1002227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1003232950Stheraven      void assign(_Fp __f, const _Alloc& __a)
1004227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1005227825Stheraven
1006227825Stheraven    // 20.7.16.2.3, function capacity:
1007227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1008227825Stheraven
1009227825Stheravenprivate:
1010227825Stheraven    // deleted overloads close possible hole in the type system
1011227825Stheraven    template<class _R2, class _B0>
1012227825Stheraven      bool operator==(const function<_R2(_B0)>&) const;// = delete;
1013227825Stheraven    template<class _R2, class _B0>
1014227825Stheraven      bool operator!=(const function<_R2(_B0)>&) const;// = delete;
1015227825Stheravenpublic:
1016227825Stheraven    // 20.7.16.2.4, function invocation:
1017232950Stheraven    _Rp operator()(_A0) const;
1018227825Stheraven
1019227825Stheraven#ifndef _LIBCPP_NO_RTTI
1020227825Stheraven    // 20.7.16.2.5, function target access:
1021227825Stheraven    const std::type_info& target_type() const;
1022232950Stheraven    template <typename _Tp> _Tp* target();
1023232950Stheraven    template <typename _Tp> const _Tp* target() const;
1024227825Stheraven#endif  // _LIBCPP_NO_RTTI
1025227825Stheraven};
1026227825Stheraven
1027232950Stheraventemplate<class _Rp, class _A0>
1028232950Stheravenfunction<_Rp(_A0)>::function(const function& __f)
1029227825Stheraven{
1030227825Stheraven    if (__f.__f_ == 0)
1031227825Stheraven        __f_ = 0;
1032227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1033227825Stheraven    {
1034227825Stheraven        __f_ = (__base*)&__buf_;
1035227825Stheraven        __f.__f_->__clone(__f_);
1036227825Stheraven    }
1037227825Stheraven    else
1038227825Stheraven        __f_ = __f.__f_->__clone();
1039227825Stheraven}
1040227825Stheraven
1041232950Stheraventemplate<class _Rp, class _A0>
1042227825Stheraventemplate<class _Alloc>
1043232950Stheravenfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1044227825Stheraven{
1045227825Stheraven    if (__f.__f_ == 0)
1046227825Stheraven        __f_ = 0;
1047227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1048227825Stheraven    {
1049227825Stheraven        __f_ = (__base*)&__buf_;
1050227825Stheraven        __f.__f_->__clone(__f_);
1051227825Stheraven    }
1052227825Stheraven    else
1053227825Stheraven        __f_ = __f.__f_->__clone();
1054227825Stheraven}
1055227825Stheraven
1056232950Stheraventemplate<class _Rp, class _A0>
1057232950Stheraventemplate <class _Fp>
1058232950Stheravenfunction<_Rp(_A0)>::function(_Fp __f,
1059232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1060227825Stheraven    : __f_(0)
1061227825Stheraven{
1062227825Stheraven    if (__not_null(__f))
1063227825Stheraven    {
1064232950Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
1065227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1066227825Stheraven        {
1067227825Stheraven            __f_ = (__base*)&__buf_;
1068227825Stheraven            ::new (__f_) _FF(__f);
1069227825Stheraven        }
1070227825Stheraven        else
1071227825Stheraven        {
1072232950Stheraven            typedef allocator<_FF> _Ap;
1073232950Stheraven            _Ap __a;
1074232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1075232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1076232950Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1077227825Stheraven            __f_ = __hold.release();
1078227825Stheraven        }
1079227825Stheraven    }
1080227825Stheraven}
1081227825Stheraven
1082232950Stheraventemplate<class _Rp, class _A0>
1083232950Stheraventemplate <class _Fp, class _Alloc>
1084232950Stheravenfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1085232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1086227825Stheraven    : __f_(0)
1087227825Stheraven{
1088227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1089227825Stheraven    if (__not_null(__f))
1090227825Stheraven    {
1091232950Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
1092227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1093227825Stheraven        {
1094227825Stheraven            __f_ = (__base*)&__buf_;
1095227825Stheraven            ::new (__f_) _FF(__f);
1096227825Stheraven        }
1097227825Stheraven        else
1098227825Stheraven        {
1099227825Stheraven            typedef typename __alloc_traits::template
1100227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1101227825Stheraven                rebind_alloc<_FF>
1102227825Stheraven#else
1103227825Stheraven                rebind_alloc<_FF>::other
1104227825Stheraven#endif
1105232950Stheraven                                                         _Ap;
1106232950Stheraven            _Ap __a(__a0);
1107232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1108232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1109227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1110227825Stheraven            __f_ = __hold.release();
1111227825Stheraven        }
1112227825Stheraven    }
1113227825Stheraven}
1114227825Stheraven
1115232950Stheraventemplate<class _Rp, class _A0>
1116232950Stheravenfunction<_Rp(_A0)>&
1117232950Stheravenfunction<_Rp(_A0)>::operator=(const function& __f)
1118227825Stheraven{
1119227825Stheraven    function(__f).swap(*this);
1120227825Stheraven    return *this;
1121227825Stheraven}
1122227825Stheraven
1123232950Stheraventemplate<class _Rp, class _A0>
1124232950Stheravenfunction<_Rp(_A0)>&
1125232950Stheravenfunction<_Rp(_A0)>::operator=(nullptr_t)
1126227825Stheraven{
1127227825Stheraven    if (__f_ == (__base*)&__buf_)
1128227825Stheraven        __f_->destroy();
1129227825Stheraven    else if (__f_)
1130227825Stheraven        __f_->destroy_deallocate();
1131227825Stheraven    __f_ = 0;
1132227825Stheraven}
1133227825Stheraven
1134232950Stheraventemplate<class _Rp, class _A0>
1135232950Stheraventemplate <class _Fp>
1136227825Stheraventypename enable_if
1137227825Stheraven<
1138232950Stheraven    !is_integral<_Fp>::value,
1139232950Stheraven    function<_Rp(_A0)>&
1140227825Stheraven>::type
1141232950Stheravenfunction<_Rp(_A0)>::operator=(_Fp __f)
1142227825Stheraven{
1143227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1144227825Stheraven    return *this;
1145227825Stheraven}
1146227825Stheraven
1147232950Stheraventemplate<class _Rp, class _A0>
1148232950Stheravenfunction<_Rp(_A0)>::~function()
1149227825Stheraven{
1150227825Stheraven    if (__f_ == (__base*)&__buf_)
1151227825Stheraven        __f_->destroy();
1152227825Stheraven    else if (__f_)
1153227825Stheraven        __f_->destroy_deallocate();
1154227825Stheraven}
1155227825Stheraven
1156232950Stheraventemplate<class _Rp, class _A0>
1157227825Stheravenvoid
1158232950Stheravenfunction<_Rp(_A0)>::swap(function& __f)
1159227825Stheraven{
1160227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1161227825Stheraven    {
1162227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1163227825Stheraven        __base* __t = (__base*)&__tempbuf;
1164227825Stheraven        __f_->__clone(__t);
1165227825Stheraven        __f_->destroy();
1166227825Stheraven        __f_ = 0;
1167227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1168227825Stheraven        __f.__f_->destroy();
1169227825Stheraven        __f.__f_ = 0;
1170227825Stheraven        __f_ = (__base*)&__buf_;
1171227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1172227825Stheraven        __t->destroy();
1173227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1174227825Stheraven    }
1175227825Stheraven    else if (__f_ == (__base*)&__buf_)
1176227825Stheraven    {
1177227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1178227825Stheraven        __f_->destroy();
1179227825Stheraven        __f_ = __f.__f_;
1180227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1181227825Stheraven    }
1182227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1183227825Stheraven    {
1184227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1185227825Stheraven        __f.__f_->destroy();
1186227825Stheraven        __f.__f_ = __f_;
1187227825Stheraven        __f_ = (__base*)&__buf_;
1188227825Stheraven    }
1189227825Stheraven    else
1190227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1191227825Stheraven}
1192227825Stheraven
1193232950Stheraventemplate<class _Rp, class _A0>
1194232950Stheraven_Rp
1195232950Stheravenfunction<_Rp(_A0)>::operator()(_A0 __a0) const
1196227825Stheraven{
1197227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1198227825Stheraven    if (__f_ == 0)
1199227825Stheraven        throw bad_function_call();
1200227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1201227825Stheraven    return (*__f_)(__a0);
1202227825Stheraven}
1203227825Stheraven
1204227825Stheraven#ifndef _LIBCPP_NO_RTTI
1205227825Stheraven
1206232950Stheraventemplate<class _Rp, class _A0>
1207227825Stheravenconst std::type_info&
1208232950Stheravenfunction<_Rp(_A0)>::target_type() const
1209227825Stheraven{
1210227825Stheraven    if (__f_ == 0)
1211227825Stheraven        return typeid(void);
1212227825Stheraven    return __f_->target_type();
1213227825Stheraven}
1214227825Stheraven
1215232950Stheraventemplate<class _Rp, class _A0>
1216232950Stheraventemplate <typename _Tp>
1217232950Stheraven_Tp*
1218232950Stheravenfunction<_Rp(_A0)>::target()
1219227825Stheraven{
1220227825Stheraven    if (__f_ == 0)
1221232950Stheraven        return (_Tp*)0;
1222232950Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1223227825Stheraven}
1224227825Stheraven
1225232950Stheraventemplate<class _Rp, class _A0>
1226232950Stheraventemplate <typename _Tp>
1227232950Stheravenconst _Tp*
1228232950Stheravenfunction<_Rp(_A0)>::target() const
1229227825Stheraven{
1230227825Stheraven    if (__f_ == 0)
1231232950Stheraven        return (const _Tp*)0;
1232232950Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1233227825Stheraven}
1234227825Stheraven
1235227825Stheraven#endif  // _LIBCPP_NO_RTTI
1236227825Stheraven
1237232950Stheraventemplate<class _Rp, class _A0, class _A1>
1238262801Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
1239232950Stheraven    : public binary_function<_A0, _A1, _Rp>
1240227825Stheraven{
1241232950Stheraven    typedef __function::__base<_Rp(_A0, _A1)> __base;
1242227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1243227825Stheraven    __base* __f_;
1244227825Stheraven
1245232950Stheraven    template <class _Fp>
1246227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1247232950Stheraven        static bool __not_null(const _Fp&) {return true;}
1248227825Stheraven    template <class _R2, class _B0, class _B1>
1249227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1250227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
1251232950Stheraven    template <class _R2, class _Cp, class _B1>
1252227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1253232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1254232950Stheraven    template <class _R2, class _Cp, class _B1>
1255227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1256232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1257232950Stheraven    template <class _R2, class _Cp, class _B1>
1258227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1259232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1260232950Stheraven    template <class _R2, class _Cp, class _B1>
1261227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1262232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
1263227825Stheraven    template <class _R2, class _B0, class _B1>
1264227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1265278724Sdim        static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
1266227825Stheravenpublic:
1267232950Stheraven    typedef _Rp result_type;
1268227825Stheraven
1269227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1270227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1271227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1272227825Stheraven    function(const function&);
1273232950Stheraven    template<class _Fp>
1274232950Stheraven      function(_Fp,
1275232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1276227825Stheraven
1277227825Stheraven    template<class _Alloc>
1278227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1279227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1280227825Stheraven    template<class _Alloc>
1281227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1282227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1283227825Stheraven    template<class _Alloc>
1284227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1285232950Stheraven    template<class _Fp, class _Alloc>
1286232950Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1287232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1288227825Stheraven
1289227825Stheraven    function& operator=(const function&);
1290227825Stheraven    function& operator=(nullptr_t);
1291232950Stheraven    template<class _Fp>
1292227825Stheraven      typename enable_if
1293227825Stheraven      <
1294232950Stheraven        !is_integral<_Fp>::value,
1295227825Stheraven        function&
1296227825Stheraven      >::type
1297232950Stheraven      operator=(_Fp);
1298227825Stheraven
1299227825Stheraven    ~function();
1300227825Stheraven
1301227825Stheraven    // 20.7.16.2.2, function modifiers:
1302227825Stheraven    void swap(function&);
1303232950Stheraven    template<class _Fp, class _Alloc>
1304227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1305232950Stheraven      void assign(_Fp __f, const _Alloc& __a)
1306227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1307227825Stheraven
1308227825Stheraven    // 20.7.16.2.3, function capacity:
1309227825Stheraven    operator bool() const {return __f_;}
1310227825Stheraven
1311227825Stheravenprivate:
1312227825Stheraven    // deleted overloads close possible hole in the type system
1313227825Stheraven    template<class _R2, class _B0, class _B1>
1314227825Stheraven      bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
1315227825Stheraven    template<class _R2, class _B0, class _B1>
1316227825Stheraven      bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
1317227825Stheravenpublic:
1318227825Stheraven    // 20.7.16.2.4, function invocation:
1319232950Stheraven    _Rp operator()(_A0, _A1) const;
1320227825Stheraven
1321227825Stheraven#ifndef _LIBCPP_NO_RTTI
1322227825Stheraven    // 20.7.16.2.5, function target access:
1323227825Stheraven    const std::type_info& target_type() const;
1324232950Stheraven    template <typename _Tp> _Tp* target();
1325232950Stheraven    template <typename _Tp> const _Tp* target() const;
1326227825Stheraven#endif  // _LIBCPP_NO_RTTI
1327227825Stheraven};
1328227825Stheraven
1329232950Stheraventemplate<class _Rp, class _A0, class _A1>
1330232950Stheravenfunction<_Rp(_A0, _A1)>::function(const function& __f)
1331227825Stheraven{
1332227825Stheraven    if (__f.__f_ == 0)
1333227825Stheraven        __f_ = 0;
1334227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1335227825Stheraven    {
1336227825Stheraven        __f_ = (__base*)&__buf_;
1337227825Stheraven        __f.__f_->__clone(__f_);
1338227825Stheraven    }
1339227825Stheraven    else
1340227825Stheraven        __f_ = __f.__f_->__clone();
1341227825Stheraven}
1342227825Stheraven
1343232950Stheraventemplate<class _Rp, class _A0, class _A1>
1344227825Stheraventemplate<class _Alloc>
1345232950Stheravenfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1346227825Stheraven{
1347227825Stheraven    if (__f.__f_ == 0)
1348227825Stheraven        __f_ = 0;
1349227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1350227825Stheraven    {
1351227825Stheraven        __f_ = (__base*)&__buf_;
1352227825Stheraven        __f.__f_->__clone(__f_);
1353227825Stheraven    }
1354227825Stheraven    else
1355227825Stheraven        __f_ = __f.__f_->__clone();
1356227825Stheraven}
1357227825Stheraven
1358232950Stheraventemplate<class _Rp, class _A0, class _A1>
1359232950Stheraventemplate <class _Fp>
1360232950Stheravenfunction<_Rp(_A0, _A1)>::function(_Fp __f,
1361232950Stheraven                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1362227825Stheraven    : __f_(0)
1363227825Stheraven{
1364227825Stheraven    if (__not_null(__f))
1365227825Stheraven    {
1366232950Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
1367227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1368227825Stheraven        {
1369227825Stheraven            __f_ = (__base*)&__buf_;
1370227825Stheraven            ::new (__f_) _FF(__f);
1371227825Stheraven        }
1372227825Stheraven        else
1373227825Stheraven        {
1374232950Stheraven            typedef allocator<_FF> _Ap;
1375232950Stheraven            _Ap __a;
1376232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1377232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1378232950Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1379227825Stheraven            __f_ = __hold.release();
1380227825Stheraven        }
1381227825Stheraven    }
1382227825Stheraven}
1383227825Stheraven
1384232950Stheraventemplate<class _Rp, class _A0, class _A1>
1385232950Stheraventemplate <class _Fp, class _Alloc>
1386232950Stheravenfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1387232950Stheraven                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1388227825Stheraven    : __f_(0)
1389227825Stheraven{
1390227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1391227825Stheraven    if (__not_null(__f))
1392227825Stheraven    {
1393232950Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
1394227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1395227825Stheraven        {
1396227825Stheraven            __f_ = (__base*)&__buf_;
1397227825Stheraven            ::new (__f_) _FF(__f);
1398227825Stheraven        }
1399227825Stheraven        else
1400227825Stheraven        {
1401227825Stheraven            typedef typename __alloc_traits::template
1402227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1403227825Stheraven                rebind_alloc<_FF>
1404227825Stheraven#else
1405227825Stheraven                rebind_alloc<_FF>::other
1406227825Stheraven#endif
1407232950Stheraven                                                         _Ap;
1408232950Stheraven            _Ap __a(__a0);
1409232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1410232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1411227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1412227825Stheraven            __f_ = __hold.release();
1413227825Stheraven        }
1414227825Stheraven    }
1415227825Stheraven}
1416227825Stheraven
1417232950Stheraventemplate<class _Rp, class _A0, class _A1>
1418232950Stheravenfunction<_Rp(_A0, _A1)>&
1419232950Stheravenfunction<_Rp(_A0, _A1)>::operator=(const function& __f)
1420227825Stheraven{
1421227825Stheraven    function(__f).swap(*this);
1422227825Stheraven    return *this;
1423227825Stheraven}
1424227825Stheraven
1425232950Stheraventemplate<class _Rp, class _A0, class _A1>
1426232950Stheravenfunction<_Rp(_A0, _A1)>&
1427232950Stheravenfunction<_Rp(_A0, _A1)>::operator=(nullptr_t)
1428227825Stheraven{
1429227825Stheraven    if (__f_ == (__base*)&__buf_)
1430227825Stheraven        __f_->destroy();
1431227825Stheraven    else if (__f_)
1432227825Stheraven        __f_->destroy_deallocate();
1433227825Stheraven    __f_ = 0;
1434227825Stheraven}
1435227825Stheraven
1436232950Stheraventemplate<class _Rp, class _A0, class _A1>
1437232950Stheraventemplate <class _Fp>
1438227825Stheraventypename enable_if
1439227825Stheraven<
1440232950Stheraven    !is_integral<_Fp>::value,
1441232950Stheraven    function<_Rp(_A0, _A1)>&
1442227825Stheraven>::type
1443232950Stheravenfunction<_Rp(_A0, _A1)>::operator=(_Fp __f)
1444227825Stheraven{
1445227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1446227825Stheraven    return *this;
1447227825Stheraven}
1448227825Stheraven
1449232950Stheraventemplate<class _Rp, class _A0, class _A1>
1450232950Stheravenfunction<_Rp(_A0, _A1)>::~function()
1451227825Stheraven{
1452227825Stheraven    if (__f_ == (__base*)&__buf_)
1453227825Stheraven        __f_->destroy();
1454227825Stheraven    else if (__f_)
1455227825Stheraven        __f_->destroy_deallocate();
1456227825Stheraven}
1457227825Stheraven
1458232950Stheraventemplate<class _Rp, class _A0, class _A1>
1459227825Stheravenvoid
1460232950Stheravenfunction<_Rp(_A0, _A1)>::swap(function& __f)
1461227825Stheraven{
1462227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1463227825Stheraven    {
1464227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1465227825Stheraven        __base* __t = (__base*)&__tempbuf;
1466227825Stheraven        __f_->__clone(__t);
1467227825Stheraven        __f_->destroy();
1468227825Stheraven        __f_ = 0;
1469227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1470227825Stheraven        __f.__f_->destroy();
1471227825Stheraven        __f.__f_ = 0;
1472227825Stheraven        __f_ = (__base*)&__buf_;
1473227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1474227825Stheraven        __t->destroy();
1475227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1476227825Stheraven    }
1477227825Stheraven    else if (__f_ == (__base*)&__buf_)
1478227825Stheraven    {
1479227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1480227825Stheraven        __f_->destroy();
1481227825Stheraven        __f_ = __f.__f_;
1482227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1483227825Stheraven    }
1484227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1485227825Stheraven    {
1486227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1487227825Stheraven        __f.__f_->destroy();
1488227825Stheraven        __f.__f_ = __f_;
1489227825Stheraven        __f_ = (__base*)&__buf_;
1490227825Stheraven    }
1491227825Stheraven    else
1492227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1493227825Stheraven}
1494227825Stheraven
1495232950Stheraventemplate<class _Rp, class _A0, class _A1>
1496232950Stheraven_Rp
1497232950Stheravenfunction<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1498227825Stheraven{
1499227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1500227825Stheraven    if (__f_ == 0)
1501227825Stheraven        throw bad_function_call();
1502227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1503227825Stheraven    return (*__f_)(__a0, __a1);
1504227825Stheraven}
1505227825Stheraven
1506227825Stheraven#ifndef _LIBCPP_NO_RTTI
1507227825Stheraven
1508232950Stheraventemplate<class _Rp, class _A0, class _A1>
1509227825Stheravenconst std::type_info&
1510232950Stheravenfunction<_Rp(_A0, _A1)>::target_type() const
1511227825Stheraven{
1512227825Stheraven    if (__f_ == 0)
1513227825Stheraven        return typeid(void);
1514227825Stheraven    return __f_->target_type();
1515227825Stheraven}
1516227825Stheraven
1517232950Stheraventemplate<class _Rp, class _A0, class _A1>
1518232950Stheraventemplate <typename _Tp>
1519232950Stheraven_Tp*
1520232950Stheravenfunction<_Rp(_A0, _A1)>::target()
1521227825Stheraven{
1522227825Stheraven    if (__f_ == 0)
1523232950Stheraven        return (_Tp*)0;
1524232950Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1525227825Stheraven}
1526227825Stheraven
1527232950Stheraventemplate<class _Rp, class _A0, class _A1>
1528232950Stheraventemplate <typename _Tp>
1529232950Stheravenconst _Tp*
1530232950Stheravenfunction<_Rp(_A0, _A1)>::target() const
1531227825Stheraven{
1532227825Stheraven    if (__f_ == 0)
1533232950Stheraven        return (const _Tp*)0;
1534232950Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1535227825Stheraven}
1536227825Stheraven
1537227825Stheraven#endif  // _LIBCPP_NO_RTTI
1538227825Stheraven
1539232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1540262801Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
1541227825Stheraven{
1542232950Stheraven    typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
1543227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1544227825Stheraven    __base* __f_;
1545227825Stheraven
1546232950Stheraven    template <class _Fp>
1547227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1548232950Stheraven        static bool __not_null(const _Fp&) {return true;}
1549227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1550227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1551227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
1552232950Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1553227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1554232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1555232950Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1556227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1557232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1558232950Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1559227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1560232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1561232950Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1562227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1563232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
1564227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1565227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1566278724Sdim        static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
1567227825Stheravenpublic:
1568232950Stheraven    typedef _Rp result_type;
1569227825Stheraven
1570227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1571227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1572227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1573227825Stheraven    function(const function&);
1574232950Stheraven    template<class _Fp>
1575232950Stheraven      function(_Fp,
1576232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1577227825Stheraven
1578227825Stheraven    template<class _Alloc>
1579227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1580227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1581227825Stheraven    template<class _Alloc>
1582227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1583227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1584227825Stheraven    template<class _Alloc>
1585227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1586232950Stheraven    template<class _Fp, class _Alloc>
1587232950Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1588232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1589227825Stheraven
1590227825Stheraven    function& operator=(const function&);
1591227825Stheraven    function& operator=(nullptr_t);
1592232950Stheraven    template<class _Fp>
1593227825Stheraven      typename enable_if
1594227825Stheraven      <
1595232950Stheraven        !is_integral<_Fp>::value,
1596227825Stheraven        function&
1597227825Stheraven      >::type
1598232950Stheraven      operator=(_Fp);
1599227825Stheraven
1600227825Stheraven    ~function();
1601227825Stheraven
1602227825Stheraven    // 20.7.16.2.2, function modifiers:
1603227825Stheraven    void swap(function&);
1604232950Stheraven    template<class _Fp, class _Alloc>
1605227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1606232950Stheraven      void assign(_Fp __f, const _Alloc& __a)
1607227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1608227825Stheraven
1609227825Stheraven    // 20.7.16.2.3, function capacity:
1610227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1611227825Stheraven
1612227825Stheravenprivate:
1613227825Stheraven    // deleted overloads close possible hole in the type system
1614227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1615227825Stheraven      bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1616227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1617227825Stheraven      bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1618227825Stheravenpublic:
1619227825Stheraven    // 20.7.16.2.4, function invocation:
1620232950Stheraven    _Rp operator()(_A0, _A1, _A2) const;
1621227825Stheraven
1622227825Stheraven#ifndef _LIBCPP_NO_RTTI
1623227825Stheraven    // 20.7.16.2.5, function target access:
1624227825Stheraven    const std::type_info& target_type() const;
1625232950Stheraven    template <typename _Tp> _Tp* target();
1626232950Stheraven    template <typename _Tp> const _Tp* target() const;
1627227825Stheraven#endif  // _LIBCPP_NO_RTTI
1628227825Stheraven};
1629227825Stheraven
1630232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1631232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(const function& __f)
1632227825Stheraven{
1633227825Stheraven    if (__f.__f_ == 0)
1634227825Stheraven        __f_ = 0;
1635227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1636227825Stheraven    {
1637227825Stheraven        __f_ = (__base*)&__buf_;
1638227825Stheraven        __f.__f_->__clone(__f_);
1639227825Stheraven    }
1640227825Stheraven    else
1641227825Stheraven        __f_ = __f.__f_->__clone();
1642227825Stheraven}
1643227825Stheraven
1644232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1645227825Stheraventemplate<class _Alloc>
1646232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
1647227825Stheraven                                      const function& __f)
1648227825Stheraven{
1649227825Stheraven    if (__f.__f_ == 0)
1650227825Stheraven        __f_ = 0;
1651227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1652227825Stheraven    {
1653227825Stheraven        __f_ = (__base*)&__buf_;
1654227825Stheraven        __f.__f_->__clone(__f_);
1655227825Stheraven    }
1656227825Stheraven    else
1657227825Stheraven        __f_ = __f.__f_->__clone();
1658227825Stheraven}
1659227825Stheraven
1660232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1661232950Stheraventemplate <class _Fp>
1662232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1663232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1664227825Stheraven    : __f_(0)
1665227825Stheraven{
1666227825Stheraven    if (__not_null(__f))
1667227825Stheraven    {
1668232950Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
1669227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1670227825Stheraven        {
1671227825Stheraven            __f_ = (__base*)&__buf_;
1672227825Stheraven            ::new (__f_) _FF(__f);
1673227825Stheraven        }
1674227825Stheraven        else
1675227825Stheraven        {
1676232950Stheraven            typedef allocator<_FF> _Ap;
1677232950Stheraven            _Ap __a;
1678232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1679232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1680232950Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1681227825Stheraven            __f_ = __hold.release();
1682227825Stheraven        }
1683227825Stheraven    }
1684227825Stheraven}
1685227825Stheraven
1686232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1687232950Stheraventemplate <class _Fp, class _Alloc>
1688232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1689232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1690227825Stheraven    : __f_(0)
1691227825Stheraven{
1692227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1693227825Stheraven    if (__not_null(__f))
1694227825Stheraven    {
1695232950Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
1696227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1697227825Stheraven        {
1698227825Stheraven            __f_ = (__base*)&__buf_;
1699227825Stheraven            ::new (__f_) _FF(__f);
1700227825Stheraven        }
1701227825Stheraven        else
1702227825Stheraven        {
1703227825Stheraven            typedef typename __alloc_traits::template
1704227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1705227825Stheraven                rebind_alloc<_FF>
1706227825Stheraven#else
1707227825Stheraven                rebind_alloc<_FF>::other
1708227825Stheraven#endif
1709232950Stheraven                                                         _Ap;
1710232950Stheraven            _Ap __a(__a0);
1711232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1712232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1713227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1714227825Stheraven            __f_ = __hold.release();
1715227825Stheraven        }
1716227825Stheraven    }
1717227825Stheraven}
1718227825Stheraven
1719232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1720232950Stheravenfunction<_Rp(_A0, _A1, _A2)>&
1721232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
1722227825Stheraven{
1723227825Stheraven    function(__f).swap(*this);
1724227825Stheraven    return *this;
1725227825Stheraven}
1726227825Stheraven
1727232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1728232950Stheravenfunction<_Rp(_A0, _A1, _A2)>&
1729232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
1730227825Stheraven{
1731227825Stheraven    if (__f_ == (__base*)&__buf_)
1732227825Stheraven        __f_->destroy();
1733227825Stheraven    else if (__f_)
1734227825Stheraven        __f_->destroy_deallocate();
1735227825Stheraven    __f_ = 0;
1736227825Stheraven}
1737227825Stheraven
1738232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1739232950Stheraventemplate <class _Fp>
1740227825Stheraventypename enable_if
1741227825Stheraven<
1742232950Stheraven    !is_integral<_Fp>::value,
1743232950Stheraven    function<_Rp(_A0, _A1, _A2)>&
1744227825Stheraven>::type
1745232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
1746227825Stheraven{
1747227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1748227825Stheraven    return *this;
1749227825Stheraven}
1750227825Stheraven
1751232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1752232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::~function()
1753227825Stheraven{
1754227825Stheraven    if (__f_ == (__base*)&__buf_)
1755227825Stheraven        __f_->destroy();
1756227825Stheraven    else if (__f_)
1757227825Stheraven        __f_->destroy_deallocate();
1758227825Stheraven}
1759227825Stheraven
1760232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1761227825Stheravenvoid
1762232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::swap(function& __f)
1763227825Stheraven{
1764227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1765227825Stheraven    {
1766227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1767227825Stheraven        __base* __t = (__base*)&__tempbuf;
1768227825Stheraven        __f_->__clone(__t);
1769227825Stheraven        __f_->destroy();
1770227825Stheraven        __f_ = 0;
1771227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1772227825Stheraven        __f.__f_->destroy();
1773227825Stheraven        __f.__f_ = 0;
1774227825Stheraven        __f_ = (__base*)&__buf_;
1775227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1776227825Stheraven        __t->destroy();
1777227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1778227825Stheraven    }
1779227825Stheraven    else if (__f_ == (__base*)&__buf_)
1780227825Stheraven    {
1781227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1782227825Stheraven        __f_->destroy();
1783227825Stheraven        __f_ = __f.__f_;
1784227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1785227825Stheraven    }
1786227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1787227825Stheraven    {
1788227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1789227825Stheraven        __f.__f_->destroy();
1790227825Stheraven        __f.__f_ = __f_;
1791227825Stheraven        __f_ = (__base*)&__buf_;
1792227825Stheraven    }
1793227825Stheraven    else
1794227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1795227825Stheraven}
1796227825Stheraven
1797232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1798232950Stheraven_Rp
1799232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1800227825Stheraven{
1801227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1802227825Stheraven    if (__f_ == 0)
1803227825Stheraven        throw bad_function_call();
1804227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1805227825Stheraven    return (*__f_)(__a0, __a1, __a2);
1806227825Stheraven}
1807227825Stheraven
1808227825Stheraven#ifndef _LIBCPP_NO_RTTI
1809227825Stheraven
1810232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1811227825Stheravenconst std::type_info&
1812232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::target_type() const
1813227825Stheraven{
1814227825Stheraven    if (__f_ == 0)
1815227825Stheraven        return typeid(void);
1816227825Stheraven    return __f_->target_type();
1817227825Stheraven}
1818227825Stheraven
1819232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1820232950Stheraventemplate <typename _Tp>
1821232950Stheraven_Tp*
1822232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::target()
1823227825Stheraven{
1824227825Stheraven    if (__f_ == 0)
1825232950Stheraven        return (_Tp*)0;
1826232950Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1827227825Stheraven}
1828227825Stheraven
1829232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1830232950Stheraventemplate <typename _Tp>
1831232950Stheravenconst _Tp*
1832232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::target() const
1833227825Stheraven{
1834227825Stheraven    if (__f_ == 0)
1835232950Stheraven        return (const _Tp*)0;
1836232950Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1837227825Stheraven}
1838227825Stheraven
1839227825Stheraven#endif  // _LIBCPP_NO_RTTI
1840227825Stheraven
1841232950Stheraventemplate <class _Fp>
1842227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1843227825Stheravenbool
1844232950Stheravenoperator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
1845227825Stheraven
1846232950Stheraventemplate <class _Fp>
1847227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1848227825Stheravenbool
1849232950Stheravenoperator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
1850227825Stheraven
1851232950Stheraventemplate <class _Fp>
1852227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1853227825Stheravenbool
1854232950Stheravenoperator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
1855227825Stheraven
1856232950Stheraventemplate <class _Fp>
1857227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1858227825Stheravenbool
1859232950Stheravenoperator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
1860227825Stheraven
1861232950Stheraventemplate <class _Fp>
1862227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1863227825Stheravenvoid
1864232950Stheravenswap(function<_Fp>& __x, function<_Fp>& __y)
1865227825Stheraven{return __x.swap(__y);}
1866227825Stheraven
1867227825Stheraventemplate<class _Tp> struct __is_bind_expression : public false_type {};
1868262801Sdimtemplate<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
1869227825Stheraven    : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1870227825Stheraven
1871227825Stheraventemplate<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1872262801Sdimtemplate<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
1873227825Stheraven    : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1874227825Stheraven
1875227825Stheravennamespace placeholders
1876227825Stheraven{
1877227825Stheraven
1878232950Stheraventemplate <int _Np> struct __ph {};
1879227825Stheraven
1880227825Stheravenextern __ph<1>   _1;
1881227825Stheravenextern __ph<2>   _2;
1882227825Stheravenextern __ph<3>   _3;
1883227825Stheravenextern __ph<4>   _4;
1884227825Stheravenextern __ph<5>   _5;
1885227825Stheravenextern __ph<6>   _6;
1886227825Stheravenextern __ph<7>   _7;
1887227825Stheravenextern __ph<8>   _8;
1888227825Stheravenextern __ph<9>   _9;
1889227825Stheravenextern __ph<10> _10;
1890227825Stheraven
1891227825Stheraven}  // placeholders
1892227825Stheraven
1893232950Stheraventemplate<int _Np>
1894232950Stheravenstruct __is_placeholder<placeholders::__ph<_Np> >
1895232950Stheraven    : public integral_constant<int, _Np> {};
1896227825Stheraven
1897227825Stheraventemplate <class _Tp, class _Uj>
1898227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1899227825Stheraven_Tp&
1900227825Stheraven__mu(reference_wrapper<_Tp> __t, _Uj&)
1901227825Stheraven{
1902227825Stheraven    return __t.get();
1903227825Stheraven}
1904227825Stheraven/*
1905227825Stheraventemplate <bool _IsBindExpr, class _Ti, class ..._Uj>
1906227825Stheravenstruct __mu_return1 {};
1907227825Stheraven
1908227825Stheraventemplate <class _Ti, class ..._Uj>
1909227825Stheravenstruct __mu_return1<true, _Ti, _Uj...>
1910227825Stheraven{
1911227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1912227825Stheraven};
1913227825Stheraven
1914227825Stheraventemplate <class _Ti, class ..._Uj, size_t ..._Indx>
1915227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1916227825Stheraventypename __mu_return1<true, _Ti, _Uj...>::type
1917227825Stheraven__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1918227825Stheraven{
1919278724Sdim    __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj))...);
1920227825Stheraven}
1921227825Stheraven
1922227825Stheraventemplate <class _Ti, class ..._Uj>
1923227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1924227825Stheraventypename enable_if
1925227825Stheraven<
1926227825Stheraven    is_bind_expression<_Ti>::value,
1927227825Stheraven    typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1928227825Stheraven>::type
1929227825Stheraven__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1930227825Stheraven{
1931227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1932227825Stheraven    return  __mu_expand(__ti, __uj, __indices());
1933227825Stheraven}
1934227825Stheraven
1935227825Stheraventemplate <bool IsPh, class _Ti, class _Uj>
1936227825Stheravenstruct __mu_return2 {};
1937227825Stheraven
1938227825Stheraventemplate <class _Ti, class _Uj>
1939227825Stheravenstruct __mu_return2<true, _Ti, _Uj>
1940227825Stheraven{
1941227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1942227825Stheraven};
1943227825Stheraven
1944227825Stheraventemplate <class _Ti, class _Uj>
1945227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1946227825Stheraventypename enable_if
1947227825Stheraven<
1948227825Stheraven    0 < is_placeholder<_Ti>::value,
1949227825Stheraven    typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1950227825Stheraven>::type
1951227825Stheraven__mu(_Ti&, _Uj& __uj)
1952227825Stheraven{
1953227825Stheraven    const size_t _Indx = is_placeholder<_Ti>::value - 1;
1954227825Stheraven    // compiler bug workaround
1955278724Sdim    typename tuple_element<_Indx, _Uj>::type __t = _VSTD::get<_Indx>(__uj);
1956227825Stheraven    return __t;
1957278724Sdim//    return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
1958227825Stheraven}
1959227825Stheraven
1960227825Stheraventemplate <class _Ti, class _Uj>
1961227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1962227825Stheraventypename enable_if
1963227825Stheraven<
1964227825Stheraven    !is_bind_expression<_Ti>::value &&
1965227825Stheraven    is_placeholder<_Ti>::value == 0 &&
1966227825Stheraven    !__is_reference_wrapper<_Ti>::value,
1967227825Stheraven    _Ti&
1968227825Stheraven>::type
1969227825Stheraven__mu(_Ti& __ti, _Uj& __uj)
1970227825Stheraven{
1971227825Stheraven    return __ti;
1972227825Stheraven}
1973227825Stheraven
1974227825Stheraventemplate <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1975227825Stheravenstruct ____mu_return;
1976227825Stheraven
1977227825Stheraventemplate <class _Ti, class ..._Uj>
1978227825Stheravenstruct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1979227825Stheraven{
1980227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1981227825Stheraven};
1982227825Stheraven
1983227825Stheraventemplate <class _Ti, class _TupleUj>
1984227825Stheravenstruct ____mu_return<_Ti, false, true, _TupleUj>
1985227825Stheraven{
1986227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1987227825Stheraven                                   _TupleUj>::type&& type;
1988227825Stheraven};
1989227825Stheraven
1990227825Stheraventemplate <class _Ti, class _TupleUj>
1991227825Stheravenstruct ____mu_return<_Ti, false, false, _TupleUj>
1992227825Stheraven{
1993227825Stheraven    typedef _Ti& type;
1994227825Stheraven};
1995227825Stheraven
1996227825Stheraventemplate <class _Ti, class _TupleUj>
1997227825Stheravenstruct __mu_return
1998227825Stheraven    : public ____mu_return<_Ti,
1999227825Stheraven                           is_bind_expression<_Ti>::value,
2000227825Stheraven                           0 < is_placeholder<_Ti>::value,
2001227825Stheraven                           _TupleUj>
2002227825Stheraven{
2003227825Stheraven};
2004227825Stheraven
2005227825Stheraventemplate <class _Ti, class _TupleUj>
2006227825Stheravenstruct __mu_return<reference_wrapper<_Ti>, _TupleUj>
2007227825Stheraven{
2008227825Stheraven    typedef _Ti& type;
2009227825Stheraven};
2010227825Stheraven
2011232950Stheraventemplate <class _Fp, class _BoundArgs, class _TupleUj>
2012227825Stheravenstruct __bind_return;
2013227825Stheraven
2014232950Stheraventemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
2015232950Stheravenstruct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
2016227825Stheraven{
2017227825Stheraven    typedef typename __ref_return
2018227825Stheraven    <
2019232950Stheraven        _Fp&,
2020227825Stheraven        typename __mu_return
2021227825Stheraven        <
2022227825Stheraven            _BoundArgs,
2023227825Stheraven            _TupleUj
2024227825Stheraven        >::type...
2025227825Stheraven    >::type type;
2026227825Stheraven};
2027227825Stheraven
2028232950Stheraventemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
2029232950Stheravenstruct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
2030227825Stheraven{
2031227825Stheraven    typedef typename __ref_return
2032227825Stheraven    <
2033232950Stheraven        _Fp&,
2034227825Stheraven        typename __mu_return
2035227825Stheraven        <
2036227825Stheraven            const _BoundArgs,
2037227825Stheraven            _TupleUj
2038227825Stheraven        >::type...
2039227825Stheraven    >::type type;
2040227825Stheraven};
2041227825Stheraven
2042232950Stheraventemplate <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
2043227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2044232950Stheraventypename __bind_return<_Fp, _BoundArgs, _Args>::type
2045232950Stheraven__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
2046227825Stheraven                _Args&& __args)
2047227825Stheraven{
2048278724Sdim    return __invoke(__f, __mu(_VSTD::get<_Indx>(__bound_args), __args)...);
2049227825Stheraven}
2050227825Stheraven
2051232950Stheraventemplate<class _Fp, class ..._BoundArgs>
2052227825Stheravenclass __bind
2053227825Stheraven{
2054232950Stheraven    _Fp __f_;
2055227825Stheraven    tuple<_BoundArgs...> __bound_args_;
2056227825Stheraven
2057227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2058227825Stheravenpublic:
2059232950Stheraven    template <class _Gp, class ..._BA>
2060232950Stheraven      explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2061232950Stheraven        : __f_(_VSTD::forward<_Gp>(__f)),
2062227825Stheraven          __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
2063227825Stheraven
2064227825Stheraven    template <class ..._Args>
2065232950Stheraven        typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2066227825Stheraven        operator()(_Args&& ...__args)
2067227825Stheraven        {
2068227825Stheraven            // compiler bug workaround
2069227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2070227825Stheraven                                  tuple<_Args&&...>(__args...));
2071227825Stheraven        }
2072227825Stheraven
2073227825Stheraven    template <class ..._Args>
2074232950Stheraven        typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2075227825Stheraven        operator()(_Args&& ...__args) const
2076227825Stheraven        {
2077227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2078227825Stheraven                                   tuple<_Args&&...>(__args...));
2079227825Stheraven        }
2080227825Stheraven};
2081227825Stheraven
2082232950Stheraventemplate<class _Fp, class ..._BoundArgs>
2083232950Stheravenstruct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
2084227825Stheraven
2085232950Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2086227825Stheravenclass __bind_r
2087232950Stheraven    : public __bind<_Fp, _BoundArgs...>
2088227825Stheraven{
2089232950Stheraven    typedef __bind<_Fp, _BoundArgs...> base;
2090227825Stheravenpublic:
2091232950Stheraven    typedef _Rp result_type;
2092227825Stheraven
2093232950Stheraven    template <class _Gp, class ..._BA>
2094232950Stheraven      explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2095232950Stheraven        : base(_VSTD::forward<_Gp>(__f),
2096227825Stheraven               _VSTD::forward<_BA>(__bound_args)...) {}
2097227825Stheraven
2098227825Stheraven    template <class ..._Args>
2099227825Stheraven        result_type
2100227825Stheraven        operator()(_Args&& ...__args)
2101227825Stheraven        {
2102227825Stheraven            return base::operator()(_VSTD::forward<_Args>(__args)...);
2103227825Stheraven        }
2104227825Stheraven
2105227825Stheraven    template <class ..._Args>
2106227825Stheraven        result_type
2107227825Stheraven        operator()(_Args&& ...__args) const
2108227825Stheraven        {
2109227825Stheraven            return base::operator()(_VSTD::forward<_Args>(__args)...);
2110227825Stheraven        }
2111227825Stheraven};
2112227825Stheraven
2113232950Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2114232950Stheravenstruct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
2115227825Stheraven
2116232950Stheraventemplate<class _Fp, class ..._BoundArgs>
2117227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2118232950Stheraven__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2119232950Stheravenbind(_Fp&& __f, _BoundArgs&&... __bound_args)
2120227825Stheraven{
2121232950Stheraven    typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2122232950Stheraven    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2123227825Stheraven}
2124227825Stheraven
2125232950Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2126227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2127232950Stheraven__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2128232950Stheravenbind(_Fp&& __f, _BoundArgs&&... __bound_args)
2129227825Stheraven{
2130232950Stheraven    typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2131232950Stheraven    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2132227825Stheraven}
2133227825Stheraven*/
2134227825Stheraven
2135227825Stheraven#endif  // _LIBCPP_FUNCTIONAL_03
2136