diff --git a/common/datastore/CDataStore.cpp b/common/datastore/CDataStore.cpp index 2e45012..6ebda53 100644 --- a/common/datastore/CDataStore.cpp +++ b/common/datastore/CDataStore.cpp @@ -8,6 +8,22 @@ void CDataStore::DetachBuffer(void** data, uint32_t* size, uint32_t* alloc) { // TODO } +int32_t CDataStore::FetchRead(uint32_t pos, uint32_t bytes) { + if (pos + bytes <= this->m_size) { + if (pos >= this->m_base && pos + bytes <= this->m_base + this->m_alloc) { + return 1; + } + + if (this->InternalFetchRead(pos, bytes, this->m_data, this->m_base, this->m_alloc)) { + return 1; + } + } + + this->m_read = this->m_size + 1; + + return 0; +} + int32_t CDataStore::FetchWrite(uint32_t pos, uint32_t bytes, const char* fileName, int32_t lineNumber) { if (pos >= this->m_base && pos + bytes <= this->m_base + this->m_alloc) { return 1; @@ -26,6 +42,20 @@ void CDataStore::Finalize() { this->m_read = 0; } +CDataStore& CDataStore::Get(uint8_t& val) { + STORM_ASSERT(this->IsFinal()); + + if (this->FetchRead(this->m_read, 1)) { + auto ofs = this->m_read - this->m_base; + auto ptr = &this->m_data[ofs]; + val = *reinterpret_cast(ptr); + + this->m_read += sizeof(val); + } + + return *this; +} + void CDataStore::GetBufferParams(const void** data, uint32_t* size, uint32_t* alloc) const { if (data) { *data = this->m_data; diff --git a/common/datastore/CDataStore.hpp b/common/datastore/CDataStore.hpp index a0ae3b3..acba417 100644 --- a/common/datastore/CDataStore.hpp +++ b/common/datastore/CDataStore.hpp @@ -27,7 +27,9 @@ class CDataStore { virtual uint32_t GetHeaderSpace(); // Member functions + 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& GetDataInSitu(void*& val, uint32_t bytes); int32_t IsFinal(); CDataStore& Put(uint8_t val); diff --git a/test/DataStore.cpp b/test/DataStore.cpp index e33ec2c..8767ed1 100644 --- a/test/DataStore.cpp +++ b/test/DataStore.cpp @@ -7,3 +7,17 @@ TEST_CASE("CDataStore::CDataStore", "[datastore]") { SUCCEED(); } } + +TEST_CASE("CDataStore::Get", "[datastore]") { + SECTION("gets uint8_t") { + uint8_t writeVal = 8; + uint8_t readVal = -1; + + CDataStore msg; + msg.Put(writeVal); + msg.Finalize(); + msg.Get(readVal); + + REQUIRE(readVal == writeVal); + } +}