diff --git a/common/datastore/CDataStoreCache.hpp b/common/datastore/CDataStoreCache.hpp index 6d0ef7d..a3e7d89 100644 --- a/common/datastore/CDataStoreCache.hpp +++ b/common/datastore/CDataStoreCache.hpp @@ -3,6 +3,7 @@ #include "common/datastore/CDataStore.hpp" #include +#include template class CDataStoreCache : public CDataStore { @@ -12,17 +13,42 @@ class CDataStoreCache : public CDataStore { // Virtual member functions virtual void InternalInitialize(uint8_t*& data, uint32_t& base, uint32_t& alloc); + virtual void InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc); + virtual ~CDataStoreCache(); // Member functions CDataStoreCache() { this->InternalInitialize(this->m_data, this->m_base, this->m_alloc); } + void Destroy(); }; +template +CDataStoreCache::~CDataStoreCache() { + this->Destroy(); +} + +template +void CDataStoreCache::Destroy() { + if (this->m_alloc != -1) { + this->InternalDestroy(this->m_data, this->m_base, this->m_alloc); + } +} + template void CDataStoreCache::InternalInitialize(uint8_t*& data, uint32_t& base, uint32_t& alloc) { data = this->m_cache; alloc = size; } +template +void CDataStoreCache::InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc) { + if (data && data != this->m_cache) { + SMemFree(data); + } + + data = nullptr; + alloc = 0; +} + #endif diff --git a/test/DataStore.cpp b/test/DataStore.cpp index dd64126..de671b2 100644 --- a/test/DataStore.cpp +++ b/test/DataStore.cpp @@ -136,3 +136,10 @@ TEST_CASE("CDataStore::Size", "[datastore]") { REQUIRE(msg.Size() == 3); } } + +TEST_CASE("CDataStoreCache<1024>::CDataStoreCache<1024>", "[datastore]") { + SECTION("constructs new data store") { + CDataStoreCache<1024> msg; + SUCCEED(); + } +}