Skip to content

Ukio-G/EventChannel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Event channel implementation

Synopsis

Problem:

Solution:

Usage:

No need to compile library/source files. Just include EventChannel.h:

#include "EventChannel.h"

Now you are ready to use all features of Event channel!

Create named subscriber object:

 std::shared_ptr<Subscriber> subscriber = std::make_shared<Subscriber>("name");

Or with macro:

NewSharedSubscriber(subscriber)

Create action for some topic:

subscriber->newAction("event_topic", [](Subscriber::ActionArgument argument) {
    std::vector<int> data = std::any_cast<std::string>(argument);
    std::cout << "argument = " << data << std::endl;
});

Subscribe subscriber object to event channel:

EventChannel::getInstance().subscribe("event_topic", subscriber);

Generate event for some topic:

EventChannel::getInstance().publish("event_topic", std::string("event data"));

One subscriber can handle multiple events:

subscriber->newAction("makeSum", [](Subscriber::ActionArgument argument) { 
    std::vector<int> data = std::any_cast<std::vector<int>>(argument); 
    std::cout << "makeSum result = " << std::accumulate(data.begin(), data.end(), 0)<< std::endl; 
});
subscriber->newAction("printerElements", [](Subscriber::ActionArgument argument) {
    std::vector<int> data = std::any_cast<std::vector<int>>(argument);
    for (auto &item : data)
        std::cout << "item " << item << std::endl;
});

Limitations:

You should initialize static EventChannel::_instance pointer:

EventChannel* EventChannel::_instance = nullptr;

Code examples:

See examples directory for sources with EventChannel usage.

ToDo:

  • Anonymous subscribers
  • Thread-safe notifications/subscribe

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages