Event Dispatcher and callback list for C++
OTHER License
eventpp is a C++ event library for callbacks, event dispatcher, and event queue. With eventpp you can easily implement signal and slot mechanism, publisher and subscriber pattern, or observer pattern.
Apache License, Version 2.0
The master branch is usable and stable. There are some releases on Github, but usually the releases are far behind the latest code. You should prefer to clone or fork the master branch instead of downloading the releases. Don't worry about the large time span between commits and releases. The library is actively maintained. The master branch is currently fully back compatible with the first version. So your project won't get any back compatible issues. If you find any back compatible issue which is not announced, please report a bug.
https://github.com/wqking/eventpp
Tested with MSVC 2022 and 2019, MinGW (Msys) GCC 7.2, Ubuntu GCC 5.4, Intel C++ 2022, and MacOS GCC. GCC 4.8.3 can compile the library, but I don't support or maintain for GCC prior to GCC 5. In brief, MSVC, GCC, Clang that has well support for C++11, or released after 2019, should be able to compile the library.
eventpp
eventpp
package is available in C++ package managers Vcpkg, Conan, and Hunter. There are various methods to use eventpp.
Please read the document for details.
#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);
#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;
});
// dispatch event 3
dispatcher.dispatch(3);
// dispatch event 5
dispatcher.dispatch(5);
#include "eventpp/eventqueue.h"
eventpp::EventQueue<int, void (const std::string &, const bool)> queue;
queue.appendListener(3, [](const std::string s, bool b) {
std::cout << std::boolalpha << "Got event 3, s is " << s << " b is " << b << std::endl;
});
queue.appendListener(5, [](const std::string s, bool b) {
std::cout << std::boolalpha << "Got event 5, s is " << s << " b is " << b << std::endl;
});
// 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();
The library itself is header only and doesn't need building. There are three parts of code to test the library,
std::any
(the library itself only requires C++11).All parts are in the tests
folder.
All three parts require CMake to build, and there is a makefile to ease the building.
Go to folder tests/build
, then run make
with different target.
make vc19
#generate solution files for Microsoft Visual Studio 2019, then open eventpptest.sln in folder project_vc19make vc17
#generate solution files for Microsoft Visual Studio 2017, then open eventpptest.sln in folder project_vc17make vc15
#generate solution files for Microsoft Visual Studio 2015, then open eventpptest.sln in folder project_vc15make mingw
#build using MinGWmake linux
#build on Linuxmake mingw_coverage
#build using MinGW and generate code coverage reportI (wqking) am a big fan of observer pattern (publish/subscribe pattern), and I used this pattern extensively in my code. I either used GCallbackList in my cpgf library which is too simple and unsafe (not support multi-threading or nested events), or repeated coding event dispatching mechanism such as I did in my Gincu game engine (the latest version has be rewritten to use eventpp). Both methods are not fun nor robust.
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 is eventpp
.
Version 0.1.3 Sep 21, 2023 Added utility class AnyData. Small bugs fixes and improvements.
Version 0.1.2 Mar 11, 2022 Bug fix. Added more unit tests. Added utilities argumentAdapter and conditionalFunctor. Added utilities AnyId. Added event maker macros.
Version 0.1.1 Dec 13, 2019 Added HeterCallbackList, HeterEventDispatcher, and HeterEventQueue.
Version 0.1.0 Sep 1, 2018 First version. Added CallbackList, EventDispatcher, EventQueue, CounterRemover, ConditionalRemover, ScopedRemover, and utilities.
I (wqking) would like to sincerely thank all participants for the contributions. Your contributions make eventpp
better and bright future.
I maintain the contributors list manually, according to the criteria below,
If you think you should be on the contributors list, such as I miss out your work, or you have work not technology related but can help
eventpp
growing (such as posting review in social network with large amount of readers), please contact me, I'm happy to get the contributors
list larger.