This commit is contained in:
aozhiwei 2023-05-27 16:44:02 +08:00
parent 424f708375
commit 6b0773c2e9
4 changed files with 95 additions and 0 deletions

31
a8/awaiter.cc Normal file
View File

@ -0,0 +1,31 @@
#include <a8/a8.h>
#include <a8/awaiter.h>
namespace a8
{
void Awaiter::Await(std::shared_ptr<Awaiter> notifyer)
{
notifyers_.push_back(notifyer);
DoAwait();
}
#if 0
std::shared_ptr<Awaiter> Awaiter::Sleep(int time)
{
return std::make_shared<TimerPromise>(time);
}
#endif
void Awaiter::DoDone()
{
done_ = true;
for (auto notifyer : notifyers_) {
if (!notifyer.expired()) {
notifyer.lock()->DoResume();
}
}
}
}

37
a8/awaiter.h Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include <a8/result.h>
namespace f8
{
class Coroutine;
}
namespace a8
{
class Awaiter : public std::enable_shared_from_this<Awaiter>
{
public:
virtual ~Awaiter() {};
std::shared_ptr<a8::Results> GetResult() { return results_; }
bool Done() const { return done_; }
virtual void DoResume() {};
protected:
bool done_ = false;
std::list<std::weak_ptr<Awaiter>> notifyers_;
void Await(std::shared_ptr<Awaiter> notifyer);
virtual void DoAwait() = 0;
void DoDone();
private:
std::shared_ptr<a8::Results> results_;
std::function<void()> cb_;
friend class f8::Coroutine;
};
}

10
a8/promise.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
namespace a8
{
class Promise : public Awaiter
{
};
}

17
a8/result.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
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_;
};
}