1
This commit is contained in:
parent
424f708375
commit
6b0773c2e9
31
a8/awaiter.cc
Normal file
31
a8/awaiter.cc
Normal 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
37
a8/awaiter.h
Normal 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
10
a8/promise.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace a8
|
||||
{
|
||||
|
||||
class Promise : public Awaiter
|
||||
{
|
||||
};
|
||||
|
||||
}
|
17
a8/result.h
Normal file
17
a8/result.h
Normal 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_;
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user