ECSM v0.2.0
Managerfinal

Systems and entities coordinator. More...

#include <ecsm.hpp>

Classes

struct  Event
 Event subscribers holder. More...
 

Public Types

using Systems = std::unordered_map<std::type_index, System*>
 
using ComponentTypes = std::unordered_map<std::type_index, System*>
 
using ComponentNames = std::map<std::string, System*>
 
using Events = std::unordered_map<std::string, Event*>
 
using OrderedEvents = std::vector<const Event*>
 
using EntityPool = LinearPool<Entity>
 
using GarbageComponent = std::pair<std::type_index, ID<Entity>>
 
using GarbageComponents = std::set<GarbageComponent>
 
typedef Singleton< Manager, UseManager > Instance
 Singleton class type.
 

Public Member Functions

 Manager (bool setSingleton=true)
 Creates a new manager instance.
 
 ~Manager ()
 Destroys manager and all components/entities/systems.
 
template<class T = System, typename... Args>
void createSystem (Args &&... args)
 Creates a new system instance.
 
void destroySystem (std::type_index type)
 Terminates and destroys system.
 
template<class T = System>
void destroySystem ()
 Terminates and destroys system.
 
bool tryDestroySystem (std::type_index type)
 Terminates and destroys system if exist.
 
template<class T = System>
bool tryDestroySystem ()
 Terminates and destroys system if exist.
 
bool has (std::type_index type) const noexcept
 Returns true if system is created.
 
template<class T = System>
bool has () const noexcept
 Returns true if system is created.
 
Systemget (std::type_index type) const
 Returns system instance.
 
template<class T = System>
T * get () const
 Returns system instance.
 
SystemtryGet (std::type_index type) const noexcept
 Returns system instance if created, otherwise nullptr.
 
template<class T = System>
T * tryGet () const noexcept
 Returns system instance if created, otherwise nullptr.
 
ID< EntitycreateEntity ()
 Creates a new entity instance.
 
void destroy (ID< Entity > instance)
 Destroys entity instance and it components.
 
View< Entityget (ID< Entity > instance) const noexcept
 Returns entity internal data accessor. (View)
 
View< Componentadd (ID< Entity > entity, std::type_index componentType)
 Adds a new component to the entity.
 
template<class T = Component>
View< T > add (ID< Entity > entity)
 Adds a new component to the entity.
 
void remove (ID< Entity > entity, std::type_index componentType)
 Removes component from the entity.
 
template<class T = Component>
void remove (ID< Entity > entity)
 Removes component from the entity.
 
bool isGarbage (ID< Entity > entity, std::type_index componentType) const noexcept
 Returns true if target entity component was removed and is in the garbage pool.
 
template<class T = Component>
bool isGarbage (ID< Entity > entity) const noexcept
 Returns true if target entity component was removed and is in the garbage pool.
 
void copy (ID< Entity > source, ID< Entity > destination, std::type_index componentType)
 Copies component data from source entity to destination.
 
template<class T = Component>
void copy (ID< Entity > source, ID< Entity > destination)
 Copies component data from source entity to destination.
 
ID< Entityduplicate (ID< Entity > entity)
 Creates a duplicate of specified entity.
 
bool has (ID< Entity > entity, std::type_index componentType) const noexcept
 Returns true if entity has target component.
 
template<class T = Component>
bool has (ID< Entity > entity) const noexcept
 Returns true if entity has target component.
 
View< Componentget (ID< Entity > entity, std::type_index componentType) const
 Returns component data accessor. (View)
 
template<class T = Component>
View< T > get (ID< Entity > entity) const
 Returns component data accessor. (View)
 
View< ComponenttryGet (ID< Entity > entity, std::type_index componentType) const noexcept
 Returns component data accessor if exist, otherwise null. (View)
 
template<class T = Component>
View< T > tryGet (ID< Entity > entity) const noexcept
 Returns component data accessor if exist, otherwise null. (View)
 
ID< ComponentgetID (ID< Entity > entity, std::type_index componentType) const
 Returns entity component ID.
 
template<class T = Component>
ID< T > getID (ID< Entity > entity) const
 Returns entity component ID.
 
ID< ComponenttryGetID (ID< Entity > entity, std::type_index componentType) const noexcept
 Returns entity component ID if added, otherwise null.
 
