eventbus is a simple, header only C++17 event bus library that doesn't require you to inherit from any sort of event class. The library implements the "Mediator" pattern. This pattern is useful when you want components to communicate to each other without necessarily "knowing" about each other. Effectively, this is a thread safe event dispatcher with a list of callbacks.
- Does not require event object inheritance A base Eventclass is not requied for use withdp::event_bus. Any class/struct can be used as an event object.
- Flexible Callback Types eventbussupports a variety different types of callbacks including:- Lambdas
- Class member functions
- Free functions
 
- Flexible Callbacks No parameter callbacks are also supported as well as taking the event type by value or by const &.
- RAII de-registrations The handler registration objects automatically de-register the handler upon destruction.
- Thread safety Multiple threads can fire events at once to the same event_bus. Handlers can also be registered from different threads.- Note: While the library can handle events fired from different threads note that the thread that fires the event is also the thread that the callback will run on. This library does not ensure that the callback is run on the thread it was registered on. This may or may not be the desired behavior especially in the context of something like thread pools.
 
- Multiple Storage Types Choose to store event types in the bus either using std::anyorstd::variant.
The basic premise of the event_bus is that with it, you can:
- Register handlers
- Fire events that call the corresponding handlers
Selecting a storage type is as simple as instantiating the event_bus:
dp::event_bus any_bus; // uses std::any
dp::event_bus<event_t_1, event_t_2> variant_bus; // uses std::variantDeclaring the event_bus with no template arguments will default to using std::any as the storage type. If you want to use std::variant you must specify the event types you want to use.
The "event" object can be any class or structure you want.
void event_callback(event_type evt)
{
    // event callback logic...
}
dp::event_bus evt_bus;
const auto registration_handler = evt_bus.register_handler<event_type>(&event_callback)dp::event_bus evt_bus;
const auto registration_handler = evt_bus.register_handler<event_type>([](const event_type& evt)
{
    // logic code...
});class event_handler
{
    public:
        void on_event(event_type type)
        {
            // handler logic...
        }
};
// other code
dp::event_bus evt_bus;
event_handler handler;
const auto registration_handler = evt_bus.register_handler<event_type>(&handler, &event_handler::on_event);Note: You can't mix a class instance of type T with the member function of another class (i.e. &U::function_name).
event_type evt
{
    // data and info..
};
dp::event_bus evt_bus;
evt_bus.fire_event(evt); // all connect handler for the given event type will be fired.A complete example can be seen in the demo project.
eventbus is a header only library. All the files you need are in the eventbus/include folder. To use the library just include eventbus/event_bus.hpp.
eventbus defines three CMake INTERFACE targets that can be used in your project:
- eventbus
- eventbus::eventbus
- dp::eventbus
find_package(dp::eventbus REQUIRED)Alternatively, you can use something like CPM which is based on CMake's Fetch_Content module.
CPMAddPackage(
    NAME eventbus
    GITHUB_REPOSITORY DeveloperPaul123/eventbus
    GIT_TAG #053902d63de5529ee65d965f8b1fb0851eceed24 change this to latest commit/release tag
)π§ This library will be on vcpkg soon. π§
In general, all callback functions must return void. Currently, eventbus only supports single argument functions as callbacks.
The following use cases are not supported:
- Registering a callback inside an event callback.
- De-registering a callback inside an event callback.
If you find an issue with this library please file an issue. Pull requests are also welcome! Please see the contribution guidelines for more information.
The project is licensed under the Apache License Version 2.0. See LICENSE for more details.
| @DeveloperPaul123 | 
|---|
See here for a list of contributors.