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.
This commit is contained in:
Charles Dang 2017-08-28 20:44:57 +11:00
parent 9324349cb3
commit 699047766a
3 changed files with 14 additions and 73 deletions

View file

@ -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;
}

View file

@ -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<wesnothd_connection>(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));
}

View file

@ -41,7 +41,8 @@ namespace boost
}
class config;
class wesnothd_connection_ptr;
using wesnothd_connection_ptr = std::shared_ptr<class wesnothd_connection>;
/** A class that represents a TCP/IP connection to the wesnothd server. */
class wesnothd_connection : public std::enable_shared_from_this<wesnothd_connection>
@ -56,7 +57,7 @@ private:
/**
* Constructor.
*
* May only be called via wesnothd_connection_ptr
* May only be called by @ref create (or another function of this class).
* @param host Name of the host to connect to
* @param service Service identifier such as "80" or "http"
*/
@ -160,48 +161,3 @@ private:
std::size_t bytes_read_;
};
class wesnothd_connection_ptr
{
private:
friend class wesnothd_connection;
wesnothd_connection_ptr(std::shared_ptr<wesnothd_connection>&& 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<wesnothd_connection> ptr_;
};