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{
372308143Sdim    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
373308143Sdim    return _Invoker::__call(__f_.first());
374227825Stheraven}
375227825Stheraven
376227825Stheraven#ifndef _LIBCPP_NO_RTTI
377227825Stheraven
378232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
379227825Stheravenconst void*
380232950Stheraven__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
381227825Stheraven{
382232950Stheraven    if (__ti == typeid(_Fp))
383227825Stheraven        return &__f_.first();
384227825Stheraven    return (const void*)0;
385227825Stheraven}
386227825Stheraven
387232950Stheraventemplate<class _Fp, class _Alloc, class _Rp>
388227825Stheravenconst std::type_info&
389232950Stheraven__func<_Fp, _Alloc, _Rp()>::target_type() const
390227825Stheraven{
391232950Stheraven    return typeid(_Fp);
392227825Stheraven}
393227825Stheraven
394227825Stheraven#endif  // _LIBCPP_NO_RTTI
395227825Stheraven
396232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
397232950Stheravenclass __func<_Fp, _Alloc, _Rp(_A0)>
398232950Stheraven    : public  __base<_Rp(_A0)>
399227825Stheraven{
400232950Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
401227825Stheravenpublic:
402232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
403232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
404227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
405232950Stheraven    virtual __base<_Rp(_A0)>* __clone() const;
406232950Stheraven    virtual void __clone(__base<_Rp(_A0)>*) const;
407227825Stheraven    virtual void destroy();
408227825Stheraven    virtual void destroy_deallocate();
409232950Stheraven    virtual _Rp operator()(_A0);
410227825Stheraven#ifndef _LIBCPP_NO_RTTI
411227825Stheraven    virtual const void* target(const type_info&) const;
412227825Stheraven    virtual const std::type_info& target_type() const;
413227825Stheraven#endif  // _LIBCPP_NO_RTTI
414227825Stheraven};
415227825Stheraven
416232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
417232950Stheraven__base<_Rp(_A0)>*
418232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
419227825Stheraven{
420232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
421232950Stheraven    _Ap __a(__f_.second());
422232950Stheraven    typedef __allocator_destructor<_Ap> _Dp;
423232950Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
424227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
425227825Stheraven    return __hold.release();
426227825Stheraven}
427227825Stheraven
428232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
429227825Stheravenvoid
430232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
431227825Stheraven{
432227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
433227825Stheraven}
434227825Stheraven
435232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
436227825Stheravenvoid
437232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
438227825Stheraven{
439232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
440227825Stheraven}
441227825Stheraven
442232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
443227825Stheravenvoid
444232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
445227825Stheraven{
446232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
447232950Stheraven    _Ap __a(__f_.second());
448232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
449227825Stheraven    __a.deallocate(this, 1);
450227825Stheraven}
451227825Stheraven
452232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
453232950Stheraven_Rp
454232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
455227825Stheraven{
456308143Sdim    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
457308143Sdim    return _Invoker::__call(__f_.first(), __a0);
458227825Stheraven}
459227825Stheraven
460227825Stheraven#ifndef _LIBCPP_NO_RTTI
461227825Stheraven
462232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
463227825Stheravenconst void*
464232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
465227825Stheraven{
466232950Stheraven    if (__ti == typeid(_Fp))
467227825Stheraven        return &__f_.first();
468227825Stheraven    return (const void*)0;
469227825Stheraven}
470227825Stheraven
471232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
472227825Stheravenconst std::type_info&
473232950Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
474227825Stheraven{
475232950Stheraven    return typeid(_Fp);
476227825Stheraven}
477227825Stheraven
478227825Stheraven#endif  // _LIBCPP_NO_RTTI
479227825Stheraven
480232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
481232950Stheravenclass __func<_Fp, _Alloc, _Rp(_A0, _A1)>
482232950Stheraven    : public  __base<_Rp(_A0, _A1)>
483227825Stheraven{
484232950Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
485227825Stheravenpublic:
486232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
487232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
488227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
489232950Stheraven    virtual __base<_Rp(_A0, _A1)>* __clone() const;
490232950Stheraven    virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
491227825Stheraven    virtual void destroy();
492227825Stheraven    virtual void destroy_deallocate();
493232950Stheraven    virtual _Rp operator()(_A0, _A1);
494227825Stheraven#ifndef _LIBCPP_NO_RTTI
495227825Stheraven    virtual const void* target(const type_info&) const;
496227825Stheraven    virtual const std::type_info& target_type() const;
497227825Stheraven#endif  // _LIBCPP_NO_RTTI
498227825Stheraven};
499227825Stheraven
500232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
501232950Stheraven__base<_Rp(_A0, _A1)>*
502232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
503227825Stheraven{
504232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
505232950Stheraven    _Ap __a(__f_.second());
506232950Stheraven    typedef __allocator_destructor<_Ap> _Dp;
507232950Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
508227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
509227825Stheraven    return __hold.release();
510227825Stheraven}
511227825Stheraven
512232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
513227825Stheravenvoid
514232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
515227825Stheraven{
516227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
517227825Stheraven}
518227825Stheraven
519232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
520227825Stheravenvoid
521232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
522227825Stheraven{
523232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
524227825Stheraven}
525227825Stheraven
526232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
527227825Stheravenvoid
528232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
529227825Stheraven{
530232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
531232950Stheraven    _Ap __a(__f_.second());
532232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
533227825Stheraven    __a.deallocate(this, 1);
534227825Stheraven}
535227825Stheraven
536232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
537232950Stheraven_Rp
538232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
539227825Stheraven{
540308143Sdim    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
541308143Sdim    return _Invoker::__call(__f_.first(), __a0, __a1);
542227825Stheraven}
543227825Stheraven
544227825Stheraven#ifndef _LIBCPP_NO_RTTI
545227825Stheraven
546232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
547227825Stheravenconst void*
548232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
549227825Stheraven{
550232950Stheraven    if (__ti == typeid(_Fp))
551227825Stheraven        return &__f_.first();
552227825Stheraven    return (const void*)0;
553227825Stheraven}
554227825Stheraven
555232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
556227825Stheravenconst std::type_info&
557232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
558227825Stheraven{
559232950Stheraven    return typeid(_Fp);
560227825Stheraven}
561227825Stheraven
562227825Stheraven#endif  // _LIBCPP_NO_RTTI
563227825Stheraven
564232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
565232950Stheravenclass __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
566232950Stheraven    : public  __base<_Rp(_A0, _A1, _A2)>
567227825Stheraven{
568232950Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
569227825Stheravenpublic:
570232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
571232950Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
572227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
573232950Stheraven    virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
574232950Stheraven    virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
575227825Stheraven    virtual void destroy();
576227825Stheraven    virtual void destroy_deallocate();
577232950Stheraven    virtual _Rp operator()(_A0, _A1, _A2);
578227825Stheraven#ifndef _LIBCPP_NO_RTTI
579227825Stheraven    virtual const void* target(const type_info&) const;
580227825Stheraven    virtual const std::type_info& target_type() const;
581227825Stheraven#endif  // _LIBCPP_NO_RTTI
582227825Stheraven};
583227825Stheraven
584232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
585232950Stheraven__base<_Rp(_A0, _A1, _A2)>*
586232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
587227825Stheraven{
588232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
589232950Stheraven    _Ap __a(__f_.second());
590232950Stheraven    typedef __allocator_destructor<_Ap> _Dp;
591232950Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
592227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
593227825Stheraven    return __hold.release();
594227825Stheraven}
595227825Stheraven
596232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
597227825Stheravenvoid
598232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
599227825Stheraven{
600227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
601227825Stheraven}
602227825Stheraven
603232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
604227825Stheravenvoid
605232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
606227825Stheraven{
607232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
608227825Stheraven}
609227825Stheraven
610232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
611227825Stheravenvoid
612232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
613227825Stheraven{
614232950Stheraven    typedef typename _Alloc::template rebind<__func>::other _Ap;
615232950Stheraven    _Ap __a(__f_.second());
616232950Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
617227825Stheraven    __a.deallocate(this, 1);
618227825Stheraven}
619227825Stheraven
620232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
621232950Stheraven_Rp
622232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
623227825Stheraven{
624308143Sdim    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
625308143Sdim    return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
626227825Stheraven}
627227825Stheraven
628227825Stheraven#ifndef _LIBCPP_NO_RTTI
629227825Stheraven
630232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
631227825Stheravenconst void*
632232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
633227825Stheraven{
634232950Stheraven    if (__ti == typeid(_Fp))
635227825Stheraven        return &__f_.first();
636227825Stheraven    return (const void*)0;
637227825Stheraven}
638227825Stheraven
639232950Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
640227825Stheravenconst std::type_info&
641232950Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
642227825Stheraven{
643232950Stheraven    return typeid(_Fp);
644227825Stheraven}
645227825Stheraven
646227825Stheraven#endif  // _LIBCPP_NO_RTTI
647227825Stheraven
648227825Stheraven}  // __function
649227825Stheraven
650232950Stheraventemplate<class _Rp>
651262801Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
652227825Stheraven{
653232950Stheraven    typedef __function::__base<_Rp()> __base;
654227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
655227825Stheraven    __base* __f_;
656227825Stheraven
657232950Stheraven    template <class _Fp>
658278724Sdim        _LIBCPP_INLINE_VISIBILITY
659232950Stheraven        static bool __not_null(const _Fp&) {return true;}
660227825Stheraven    template <class _R2>
661278724Sdim        _LIBCPP_INLINE_VISIBILITY
662278724Sdim        static bool __not_null(_R2 (*__p)()) {return __p;}
663278724Sdim    template <class _R2>
664278724Sdim        _LIBCPP_INLINE_VISIBILITY
665278724Sdim        static bool __not_null(const function<_R2()>& __p) {return __p;}
666227825Stheravenpublic:
667232950Stheraven    typedef _Rp result_type;
668227825Stheraven
669227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
670227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
671227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
672227825Stheraven    function(const function&);
673232950Stheraven    template<class _Fp>
674232950Stheraven      function(_Fp,
675232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
676227825Stheraven
677227825Stheraven    template<class _Alloc>
678227825Stheraven      _LIBCPP_INLINE_VISIBILITY
679227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
680227825Stheraven    template<class _Alloc>
681227825Stheraven      _LIBCPP_INLINE_VISIBILITY
682227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
683227825Stheraven    template<class _Alloc>
684227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
685232950Stheraven    template<class _Fp, class _Alloc>
686232950Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
687232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
688227825Stheraven
689227825Stheraven    function& operator=(const function&);
690227825Stheraven    function& operator=(nullptr_t);
691232950Stheraven    template<class _Fp>
692227825Stheraven      typename enable_if
693227825Stheraven      <
694232950Stheraven        !is_integral<_Fp>::value,
695227825Stheraven        function&
696227825Stheraven      >::type
697232950Stheraven      operator=(_Fp);
698227825Stheraven
699227825Stheraven    ~function();
700227825Stheraven
701227825Stheraven    // 20.7.16.2.2, function modifiers:
702227825Stheraven    void swap(function&);
703232950Stheraven    template<class _Fp, class _Alloc>
704227825Stheraven      _LIBCPP_INLINE_VISIBILITY
705232950Stheraven      void assign(_Fp __f, const _Alloc& __a)
706227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
707227825Stheraven
708227825Stheraven    // 20.7.16.2.3, function capacity:
709227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
710227825Stheraven
711227825Stheravenprivate:
712227825Stheraven    // deleted overloads close possible hole in the type system
713227825Stheraven    template<class _R2>
714227825Stheraven      bool operator==(const function<_R2()>&) const;// = delete;
715227825Stheraven    template<class _R2>
716227825Stheraven      bool operator!=(const function<_R2()>&) const;// = delete;
717227825Stheravenpublic:
718227825Stheraven    // 20.7.16.2.4, function invocation:
719232950Stheraven    _Rp operator()() const;
720227825Stheraven
721227825Stheraven#ifndef _LIBCPP_NO_RTTI
722227825Stheraven    // 20.7.16.2.5, function target access:
723227825Stheraven    const std::type_info& target_type() const;
724232950Stheraven    template <typename _Tp> _Tp* target();
725232950Stheraven    template <typename _Tp> const _Tp* target() const;
726227825Stheraven#endif  // _LIBCPP_NO_RTTI
727227825Stheraven};
728227825Stheraven
729232950Stheraventemplate<class _Rp>
730232950Stheravenfunction<_Rp()>::function(const function& __f)
731227825Stheraven{
732227825Stheraven    if (__f.__f_ == 0)
733227825Stheraven        __f_ = 0;
734227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
735227825Stheraven    {
736227825Stheraven        __f_ = (__base*)&__buf_;
737227825Stheraven        __f.__f_->__clone(__f_);
738227825Stheraven    }
739227825Stheraven    else
740227825Stheraven        __f_ = __f.__f_->__clone();
741227825Stheraven}
742227825Stheraven
743232950Stheraventemplate<class _Rp>
744227825Stheraventemplate<class _Alloc>
745232950Stheravenfunction<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
746227825Stheraven{
747227825Stheraven    if (__f.__f_ == 0)
748227825Stheraven        __f_ = 0;
749227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
750227825Stheraven    {
751227825Stheraven        __f_ = (__base*)&__buf_;
752227825Stheraven        __f.__f_->__clone(__f_);
753227825Stheraven    }
754227825Stheraven    else
755227825Stheraven        __f_ = __f.__f_->__clone();
756227825Stheraven}
757227825Stheraven
758232950Stheraventemplate<class _Rp>
759232950Stheraventemplate <class _Fp>
760232950Stheravenfunction<_Rp()>::function(_Fp __f,
761232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
762227825Stheraven    : __f_(0)
763227825Stheraven{
764227825Stheraven    if (__not_null(__f))
765227825Stheraven    {
766232950Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
767227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
768227825Stheraven        {
769227825Stheraven            __f_ = (__base*)&__buf_;
770227825Stheraven            ::new (__f_) _FF(__f);
771227825Stheraven        }
772227825Stheraven        else
773227825Stheraven        {
774232950Stheraven            typedef allocator<_FF> _Ap;
775232950Stheraven            _Ap __a;
776232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
777232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
778232950Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
779227825Stheraven            __f_ = __hold.release();
780227825Stheraven        }
781227825Stheraven    }
782227825Stheraven}
783227825Stheraven
784232950Stheraventemplate<class _Rp>
785232950Stheraventemplate <class _Fp, class _Alloc>
786232950Stheravenfunction<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
787232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
788227825Stheraven    : __f_(0)
789227825Stheraven{
790227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
791227825Stheraven    if (__not_null(__f))
792227825Stheraven    {
793232950Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
794227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
795227825Stheraven        {
796227825Stheraven            __f_ = (__base*)&__buf_;
797227825Stheraven            ::new (__f_) _FF(__f);
798227825Stheraven        }
799227825Stheraven        else
800227825Stheraven        {
801227825Stheraven            typedef typename __alloc_traits::template
802227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
803227825Stheraven                rebind_alloc<_FF>
804227825Stheraven#else
805227825Stheraven                rebind_alloc<_FF>::other
806227825Stheraven#endif
807232950Stheraven                                                         _Ap;
808232950Stheraven            _Ap __a(__a0);
809232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
810232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
811227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
812227825Stheraven            __f_ = __hold.release();
813227825Stheraven        }
814227825Stheraven    }
815227825Stheraven}
816227825Stheraven
817232950Stheraventemplate<class _Rp>
818232950Stheravenfunction<_Rp()>&
819232950Stheravenfunction<_Rp()>::operator=(const function& __f)
820227825Stheraven{
821227825Stheraven    function(__f).swap(*this);
822227825Stheraven    return *this;
823227825Stheraven}
824227825Stheraven
825232950Stheraventemplate<class _Rp>
826232950Stheravenfunction<_Rp()>&
827232950Stheravenfunction<_Rp()>::operator=(nullptr_t)
828227825Stheraven{
829227825Stheraven    if (__f_ == (__base*)&__buf_)
830227825Stheraven        __f_->destroy();
831227825Stheraven    else if (__f_)
832227825Stheraven        __f_->destroy_deallocate();
833227825Stheraven    __f_ = 0;
834227825Stheraven}
835227825Stheraven
836232950Stheraventemplate<class _Rp>
837232950Stheraventemplate <class _Fp>
838227825Stheraventypename enable_if
839227825Stheraven<
840232950Stheraven    !is_integral<_Fp>::value,
841232950Stheraven    function<_Rp()>&
842227825Stheraven>::type
843232950Stheravenfunction<_Rp()>::operator=(_Fp __f)
844227825Stheraven{
845227825Stheraven    function(_VSTD::move(__f)).swap(*this);
846227825Stheraven    return *this;
847227825Stheraven}
848227825Stheraven
849232950Stheraventemplate<class _Rp>
850232950Stheravenfunction<_Rp()>::~function()
851227825Stheraven{
852227825Stheraven    if (__f_ == (__base*)&__buf_)
853227825Stheraven        __f_->destroy();
854227825Stheraven    else if (__f_)
855227825Stheraven        __f_->destroy_deallocate();
856227825Stheraven}
857227825Stheraven
858232950Stheraventemplate<class _Rp>
859227825Stheravenvoid
860232950Stheravenfunction<_Rp()>::swap(function& __f)
861227825Stheraven{
862227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
863227825Stheraven    {
864227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
865227825Stheraven        __base* __t = (__base*)&__tempbuf;
866227825Stheraven        __f_->__clone(__t);
867227825Stheraven        __f_->destroy();
868227825Stheraven        __f_ = 0;
869227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
870227825Stheraven        __f.__f_->destroy();
871227825Stheraven        __f.__f_ = 0;
872227825Stheraven        __f_ = (__base*)&__buf_;
873227825Stheraven        __t->__clone((__base*)&__f.__buf_);
874227825Stheraven        __t->destroy();
875227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
876227825Stheraven    }
877227825Stheraven    else if (__f_ == (__base*)&__buf_)
878227825Stheraven    {
879227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
880227825Stheraven        __f_->destroy();
881227825Stheraven        __f_ = __f.__f_;
882227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
883227825Stheraven    }
884227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
885227825Stheraven    {
886227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
887227825Stheraven        __f.__f_->destroy();
888227825Stheraven        __f.__f_ = __f_;
889227825Stheraven        __f_ = (__base*)&__buf_;
890227825Stheraven    }
891227825Stheraven    else
892227825Stheraven        _VSTD::swap(__f_, __f.__f_);
893227825Stheraven}
894227825Stheraven
895232950Stheraventemplate<class _Rp>
896232950Stheraven_Rp
897232950Stheravenfunction<_Rp()>::operator()() const
898227825Stheraven{
899227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
900227825Stheraven    if (__f_ == 0)
901227825Stheraven        throw bad_function_call();
902227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
903227825Stheraven    return (*__f_)();
904227825Stheraven}
905227825Stheraven
906227825Stheraven#ifndef _LIBCPP_NO_RTTI
907227825Stheraven
908232950Stheraventemplate<class _Rp>
909227825Stheravenconst std::type_info&
910232950Stheravenfunction<_Rp()>::target_type() const
911227825Stheraven{
912227825Stheraven    if (__f_ == 0)
913227825Stheraven        return typeid(void);
914227825Stheraven    return __f_->target_type();
915227825Stheraven}
916227825Stheraven
917232950Stheraventemplate<class _Rp>
918232950Stheraventemplate <typename _Tp>
919232950Stheraven_Tp*
920232950Stheravenfunction<_Rp()>::target()
921227825Stheraven{
922227825Stheraven    if (__f_ == 0)
923232950Stheraven        return (_Tp*)0;
924232950Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
925227825Stheraven}
926227825Stheraven
927232950Stheraventemplate<class _Rp>
928232950Stheraventemplate <typename _Tp>
929232950Stheravenconst _Tp*
930232950Stheravenfunction<_Rp()>::target() const
931227825Stheraven{
932227825Stheraven    if (__f_ == 0)
933232950Stheraven        return (const _Tp*)0;
934232950Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
935227825Stheraven}
936227825Stheraven
937227825Stheraven#endif  // _LIBCPP_NO_RTTI
938227825Stheraven
939232950Stheraventemplate<class _Rp, class _A0>
940262801Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
941232950Stheraven    : public unary_function<_A0, _Rp>
942227825Stheraven{
943232950Stheraven    typedef __function::__base<_Rp(_A0)> __base;
944227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
945227825Stheraven    __base* __f_;
946227825Stheraven
947232950Stheraven    template <class _Fp>
948227825Stheraven        _LIBCPP_INLINE_VISIBILITY
949232950Stheraven        static bool __not_null(const _Fp&) {return true;}
950227825Stheraven    template <class _R2, class _B0>
951227825Stheraven        _LIBCPP_INLINE_VISIBILITY
952227825Stheraven        static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
953232950Stheraven    template <class _R2, class _Cp>
954227825Stheraven        _LIBCPP_INLINE_VISIBILITY
955232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
956232950Stheraven    template <class _R2, class _Cp>
957227825Stheraven        _LIBCPP_INLINE_VISIBILITY
958232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
959232950Stheraven    template <class _R2, class _Cp>
960227825Stheraven        _LIBCPP_INLINE_VISIBILITY
961232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
962232950Stheraven    template <class _R2, class _Cp>
963227825Stheraven        _LIBCPP_INLINE_VISIBILITY
964232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
965227825Stheraven    template <class _R2, class _B0>
966227825Stheraven        _LIBCPP_INLINE_VISIBILITY
967278724Sdim        static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
968227825Stheravenpublic:
969232950Stheraven    typedef _Rp result_type;
970227825Stheraven
971227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
972227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
973227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
974227825Stheraven    function(const function&);
975232950Stheraven    template<class _Fp>
976232950Stheraven      function(_Fp,
977232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
978227825Stheraven
979227825Stheraven    template<class _Alloc>
980227825Stheraven      _LIBCPP_INLINE_VISIBILITY
981227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
982227825Stheraven    template<class _Alloc>
983227825Stheraven      _LIBCPP_INLINE_VISIBILITY
984227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
985227825Stheraven    template<class _Alloc>
986227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
987232950Stheraven    template<class _Fp, class _Alloc>
988232950Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
989232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
990227825Stheraven
991227825Stheraven    function& operator=(const function&);
992227825Stheraven    function& operator=(nullptr_t);
993232950Stheraven    template<class _Fp>
994227825Stheraven      typename enable_if
995227825Stheraven      <
996232950Stheraven        !is_integral<_Fp>::value,
997227825Stheraven        function&
998227825Stheraven      >::type
999232950Stheraven      operator=(_Fp);
1000227825Stheraven
1001227825Stheraven    ~function();
1002227825Stheraven
1003227825Stheraven    // 20.7.16.2.2, function modifiers:
1004227825Stheraven    void swap(function&);
1005232950Stheraven    template<class _Fp, class _Alloc>
1006227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1007232950Stheraven      void assign(_Fp __f, const _Alloc& __a)
1008227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1009227825Stheraven
1010227825Stheraven    // 20.7.16.2.3, function capacity:
1011227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1012227825Stheraven
1013227825Stheravenprivate:
1014227825Stheraven    // deleted overloads close possible hole in the type system
1015227825Stheraven    template<class _R2, class _B0>
1016227825Stheraven      bool operator==(const function<_R2(_B0)>&) const;// = delete;
1017227825Stheraven    template<class _R2, class _B0>
1018227825Stheraven      bool operator!=(const function<_R2(_B0)>&) const;// = delete;
1019227825Stheravenpublic:
1020227825Stheraven    // 20.7.16.2.4, function invocation:
1021232950Stheraven    _Rp operator()(_A0) const;
1022227825Stheraven
1023227825Stheraven#ifndef _LIBCPP_NO_RTTI
1024227825Stheraven    // 20.7.16.2.5, function target access:
1025227825Stheraven    const std::type_info& target_type() const;
1026232950Stheraven    template <typename _Tp> _Tp* target();
1027232950Stheraven    template <typename _Tp> const _Tp* target() const;
1028227825Stheraven#endif  // _LIBCPP_NO_RTTI
1029227825Stheraven};
1030227825Stheraven
1031232950Stheraventemplate<class _Rp, class _A0>
1032232950Stheravenfunction<_Rp(_A0)>::function(const function& __f)
1033227825Stheraven{
1034227825Stheraven    if (__f.__f_ == 0)
1035227825Stheraven        __f_ = 0;
1036227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1037227825Stheraven    {
1038227825Stheraven        __f_ = (__base*)&__buf_;
1039227825Stheraven        __f.__f_->__clone(__f_);
1040227825Stheraven    }
1041227825Stheraven    else
1042227825Stheraven        __f_ = __f.__f_->__clone();
1043227825Stheraven}
1044227825Stheraven
1045232950Stheraventemplate<class _Rp, class _A0>
1046227825Stheraventemplate<class _Alloc>
1047232950Stheravenfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1048227825Stheraven{
1049227825Stheraven    if (__f.__f_ == 0)
1050227825Stheraven        __f_ = 0;
1051227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1052227825Stheraven    {
1053227825Stheraven        __f_ = (__base*)&__buf_;
1054227825Stheraven        __f.__f_->__clone(__f_);
1055227825Stheraven    }
1056227825Stheraven    else
1057227825Stheraven        __f_ = __f.__f_->__clone();
1058227825Stheraven}
1059227825Stheraven
1060232950Stheraventemplate<class _Rp, class _A0>
1061232950Stheraventemplate <class _Fp>
1062232950Stheravenfunction<_Rp(_A0)>::function(_Fp __f,
1063232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1064227825Stheraven    : __f_(0)
1065227825Stheraven{
1066227825Stheraven    if (__not_null(__f))
1067227825Stheraven    {
1068232950Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
1069227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1070227825Stheraven        {
1071227825Stheraven            __f_ = (__base*)&__buf_;
1072227825Stheraven            ::new (__f_) _FF(__f);
1073227825Stheraven        }
1074227825Stheraven        else
1075227825Stheraven        {
1076232950Stheraven            typedef allocator<_FF> _Ap;
1077232950Stheraven            _Ap __a;
1078232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1079232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1080232950Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1081227825Stheraven            __f_ = __hold.release();
1082227825Stheraven        }
1083227825Stheraven    }
1084227825Stheraven}
1085227825Stheraven
1086232950Stheraventemplate<class _Rp, class _A0>
1087232950Stheraventemplate <class _Fp, class _Alloc>
1088232950Stheravenfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1089232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1090227825Stheraven    : __f_(0)
1091227825Stheraven{
1092227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1093227825Stheraven    if (__not_null(__f))
1094227825Stheraven    {
1095232950Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
1096227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1097227825Stheraven        {
1098227825Stheraven            __f_ = (__base*)&__buf_;
1099227825Stheraven            ::new (__f_) _FF(__f);
1100227825Stheraven        }
1101227825Stheraven        else
1102227825Stheraven        {
1103227825Stheraven            typedef typename __alloc_traits::template
1104227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1105227825Stheraven                rebind_alloc<_FF>
1106227825Stheraven#else
1107227825Stheraven                rebind_alloc<_FF>::other
1108227825Stheraven#endif
1109232950Stheraven                                                         _Ap;
1110232950Stheraven            _Ap __a(__a0);
1111232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1112232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1113227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1114227825Stheraven            __f_ = __hold.release();
1115227825Stheraven        }
1116227825Stheraven    }
1117227825Stheraven}
1118227825Stheraven
1119232950Stheraventemplate<class _Rp, class _A0>
1120232950Stheravenfunction<_Rp(_A0)>&
1121232950Stheravenfunction<_Rp(_A0)>::operator=(const function& __f)
1122227825Stheraven{
1123227825Stheraven    function(__f).swap(*this);
1124227825Stheraven    return *this;
1125227825Stheraven}
1126227825Stheraven
1127232950Stheraventemplate<class _Rp, class _A0>
1128232950Stheravenfunction<_Rp(_A0)>&
1129232950Stheravenfunction<_Rp(_A0)>::operator=(nullptr_t)
1130227825Stheraven{
1131227825Stheraven    if (__f_ == (__base*)&__buf_)
1132227825Stheraven        __f_->destroy();
1133227825Stheraven    else if (__f_)
1134227825Stheraven        __f_->destroy_deallocate();
1135227825Stheraven    __f_ = 0;
1136227825Stheraven}
1137227825Stheraven
1138232950Stheraventemplate<class _Rp, class _A0>
1139232950Stheraventemplate <class _Fp>
1140227825Stheraventypename enable_if
1141227825Stheraven<
1142232950Stheraven    !is_integral<_Fp>::value,
1143232950Stheraven    function<_Rp(_A0)>&
1144227825Stheraven>::type
1145232950Stheravenfunction<_Rp(_A0)>::operator=(_Fp __f)
1146227825Stheraven{
1147227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1148227825Stheraven    return *this;
1149227825Stheraven}
1150227825Stheraven
1151232950Stheraventemplate<class _Rp, class _A0>
1152232950Stheravenfunction<_Rp(_A0)>::~function()
1153227825Stheraven{
1154227825Stheraven    if (__f_ == (__base*)&__buf_)
1155227825Stheraven        __f_->destroy();
1156227825Stheraven    else if (__f_)
1157227825Stheraven        __f_->destroy_deallocate();
1158227825Stheraven}
1159227825Stheraven
1160232950Stheraventemplate<class _Rp, class _A0>
1161227825Stheravenvoid
1162232950Stheravenfunction<_Rp(_A0)>::swap(function& __f)
1163227825Stheraven{
1164227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1165227825Stheraven    {
1166227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1167227825Stheraven        __base* __t = (__base*)&__tempbuf;
1168227825Stheraven        __f_->__clone(__t);
1169227825Stheraven        __f_->destroy();
1170227825Stheraven        __f_ = 0;
1171227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1172227825Stheraven        __f.__f_->destroy();
1173227825Stheraven        __f.__f_ = 0;
1174227825Stheraven        __f_ = (__base*)&__buf_;
1175227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1176227825Stheraven        __t->destroy();
1177227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1178227825Stheraven    }
1179227825Stheraven    else if (__f_ == (__base*)&__buf_)
1180227825Stheraven    {
1181227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1182227825Stheraven        __f_->destroy();
1183227825Stheraven        __f_ = __f.__f_;
1184227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1185227825Stheraven    }
1186227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1187227825Stheraven    {
1188227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1189227825Stheraven        __f.__f_->destroy();
1190227825Stheraven        __f.__f_ = __f_;
1191227825Stheraven        __f_ = (__base*)&__buf_;
1192227825Stheraven    }
1193227825Stheraven    else
1194227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1195227825Stheraven}
1196227825Stheraven
1197232950Stheraventemplate<class _Rp, class _A0>
1198232950Stheraven_Rp
1199232950Stheravenfunction<_Rp(_A0)>::operator()(_A0 __a0) const
1200227825Stheraven{
1201227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1202227825Stheraven    if (__f_ == 0)
1203227825Stheraven        throw bad_function_call();
1204227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1205227825Stheraven    return (*__f_)(__a0);
1206227825Stheraven}
1207227825Stheraven
1208227825Stheraven#ifndef _LIBCPP_NO_RTTI
1209227825Stheraven
1210232950Stheraventemplate<class _Rp, class _A0>
1211227825Stheravenconst std::type_info&
1212232950Stheravenfunction<_Rp(_A0)>::target_type() const
1213227825Stheraven{
1214227825Stheraven    if (__f_ == 0)
1215227825Stheraven        return typeid(void);
1216227825Stheraven    return __f_->target_type();
1217227825Stheraven}
1218227825Stheraven
1219232950Stheraventemplate<class _Rp, class _A0>
1220232950Stheraventemplate <typename _Tp>
1221232950Stheraven_Tp*
1222232950Stheravenfunction<_Rp(_A0)>::target()
1223227825Stheraven{
1224227825Stheraven    if (__f_ == 0)
1225232950Stheraven        return (_Tp*)0;
1226232950Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1227227825Stheraven}
1228227825Stheraven
1229232950Stheraventemplate<class _Rp, class _A0>
1230232950Stheraventemplate <typename _Tp>
1231232950Stheravenconst _Tp*
1232232950Stheravenfunction<_Rp(_A0)>::target() const
1233227825Stheraven{
1234227825Stheraven    if (__f_ == 0)
1235232950Stheraven        return (const _Tp*)0;
1236232950Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1237227825Stheraven}
1238227825Stheraven
1239227825Stheraven#endif  // _LIBCPP_NO_RTTI
1240227825Stheraven
1241232950Stheraventemplate<class _Rp, class _A0, class _A1>
1242262801Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
1243232950Stheraven    : public binary_function<_A0, _A1, _Rp>
1244227825Stheraven{
1245232950Stheraven    typedef __function::__base<_Rp(_A0, _A1)> __base;
1246227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1247227825Stheraven    __base* __f_;
1248227825Stheraven
1249232950Stheraven    template <class _Fp>
1250227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1251232950Stheraven        static bool __not_null(const _Fp&) {return true;}
1252227825Stheraven    template <class _R2, class _B0, class _B1>
1253227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1254227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
1255232950Stheraven    template <class _R2, class _Cp, class _B1>
1256227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1257232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1258232950Stheraven    template <class _R2, class _Cp, class _B1>
1259227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1260232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1261232950Stheraven    template <class _R2, class _Cp, class _B1>
1262227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1263232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1264232950Stheraven    template <class _R2, class _Cp, class _B1>
1265227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1266232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
1267227825Stheraven    template <class _R2, class _B0, class _B1>
1268227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1269278724Sdim        static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
1270227825Stheravenpublic:
1271232950Stheraven    typedef _Rp result_type;
1272227825Stheraven
1273227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1274227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1275227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1276227825Stheraven    function(const function&);
1277232950Stheraven    template<class _Fp>
1278232950Stheraven      function(_Fp,
1279232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1280227825Stheraven
1281227825Stheraven    template<class _Alloc>
1282227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1283227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1284227825Stheraven    template<class _Alloc>
1285227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1286227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1287227825Stheraven    template<class _Alloc>
1288227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1289232950Stheraven    template<class _Fp, class _Alloc>
1290232950Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1291232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1292227825Stheraven
1293227825Stheraven    function& operator=(const function&);
1294227825Stheraven    function& operator=(nullptr_t);
1295232950Stheraven    template<class _Fp>
1296227825Stheraven      typename enable_if
1297227825Stheraven      <
1298232950Stheraven        !is_integral<_Fp>::value,
1299227825Stheraven        function&
1300227825Stheraven      >::type
1301232950Stheraven      operator=(_Fp);
1302227825Stheraven
1303227825Stheraven    ~function();
1304227825Stheraven
1305227825Stheraven    // 20.7.16.2.2, function modifiers:
1306227825Stheraven    void swap(function&);
1307232950Stheraven    template<class _Fp, class _Alloc>
1308227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1309232950Stheraven      void assign(_Fp __f, const _Alloc& __a)
1310227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1311227825Stheraven
1312227825Stheraven    // 20.7.16.2.3, function capacity:
1313227825Stheraven    operator bool() const {return __f_;}
1314227825Stheraven
1315227825Stheravenprivate:
1316227825Stheraven    // deleted overloads close possible hole in the type system
1317227825Stheraven    template<class _R2, class _B0, class _B1>
1318227825Stheraven      bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
1319227825Stheraven    template<class _R2, class _B0, class _B1>
1320227825Stheraven      bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
1321227825Stheravenpublic:
1322227825Stheraven    // 20.7.16.2.4, function invocation:
1323232950Stheraven    _Rp operator()(_A0, _A1) const;
1324227825Stheraven
1325227825Stheraven#ifndef _LIBCPP_NO_RTTI
1326227825Stheraven    // 20.7.16.2.5, function target access:
1327227825Stheraven    const std::type_info& target_type() const;
1328232950Stheraven    template <typename _Tp> _Tp* target();
1329232950Stheraven    template <typename _Tp> const _Tp* target() const;
1330227825Stheraven#endif  // _LIBCPP_NO_RTTI
1331227825Stheraven};
1332227825Stheraven
1333232950Stheraventemplate<class _Rp, class _A0, class _A1>
1334232950Stheravenfunction<_Rp(_A0, _A1)>::function(const function& __f)
1335227825Stheraven{
1336227825Stheraven    if (__f.__f_ == 0)
1337227825Stheraven        __f_ = 0;
1338227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1339227825Stheraven    {
1340227825Stheraven        __f_ = (__base*)&__buf_;
1341227825Stheraven        __f.__f_->__clone(__f_);
1342227825Stheraven    }
1343227825Stheraven    else
1344227825Stheraven        __f_ = __f.__f_->__clone();
1345227825Stheraven}
1346227825Stheraven
1347232950Stheraventemplate<class _Rp, class _A0, class _A1>
1348227825Stheraventemplate<class _Alloc>
1349232950Stheravenfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1350227825Stheraven{
1351227825Stheraven    if (__f.__f_ == 0)
1352227825Stheraven        __f_ = 0;
1353227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1354227825Stheraven    {
1355227825Stheraven        __f_ = (__base*)&__buf_;
1356227825Stheraven        __f.__f_->__clone(__f_);
1357227825Stheraven    }
1358227825Stheraven    else
1359227825Stheraven        __f_ = __f.__f_->__clone();
1360227825Stheraven}
1361227825Stheraven
1362232950Stheraventemplate<class _Rp, class _A0, class _A1>
1363232950Stheraventemplate <class _Fp>
1364232950Stheravenfunction<_Rp(_A0, _A1)>::function(_Fp __f,
1365232950Stheraven                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1366227825Stheraven    : __f_(0)
1367227825Stheraven{
1368227825Stheraven    if (__not_null(__f))
1369227825Stheraven    {
1370232950Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
1371227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1372227825Stheraven        {
1373227825Stheraven            __f_ = (__base*)&__buf_;
1374227825Stheraven            ::new (__f_) _FF(__f);
1375227825Stheraven        }
1376227825Stheraven        else
1377227825Stheraven        {
1378232950Stheraven            typedef allocator<_FF> _Ap;
1379232950Stheraven            _Ap __a;
1380232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1381232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1382232950Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1383227825Stheraven            __f_ = __hold.release();
1384227825Stheraven        }
1385227825Stheraven    }
1386227825Stheraven}
1387227825Stheraven
1388232950Stheraventemplate<class _Rp, class _A0, class _A1>
1389232950Stheraventemplate <class _Fp, class _Alloc>
1390232950Stheravenfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1391232950Stheraven                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1392227825Stheraven    : __f_(0)
1393227825Stheraven{
1394227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1395227825Stheraven    if (__not_null(__f))
1396227825Stheraven    {
1397232950Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
1398227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1399227825Stheraven        {
1400227825Stheraven            __f_ = (__base*)&__buf_;
1401227825Stheraven            ::new (__f_) _FF(__f);
1402227825Stheraven        }
1403227825Stheraven        else
1404227825Stheraven        {
1405227825Stheraven            typedef typename __alloc_traits::template
1406227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1407227825Stheraven                rebind_alloc<_FF>
1408227825Stheraven#else
1409227825Stheraven                rebind_alloc<_FF>::other
1410227825Stheraven#endif
1411232950Stheraven                                                         _Ap;
1412232950Stheraven            _Ap __a(__a0);
1413232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1414232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1415227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1416227825Stheraven            __f_ = __hold.release();
1417227825Stheraven        }
1418227825Stheraven    }
1419227825Stheraven}
1420227825Stheraven
1421232950Stheraventemplate<class _Rp, class _A0, class _A1>
1422232950Stheravenfunction<_Rp(_A0, _A1)>&
1423232950Stheravenfunction<_Rp(_A0, _A1)>::operator=(const function& __f)
1424227825Stheraven{
1425227825Stheraven    function(__f).swap(*this);
1426227825Stheraven    return *this;
1427227825Stheraven}
1428227825Stheraven
1429232950Stheraventemplate<class _Rp, class _A0, class _A1>
1430232950Stheravenfunction<_Rp(_A0, _A1)>&
1431232950Stheravenfunction<_Rp(_A0, _A1)>::operator=(nullptr_t)
1432227825Stheraven{
1433227825Stheraven    if (__f_ == (__base*)&__buf_)
1434227825Stheraven        __f_->destroy();
1435227825Stheraven    else if (__f_)
1436227825Stheraven        __f_->destroy_deallocate();
1437227825Stheraven    __f_ = 0;
1438227825Stheraven}
1439227825Stheraven
1440232950Stheraventemplate<class _Rp, class _A0, class _A1>
1441232950Stheraventemplate <class _Fp>
1442227825Stheraventypename enable_if
1443227825Stheraven<
1444232950Stheraven    !is_integral<_Fp>::value,
1445232950Stheraven    function<_Rp(_A0, _A1)>&
1446227825Stheraven>::type
1447232950Stheravenfunction<_Rp(_A0, _A1)>::operator=(_Fp __f)
1448227825Stheraven{
1449227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1450227825Stheraven    return *this;
1451227825Stheraven}
1452227825Stheraven
1453232950Stheraventemplate<class _Rp, class _A0, class _A1>
1454232950Stheravenfunction<_Rp(_A0, _A1)>::~function()
1455227825Stheraven{
1456227825Stheraven    if (__f_ == (__base*)&__buf_)
1457227825Stheraven        __f_->destroy();
1458227825Stheraven    else if (__f_)
1459227825Stheraven        __f_->destroy_deallocate();
1460227825Stheraven}
1461227825Stheraven
1462232950Stheraventemplate<class _Rp, class _A0, class _A1>
1463227825Stheravenvoid
1464232950Stheravenfunction<_Rp(_A0, _A1)>::swap(function& __f)
1465227825Stheraven{
1466227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1467227825Stheraven    {
1468227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1469227825Stheraven        __base* __t = (__base*)&__tempbuf;
1470227825Stheraven        __f_->__clone(__t);
1471227825Stheraven        __f_->destroy();
1472227825Stheraven        __f_ = 0;
1473227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1474227825Stheraven        __f.__f_->destroy();
1475227825Stheraven        __f.__f_ = 0;
1476227825Stheraven        __f_ = (__base*)&__buf_;
1477227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1478227825Stheraven        __t->destroy();
1479227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1480227825Stheraven    }
1481227825Stheraven    else if (__f_ == (__base*)&__buf_)
1482227825Stheraven    {
1483227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1484227825Stheraven        __f_->destroy();
1485227825Stheraven        __f_ = __f.__f_;
1486227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1487227825Stheraven    }
1488227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1489227825Stheraven    {
1490227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1491227825Stheraven        __f.__f_->destroy();
1492227825Stheraven        __f.__f_ = __f_;
1493227825Stheraven        __f_ = (__base*)&__buf_;
1494227825Stheraven    }
1495227825Stheraven    else
1496227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1497227825Stheraven}
1498227825Stheraven
1499232950Stheraventemplate<class _Rp, class _A0, class _A1>
1500232950Stheraven_Rp
1501232950Stheravenfunction<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1502227825Stheraven{
1503227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1504227825Stheraven    if (__f_ == 0)
1505227825Stheraven        throw bad_function_call();
1506227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1507227825Stheraven    return (*__f_)(__a0, __a1);
1508227825Stheraven}
1509227825Stheraven
1510227825Stheraven#ifndef _LIBCPP_NO_RTTI
1511227825Stheraven
1512232950Stheraventemplate<class _Rp, class _A0, class _A1>
1513227825Stheravenconst std::type_info&
1514232950Stheravenfunction<_Rp(_A0, _A1)>::target_type() const
1515227825Stheraven{
1516227825Stheraven    if (__f_ == 0)
1517227825Stheraven        return typeid(void);
1518227825Stheraven    return __f_->target_type();
1519227825Stheraven}
1520227825Stheraven
1521232950Stheraventemplate<class _Rp, class _A0, class _A1>
1522232950Stheraventemplate <typename _Tp>
1523232950Stheraven_Tp*
1524232950Stheravenfunction<_Rp(_A0, _A1)>::target()
1525227825Stheraven{
1526227825Stheraven    if (__f_ == 0)
1527232950Stheraven        return (_Tp*)0;
1528232950Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1529227825Stheraven}
1530227825Stheraven
1531232950Stheraventemplate<class _Rp, class _A0, class _A1>
1532232950Stheraventemplate <typename _Tp>
1533232950Stheravenconst _Tp*
1534232950Stheravenfunction<_Rp(_A0, _A1)>::target() const
1535227825Stheraven{
1536227825Stheraven    if (__f_ == 0)
1537232950Stheraven        return (const _Tp*)0;
1538232950Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1539227825Stheraven}
1540227825Stheraven
1541227825Stheraven#endif  // _LIBCPP_NO_RTTI
1542227825Stheraven
1543232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1544262801Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
1545227825Stheraven{
1546232950Stheraven    typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
1547227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1548227825Stheraven    __base* __f_;
1549227825Stheraven
1550232950Stheraven    template <class _Fp>
1551227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1552232950Stheraven        static bool __not_null(const _Fp&) {return true;}
1553227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1554227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1555227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
1556232950Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1557227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1558232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1559232950Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1560227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1561232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1562232950Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1563227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1564232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1565232950Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1566227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1567232950Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
1568227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1569227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1570278724Sdim        static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
1571227825Stheravenpublic:
1572232950Stheraven    typedef _Rp result_type;
1573227825Stheraven
1574227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1575227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1576227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1577227825Stheraven    function(const function&);
1578232950Stheraven    template<class _Fp>
1579232950Stheraven      function(_Fp,
1580232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1581227825Stheraven
1582227825Stheraven    template<class _Alloc>
1583227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1584227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1585227825Stheraven    template<class _Alloc>
1586227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1587227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1588227825Stheraven    template<class _Alloc>
1589227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1590232950Stheraven    template<class _Fp, class _Alloc>
1591232950Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1592232950Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1593227825Stheraven
1594227825Stheraven    function& operator=(const function&);
1595227825Stheraven    function& operator=(nullptr_t);
1596232950Stheraven    template<class _Fp>
1597227825Stheraven      typename enable_if
1598227825Stheraven      <
1599232950Stheraven        !is_integral<_Fp>::value,
1600227825Stheraven        function&
1601227825Stheraven      >::type
1602232950Stheraven      operator=(_Fp);
1603227825Stheraven
1604227825Stheraven    ~function();
1605227825Stheraven
1606227825Stheraven    // 20.7.16.2.2, function modifiers:
1607227825Stheraven    void swap(function&);
1608232950Stheraven    template<class _Fp, class _Alloc>
1609227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1610232950Stheraven      void assign(_Fp __f, const _Alloc& __a)
1611227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1612227825Stheraven
1613227825Stheraven    // 20.7.16.2.3, function capacity:
1614227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1615227825Stheraven
1616227825Stheravenprivate:
1617227825Stheraven    // deleted overloads close possible hole in the type system
1618227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1619227825Stheraven      bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1620227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1621227825Stheraven      bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1622227825Stheravenpublic:
1623227825Stheraven    // 20.7.16.2.4, function invocation:
1624232950Stheraven    _Rp operator()(_A0, _A1, _A2) const;
1625227825Stheraven
1626227825Stheraven#ifndef _LIBCPP_NO_RTTI
1627227825Stheraven    // 20.7.16.2.5, function target access:
1628227825Stheraven    const std::type_info& target_type() const;
1629232950Stheraven    template <typename _Tp> _Tp* target();
1630232950Stheraven    template <typename _Tp> const _Tp* target() const;
1631227825Stheraven#endif  // _LIBCPP_NO_RTTI
1632227825Stheraven};
1633227825Stheraven
1634232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1635232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(const function& __f)
1636227825Stheraven{
1637227825Stheraven    if (__f.__f_ == 0)
1638227825Stheraven        __f_ = 0;
1639227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1640227825Stheraven    {
1641227825Stheraven        __f_ = (__base*)&__buf_;
1642227825Stheraven        __f.__f_->__clone(__f_);
1643227825Stheraven    }
1644227825Stheraven    else
1645227825Stheraven        __f_ = __f.__f_->__clone();
1646227825Stheraven}
1647227825Stheraven
1648232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1649227825Stheraventemplate<class _Alloc>
1650232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
1651227825Stheraven                                      const function& __f)
1652227825Stheraven{
1653227825Stheraven    if (__f.__f_ == 0)
1654227825Stheraven        __f_ = 0;
1655227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1656227825Stheraven    {
1657227825Stheraven        __f_ = (__base*)&__buf_;
1658227825Stheraven        __f.__f_->__clone(__f_);
1659227825Stheraven    }
1660227825Stheraven    else
1661227825Stheraven        __f_ = __f.__f_->__clone();
1662227825Stheraven}
1663227825Stheraven
1664232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1665232950Stheraventemplate <class _Fp>
1666232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1667232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1668227825Stheraven    : __f_(0)
1669227825Stheraven{
1670227825Stheraven    if (__not_null(__f))
1671227825Stheraven    {
1672232950Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
1673227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1674227825Stheraven        {
1675227825Stheraven            __f_ = (__base*)&__buf_;
1676227825Stheraven            ::new (__f_) _FF(__f);
1677227825Stheraven        }
1678227825Stheraven        else
1679227825Stheraven        {
1680232950Stheraven            typedef allocator<_FF> _Ap;
1681232950Stheraven            _Ap __a;
1682232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1683232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1684232950Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1685227825Stheraven            __f_ = __hold.release();
1686227825Stheraven        }
1687227825Stheraven    }
1688227825Stheraven}
1689227825Stheraven
1690232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1691232950Stheraventemplate <class _Fp, class _Alloc>
1692232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1693232950Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1694227825Stheraven    : __f_(0)
1695227825Stheraven{
1696227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1697227825Stheraven    if (__not_null(__f))
1698227825Stheraven    {
1699232950Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
1700227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1701227825Stheraven        {
1702227825Stheraven            __f_ = (__base*)&__buf_;
1703227825Stheraven            ::new (__f_) _FF(__f);
1704227825Stheraven        }
1705227825Stheraven        else
1706227825Stheraven        {
1707227825Stheraven            typedef typename __alloc_traits::template
1708227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1709227825Stheraven                rebind_alloc<_FF>
1710227825Stheraven#else
1711227825Stheraven                rebind_alloc<_FF>::other
1712227825Stheraven#endif
1713232950Stheraven                                                         _Ap;
1714232950Stheraven            _Ap __a(__a0);
1715232950Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1716232950Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1717227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1718227825Stheraven            __f_ = __hold.release();
1719227825Stheraven        }
1720227825Stheraven    }
1721227825Stheraven}
1722227825Stheraven
1723232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1724232950Stheravenfunction<_Rp(_A0, _A1, _A2)>&
1725232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
1726227825Stheraven{
1727227825Stheraven    function(__f).swap(*this);
1728227825Stheraven    return *this;
1729227825Stheraven}
1730227825Stheraven
1731232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1732232950Stheravenfunction<_Rp(_A0, _A1, _A2)>&
1733232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
1734227825Stheraven{
1735227825Stheraven    if (__f_ == (__base*)&__buf_)
1736227825Stheraven        __f_->destroy();
1737227825Stheraven    else if (__f_)
1738227825Stheraven        __f_->destroy_deallocate();
1739227825Stheraven    __f_ = 0;
1740227825Stheraven}
1741227825Stheraven
1742232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1743232950Stheraventemplate <class _Fp>
1744227825Stheraventypename enable_if
1745227825Stheraven<
1746232950Stheraven    !is_integral<_Fp>::value,
1747232950Stheraven    function<_Rp(_A0, _A1, _A2)>&
1748227825Stheraven>::type
1749232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
1750227825Stheraven{
1751227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1752227825Stheraven    return *this;
1753227825Stheraven}
1754227825Stheraven
1755232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1756232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::~function()
1757227825Stheraven{
1758227825Stheraven    if (__f_ == (__base*)&__buf_)
1759227825Stheraven        __f_->destroy();
1760227825Stheraven    else if (__f_)
1761227825Stheraven        __f_->destroy_deallocate();
1762227825Stheraven}
1763227825Stheraven
1764232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1765227825Stheravenvoid
1766232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::swap(function& __f)
1767227825Stheraven{
1768227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1769227825Stheraven    {
1770227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1771227825Stheraven        __base* __t = (__base*)&__tempbuf;
1772227825Stheraven        __f_->__clone(__t);
1773227825Stheraven        __f_->destroy();
1774227825Stheraven        __f_ = 0;
1775227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1776227825Stheraven        __f.__f_->destroy();
1777227825Stheraven        __f.__f_ = 0;
1778227825Stheraven        __f_ = (__base*)&__buf_;
1779227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1780227825Stheraven        __t->destroy();
1781227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1782227825Stheraven    }
1783227825Stheraven    else if (__f_ == (__base*)&__buf_)
1784227825Stheraven    {
1785227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1786227825Stheraven        __f_->destroy();
1787227825Stheraven        __f_ = __f.__f_;
1788227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1789227825Stheraven    }
1790227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1791227825Stheraven    {
1792227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1793227825Stheraven        __f.__f_->destroy();
1794227825Stheraven        __f.__f_ = __f_;
1795227825Stheraven        __f_ = (__base*)&__buf_;
1796227825Stheraven    }
1797227825Stheraven    else
1798227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1799227825Stheraven}
1800227825Stheraven
1801232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1802232950Stheraven_Rp
1803232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1804227825Stheraven{
1805227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1806227825Stheraven    if (__f_ == 0)
1807227825Stheraven        throw bad_function_call();
1808227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1809227825Stheraven    return (*__f_)(__a0, __a1, __a2);
1810227825Stheraven}
1811227825Stheraven
1812227825Stheraven#ifndef _LIBCPP_NO_RTTI
1813227825Stheraven
1814232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1815227825Stheravenconst std::type_info&
1816232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::target_type() const
1817227825Stheraven{
1818227825Stheraven    if (__f_ == 0)
1819227825Stheraven        return typeid(void);
1820227825Stheraven    return __f_->target_type();
1821227825Stheraven}
1822227825Stheraven
1823232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1824232950Stheraventemplate <typename _Tp>
1825232950Stheraven_Tp*
1826232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::target()
1827227825Stheraven{
1828227825Stheraven    if (__f_ == 0)
1829232950Stheraven        return (_Tp*)0;
1830232950Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1831227825Stheraven}
1832227825Stheraven
1833232950Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1834232950Stheraventemplate <typename _Tp>
1835232950Stheravenconst _Tp*
1836232950Stheravenfunction<_Rp(_A0, _A1, _A2)>::target() const
1837227825Stheraven{
1838227825Stheraven    if (__f_ == 0)
1839232950Stheraven        return (const _Tp*)0;
1840232950Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1841227825Stheraven}
1842227825Stheraven
1843227825Stheraven#endif  // _LIBCPP_NO_RTTI
1844227825Stheraven
1845232950Stheraventemplate <class _Fp>
1846227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1847227825Stheravenbool
1848232950Stheravenoperator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
1849227825Stheraven
1850232950Stheraventemplate <class _Fp>
1851227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1852227825Stheravenbool
1853232950Stheravenoperator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
1854227825Stheraven
1855232950Stheraventemplate <class _Fp>
1856227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1857227825Stheravenbool
1858232950Stheravenoperator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
1859227825Stheraven
1860232950Stheraventemplate <class _Fp>
1861227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1862227825Stheravenbool
1863232950Stheravenoperator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
1864227825Stheraven
1865232950Stheraventemplate <class _Fp>
1866227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1867227825Stheravenvoid
1868232950Stheravenswap(function<_Fp>& __x, function<_Fp>& __y)
1869227825Stheraven{return __x.swap(__y);}
1870227825Stheraven
1871227825Stheraventemplate<class _Tp> struct __is_bind_expression : public false_type {};
1872262801Sdimtemplate<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
1873227825Stheraven    : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1874227825Stheraven
1875227825Stheraventemplate<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1876262801Sdimtemplate<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
1877227825Stheraven    : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1878227825Stheraven
1879227825Stheravennamespace placeholders
1880227825Stheraven{
1881227825Stheraven
1882232950Stheraventemplate <int _Np> struct __ph {};
1883227825Stheraven
1884227825Stheravenextern __ph<1>   _1;
1885227825Stheravenextern __ph<2>   _2;
1886227825Stheravenextern __ph<3>   _3;
1887227825Stheravenextern __ph<4>   _4;
1888227825Stheravenextern __ph<5>   _5;
1889227825Stheravenextern __ph<6>   _6;
1890227825Stheravenextern __ph<7>   _7;
1891227825Stheravenextern __ph<8>   _8;
1892227825Stheravenextern __ph<9>   _9;
1893227825Stheravenextern __ph<10> _10;
1894227825Stheraven
1895227825Stheraven}  // placeholders
1896227825Stheraven
1897232950Stheraventemplate<int _Np>
1898232950Stheravenstruct __is_placeholder<placeholders::__ph<_Np> >
1899232950Stheraven    : public integral_constant<int, _Np> {};
1900227825Stheraven
1901227825Stheraventemplate <class _Tp, class _Uj>
1902227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1903227825Stheraven_Tp&
1904227825Stheraven__mu(reference_wrapper<_Tp> __t, _Uj&)
1905227825Stheraven{
1906227825Stheraven    return __t.get();
1907227825Stheraven}
1908227825Stheraven/*
1909227825Stheraventemplate <bool _IsBindExpr, class _Ti, class ..._Uj>
1910227825Stheravenstruct __mu_return1 {};
1911227825Stheraven
1912227825Stheraventemplate <class _Ti, class ..._Uj>
1913227825Stheravenstruct __mu_return1<true, _Ti, _Uj...>
1914227825Stheraven{
1915227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1916227825Stheraven};
1917227825Stheraven
1918227825Stheraventemplate <class _Ti, class ..._Uj, size_t ..._Indx>
1919227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1920227825Stheraventypename __mu_return1<true, _Ti, _Uj...>::type
1921227825Stheraven__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1922227825Stheraven{
1923278724Sdim    __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj))...);
1924227825Stheraven}
1925227825Stheraven
1926227825Stheraventemplate <class _Ti, class ..._Uj>
1927227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1928227825Stheraventypename enable_if
1929227825Stheraven<
1930227825Stheraven    is_bind_expression<_Ti>::value,
1931227825Stheraven    typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1932227825Stheraven>::type
1933227825Stheraven__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1934227825Stheraven{
1935227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1936227825Stheraven    return  __mu_expand(__ti, __uj, __indices());
1937227825Stheraven}
1938227825Stheraven
1939227825Stheraventemplate <bool IsPh, class _Ti, class _Uj>
1940227825Stheravenstruct __mu_return2 {};
1941227825Stheraven
1942227825Stheraventemplate <class _Ti, class _Uj>
1943227825Stheravenstruct __mu_return2<true, _Ti, _Uj>
1944227825Stheraven{
1945227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1946227825Stheraven};
1947227825Stheraven
1948227825Stheraventemplate <class _Ti, class _Uj>
1949227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1950227825Stheraventypename enable_if
1951227825Stheraven<
1952227825Stheraven    0 < is_placeholder<_Ti>::value,
1953227825Stheraven    typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1954227825Stheraven>::type
1955227825Stheraven__mu(_Ti&, _Uj& __uj)
1956227825Stheraven{
1957227825Stheraven    const size_t _Indx = is_placeholder<_Ti>::value - 1;
1958227825Stheraven    // compiler bug workaround
1959278724Sdim    typename tuple_element<_Indx, _Uj>::type __t = _VSTD::get<_Indx>(__uj);
1960227825Stheraven    return __t;
1961278724Sdim//    return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
1962227825Stheraven}
1963227825Stheraven
1964227825Stheraventemplate <class _Ti, class _Uj>
1965227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1966227825Stheraventypename enable_if
1967227825Stheraven<
1968227825Stheraven    !is_bind_expression<_Ti>::value &&
1969227825Stheraven    is_placeholder<_Ti>::value == 0 &&
1970227825Stheraven    !__is_reference_wrapper<_Ti>::value,
1971227825Stheraven    _Ti&
1972227825Stheraven>::type
1973227825Stheraven__mu(_Ti& __ti, _Uj& __uj)
1974227825Stheraven{
1975227825Stheraven    return __ti;
1976227825Stheraven}
1977227825Stheraven
1978227825Stheraventemplate <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1979227825Stheravenstruct ____mu_return;
1980227825Stheraven
1981227825Stheraventemplate <class _Ti, class ..._Uj>
1982227825Stheravenstruct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1983227825Stheraven{
1984227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1985227825Stheraven};
1986227825Stheraven
1987227825Stheraventemplate <class _Ti, class _TupleUj>
1988227825Stheravenstruct ____mu_return<_Ti, false, true, _TupleUj>
1989227825Stheraven{
1990227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1991227825Stheraven                                   _TupleUj>::type&& type;
1992227825Stheraven};
1993227825Stheraven
1994227825Stheraventemplate <class _Ti, class _TupleUj>
1995227825Stheravenstruct ____mu_return<_Ti, false, false, _TupleUj>
1996227825Stheraven{
1997227825Stheraven    typedef _Ti& type;
1998227825Stheraven};
1999227825Stheraven
2000227825Stheraventemplate <class _Ti, class _TupleUj>
2001227825Stheravenstruct __mu_return
2002227825Stheraven    : public ____mu_return<_Ti,
2003227825Stheraven                           is_bind_expression<_Ti>::value,
2004227825Stheraven                           0 < is_placeholder<_Ti>::value,
2005227825Stheraven                           _TupleUj>
2006227825Stheraven{
2007227825Stheraven};
2008227825Stheraven
2009227825Stheraventemplate <class _Ti, class _TupleUj>
2010227825Stheravenstruct __mu_return<reference_wrapper<_Ti>, _TupleUj>
2011227825Stheraven{
2012227825Stheraven    typedef _Ti& type;
2013227825Stheraven};
2014227825Stheraven
2015232950Stheraventemplate <class _Fp, class _BoundArgs, class _TupleUj>
2016227825Stheravenstruct __bind_return;
2017227825Stheraven
2018232950Stheraventemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
2019232950Stheravenstruct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
2020227825Stheraven{
2021227825Stheraven    typedef typename __ref_return
2022227825Stheraven    <
2023232950Stheraven        _Fp&,
2024227825Stheraven        typename __mu_return
2025227825Stheraven        <
2026227825Stheraven            _BoundArgs,
2027227825Stheraven            _TupleUj
2028227825Stheraven        >::type...
2029227825Stheraven    >::type type;
2030227825Stheraven};
2031227825Stheraven
2032232950Stheraventemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
2033232950Stheravenstruct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
2034227825Stheraven{
2035227825Stheraven    typedef typename __ref_return
2036227825Stheraven    <
2037232950Stheraven        _Fp&,
2038227825Stheraven        typename __mu_return
2039227825Stheraven        <
2040227825Stheraven            const _BoundArgs,
2041227825Stheraven            _TupleUj
2042227825Stheraven        >::type...
2043227825Stheraven    >::type type;
2044227825Stheraven};
2045227825Stheraven
2046232950Stheraventemplate <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
2047227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2048232950Stheraventypename __bind_return<_Fp, _BoundArgs, _Args>::type
2049232950Stheraven__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
2050227825Stheraven                _Args&& __args)
2051227825Stheraven{
2052278724Sdim    return __invoke(__f, __mu(_VSTD::get<_Indx>(__bound_args), __args)...);
2053227825Stheraven}
2054227825Stheraven
2055232950Stheraventemplate<class _Fp, class ..._BoundArgs>
2056227825Stheravenclass __bind
2057227825Stheraven{
2058232950Stheraven    _Fp __f_;
2059227825Stheraven    tuple<_BoundArgs...> __bound_args_;
2060227825Stheraven
2061227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2062227825Stheravenpublic:
2063232950Stheraven    template <class _Gp, class ..._BA>
2064232950Stheraven      explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2065232950Stheraven        : __f_(_VSTD::forward<_Gp>(__f)),
2066227825Stheraven          __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
2067227825Stheraven
2068227825Stheraven    template <class ..._Args>
2069232950Stheraven        typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2070227825Stheraven        operator()(_Args&& ...__args)
2071227825Stheraven        {
2072227825Stheraven            // compiler bug workaround
2073227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2074227825Stheraven                                  tuple<_Args&&...>(__args...));
2075227825Stheraven        }
2076227825Stheraven
2077227825Stheraven    template <class ..._Args>
2078232950Stheraven        typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2079227825Stheraven        operator()(_Args&& ...__args) const
2080227825Stheraven        {
2081227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2082227825Stheraven                                   tuple<_Args&&...>(__args...));
2083227825Stheraven        }
2084227825Stheraven};
2085227825Stheraven
2086232950Stheraventemplate<class _Fp, class ..._BoundArgs>
2087232950Stheravenstruct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
2088227825Stheraven
2089232950Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2090227825Stheravenclass __bind_r
2091232950Stheraven    : public __bind<_Fp, _BoundArgs...>
2092227825Stheraven{
2093232950Stheraven    typedef __bind<_Fp, _BoundArgs...> base;
2094227825Stheravenpublic:
2095232950Stheraven    typedef _Rp result_type;
2096227825Stheraven
2097232950Stheraven    template <class _Gp, class ..._BA>
2098232950Stheraven      explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2099232950Stheraven        : base(_VSTD::forward<_Gp>(__f),
2100227825Stheraven               _VSTD::forward<_BA>(__bound_args)...) {}
2101227825Stheraven
2102227825Stheraven    template <class ..._Args>
2103227825Stheraven        result_type
2104227825Stheraven        operator()(_Args&& ...__args)
2105227825Stheraven        {
2106227825Stheraven            return base::operator()(_VSTD::forward<_Args>(__args)...);
2107227825Stheraven        }
2108227825Stheraven
2109227825Stheraven    template <class ..._Args>
2110227825Stheraven        result_type
2111227825Stheraven        operator()(_Args&& ...__args) const
2112227825Stheraven        {
2113227825Stheraven            return base::operator()(_VSTD::forward<_Args>(__args)...);
2114227825Stheraven        }
2115227825Stheraven};
2116227825Stheraven
2117232950Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2118232950Stheravenstruct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
2119227825Stheraven
2120232950Stheraventemplate<class _Fp, class ..._BoundArgs>
2121227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2122232950Stheraven__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2123232950Stheravenbind(_Fp&& __f, _BoundArgs&&... __bound_args)
2124227825Stheraven{
2125232950Stheraven    typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2126232950Stheraven    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2127227825Stheraven}
2128227825Stheraven
2129232950Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2130227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2131232950Stheraven__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2132232950Stheravenbind(_Fp&& __f, _BoundArgs&&... __bound_args)
2133227825Stheraven{
2134232950Stheraven    typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2135232950Stheraven    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2136227825Stheraven}
2137227825Stheraven*/
2138227825Stheraven
2139227825Stheraven#endif  // _LIBCPP_FUNCTIONAL_03
2140