1
0
mirror of https://github.com/wqking/eventpp.git synced 2024-12-27 16:41:11 +08:00
eventpp/readme.md

122 lines
5.1 KiB
Markdown
Raw Normal View History

2018-05-16 20:39:38 +08:00
# eventpp -- C++ library for event dispatcher and callback list
2018-05-13 12:26:27 +08:00
2018-07-15 22:32:24 +08:00
eventpp is a C++ library that provides tools that allow your application components to communicate with each other by dispatching events and listening to them. With eventpp you can implement signal/slot mechanism, or observer pattern, very easily.
2018-05-13 12:26:27 +08:00
## Facts and features
- Supports both synchronous event dispatching (EventDispatcher) and asynchronous event queue (EventQueue).
2018-07-15 22:32:24 +08:00
- Supports nested event. A listener can safely dispatch event, append/prepend/insert/remove other listeners, during capturing an event.
2018-05-28 17:18:49 +08:00
- Configurable and extensible with policies and mixins.
2018-07-15 22:32:24 +08:00
- Supports event filter via mixins.
- Supports multiple threading.
- Template based, less runtime overhead, unlimited possibilities. The event and callback can be almost any C++ types that satisfy minimum requirements.
2018-05-14 20:42:54 +08:00
- Backed by unit tests.
- Written in portable and standard C++. (I'm not a C++ standard expert so if you find any non-standard code or undefined behavior please let me know.)
2018-05-28 17:18:49 +08:00
- Requires C++ 11 (tested with MSVC 2017, MSVC 2015, MinGW (Msys) gcc 7.2, and Ubuntu gcc 5.4).
2018-05-14 20:42:54 +08:00
- Doesn't depend on any other libraries.
- Header only, no source file, no need to build.
2018-05-13 12:26:27 +08:00
## License
Apache License, Version 2.0
## Version 0.1.0
eventpp is currently usable and near stable.
2018-05-13 12:26:27 +08:00
## Source code
[https://github.com/wqking/eventpp](https://github.com/wqking/eventpp)
## Quick start
2018-05-13 12:26:27 +08:00
2018-05-14 20:42:54 +08:00
### Namespace
`eventpp`
2018-05-19 17:42:41 +08:00
### Using CallbackList
```c++
#include "eventpp/callbacklist.h"
eventpp::CallbackList<void (const std::string &, const bool)> callbackList;
callbackList.append([](const std::string & s, const bool b) {
std::cout << std::boolalpha << "Got callback 1, s is " << s << " b is " << b << std::endl;
});
callbackList.append([](std::string s, int b) {
std::cout << std::boolalpha << "Got callback 2, s is " << s << " b is " << b << std::endl;
});
callbackList("Hello world", true);
```
### Using EventDispatcher
2018-05-13 12:26:27 +08:00
```c++
#include "eventpp/eventdispatcher.h"
eventpp::EventDispatcher<int, void ()> dispatcher;
dispatcher.appendListener(3, []() {
std::cout << "Got event 3." << std::endl;
});
dispatcher.appendListener(5, []() {
std::cout << "Got event 5." << std::endl;
});
dispatcher.appendListener(5, []() {
std::cout << "Got another event 5." << std::endl;
});
2018-05-19 17:42:41 +08:00
// dispatch event 3
2018-05-13 12:26:27 +08:00
dispatcher.dispatch(3);
2018-05-19 17:42:41 +08:00
// dispatch event 5
2018-05-13 12:26:27 +08:00
dispatcher.dispatch(5);
```
2018-05-19 17:42:41 +08:00
### Using EventQueue
2018-05-13 12:26:27 +08:00
```c++
2018-05-19 17:42:41 +08:00
eventpp::EventQueue<int, void (const std::string &, const bool)> queue;
2018-05-19 17:42:41 +08:00
dispatcher.appendListener(3, [](const std::string s, bool b) {
std::cout << std::boolalpha << "Got event 3, s is " << s << " b is " << b << std::endl;
2018-05-13 12:26:27 +08:00
});
2018-05-19 17:42:41 +08:00
dispatcher.appendListener(5, [](const std::string s, bool b) {
std::cout << std::boolalpha << "Got event 5, s is " << s << " b is " << b << std::endl;
2018-05-13 12:26:27 +08:00
});
2018-05-19 17:42:41 +08:00
// The listeners are not triggered during enqueue.
queue.enqueue(3, "Hello", true);
queue.enqueue(5, "World", false);
// Process the event queue, dispatch all queued events.
queue.process();
2018-05-13 12:26:27 +08:00
```
## Documentations
2018-05-19 17:42:41 +08:00
* [An overview introduction](doc/introduction.md)
* [Tutorials of CallbackList](doc/tutorial_callbacklist.md)
2018-05-19 20:04:09 +08:00
* [Tutorials of EventDispatcher](doc/tutorial_eventdispatcher.md)
2018-05-19 17:42:41 +08:00
* [Tutorials of EventQueue](doc/tutorial_eventqueue.md)
* [Class CallbackList](doc/callbacklist.md)
* [Class EventDispatcher](doc/eventdispatcher.md)
* [Class EventQueue](doc/eventqueue.md)
* [Utility class CounterRemover -- auto remove listeners after triggered certain times](doc/counterremover.md)
2018-07-09 22:30:28 +08:00
* [Utility class ConditionalRemover -- auto remove listeners when certain condition is satisfied](doc/conditionalremover.md)
* [Utility class ScopedRemover -- auto remove listeners when out of scope](doc/scopedremover.md)
* [Document of utilities](doc/eventutil.md)
2018-05-28 17:18:49 +08:00
* [Policies -- configure eventpp](doc/policies.md)
* [Mixins -- extend eventpp](doc/mixins.md)
2018-05-31 12:48:40 +08:00
* [Performance benchmarks](doc/benchmark.md)
* [Frequently Asked Questions](doc/faq.md)
2018-05-16 20:39:38 +08:00
* There are compilable tutorials in the unit tests.
2018-05-13 12:26:27 +08:00
## Build the unit tests
2018-05-14 20:00:29 +08:00
The library itself is header only and doesn't need building.
The unit test requires CMake to build, and there is a makefile to ease the building.
Go to folder `tests/build`, then run `make` with different target.
- `make vc17` #generate solution files for Microsoft Visual Studio 2017, then open eventpptest.sln in folder project_vc17
- `make vc15` #generate solution files for Microsoft Visual Studio 2015, then open eventpptest.sln in folder project_vc15
- `make mingw` #build using MinGW
- `make linux` #build on Linux
2018-05-13 12:26:27 +08:00
## Motivations
2018-07-15 22:32:24 +08:00
I (wqking) am a big fan of observer pattern (publish/subscribe pattern), I used such pattern a lot in my code. I either used GCallbackList in my [cpgf library](https://github.com/cpgf/cpgf) which is too simple and not safe (not support multi threading nor nested event), or repeated coding event dispatching mechanism such as I did in my [Gincu game engine](https://github.com/wqking/gincu) (the latest version has be rewritten to use eventpp). Both approaches are neither fun nor robust.
2018-05-16 20:39:38 +08:00
Thanking to C++11, now it's quite easy to write a reusable event library with beautiful syntax (it's a nightmare to simulate the variadic template in C++03), so here comes `eventpp`.
2018-05-13 12:26:27 +08:00