From 94bc48dd783989a178ae8cb5956f50d990878a92 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 21 Sep 2018 19:53:14 -0400 Subject: common/swap: add swap template for enum --- src/common/swap.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/common/swap.h') 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 @@ #pragma once +#include + #if defined(_MSC_VER) #include #elif defined(__linux__) @@ -605,6 +607,44 @@ struct swap_double_t { } }; +template +struct swap_enum_t { + static_assert(std::is_enum_v); + using base = std::underlying_type_t; + +public: + swap_enum_t() = default; + swap_enum_t(const T& v) : value(swap(v)) {} + + swap_enum_t& operator=(const T& v) { + value = swap(v); + return *this; + } + + operator T() const { + return swap(value); + } + + explicit operator base() const { + return static_cast(swap(value)); + } + +protected: + T value{}; + // clang-format off + using swap_t = std::conditional_t< + std::is_same_v, swap_16_t, std::conditional_t< + std::is_same_v, swap_16_t, std::conditional_t< + std::is_same_v, swap_32_t, std::conditional_t< + std::is_same_v, swap_32_t, std::conditional_t< + std::is_same_v, swap_64_t, std::conditional_t< + std::is_same_v, swap_64_t, void>>>>>>; + // clang-format on + static T swap(T x) { + return static_cast(swap_t::swap(static_cast(x))); + } +}; + #if COMMON_LITTLE_ENDIAN using u16_le = u16; using u32_le = u32; @@ -614,6 +654,9 @@ using s16_le = s16; using s32_le = s32; using s64_le = s64; +template +using enum_le = std::enable_if_t, T>; + using float_le = float; using double_le = double; @@ -626,6 +669,9 @@ using s32_be = swap_struct_t>; using u16_be = swap_struct_t>; using s16_be = swap_struct_t>; +template +using enum_be = swap_enum_t; + using float_be = swap_struct_t>; using double_be = swap_struct_t>; #else @@ -639,6 +685,9 @@ using s32_le = swap_struct_t>; using u16_le = swap_struct_t>; using s16_le = swap_struct_t>; +template +using enum_le = swap_enum_t; + using float_le = swap_struct_t>; using double_le = swap_struct_t>; @@ -650,6 +699,9 @@ using s16_be = s16; using s32_be = s32; using s64_be = s64; +template +using enum_be = std::enable_if_t, T>; + using float_be = float; using double_be = double; -- cgit v1.2.3 From 6734c6497653b93d8313ecc30e4eae5348dca2c0 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 25 Jan 2019 12:09:12 -0500 Subject: common/swap: use template and tag for LE/BE specification The tag can be useful for other type-generic templates like BitFields to forward the endianness specification --- src/common/swap.h | 130 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 39 deletions(-) (limited to 'src/common/swap.h') diff --git a/src/common/swap.h b/src/common/swap.h index 466096f58..97aacb4dc 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -645,64 +645,116 @@ protected: } }; -#if COMMON_LITTLE_ENDIAN -using u16_le = u16; -using u32_le = u32; -using u64_le = u64; +struct SwapTag {}; // Use the different endianness from the system +struct KeepTag {}; // Use the same endianness as the system + +template +struct AddEndian; -using s16_le = s16; -using s32_le = s32; -using s64_le = s64; +// KeepTag specializations template -using enum_le = std::enable_if_t, T>; +struct AddEndian { + using type = T; +}; + +// SwapTag specializations -using float_le = float; -using double_le = double; +template <> +struct AddEndian { + using type = u8; +}; -using u64_be = swap_struct_t>; -using s64_be = swap_struct_t>; +template <> +struct AddEndian { + using type = swap_struct_t>; +}; -using u32_be = swap_struct_t>; -using s32_be = swap_struct_t>; +template <> +struct AddEndian { + using type = swap_struct_t>; +}; -using u16_be = swap_struct_t>; -using s16_be = swap_struct_t>; +template <> +struct AddEndian { + using type = swap_struct_t>; +}; + +template <> +struct AddEndian { + using type = s8; +}; + +template <> +struct AddEndian { + using type = swap_struct_t>; +}; + +template <> +struct AddEndian { + using type = swap_struct_t>; +}; + +template <> +struct AddEndian { + using type = swap_struct_t>; +}; + +template <> +struct AddEndian { + using type = swap_struct_t>; +}; + +template <> +struct AddEndian { + using type = swap_struct_t>; +}; template -using enum_be = swap_enum_t; +struct AddEndian { + static_assert(std::is_enum_v); + using type = swap_enum_t; +}; + +// Alias LETag/BETag as KeepTag/SwapTag depending on the system +#if COMMON_LITTLE_ENDIAN + +using LETag = KeepTag; +using BETag = SwapTag; -using float_be = swap_struct_t>; -using double_be = swap_struct_t>; #else -using u64_le = swap_struct_t>; -using s64_le = swap_struct_t>; +using BETag = KeepTag; +using LETag = SwapTag; -using u32_le = swap_struct_t>; -using s32_le = swap_struct_t>; +#endif -using u16_le = swap_struct_t>; -using s16_le = swap_struct_t>; +// Aliases for LE types +using u16_le = AddEndian::type; +using u32_le = AddEndian::type; +using u64_le = AddEndian::type; + +using s16_le = AddEndian::type; +using s32_le = AddEndian::type; +using s64_le = AddEndian::type; template -using enum_le = swap_enum_t; +using enum_le = std::enable_if_t, typename AddEndian::type>; -using float_le = swap_struct_t>; -using double_le = swap_struct_t>; +using float_le = AddEndian::type; +using double_le = AddEndian::type; -using u16_be = u16; -using u32_be = u32; -using u64_be = u64; +// Aliases for BE types +using u16_be = AddEndian::type; +using u32_be = AddEndian::type; +using u64_be = AddEndian::type; -using s16_be = s16; -using s32_be = s32; -using s64_be = s64; +using s16_be = AddEndian::type; +using s32_be = AddEndian::type; +using s64_be = AddEndian::type; template -using enum_be = std::enable_if_t, T>; - -using float_be = float; -using double_be = double; +using enum_be = std::enable_if_t, typename AddEndian::type>; -#endif +using float_be = AddEndian::type; +using double_be = AddEndian::type; -- cgit v1.2.3 From 71530781f33423fc48c9bf43702f1291a38a259d Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 25 Jan 2019 12:16:00 -0500 Subject: common/swap: remove default value for swap type internal storage This is compromise for swap type being used in union. A union has deleted default constructor if it has at least one variant member with non-trivial default constructor, and no variant member of T has a default member initializer. In the use case of Bitfield, all variant members will be the swap type on endianness mismatch, which would all have non-trivial default constructor if default value is specified, and non of them can have member initializer --- src/common/swap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common/swap.h') diff --git a/src/common/swap.h b/src/common/swap.h index 97aacb4dc..4b82865fe 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -172,7 +172,7 @@ struct swap_struct_t { using swapped_t = swap_struct_t; protected: - T value = T(); + T value; static T swap(T v) { return F::swap(v); -- cgit v1.2.3