Cogs.Core
Pool.h
1#pragma once
2
3#include "PoolBase.h"
4
5namespace Cogs
6{
7 namespace Collections
8 {
15 template<typename ElementType>
16 struct Pool
17 {
21 Pool(MemBlockType memType = MemBlockType::Bucket) : Pool(1024, 1024, memType) {}
22
31 Pool(ElementOffset capacity, ElementOffset pageSize = 128, MemBlockType memType = MemBlockType::Bucket) : Pool(capacity, pageSize, Memory::Allocator::defaultAllocator(), memType) {}
32
42 Pool(ElementOffset capacity, ElementOffset pageSize, Memory::Allocator * allocator, MemBlockType memType = MemBlockType::Bucket) : base(sizeof(ElementType), capacity, pageSize, allocator, memType) {}
43
45 Pool(Pool &&) noexcept = default;
46
48 Pool & operator=(Pool &&) noexcept = default;
49
51 Pool(const Pool &) = delete;
52
54 Pool & operator=(const Pool &) = delete;
55
59 void resize(size_t capacity) { base.resize(capacity); }
60
68 template<typename... ARGS>
69 ElementType* create(ARGS&&... args)
70 {
71 return new (base.allocate()) ElementType(std::forward<ARGS>(args)...);
72 }
73
82 void destroy(ElementType * element)
83 {
84 element->~ElementType();
85
86 base.deallocate(element);
87 }
88
92 size_t size() const { return base.size(); }
93
98 size_t getCapacity() const { return base.getCapacity(); }
99
105 size_t getPageSize() const { return base.getPageSize(); }
106
114 ElementHandle getHandle(ElementType * element) const { return base.getHandle(element); }
115
126 ElementType * operator[](ElementHandle handle)
127 {
128 return static_cast<ElementType *>(base.getElement(handle));
129 }
130
134 bool isValid(ElementHandle handle) const { return base.isValid(handle); }
135
140 bool isAllocated(ElementHandle handle) const { return base.isAllocated(handle); }
141
142 private:
144 PoolBase base;
146 };
147 }
148}
Base allocator implementation.
Definition: Allocator.h:30
uint16_t ElementOffset
Offset type used to index elements in resource pools.
Definition: PoolBase.h:13
uint32_t ElementHandle
Handle type for elements.
Definition: PoolBase.h:16
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Base untyped pool.
Definition: PoolBase.h:22
Pool used to store elements of ElementType.
Definition: Pool.h:17
Pool(ElementOffset capacity, ElementOffset pageSize=128, MemBlockType memType=MemBlockType::Bucket)
Create a pool with the given capacity and page sizes given in number of elements.
Definition: Pool.h:31
void resize(size_t capacity)
Resize the pool to the given capacity.
Definition: Pool.h:59
Pool(Pool &&) noexcept=default
Defaulted move constructor.
Pool(ElementOffset capacity, ElementOffset pageSize, Memory::Allocator *allocator, MemBlockType memType=MemBlockType::Bucket)
Create a pool with the given capacity and page sizes given in number of elements.
Definition: Pool.h:42
bool isAllocated(ElementHandle handle) const
Checks if the given handle is a valid handle to an allocated element.
Definition: Pool.h:140
bool isValid(ElementHandle handle) const
Checks if the given handle is a valid handle to an allocated or free element.
Definition: Pool.h:134
ElementType * operator[](ElementHandle handle)
Lookup an element using the handle given, using constant time.
Definition: Pool.h:126
ElementType * create(ARGS &&... args)
Allocate and initialize a new element from the pool passing any arguments to the constructor of the n...
Definition: Pool.h:69
ElementHandle getHandle(ElementType *element) const
Get a handle for the given element.
Definition: Pool.h:114
Pool(MemBlockType memType=MemBlockType::Bucket)
Create a pool with default capacity and page size.
Definition: Pool.h:21
size_t getPageSize() const
Get the page size in number of elements.
Definition: Pool.h:105
size_t getCapacity() const
Get the capacity of the pool, i.e the number of elements that may be allocated before any additional ...
Definition: Pool.h:98
size_t size() const
Get the size of the pool, i.e the number of created elements.
Definition: Pool.h:92
void destroy(ElementType *element)
Free and destroy the given element in the pool.
Definition: Pool.h:82