2016-06-15 23:48:41 +02:00
|
|
|
#ifndef FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_
|
|
|
|
#define FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_
|
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "../storagemanager/StorageManagerIF.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
#include <utility>
|
2020-09-30 14:34:02 +02:00
|
|
|
/**
|
|
|
|
* The Placement Factory is used to create objects at runtime in a specific pool.
|
2020-09-30 14:38:46 +02:00
|
|
|
* In general, this should be avoided and it should only be used if you know what you are doing.
|
2020-09-30 14:34:02 +02:00
|
|
|
* You are not allowed to use this container with a type that allocates memory internally like ArrayList.
|
|
|
|
*
|
2020-09-30 14:38:46 +02:00
|
|
|
* Also, you have to check the returned pointer in generate against nullptr!
|
2020-09-30 14:34:02 +02:00
|
|
|
*
|
|
|
|
* A backend of Type StorageManagerIF must be given as a place to store the new objects.
|
2020-09-30 14:38:46 +02:00
|
|
|
* Therefore ThreadSafety is only provided by your StorageManager Implementation.
|
2020-09-30 14:34:02 +02:00
|
|
|
*
|
2020-09-30 14:38:46 +02:00
|
|
|
* Objects must be destroyed by the user with "destroy"! Otherwise the pool will not be cleared.
|
2020-09-30 14:34:02 +02:00
|
|
|
*
|
|
|
|
* The concept is based on the placement new operator.
|
|
|
|
*
|
|
|
|
* @warning Do not use with any Type that allocates memory internally!
|
|
|
|
* @ingroup container
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
class PlacementFactory {
|
|
|
|
public:
|
|
|
|
PlacementFactory(StorageManagerIF* backend) :
|
|
|
|
dataBackend(backend) {
|
|
|
|
}
|
2020-09-30 14:34:02 +02:00
|
|
|
|
|
|
|
/***
|
|
|
|
* 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
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
template<typename T, typename ... Args>
|
|
|
|
T* generate(Args&&... args) {
|
|
|
|
store_address_t tempId;
|
2020-09-30 14:34:02 +02:00
|
|
|
uint8_t* pData = nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
ReturnValue_t result = dataBackend->getFreeElement(&tempId, sizeof(T),
|
|
|
|
&pData);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
2020-09-30 14:34:02 +02:00
|
|
|
return nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
T* temp = new (pData) T(std::forward<Args>(args)...);
|
|
|
|
return temp;
|
|
|
|
}
|
2020-09-30 14:34:02 +02:00
|
|
|
/***
|
|
|
|
* 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
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
template<typename T>
|
|
|
|
ReturnValue_t destroy(T* thisElement) {
|
2020-09-30 14:34:02 +02:00
|
|
|
if (thisElement == nullptr){
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
//Need to call destructor first, in case something was allocated by the object (shouldn't do that, however).
|
|
|
|
thisElement->~T();
|
2016-06-15 23:48:41 +02:00
|
|
|
uint8_t* pointer = (uint8_t*) (thisElement);
|
|
|
|
return dataBackend->deleteData(pointer, sizeof(T));
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
StorageManagerIF* dataBackend;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_ */
|