dxFeed Graal CXX API v8.1.0
Loading...
Searching...
No Matches
WeakEntityManager< EntityType_, EntityIdType_ > Class Template Reference

Thread-safe registry that associates callback identifiers with entities without owning them. More...

#include <EntityManager.hpp>

Public Types

using EntityType = EntityType_
 The registered entity type.
 
using EntityIdType = EntityIdType_
 The type whose identifier domain is used by the registry.
 

Public Member Functions

 WeakEntityManager ()=default
 Constructs an empty weak entity registry.
 
Id< EntityIdType > registerEntity (const std::shared_ptr< EntityType > &entity)
 Registers an entity and returns its callback identifier.
 
std::size_t getLastId ()
 
bool unregisterEntity (const std::shared_ptr< EntityType > &entity)
 Unregisters an entity.
 
bool unregisterEntity (Id< EntityIdType > id)
 Unregisters an entity by identifier.
 
template<typename H>
bool unregisterEntity (H *handle)
 Unregisters an entity using an opaque callback handle.
 
template<typename H>
bool unregisterEntity (const H *handle)
 Unregisters an entity using a constant opaque callback handle.
 
std::shared_ptr< EntityType > getEntity (Id< EntityIdType > id)
 Locks and returns the entity registered for an identifier.
 
template<typename H>
std::shared_ptr< EntityType > getEntity (H *handle)
 Locks and returns the entity identified by an opaque callback handle.
 
template<typename H>
std::shared_ptr< EntityType > getEntity (const H *handle)
 Locks and returns the entity identified by a constant opaque callback handle.
 
bool contains (Id< EntityIdType > id)
 
std::optional< Id< EntityIdType > > getId (const std::shared_ptr< EntityType > &entity)
 
bool contains (const std::shared_ptr< EntityType > &entity)
 

Detailed Description

template<typename EntityType_, typename EntityIdType_ = EntityType_>
class WeakEntityManager< EntityType_, EntityIdType_ >

Thread-safe registry that associates callback identifiers with entities without owning them.

A callback obtains a temporary shared pointer with getEntity(), so an entity cannot be destroyed while that callback is using it. Unlike EntityManager, this registry does not keep an otherwise unused entity alive. Expired entries are removed lazily during lookups and explicitly by entity destructors.

Template Parameters
EntityType_The registered entity type.
EntityIdType_The type used to distinguish the identifier domain.

Member Function Documentation

◆ contains() [1/2]

template<typename EntityType_, typename EntityIdType_ = EntityType_>
bool WeakEntityManager< EntityType_, EntityIdType_ >::contains ( const std::shared_ptr< EntityType > & entity)
inline
Returns
true if the entity is currently registered and alive.

◆ contains() [2/2]

template<typename EntityType_, typename EntityIdType_ = EntityType_>
bool WeakEntityManager< EntityType_, EntityIdType_ >::contains ( Id< EntityIdType > id)
inline
Returns
true if the identifier currently refers to a live entity.

◆ getEntity() [1/3]

template<typename EntityType_, typename EntityIdType_ = EntityType_>
template<typename H>
std::shared_ptr< EntityType > WeakEntityManager< EntityType_, EntityIdType_ >::getEntity ( const H * handle)
inline

Locks and returns the entity identified by a constant opaque callback handle.

Template Parameters
HThe callback handle type.
Parameters
handleThe handle containing the registration identifier.
Returns
The registered entity, or an empty pointer if it has expired or is not registered.

◆ getEntity() [2/3]

template<typename EntityType_, typename EntityIdType_ = EntityType_>
template<typename H>
std::shared_ptr< EntityType > WeakEntityManager< EntityType_, EntityIdType_ >::getEntity ( H * handle)
inline

Locks and returns the entity identified by an opaque callback handle.

Template Parameters
HThe callback handle type.
Parameters
handleThe handle containing the registration identifier.
Returns
The registered entity, or an empty pointer if it has expired or is not registered.

◆ getEntity() [3/3]

template<typename EntityType_, typename EntityIdType_ = EntityType_>
std::shared_ptr< EntityType > WeakEntityManager< EntityType_, EntityIdType_ >::getEntity ( Id< EntityIdType > id)
inline

Locks and returns the entity registered for an identifier.

The returned shared pointer keeps the entity alive until the caller finishes using it.

Parameters
idThe registration identifier.
Returns
The registered entity, or an empty pointer if it has expired or is not registered.

◆ getId()

template<typename EntityType_, typename EntityIdType_ = EntityType_>
std::optional< Id< EntityIdType > > WeakEntityManager< EntityType_, EntityIdType_ >::getId ( const std::shared_ptr< EntityType > & entity)
inline
Returns
The identifier of a live registered entity, if present.

◆ getLastId()

template<typename EntityType_, typename EntityIdType_ = EntityType_>
std::size_t WeakEntityManager< EntityType_, EntityIdType_ >::getLastId ( )
inline
Returns
The numeric value of the most recently assigned identifier.

◆ registerEntity()

template<typename EntityType_, typename EntityIdType_ = EntityType_>
Id< EntityIdType > WeakEntityManager< EntityType_, EntityIdType_ >::registerEntity ( const std::shared_ptr< EntityType > & entity)
inline

Registers an entity and returns its callback identifier.

Registering the same live entity more than once returns its existing identifier.

Parameters
entityThe entity to register.
Returns
The identifier associated with the entity.

◆ unregisterEntity() [1/4]

template<typename EntityType_, typename EntityIdType_ = EntityType_>
template<typename H>
bool WeakEntityManager< EntityType_, EntityIdType_ >::unregisterEntity ( const H * handle)
inline

Unregisters an entity using a constant opaque callback handle.

Template Parameters
HThe callback handle type.
Parameters
handleThe handle containing the registration identifier.
Returns
true if a registration was removed.

◆ unregisterEntity() [2/4]

template<typename EntityType_, typename EntityIdType_ = EntityType_>
bool WeakEntityManager< EntityType_, EntityIdType_ >::unregisterEntity ( const std::shared_ptr< EntityType > & entity)
inline

Unregisters an entity.

Parameters
entityThe entity to unregister.
Returns
true if a registration was removed.

◆ unregisterEntity() [3/4]

template<typename EntityType_, typename EntityIdType_ = EntityType_>
template<typename H>
bool WeakEntityManager< EntityType_, EntityIdType_ >::unregisterEntity ( H * handle)
inline

Unregisters an entity using an opaque callback handle.

Template Parameters
HThe callback handle type.
Parameters
handleThe handle containing the registration identifier.
Returns
true if a registration was removed.

◆ unregisterEntity() [4/4]

template<typename EntityType_, typename EntityIdType_ = EntityType_>
bool WeakEntityManager< EntityType_, EntityIdType_ >::unregisterEntity ( Id< EntityIdType > id)
inline

Unregisters an entity by identifier.

Parameters
idThe registration identifier.
Returns
true if a registration was removed.