3#include "IObjectPool.hpp"
4#include "../Utils/Delegate.hpp"
5#include "../ChatPlexSDK.hpp"
20 template<
class t_Type>
26 std::vector<t_Type> m_Vector;
33 bool m_CollectionCheck;
36 using Ptr = std::shared_ptr<ObjectPool<t_Type>>;
49 if (!createFunc.IsValid())
50 throw std::runtime_error(
"createFunc is null");
53 throw std::runtime_error(
"Max Size must be greater than 0");
55 m_CreateFunc = createFunc;
57 m_ActionOnGet = actionOnGet;
58 m_ActionOnRelease = actionOnRelease;
59 m_ActionOnDestroy = actionOnDestroy;
60 m_CollectionCheck = collectionCheck;
62 m_Vector.reserve(maxSize);
64 while (defaultCapacity-- > 0)
65 m_Vector.push_back(m_CreateFunc());
79 bool collectionCheck =
true,
80 int defaultCapacity = 10,
84 createFunc, actionOnGet, actionOnRelease, actionOnDestroy, collectionCheck, defaultCapacity, maxSize
92 return m_CountAll - m_Vector.size();
97 return m_Vector.size();
106 if (m_Vector.size() == 0)
108 l_Result = m_CreateFunc();
113 l_Result = m_Vector.back();
117 m_ActionOnGet(l_Result);
124 if (m_CollectionCheck && m_Vector.size() > 0 && std::find(m_Vector.begin(), m_Vector.end(), p_Element) != m_Vector.end())
125 throw std::runtime_error(
"Trying to release an object that has already been released to the pool.");
127 m_ActionOnRelease(p_Element);
130 m_Vector.push_back(
const_cast<t_Type&
>(p_Element));
132 m_ActionOnDestroy(p_Element);
139 for (
auto& l_Current : m_Vector)
140 m_ActionOnDestroy(l_Current);
Vector based 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.
ObjectPool(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.
t_Type Get() override
Simple get.
void Clear() override
Clear the object pool.
void Release(t_Type &p_Element) override
Release an element.
int CountInactive() override
Released element.
int CountActive()
Active elements.
Memory management & pools utilities.
Various platform utils like Delegate/Action/Function/Event system.