From 699047766a8852014df20c44d5ed84dadb37015e Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Mon, 28 Aug 2017 20:44:57 +1100 Subject: [PATCH] Refactor out wesnothd_connection_ptr wrapper class Just used the shared_ptr directly. The wrapper basically duplicated the functionality of shared_ptr. This also adds moves the stop() call to the wesnothd_connection dtor instead of the wrapper class. --- src/game_initialization/multiplayer.cpp | 4 +- src/wesnothd_connection.cpp | 33 +++++----------- src/wesnothd_connection.hpp | 50 ++----------------------- 3 files changed, 14 insertions(+), 73 deletions(-) diff --git a/src/game_initialization/multiplayer.cpp b/src/game_initialization/multiplayer.cpp index fbd93a4ac0a6..b8c7343ba9e3 100644 --- a/src/game_initialization/multiplayer.cpp +++ b/src/game_initialization/multiplayer.cpp @@ -64,7 +64,7 @@ static wesnothd_connection_ptr open_connection(CVideo& video, const std::string& h = preferences::network_host(); } - wesnothd_connection_ptr sock; + wesnothd_connection_ptr sock(nullptr); const int colon_index = h.find_first_of(":"); std::string host; @@ -120,7 +120,7 @@ static wesnothd_connection_ptr open_connection(CVideo& video, const std::string& throw wesnothd_error(_("Server-side redirect loop")); } shown_hosts.insert(hostpair(host, port)); - sock = wesnothd_connection_ptr(); + sock.reset(); sock = gui2::dialogs::network_transmission::wesnothd_connect_dialog(video, "redirect", host, port); continue; } diff --git a/src/wesnothd_connection.cpp b/src/wesnothd_connection.cpp index 6fe4b72b20ae..8a56454c78af 100644 --- a/src/wesnothd_connection.cpp +++ b/src/wesnothd_connection.cpp @@ -373,33 +373,18 @@ bool wesnothd_connection::receive_data(config& result) wesnothd_connection::~wesnothd_connection() { - MPTEST_LOG; + MPTEST_LOG; + stop(); } -// wesnothd_connection_ptr - - -wesnothd_connection_ptr& wesnothd_connection_ptr::operator=(wesnothd_connection_ptr&& other) -{ - MPTEST_LOG; - if(ptr_) { - ptr_->stop(); - ptr_.reset(); - } - ptr_ = std::move(other.ptr_); - return *this; -} +// +// wesnothd_connection_ptr generator +// wesnothd_connection_ptr wesnothd_connection::create(const std::string& host, const std::string& service) { - //can't use make_shared becasue the ctor is private - return wesnothd_connection_ptr(std::shared_ptr(new wesnothd_connection(host, service))); -} - -wesnothd_connection_ptr::~wesnothd_connection_ptr() -{ - MPTEST_LOG; - if(ptr_) { - ptr_->stop(); - } + // We can't use std::make_shared here since the wesnothd_connection ctor + // is private since we want only this function to be able to create a + // new wesnothd_connection object. + return wesnothd_connection_ptr(new wesnothd_connection(host, service)); } diff --git a/src/wesnothd_connection.hpp b/src/wesnothd_connection.hpp index 80f1f8b85f7f..ac187235024f 100644 --- a/src/wesnothd_connection.hpp +++ b/src/wesnothd_connection.hpp @@ -41,7 +41,8 @@ namespace boost } class config; -class wesnothd_connection_ptr; + +using wesnothd_connection_ptr = std::shared_ptr; /** A class that represents a TCP/IP connection to the wesnothd server. */ class wesnothd_connection : public std::enable_shared_from_this @@ -56,7 +57,7 @@ class wesnothd_connection : public std::enable_shared_from_this&& ptr) - : ptr_(std::move(ptr)) - {} -public: - - wesnothd_connection_ptr() = default; - - wesnothd_connection_ptr(const wesnothd_connection_ptr&) = delete; - wesnothd_connection_ptr& operator=(const wesnothd_connection_ptr&) = delete; -#if defined(_MSC_VER) &&_MSC_VER == 1800 - wesnothd_connection_ptr(wesnothd_connection_ptr&& other) : ptr_(std::move(other.ptr_)) {} -#else - wesnothd_connection_ptr(wesnothd_connection_ptr&&) = default; -#endif - wesnothd_connection_ptr& operator=(wesnothd_connection_ptr&&); - - ~wesnothd_connection_ptr(); - - explicit operator bool() const { - return !!ptr_; - } - wesnothd_connection& operator*() { - return *ptr_; - } - const wesnothd_connection& operator*() const { - return *ptr_; - } - wesnothd_connection* operator->() { - return ptr_.get(); - } - const wesnothd_connection* operator->() const { - return ptr_.get(); - } - wesnothd_connection* get() const { - return ptr_.get(); - } - -private: - std::shared_ptr ptr_; -};