From dc28b13e8bf4e5e7a788f289739e6301494c43a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=85strand?= Date: Mon, 28 Sep 2020 13:02:18 +0200 Subject: Refactor code to avoid typepunning tricks Change-Id: Iecefa233561ddbef3e04f395f12fb56cd76ffd42 --- applications/message_process/include/message_process.hpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'applications/message_process/include/message_process.hpp') diff --git a/applications/message_process/include/message_process.hpp b/applications/message_process/include/message_process.hpp index 51f474d..e820b60 100644 --- a/applications/message_process/include/message_process.hpp +++ b/applications/message_process/include/message_process.hpp @@ -55,12 +55,22 @@ public: bool read(uint8_t *dst, uint32_t length); bool write(const Vec *vec, size_t length); bool write(const uint32_t type, const void *src = nullptr, uint32_t length = 0); + bool skip(uint32_t length); template bool read(T &dst) { return read(reinterpret_cast(&dst), sizeof(dst)); } + template + bool readOrSkip(T &dst, uint32_t expectedLength) { + if (expectedLength == sizeof(dst)) { + return read(reinterpret_cast(&dst), sizeof(dst)); + } else { + return skip(expectedLength); + } + } + template bool write(const uint32_t type, const T &src) { return write(type, reinterpret_cast(&src), sizeof(src)); -- cgit v1.2.1