eventbus
eventbus
is a simple, header only C++17 event bus library that doesn't require you to inherit from any sort of event
class.
Design Goals
eventbus
implements the "Mediator" pattern. This pattern is useful when you want components to communicate to each other without necessarily "knowing" about each other. This can be useful in some situations but should be used with caution (there are alternative design patterns to consider).
- Do not require event object inheritance I wanted to implement an event bus system that doesn't require users to inherit from some base
Event
class in order to use the event class. - Flexible Callback Types It's important that the library supports a variety different types of callbacks including:
- Lambdas
- Class member functions
- Free functions
- Flexible Callbacks Callbacks should be able to take no input parameters, the event type by
const&
or by value.
Integration
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
.
CMake
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
)
vcpkg
vcpkg
soon.
Usage
The basic premise of the event_bus
is that with it, you can:
- Register handlers
- Fire events that call the corresponding handlers
Define An Event Object
The "event" object can really be any class or structure you want. This is where the flexibility of this library shines.
Registering Handlers
Free function
void event_callback(event_type evt)
{
// event callback logic...
}
dp::event_bus evt_bus;
evt_bus.register_handler<event_type>(&event_callback)
Lambda
dp::event_bus evt_bus;
evt_bus.register_handler<event_type>([](const event_type& evt)
{
// logic code...
});
Class Member Function
class event_handler
{
public:
void on_event(event_type type)
{
// handler logic...
}
};
// other code
dp::event_bus evt_bus;
event_handler 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
).
Firing Events
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.
Limitations
In general, all callback functions must return void
. Currently, eventbus
only supports single argument functions as callbacks.
Contributing
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.
License
The project is licensed under the Apache License Version 2.0. See LICENSE for more details.
Author
@DeveloperPaul123 |
---|
Contributors
None yet, be the first!