Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#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