diff --git a/main.cpp b/main.cpp index 622c388..38b2adf 100644 --- a/main.cpp +++ b/main.cpp @@ -62,6 +62,14 @@ auto start(boost::asio::io_context & io, Settings const& settings) -> void CommandThread::start(*connection); echo_thread(*connection); + connection->add_listener([](SnoteEvent& event) { + std::cout << "SNOTE " << static_cast(event.get_tag()) << std::endl; + for (auto c : event.get_results()) + { + std::cout << " " << std::string_view{c.first, c.second} << std::endl; + } + }); + boost::asio::co_spawn( io, connection->connect(io, settings.host, settings.service), diff --git a/snote_thread.cpp b/snote_thread.cpp index a64572b..6a74427 100644 --- a/snote_thread.cpp +++ b/snote_thread.cpp @@ -155,19 +155,7 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr case HS_SCAN_TERMINATED: { auto& pattern = patterns[match_id]; - std::match_results results; - if (not std::regex_match(message.begin(), message.end(), results, pattern.regex)) - { - // something went wrong - hyperscan disagrees with std::regex - abort(); - } - - std::vector parts; - for (auto const sub : results) - { - parts.push_back(std::string_view{sub.first, sub.second}); - } - connection.make_event(pattern.tag, std::move(parts)); + connection.make_event(pattern.tag, pattern.regex, message); break; } @@ -180,6 +168,28 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr return thread; } +auto SnoteEvent::get_tag() const -> SnoteTag +{ + return tag_; +} + +auto SnoteEvent::get_results() -> std::match_results const& +{ + if (auto results = std::get_if<1>(&components_)) { + return *results; + } + + auto& regex = std::get<0>(components_).first; + auto message = std::get<0>(components_).second; + auto& results = components_.emplace<1>(); + if (not std::regex_match(message.begin(), message.end(), results, regex)) + { + // something went wrong - hyperscan disagrees with std::regex + abort(); + } + return results; +} + auto SnoteThread::DbDeleter::operator()(hs_database_t * db) const -> void { if (HS_SUCCESS != hs_free_database(db)) diff --git a/snote_thread.hpp b/snote_thread.hpp index 08a4ab9..0bf759f 100644 --- a/snote_thread.hpp +++ b/snote_thread.hpp @@ -3,8 +3,9 @@ #include "event.hpp" #include +#include #include -#include +#include class Connection; struct hs_database; @@ -27,15 +28,20 @@ enum class SnoteTag Killed, }; -struct SnoteEvent : Event +class SnoteEvent : public Event { - SnoteEvent(SnoteTag tag, std::vector parts) - : tag{tag} - , parts{std::move(parts)} +public: + SnoteEvent(SnoteTag tag, std::regex const& regex, std::string_view full) + : tag_{tag} + , components_{std::make_pair(std::ref(regex), full)} {} - SnoteTag tag; - std::vector parts; + auto get_tag() const -> SnoteTag; + auto get_results() -> std::match_results const&; + +private: + SnoteTag tag_; + std::variant, std::match_results> components_; }; struct SnoteThread