# handlerloopercpp **Repository Path**: robinxdroid/handlerloopercpp ## Basic Information - **Project Name**: handlerloopercpp - **Description**: looper handler and handler thread implemented by cpp - **Primary Language**: C++ - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2024-10-31 - **Last Updated**: 2024-10-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # cpp_handler_looper ## 介绍 looper, handler and handler-thread implemented by cpp ### 软件架构 软件架构说明 - Message ---- MessageQueue - Looper ---- Handler - Thread ---- HandlerThread ### 安装教程 1. include HandlerLooper.hpp 2. add -lpthread to link when complie ### 使用说明 1. create an instance of HLHandlerThread, then start the handler thread. ```cpp auto handlerThread = std::make_shared("HandlerThread"); handlerThread->start(); assert(handlerThread->getLooper()); ``` 2. create an instance derived from HLHandler with the handler thread looper. ```cpp auto handler = std::make_shared(handlerThread->getLooper()); ``` or create an instance of HLHandler with the handler thread looper and a function. ```cpp auto handler = std::make_shared(handlerThread->getLooper(), [](const HLMessageRefPtr & m) { handleMessage(m); return true; }); ``` 3. use the handler to send message or post method. ```cpp handler->post([](const HLMessageRefPtr &) { LOGI("post callback test."); return true; }); handler->postDelayed([](const HLMessageRefPtr &) { LOGI("postDelayed callback test."); return true; }, 1000); handler->sendMessage(3, 4, 5); handler->sendMessage(3, 4, 5, make_any_obj(8)); handler->sendMessage(4, 5, 6, make_any_obj(s4)); handler->sendMessage(4, 5, 6, make_any_obj("hello string")); std::vector v5 {1, 2, 3, 4, 5}; handler->sendMessage(5, 6, 7, make_any_obj>(v5)); std::vector v52(10); std::iota(v52.begin(), v52.end(), 100); handler->sendMessage(5, 6, 7, make_any_obj>(v52)); handler->sendMessage(5, 6, 7, make_any_obj>(1, 2, 3, 4, 5)); using ST = struct {int id; std::string info}; ST st{1, "hello ST 1"}; handler->sendMessage(6, 7, 8, make_any_obj(st)); handler->sendMessage(6, 7, 8, make_any_obj(2, "hello ST 2")); ``` 4. call quit() method to quit its looper, and stop() to quit its looper and terminate its thread. ```cpp handlerThread->quit(); handlerThread->stop(); ``` ### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request ### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)