From 5dce613aeb790c30d02c222444d0379e4619c184 Mon Sep 17 00:00:00 2001 From: fallenoak Date: Sun, 8 Jan 2023 14:11:48 -0600 Subject: [PATCH] feat(datastore): add getter and setter for uint64_t --- common/datastore/CDataStore.cpp | 30 ++++++++++++++++++++++++++++++ common/datastore/CDataStore.hpp | 2 ++ test/DataStore.cpp | 12 ++++++++++++ 3 files changed, 44 insertions(+) diff --git a/common/datastore/CDataStore.cpp b/common/datastore/CDataStore.cpp index 99e02d4..b67e1a7 100644 --- a/common/datastore/CDataStore.cpp +++ b/common/datastore/CDataStore.cpp @@ -74,6 +74,22 @@ CDataStore& CDataStore::Get(uint32_t& val) { return *this; } +CDataStore& CDataStore::Get(uint64_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; +} + void CDataStore::GetBufferParams(const void** data, uint32_t* size, uint32_t* alloc) const { if (data) { *data = this->m_data; @@ -183,6 +199,20 @@ CDataStore& CDataStore::Put(uint32_t val) { return *this; } +CDataStore& CDataStore::Put(uint64_t val) { + STORM_ASSERT(!this->IsFinal()); + + this->FetchWrite(this->m_size, sizeof(val), nullptr, 0); + + auto ofs = this->m_size - this->m_base; + auto ptr = &this->m_data[ofs]; + *reinterpret_cast(ptr) = val; + + this->m_size += sizeof(val); + + return *this; +} + CDataStore& CDataStore::PutArray(const uint8_t* val, uint32_t count) { STORM_ASSERT(!this->IsFinal()); STORM_ASSERT(val || !count); diff --git a/common/datastore/CDataStore.hpp b/common/datastore/CDataStore.hpp index 9027692..727e5eb 100644 --- a/common/datastore/CDataStore.hpp +++ b/common/datastore/CDataStore.hpp @@ -31,11 +31,13 @@ class CDataStore { int32_t FetchWrite(uint32_t pos, uint32_t bytes, const char* fileName, int32_t lineNumber); CDataStore& Get(uint8_t& val); CDataStore& Get(uint32_t& val); + CDataStore& Get(uint64_t& val); CDataStore& GetDataInSitu(void*& val, uint32_t bytes); int32_t IsFinal(); CDataStore& Put(uint8_t val); CDataStore& Put(uint16_t val); CDataStore& Put(uint32_t val); + CDataStore& Put(uint64_t val); CDataStore& PutArray(const uint8_t* val, uint32_t count); CDataStore& PutData(const void* val, uint32_t bytes); CDataStore& Set(uint32_t pos, uint16_t val); diff --git a/test/DataStore.cpp b/test/DataStore.cpp index 1a45974..f57eeef 100644 --- a/test/DataStore.cpp +++ b/test/DataStore.cpp @@ -32,4 +32,16 @@ TEST_CASE("CDataStore::Get", "[datastore]") { REQUIRE(readVal == writeVal); } + + SECTION("gets uint64_t") { + uint64_t writeVal = 0x1122334455667788; + uint64_t readVal = 0x1122334455667788; + + CDataStore msg; + msg.Put(writeVal); + msg.Finalize(); + msg.Get(readVal); + + REQUIRE(readVal == writeVal); + } }