31 template <
class T =
int>
39 std::vector<uPtrResource>
_pool;
43 _pool.emplace_back(std::move(p));
53 pPool = std::make_shared<Pool>();
59 return pPool->_pool.size();
72 std::weak_ptr<Pool> pool;
79 _ptr = std::move(n_ptr);
83 _ptr = std::move(R._ptr);
84 pool = std::move(R.pool);
88 _ptr = std::move(R._ptr);
89 pool = std::move(R.pool);
94 operator bool()
const {
return bool(_ptr); }
100 auto poolLocked = pool.lock();
101 if (poolLocked && _ptr)
102 poolLocked->_pool.emplace_back(std::move(_ptr));
108 template <
class... _CtorArgs>
109 void resize(
size_t N, _CtorArgs &&...__ctorArgs)
111 pPool->_pool.clear();
113 while (
pPool->_pool.size() <
N)
115 uPtrResource p = std::make_unique<T>(std::forward<_CtorArgs>(__ctorArgs)...);
116 pPool->_pool.emplace_back(std::move(p));
121 template <
class TFInit,
class... _CtorArgs>
122 void resizeInit(
size_t N, TFInit &&FInit, _CtorArgs &&...__ctorArgs)
124 pPool->_pool.clear();
126 while (
pPool->_pool.size() <
N)
128 uPtrResource p = std::make_unique<T>(std::forward<_CtorArgs>(__ctorArgs)...);
130 pPool->_pool.emplace_back(std::move(p));
137 if (
pPool->_pool.size())
140 pPool->_pool.pop_back();
147 template <
class... _CtorArgs>
150 if (
pPool->_pool.size())
153 pPool->_pool.pop_back();
156 uPtrResource p = std::make_unique<T>(std::forward<_CtorArgs>(__ctorArgs)...);
161 template <
class TFInit,
class... _CtorArgs>
164 if (
pPool->_pool.size())
167 pPool->_pool.pop_back();
170 uPtrResource p = std::make_unique<T>(std::forward<_CtorArgs>(__ctorArgs)...);
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
#define DNDS_assert_info(expr, info)
Debug-only assertion with an extra std::string info message.
RAII handle returned by ObjectPoolget (and friends).
ObjectPoolAllocated(uPtrResource n_ptr, std::shared_ptr< Pool > &pPool)
const T & operator*() const
void operator=(ObjectPoolAllocated &&R) noexcept
uPtrResource & operator->()
Arrow-access to the underlying unique_ptr (lets callers reach through it to T).
ObjectPoolAllocated(ObjectPoolAllocated &&R) noexcept
Generic object pool: caches unique_ptr<T> instances and hands them out with RAII return-on-destructio...
void resize(size_t N, _CtorArgs &&...__ctorArgs)
Pre-allocate N objects, forwarding __ctorArgs to each ctor. Clears any previously pooled instances.
ObjectPoolAllocated get()
Take an object out of the pool. Returns an empty handle if exhausted.
size_t size()
Number of objects currently available (not checked out).
ObjectPoolAllocated getAlloc(_CtorArgs &&...__ctorArgs)
Like get, but allocates a brand-new object if the pool is empty.
std::unique_ptr< T > uPtrResource
void resizeInit(size_t N, TFInit &&FInit, _CtorArgs &&...__ctorArgs)
Like resize but calls FInit(obj) on each newly-created object.
ObjectPool()
Construct an empty pool; populate with resize.
ObjectPoolAllocated getAllocInit(TFInit &&FInit, _CtorArgs &&...__ctorArgs)
Like getAlloc, but additionally runs FInit(obj) on newly-allocated objects.
std::shared_ptr< Pool > pPool
Shared pointer to the underlying storage.
the host side operators are provided as implemented
Internal storage shared among all handles.
void recycle(uPtrResource p)
Push an object back onto the free-list.
std::vector< uPtrResource > _pool