Skip to content

Sebight/EventBus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

EventBus

A single header C++ event bus implementation for decoupled event-driven communication.

Usage

#include "event_bus.hpp"

// Define custom events (any type works)
struct PlayerDamaged {
    int playerId;
    float damage;
};

struct GameOver {
    int winnerId;
};

// Create event bus
EventBus bus;

// Subscribe to events, with the pointer to this class as a handle for later unsubscription.
bus.Subscribe<PlayerDamaged>([](const PlayerDamaged& e) {
    std::cout << "Player " << e.playerId << " took " << e.damage << " damage\n";
}, this);

bus.Subscribe<GameOver>([](const GameOver& e) {
    std::cout << "Player " << e.winnerId << " wins!\n";
}, this);

// Publish events
bus.Publish(PlayerDamaged{1, 25.0f});
bus.Publish(GameOver{1});

// Unsubscribe
bus.Unsubscribe<PlayerDamaged>(this);
bus.Unsubscribe<GameOver>(this);

Installation

Copy event_bus.hpp to your project and include it.

Limitations

  • Not thread-safe
  • Subscribers must manage their own lifetime
  • No event priority or ordering guarantees

Plans

  • More robust subscription handle system
  • Support for multiple channels (game, engine, ...)

About

A single header C++ event bus implementation for decoupled event-driven communication.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages