目录
eventpp 是一个用于回调、事件调度程序和事件队列的C++事件库。使用 eventpp,您可以轻松实现信号和插槽机制、发布者和订阅者模式或观察者模式。
wqking/eventpp:事件调度程序和C++回调列表 (github.com)
https://github.com/wqking/eventpp
直接将源代码包含在项目中。
Eventpp 是仅标头库。只需克隆源代码,然后将 eventpp 中的“include”文件夹添加到您的项目包含目录中,然后您就可以使用该库。您无需链接到任何源代码。
- #include
- #include "eventpp/callbacklist.h"
-
- using namespace std;
-
- int main()
- {
- eventpp::CallbackList<void (const string &, const bool)> callbackList;
-
- callbackList.append([](const string & s, const bool b) {
- cout << boolalpha << "s:" << s << "b:" << b << endl;
- });
-
- callbackList.append([](string s, int b) {
- std::cout << std::boolalpha << "Got callback 2, s is " << s << " b is " << b << std::endl;
- });
-
- callbackList("fan", true);
-
- return 0;
- }

#include "eventpp/eventdispatcher.h"
- static void test() {
- cout << "test" << endl;
- }
-
- void CFan::test_2() {
- eventpp::EventDispatcher<int, void()> dispatcher;
- dispatcher.appendListener(3, []() {
- cout << "get 3" << endl;
- });
-
- dispatcher.appendListener(6, [](){
- cout << "get 6(1)" << endl;
- });
-
- dispatcher.appendListener(6, []() {
- cout << "get 6(2)" << endl;
- });
-
- dispatcher.appendListener(8, test);
-
- // dispatch
- dispatcher.dispatch(8);
-
- dispatcher.dispatch(3);
-
- dispatcher.dispatch(6);
- }

#include "eventpp/eventqueue.h"
- static void task_1(const string & arg1, const string & arg2) {
- cout << arg1 << "+" << arg2 << endl;
- }
-
- static void task_2(const string &arg1, const string & arg2) {
- cout << arg1 << "+" << arg2 << endl;
- }
-
- void CFan::test_3() {
- eventpp::EventQueue<int, void (const string &, const string &)> queue;
- queue.appendListener(1, task_1);
- queue.appendListener(5, task_2);
-
- queue.enqueue(1, "Hello", "world!");
- queue.enqueue(5, "Fan", "1111");
- queue.process();
- }
