This file contains 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 <algorithm> | |
#include <istream> | |
#include <limits> | |
#include <ranges> | |
#include <utility> | |
uint32_t count_increasing(std::istream &input) { | |
return std::ranges::count_if( | |
std::ranges::istream_view<uint32_t>(input), | |
[prev = std::numeric_limits<uint32_t>::max()](uint32_t curr) mutable |
This file contains 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 |
This file contains 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 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 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 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 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 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 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 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); } |