CPP-TOOLBOX
Loading...
Searching...
No Matches
SignalEmitter Class Reference

A type-safe signal emitter that allows different signal types (represented as structs). More...

#include <signal_emitter.hpp>

Public Member Functions

template<typename Signal>
void connect (std::function< void(const Signal &)> listener)
 
template<typename Signal>
void emit (const Signal &signal)
 

Detailed Description

A type-safe signal emitter that allows different signal types (represented as structs).

Each signal type is a distinct struct that defines the data associated with that event. Listeners can register callbacks for a specific signal type, and when that signal is emitted, all corresponding callbacks are invoked with the signal instance.

This design avoids string-based dispatch and leverages compile-time type safety.

Example:

struct PlayerJoined { int player_id; std::string name; };
struct PlayerLeft { int player_id; };
SignalEmitter emitter;
emitter.connect<PlayerJoined>([](const PlayerJoined& e) {
std::cout << e.name << " joined (id=" << e.player_id << ")\n";
});
emitter.connect<PlayerLeft>([](const PlayerLeft& e) {
std::cout << "Player " << e.player_id << " left\n";
});
emitter.emit(PlayerJoined{42, "Joe"});
emitter.emit(PlayerLeft{42});
void connect(std::function< void(const Signal &)> listener)
Definition signal_emitter.hpp:64
void emit(const Signal &signal)
Definition signal_emitter.hpp:83
@ e
Definition input_state.hpp:31
Note
Suppose there is a signal type T and a listener is connected to it. Since signals are emitted within a specific instance of SignalEmitter, other SignalEmitter instances even if they use the same signal type T will not trigger listeners connected to a different object.
Examples
/home/runner/work/website/website/code/src/utility/reactive_unordered_map/reactive_unordered_map.hpp, and /home/runner/work/website/website/code/src/utility/signal_emitter/signal_emitter.hpp.

Member Function Documentation

◆ connect()

template<typename Signal>
void SignalEmitter::connect ( std::function< void(const Signal &)> listener)
inline

◆ emit()

template<typename Signal>
void SignalEmitter::emit ( const Signal & signal)
inline

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