template<class T = Component>
ID< T > tryGetID (ID< Entity > entity) const noexcept
 Returns entity component ID if added, otherwise null.
 
uint32_t getComponentCount (ID< Entity > entity) const noexcept
 Returns entity component count.
 
void registerEvent (const std::string &name)
 Registers a new unordered event.
 
bool tryRegisterEvent (const std::string &name)
 Registers a new unordered event if not exist.
 
void registerEventBefore (const std::string &newEvent, const std::string &beforeEvent)
 Registers a new ordered event before another.
 
void registerEventAfter (const std::string &newEvent, const std::string &afterEvent)
 Registers a new ordered event after another.
 
void unregisterEvent (const std::string &name)
 Unregisters existing event.
 
bool tryUnregisterEvent (const std::string &name)
 Unregisters event if exist.
 
bool hasEvent (const std::string &name) const noexcept
 Returns true if event is registered.
 
bool isEventOrdered (const std::string &name) const
 Returns true if event is ordered.
 
const Event::SubscribersgetEventSubscribers (const std::string &name) const
 Returns all event subscribers.
 
bool isEventHasSubscribers (const std::string &name) const
 Returns true if event has subscribers.
 
void runEvent (const std::string &name)
 Calls all event subscribers.
 
bool tryRunEvent (const std::string &name)
 Calls all event subscribers if event exist.
 
void runOrderedEvents ()
 Runs all ordered events.
 
void subscribeToEvent (const std::string &name, const std::function< void()> &onEvent)
 Adds a new event subscriber.
 
void unsubscribeFromEvent (const std::string &name, const std::function< void()> &onEvent)
 Removes existing event subscriber.
 
bool trySubscribeToEvent (const std::string &name, const std::function< void()> &onEvent)
 Adds a new event subscriber if not exist.
 
bool tryUnsubscribeFromEvent (const std::string &name, const std::function< void()> &onEvent)
 Removes existing event subscriber if exist.
 
const SystemsgetSystems () const noexcept
 Returns all manager systems.
 
const ComponentTypesgetComponentTypes () const noexcept
 Returns all manager component types.
 
const ComponentNamesgetComponentNames () const noexcept
 Returns all manager component names.
 
const EventsgetEvents () const noexcept
 Returns all manager events.
 
const OrderedEventsgetOrderedEvents () const noexcept
 Returns ordered manager events.
 
const EntityPoolgetEntities () const noexcept
 Returns all manager entities.
 
const GarbageComponentsgetGarbageComponents () const noexcept
 Returns manager garbage components pool.
 
bool isInitialized () const noexcept
 Returns true if manager is initialized.
 
void initialize ()
 Initializes all created systems.
 
void update ()
 Runs ordered events and disposes destroyed resources on each tick.
 
void start ()
 Enters update loop. Executes update() on each tick.
 
void disposeGarbageComponents ()
 Actually destroys garbage components.
 
void disposeSystemComponents ()
 Actually destroys system components and internal resources.
 
void disposeEntities ()
 Actually destroys entities.
 
void lock ()
 Locks manager for synchronous access. (MT-Safe)
 
bool tryLock () noexcept
 Tries to locks manager for synchronous access. (MT-Safe)
 
void unlock () noexcept
 Unlock manager after synchronous access. (MT-Safe)
 
void setSingletonCurrent () noexcept
 Sets manager singleton to this instance.
 
void unsetSingletonCurrent () noexcept
 Unsets manager singleton instance.
 

Static Public Member Functions

static bool has ()
 Returns true if class singleton or manager instance is exist.
 
static Managerget ()
 Returns class singleton or manager instance.
 
static ManagertryGet ()
 Returns class singleton or manager instance if exist.
 

Public Attributes

bool isRunning = false
 

Protected Member Functions

void setSingleton ()
 Sets a new class singleton instance.
 
void unsetSingleton () noexcept
 Unsets this class singleton instance.
 

Static Protected Attributes

static ManagersingletonInstance
 Singleton class instance.
 

Detailed Description

Systems and entities coordinator.

