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>
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; }
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>
140 T, typename std::enable_if<std::is_integral<T>::value>
::type>
151 #if defined(OSVR_FLOAT_ORDER_MIXED) 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) {
189 #endif // INCLUDED_Endianness_h_GUID_A8D4BB43_3F1B_46AA_8044_BD082A07C299 Dest safe_pun(Src const src)
Take the binary representation of a value with one type, and return it as another type...
Definition: Endianness.h:86
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
Header wrapping the C99 standard stdint header.
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Stock implementation of a no-op host-network conversion.
Definition: Endianness.h:97
T ntoh(T const v)
Convert network byte order (big endian) to host byte order.
Definition: Endianness.h:177
Header providing optimized integer byte-swap functionality through a single function template...
Stock implementation of a type-punning host-network conversion.
Definition: Endianness.h:110
Definition: newuoa.h:1888
T hton(T const v)
Convert host byte order to network byte order (big endian)
Definition: Endianness.h:182
Stock implementation of a byte-swapping host-network conversion.
Definition: Endianness.h:103
Type integerByteSwap(Type const v)
Swap the order of bytes of an arbitrary integer type.
Definition: IntegerByteSwap.h:215
Definition: Endianness.h:81