blob: 1551fb59aa570819c60f6aa0521f3cef59658b36 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------ functional ----------------------------------===//
3//
4// ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure
5//
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
35template <ObjectType T>
36class 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>
59 requires Callable<T, ArgTypes&&...>
60 Callable<T, ArgTypes&&...>::result_type
61 operator() (ArgTypes&&...) const;
62};
63
64template <ObjectType T> reference_wrapper<T> ref(T& t);
65template <ObjectType T> void ref(const T&& t) = delete; // LWG 688
66template <ObjectType T> reference_wrapper<T> ref(reference_wrapper<T>t);
67
68template <ObjectType T> reference_wrapper<const T> cref(const T& t);
69template <ObjectType T> void cref(const T&& t) = delete; // LWG 688
70template <ObjectType T> reference_wrapper<const T> cref(reference_wrapper<T> t);
71
72template <class T>
73struct plus : binary_function<T, T, T>
74{
75 T operator()(const T& x, const T& y) const;
76};
77
78template <class T>
79struct minus : binary_function<T, T, T>
80{
81 T operator()(const T& x, const T& y) const;
82};
83
84template <class T>
85struct multiplies : binary_function<T, T, T>
86{
87 T operator()(const T& x, const T& y) const;
88};
89
90template <class T>
91struct divides : binary_function<T, T, T>
92{
93 T operator()(const T& x, const T& y) const;
94};
95
96template <class T>
97struct modulus : binary_function<T, T, T>
98{
99 T operator()(const T& x, const T& y) const;
100};
101
102template <class T>
103struct negate : unary_function<T, T>
104{
105 T operator()(const T& x) const;
106};
107
108template <class T>
109struct equal_to : binary_function<T, T, bool>
110{
111 bool operator()(const T& x, const T& y) const;
112};
113
114template <class T>
115struct not_equal_to : binary_function<T, T, bool>
116{
117 bool operator()(const T& x, const T& y) const;
118};
119
120template <class T>
121struct greater : binary_function<T, T, bool>
122{
123 bool operator()(const T& x, const T& y) const;
124};
125
126template <class T>
127struct less : binary_function<T, T, bool>
128{
129 bool operator()(const T& x, const T& y) const;
130};
131
132template <class T>
133struct greater_equal : binary_function<T, T, bool>
134{
135 bool operator()(const T& x, const T& y) const;
136};
137
138template <class T>
139struct less_equal : binary_function<T, T, bool>
140{
141 bool operator()(const T& x, const T& y) const;
142};
143
144template <class T>
145struct logical_and : binary_function<T, T, bool>
146{
147 bool operator()(const T& x, const T& y) const;
148};
149
150template <class T>
151struct logical_or : binary_function<T, T, bool>
152{
153 bool operator()(const T& x, const T& y) const;
154};
155
156template <class T>
157struct logical_not : unary_function<T, bool>
158{
159 bool operator()(const T& x) const;
160};
161
162template <class Predicate>
163class unary_negate
164 : public unary_function<typename Predicate::argument_type, bool>
165{
166public:
167 explicit unary_negate(const Predicate& pred);
168 bool operator()(const typename Predicate::argument_type& x) const;
169};
170
171template <class Predicate> unary_negate<Predicate> not1(const Predicate& pred);
172
173template <class Predicate>
174class binary_negate
175 : public binary_function<typename Predicate::first_argument_type,
176 typename Predicate::second_argument_type,
177 bool>
178{
179public:
180 explicit binary_negate(const Predicate& pred);
181 bool operator()(const typename Predicate::first_argument_type& x,
182 const typename Predicate::second_argument_type& y) const;
183};
184
185template <class Predicate> binary_negate<Predicate> not2(const Predicate& pred);
186
187template<class T> struct is_bind_expression;
188template<class T> struct is_placeholder;
189
190template<CopyConstructible Fn, CopyConstructible... Types>
191 unspecified bind(Fn, Types...);
192template<Returnable R, CopyConstructible Fn, CopyConstructible... Types>
193 unspecified bind(Fn, Types...);
194
195namespace placeholders {
196 // M is the implementation-defined number of placeholders
197 extern unspecified _1;
198 extern unspecified _2;
199 .
200 .
201 .
202 extern unspecified _M;
203}
204
205template <class Operation>
206class binder1st
207 : public unary_function<typename Operation::second_argument_type,
208 typename Operation::result_type>
209{
210protected:
211 Operation op;
212 typename Operation::first_argument_type value;
213public:
214 binder1st(const Operation& x, const typename Operation::first_argument_type y);
215 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
216 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
217};
218
219template <class Operation, class T>
220binder1st<Operation> bind1st(const Operation& op, const T& x);
221
222template <class Operation>
223class binder2nd
224 : public unary_function<typename Operation::first_argument_type,
225 typename Operation::result_type>
226{
227protected:
228 Operation op;
229 typename Operation::second_argument_type value;
230public:
231 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
232 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
233 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
234};
235
236template <class Operation, class T>
237binder2nd<Operation> bind2nd(const Operation& op, const T& x);
238
239template <class Arg, class Result>
240class pointer_to_unary_function : public unary_function<Arg, Result>
241{
242public:
243 explicit pointer_to_unary_function(Result (*f)(Arg));
244 Result operator()(Arg x) const;
245};
246
247template <class Arg, class Result>
248pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg));
249
250template <class Arg1, class Arg2, class Result>
251class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
252{
253public:
254 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
255 Result operator()(Arg1 x, Arg2 y) const;
256};
257
258template <class Arg1, class Arg2, class Result>
259pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2));
260
261template<class S, class T>
262class mem_fun_t : public unary_function<T*, S>
263{
264public:
265 explicit mem_fun_t(S (T::*p)());
266 S operator()(T* p) const;
267};
268
269template<class S, class T, class A>
270class mem_fun1_t : public binary_function<T*, A, S>
271{
272public:
273 explicit mem_fun1_t(S (T::*p)(A));
274 S operator()(T* p, A x) const;
275};
276
277template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)());
278template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A));
279
280template<class S, class T>
281class mem_fun_ref_t : public unary_function<T, S>
282{
283public:
284 explicit mem_fun_ref_t(S (T::*p)());
285 S operator()(T& p) const;
286};
287
288template<class S, class T, class A>
289class mem_fun1_ref_t : public binary_function<T, A, S>
290{
291public:
292 explicit mem_fun1_ref_t(S (T::*p)(A));
293 S operator()(T& p, A x) const;
294};
295
296template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)());
297template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A));
298
299template <class S, class T>
300class const_mem_fun_t : public unary_function<const T*, S>
301{
302public:
303 explicit const_mem_fun_t(S (T::*p)() const);
304 S operator()(const T* p) const;
305};
306
307template <class S, class T, class A>
308class const_mem_fun1_t : public binary_function<const T*, A, S>
309{
310public:
311 explicit const_mem_fun1_t(S (T::*p)(A) const);
312 S operator()(const T* p, A x) const;
313};
314
315template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const);
316template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const);
317
318template <class S, class T>
319class const_mem_fun_ref_t : public unary_function<T, S>
320{
321public:
322 explicit const_mem_fun_ref_t(S (T::*p)() const);
323 S operator()(const T& p) const;
324};
325
326template <class S, class T, class A>
327class const_mem_fun1_ref_t : public binary_function<T, A, S>
328{
329public:
330 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
331 S operator()(const T& p, A x) const;
332};
333
334template <class S, class T> const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const);
335template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const);
336
337class bad_function_call
338 : public exception
339{
340};
341
342template<Returnable R, class T> unspecified mem_fn(R T::* pm);
343template<Returnable R, class T, CopyConstructible... Args>
344 unspecified mem_fn(R (T::* pm)(Args...));
345template<Returnable R, class T, CopyConstructible... Args>
346 unspecified mem_fn(R (T::* pm)(Args...) const);
347template<Returnable R, class T, CopyConstructible... Args>
348 unspecified mem_fn(R (T::* pm)(Args...) volatile);
349template<Returnable R, class T, CopyConstructible... Args>
350 unspecified mem_fn(R (T::* pm)(Args...) const volatile);
351
352template<FunctionType> class function; // undefined
353
354template<Returnable R, CopyConstructible... ArgTypes>
355class function<R(ArgTypes...)>
356 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
357 // ArgTypes contains T1
358 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
359 // ArgTypes contains T1 and T2
360{
361public:
362 typedef R result_type;
363
364 // 20.7.16.2.1, construct/copy/destroy:
365 explicit function();
366 function(nullptr_t);
367 function(const function&);
368 function(function&&);
369 template<class F>
370 requires CopyConstructible<F> && Callable<F, ArgTypes...>
371 && Convertible<Callable<F, ArgTypes...>::result_type, R>
372 function(F);
373// template<class F>
374// requires CopyConstructible<F> && Callable<F, ArgTypes...>
375// && Convertible<Callable<F, ArgTypes...>::result_type, R>
376// function(F&&);
377 template<Allocator Alloc>
378 function(allocator_arg_t, const Alloc&);
379 template<Allocator Alloc>
380 function(allocator_arg_t, const Alloc&, nullptr_t);
381 template<Allocator Alloc>
382 function(allocator_arg_t, const Alloc&, const function&);
383 template<Allocator Alloc>
384 function(allocator_arg_t, const Alloc&, function&&);
385 template<class F, Allocator Alloc>
386 function(allocator_arg_t, const Alloc&, F);
387// template<class F, Allocator Alloc>
388// function(allocator_arg_t, const Alloc&, F&&);
389
390 function& operator=(const function&);
391 function& operator=(function&&);
392 function& operator=(nullptr_t);
393 template<class F>
394 requires CopyConstructible<F> && Callable<F, ArgTypes..>
395 && Convertible<Callable<F, ArgTypes...>::result_type
396 function& operator=(F);
397// template<class F>
398// requires CopyConstructible<F> && Callable<F, ArgTypes...>
399// && Convertible<Callable<F, ArgTypes...>::result_type, R>
400// function& operator=(F&&);
401 template<class F>
402 requires Callable<F, ArgTypes...>
403 && Convertible<Callable<F, ArgTypes...>::result_type, R>
404 function& operator=(reference_wrapper<F>);
405
406 ~function();
407
408 // 20.7.16.2.2, function modifiers:
409 void swap(function&);
410 template<class F, Allocator Alloc>
411 requires Callable<F, ArgTypes...>
412 && Convertible<Callable<F, ArgTypes...>::result_type, R>
413 void assign(F, const Alloc&);
414
415 // 20.7.16.2.3, function capacity:
416 explicit operator bool() const;
417
418 // deleted overloads close possible hole in the type system
419 template<class R2, class... ArgTypes2>
420 bool operator==(const function<R2(ArgTypes2...)>&) = delete;
421 template<class R2, class... ArgTypes2>
422 bool operator!=(const function<R2(ArgTypes2...)>&) = delete;
423
424 // 20.7.16.2.4, function invocation:
425 R operator()(ArgTypes...) const;
426
427 // 20.7.16.2.5, function target access:
428 const std::type_info& target_type() const;
429 template <typename T>
430 requires Callable<T, ArgTypes...>
431 && Convertible<Callable<T, ArgTypes...>::result_type, R>
432 T* target();
433 template <typename T>
434 requires Callable<T, ArgTypes...>
435 && Convertible<Callable<T, ArgTypes...>::result_type, R>
436 const T* target() const;
437};
438
439// 20.7.16.2.6, Null pointer comparisons:
440template <MoveConstructible R, MoveConstructible ... ArgTypes>
441 bool operator==(const function<R(ArgTypes...)>&, nullptr_t);
442
443template <MoveConstructible R, MoveConstructible ... ArgTypes>
444 bool operator==(nullptr_t, const function<R(ArgTypes...)>&);
445
446template <MoveConstructible R, MoveConstructible ... ArgTypes>
447 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t);
448
449template <MoveConstructible R, MoveConstructible ... ArgTypes>
450 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&);
451
452// 20.7.16.2.7, specialized algorithms:
453template <MoveConstructible R, MoveConstructible ... ArgTypes>
454 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&);
455
456template <class T> struct hash;
457
458template <> struct hash<bool>;
459template <> struct hash<char>;
460template <> struct hash<signed char>;
461template <> struct hash<unsigned char>;
462template <> struct hash<char16_t>;
463template <> struct hash<char32_t>;
464template <> struct hash<wchar_t>;
465template <> struct hash<short>;
466template <> struct hash<unsigned short>;
467template <> struct hash<int>;
468template <> struct hash<unsigned int>;
469template <> struct hash<long>;
470template <> struct hash<long long>;
471template <> struct hash<unsigned long>;
472template <> struct hash<unsigned long long>;
473
474template <> struct hash<float>;
475template <> struct hash<double>;
476template <> struct hash<long double>;
477
478template<class T> struct hash<T*>;
479
480} // std
481
482POLICY: For non-variadic implementations, the number of arguments is limited
483 to 3. It is hoped that the need for non-variadic implementations
484 will be minimal.
485
486*/
487
488#include <__config>
489#include <type_traits>
490#include <typeinfo>
491#include <exception>
492#include <memory>
493#include <tuple>
494
495#include <__functional_base>
496
497#pragma GCC system_header
498
499_LIBCPP_BEGIN_NAMESPACE_STD
500
501template <class _Tp>
502struct plus : 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 minus : 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 multiplies : binary_function<_Tp, _Tp, _Tp>
517{
518 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
519 {return __x * __y;}
520};
521
522template <class _Tp>
523struct divides : binary_function<_Tp, _Tp, _Tp>
524{
525 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
526 {return __x / __y;}
527};
528
529template <class _Tp>
530struct modulus : binary_function<_Tp, _Tp, _Tp>
531{
532 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
533 {return __x % __y;}
534};
535
536template <class _Tp>
537struct negate : unary_function<_Tp, _Tp>
538{
539 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x) const
540 {return -__x;}
541};
542
543template <class _Tp>
544struct equal_to : 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 not_equal_to : 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 greater : 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 less : 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 greater_equal : 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 less_equal : binary_function<_Tp, _Tp, bool>
580{
581 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
582 {return __x <= __y;}
583};
584
585template <class _Tp>
586struct logical_and : binary_function<_Tp, _Tp, bool>
587{
588 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
589 {return __x && __y;}
590};
591
592template <class _Tp>
593struct logical_or : binary_function<_Tp, _Tp, bool>
594{
595 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
596 {return __x || __y;}
597};
598
599template <class _Tp>
600struct logical_not : unary_function<_Tp, bool>
601{
602 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x) const
603 {return !__x;}
604};
605
606template <class _Tp>
607struct bit_and : binary_function<_Tp, _Tp, _Tp>
608{
609 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
610 {return __x & __y;}
611};
612
613template <class _Tp>
614struct bit_or : binary_function<_Tp, _Tp, _Tp>
615{
616 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
617 {return __x | __y;}
618};
619
620template <class _Tp>
621struct bit_xor : binary_function<_Tp, _Tp, _Tp>
622{
623 _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x, const _Tp& __y) const
624 {return __x ^ __y;}
625};
626
627template <class _Predicate>
628class unary_negate
629 : public unary_function<typename _Predicate::argument_type, bool>
630{
631 _Predicate __pred_;
632public:
633 _LIBCPP_INLINE_VISIBILITY explicit unary_negate(const _Predicate& __pred)
634 : __pred_(__pred) {}
635 _LIBCPP_INLINE_VISIBILITY bool operator()(const typename _Predicate::argument_type& __x) const
636 {return !__pred_(__x);}
637};
638
639template <class _Predicate>
640inline _LIBCPP_INLINE_VISIBILITY
641unary_negate<_Predicate>
642not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
643
644template <class _Predicate>
645class binary_negate
646 : public binary_function<typename _Predicate::first_argument_type,
647 typename _Predicate::second_argument_type,
648 bool>
649{
650 _Predicate __pred_;
651public:
652 _LIBCPP_INLINE_VISIBILITY explicit binary_negate(const _Predicate& __pred)
653 : __pred_(__pred) {}
654 _LIBCPP_INLINE_VISIBILITY bool operator()(const typename _Predicate::first_argument_type& __x,
655 const typename _Predicate::second_argument_type& __y) const
656 {return !__pred_(__x, __y);}
657};
658
659template <class _Predicate>
660inline _LIBCPP_INLINE_VISIBILITY
661binary_negate<_Predicate>
662not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
663
664template <class __Operation>
665class binder1st
666 : public unary_function<typename __Operation::second_argument_type,
667 typename __Operation::result_type>
668{
669protected:
670 __Operation op;
671 typename __Operation::first_argument_type value;
672public:
673 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
674 const typename __Operation::first_argument_type __y)
675 : op(__x), value(__y) {}
676 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
677 (typename __Operation::second_argument_type& __x) const
678 {return op(value, __x);}
679 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
680 (const typename __Operation::second_argument_type& __x) const
681 {return op(value, __x);}
682};
683
684template <class __Operation, class _Tp>
685inline _LIBCPP_INLINE_VISIBILITY
686binder1st<__Operation>
687bind1st(const __Operation& __op, const _Tp& __x)
688 {return binder1st<__Operation>(__op, __x);}
689
690template <class __Operation>
691class binder2nd
692 : public unary_function<typename __Operation::first_argument_type,
693 typename __Operation::result_type>
694{
695protected:
696 __Operation op;
697 typename __Operation::second_argument_type value;
698public:
699 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
700 : op(__x), value(__y) {}
701 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
702 ( typename __Operation::first_argument_type& __x) const
703 {return op(__x, value);}
704 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
705 (const typename __Operation::first_argument_type& __x) const
706 {return op(__x, value);}
707};
708
709template <class __Operation, class _Tp>
710inline _LIBCPP_INLINE_VISIBILITY
711binder2nd<__Operation>
712bind2nd(const __Operation& __op, const _Tp& __x)
713 {return binder2nd<__Operation>(__op, __x);}
714
715template <class _Arg, class _Result>
716class pointer_to_unary_function : public unary_function<_Arg, _Result>
717{
718 _Result (*__f_)(_Arg);
719public:
720 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
721 : __f_(__f) {}
722 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
723 {return __f_(__x);}
724};
725
726template <class _Arg, class _Result>
727inline _LIBCPP_INLINE_VISIBILITY
728pointer_to_unary_function<_Arg,_Result>
729ptr_fun(_Result (*__f)(_Arg))
730 {return pointer_to_unary_function<_Arg,_Result>(__f);}
731
732template <class _Arg1, class _Arg2, class _Result>
733class pointer_to_binary_function : public binary_function<_Arg1, _Arg2, _Result>
734{
735 _Result (*__f_)(_Arg1, _Arg2);
736public:
737 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
738 : __f_(__f) {}
739 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
740 {return __f_(__x, __y);}
741};
742
743template <class _Arg1, class _Arg2, class _Result>
744inline _LIBCPP_INLINE_VISIBILITY
745pointer_to_binary_function<_Arg1,_Arg2,_Result>
746ptr_fun(_Result (*__f)(_Arg1,_Arg2))
747 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
748
749template<class _Sp, class _Tp>
750class mem_fun_t : public unary_function<_Tp*, _Sp>
751{
752 _Sp (_Tp::*__p_)();
753public:
754 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
755 : __p_(__p) {}
756 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
757 {return (__p->*__p_)();}
758};
759
760template<class _Sp, class _Tp, class _Ap>
761class mem_fun1_t : public binary_function<_Tp*, _Ap, _Sp>
762{
763 _Sp (_Tp::*__p_)(_Ap);
764public:
765 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
766 : __p_(__p) {}
767 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
768 {return (__p->*__p_)(__x);}
769};
770
771template<class _Sp, class _Tp>
772inline _LIBCPP_INLINE_VISIBILITY
773mem_fun_t<_Sp,_Tp>
774mem_fun(_Sp (_Tp::*__f)())
775 {return mem_fun_t<_Sp,_Tp>(__f);}
776
777template<class _Sp, class _Tp, class _Ap>
778inline _LIBCPP_INLINE_VISIBILITY
779mem_fun1_t<_Sp,_Tp,_Ap>
780mem_fun(_Sp (_Tp::*__f)(_Ap))
781 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
782
783template<class _Sp, class _Tp>
784class mem_fun_ref_t : public unary_function<_Tp, _Sp>
785{
786 _Sp (_Tp::*__p_)();
787public:
788 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
789 : __p_(__p) {}
790 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
791 {return (__p.*__p_)();}
792};
793
794template<class _Sp, class _Tp, class _Ap>
795class mem_fun1_ref_t : public binary_function<_Tp, _Ap, _Sp>
796{
797 _Sp (_Tp::*__p_)(_Ap);
798public:
799 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
800 : __p_(__p) {}
801 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
802 {return (__p.*__p_)(__x);}
803};
804
805template<class _Sp, class _Tp>
806inline _LIBCPP_INLINE_VISIBILITY
807mem_fun_ref_t<_Sp,_Tp>
808mem_fun_ref(_Sp (_Tp::*__f)())
809 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
810
811template<class _Sp, class _Tp, class _Ap>
812inline _LIBCPP_INLINE_VISIBILITY
813mem_fun1_ref_t<_Sp,_Tp,_Ap>
814mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
815 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
816
817template <class _Sp, class _Tp>
818class const_mem_fun_t : public unary_function<const _Tp*, _Sp>
819{
820 _Sp (_Tp::*__p_)() const;
821public:
822 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
823 : __p_(__p) {}
824 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
825 {return (__p->*__p_)();}
826};
827
828template <class _Sp, class _Tp, class _Ap>
829class const_mem_fun1_t : public binary_function<const _Tp*, _Ap, _Sp>
830{
831 _Sp (_Tp::*__p_)(_Ap) const;
832public:
833 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
834 : __p_(__p) {}
835 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
836 {return (__p->*__p_)(__x);}
837};
838
839template <class _Sp, class _Tp>
840inline _LIBCPP_INLINE_VISIBILITY
841const_mem_fun_t<_Sp,_Tp>
842mem_fun(_Sp (_Tp::*__f)() const)
843 {return const_mem_fun_t<_Sp,_Tp>(__f);}
844
845template <class _Sp, class _Tp, class _Ap>
846inline _LIBCPP_INLINE_VISIBILITY
847const_mem_fun1_t<_Sp,_Tp,_Ap>
848mem_fun(_Sp (_Tp::*__f)(_Ap) const)
849 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
850
851template <class _Sp, class _Tp>
852class const_mem_fun_ref_t : public unary_function<_Tp, _Sp>
853{
854 _Sp (_Tp::*__p_)() const;
855public:
856 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
857 : __p_(__p) {}
858 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
859 {return (__p.*__p_)();}
860};
861
862template <class _Sp, class _Tp, class _Ap>
863class const_mem_fun1_ref_t : public binary_function<_Tp, _Ap, _Sp>
864{
865 _Sp (_Tp::*__p_)(_Ap) const;
866public:
867 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
868 : __p_(__p) {}
869 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
870 {return (__p.*__p_)(__x);}
871};
872
873template <class _Sp, class _Tp>
874inline _LIBCPP_INLINE_VISIBILITY
875const_mem_fun_ref_t<_Sp,_Tp>
876mem_fun_ref(_Sp (_Tp::*__f)() const)
877 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
878
879template <class _Sp, class _Tp, class _Ap>
880inline _LIBCPP_INLINE_VISIBILITY
881const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
882mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
883 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
884
885
886#ifdef _LIBCPP_HAS_NO_VARIADICS
887
888#include <__functional_03>
889
890#else // _LIBCPP_HAS_NO_VARIADICS
891
892template <class _Tp>
893class __mem_fn
894 : public __weak_result_type<_Tp>
895{
896public:
897 // types
898 typedef _Tp type;
899private:
900 type __f_;
901
902public:
903 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
904
905 // invoke
906 template <class... _ArgTypes>
907 typename __invoke_return<type, _ArgTypes...>::type
908 operator() (_ArgTypes&&... __args)
909 {
910 return __invoke(__f_, _STD::forward<_ArgTypes>(__args)...);
911 }
912};
913
914template<class _R, class _T>
915inline _LIBCPP_INLINE_VISIBILITY
916__mem_fn<_R _T::*>
917mem_fn(_R _T::* __pm)
918{
919 return __mem_fn<_R _T::*>(__pm);
920}
921
922template<class _R, class _T, class ..._Args>
923inline _LIBCPP_INLINE_VISIBILITY
924__mem_fn<_R (_T::*)(_Args...)>
925mem_fn(_R (_T::* __pm)(_Args...))
926{
927 return __mem_fn<_R (_T::*)(_Args...)>(__pm);
928}
929
930template<class _R, class _T, class ..._Args>
931inline _LIBCPP_INLINE_VISIBILITY
932__mem_fn<_R (_T::*)(_Args...) const>
933mem_fn(_R (_T::* __pm)(_Args...) const)
934{
935 return __mem_fn<_R (_T::*)(_Args...) const>(__pm);
936}
937
938template<class _R, class _T, class ..._Args>
939inline _LIBCPP_INLINE_VISIBILITY
940__mem_fn<_R (_T::*)(_Args...) volatile>
941mem_fn(_R (_T::* __pm)(_Args...) volatile)
942{
943 return __mem_fn<_R (_T::*)(_Args...) volatile>(__pm);
944}
945
946template<class _R, class _T, class ..._Args>
947inline _LIBCPP_INLINE_VISIBILITY
948__mem_fn<_R (_T::*)(_Args...) const volatile>
949mem_fn(_R (_T::* __pm)(_Args...) const volatile)
950{
951 return __mem_fn<_R (_T::*)(_Args...) const volatile>(__pm);
952}
953
954// bad_function_call
955
956class bad_function_call
957 : public exception
958{
959};
960
961template<class _Fp> class function; // undefined
962
963namespace __function
964{
965
966template<class _R, class ..._ArgTypes>
967struct __maybe_derive_from_unary_function
968{
969};
970
971template<class _R, class _A1>
972struct __maybe_derive_from_unary_function<_R(_A1)>
973 : public unary_function<_A1, _R>
974{
975};
976
977template<class _R, class ..._ArgTypes>
978struct __maybe_derive_from_binary_function
979{
980};
981
982template<class _R, class _A1, class _A2>
983struct __maybe_derive_from_binary_function<_R(_A1, _A2)>
984 : public binary_function<_A1, _A2, _R>
985{
986};
987
988template<class _Fp> class __base;
989
990template<class _R, class ..._ArgTypes>
991class __base<_R(_ArgTypes...)>
992{
993 __base(const __base&);
994 __base& operator=(const __base&);
995public:
996 __base() {}
997 virtual ~__base() {}
998 virtual __base* __clone() const = 0;
999 virtual void __clone(__base*) const = 0;
1000 virtual void destroy() = 0;
1001 virtual void destroy_deallocate() = 0;
1002 virtual _R operator()(_ArgTypes&& ...) = 0;
1003 virtual const void* target(const type_info&) const = 0;
1004 virtual const std::type_info& target_type() const = 0;
1005};
1006
1007template<class _FD, class _Alloc, class _FB> class __func;
1008
1009template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1010class __func<_F, _Alloc, _R(_ArgTypes...)>
1011 : public __base<_R(_ArgTypes...)>
1012{
1013 __compressed_pair<_F, _Alloc> __f_;
1014public:
1015 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
1016 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
1017 virtual __base<_R(_ArgTypes...)>* __clone() const;
1018 virtual void __clone(__base<_R(_ArgTypes...)>*) const;
1019 virtual void destroy();
1020 virtual void destroy_deallocate();
1021 virtual _R operator()(_ArgTypes&& ... __arg);
1022 virtual const void* target(const type_info&) const;
1023 virtual const std::type_info& target_type() const;
1024};
1025
1026template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1027__base<_R(_ArgTypes...)>*
1028__func<_F, _Alloc, _R(_ArgTypes...)>::__clone() const
1029{
1030 typedef typename _Alloc::template rebind<__func>::other _A;
1031 _A __a(__f_.second());
1032 typedef __allocator_destructor<_A> _D;
1033 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
1034 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
1035 return __hold.release();
1036}
1037
1038template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1039void
1040__func<_F, _Alloc, _R(_ArgTypes...)>::__clone(__base<_R(_ArgTypes...)>* __p) const
1041{
1042 ::new (__p) __func(__f_.first(), __f_.second());
1043}
1044
1045template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1046void
1047__func<_F, _Alloc, _R(_ArgTypes...)>::destroy()
1048{
1049 __f_.~__compressed_pair<_F, _Alloc>();
1050}
1051
1052template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1053void
1054__func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate()
1055{
1056 typedef typename _Alloc::template rebind<__func>::other _A;
1057 _A __a(__f_.second());
1058 __f_.~__compressed_pair<_F, _Alloc>();
1059 __a.deallocate(this, 1);
1060}
1061
1062template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1063_R
1064__func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1065{
1066 return __invoke(__f_.first(), _STD::forward<_ArgTypes>(__arg)...);
1067}
1068
1069template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1070const void*
1071__func<_F, _Alloc, _R(_ArgTypes...)>::target(const type_info& __ti) const
1072{
1073 if (__ti == typeid(_F))
1074 return &__f_.first();
1075 return (const void*)0;
1076}
1077
1078template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1079const std::type_info&
1080__func<_F, _Alloc, _R(_ArgTypes...)>::target_type() const
1081{
1082 return typeid(_F);
1083}
1084
1085} // __function
1086
1087template<class _R, class ..._ArgTypes>
1088class function<_R(_ArgTypes...)>
1089 : public __function::__maybe_derive_from_unary_function<_R(_ArgTypes...)>,
1090 public __function::__maybe_derive_from_binary_function<_R(_ArgTypes...)>
1091{
1092 typedef __function::__base<_R(_ArgTypes...)> __base;
1093 aligned_storage<3*sizeof(void*)>::type __buf_;
1094 __base* __f_;
1095
1096 template <class _F>
1097 static bool __not_null(const _F&) {return true;}
1098 template <class _R2, class ..._A>
1099 static bool __not_null(_R2 (*__p)(_A...)) {return __p;}
1100 template <class _R2, class _C, class ..._A>
1101 static bool __not_null(_R2 (_C::*__p)(_A...)) {return __p;}
1102 template <class _R2, class _C, class ..._A>
1103 static bool __not_null(_R2 (_C::*__p)(_A...) const) {return __p;}
1104 template <class _R2, class _C, class ..._A>
1105 static bool __not_null(_R2 (_C::*__p)(_A...) volatile) {return __p;}
1106 template <class _R2, class _C, class ..._A>
1107 static bool __not_null(_R2 (_C::*__p)(_A...) const volatile) {return __p;}
1108 template <class _R2, class ..._A>
1109 static bool __not_null(const function<_R(_A...)>& __p) {return __p;}
1110public:
1111 typedef _R result_type;
1112
1113 // 20.7.16.2.1, construct/copy/destroy:
1114 explicit function() : __f_(0) {}
1115 function(nullptr_t) : __f_(0) {}
1116 function(const function&);
1117#ifdef _LIBCPP_MOVE
1118 function(function&&);
1119#endif
1120 template<class _F>
1121 function(_F,
1122 typename enable_if<!is_integral<_F>::value>::type* = 0);
1123
1124// template<class _Alloc>
1125// function(allocator_arg_t, const _Alloc&);
1126// template<Allocator Alloc>
1127// function(allocator_arg_t, const Alloc&, nullptr_t);
1128// template<Allocator Alloc>
1129// function(allocator_arg_t, const Alloc&, const function&);
1130// template<Allocator Alloc>
1131// function(allocator_arg_t, const Alloc&, function&&);
1132// template<class F, Allocator Alloc>
1133// function(allocator_arg_t, const Alloc&, F);
1134
1135 function& operator=(const function&);
1136 function& operator=(function&&);
1137 function& operator=(nullptr_t);
1138 template<class _F>
1139 typename enable_if
1140 <
1141 !is_integral<typename decay<_F>::type>::value,
1142 function&
1143 >::type
1144 operator=(_F&&);
1145
1146 ~function();
1147
1148 // 20.7.16.2.2, function modifiers:
1149 void swap(function&);
1150// template<class _F, class _Alloc>
1151// void assign(_F, const _Alloc&);
1152
1153 // 20.7.16.2.3, function capacity:
1154 /*explicit*/ operator bool() const {return __f_;}
1155
1156private:
1157 // deleted overloads close possible hole in the type system
1158 template<class _R2, class... _ArgTypes2>
1159 bool operator==(const function<_R2(_ArgTypes2...)>&);// = delete;
1160 template<class _R2, class... _ArgTypes2>
1161 bool operator!=(const function<_R2(_ArgTypes2...)>&);// = delete;
1162public:
1163 // 20.7.16.2.4, function invocation:
1164 _R operator()(_ArgTypes...) const;
1165
1166 // 20.7.16.2.5, function target access:
1167 const std::type_info& target_type() const;
1168 template <typename _T> _T* target();
1169 template <typename _T> const _T* target() const;
1170};
1171
1172template<class _R, class ..._ArgTypes>
1173function<_R(_ArgTypes...)>::function(const function& __f)
1174{
1175 if (__f.__f_ == 0)
1176 __f_ = 0;
1177 else if (__f.__f_ == (const __base*)&__f.__buf_)
1178 {
1179 __f_ = (__base*)&__buf_;
1180 __f.__f_->__clone(__f_);
1181 }
1182 else
1183 __f_ = __f.__f_->__clone();
1184}
1185
1186template<class _R, class ..._ArgTypes>
1187function<_R(_ArgTypes...)>::function(function&& __f)
1188{
1189 if (__f.__f_ == 0)
1190 __f_ = 0;
1191 else if (__f.__f_ == (__base*)&__f.__buf_)
1192 {
1193 __f_ = (__base*)&__buf_;
1194 __f.__f_->__clone(__f_);
1195 }
1196 else
1197 {
1198 __f_ = __f.__f_;
1199 __f.__f_ = 0;
1200 }
1201}
1202
1203template<class _R, class ..._ArgTypes>
1204template <class _F>
1205function<_R(_ArgTypes...)>::function(_F __f,
1206 typename enable_if<!is_integral<_F>::value>::type*)
1207 : __f_(0)
1208{
1209 if (__not_null(__f))
1210 {
1211 typedef __function::__func<_F, allocator<_F>, _R(_ArgTypes...)> _FF;
1212 if (sizeof(_FF) <= sizeof(__buf_))
1213 {
1214 __f_ = (__base*)&__buf_;
1215 ::new (__f_) _FF(_STD::move(__f));
1216 }
1217 else
1218 {
1219 typedef allocator<_FF> _A;
1220 _A __a;
1221 typedef __allocator_destructor<_A> _D;
1222 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1223 ::new (__hold.get()) _FF(_STD::move(__f), allocator<_F>(__a));
1224 __f_ = __hold.release();
1225 }
1226 }
1227}
1228
1229template<class _R, class ..._ArgTypes>
1230function<_R(_ArgTypes...)>&
1231function<_R(_ArgTypes...)>::operator=(const function& __f)
1232{
1233 function(__f).swap(*this);
1234 return *this;
1235}
1236
1237template<class _R, class ..._ArgTypes>
1238function<_R(_ArgTypes...)>&
1239function<_R(_ArgTypes...)>::operator=(function&& __f)
1240{
1241 if (__f_ == (__base*)&__buf_)
1242 __f_->destroy();
1243 else if (__f_)
1244 __f_->destroy_deallocate();
1245 __f_ = 0;
1246 if (__f.__f_ == 0)
1247 __f_ = 0;
1248 else if (__f.__f_ == (__base*)&__f.__buf_)
1249 {
1250 __f_ = (__base*)&__buf_;
1251 __f.__f_->__clone(__f_);
1252 }
1253 else
1254 {
1255 __f_ = __f.__f_;
1256 __f.__f_ = 0;
1257 }
1258}
1259
1260template<class _R, class ..._ArgTypes>
1261function<_R(_ArgTypes...)>&
1262function<_R(_ArgTypes...)>::operator=(nullptr_t)
1263{
1264 if (__f_ == (__base*)&__buf_)
1265 __f_->destroy();
1266 else if (__f_)
1267 __f_->destroy_deallocate();
1268 __f_ = 0;
1269}
1270
1271template<class _R, class ..._ArgTypes>
1272template <class _F>
1273typename enable_if
1274<
1275 !is_integral<typename decay<_F>::type>::value,
1276 function<_R(_ArgTypes...)>&
1277>::type
1278function<_R(_ArgTypes...)>::operator=(_F&& __f)
1279{
1280 function(_STD::forward<_F>(__f)).swap(*this);
1281 return *this;
1282}
1283
1284template<class _R, class ..._ArgTypes>
1285function<_R(_ArgTypes...)>::~function()
1286{
1287 if (__f_ == (__base*)&__buf_)
1288 __f_->destroy();
1289 else if (__f_)
1290 __f_->destroy_deallocate();
1291}
1292
1293template<class _R, class ..._ArgTypes>
1294void
1295function<_R(_ArgTypes...)>::swap(function& __f)
1296{
1297 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1298 {
1299 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1300 __base* __t = (__base*)&__tempbuf;
1301 __f_->__clone(__t);
1302 __f_->destroy();
1303 __f_ = 0;
1304 __f.__f_->__clone((__base*)&__buf_);
1305 __f.__f_->destroy();
1306 __f.__f_ = 0;
1307 __f_ = (__base*)&__buf_;
1308 __t->__clone((__base*)&__f.__buf_);
1309 __t->destroy();
1310 __f.__f_ = (__base*)&__f.__buf_;
1311 }
1312 else if (__f_ == (__base*)&__buf_)
1313 {
1314 __f_->__clone((__base*)&__f.__buf_);
1315 __f_->destroy();
1316 __f_ = __f.__f_;
1317 __f.__f_ = (__base*)&__f.__buf_;
1318 }
1319 else if (__f.__f_ == (__base*)&__f.__buf_)
1320 {
1321 __f.__f_->__clone((__base*)&__buf_);
1322 __f.__f_->destroy();
1323 __f.__f_ = __f_;
1324 __f_ = (__base*)&__buf_;
1325 }
1326 else
1327 _STD::swap(__f_, __f.__f_);
1328}
1329
1330template<class _R, class ..._ArgTypes>
1331_R
1332function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1333{
1334 if (__f_ == 0)
1335 throw bad_function_call();
1336 return (*__f_)(_STD::forward<_ArgTypes>(__arg)...);
1337}
1338
1339template<class _R, class ..._ArgTypes>
1340const std::type_info&
1341function<_R(_ArgTypes...)>::target_type() const
1342{
1343 if (__f_ == 0)
1344 return typeid(void);
1345 return __f_->target_type();
1346}
1347
1348template<class _R, class ..._ArgTypes>
1349template <typename _T>
1350_T*
1351function<_R(_ArgTypes...)>::target()
1352{
1353 if (__f_ == 0)
1354 return (_T*)0;
1355 return (_T*)__f_->target(typeid(_T));
1356}
1357
1358template<class _R, class ..._ArgTypes>
1359template <typename _T>
1360const _T*
1361function<_R(_ArgTypes...)>::target() const
1362{
1363 if (__f_ == 0)
1364 return (const _T*)0;
1365 return (const _T*)__f_->target(typeid(_T));
1366}
1367
1368template <class _R, class... _ArgTypes>
1369inline _LIBCPP_INLINE_VISIBILITY
1370bool
1371operator==(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return !__f;}
1372
1373template <class _R, class... _ArgTypes>
1374inline _LIBCPP_INLINE_VISIBILITY
1375bool
1376operator==(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return !__f;}
1377
1378template <class _R, class... _ArgTypes>
1379inline _LIBCPP_INLINE_VISIBILITY
1380bool
1381operator!=(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return (bool)__f;}
1382
1383template <class _R, class... _ArgTypes>
1384inline _LIBCPP_INLINE_VISIBILITY
1385bool
1386operator!=(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return (bool)__f;}
1387
1388template <class _R, class... _ArgTypes>
1389inline _LIBCPP_INLINE_VISIBILITY
1390void
1391swap(function<_R(_ArgTypes...)>& __x, function<_R(_ArgTypes...)>& __y)
1392{return __x.swap(__y);}
1393
1394template<class _Tp> struct __is_bind_expression : public false_type {};
1395template<class _Tp> struct is_bind_expression
1396 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1397
1398template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1399template<class _Tp> struct is_placeholder
1400 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1401
1402namespace placeholders
1403{
1404
1405template <int _N> struct __ph {};
1406
1407extern __ph<1> _1;
1408extern __ph<2> _2;
1409extern __ph<3> _3;
1410extern __ph<4> _4;
1411extern __ph<5> _5;
1412extern __ph<6> _6;
1413extern __ph<7> _7;
1414extern __ph<8> _8;
1415extern __ph<9> _9;
1416extern __ph<10> _10;
1417
1418} // placeholders
1419
1420template<int _N>
1421struct __is_placeholder<placeholders::__ph<_N> >
1422 : public integral_constant<int, _N> {};
1423
1424template <class _Tp, class _Uj>
1425inline _LIBCPP_INLINE_VISIBILITY
1426_Tp&
1427__mu(reference_wrapper<_Tp> __t, _Uj&)
1428{
1429 return __t.get();
1430}
1431
1432template <bool _IsBindExpr, class _Ti, class ..._Uj>
1433struct __mu_return1 {};
1434
1435template <class _Ti, class ..._Uj>
1436struct __mu_return1<true, _Ti, _Uj...>
1437{
1438 typedef typename result_of<_Ti(_Uj...)>::type type;
1439};
1440
1441
1442template <class _Ti, class ..._Uj, size_t ..._Indx>
1443inline _LIBCPP_INLINE_VISIBILITY
1444typename __mu_return1<true, _Ti, _Uj...>::type
1445__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1446{
1447 return __ti(_STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
1448}
1449
1450template <class _Ti, class ..._Uj>
1451inline _LIBCPP_INLINE_VISIBILITY
1452typename enable_if
1453<
1454 is_bind_expression<_Ti>::value,
1455 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1456>::type
1457__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1458{
1459 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1460 return __mu_expand(__ti, __uj, __indices());
1461}
1462
1463template <bool IsPh, class _Ti, class _Uj>
1464struct __mu_return2 {};
1465
1466template <class _Ti, class _Uj>
1467struct __mu_return2<true, _Ti, _Uj>
1468{
1469 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1470};
1471
1472template <class _Ti, class _Uj>
1473inline _LIBCPP_INLINE_VISIBILITY
1474typename enable_if
1475<
1476 0 < is_placeholder<_Ti>::value,
1477 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1478>::type
1479__mu(_Ti&, _Uj& __uj)
1480{
1481 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1482 // compiler bug workaround
1483 typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1484 return __t;
1485// return _STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
1486}
1487
1488template <class _Ti, class _Uj>
1489inline _LIBCPP_INLINE_VISIBILITY
1490typename enable_if
1491<
1492 !is_bind_expression<_Ti>::value &&
1493 is_placeholder<_Ti>::value == 0 &&
1494 !__is_reference_wrapper<_Ti>::value,
1495 _Ti&
1496>::type
1497__mu(_Ti& __ti, _Uj& __uj)
1498{
1499 return __ti;
1500}
1501
1502template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1503struct ____mu_return;
1504
1505template <class _Ti, class ..._Uj>
1506struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1507{
1508 typedef typename result_of<_Ti(_Uj...)>::type type;
1509};
1510
1511template <class _Ti, class _TupleUj>
1512struct ____mu_return<_Ti, false, true, _TupleUj>
1513{
1514 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1515 _TupleUj>::type&& type;
1516};
1517
1518template <class _Ti, class _TupleUj>
1519struct ____mu_return<_Ti, false, false, _TupleUj>
1520{
1521 typedef _Ti& type;
1522};
1523
1524template <class _Ti, class _TupleUj>
1525struct __mu_return
1526 : public ____mu_return<_Ti,
1527 is_bind_expression<_Ti>::value,
1528 0 < is_placeholder<_Ti>::value,
1529 _TupleUj>
1530{
1531};
1532
1533template <class _Ti, class _TupleUj>
1534struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1535{
1536 typedef _Ti& type;
1537};
1538
1539template <class _F, class _BoundArgs, class _TupleUj>
1540struct __bind_return;
1541
1542template <class _F, class ..._BoundArgs, class _TupleUj>
1543struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj>
1544{
1545 typedef typename __invoke_return
1546 <
1547 _F&,
1548 typename __mu_return
1549 <
1550 _BoundArgs,
1551 _TupleUj
1552 >::type...
1553 >::type type;
1554};
1555
1556template <class _F, class ..._BoundArgs, class _TupleUj>
1557struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj>
1558{
1559 typedef typename __invoke_return
1560 <
1561 _F&,
1562 typename __mu_return
1563 <
1564 const _BoundArgs,
1565 _TupleUj
1566 >::type...
1567 >::type type;
1568};
1569
1570template <class _F, class _BoundArgs, size_t ..._Indx, class _Args>
1571inline _LIBCPP_INLINE_VISIBILITY
1572typename __bind_return<_F, _BoundArgs, _Args>::type
1573__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
1574 _Args&& __args)
1575{
1576 return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
1577}
1578
1579template<class _F, class ..._BoundArgs>
1580class __bind
1581 : public __weak_result_type<_F>
1582{
1583 _F __f_;
1584 tuple<_BoundArgs...> __bound_args_;
1585
1586 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
1587public:
1588 __bind(__bind&& __b)
1589 : __f_(_STD::move(__b.__f_)),
1590 __bound_args_(_STD::move(__b.__bound_args_)) {}
1591
1592 template <class _G, class ..._BA>
1593 explicit __bind(_G&& __f, _BA&& ...__bound_args)
1594 : __f_(_STD::forward<_G>(__f)),
1595 __bound_args_(_STD::forward<_BA>(__bound_args)...) {}
1596
1597 template <class ..._Args>
1598 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1599 operator()(_Args&& ...__args)
1600 {
1601 // compiler bug workaround
1602 return __apply_functor(__f_, __bound_args_, __indices(),
1603 tuple<_Args&&...>(__args...));
1604 }
1605
1606 template <class ..._Args>
1607 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1608 operator()(_Args&& ...__args) const
1609 {
1610 return __apply_functor(__f_, __bound_args_, __indices(),
1611 tuple<_Args&&...>(__args...));
1612 }
1613};
1614
1615template<class _F, class ..._BoundArgs>
1616struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {};
1617
1618template<class _R, class _F, class ..._BoundArgs>
1619class __bind_r
1620 : public __bind<_F, _BoundArgs...>
1621{
1622 typedef __bind<_F, _BoundArgs...> base;
1623public:
1624 typedef _R result_type;
1625
1626 template <class _G, class ..._BA>
1627 explicit __bind_r(_G&& __f, _BA&& ...__bound_args)
1628 : base(_STD::forward<_G>(__f),
1629 _STD::forward<_BA>(__bound_args)...) {}
1630
1631 template <class ..._Args>
1632 result_type
1633 operator()(_Args&& ...__args)
1634 {
1635 return base::operator()(_STD::forward<_Args>(__args)...);
1636 }
1637
1638 template <class ..._Args>
1639 result_type
1640 operator()(_Args&& ...__args) const
1641 {
1642 return base::operator()(_STD::forward<_Args>(__args)...);
1643 }
1644};
1645
1646template<class _R, class _F, class ..._BoundArgs>
1647struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {};
1648
1649template<class _F, class ..._BoundArgs>
1650inline _LIBCPP_INLINE_VISIBILITY
1651__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1652bind(_F&& __f, _BoundArgs&&... __bound_args)
1653{
1654 typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1655 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1656}
1657
1658template<class _R, class _F, class ..._BoundArgs>
1659inline _LIBCPP_INLINE_VISIBILITY
1660__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1661bind(_F&& __f, _BoundArgs&&... __bound_args)
1662{
1663 typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1664 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1665}
1666
1667#endif // _LIBCPP_HAS_NO_VARIADICS
1668
1669template <>
1670struct hash<bool>
1671 : public unary_function<bool, size_t>
1672{
1673 size_t operator()(bool __v) const {return static_cast<size_t>(__v);}
1674};
1675
1676template <>
1677struct hash<char>
1678 : public unary_function<char, size_t>
1679{
1680 size_t operator()(char __v) const {return static_cast<size_t>(__v);}
1681};
1682
1683template <>
1684struct hash<signed char>
1685 : public unary_function<signed char, size_t>
1686{
1687 size_t operator()(signed char __v) const {return static_cast<size_t>(__v);}
1688};
1689
1690template <>
1691struct hash<unsigned char>
1692 : public unary_function<unsigned char, size_t>
1693{
1694 size_t operator()(unsigned char __v) const {return static_cast<size_t>(__v);}
1695};
1696
1697#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
1698
1699template <>
1700struct hash<char16_t>
1701 : public unary_function<char16_t, size_t>
1702{
1703 size_t operator()(char16_t __v) const {return static_cast<size_t>(__v);}
1704};
1705
1706template <>
1707struct hash<char32_t>
1708 : public unary_function<char32_t, size_t>
1709{
1710 size_t operator()(char32_t __v) const {return static_cast<size_t>(__v);}
1711};
1712
1713#endif
1714
1715template <>
1716struct hash<wchar_t>
1717 : public unary_function<wchar_t, size_t>
1718{
1719 size_t operator()(wchar_t __v) const {return static_cast<size_t>(__v);}
1720};
1721
1722template <>
1723struct hash<short>
1724 : public unary_function<short, size_t>
1725{
1726 size_t operator()(short __v) const {return static_cast<size_t>(__v);}
1727};
1728
1729template <>
1730struct hash<unsigned short>
1731 : public unary_function<unsigned short, size_t>
1732{
1733 size_t operator()(unsigned short __v) const {return static_cast<size_t>(__v);}
1734};
1735
1736template <>
1737struct hash<int>
1738 : public unary_function<int, size_t>
1739{
1740 size_t operator()(int __v) const {return static_cast<size_t>(__v);}
1741};
1742
1743template <>
1744struct hash<unsigned int>
1745 : public unary_function<unsigned int, size_t>
1746{
1747 size_t operator()(unsigned int __v) const {return static_cast<size_t>(__v);}
1748};
1749
1750template <>
1751struct hash<long>
1752 : public unary_function<long, size_t>
1753{
1754 size_t operator()(long __v) const {return static_cast<size_t>(__v);}
1755};
1756
1757template <>
1758struct hash<unsigned long>
1759 : public unary_function<unsigned long, size_t>
1760{
1761 size_t operator()(unsigned long __v) const {return static_cast<size_t>(__v);}
1762};
1763
1764template <>
1765struct hash<long long>
1766 : public unary_function<long long, size_t>
1767{
1768 size_t operator()(long long __v) const
1769 {
1770 size_t __r = 0;
1771 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1772 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1773 __r ^= __p[__i];
1774 return __r;
1775 }
1776};
1777
1778template <>
1779struct hash<unsigned long long>
1780 : public unary_function<unsigned long long, size_t>
1781{
1782 size_t operator()(unsigned long long __v) const
1783 {
1784 size_t __r = 0;
1785 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1786 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1787 __r ^= __p[__i];
1788 return __r;
1789 }
1790};
1791
1792template <>
1793struct hash<float>
1794 : public unary_function<float, size_t>
1795{
1796 size_t operator()(float __v) const
1797 {
1798 if (__v == 0)
1799 return 0;
1800 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1801 return *__p;
1802 }
1803};
1804
1805template <>
1806struct hash<double>
1807 : public unary_function<double, size_t>
1808{
1809 size_t operator()(double __v) const
1810 {
1811 if (__v == 0)
1812 return 0;
1813 size_t __r = 0;
1814 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1815 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1816 __r ^= __p[__i];
1817 return __r;
1818 }
1819};
1820
1821template <>
1822struct hash<long double>
1823 : public unary_function<long double, size_t>
1824{
1825 size_t operator()(long double __v) const
1826 {
1827 if (__v == 0)
1828 return 0;
1829 size_t __r = 0;
1830 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1831 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1832 __r ^= __p[__i];
1833 return __r;
1834 }
1835};
1836
1837template<class _Tp>
1838struct hash<_Tp*>
1839 : public unary_function<_Tp*, size_t>
1840{
1841 size_t operator()(_Tp* __v) const
1842 {
1843 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1844 return *__p;
1845 }
1846};
1847
1848_LIBCPP_END_NAMESPACE_STD
1849
1850#endif // _LIBCPP_FUNCTIONAL