Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------ functional ----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | template <class Arg, class Result> |
| 21 | struct unary_function |
| 22 | { |
| 23 | typedef Arg argument_type; |
| 24 | typedef Result result_type; |
| 25 | }; |
| 26 | |
| 27 | template <class Arg1, class Arg2, class Result> |
| 28 | struct binary_function |
| 29 | { |
| 30 | typedef Arg1 first_argument_type; |
| 31 | typedef Arg2 second_argument_type; |
| 32 | typedef Result result_type; |
| 33 | }; |
| 34 | |
| 35 | template <ObjectType T> |
| 36 | class 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 | { |
| 40 | public: |
| 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 | |
| 64 | template <ObjectType T> reference_wrapper<T> ref(T& t); |
| 65 | template <ObjectType T> void ref(const T&& t) = delete; // LWG 688 |
| 66 | template <ObjectType T> reference_wrapper<T> ref(reference_wrapper<T>t); |
| 67 | |
| 68 | template <ObjectType T> reference_wrapper<const T> cref(const T& t); |
| 69 | template <ObjectType T> void cref(const T&& t) = delete; // LWG 688 |
| 70 | template <ObjectType T> reference_wrapper<const T> cref(reference_wrapper<T> t); |
| 71 | |
| 72 | template <class T> |
| 73 | struct plus : binary_function<T, T, T> |
| 74 | { |
| 75 | T operator()(const T& x, const T& y) const; |
| 76 | }; |
| 77 | |
| 78 | template <class T> |
| 79 | struct minus : binary_function<T, T, T> |
| 80 | { |
| 81 | T operator()(const T& x, const T& y) const; |
| 82 | }; |
| 83 | |
| 84 | template <class T> |
| 85 | struct multiplies : binary_function<T, T, T> |
| 86 | { |
| 87 | T operator()(const T& x, const T& y) const; |
| 88 | }; |
| 89 | |
| 90 | template <class T> |
| 91 | struct divides : binary_function<T, T, T> |
| 92 | { |
| 93 | T operator()(const T& x, const T& y) const; |
| 94 | }; |
| 95 | |
| 96 | template <class T> |
| 97 | struct modulus : binary_function<T, T, T> |
| 98 | { |
| 99 | T operator()(const T& x, const T& y) const; |
| 100 | }; |
| 101 | |
| 102 | template <class T> |
| 103 | struct negate : unary_function<T, T> |
| 104 | { |
| 105 | T operator()(const T& x) const; |
| 106 | }; |
| 107 | |
| 108 | template <class T> |
| 109 | struct equal_to : binary_function<T, T, bool> |
| 110 | { |
| 111 | bool operator()(const T& x, const T& y) const; |
| 112 | }; |
| 113 | |
| 114 | template <class T> |
| 115 | struct not_equal_to : binary_function<T, T, bool> |
| 116 | { |
| 117 | bool operator()(const T& x, const T& y) const; |
| 118 | }; |
| 119 | |
| 120 | template <class T> |
| 121 | struct greater : binary_function<T, T, bool> |
| 122 | { |
| 123 | bool operator()(const T& x, const T& y) const; |
| 124 | }; |
| 125 | |
| 126 | template <class T> |
| 127 | struct less : binary_function<T, T, bool> |
| 128 | { |
| 129 | bool operator()(const T& x, const T& y) const; |
| 130 | }; |
| 131 | |
| 132 | template <class T> |
| 133 | struct greater_equal : binary_function<T, T, bool> |
| 134 | { |
| 135 | bool operator()(const T& x, const T& y) const; |
| 136 | }; |
| 137 | |
| 138 | template <class T> |
| 139 | struct less_equal : binary_function<T, T, bool> |
| 140 | { |
| 141 | bool operator()(const T& x, const T& y) const; |
| 142 | }; |
| 143 | |
| 144 | template <class T> |
| 145 | struct logical_and : binary_function<T, T, bool> |
| 146 | { |
| 147 | bool operator()(const T& x, const T& y) const; |
| 148 | }; |
| 149 | |
| 150 | template <class T> |
| 151 | struct logical_or : binary_function<T, T, bool> |
| 152 | { |
| 153 | bool operator()(const T& x, const T& y) const; |
| 154 | }; |
| 155 | |
| 156 | template <class T> |
| 157 | struct logical_not : unary_function<T, bool> |
| 158 | { |
| 159 | bool operator()(const T& x) const; |
| 160 | }; |
| 161 | |
| 162 | template <class Predicate> |
| 163 | class unary_negate |
| 164 | : public unary_function<typename Predicate::argument_type, bool> |
| 165 | { |
| 166 | public: |
| 167 | explicit unary_negate(const Predicate& pred); |
| 168 | bool operator()(const typename Predicate::argument_type& x) const; |
| 169 | }; |
| 170 | |
| 171 | template <class Predicate> unary_negate<Predicate> not1(const Predicate& pred); |
| 172 | |
| 173 | template <class Predicate> |
| 174 | class binary_negate |
| 175 | : public binary_function<typename Predicate::first_argument_type, |
| 176 | typename Predicate::second_argument_type, |
| 177 | bool> |
| 178 | { |
| 179 | public: |
| 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 | |
| 185 | template <class Predicate> binary_negate<Predicate> not2(const Predicate& pred); |
| 186 | |
| 187 | template<class T> struct is_bind_expression; |
| 188 | template<class T> struct is_placeholder; |
| 189 | |
| 190 | template<CopyConstructible Fn, CopyConstructible... Types> |
| 191 | unspecified bind(Fn, Types...); |
| 192 | template<Returnable R, CopyConstructible Fn, CopyConstructible... Types> |
| 193 | unspecified bind(Fn, Types...); |
| 194 | |
| 195 | namespace 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 | |
| 205 | template <class Operation> |
| 206 | class binder1st |
| 207 | : public unary_function<typename Operation::second_argument_type, |
| 208 | typename Operation::result_type> |
| 209 | { |
| 210 | protected: |
| 211 | Operation op; |
| 212 | typename Operation::first_argument_type value; |
| 213 | public: |
| 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 | |
| 219 | template <class Operation, class T> |
| 220 | binder1st<Operation> bind1st(const Operation& op, const T& x); |
| 221 | |
| 222 | template <class Operation> |
| 223 | class binder2nd |
| 224 | : public unary_function<typename Operation::first_argument_type, |
| 225 | typename Operation::result_type> |
| 226 | { |
| 227 | protected: |
| 228 | Operation op; |
| 229 | typename Operation::second_argument_type value; |
| 230 | public: |
| 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 | |
| 236 | template <class Operation, class T> |
| 237 | binder2nd<Operation> bind2nd(const Operation& op, const T& x); |
| 238 | |
| 239 | template <class Arg, class Result> |
| 240 | class pointer_to_unary_function : public unary_function<Arg, Result> |
| 241 | { |
| 242 | public: |
| 243 | explicit pointer_to_unary_function(Result (*f)(Arg)); |
| 244 | Result operator()(Arg x) const; |
| 245 | }; |
| 246 | |
| 247 | template <class Arg, class Result> |
| 248 | pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg)); |
| 249 | |
| 250 | template <class Arg1, class Arg2, class Result> |
| 251 | class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result> |
| 252 | { |
| 253 | public: |
| 254 | explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2)); |
| 255 | Result operator()(Arg1 x, Arg2 y) const; |
| 256 | }; |
| 257 | |
| 258 | template <class Arg1, class Arg2, class Result> |
| 259 | pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2)); |
| 260 | |
| 261 | template<class S, class T> |
| 262 | class mem_fun_t : public unary_function<T*, S> |
| 263 | { |
| 264 | public: |
| 265 | explicit mem_fun_t(S (T::*p)()); |
| 266 | S operator()(T* p) const; |
| 267 | }; |
| 268 | |
| 269 | template<class S, class T, class A> |
| 270 | class mem_fun1_t : public binary_function<T*, A, S> |
| 271 | { |
| 272 | public: |
| 273 | explicit mem_fun1_t(S (T::*p)(A)); |
| 274 | S operator()(T* p, A x) const; |
| 275 | }; |
| 276 | |
| 277 | template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); |
| 278 | template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A)); |
| 279 | |
| 280 | template<class S, class T> |
| 281 | class mem_fun_ref_t : public unary_function<T, S> |
| 282 | { |
| 283 | public: |
| 284 | explicit mem_fun_ref_t(S (T::*p)()); |
| 285 | S operator()(T& p) const; |
| 286 | }; |
| 287 | |
| 288 | template<class S, class T, class A> |
| 289 | class mem_fun1_ref_t : public binary_function<T, A, S> |
| 290 | { |
| 291 | public: |
| 292 | explicit mem_fun1_ref_t(S (T::*p)(A)); |
| 293 | S operator()(T& p, A x) const; |
| 294 | }; |
| 295 | |
| 296 | template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); |
| 297 | template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A)); |
| 298 | |
| 299 | template <class S, class T> |
| 300 | class const_mem_fun_t : public unary_function<const T*, S> |
| 301 | { |
| 302 | public: |
| 303 | explicit const_mem_fun_t(S (T::*p)() const); |
| 304 | S operator()(const T* p) const; |
| 305 | }; |
| 306 | |
| 307 | template <class S, class T, class A> |
| 308 | class const_mem_fun1_t : public binary_function<const T*, A, S> |
| 309 | { |
| 310 | public: |
| 311 | explicit const_mem_fun1_t(S (T::*p)(A) const); |
| 312 | S operator()(const T* p, A x) const; |
| 313 | }; |
| 314 | |
| 315 | template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); |
| 316 | template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const); |
| 317 | |
| 318 | template <class S, class T> |
| 319 | class const_mem_fun_ref_t : public unary_function<T, S> |
| 320 | { |
| 321 | public: |
| 322 | explicit const_mem_fun_ref_t(S (T::*p)() const); |
| 323 | S operator()(const T& p) const; |
| 324 | }; |
| 325 | |
| 326 | template <class S, class T, class A> |
| 327 | class const_mem_fun1_ref_t : public binary_function<T, A, S> |
| 328 | { |
| 329 | public: |
| 330 | explicit const_mem_fun1_ref_t(S (T::*p)(A) const); |
| 331 | S operator()(const T& p, A x) const; |
| 332 | }; |
| 333 | |
| 334 | template <class S, class T> const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const); |
| 335 | template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const); |
| 336 | |
| 337 | class bad_function_call |
| 338 | : public exception |
| 339 | { |
| 340 | }; |
| 341 | |
| 342 | template<Returnable R, class T> unspecified mem_fn(R T::* pm); |
| 343 | template<Returnable R, class T, CopyConstructible... Args> |
| 344 | unspecified mem_fn(R (T::* pm)(Args...)); |
| 345 | template<Returnable R, class T, CopyConstructible... Args> |
| 346 | unspecified mem_fn(R (T::* pm)(Args...) const); |
| 347 | template<Returnable R, class T, CopyConstructible... Args> |
| 348 | unspecified mem_fn(R (T::* pm)(Args...) volatile); |
| 349 | template<Returnable R, class T, CopyConstructible... Args> |
| 350 | unspecified mem_fn(R (T::* pm)(Args...) const volatile); |
| 351 | |
| 352 | template<FunctionType> class function; // undefined |
| 353 | |
| 354 | template<Returnable R, CopyConstructible... ArgTypes> |
| 355 | class 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 | { |
| 361 | public: |
| 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: |
| 440 | template <MoveConstructible R, MoveConstructible ... ArgTypes> |
| 441 | bool operator==(const function<R(ArgTypes...)>&, nullptr_t); |
| 442 | |
| 443 | template <MoveConstructible R, MoveConstructible ... ArgTypes> |
| 444 | bool operator==(nullptr_t, const function<R(ArgTypes...)>&); |
| 445 | |
| 446 | template <MoveConstructible R, MoveConstructible ... ArgTypes> |
| 447 | bool operator!=(const function<R(ArgTypes...)>&, nullptr_t); |
| 448 | |
| 449 | template <MoveConstructible R, MoveConstructible ... ArgTypes> |
| 450 | bool operator!=(nullptr_t, const function<R(ArgTypes...)>&); |
| 451 | |
| 452 | // 20.7.16.2.7, specialized algorithms: |
| 453 | template <MoveConstructible R, MoveConstructible ... ArgTypes> |
| 454 | void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&); |
| 455 | |
| 456 | template <class T> struct hash; |
| 457 | |
| 458 | template <> struct hash<bool>; |
| 459 | template <> struct hash<char>; |
| 460 | template <> struct hash<signed char>; |
| 461 | template <> struct hash<unsigned char>; |
| 462 | template <> struct hash<char16_t>; |
| 463 | template <> struct hash<char32_t>; |
| 464 | template <> struct hash<wchar_t>; |
| 465 | template <> struct hash<short>; |
| 466 | template <> struct hash<unsigned short>; |
| 467 | template <> struct hash<int>; |
| 468 | template <> struct hash<unsigned int>; |
| 469 | template <> struct hash<long>; |
| 470 | template <> struct hash<long long>; |
| 471 | template <> struct hash<unsigned long>; |
| 472 | template <> struct hash<unsigned long long>; |
| 473 | |
| 474 | template <> struct hash<float>; |
| 475 | template <> struct hash<double>; |
| 476 | template <> struct hash<long double>; |
| 477 | |
| 478 | template<class T> struct hash<T*>; |
| 479 | |
| 480 | } // std |
| 481 | |
| 482 | POLICY: 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 | |
| 501 | template <class _Tp> |
| 502 | struct 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 | |
| 508 | template <class _Tp> |
| 509 | struct 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 | |
| 515 | template <class _Tp> |
| 516 | struct 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 | |
| 522 | template <class _Tp> |
| 523 | struct 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 | |
| 529 | template <class _Tp> |
| 530 | struct 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 | |
| 536 | template <class _Tp> |
| 537 | struct negate : unary_function<_Tp, _Tp> |
| 538 | { |
| 539 | _LIBCPP_INLINE_VISIBILITY _Tp operator()(const _Tp& __x) const |
| 540 | {return -__x;} |
| 541 | }; |
| 542 | |
| 543 | template <class _Tp> |
| 544 | struct 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 | |
| 550 | template <class _Tp> |
| 551 | struct 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 | |
| 557 | template <class _Tp> |
| 558 | struct 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 | |
| 564 | template <class _Tp> |
| 565 | struct 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 | |
| 571 | template <class _Tp> |
| 572 | struct 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 | |
| 578 | template <class _Tp> |
| 579 | struct 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 | |
| 585 | template <class _Tp> |
| 586 | struct 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 | |
| 592 | template <class _Tp> |
| 593 | struct 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 | |
| 599 | template <class _Tp> |
| 600 | struct logical_not : unary_function<_Tp, bool> |
| 601 | { |
| 602 | _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x) const |
| 603 | {return !__x;} |
| 604 | }; |
| 605 | |
| 606 | template <class _Tp> |
| 607 | struct 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 | |
| 613 | template <class _Tp> |
| 614 | struct 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 | |
| 620 | template <class _Tp> |
| 621 | struct 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 | |
| 627 | template <class _Predicate> |
| 628 | class unary_negate |
| 629 | : public unary_function<typename _Predicate::argument_type, bool> |
| 630 | { |
| 631 | _Predicate __pred_; |
| 632 | public: |
| 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 | |
| 639 | template <class _Predicate> |
| 640 | inline _LIBCPP_INLINE_VISIBILITY |
| 641 | unary_negate<_Predicate> |
| 642 | not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);} |
| 643 | |
| 644 | template <class _Predicate> |
| 645 | class binary_negate |
| 646 | : public binary_function<typename _Predicate::first_argument_type, |
| 647 | typename _Predicate::second_argument_type, |
| 648 | bool> |
| 649 | { |
| 650 | _Predicate __pred_; |
| 651 | public: |
| 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 | |
| 659 | template <class _Predicate> |
| 660 | inline _LIBCPP_INLINE_VISIBILITY |
| 661 | binary_negate<_Predicate> |
| 662 | not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);} |
| 663 | |
| 664 | template <class __Operation> |
| 665 | class binder1st |
| 666 | : public unary_function<typename __Operation::second_argument_type, |
| 667 | typename __Operation::result_type> |
| 668 | { |
| 669 | protected: |
| 670 | __Operation op; |
| 671 | typename __Operation::first_argument_type value; |
| 672 | public: |
| 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 | |
| 684 | template <class __Operation, class _Tp> |
| 685 | inline _LIBCPP_INLINE_VISIBILITY |
| 686 | binder1st<__Operation> |
| 687 | bind1st(const __Operation& __op, const _Tp& __x) |
| 688 | {return binder1st<__Operation>(__op, __x);} |
| 689 | |
| 690 | template <class __Operation> |
| 691 | class binder2nd |
| 692 | : public unary_function<typename __Operation::first_argument_type, |
| 693 | typename __Operation::result_type> |
| 694 | { |
| 695 | protected: |
| 696 | __Operation op; |
| 697 | typename __Operation::second_argument_type value; |
| 698 | public: |
| 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 | |
| 709 | template <class __Operation, class _Tp> |
| 710 | inline _LIBCPP_INLINE_VISIBILITY |
| 711 | binder2nd<__Operation> |
| 712 | bind2nd(const __Operation& __op, const _Tp& __x) |
| 713 | {return binder2nd<__Operation>(__op, __x);} |
| 714 | |
| 715 | template <class _Arg, class _Result> |
| 716 | class pointer_to_unary_function : public unary_function<_Arg, _Result> |
| 717 | { |
| 718 | _Result (*__f_)(_Arg); |
| 719 | public: |
| 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 | |
| 726 | template <class _Arg, class _Result> |
| 727 | inline _LIBCPP_INLINE_VISIBILITY |
| 728 | pointer_to_unary_function<_Arg,_Result> |
| 729 | ptr_fun(_Result (*__f)(_Arg)) |
| 730 | {return pointer_to_unary_function<_Arg,_Result>(__f);} |
| 731 | |
| 732 | template <class _Arg1, class _Arg2, class _Result> |
| 733 | class pointer_to_binary_function : public binary_function<_Arg1, _Arg2, _Result> |
| 734 | { |
| 735 | _Result (*__f_)(_Arg1, _Arg2); |
| 736 | public: |
| 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 | |
| 743 | template <class _Arg1, class _Arg2, class _Result> |
| 744 | inline _LIBCPP_INLINE_VISIBILITY |
| 745 | pointer_to_binary_function<_Arg1,_Arg2,_Result> |
| 746 | ptr_fun(_Result (*__f)(_Arg1,_Arg2)) |
| 747 | {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);} |
| 748 | |
| 749 | template<class _Sp, class _Tp> |
| 750 | class mem_fun_t : public unary_function<_Tp*, _Sp> |
| 751 | { |
| 752 | _Sp (_Tp::*__p_)(); |
| 753 | public: |
| 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 | |
| 760 | template<class _Sp, class _Tp, class _Ap> |
| 761 | class mem_fun1_t : public binary_function<_Tp*, _Ap, _Sp> |
| 762 | { |
| 763 | _Sp (_Tp::*__p_)(_Ap); |
| 764 | public: |
| 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 | |
| 771 | template<class _Sp, class _Tp> |
| 772 | inline _LIBCPP_INLINE_VISIBILITY |
| 773 | mem_fun_t<_Sp,_Tp> |
| 774 | mem_fun(_Sp (_Tp::*__f)()) |
| 775 | {return mem_fun_t<_Sp,_Tp>(__f);} |
| 776 | |
| 777 | template<class _Sp, class _Tp, class _Ap> |
| 778 | inline _LIBCPP_INLINE_VISIBILITY |
| 779 | mem_fun1_t<_Sp,_Tp,_Ap> |
| 780 | mem_fun(_Sp (_Tp::*__f)(_Ap)) |
| 781 | {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);} |
| 782 | |
| 783 | template<class _Sp, class _Tp> |
| 784 | class mem_fun_ref_t : public unary_function<_Tp, _Sp> |
| 785 | { |
| 786 | _Sp (_Tp::*__p_)(); |
| 787 | public: |
| 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 | |
| 794 | template<class _Sp, class _Tp, class _Ap> |
| 795 | class mem_fun1_ref_t : public binary_function<_Tp, _Ap, _Sp> |
| 796 | { |
| 797 | _Sp (_Tp::*__p_)(_Ap); |
| 798 | public: |
| 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 | |
| 805 | template<class _Sp, class _Tp> |
| 806 | inline _LIBCPP_INLINE_VISIBILITY |
| 807 | mem_fun_ref_t<_Sp,_Tp> |
| 808 | mem_fun_ref(_Sp (_Tp::*__f)()) |
| 809 | {return mem_fun_ref_t<_Sp,_Tp>(__f);} |
| 810 | |
| 811 | template<class _Sp, class _Tp, class _Ap> |
| 812 | inline _LIBCPP_INLINE_VISIBILITY |
| 813 | mem_fun1_ref_t<_Sp,_Tp,_Ap> |
| 814 | mem_fun_ref(_Sp (_Tp::*__f)(_Ap)) |
| 815 | {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);} |
| 816 | |
| 817 | template <class _Sp, class _Tp> |
| 818 | class const_mem_fun_t : public unary_function<const _Tp*, _Sp> |
| 819 | { |
| 820 | _Sp (_Tp::*__p_)() const; |
| 821 | public: |
| 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 | |
| 828 | template <class _Sp, class _Tp, class _Ap> |
| 829 | class const_mem_fun1_t : public binary_function<const _Tp*, _Ap, _Sp> |
| 830 | { |
| 831 | _Sp (_Tp::*__p_)(_Ap) const; |
| 832 | public: |
| 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 | |
| 839 | template <class _Sp, class _Tp> |
| 840 | inline _LIBCPP_INLINE_VISIBILITY |
| 841 | const_mem_fun_t<_Sp,_Tp> |
| 842 | mem_fun(_Sp (_Tp::*__f)() const) |
| 843 | {return const_mem_fun_t<_Sp,_Tp>(__f);} |
| 844 | |
| 845 | template <class _Sp, class _Tp, class _Ap> |
| 846 | inline _LIBCPP_INLINE_VISIBILITY |
| 847 | const_mem_fun1_t<_Sp,_Tp,_Ap> |
| 848 | mem_fun(_Sp (_Tp::*__f)(_Ap) const) |
| 849 | {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);} |
| 850 | |
| 851 | template <class _Sp, class _Tp> |
| 852 | class const_mem_fun_ref_t : public unary_function<_Tp, _Sp> |
| 853 | { |
| 854 | _Sp (_Tp::*__p_)() const; |
| 855 | public: |
| 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 | |
| 862 | template <class _Sp, class _Tp, class _Ap> |
| 863 | class const_mem_fun1_ref_t : public binary_function<_Tp, _Ap, _Sp> |
| 864 | { |
| 865 | _Sp (_Tp::*__p_)(_Ap) const; |
| 866 | public: |
| 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 | |
| 873 | template <class _Sp, class _Tp> |
| 874 | inline _LIBCPP_INLINE_VISIBILITY |
| 875 | const_mem_fun_ref_t<_Sp,_Tp> |
| 876 | mem_fun_ref(_Sp (_Tp::*__f)() const) |
| 877 | {return const_mem_fun_ref_t<_Sp,_Tp>(__f);} |
| 878 | |
| 879 | template <class _Sp, class _Tp, class _Ap> |
| 880 | inline _LIBCPP_INLINE_VISIBILITY |
| 881 | const_mem_fun1_ref_t<_Sp,_Tp,_Ap> |
| 882 | mem_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 | |
| 892 | template <class _Tp> |
| 893 | class __mem_fn |
| 894 | : public __weak_result_type<_Tp> |
| 895 | { |
| 896 | public: |
| 897 | // types |
| 898 | typedef _Tp type; |
| 899 | private: |
| 900 | type __f_; |
| 901 | |
| 902 | public: |
| 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 | |
| 914 | template<class _R, class _T> |
| 915 | inline _LIBCPP_INLINE_VISIBILITY |
| 916 | __mem_fn<_R _T::*> |
| 917 | mem_fn(_R _T::* __pm) |
| 918 | { |
| 919 | return __mem_fn<_R _T::*>(__pm); |
| 920 | } |
| 921 | |
| 922 | template<class _R, class _T, class ..._Args> |
| 923 | inline _LIBCPP_INLINE_VISIBILITY |
| 924 | __mem_fn<_R (_T::*)(_Args...)> |
| 925 | mem_fn(_R (_T::* __pm)(_Args...)) |
| 926 | { |
| 927 | return __mem_fn<_R (_T::*)(_Args...)>(__pm); |
| 928 | } |
| 929 | |
| 930 | template<class _R, class _T, class ..._Args> |
| 931 | inline _LIBCPP_INLINE_VISIBILITY |
| 932 | __mem_fn<_R (_T::*)(_Args...) const> |
| 933 | mem_fn(_R (_T::* __pm)(_Args...) const) |
| 934 | { |
| 935 | return __mem_fn<_R (_T::*)(_Args...) const>(__pm); |
| 936 | } |
| 937 | |
| 938 | template<class _R, class _T, class ..._Args> |
| 939 | inline _LIBCPP_INLINE_VISIBILITY |
| 940 | __mem_fn<_R (_T::*)(_Args...) volatile> |
| 941 | mem_fn(_R (_T::* __pm)(_Args...) volatile) |
| 942 | { |
| 943 | return __mem_fn<_R (_T::*)(_Args...) volatile>(__pm); |
| 944 | } |
| 945 | |
| 946 | template<class _R, class _T, class ..._Args> |
| 947 | inline _LIBCPP_INLINE_VISIBILITY |
| 948 | __mem_fn<_R (_T::*)(_Args...) const volatile> |
| 949 | mem_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 | |
| 956 | class bad_function_call |
| 957 | : public exception |
| 958 | { |
| 959 | }; |
| 960 | |
| 961 | template<class _Fp> class function; // undefined |
| 962 | |
| 963 | namespace __function |
| 964 | { |
| 965 | |
| 966 | template<class _R, class ..._ArgTypes> |
| 967 | struct __maybe_derive_from_unary_function |
| 968 | { |
| 969 | }; |
| 970 | |
| 971 | template<class _R, class _A1> |
| 972 | struct __maybe_derive_from_unary_function<_R(_A1)> |
| 973 | : public unary_function<_A1, _R> |
| 974 | { |
| 975 | }; |
| 976 | |
| 977 | template<class _R, class ..._ArgTypes> |
| 978 | struct __maybe_derive_from_binary_function |
| 979 | { |
| 980 | }; |
| 981 | |
| 982 | template<class _R, class _A1, class _A2> |
| 983 | struct __maybe_derive_from_binary_function<_R(_A1, _A2)> |
| 984 | : public binary_function<_A1, _A2, _R> |
| 985 | { |
| 986 | }; |
| 987 | |
| 988 | template<class _Fp> class __base; |
| 989 | |
| 990 | template<class _R, class ..._ArgTypes> |
| 991 | class __base<_R(_ArgTypes...)> |
| 992 | { |
| 993 | __base(const __base&); |
| 994 | __base& operator=(const __base&); |
| 995 | public: |
| 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 Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1003 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1004 | virtual const void* target(const type_info&) const = 0; |
| 1005 | virtual const std::type_info& target_type() const = 0; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1006 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1007 | }; |
| 1008 | |
| 1009 | template<class _FD, class _Alloc, class _FB> class __func; |
| 1010 | |
| 1011 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1012 | class __func<_F, _Alloc, _R(_ArgTypes...)> |
| 1013 | : public __base<_R(_ArgTypes...)> |
| 1014 | { |
| 1015 | __compressed_pair<_F, _Alloc> __f_; |
| 1016 | public: |
| 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 Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1024 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1025 | virtual const void* target(const type_info&) const; |
| 1026 | virtual const std::type_info& target_type() const; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1027 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1028 | }; |
| 1029 | |
| 1030 | template<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 | |
| 1042 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1043 | void |
| 1044 | __func<_F, _Alloc, _R(_ArgTypes...)>::__clone(__base<_R(_ArgTypes...)>* __p) const |
| 1045 | { |
| 1046 | ::new (__p) __func(__f_.first(), __f_.second()); |
| 1047 | } |
| 1048 | |
| 1049 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1050 | void |
| 1051 | __func<_F, _Alloc, _R(_ArgTypes...)>::destroy() |
| 1052 | { |
| 1053 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 1054 | } |
| 1055 | |
| 1056 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1057 | void |
| 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 | |
| 1066 | template<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 Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1073 | #ifndef _LIBCPP_NO_RTTI |
| 1074 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1075 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1076 | const 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 | |
| 1084 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1085 | const std::type_info& |
| 1086 | __func<_F, _Alloc, _R(_ArgTypes...)>::target_type() const |
| 1087 | { |
| 1088 | return typeid(_F); |
| 1089 | } |
| 1090 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1091 | #endif |
| 1092 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1093 | } // __function |
| 1094 | |
| 1095 | template<class _R, class ..._ArgTypes> |
| 1096 | class 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;} |
| 1118 | public: |
| 1119 | typedef _R result_type; |
| 1120 | |
| 1121 | // 20.7.16.2.1, construct/copy/destroy: |
Howard Hinnant | e00e030 | 2010-08-19 19:20:10 +0000 | [diff] [blame^] | 1122 | function() : __f_(0) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1123 | 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 | |
| 1164 | private: |
| 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; |
| 1170 | public: |
| 1171 | // 20.7.16.2.4, function invocation: |
| 1172 | _R operator()(_ArgTypes...) const; |
| 1173 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1174 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1175 | // 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 Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1179 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1180 | }; |
| 1181 | |
| 1182 | template<class _R, class ..._ArgTypes> |
| 1183 | function<_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 | |
| 1196 | template<class _R, class ..._ArgTypes> |
| 1197 | function<_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 | |
| 1213 | template<class _R, class ..._ArgTypes> |
| 1214 | template <class _F> |
| 1215 | function<_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 | |
| 1239 | template<class _R, class ..._ArgTypes> |
| 1240 | function<_R(_ArgTypes...)>& |
| 1241 | function<_R(_ArgTypes...)>::operator=(const function& __f) |
| 1242 | { |
| 1243 | function(__f).swap(*this); |
| 1244 | return *this; |
| 1245 | } |
| 1246 | |
| 1247 | template<class _R, class ..._ArgTypes> |
| 1248 | function<_R(_ArgTypes...)>& |
| 1249 | function<_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 | |
| 1270 | template<class _R, class ..._ArgTypes> |
| 1271 | function<_R(_ArgTypes...)>& |
| 1272 | function<_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 | |
| 1281 | template<class _R, class ..._ArgTypes> |
| 1282 | template <class _F> |
| 1283 | typename enable_if |
| 1284 | < |
| 1285 | !is_integral<typename decay<_F>::type>::value, |
| 1286 | function<_R(_ArgTypes...)>& |
| 1287 | >::type |
| 1288 | function<_R(_ArgTypes...)>::operator=(_F&& __f) |
| 1289 | { |
| 1290 | function(_STD::forward<_F>(__f)).swap(*this); |
| 1291 | return *this; |
| 1292 | } |
| 1293 | |
| 1294 | template<class _R, class ..._ArgTypes> |
| 1295 | function<_R(_ArgTypes...)>::~function() |
| 1296 | { |
| 1297 | if (__f_ == (__base*)&__buf_) |
| 1298 | __f_->destroy(); |
| 1299 | else if (__f_) |
| 1300 | __f_->destroy_deallocate(); |
| 1301 | } |
| 1302 | |
| 1303 | template<class _R, class ..._ArgTypes> |
| 1304 | void |
| 1305 | function<_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 | |
| 1340 | template<class _R, class ..._ArgTypes> |
| 1341 | _R |
| 1342 | function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const |
| 1343 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1344 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1345 | if (__f_ == 0) |
| 1346 | throw bad_function_call(); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1347 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1348 | return (*__f_)(_STD::forward<_ArgTypes>(__arg)...); |
| 1349 | } |
| 1350 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1351 | #ifndef _LIBCPP_NO_RTTI |
| 1352 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1353 | template<class _R, class ..._ArgTypes> |
| 1354 | const std::type_info& |
| 1355 | function<_R(_ArgTypes...)>::target_type() const |
| 1356 | { |
| 1357 | if (__f_ == 0) |
| 1358 | return typeid(void); |
| 1359 | return __f_->target_type(); |
| 1360 | } |
| 1361 | |
| 1362 | template<class _R, class ..._ArgTypes> |
| 1363 | template <typename _T> |
| 1364 | _T* |
| 1365 | function<_R(_ArgTypes...)>::target() |
| 1366 | { |
| 1367 | if (__f_ == 0) |
| 1368 | return (_T*)0; |
| 1369 | return (_T*)__f_->target(typeid(_T)); |
| 1370 | } |
| 1371 | |
| 1372 | template<class _R, class ..._ArgTypes> |
| 1373 | template <typename _T> |
| 1374 | const _T* |
| 1375 | function<_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 Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1382 | #endif |
| 1383 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1384 | template <class _R, class... _ArgTypes> |
| 1385 | inline _LIBCPP_INLINE_VISIBILITY |
| 1386 | bool |
| 1387 | operator==(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return !__f;} |
| 1388 | |
| 1389 | template <class _R, class... _ArgTypes> |
| 1390 | inline _LIBCPP_INLINE_VISIBILITY |
| 1391 | bool |
| 1392 | operator==(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return !__f;} |
| 1393 | |
| 1394 | template <class _R, class... _ArgTypes> |
| 1395 | inline _LIBCPP_INLINE_VISIBILITY |
| 1396 | bool |
| 1397 | operator!=(const function<_R(_ArgTypes...)>& __f, nullptr_t) {return (bool)__f;} |
| 1398 | |
| 1399 | template <class _R, class... _ArgTypes> |
| 1400 | inline _LIBCPP_INLINE_VISIBILITY |
| 1401 | bool |
| 1402 | operator!=(nullptr_t, const function<_R(_ArgTypes...)>& __f) {return (bool)__f;} |
| 1403 | |
| 1404 | template <class _R, class... _ArgTypes> |
| 1405 | inline _LIBCPP_INLINE_VISIBILITY |
| 1406 | void |
| 1407 | swap(function<_R(_ArgTypes...)>& __x, function<_R(_ArgTypes...)>& __y) |
| 1408 | {return __x.swap(__y);} |
| 1409 | |
| 1410 | template<class _Tp> struct __is_bind_expression : public false_type {}; |
| 1411 | template<class _Tp> struct is_bind_expression |
| 1412 | : public __is_bind_expression<typename remove_cv<_Tp>::type> {}; |
| 1413 | |
| 1414 | template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {}; |
| 1415 | template<class _Tp> struct is_placeholder |
| 1416 | : public __is_placeholder<typename remove_cv<_Tp>::type> {}; |
| 1417 | |
| 1418 | namespace placeholders |
| 1419 | { |
| 1420 | |
| 1421 | template <int _N> struct __ph {}; |
| 1422 | |
| 1423 | extern __ph<1> _1; |
| 1424 | extern __ph<2> _2; |
| 1425 | extern __ph<3> _3; |
| 1426 | extern __ph<4> _4; |
| 1427 | extern __ph<5> _5; |
| 1428 | extern __ph<6> _6; |
| 1429 | extern __ph<7> _7; |
| 1430 | extern __ph<8> _8; |
| 1431 | extern __ph<9> _9; |
| 1432 | extern __ph<10> _10; |
| 1433 | |
| 1434 | } // placeholders |
| 1435 | |
| 1436 | template<int _N> |
| 1437 | struct __is_placeholder<placeholders::__ph<_N> > |
| 1438 | : public integral_constant<int, _N> {}; |
| 1439 | |
| 1440 | template <class _Tp, class _Uj> |
| 1441 | inline _LIBCPP_INLINE_VISIBILITY |
| 1442 | _Tp& |
| 1443 | __mu(reference_wrapper<_Tp> __t, _Uj&) |
| 1444 | { |
| 1445 | return __t.get(); |
| 1446 | } |
| 1447 | |
| 1448 | template <bool _IsBindExpr, class _Ti, class ..._Uj> |
| 1449 | struct __mu_return1 {}; |
| 1450 | |
| 1451 | template <class _Ti, class ..._Uj> |
| 1452 | struct __mu_return1<true, _Ti, _Uj...> |
| 1453 | { |
| 1454 | typedef typename result_of<_Ti(_Uj...)>::type type; |
| 1455 | }; |
| 1456 | |
| 1457 | |
| 1458 | template <class _Ti, class ..._Uj, size_t ..._Indx> |
| 1459 | inline _LIBCPP_INLINE_VISIBILITY |
| 1460 | typename __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 | |
| 1466 | template <class _Ti, class ..._Uj> |
| 1467 | inline _LIBCPP_INLINE_VISIBILITY |
| 1468 | typename 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 | |
| 1479 | template <bool IsPh, class _Ti, class _Uj> |
| 1480 | struct __mu_return2 {}; |
| 1481 | |
| 1482 | template <class _Ti, class _Uj> |
| 1483 | struct __mu_return2<true, _Ti, _Uj> |
| 1484 | { |
| 1485 | typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type; |
| 1486 | }; |
| 1487 | |
| 1488 | template <class _Ti, class _Uj> |
| 1489 | inline _LIBCPP_INLINE_VISIBILITY |
| 1490 | typename 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 | |
| 1504 | template <class _Ti, class _Uj> |
| 1505 | inline _LIBCPP_INLINE_VISIBILITY |
| 1506 | typename 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 | |
| 1518 | template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj> |
| 1519 | struct ____mu_return; |
| 1520 | |
| 1521 | template <class _Ti, class ..._Uj> |
| 1522 | struct ____mu_return<_Ti, true, false, tuple<_Uj...> > |
| 1523 | { |
| 1524 | typedef typename result_of<_Ti(_Uj...)>::type type; |
| 1525 | }; |
| 1526 | |
| 1527 | template <class _Ti, class _TupleUj> |
| 1528 | struct ____mu_return<_Ti, false, true, _TupleUj> |
| 1529 | { |
| 1530 | typedef typename tuple_element<is_placeholder<_Ti>::value - 1, |
| 1531 | _TupleUj>::type&& type; |
| 1532 | }; |
| 1533 | |
| 1534 | template <class _Ti, class _TupleUj> |
| 1535 | struct ____mu_return<_Ti, false, false, _TupleUj> |
| 1536 | { |
| 1537 | typedef _Ti& type; |
| 1538 | }; |
| 1539 | |
| 1540 | template <class _Ti, class _TupleUj> |
| 1541 | struct __mu_return |
| 1542 | : public ____mu_return<_Ti, |
| 1543 | is_bind_expression<_Ti>::value, |
| 1544 | 0 < is_placeholder<_Ti>::value, |
| 1545 | _TupleUj> |
| 1546 | { |
| 1547 | }; |
| 1548 | |
| 1549 | template <class _Ti, class _TupleUj> |
| 1550 | struct __mu_return<reference_wrapper<_Ti>, _TupleUj> |
| 1551 | { |
| 1552 | typedef _Ti& type; |
| 1553 | }; |
| 1554 | |
| 1555 | template <class _F, class _BoundArgs, class _TupleUj> |
| 1556 | struct __bind_return; |
| 1557 | |
| 1558 | template <class _F, class ..._BoundArgs, class _TupleUj> |
| 1559 | struct __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 | |
| 1572 | template <class _F, class ..._BoundArgs, class _TupleUj> |
| 1573 | struct __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 | |
| 1586 | template <class _F, class _BoundArgs, size_t ..._Indx, class _Args> |
| 1587 | inline _LIBCPP_INLINE_VISIBILITY |
| 1588 | typename __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 | |
| 1595 | template<class _F, class ..._BoundArgs> |
| 1596 | class __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; |
| 1603 | public: |
| 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 | |
| 1631 | template<class _F, class ..._BoundArgs> |
| 1632 | struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {}; |
| 1633 | |
| 1634 | template<class _R, class _F, class ..._BoundArgs> |
| 1635 | class __bind_r |
| 1636 | : public __bind<_F, _BoundArgs...> |
| 1637 | { |
| 1638 | typedef __bind<_F, _BoundArgs...> base; |
| 1639 | public: |
| 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 | |
| 1662 | template<class _R, class _F, class ..._BoundArgs> |
| 1663 | struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {}; |
| 1664 | |
| 1665 | template<class _F, class ..._BoundArgs> |
| 1666 | inline _LIBCPP_INLINE_VISIBILITY |
| 1667 | __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> |
| 1668 | bind(_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 | |
| 1674 | template<class _R, class _F, class ..._BoundArgs> |
| 1675 | inline _LIBCPP_INLINE_VISIBILITY |
| 1676 | __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> |
| 1677 | bind(_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 | |
| 1685 | template <> |
| 1686 | struct 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 | |
| 1692 | template <> |
| 1693 | struct 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 | |
| 1699 | template <> |
| 1700 | struct 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 | |
| 1706 | template <> |
| 1707 | struct 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 | |
| 1715 | template <> |
| 1716 | struct 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 | |
| 1722 | template <> |
| 1723 | struct 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 | |
| 1731 | template <> |
| 1732 | struct 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 | |
| 1738 | template <> |
| 1739 | struct 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 | |
| 1745 | template <> |
| 1746 | struct 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 | |
| 1752 | template <> |
| 1753 | struct 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 | |
| 1759 | template <> |
| 1760 | struct 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 | |
| 1766 | template <> |
| 1767 | struct 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 | |
| 1773 | template <> |
| 1774 | struct 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 | |
| 1780 | template <> |
| 1781 | struct 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 | |
| 1794 | template <> |
| 1795 | struct 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 | |
| 1808 | template <> |
| 1809 | struct 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 | |
| 1821 | template <> |
| 1822 | struct 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 | |
| 1837 | template <> |
| 1838 | struct 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 Hinnant | 21aefc3 | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 1853 | // struct hash<T*> in <memory> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1854 | |
| 1855 | _LIBCPP_END_NAMESPACE_STD |
| 1856 | |
| 1857 | #endif // _LIBCPP_FUNCTIONAL |