entt  2.0.1
Public Types | Public Member Functions | List of all members
entt::PersistentView< Entity, Component > Class Template Referencefinal

Persistent view. More...

#include <view.hpp>

Public Types

using iterator_type = typename view_type::iterator_type
 
using entity_type = typename view_type::entity_type
 Underlying entity identifier.
 
using size_type = typename view_type::size_type
 Unsigned integer type.
 

Public Member Functions

 PersistentView (view_type &view, pool_type< Component > &... pools) noexcept
 Constructs a persistent view around a dedicated pool of entities. More...
 
size_type size () const noexcept
 Returns the number of entities that have the given components. More...
 
const entity_type * data () const noexcept
 Direct access to the list of entities. More...
 
iterator_type begin () const noexcept
 Returns an iterator to the first entity that has the given components. More...
 
iterator_type end () const noexcept
 Returns an iterator that is past the last entity that has the given components. More...
 
template<typename Comp >
const Comp & get (entity_type entity) const noexcept
 Returns the component assigned to the given entity. More...
 
template<typename Comp >
Comp & get (entity_type entity) noexcept
 Returns the component assigned to the given entity. More...
 
template<typename Comp >
void sort ()
 Sort the shared pool of entities according to the given component. More...
 

Detailed Description

template<typename Entity, typename... Component>
class entt::PersistentView< Entity, Component >

Persistent view.

A persistent view returns all the entities and only the entities that have at least the given components. Moreover, it's guaranteed that the entity list is thightly packed in memory for fast iterations.
In general, persistent views don't stay true to the order of any set of components unless users explicitly sort them.

Important

Iterators aren't invalidated if:

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

Note
Views share references to the underlying data structures with the Registry that generated them. Therefore any change to the entities and to the components made by means of the registry are immediately reflected by views.
Moreover, sorting a persistent view affects all the other views of the same type (it means that users don't have to call sort on each view to sort all of them because they share the set of entities).
Warning
Lifetime of a view must overcome the one of the registry that generated it. In any other case, attempting to use a view results in undefined behavior.
See also
View
View<Entity, Component>
Template Parameters
EntityA valid entity type (see entt_traits for more details).
ComponentThe types of the components iterated by the view.

Definition at line 52 of file view.hpp.

Member Typedef Documentation

◆ iterator_type

template<typename Entity , typename... Component>
using entt::PersistentView< Entity, Component >::iterator_type = typename view_type::iterator_type

Input iterator type.

Definition at line 62 of file view.hpp.

Constructor & Destructor Documentation

◆ PersistentView()

template<typename Entity , typename... Component>
entt::PersistentView< Entity, Component >::PersistentView ( view_type &  view,
pool_type< Component > &...  pools 
)
inlineexplicitnoexcept

Constructs a persistent view around a dedicated pool of entities.

A persistent view is created out of:

  • A dedicated pool of entities that is shared between all the persistent views of the same type.
  • A bunch of pools of components to which to refer to get instances.
Parameters
viewShared reference to a dedicated pool of entities.
poolsReferences to pools of components.

Definition at line 79 of file view.hpp.

Member Function Documentation

◆ begin()

template<typename Entity , typename... Component>
iterator_type entt::PersistentView< Entity, Component >::begin ( ) const
inlinenoexcept

Returns an iterator to the first entity that has the given components.

The returned iterator points to the first entity that has the given components. If the view is empty, the returned iterator will be equal to end().

Note
Input iterators stay true to the order imposed to the underlying data structures.
Returns
An iterator to the first entity that has the given components.

Definition at line 121 of file view.hpp.

◆ data()

template<typename Entity , typename... Component>
const entity_type* entt::PersistentView< Entity, Component >::data ( ) const
inlinenoexcept

Direct access to the list of entities.

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 view in the expected order.
Returns
A pointer to the array of entities.

Definition at line 103 of file view.hpp.

◆ end()

template<typename Entity , typename... Component>
iterator_type entt::PersistentView< Entity, Component >::end ( ) const
inlinenoexcept

Returns an iterator that is past the last entity that has the given components.

The returned iterator points to the entity following the last entity that has the given components. Attempting to dereference the returned iterator results in undefined behavior.

Note
Input iterators stay true to the order imposed to the underlying data structures.
Returns
An iterator to the entity following the last entity that has the given components.

Definition at line 140 of file view.hpp.

◆ get() [1/2]

template<typename Entity , typename... Component>
template<typename Comp >
const Comp& entt::PersistentView< Entity, Component >::get ( entity_type  entity) const
inlinenoexcept

Returns the component assigned to the given entity.

Prefer this function instead of Registry::get during iterations. It has far better performance than its companion function.

Warning
Attempting to use an invalid component type results in a compilation error. Attempting to use an entity that doesn't belong to the view results in undefined behavior.
An assertion will abort the execution at runtime in debug mode if the view doesn't contain the given entity.
Template Parameters
CompThe type of the component to get.
Parameters
entityA valid entity identifier.
Returns
The component assigned to the entity.

Definition at line 162 of file view.hpp.

◆ get() [2/2]

template<typename Entity , typename... Component>
template<typename Comp >
Comp& entt::PersistentView< Entity, Component >::get ( entity_type  entity)
inlinenoexcept

Returns the component assigned to the given entity.

Prefer this function instead of Registry::get during iterations. It has far better performance than its companion function.

Warning
Attempting to use an invalid component type results in a compilation error. Attempting to use an entity that doesn't belong to the view results in undefined behavior.
An assertion will abort the execution at runtime in debug mode if the view doesn't contain the given entity.
Template Parameters
CompThe type of the component to get.
Parameters
entityA valid entity identifier.
Returns
The component assigned to the entity.

Definition at line 184 of file view.hpp.

◆ size()

template<typename Entity , typename... Component>
size_type entt::PersistentView< Entity, Component >::size ( ) const
inlinenoexcept

Returns the number of entities that have the given components.

Returns
The number of entities that have the given components.

Definition at line 87 of file view.hpp.

◆ sort()

template<typename Entity , typename... Component>
template<typename Comp >
void entt::PersistentView< Entity, Component >::sort ( )
inline

Sort the shared pool of entities according to the given component.

Persistent views of the same type share with the Registry a pool of entities with its own order that doesn't depend on the order of any pool of components. Users can order the underlying data structure so that it respects the order of the pool of the given component.

Note
The shared pool of entities and thus its order is affected by the changes to each and every pool of components that it tracks. Therefore changes to the pools of components can quickly ruin the order imposed to the pool of entities shared between the persistent views.
Template Parameters
CompThe type of the component to use to impose the order.

Definition at line 205 of file view.hpp.


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