Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ObjectPool.inl
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the Software
14is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_OBJECT_POOL_INL_
34#define DVD_OBJECT_POOL_INL_
35
36namespace Divide {
37
38template<typename T, size_t N, bool allowResize>
40{
41 _ids.resize(N, PoolHandle{});
42 _pool.resize(N, nullptr);
43}
44
45template<typename T, size_t N, bool allowResize>
47{
48 SharedLock<SharedMutex> r_lock(_poolLock);
49 if (_ids[handle._id - 1]._generation == handle._generation)
50 {
51 return _pool[handle._id - 1];
52 }
53
54 return nullptr;
55}
56
57template<typename T, size_t N, bool allowResize>
58template<typename... Args>
60{
61 T* obj = new T(FWD(args)...);
62 return registerExisting(*obj);
63}
64
65template<typename T, size_t N, bool allowResize>
67{
68 T* obj = find(handle);
69 if ( obj != nullptr )
70 {
71 delete obj;
72 }
73 unregisterExisting(handle);
74}
75
76template<typename T, size_t N, bool allowResize>
78{
79 LockGuard<SharedMutex> w_lock( _poolLock );
80 return registerExistingInternal(object, false);
81}
82
83template<typename T, size_t N, bool allowResize>
85{
86 for (size_t i = 0; i < _ids.size(); ++i)
87 {
88 PoolHandle& handle = _ids[i];
89 if (handle._id == 0)
90 {
91 _pool[i] = &object;
92 handle._id = to_U16(i) + 1;
93 return handle;
94 }
95 }
96
97 if (!allowResize || retry)
98 {
100 return {};
101 }
102
103 _ids.resize( _ids.size() * 2, PoolHandle{} );
104 _pool.resize( _pool.size() * 2, nullptr );
105
106 return registerExistingInternal( object, true );
107}
108
109template<typename T, size_t N, bool allowResize>
111{
112 LockGuard<SharedMutex> w_lock(_poolLock);
113 PoolHandle& it = _ids[handle._id - 1];
114 if (it._generation == handle._generation)
115 {
116 _pool[handle._id - 1] = nullptr;
117 it._id = 0;
118 ++it._generation;
119 return;
120 }
121
123}
124
125} //namespace Divide
126
127namespace eastl
128{
129 template <>
130 struct hash<Divide::PoolHandle>
131 {
132 size_t operator()(const Divide::PoolHandle& x) const noexcept
133 {
134 size_t h = 17;
135 Divide::Util::Hash_combine(h, x._generation, x._id);
136 return h;
137 }
138 };
139} //namespace eastl
140
141#endif //DVD_OBJECT_POOL_INL_
#define FWD(...)
#define DIVIDE_UNEXPECTED_CALL()
#define FORCE_INLINE
PoolHandle registerExisting(T &object)
Definition: ObjectPool.inl:77
T * find(PoolHandle handle) const
Definition: ObjectPool.inl:46
void unregisterExisting(PoolHandle handle)
Definition: ObjectPool.inl:110
void deallocate(PoolHandle handle)
Definition: ObjectPool.inl:66
PoolHandle registerExistingInternal(T &object, bool retry)
Definition: ObjectPool.inl:84
PoolHandle allocate(Args... args)
Definition: ObjectPool.inl:59
void Hash_combine(size_t &seed, const T &v, const Rest &... rest) noexcept
a la Boost
Definition: MathHelper.inl:799
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::lock_guard< mutex > LockGuard
Definition: SharedMutex.h:55
constexpr U16 to_U16(const T value)
std::shared_lock< mutex > SharedLock
Definition: SharedMutex.h:49
size_t operator()(const Divide::PoolHandle &x) const noexcept
Definition: ObjectPool.inl:132