Skip to main content

View Class Template

A view class to iterate over entities having specific components. More...

Declaration

template <typename TEntityManager, typename... Components> class helios::ecs::View<TEntityManager, Components> { ... }

Public Constructors Index

template <typename TEntityManager, typename... Components>
View (TEntityManager *em)

Constructs the view and retrieves the necessary component sets. More...

Public Member Functions Index

template <typename T>
View &exclude ()

Excludes entities that have a specific component. More...

template <typename TEntityManager, typename... Components>
boolempty ()
template <typename TEntityManager, typename... Components>
View &whereEnabled ()

Filters to only include entities with enabled components. More...

template <typename TEntityManager, typename... Components>
Iteratorbegin ()

Returns an iterator to the first valid entity. More...

template <typename TEntityManager, typename... Components>
Iteratorend ()

Returns an iterator to the end (past the last entity). More...

Private Member Attributes Index

template <typename TEntityManager, typename... Components>
TEntityManager *em_
template <typename TEntityManager, typename... Components>
std::tuple< SparseSet< Components > *... >includeSets_

Pointers to the SparseSets of the included components. More...

template <typename TEntityManager, typename... Components>
std::vector< std::function< bool(EntityId)> >excludeChecks_

List of exclusion predicates. Stores functions that return true if an entity should be EXCLUDED. Operates on EntityId (index) because the SparseSet uses it internally. More...

template <typename TEntityManager, typename... Components>
boolfilterEnabledOnly_ = false

Flag to filter only enabled components. More...

template <typename TEntityManager, typename... Components>
boolfilterActiveOnly_ = false

Description

A view class to iterate over entities having specific components.

The View acts as a lightweight iterator over the SparseSets of the requested components. It uses the first component type (Lead) as the primary iterator and cross-references existence in other sets.

## Usage

```cpp for (auto [entity, transform, velocity, active] : world.view< GameHandle, TransformComponent, VelocityComponent, Active >().whereEnabled()) { // Process entity } ```

Template Parameters
TEntityManager

The concrete `EntityManager` specialisation to iterate over. Determines the handle type and component storage used.

Components

The component types to query for.

See Also

EntityManager

See Also

SparseSet

See Also

TypedHandleWorld

Definition at line 52 of file View.ixx.

Public Constructors

View()

template <typename TEntityManager, typename... Components>
helios::ecs::View< TEntityManager, Components >::View (TEntityManager * em)
inline explicit

Constructs the view and retrieves the necessary component sets.

Parameters
em

Pointer to the EntityManager to retrieve sets and construct Entities.

Definition at line 82 of file View.ixx.

82 explicit View(TEntityManager* em) : em_(em) {
83 // Retrieve pointers to the specific component sets immediately.
84 includeSets_ = std::make_tuple(em_->getSparseSet<Components>()...);
85 };

Referenced by helios::ecs::View< TEntityManager, Components >::exclude, helios::ecs::View< TEntityManager, Components >::Iterator::Iterator and helios::ecs::View< TEntityManager, Components >::whereEnabled.

Public Member Functions

begin()

template <typename TEntityManager, typename... Components>
Iterator helios::ecs::View< TEntityManager, Components >::begin ()
inline nodiscard

Returns an iterator to the first valid entity.

Uses the first component type's SparseSet as the lead. If the first entity is invalid, advances to the next valid one.

Returns

Iterator to the first valid entity, or end() if none found.

Definition at line 317 of file View.ixx.

317 [[nodiscard]] Iterator begin() {
318 auto* leadSet = std::get<0>(includeSets_);
319
320 if (!leadSet) {
321 return Iterator{};
322 }
323
324 Iterator it{leadSet->begin(), leadSet->end(), this};
325
326 if (!it.isValid()) {
327 it.advance();
328 }
329
330 return it;
331 }

References helios::ecs::View< TEntityManager, Components >::Iterator::advance and helios::ecs::View< TEntityManager, Components >::Iterator::isValid.

Referenced by helios::ecs::View< TEntityManager, Components >::empty.

empty()

