ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
SurveyHandle.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <array> // for array to store pools
10#include <vector> // for entity vector in iterator
11
12#include "Imago/ECS/Entity.hpp"
14
15// Note: SurveyHandle and BondHandle will contain in class method definitions opposed to the style and structure of most ImagoECS classes, this is to avoid confusing
16// and noise with out of class definitions for a nested class
17
18namespace Imago::ECS
19{
20
21 // Note: SurveyHandle<Components...> can take any number of component types. This is using a C++ variadic template parameter pack
22
31 template <typename... Components>
33 private:
34
35 // Note: we are using an array here instead of a vector because the count of components and hence the size of the _pools array will be known at compile time
36
37 std::array<SparseSetBase*, sizeof...(Components)> _pools;
38 SparseSetBase* _smallestPool;
39
40 public:
41
42 // Note: The explicit keyword here prevents the compiler from converting std::array<SparseSetBase*, N> to a Survey
43 // which C++ allows for under normal circumstances since that is the only element being passed into the constructor
44
52 explicit SurveyHandle(const std::array<SparseSetBase*, sizeof...(Components)>& pools)
53 : _pools(pools)
54 {
55 // ensure an empty array was not passed in
56 static_assert(sizeof...(Components) > 0, "[SurveyHandle] Must be queried for at least one component type.");
57
58 // find and set the smallest pool with a linear scan
59 _smallestPool = _pools[0];
60 for (SparseSetBase* pool : _pools) {
61 if (pool->get_size() < _smallestPool->get_size()) {
62 _smallestPool = pool;
63 }
64 }
65 }
66
67 // Note: since the most common use case for a Survey is iterating through it with a range-based forloop
68 // we need to define an Iterator inner class with the necessary methods
69
75 class Iterator {
76 private:
77
78 // helper methods
79
83 void skip_unmatched()
84 {
85 // increment index while we are not at the end of the pools and every Entity is in each component pool
86 while (_index < _entities->size() && !matches_all((*_entities)[_index])) {
87 _index++;
88 }
89 }
90
97 bool matches_all(Entity e) const
98 {
99 // iterate through every pool for the survey
100 for (SparseSetBase* pool : *_pools) {
101
102 // if any entity does not have the pool return false
103 if (!pool->has(e)) {
104 return false;
105 }
106 }
107
108 // if we make it out every entity had the pool so return true
109 return true;
110 }
111
112 // data
113 const std::vector<Entity>* _entities;
114 size_t _index;
115 const std::array<SparseSetBase*, sizeof...(Components)>* _pools;
116
117 public:
118
125 Iterator(const std::vector<Entity>* entities, size_t index, const std::array<SparseSetBase*, sizeof...(Components)>* pools)
126 : _entities(entities), _index(index), _pools(pools)
127 {
128 skip_unmatched();
129 }
130
131 // operator overloads for for loops
132
138 {
139 return (*_entities)[_index]; // dereferences should return the entity at the current index
140 }
141
147 {
148 _index++; // increment the index
149 skip_unmatched(); // skip to the next valid index
150 return *this; // return the iterator
151 }
152
159 bool operator!=(const Iterator& other) const
160 {
161 return _index != other._index; // comparisons should compare indices
162 }
163
164 };
165
166 // begin and end methods for for loop usage with Iterator, they just return two iterators at where the start should be and where the end should be
167
172 {
173 return Iterator(&_smallestPool->get_entities(), 0, &_pools);
174 }
175
179 Iterator end() const
180 {
181 return Iterator(&_smallestPool->get_entities(), _smallestPool->get_size(), &_pools);
182 }
183 };
184}
Defines the Entity alias and the free functions used to pack and unpack it.
uint32_t Entity
A packed identifier: 20 bits of index, 12 bits of generation.
Definition Entity.hpp:20
Declares the SparseSetBase interface.
Type-erased interface for SparseSet<T>.
Definition SparseSetBase.hpp:21
Definition SurveyHandle.hpp:75
Iterator & operator++()
Advances the Iterator to the next element, skipping indices that represent Entities not in every pool...
Definition SurveyHandle.hpp:146
Entity operator*() const
Dereference the iterator to access the current element.
Definition SurveyHandle.hpp:137
Iterator(const std::vector< Entity > *entities, size_t index, const std::array< SparseSetBase *, sizeof...(Components)> *pools)
Construct a new Iterator object.
Definition SurveyHandle.hpp:125
bool operator!=(const Iterator &other) const
Checks whether two iterators point to different positions.
Definition SurveyHandle.hpp:159
Iterator begin() const
Returns an Iterator to the first element.
Definition SurveyHandle.hpp:171
SurveyHandle(const std::array< SparseSetBase *, sizeof...(Components)> &pools)
Constructs a new Survey object by defining _pools and iterating through them to find the smallest poo...
Definition SurveyHandle.hpp:52
Iterator end() const
Returns an Iterator past the last element.
Definition SurveyHandle.hpp:179