From 7d5e14910222370bc8c01c3f3f5ef704aa2aafbd Mon Sep 17 00:00:00 2001 From: Simon Rasmussen Date: Wed, 1 Mar 2017 00:00:55 +0100 Subject: [PATCH] Fixed strict alisasing warnings and double/float big endian issues --- include/ur_modern_driver/bin_parser.h | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/include/ur_modern_driver/bin_parser.h b/include/ur_modern_driver/bin_parser.h index 12d49cb..2bc102e 100644 --- a/include/ur_modern_driver/bin_parser.h +++ b/include/ur_modern_driver/bin_parser.h @@ -66,20 +66,14 @@ public: { return be64toh(val); } - float decode(float val) - { - return be32toh(val); - } - double decode(double val) - { - return be64toh(val); - } template T peek() { assert(_buf_pos <= _buf_end); - return decode(*(reinterpret_cast(_buf_pos))); + T val; + std::memcpy(&val, _buf_pos, sizeof(T)); + return decode(val); } template @@ -89,6 +83,19 @@ public: _buf_pos += sizeof(T); } + void parse(double& val) + { + uint64_t inner; + parse(inner); + std::memcpy(&val, &inner, sizeof(double)); + } + void parse(float& val) + { + uint32_t inner; + parse(inner); + std::memcpy(&val, &inner, sizeof(float)); + } + // UR uses 1 byte for boolean values but sizeof(bool) is implementation // defined so we must ensure they're parsed as uint8_t on all compilers void parse(bool& val)