diff --git a/a8/awaiter.cc b/a8/awaiter.cc new file mode 100644 index 0000000..d9db7b8 --- /dev/null +++ b/a8/awaiter.cc @@ -0,0 +1,31 @@ +#include + +#include + +namespace a8 +{ + + void Awaiter::Await(std::shared_ptr notifyer) + { + notifyers_.push_back(notifyer); + DoAwait(); + } + +#if 0 + std::shared_ptr Awaiter::Sleep(int time) + { + return std::make_shared(time); + } +#endif + + void Awaiter::DoDone() + { + done_ = true; + for (auto notifyer : notifyers_) { + if (!notifyer.expired()) { + notifyer.lock()->DoResume(); + } + } + } + +} diff --git a/a8/awaiter.h b/a8/awaiter.h new file mode 100644 index 0000000..b7d09ff --- /dev/null +++ b/a8/awaiter.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +namespace f8 +{ + class Coroutine; +} + +namespace a8 +{ + + class Awaiter : public std::enable_shared_from_this + { + public: + virtual ~Awaiter() {}; + + std::shared_ptr GetResult() { return results_; } + bool Done() const { return done_; } + virtual void DoResume() {}; + + protected: + bool done_ = false; + + std::list> notifyers_; + void Await(std::shared_ptr notifyer); + virtual void DoAwait() = 0; + void DoDone(); + + private: + std::shared_ptr results_; + std::function cb_; + + friend class f8::Coroutine; + }; + +} diff --git a/a8/promise.h b/a8/promise.h new file mode 100644 index 0000000..b602e28 --- /dev/null +++ b/a8/promise.h @@ -0,0 +1,10 @@ +#pragma once + +namespace a8 +{ + + class Promise : public Awaiter + { + }; + +} diff --git a/a8/result.h b/a8/result.h new file mode 100644 index 0000000..950f41a --- /dev/null +++ b/a8/result.h @@ -0,0 +1,17 @@ +#pragma once + +namespace a8 +{ + class Results + { + public: + + Results(std::vector results):results_(std::move(results)) {}; + + template + T Get(size_t index) const { return std::any_cast(results_.at(index));}; + + private: + std::vector results_; + }; +}