Enable move semantics for socket types

This commit is contained in:
Chris Thrasher 2023-08-21 21:29:39 -06:00
parent 29ed8bf3a0
commit e5c41c4eb5
6 changed files with 67 additions and 8 deletions

View File

@ -83,6 +83,18 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Socket& operator=(const Socket&) = delete; Socket& operator=(const Socket&) = delete;
////////////////////////////////////////////////////////////
/// \brief Move constructor
///
////////////////////////////////////////////////////////////
Socket(Socket&& socket) noexcept;
////////////////////////////////////////////////////////////
/// \brief Move assignment
///
////////////////////////////////////////////////////////////
Socket& operator=(Socket&& socket) noexcept;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Set the blocking state of the socket /// \brief Set the blocking state of the socket
/// ///

View File

@ -31,6 +31,7 @@
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <ostream> #include <ostream>
#include <utility>
namespace sf namespace sf
@ -49,6 +50,28 @@ Socket::~Socket()
} }
////////////////////////////////////////////////////////////
Socket::Socket(Socket&& socket) noexcept :
m_type(socket.m_type),
m_socket(std::exchange(socket.m_socket, priv::SocketImpl::invalidSocket())),
m_isBlocking(socket.m_isBlocking)
{
}
////////////////////////////////////////////////////////////
Socket& Socket::operator=(Socket&& socket) noexcept
{
if (&socket == this)
return *this;
m_type = socket.m_type;
m_socket = std::exchange(socket.m_socket, priv::SocketImpl::invalidSocket());
m_isBlocking = socket.m_isBlocking;
return *this;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Socket::setBlocking(bool blocking) void Socket::setBlocking(bool blocking)
{ {

View File

@ -23,8 +23,8 @@ TEST_CASE("[Network] sf::Socket")
STATIC_CHECK(!std::is_constructible_v<sf::Socket>); STATIC_CHECK(!std::is_constructible_v<sf::Socket>);
STATIC_CHECK(!std::is_copy_constructible_v<sf::Socket>); STATIC_CHECK(!std::is_copy_constructible_v<sf::Socket>);
STATIC_CHECK(!std::is_copy_assignable_v<sf::Socket>); STATIC_CHECK(!std::is_copy_assignable_v<sf::Socket>);
STATIC_CHECK(!std::is_nothrow_move_constructible_v<sf::Socket>); STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Socket>);
STATIC_CHECK(!std::is_nothrow_move_assignable_v<sf::Socket>); STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Socket>);
} }
SECTION("Constants") SECTION("Constants")
@ -41,6 +41,30 @@ TEST_CASE("[Network] sf::Socket")
CHECK(testSocket.getHandle() == invalidHandle); CHECK(testSocket.getHandle() == invalidHandle);
} }
SECTION("Move semantics")
{
SECTION("Construction")
{
TestSocket movedTestSocket;
movedTestSocket.setBlocking(false);
movedTestSocket.create();
const TestSocket testSocket(std::move(movedTestSocket));
CHECK(!testSocket.isBlocking());
CHECK(testSocket.getHandle() != invalidHandle);
}
SECTION("Assignment")
{
TestSocket movedTestSocket;
movedTestSocket.setBlocking(false);
movedTestSocket.create();
TestSocket testSocket;
testSocket = std::move(movedTestSocket);
CHECK(!testSocket.isBlocking());
CHECK(testSocket.getHandle() != invalidHandle);
}
}
SECTION("Set/get blocking") SECTION("Set/get blocking")
{ {
TestSocket testSocket; TestSocket testSocket;

View File

@ -4,5 +4,5 @@
static_assert(!std::is_copy_constructible_v<sf::TcpListener>); static_assert(!std::is_copy_constructible_v<sf::TcpListener>);
static_assert(!std::is_copy_assignable_v<sf::TcpListener>); static_assert(!std::is_copy_assignable_v<sf::TcpListener>);
static_assert(!std::is_nothrow_move_constructible_v<sf::TcpListener>); static_assert(std::is_nothrow_move_constructible_v<sf::TcpListener>);
static_assert(!std::is_nothrow_move_assignable_v<sf::TcpListener>); static_assert(std::is_nothrow_move_assignable_v<sf::TcpListener>);

View File

@ -4,5 +4,5 @@
static_assert(!std::is_copy_constructible_v<sf::TcpSocket>); static_assert(!std::is_copy_constructible_v<sf::TcpSocket>);
static_assert(!std::is_copy_assignable_v<sf::TcpSocket>); static_assert(!std::is_copy_assignable_v<sf::TcpSocket>);
static_assert(!std::is_nothrow_move_constructible_v<sf::TcpSocket>); static_assert(std::is_nothrow_move_constructible_v<sf::TcpSocket>);
static_assert(!std::is_nothrow_move_assignable_v<sf::TcpSocket>); static_assert(std::is_nothrow_move_assignable_v<sf::TcpSocket>);

View File

@ -4,5 +4,5 @@
static_assert(!std::is_copy_constructible_v<sf::UdpSocket>); static_assert(!std::is_copy_constructible_v<sf::UdpSocket>);
static_assert(!std::is_copy_assignable_v<sf::UdpSocket>); static_assert(!std::is_copy_assignable_v<sf::UdpSocket>);
static_assert(!std::is_nothrow_move_constructible_v<sf::UdpSocket>); static_assert(std::is_nothrow_move_constructible_v<sf::UdpSocket>);
static_assert(!std::is_nothrow_move_assignable_v<sf::UdpSocket>); static_assert(std::is_nothrow_move_assignable_v<sf::UdpSocket>);