Cogs.Core
ComponentDataPool.h
1#pragma once
2
3#include "Component.h"
4
5#include "../Memory/MemoryBuffer.h"
6
7namespace Cogs
8{
9 namespace ComponentModel
10 {
24 {
25 public:
32 COGSFOUNDATION_API ComponentDataPoolBase(SizeType capacity, size_t elementSize, MemBlockType memType = MemBlockType::Bucket);
33
42 COGSFOUNDATION_API void allocate(ComponentHandle handle);
43
45 COGSFOUNDATION_API void deallocate(ComponentHandle handle);
46
48 void * operator[](SizeType index)
49 {
50 debug_assert(index < capacity && "Element index out of bounds.");
51 debug_assert(index * elementSize < store.size() && "Storage not allocated for specified index.");
52
53 return store.data() + elementSize * index;
54 }
55
57 const void * operator[](SizeType index) const
58 {
59 debug_assert(index < capacity && "Element index out of bounds.");
60 debug_assert(index * elementSize < store.size() && "Storage not allocated for specified index.");
61
62 return store.data() + elementSize * index;
63 }
64
65 private:
68 size_t elementSize;
69 SizeType capacity;
71 };
72
81 template<typename DataType>
83 {
84 public:
89
95 ComponentDataPool(SizeType capacity, MemBlockType memType = MemBlockType::Bucket) : ComponentDataPoolBase(capacity, sizeof(DataType), memType) {}
96
104 {
105 allocate(handle);
106
107 auto data = reinterpret_cast<DataType *>(ComponentDataPoolBase::operator[](handle.index));
108
109 new (data) DataType();
110 }
111
118 {
119 auto data = reinterpret_cast<DataType *>(ComponentDataPoolBase::operator[](handle.index));
120
121 data->~DataType();
122
123 deallocate(handle);
124 }
125
132 DataType & operator[](SizeType index)
133 {
134 return *reinterpret_cast<DataType *>(ComponentDataPoolBase::operator[](index));
135 }
136
143 const DataType & operator[](SizeType index) const
144 {
145 return *reinterpret_cast<const DataType *>(ComponentDataPoolBase::operator[](index));
146 }
147 };
148 }
149}
const void * operator[](SizeType index) const
Retrieve a const pointer to the data for the element at the given index.
COGSFOUNDATION_API void allocate(ComponentHandle handle)
Allocate a new data element in the pool at the index used by the given component handle.
void * operator[](SizeType index)
Retrieve a pointer to the data for the element at the given index.
COGSFOUNDATION_API void deallocate(ComponentHandle handle)
Deallocate the data element in the pool at the index used by the given component handle.
Typed data pool for storing data in parallel to a component pool.
void create(ComponentHandle handle)
Create a new instance of DataType.
ComponentDataPool(SizeType capacity, MemBlockType memType=MemBlockType::Bucket)
Constructs a data pool with the given capacity.
DataType & operator[](SizeType index)
Retrieve a reference to the element at the given index.
const DataType & operator[](SizeType index) const
Retrieve a const reference to the element at the given index.
void destroy(ComponentHandle handle)
Destroy the instance of DataType at the index used by the given handle.
ComponentDataPool()
Constructs a data pool with default capacity.
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