template <typename TEntityManager, typename... Components>
bool helios::ecs::View< TEntityManager, Components >::empty ()
inline nodiscard

Definition at line 118 of file View.ixx.

118 [[nodiscard]] bool empty() {
119 auto* leadSet = std::get<0>(includeSets_);
120 if (!leadSet) {
121 return true;
122 }
123 return begin() == end();
124 }

References helios::ecs::View< TEntityManager, Components >::begin and helios::ecs::View< TEntityManager, Components >::end.

end()

template <typename TEntityManager, typename... Components>
Iterator helios::ecs::View< TEntityManager, Components >::end ()
inline nodiscard

Returns an iterator to the end (past the last entity).

Returns

End iterator for comparison.

Definition at line 338 of file View.ixx.

338 [[nodiscard]] Iterator end() {
339 auto* leadSet = std::get<0>(includeSets_);
340
341 if (!leadSet) {
342 return Iterator{};
343 }
344
345 return Iterator{leadSet->end(), leadSet->end(), this};
346 }

Referenced by helios::ecs::View< TEntityManager, Components >::empty and helios::ecs::View< TEntityManager, Components >::Iterator::Iterator.

exclude()

template <typename T>
View & helios::ecs::View< TEntityManager, Components >::exclude ()
inline

Excludes entities that have a specific component.

Entities possessing the specified component type will be skipped during iteration. Multiple exclusions can be chained.

```cpp // Skip entities with Shield or Invincible for (auto [e, health] : world->view<HealthComponent>() .exclude<ShieldComponent>() .exclude<InvincibleComponent>()) { // Only vulnerable entities } ```

Template Parameters
T

The component type to exclude.

Returns

Reference to this View for method chaining.

Definition at line 107 of file View.ixx.

108 auto* set = em_->getSparseSet<T>();
109
110 if (set) {
111 excludeChecks_.emplace_back([set](EntityId entityId) {
112 return set->contains(entityId);
113 });
114 }
115 return *this;
116 }

Reference helios::ecs::View< TEntityManager, Components >::View.

whereEnabled()

template <typename TEntityManager, typename... Components>
View & helios::ecs::View< TEntityManager, Components >::whereEnabled ()
inline

Filters to only include entities with enabled components.

Components must implement `isEnabled()` returning bool. Components without this method are assumed to be enabled.

Returns

Reference to this View for method chaining.

Definition at line 134 of file View.ixx.

135 filterEnabledOnly_ = true;
136 return *this;
137 }

Reference helios::ecs::View< TEntityManager, Components >::View.

Private Member Attributes

em_

template <typename TEntityManager, typename... Components>
TEntityManager* helios::ecs::View< TEntityManager, Components >::em_

Definition at line 55 of file View.ixx.

55 TEntityManager* em_;

excludeChecks_

template <typename TEntityManager, typename... Components>
std::vector<std::function<bool(EntityId)> > helios::ecs::View< TEntityManager, Components >::excludeChecks_

List of exclusion predicates. Stores functions that return true if an entity should be EXCLUDED. Operates on EntityId (index) because the SparseSet uses it internally.

Definition at line 67 of file View.ixx.

67 std::vector<std::function<bool(EntityId)>> excludeChecks_;

filterActiveOnly_

template <typename TEntityManager, typename... Components>
bool helios::ecs::View< TEntityManager, Components >::filterActiveOnly_ = false

Definition at line 74 of file View.ixx.

74 bool filterActiveOnly_ = false;

filterEnabledOnly_

template <typename TEntityManager, typename... Components>
bool helios::ecs::View< TEntityManager, Components >::filterEnabledOnly_ = false

Flag to filter only enabled components.

Definition at line 72 of file View.ixx.

72 bool filterEnabledOnly_ = false;

includeSets_

template <typename TEntityManager, typename... Components>
std::tuple<SparseSet<Components>*... > helios::ecs::View< TEntityManager, Components >::includeSets_

Pointers to the SparseSets of the included components.

Definition at line 60 of file View.ixx.

60 std::tuple<SparseSet<Components>*... > includeSets_;

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.