blob: 55c38e5a978a043ae7b03b38c46bd97a1fee4fec [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
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;
Howard Hinnantd4444702010-08-11 17:04:31 +00001003#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004 virtual const void* target(const type_info&) const = 0;
1005 virtual const std::type_info& target_type() const = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +00001006#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007};
1008
1009template<class _FD, class _Alloc, class _FB> class __func;
1010
1011template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1012class __func<_F, _Alloc, _R(_ArgTypes...)>
1013 : public __base<_R(_ArgTypes...)>
1014{
1015 __compressed_pair<_F, _Alloc> __f_;
1016public:
1017 explicit __func(_F __f) : __f_(_STD::move(__f)) {}
1018 explicit __func(_F __f, _Alloc __a) : __f_(_STD::move(__f), _STD::move(__a)) {}
1019 virtual __base<_R(_ArgTypes...)>* __clone() const;
1020 virtual void __clone(__base<_R(_ArgTypes...)>*) const;
1021 virtual void destroy();
1022 virtual void destroy_deallocate();
1023 virtual _R operator()(_ArgTypes&& ... __arg);
Howard Hinnantd4444702010-08-11 17:04:31 +00001024#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025 virtual const void* target(const type_info&) const;
1026 virtual const std::type_info& target_type() const;
Howard Hinnantd4444702010-08-11 17:04:31 +00001027#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028};
1029
1030template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1031__base<_R(_ArgTypes...)>*
1032__func<_F, _Alloc, _R(_ArgTypes...)>::__clone() const
1033{
1034 typedef typename _Alloc::template rebind<__func>::other _A;
1035 _A __a(__f_.second());
1036 typedef __allocator_destructor<_A> _D;
1037 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
1038 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
1039 return __hold.release();
1040}
1041
1042template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1043void
1044__func<_F, _Alloc, _R(_ArgTypes...)>::__clone(__base<_R(_ArgTypes...)>* __p) const
1045{
1046 ::new (__p) __func(__f_.first(), __f_.second());
1047}
1048
1049template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1050void
1051__func<_F, _Alloc, _R(_ArgTypes...)>::destroy()
1052{
1053 __f_.~__compressed_pair<_F, _Alloc>();
1054}
1055
1056template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1057void
1058__func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate()
1059{
1060 typedef typename _Alloc::template rebind<__func>::other _A;
1061 _A __a(__f_.second());
1062 __f_.~__compressed_pair<_F, _Alloc>();
1063 __a.deallocate(this, 1);
1064}
1065
1066template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1067_R
1068__func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1069{
1070 return __invoke(__f_.first(), _STD::forward<_ArgTypes>(__arg)...);
1071}
1072
Howard Hinnantd4444702010-08-11 17:04:31 +00001073#ifndef _LIBCPP_NO_RTTI
1074
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1076const void*
1077__func<_F, _Alloc, _R(_ArgTypes...)>::target(const type_info& __ti) const
1078{
1079 if (__ti == typeid(_F))
1080 return &__f_.first();
1081 return (const void*)0;
1082}
1083
1084template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1085const std::type_info&
1086__func<_F, _Alloc, _R(_ArgTypes...)>::target_type() const
1087{
1088 return typeid(_F);
1089}
1090
Howard Hinnantd4444702010-08-11 17:04:31 +00001091#endif
1092
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093} // __function
1094
1095template<class _R, class ..._ArgTypes>
1096class function<_R(_ArgTypes...)>
1097 : public __function::__maybe_derive_from_unary_function<_R(_ArgTypes...)>,
1098 public __function::__maybe_derive_from_binary_function<_R(_ArgTypes...)>
1099{
1100 typedef __function::__base<_R(_ArgTypes...)> __base;
1101 aligned_storage<3*sizeof(void*)>::type __buf_;
1102 __base* __f_;
1103
1104 template <class _F>
1105 static bool __not_null(const _F&) {return true;}
1106 template <class _R2, class ..._A>
1107 static bool __not_null(_R2 (*__p)(_A...)) {return __p;}
1108 template <class _R2, class _C, class ..._A>
1109 static bool __not_null(_R2 (_C::*__p)(_A...)) {return __p;}
1110 template <class _R2, class _C, class ..._A>
1111 static bool __not_null(_R2 (_C::*__p)(_A...) const) {return __p;}
1112 template <class _R2, class _C, class ..._A>
1113 static bool __not_null(_R2 (_C::*__p)(_A...) volatile) {return __p;}
1114 template <class _R2, class _C, class ..._A>
1115 static bool __not_null(_R2 (_C::*__p)(_A...) const volatile) {return __p;}
1116 template <class _R2, class ..._A>
1117 static bool __not_null(const function<_R(_A...)>& __p) {return __p;}
1118public:
1119 typedef _R result_type;
1120
1121 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnante00e0302010-08-19 19:20:10 +00001122 function() : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123 function(nullptr_t) : __f_(0) {}
1124 function(const function&);
1125#ifdef _LIBCPP_MOVE
1126 function(function&&);
1127#endif
1128 template<class _F>
1129 function(_F,
1130 typename enable_if<!is_integral<_F>::value>::type* = 0);
1131
1132// template<class _Alloc>
1133// function(allocator_arg_t, const _Alloc&);
1134// template<Allocator Alloc>
1135// function(allocator_arg_t, const Alloc&, nullptr_t);
1136// template<Allocator Alloc>
1137// function(allocator_arg_t, const Alloc&, const function&);
1138// template<Allocator Alloc>
1139// function(allocator_arg_t, const Alloc&, function&&);
1140// template<class F, Allocator Alloc>
1141// function(allocator_arg_t, const Alloc&, F);
1142
1143 function& operator=(const function&);
1144 function& operator=(function&&);
1145 function& operator=(nullptr_t);
1146 template<class _F>
1147 typename enable_if
1148 <
1149 !is_integral<typename decay<_F>::type>::value,
1150 function&
1151 >::type
1152 operator=(_F&&);
1153
1154 ~function();
1155
1156 // 20.7.16.2.2, function modifiers:
1157 void swap(function&);
1158// template<class _F, class _Alloc>
1159// void assign(_F, const _Alloc&);
1160
1161 // 20.7.16.2.3, function capacity:
1162 /*explicit*/ operator bool() const {return __f_;}
1163
1164private:
1165 // deleted overloads close possible hole in the type system
1166 template<class _R2, class... _ArgTypes2>
1167 bool operator==(const function<_R2(_ArgTypes2...)>&);// = delete;
1168 template<class _R2, class... _ArgTypes2>
1169 bool operator!=(const function<_R2(_ArgTypes2...)>&);// = delete;
1170public:
1171 // 20.7.16.2.4, function invocation:
1172 _R operator()(_ArgTypes...) const;
1173
Howard Hinnantd4444702010-08-11 17:04:31 +00001174#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175 // 20.7.16.2.5, function target access:
1176 const std::type_info& target_type() const;
1177 template <typename _T> _T* target();
1178 template <typename _T> const _T* target() const;
Howard Hinnantd4444702010-08-11 17:04:31 +00001179#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180};
1181
1182template<class _R, class ..._ArgTypes>
1183function<_R(_ArgTypes...)>::function(const function& __f)
1184{
1185 if (__f.__f_ == 0)
1186 __f_ = 0;
1187 else if (__f.__f_ == (const __base*)&__f.__buf_)
1188 {
1189 __f_ = (__base*)&__buf_;
1190 __f.__f_->__clone(__f_);
1191 }
1192 else
1193 __f_ = __f.__f_->__clone();
1194}
1195
1196template<class _R, class ..._ArgTypes>
1197function<_R(_ArgTypes...)>::function(function&& __f)
1198{
1199 if (__f.__f_ == 0)
1200 __f_ = 0;
1201 else if (__f.__f_ == (__base*)&__f.__buf_)
1202 {
1203 __f_ = (__base*)&__buf_;
1204 __f.__f_->__clone(__f_);
1205 }
1206 else
1207 {
1208 __f_ = __f.__f_;
1209 __f.__f_ = 0;
1210 }
1211}
1212
1213template<class _R, class ..._ArgTypes>
1214template <class _F>
1215function<_R(_ArgTypes...)>::function(_F __f,
1216 typename enable_if<!is_integral<_F>::value>::type*)
1217 : __f_(0)
1218{
1219 if (__not_null(__f))
1220 {
1221 typedef __function::__func<_F, allocator<_F>, _R(_ArgTypes...)> _FF;
1222 if (sizeof(_FF) <= sizeof(__buf_))
1223 {
1224 __f_ = (__base*)&__buf_;
1225 ::new (__f_) _FF(_STD::move(__f));
1226 }
1227 else
1228 {
1229 typedef allocator<_FF> _A;
1230 _A __a;
1231 typedef __allocator_destructor<_A> _D;
1232 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1233 ::new (__hold.get()) _FF(_STD::move(__f), allocator<_F>(__a));
1234 __f_ = __hold.release();
1235 }
1236 }
1237}
1238
1239template<class _R, class ..._ArgTypes>
1240function<_R(_ArgTypes...)>&
1241function<_R(_ArgTypes...)>::operator=(const function& __f)
1242{
1243 function(__f).swap(*this);
1244 return *this;
1245}
1246
1247template<class _R, class ..._ArgTypes>
1248function<_R(_ArgTypes...)>&
1249function<_R(_ArgTypes...)>::operator=(function&& __f)
1250{
1251 if (__f_ == (__base*)&__buf_)
1252 __f_->destroy();
1253 else if (__f_)
1254 __f_->destroy_deallocate();
1255 __f_ = 0;
1256 if (__f.__f_ == 0)
1257 __f_ = 0;
1258 else if (__f.__f_ == (__base*)&__f.__buf_)
1259 {
1260 __f_ = (__base*)&__buf_;
1261 __f.__f_->__clone(__f_);
1262 }
1263 else
1264 {
1265 __f_ = __f.__f_;
1266 __f.__f_ = 0;
1267 }
1268}
1269
1270template<class _R, class ..._ArgTypes>
1271function<_R(_ArgTypes...)>&
1272function<_R(_ArgTypes...)>::operator=(nullptr_t)
1273{
1274 if (__f_ == (__base*)&__buf_)
1275 __f_->destroy();
1276 else if (__f_)
1277 __f_->destroy_deallocate();
1278 __f_ = 0;
1279}
1280
1281template<class _R, class ..._ArgTypes>
1282template <class _F>
1283typename enable_if
1284<
1285 !is_integral<typename decay<_F>::type>::value,
1286 function<_R(_ArgTypes...)>&
1287>::type
1288function<_R(_ArgTypes...)>::operator=(_F&& __f)
1289{
1290 function(_STD::forward<_F>(__f)).swap(*this);
1291 return *this;
1292}
1293
1294template<class _R, class ..._ArgTypes>
1295function<_R(_ArgTypes...)>::~function()
1296{
1297 if (__f_ == (__base*)&__buf_)
1298 __f_->destroy();
1299 else if (__f_)
1300 __f_->destroy_deallocate();
1301}
1302
1303template<class _R, class ..._ArgTypes>
1304void
1305function<_R(_ArgTypes...)>::swap(function& __f)
1306{
1307 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1308 {
1309 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1310 __base* __t = (__base*)&__tempbuf;
1311 __f_->__clone(__t);
1312 __f_->destroy();
1313 __f_ = 0;
1314 __f.__f_->__clone((__base*)&__buf_);
1315 __f.__f_->destroy();
1316 __f.__f_ = 0;
1317 __f_ = (__base*)&__buf_;
1318 __t->__clone((__base*)&__f.__buf_);
1319 __t->destroy();
1320 __f.__f_ = (__base*)&__f.__buf_;
1321 }
1322 else if (__f_ == (__base*)&__buf_)
1323 {
1324 __f_->__clone((__base*)&__f.__buf_);
1325 __f_->destroy();
1326 __f_ = __f.__f_;
1327 __f.__f_ = (__base*)&__f.__buf_;
1328 }
1329 else if (__f.__f_ == (__base*)&__f.__buf_)
1330 {
1331 __f.__f_->__clone((__base*)&__buf_);
1332 __f.__f_->destroy();
1333 __f.__f_ = __f_;
1334 __f_ = (__base*)&__buf_;
1335 }
1336 else
1337 _STD::swap(__f_, __f.__f_);
1338}
1339
1340template<class _R, class ..._ArgTypes>
1341_R
1342function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1343{
Howard Hinnantd4444702010-08-11 17:04:31 +00001344#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345 if (__f_ == 0)
1346 throw bad_function_call();
Howard Hinnantd4444702010-08-11 17:04:31 +00001347#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 return (*__f_)(_STD::forward<_ArgTypes>(__arg)...);
1349}
1350
Howard Hinnantd4444702010-08-11 17:04:31 +00001351#ifndef _LIBCPP_NO_RTTI
1352
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001353template<class _R, class ..._ArgTypes>
1354const std::type_info&
1355function<_R(_ArgTypes...)>::target_type() const
1356{
1357 if (__f_ == 0)
1358 return typeid(void);
1359 return __f_->target_type();
1360}
1361
1362template<class _R, class ..._ArgTypes>
1363template <typename _T>
1364_T*
1365function<_R(_ArgTypes...)>::target()
1366{
1367 if (__f_ == 0)
1368 return (_T*)0;
1369 return (_T*)__f_->target(typeid(_T));
1370}
1371
1372template<class _R, class ..._ArgTypes>
1373template <typename _T>
1374const _T*
1375function<_R(_ArgTypes...)>::target() const
1376{
1377 if (__f_ == 0)
1378 return (const _T*)0;
1379 return (const _T*)__f_->target(typeid(_T));
1380}
1381
Howard Hinnantd4444702010-08-11 17:04:31 +00001382#endif
1383
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001384template <class _R, class... _ArgTypes>
1385inline _LIBCPP_INLINE_VISIBILITY
1386bool
1387operator==(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return !__f;}
1388
1389template <class _R, class... _ArgTypes>
1390inline _LIBCPP_INLINE_VISIBILITY
1391bool
1392operator==(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return !__f;}
1393
1394template <class _R, class... _ArgTypes>
1395inline _LIBCPP_INLINE_VISIBILITY
1396bool
1397operator!=(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return (bool)__f;}
1398
1399template <class _R, class... _ArgTypes>
1400inline _LIBCPP_INLINE_VISIBILITY
1401bool
1402operator!=(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return (bool)__f;}
1403
1404template <class _R, class... _ArgTypes>
1405inline _LIBCPP_INLINE_VISIBILITY
1406void
1407swap(function<_R(_ArgTypes...)>& __x, function<_R(_ArgTypes...)>& __y)
1408{return __x.swap(__y);}
1409
1410template<class _Tp> struct __is_bind_expression : public false_type {};
1411template<class _Tp> struct is_bind_expression
1412 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1413
1414template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1415template<class _Tp> struct is_placeholder
1416 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1417
1418namespace placeholders
1419{
1420
1421template <int _N> struct __ph {};
1422
1423extern __ph<1> _1;
1424extern __ph<2> _2;
1425extern __ph<3> _3;
1426extern __ph<4> _4;
1427extern __ph<5> _5;
1428extern __ph<6> _6;
1429extern __ph<7> _7;
1430extern __ph<8> _8;
1431extern __ph<9> _9;
1432extern __ph<10> _10;
1433
1434} // placeholders
1435
1436template<int _N>
1437struct __is_placeholder<placeholders::__ph<_N> >
1438 : public integral_constant<int, _N> {};
1439
1440template <class _Tp, class _Uj>
1441inline _LIBCPP_INLINE_VISIBILITY
1442_Tp&
1443__mu(reference_wrapper<_Tp> __t, _Uj&)
1444{
1445 return __t.get();
1446}
1447
1448template <bool _IsBindExpr, class _Ti, class ..._Uj>
1449struct __mu_return1 {};
1450
1451template <class _Ti, class ..._Uj>
1452struct __mu_return1<true, _Ti, _Uj...>
1453{
1454 typedef typename result_of<_Ti(_Uj...)>::type type;
1455};
1456
1457
1458template <class _Ti, class ..._Uj, size_t ..._Indx>
1459inline _LIBCPP_INLINE_VISIBILITY
1460typename __mu_return1<true, _Ti, _Uj...>::type
1461__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1462{
1463 return __ti(_STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
1464}
1465
1466template <class _Ti, class ..._Uj>
1467inline _LIBCPP_INLINE_VISIBILITY
1468typename enable_if
1469<
1470 is_bind_expression<_Ti>::value,
1471 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1472>::type
1473__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1474{
1475 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1476 return __mu_expand(__ti, __uj, __indices());
1477}
1478
1479template <bool IsPh, class _Ti, class _Uj>
1480struct __mu_return2 {};
1481
1482template <class _Ti, class _Uj>
1483struct __mu_return2<true, _Ti, _Uj>
1484{
1485 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1486};
1487
1488template <class _Ti, class _Uj>
1489inline _LIBCPP_INLINE_VISIBILITY
1490typename enable_if
1491<
1492 0 < is_placeholder<_Ti>::value,
1493 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1494>::type
1495__mu(_Ti&, _Uj& __uj)
1496{
1497 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1498 // compiler bug workaround
1499 typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1500 return __t;
1501// return _STD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
1502}
1503
1504template <class _Ti, class _Uj>
1505inline _LIBCPP_INLINE_VISIBILITY
1506typename enable_if
1507<
1508 !is_bind_expression<_Ti>::value &&
1509 is_placeholder<_Ti>::value == 0 &&
1510 !__is_reference_wrapper<_Ti>::value,
1511 _Ti&
1512>::type
1513__mu(_Ti& __ti, _Uj& __uj)
1514{
1515 return __ti;
1516}
1517
1518template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1519struct ____mu_return;
1520
1521template <class _Ti, class ..._Uj>
1522struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1523{
1524 typedef typename result_of<_Ti(_Uj...)>::type type;
1525};
1526
1527template <class _Ti, class _TupleUj>
1528struct ____mu_return<_Ti, false, true, _TupleUj>
1529{
1530 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1531 _TupleUj>::type&& type;
1532};
1533
1534template <class _Ti, class _TupleUj>
1535struct ____mu_return<_Ti, false, false, _TupleUj>
1536{
1537 typedef _Ti& type;
1538};
1539
1540template <class _Ti, class _TupleUj>
1541struct __mu_return
1542 : public ____mu_return<_Ti,
1543 is_bind_expression<_Ti>::value,
1544 0 < is_placeholder<_Ti>::value,
1545 _TupleUj>
1546{
1547};
1548
1549template <class _Ti, class _TupleUj>
1550struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1551{
1552 typedef _Ti& type;
1553};
1554
1555template <class _F, class _BoundArgs, class _TupleUj>
1556struct __bind_return;
1557
1558template <class _F, class ..._BoundArgs, class _TupleUj>
1559struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj>
1560{
1561 typedef typename __invoke_return
1562 <
1563 _F&,
1564 typename __mu_return
1565 <
1566 _BoundArgs,
1567 _TupleUj
1568 >::type...
1569 >::type type;
1570};
1571
1572template <class _F, class ..._BoundArgs, class _TupleUj>
1573struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj>
1574{
1575 typedef typename __invoke_return
1576 <
1577 _F&,
1578 typename __mu_return
1579 <
1580 const _BoundArgs,
1581 _TupleUj
1582 >::type...
1583 >::type type;
1584};
1585
1586template <class _F, class _BoundArgs, size_t ..._Indx, class _Args>
1587inline _LIBCPP_INLINE_VISIBILITY
1588typename __bind_return<_F, _BoundArgs, _Args>::type
1589__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
1590 _Args&& __args)
1591{
1592 return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
1593}
1594
1595template<class _F, class ..._BoundArgs>
1596class __bind
1597 : public __weak_result_type<_F>
1598{
1599 _F __f_;
1600 tuple<_BoundArgs...> __bound_args_;
1601
1602 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
1603public:
1604 __bind(__bind&& __b)
1605 : __f_(_STD::move(__b.__f_)),
1606 __bound_args_(_STD::move(__b.__bound_args_)) {}
1607
1608 template <class _G, class ..._BA>
1609 explicit __bind(_G&& __f, _BA&& ...__bound_args)
1610 : __f_(_STD::forward<_G>(__f)),
1611 __bound_args_(_STD::forward<_BA>(__bound_args)...) {}
1612
1613 template <class ..._Args>
1614 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1615 operator()(_Args&& ...__args)
1616 {
1617 // compiler bug workaround
1618 return __apply_functor(__f_, __bound_args_, __indices(),
1619 tuple<_Args&&...>(__args...));
1620 }
1621
1622 template <class ..._Args>
1623 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
1624 operator()(_Args&& ...__args) const
1625 {
1626 return __apply_functor(__f_, __bound_args_, __indices(),
1627 tuple<_Args&&...>(__args...));
1628 }
1629};
1630
1631template<class _F, class ..._BoundArgs>
1632struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {};
1633
1634template<class _R, class _F, class ..._BoundArgs>
1635class __bind_r
1636 : public __bind<_F, _BoundArgs...>
1637{
1638 typedef __bind<_F, _BoundArgs...> base;
1639public:
1640 typedef _R result_type;
1641
1642 template <class _G, class ..._BA>
1643 explicit __bind_r(_G&& __f, _BA&& ...__bound_args)
1644 : base(_STD::forward<_G>(__f),
1645 _STD::forward<_BA>(__bound_args)...) {}
1646
1647 template <class ..._Args>
1648 result_type
1649 operator()(_Args&& ...__args)
1650 {
1651 return base::operator()(_STD::forward<_Args>(__args)...);
1652 }
1653
1654 template <class ..._Args>
1655 result_type
1656 operator()(_Args&& ...__args) const
1657 {
1658 return base::operator()(_STD::forward<_Args>(__args)...);
1659 }
1660};
1661
1662template<class _R, class _F, class ..._BoundArgs>
1663struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {};
1664
1665template<class _F, class ..._BoundArgs>
1666inline _LIBCPP_INLINE_VISIBILITY
1667__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1668bind(_F&& __f, _BoundArgs&&... __bound_args)
1669{
1670 typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1671 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1672}
1673
1674template<class _R, class _F, class ..._BoundArgs>
1675inline _LIBCPP_INLINE_VISIBILITY
1676__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...>
1677bind(_F&& __f, _BoundArgs&&... __bound_args)
1678{
1679 typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
1680 return type(_STD::forward<_F>(__f), _STD::forward<_BoundArgs>(__bound_args)...);
1681}
1682
1683#endif // _LIBCPP_HAS_NO_VARIADICS
1684
1685template <>
1686struct hash<bool>
1687 : public unary_function<bool, size_t>
1688{
1689 size_t operator()(bool __v) const {return static_cast<size_t>(__v);}
1690};
1691
1692template <>
1693struct hash<char>
1694 : public unary_function<char, size_t>
1695{
1696 size_t operator()(char __v) const {return static_cast<size_t>(__v);}
1697};
1698
1699template <>
1700struct hash<signed char>
1701 : public unary_function<signed char, size_t>
1702{
1703 size_t operator()(signed char __v) const {return static_cast<size_t>(__v);}
1704};
1705
1706template <>
1707struct hash<unsigned char>
1708 : public unary_function<unsigned char, size_t>
1709{
1710 size_t operator()(unsigned char __v) const {return static_cast<size_t>(__v);}
1711};
1712
1713#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
1714
1715template <>
1716struct hash<char16_t>
1717 : public unary_function<char16_t, size_t>
1718{
1719 size_t operator()(char16_t __v) const {return static_cast<size_t>(__v);}
1720};
1721
1722template <>
1723struct hash<char32_t>
1724 : public unary_function<char32_t, size_t>
1725{
1726 size_t operator()(char32_t __v) const {return static_cast<size_t>(__v);}
1727};
1728
1729#endif
1730
1731template <>
1732struct hash<wchar_t>
1733 : public unary_function<wchar_t, size_t>
1734{
1735 size_t operator()(wchar_t __v) const {return static_cast<size_t>(__v);}
1736};
1737
1738template <>
1739struct hash<short>
1740 : public unary_function<short, size_t>
1741{
1742 size_t operator()(short __v) const {return static_cast<size_t>(__v);}
1743};
1744
1745template <>
1746struct hash<unsigned short>
1747 : public unary_function<unsigned short, size_t>
1748{
1749 size_t operator()(unsigned short __v) const {return static_cast<size_t>(__v);}
1750};
1751
1752template <>
1753struct hash<int>
1754 : public unary_function<int, size_t>
1755{
1756 size_t operator()(int __v) const {return static_cast<size_t>(__v);}
1757};
1758
1759template <>
1760struct hash<unsigned int>
1761 : public unary_function<unsigned int, size_t>
1762{
1763 size_t operator()(unsigned int __v) const {return static_cast<size_t>(__v);}
1764};
1765
1766template <>
1767struct hash<long>
1768 : public unary_function<long, size_t>
1769{
1770 size_t operator()(long __v) const {return static_cast<size_t>(__v);}
1771};
1772
1773template <>
1774struct hash<unsigned long>
1775 : public unary_function<unsigned long, size_t>
1776{
1777 size_t operator()(unsigned long __v) const {return static_cast<size_t>(__v);}
1778};
1779
1780template <>
1781struct hash<long long>
1782 : public unary_function<long long, size_t>
1783{
1784 size_t operator()(long long __v) const
1785 {
1786 size_t __r = 0;
1787 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1788 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1789 __r ^= __p[__i];
1790 return __r;
1791 }
1792};
1793
1794template <>
1795struct hash<unsigned long long>
1796 : public unary_function<unsigned long long, size_t>
1797{
1798 size_t operator()(unsigned long long __v) const
1799 {
1800 size_t __r = 0;
1801 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1802 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1803 __r ^= __p[__i];
1804 return __r;
1805 }
1806};
1807
1808template <>
1809struct hash<float>
1810 : public unary_function<float, size_t>
1811{
1812 size_t operator()(float __v) const
1813 {
1814 if (__v == 0)
1815 return 0;
1816 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1817 return *__p;
1818 }
1819};
1820
1821template <>
1822struct hash<double>
1823 : public unary_function<double, size_t>
1824{
1825 size_t operator()(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 <>
1838struct hash<long double>
1839 : public unary_function<long double, size_t>
1840{
1841 size_t operator()(long double __v) const
1842 {
1843 if (__v == 0)
1844 return 0;
1845 size_t __r = 0;
1846 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
1847 for (unsigned __i = 0; __i < sizeof(argument_type)/sizeof(size_t); ++__i)
1848 __r ^= __p[__i];
1849 return __r;
1850 }
1851};
1852
Howard Hinnant21aefc32010-06-03 16:42:57 +00001853// struct hash<T*> in <memory>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854
1855_LIBCPP_END_NAMESPACE_STD
1856
1857#endif // _LIBCPP_FUNCTIONAL