OpenLexocad  27.1
entt::basic_observer< Entity > Class Template Reference

Observer. More...

#include <entt.hpp>

Public Types

using entity_type = Entity
 Underlying entity identifier. More...
 
using size_type = std::size_t
 Unsigned integer type. More...
 
using iterator_type = typename sparse_set< Entity >::iterator_type
 Input iterator type. More...
 

Public Member Functions

 basic_observer () ENTT_NOEXCEPT
 Default constructor. More...
 
 basic_observer (const basic_observer &)=delete
 Default copy constructor, deleted on purpose. More...
 
 basic_observer (basic_observer &&)=delete
 Default move constructor, deleted on purpose. More...
 
template<typename... Matcher>
 basic_observer (basic_registry< entity_type > &reg, basic_collector< Matcher... >) ENTT_NOEXCEPT
 Creates an observer and connects it to a given registry. More...
 
 ~basic_observer ()=default
 Default destructor. More...
 
basic_observeroperator= (const basic_observer &)=delete
 Default copy assignment operator, deleted on purpose. More...
 
basic_observeroperator= (basic_observer &&)=delete
 Default move assignment operator, deleted on purpose. More...
 
template<typename... Matcher>
void connect (basic_registry< entity_type > &reg, basic_collector< Matcher... >)
 Connects an observer to a given registry. More...
 
void disconnect ()
 Disconnects an observer from the registry it keeps track of. More...
 
size_type size () const ENTT_NOEXCEPT
 Returns the number of elements in an observer. More...
 
bool empty () const ENTT_NOEXCEPT
 Checks whether an observer is empty. More...
 
const entity_typedata () const ENTT_NOEXCEPT
 Direct access to the list of entities of the observer. More...
 
iterator_type begin () const ENTT_NOEXCEPT
 Returns an iterator to the first entity of the observer. More...
 
iterator_type end () const ENTT_NOEXCEPT
 Returns an iterator that is past the last entity of the observer. More...
 
void clear ()
 Resets the underlying container. More...
 
template<typename Func >
void each (Func func) const
 Iterates entities and applies the given function object to them, then clears the observer. More...
 
template<typename Func >
void each (Func func)
 Iterates entities and applies the given function object to them, then clears the observer. More...
 

Detailed Description

template<typename Entity>
class entt::basic_observer< Entity >

Observer.

An observer returns all the entities and only the entities that fit the requirements of at least one matcher. Moreover, it's guaranteed that the entity list is tightly packed in memory for fast iterations.
In general, observers don't stay true to the order of any set of components.

Observers work mainly with two types of matchers, provided through a collector:

  • Observing matcher: an observer will return at least all the living entities for which one or more of the given components have been explicitly replaced and not yet destroyed.
  • Grouping matcher: an observer will return at least all the living entities that would have entered the given group if it existed and that would have not yet left it.

If an entity respects the requirements of multiple matchers, it will be returned once and only once by the observer in any case.

Matchers support also filtering by means of a where clause that accepts both a list of types and an exclusion list.
Whenever a matcher finds that an entity matches its requirements, the condition of the filter is verified before to register the entity itself. Moreover, a registered entity isn't returned by the observer if the condition set by the filter is broken in the meantime.

Important

Iterators aren't invalidated if:

  • New instances of the given components are created and assigned to entities.
  • The entity currently pointed is modified (as an example, if one of the given components is removed from the entity to which the iterator points).
  • The entity currently pointed is destroyed.

In all the other cases, modifying the pools of the given components in any way invalidates all the iterators and using them results in undefined behavior.

Warning
Lifetime of an observer doesn't necessarily have to overcome the one of the registry to which it is connected. However, the observer must be disconnected from the registry before being destroyed to avoid crashes due to dangling pointers.
Template Parameters
EntityA valid entity type (see entt_traits for more details).

Member Typedef Documentation

◆ entity_type

template<typename Entity >
using entt::basic_observer< Entity >::entity_type = Entity

Underlying entity identifier.

◆ iterator_type

template<typename Entity >
using entt::basic_observer< Entity >::iterator_type = typename sparse_set<Entity>::iterator_type

Input iterator type.

◆ size_type

template<typename Entity >
using entt::basic_observer< Entity >::size_type = std::size_t

Unsigned integer type.

Constructor & Destructor Documentation

◆ basic_observer() [1/4]

