diff --git a/include/asyncio.hpp b/include/asyncio.hpp index 813e0f4..e69de29 100644 --- a/include/asyncio.hpp +++ b/include/asyncio.hpp @@ -1,24 +0,0 @@ -#include -#include -namespace zt{ - template - struct Task:std::coroutine_handle{ - struct promise_type; - struct promise_type{ - std::promise promise_; - void return_value(T value){promise_.set_value(value);} - auto initial_suspend(){ - return std::suspend_always{}; - } - auto final_suspend()noexcept{ - return std::suspend_always{}; - } - auto get_return_object(){ - return Task::from_promise(*this); - } - void unhandled_exception(){ - return; - } - }; - }; -} \ No newline at end of file diff --git a/src/test1.cpp b/src/test1.cpp index b4b3e9d..aa2849f 100644 --- a/src/test1.cpp +++ b/src/test1.cpp @@ -1,15 +1,60 @@ -#include +#include +#include +#include #include -#include -#include -#include "asyncio.hpp" +#include +#include +#include -zt::Task return0(){ - std::println(std::cout,"return 0;"); +template +struct Promise; + +template +struct coroutine: std::coroutine_handle>{ + using promise_type = ::Promise; +}; + +template +struct Promise{ + auto initial_suspend(){ + std::cout<<__FUNCTION__<<'\n'; + return std::suspend_always{}; + } + auto final_suspend()noexcept{ + std::cout<<__FUNCTION__<<'\n'; + return std::suspend_always{}; + } + void unhandled_exception(){ + std::cout<<__FUNCTION__<<'\n'; + throw std::runtime_error("unhandled exeception\n"); + } + void return_value(const std::decay_t &t){ + std::cout<<__FUNCTION__<<' '< get_return_object(){ + std::cout<<__FUNCTION__<<'\n'; + return {coroutine::from_promise(*this)}; + } + auto yield_value(const std::decay_t &t){ + std::cout<<__FUNCTION__<<' '< h; +}; + +coroutine test(){ + std::cout<<"testing\n"; + co_yield 1; + co_yield 2; co_return 0; } int main(){ - auto ret = return0(); - ret.promise_type; + std::cout<<__FUNCTION__<<'\n'; + auto handle = test(); + while (!handle.done()) { + std::cout<<__FUNCTION__<<'\n'; + handle.resume(); + } } \ No newline at end of file