• Stars
    star
    100
  • Rank 328,775 (Top 7 %)
  • Language
    C++
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple, header only event bus library written in modern C++17.

eventbus

License Apache 2.0 Say thanks Discord Windows Ubuntu

Overview

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 libraryimplements 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.

Features

  • Does not require event object inheritance A base Event class is not requied for use with dp::event_bus. Any class/struct can be used as an event object.
  • Flexible Callback Types eventbus supports 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.

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 be any class or structure you want.

Registering Handlers

Free function

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)

Lambda

dp::event_bus evt_bus;
const auto registration_handler = 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;
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).

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.

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

🚧 This library will be on vcpkg soon. 🚧

Limitations

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.

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!

More Repositories

1

thread-pool

A modern, fast, lightweight thread pool library based on C++20
C++
323
star
2

SimpleBluetoothLibrary

Android library for simplifying bluetooth usage.
Java
138
star
3

FilePickerLibrary

Simple library that allows for the picking of files and/or directories.
Java
79
star
4

MaterialLibrary

This is a simple library that provides some unique components for aiding in making apps comply with the material design guidelines.
Java
44
star
5

CMakeInstallExample

Installation example for a C++ project (Windows) with Cmake.
CMake
30
star
6

AnalogStickLibrary

A simple library that provides an anlog stick to use for on screen controls.
Java
23
star
7

periodic-function

Small header only library to call a function at a specific time interval.
CMake
23
star
8

rayray

Rayray is a baby ray tracer written in C++.
C++
18
star
9

genetic

A performant and flexible genetic algorithm implemented in C++20/23.
C++
15
star
10

Optimum

Accessible and modern implementations of common optimization algorithms.
C++
14
star
11

StarView

🌟Simple custom star view for Android🌟
Kotlin
9
star
12

delaunaypp

A fluent and easy to use implementation of Delaunay triangulation in C++.
C++
8
star
13

GettingStartedWithQt5Cmake-Tutorial

C++
7
star
14

modern-cpp-challenge

Code implementations for various C++ challenges.
C++
3
star
15

Teensy-Quadcopter

Teensy based quadcopter project.
Arduino
3
star
16

feline-facts

Simple flutter app that shows cat facts.
Dart
3
star
17

CMakeQt5Example

CMake
2
star
18

DeveloperPaul123

2
star
19

CUDA

CUDA Samples and HW from Udacity CS344 Class
C
2
star
20

lyric-analysis

Simple word analysis of music lyrics of a given artist.
Python
1
star
21

CMakeWizard

CMake configuration tool.
C++
1
star