blob: 1ade26fa50359967a1bb93ccd99fe7295f45e327 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------ functional ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL
12#define _LIBCPP_FUNCTIONAL
13
14/*
15 functional synopsis
16
17namespace std
18{
19
20template <class Arg, class Result>
21struct unary_function
22{
23 typedef Arg argument_type;
24 typedef Result result_type;
25};
26
27template <class Arg1, class Arg2, class Result>
28struct binary_function
29{
30 typedef Arg1 first_argument_type;
31 typedef Arg2 second_argument_type;
32 typedef Result result_type;
33};
34
Howard Hinnant72552802010-08-20 19:36:46 +000035template <class T>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000036class reference_wrapper
37 : public unary_function<T1, R> // if wrapping a unary functor
38 : public binary_function<T1, T2, R> // if wraping a binary functor
39{
40public:
41 // types
42 typedef T type;
43 typedef see below result_type; // Not always defined
44
45 // construct/copy/destroy
Howard Hinnant603d2c02011-05-28 17:59:48 +000046 reference_wrapper(T&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047 reference_wrapper(T&&) = delete; // do not bind to temps
Howard Hinnant603d2c02011-05-28 17:59:48 +000048 reference_wrapper(const reference_wrapper<T>& x) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049
50 // assignment
Howard Hinnant603d2c02011-05-28 17:59:48 +000051 reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052
53 // access
Howard Hinnant603d2c02011-05-28 17:59:48 +000054 operator T& () const noexcept;
55 T& get() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056
57 // invoke
58 template <class... ArgTypes>
Howard Hinnant72552802010-08-20 19:36:46 +000059 typename result_of<T(ArgTypes...)>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 operator() (ArgTypes&&...) const;
61};
62
Howard Hinnant603d2c02011-05-28 17:59:48 +000063template <class T> reference_wrapper<T> ref(T& t) noexcept;
Howard Hinnant72552802010-08-20 19:36:46 +000064template <class T> void ref(const T&& t) = delete;
Howard Hinnant603d2c02011-05-28 17:59:48 +000065template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066
Howard Hinnant603d2c02011-05-28 17:59:48 +000067template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
Howard Hinnant72552802010-08-20 19:36:46 +000068template <class T> void cref(const T&& t) = delete;
Howard Hinnant603d2c02011-05-28 17:59:48 +000069template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070
71template <class T>
72struct plus : binary_function<T, T, T>
73{
74 T operator()(const T& x, const T& y) const;
75};
76
77template <class T>
78struct minus : binary_function<T, T, T>
79{
80 T operator()(const T& x, const T& y) const;
81};
82
83template <class T>
84struct multiplies : binary_function<T, T, T>
85{
86 T operator()(const T& x, const T& y) const;
87};
88
89template <class T>
90struct divides : binary_function<T, T, T>
91{
92 T operator()(const T& x, const T& y) const;
93};
94
95template <class T>
96struct modulus : binary_function<T, T, T>
97{
98 T operator()(const T& x, const T& y) const;
99};
100
101template <class T>
102struct negate : unary_function<T, T>
103{
104 T operator()(const T& x) const;
105};
106
107template <class T>
108struct equal_to : binary_function<T, T, bool>
109{
110 bool operator()(const T& x, const T& y) const;
111};
112
113template <class T>
114struct not_equal_to : binary_function<T, T, bool>
115{
116 bool operator()(const T& x, const T& y) const;
117};
118
119template <class T>
120struct greater : binary_function<T, T, bool>
121{
122 bool operator()(const T& x, const T& y) const;
123};
124
125template <class T>
126struct less : binary_function<T, T, bool>
127{
128 bool operator()(const T& x, const T& y) const;
129};
130
131template <class T>
132struct greater_equal : binary_function<T, T, bool>
133{
134 bool operator()(const T& x, const T& y) const;
135};
136
137template <class T>
138struct less_equal : binary_function<T, T, bool>
139{
140 bool operator()(const T& x, const T& y) const;
141};
142
143template <class T>
144struct logical_and : binary_function<T, T, bool>
145{
146 bool operator()(const T& x, const T& y) const;
147};
148
149template <class T>
150struct logical_or : binary_function<T, T, bool>
151{
152 bool operator()(const T& x, const T& y) const;
153};
154
155template <class T>
156struct logical_not : unary_function<T, bool>
157{
158 bool operator()(const T& x) const;
159};
160
161template <class Predicate>
162class unary_negate
163 : public unary_function<typename Predicate::argument_type, bool>
164{
165public:
166 explicit unary_negate(const Predicate& pred);
167 bool operator()(const typename Predicate::argument_type& x) const;
168};
169
170template <class Predicate> unary_negate<Predicate> not1(const Predicate& pred);
171
172template <class Predicate>
173class binary_negate
174 : public binary_function<typename Predicate::first_argument_type,
175 typename Predicate::second_argument_type,
176 bool>
177{
178public:
179 explicit binary_negate(const Predicate& pred);
180 bool operator()(const typename Predicate::first_argument_type& x,
181 const typename Predicate::second_argument_type& y) const;
182};
183
184template <class Predicate> binary_negate<Predicate> not2(const Predicate& pred);
185
186template<class T> struct is_bind_expression;
187template<class T> struct is_placeholder;
188
Howard Hinnant324bb032010-08-22 00:02:43 +0000189template<class Fn, class... BoundArgs>
Howard Hinnant72552802010-08-20 19:36:46 +0000190 unspecified bind(Fn&&, BoundArgs&&...);
Howard Hinnant324bb032010-08-22 00:02:43 +0000191template<class R, class Fn, class... BoundArgs>
Howard Hinnant72552802010-08-20 19:36:46 +0000192 unspecified bind(Fn&&, BoundArgs&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193
Howard Hinnant324bb032010-08-22 00:02:43 +0000194namespace placeholders {
195 // M is the implementation-defined number of placeholders
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000196 extern unspecified _1;
197 extern unspecified _2;
Howard Hinnant324bb032010-08-22 00:02:43 +0000198 .
199 .
200 .
Howard Hinnant99968442011-11-29 18:15:50 +0000201 extern unspecified _Mp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202}
203
204template <class Operation>
205class binder1st
206 : public unary_function<typename Operation::second_argument_type,
207 typename Operation::result_type>
208{
209protected:
210 Operation op;
211 typename Operation::first_argument_type value;
212public:
213 binder1st(const Operation& x, const typename Operation::first_argument_type y);
214 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
215 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
216};
217
218template <class Operation, class T>
219binder1st<Operation> bind1st(const Operation& op, const T& x);
220
221template <class Operation>
222class binder2nd
223 : public unary_function<typename Operation::first_argument_type,
224 typename Operation::result_type>
225{
226protected:
227 Operation op;
228 typename Operation::second_argument_type value;
229public:
230 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
231 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
232 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
233};
234
235template <class Operation, class T>
236binder2nd<Operation> bind2nd(const Operation& op, const T& x);
237
238template <class Arg, class Result>
239class pointer_to_unary_function : public unary_function<Arg, Result>
240{
241public:
242 explicit pointer_to_unary_function(Result (*f)(Arg));
243 Result operator()(Arg x) const;
244};
245
246template <class Arg, class Result>
247pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg));
248
249template <class Arg1, class Arg2, class Result>
250class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
251{
252public:
253 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
254 Result operator()(Arg1 x, Arg2 y) const;
255};
256
257template <class Arg1, class Arg2, class Result>
258pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2));
259
260template<class S, class T>
261class mem_fun_t : public unary_function<T*, S>
262{
263public:
264 explicit mem_fun_t(S (T::*p)());
265 S operator()(T* p) const;
266};
267
268template<class S, class T, class A>
269class mem_fun1_t : public binary_function<T*, A, S>
270{
271public:
272 explicit mem_fun1_t(S (T::*p)(A));
273 S operator()(T* p, A x) const;
274};
275
276template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)());
277template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A));
278
279template<class S, class T>
280class mem_fun_ref_t : public unary_function<T, S>
281{
282public:
283 explicit mem_fun_ref_t(S (T::*p)());
284 S operator()(T& p) const;
285};
286
287template<class S, class T, class A>
288class mem_fun1_ref_t : public binary_function<T, A, S>
289{
290public:
291 explicit mem_fun1_ref_t(S (T::*p)(A));
292 S operator()(T& p, A x) const;
293};
294
295template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)());
296template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A));
297
298template <class S, class T>
299class const_mem_fun_t : public unary_function<const T*, S>
300{
301public:
302 explicit const_mem_fun_t(S (T::*p)() const);
303 S operator()(const T* p) const;
304};
305
306template <class S, class T, class A>
307class const_mem_fun1_t : public binary_function<const T*, A, S>
308{
309public:
310 explicit const_mem_fun1_t(S (T::*p)(A) const);
311 S operator()(const T* p, A x) const;
312};
313
314template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const);
315template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const);
316
317template <class S, class T>
318class const_mem_fun_ref_t : public unary_function<T, S>
319{
320public:
321 explicit const_mem_fun_ref_t(S (T::*p)() const);
322 S operator()(const T& p) const;
323};
324
325template <class S, class T, class A>
326class const_mem_fun1_ref_t : public binary_function<T, A, S>
327{
328public:
329 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
330 S operator()(const T& p, A x) const;
331};
332
333template <class S, class T> const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const);
334template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const);
335
Howard Hinnant72552802010-08-20 19:36:46 +0000336template<class R, class T> unspecified mem_fn(R T::*);
337template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...));
338template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const);
339template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) volatile);
340template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const volatile);
341template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) &);
342template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const &);
343template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) volatile &);
344template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const volatile &);
345template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) &&);
346template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const &&);
347template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) volatile &&);
348template<class R, class T, class... Args> unspecified mem_fn(R (T::*)(Args...) const volatile &&);
349
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350class bad_function_call
351 : public exception
352{
353};
354
Howard Hinnant72552802010-08-20 19:36:46 +0000355template<class> class function; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356
Howard Hinnant72552802010-08-20 19:36:46 +0000357template<class R, class... ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358class function<R(ArgTypes...)>
359 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
360 // ArgTypes contains T1
361 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
362 // ArgTypes contains T1 and T2
363{
364public:
365 typedef R result_type;
366
Howard Hinnant72552802010-08-20 19:36:46 +0000367 // construct/copy/destroy:
Howard Hinnant603d2c02011-05-28 17:59:48 +0000368 function() noexcept;
369 function(nullptr_t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000370 function(const function&);
Howard Hinnant603d2c02011-05-28 17:59:48 +0000371 function(function&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372 template<class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373 function(F);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374 template<Allocator Alloc>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000375 function(allocator_arg_t, const Alloc&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376 template<Allocator Alloc>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000377 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378 template<Allocator Alloc>
379 function(allocator_arg_t, const Alloc&, const function&);
380 template<Allocator Alloc>
381 function(allocator_arg_t, const Alloc&, function&&);
382 template<class F, Allocator Alloc>
383 function(allocator_arg_t, const Alloc&, F);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384
385 function& operator=(const function&);
Howard Hinnant603d2c02011-05-28 17:59:48 +0000386 function& operator=(function&&) noexcept;
Howard Hinnantad1a5cc2011-05-29 13:53:56 +0000387 function& operator=(nullptr_t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388 template<class F>
Howard Hinnant72552802010-08-20 19:36:46 +0000389 function& operator=(F&&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390 template<class F>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000391 function& operator=(reference_wrapper<F>) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
393 ~function();
394
Howard Hinnant72552802010-08-20 19:36:46 +0000395 // function modifiers:
Howard Hinnant603d2c02011-05-28 17:59:48 +0000396 void swap(function&) noexcept;
Howard Hinnant72552802010-08-20 19:36:46 +0000397 template<class F, class Alloc>
398 void assign(F&&, const Alloc&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399
Howard Hinnant72552802010-08-20 19:36:46 +0000400 // function capacity:
Howard Hinnant603d2c02011-05-28 17:59:48 +0000401 explicit operator bool() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000402
Howard Hinnant72552802010-08-20 19:36:46 +0000403 // function invocation:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 R operator()(ArgTypes...) const;
405
Howard Hinnant72552802010-08-20 19:36:46 +0000406 // function target access:
Howard Hinnant603d2c02011-05-28 17:59:48 +0000407 const std::type_info& target_type() const noexcept;
408 template <typename T> T* target() noexcept;
409 template <typename T> const T* target() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410};
411
Howard Hinnant324bb032010-08-22 00:02:43 +0000412// Null pointer comparisons:
413template <class R, class ... ArgTypes>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000414 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415
Howard Hinnant324bb032010-08-22 00:02:43 +0000416template <class R, class ... ArgTypes>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000417 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418
Howard Hinnant324bb032010-08-22 00:02:43 +0000419template <class R, class ... ArgTypes>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000420 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421
Howard Hinnant324bb032010-08-22 00:02:43 +0000422template <class R, class ... ArgTypes>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000423 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424
Howard Hinnant324bb032010-08-22 00:02:43 +0000425// specialized algorithms:
426template <class R, class ... ArgTypes>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000427 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428
429template <class T> struct hash;
430
431template <> struct hash<bool>;
432template <> struct hash<char>;
433template <> struct hash<signed char>;
434template <> struct hash<unsigned char>;
435template <> struct hash<char16_t>;
436template <> struct hash<char32_t>;
437template <> struct hash<wchar_t>;
438template <> struct hash<short>;
439template <> struct hash<unsigned short>;
440template <> struct hash<int>;
441template <> struct hash<unsigned int>;
442template <> struct hash<long>;
443template <> struct hash<long long>;
444template <> struct hash<unsigned long>;
445template <> struct hash<unsigned long long>;
446
447template <> struct hash<float>;
448template <> struct hash<double>;
449template <> struct hash<long double>;
450
451template<class T> struct hash<T*>;
452
453} // std
454
455POLICY: For non-variadic implementations, the number of arguments is limited
456 to 3. It is hoped that the need for non-variadic implementations
457 will be minimal.
458
459*/
460
461#include <__config>
462#include <type_traits>
463#include <typeinfo>
464#include <exception>
465#include <memory>
466#include <tuple>
467
468#include <__functional_base>
469
Howard Hinnant08e17472011-10-17 20:05:10 +0000470#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000472#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473
474_LIBCPP_BEGIN_NAMESPACE_STD
475
476template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000477struct _LIBCPP_VISIBLE plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478{
479 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
480 {return __x + __y;}
481};
482
483template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000484struct _LIBCPP_VISIBLE minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485{
486 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
487 {return __x - __y;}
488};
489
490template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000491struct _LIBCPP_VISIBLE multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492{
493 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
494 {return __x * __y;}
495};
496
497template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000498struct _LIBCPP_VISIBLE divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499{
500 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
501 {return __x / __y;}
502};
503
504template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000505struct _LIBCPP_VISIBLE modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506{
507 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
508 {return __x % __y;}
509};
510
511template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000512struct _LIBCPP_VISIBLE negate : unary_function<_Tp, _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513{
514 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x) const
515 {return -__x;}
516};
517
518template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000519struct _LIBCPP_VISIBLE equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520{
521 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
522 {return __x == __y;}
523};
524
525template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000526struct _LIBCPP_VISIBLE not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527{
528 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
529 {return __x != __y;}
530};
531
532template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000533struct _LIBCPP_VISIBLE greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534{
535 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
536 {return __x > __y;}
537};
538
539template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000540struct _LIBCPP_VISIBLE less : binary_function<_Tp, _Tp, bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541{
542 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
543 {return __x < __y;}
544};
545
546template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000547struct _LIBCPP_VISIBLE greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548{
549 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
550 {return __x >= __y;}
551};
552
553template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000554struct _LIBCPP_VISIBLE less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555{
556 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
557 {return __x <= __y;}
558};
559
560template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000561struct _LIBCPP_VISIBLE logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562{
563 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
564 {return __x && __y;}
565};
566
567template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000568struct _LIBCPP_VISIBLE logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569{
570 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
571 {return __x || __y;}
572};
573
574template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000575struct _LIBCPP_VISIBLE logical_not : unary_function<_Tp, bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576{
577 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x) const
578 {return !__x;}
579};
580
581template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000582struct _LIBCPP_VISIBLE bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583{
584 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
585 {return __x & __y;}
586};
587
588template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000589struct _LIBCPP_VISIBLE bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000590{
591 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
592 {return __x | __y;}
593};
594
595template <class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000596struct _LIBCPP_VISIBLE bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597{
598 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
599 {return __x ^ __y;}
600};
601
602template <class _Predicate>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000603class _LIBCPP_VISIBLE unary_negate
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604 : public unary_function<typename _Predicate::argument_type, bool>
605{
606 _Predicate __pred_;
607public:
608 _LIBCPP_INLINE_VISIBILITY explicit unary_negate(const _Predicate& __pred)
609 : __pred_(__pred) {}
610 _LIBCPP_INLINE_VISIBILITY bool operator()(const typename _Predicate::argument_type& __x) const
611 {return !__pred_(__x);}
612};
613
614template <class _Predicate>
615inline _LIBCPP_INLINE_VISIBILITY
616unary_negate<_Predicate>
617not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
618
619template <class _Predicate>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000620class _LIBCPP_VISIBLE binary_negate
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621 : public binary_function<typename _Predicate::first_argument_type,
622 typename _Predicate::second_argument_type,
623 bool>
624{
625 _Predicate __pred_;
626public:
627 _LIBCPP_INLINE_VISIBILITY explicit binary_negate(const _Predicate& __pred)
628 : __pred_(__pred) {}
629 _LIBCPP_INLINE_VISIBILITY bool operator()(const typename _Predicate::first_argument_type& __x,
630 const typename _Predicate::second_argument_type& __y) const
631 {return !__pred_(__x, __y);}
632};
633
634template <class _Predicate>
635inline _LIBCPP_INLINE_VISIBILITY
636binary_negate<_Predicate>
637not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
638
639template <class __Operation>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000640class _LIBCPP_VISIBLE binder1st
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641 : public unary_function<typename __Operation::second_argument_type,
642 typename __Operation::result_type>
643{
644protected:
645 __Operation op;
646 typename __Operation::first_argument_type value;
647public:
648 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
649 const typename __Operation::first_argument_type __y)
650 : op(__x), value(__y) {}
651 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
652 (typename __Operation::second_argument_type& __x) const
653 {return op(value, __x);}
654 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
655 (const typename __Operation::second_argument_type& __x) const
656 {return op(value, __x);}
657};
658
659template <class __Operation, class _Tp>
660inline _LIBCPP_INLINE_VISIBILITY
661binder1st<__Operation>
662bind1st(const __Operation& __op, const _Tp& __x)
663 {return binder1st<__Operation>(__op, __x);}
664
665template <class __Operation>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000666class _LIBCPP_VISIBLE binder2nd
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667 : public unary_function<typename __Operation::first_argument_type,
668 typename __Operation::result_type>
669{
670protected:
671 __Operation op;
672 typename __Operation::second_argument_type value;
673public:
Howard Hinnant42a63a72010-09-21 22:55:27 +0000674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
676 : op(__x), value(__y) {}
677 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
678 ( typename __Operation::first_argument_type& __x) const
679 {return op(__x, value);}
680 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
681 (const typename __Operation::first_argument_type& __x) const
682 {return op(__x, value);}
683};
684
685template <class __Operation, class _Tp>
686inline _LIBCPP_INLINE_VISIBILITY
687binder2nd<__Operation>
688bind2nd(const __Operation& __op, const _Tp& __x)
689 {return binder2nd<__Operation>(__op, __x);}
690
691template <class _Arg, class _Result>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000692class _LIBCPP_VISIBLE pointer_to_unary_function
693 : public unary_function<_Arg, _Result>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000694{
695 _Result (*__f_)(_Arg);
696public:
697 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
698 : __f_(__f) {}
699 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
700 {return __f_(__x);}
701};
702
703template <class _Arg, class _Result>
704inline _LIBCPP_INLINE_VISIBILITY
705pointer_to_unary_function<_Arg,_Result>
706ptr_fun(_Result (*__f)(_Arg))
707 {return pointer_to_unary_function<_Arg,_Result>(__f);}
708
709template <class _Arg1, class _Arg2, class _Result>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000710class _LIBCPP_VISIBLE pointer_to_binary_function
711 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712{
713 _Result (*__f_)(_Arg1, _Arg2);
714public:
715 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
716 : __f_(__f) {}
717 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
718 {return __f_(__x, __y);}
719};
720
721template <class _Arg1, class _Arg2, class _Result>
722inline _LIBCPP_INLINE_VISIBILITY
723pointer_to_binary_function<_Arg1,_Arg2,_Result>
724ptr_fun(_Result (*__f)(_Arg1,_Arg2))
725 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
726
727template<class _Sp, class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000728class _LIBCPP_VISIBLE mem_fun_t : public unary_function<_Tp*, _Sp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000729{
730 _Sp (_Tp::*__p_)();
731public:
732 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
733 : __p_(__p) {}
734 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
735 {return (__p->*__p_)();}
736};
737
738template<class _Sp, class _Tp, class _Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000739class _LIBCPP_VISIBLE mem_fun1_t : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740{
741 _Sp (_Tp::*__p_)(_Ap);
742public:
743 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
744 : __p_(__p) {}
745 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
746 {return (__p->*__p_)(__x);}
747};
748
749template<class _Sp, class _Tp>
750inline _LIBCPP_INLINE_VISIBILITY
751mem_fun_t<_Sp,_Tp>
752mem_fun(_Sp (_Tp::*__f)())
753 {return mem_fun_t<_Sp,_Tp>(__f);}
754
755template<class _Sp, class _Tp, class _Ap>
756inline _LIBCPP_INLINE_VISIBILITY
757mem_fun1_t<_Sp,_Tp,_Ap>
758mem_fun(_Sp (_Tp::*__f)(_Ap))
759 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
760
761template<class _Sp, class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000762class _LIBCPP_VISIBLE mem_fun_ref_t : public unary_function<_Tp, _Sp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763{
764 _Sp (_Tp::*__p_)();
765public:
766 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
767 : __p_(__p) {}
768 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
769 {return (__p.*__p_)();}
770};
771
772template<class _Sp, class _Tp, class _Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000773class _LIBCPP_VISIBLE mem_fun1_ref_t : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774{
775 _Sp (_Tp::*__p_)(_Ap);
776public:
777 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
778 : __p_(__p) {}
779 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
780 {return (__p.*__p_)(__x);}
781};
782
783template<class _Sp, class _Tp>
784inline _LIBCPP_INLINE_VISIBILITY
785mem_fun_ref_t<_Sp,_Tp>
786mem_fun_ref(_Sp (_Tp::*__f)())
787 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
788
789template<class _Sp, class _Tp, class _Ap>
790inline _LIBCPP_INLINE_VISIBILITY
791mem_fun1_ref_t<_Sp,_Tp,_Ap>
792mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
793 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
794
795template <class _Sp, class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000796class _LIBCPP_VISIBLE const_mem_fun_t : public unary_function<const _Tp*, _Sp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000797{
798 _Sp (_Tp::*__p_)() const;
799public:
800 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
801 : __p_(__p) {}
802 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
803 {return (__p->*__p_)();}
804};
805
806template <class _Sp, class _Tp, class _Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000807class _LIBCPP_VISIBLE const_mem_fun1_t : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000808{
809 _Sp (_Tp::*__p_)(_Ap) const;
810public:
811 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
812 : __p_(__p) {}
813 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
814 {return (__p->*__p_)(__x);}
815};
816
817template <class _Sp, class _Tp>
818inline _LIBCPP_INLINE_VISIBILITY
819const_mem_fun_t<_Sp,_Tp>
820mem_fun(_Sp (_Tp::*__f)() const)
821 {return const_mem_fun_t<_Sp,_Tp>(__f);}
822
823template <class _Sp, class _Tp, class _Ap>
824inline _LIBCPP_INLINE_VISIBILITY
825const_mem_fun1_t<_Sp,_Tp,_Ap>
826mem_fun(_Sp (_Tp::*__f)(_Ap) const)
827 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
828
829template <class _Sp, class _Tp>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000830class _LIBCPP_VISIBLE const_mem_fun_ref_t : public unary_function<_Tp, _Sp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831{
832 _Sp (_Tp::*__p_)() const;
833public:
834 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
835 : __p_(__p) {}
836 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
837 {return (__p.*__p_)();}
838};
839
840template <class _Sp, class _Tp, class _Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000841class _LIBCPP_VISIBLE const_mem_fun1_ref_t
842 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843{
844 _Sp (_Tp::*__p_)(_Ap) const;
845public:
846 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
847 : __p_(__p) {}
848 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
849 {return (__p.*__p_)(__x);}
850};
851
852template <class _Sp, class _Tp>
853inline _LIBCPP_INLINE_VISIBILITY
854const_mem_fun_ref_t<_Sp,_Tp>
855mem_fun_ref(_Sp (_Tp::*__f)() const)
856 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
857
858template <class _Sp, class _Tp, class _Ap>
859inline _LIBCPP_INLINE_VISIBILITY
860const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
861mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
862 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
863
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864#ifdef _LIBCPP_HAS_NO_VARIADICS
865
866#include <__functional_03>
867
868#else // _LIBCPP_HAS_NO_VARIADICS
869
870template <class _Tp>
871class __mem_fn
872 : public __weak_result_type<_Tp>
873{
874public:
875 // types
876 typedef _Tp type;
877private:
878 type __f_;
879
880public:
881 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
882
883 // invoke
884 template <class... _ArgTypes>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000886 typename __invoke_return<type, _ArgTypes...>::type
887 operator() (_ArgTypes&&... __args)
888 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000889 return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890 }
891};
892
Howard Hinnant99968442011-11-29 18:15:50 +0000893template<class _Rp, class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000895__mem_fn<_Rp _Tp::*>
896mem_fn(_Rp _Tp::* __pm)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000897{
Howard Hinnant99968442011-11-29 18:15:50 +0000898 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899}
900
Howard Hinnant99968442011-11-29 18:15:50 +0000901template<class _Rp, class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000903__mem_fn<_Rp (_Tp::*)(_Args...)>
904mem_fn(_Rp (_Tp::* __pm)(_Args...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905{
Howard Hinnant99968442011-11-29 18:15:50 +0000906 return __mem_fn<_Rp (_Tp::*)(_Args...)>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000907}
908
Howard Hinnant99968442011-11-29 18:15:50 +0000909template<class _Rp, class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000910inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000911__mem_fn<_Rp (_Tp::*)(_Args...) const>
912mem_fn(_Rp (_Tp::* __pm)(_Args...) const)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913{
Howard Hinnant99968442011-11-29 18:15:50 +0000914 return __mem_fn<_Rp (_Tp::*)(_Args...) const>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915}
916
Howard Hinnant99968442011-11-29 18:15:50 +0000917template<class _Rp, class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000919__mem_fn<_Rp (_Tp::*)(_Args...) volatile>
920mem_fn(_Rp (_Tp::* __pm)(_Args...) volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921{
Howard Hinnant99968442011-11-29 18:15:50 +0000922 return __mem_fn<_Rp (_Tp::*)(_Args...) volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923}
924
Howard Hinnant99968442011-11-29 18:15:50 +0000925template<class _Rp, class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000927__mem_fn<_Rp (_Tp::*)(_Args...) const volatile>
928mem_fn(_Rp (_Tp::* __pm)(_Args...) const volatile)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929{
Howard Hinnant99968442011-11-29 18:15:50 +0000930 return __mem_fn<_Rp (_Tp::*)(_Args...) const volatile>(__pm);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931}
932
933// bad_function_call
934
Howard Hinnant42a63a72010-09-21 22:55:27 +0000935class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936 : public exception
937{
938};
939
Howard Hinnant42a63a72010-09-21 22:55:27 +0000940template<class _Fp> class _LIBCPP_VISIBLE function; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941
942namespace __function
943{
944
Howard Hinnant99968442011-11-29 18:15:50 +0000945template<class _Rp, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946struct __maybe_derive_from_unary_function
947{
948};
949
Howard Hinnant99968442011-11-29 18:15:50 +0000950template<class _Rp, class _A1>
951struct __maybe_derive_from_unary_function<_Rp(_A1)>
952 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953{
954};
955
Howard Hinnant99968442011-11-29 18:15:50 +0000956template<class _Rp, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957struct __maybe_derive_from_binary_function
958{
959};
960
Howard Hinnant99968442011-11-29 18:15:50 +0000961template<class _Rp, class _A1, class _A2>
962struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
963 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964{
965};
966
967template<class _Fp> class __base;
968
Howard Hinnant99968442011-11-29 18:15:50 +0000969template<class _Rp, class ..._ArgTypes>
970class __base<_Rp(_ArgTypes...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971{
972 __base(const __base&);
973 __base& operator=(const __base&);
974public:
Howard Hinnant42a63a72010-09-21 22:55:27 +0000975 _LIBCPP_INLINE_VISIBILITY __base() {}
976 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977 virtual __base* __clone() const = 0;
978 virtual void __clone(__base*) const = 0;
Howard Hinnant603d2c02011-05-28 17:59:48 +0000979 virtual void destroy() _NOEXCEPT = 0;
980 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000981 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000982#ifndef _LIBCPP_NO_RTTI
Howard Hinnant603d2c02011-05-28 17:59:48 +0000983 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
984 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000985#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000986};
987
988template<class _FD, class _Alloc, class _FB> class __func;
989
Howard Hinnant99968442011-11-29 18:15:50 +0000990template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
991class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
992 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993{
Howard Hinnant99968442011-11-29 18:15:50 +0000994 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995public:
Howard Hinnant42a63a72010-09-21 22:55:27 +0000996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000997 explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant42a63a72010-09-21 22:55:27 +0000998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000999 explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1000 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1001 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnant603d2c02011-05-28 17:59:48 +00001002 virtual void destroy() _NOEXCEPT;
1003 virtual void destroy_deallocate() _NOEXCEPT;
Howard Hinnant99968442011-11-29 18:15:50 +00001004 virtual _Rp operator()(_ArgTypes&& ... __arg);
Howard Hinnantd4444702010-08-11 17:04:31 +00001005#ifndef _LIBCPP_NO_RTTI
Howard Hinnant603d2c02011-05-28 17:59:48 +00001006 virtual const void* target(const type_info&) const _NOEXCEPT;
1007 virtual const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00001008#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009};
1010
Howard Hinnant99968442011-11-29 18:15:50 +00001011template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1012__base<_Rp(_ArgTypes...)>*
1013__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001014{
Howard Hinnant99968442011-11-29 18:15:50 +00001015 typedef typename _Alloc::template rebind<__func>::other _Ap;
1016 _Ap __a(__f_.second());
1017 typedef __allocator_destructor<_Ap> _Dp;
1018 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
1020 return __hold.release();
1021}
1022
Howard Hinnant99968442011-11-29 18:15:50 +00001023template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024void
Howard Hinnant99968442011-11-29 18:15:50 +00001025__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026{
1027 ::new (__p) __func(__f_.first(), __f_.second());
1028}
1029
Howard Hinnant99968442011-11-29 18:15:50 +00001030template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031void
Howard Hinnant99968442011-11-29 18:15:50 +00001032__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033{
Howard Hinnant99968442011-11-29 18:15:50 +00001034 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035}
1036
Howard Hinnant99968442011-11-29 18:15:50 +00001037template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038void
Howard Hinnant99968442011-11-29 18:15:50 +00001039__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040{
Howard Hinnant99968442011-11-29 18:15:50 +00001041 typedef typename _Alloc::template rebind<__func>::other _Ap;
1042 _Ap __a(__f_.second());
1043 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044 __a.deallocate(this, 1);
1045}
1046
Howard Hinnant99968442011-11-29 18:15:50 +00001047template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1048_Rp
1049__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001051 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052}
1053
Howard Hinnantd4444702010-08-11 17:04:31 +00001054#ifndef _LIBCPP_NO_RTTI
1055
Howard Hinnant99968442011-11-29 18:15:50 +00001056template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057const void*
Howard Hinnant99968442011-11-29 18:15:50 +00001058__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059{
Howard Hinnant99968442011-11-29 18:15:50 +00001060 if (__ti == typeid(_Fp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061 return &__f_.first();
1062 return (const void*)0;
1063}
1064
Howard Hinnant99968442011-11-29 18:15:50 +00001065template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001067__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068{
Howard Hinnant99968442011-11-29 18:15:50 +00001069 return typeid(_Fp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001070}
1071
Howard Hinnant324bb032010-08-22 00:02:43 +00001072#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001073
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074} // __function
1075
Howard Hinnant99968442011-11-29 18:15:50 +00001076template<class _Rp, class ..._ArgTypes>
1077class _LIBCPP_VISIBLE function<_Rp(_ArgTypes...)>
1078 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
1079 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001080{
Howard Hinnant99968442011-11-29 18:15:50 +00001081 typedef __function::__base<_Rp(_ArgTypes...)> __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082 aligned_storage<3*sizeof(void*)>::type __buf_;
1083 __base* __f_;
1084
Howard Hinnant99968442011-11-29 18:15:50 +00001085 template <class _Fp>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001087 static bool __not_null(const _Fp&) {return true;}
1088 template <class _R2, class ..._Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001090 static bool __not_null(_R2 (*__p)(_Ap...)) {return __p;}
1091 template <class _R2, class _Cp, class ..._Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001093 static bool __not_null(_R2 (_Cp::*__p)(_Ap...)) {return __p;}
1094 template <class _R2, class _Cp, class ..._Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001096 static bool __not_null(_R2 (_Cp::*__p)(_Ap...) const) {return __p;}
1097 template <class _R2, class _Cp, class ..._Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001099 static bool __not_null(_R2 (_Cp::*__p)(_Ap...) volatile) {return __p;}
1100 template <class _R2, class _Cp, class ..._Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001102 static bool __not_null(_R2 (_Cp::*__p)(_Ap...) const volatile) {return __p;}
1103 template <class _R2, class ..._Ap>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001105 static bool __not_null(const function<_Rp(_Ap...)>& __p) {return __p;}
Howard Hinnant083ba5f2011-05-31 21:45:26 +00001106
Howard Hinnant99968442011-11-29 18:15:50 +00001107 template <class _Fp, bool = __invokable<_Fp&, _ArgTypes...>::value>
Howard Hinnant083ba5f2011-05-31 21:45:26 +00001108 struct __callable;
Howard Hinnant99968442011-11-29 18:15:50 +00001109 template <class _Fp>
1110 struct __callable<_Fp, true>
Howard Hinnant083ba5f2011-05-31 21:45:26 +00001111 {
1112 static const bool value =
Howard Hinnant99968442011-11-29 18:15:50 +00001113 is_convertible<typename __invoke_of<_Fp&, _ArgTypes...>::type,
1114 _Rp>::value;
Howard Hinnant083ba5f2011-05-31 21:45:26 +00001115 };
Howard Hinnant99968442011-11-29 18:15:50 +00001116 template <class _Fp>
1117 struct __callable<_Fp, false>
Howard Hinnant083ba5f2011-05-31 21:45:26 +00001118 {
1119 static const bool value = false;
1120 };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121public:
Howard Hinnant99968442011-11-29 18:15:50 +00001122 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123
Howard Hinnant72552802010-08-20 19:36:46 +00001124 // construct/copy/destroy:
Howard Hinnant42a63a72010-09-21 22:55:27 +00001125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001126 function() _NOEXCEPT : __f_(0) {}
Howard Hinnant42a63a72010-09-21 22:55:27 +00001127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001128 function(nullptr_t) _NOEXCEPT : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129 function(const function&);
Howard Hinnant603d2c02011-05-28 17:59:48 +00001130 function(function&&) _NOEXCEPT;
Howard Hinnant99968442011-11-29 18:15:50 +00001131 template<class _Fp>
1132 function(_Fp,
1133 typename enable_if<__callable<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134
Howard Hinnant72552802010-08-20 19:36:46 +00001135 template<class _Alloc>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001137 function(allocator_arg_t, const _Alloc&) _NOEXCEPT : __f_(0) {}
Howard Hinnant72552802010-08-20 19:36:46 +00001138 template<class _Alloc>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001140 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT : __f_(0) {}
Howard Hinnant72552802010-08-20 19:36:46 +00001141 template<class _Alloc>
1142 function(allocator_arg_t, const _Alloc&, const function&);
1143 template<class _Alloc>
1144 function(allocator_arg_t, const _Alloc&, function&&);
Howard Hinnant99968442011-11-29 18:15:50 +00001145 template<class _Fp, class _Alloc>
1146 function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1147 typename enable_if<__callable<_Fp>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148
1149 function& operator=(const function&);
Howard Hinnant603d2c02011-05-28 17:59:48 +00001150 function& operator=(function&&) _NOEXCEPT;
1151 function& operator=(nullptr_t) _NOEXCEPT;
Howard Hinnant99968442011-11-29 18:15:50 +00001152 template<class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153 typename enable_if
1154 <
Howard Hinnant99968442011-11-29 18:15:50 +00001155 __callable<typename decay<_Fp>::type>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001156 function&
1157 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00001158 operator=(_Fp&&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159
1160 ~function();
1161
Howard Hinnant72552802010-08-20 19:36:46 +00001162 // function modifiers:
Howard Hinnant603d2c02011-05-28 17:59:48 +00001163 void swap(function&) _NOEXCEPT;
Howard Hinnant99968442011-11-29 18:15:50 +00001164 template<class _Fp, class _Alloc>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001166 void assign(_Fp&& __f, const _Alloc& __a)
1167 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168
Howard Hinnant72552802010-08-20 19:36:46 +00001169 // function capacity:
Howard Hinnant42a63a72010-09-21 22:55:27 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001171 /*explicit*/ operator bool() const _NOEXCEPT {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173 // deleted overloads close possible hole in the type system
1174 template<class _R2, class... _ArgTypes2>
Howard Hinnanta0f1dc92010-09-11 15:33:21 +00001175 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176 template<class _R2, class... _ArgTypes2>
Howard Hinnanta0f1dc92010-09-11 15:33:21 +00001177 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178public:
Howard Hinnant72552802010-08-20 19:36:46 +00001179 // function invocation:
Howard Hinnant99968442011-11-29 18:15:50 +00001180 _Rp operator()(_ArgTypes...) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181
Howard Hinnantd4444702010-08-11 17:04:31 +00001182#ifndef _LIBCPP_NO_RTTI
Howard Hinnant72552802010-08-20 19:36:46 +00001183 // function target access:
Howard Hinnant603d2c02011-05-28 17:59:48 +00001184 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant99968442011-11-29 18:15:50 +00001185 template <typename _Tp> _Tp* target() _NOEXCEPT;
1186 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00001187#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188};
1189
Howard Hinnant99968442011-11-29 18:15:50 +00001190template<class _Rp, class ..._ArgTypes>
1191function<_Rp(_ArgTypes...)>::function(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192{
1193 if (__f.__f_ == 0)
1194 __f_ = 0;
1195 else if (__f.__f_ == (const __base*)&__f.__buf_)
1196 {
1197 __f_ = (__base*)&__buf_;
1198 __f.__f_->__clone(__f_);
1199 }
1200 else
1201 __f_ = __f.__f_->__clone();
1202}
1203
Howard Hinnant99968442011-11-29 18:15:50 +00001204template<class _Rp, class ..._ArgTypes>
Howard Hinnant72552802010-08-20 19:36:46 +00001205template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001206function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnant72552802010-08-20 19:36:46 +00001207 const function& __f)
1208{
1209 if (__f.__f_ == 0)
1210 __f_ = 0;
1211 else if (__f.__f_ == (const __base*)&__f.__buf_)
1212 {
1213 __f_ = (__base*)&__buf_;
1214 __f.__f_->__clone(__f_);
1215 }
1216 else
1217 __f_ = __f.__f_->__clone();
1218}
1219
Howard Hinnant99968442011-11-29 18:15:50 +00001220template<class _Rp, class ..._ArgTypes>
1221function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222{
1223 if (__f.__f_ == 0)
1224 __f_ = 0;
1225 else if (__f.__f_ == (__base*)&__f.__buf_)
1226 {
1227 __f_ = (__base*)&__buf_;
1228 __f.__f_->__clone(__f_);
1229 }
1230 else
1231 {
1232 __f_ = __f.__f_;
1233 __f.__f_ = 0;
1234 }
1235}
1236
Howard Hinnant99968442011-11-29 18:15:50 +00001237template<class _Rp, class ..._ArgTypes>
Howard Hinnant72552802010-08-20 19:36:46 +00001238template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001239function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnant72552802010-08-20 19:36:46 +00001240 function&& __f)
1241{
1242 if (__f.__f_ == 0)
1243 __f_ = 0;
1244 else if (__f.__f_ == (__base*)&__f.__buf_)
1245 {
1246 __f_ = (__base*)&__buf_;
1247 __f.__f_->__clone(__f_);
1248 }
1249 else
1250 {
1251 __f_ = __f.__f_;
1252 __f.__f_ = 0;
1253 }
1254}
1255
Howard Hinnant99968442011-11-29 18:15:50 +00001256template<class _Rp, class ..._ArgTypes>
1257template <class _Fp>
1258function<_Rp(_ArgTypes...)>::function(_Fp __f,
1259 typename enable_if<__callable<_Fp>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260 : __f_(0)
1261{
1262 if (__not_null(__f))
1263 {
Howard Hinnant99968442011-11-29 18:15:50 +00001264 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_ArgTypes...)> _FF;
1265 if (sizeof(_FF) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266 {
1267 __f_ = (__base*)&__buf_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001268 ::new (__f_) _FF(_VSTD::move(__f));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 }
1270 else
1271 {
Howard Hinnant99968442011-11-29 18:15:50 +00001272 typedef allocator<_FF> _Ap;
1273 _Ap __a;
1274 typedef __allocator_destructor<_Ap> _Dp;
1275 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1276 ::new (__hold.get()) _FF(_VSTD::move(__f), allocator<_Fp>(__a));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 __f_ = __hold.release();
1278 }
1279 }
1280}
1281
Howard Hinnant99968442011-11-29 18:15:50 +00001282template<class _Rp, class ..._ArgTypes>
1283template <class _Fp, class _Alloc>
1284function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1285 typename enable_if<__callable<_Fp>::value>::type*)
Howard Hinnant72552802010-08-20 19:36:46 +00001286 : __f_(0)
1287{
1288 typedef allocator_traits<_Alloc> __alloc_traits;
1289 if (__not_null(__f))
1290 {
Howard Hinnant99968442011-11-29 18:15:50 +00001291 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _FF;
1292 if (sizeof(_FF) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value)
Howard Hinnant72552802010-08-20 19:36:46 +00001293 {
1294 __f_ = (__base*)&__buf_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001295 ::new (__f_) _FF(_VSTD::move(__f));
Howard Hinnant72552802010-08-20 19:36:46 +00001296 }
1297 else
1298 {
1299 typedef typename __alloc_traits::template
1300#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1301 rebind_alloc<_FF>
1302#else
1303 rebind_alloc<_FF>::other
1304#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001305 _Ap;
1306 _Ap __a(__a0);
1307 typedef __allocator_destructor<_Ap> _Dp;
1308 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001309 ::new (__hold.get()) _FF(_VSTD::move(__f), _Alloc(__a));
Howard Hinnant72552802010-08-20 19:36:46 +00001310 __f_ = __hold.release();
1311 }
1312 }
1313}
1314
Howard Hinnant99968442011-11-29 18:15:50 +00001315template<class _Rp, class ..._ArgTypes>
1316function<_Rp(_ArgTypes...)>&
1317function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318{
1319 function(__f).swap(*this);
1320 return *this;
1321}
1322
Howard Hinnant99968442011-11-29 18:15:50 +00001323template<class _Rp, class ..._ArgTypes>
1324function<_Rp(_ArgTypes...)>&
1325function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326{
1327 if (__f_ == (__base*)&__buf_)
1328 __f_->destroy();
1329 else if (__f_)
1330 __f_->destroy_deallocate();
1331 __f_ = 0;
1332 if (__f.__f_ == 0)
1333 __f_ = 0;
1334 else if (__f.__f_ == (__base*)&__f.__buf_)
1335 {
1336 __f_ = (__base*)&__buf_;
1337 __f.__f_->__clone(__f_);
1338 }
1339 else
1340 {
1341 __f_ = __f.__f_;
1342 __f.__f_ = 0;
1343 }
1344}
1345
Howard Hinnant99968442011-11-29 18:15:50 +00001346template<class _Rp, class ..._ArgTypes>
1347function<_Rp(_ArgTypes...)>&
1348function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349{
1350 if (__f_ == (__base*)&__buf_)
1351 __f_->destroy();
1352 else if (__f_)
1353 __f_->destroy_deallocate();
1354 __f_ = 0;
1355}
1356
Howard Hinnant99968442011-11-29 18:15:50 +00001357template<class _Rp, class ..._ArgTypes>
1358template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359typename enable_if
1360<
Howard Hinnant99968442011-11-29 18:15:50 +00001361 function<_Rp(_ArgTypes...)>::template __callable<typename decay<_Fp>::type>::value,
1362 function<_Rp(_ArgTypes...)>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363>::type
Howard Hinnant99968442011-11-29 18:15:50 +00001364function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001365{
Howard Hinnant99968442011-11-29 18:15:50 +00001366 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367 return *this;
1368}
1369
Howard Hinnant99968442011-11-29 18:15:50 +00001370template<class _Rp, class ..._ArgTypes>
1371function<_Rp(_ArgTypes...)>::~function()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001372{
1373 if (__f_ == (__base*)&__buf_)
1374 __f_->destroy();
1375 else if (__f_)
1376 __f_->destroy_deallocate();
1377}
1378
Howard Hinnant99968442011-11-29 18:15:50 +00001379template<class _Rp, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380void
Howard Hinnant99968442011-11-29 18:15:50 +00001381function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001382{
1383 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1384 {
1385 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1386 __base* __t = (__base*)&__tempbuf;
1387 __f_->__clone(__t);
1388 __f_->destroy();
1389 __f_ = 0;
1390 __f.__f_->__clone((__base*)&__buf_);
1391 __f.__f_->destroy();
1392 __f.__f_ = 0;
1393 __f_ = (__base*)&__buf_;
1394 __t->__clone((__base*)&__f.__buf_);
1395 __t->destroy();
1396 __f.__f_ = (__base*)&__f.__buf_;
1397 }
1398 else if (__f_ == (__base*)&__buf_)
1399 {
1400 __f_->__clone((__base*)&__f.__buf_);
1401 __f_->destroy();
1402 __f_ = __f.__f_;
1403 __f.__f_ = (__base*)&__f.__buf_;
1404 }
1405 else if (__f.__f_ == (__base*)&__f.__buf_)
1406 {
1407 __f.__f_->__clone((__base*)&__buf_);
1408 __f.__f_->destroy();
1409 __f.__f_ = __f_;
1410 __f_ = (__base*)&__buf_;
1411 }
1412 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001413 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414}
1415
Howard Hinnant99968442011-11-29 18:15:50 +00001416template<class _Rp, class ..._ArgTypes>
1417_Rp
1418function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419{
Howard Hinnantd4444702010-08-11 17:04:31 +00001420#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421 if (__f_ == 0)
1422 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001423#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00001424 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425}
1426
Howard Hinnantd4444702010-08-11 17:04:31 +00001427#ifndef _LIBCPP_NO_RTTI
1428
Howard Hinnant99968442011-11-29 18:15:50 +00001429template<class _Rp, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430const std::type_info&
Howard Hinnant99968442011-11-29 18:15:50 +00001431function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432{
1433 if (__f_ == 0)
1434 return typeid(void);
1435 return __f_->target_type();
1436}
1437
Howard Hinnant99968442011-11-29 18:15:50 +00001438template<class _Rp, class ..._ArgTypes>
1439template <typename _Tp>
1440_Tp*
1441function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442{
1443 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001444 return (_Tp*)0;
1445 return (_Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446}
1447
Howard Hinnant99968442011-11-29 18:15:50 +00001448template<class _Rp, class ..._ArgTypes>
1449template <typename _Tp>
1450const _Tp*
1451function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452{
1453 if (__f_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00001454 return (const _Tp*)0;
1455 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001456}
1457
Howard Hinnant324bb032010-08-22 00:02:43 +00001458#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001459
Howard Hinnant99968442011-11-29 18:15:50 +00001460template <class _Rp, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461inline _LIBCPP_INLINE_VISIBILITY
1462bool
Howard Hinnant99968442011-11-29 18:15:50 +00001463operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001464
Howard Hinnant99968442011-11-29 18:15:50 +00001465template <class _Rp, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466inline _LIBCPP_INLINE_VISIBILITY
1467bool
Howard Hinnant99968442011-11-29 18:15:50 +00001468operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469
Howard Hinnant99968442011-11-29 18:15:50 +00001470template <class _Rp, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471inline _LIBCPP_INLINE_VISIBILITY
1472bool
Howard Hinnant99968442011-11-29 18:15:50 +00001473operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474
Howard Hinnant99968442011-11-29 18:15:50 +00001475template <class _Rp, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476inline _LIBCPP_INLINE_VISIBILITY
1477bool
Howard Hinnant99968442011-11-29 18:15:50 +00001478operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479
Howard Hinnant99968442011-11-29 18:15:50 +00001480template <class _Rp, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481inline _LIBCPP_INLINE_VISIBILITY
1482void
Howard Hinnant99968442011-11-29 18:15:50 +00001483swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484{return __x.swap(__y);}
1485
1486template<class _Tp> struct __is_bind_expression : public false_type {};
Howard Hinnant42a63a72010-09-21 22:55:27 +00001487template<class _Tp> struct _LIBCPP_VISIBLE is_bind_expression
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1489
1490template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Howard Hinnant42a63a72010-09-21 22:55:27 +00001491template<class _Tp> struct _LIBCPP_VISIBLE is_placeholder
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1493
1494namespace placeholders
1495{
1496
Howard Hinnant99968442011-11-29 18:15:50 +00001497template <int _Np> struct __ph {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498
1499extern __ph<1> _1;
1500extern __ph<2> _2;
1501extern __ph<3> _3;
1502extern __ph<4> _4;
1503extern __ph<5> _5;
1504extern __ph<6> _6;
1505extern __ph<7> _7;
1506extern __ph<8> _8;
1507extern __ph<9> _9;
1508extern __ph<10> _10;
1509
1510} // placeholders
1511
Howard Hinnant99968442011-11-29 18:15:50 +00001512template<int _Np>
1513struct __is_placeholder<placeholders::__ph<_Np> >
1514 : public integral_constant<int, _Np> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515
1516template <class _Tp, class _Uj>
1517inline _LIBCPP_INLINE_VISIBILITY
1518_Tp&
1519__mu(reference_wrapper<_Tp> __t, _Uj&)
1520{
1521 return __t.get();
1522}
1523
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524template <class _Ti, class ..._Uj, size_t ..._Indx>
1525inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0148a832011-05-19 19:41:47 +00001526typename __invoke_of<_Ti&, _Uj...>::type
1527__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001528{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001529 return __ti(_VSTD::forward<_Uj>(get<_Indx>(__uj))...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530}
1531
1532template <class _Ti, class ..._Uj>
1533inline _LIBCPP_INLINE_VISIBILITY
1534typename enable_if
1535<
1536 is_bind_expression<_Ti>::value,
Howard Hinnant0148a832011-05-19 19:41:47 +00001537 typename __invoke_of<_Ti&, _Uj...>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538>::type
1539__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1540{
1541 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1542 return __mu_expand(__ti, __uj, __indices());
1543}
1544
1545template <bool IsPh, class _Ti, class _Uj>
1546struct __mu_return2 {};
1547
1548template <class _Ti, class _Uj>
1549struct __mu_return2<true, _Ti, _Uj>
1550{
1551 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1552};
1553
1554template <class _Ti, class _Uj>
1555inline _LIBCPP_INLINE_VISIBILITY
1556typename enable_if
1557<
1558 0 < is_placeholder<_Ti>::value,
1559 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1560>::type
1561__mu(_Ti&, _Uj& __uj)
1562{
1563 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001564 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565}
1566
1567template <class _Ti, class _Uj>
1568inline _LIBCPP_INLINE_VISIBILITY
1569typename enable_if
1570<
1571 !is_bind_expression<_Ti>::value &&
1572 is_placeholder<_Ti>::value == 0 &&
1573 !__is_reference_wrapper<_Ti>::value,
1574 _Ti&
1575>::type
1576__mu(_Ti& __ti, _Uj& __uj)
1577{
1578 return __ti;
1579}
1580
Howard Hinnantef542512011-05-22 15:07:43 +00001581template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
1582 class _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001583struct ____mu_return;
1584
1585template <class _Ti, class ..._Uj>
Howard Hinnantef542512011-05-22 15:07:43 +00001586struct ____mu_return<_Ti, false, true, false, tuple<_Uj...> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587{
Howard Hinnant0148a832011-05-19 19:41:47 +00001588 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589};
1590
1591template <class _Ti, class _TupleUj>
Howard Hinnantef542512011-05-22 15:07:43 +00001592struct ____mu_return<_Ti, false, false, true, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593{
1594 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1595 _TupleUj>::type&& type;
1596};
1597
1598template <class _Ti, class _TupleUj>
Howard Hinnantef542512011-05-22 15:07:43 +00001599struct ____mu_return<_Ti, true, false, false, _TupleUj>
1600{
1601 typedef typename _Ti::type& type;
1602};
1603
1604template <class _Ti, class _TupleUj>
1605struct ____mu_return<_Ti, false, false, false, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606{
1607 typedef _Ti& type;
1608};
1609
1610template <class _Ti, class _TupleUj>
1611struct __mu_return
1612 : public ____mu_return<_Ti,
Howard Hinnantef542512011-05-22 15:07:43 +00001613 __is_reference_wrapper<_Ti>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614 is_bind_expression<_Ti>::value,
1615 0 < is_placeholder<_Ti>::value,
1616 _TupleUj>
1617{
1618};
1619
Howard Hinnant99968442011-11-29 18:15:50 +00001620template <class _Fp, class _BoundArgs, class _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001621struct __bind_return;
1622
Howard Hinnant99968442011-11-29 18:15:50 +00001623template <class _Fp, class ..._BoundArgs, class _TupleUj>
1624struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625{
Howard Hinnant0148a832011-05-19 19:41:47 +00001626 typedef typename __invoke_of
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627 <
Howard Hinnant99968442011-11-29 18:15:50 +00001628 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629 typename __mu_return
1630 <
1631 _BoundArgs,
1632 _TupleUj
1633 >::type...
1634 >::type type;
1635};
1636
Howard Hinnant99968442011-11-29 18:15:50 +00001637template <class _Fp, class ..._BoundArgs, class _TupleUj>
1638struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639{
Howard Hinnant0148a832011-05-19 19:41:47 +00001640 typedef typename __invoke_of
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 <
Howard Hinnant99968442011-11-29 18:15:50 +00001642 _Fp&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643 typename __mu_return
1644 <
1645 const _BoundArgs,
1646 _TupleUj
1647 >::type...
1648 >::type type;
1649};
1650
Howard Hinnant99968442011-11-29 18:15:50 +00001651template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001653typename __bind_return<_Fp, _BoundArgs, _Args>::type
1654__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655 _Args&& __args)
1656{
1657 return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
1658}
1659
Howard Hinnant99968442011-11-29 18:15:50 +00001660template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661class __bind
Howard Hinnant99968442011-11-29 18:15:50 +00001662 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663{
Howard Hinnant99968442011-11-29 18:15:50 +00001664 typedef typename decay<_Fp>::type _Fd;
Howard Hinnant0148a832011-05-19 19:41:47 +00001665 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
1666 _Fd __f_;
1667 _Td __bound_args_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668
1669 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
1670public:
Howard Hinnant90d77852011-07-02 18:22:36 +00001671#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1672
1673 _LIBCPP_INLINE_VISIBILITY
1674 __bind(const __bind& __b)
1675 : __f_(__b.__f_),
1676 __bound_args_(__b.__bound_args_) {}
1677
1678 _LIBCPP_INLINE_VISIBILITY
1679 __bind& operator=(const __bind& __b)
1680 {
1681 __f_ = __b.__f_;
1682 __bound_args_ = __b.__bound_args_;
1683 return *this;
1684 }
1685
Howard Hinnant42a63a72010-09-21 22:55:27 +00001686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687 __bind(__bind&& __b)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001688 : __f_(_VSTD::move(__b.__f_)),
1689 __bound_args_(_VSTD::move(__b.__bound_args_)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690
Howard Hinnant90d77852011-07-02 18:22:36 +00001691 _LIBCPP_INLINE_VISIBILITY
1692 __bind& operator=(__bind&& __b)
1693 {
1694 __f_ = _VSTD::move(__b.__f_);
1695 __bound_args_ = _VSTD::move(__b.__bound_args_);
1696 return *this;
1697 }
1698
1699#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1700
Howard Hinnant99968442011-11-29 18:15:50 +00001701 template <class _Gp, class ..._BA>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001703 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
1704 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001705 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706
1707 template <class ..._Args>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0148a832011-05-19 19:41:47 +00001709 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710 operator()(_Args&& ...__args)
1711 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001712 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001713 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001714 }
1715
1716 template <class ..._Args>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0148a832011-05-19 19:41:47 +00001718 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719 operator()(_Args&& ...__args) const
1720 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001721 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001722 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723 }
1724};
1725
Howard Hinnant99968442011-11-29 18:15:50 +00001726template<class _Fp, class ..._BoundArgs>
1727struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728
Howard Hinnant99968442011-11-29 18:15:50 +00001729template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730class __bind_r
Howard Hinnant99968442011-11-29 18:15:50 +00001731 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732{
Howard Hinnant99968442011-11-29 18:15:50 +00001733 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734public:
Howard Hinnant99968442011-11-29 18:15:50 +00001735 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736
Howard Hinnant90d77852011-07-02 18:22:36 +00001737#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1738
1739 _LIBCPP_INLINE_VISIBILITY
1740 __bind_r(const __bind_r& __b)
1741 : base(_VSTD::forward<const base&>(__b)) {}
1742
1743 _LIBCPP_INLINE_VISIBILITY
1744 __bind_r& operator=(const __bind_r& __b)
1745 {
1746 base::operator=(_VSTD::forward<const base&>(__b));
1747 return *this;
1748 }
1749
Howard Hinnant496934a2011-05-16 16:19:01 +00001750 _LIBCPP_INLINE_VISIBILITY
1751 __bind_r(__bind_r&& __b)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001752 : base(_VSTD::forward<base>(__b)) {}
Howard Hinnant496934a2011-05-16 16:19:01 +00001753
Howard Hinnant90d77852011-07-02 18:22:36 +00001754 _LIBCPP_INLINE_VISIBILITY
1755 __bind_r& operator=(__bind_r&& __b)
1756 {
1757 base::operator=(_VSTD::forward<base>(__b));
1758 return *this;
1759 }
1760
1761#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1762
Howard Hinnant99968442011-11-29 18:15:50 +00001763 template <class _Gp, class ..._BA>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001765 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
1766 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001767 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001768
1769 template <class ..._Args>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001771 result_type
1772 operator()(_Args&& ...__args)
1773 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001774 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 }
1776
1777 template <class ..._Args>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001779 result_type
1780 operator()(_Args&& ...__args) const
1781 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001782 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783 }
1784};
1785
Howard Hinnant99968442011-11-29 18:15:50 +00001786template<class _Rp, class _Fp, class ..._BoundArgs>
1787struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001788
Howard Hinnant99968442011-11-29 18:15:50 +00001789template<class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001791__bind<_Fp, _BoundArgs...>
1792bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793{
Howard Hinnant99968442011-11-29 18:15:50 +00001794 typedef __bind<_Fp, _BoundArgs...> type;
1795 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796}
1797
Howard Hinnant99968442011-11-29 18:15:50 +00001798template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001800__bind_r<_Rp, _Fp, _BoundArgs...>
1801bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802{
Howard Hinnant99968442011-11-29 18:15:50 +00001803 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
1804 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805}
1806
1807#endif // _LIBCPP_HAS_NO_VARIADICS
1808
1809template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001810struct _LIBCPP_VISIBLE hash<bool>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811 : public unary_function<bool, size_t>
1812{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001814 size_t operator()(bool __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815};
1816
1817template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001818struct _LIBCPP_VISIBLE hash<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819 : public unary_function<char, size_t>
1820{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001822 size_t operator()(char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823};
1824
1825template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001826struct _LIBCPP_VISIBLE hash<signed char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827 : public unary_function<signed char, size_t>
1828{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001830 size_t operator()(signed char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831};
1832
1833template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001834struct _LIBCPP_VISIBLE hash<unsigned char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001835 : public unary_function<unsigned char, size_t>
1836{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001838 size_t operator()(unsigned char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839};
1840
1841#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
1842
1843template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001844struct _LIBCPP_VISIBLE hash<char16_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845 : public unary_function<char16_t, size_t>
1846{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001848 size_t operator()(char16_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849};
1850
1851template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001852struct _LIBCPP_VISIBLE hash<char32_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001853 : public unary_function<char32_t, size_t>
1854{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001856 size_t operator()(char32_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857};
1858
Howard Hinnant324bb032010-08-22 00:02:43 +00001859#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860
1861template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001862struct _LIBCPP_VISIBLE hash<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 : public unary_function<wchar_t, size_t>
1864{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001866 size_t operator()(wchar_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001867};
1868
1869template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001870struct _LIBCPP_VISIBLE hash<short>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871 : public unary_function<short, size_t>
1872{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001874 size_t operator()(short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875};
1876
1877template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001878struct _LIBCPP_VISIBLE hash<unsigned short>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879 : public unary_function<unsigned short, size_t>
1880{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001882 size_t operator()(unsigned short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883};
1884
1885template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001886struct _LIBCPP_VISIBLE hash<int>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 : public unary_function<int, size_t>
1888{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001890 size_t operator()(int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891};
1892
1893template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001894struct _LIBCPP_VISIBLE hash<unsigned int>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001895 : public unary_function<unsigned int, size_t>
1896{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001898 size_t operator()(unsigned int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001899};
1900
1901template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001902struct _LIBCPP_VISIBLE hash<long>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 : public unary_function<long, size_t>
1904{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001906 size_t operator()(long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907};
1908
1909template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001910struct _LIBCPP_VISIBLE hash<unsigned long>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 : public unary_function<unsigned long, size_t>
1912{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001914 size_t operator()(unsigned long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915};
1916
1917template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001918struct _LIBCPP_VISIBLE hash<long long>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919 : public unary_function<long long, size_t>
1920{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001922 size_t operator()(long long __v) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001923 {
1924 size_t __r = 0;
1925 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1926 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1927 __r ^= __p[__i];
1928 return __r;
1929 }
1930};
1931
1932template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001933struct _LIBCPP_VISIBLE hash<unsigned long long>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 : public unary_function<unsigned long long, size_t>
1935{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001937 size_t operator()(unsigned long long __v) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938 {
1939 size_t __r = 0;
1940 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1941 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1942 __r ^= __p[__i];
1943 return __r;
1944 }
1945};
1946
1947template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001948struct _LIBCPP_VISIBLE hash<float>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949 : public unary_function<float, size_t>
1950{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001952 size_t operator()(float __v) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 {
1954 if (__v == 0)
1955 return 0;
1956 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1957 return *__p;
1958 }
1959};
1960
1961template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001962struct _LIBCPP_VISIBLE hash<double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001963 : public unary_function<double, size_t>
1964{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001965 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001966 size_t operator()(double __v) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001967 {
1968 if (__v == 0)
1969 return 0;
1970 size_t __r = 0;
1971 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1972 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1973 __r ^= __p[__i];
1974 return __r;
1975 }
1976};
1977
1978template <>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001979struct _LIBCPP_VISIBLE hash<long double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001980 : public unary_function<long double, size_t>
1981{
Howard Hinnant42a63a72010-09-21 22:55:27 +00001982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant603d2c02011-05-28 17:59:48 +00001983 size_t operator()(long double __v) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001984 {
1985 if (__v == 0)
1986 return 0;
1987 size_t __r = 0;
1988 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1989 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1990 __r ^= __p[__i];
1991 return __r;
1992 }
1993};
1994
Howard Hinnant21aefc32010-06-03 16:42:57 +00001995// struct hash<T*> in <memory>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996
1997_LIBCPP_END_NAMESPACE_STD
1998
1999#endif // _LIBCPP_FUNCTIONAL