1
This commit is contained in:
parent
10c9ce7c68
commit
3d6b01a527
@ -56,14 +56,18 @@ void Coroutine::CoYield()
|
||||
(*sink_)();
|
||||
}
|
||||
|
||||
void Coroutine::CoAwait(Awaiter& awaiter)
|
||||
std::shared_ptr<a8::Results> Coroutine::CoAwait(Awaiter& awaiter)
|
||||
{
|
||||
|
||||
return CoAwait(&awaiter);
|
||||
}
|
||||
|
||||
void Coroutine::CoAwait(Awaiter* awaiter)
|
||||
std::shared_ptr<a8::Results> Coroutine::CoAwait(Awaiter* awaiter)
|
||||
{
|
||||
|
||||
CoSuspend();
|
||||
while (!awaiter->Done()) {
|
||||
CoYield();
|
||||
}
|
||||
return awaiter->GetResult();
|
||||
}
|
||||
|
||||
void Coroutine::Attach()
|
||||
|
@ -2,9 +2,40 @@
|
||||
|
||||
#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
|
||||
@ -20,8 +51,8 @@ class Coroutine : public Awaiter, public std::enable_shared_from_this<Coroutine>
|
||||
void CoSuspend();
|
||||
void CoResume();
|
||||
void CoYield();
|
||||
void CoAwait(Awaiter& awaiter);
|
||||
void CoAwait(Awaiter* awaiter);
|
||||
std::shared_ptr<a8::Results> CoAwait(Awaiter& awaiter);
|
||||
std::shared_ptr<a8::Results> CoAwait(Awaiter* awaiter);
|
||||
|
||||
private:
|
||||
Coroutine(std::function<void(Coroutine*)> cb);
|
||||
|
Loading…
x
Reference in New Issue
Block a user