3#include "IObjectPool.hpp"
4#include "../Utils/Delegate.hpp"
5#include "../ChatPlexSDK.hpp"
21 template<
class t_Type>
27 std::vector<t_Type> m_Vector;
35 bool m_CollectionCheck;
38 using Ptr = std::shared_ptr<MTObjectPool<t_Type>>;
51 if (!createFunc.IsValid())
52 throw std::runtime_error(
"createFunc is null");
55 throw std::runtime_error(
"Max Size must be greater than 0");
57 m_CreateFunc = createFunc;
59 m_ActionOnGet = actionOnGet;
60 m_ActionOnRelease = actionOnRelease;
61 m_ActionOnDestroy = actionOnDestroy;
62 m_CollectionCheck = collectionCheck;
64 m_Vector.reserve(maxSize);
66 while (defaultCapacity-- > 0)
67 m_Vector.push_back(m_CreateFunc());
82 bool collectionCheck =
true,
83 int defaultCapacity = 10,
87 createFunc, actionOnGet, actionOnRelease, actionOnDestroy, collectionCheck, defaultCapacity, maxSize
95 return m_CountAll - m_Vector.size();
100 return m_Vector.size();
111 std::lock_guard<std::mutex> l_Lock(m_Mutex);
113 if (m_Vector.size() == 0)
115 l_Result = m_CreateFunc();
120 l_Result = m_Vector.back();
125 m_ActionOnGet(l_Result);
134 std::lock_guard<std::mutex> l_Lock(m_Mutex);
136 if (m_CollectionCheck && m_Vector.size() > 0 && std::find(m_Vector.begin(), m_Vector.end(), p_Element) != m_Vector.end())
137 throw std::runtime_error(
"Trying to release an object that has already been released to the pool.");
139 m_ActionOnRelease(p_Element);
142 m_Vector.push_back(
const_cast<t_Type&
>(p_Element));
144 m_ActionOnDestroy(p_Element);
154 std::lock_guard<std::mutex> l_Lock(m_Mutex);
156 for (
auto& l_Current : m_Vector)
157 m_ActionOnDestroy(l_Current);
static Logging::ILogger * Logger()
Logger instance.
Vector based object pool.
int CountInactive() override
Released element.
void Release(t_Type &p_Element) override
Release an element.
t_Type Get() override
Simple get.
void Clear() override
Clear the object pool.
static Ptr Make(_v::CFuncRef< t_Type > createFunc, _v::CActionRef< t_Type & > actionOnGet=nullptr, _v::CActionRef< t_Type & > actionOnRelease=nullptr, _v::CActionRef< t_Type & > actionOnDestroy=nullptr, bool collectionCheck=true, int defaultCapacity=10, int maxSize=100)
Constructor.
MTObjectPool(const typename IObjectPool< t_Type >::__this_is_private &__pvTag, _v::CFuncRef< t_Type > createFunc, _v::CActionRef< t_Type & > actionOnGet, _v::CActionRef< t_Type & > actionOnRelease, _v::CActionRef< t_Type & > actionOnDestroy, bool collectionCheck, int defaultCapacity, int maxSize)
Constructor.
~MTObjectPool()
Destructor.
int CountActive()
Active elements.
Memory management & pools utilities.
Various platform utils like Delegate/Action/Function/Event system.