Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::ecs::details::QueryWithEntityIterator< Components > Class Template Reference

Iterator for query results with entity information. More...

#include <query.hpp>

Inheritance diagram for helios::ecs::details::QueryWithEntityIterator< Components >:
helios::utils::FunctionalAdapterBase< QueryWithEntityIterator< Components... > >

Public Types

using ComponentsType = std::conditional_t< AllComponentsConst< Components... >, const details::Components, details::Components >
 Components manager type based on whether all components are const-qualified.
 
using iterator_category = std::forward_iterator_tag
 
using value_type = std::tuple< Entity, ComponentAccessType< Components >... >
 
using difference_type = ptrdiff_t
 
using pointer = value_type *
 
using reference = value_type
 

Public Member Functions

 QueryWithEntityIterator (std::span< const std::reference_wrapper< const Archetype > > archetypes, ComponentsType &components, size_t archetype_index=0, size_t entity_index=0)
 Constructs iterator for query results with entity.
 
 QueryWithEntityIterator (const QueryWithEntityIterator &) noexcept=default
 
 QueryWithEntityIterator (QueryWithEntityIterator &&) noexcept=default
 
 ~QueryWithEntityIterator () noexcept=default
 
QueryWithEntityIteratoroperator= (const QueryWithEntityIterator &) noexcept=default
 
QueryWithEntityIteratoroperator= (QueryWithEntityIterator &&) noexcept=default
 
QueryWithEntityIteratoroperator++ ()
 Advances iterator to next matching entity.
 
QueryWithEntityIterator operator++ (int)
 Advances iterator to next matching entity.
 
QueryWithEntityIteratoroperator-- ()
 Moves iterator to previous matching entity.
 
QueryWithEntityIterator operator-- (int)
 Moves iterator to previous matching entity.
 
value_type operator* () const
 Dereferences iterator to get entity and component tuple.
 
bool operator== (const QueryWithEntityIterator &other) const noexcept
 Compares iterators for equality.
 
bool operator!= (const QueryWithEntityIterator &other) const noexcept
 Compares iterators for inequality.
 
QueryWithEntityIterator begin () const noexcept
 Returns iterator to the beginning.
 
QueryWithEntityIterator end () const noexcept
 Returns iterator to the end.
 

Detailed Description

template<ComponentTrait... Components>
requires utils::UniqueTypes<Components...>
class helios::ecs::details::QueryWithEntityIterator< Components >

Iterator for query results with entity information.

Provides forward iteration over entities matching the query criteria, returning tuples that include the entity followed by requested component references.

Note
Not thread-safe.
Template Parameters
ComponentsRequested component access types (may include const qualifiers)

Definition at line 249 of file query.hpp.

Member Typedef Documentation

◆ ComponentsType

template<ComponentTrait... Components>
using helios::ecs::details::QueryWithEntityIterator< Components >::ComponentsType = std::conditional_t<AllComponentsConst<Components...>, const details::Components, details::Components>

Components manager type based on whether all components are const-qualified.

Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/query.hpp.

Definition at line 252 of file query.hpp.

◆ difference_type

◆ iterator_category

template<ComponentTrait... Components>
using helios::ecs::details::QueryWithEntityIterator< Components >::iterator_category = std::forward_iterator_tag

◆ pointer

◆ reference

◆ value_type

Constructor & Destructor Documentation

◆ QueryWithEntityIterator() [1/3]

template<ComponentTrait... Components>
requires utils::UniqueTypes<Components...>
helios::ecs::details::QueryWithEntityIterator< Components >::QueryWithEntityIterator ( std::span< const std::reference_wrapper< const Archetype > >  archetypes,
ComponentsType components,
size_t  archetype_index = 0,
size_t  entity_index = 0 
)

Constructs iterator for query results with entity.

Initializes iterator to point to the first matching entity.

Parameters
archetypesSpan of archetypes matching the query
componentsComponent manager for accessing component data
archetype_indexStarting archetype index (default: 0)
entity_indexStarting entity index within archetype (default: 0)
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/query.hpp.

Definition at line 453 of file query.hpp.

456 : archetypes_(archetypes), components_(components), archetype_index_(archetype_index), entity_index_(entity_index) {
457 AdvanceToValidEntity();
458}
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ QueryWithEntityIterator() [2/3]

template<ComponentTrait... Components>
helios::ecs::details::QueryWithEntityIterator< Components >::QueryWithEntityIterator ( const QueryWithEntityIterator< Components > &  )
defaultnoexcept

◆ QueryWithEntityIterator() [3/3]

template<ComponentTrait... Components>
helios::ecs::details::QueryWithEntityIterator< Components >::QueryWithEntityIterator ( QueryWithEntityIterator< Components > &&  )
defaultnoexcept

◆ ~QueryWithEntityIterator()

Member Function Documentation

◆ begin()

template<ComponentTrait... Components>
QueryWithEntityIterator helios::ecs::details::QueryWithEntityIterator< Components >::begin ( ) const
inlinenoexcept

