ChatPlexSDK-BS 1.0.1-v6.2.0
C++ ChatPlex BeatSaber SDK
Loading...
Searching...
No Matches
MTObjectPool.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#include <mutex>
11
12namespace CP_SDK::Pool {
13
14 namespace _v
15 {
16 using namespace CP_SDK::Utils;
17 }
18
21 template<class t_Type>
22 class MTObjectPool : public IObjectPool<t_Type>
23 {
24 CP_SDK_NO_DEF_CTORS(MTObjectPool);
25
26 private:
27 std::vector<t_Type> m_Vector;
28 std::mutex m_Mutex;
29 _v::Func<t_Type> m_CreateFunc;
30 _v::Action<t_Type&> m_ActionOnGet;
31 _v::Action<t_Type&> m_ActionOnRelease;
32 _v::Action<t_Type&> m_ActionOnDestroy;
33 int m_MaxSize;
34 int m_CountAll;
35 bool m_CollectionCheck;
36
37 public:
38 using Ptr = std::shared_ptr<MTObjectPool<t_Type>>;
39
42 _v::CFuncRef<t_Type> createFunc,
43 _v::CActionRef<t_Type&> actionOnGet,
44 _v::CActionRef<t_Type&> actionOnRelease,
45 _v::CActionRef<t_Type&> actionOnDestroy,
46 bool collectionCheck,
47 int defaultCapacity,
48 int maxSize)
49 : IObjectPool<t_Type>::IObjectPool(CP_SDK_PRIV_TAG_FWD())
50 {
51 if (!createFunc.IsValid())
52 throw std::runtime_error("createFunc is null");
53
54 if (maxSize <= 0)
55 throw std::runtime_error("Max Size must be greater than 0");
56
57 m_CreateFunc = createFunc;
58 m_MaxSize = maxSize;
59 m_ActionOnGet = actionOnGet;
60 m_ActionOnRelease = actionOnRelease;
61 m_ActionOnDestroy = actionOnDestroy;
62 m_CollectionCheck = collectionCheck;
63
64 m_Vector.reserve(maxSize);
65
66 while (defaultCapacity-- > 0)
67 m_Vector.push_back(m_CreateFunc());
68 }
71 {
72 ChatPlexSDK::Logger()->Error(u"~MTObjectPool");
73 Clear();
74 }
75
78 static Ptr Make(_v::CFuncRef<t_Type> createFunc,
79 _v::CActionRef<t_Type&> actionOnGet = nullptr,
80 _v::CActionRef<t_Type&> actionOnRelease = nullptr,
81 _v::CActionRef<t_Type&> actionOnDestroy = nullptr,
82 bool collectionCheck = true,
83 int defaultCapacity = 10,
84 int maxSize = 100)
85 {
86 return std::make_shared<MTObjectPool<t_Type>>(typename IObjectPool<t_Type>::__this_is_private{0},
87 createFunc, actionOnGet, actionOnRelease, actionOnDestroy, collectionCheck, defaultCapacity, maxSize
88 );
89 }
90
91 public:
94 {
95 return m_CountAll - m_Vector.size();
96 }
98 int CountInactive() override
99 {
100 return m_Vector.size();
101 }
102
103 public:
105 t_Type Get() override
106 {
107 t_Type l_Result{};
108
109 //lock (m_Stack)
110 {
111 std::lock_guard<std::mutex> l_Lock(m_Mutex);
112
113 if (m_Vector.size() == 0)
114 {
115 l_Result = m_CreateFunc();
116 m_CountAll++;
117 }
118 else
119 {
120 l_Result = m_Vector.back();
121 m_Vector.pop_back();
122 }
123 }
124
125 m_ActionOnGet(l_Result);
126 return l_Result;
127 }
130 void Release(t_Type& p_Element) override
131 {
132 //lock (m_Stack)
133 {
134 std::lock_guard<std::mutex> l_Lock(m_Mutex);
135
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.");
138
139 m_ActionOnRelease(p_Element);
140
141 if (CountInactive() < m_MaxSize)
142 m_Vector.push_back(const_cast<t_Type&>(p_Element));
143 else
144 m_ActionOnDestroy(p_Element);
145 }
146 }
147
148 public:
150 void Clear() override
151 {
152 //lock (m_Stack)
153 {
154 std::lock_guard<std::mutex> l_Lock(m_Mutex);
155
156 for (auto& l_Current : m_Vector)
157 m_ActionOnDestroy(l_Current);
158
159 m_Vector.clear();
160 }
161
162 m_CountAll = 0;
163 }
164
165 };
166
167}
static Logging::ILogger * Logger()
Logger instance.
Object pool interface.
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.
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