21#ifndef HEDGEHOG_STATIC_MEMORY_MANAGER_H_
22#define HEDGEHOG_STATIC_MEMORY_MANAGER_H_
26#include "../managed_memory.h"
37template<
class T,
class ...Args>
39 static_assert(std::is_base_of_v<ManagedMemory, T>,
"The type managed by the StaticMemoryManager should derive from hh::ManagedMemory.");
40 static_assert(std::is_constructible_v<T, Args...>,
"The type managed by the StaticMemoryManager should be constructible with Args type(s).");
65 std::shared_ptr<ManagedMemory> managedMemory =
nullptr;
66 managedMemory = this->
pool()->pop_front();
67 managedMemory->preProcess();
74 std::shared_ptr<AbstractMemoryManager>
copy()
override {
88 void recycleMemory(std::shared_ptr<ManagedMemory>
const &managedMemory)
final {
90 managedMemory->postProcess();
91 if (managedMemory->canBeRecycled()) {
92 managedMemory->clean();
93 this->
pool()->push_back(managedMemory);
104 initialize(std::make_index_sequence<
sizeof...(Args)>());
112 template<
size_t... Is>
115 this->
pool()->begin(), this->
pool()->end(),
116 [
this](std::shared_ptr<ManagedMemory> &emptyShared) {
117 emptyShared = std::make_shared<T>(std::get<Is>(
args_)...);
118 emptyShared->memoryManager(
this);
125 [[nodiscard]] std::string
managedType() const final {
return hh::tool::typeToStr<T>(); }
std::mutex memoryManagerMutex_
Mutex for user interface.
bool isInitialized() const
Initialized flag accessor.
size_t capacity() const
Capacity accessor.
std::unique_ptr< tool::Pool< ManagedMemory > > const & pool() const
Inside pool accessor.
void initialized()
Flag the memory manager has initialized.
void initialize() final
Initialize the memory manager.
virtual void initializeMemoryManager()
User-definable initialization step for a memory manager.
void recycleMemory(std::shared_ptr< ManagedMemory > const &managedMemory) final
Recycling mechanism for managed memory.
~StaticMemoryManager() override=default
Default destructor.
std::shared_ptr< AbstractMemoryManager > copy() override
Default copy method.
std::tuple< Args... > args_
Values to pass to the constructor.
std::string managedType() const final
Getter to real managed type as string.
StaticMemoryManager(size_t const &capacity, Args ... args)
Constructor to a static memory manager.
std::shared_ptr< ManagedMemory > getManagedMemory() final
Get a managed memory from the pool.
void initialize(std::index_sequence< Is... >)
Initialize implementation using the tuple of arguments stored.
StaticMemoryManager(StaticMemoryManager< T, Args... > const &rhs)
Copy constructor.