This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct main_task { | |
| struct main_promise { | |
| using handle_t = std::coroutine_handle<main_promise>; | |
| main_task get_return_object() { | |
| return main_task{handle_t::from_promise(*this)}; | |
| } | |
| std::suspend_never initial_suspend() noexcept { return {}; } | |
| std::suspend_always final_suspend() noexcept { return {}; } | |
| void unhandled_exception() { std::terminate(); } | |
| void return_value(int ret) { ret_ = ret; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ces::main_task async_main(int argc, char *argv[]) { | |
| std::cout << "Arguments: \n"; | |
| for (int i = 0; i < argc; i++) { | |
| std::cout << "\t" << argv[i] << "\n"; | |
| } | |
| co_return 0; | |
| } | |
| int main(int argc, char *argv[]) { return async_main(argc, argv); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ces::chainable_task async_op(bool flag) { | |
| using namespace ces::utils; | |
| if (flag) { | |
| co_return AwaitResult{ | |
| EventType::Error, 1, | |
| "Couldn't find the truth in the eye of the beholder."}; | |
| } else { | |
| co_return AwaitResult{EventType::WakeUp, 0, ""}; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct awaitable_final_continuation { | |
| constexpr bool await_ready() const noexcept { return false; } | |
| std::coroutine_handle<> | |
| await_suspend(HandleWithContinuation auto caller) const noexcept { | |
| return caller.promise().continuation; | |
| } | |
| constexpr void await_resume() const noexcept {} | |
| }; | |
| struct chainable_task { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| scheduler_task main_scheduler_impl() { | |
| auto &promise = co_await scheduler_task::get_promise; | |
| while (true) { | |
| for (auto &em : promise.emitters_) { | |
| for (auto *ev = em->emit(); ev != nullptr; ev = em->emit()) { | |
| co_yield ev; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| awaitable_handoff yield_value(utils::AwaitableData *data) { | |
| if (data == nullptr) | |
| return awaitable_handoff(std::noop_coroutine()); | |
| for (auto &em : emitters_) { | |
| em->notify_departure(data); | |
| } | |
| return awaitable_handoff(data->continuation); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct universal_awaiter { | |
| utils::AwaitableData data_; | |
| std::coroutine_handle<> handle_; | |
| bool await_ready() { | |
| if (!data_.condition) | |
| return false; | |
| bool result = data_.condition(); | |
| if (result) | |
| data_.result.result_type = utils::EventType::WakeUp; | |
| return result; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void schedule(CoroutineWithContinuation auto &coro) { | |
| handle_.promise().scheduled_.push_back(coro.handle_); | |
| coro.handle_.promise().continuation = handle_; | |
| } | |
| void register_emitter(std::unique_ptr<EventEmitter> emitter); | |
| void run() { handle_.resume(); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| auto &sched = ces::main_scheduler(); | |
| sched.register_emitter(std::make_unique<ces::TimeoutEmitter>()); | |
| sched.register_emitter(std::make_unique<ces::ConditionEmitter>()); | |
| sched.register_emitter(std::make_unique<ces::EpollEmitter>()); | |
| ces::Socket s = | |
| ces::Socket::ServerSocket(ces::SockAddr(ces::IPv4{}, "127.0.0.1", 9090)); | |
| auto server = async_server(s.without_timeout(), echo_server_handle); | |
| auto client = echo_client(s.without_timeout()); | |
| sched.schedule(server); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "trivial.h" | |
| #include <gtest/gtest.h> | |
| #include <sstream> | |
| TEST(TrivialTest, Simple) { | |
| std::vector<std::pair<std::string, uint32_t>> inputs = { | |
| {"", 0}, // empty input 0 increasing | |
| {"0", 0}, // single element 0 increasing | |
| {"0 1", 1}, // two elements, with increase |