Cogs.Core
ComponentCollection.h
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
96 COGSFOUNDATION_API ComponentCollectionBase();
97
106 COGSFOUNDATION_API void add(ComponentHandle component);
107
111 COGSFOUNDATION_API void remove(ComponentHandle component);
112
118 COGSFOUNDATION_API void clear();
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}
Iterator type for iterating over ComponentCollectionBase.
const ComponentHandle * operator->() const
Pointer operator. Returns a pointer to the component handle at the current iterator index.
ComponentCollectionIterator(const ComponentCollectionIterator &other)
Create a new iterator, copying the state of other.
size_t difference_type
Difference type to calculate iterator distance.
const ComponentHandle & operator*()
Dereference operator. Returns a reference to the component handle at the iterators current iterator i...
ComponentCollectionIterator operator++()
Post increment operator. Moves the iterator to the next component handle in the collection.
std::forward_iterator_tag iterator_category
Type of iterator. Can only be used to iterate forward over component handles in a collection.
const ComponentHandle & operator*() const
Const dereference operator. Returns a const reference to the component handle at the current iterator...
ComponentCollectionIterator & operator++(int)
Pre increment operator. Moves the iterator to the next component handle in the collection.
constexpr bool operator==(const ComponentCollectionIterator &other) const
Comparison operator. If the current index of both iterators are equal the iterators are considered eq...
ComponentCollectionIterator(const ComponentCollectionBase &collection, size_t index)
Creates a new iterator over the given collection with the given index.
A collection of components, held by component handles.
ComponentCollectionIterator begin() const
Iterator to the beginning of the collection.
COGSFOUNDATION_API void add(ComponentHandle component)
Adds the given component handle to the collection.
size_t size() const
Get the size of the collection.
const ComponentHandle & operator[](size_t index) const
Get the component at the given index.
ComponentCollectionIterator end() const
Iterator to the end of the collection.
COGSFOUNDATION_API ComponentCollectionBase()
Creates an empty component collection.
COGSFOUNDATION_API void remove(ComponentHandle component)
Removes the given component from the collection.
COGSFOUNDATION_API void clear()
Clears the contents of the collection.
Iterator type for iterating over typed component collections.
ComponentType & reference
Reference type to the templated ComponentType.
ComponentType value_type
Value type for dereferencing the iterator is the templated ComponentType.
const ComponentType & operator*() const
Const dereference operator. Returns a const reference to the component at the current iterator index.
std::forward_iterator_tag iterator_category
Type of iterator. Can only be used to iterate forward over components in a pool.
ComponentCollectionIterator operator++()
Post increment operator. Moves the iterator to the next component indexed in the collection.
ComponentCollectionIterator & operator++(int)
Pre increment operator. Moves the iterator to the next component indexed in the collection.
size_t difference_type
Difference type to calculate iterator distance.
const ComponentType * operator->() const
Pointer operator. Returns a pointer to the component at the current iterator index.
ComponentType & operator*()
Dereference operator. Returns a reference to the component at the iterators current iterator index.
ComponentCollectionIterator(const ComponentCollectionIterator &other)
Creates a new iterator copying the state of other.
constexpr bool operator==(const ComponentCollectionIterator &other) const
Comparison operator. If the current index of both iterators are equal the iterators are considered eq...
ComponentCollectionIterator(ComponentCollection &collection, size_t index)
Creates a new iterator over the given collection with the given index.
ComponentType * pointer
Pointer type to the templated ComponentType.
ComponentCollectionIterator end()
Iterator to the end of the collection.
ComponentCollectionIterator begin()
Iterator to the beginning of the collection.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Handle to a Component instance.
Definition: Component.h:67