[PATCH] D59141: [Support/Endian] Add support for endian-specific enums
Pavel Labath via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 11 02:05:49 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL355812: [Support/Endian] Add support for endian-specific enums (authored by labath, committed by ).
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D59141/new/
https://reviews.llvm.org/D59141
Files:
llvm/trunk/include/llvm/Support/Endian.h
llvm/trunk/include/llvm/Support/SwapByteOrder.h
llvm/trunk/unittests/Support/EndianTest.cpp
Index: llvm/trunk/unittests/Support/EndianTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/EndianTest.cpp
+++ llvm/trunk/unittests/Support/EndianTest.cpp
@@ -200,4 +200,13 @@
EXPECT_EQ(*big_val, *little_val);
}
+TEST(Endian, PacketEndianSpecificIntegralAsEnum) {
+ enum class Test : uint16_t { ONETWO = 0x0102, TWOONE = 0x0201 };
+ unsigned char bytes[] = {0x01, 0x02};
+ using LittleTest = little_t<Test>;
+ using BigTest = big_t<Test>;
+ EXPECT_EQ(Test::TWOONE, *reinterpret_cast<LittleTest *>(bytes));
+ EXPECT_EQ(Test::ONETWO, *reinterpret_cast<BigTest *>(bytes));
+}
+
} // end anon namespace
Index: llvm/trunk/include/llvm/Support/Endian.h
===================================================================
--- llvm/trunk/include/llvm/Support/Endian.h
+++ llvm/trunk/include/llvm/Support/Endian.h
@@ -338,6 +338,17 @@
using unaligned_int64_t =
detail::packed_endian_specific_integral<int64_t, native, unaligned>;
+template <typename T>
+using little_t = detail::packed_endian_specific_integral<T, little, unaligned>;
+template <typename T>
+using big_t = detail::packed_endian_specific_integral<T, big, unaligned>;
+
+template <typename T>
+using aligned_little_t =
+ detail::packed_endian_specific_integral<T, little, aligned>;
+template <typename T>
+using aligned_big_t = detail::packed_endian_specific_integral<T, big, aligned>;
+
namespace endian {
template <typename T> inline T read(const void *P, endianness E) {
Index: llvm/trunk/include/llvm/Support/SwapByteOrder.h
===================================================================
--- llvm/trunk/include/llvm/Support/SwapByteOrder.h
+++ llvm/trunk/include/llvm/Support/SwapByteOrder.h
@@ -115,6 +115,13 @@
return out.d;
}
+template <typename T>
+inline typename std::enable_if<std::is_enum<T>::value, T>::type
+getSwappedBytes(T C) {
+ return static_cast<T>(
+ getSwappedBytes(static_cast<typename std::underlying_type<T>::type>(C)));
+}
+
template<typename T>
inline void swapByteOrder(T &Value) {
Value = getSwappedBytes(Value);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59141.190051.patch
Type: text/x-patch
Size: 2107 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190311/93b55358/attachment.bin>
More information about the llvm-commits
mailing list