ChatPlexSDK-BS 1.0.1-v6.2.0
C++ ChatPlex BeatSaber SDK
Loading...
Searching...
No Matches
ObjectPool.hpp
1#pragma once
2
3#include "IObjectPool.hpp"
4#include "../Utils/Delegate.hpp"
5#include "../ChatPlexSDK.hpp"
6
7#include <vector>
8#include <exception>
9#include <memory>
10
11namespace CP_SDK::Pool {
12
13 namespace _v
14 {
15 using namespace CP_SDK::Utils;
16 }
17
20 template<class t_Type>
21 class ObjectPool : public IObjectPool<t_Type>
22 {
23 CP_SDK_NO_DEF_CTORS(ObjectPool);
24
25 private:
26 std::vector<t_Type> m_Vector;
27 _v::Func<t_Type> m_CreateFunc;
28 _v::Action<t_Type&> m_ActionOnGet;
29 _v::Action<t_Type&> m_ActionOnRelease;
30 _v::Action<t_Type&> m_ActionOnDestroy;
31 int m_MaxSize;
32 int m_CountAll;
33 bool m_CollectionCheck;
34
35 public:
36 using Ptr = std::shared_ptr<ObjectPool<t_Type>>;
37
40 _v::CFuncRef<t_Type> createFunc,
41 _v::CActionRef<t_Type&> actionOnGet,
42 _v::CActionRef<t_Type&> actionOnRelease,
43 _v::CActionRef<t_Type&> actionOnDestroy,
44 bool collectionCheck,
45 int defaultCapacity,
46 int maxSize)
47 : IObjectPool<t_Type>::IObjectPool(CP_SDK_PRIV_TAG_FWD())
48 {
49 if (!createFunc.IsValid())
50 throw std::runtime_error("createFunc is null");
51
52 if (maxSize <= 0)
53 throw std::runtime_error("Max Size must be greater than 0");
54
55 m_CreateFunc = createFunc;
56 m_MaxSize = maxSize;
57 m_ActionOnGet = actionOnGet;
58 m_ActionOnRelease = actionOnRelease;
59 m_ActionOnDestroy = actionOnDestroy;
60 m_CollectionCheck = collectionCheck;
61
62 m_Vector.reserve(maxSize);
63
64 while (defaultCapacity-- > 0)
65 m_Vector.push_back(m_CreateFunc());
66 }
69 {
70 Clear();
71 }
72
75 static Ptr Make(_v::CFuncRef<t_Type> createFunc,
76 _v::CActionRef<t_Type&> actionOnGet = nullptr,
77 _v::CActionRef<t_Type&> actionOnRelease = nullptr,
78 _v::CActionRef<t_Type&> actionOnDestroy = nullptr,
79 bool collectionCheck = true,
80 int defaultCapacity = 10,
81 int maxSize = 100)
82 {
83 return std::make_shared<ObjectPool<t_Type>>(typename IObjectPool<t_Type>::__this_is_private{0},
84 createFunc, actionOnGet, actionOnRelease, actionOnDestroy, collectionCheck, defaultCapacity, maxSize
85 );
86 }
87
88 public:
91 {
92 return m_CountAll - m_Vector.size();
93 }
95 int CountInactive() override
96 {
97 return m_Vector.size();
98 }
99
100 public:
102 t_Type Get() override
103 {
104 t_Type l_Result{};
105
106 if (m_Vector.size() == 0)
107 {
108 l_Result = m_CreateFunc();
109 m_CountAll++;
110 }
111 else
112 {
113 l_Result = m_Vector.back();
114 m_Vector.pop_back();
115 }
116
117 m_ActionOnGet(l_Result);
118 return l_Result;
119 }
122 void Release(t_Type& p_Element) override
123 {
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.");
126
127 m_ActionOnRelease(p_Element);
128
129 if (CountInactive() < m_MaxSize)
130 m_Vector.push_back(const_cast<t_Type&>(p_Element));
131 else
132 m_ActionOnDestroy(p_Element);
133 }
134
135 public:
137 void Clear() override
138 {
139 for (auto& l_Current : m_Vector)
140 m_ActionOnDestroy(l_Current);
141
142 m_Vector.clear();
143
144 m_CountAll = 0;
145 }
146
147 };
148
149}
Object pool interface.
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.
Delegate helper class.
Definition Delegate.hpp:123
Memory management & pools utilities.
Various platform utils like Delegate/Action/Function/Event system.
Definition Delegate.hpp:8