From f18a440b203b5f3dd0411c0bab186dcd978ce176 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sun, 26 Nov 2023 15:40:40 -0800 Subject: [PATCH] Pass connection as a reference --- irc_parse_thread.cpp | 8 ++++---- irc_parse_thread.hpp | 2 +- main.cpp | 14 +++++++------- ping_thread.cpp | 6 +++--- ping_thread.hpp | 2 +- registration_thread.cpp | 32 ++++++++++++++++---------------- registration_thread.hpp | 4 ++-- snote_thread.cpp | 6 +++--- snote_thread.hpp | 2 +- 9 files changed, 38 insertions(+), 38 deletions(-) diff --git a/irc_parse_thread.cpp b/irc_parse_thread.cpp index d8f0f31..2bcbf5d 100644 --- a/irc_parse_thread.cpp +++ b/irc_parse_thread.cpp @@ -10,9 +10,9 @@ namespace { } // namespace -auto irc_parse_thread(Connection * connection) -> void +auto irc_parse_thread(Connection& connection) -> void { - connection->add_listener([connection](LineEvent const& event) + connection.add_listener([&connection](LineEvent const& event) { auto const msg = parse_irc_message(event.line); auto const recognized = IrcCommandHash::in_word_set(msg.command.data(), msg.command.size()); @@ -21,6 +21,6 @@ auto irc_parse_thread(Connection * connection) -> void && recognized->min_args <= msg.args.size() && recognized->max_args >= msg.args.size() ? recognized->command : IrcCommand::UNKNOWN; - connection->make_event(command, msg); + connection.make_event(command, msg); }); -} \ No newline at end of file +} diff --git a/irc_parse_thread.hpp b/irc_parse_thread.hpp index a6176e1..7681c42 100644 --- a/irc_parse_thread.hpp +++ b/irc_parse_thread.hpp @@ -293,4 +293,4 @@ struct IrcMsgEvent : Event IrcMsg const& irc; }; -auto irc_parse_thread(Connection * connection) -> void; +auto irc_parse_thread(Connection& connection) -> void; diff --git a/main.cpp b/main.cpp index 1aca1a3..237f343 100644 --- a/main.cpp +++ b/main.cpp @@ -32,9 +32,9 @@ using namespace std::chrono_literals; -auto unhandled_message_thread(Connection * connection) -> void +auto unhandled_message_thread(Connection& connection) -> void { - connection->add_listener([](IrcMsgEvent const& event) + connection.add_listener([](IrcMsgEvent const& event) { if (IrcCommand::UNKNOWN == event.command) { @@ -54,11 +54,11 @@ auto start(boost::asio::io_context & io, Settings const& settings) -> void auto connection = std::make_shared(io); watchdog_thread(*connection); - irc_parse_thread(connection.get()); - ping_thread(connection.get()); - registration_thread(connection.get(), settings.password, settings.username, settings.realname, settings.nickname); - snote_thread(connection.get()); - unhandled_message_thread(connection.get()); + irc_parse_thread(*connection); + ping_thread(*connection); + registration_thread(*connection, settings.password, settings.username, settings.realname, settings.nickname); + snote_thread(*connection); + unhandled_message_thread(*connection); boost::asio::co_spawn( io, diff --git a/ping_thread.cpp b/ping_thread.cpp index 9d3921d..25d5df7 100644 --- a/ping_thread.cpp +++ b/ping_thread.cpp @@ -3,14 +3,14 @@ #include "irc_parse_thread.hpp" #include "write_irc.hpp" -auto ping_thread(Connection * connection) -> void +auto ping_thread(Connection& connection) -> void { - connection->add_listener([connection](IrcMsgEvent& event) + connection.add_listener([&connection](IrcMsgEvent& event) { auto& irc = event.irc; if (IrcCommand::PING == event.command) { - send_pong(*connection, irc.args[0]); + send_pong(connection, irc.args[0]); event.handled_ = true; } }); diff --git a/ping_thread.hpp b/ping_thread.hpp index a45a31c..3864a55 100644 --- a/ping_thread.hpp +++ b/ping_thread.hpp @@ -3,4 +3,4 @@ #include "connection.hpp" #include "thread.hpp" -auto ping_thread(Connection * connection) -> void; +auto ping_thread(Connection& connection) -> void; diff --git a/registration_thread.cpp b/registration_thread.cpp index cbffd4f..58b7584 100644 --- a/registration_thread.cpp +++ b/registration_thread.cpp @@ -7,7 +7,7 @@ namespace { struct RegistrationThread : std::enable_shared_from_this { - Connection * connection_; + Connection& connection_; std::string password_; std::string username_; std::string realname_; @@ -28,7 +28,7 @@ struct RegistrationThread : std::enable_shared_from_this Stage stage_; RegistrationThread( - Connection * connection_, + Connection& connection_, std::string password, std::string username, std::string realname, @@ -47,7 +47,7 @@ struct RegistrationThread : std::enable_shared_from_this }; RegistrationThread::RegistrationThread( - Connection * connection, + Connection& connection, std::string password, std::string username, std::string realname, @@ -63,12 +63,12 @@ RegistrationThread::RegistrationThread( auto RegistrationThread::on_connect() -> void { - send_cap_ls(*connection_); - send_pass(*connection_, password_); - send_user(*connection_, username_, realname_); - send_nick(*connection_, nickname_); + send_cap_ls(connection_); + send_pass(connection_, password_); + send_user(connection_, username_, realname_); + send_nick(connection_, nickname_); - connection_->remove_listener(connect_handle_); + connection_.remove_listener(connect_handle_); } auto RegistrationThread::send_req() -> void @@ -103,13 +103,13 @@ auto RegistrationThread::send_req() -> void if (not outstanding.empty()) { request.pop_back(); - send_cap_req(*connection_, request); + send_cap_req(connection_, request); stage_ = Stage::AckReply; } else { - send_cap_end(*connection_); - connection_->remove_listener(message_handle_); + send_cap_end(connection_); + connection_.remove_listener(message_handle_); } } @@ -128,8 +128,8 @@ auto RegistrationThread::capack(IrcMsg const& msg) -> void ); if (outstanding.empty()) { - send_cap_end(*connection_); - connection_->remove_listener(message_handle_); + send_cap_end(connection_); + connection_.remove_listener(message_handle_); } } } @@ -194,7 +194,7 @@ auto RegistrationThread::on_msg(IrcMsg const& msg) -> void } // namespace auto registration_thread( - Connection * connection, + Connection& connection, std::string password, std::string username, std::string realname, @@ -202,14 +202,14 @@ auto registration_thread( ) -> void { auto const thread = std::make_shared(connection, password, username, realname, nickname); - thread->message_handle_ = connection->add_listener([thread](IrcMsgEvent const& event) + thread->message_handle_ = connection.add_listener([thread](IrcMsgEvent const& event) { if (IrcCommand::CAP == event.command) { thread->on_msg(event.irc); } }); - thread->connect_handle_ = connection->add_listener([thread](ConnectEvent const&) + thread->connect_handle_ = connection.add_listener([thread](ConnectEvent const&) { thread->on_connect(); }); diff --git a/registration_thread.hpp b/registration_thread.hpp index bcf780d..301df68 100644 --- a/registration_thread.hpp +++ b/registration_thread.hpp @@ -10,9 +10,9 @@ #include auto registration_thread( - Connection * connection, + Connection& connection, std::string password, std::string username, std::string realname, std::string nickname -) -> void; \ No newline at end of file +) -> void; diff --git a/snote_thread.cpp b/snote_thread.cpp index 921054a..526678b 100644 --- a/snote_thread.cpp +++ b/snote_thread.cpp @@ -6,10 +6,10 @@ #include -auto snote_thread(Connection * connection) -> void +auto snote_thread(Connection& connection) -> void { static char const* const prefix = "*** Notice -- "; - connection->add_listener([connection](IrcMsgEvent& event) + connection.add_listener([&connection](IrcMsgEvent& event) { auto& args = event.irc.args; if (IrcCommand::NOTICE == event.command @@ -17,7 +17,7 @@ auto snote_thread(Connection * connection) -> void && args[1].starts_with(prefix)) { event.handled_ = true; - connection->make_event(args[1].substr(strlen(prefix))); + connection.make_event(args[1].substr(strlen(prefix))); } }); } diff --git a/snote_thread.hpp b/snote_thread.hpp index 91f66ed..266ce66 100644 --- a/snote_thread.hpp +++ b/snote_thread.hpp @@ -11,4 +11,4 @@ struct SnoteEvent : Event std::string_view raw; }; -auto snote_thread(Connection * connection) -> void; \ No newline at end of file +auto snote_thread(Connection& connection) -> void;