├── .gitignore ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 topcpporg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # purecpp社区开源项目列表 2 | 3 | **社区网站**:www.purecpp.cn 4 | 5 | **微信公号**: purecpp 6 | 7 | ## 格式 8 | 9 | 项目名称: 10 | 11 | 状态:(已发布/孵化中) 12 | 13 | 需要的C++版本: 14 | 15 | 项目简介: 16 | 17 | code first:(10行以内的代码展示项目) 18 | 19 | # 项目列表 20 | 21 | ## 已发布 22 | 23 | * [rest_rpc](#rest_rpc) 24 | * [cinatra](#cinatra) 25 | * [iguana](#iguana) 26 | * [ormpp](#ormpp) 27 | * [feather](#feather) 28 | * [asio_redis_client](#asio_redis_client) 29 | * [future](#future) 30 | * [NoahGameFrame](#NoahGameFrame) 31 | * [ajson](#ajson) 32 | * [drogon](#drogon) 33 | * [workflow](#workflow) 34 | * [srpc](#srpc) 35 | * [librf](#librf) 36 | * [nebula](#nebula) 37 | * [cpp-ipc](#cpp-ipc) 38 | * [luatinkerE](#luatinkerE) 39 | * [ScriptX](#ScriptX) 40 | * [moon](#moon) 41 | * [async](#async) 42 | 43 | ## 孵化中 44 | 45 | * [plugincpp](#plugincpp) 46 | * [raftcpp](#raftcpp) 47 | 48 | ## rest_rpc 49 | 50 | 项目名称:[rest_rpc](https://github.com/qicosmos/rest_rpc) 51 | 52 | 状态:已发布 53 | 54 | 需要的C++版本:C++11 55 | 56 | 项目简介: 57 | 58 | rest_rpc是一个高性能、易用、跨平台、header only的c++11 rpc库,它的目标是让tcp通信变得非常简单易用,**零依赖,下载下来就可以直接使用**。 59 | 60 | code first: 61 | 62 | ```c++ 63 | //server 64 | std::string echo(rpc_conn conn, const std::string& src) { 65 | return src; 66 | } 67 | 68 | server.register_handler("echo", echo); 69 | 70 | //client 71 | std::string result = client.call("echo", "hello"); //sync call 72 | 73 | std::future future = client->async_call("echo", "hello"); //future 74 | 75 | client.async_call("echo", [](boost::system::error_code ec, string_view data){ //async call 76 | std::cout << "echo " << as(data) << '\n'; 77 | }); 78 | ``` 79 | 80 | ## cinatra 81 | 82 | 项目名称:[cinatra](https://github.com/qicosmos/cinatra) 83 | 84 | 状态:已发布 85 | 86 | 需要的C++版本:C++17 87 | 88 | 项目简介: 89 | 90 | cinatra是一个高性能易用的跨平台http(server和client)框架,它是用modern c++(c++17)开发的,它的目标是提供一个快速开发的c++ http框架。**零依赖,下载下来就可以直接使用**。它的主要特点如下: 91 | 92 | 统一而简单的接口 93 | header-only 94 | 跨平台 95 | 高效 96 | 支持面向切面编程 97 | 98 | cinatra目前支持了http1.1/1.0, ssl、websocket和文件上传下载, 你可以用它轻易地开发一个http服务器。 99 | 100 | code first: 101 | 102 | ```c++ 103 | //server 104 | server.set_http_handler("/", [](request& req, response& res) mutable{ 105 | res.set_status_and_content(status_type::ok, "hello world"); 106 | }); 107 | 108 | //client 109 | std::string uri = "http://www.purecpp.org"; 110 | response_data result = client->get(uri); //sync get 111 | 112 | client->async_get(uri, [](response_data data) {// async get 113 | print(data); 114 | }); 115 | ``` 116 | 117 | ## iguana 118 | 119 | 项目名称:[iguana](https://github.com/qicosmos/iguana) 120 | 121 | 状态:已发布 122 | 123 | 需要的C++版本:C++17 124 | 125 | 项目简介:通用的跨平台的序列化引擎,支持c++对象到json、xml等格式的相互转换,**零依赖,下载下来就可以直接使用**。 126 | 127 | code first: 128 | 129 | ```c++ 130 | struct person{ 131 | std::string name; 132 | int age; 133 | }; 134 | REFLECTION(person, name, age) //define meta data 135 | 136 | //serialize and deserialize 137 | person p = { "tom", 28 }; 138 | iguana::string_stream ss; 139 | iguana::json::to_json(ss, p); 140 | iguana::json::from_json(p, ss.str()); 141 | ``` 142 | 143 | ## ormpp 144 | 145 | 项目名称:[ormpp](https://github.com/qicosmos/ormpp) 146 | 147 | 状态:已发布 148 | 149 | 需要的C++版本:C++17 150 | 151 | 项目简介:ormpp是一个跨平台易用的ORM库,最重要的目标就是让c++中的数据库编程变得简单,为用户提供统一的接口,支持多种数据库,提高数据库开发效率。 152 | 153 | code first: 154 | 155 | ```c++ 156 | struct person{ 157 | int id; 158 | std::string name; 159 | int age; 160 | }; 161 | REFLECTION(person, id, name, age) 162 | 163 | person p = {1, "test1", 2}; 164 | mysql.insert(p); 165 | std::vector result = mysql.query(); //get all 166 | auto result = mysql.query>("select name from person"); //get part 167 | ``` 168 | 169 | ## feather 170 | 171 | 项目名称:[feather](https://github.com/qicosmos/feather) 172 | 173 | 状态:已发布 174 | 175 | 需要的C++版本:C++17 176 | 177 | 项目简介: 178 | 179 | Feather是一个快速开发的跨平台的modern c++ web框架,Feather的目标是让使用者快速开发一个高性能的web网站。 180 | 181 | code first: 182 | 183 | ```c++ 184 | void comment(request& req, response& res) { 185 | pp_comment comment{};//ommit init of comment. 186 | int r = dao.add_object(comment); 187 | if (r < 0) { 188 | res.set_status_and_content(status_type::internal_server_error); 189 | } 190 | else { 191 | res.redirect("./detail?id=" + post_id); 192 | } 193 | } 194 | 195 | server.set_http_handler("/comment", comment, check_login{}, check_comment_input{}); 196 | 197 | //post request 198 | http://purecpp.org/comment 199 | ``` 200 | 201 | ## asio_redis_client 202 | 203 | 项目名称:[asio_redis_client](https://github.com/topcpporg/asio_redis_client) 204 | 205 | 状态:已发布 206 | 207 | 需要的C++版本:C++11 208 | 209 | 项目简介: 210 | 211 | an easy to use, thread-safe, cross-platform async redis client implemented in c++11. 212 | 213 | The best c++ redis client! 214 | 215 | code first: 216 | 217 | ```c++ 218 | redis_client->set("hello", "world", [](RedisValue value) { 219 | if(value.isError()){ 220 | std::cout<<"error:"<get("hello", [](RedisValue value) { 228 | std::cout << "get: " << value.toString() << '\n'; 229 | }); 230 | ``` 231 | 232 | ## future 233 | 234 | 项目名称:[future](https://github.com/topcpporg/future) 235 | 236 | 状态:已发布 237 | 238 | 需要的C++版本:C++11 239 | 240 | 项目简介: 241 | 242 | a std::future extension implemented in c++11. 243 | 244 | C++11标准中提供了std::future和std::promise,但是标准库的future无法实现链式调用,无法满足异步并行编程的场景, 这个库提供了多线程异步并行的解决方法。 245 | 246 | code first: 247 | 248 | ```c++ 249 | auto future = Async([]{ 250 | return 42; 251 | }).Then([](int i){ 252 | return i + 2; 253 | }).Then([](int x){ 254 | return std::to_string(x); 255 | }); 256 | 257 | std::string str = future.Get(); //44 258 | ``` 259 | 260 | ## NoahGameFrame 261 | 262 | 项目名称:[NoahGameFrame](https://github.com/ketoo/NoahGameFrame) 263 | 264 | 状态:已发布 265 | 266 | 需要的C++版本:C++11 267 | 268 | 项目简介: 269 | 270 | C++跨平台插件式,模块化的游戏服务器架构,面向接口编程,下载即用. 271 | 272 | code first: 273 | 274 | ```c++ 275 | bool NFHelloWorld3Module::Init() 276 | { 277 | m_pKernelModule = pPluginManager->FindModule(); 278 | m_pKernelModule->AddClassCallBack(NFrame::Player::ThisName(), this, &NFHelloWorld3Module::OnClassCallBackEvent); 279 | } 280 | 281 | int NFHelloWorld3Module::OnClassCallBackEvent(const NFGUID& self, const std::string& className, const CLASS_OBJECT_EVENT event, const NFDataList& arg) 282 | { 283 | m_pEventModule->AddEventCallBack(self, 1, this, &NFHelloWorld3Module::OnEvent); 284 | m_pKernelModule->SetPropertyInt(self, "Hello", 1); 285 | m_pKernelModule->SetPropertyString(self, "Hello", "hello world"); 286 | return 0; 287 | } 288 | ``` 289 | 290 | ## ajson 291 | 292 | 项目名称:[ajson](https://github.com/lordoffox/ajson) 293 | 294 | 状态:已发布 295 | 296 | 需要的C++版本:C++11 297 | 298 | 项目简介: 299 | 300 | a utility for serialize C++ and json. 301 | 302 | code first: 303 | 304 | ```c++ 305 | struct Person{ 306 | std::string Name; 307 | int Age; 308 | }; 309 | 310 | AJSON(Person , Name , Age) 311 | 312 | int main(int argc,char* argv[]){ 313 | Person obj; 314 | char * json= "{\"Name\" : \"Boo\", \"Age\" : 28}"; 315 | ajson::load_from_buff(obj,json); 316 | return 0; 317 | } 318 | ``` 319 | 320 | 321 | ## plugincpp 322 | 323 | 项目名称:[plugincpp](https://github.com/qicosmos/plugincpp) 324 | 325 | 状态:孵化中 326 | 327 | 需要的C++版本:C++11 328 | 329 | 项目简介: 330 | 331 | 现代C++跨平台插件框架,零依赖,下载即用。处于开发中状态。 332 | 333 | code first: 334 | 335 | ```c++ 336 | TODO 337 | ``` 338 | 339 | ## raftcpp 340 | 341 | 项目名称:[raftcpp](https://github.com/topcpporg/raftcpp) 342 | 343 | 状态:孵化中 344 | 345 | 需要的C++版本:C++17 346 | 347 | 项目简介: 348 | 349 | An implementation of Raft consensus algorithm in modern C++. 350 | 351 | code first: 352 | 353 | ```c++ 354 | TODO 355 | ``` 356 | 357 | ## moon 358 | 359 | 项目名称:[moon](https://github.com/sniper00/moon) 360 | 361 | 状态:已发布 362 | 363 | 需要的C++版本:C++17 364 | 365 | 项目简介: 366 | 367 | C++编写的轻量级游戏服务器框架,采用Actor模型,C/CPP编写核心库,Lua编写逻辑代码的开发方式,框架专注提供游戏开发核心功能。 368 | 369 | code first: 370 | 371 | ```lua 372 | ---Scene service 373 | function CMD.PlayerMove(targetPos, speed) 374 | --call navmesh service(write with cpp navmeshlib),return path points 375 | return moon.co_call("lua", addr_namvesh, "FindPath", startPos, targetPos) 376 | end 377 | 378 | ---Player service 379 | local path, err = moon.co_call("lua", addr_scene, "PlayerMove", {x=123.0,y = 124.0}, 1.0) 380 | -- do something 381 | ``` 382 | 383 | ## drogon 384 | 385 | 项目名称: [drogon](https://github.com/an-tao/drogon) 386 | 387 | 状态: 已发布 388 | 389 | 需要的C++版本: C++14 390 | 391 | 项目简介: 392 | 393 | Drogon是一个基于C++14/17的Http应用高性能跨平台异步框架,使用Drogon可以方便的使用C++构建各种类型的Web应用程序。 394 | 395 | code first: 396 | 397 | ```c++ 398 | #include 399 | using namespace drogon; 400 | int main() 401 | { 402 | app().setLogPath("./") 403 | .setLogLevel(trantor::Logger::kWarn) 404 | .addListener("0.0.0.0", 80) 405 | .setThreadNum(16) 406 | .enableRunAsDaemon() 407 | .run(); 408 | } 409 | ``` 410 | 411 | ## workflow 412 | 413 | 项目名称: [workflow](https://github.com/sogou/workflow) 414 | 415 | 状态: 已发布 416 | 417 | 需要的C++版本: C++11 418 | 419 | 项目简介: 420 | 421 | Workflow可以同时用于异步调度和并行计算,自带Http/Redis/MySQL/Kafka协议,除OpenSSL无其他依赖,通过任务流模式为用户提供完备的通信计算融为一体的编程范式,自带服务治理,是一个设计优雅的企业级编程引擎,在搜狗内部支撑搜索服务、云输入法、在线广告的每日数百亿以上的请求。 422 | 423 | code first: 424 | 425 | ```c++ 426 | int main() 427 | { 428 | WFHttpServer server([](WFHttpTask *task) { 429 | task->get_resp()->append_output_body("Hello World!"); 430 | }); 431 | if (server.start(8888) == 0) { // start server on port 8888 432 | getchar(); // press "Enter" to end. 433 | server.stop(); 434 | } 435 | return 0; 436 | } 437 | ``` 438 | 439 | ## srpc 440 | 441 | 项目名称: [srpc](https://github.com/sogou/srpc) 442 | 443 | 状态: 已发布 444 | 445 | 需要的C++版本: C++11 446 | 447 | 项目简介: 448 | 449 | srpc是基于workflow开发的RPC系统,兼具高性能和低门槛。支持IDL:**Protobuf/Thrift**;支持协议:**SRPC/BRPC/ThriftFramed/ThriftHttp**;支持压缩类型:**snappy/gzip/zlib/lz4**;支持json且可使用Http进行跨语言。自带部分代码生成,其中thrift纯手工解析,并且打通workflow自带的其他功能包括任务流、计算调度和服务治理等。 450 | 451 | code first: 452 | 453 | ```c++ 454 | int main() 455 | { 456 | Example::SRPCClient client("127.0.0.1", 1412); 457 | EchoRequest req; 458 | req.set_message("Hello, srpc!"); 459 | client.Echo(&req, [](EchoResponse *response, RPCContext *ctx) { 460 | printf("%s\n", response->DebugString().c_str()); 461 | }); 462 | pause(); 463 | return 0; 464 | } 465 | ``` 466 | 467 | ## librf 468 | 469 | 项目名称: [librf](https://github.com/tearshark/librf) 470 | 471 | 状态: 已发布 472 | 473 | 需要的C++版本: C++17(msvc&clang) / C++20(gcc) 474 | 475 | 项目简介: 476 | 477 | 一个基于C++20 coroutines编写的无栈协程库。 478 | 479 | code first: 480 | 481 | ```c++ 482 | using namespace resumef; 483 | using namespace asio::ip; 484 | 485 | future_t<> RunPingPongEchoClient(asio::io_service & ios, tcp::resolver::iterator ep) 486 | { 487 | std::array read_buff_; 488 | std::array write_buff_; 489 | 490 | tcp::socket socket_{ ios }; 491 | co_await asio::async_connect(socket_, ep, rf_task); 492 | 493 | for (auto & c : write_buff_) 494 | c = 'A' + rand() % 52; 495 | 496 | for (;;) 497 | { 498 | co_await asio::async_write(socket_, asio::buffer(write_buff_), rf_task); 499 | co_await socket_.async_read_some(asio::buffer(read_buff_), rf_task); 500 | } 501 | } 502 | ``` 503 | 504 | ## nebula 505 | 506 | 项目名称: [nebula](https://github.com/vesoft-inc/nebula) 507 | 508 | 状态: 已发布 509 | 510 | 需要的C++版本: C++14(gcc&clang), 重度依赖 Facebook folly 511 | 512 | 项目简介: 513 | 514 | Nebula Graph 是一款开源的图数据库,擅长处理千亿个顶点和万亿条边的超大规模数据集。 515 | 516 | code first: 517 | 518 | ```c++ 519 | namespace nebula { 520 | namespace storage { 521 | 522 | folly::SemiFuture> getNeighbors( 523 | GraphSpaceID space, 524 | const std::vector &vertices, 525 | const std::vector &edgeTypes, 526 | std::string filter, 527 | std::vector returnCols, 528 | folly::EventBase* evb = nullptr); 529 | } 530 | } 531 | ``` 532 | 533 | ## cpp-ipc 534 | 535 | 项目名称: [cpp-ipc](https://github.com/mutouyun/cpp-ipc) 536 | 537 | 状态: 已发布 538 | 539 | 需要的C++版本: C++14(msvc&gcc&clang), 推荐支持C++17的编译器 540 | 541 | 项目简介: 542 | 543 | 使用共享内存的跨平台(Linux/Windows,x86/x64/ARM)高性能IPC通讯库。 544 | 545 | code first: 546 | 547 | ```c++ 548 | std::vector const datas = { 549 | "hello!", 550 | "foo", 551 | "bar", 552 | "ISO/IEC", 553 | "14882:2011", 554 | "ISO/IEC 14882:2017 Information technology - Programming languages - C++", 555 | "ISO/IEC 14882:2020", 556 | "Modern C++ Design: Generic Programming and Design Patterns Applied" 557 | }; 558 | 559 | // thread producer 560 | std::thread t1 {[&] { 561 | ipc::route cc { "my-ipc-route" }; 562 | // waiting for connection 563 | cc.wait_for_recv(1); 564 | // sending datas 565 | for (auto str : datas) cc.send(str); 566 | // quit 567 | cc.send(ipc::buff_t('\0')); 568 | }}; 569 | 570 | // thread consumer 571 | std::thread t2 {[&] { 572 | ipc::route cc { "my-ipc-route", ipc::receiver }; 573 | while (1) { 574 | auto buf = cc.recv(); 575 | auto str = static_cast(buf.data()); 576 | if (str == nullptr || str[0] == '\0') return; 577 | std::printf("recv: %s\n", str); 578 | } 579 | }}; 580 | 581 | t1.join(); 582 | t2.join(); 583 | ``` 584 | 585 | 586 | ## luatinkerE 587 | 588 | 项目名称:[luatinkerE](https://github.com/yanwei1983/luatinkerE) 589 | 590 | 状态:已发布 591 | 592 | 需要的C++版本:主版本C++11 或 C++17分支 593 | 594 | 项目简介: 595 | 596 | LUA-C++ 绑定库"lua_tinker" 扩展为支持 c++11/14/17各种新特性 和lua 5.3 597 | 598 | code first: 599 | 600 | ```c++ 601 | //c++bind 602 | lua_tinker::def("func_name", &func); 603 | lua_tinker::set("var_name", &var); 604 | lua_tinker::class_add("class_name"); 605 | lua_tinker::class_def("func_name", &class_t::func); 606 | lua_tinker::class_mem_readonly("member_name", &class_t::member); 607 | lua_tinker::class_mem_static("member_name", &class_t::member); 608 | lua_tinker::class_property(L, "m_prop", &ff::getVal, &ff::setVal); 609 | lua_tinker::class_property(L, "m_prop_readonly", &ff::getVal, nullptr); 610 | lua_tinker::class_inh(L); 611 | lua_tinker::def(L, "test_overload", lua_tinker::args_type_overload_functor( 612 | lua_tinker::make_functor_ptr((int(*)(int))(&test_overload)), 613 | lua_tinker::make_functor_ptr((int(*)(double))(&test_overload)), 614 | lua_tinker::make_functor_ptr((int(*)(int, double))(&test_overload)), 615 | lua_tinker::make_functor_ptr((int(*)(int, int, double))(&test_overload)))); 616 | 617 | 618 | //call lua 619 | ret_val_t ret = lua_tinker::call("func_name", param1, param2); 620 | var_t var = lua_tinker::get("var_name"); 621 | 622 | std::function func = [](int k, int j)->int { return k + j; }; 623 | lua_tinker::def(L, "std_function_int_int", func); //can hold function 624 | 625 | lua_tinker::lua_function_ref lua_func = lua_tinker::call(L, "test_lua_luafunction"); 626 | ``` 627 | 628 | ## ScriptX 629 | 630 | 项目名称:[ScriptX](https://github.com/Tencent/ScriptX) 631 | 632 | 状态:已发布 633 | 634 | 需要的C++版本:C++17 635 | 636 | 项目简介: 637 | 638 | ScriptX是一个脚本引擎抽象层。对下封装多种脚本引擎,对上暴露统一的API,使得上层调用者可以完全隔离底层的引擎实现(后端)。 639 | 640 | ScriptX不仅隔离了几种JavaScript引擎,甚至可以隔离不同脚本语言,使得上层仅需修改一个编译选项即可无缝切换脚本引擎和脚本语言。 641 | 642 | ScriptX的术语中,"前端"指对外的C++ API,"后端"则指不同的底层引擎,目前已经实现的后端有:V8, node.js, JavaScriptCore, WebAssembly, Lua. 643 | 644 | code first: 645 | 646 | ```c++ 647 | EngineScope enter(engine); 648 | try { 649 | engine->eval("function fibo(x) { if (x<=2 ) return 1; else return fibo(x-1) + fibo(x-2) }"); 650 | Local fibo = engine->get("fibo").asFunction(); 651 | Local ret = fibo.call({}, 10); 652 | ret.asNumber().toInt32() == 55; 653 | 654 | // or use: Function::newFunction(std::puts); 655 | engine->set("log", Function::newFunction( 656 | [](const std::string& msg) { 657 | std::cerr << "[log]: " << msg << std::endl; 658 | })); 659 | engine->eval("log('hello world');"); 660 | 661 | auto info = json.get("info").asObject(); 662 | info.get("version").asString().toString() == "1.18"; 663 | info.get("time").asNumber().toInt32() == 132; 664 | 665 | Local bind = engine->eval("...").asObject(); 666 | MyBind* ptr = engine->getNativeInstance(bind); 667 | ptr->callCppFunction(); 668 | 669 | } catch (Exception& e) { 670 | FAIL() << e.message() << e.stacktrace(); 671 | // or FAIL() << e; 672 | } 673 | ``` 674 | 675 | 1. 使用 EngineScope 进入引擎环境 676 | 2. 绝大多是API可以接受C++原生类型作为参数,内部自动转换类型 677 | 3. 可以从C/C++函数直接创建脚本函数(native 绑定) 678 | 4. 支持脚本的异常处理 679 | 5. API强类型 680 | 681 | 682 | ## async 683 | 684 | 项目名称:[async](https://github.com/xiaoquanjie/async) 685 | 686 | 状态:已发布 687 | 688 | 需要的C++版本:C++11 689 | 690 | 项目简介: 691 | async的目标是为异步代码的“同步编写”提供一个解决方案。其中内置了一些项目中常用的io接口实现, 692 | 比如:mysql,curl,mongo,redis以及基于zeromq而实现的服务端进程间通信。简单易用 693 | 694 | 支持跨平台(linux/windwos) 695 | 696 | code first: 697 | 698 | // 下面代码的输出结果是: 699 | promise begin 700 | 异步执行结束 701 | promise over:0 702 | 703 | ```c++ 704 | void promise_test() { 705 | // 起一个协程任务,回调将在协程里运行 706 | CoroutineTask::doTask([](void*){ 707 | printf("promise begin\n"); 708 | auto ret = co_async::promise([](co_async::Resolve resolve, co_async::Reject reject) { 709 | // 两秒后执行下面的回调 710 | co_async::setTimeout([resolve]() { 711 | printf("异步执行结束\n"); 712 | // 报告promise异步执行结束,并传递结果集,这里传nullptr 713 | resolve(nullptr); 714 | }, 2*1000); 715 | }); 716 | // promise返回,往下继续走 717 | printf("promise over:%d\n", ret.first); 718 | }, 0); 719 | } 720 | 721 | ``` 722 | 723 | --------------------------------------------------------------------------------