13class ComponentListSparse :
public ComponentListBase {
17 using ComponentType = T;
20 using iterator_category = std::forward_iterator_tag;
21 using difference_type = std::ptrdiff_t;
22 using value_type = std::optional<T>;
23 using pointer = std::optional<T>*;
24 using reference = std::optional<T>&;
26 Iterator(pointer ptr,
unsigned pos) : m_ptr_(ptr), pos_(pos) {}
28 reference operator*()
const {
return *m_ptr_; }
29 pointer operator->() {
return m_ptr_; }
31 Iterator& operator++() {
36 Iterator& operator--() {
41 Iterator operator++(
int) {
47 Iterator operator--(
int) {
53 friend bool operator==(
const Iterator& a,
const Iterator& b) {
54 return a.m_ptr_ == b.m_ptr_;
56 friend bool operator!=(
const Iterator& a,
const Iterator& b) {
57 return a.m_ptr_ != b.m_ptr_;
60 unsigned pos()
const {
return pos_; }
61 bool valid() {
return m_ptr_->has_value(); }
62 T& component()
const {
return m_ptr_->value(); }
69 Iterator begin() {
return Iterator(components_.data(), 1); }
70 Iterator at(
unsigned e) {
return Iterator(&components_.at(e - 1), e); }
72 auto it = Iterator(&components_[components_.size() - 1],
73 static_cast<unsigned>(components_.size()));
79 bool addEntity(
unsigned e)
override {
80 if (e >= components_.size()) {
81 components_.emplace_back(std::optional<T>());
87 bool moveComponent(
unsigned src,
unsigned dst)
override {
88 if(components_.at(src - 1).has_value()) {
89 components_.at(dst - 1) = components_.at(src - 1).value();
96 bool removeComponent(
unsigned e)
override {
97 if (e > 0 && e <= components_.size()) {
98 components_[e - 1].reset();
103 std::vector<std::optional<T>> components_;
Handles anything related to entities and components in the engine.
Definition: component_manager.hpp:29
base class for component lists
ComponentListType
type of container to be used for storing componets
Definition: component_lists.hpp:8
@ kSparse
should be used for compnents that are used by most entities