From fcce8221aaa66498f7328dc3473587900254f421 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 24 Nov 2023 01:20:19 +0800 Subject: [PATCH] add more coroutine helper functions. --- Source/Common/Utils.cpp | 10 ++++++++++ Source/Common/Utils.h | 3 ++- Source/Test/HelloWorldCpp.cpp | 16 ++++++++-------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Source/Common/Utils.cpp b/Source/Common/Utils.cpp index 508ef3d0d..a585d2844 100644 --- a/Source/Common/Utils.cpp +++ b/Source/Common/Utils.cpp @@ -11,6 +11,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include "Common/Utils.h" #include "Basic/Application.h" +#include "Basic/Director.h" +#include "Basic/Scheduler.h" #include "Event/Event.h" #include "Lua/ToLua/tolua++.h" @@ -166,4 +168,12 @@ std::function loop(const std::function& work) { }); } +void thread(const std::function& work) { + SharedDirector.getScheduler()->schedule(once(work)); +} + +void threadLoop(const std::function& work) { + SharedDirector.getScheduler()->schedule(loop(work)); +} + NS_DOROTHY_END diff --git a/Source/Common/Utils.h b/Source/Common/Utils.h index baea78622..98fe18096 100644 --- a/Source/Common/Utils.h +++ b/Source/Common/Utils.h @@ -460,7 +460,8 @@ using Job = Coroutine; } std::function once(const std::function& work); - std::function loop(const std::function& work); +void thread(const std::function& work); +void threadLoop(const std::function& work); NS_DOROTHY_END diff --git a/Source/Test/HelloWorldCpp.cpp b/Source/Test/HelloWorldCpp.cpp index ef4b64c02..db3ada573 100644 --- a/Source/Test/HelloWorldCpp.cpp +++ b/Source/Test/HelloWorldCpp.cpp @@ -13,21 +13,21 @@ using namespace Dorothy; DORA_TEST_ENTRY(HelloWorldCpp) { auto node = Node::create(); - node->slot("Enter"_slice, [](Event*) { - println("on enter event"); + node->slot("Enter"sv, [](Event*) { + println("on enter event"sv); }); - node->slot("Exit"_slice, [](Event*) { - println("on exit event"); + node->slot("Exit"sv, [](Event*) { + println("on exit event"sv); }); - node->slot("Cleanup"_slice, [](Event*) { - println("on node destoyed event"); + node->slot("Cleanup"sv, [](Event*) { + println("on node destoyed event"sv); }); node->schedule(once([]() -> Job { - for (int i = 3; i > 0; i--) { + for (int i = 5; i > 0; i--) { println("{}", i); co_sleep(1); } - println("Hello World!"); + println("Hello World!"sv); })); SharedDirector.getEntry()->addChild(node);