CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
ComponentStore.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
13
17
20
21#include <cassert>
22#include <type_traits>
23
24namespace CeresEngine {
25
29 protected:
31 size_t mSize = 0;
32
34 size_t mCapacity = 0;
35
37 size_t mChunkSize;
38
41
44
45 public:
47
50
51 public:
62
66
71
81
91
92 public:
94 [[nodiscard]] size_t getSize() const noexcept { return mSize; }
95
97 [[nodiscard]] size_t getCapacity() const noexcept { return mCapacity; }
98
101
103 [[nodiscard]] size_t getChunkCount() const noexcept { return mChunks.size(); }
104
105 protected:
109 [[nodiscard]] inline void* getRawPointer(const EntityIndex index) noexcept { return mChunks[index / mChunkSize] + (index % mChunkSize) * mElementSize; }
110
114 [[nodiscard]] inline const void* getRawPointer(const EntityIndex index) const noexcept {
115 return mChunks[index / mChunkSize] + (index % mChunkSize) * mElementSize;
116 }
117
121 void ensureMinimumSize(std::size_t size);
122
127 };
128
135 template<CComponent C> class ComponentStore final : public AbstractComponentStore {
136 public:
139 inline explicit ComponentStore(const size_t chunkSize) : AbstractComponentStore(chunkSize, (sizeof(C) / alignof(C) + 1) * alignof(C)) {}
140
144
145 public:
159 C* const component = getPointer(entityIndex);
160 return *new(component) C(std::forward<Args>(args)...);
161 }
162
164 inline C& create(const EntityIndex entityIndex) final {
165 if constexpr(std::is_default_constructible_v<C>) {
167 C* const component = getPointer(entityIndex);
168 return *new(component) C();
169 } else {
170 throw RuntimeError("The component C is not default constructible!");
171 }
172 }
173
175 inline void destroy(const EntityIndex entityIndex) final {
176 C* const component = getPointer(entityIndex);
177 component->~C();
178 }
179
182 if constexpr(std::is_copy_constructible_v<C>) {
184 const C* source = getPointer(sourceEntityIndex);
185 C* const target = getPointer(targetEntityIndex);
186 new(target) C(*source);
187 } else {
188 throw RuntimeError("Component is not copyable!");
189 }
190 }
191
193 [[nodiscard]] inline C& get(const EntityIndex entityIndex) final { return *getPointer(entityIndex); }
194
196 [[nodiscard]] inline const C& get(const EntityIndex entityIndex) const final { return *getPointer(entityIndex); }
197
198 private:
202 [[nodiscard]] inline C* getPointer(const EntityIndex index) noexcept { return reinterpret_cast<C*>(getRawPointer(index)); }
203
207 [[nodiscard]] inline const C* getPointer(const EntityIndex index) const noexcept { return reinterpret_cast<const C*>(getRawPointer(index)); }
208 };
209
210} // namespace CeresEngine
The component store class is responsible for managing and organizing component data storage in memory...
Definition ComponentStore.hpp:28
virtual ~AbstractComponentStore() noexcept
Defaulted virtual destructor.
const void * getRawPointer(const EntityIndex index) const noexcept
Gets a pointer to the the component stored at index.
Definition ComponentStore.hpp:114
virtual AbstractComponent & get(EntityIndex entityIndex)=0
Gets the component at entityIndex position.
size_t getChunkCount() const noexcept
Definition ComponentStore.hpp:103
Vector< char * > mChunks
A vector of all allocated memory chunks.
Definition ComponentStore.hpp:43
size_t mElementSize
The amount of bytes allocated for each element in the store.
Definition ComponentStore.hpp:40
void * getRawPointer(const EntityIndex index) noexcept
Gets a pointer to the the component stored at index.
Definition ComponentStore.hpp:109
size_t mChunkSize
The size to be used for each memory chunk (in bytes)
Definition ComponentStore.hpp:37
size_t mSize
The current store size (in bytes)
Definition ComponentStore.hpp:31
AbstractComponentStore(const size_t chunkSize, const size_t elementSize)
Definition ComponentStore.hpp:46
size_t getCapacity() const noexcept
Definition ComponentStore.hpp:97
size_t mCapacity
The current store capacity (in bytes)
Definition ComponentStore.hpp:34
size_t getSize() const noexcept
Definition ComponentStore.hpp:94
size_t getChunkSize() const noexcept
Definition ComponentStore.hpp:100
void ensureMinimumCapacity(size_t minCapacity)
Ensures that the store capacity is at least minCapacity bytes.
virtual void destroy(EntityIndex entityIndex)=0
Destroys the component located at entityIndex.
virtual AbstractComponent & create(EntityIndex entityIndex)=0
Creates a new component at entityIndex position.
virtual void copy(EntityIndex sourceEntityIndex, EntityIndex targetEntityIndex)=0
Copies the component located at entityIndex
void ensureMinimumSize(std::size_t size)
Ensures that the store size is at least size bytes.
A type-safe component store implementation.
Definition ComponentStore.hpp:135
const C * getPointer(const EntityIndex index) const noexcept
Gets a pointer to the the component stored at index.
Definition ComponentStore.hpp:207
~ComponentStore() noexcept final=default
Destroys the component store.
C & get(const EntityIndex entityIndex) final
Gets the component at entityIndex position.
Definition ComponentStore.hpp:193
void copy(const EntityIndex sourceEntityIndex, const EntityIndex targetEntityIndex) final
Copies the component located at entityIndex
Definition ComponentStore.hpp:181
C & create(const EntityIndex entityIndex, Args &&... args) noexcept(std::is_nothrow_constructible_v< C, Args... >)
Creates a new component at entityIndex position.
Definition ComponentStore.hpp:157
C * getPointer(const EntityIndex index) noexcept
Gets a pointer to the the component stored at index.
Definition ComponentStore.hpp:202
void destroy(const EntityIndex entityIndex) final
Destroys the component located at entityIndex.
Definition ComponentStore.hpp:175
ComponentStore(const size_t chunkSize)
Creates a new component store with the given chunkSize.
Definition ComponentStore.hpp:139
const C & get(const EntityIndex entityIndex) const final
Gets the component at entityIndex position.
Definition ComponentStore.hpp:196
C & create(const EntityIndex entityIndex) final
Creates a new component at entityIndex position.
Definition ComponentStore.hpp:164
Definition Exception.hpp:115
Definition Application.hpp:19
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
UInt32 EntityIndex
Definition EntityID.hpp:22
Definition Span.hpp:668
A abstract class that provides a trait that allows checking for component implementations.
Definition Component.hpp:39