From 0d797e04b83a5013b27ceca539f433814691a182 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Wed, 23 Oct 2024 17:54:36 +0000 Subject: [PATCH] Add user events support --- include/yail/core.hpp | 23 ++++++++++++++--------- main.cpp | 8 ++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/include/yail/core.hpp b/include/yail/core.hpp index ceadd98..da67fa7 100644 --- a/include/yail/core.hpp +++ b/include/yail/core.hpp @@ -118,6 +118,13 @@ struct IrcClient { co_await boost::asio::async_write(this->socket, boost::asio::buffer(request), boost::asio::transfer_all(), boost::asio::use_awaitable); }; + template + auto SendEvent(Event&& event) -> boost::asio::awaitable { + return [&](utempl::TypeList...>>) { + constexpr auto i = utempl::Find>(utempl::kTypeList); + return std::get(this->callbacks).f(*this, std::forward(event)); + }(utempl::kType); + }; auto Pong(std::string_view buff) -> boost::asio::awaitable { const auto pong = std::format("PONG {}", buff); @@ -131,20 +138,18 @@ struct IrcClient { std::string buff; for(;;) { auto messageStr = co_await yail::ReadLine(this->socket, buff); - - std::println("Readed message: {}", messageStr); co_await std::apply( // NOLINTNEXTLINE [&](Function&... fs) -> boost::asio::awaitable { auto self = this; auto& str = messageStr; // clang-format off - ( // NOLINTNEXTLINE - co_await [](auto self, auto& messageStr, auto& fs) -> boost::asio::awaitable { - if(auto value = Ids::TryParse(*self, messageStr)) { - co_await fs.f(*self, *value); - } - }(self, str, fs), - ...); + (co_await [](auto self, auto& messageStr, auto& fs) -> boost::asio::awaitable { // NOLINT + if constexpr(requires {Ids::TryParse(*self, messageStr);}) { + if(auto value = Ids::TryParse(*self, messageStr)) { + co_await fs.f(*self, std::move(*value)); + }; + }; + }(self, str, fs), ...); // clang-format on }, this->callbacks); diff --git a/main.cpp b/main.cpp index fe56583..271ee5c 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,8 @@ #include #include +struct UserEvent {}; + auto Test() -> boost::asio::awaitable { try { auto executor = co_await boost::asio::this_coro::executor; @@ -14,6 +16,11 @@ auto Test() -> boost::asio::awaitable { .server = "irc.ilita.i2p"} .AddState, "ruUsers">() // NOLINTNEXTLINE + .With([](auto& self, UserEvent event) -> boost::asio::awaitable { + std::println("User event!"); + co_return; + }) + // NOLINTNEXTLINE .With([](auto& self, yail::event::Ping ping) -> boost::asio::awaitable { co_await self.Pong(ping.data); }) @@ -21,6 +28,7 @@ auto Test() -> boost::asio::awaitable { .With([](auto& self, yail::event::Welcome message) -> boost::asio::awaitable { std::println("Welcome: {}", message.message); co_await self.Join("#ru"); + co_await self.SendEvent(UserEvent{}); co_await self.SendMessage("#ru", "Hello from yet another irc library"); }) // NOLINTNEXTLINE