Skip to content

Commit

Permalink
Refactor out wesnothd_connection_ptr wrapper class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Vultraz committed Aug 28, 2017
1 parent 9324349 commit 6990477
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/game_initialization/multiplayer.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
33 changes: 9 additions & 24 deletions src/wesnothd_connection.cpp
Expand Up @@ -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));
}
50 changes: 3 additions & 47 deletions src/wesnothd_connection.hpp
Expand Up @@ -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>
Expand All @@ -56,7 +57,7 @@ class wesnothd_connection : public std::enable_shared_from_this<wesnothd_connect
/**
* 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"
*/
Expand Down Expand Up @@ -160,48 +161,3 @@ class wesnothd_connection : public std::enable_shared_from_this<wesnothd_connect
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_;
};

0 comments on commit 6990477

Please sign in to comment.