Skip to content
Snippets Groups Projects
Commit de768167 authored by Daniel Müller's avatar Daniel Müller :speech_balloon:
Browse files

Add SockCopy wrapper for copyable sockets

- SockCopy class implements copy constructor and assignments that clone
  the wrapped socket
- This allows for implicitly copyable sockets, but only if wrapped in a
  SockCopy
parent 78fd471b
Branches
No related tags found
No related merge requests found
Pipeline #71669 passed
......@@ -7,5 +7,6 @@
#include "tcplistener.hpp"
#include "udpsocket.hpp"
#include "resolver.hpp"
#include "sockcopy.hpp"
#endif // _NETLIB_HPP
\ No newline at end of file
#ifndef _SOCKCOPY_HPP
#define _SOCKCOPY_HPP
#include "tcpstream.hpp"
#include "tcplistener.hpp"
#include "udpsocket.hpp"
namespace netlib
{
/**
* @brief A special wrapper type that can be used to create
* automatically copyable socket objects. Without wrapping
* socket objects in this class, an explicit clone function
* is needed to copy socket objects.
*/
template <class SockType>
class SockCopy
{
private:
/**
* @brief The wrapped socket instance.
*/
SockType socket;
public:
/**
* @brief Wrap the given socket so that it is copyable.
*
* @note Autoclose should most likely be set to false on
* the either one or both of the sockets to prevent accidentally
* closing the socket.
*
* @param socket The socket that will be wrapped.
*/
SockCopy(const SockType &socket)
: socket{std::move(socket.clone())}
{ }
/**
* @brief Wrap the given socket so that it is copyable.
*
* @param socket The socket that will be wrapped.
*/
SockCopy(SockType &&socket)
: socket{std::move(socket)}
{ }
/**
* @brief Copy the given wrapped socket.
*
* @note Autoclose should most likely be set to false on
* the either one or both of the sockets to prevent accidentally
* closing the socket.
*
* @param socket The warpped socket to copy.
*/
SockCopy(const SockCopy & other)
: SockCopy{std::move(other.socket.clone())}
{ }
/**
* @brief Copy the given wrapped socket.
*
* @note Autoclose should most likely be set to false on
* the either one or both of the sockets to prevent accidentally
* closing the socket.
*
* @param socket The warpped socket to copy.
*/
SockCopy & operator=(const SockCopy & other)
{
socket = std::move(other.socket.clone());
}
/**
* @brief Get the wrapped socket
*/
SockType & getSocket()
{
return socket;
}
/**
* @brief Get the wrapped socket
*/
SockType & operator() ()
{
return socket;
}
};
SockCopy<TcpStream> wrapCopy(TcpStream & other, bool disableAutoclose = true)
{
if (disableAutoclose) other.setAutoclose(false);
return SockCopy<TcpStream>(other);
}
SockCopy<TcpListener> wrapCopy(TcpListener & other, bool disableAutoclose = true)
{
if (disableAutoclose) other.setAutoclose(false);
return SockCopy<TcpListener>(other);
}
SockCopy<UdpSocket> wrapCopy(UdpSocket & other, bool disableAutoclose = true)
{
if (disableAutoclose) other.setAutoclose(false);
return SockCopy<UdpSocket>(other);
}
SockCopy<TcpStream> wrapCopy(TcpStream && other, bool disableAutoclose = true)
{
if (disableAutoclose) other.setAutoclose(false);
return SockCopy<TcpStream>(std::move(other));
}
SockCopy<TcpListener> wrapCopy(TcpListener && other, bool disableAutoclose = true)
{
if (disableAutoclose) other.setAutoclose(false);
return SockCopy<TcpListener>(std::move(other));
}
SockCopy<UdpSocket> wrapCopy(UdpSocket && other, bool disableAutoclose = true)
{
if (disableAutoclose) other.setAutoclose(false);
return SockCopy<UdpSocket>(std::move(other));
}
} // namespace netlib
#endif // _SOCKCOPY_HPP
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment