Dawid Drozd fc1dbe335f Update EventBus to 2.1.0 inspired by Boost::MSM
I was wondering if it is a good step for EventBus. Of course, it will break back
compatibility again as well as it was when changing v1 -> v2, but this change is
smaller. Those changes were inspired by Boost::MSM how it handles events.

Why i decided to change:

+ It will prevent from bugs like typo in Event string eg. Event<int>{"text"}
+ If we want to change signature of Event, we won't have to update all listeners
and their signature
+ Less includes for listener. Simply in our class header we will have eg.
pointer/ref to event type not to all args
+ Strongly typed (this is always better)
+ Storing event for future reuse
+ More easy to introduce thread safe EventBus in future
+ EventBus is more simple
+ const Event forbids some kind of communication. Eg. passing and modifying
reference
+ Less errors when using std::bind

- Breaking back compatibility
- Need fixes in projects that using this lib
- Someone can add methods etc. to Event :(
- We can't generate easily multiple "types" of events like in 'for' loop
- Worst performance (still not such bad as CCNotificationCenter)
2017-08-26 13:28:45 +02:00
2017-08-05 19:39:41 +02:00
2017-08-15 21:48:15 +02:00
2017-08-06 11:22:59 +02:00
2015-10-07 18:11:14 +02:00

EventBus

Join the chat at https://gitter.im/EventBusCpp/Lobby Build Status

Simple and very fast event bus. The EventBus library is a convenient realization of the observer pattern. It works perfectly to supplement the implementation of MVC logic (model-view-controller) in event-driven UIs

EventBus Diagram

EventBus was created because I want something easy to use and faster than CCNotificationCenter from cocos2d-x library. Of course C++11 support was mandatory.

EventBus main goals:

  • Fast
  • Easy to use
  • Strongly typed
  • Free
  • Decouples notification senders and receivers

Usage

Notify by Event object:

Dexode::EventBus bus;
Dexode::Event<int> simpleEvent{"simple"};
//...
bus.notify(simpleEvent, 2);//Everyone who listens will receive this notification.

Notify without Event object:

Dexode::EventBus bus;
//...
bus.notify<int>("simple", 2);//Everyone who listens will receive this notification.

Lambda listener:

Dexode::EventBus bus;
//...
int token = bus.listen<int>("simple", [](int value) // register listener
{
});
//If we want unlisten exact listener we can use token for it
bus.unlistenAll(token);

Listener is identified by token. Token is returned from EventBus::listen methods. We can register multiple listeners on one token:

Dexode::EventBus bus;
Dexode::Event<int> event{"simple"};
//...
int token = bus.listen(event, [](int value) // register listener
{
});

bus.listen(token, event, [](int value) // another listener
{
});

bus.unlistenAll(token);//Now those two lambdas will be removed from listeners

If you don't want to handle manually with token you can use EventCollector class. It is useful when we want to have multiple listeners in one class. So above example could look like this:

Dexode::EventBus bus;
Dexode::Event<int> event{"simple"};
Dexode::EventCollector collector{&bus};
//...
collector.listen(event, [](int value) // register listener
{
});

collector.listen(event, [](int value) // another listener
{
});

collector.unlistenAll();//Now those two lambdas will be removed from listeners

Or as component of class:

class Example
{
public:
	Example(Dexode::EventBus& bus)
			: _collector{&bus}
	{
		_collector.listen<int>("event1", std::bind(&Example::onEvent1, this, std::placeholders::_1));
		_collector.listen<std::string>("event2", std::bind(&Example::onEvent2, this, std::placeholders::_1));
	}

	void onEvent1(int value)
	{
	}

	void onEvent2(std::string value)
	{
	}

private:
	Dexode::EventCollector _collector;// use RAII
};

//EventCollector sample
Dexode::EventBus bus;
Example ex{bus};
//...
bus.notify<int>("event1", 2);

Add to your project

EventBus can be added as ADD_SUBDIRECTORY to your cmake file. Then simply link it via TARGET_LINK_LIBRARIES
Example:

ADD_SUBDIRECTORY(lib/EventBus)
ADD_EXECUTABLE(MyExecutable
		main.cpp
		)

SET_TARGET_PROPERTIES(MyExecutable PROPERTIES
		CXX_STANDARD 14
		CXX_STANDARD_REQUIRED YES
		)

TARGET_LINK_LIBRARIES(MyExecutable PUBLIC Dexode::EventBus)

Also if you want you can install library and add it at any way you want.
Eg.

cmake -DCMAKE_INSTALL_PREFIX=./install ..
make && make install

Performance

I have prepared some performance results. You can read about them here
Small example:

check10NotificationsFor1kListeners                                     263 ns        263 ns    2668786 sum=-1.76281G
check10NotificationsFor1kListeners_CCNotificationCenter              11172 ns      11171 ns      62865 sum=54.023M

checkNotifyFor10kListenersWhenNoOneListens                              18 ns         18 ns   38976599 sum=0
checkNotifyFor10kListenersWhenNoOneListens_CCNotificationCenter     127388 ns     127378 ns       5460 sum=0

Future plans

  • Thread safe EventBus
  • Verbose messages for easy debugging
  • Fix removing/adding listeners during other notification
  • Write more and better tests
  • ...

Thanks to

License

EventBus source code can be used according to the Apache License, Version 2.0.
For more information see LICENSE file

Description
A lightweight and very fast event bus / event framework for C++17
Readme Apache-2.0
Languages
C++ 85%
CMake 15%