#define BitUnion16(name) __BitUnion(uint16_t, name)
#define BitUnion8(name) __BitUnion(uint8_t, name)
+
+//These templates make it possible to define other templates related to
+//BitUnions without having to refer to internal typedefs or the BitfieldBackend
+//namespace.
+
+//To build a template specialization which works for all BitUnions, accept a
+//template argument T, and then use BitUnionType<T> as an argument in the
+//template. To refer to the basic type the BitUnion wraps, use
+//BitUnionBaseType<T>.
+
+//For example:
+//template <typename T>
+//void func(BitUnionType<T> u) { BitUnionBaseType<T> b = u; }
+
+//Also, BitUnionBaseType can be used on a BitUnion type directly.
+
+template <typename T>
+using BitUnionType = BitfieldBackend::BitUnionOperators<T>;
+
+namespace BitfieldBackend
+{
+ template<typename T>
+ struct BitUnionBaseType
+ {
+ typedef typename BitUnionType<T>::__StorageType Type;
+ };
+
+ template<typename T>
+ struct BitUnionBaseType<BitUnionType<T> >
+ {
+ typedef typename BitUnionType<T>::__StorageType Type;
+ };
+}
+
+template <typename T>
+using BitUnionBaseType = typename BitfieldBackend::BitUnionBaseType<T>::Type;
+
#endif // __BASE_BITUNION_HH__
#include <cassert>
#include <iostream>
+#include <type_traits>
#include "base/bitunion.hh"
#include "base/cprintf.hh"
Split split;
void SetUp() override { sixtyFour = 0; split = 0; }
+
+ template <typename T>
+ uint64_t templatedFunction(T) { return 0; }
+
+ template <typename T>
+ uint64_t
+ templatedFunction(BitUnionType<T> u)
+ {
+ BitUnionBaseType<T> b = u;
+ return b;
+ }
};
TEST_F(BitUnionData, NormalBitfield)
EXPECT_EQ(split, 0xf0f0);
EXPECT_EQ((uint64_t)split.split, 0xff);
}
+
+TEST_F(BitUnionData, Templating)
+{
+ sixtyFour = 0xff;
+ EXPECT_EQ(templatedFunction(sixtyFour), 0xff);
+ EXPECT_EQ(templatedFunction((uint64_t)sixtyFour), 0);
+
+ BitUnion(uint64_t, Dummy64)
+ EndBitUnion(Dummy64);
+
+ BitUnion(uint32_t, Dummy32)
+ EndBitUnion(Dummy32);
+
+ bool is64;
+ is64 = std::is_same<BitUnionBaseType<Dummy64>, uint64_t>::value;
+ EXPECT_TRUE(is64);
+ is64 = std::is_same<BitUnionBaseType<Dummy32>, uint64_t>::value;
+ EXPECT_FALSE(is64);
+}