diff --git a/include/yail/core.hpp b/include/yail/core.hpp index 1ae9f77..d2b31b6 100644 --- a/include/yail/core.hpp +++ b/include/yail/core.hpp @@ -34,6 +34,10 @@ struct Ping { } }; +struct AnyMessage { + std::string_view data; +}; + } // namespace event template @@ -124,12 +128,15 @@ struct IrcClient { co_await boost::asio::async_write(this->socket, boost::asio::buffer(request), boost::asio::transfer_all(), boost::asio::use_awaitable); this->nick = std::move(nick); }; + + static constexpr auto kHandledEvents = [](utempl::TypeList...>>) { + return utempl::kTypeList; + }(utempl::kType); + 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 SendEvent(Event&& event) + -> decltype(std::get>(kHandledEvents)>(this->callbacks).f(*this, std::forward(event))) { + return std::get>(kHandledEvents)>(this->callbacks).f(*this, std::forward(event)); }; auto Pong(std::string_view buff) -> boost::asio::awaitable { const auto pong = std::format("PONG {}", buff); @@ -148,13 +155,16 @@ struct IrcClient { [&](Function&... fs) -> boost::asio::awaitable { auto self = this; auto& str = messageStr; + if constexpr(requires { self->SendEvent(event::AnyMessage{messageStr}); }) { + co_await self->SendEvent(event::AnyMessage{messageStr}); + }; // clang-format off (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 }, diff --git a/main.cpp b/main.cpp index 271ee5c..f60da1d 100644 --- a/main.cpp +++ b/main.cpp @@ -16,6 +16,11 @@ auto Test() -> boost::asio::awaitable { .server = "irc.ilita.i2p"} .AddState, "ruUsers">() // NOLINTNEXTLINE + .With([](auto& self, yail::event::AnyMessage message) -> boost::asio::awaitable { + std::println("Readed message: {}", message.data); + co_return; + }) + // NOLINTNEXTLINE .With([](auto& self, UserEvent event) -> boost::asio::awaitable { std::println("User event!"); co_return;