Changeset 22ba300 in mainline
- Timestamp:
- 2018-07-05T21:41:19Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ee51635
- Parents:
- e65c9285
- git-author:
- Dzejrou <dzejrou@…> (2018-03-01 17:42:10)
- git-committer:
- Dzejrou <dzejrou@…> (2018-07-05 21:41:19)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/cpp/include/impl/functional.hpp
re65c9285 r22ba300 30 30 #define LIBCPP_FUNCTIONAL 31 31 32 #include <limits> 32 33 #include <type_traits> 33 34 extern "C" { 35 #include <adt/hash.h> 36 } 34 #include <utility> 37 35 38 36 namespace std … … 44 42 */ 45 43 template<class R, class T, class T1, class... Ts> 46 decltype(auto) invoke(R T::* f, T1&& t1, Args&&... args)44 decltype(auto) invoke(R T::* f, T1&& t1, Ts&&... args) 47 45 { 48 46 if constexpr (is_member_function_pointer_v<decltype(f)>) … … 50 48 if constexpr (is_base_of_v<T, remove_reference_t<T1>>) 51 49 // (1.1) 52 return (t1.*f)(forward< Args>(args)...);50 return (t1.*f)(forward<Ts>(args)...); 53 51 else 54 52 // (1.2) 55 return ((*t1).*f)(forward< Args>(args)...);53 return ((*t1).*f)(forward<Ts>(args)...); 56 54 } 57 else if (is_member_object_pointer_v<decltype(f)> && sizeof...(args) == 0)55 else if constexpr (is_member_object_pointer_v<decltype(f)> && sizeof...(args) == 0) 58 56 { 59 57 /** … … 68 66 return (*t1).*f; 69 67 } 70 static_assert(false, "invalid invoke"); 68 69 /** 70 * Note: If this condition holds this will not be reachable, 71 * but a new addition to the standard (17.7 point (8.1)) 72 * prohibits us from simply using false as the condition here, 73 * so we use this because we know it is false here. 74 */ 75 static_assert(is_member_function_pointer_v<decltype(f)>, "invalid invoke"); 71 76 } 72 77 … … 208 213 { 209 214 template<class T, class U> 210 constexpr auto operator( T&& lhs, U&& rhs) const215 constexpr auto operator()(T&& lhs, U&& rhs) const 211 216 -> decltype(forward<T>(lhs) + forward<U>(rhs)) 212 217 { … … 221 226 { 222 227 template<class T, class U> 223 constexpr auto operator( T&& lhs, U&& rhs) const228 constexpr auto operator()(T&& lhs, U&& rhs) const 224 229 -> decltype(forward<T>(lhs) - forward<U>(rhs)) 225 230 { … … 234 239 { 235 240 template<class T, class U> 236 constexpr auto operator( T&& lhs, U&& rhs) const241 constexpr auto operator()(T&& lhs, U&& rhs) const 237 242 -> decltype(forward<T>(lhs) * forward<U>(rhs)) 238 243 { … … 247 252 { 248 253 template<class T, class U> 249 constexpr auto operator( T&& lhs, U&& rhs) const254 constexpr auto operator()(T&& lhs, U&& rhs) const 250 255 -> decltype(forward<T>(lhs) / forward<U>(rhs)) 251 256 { … … 260 265 { 261 266 template<class T, class U> 262 constexpr auto operator( T&& lhs, U&& rhs) const267 constexpr auto operator()(T&& lhs, U&& rhs) const 263 268 -> decltype(forward<T>(lhs) % forward<U>(rhs)) 264 269 { … … 273 278 { 274 279 template<class T> 275 constexpr auto operator( T&& x) const280 constexpr auto operator()(T&& x) const 276 281 -> decltype(-forward<T>(x)) 277 282 { … … 287 292 288 293 template<class T = void> 289 struct equal_to ;294 struct equal_to 290 295 { 291 296 constexpr bool operator()(const T& lhs, const T& rhs) const … … 368 373 { 369 374 template<class T, class U> 370 constexpr auto operator( T&& lhs, U&& rhs) const375 constexpr auto operator()(T&& lhs, U&& rhs) const 371 376 -> decltype(forward<T>(lhs) == forward<U>(rhs)) 372 377 { … … 381 386 { 382 387 template<class T, class U> 383 constexpr auto operator( T&& lhs, U&& rhs) const388 constexpr auto operator()(T&& lhs, U&& rhs) const 384 389 -> decltype(forward<T>(lhs) != forward<U>(rhs)) 385 390 { … … 394 399 { 395 400 template<class T, class U> 396 constexpr auto operator( T&& lhs, U&& rhs) const401 constexpr auto operator()(T&& lhs, U&& rhs) const 397 402 -> decltype(forward<T>(lhs) > forward<U>(rhs)) 398 403 { … … 407 412 { 408 413 template<class T, class U> 409 constexpr auto operator( T&& lhs, U&& rhs) const414 constexpr auto operator()(T&& lhs, U&& rhs) const 410 415 -> decltype(forward<T>(lhs) < forward<U>(rhs)) 411 416 { … … 420 425 { 421 426 template<class T, class U> 422 constexpr auto operator( T&& lhs, U&& rhs) const427 constexpr auto operator()(T&& lhs, U&& rhs) const 423 428 -> decltype(forward<T>(lhs) >= forward<U>(rhs)) 424 429 { … … 433 438 { 434 439 template<class T, class U> 435 constexpr auto operator( T&& lhs, U&& rhs) const440 constexpr auto operator()(T&& lhs, U&& rhs) const 436 441 -> decltype(forward<T>(lhs) <= forward<U>(rhs)) 437 442 { … … 488 493 { 489 494 template<class T, class U> 490 constexpr auto operator( T&& lhs, U&& rhs) const495 constexpr auto operator()(T&& lhs, U&& rhs) const 491 496 -> decltype(forward<T>(lhs) && forward<U>(rhs)) 492 497 { … … 501 506 { 502 507 template<class T, class U> 503 constexpr auto operator( T&& lhs, U&& rhs) const508 constexpr auto operator()(T&& lhs, U&& rhs) const 504 509 -> decltype(forward<T>(lhs) || forward<U>(rhs)) 505 510 { … … 514 519 { 515 520 template<class T> 516 constexpr auto operator( T&& x) const521 constexpr auto operator()(T&& x) const 517 522 -> decltype(!forward<T>(x)) 518 523 { 519 return !forward<T>( lhs);524 return !forward<T>(x); 520 525 } 521 526 … … 582 587 { 583 588 template<class T, class U> 584 constexpr auto operator( T&& lhs, U&& rhs) const589 constexpr auto operator()(T&& lhs, U&& rhs) const 585 590 -> decltype(forward<T>(lhs) & forward<U>(rhs)) 586 591 { … … 595 600 { 596 601 template<class T, class U> 597 constexpr auto operator( T&& lhs, U&& rhs) const602 constexpr auto operator()(T&& lhs, U&& rhs) const 598 603 -> decltype(forward<T>(lhs) | forward<U>(rhs)) 599 604 { … … 608 613 { 609 614 template<class T, class U> 610 constexpr auto operator( T&& lhs, U&& rhs) const615 constexpr auto operator()(T&& lhs, U&& rhs) const 611 616 -> decltype(forward<T>(lhs) ^ forward<U>(rhs)) 612 617 { … … 621 626 { 622 627 template<class T> 623 constexpr auto operator( T&& x) const628 constexpr auto operator()(T&& x) const 624 629 -> decltype(~forward<T>(x)) 625 630 { 626 return ~forward<T>( lhs);631 return ~forward<T>(x); 627 632 } 628 633 … … 710 715 */ 711 716 717 namespace aux 718 { 719 template<class T> 720 union converter 721 { 722 T value; 723 uint64_t converted; 724 }; 725 726 template<class T> 727 T hash_(uint64_t x) noexcept 728 { 729 // TODO: This is copied form adt/hash (temporarily), 730 // check if we can use something better. 731 x = (x ^ 61) ^ (x >> 16); 732 x = x + (x << 3); 733 x = x ^ (x >> 4); 734 x = x * 0x27d4eb2d; 735 x = x ^ (x >> 15); 736 737 return static_cast<T>((x << 32) | (x >> 32)); 738 } 739 740 template<class T> 741 size_t hash(T x) noexcept 742 { 743 static_assert(is_arithmetic_v<T> || is_pointer_v<T>, 744 "invalid type passed to aux::hash"); 745 746 converter<T> conv; 747 conv.value = x; 748 749 return hash_<size_t>(conv.converted); 750 } 751 } 752 712 753 template<class T> 713 754 struct hash … … 719 760 size_t operator()(bool x) const noexcept 720 761 { 721 return hash_mix(static_cast<size_t>(x));762 return aux::hash(x); 722 763 } 723 764 … … 731 772 size_t operator()(char x) const noexcept 732 773 { 733 return hash_mix(static_cast<size_t>(x));774 return aux::hash(x); 734 775 } 735 776 … … 743 784 size_t operator()(signed char x) const noexcept 744 785 { 745 return hash_mix(static_cast<size_t>(x));786 return aux::hash(x); 746 787 } 747 788 … … 755 796 size_t operator()(unsigned char x) const noexcept 756 797 { 757 return hash_mix(static_cast<size_t>(x));798 return aux::hash(x); 758 799 } 759 800 … … 767 808 size_t operator()(char16_t x) const noexcept 768 809 { 769 return hash_mix(static_cast<size_t>(x));810 return aux::hash(x); 770 811 } 771 812 … … 779 820 size_t operator()(char32_t x) const noexcept 780 821 { 781 return hash_mix(static_cast<size_t>(x));822 return aux::hash(x); 782 823 } 783 824 … … 791 832 size_t operator()(wchar_t x) const noexcept 792 833 { 793 return hash_mix(static_cast<size_t>(x));834 return aux::hash(x); 794 835 } 795 836 … … 803 844 size_t operator()(short x) const noexcept 804 845 { 805 return hash_mix(static_cast<size_t>(x));846 return aux::hash(x); 806 847 } 807 848 … … 815 856 size_t operator()(unsigned short x) const noexcept 816 857 { 817 return hash_mix(static_cast<size_t>(x));858 return aux::hash(x); 818 859 } 819 860 … … 827 868 size_t operator()(int x) const noexcept 828 869 { 829 return hash_mix(static_cast<size_t>(x));870 return aux::hash(x); 830 871 } 831 872 … … 839 880 size_t operator()(unsigned int x) const noexcept 840 881 { 841 return hash_mix(static_cast<size_t>(x));882 return aux::hash(x); 842 883 } 843 884 … … 851 892 size_t operator()(long x) const noexcept 852 893 { 853 return hash_mix(static_cast<size_t>(x));894 return aux::hash(x); 854 895 } 855 896 … … 863 904 size_t operator()(long long x) const noexcept 864 905 { 865 return hash_mix(static_cast<size_t>(x));906 return aux::hash(x); 866 907 } 867 908 … … 875 916 size_t operator()(unsigned long x) const noexcept 876 917 { 877 return hash_mix(static_cast<size_t>(x));918 return aux::hash(x); 878 919 } 879 920 … … 887 928 size_t operator()(unsigned long long x) const noexcept 888 929 { 889 return hash_mix(static_cast<size_t>(x));930 return aux::hash(x); 890 931 } 891 932 … … 899 940 size_t operator()(float x) const noexcept 900 941 { 901 return hash_mix(*(size_t*)(&x));942 return aux::hash(x); 902 943 } 903 944 … … 907 948 908 949 template<> 909 struct hash<double> ;950 struct hash<double> 910 951 { 911 952 size_t operator()(double x) const noexcept 912 953 { 913 return hash_mix(*(size_t*)(&x));954 return aux::hash(x); 914 955 } 915 956 … … 919 960 920 961 template<> 921 struct hash<long double> ;962 struct hash<long double> 922 963 { 923 964 size_t operator()(long double x) const noexcept 924 965 { 925 return hash_mix(*(size_t*)(&x));966 return aux::hash(x); 926 967 } 927 968 … … 935 976 size_t operator()(T* x) const noexcept 936 977 { 937 return hash_mix(static_cast<size_t>(x));978 return aux::hash(x); 938 979 } 939 980
Note:
See TracChangeset
for help on using the changeset viewer.