update
This commit is contained in:
parent
5fe8a0d976
commit
4febfcb225
@ -1,24 +0,0 @@
|
||||
#include <coroutine>
|
||||
#include <future>
|
||||
namespace zt{
|
||||
template<class T>
|
||||
struct Task:std::coroutine_handle<T>{
|
||||
struct promise_type;
|
||||
struct promise_type{
|
||||
std::promise<T> 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<T>::from_promise(*this);
|
||||
}
|
||||
void unhandled_exception(){
|
||||
return;
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
@ -1,15 +1,60 @@
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <cstdio>
|
||||
#include<coroutine>
|
||||
#include "asyncio.hpp"
|
||||
#include <ios>
|
||||
#include <iostream>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
zt::Task<int> return0(){
|
||||
std::println(std::cout,"return 0;");
|
||||
template<class T>
|
||||
struct Promise;
|
||||
|
||||
template<class T>
|
||||
struct coroutine: std::coroutine_handle<Promise<T>>{
|
||||
using promise_type = ::Promise<T>;
|
||||
};
|
||||
|
||||
template<class T=void>
|
||||
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> &t){
|
||||
std::cout<<__FUNCTION__<<' '<<t<<'\n';
|
||||
}
|
||||
coroutine<T> get_return_object(){
|
||||
std::cout<<__FUNCTION__<<'\n';
|
||||
return {coroutine<T>::from_promise(*this)};
|
||||
}
|
||||
auto yield_value(const std::decay_t<T> &t){
|
||||
std::cout<<__FUNCTION__<<' '<<t<<'\n';
|
||||
return std::suspend_always{};
|
||||
}
|
||||
|
||||
coroutine<T> h;
|
||||
};
|
||||
|
||||
coroutine<int> 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user