comparison C++/SocketListener.h @ 277:b544a599e08e

Socket: remove enum class
author David Demelier <markand@malikania.fr>
date Thu, 23 Oct 2014 17:59:14 +0200
parents f7000cc599d0
children adcae2bde2f0
comparison
equal deleted inserted replaced
276:05f0a3e09cbf 277:b544a599e08e
37 * @enum SocketDirection 37 * @enum SocketDirection
38 * @brief The SocketDirection enum 38 * @brief The SocketDirection enum
39 * 39 *
40 * Bitmask that can be set to both reading and writing. 40 * Bitmask that can be set to both reading and writing.
41 */ 41 */
42 enum class SocketDirection { 42 enum SocketDirection {
43 Read = (1 << 0), //!< only for receive 43 Read = (1 << 0), //!< only for receive
44 Write = (1 << 1) //!< only for sending 44 Write = (1 << 1) //!< only for sending
45 }; 45 };
46 46
47 inline SocketDirection operator&(SocketDirection x, SocketDirection y)
48 {
49 return static_cast<SocketDirection>(static_cast<int>(x) & static_cast<int>(y));
50 }
51
52 inline SocketDirection operator|(SocketDirection x, SocketDirection y)
53 {
54 return static_cast<SocketDirection>(static_cast<int>(x) | static_cast<int>(y));
55 }
56
57 inline SocketDirection operator^(SocketDirection x, SocketDirection y)
58 {
59 return static_cast<SocketDirection>(static_cast<int>(x) ^ static_cast<int>(y));
60 }
61
62 inline SocketDirection operator~(SocketDirection x)
63 {
64 return static_cast<SocketDirection>(~static_cast<int>(x));
65 }
66
67 inline SocketDirection &operator&=(SocketDirection &x, SocketDirection y)
68 {
69 x = x & y;
70
71 return x;
72 }
73
74 inline SocketDirection &operator|=(SocketDirection &x, SocketDirection y)
75 {
76 x = x | y;
77
78 return x;
79 }
80
81 inline SocketDirection &operator^=(SocketDirection &x, SocketDirection y)
82 {
83 x = x ^ y;
84
85 return x;
86 }
87
88 /** 47 /**
89 * @enum SocketMethod 48 * @enum SocketMethod
90 * @brief The SocketMethod enum 49 * @brief The SocketMethod enum
91 * 50 *
92 * Select the method of polling. It is only a preferred method, for example if you 51 * Select the method of polling. It is only a preferred method, for example if you
93 * request for poll but it is not available, select will be used. 52 * request for poll but it is not available, select will be used.
94 */ 53 */
95 enum class SocketMethod { 54 enum SocketMethod {
96 Select, //!< select(2) method, fallback 55 Select, //!< select(2) method, fallback
97 Poll //!< poll(2), everywhere possible 56 Poll //!< poll(2), everywhere possible
98 }; 57 };
99 58
100 /** 59 /**
103 * 62 *
104 * Result of a select call, returns the first ready socket found with its 63 * Result of a select call, returns the first ready socket found with its
105 * direction. 64 * direction.
106 */ 65 */
107 struct SocketStatus { 66 struct SocketStatus {
108 Socket socket; //!< which socket is ready 67 Socket socket; //!< which socket is ready
109 SocketDirection direction; //!< the direction 68 int direction; //!< the direction
110 }; 69 };
111 70
112 /** 71 /**
113 * @class SocketListener 72 * @class SocketListener
114 * @brief Synchronous multiplexing 73 * @brief Synchronous multiplexing
118 class SocketListener final { 77 class SocketListener final {
119 public: 78 public:
120 /** 79 /**
121 * @brief Function for listing all sockets 80 * @brief Function for listing all sockets
122 */ 81 */
123 using MapFunc = std::function<void (Socket &, SocketDirection)>; 82 using MapFunc = std::function<void (Socket &, int)>;
124 83
125 #if defined(SOCKET_LISTENER_HAVE_POLL) 84 #if defined(SOCKET_LISTENER_HAVE_POLL)
126 static constexpr const SocketMethod PreferredMethod = SocketMethod::Poll; 85 static constexpr const SocketMethod PreferredMethod = SocketMethod::Poll;
127 #else 86 #else
128 static constexpr const SocketMethod PreferredMethod = SocketMethod::Select; 87 static constexpr const SocketMethod PreferredMethod = SocketMethod::Select;
150 * Add a socket with a specified direction. 109 * Add a socket with a specified direction.
151 * 110 *
152 * @param s the socket 111 * @param s the socket
153 * @param direction the direction 112 * @param direction the direction
154 */ 113 */
155 virtual void add(Socket &&s, SocketDirection direction) = 0; 114 virtual void add(Socket s, int direction) = 0;
156 115
157 /** 116 /**
158 * Remove a socket with a specified direction. 117 * Remove a socket with a specified direction.
159 * 118 *
160 * @param s the socket 119 * @param s the socket
161 * @param direction the direction 120 * @param direction the direction
162 */ 121 */
163 virtual void remove(const Socket &s, SocketDirection direction) = 0; 122 virtual void remove(const Socket &s, int direction) = 0;
164 123
165 /** 124 /**
166 * Remove all sockets. 125 * Remove all sockets.
167 */ 126 */
168 virtual void clear() = 0; 127 virtual void clear() = 0;
214 /** 173 /**
215 * Create a socket listener. 174 * Create a socket listener.
216 * 175 *
217 * @param method the preferred method 176 * @param method the preferred method
218 */ 177 */
219 SocketListener(SocketMethod method = SocketMethod::Poll); 178 SocketListener(int method = Poll);
220 179
221 /** 180 /**
222 * Add a socket to listen to. 181 * Add a socket to listen to.
223 * 182 *
224 * @param s the socket 183 * @param s the socket
225 * @param direction the direction 184 * @param direction the direction
226 */ 185 */
227 inline void add(Socket s, SocketDirection direction) 186 inline void add(Socket s, int direction)
228 { 187 {
229 m_interface->add(std::move(s), direction); 188 m_interface->add(std::move(s), direction);
230 } 189 }
231 190
232 /** 191 /**
233 * Remove a socket from the list. 192 * Remove a socket from the list.
234 * 193 *
235 * @param s the socket 194 * @param s the socket
236 * @param direction the direction 195 * @param direction the direction
237 */ 196 */
238 inline void remove(const Socket &s, SocketDirection direction) 197 inline void remove(const Socket &s, int direction)
239 { 198 {
240 m_interface->remove(s, direction); 199 m_interface->remove(s, direction);
241 } 200 }
242 201
243 /** 202 /**