Manager serves as a central coordinating object responsible for overseeing various aspects of the system. It manages entity-related tasks such as creation, destruction, and component assignment. Additionally, it handles the storage and retrieval of components, facilitates system initialization, configuration and supports event handling for communication and reaction to changes.

  • PreInit event or phase is the earliest stage in the initialization process. It occurs before most of the system's or component's initialization logic runs. This phase is typically used for preliminary setup tasks that need to occur before the main initialization.
  • Init event or phase is the main stage of initialization. During this phase, components and systems perform their core setup tasks. This includes initializing internal data structures, loading resources, or setting up dependencies with other components or systems.
  • PostInit event or phase happens after the main initialization logic. This stage is used for tasks that must occur after all components and systems have been initialized. It's particularly useful for setup steps that require all other components to be in a ready state or for cross-component communications and linking.

Constructor & Destructor Documentation

◆ Manager()

Manager ( bool setSingleton = true)

Creates a new manager instance.

Parameters
setSingletonset manager singleton instance

Member Function Documentation

◆ createSystem()

template<class T = System, typename... Args>
void createSystem ( Args &&... args)
inline

Creates a new system instance.

Instantiates a new system and registers it component, but initialization occurs only after the initialize() call.

Template Parameters
Ttarget system type
Argsadditional argument types
Parameters
argsadditional system creation arguments
Exceptions
EcsmErrorif system is already created or component type registered.

◆ destroySystem() [1/2]

void destroySystem ( std::type_index type)

Terminates and destroys system.

Parameters
typetarget system typeid()
Exceptions
EcsmErrorif system is not found.

◆ destroySystem() [2/2]

template<class T = System>
void destroySystem ( )
inline

Terminates and destroys system.

Template Parameters
Ttarget system type
Exceptions
EcsmErrorif system is not found.

◆ tryDestroySystem() [1/2]

bool tryDestroySystem ( std::type_index type)

Terminates and destroys system if exist.

Parameters
typetarget system typeid()
Returns
True if system is destroyed, otherwise false.

◆ tryDestroySystem() [2/2]

template<class T = System>
bool tryDestroySystem ( )
inline

Terminates and destroys system if exist.

Template Parameters
Ttarget system type
Returns
True if system is destroyed, otherwise false.

◆ has() [1/4]

bool has ( std::type_index type) const
inlinenoexcept

Returns true if system is created.

Parameters
typetarget system typeid()

◆ has() [2/4]

template<class T = System>
bool has ( ) const
inlinenoexcept

Returns true if system is created.

Template Parameters
Ttarget system type

◆ get() [1/5]

System * get ( std::type_index type) const
inline

Returns system instance.

Warning
Be carefull with system pointer, it can be destroyed later.
Parameters
typetarget system typeid()
Exceptions
EcsmErrorif system is not found.

◆ get() [2/5]

template<class T = System>
T * get ( ) const
inline

Returns system instance.

Template Parameters
Ttarget system type
Exceptions
EcsmErrorif system is not found.

◆ tryGet() [1/4]

System * tryGet ( std::type_index type) const
inlinenoexcept

Returns system instance if created, otherwise nullptr.

Parameters
typetarget system typeid()

◆ tryGet() [2/4]

template<class T = System>
T * tryGet ( ) const
inlinenoexcept

Returns system instance if created, otherwise nullptr.

Template Parameters
Ttarget system type

◆ createEntity()

ID< Entity > createEntity ( )
inline

Creates a new entity instance.

Created entity does not have any default component.

◆ destroy()

void destroy ( ID< Entity > instance)
inline

Destroys entity instance and it components.

Note
Entities are not destroyed immediately, only after the dispose call.
Parameters
instancetarget entity instance or null

◆ get() [3/5]

View< Entity > get ( ID< Entity > instance) const
inlinenoexcept

Returns entity internal data accessor. (View)

Warning
Do not store views, use them only in place. Because entity memory can be reallocated later.
Parameters
instancetarget entity instance

◆ add() [1/2]

View< Component > add ( ID< Entity > entity,
std::type_index componentType )

Adds a new component to the entity.

See the add<T>(ID<Entity> entity).

Warning
Do not store views, use them only in place. Because component memory can be reallocated later.
Parameters
entityentity instance
componentTypetarget component typeid()
Returns
Returns View of the created component.
Exceptions
EcsmErrorif component type is not registered, or component is already added.

◆ add() [2/2]

template<class T = Component>
View< T > add ( ID< Entity > entity)
inline

Adds a new component to the entity.