Returns iterator to the beginning.

Returns
Copy of this iterator
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/query.hpp.

Definition at line 331 of file query.hpp.

331{ return *this; }

◆ end()

template<ComponentTrait... Components>
QueryWithEntityIterator helios::ecs::details::QueryWithEntityIterator< Components >::end ( ) const
inlinenoexcept

Returns iterator to the end.

Returns
End iterator
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/query.hpp.

Definition at line 337 of file query.hpp.

337 {
338 return {archetypes_, components_.get(), archetypes_.size(), 0};
339 }

◆ operator!=()

template<ComponentTrait... Components>
bool helios::ecs::details::QueryWithEntityIterator< Components >::operator!= ( const QueryWithEntityIterator< Components > &  other) const
inlinenoexcept

Compares iterators for inequality.

Parameters
otherIterator to compare with
Returns
True if iterators are not equal, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/query.hpp.

Definition at line 325 of file query.hpp.

325{ return !(*this == other); }

◆ operator*()

template<ComponentTrait... Components>
requires utils::UniqueTypes<Components...>
auto helios::ecs::details::QueryWithEntityIterator< Components >::operator* ( ) const
inline

Dereferences iterator to get entity and component tuple.

Returns tuple starting with entity followed by component references.

Warning
Triggers assertion if iterator is at end or in invalid state.
Returns
Tuple of entity and component references
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/query.hpp.

Definition at line 514 of file query.hpp.

514 {
515 HELIOS_ASSERT(!IsAtEnd(), "Cannot dereference end iterator!");
516 HELIOS_ASSERT(archetype_index_ < archetypes_.size(), "Archetype index out of bounds!");
517 HELIOS_ASSERT(entity_index_ < archetypes_[archetype_index_].get().Entities().size(), "Entity index out of bounds!");
518
519 const auto& archetype = archetypes_[archetype_index_].get();
520 const Entity entity = archetype.Entities()[entity_index_];
521
522 return {entity,
524 return static_cast<ComponentAccessType<Components>>(
525 components_.get().template GetComponent<ComponentType>(entity));
526 }()...};
527}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140

◆ operator++() [1/2]

template<ComponentTrait... Components>
requires utils::UniqueTypes<Components...>
auto helios::ecs::details::QueryWithEntityIterator< Components >::operator++ ( )
inline

Advances iterator to next matching entity.

Returns
Reference to this iterator after advancement
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/query.hpp.

Definition at line 462 of file query.hpp.

462 {
463 ++entity_index_;
464 AdvanceToValidEntity();
465 return *this;
466}

◆ operator++() [2/2]

template<ComponentTrait... Components>
requires utils::UniqueTypes<Components...>
auto helios::ecs::details::QueryWithEntityIterator< Components >::operator++ ( int  )
inline

Advances iterator to next matching entity.

Returns
Copy of iterator before advancement

Definition at line 470 of file query.hpp.

470 {
471 auto copy = *this;
472 ++(*this);
473 return copy;
474}

◆ operator--() [1/2]

template<ComponentTrait... Components>
requires utils::UniqueTypes<Components...>
auto helios::ecs::details::QueryWithEntityIterator< Components >::operator-- ( )
inline

Moves iterator to previous matching entity.

Returns
Reference to this iterator after moving backward
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/query.hpp.

Definition at line 478 of file query.hpp.

478 {
479 // If at end or beginning of current archetype, need to go back
480 while (true) {
481 if (entity_index_ > 0) {
482 --entity_index_;
483 return *this;
484 }
485
486 // Need to move to previous archetype
487 if (archetype_index_ == 0) {
488 // Already at beginning, can't go back further
489 return *this;
490 }
491
492 --archetype_index_;
493 // Move to last entity in previous archetype
494 if (archetype_index_ < archetypes_.size()) {
495 entity_index_ = archetypes_[archetype_index_].get().EntityCount();
496 if (entity_index_ > 0) {
497 --entity_index_;
498 return *this;
499 }
500 }
501 }
502}

◆ operator--() [2/2]

template<ComponentTrait... Components>
requires utils::UniqueTypes<Components...>
auto helios::ecs::details::QueryWithEntityIterator< Components >::operator-- ( int  )
inline

Moves iterator to previous matching entity.

Returns
Copy of iterator before moving backward

Definition at line 506 of file query.hpp.

506 {
507 auto copy = *this;
508 --(*this);
509 return copy;
510}

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ operator==()

template<ComponentTrait... Components>
bool helios::ecs::details::QueryWithEntityIterator< Components >::operator== ( const QueryWithEntityIterator< Components > &  other) const
inlinenoexcept

Compares iterators for equality.

Parameters
otherIterator to compare with
Returns
True if iterators are equal, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/query.hpp.

Definition at line 316 of file query.hpp.

316 {
317 return archetype_index_ == other.archetype_index_ && entity_index_ == other.entity_index_;
318 }