ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
Imago::Engine::Broadcaster Class Reference

Broadcaster acts as ImagoEngine's event system. It lets systems communicate without knowing about eachother. More...

#include <Imago/Engine/Broadcaster.hpp>

Public Types

template<typename T>
using SystemFn = void (*)(SceneContext&, const T&)
 Function pointer to a system that recieves SceneContext and an EventType and returns nothing.

Public Member Functions

 Broadcaster (SceneContext &ctx)
 Construct a new Broadcaster object with the current SceneContext.
 Broadcaster (const Broadcaster &)=delete
Broadcaster & operator= (const Broadcaster &)=delete
 Broadcaster (Broadcaster &&)=delete
Broadcaster & operator= (Broadcaster &&)=delete
template<typename T>
void emit (const T &event)
 Call every system subscribed to EventType T in subcription order.
template<typename T>
void subscribe (SystemFn< T > system)
 Adds the system function pointer to the list for EventType T. Called during scene initialization.
template<typename T>
void unsubscribe (SystemFn< T > system)
 Removes the system function pointer from the list for EventType T.
void clear ()
 Drops every subscription for every event type.

Detailed Description

Broadcaster acts as ImagoEngine's event system. It lets systems communicate without knowing about eachother.

Subscribed systems subscribe to an event and the Broadcaster stores them as type-erased std::function<void(SceneContext&, const void*)> and their addresses in a Subscriber wrapper. When emit is called for that even the Broadcaster iterates through all the subscribers corresponding to that event and invokes them.

Member Typedef Documentation

◆ SystemFn

template<typename T>
using Imago::Engine::Broadcaster::SystemFn = void (*)(SceneContext&, const T&)

Function pointer to a system that recieves SceneContext and an EventType and returns nothing.

Template Parameters
TheEventType.

Constructor & Destructor Documentation

◆ Broadcaster()

Imago::Engine::Broadcaster::Broadcaster ( SceneContext & ctx)
explicit

Construct a new Broadcaster object with the current SceneContext.

Parameters
ctxThe current SceneContext.

Member Function Documentation

◆ emit()

template<typename T>
void Imago::Engine::Broadcaster::emit ( const T & event)

Call every system subscribed to EventType T in subcription order.

emit<T>() simply does nothing if there are no systems subscribed to a particular event. It also increments _broadcast depth for every emit currently happening.

Template Parameters
TheEvent Type data to broadcast
Parameters
eventThe Event to emit.

◆ subscribe()

template<typename T>
void Imago::Engine::Broadcaster::subscribe ( SystemFn< T > system)

Adds the system function pointer to the list for EventType T. Called during scene initialization.

Template Parameters
TheEvent to subscribe to
Parameters
systemThe function pointer to the system that is subscribing.

◆ unsubscribe()

template<typename T>
void Imago::Engine::Broadcaster::unsubscribe ( SystemFn< T > system)

Removes the system function pointer from the list for EventType T.

This preserves the order of the remaining subscribers. It finds the specific system to unsubscribe from by comparing the ErasedFn address to the one's of those in the event list.

Template Parameters
TheEvent to unsubscribe from.
Parameters
systemThe function pointer to the system that is unsubscribing.

The documentation for this class was generated from the following files:
  • engine/include/Imago/Engine/Broadcaster.hpp
  • engine/src/Engine/Broadcaster.cpp