Only one component type can be attached to the entity, but several components with different types. Target component should be registered by some System.

Warning
Do not store views, use them only in place. Because component memory can be reallocated later.
Parameters
entityentity instance
Template Parameters
Ttarget component type
Returns
Returns View of the created component.
Exceptions
EcsmErrorif component type is not registered, or component is already added.

◆ remove() [1/2]

void remove ( ID< Entity > entity,
std::type_index componentType )

Removes component from the entity.

See the remove<T>(ID<Entity> entity).

Note
Components are not destroyed immediately, only after the dispose call.
Parameters
entityentity instance
componentTypetarget component typeid()
Exceptions
EcsmErrorif component is not found.

◆ remove() [2/2]

template<class T = Component>
void remove ( ID< Entity > entity)
inline

Removes component from the entity.

Component data destruction is handled by the System.

Note
Components are not destroyed immediately, only after the dispose call.
Parameters
entityentity instance
Template Parameters
Ttarget component type
Exceptions
EcsmErrorif component is not found.

◆ isGarbage() [1/2]

bool isGarbage ( ID< Entity > entity,
std::type_index componentType ) const
inlinenoexcept

Returns true if target entity component was removed and is in the garbage pool.

See the remove<T>(ID<Entity> entity).

Note
Components are not destroyed immediately, only after the dispose call.
Parameters
entityentity instance
componentTypetarget component typeid()

◆ isGarbage() [2/2]

template<class T = Component>
bool isGarbage ( ID< Entity > entity) const
inlinenoexcept

Returns true if target entity component was removed and is in the garbage pool.

See the remove<T>(ID<Entity> entity).

Note
Components are not destroyed immediately, only after the dispose call.
Parameters
entityentity instance
Template Parameters
Ttarget component type

◆ copy() [1/2]

void copy ( ID< Entity > source,
ID< Entity > destination,
std::type_index componentType )

Copies component data from source entity to destination.

See the copy<T>(ID<Entity> source, ID<Entity> destination).

Parameters
sourcecopy from entity instance
destinationcopy to entity instance
componentTypetarget component typeid()
Exceptions
EcsmErrorif source or destination component is not found.

◆ copy() [2/2]

template<class T = Component>
void copy ( ID< Entity > source,
ID< Entity > destination )
inline

Copies component data from source entity to destination.

Component data copying is handled by the System.

Parameters
sourcecopy from entity instance
destinationcopy to entity instance
Template Parameters
Ttarget component type
Exceptions
EcsmErrorif source or destination component is not found.

◆ duplicate()

ID< Entity > duplicate ( ID< Entity > entity)

Creates a duplicate of specified entity.

Component data copying is handled by the System.

Parameters
entitytarget entity instance to duplicate from

◆ has() [3/4]

bool has ( ID< Entity > entity,
std::type_index componentType ) const
inlinenoexcept

Returns true if entity has target component.

Note
It also checks for component in the garbage pool.
Parameters
entityentity instance
componentTypetarget component typeid()

◆ has() [4/4]

template<class T = Component>
bool has ( ID< Entity > entity) const
inlinenoexcept

Returns true if entity has target component.

Note
It also checks for component in the garbage pool.
Parameters
entityentity instance
Template Parameters
Ttarget component type

◆ get() [4/5]

View< Component > get ( ID< Entity > entity,
std::type_index componentType ) const
inline

Returns component data accessor. (View)

Warning
Do not store views, use them only in place. Because component memory can be reallocated later.
Parameters
entityentity instance
componentTypetarget component typeid()
Exceptions
EcsmErrorif component is not found.

◆ get() [5/5]

template<class T = Component>
View< T > get ( ID< Entity > entity) const
inline

Returns component data accessor. (View)

Warning
Do not store views, use them only in place. Because component memory can be reallocated later.
Parameters
entityentity instance
Template Parameters
Ttarget component type
Exceptions
EcsmErrorif component is not found.

◆ tryGet() [3/4]

View< Component > tryGet ( ID< Entity > entity,
std::type_index componentType ) const
inlinenoexcept

Returns component data accessor if exist, otherwise null. (View)

Warning
Do not store views, use them only in place. Because component memory can be reallocated later.
Note
It also checks for component in the garbage pool.
Parameters
entityentity instance
componentTypetarget component typeid()

