blob: 3cc21d48629cee847f02931c442505a150451f57 [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//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
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
46 reference_wrapper(T&);
47 reference_wrapper(T&&) = delete; // do not bind to temps
48 reference_wrapper(const reference_wrapper<T>& x);
49
50 // assignment
51 reference_wrapper& operator=(const reference_wrapper<T>& x);
52
53 // access
54 operator T& () const;
55 T& get() const;
56
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 Hinnant72552802010-08-20 19:36:46 +000063template <class T> reference_wrapper<T> ref(T& t);
64template <class T> void ref(const T&& t) = delete;
65template <class T> reference_wrapper<T> ref(reference_wrapper<T>t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066
Howard Hinnant72552802010-08-20 19:36:46 +000067template <class T> reference_wrapper<const T> cref(const T& t);
68template <class T> void cref(const T&& t) = delete;
69template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t);
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 Hinnantbc8d3f92010-05-11 19:42:16 +0000201 extern unspecified _M;
202}
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:
368 function();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369 function(nullptr_t);
370 function(const function&);
371 function(function&&);
372 template<class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373 function(F);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374 template<Allocator Alloc>
375 function(allocator_arg_t, const Alloc&);
376 template<Allocator Alloc>
377 function(allocator_arg_t, const Alloc&, nullptr_t);
378 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&);
386 function& operator=(function&&);
387 function& operator=(nullptr_t);
388 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 Hinnantbc8d3f92010-05-11 19:42:16 +0000391 function& operator=(reference_wrapper<F>);
392
393 ~function();
394
Howard Hinnant72552802010-08-20 19:36:46 +0000395 // function modifiers:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396 void swap(function&);
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 Hinnantbc8d3f92010-05-11 19:42:16 +0000401 explicit operator bool() const;
402
403 // deleted overloads close possible hole in the type system
404 template<class R2, class... ArgTypes2>
405 bool operator==(const function<R2(ArgTypes2...)>&) = delete;
406 template<class R2, class... ArgTypes2>
407 bool operator!=(const function<R2(ArgTypes2...)>&) = delete;
408
Howard Hinnant72552802010-08-20 19:36:46 +0000409 // function invocation:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 R operator()(ArgTypes...) const;
411
Howard Hinnant72552802010-08-20 19:36:46 +0000412 // function target access:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413 const std::type_info& target_type() const;
Howard Hinnant72552802010-08-20 19:36:46 +0000414 template <typename T> T* target();
415 template <typename T> const T* target() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416};
417
Howard Hinnant324bb032010-08-22 00:02:43 +0000418// Null pointer comparisons:
419template <class R, class ... ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420 bool operator==(const function<R(ArgTypes...)>&, nullptr_t);
421
Howard Hinnant324bb032010-08-22 00:02:43 +0000422template <class R, class ... ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423 bool operator==(nullptr_t, const function<R(ArgTypes...)>&);
424
Howard Hinnant324bb032010-08-22 00:02:43 +0000425template <class R, class ... ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t);
427
Howard Hinnant324bb032010-08-22 00:02:43 +0000428template <class R, class ... ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&);
430
Howard Hinnant324bb032010-08-22 00:02:43 +0000431// specialized algorithms:
432template <class R, class ... ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&);
434
435template <class T> struct hash;
436
437template <> struct hash<bool>;
438template <> struct hash<char>;
439template <> struct hash<signed char>;
440template <> struct hash<unsigned char>;
441template <> struct hash<char16_t>;
442template <> struct hash<char32_t>;
443template <> struct hash<wchar_t>;
444template <> struct hash<short>;
445template <> struct hash<unsigned short>;
446template <> struct hash<int>;
447template <> struct hash<unsigned int>;
448template <> struct hash<long>;
449template <> struct hash<long long>;
450template <> struct hash<unsigned long>;
451template <> struct hash<unsigned long long>;
452
453template <> struct hash<float>;
454template <> struct hash<double>;
455template <> struct hash<long double>;
456
457template<class T> struct hash<T*>;
458
459} // std
460
461POLICY: For non-variadic implementations, the number of arguments is limited
462 to 3. It is hoped that the need for non-variadic implementations
463 will be minimal.
464
465*/
466
467#include <__config>
468#include <type_traits>
469#include <typeinfo>
470#include <exception>
471#include <memory>
472#include <tuple>
473
474#include <__functional_base>
475
476#pragma GCC system_header
477
478_LIBCPP_BEGIN_NAMESPACE_STD
479
480template <class _Tp>
481struct plus : binary_function<_Tp, _Tp, _Tp>
482{
483 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
484 {return __x + __y;}
485};
486
487template <class _Tp>
488struct minus : binary_function<_Tp, _Tp, _Tp>
489{
490 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
491 {return __x - __y;}
492};
493
494template <class _Tp>
495struct multiplies : binary_function<_Tp, _Tp, _Tp>
496{
497 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
498 {return __x * __y;}
499};
500
501template <class _Tp>
502struct divides : binary_function<_Tp, _Tp, _Tp>
503{
504 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
505 {return __x / __y;}
506};
507
508template <class _Tp>
509struct modulus : binary_function<_Tp, _Tp, _Tp>
510{
511 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
512 {return __x % __y;}
513};
514
515template <class _Tp>
516struct negate : unary_function<_Tp, _Tp>
517{
518 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x) const
519 {return -__x;}
520};
521
522template <class _Tp>
523struct equal_to : binary_function<_Tp, _Tp, bool>
524{
525 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
526 {return __x == __y;}
527};
528
529template <class _Tp>
530struct not_equal_to : binary_function<_Tp, _Tp, bool>
531{
532 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
533 {return __x != __y;}
534};
535
536template <class _Tp>
537struct greater : binary_function<_Tp, _Tp, bool>
538{
539 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
540 {return __x > __y;}
541};
542
543template <class _Tp>
544struct less : binary_function<_Tp, _Tp, bool>
545{
546 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
547 {return __x < __y;}
548};
549
550template <class _Tp>
551struct greater_equal : binary_function<_Tp, _Tp, bool>
552{
553 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
554 {return __x >= __y;}
555};
556
557template <class _Tp>
558struct less_equal : binary_function<_Tp, _Tp, bool>
559{
560 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
561 {return __x <= __y;}
562};
563
564template <class _Tp>
565struct logical_and : binary_function<_Tp, _Tp, bool>
566{
567 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
568 {return __x && __y;}
569};
570
571template <class _Tp>
572struct logical_or : binary_function<_Tp, _Tp, bool>
573{
574 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
575 {return __x || __y;}
576};
577
578template <class _Tp>
579struct logical_not : unary_function<_Tp, bool>
580{
581 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x) const
582 {return !__x;}
583};
584
585template <class _Tp>
586struct bit_and : binary_function<_Tp, _Tp, _Tp>
587{
588 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
589 {return __x & __y;}
590};
591
592template <class _Tp>
593struct bit_or : binary_function<_Tp, _Tp, _Tp>
594{
595 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
596 {return __x | __y;}
597};
598
599template <class _Tp>
600struct bit_xor : binary_function<_Tp, _Tp, _Tp>
601{
602 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
603 {return __x ^ __y;}
604};
605
606template <class _Predicate>
607class unary_negate
608 : public unary_function<typename _Predicate::argument_type, bool>
609{
610 _Predicate __pred_;
611public:
612 _LIBCPP_INLINE_VISIBILITY explicit unary_negate(const _Predicate& __pred)
613 : __pred_(__pred) {}
614 _LIBCPP_INLINE_VISIBILITY bool operator()(const typename _Predicate::argument_type& __x) const
615 {return !__pred_(__x);}
616};
617
618template <class _Predicate>
619inline _LIBCPP_INLINE_VISIBILITY
620unary_negate<_Predicate>
621not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
622
623template <class _Predicate>
624class binary_negate
625 : public binary_function<typename _Predicate::first_argument_type,
626 typename _Predicate::second_argument_type,
627 bool>
628{
629 _Predicate __pred_;
630public:
631 _LIBCPP_INLINE_VISIBILITY explicit binary_negate(const _Predicate& __pred)
632 : __pred_(__pred) {}
633 _LIBCPP_INLINE_VISIBILITY bool operator()(const typename _Predicate::first_argument_type& __x,
634 const typename _Predicate::second_argument_type& __y) const
635 {return !__pred_(__x, __y);}
636};
637
638template <class _Predicate>
639inline _LIBCPP_INLINE_VISIBILITY
640binary_negate<_Predicate>
641not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
642
643template <class __Operation>
644class binder1st
645 : public unary_function<typename __Operation::second_argument_type,
646 typename __Operation::result_type>
647{
648protected:
649 __Operation op;
650 typename __Operation::first_argument_type value;
651public:
652 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
653 const typename __Operation::first_argument_type __y)
654 : op(__x), value(__y) {}
655 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
656 (typename __Operation::second_argument_type& __x) const
657 {return op(value, __x);}
658 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
659 (const typename __Operation::second_argument_type& __x) const
660 {return op(value, __x);}
661};
662
663template <class __Operation, class _Tp>
664inline _LIBCPP_INLINE_VISIBILITY
665binder1st<__Operation>
666bind1st(const __Operation& __op, const _Tp& __x)
667 {return binder1st<__Operation>(__op, __x);}
668
669template <class __Operation>
670class binder2nd
671 : public unary_function<typename __Operation::first_argument_type,
672 typename __Operation::result_type>
673{
674protected:
675 __Operation op;
676 typename __Operation::second_argument_type value;
677public:
678 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
679 : op(__x), value(__y) {}
680 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
681 ( typename __Operation::first_argument_type& __x) const
682 {return op(__x, value);}
683 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
684 (const typename __Operation::first_argument_type& __x) const
685 {return op(__x, value);}
686};
687
688template <class __Operation, class _Tp>
689inline _LIBCPP_INLINE_VISIBILITY
690binder2nd<__Operation>
691bind2nd(const __Operation& __op, const _Tp& __x)
692 {return binder2nd<__Operation>(__op, __x);}
693
694template <class _Arg, class _Result>
695class pointer_to_unary_function : public unary_function<_Arg, _Result>
696{
697 _Result (*__f_)(_Arg);
698public:
699 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
700 : __f_(__f) {}
701 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
702 {return __f_(__x);}
703};
704
705template <class _Arg, class _Result>
706inline _LIBCPP_INLINE_VISIBILITY
707pointer_to_unary_function<_Arg,_Result>
708ptr_fun(_Result (*__f)(_Arg))
709 {return pointer_to_unary_function<_Arg,_Result>(__f);}
710
711template <class _Arg1, class _Arg2, class _Result>
712class pointer_to_binary_function : public binary_function<_Arg1, _Arg2, _Result>
713{
714 _Result (*__f_)(_Arg1, _Arg2);
715public:
716 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
717 : __f_(__f) {}
718 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
719 {return __f_(__x, __y);}
720};
721
722template <class _Arg1, class _Arg2, class _Result>
723inline _LIBCPP_INLINE_VISIBILITY
724pointer_to_binary_function<_Arg1,_Arg2,_Result>
725ptr_fun(_Result (*__f)(_Arg1,_Arg2))
726 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
727
728template<class _Sp, class _Tp>
729class mem_fun_t : public unary_function<_Tp*, _Sp>
730{
731 _Sp (_Tp::*__p_)();
732public:
733 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
734 : __p_(__p) {}
735 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
736 {return (__p->*__p_)();}
737};
738
739template<class _Sp, class _Tp, class _Ap>
740class mem_fun1_t : public binary_function<_Tp*, _Ap, _Sp>
741{
742 _Sp (_Tp::*__p_)(_Ap);
743public:
744 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
745 : __p_(__p) {}
746 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
747 {return (__p->*__p_)(__x);}
748};
749
750template<class _Sp, class _Tp>
751inline _LIBCPP_INLINE_VISIBILITY
752mem_fun_t<_Sp,_Tp>
753mem_fun(_Sp (_Tp::*__f)())
754 {return mem_fun_t<_Sp,_Tp>(__f);}
755
756template<class _Sp, class _Tp, class _Ap>
757inline _LIBCPP_INLINE_VISIBILITY
758mem_fun1_t<_Sp,_Tp,_Ap>
759mem_fun(_Sp (_Tp::*__f)(_Ap))
760 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
761
762template<class _Sp, class _Tp>
763class mem_fun_ref_t : public unary_function<_Tp, _Sp>
764{
765 _Sp (_Tp::*__p_)();
766public:
767 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
768 : __p_(__p) {}
769 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
770 {return (__p.*__p_)();}
771};
772
773template<class _Sp, class _Tp, class _Ap>
774class mem_fun1_ref_t : public binary_function<_Tp, _Ap, _Sp>
775{
776 _Sp (_Tp::*__p_)(_Ap);
777public:
778 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
779 : __p_(__p) {}
780 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
781 {return (__p.*__p_)(__x);}
782};
783
784template<class _Sp, class _Tp>
785inline _LIBCPP_INLINE_VISIBILITY
786mem_fun_ref_t<_Sp,_Tp>
787mem_fun_ref(_Sp (_Tp::*__f)())
788 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
789
790template<class _Sp, class _Tp, class _Ap>
791inline _LIBCPP_INLINE_VISIBILITY
792mem_fun1_ref_t<_Sp,_Tp,_Ap>
793mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
794 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
795
796template <class _Sp, class _Tp>
797class const_mem_fun_t : public unary_function<const _Tp*, _Sp>
798{
799 _Sp (_Tp::*__p_)() const;
800public:
801 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
802 : __p_(__p) {}
803 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
804 {return (__p->*__p_)();}
805};
806
807template <class _Sp, class _Tp, class _Ap>
808class const_mem_fun1_t : public binary_function<const _Tp*, _Ap, _Sp>
809{
810 _Sp (_Tp::*__p_)(_Ap) const;
811public:
812 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
813 : __p_(__p) {}
814 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
815 {return (__p->*__p_)(__x);}
816};
817
818template <class _Sp, class _Tp>
819inline _LIBCPP_INLINE_VISIBILITY
820const_mem_fun_t<_Sp,_Tp>
821mem_fun(_Sp (_Tp::*__f)() const)
822 {return const_mem_fun_t<_Sp,_Tp>(__f);}
823
824template <class _Sp, class _Tp, class _Ap>
825inline _LIBCPP_INLINE_VISIBILITY
826const_mem_fun1_t<_Sp,_Tp,_Ap>
827mem_fun(_Sp (_Tp::*__f)(_Ap) const)
828 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
829
830template <class _Sp, class _Tp>
831class const_mem_fun_ref_t : public unary_function<_Tp, _Sp>
832{
833 _Sp (_Tp::*__p_)() const;
834public:
835 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
836 : __p_(__p) {}
837 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
838 {return (__p.*__p_)();}
839};
840
841template <class _Sp, class _Tp, class _Ap>
842class const_mem_fun1_ref_t : public binary_function<_Tp, _Ap, _Sp>
843{
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>
885 typename __invoke_return<type, _ArgTypes...>::type
886 operator() (_ArgTypes&&... __args)
887 {
888 return __invoke(__f_, _STD::forward<_ArgTypes>(__args)...);
889 }
890};
891
892template<class _R, class _T>
893inline _LIBCPP_INLINE_VISIBILITY
894__mem_fn<_R _T::*>
895mem_fn(_R _T::* __pm)
896{
897 return __mem_fn<_R _T::*>(__pm);
898}
899
900template<class _R, class _T, class ..._Args>
901inline _LIBCPP_INLINE_VISIBILITY
902__mem_fn<_R (_T::*)(_Args...)>
903mem_fn(_R (_T::* __pm)(_Args...))
904{
905 return __mem_fn<_R (_T::*)(_Args...)>(__pm);
906}
907
908template<class _R, class _T, class ..._Args>
909inline _LIBCPP_INLINE_VISIBILITY
910__mem_fn<_R (_T::*)(_Args...) const>
911mem_fn(_R (_T::* __pm)(_Args...) const)
912{
913 return __mem_fn<_R (_T::*)(_Args...) const>(__pm);
914}
915
916template<class _R, class _T, class ..._Args>
917inline _LIBCPP_INLINE_VISIBILITY
918__mem_fn<_R (_T::*)(_Args...) volatile>
919mem_fn(_R (_T::* __pm)(_Args...) volatile)
920{
921 return __mem_fn<_R (_T::*)(_Args...) volatile>(__pm);
922}
923
924template<class _R, class _T, class ..._Args>
925inline _LIBCPP_INLINE_VISIBILITY
926__mem_fn<_R (_T::*)(_Args...) const volatile>
927mem_fn(_R (_T::* __pm)(_Args...) const volatile)
928{
929 return __mem_fn<_R (_T::*)(_Args...) const volatile>(__pm);
930}
931
932// bad_function_call
933
934class bad_function_call
935 : public exception
936{
937};
938
939template<class _Fp> class function; // undefined
940
941namespace __function
942{
943
944template<class _R, class ..._ArgTypes>
945struct __maybe_derive_from_unary_function
946{
947};
948
949template<class _R, class _A1>
950struct __maybe_derive_from_unary_function<_R(_A1)>
951 : public unary_function<_A1, _R>
952{
953};
954
955template<class _R, class ..._ArgTypes>
956struct __maybe_derive_from_binary_function
957{
958};
959
960template<class _R, class _A1, class _A2>
961struct __maybe_derive_from_binary_function<_R(_A1, _A2)>
962 : public binary_function<_A1, _A2, _R>
963{
964};
965
966template<class _Fp> class __base;
967
968template<class _R, class ..._ArgTypes>
969class __base<_R(_ArgTypes...)>
970{
971 __base(const __base&);
972 __base& operator=(const __base&);
973public:
974 __base() {}
975 virtual ~__base() {}
976 virtual __base* __clone() const = 0;
977 virtual void __clone(__base*) const = 0;
978 virtual void destroy() = 0;
979 virtual void destroy_deallocate() = 0;
980 virtual _R operator()(_ArgTypes&& ...) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000981#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982 virtual const void* target(const type_info&) const = 0;
983 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000984#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985};
986
987template<class _FD, class _Alloc, class _FB> class __func;
988
989template<class _F, class _Alloc, class _R, class ..._ArgTypes>
990class __func<_F, _Alloc, _R(_ArgTypes...)>
991 : public __base<_R(_ArgTypes...)>
992{
993 __compressed_pair<_F, _Alloc> __f_;
994public:
995 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
996 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
997 virtual __base<_R(_ArgTypes...)>* __clone() const;
998 virtual void __clone(__base<_R(_ArgTypes...)>*) const;
999 virtual void destroy();
1000 virtual void destroy_deallocate();
1001 virtual _R operator()(_ArgTypes&& ... __arg);
Howard Hinnantd4444702010-08-11 17:04:31 +00001002#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003 virtual const void* target(const type_info&) const;
1004 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001005#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006};
1007
1008template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1009__base<_R(_ArgTypes...)>*
1010__func<_F, _Alloc, _R(_ArgTypes...)>::__clone() const
1011{
1012 typedef typename _Alloc::template rebind<__func>::other _A;
1013 _A __a(__f_.second());
1014 typedef __allocator_destructor<_A> _D;
1015 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
1016 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
1017 return __hold.release();
1018}
1019
1020template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1021void
1022__func<_F, _Alloc, _R(_ArgTypes...)>::__clone(__base<_R(_ArgTypes...)>* __p) const
1023{
1024 ::new (__p) __func(__f_.first(), __f_.second());
1025}
1026
1027template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1028void
1029__func<_F, _Alloc, _R(_ArgTypes...)>::destroy()
1030{
1031 __f_.~__compressed_pair<_F, _Alloc>();
1032}
1033
1034template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1035void
1036__func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate()
1037{
1038 typedef typename _Alloc::template rebind<__func>::other _A;
1039 _A __a(__f_.second());
1040 __f_.~__compressed_pair<_F, _Alloc>();
1041 __a.deallocate(this, 1);
1042}
1043
1044template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1045_R
1046__func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1047{
1048 return __invoke(__f_.first(), _STD::forward<_ArgTypes>(__arg)...);
1049}
1050
Howard Hinnantd4444702010-08-11 17:04:31 +00001051#ifndef _LIBCPP_NO_RTTI
1052
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1054const void*
1055__func<_F, _Alloc, _R(_ArgTypes...)>::target(const type_info& __ti) const
1056{
1057 if (__ti == typeid(_F))
1058 return &__f_.first();
1059 return (const void*)0;
1060}
1061
1062template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1063const std::type_info&
1064__func<_F, _Alloc, _R(_ArgTypes...)>::target_type() const
1065{
1066 return typeid(_F);
1067}
1068
Howard Hinnant324bb032010-08-22 00:02:43 +00001069#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001070
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071} // __function
1072
1073template<class _R, class ..._ArgTypes>
1074class function<_R(_ArgTypes...)>
1075 : public __function::__maybe_derive_from_unary_function<_R(_ArgTypes...)>,
1076 public __function::__maybe_derive_from_binary_function<_R(_ArgTypes...)>
1077{
1078 typedef __function::__base<_R(_ArgTypes...)> __base;
1079 aligned_storage<3*sizeof(void*)>::type __buf_;
1080 __base* __f_;
1081
1082 template <class _F>
1083 static bool __not_null(const _F&) {return true;}
1084 template <class _R2, class ..._A>
1085 static bool __not_null(_R2 (*__p)(_A...)) {return __p;}
1086 template <class _R2, class _C, class ..._A>
1087 static bool __not_null(_R2 (_C::*__p)(_A...)) {return __p;}
1088 template <class _R2, class _C, class ..._A>
1089 static bool __not_null(_R2 (_C::*__p)(_A...) const) {return __p;}
1090 template <class _R2, class _C, class ..._A>
1091 static bool __not_null(_R2 (_C::*__p)(_A...) volatile) {return __p;}
1092 template <class _R2, class _C, class ..._A>
1093 static bool __not_null(_R2 (_C::*__p)(_A...) const volatile) {return __p;}
1094 template <class _R2, class ..._A>
1095 static bool __not_null(const function<_R(_A...)>& __p) {return __p;}
1096public:
1097 typedef _R result_type;
1098
Howard Hinnant72552802010-08-20 19:36:46 +00001099 // construct/copy/destroy:
Howard Hinnante00e0302010-08-19 19:20:10 +00001100 function() : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101 function(nullptr_t) : __f_(0) {}
1102 function(const function&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001103 function(function&&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104 template<class _F>
1105 function(_F,
1106 typename enable_if<!is_integral<_F>::value>::type* = 0);
1107
Howard Hinnant72552802010-08-20 19:36:46 +00001108 template<class _Alloc>
1109 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1110 template<class _Alloc>
1111 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1112 template<class _Alloc>
1113 function(allocator_arg_t, const _Alloc&, const function&);
1114 template<class _Alloc>
1115 function(allocator_arg_t, const _Alloc&, function&&);
1116 template<class _F, class _Alloc>
1117 function(allocator_arg_t, const _Alloc& __a, _F __f,
1118 typename enable_if<!is_integral<_F>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001119
1120 function& operator=(const function&);
1121 function& operator=(function&&);
1122 function& operator=(nullptr_t);
1123 template<class _F>
1124 typename enable_if
1125 <
1126 !is_integral<typename decay<_F>::type>::value,
1127 function&
1128 >::type
1129 operator=(_F&&);
1130
1131 ~function();
1132
Howard Hinnant72552802010-08-20 19:36:46 +00001133 // function modifiers:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134 void swap(function&);
Howard Hinnant72552802010-08-20 19:36:46 +00001135 template<class _F, class _Alloc>
1136 void assign(_F&& __f, const _Alloc& __a)
1137 {function(allocator_arg, __a, _STD::forward<_F>(__f)).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138
Howard Hinnant72552802010-08-20 19:36:46 +00001139 // function capacity:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140 /*explicit*/ operator bool() const {return __f_;}
1141
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142 // deleted overloads close possible hole in the type system
1143 template<class _R2, class... _ArgTypes2>
Howard Hinnanta0f1dc92010-09-11 15:33:21 +00001144 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001145 template<class _R2, class... _ArgTypes2>
Howard Hinnanta0f1dc92010-09-11 15:33:21 +00001146 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147public:
Howard Hinnant72552802010-08-20 19:36:46 +00001148 // function invocation:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149 _R operator()(_ArgTypes...) const;
1150
Howard Hinnantd4444702010-08-11 17:04:31 +00001151#ifndef _LIBCPP_NO_RTTI
Howard Hinnant72552802010-08-20 19:36:46 +00001152 // function target access:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153 const std::type_info& target_type() const;
1154 template <typename _T> _T* target();
1155 template <typename _T> const _T* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001156#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001157};
1158
1159template<class _R, class ..._ArgTypes>
1160function<_R(_ArgTypes...)>::function(const function& __f)
1161{
1162 if (__f.__f_ == 0)
1163 __f_ = 0;
1164 else if (__f.__f_ == (const __base*)&__f.__buf_)
1165 {
1166 __f_ = (__base*)&__buf_;
1167 __f.__f_->__clone(__f_);
1168 }
1169 else
1170 __f_ = __f.__f_->__clone();
1171}
1172
1173template<class _R, class ..._ArgTypes>
Howard Hinnant72552802010-08-20 19:36:46 +00001174template <class _Alloc>
1175function<_R(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1176 const function& __f)
1177{
1178 if (__f.__f_ == 0)
1179 __f_ = 0;
1180 else if (__f.__f_ == (const __base*)&__f.__buf_)
1181 {
1182 __f_ = (__base*)&__buf_;
1183 __f.__f_->__clone(__f_);
1184 }
1185 else
1186 __f_ = __f.__f_->__clone();
1187}
1188
1189template<class _R, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190function<_R(_ArgTypes...)>::function(function&& __f)
1191{
1192 if (__f.__f_ == 0)
1193 __f_ = 0;
1194 else if (__f.__f_ == (__base*)&__f.__buf_)
1195 {
1196 __f_ = (__base*)&__buf_;
1197 __f.__f_->__clone(__f_);
1198 }
1199 else
1200 {
1201 __f_ = __f.__f_;
1202 __f.__f_ = 0;
1203 }
1204}
1205
1206template<class _R, class ..._ArgTypes>
Howard Hinnant72552802010-08-20 19:36:46 +00001207template <class _Alloc>
1208function<_R(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1209 function&& __f)
1210{
1211 if (__f.__f_ == 0)
1212 __f_ = 0;
1213 else if (__f.__f_ == (__base*)&__f.__buf_)
1214 {
1215 __f_ = (__base*)&__buf_;
1216 __f.__f_->__clone(__f_);
1217 }
1218 else
1219 {
1220 __f_ = __f.__f_;
1221 __f.__f_ = 0;
1222 }
1223}
1224
1225template<class _R, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226template <class _F>
1227function<_R(_ArgTypes...)>::function(_F __f,
1228 typename enable_if<!is_integral<_F>::value>::type*)
1229 : __f_(0)
1230{
1231 if (__not_null(__f))
1232 {
1233 typedef __function::__func<_F, allocator<_F>, _R(_ArgTypes...)> _FF;
1234 if (sizeof(_FF) <= sizeof(__buf_))
1235 {
1236 __f_ = (__base*)&__buf_;
1237 ::new (__f_) _FF(_STD::move(__f));
1238 }
1239 else
1240 {
1241 typedef allocator<_FF> _A;
1242 _A __a;
1243 typedef __allocator_destructor<_A> _D;
1244 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1245 ::new (__hold.get()) _FF(_STD::move(__f), allocator<_F>(__a));
1246 __f_ = __hold.release();
1247 }
1248 }
1249}
1250
1251template<class _R, class ..._ArgTypes>
Howard Hinnant72552802010-08-20 19:36:46 +00001252template <class _F, class _Alloc>
1253function<_R(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1254 typename enable_if<!is_integral<_F>::value>::type*)
1255 : __f_(0)
1256{
1257 typedef allocator_traits<_Alloc> __alloc_traits;
1258 if (__not_null(__f))
1259 {
1260 typedef __function::__func<_F, _Alloc, _R(_ArgTypes...)> _FF;
1261 if (sizeof(_FF) <= sizeof(__buf_))
1262 {
1263 __f_ = (__base*)&__buf_;
1264 ::new (__f_) _FF(_STD::move(__f));
1265 }
1266 else
1267 {
1268 typedef typename __alloc_traits::template
1269#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1270 rebind_alloc<_FF>
1271#else
1272 rebind_alloc<_FF>::other
1273#endif
1274 _A;
1275 _A __a(__a0);
1276 typedef __allocator_destructor<_A> _D;
1277 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1278 ::new (__hold.get()) _FF(_STD::move(__f), _Alloc(__a));
1279 __f_ = __hold.release();
1280 }
1281 }
1282}
1283
1284template<class _R, class ..._ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285function<_R(_ArgTypes...)>&
1286function<_R(_ArgTypes...)>::operator=(const function& __f)
1287{
1288 function(__f).swap(*this);
1289 return *this;
1290}
1291
1292template<class _R, class ..._ArgTypes>
1293function<_R(_ArgTypes...)>&
1294function<_R(_ArgTypes...)>::operator=(function&& __f)
1295{
1296 if (__f_ == (__base*)&__buf_)
1297 __f_->destroy();
1298 else if (__f_)
1299 __f_->destroy_deallocate();
1300 __f_ = 0;
1301 if (__f.__f_ == 0)
1302 __f_ = 0;
1303 else if (__f.__f_ == (__base*)&__f.__buf_)
1304 {
1305 __f_ = (__base*)&__buf_;
1306 __f.__f_->__clone(__f_);
1307 }
1308 else
1309 {
1310 __f_ = __f.__f_;
1311 __f.__f_ = 0;
1312 }
1313}
1314
1315template<class _R, class ..._ArgTypes>
1316function<_R(_ArgTypes...)>&
1317function<_R(_ArgTypes...)>::operator=(nullptr_t)
1318{
1319 if (__f_ == (__base*)&__buf_)
1320 __f_->destroy();
1321 else if (__f_)
1322 __f_->destroy_deallocate();
1323 __f_ = 0;
1324}
1325
1326template<class _R, class ..._ArgTypes>
1327template <class _F>
1328typename enable_if
1329<
1330 !is_integral<typename decay<_F>::type>::value,
1331 function<_R(_ArgTypes...)>&
1332>::type
1333function<_R(_ArgTypes...)>::operator=(_F&& __f)
1334{
1335 function(_STD::forward<_F>(__f)).swap(*this);
1336 return *this;
1337}
1338
1339template<class _R, class ..._ArgTypes>
1340function<_R(_ArgTypes...)>::~function()
1341{
1342 if (__f_ == (__base*)&__buf_)
1343 __f_->destroy();
1344 else if (__f_)
1345 __f_->destroy_deallocate();
1346}
1347
1348template<class _R, class ..._ArgTypes>
1349void
1350function<_R(_ArgTypes...)>::swap(function& __f)
1351{
1352 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1353 {
1354 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1355 __base* __t = (__base*)&__tempbuf;
1356 __f_->__clone(__t);
1357 __f_->destroy();
1358 __f_ = 0;
1359 __f.__f_->__clone((__base*)&__buf_);
1360 __f.__f_->destroy();
1361 __f.__f_ = 0;
1362 __f_ = (__base*)&__buf_;
1363 __t->__clone((__base*)&__f.__buf_);
1364 __t->destroy();
1365 __f.__f_ = (__base*)&__f.__buf_;
1366 }
1367 else if (__f_ == (__base*)&__buf_)
1368 {
1369 __f_->__clone((__base*)&__f.__buf_);
1370 __f_->destroy();
1371 __f_ = __f.__f_;
1372 __f.__f_ = (__base*)&__f.__buf_;
1373 }
1374 else if (__f.__f_ == (__base*)&__f.__buf_)
1375 {
1376 __f.__f_->__clone((__base*)&__buf_);
1377 __f.__f_->destroy();
1378 __f.__f_ = __f_;
1379 __f_ = (__base*)&__buf_;
1380 }
1381 else
1382 _STD::swap(__f_, __f.__f_);
1383}
1384
1385template<class _R, class ..._ArgTypes>
1386_R
1387function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1388{
Howard Hinnantd4444702010-08-11 17:04:31 +00001389#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390 if (__f_ == 0)
1391 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001392#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 return (*__f_)(_STD::forward<_ArgTypes>(__arg)...);
1394}
1395
Howard Hinnantd4444702010-08-11 17:04:31 +00001396#ifndef _LIBCPP_NO_RTTI
1397
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001398template<class _R, class ..._ArgTypes>
1399const std::type_info&
1400function<_R(_ArgTypes...)>::target_type() const
1401{
1402 if (__f_ == 0)
1403 return typeid(void);
1404 return __f_->target_type();
1405}
1406
1407template<class _R, class ..._ArgTypes>
1408template <typename _T>
1409_T*
1410function<_R(_ArgTypes...)>::target()
1411{
1412 if (__f_ == 0)
1413 return (_T*)0;
1414 return (_T*)__f_->target(typeid(_T));
1415}
1416
1417template<class _R, class ..._ArgTypes>
1418template <typename _T>
1419const _T*
1420function<_R(_ArgTypes...)>::target() const
1421{
1422 if (__f_ == 0)
1423 return (const _T*)0;
1424 return (const _T*)__f_->target(typeid(_T));
1425}
1426
Howard Hinnant324bb032010-08-22 00:02:43 +00001427#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001428
Howard Hinnant324bb032010-08-22 00:02:43 +00001429template <class _R, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430inline _LIBCPP_INLINE_VISIBILITY
1431bool
1432operator==(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return !__f;}
1433
Howard Hinnant324bb032010-08-22 00:02:43 +00001434template <class _R, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435inline _LIBCPP_INLINE_VISIBILITY
1436bool
1437operator==(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return !__f;}
1438
Howard Hinnant324bb032010-08-22 00:02:43 +00001439template <class _R, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440inline _LIBCPP_INLINE_VISIBILITY
1441bool
1442operator!=(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return (bool)__f;}
1443
Howard Hinnant324bb032010-08-22 00:02:43 +00001444template <class _R, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445inline _LIBCPP_INLINE_VISIBILITY
1446bool
1447operator!=(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return (bool)__f;}
1448
Howard Hinnant324bb032010-08-22 00:02:43 +00001449template <class _R, class... _ArgTypes>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450inline _LIBCPP_INLINE_VISIBILITY
1451void
1452swap(function<_R(_ArgTypes...)>& __x, function<_R(_ArgTypes...)>& __y)
1453{return __x.swap(__y);}
1454
1455template<class _Tp> struct __is_bind_expression : public false_type {};
1456template<class _Tp> struct is_bind_expression
1457 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1458
1459template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1460template<class _Tp> struct is_placeholder
1461 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1462
1463namespace placeholders
1464{
1465
1466template <int _N> struct __ph {};
1467
1468extern __ph<1> _1;
1469extern __ph<2> _2;
1470extern __ph<3> _3;
1471extern __ph<4> _4;
1472extern __ph<5> _5;
1473extern __ph<6> _6;
1474extern __ph<7> _7;
1475extern __ph<8> _8;
1476extern __ph<9> _9;
1477extern __ph<10> _10;
1478
1479} // placeholders
1480
1481template<int _N>
1482struct __is_placeholder<placeholders::__ph<_N> >
1483 : public integral_constant<int, _N> {};
1484
1485template <class _Tp, class _Uj>
1486inline _LIBCPP_INLINE_VISIBILITY
1487_Tp&
1488__mu(reference_wrapper<_Tp> __t, _Uj&)
1489{
1490 return __t.get();
1491}
1492
1493template <bool _IsBindExpr, class _Ti, class ..._Uj>
1494struct __mu_return1 {};
1495
1496template <class _Ti, class ..._Uj>
1497struct __mu_return1<true, _Ti, _Uj...>
1498{
1499 typedef typename result_of<_Ti(_Uj...)>::type type;
1500};
1501
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502template <class _Ti, class ..._Uj, size_t ..._Indx>
1503inline _LIBCPP_INLINE_VISIBILITY
1504typename __mu_return1<true, _Ti, _Uj...>::type
1505__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1506{
1507 return __ti(_STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
1508}
1509
1510template <class _Ti, class ..._Uj>
1511inline _LIBCPP_INLINE_VISIBILITY
1512typename enable_if
1513<
1514 is_bind_expression<_Ti>::value,
1515 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1516>::type
1517__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1518{
1519 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1520 return __mu_expand(__ti, __uj, __indices());
1521}
1522
1523template <bool IsPh, class _Ti, class _Uj>
1524struct __mu_return2 {};
1525
1526template <class _Ti, class _Uj>
1527struct __mu_return2<true, _Ti, _Uj>
1528{
1529 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1530};
1531
1532template <class _Ti, class _Uj>
1533inline _LIBCPP_INLINE_VISIBILITY
1534typename enable_if
1535<
1536 0 < is_placeholder<_Ti>::value,
1537 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1538>::type
1539__mu(_Ti&, _Uj& __uj)
1540{
1541 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1542 // compiler bug workaround
1543 typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1544 return __t;
1545// return _STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
1546}
1547
1548template <class _Ti, class _Uj>
1549inline _LIBCPP_INLINE_VISIBILITY
1550typename enable_if
1551<
1552 !is_bind_expression<_Ti>::value &&
1553 is_placeholder<_Ti>::value == 0 &&
1554 !__is_reference_wrapper<_Ti>::value,
1555 _Ti&
1556>::type
1557__mu(_Ti& __ti, _Uj& __uj)
1558{
1559 return __ti;
1560}
1561
1562template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1563struct ____mu_return;
1564
1565template <class _Ti, class ..._Uj>
1566struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1567{
1568 typedef typename result_of<_Ti(_Uj...)>::type type;
1569};
1570
1571template <class _Ti, class _TupleUj>
1572struct ____mu_return<_Ti, false, true, _TupleUj>
1573{
1574 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1575 _TupleUj>::type&& type;
1576};
1577
1578template <class _Ti, class _TupleUj>
1579struct ____mu_return<_Ti, false, false, _TupleUj>
1580{
1581 typedef _Ti& type;
1582};
1583
1584template <class _Ti, class _TupleUj>
1585struct __mu_return
1586 : public ____mu_return<_Ti,
1587 is_bind_expression<_Ti>::value,
1588 0 < is_placeholder<_Ti>::value,
1589 _TupleUj>
1590{
1591};
1592
1593template <class _Ti, class _TupleUj>
1594struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1595{
1596 typedef _Ti& type;
1597};
1598
1599template <class _F, class _BoundArgs, class _TupleUj>
1600struct __bind_return;
1601
1602template <class _F, class ..._BoundArgs, class _TupleUj>
1603struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj>
1604{
1605 typedef typename __invoke_return
1606 <
1607 _F&,
1608 typename __mu_return
1609 <
1610 _BoundArgs,
1611 _TupleUj
1612 >::type...
1613 >::type type;
1614};
1615
1616template <class _F, class ..._BoundArgs, class _TupleUj>
1617struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj>
1618{
1619 typedef typename __invoke_return
1620 <
1621 _F&,
1622 typename __mu_return
1623 <
1624 const _BoundArgs,
1625 _TupleUj
1626 >::type...
1627 >::type type;
1628};
1629
1630template <class _F, class _BoundArgs, size_t ..._Indx, class _Args>
1631inline _LIBCPP_INLINE_VISIBILITY
1632typename __bind_return<_F, _BoundArgs, _Args>::type
1633__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
1634 _Args&& __args)
1635{
1636 return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
1637}
1638
Howard Hinnant324bb032010-08-22 00:02:43 +00001639template<class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640class __bind
1641 : public __weak_result_type<_F>
1642{
1643 _F __f_;
1644 tuple<_BoundArgs...> __bound_args_;
1645
1646 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
1647public:
1648 __bind(__bind&& __b)
1649 : __f_(_STD::move(__b.__f_)),
1650 __bound_args_(_STD::move(__b.__bound_args_)) {}
1651
1652 template <class _G, class ..._BA>
1653 explicit __bind(_G&& __f, _BA&& ...__bound_args)
1654 : __f_(_STD::forward<_G>(__f)),
1655 __bound_args_(_STD::forward<_BA>(__bound_args)...) {}
1656
1657 template <class ..._Args>
1658 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1659 operator()(_Args&& ...__args)
1660 {
1661 // compiler bug workaround
Howard Hinnant324bb032010-08-22 00:02:43 +00001662 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663 tuple<_Args&&...>(__args...));
1664 }
1665
1666 template <class ..._Args>
1667 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1668 operator()(_Args&& ...__args) const
1669 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001670 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671 tuple<_Args&&...>(__args...));
1672 }
1673};
1674
Howard Hinnant324bb032010-08-22 00:02:43 +00001675template<class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {};
1677
Howard Hinnant324bb032010-08-22 00:02:43 +00001678template<class _R, class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679class __bind_r
1680 : public __bind<_F, _BoundArgs...>
1681{
1682 typedef __bind<_F, _BoundArgs...> base;
1683public:
1684 typedef _R result_type;
1685
1686 template <class _G, class ..._BA>
1687 explicit __bind_r(_G&& __f, _BA&& ...__bound_args)
1688 : base(_STD::forward<_G>(__f),
1689 _STD::forward<_BA>(__bound_args)...) {}
1690
1691 template <class ..._Args>
1692 result_type
1693 operator()(_Args&& ...__args)
1694 {
1695 return base::operator()(_STD::forward<_Args>(__args)...);
1696 }
1697
1698 template <class ..._Args>
1699 result_type
1700 operator()(_Args&& ...__args) const
1701 {
1702 return base::operator()(_STD::forward<_Args>(__args)...);
1703 }
1704};
1705
Howard Hinnant324bb032010-08-22 00:02:43 +00001706template<class _R, class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {};
1708
Howard Hinnant324bb032010-08-22 00:02:43 +00001709template<class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710inline _LIBCPP_INLINE_VISIBILITY
1711__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1712bind(_F&& __f, _BoundArgs&&... __bound_args)
1713{
1714 typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1715 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1716}
1717
Howard Hinnant324bb032010-08-22 00:02:43 +00001718template<class _R, class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719inline _LIBCPP_INLINE_VISIBILITY
1720__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1721bind(_F&& __f, _BoundArgs&&... __bound_args)
1722{
1723 typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1724 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1725}
1726
1727#endif // _LIBCPP_HAS_NO_VARIADICS
1728
1729template <>
1730struct hash<bool>
1731 : public unary_function<bool, size_t>
1732{
1733 size_t operator()(bool __v) const {return static_cast<size_t>(__v);}
1734};
1735
1736template <>
1737struct hash<char>
1738 : public unary_function<char, size_t>
1739{
1740 size_t operator()(char __v) const {return static_cast<size_t>(__v);}
1741};
1742
1743template <>
1744struct hash<signed char>
1745 : public unary_function<signed char, size_t>
1746{
1747 size_t operator()(signed char __v) const {return static_cast<size_t>(__v);}
1748};
1749
1750template <>
1751struct hash<unsigned char>
1752 : public unary_function<unsigned char, size_t>
1753{
1754 size_t operator()(unsigned char __v) const {return static_cast<size_t>(__v);}
1755};
1756
1757#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
1758
1759template <>
1760struct hash<char16_t>
1761 : public unary_function<char16_t, size_t>
1762{
1763 size_t operator()(char16_t __v) const {return static_cast<size_t>(__v);}
1764};
1765
1766template <>
1767struct hash<char32_t>
1768 : public unary_function<char32_t, size_t>
1769{
1770 size_t operator()(char32_t __v) const {return static_cast<size_t>(__v);}
1771};
1772
Howard Hinnant324bb032010-08-22 00:02:43 +00001773#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774
1775template <>
1776struct hash<wchar_t>
1777 : public unary_function<wchar_t, size_t>
1778{
1779 size_t operator()(wchar_t __v) const {return static_cast<size_t>(__v);}
1780};
1781
1782template <>
1783struct hash<short>
1784 : public unary_function<short, size_t>
1785{
1786 size_t operator()(short __v) const {return static_cast<size_t>(__v);}
1787};
1788
1789template <>
1790struct hash<unsigned short>
1791 : public unary_function<unsigned short, size_t>
1792{
1793 size_t operator()(unsigned short __v) const {return static_cast<size_t>(__v);}
1794};
1795
1796template <>
1797struct hash<int>
1798 : public unary_function<int, size_t>
1799{
1800 size_t operator()(int __v) const {return static_cast<size_t>(__v);}
1801};
1802
1803template <>
1804struct hash<unsigned int>
1805 : public unary_function<unsigned int, size_t>
1806{
1807 size_t operator()(unsigned int __v) const {return static_cast<size_t>(__v);}
1808};
1809
1810template <>
1811struct hash<long>
1812 : public unary_function<long, size_t>
1813{
1814 size_t operator()(long __v) const {return static_cast<size_t>(__v);}
1815};
1816
1817template <>
1818struct hash<unsigned long>
1819 : public unary_function<unsigned long, size_t>
1820{
1821 size_t operator()(unsigned long __v) const {return static_cast<size_t>(__v);}
1822};
1823
1824template <>
1825struct hash<long long>
1826 : public unary_function<long long, size_t>
1827{
1828 size_t operator()(long long __v) const
1829 {
1830 size_t __r = 0;
1831 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1832 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1833 __r ^= __p[__i];
1834 return __r;
1835 }
1836};
1837
1838template <>
1839struct hash<unsigned long long>
1840 : public unary_function<unsigned long long, size_t>
1841{
1842 size_t operator()(unsigned long long __v) const
1843 {
1844 size_t __r = 0;
1845 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1846 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1847 __r ^= __p[__i];
1848 return __r;
1849 }
1850};
1851
1852template <>
1853struct hash<float>
1854 : public unary_function<float, size_t>
1855{
1856 size_t operator()(float __v) const
1857 {
1858 if (__v == 0)
1859 return 0;
1860 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1861 return *__p;
1862 }
1863};
1864
1865template <>
1866struct hash<double>
1867 : public unary_function<double, size_t>
1868{
1869 size_t operator()(double __v) const
1870 {
1871 if (__v == 0)
1872 return 0;
1873 size_t __r = 0;
1874 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1875 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1876 __r ^= __p[__i];
1877 return __r;
1878 }
1879};
1880
1881template <>
1882struct hash<long double>
1883 : public unary_function<long double, size_t>
1884{
1885 size_t operator()(long double __v) const
1886 {
1887 if (__v == 0)
1888 return 0;
1889 size_t __r = 0;
1890 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1891 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1892 __r ^= __p[__i];
1893 return __r;
1894 }
1895};
1896
Howard Hinnant21aefc32010-06-03 16:42:57 +00001897// struct hash<T*> in <memory>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898
1899_LIBCPP_END_NAMESPACE_STD
1900
1901#endif // _LIBCPP_FUNCTIONAL