template<typename Entity >
entt::basic_observer< Entity >::basic_observer ( )
inline

Default constructor.

◆ basic_observer() [2/4]

template<typename Entity >
entt::basic_observer< Entity >::basic_observer ( const basic_observer< Entity > &  )
delete

Default copy constructor, deleted on purpose.

◆ basic_observer() [3/4]

template<typename Entity >
entt::basic_observer< Entity >::basic_observer ( basic_observer< Entity > &&  )
delete

Default move constructor, deleted on purpose.

◆ basic_observer() [4/4]

template<typename Entity >
template<typename... Matcher>
entt::basic_observer< Entity >::basic_observer ( basic_registry< entity_type > &  reg,
basic_collector< Matcher... >   
)
inline

Creates an observer and connects it to a given registry.

Template Parameters
MatcherTypes of matchers to use to initialize the observer.
Parameters
regA valid reference to a registry.

◆ ~basic_observer()

template<typename Entity >
entt::basic_observer< Entity >::~basic_observer ( )
default

Default destructor.

Member Function Documentation

◆ begin()

template<typename Entity >
iterator_type entt::basic_observer< Entity >::begin ( ) const
inline

Returns an iterator to the first entity of the observer.

The returned iterator points to the first entity of the observer. If the container is empty, the returned iterator will be equal to end().

Returns
An iterator to the first entity of the observer.

◆ clear()

template<typename Entity >
void entt::basic_observer< Entity >::clear ( void  )
inline

Resets the underlying container.

◆ connect()

template<typename Entity >
template<typename... Matcher>
void entt::basic_observer< Entity >::connect ( basic_registry< entity_type > &  reg,
basic_collector< Matcher... >   
)
inline

Connects an observer to a given registry.

Template Parameters
MatcherTypes of matchers to use to initialize the observer.
Parameters
regA valid reference to a registry.

◆ data()

template<typename Entity >
const entity_type* entt::basic_observer< Entity >::data ( ) const
inline

Direct access to the list of entities of the observer.

The returned pointer is such that range [data(), data() + size()] is always a valid range, even if the container is empty.

Note
There are no guarantees on the order of the entities. Use begin and end if you want to iterate the observer in the expected order.
Returns
A pointer to the array of entities.

◆ disconnect()

template<typename Entity >
void entt::basic_observer< Entity >::disconnect ( )
inline

Disconnects an observer from the registry it keeps track of.

◆ each() [1/2]

template<typename Entity >
template<typename Func >
void entt::basic_observer< Entity >::each ( Func  func) const
inline

Iterates entities and applies the given function object to them, then clears the observer.

The function object is invoked for each entity.
The signature of the function must be equivalent to the following form:

void(const entity_type);
Template Parameters
FuncType of the function object to invoke.
Parameters
funcA valid function object.

◆ each() [2/2]

template<typename Entity >
template<typename Func >
void entt::basic_observer< Entity >::each ( Func  func)
inline

Iterates entities and applies the given function object to them, then clears the observer.

The function object is invoked for each entity.
The signature of the function must be equivalent to the following form:

void(const entity_type);
Template Parameters
FuncType of the function object to invoke.
Parameters
funcA valid function object.

◆ empty()

template<typename Entity >
bool entt::basic_observer< Entity >::empty ( ) const
inline

Checks whether an observer is empty.

Returns
True if the observer is empty, false otherwise.

◆ end()

template<typename Entity >
iterator_type entt::basic_observer< Entity >::end ( ) const
inline

Returns an iterator that is past the last entity of the observer.

The returned iterator points to the entity following the last entity of the observer. Attempting to dereference the returned iterator results in undefined behavior.

Returns
An iterator to the entity following the last entity of the observer.

◆ operator=() [1/2]

template<typename Entity >
basic_observer& entt::basic_observer< Entity >::operator= ( const basic_observer< Entity > &  )
delete

Default copy assignment operator, deleted on purpose.

Returns
This observer.

◆ operator=() [2/2]

template<typename Entity >
basic_observer& entt::basic_observer< Entity >::operator= ( basic_observer< Entity > &&  )
delete

Default move assignment operator, deleted on purpose.

Returns
This observer.

◆ size()

template<typename Entity >
size_type entt::basic_observer< Entity >::size ( ) const
inline

Returns the number of elements in an observer.

Returns
Number of elements.

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