Cogs.Foundation
Loading...
Searching...
No Matches
ComponentCollection.h
Go to the documentation of this file.
1#pragma once
2
3#include "Component.h"
4
5namespace Cogs
6{
7 namespace ComponentModel
8 {
19 {
20 public:
25 {
26 public:
28 typedef std::forward_iterator_tag iterator_category;
29
32
34 typedef size_t difference_type;
35
38
41
43 ComponentCollectionIterator(const ComponentCollectionBase & collection, size_t index) : collection(collection), index(index) {}
44
46 ComponentCollectionIterator(const ComponentCollectionIterator & other) : collection(other.collection), index(other.index) {}
47
49 [[nodiscard]] constexpr bool operator==(const ComponentCollectionIterator & other) const { return index == other.index; }
50
53
55 ComponentCollectionIterator & operator++(int) { ++index; return *this; }
56
58 const ComponentHandle & operator*() { return collection[index]; }
59
61 const ComponentHandle & operator*() const { return collection[index]; }
62
64 const ComponentHandle * operator->() const { return &collection[index]; }
65
66 private:
68
70 const ComponentCollectionBase & collection;
71
73 size_t index;
74
76 };
77
85
92
97
106 COGSFOUNDATION_API void add(ComponentHandle component);
107
112
119
125 size_t size() const
126 {
127 return components.size();
128 }
129
135 const ComponentHandle & operator[](size_t index) const
136 {
137 return components[index];
138 }
139
140 private:
142
144 std::vector<ComponentHandle> components;
145
147 };
148
155 template<typename ComponentType>
157 {
158 public:
163 {
164 public:
166 typedef std::forward_iterator_tag iterator_category;
167
169 typedef ComponentType value_type;
170
172 typedef size_t difference_type;
173
175 typedef ComponentType * pointer;
176
178 typedef ComponentType & reference;
179
181 ComponentCollectionIterator(ComponentCollection & collection, size_t index) : collection(collection), index(index) {}
182
184 ComponentCollectionIterator(const ComponentCollectionIterator & other) : collection(other.collection), index(other.index) {}
185
187 [[nodiscard]] constexpr bool operator==(const ComponentCollectionIterator & other) const { return index == other.index; }
188
191
193 ComponentCollectionIterator & operator++(int) { ++index; return *this; }
194
196 ComponentType & operator*() { return *collection[index].template resolveComponent<ComponentType>(); }
197
199 const ComponentType & operator*() const { return *collection[index].template resolveComponent<ComponentType>(); }
200
202 const ComponentType * operator->() const { return collection[index].template resolveComponent<ComponentType>(); }
203
204 private:
206
208 ComponentCollection & collection;
209
211 size_t index;
212
214 };
215
223
230 };
231 }
232}
#define COGSFOUNDATION_API
Definition: FoundationBase.h:31
Iterator type for iterating over ComponentCollectionBase.
Definition: ComponentCollection.h:25
ComponentHandle & reference
Reference type.
Definition: ComponentCollection.h:40
const ComponentHandle * operator->() const
Pointer operator. Returns a pointer to the component handle at the current iterator index.
Definition: ComponentCollection.h:64
ComponentCollectionIterator(const ComponentCollectionIterator &other)
Create a new iterator, copying the state of other.
Definition: ComponentCollection.h:46
size_t difference_type
Difference type to calculate iterator distance.
Definition: ComponentCollection.h:34
const ComponentHandle & operator*()
Dereference operator. Returns a reference to the component handle at the iterators current iterator i...
Definition: ComponentCollection.h:58
ComponentHandle value_type
Value type.
Definition: ComponentCollection.h:31
ComponentCollectionIterator operator++()
Post increment operator. Moves the iterator to the next component handle in the collection.
Definition: ComponentCollection.h:52
ComponentHandle * pointer
Pointer type.
Definition: ComponentCollection.h:37
std::forward_iterator_tag iterator_category
Type of iterator. Can only be used to iterate forward over component handles in a collection.
Definition: ComponentCollection.h:28
const ComponentHandle & operator*() const
Const dereference operator. Returns a const reference to the component handle at the current iterator...
Definition: ComponentCollection.h:61
ComponentCollectionIterator & operator++(int)
Pre increment operator. Moves the iterator to the next component handle in the collection.
Definition: ComponentCollection.h:55
constexpr bool operator==(const ComponentCollectionIterator &other) const
Comparison operator. If the current index of both iterators are equal the iterators are considered eq...
Definition: ComponentCollection.h:49
ComponentCollectionIterator(const ComponentCollectionBase &collection, size_t index)
Creates a new iterator over the given collection with the given index.
Definition: ComponentCollection.h:43
A collection of components, held by component handles.
Definition: ComponentCollection.h:19
ComponentCollectionIterator begin() const
Iterator to the beginning of the collection.
Definition: ComponentCollection.h:84
COGSFOUNDATION_API void add(ComponentHandle component)
Adds the given component handle to the collection.
Definition: ComponentCollection.cpp:10
size_t size() const
Get the size of the collection.
Definition: ComponentCollection.h:125
const ComponentHandle & operator[](size_t index) const
Get the component at the given index.
Definition: ComponentCollection.h:135
ComponentCollectionIterator end() const
Iterator to the end of the collection.
Definition: ComponentCollection.h:91
COGSFOUNDATION_API ComponentCollectionBase()
Creates an empty component collection.
COGSFOUNDATION_API void remove(ComponentHandle component)
Removes the given component from the collection.
Definition: ComponentCollection.cpp:15
COGSFOUNDATION_API void clear()
Clears the contents of the collection.
Definition: ComponentCollection.cpp:22
Iterator type for iterating over typed component collections.
Definition: ComponentCollection.h:163
ComponentType & reference
Reference type to the templated ComponentType.
Definition: ComponentCollection.h:178
ComponentType value_type
Value type for dereferencing the iterator is the templated ComponentType.
Definition: ComponentCollection.h:169
const ComponentType & operator*() const
Const dereference operator. Returns a const reference to the component at the current iterator index.
Definition: ComponentCollection.h:199
std::forward_iterator_tag iterator_category
Type of iterator. Can only be used to iterate forward over components in a pool.
Definition: ComponentCollection.h:166
ComponentCollectionIterator operator++()
Post increment operator. Moves the iterator to the next component indexed in the collection.
Definition: ComponentCollection.h:190
ComponentCollectionIterator & operator++(int)
Pre increment operator. Moves the iterator to the next component indexed in the collection.
Definition: ComponentCollection.h:193
size_t difference_type
Difference type to calculate iterator distance.
Definition: ComponentCollection.h:172
const ComponentType * operator->() const
Pointer operator. Returns a pointer to the component at the current iterator index.
Definition: ComponentCollection.h:202
ComponentType & operator*()
Dereference operator. Returns a reference to the component at the iterators current iterator index.
Definition: ComponentCollection.h:196
ComponentCollectionIterator(const ComponentCollectionIterator &other)
Creates a new iterator copying the state of other.
Definition: ComponentCollection.h:184
constexpr bool operator==(const ComponentCollectionIterator &other) const
Comparison operator. If the current index of both iterators are equal the iterators are considered eq...
Definition: ComponentCollection.h:187
ComponentCollectionIterator(ComponentCollection &collection, size_t index)
Creates a new iterator over the given collection with the given index.
Definition: ComponentCollection.h:181
ComponentType * pointer
Pointer type to the templated ComponentType.
Definition: ComponentCollection.h:175
Typed collection of components.
Definition: ComponentCollection.h:157
ComponentCollectionIterator end()
Iterator to the end of the collection.
Definition: ComponentCollection.h:229
ComponentCollectionIterator begin()
Iterator to the beginning of the collection.
Definition: ComponentCollection.h:222
Main Cogs namespace.
Definition: MortonCode.h:5
Handle to a Component instance.
Definition: Component.h:67