2015-10-07 18:11:14 +02:00
# EventBus
2017-08-06 11:22:59 +02:00
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 ](EventBusDiagram.png )
EventBus was created because I want something easy to use and faster than [CCNotificationCenter ](https://github.com/cocos2d/cocos2d-x/blob/v2/cocos2dx/support/CCNotificationCenter.h )
from [cocos2d-x ](https://github.com/cocos2d/cocos2d-x ) library. Of course C++11 support was mandatory.
2017-08-06 18:16:41 +02:00
EventBus main goals:
2017-08-06 11:22:59 +02:00
- Fast
- Easy to use
2017-08-06 16:32:09 +02:00
- Strongly typed
2017-08-06 11:22:59 +02:00
- Free
- Decouples notification senders and receivers
# Usage
2017-08-06 16:32:09 +02:00
Notify by Event object:
2017-08-06 11:22:59 +02:00
```cpp
Dexode::EventBus bus;
Dexode::Event< int > simpleEvent{"simple"};
//...
bus.notify(simpleEvent, 2);//Everyone who listens will receive this notification.
```
2017-08-06 16:32:09 +02:00
Notify without Event object:
2017-08-06 11:22:59 +02:00
```cpp
Dexode::EventBus bus;
//...
bus.notify< int > ("simple", 2);//Everyone who listens will receive this notification.
```
2017-08-06 16:32:09 +02:00
Lambda listener:
2017-08-06 11:22:59 +02:00
```cpp
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.
2017-08-06 16:32:09 +02:00
We can register multiple listeners on one token:
2017-08-06 11:22:59 +02:00
```cpp
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
```
2017-08-06 18:16:41 +02:00
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:
2017-08-06 11:22:59 +02:00
```cpp
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
```
2017-08-06 16:32:09 +02:00
Or as component of class:
2017-08-06 11:22:59 +02:00
```cpp
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)
```
2017-08-06 16:32:09 +02:00
Also if you want you can install library and add it at any way you want.
2017-08-06 11:22:59 +02:00
# Performance
I have prepared some performance results. You can read about them [here ](performance/README.md )
Small example:
```
check10NotificationsFor1kListeners 273 ns 273 ns 2609987 sum=-1.81219G
check10NotificationsFor1kListeners_EventBus2 267 ns 267 ns 2652159 sum=-1.77676G
check10NotificationsFor1kListeners_CCNotificationCenter 11172 ns 11171 ns 62865 sum=54.023M
```
2017-08-05 00:36:05 +02:00
# Thanks to
2017-08-05 02:36:12 +02:00
- [stanislawkabacinski ](https://github.com/stanislawkabacinski ) for fixing windows ;) [53d5026 ](https://github.com/gelldur/EventBus/commit/53d5026cad24810e82cd8d4a43d58cbfe329c502 )
- [kuhar ](https://github.com/kuhar ) for his advice and suggestions for EventBus
2017-08-06 16:32:09 +02:00
- [swietlana ](https://github.com/swietlana ) for english correction and support ;)
2017-08-05 02:36:12 +02:00
- [ruslo ](https://github.com/ruslo ) for this great example: https://github.com/forexample/package-example
2017-08-06 11:22:59 +02:00
# License
EventBus source code can be used according to the Apache License, Version 2.0.
For more information see [LICENSE ](LICENSE ) file