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') 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