Cogs.Core
ComponentPool.h
1#pragma once
2
3#include "Component.h"
4
5#include "../Memory/MemoryBuffer.h"
6#include "../Reflection/TypeDatabase.h"
7
8namespace Cogs
9{
10 namespace ComponentModel
11 {
14 {
19 {
21 None = 0x0000,
23 AllowGrow = 0x0001,
24 };
25 };
26
37 {
38 public:
43 COGSFOUNDATION_API ComponentPoolBase(const Reflection::TypeId typeId, SizeType capacity, Memory::Allocator * allocator, MemBlockType memType);
44
48 COGSFOUNDATION_API ComponentHandle allocateComponent();
49
53 COGSFOUNDATION_API void deallocateComponent(ComponentHandle handle);
54
57 {
58 return *get(roster[index]);
59 }
60
62 Component * get(size_t index)
63 {
64 return reinterpret_cast<Component *>(store.data() + (index * elementSize));
65 }
66
69 {
70 return get(handle.index);
71 }
72
74 COGSFOUNDATION_API void resize(SizeType capacity);
75
77 SizeType size() const { return next; }
78
80 SizeType capacity() const { return static_cast<SizeType>(roster.size()); }
81
83 COGSFOUNDATION_API SizeType maxSize() const;
84
86 SizeType getComponentIndex(const Component * component) const { return roster[component->getIndex()]; }
87
90
92 bool isSet(const uint32_t flag) const { return (flags & flag) == flag; }
93
94 private:
96
98 SizeType next;
99
103
106
108 size_t elementSize;
109
111 Reflection::TypeId componentType;
112
114 uint32_t flags;
115
117 };
118
122 template<typename ComponentType>
124 {
125 static_assert(sizeof(Component) <= sizeof(ComponentType), "Component type incompatible.");
126
131 {
132 public:
134 typedef std::forward_iterator_tag iterator_category;
135
137 typedef ComponentType value_type;
138
140 typedef size_t difference_type;
141
143 typedef ComponentType * pointer;
144
146 typedef ComponentType & reference;
147
149 ComponentIterator(ComponentPool & pool, SizeType index) : pool(pool), index(index) {}
150
152 ComponentIterator(const ComponentIterator & other) : pool(other.pool), index(other.index) {}
153
155 [[nodiscard]] constexpr bool operator==(const ComponentIterator & other) const { return index == other.index; }
156
158 ComponentIterator operator++() { ComponentIterator t = *this; ++index; return t; }
159
161 ComponentIterator & operator++(int) { ++index; return *this; }
162
164 ComponentType & operator*() { return pool[index]; }
165
167 const ComponentType & operator*() const { return pool[index]; }
168
170 const ComponentType * operator->() const { return &pool[index]; }
171
173
175 ComponentPool & pool;
176
178 SizeType index;
179
181 };
182
186 ComponentPool(Memory::Allocator * allocator, SizeType poolSize, MemBlockType memType = MemBlockType::Bucket) :
187 ComponentPoolBase(Reflection::TypeDatabase::getType<ComponentType>().getTypeId(), poolSize, allocator, memType)
188 {
189 }
190
194 ComponentPool(SizeType poolSize = 16384) :
195 ComponentPoolBase(Reflection::TypeDatabase::getType<ComponentType>().getTypeId(), poolSize, Memory::Allocator::defaultAllocator(), MemBlockType::Bucket)
196 {
197 }
198
201
204
206 ComponentType & operator[](SizeType index)
207 {
208 return *static_cast<ComponentType *>(&ComponentPoolBase::operator[](index));
209 }
210
211 private:
212 friend struct ComponentIterator;
213 };
214 }
215}
Untyped Component pool base.
Definition: ComponentPool.h:37
Component & operator[](SizeType index)
Get the component at the given roster index.
Definition: ComponentPool.h:56
COGSFOUNDATION_API void resize(SizeType capacity)
Resize the pool.
COGSFOUNDATION_API SizeType maxSize() const
Get the maximum size of the pool.
SizeType getComponentIndex(const Component *component) const
Get the index of the given component in the storage pool.
Definition: ComponentPool.h:86
COGSFOUNDATION_API ComponentHandle allocateComponent()
Allocates and initializes a new component using placement new into the backing store.
bool isSet(const uint32_t flag) const
Checks if the given flag is set.
Definition: ComponentPool.h:92
SizeType capacity() const
Get the capacity of the pool, the number of components that can be allocated without resizing the poo...
Definition: ComponentPool.h:80
Component * resolve(const ComponentHandle handle)
Resolve a pointer to the component with the given handle.
Definition: ComponentPool.h:68
COGSFOUNDATION_API void deallocateComponent(ComponentHandle handle)
Deallocates a component from the pool, calling the destructor of the derived component type.
Component * get(size_t index)
Retrieve a pointer to the component at the given index. The pointer is valid until any new allocation...
Definition: ComponentPool.h:62
SizeType size() const
Gets the current size of the pool, the number components currently active.
Definition: ComponentPool.h:77
void setFlag(ComponentPoolFlags::EComponentPoolFlags flag)
Set the given flag.
Definition: ComponentPool.h:89
Base class for Component instances.
Definition: Component.h:143
constexpr ComponentIndex getIndex() const
Get the components pool index. For internal use only.
Definition: Component.h:392
Base allocator implementation.
Definition: Allocator.h:30
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Handle to a Component instance.
Definition: Component.h:67
Contains component pool flags.
Definition: ComponentPool.h:14
EComponentPoolFlags
Contains flags controlling the behavior of a component pool instance.
Definition: ComponentPool.h:19
@ AllowGrow
Allows the component pool to grow on demand.
Definition: ComponentPool.h:23
Custom iterator providing iteration support for component pools.
std::forward_iterator_tag iterator_category
Type of iterator. Can only be used to iterate forward over components in a pool.
ComponentIterator & operator++(int)
Pre increment operator. Moves the iterator to the next component indexed in the roster of the pool.
ComponentType & operator*()
Dereference operator. Returns a reference to the component at the iterators current roster index.
size_t difference_type
Difference type to calculate iterator distance.
ComponentType * pointer
Pointer type to the templated ComponentType.
const ComponentType & operator*() const
Const dereference operator. Returns a const reference to the component at the current roster index.
ComponentIterator(ComponentPool &pool, SizeType index)
Construct a new component iterator on the given pool, starting at the given roster index.
ComponentIterator operator++()
Post increment operator. Moves the iterator to the next component indexed in the roster of the pool.
ComponentType value_type
Value type for dereferencing the iterator is the templated ComponentType.
ComponentType & reference
Reference type to the templated ComponentType.
ComponentIterator(const ComponentIterator &other)
Copy construct a new iterator instance from the given iterator.
const ComponentType * operator->() const
Pointer operator. Returns a pointer to the component at the current roster index.
constexpr bool operator==(const ComponentIterator &other) const
Comparison operator. If the current index of both iterators are equal the iterators are considered eq...
ComponentIterator end()
Returns an iterator to the end of the pool.
ComponentType & operator[](SizeType index)
Access the component at the given index.
ComponentPool(SizeType poolSize=16384)
Construct a component pool instance.
ComponentPool(Memory::Allocator *allocator, SizeType poolSize, MemBlockType memType=MemBlockType::Bucket)
Construct a component pool instance.
ComponentIterator begin()
Returns an iterator to the beginning of the pool.