From 1113e846dad906864e7e4e9e5275334f6ca3df0e Mon Sep 17 00:00:00 2001 From: fallenoak Date: Mon, 6 Feb 2023 22:56:31 -0600 Subject: [PATCH] feat(datastore): add getter for uint16_t --- common/datastore/CDataStore.cpp | 16 ++++++++++++++++ common/datastore/CDataStore.hpp | 1 + test/DataStore.cpp | 12 ++++++++++++ 3 files changed, 29 insertions(+) diff --git a/common/datastore/CDataStore.cpp b/common/datastore/CDataStore.cpp index aef6a28..7ffeef3 100644 --- a/common/datastore/CDataStore.cpp +++ b/common/datastore/CDataStore.cpp @@ -58,6 +58,22 @@ CDataStore& CDataStore::Get(uint8_t& val) { return *this; } +CDataStore& CDataStore::Get(uint16_t& val) { + STORM_ASSERT(this->IsFinal()); + + auto bytes = sizeof(val); + + if (this->FetchRead(this->m_read, bytes)) { + auto ofs = this->m_read - this->m_base; + auto ptr = &this->m_data[ofs]; + val = *reinterpret_cast(ptr); + + this->m_read += bytes; + } + + return *this; +} + CDataStore& CDataStore::Get(uint32_t& val) { STORM_ASSERT(this->IsFinal()); diff --git a/common/datastore/CDataStore.hpp b/common/datastore/CDataStore.hpp index d13dd6c..9d511f8 100644 --- a/common/datastore/CDataStore.hpp +++ b/common/datastore/CDataStore.hpp @@ -30,6 +30,7 @@ class CDataStore { int32_t FetchRead(uint32_t pos, uint32_t bytes); int32_t FetchWrite(uint32_t pos, uint32_t bytes, const char* fileName, int32_t lineNumber); CDataStore& Get(uint8_t& val); + CDataStore& Get(uint16_t& val); CDataStore& Get(uint32_t& val); CDataStore& Get(uint64_t& val); CDataStore& GetDataInSitu(void*& val, uint32_t bytes); diff --git a/test/DataStore.cpp b/test/DataStore.cpp index f57eeef..d2f82da 100644 --- a/test/DataStore.cpp +++ b/test/DataStore.cpp @@ -21,6 +21,18 @@ TEST_CASE("CDataStore::Get", "[datastore]") { REQUIRE(readVal == writeVal); } + SECTION("gets uint16_t") { + uint16_t writeVal = 0x1234; + uint16_t readVal = -1; + + CDataStore msg; + msg.Put(writeVal); + msg.Finalize(); + msg.Get(readVal); + + REQUIRE(readVal == writeVal); + } + SECTION("gets uint32_t") { uint32_t writeVal = 0x12345678; uint32_t readVal = 0x12345678;