mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 03:02:29 +00:00
feat(datastore): add getter for uint8_t
This commit is contained in:
parent
1c4d306a7f
commit
30c34ca015
3 changed files with 46 additions and 0 deletions
|
|
@ -8,6 +8,22 @@ void CDataStore::DetachBuffer(void** data, uint32_t* size, uint32_t* alloc) {
|
||||||
// TODO
|
// 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) {
|
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) {
|
if (pos >= this->m_base && pos + bytes <= this->m_base + this->m_alloc) {
|
||||||
return 1;
|
return 1;
|
||||||
|
|
@ -26,6 +42,20 @@ void CDataStore::Finalize() {
|
||||||
this->m_read = 0;
|
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<uint8_t*>(ptr);
|
||||||
|
|
||||||
|
this->m_read += sizeof(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
void CDataStore::GetBufferParams(const void** data, uint32_t* size, uint32_t* alloc) const {
|
void CDataStore::GetBufferParams(const void** data, uint32_t* size, uint32_t* alloc) const {
|
||||||
if (data) {
|
if (data) {
|
||||||
*data = this->m_data;
|
*data = this->m_data;
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,9 @@ class CDataStore {
|
||||||
virtual uint32_t GetHeaderSpace();
|
virtual uint32_t GetHeaderSpace();
|
||||||
|
|
||||||
// Member functions
|
// 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);
|
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);
|
CDataStore& GetDataInSitu(void*& val, uint32_t bytes);
|
||||||
int32_t IsFinal();
|
int32_t IsFinal();
|
||||||
CDataStore& Put(uint8_t val);
|
CDataStore& Put(uint8_t val);
|
||||||
|
|
|
||||||
|
|
@ -7,3 +7,17 @@ TEST_CASE("CDataStore::CDataStore", "[datastore]") {
|
||||||
SUCCEED();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue