EventBus/README.md

259 lines
7.8 KiB
Markdown
Raw Normal View History

# EventBus [![GitHub version](https://badge.fury.io/gh/gelldur%2FEventBus.svg)](https://badge.fury.io/gh/gelldur%2FEventBus)
  [![Build status for Travis](https://travis-ci.org/gelldur/EventBus.svg?branch=master)](https://travis-ci.org/gelldur/EventBus)
  [![Build status for Appveyor](https://ci.appveyor.com/api/projects/status/github/gelldur/EventBus)](https://ci.appveyor.com/project/gelldur/EventBus)
2017-08-13 14:37:19 +02:00
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
2017-09-10 14:32:21 +02:00
- tiny (~15 KB)
2017-08-06 11:22:59 +02:00
- Decouples notification senders and receivers
2018-09-09 13:47:16 +02:00
- on every platform you need (cross-platform)
2018-07-02 13:26:13 +02:00
# Brief @ presentation
Presentation [google docs](https://docs.google.com/presentation/d/1apAlKcVWo9FcqkPqL8108a1Fy9LGmhgLT56hSVpoI3w/edit?usp=sharing)
2017-09-20 20:42:01 +02:00
# Sample
2018-09-09 13:47:16 +02:00
You can checkout [sample/](sample/)
2017-09-20 20:42:01 +02:00
If you want to play with sample online checkout this link: [wandbox.org](https://wandbox.org/permlink/p3xeQaosuxv6w1rv)
2017-08-06 11:22:59 +02:00
# Usage
2017-09-10 14:32:21 +02:00
0. Store bus
2017-08-06 11:22:59 +02:00
```cpp
2018-07-02 13:26:13 +02:00
// store it in controller / singleton / std::sharted_ptr whatever you want
2017-08-06 11:22:59 +02:00
Dexode::EventBus bus;
```
2017-09-10 14:32:21 +02:00
1. Define events
2017-08-06 11:22:59 +02:00
```cpp
2017-09-10 14:32:21 +02:00
namespace Event // optional namespace
{
struct Gold
{
int goldReceived = 0;
};
2018-09-09 13:47:16 +02:00
2017-09-10 14:32:21 +02:00
struct OK {}; // Simple event when user press "OK" button
}
```
2. Subscribe
```cpp
// ...
bus.listen<Event::Gold>
([](const auto& event) // listen with lambda
{
std::cout << "I received gold: " << event.goldReceived << "!" << std::endl;
});
2018-09-09 13:47:16 +02:00
2017-09-10 14:32:21 +02:00
HudLayer* hudLayer;
// Hud layer will receive info about gold
bus.listen<Event::Gold>(std::bind(&HudLayer::onGoldReceived,hudLayer,std::placeholders::_1));
```
3. Notify
```cpp
//Inform listeners about event
bus.notify(Event::Gold{12}); // 1 way
bus.notify<Event::Gold>({12}); // 2 way
Event::Gold myGold{12};
bus.notify(myGold); // 3 way
2017-08-06 11:22:59 +02:00
```
2017-08-06 16:32:09 +02:00
Lambda listener:
2017-08-06 11:22:59 +02:00
```cpp
2017-09-10 14:32:21 +02:00
struct SampleEvent {};
2017-08-06 11:22:59 +02:00
Dexode::EventBus bus;
//...
2017-09-10 14:32:21 +02:00
int token = bus.listen<SampleEvent>([](const SampleEvent& event) // register listener
2017-08-06 11:22:59 +02:00
{
});
2017-09-10 14:32:21 +02:00
2017-08-06 11:22:59 +02:00
//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;
2017-09-10 14:32:21 +02:00
struct SampleEvent {};
2017-08-06 11:22:59 +02:00
//...
2017-09-10 14:32:21 +02:00
int token = bus.listen<SimpleEvent>([](const auto& event) // register listener
2017-08-06 11:22:59 +02:00
{
});
2017-09-10 14:32:21 +02:00
bus.listen<SimpleEvent>(token, [](const auto& event) // another listener
2017-08-06 11:22:59 +02:00
{
});
bus.unlistenAll(token);//Now those two lambdas will be removed from listeners
2018-09-09 13:47:16 +02:00
```
2017-08-06 11:22:59 +02:00
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;
2017-09-10 14:32:21 +02:00
struct SampleEvent {};
2017-08-06 11:22:59 +02:00
Dexode::EventCollector collector{&bus};
//...
2017-09-10 14:32:21 +02:00
collector.listen<SampleEvent>([](const SampleEvent& event) // register listener
2017-08-06 11:22:59 +02:00
{
});
2017-09-10 14:32:21 +02:00
collector.listen<SampleEvent>([](const SampleEvent& event) // another listener
2017-08-06 11:22:59 +02:00
{
});
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:
2017-09-10 14:32:21 +02:00
Example(const std::shared_ptr<Dexode::EventBus>& bus)
: _collector{bus}
2017-08-06 11:22:59 +02:00
{
2017-09-10 14:32:21 +02:00
_collector.listen<SimpleEvent>(std::bind(&Example::onEvent1, this, std::placeholders::_1));
_collector.listen<OtherEvent>(std::bind(&Example::onEvent2, this, std::placeholders::_1));
2017-08-06 11:22:59 +02:00
}
2017-09-10 14:32:21 +02:00
void onEvent1(const SimpleEvent& event)
2017-08-06 11:22:59 +02:00
{
}
2017-09-10 14:32:21 +02:00
void onEvent2(const OtherEvent& event)
2017-08-06 11:22:59 +02:00
{
}
private:
Dexode::EventCollector _collector;// use RAII
};
//EventCollector sample
2017-09-10 14:32:21 +02:00
std::shared_ptr<Dexode::EventBus> bus;
2017-08-06 11:22:59 +02:00
Example ex{bus};
//...
bus.notify<int>("event1", 2);
```
# Add to your project
EventBus can be added as `ADD_SUBDIRECTORY` to your cmake file.
2018-09-09 13:47:16 +02:00
Then simply link it via `TARGET_LINK_LIBRARIES`
2017-08-06 11:22:59 +02:00
Example:
```
2019-03-15 15:50:37 +01:00
# No tests/benchmarks target won't be added. Root CMakeLists is for development.
ADD_SUBDIRECTORY(path/to/EventBus/lib)
2017-08-06 11:22:59 +02:00
ADD_EXECUTABLE(MyExecutable
main.cpp
)
TARGET_LINK_LIBRARIES(MyExecutable PUBLIC Dexode::EventBus)
```
2018-09-09 13:47:16 +02:00
Also if you want you can install library and add it at any way you want.
2017-08-15 21:50:49 +02:00
Eg.
```commandline
Reorganize project structure commit f7dd4172cf535cf52601a8819cf5c8bfabcd1fe4 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 31 10:26:27 2018 +0200 Improve Travis script Fixed after reordering project structure commit d054e5c91762da15defa458404e355d7c670e301 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Mon Jul 30 15:11:21 2018 +0200 Update Travis CI for linux and OSX commit 63395f5a7e3dd9f2a52b2d6a254da89ec1d6e5e9 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Fri Jul 27 14:38:40 2018 +0200 Secure EventBus from wrong usage For example user previously could do such thing: bus.listen<const MyEvent>(...) bus.listen<MyEvent>(...) Those we 2 different events :/ commit f9195316d3ba6313ee425e3194b65b32fe52d641 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Thu Jul 26 12:44:17 2018 +0200 Update for better managing Debug/Release Updated project for easy switch between debug/release versions of library Thanks to that we can do only find_package and don't have to care about if's switching between debug/release Thanks: https://github.com/forexample/package-example commit 7d708959d9e96176875ca882f0f69a72622added Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Wed Jul 25 13:45:55 2018 +0200 Update clang-format style commit beb1d3b863379490f321e43f3e42ab272954ea67 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 16:05:16 2018 +0200 Add some docs commit ca450dfeee2d4bc604bbb9bf0599f373c21a4173 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:49:52 2018 +0200 Remove not needed includes commit 6473b80e8e60408675bcc4adc88653390576c4bd Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:41:57 2018 +0200 Code format commit 8abb56e1dd4b71df2b05bb34bef0530567e4ff2e Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:41:40 2018 +0200 Improve performance Thanks to that we don't need RTTI commit 1feacbb1f9ae6a5ac2209a6dc1df5c868ead8fd4 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:14:16 2018 +0200 Remove trash commit b5dc5c05589b969dd61eb65b68e4cdce69c5a5fb Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:00:15 2018 +0200 Fix include path commit 9939fd09805191f0bdada6cb85193a291d519116 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:51:02 2018 +0200 Update install lib commit 9eaa09f9ec5a29045b03ffc7632878863a2b2b9b Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:50:47 2018 +0200 Fix commit 7a5b3323af0b728f7e511ac22ff5027c6d06402e Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:16:12 2018 +0200 Update README commit beb6599ee4385fdffc747dc866db46e160be1358 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:16:05 2018 +0200 Add performance compare to Poco::NotifactionCenter commit 1d25b997580a9ee09c9db86135b4ca9e1b1a10c6 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:14:08 2018 +0200 Update clang-format commit 4f4cb4a7e8a849c067a42085eb3e76c3df894bc7 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 13:04:30 2018 +0200 Remove bad flag It is only working for GCC commit 66a7945084607f94d9d0c803008398e8d281fd06 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 13:03:15 2018 +0200 Remove deprecated stuff It was breaking encapsulation commit 1e7500607b42bff3632250f623888b95a503dfd0 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 13:01:37 2018 +0200 Update sample commit 11a146bb9145fa55f9b9a39a9e033387007a7151 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 13:01:27 2018 +0200 Add clang-format rules commit 685562c632d9751f50a2f05b92ef9ebf53a5d6e0 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 12:47:10 2018 +0200 Reorganize project layout Inspired by: https://www.youtube.com/watch?v=6sWec7b0JIc commit 40d1d6487814730533d7dd7cbedbaf2b4e34ef19 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 12:45:55 2018 +0200 Remove Catch2 submodule Switch to own dependency commit ca21df04f392adcb027a5b4f25ffac085b51f48c Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 11:28:51 2018 +0200 Remove old code
2018-07-31 11:33:35 +02:00
mkdir -p lib/build/
cd lib/build
cmake -DCMAKE_BUILD_TYPE=Relase -DCMAKE_INSTALL_PREFIX=~/.local/ ..
cmake --build . --target install
# OR
2017-08-15 21:50:49 +02:00
make && make install
```
2017-08-06 11:22:59 +02:00
2018-09-09 13:47:16 +02:00
Now in `Release/install` library is placed.
2017-08-06 11:22:59 +02:00
2019-04-12 13:37:35 +02:00
Or, you can install the library through your package manager (dpkg, rpm, etc).
2019-04-21 14:35:04 +02:00
E.g.
2019-04-12 13:37:35 +02:00
```commandline
mkdir -p lib/build/
cd lib/build
# For most RH-based Distributions
cmake -DCMAKE_BUILD_TYPE=Relase -DCPACK_GENERATOR="RPM" ..
# For most Debian-based Distributions
cmake -DCMAKE_BUILD_TYPE=Relase -DCPACK_GENERATOR="DEB" ..
# Or for both of them
cmake -DCMAKE_BUILD_TYPE=Relase -DCPACK_GENERATOR="RPM;DEB" ..
cmake --build . --target package
2019-04-21 14:35:04 +02:00
# Or
2019-04-12 13:37:35 +02:00
make package
# For most Debian-based systems
sudo dpkg -i EventBus*.deb
# For most RH-based systems
sudo rpm -i EventBus*.rpm
# OR
sudo yum install EventBus*.rpm
```
2018-09-09 13:47:16 +02:00
# Performance (could be outdated)
I have prepared some performance results. You can read about them [here](performance/README.md)
2017-08-06 11:22:59 +02:00
Small example:
```
check10NotificationsFor1kListeners 263 ns 263 ns 2668786 sum=-1.76281G
2017-08-06 11:22:59 +02:00
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
2017-08-06 11:22:59 +02:00
```
2017-08-05 00:36:05 +02:00
# Issues ? [![GitHub issues](https://img.shields.io/github/issues/gelldur/EventBus.svg)](https://github.com/gelldur/EventBus/issues)
2018-09-09 13:47:16 +02:00
Please report here issue / question / whatever, there is chance 99% I will answer ;)
2018-07-02 13:26:13 +02:00
If you have any questions or want to chat use gitter.
[![Join the chat at https://gitter.im/EventBusCpp/Lobby](https://badges.gitter.im/EventBusCpp/Lobby.svg)](https://gitter.im/EventBusCpp/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
# Tesing and metrics
// TODO
[![LoC](https://tokei.rs/b1/github/gelldur/EventBus)](https://github.com/gelldur/EventBus)
[![Coverage Status](https://coveralls.io/repos/github/gelldur/EventBus/badge.svg?branch=master)](https://coveralls.io/github/gelldur/EventBus?branch=master)
# Thanks to [![GitHub contributors](https://img.shields.io/github/contributors/gelldur/EventBus.svg)](https://github.com/gelldur/EventBus/graphs/contributors) [![Open Source Helpers](https://www.codetriage.com/gelldur/eventbus/badges/users.svg)](https://www.codetriage.com/gelldur/eventbus)
2017-08-20 21:03:19 +02:00
- [staakk](https://github.com/stanislawkabacinski) for fixing windows ;) [53d5026](https://github.com/gelldur/EventBus/commit/53d5026cad24810e82cd8d4a43d58cbfe329c502)
2017-08-05 02:36:12 +02:00
- [kuhar](https://github.com/kuhar) for his advice and suggestions for EventBus
2018-09-09 13:47:16 +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
Reorganize project structure commit f7dd4172cf535cf52601a8819cf5c8bfabcd1fe4 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 31 10:26:27 2018 +0200 Improve Travis script Fixed after reordering project structure commit d054e5c91762da15defa458404e355d7c670e301 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Mon Jul 30 15:11:21 2018 +0200 Update Travis CI for linux and OSX commit 63395f5a7e3dd9f2a52b2d6a254da89ec1d6e5e9 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Fri Jul 27 14:38:40 2018 +0200 Secure EventBus from wrong usage For example user previously could do such thing: bus.listen<const MyEvent>(...) bus.listen<MyEvent>(...) Those we 2 different events :/ commit f9195316d3ba6313ee425e3194b65b32fe52d641 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Thu Jul 26 12:44:17 2018 +0200 Update for better managing Debug/Release Updated project for easy switch between debug/release versions of library Thanks to that we can do only find_package and don't have to care about if's switching between debug/release Thanks: https://github.com/forexample/package-example commit 7d708959d9e96176875ca882f0f69a72622added Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Wed Jul 25 13:45:55 2018 +0200 Update clang-format style commit beb1d3b863379490f321e43f3e42ab272954ea67 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 16:05:16 2018 +0200 Add some docs commit ca450dfeee2d4bc604bbb9bf0599f373c21a4173 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:49:52 2018 +0200 Remove not needed includes commit 6473b80e8e60408675bcc4adc88653390576c4bd Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:41:57 2018 +0200 Code format commit 8abb56e1dd4b71df2b05bb34bef0530567e4ff2e Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:41:40 2018 +0200 Improve performance Thanks to that we don't need RTTI commit 1feacbb1f9ae6a5ac2209a6dc1df5c868ead8fd4 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:14:16 2018 +0200 Remove trash commit b5dc5c05589b969dd61eb65b68e4cdce69c5a5fb Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 15:00:15 2018 +0200 Fix include path commit 9939fd09805191f0bdada6cb85193a291d519116 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:51:02 2018 +0200 Update install lib commit 9eaa09f9ec5a29045b03ffc7632878863a2b2b9b Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:50:47 2018 +0200 Fix commit 7a5b3323af0b728f7e511ac22ff5027c6d06402e Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:16:12 2018 +0200 Update README commit beb6599ee4385fdffc747dc866db46e160be1358 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:16:05 2018 +0200 Add performance compare to Poco::NotifactionCenter commit 1d25b997580a9ee09c9db86135b4ca9e1b1a10c6 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 14:14:08 2018 +0200 Update clang-format commit 4f4cb4a7e8a849c067a42085eb3e76c3df894bc7 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 13:04:30 2018 +0200 Remove bad flag It is only working for GCC commit 66a7945084607f94d9d0c803008398e8d281fd06 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 13:03:15 2018 +0200 Remove deprecated stuff It was breaking encapsulation commit 1e7500607b42bff3632250f623888b95a503dfd0 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 13:01:37 2018 +0200 Update sample commit 11a146bb9145fa55f9b9a39a9e033387007a7151 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 13:01:27 2018 +0200 Add clang-format rules commit 685562c632d9751f50a2f05b92ef9ebf53a5d6e0 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 12:47:10 2018 +0200 Reorganize project layout Inspired by: https://www.youtube.com/watch?v=6sWec7b0JIc commit 40d1d6487814730533d7dd7cbedbaf2b4e34ef19 Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 12:45:55 2018 +0200 Remove Catch2 submodule Switch to own dependency commit ca21df04f392adcb027a5b4f25ffac085b51f48c Author: Dawid Drozd <dawid.drozd@mobica.com> Date: Tue Jul 24 11:28:51 2018 +0200 Remove old code
2018-07-31 11:33:35 +02:00
## For modern cmake refer
- https://github.com/forexample/package-example
- https://www.youtube.com/watch?v=6sWec7b0JIc
2017-08-06 11:22:59 +02:00
# License
2018-09-09 13:47:16 +02:00
EventBus source code can be used according to the **Apache License, Version 2.0**.
2017-08-06 11:22:59 +02:00
For more information see [LICENSE](LICENSE) file
2018-07-02 13:26:13 +02:00
If you don't like to read to much here is sumup about license [Apache 2.0](https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)#summary)