◆ tryGet() [4/4]

template<class T = Component>
View< T > tryGet ( ID< Entity > entity) const
inlinenoexcept

Returns component data accessor if exist, otherwise null. (View)

Warning
Do not store views, use them only in place. Because component memory can be reallocated later.
Note
It also checks for component in the garbage pool.
Parameters
entityentity instance
Template Parameters
Ttarget component type

◆ getID() [1/2]

ID< Component > getID ( ID< Entity > entity,
std::type_index componentType ) const
inline

Returns entity component ID.

See the getID<T>(ID<Entity> entity).

Parameters
entityentity instance
componentTypetarget component typeid()
Exceptions
EcsmErrorif component is not found.

◆ getID() [2/2]

template<class T = Component>
ID< T > getID ( ID< Entity > entity) const
inline

Returns entity component ID.

Useful when we need to access and store entity component IDs.

Parameters
entityentity instance
Template Parameters
Ttarget component type
Exceptions
EcsmErrorif component is not found.

◆ tryGetID() [1/2]

ID< Component > tryGetID ( ID< Entity > entity,
std::type_index componentType ) const
inlinenoexcept

Returns entity component ID if added, otherwise null.

Note
It also checks for component in the garbage pool.

See the tryGetID<T>(ID<Entity> entity).

Parameters
entityentity instance
componentTypetarget component typeid()

◆ tryGetID() [2/2]

template<class T = Component>
ID< T > tryGetID ( ID< Entity > entity) const
inlinenoexcept

Returns entity component ID if added, otherwise null.

Useful when we need to access and store entity component identifiers.

Parameters
entityentity instance
Template Parameters
Ttarget component type

◆ getComponentCount()

uint32_t getComponentCount ( ID< Entity > entity) const
inlinenoexcept

Returns entity component count.

Parameters
entitytarget entity instance

◆ registerEvent()

void registerEvent ( const std::string & name)

Registers a new unordered event.

Parameters
[in]nametarget event name
Exceptions
EcsmErrorif event is already registered.

◆ tryRegisterEvent()

bool tryRegisterEvent ( const std::string & name)

Registers a new unordered event if not exist.

Parameters
[in]nametarget event name
Returns
True if event is registered, otherwise false.

◆ registerEventBefore()

void registerEventBefore ( const std::string & newEvent,
const std::string & beforeEvent )

Registers a new ordered event before another.

Parameters
[in]newEventtarget event name
[in]beforeEventname of the event after target event
Exceptions
EcsmErrorif event is already registered.

◆ registerEventAfter()

void registerEventAfter ( const std::string & newEvent,
const std::string & afterEvent )

Registers a new ordered event after another.

Parameters
[in]newEventtarget event name
[in]afterEventname of the event before target event
Exceptions
EcsmErrorif event is already registered.

◆ unregisterEvent()

void unregisterEvent ( const std::string & name)

Unregisters existing event.

Parameters
[in]nametarget event name
Exceptions
EcsmErrorif event is not registered, or not found.

◆ tryUnregisterEvent()

bool tryUnregisterEvent ( const std::string & name)

Unregisters event if exist.

Parameters
[in]nametarget event name
Returns
True if event is unregistered, otherwise false.

◆ hasEvent()

bool hasEvent ( const std::string & name) const
inlinenoexcept

Returns true if event is registered.

Parameters
[in]nametarget event name

◆ isEventOrdered()

bool isEventOrdered ( const std::string & name) const

Returns true if event is ordered.

Parameters
[in]nametarget event name
Exceptions
EcsmErrorif event is not registered.

◆ getEventSubscribers()

const Event::Subscribers & getEventSubscribers ( const std::string & name) const

Returns all event subscribers.

Parameters
[in]nametarget event name
Exceptions
EcsmErrorif event is not registered.

◆ isEventHasSubscribers()

bool isEventHasSubscribers ( const std::string & name) const

Returns true if event has subscribers.

Parameters
[in]nametarget event name
Exceptions
EcsmErrorif event is not registered.

◆ runEvent()

void runEvent ( const std::string & name)

Calls all event subscribers.

Parameters
[in]nametarget event name
Exceptions
EcsmErrorif event is not registered.

◆ tryRunEvent()

bool tryRunEvent ( const std::string & name)

