Giter Club home page Giter Club logo

event-bus's Introduction

EventBus

A simple event bus with type-safety provided by C++20' s concepts.

  • Currently not thread-safe. WIP for future versions.

Before calling any code related to EventBus, the EventBus itself must be created with:

int main()
{
    eventbus::CreateEventbus();
    // No return value, since EventBus can be used statically.
    ...
}

To create an event that will be raised:

struct print_event : eventbus::Event<print_event>
{
    explicit print_event(std::string t_msg) : m_msg{std::move(t_msg)} { }

    std::string m_msg;
};

All events should be created in public headers to use in other compilation units.

  • event_bus.hpp header provides 4 macros for ease of use. These macros are for making the process of creating Delegates easier and provide a straight-forward API.

After an initial setup, events can be subscribed to and raised like:

void PrintMessage(print_event& t_event)
{
    std::cout << t_event.m_msg << std::endl;
}

int main()
{
    // Instantiate EventBus in the internal namespace
    eventbus::CreateEventbus();

    // Notice the usage of this macro is pretty straight-forward.
    // Event type as the first parameter, the function name as the second.
    SUB_TO_EVENT_FREE_FUN(print_event, PrintMessage);

    // If we were the constructor of a class. This would instead be used as:
    // -> SUB_TO_EVENT_MEM_FUN(print_event, PrintMessage);
    // Where the PrintMessage would be a member function. No additional information
    // about the class is needed.

    // We call all the subscribers of the event whether they are member functions or free functions. 
    eventbus::EventBus::RaiseEvent<print_event>("Hello from eventbus!");
}

event-bus's People

Contributors

sorbatdev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.