summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Weiyi Wang2018-09-21 19:53:14 -0400
committerGravatar fearlessTobi2019-02-06 17:21:15 +0100
commit94bc48dd783989a178ae8cb5956f50d990878a92 (patch)
treed1205cb5355b664cd220761235fddc2914307815 /src
parentMerge pull request #2081 from ReinUsesLisp/lmem-64 (diff)
downloadyuzu-94bc48dd783989a178ae8cb5956f50d990878a92.tar.gz
yuzu-94bc48dd783989a178ae8cb5956f50d990878a92.tar.xz
yuzu-94bc48dd783989a178ae8cb5956f50d990878a92.zip
common/swap: add swap template for enum
Diffstat (limited to 'src')
-rw-r--r--src/common/swap.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/common/swap.h b/src/common/swap.h
index 32af0b6ac..466096f58 100644
--- a/src/common/swap.h
+++ b/src/common/swap.h
@@ -17,6 +17,8 @@
17 17
18#pragma once 18#pragma once
19 19
20#include <type_traits>
21
20#if defined(_MSC_VER) 22#if defined(_MSC_VER)
21#include <cstdlib> 23#include <cstdlib>
22#elif defined(__linux__) 24#elif defined(__linux__)
@@ -605,6 +607,44 @@ struct swap_double_t {
605 } 607 }
606}; 608};
607 609
610template <typename T>
611struct swap_enum_t {
612 static_assert(std::is_enum_v<T>);
613 using base = std::underlying_type_t<T>;
614
615public:
616 swap_enum_t() = default;
617 swap_enum_t(const T& v) : value(swap(v)) {}
618
619 swap_enum_t& operator=(const T& v) {
620 value = swap(v);
621 return *this;
622 }
623
624 operator T() const {
625 return swap(value);
626 }
627
628 explicit operator base() const {
629 return static_cast<base>(swap(value));
630 }
631
632protected:
633 T value{};
634 // clang-format off
635 using swap_t = std::conditional_t<
636 std::is_same_v<base, u16>, swap_16_t<u16>, std::conditional_t<
637 std::is_same_v<base, s16>, swap_16_t<s16>, std::conditional_t<
638 std::is_same_v<base, u32>, swap_32_t<u32>, std::conditional_t<
639 std::is_same_v<base, s32>, swap_32_t<s32>, std::conditional_t<
640 std::is_same_v<base, u64>, swap_64_t<u64>, std::conditional_t<
641 std::is_same_v<base, s64>, swap_64_t<s64>, void>>>>>>;
642 // clang-format on
643 static T swap(T x) {
644 return static_cast<T>(swap_t::swap(static_cast<base>(x)));
645 }
646};
647
608#if COMMON_LITTLE_ENDIAN 648#if COMMON_LITTLE_ENDIAN
609using u16_le = u16; 649using u16_le = u16;
610using u32_le = u32; 650using u32_le = u32;
@@ -614,6 +654,9 @@ using s16_le = s16;
614using s32_le = s32; 654using s32_le = s32;
615using s64_le = s64; 655using s64_le = s64;
616 656
657template <typename T>
658using enum_le = std::enable_if_t<std::is_enum_v<T>, T>;
659
617using float_le = float; 660using float_le = float;
618using double_le = double; 661using double_le = double;
619 662
@@ -626,6 +669,9 @@ using s32_be = swap_struct_t<s32, swap_32_t<s32>>;
626using u16_be = swap_struct_t<u16, swap_16_t<u16>>; 669using u16_be = swap_struct_t<u16, swap_16_t<u16>>;
627using s16_be = swap_struct_t<s16, swap_16_t<s16>>; 670using s16_be = swap_struct_t<s16, swap_16_t<s16>>;
628 671
672template <typename T>
673using enum_be = swap_enum_t<T>;
674
629using float_be = swap_struct_t<float, swap_float_t<float>>; 675using float_be = swap_struct_t<float, swap_float_t<float>>;
630using double_be = swap_struct_t<double, swap_double_t<double>>; 676using double_be = swap_struct_t<double, swap_double_t<double>>;
631#else 677#else
@@ -639,6 +685,9 @@ using s32_le = swap_struct_t<s32, swap_32_t<s32>>;
639using u16_le = swap_struct_t<u16, swap_16_t<u16>>; 685using u16_le = swap_struct_t<u16, swap_16_t<u16>>;
640using s16_le = swap_struct_t<s16, swap_16_t<s16>>; 686using s16_le = swap_struct_t<s16, swap_16_t<s16>>;
641 687
688template <typename T>
689using enum_le = swap_enum_t<T>;
690
642using float_le = swap_struct_t<float, swap_float_t<float>>; 691using float_le = swap_struct_t<float, swap_float_t<float>>;
643using double_le = swap_struct_t<double, swap_double_t<double>>; 692using double_le = swap_struct_t<double, swap_double_t<double>>;
644 693
@@ -650,6 +699,9 @@ using s16_be = s16;
650using s32_be = s32; 699using s32_be = s32;
651using s64_be = s64; 700using s64_be = s64;
652 701
702template <typename T>
703using enum_be = std::enable_if_t<std::is_enum_v<T>, T>;
704
653using float_be = float; 705using float_be = float;
654using double_be = double; 706using double_be = double;
655 707