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

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

#include <query.hpp>

Inheritance diagram for helios::ecs::details::QueryIterator< Components >:
helios::utils::FunctionalAdapterBase< QueryIterator< 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< ComponentAccessType< Components >... >
 
using difference_type = ptrdiff_t
 
using pointer = value_type *
 
using reference = value_type
 

Public Member Functions

 QueryIterator (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.
 
 QueryIterator (const QueryIterator &) noexcept=default
 
 QueryIterator (QueryIterator &&) noexcept=default
 
 ~QueryIterator () noexcept=default
 
QueryIteratoroperator= (const QueryIterator &) noexcept=default
 
QueryIteratoroperator= (QueryIterator &&) noexcept=default
 
QueryIteratoroperator++ ()
 Advances iterator to next matching entity.
 
QueryIterator operator++ (int)
 Advances iterator to next matching entity.
 
QueryIteratoroperator-- ()
 Moves iterator to previous matching entity.
 
QueryIterator operator-- (int)
 Moves iterator to previous matching entity.
 
value_type operator* () const
 Dereferences iterator to get component tuple.
 
bool operator== (const QueryIterator &other) const noexcept
 Compares iterators for equality.
 
bool operator!= (const QueryIterator &other) const noexcept
 Compares iterators for inequality.
 
QueryIterator begin () const noexcept
 Returns iterator to the beginning.
 
QueryIterator end () const noexcept
 Returns iterator to the end.
 

Detailed Description

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

Iterator for query results without entity information.

Provides forward iteration over entities matching the query criteria, returning tuples of requested component references. Supports const-qualified component access for read-only operations.

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

Definition at line 129 of file query.hpp.

Member Typedef Documentation

◆ ComponentsType

template<ComponentTrait... Components>
using helios::ecs::details::QueryIterator< 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 132 of file query.hpp.

◆ difference_type

template<ComponentTrait... Components>
using helios::ecs::details::QueryIterator< Components >::difference_type = ptrdiff_t

◆ iterator_category

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

◆ pointer

◆ reference

◆ value_type

template<ComponentTrait... Components>
using helios::ecs::details::QueryIterator< Components >::value_type = std::tuple<ComponentAccessType<Components>...>

Constructor & Destructor Documentation

◆ QueryIterator() [1/3]

template<ComponentTrait... Components>
requires utils::UniqueTypes<Components...>
helios::ecs::details::QueryIterator< Components >::QueryIterator ( 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.

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 362 of file query.hpp.

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

◆ QueryIterator() [2/3]

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

◆ QueryIterator() [3/3]

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

◆ ~QueryIterator()

Member Function Documentation

◆ begin()

template<ComponentTrait... Components>
QueryIterator helios::ecs::details::QueryIterator< 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 213 of file query.hpp.

213{ return *this; }

◆ end()

template<ComponentTrait... Components>
QueryIterator helios::ecs::details::QueryIterator< 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 219 of file query.hpp.

219{ return {archetypes_, components_.get(), archetypes_.size(), 0}; }

◆ operator!=()

template<ComponentTrait... Components>
bool helios::ecs::details::QueryIterator< Components >::operator!= ( const QueryIterator< 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 207 of file query.hpp.

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

◆ operator*()

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

Dereferences iterator to get component tuple.

Returns tuple of component references for the current entity. Handles const-qualified access appropriately.

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

Definition at line 422 of file query.hpp.

422 {
423 HELIOS_ASSERT(!IsAtEnd(), "Cannot dereference end iterator!");
424 HELIOS_ASSERT(archetype_index_ < archetypes_.size(), "Archetype index out of bounds!");
425 HELIOS_ASSERT(entity_index_ < archetypes_[archetype_index_].get().Entities().size(), "Entity index out of bounds!");
426
427 const auto& archetype = archetypes_[archetype_index_].get();
428 const Entity entity = archetype.Entities()[entity_index_];
429
431 return static_cast<ComponentAccessType<Components>>(components_.get().template GetComponent<ComponentType>(entity));
432 }()...};
433}
#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::QueryIterator< 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 370 of file query.hpp.

370 {
371 ++entity_index_;
372 AdvanceToValidEntity();
373 return *this;
374}

◆ operator++() [2/2]

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

Advances iterator to next matching entity.

Returns
Copy of iterator before advancement

Definition at line 378 of file query.hpp.

378 {
379 auto copy = *this;
380 ++(*this);
381 return copy;
382}

◆ operator--() [1/2]

template<ComponentTrait... Components>
requires utils::UniqueTypes<Components...>
auto helios::ecs::details::QueryIterator< 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 386 of file query.hpp.

386 {
387 // If at end or beginning of current archetype, need to go back
388 while (true) {
389 if (entity_index_ > 0) {
390 --entity_index_;
391 return *this;
392 }
393
394 // Need to move to previous archetype
395 if (archetype_index_ == 0) {
396 // Already at beginning, can't go back further
397 return *this;
398 }
399
400 --archetype_index_;
401 // Move to last entity in previous archetype
402 if (archetype_index_ < archetypes_.size()) {
403 entity_index_ = archetypes_[archetype_index_].get().EntityCount();
404 if (entity_index_ > 0) {
405 --entity_index_;
406 return *this;
407 }
408 }
409 }
410}

◆ operator--() [2/2]

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

Moves iterator to previous matching entity.

Returns
Copy of iterator before moving backward

Definition at line 414 of file query.hpp.

414 {
415 auto copy = *this;
416 --(*this);
417 return copy;
418}

◆ operator=() [1/2]

◆ operator=() [2/2]

template<ComponentTrait... Components>
QueryIterator & helios::ecs::details::QueryIterator< Components >::operator= ( QueryIterator< Components > &&  )
defaultnoexcept

◆ operator==()

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

Compares iterators for equality.

Two iterators are equal if they point to the same archetype and entity indices.

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 198 of file query.hpp.

198 {
199 return archetype_index_ == other.archetype_index_ && entity_index_ == other.entity_index_;
200 }