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
BondHandle.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <cstddef> // for size_t
10#include <vector> // for storing the Entities to iterate over
11#include <cassert> // for asserts in constructor
12
13#include "Imago/ECS/Entity.hpp"
14#include "Imago/ECS/Bond.hpp"
15
16namespace Imago::ECS
17{
18
19 // Note: BondHandle<Components...> can take any number of component types. This is using a C++ variadic template parameter pack
20
21 //TODO: direct component _dense access instead of entities
30 template <typename... Components>
31 class BondHandle {
32 private:
33
34 const Bond* _bond;
35
36 public:
37
43 explicit BondHandle(const Bond* bond)
44 : _bond(bond)
45 {
46 // ensure an empty array was not passed in
47 static_assert(sizeof...(Components) > 0, "[BondHandle] Must be queried for at least one component type.");
48
49 // ensure the bond is not null
50 assert(_bond != nullptr && "[BondHandle] cannot be created with a nullptr Bond!");
51
52 // ensure the bond's size and the query size are the same
53 assert(_bond->get_pool_count() == sizeof...(Components) && "[BondHandle] BondHandle size and Bond pool count do not match!");
54 }
55
61 class Iterator {
62 private:
63
64 const std::vector<Entity>* _entities;
65 size_t _index;
66
67 public:
68
74 Iterator(const std::vector<Entity>* entities, size_t index)
75 : _entities(entities), _index(index) { }
76
82 {
83 return (*_entities)[_index];
84 }
85
91 {
92 _index++; // increment index
93 return *this; // return the iterator.
94 }
95
102 bool operator!=(const Iterator& other) const
103 {
104 return _index != other._index;
105 }
106 };
107
112 {
113 return Iterator(&(_bond->get_entities()), 0);
114 }
115
119 Iterator end() const
120 {
121 return Iterator(&(_bond->get_entities()), _bond->get_bond_size());
122 }
123 };
124}
Defines the Bond class which will be a permanent group of component pools that are kept sorted so tha...
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
Definition BondHandle.hpp:61
Entity operator*() const
Dereference the Iterator to access the current element.
Definition BondHandle.hpp:81
Iterator(const std::vector< Entity > *entities, size_t index)
Construct a new Iterator object.
Definition BondHandle.hpp:74
bool operator!=(const Iterator &other) const
Compare the indices of the two Iterators.
Definition BondHandle.hpp:102
Iterator & operator++()
Advances the Iterator to the next element which is just _index + 1.
Definition BondHandle.hpp:90
BondHandle(const Bond *bond)
Constructs a new BondHandle object, initializing it with is according Bond. This can only be called b...
Definition BondHandle.hpp:43
Iterator end() const
Returns an Iterator past the last element.
Definition BondHandle.hpp:119
Iterator begin() const
Returns an Iterator to the first element.
Definition BondHandle.hpp:111
A Bond permanently reorders every pool within it so that every Entity that has all of the components ...
Definition Bond.hpp:29