VOX
A little voxel engine
Loading...
Searching...
No Matches
ComponentStorage.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4
5#include "ecs_Exceptions.hpp"
6#include "SparseSet.hpp"
7
8namespace ecs
9{
10 template <ValidEntity EntityType, typename ComponentType>
11 class ComponentStorage : public SparseSet<EntityType>
12 {
13 public:
14 typedef std::vector<ComponentType> container_type;
16 typedef typename container_type::iterator iterator;
17 typedef typename container_type::const_iterator const_iterator;
18 typedef typename container_type::reverse_iterator reverse_iterator;
19 typedef typename container_type::const_reverse_iterator const_reverse_iterator;
20
23
28
29 void insert(EntityType entity, ComponentType component)
30 {
32 return;
34 m_components.push_back(component);
35 }
36
37 size_t remove(EntityType entity) override
38 {
40 return 0;
42 std::swap(m_components[index], m_components.back());
43 m_components.pop_back();
44 return index;
45 }
46
47 ComponentType & get(EntityType entity)
48 {
50 std::out_of_range("Entity does not have component");
51 return m_components[base_type::index(entity)];
52 }
53
54 const ComponentType & get(EntityType entity) const
55 {
57 std::out_of_range("Entity does not have component");
58 return m_components[base_type::index(entity)];
59 }
60
61 ComponentType * tryGet(EntityType entity)
62 {
64 return nullptr;
65 return &m_components[base_type::index(entity)];
66 }
67
68 const ComponentType * tryGet(EntityType entity) const
69 {
71 return nullptr;
72 return &m_components[base_type::index(entity)];
73 }
74
75 size_t size() const
76 {
77 return m_components.size();
78 }
79
80 iterator begin() { return m_components.begin(); }
81 const_iterator begin() const { return m_components.begin(); }
82 iterator end() { return m_components.end(); }
83 const_iterator end() const { return m_components.end(); }
84
85 reverse_iterator rbegin() { return m_components.rbegin(); }
86 const_reverse_iterator rbegin() const { return m_components.rbegin(); }
87 reverse_iterator rend() { return m_components.rend(); }
88 const_reverse_iterator rend() const { return m_components.rend(); }
89 private:
90 container_type m_components;
91 };
92}
Definition: ComponentStorage.hpp:12
ComponentType * tryGet(EntityType entity)
Definition: ComponentStorage.hpp:61
const_iterator begin() const
Definition: ComponentStorage.hpp:81
const ComponentType & get(EntityType entity) const
Definition: ComponentStorage.hpp:54
const_reverse_iterator rend() const
Definition: ComponentStorage.hpp:88
ComponentStorage & operator=(ComponentStorage &other)=default
std::vector< ComponentType > container_type
Definition: ComponentStorage.hpp:14
ComponentStorage(ComponentStorage &&other)=default
void insert(EntityType entity, ComponentType component)
Definition: ComponentStorage.hpp:29
container_type::const_iterator const_iterator
Definition: ComponentStorage.hpp:17
container_type::reverse_iterator reverse_iterator
Definition: ComponentStorage.hpp:18
container_type::iterator iterator
Definition: ComponentStorage.hpp:16
ComponentType & get(EntityType entity)
Definition: ComponentStorage.hpp:47
const_reverse_iterator rbegin() const
Definition: ComponentStorage.hpp:86
ComponentStorage(ComponentStorage &other)=default
reverse_iterator rend()
Definition: ComponentStorage.hpp:87
iterator begin()
Definition: ComponentStorage.hpp:80
~ComponentStorage()
Definition: ComponentStorage.hpp:22
const ComponentType * tryGet(EntityType entity) const
Definition: ComponentStorage.hpp:68
container_type::const_reverse_iterator const_reverse_iterator
Definition: ComponentStorage.hpp:19
size_t remove(EntityType entity) override
Definition: ComponentStorage.hpp:37
ComponentStorage()
Definition: ComponentStorage.hpp:21
reverse_iterator rbegin()
Definition: ComponentStorage.hpp:85
iterator end()
Definition: ComponentStorage.hpp:82
size_t size() const
Definition: ComponentStorage.hpp:75
SparseSet< EntityType > base_type
Definition: ComponentStorage.hpp:15
ComponentStorage & operator=(ComponentStorage &&other)=default
const_iterator end() const
Definition: ComponentStorage.hpp:83
Definition: SparseSet.hpp:20
size_t index(entityType entity) const
Definition: SparseSet.hpp:114
size_t insert(entityType entity)
Definition: SparseSet.hpp:41
virtual size_t remove(entityType entity)
Definition: SparseSet.hpp:57
bool contains(entityType entity) const
Definition: SparseSet.hpp:120
Definition: ComponentStorage.hpp:9
uint32_t entity
Definition: ecs_CONSTANTS.hpp:10