From aeeef53508627aa802d14ed9b45a93689171cac6 Mon Sep 17 00:00:00 2001 From: Steffen Gaisser Date: Wed, 30 Sep 2020 14:34:02 +0200 Subject: [PATCH 1/2] Added Documentation to placement factory --- container/PlacementFactory.h | 42 +++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/container/PlacementFactory.h b/container/PlacementFactory.h index 05538478..529c7804 100644 --- a/container/PlacementFactory.h +++ b/container/PlacementFactory.h @@ -3,26 +3,62 @@ #include "../storagemanager/StorageManagerIF.h" #include - +/** + * The Placement Factory is used to create objects at runtime in a specific pool. + * In general this should be avoided and is should only be used if you know what you are doing. + * You are not allowed to use this container with a type that allocates memory internally like ArrayList. + * + * You have to check the returned pointer against nullptr! + * + * A backend of Type StorageManagerIF must be given as a place to store the new objects. + * Objects must be destroyed by the user with "destroy"! Otherwise the pool will not be cleared. + * + * + * + * The concept is based on the placement new operator. + * + * @warning Do not use with any Type that allocates memory internally! + * @ingroup container + */ class PlacementFactory { public: PlacementFactory(StorageManagerIF* backend) : dataBackend(backend) { } + + /*** + * Generates an object of type T in the backend storage. + * + * @warning Do not use with any Type that allocates memory internally! + * + * @tparam T Type of Object + * @param args Constructor Arguments to be passed + * @return A pointer to the new object or a nullptr in case of failure + */ template T* generate(Args&&... args) { store_address_t tempId; - uint8_t* pData = NULL; + uint8_t* pData = nullptr; ReturnValue_t result = dataBackend->getFreeElement(&tempId, sizeof(T), &pData); if (result != HasReturnvaluesIF::RETURN_OK) { - return NULL; + return nullptr; } T* temp = new (pData) T(std::forward(args)...); return temp; } + /*** + * Function to destroy the object allocated with generate and free space in backend. + * This must be called by the user. + * + * @param thisElement Element to be destroyed + * @return RETURN_OK if the element was destroyed, different errors on failure + */ template ReturnValue_t destroy(T* thisElement) { + if (thisElement == nullptr){ + return HasReturnvaluesIF::RETURN_FAILED; + } //Need to call destructor first, in case something was allocated by the object (shouldn't do that, however). thisElement->~T(); uint8_t* pointer = (uint8_t*) (thisElement); From 2f61bd0a0f5ac7f9252e9d18b60253fbf39e53f5 Mon Sep 17 00:00:00 2001 From: Steffen Gaisser Date: Wed, 30 Sep 2020 14:38:46 +0200 Subject: [PATCH 2/2] A few typos and more docu --- container/PlacementFactory.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/container/PlacementFactory.h b/container/PlacementFactory.h index 529c7804..a0aebb7d 100644 --- a/container/PlacementFactory.h +++ b/container/PlacementFactory.h @@ -5,16 +5,16 @@ #include /** * The Placement Factory is used to create objects at runtime in a specific pool. - * In general this should be avoided and is should only be used if you know what you are doing. + * In general, this should be avoided and it should only be used if you know what you are doing. * You are not allowed to use this container with a type that allocates memory internally like ArrayList. * - * You have to check the returned pointer against nullptr! + * Also, you have to check the returned pointer in generate against nullptr! * * A backend of Type StorageManagerIF must be given as a place to store the new objects. + * Therefore ThreadSafety is only provided by your StorageManager Implementation. + * * Objects must be destroyed by the user with "destroy"! Otherwise the pool will not be cleared. * - * - * * The concept is based on the placement new operator. * * @warning Do not use with any Type that allocates memory internally!