3#include "Foundation/Collections/Pool.h"
9#pragma warning(disable:4512)
17 template<
typename ResourceType>
32 uint16_t debugFlags = 0;
42 template<
typename ResourceType>
47 typedef std::forward_iterator_tag iterator_category;
65 PoolIterator operator++() { PoolIterator t(current); current = current->
next;
return t; }
66 PoolIterator & operator++(
int) { current = current->
next;
return *
this; }
68 [[nodiscard]]
constexpr bool operator==(
const PoolIterator & other)
const {
return current == other.current; }
79 WrapperType * current;
85 template<
typename HandleType,
typename ResourceType>
91 constexpr static bool SafeDereference() {
return true; }
93 PoolStoragePolicy(MemBlockType memType = MemBlockType::Rendering) : pool(memType) {}
99 HandleType allocate(ResourceType && resource,
bool persistent)
101 auto r = pool.create();
105 r->resource = std::move(resource);
108 r->next = allocatedHead;
117 auto handle = getHandle(r->resource);
119 if (persistent) pin(handle);
124 HandleType allocate(
const ResourceType & resource,
bool persistent)
126 auto r = pool.create();
130 r->resource = resource;
133 r->next = allocatedHead;
142 auto handle = getHandle(r->resource);
144 if (persistent) pin(handle);
149 void deallocate(
const HandleType & handle)
151 auto r =
reinterpret_cast<WrapperType *
>(handle.handle);
154 assert(r->debugFlags == 0 &&
"Resource already deallocated.");
160 if (r->next) r->next->prev = r->prev;
161 if (r->prev) r->prev->next = r->next;
162 if (r == allocatedHead) allocatedHead = r->
next;
169 bool hasResource(
const HandleType & handle)
const
171 return HandleIsValid(handle);
174 ResourceType & operator[](
const HandleType & handle)
176 auto wrapper =
reinterpret_cast<WrapperType *
>(handle.handle);
181 void clear(
bool force =
false)
183 auto next = allocatedHead;
188 if (!current->pinned || force) {
189 deallocate(getHandle(current->resource));
194 void pin(
const HandleType & handle)
201 bool pinned(
const HandleType & handle)
const {
return reinterpret_cast<WrapperType *
>(handle.handle)->pinned; }
206 size_t size()
const {
return count; }
208 HandleType getHandle(
const ResourceType & r) {
return HandleType(
reinterpret_cast<int64_t
>(getWrapperPointer(r))); }
210 static WrapperType * getWrapperPointer(
const ResourceType & r) {
return reinterpret_cast<WrapperType *
>(
reinterpret_cast<intptr_t
>(&r) -
sizeof(ResourceType *) * 2); }
227 void lock() { m.lock(); }
228 void unlock() { m.unlock(); }
237 template<
typename HandleType,
typename StorageType>
238 static void verify(
const HandleType & handle,
const StorageType & storage) { assert(storage.hasResource(handle) &&
"Resource handle does not exist."); }
240 template<
typename HandleType,
typename StorageType>
241 static void verify(
const HandleType &,
const StorageType &) {}
Base allocator implementation.
uint16_t ElementOffset
Offset type used to index elements in resource pools.
Contains all Cogs related functionality.
Pool used to store elements of ElementType.
Iterator type for iterating over resource pools accessing the actual wrapped resource instances.
ResourceType * pointer
Pointer type to the templated ResourceType.
ResourceType & reference
Reference type to the templated ResourceType.
size_t difference_type
Difference type to calculate iterator distance.
ResourceType value_type
Value type for dereferencing the iterator is the templated ResourceType.
Storage policy storing all resources in a pool, reusing resource memory when possible.
Provides a wrapper around resources for pool storage with intrusive linked list pointers added.
ResourceWrapper * prev
Stores a pointer to the next active element when allocated.
bool pinned
If the wrapped resource is pinned and should be persistently allocated.
ResourceType resource
Wrapped resource.