学习笔记-eventpp库
0.引用
https://blog.csdn.net/fanjufei123456/article/details/128097861
https://article.juejin.cn/post/7165650299154071582
https://article.juejin.cn/post/6850418118864994311?from=search-suggest
1.前言
本文是自己的学习笔记,主要是作为备忘录使用,所以内容不够系统可能有很多问题。
1.正文
1.1 使用eventpp::Callbacklist 模板
/* Eventpp test code */
#include <eventpp/callbacklist.h>
#include <iostream>
#include <string>void freeFunction(int a, const std::string & b) {std::cout << "freeFunction called with a = " << a << ", b = " << b << std::endl;
}int main(int argc, char *argv[]) {eventpp::CallbackList<void(int, const std::string &b)> callbacklist;callbacklist.append(freeFunction);callbacklist.append([](int a, const std::string & b) {std::cout << "Lamba called with a = " << a << ", b = " << b << std::endl;});callbacklist(33, "Hello Eventpp test");return 0;
}
// g++ -std=c++17 -I../eventpp eventpp_test.cc -o eventpp_test
// ./eventpp_test
执行结果
➜ test ./test
freeFunction called with a = 33, b = Hello Eventpp test
Lamba called with a = 33, b = Hello Eventpp test
➜ test
1.2 使用 eventpp::Dispatcher
程序
/* eventpp dispatcher test */
#include <eventpp/eventdispatcher.h>
#include <iostream>
#include <string>int main(int argc, char *argv[]) {eventpp::EventDispatcher<int, void(int, const std::string &b)> dispatcher;dispatcher.appendListener(1, [](int a, const std::string &b) {std::cout << "Listener 1 callwed with a = " << a << ", b = " << b << std::endl;});dispatcher.appendListener(2, [](int a, const std::string &b) {std::cout << "Listener 2 callwed with a = " << a << ", b = " << b << std::endl;});dispatcher.appendListener(2, [](int a, const std::string &b) {std::cout << "Listener 2(2) callwed with a = " << a << ", b = " << b << std::endl;});dispatcher.appendListener(2, [](int a, const std::string &b) {std::cout << "Listener 2(3) callwed with a = " << a << ", b = " << b << std::endl;});dispatcher.dispatch(1, 11, "Hello from listener 1");dispatcher.dispatch(2, 11, "Hello from listener 2");dispatcher.dispatch(2, 11, "Hello from listener 2 again");dispatcher.dispatch(3, 11, "Hello from listener 3 ");return 0;
}
执行结果
➜ test g++ -o test eventpp_test_dispatcher.cc -I eventpp/include/
➜ test ./test
Listener 1 callwed with a = 11, b = Hello from listener 1
Listener 2 callwed with a = 11, b = Hello from listener 2
Listener 2(2) callwed with a = 11, b = Hello from listener 2
Listener 2(3) callwed with a = 11, b = Hello from listener 2
Listener 2 callwed with a = 11, b = Hello from listener 2 again
Listener 2(2) callwed with a = 11, b = Hello from listener 2 again
Listener 2(3) callwed with a = 11, b = Hello from listener 2 again
➜ test
1.3 使用 eventpp::Eventqueue
程序
#include <iostream>
#include <string>
#include <thread>
#include <eventpp/eventqueue.h>int main(int argc, char *artv[]) {using EQ = eventpp::EventQueue<int, void(int)>;EQ queue;// consumer threadstd::thread consumer([&queue]() {queue.appendListener(1, [&queue](int a) {std::cout << "Listener 1 called with a = " << a << std::endl;queue.process();});queue.appendListener(2, [&queue](int a) {std::cout << "Listener 2 called with a = " << a << std::endl;queue.process();});// while(1) {queue.wait();queue.process();// }});// producer thread{EQ::DisableQueueNotify disableNotify(&queue);queue.enqueue(1, 1);queue.enqueue(1, 1);queue.enqueue(2, 2);queue.enqueue(1, 11);queue.enqueue(2, 22);}consumer.join();return 0;
}
执行结果
➜ test g++ -o test eventpp_test_eventqueue.cc -I eventpp/include
➜ test ./test
Listener 1 called with a = 1
Listener 1 called with a = 1
Listener 2 called with a = 2
Listener 1 called with a = 11
Listener 2 called with a = 22
➜ test