Calls all event subscribers if event exist.

Parameters
[in]nametarget event name
Returns
True if event is found.

◆ runOrderedEvents()

void runOrderedEvents ( )

Runs all ordered events.

Unordered events subscribers are not called.

◆ subscribeToEvent()

void subscribeToEvent ( const std::string & name,
const std::function< void()> & onEvent )

Adds a new event subscriber.

Parameters
[in]nametarget event name
[in]onEventon event function callback
Exceptions
EcsmErrorif event is not registered.

◆ unsubscribeFromEvent()

void unsubscribeFromEvent ( const std::string & name,
const std::function< void()> & onEvent )

Removes existing event subscriber.

Parameters
[in]nametarget event name
[in]onEventon event function callback
Exceptions
EcsmErrorif event is not registered, or not subscribed.

◆ trySubscribeToEvent()

bool trySubscribeToEvent ( const std::string & name,
const std::function< void()> & onEvent )

Adds a new event subscriber if not exist.

Parameters
[in]nametarget event name
[in]onEventon event function callback
Returns
True if subscribed to the event, otherwise false.

◆ tryUnsubscribeFromEvent()

bool tryUnsubscribeFromEvent ( const std::string & name,
const std::function< void()> & onEvent )

Removes existing event subscriber if exist.

Parameters
[in]nametarget event name
[in]onEventon event function callback
Exceptions
Trueif unsubscribed from the event, otherwise false.

◆ getSystems()

const Systems & getSystems ( ) const
inlinenoexcept

Returns all manager systems.

Note
Use manager functions to access systems.

◆ getComponentTypes()

const ComponentTypes & getComponentTypes ( ) const
inlinenoexcept

Returns all manager component types.

Note
Use manager functions to access components.

◆ getComponentNames()

const ComponentNames & getComponentNames ( ) const
inlinenoexcept

Returns all manager component names.

Note
Use manager functions to access components.

◆ getEvents()

const Events & getEvents ( ) const
inlinenoexcept

Returns all manager events.

Note
Use manager functions to access events.

◆ getOrderedEvents()

const OrderedEvents & getOrderedEvents ( ) const
inlinenoexcept

Returns ordered manager events.

Note
Use manager functions to access events.

◆ getEntities()

const EntityPool & getEntities ( ) const
inlinenoexcept

Returns all manager entities.

Note
Use manager functions to access entities.

◆ getGarbageComponents()

const GarbageComponents & getGarbageComponents ( ) const
inlinenoexcept

Returns manager garbage components pool.

Note
Use manager functions to check if component is garbage.

◆ initialize()

void initialize ( )

Initializes all created systems.

Exceptions
EcsmErrorif manager is already initialized.

◆ update()

void update ( )

Runs ordered events and disposes destroyed resources on each tick.

Exceptions
EcsmErrorif manager is not initialized.

◆ start()

void start ( )

Enters update loop. Executes update() on each tick.

Exceptions
EcsmErrorif manager is not initialized.

◆ disposeGarbageComponents()

void disposeGarbageComponents ( )

Actually destroys garbage components.

Components are not destroyed immediately, only after the dispose call.

◆ disposeSystemComponents()

void disposeSystemComponents ( )

Actually destroys system components and internal resources.

Systen components are not destroyed immediately, only after the dispose call.

◆ disposeEntities()

void disposeEntities ( )
inline

Actually destroys entities.

Entities are not destroyed immediately, only after the dispose call.

◆ lock()

void lock ( )
inline

Locks manager for synchronous access. (MT-Safe)

Note
Use it if you want to access manager from multiple threads asynchronously.

◆ tryLock()

bool tryLock ( )
inlinenoexcept

Tries to locks manager for synchronous access. (MT-Safe)

Note
Use it if you want to access manager from multiple threads asynchronously.

◆ unlock()

void unlock ( )
inlinenoexcept

Unlock manager after synchronous access. (MT-Safe)

Note
Always unlock manager after synchronous access!

◆ setSingletonCurrent()

void setSingletonCurrent ( )
inlinenoexcept

Sets manager singleton to this instance.

Useful in cases when we need to switch between multiple managers.

◆ unsetSingletonCurrent()

void unsetSingletonCurrent ( )
inlinenoexcept

Unsets manager singleton instance.

See the setSingletonCurrent().