25 #ifndef INCLUDED_Endianness_h_GUID_A8D4BB43_3F1B_46AA_8044_BD082A07C299
26 #define INCLUDED_Endianness_h_GUID_A8D4BB43_3F1B_46AA_8044_BD082A07C299
33 #include <boost/version.hpp>
35 #if BOOST_VERSION >= 105500
36 #include <boost/predef.h>
37 #if BOOST_ENDIAN_LITTLE_BYTE
38 #define OSVR_IS_LITTLE_ENDIAN
39 #define OSVR_BYTE_ORDER_ABCD
40 #elif BOOST_ENDIAN_BIG_BYTE
41 #define OSVR_IS_BIG_ENDIAN
42 #define OSVR_BYTE_ORDER_DCBA
44 #error "Unsupported byte order!"
48 #include <boost/detail/endian.hpp>
50 #if defined(BOOST_LITTLE_ENDIAN)
51 #define OSVR_IS_LITTLE_ENDIAN
52 #define OSVR_BYTE_ORDER_ABCD
53 #elif defined(BOOST_BIG_ENDIAN)
54 #define OSVR_IS_BIG_ENDIAN
55 #define OSVR_BYTE_ORDER_DCBA
57 #error "Unsupported byte order!"
62 #include <boost/integer.hpp>
65 #include <type_traits>
67 #if defined(__FLOAT_WORD_ORDER) && defined(__BYTE_ORDER)
68 #if (__FLOAT_WORD_ORDER != __BYTE_ORDER)
69 #define OSVR_FLOAT_ORDER_MIXED
79 namespace serialization {
80 template <
typename T,
typename Dummy =
void>
85 template <
typename Dest,
typename Src>
86 inline Dest safe_pun(Src
const src) {
87 static_assert(
sizeof(Src) ==
sizeof(Dest),
88 "Can only use safe_pun for types of the same size");
90 memcpy(reinterpret_cast<char *>(&ret),
91 reinterpret_cast<char const *>(&src),
sizeof(src));
98 static T hton(T
const v) {
return v; }
99 static T ntoh(T
const v) {
return v; }
105 static T ntoh(T
const v) {
return hton(v); }
109 template <
typename T,
typename Un
signedIntType>
113 typedef UnsignedIntType uint_t;
115 static_assert(
sizeof(T) ==
sizeof(UnsignedIntType),
116 "Type-punning host-network conversion only works "
117 "when the types are the same size");
119 std::is_unsigned<UnsignedIntType>::value,
120 "Template parameter UnsignedIntType should be unsigned!");
123 static type hton(type
const v) {
124 return safe_pun<type>(IntTraits::hton(safe_pun<uint_t>(v)));
126 static type ntoh(type
const v) {
return hton(v); }
131 #if defined(OSVR_IS_BIG_ENDIAN)
132 template <
typename T>
134 T, typename
std::enable_if<std::is_integral<T>::value>::type>
137 #elif defined(OSVR_IS_LITTLE_ENDIAN)
138 template <
typename T>
139 struct NetworkByteOrderTraits<
140 T, typename
std::enable_if<std::is_integral<T>::value>::type>
141 : detail::IntegerByteOrderSwap<T> {};
147 struct NetworkByteOrderTraits<float, void> : detail::TypePunByteOrder<float, uint32_t> {
151 #if defined(OSVR_FLOAT_ORDER_MIXED)
152 template <>
struct NetworkByteOrderTraits<double, void> {
153 typedef detail::TypePunByteOrder<double, uint64_t> ByteOrder;
154 typedef ByteOrder::type type;
155 static const size_t WORD_SIZE =
sizeof(type) / 2;
156 static inline type hton(type
const v) {
158 type src = ByteOrder::hton(v);
161 memcpy(reinterpret_cast<char *>(&ret),
162 reinterpret_cast<char const *>(&src) + WORD_SIZE,
164 memcpy(reinterpret_cast<char *>(&ret) + WORD_SIZE,
165 reinterpret_cast<char const *>(&src), WORD_SIZE);
168 static type ntoh(type
const v) {
return hton(v); }
177 template <
typename T>
inline T ntoh(T
const v) {
182 template <
typename T>
inline T hton(T
const v) {
183 return NetworkByteOrderTraits<T>::hton(v);
189 #endif // INCLUDED_Endianness_h_GUID_A8D4BB43_3F1B_46AA_8044_BD082A07C299
Header wrapping the C99 standard stdint header.
Stock implementation of a no-op host-network conversion.
Header providing optimized integer byte-swap functionality through a single function template...
Stock implementation of a type-punning host-network conversion.
Stock implementation of a byte-swapping host-network conversion.
Type integerByteSwap(Type const v)
Swap the order of bytes of an arbitrary integer type.