From 291710f257745e60e8417b2fa3648dcbfa09ed5d Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Tue, 12 May 2020 17:57:37 +0200 Subject: [PATCH] new ctor and bugfixes --- storagemanager/LocalPool.tpp | 6 +++-- storagemanager/StorageAccessor.cpp | 13 +++++++++- storagemanager/StorageAccessor.h | 39 +++++++++++++++--------------- storagemanager/StorageManagerIF.h | 4 +++ 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/storagemanager/LocalPool.tpp b/storagemanager/LocalPool.tpp index 31034974..98343ea7 100644 --- a/storagemanager/LocalPool.tpp +++ b/storagemanager/LocalPool.tpp @@ -148,7 +148,7 @@ template inline ConstAccessorPair LocalPool::getData( store_address_t storeId) { uint8_t* tempData = nullptr; - ConstStorageAccessor constAccessor(storeId); + ConstStorageAccessor constAccessor(storeId, this); ReturnValue_t status = modifyData(storeId, &tempData, &constAccessor.size_); constAccessor.constDataPointer = tempData; return ConstAccessorPair(status, std::move(constAccessor)); @@ -159,6 +159,7 @@ inline ReturnValue_t LocalPool::getData(store_address_t storeId ConstStorageAccessor& storeAccessor) { uint8_t* tempData = nullptr; ReturnValue_t status = modifyData(storeId, &tempData, &storeAccessor.size_); + storeAccessor.assignStore(this); storeAccessor.constDataPointer = tempData; return status; } @@ -176,7 +177,7 @@ template inline AccessorPair LocalPool::modifyData( store_address_t storeId) { uint8_t* tempData = nullptr; - StorageAccessor accessor(storeId); + StorageAccessor accessor(storeId, this); ReturnValue_t status = modifyData(storeId, &tempData, &accessor.size_); accessor.constDataPointer = tempData; accessor.assignConstPointer(); @@ -188,6 +189,7 @@ inline ReturnValue_t LocalPool::modifyData( store_address_t storeId, StorageAccessor& storeAccessor) { ReturnValue_t status = modifyData(storeId, &storeAccessor.dataPointer, &storeAccessor.size_); + storeAccessor.assignStore(this); storeAccessor.assignConstPointer(); return status; } diff --git a/storagemanager/StorageAccessor.cpp b/storagemanager/StorageAccessor.cpp index 51b93f11..67693054 100644 --- a/storagemanager/StorageAccessor.cpp +++ b/storagemanager/StorageAccessor.cpp @@ -3,6 +3,12 @@ ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId): storeId(storeId) {} +ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId, + StorageManagerIF* store): + storeId(storeId), store(store) { + internalState = AccessState::ASSIGNED; +} + ConstStorageAccessor::~ConstStorageAccessor() { if(deleteData and store != nullptr) { sif::debug << "deleting store data" << std::endl; @@ -27,6 +33,11 @@ StorageAccessor::StorageAccessor(store_address_t storeId): ConstStorageAccessor(storeId) { } +StorageAccessor::StorageAccessor(store_address_t storeId, + StorageManagerIF* store): + ConstStorageAccessor(storeId, store) { +} + StorageAccessor& StorageAccessor::operator =( StorageAccessor&& other) { // Call the parent move assignment and also assign own member. @@ -97,7 +108,7 @@ void ConstStorageAccessor::print() const { } void ConstStorageAccessor::assignStore(StorageManagerIF* store) { - internalState = AccessState::READ; + internalState = AccessState::ASSIGNED; this->store = store; } diff --git a/storagemanager/StorageAccessor.h b/storagemanager/StorageAccessor.h index da319a97..2be80601 100644 --- a/storagemanager/StorageAccessor.h +++ b/storagemanager/StorageAccessor.h @@ -31,23 +31,7 @@ public: * @param storeId */ ConstStorageAccessor(store_address_t storeId); - - /** - * @brief Move ctor and move assignment allow returning accessors as - * a returnvalue. They prevent resource being free prematurely. - * Refer to: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/ - * move-constructors-and-move-assignment-operators-cpp.md - * @param - * @return - */ - ConstStorageAccessor& operator= (ConstStorageAccessor&&); - ConstStorageAccessor (ConstStorageAccessor&&); - - //! The copy ctor and copy assignemnt should be deleted implicitely - //! according to https://foonathan.net/2019/02/special-member-functions/ - //! but I still deleted them to make it more explicit. (remember rule of 5). - ConstStorageAccessor& operator= (ConstStorageAccessor&) = delete; - ConstStorageAccessor (ConstStorageAccessor&) = delete; + ConstStorageAccessor(store_address_t storeId, StorageManagerIF* store); /** * @brief The destructor in default configuration takes care of @@ -86,6 +70,23 @@ public: store_address_t getId() const; void print() const; + + /** + * @brief Move ctor and move assignment allow returning accessors as + * a returnvalue. They prevent resource being free prematurely. + * Refer to: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/ + * move-constructors-and-move-assignment-operators-cpp.md + * @param + * @return + */ + ConstStorageAccessor& operator= (ConstStorageAccessor&&); + ConstStorageAccessor (ConstStorageAccessor&&); + + //! The copy ctor and copy assignemnt should be deleted implicitely + //! according to https://foonathan.net/2019/02/special-member-functions/ + //! but I still deleted them to make it more explicit. (remember rule of 5). + ConstStorageAccessor& operator= (ConstStorageAccessor&) = delete; + ConstStorageAccessor (ConstStorageAccessor&) = delete; protected: const uint8_t* constDataPointer = nullptr; store_address_t storeId; @@ -96,7 +97,7 @@ protected: enum class AccessState { UNINIT, - READ + ASSIGNED }; //! Internal state for safety reasons. AccessState internalState = AccessState::UNINIT; @@ -109,7 +110,6 @@ protected: * @param */ void assignStore(StorageManagerIF*); - }; @@ -124,6 +124,7 @@ class StorageAccessor: public ConstStorageAccessor { friend class LocalPool; public: StorageAccessor(store_address_t storeId); + StorageAccessor(store_address_t storeId, StorageManagerIF* store); /** * @brief Move ctor and move assignment allow returning accessors as * a returnvalue. They prevent resource being free prematurely. diff --git a/storagemanager/StorageManagerIF.h b/storagemanager/StorageManagerIF.h index b5508da8..559c4b9a 100644 --- a/storagemanager/StorageManagerIF.h +++ b/storagemanager/StorageManagerIF.h @@ -55,6 +55,10 @@ union store_address_t { * Alternative access to the raw value. */ uint32_t raw; + + bool operator==(const store_address_t& other) const { + return raw == other.raw; + } }; /**