76 lines
1.5 KiB
C++
76 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <boost/coroutine2/all.hpp>
|
|
|
|
namespace a8
|
|
{
|
|
class Results
|
|
{
|
|
public:
|
|
|
|
Results(std::vector<std::any> results):results_(std::move(results)) {};
|
|
|
|
template <typename T>
|
|
T Get(size_t index) const { return std::any_cast<T>(results_.at(index));};
|
|
|
|
private:
|
|
std::vector<std::any> results_;
|
|
};
|
|
}
|
|
|
|
class Awaiter
|
|
{
|
|
public:
|
|
|
|
virtual std::shared_ptr<a8::Results> GetResult()
|
|
{
|
|
return results_;
|
|
}
|
|
|
|
virtual bool Done()
|
|
{
|
|
return done_;
|
|
}
|
|
|
|
private:
|
|
bool done_ = false;
|
|
std::shared_ptr<a8::Results> results_;
|
|
std::function<void()> cb_;
|
|
};
|
|
|
|
class Promise : public Awaiter
|
|
{
|
|
|
|
};
|
|
|
|
class Coroutine : public Awaiter, public std::enable_shared_from_this<Coroutine>
|
|
{
|
|
public:
|
|
~Coroutine();
|
|
|
|
void CoSuspend();
|
|
void CoResume();
|
|
void CoYield();
|
|
std::shared_ptr<a8::Results> CoAwait(Awaiter& awaiter);
|
|
std::shared_ptr<a8::Results> CoAwait(Awaiter* awaiter);
|
|
|
|
private:
|
|
Coroutine(std::function<void(Coroutine*)> cb);
|
|
|
|
bool Exec();
|
|
void Attach();
|
|
void Deatch();
|
|
void CallEnter(boost::coroutines2::coroutine<void>::push_type& sink);
|
|
void CallExit(boost::coroutines2::coroutine<void>::push_type& sink);
|
|
|
|
private:
|
|
list_head co_entry_;
|
|
list_head exec_entry_;
|
|
std::shared_ptr<Coroutine> hold_self_;
|
|
std::shared_ptr<boost::coroutines2::coroutine<void>::pull_type> source_;
|
|
std::function<void(Coroutine* co)> cb_;
|
|
boost::coroutines2::coroutine<void>::push_type* sink_ = nullptr;
|
|
|
|
friend class CoMgr;
|
|
};
|