|
2 | 2 |
|
3 | 3 | #include <memory> |
4 | 4 | #include <functional> |
5 | | -#include <cstdlib> |
6 | | -#include <cstdarg> |
| 5 | +#include <string> |
7 | 6 | #include <zmq.h> |
8 | 7 |
|
| 8 | +#include <boost/format.hpp> |
9 | 9 |
|
10 | 10 | template < typename T > |
11 | 11 | struct cptr |
@@ -70,7 +70,7 @@ class error : public std::exception |
70 | 70 | * used like a regular %socket pointer with the C API. For example |
71 | 71 | * the `zmq_sendmsg` is not implemented but can be used nevertheless. |
72 | 72 | * \code |
73 | | - * if ( zmq_sendmsg(socket, msg, flags) != 0 ) |
| 73 | + * if ( zmq_sendmsg(socket, msg, flags) == -1 ) |
74 | 74 | * throw zmq::error(); |
75 | 75 | * \endcode |
76 | 76 | * When using the C API directly, errors have to be checked explicitly! |
@@ -79,7 +79,7 @@ class error : public std::exception |
79 | 79 | */ |
80 | 80 | class socket |
81 | 81 | { |
82 | | - std::unique_ptr<void, std::function<void(void*)>> m_socket; |
| 82 | + std::unique_ptr < void, std::function < void(void*) > > m_socket; |
83 | 83 | public: |
84 | 84 | /** \brief Constructor |
85 | 85 | * |
@@ -89,7 +89,7 @@ class socket |
89 | 89 | * \param[in] new_socket pre-allocated ZeroMQ socket |
90 | 90 | * \throws zmq::error |
91 | 91 | */ |
92 | | - explicit socket(void * new_socket) throw(zmq::error) |
| 92 | + explicit socket(void * new_socket) |
93 | 93 | : m_socket( new_socket, zmq_close ) |
94 | 94 | { |
95 | 95 | if ( !new_socket ) |
@@ -112,9 +112,9 @@ class socket |
112 | 112 | * \param[in] endpoint string with transport protocal and address |
113 | 113 | * \throws zmq::error |
114 | 114 | */ |
115 | | - void bind(char const *endpoint) const throw(zmq::error) |
| 115 | + void bind(char const *endpoint) const |
116 | 116 | { |
117 | | - if ( zmq_bind(*this, endpoint) != 0 ) |
| 117 | + if ( zmq_bind(*this, endpoint) == -1 ) |
118 | 118 | throw error(); |
119 | 119 | } |
120 | 120 |
|
@@ -126,31 +126,36 @@ class socket |
126 | 126 | * \param[in] endpoint string with transport protocal and address |
127 | 127 | * \throws zmq::error |
128 | 128 | */ |
129 | | - void connect(char const *endpoint) const throw(zmq::error) |
| 129 | + void connect(char const *endpoint) const |
130 | 130 | { |
131 | | - if ( zmq_connect(*this, endpoint) != 0 ) |
| 131 | + if ( zmq_connect(*this, endpoint) == -1 ) |
132 | 132 | throw error(); |
133 | 133 | } |
134 | | - |
| 134 | + |
135 | 135 | /** \brief Send a message part on a socket |
136 | 136 | * |
137 | 137 | * The send() function shall queue a message created from its |
138 | 138 | * printf-style arguments. |
139 | 139 | * |
140 | | - * \param[in] fmt printf-style string for argument formatting |
141 | | - * \param[in] ... variadic arguments |
| 140 | + * \param[in] fmt printf-style string for argument formatting |
| 141 | + * \param[in] args variadic arguments |
| 142 | + * \returns number of bytes written (without zero terminator) |
142 | 143 | */ |
143 | | - void send(char const *fmt, ...) const __attribute__((format (printf, 2, 3))); |
144 | | - void send(char const *fmt, ...) |
| 144 | + template < typename... Args > |
| 145 | + int send(char const *fmt, Args&&... args) |
145 | 146 | { |
146 | | - va_list args; |
147 | | - va_start(args, fmt); |
148 | | - cptr<char> tmp; |
149 | | - int len = vasprintf(&tmp.ptr, fmt, args); |
150 | | - if ( len == -1 ) |
151 | | - throw std::bad_alloc(); |
152 | | - if ( zmq_send(*this, tmp, len+1, 0) == -1 ) |
| 147 | + boost::format formatter(fmt); |
| 148 | + |
| 149 | + // This is horrible. In C++17 we have the binary fold expression |
| 150 | + using expander = int[]; |
| 151 | + (void) expander { 0, (void(formatter % std::forward<Args>(args)), 0)... }; |
| 152 | + |
| 153 | + std::string tmp = formatter.str(); |
| 154 | + int len = tmp.size(); |
| 155 | + |
| 156 | + if ( zmq_send(*this, tmp.c_str(), len+1, 0) == -1 ) |
153 | 157 | throw error(); |
| 158 | + return len; |
154 | 159 | } |
155 | 160 | }; |
156 | 161 |
|
@@ -162,9 +167,9 @@ class socket |
162 | 167 | * implemented. The context object implicitly behaves like a pointer |
163 | 168 | * to void which is what ZeroMQ expects. Thus a context object can be |
164 | 169 | * used like a regular %context pointer with the C API. For example |
165 | | - * the `zmq_sendmsg` is not implemented but can be used nevertheless. |
| 170 | + * the `zmq_ctx_set` is not implemented but can be used nevertheless. |
166 | 171 | * \code |
167 | | - * if ( zmq_ctx_set(context, ZMQ_IPV6, 1) != 0 ) |
| 172 | + * if ( zmq_ctx_set(context, ZMQ_IPV6, 1) == -1 ) |
168 | 173 | * throw zmq::error(); |
169 | 174 | * \endcode |
170 | 175 | * When using the C API directly, errors have to be checked explicitly! |
@@ -173,7 +178,7 @@ class socket |
173 | 178 | */ |
174 | 179 | class context |
175 | 180 | { |
176 | | - std::unique_ptr<void, std::function<int(void*)>> m_context; |
| 181 | + std::unique_ptr < void, std::function < int(void*) > > m_context; |
177 | 182 | public: |
178 | 183 | /** \brief Constructor */ |
179 | 184 | context() noexcept : m_context( zmq_ctx_new(), zmq_ctx_term ) {} |
@@ -195,6 +200,7 @@ class context |
195 | 200 | */ |
196 | 201 | zmq::socket socket(int type) const |
197 | 202 | { |
| 203 | + /** errors of `zmq_socket` are handled in the zmq::socket constructor */ |
198 | 204 | return zmq::socket( zmq_socket(*this, type) ); |
199 | 205 | } |
200 | 206 | }; |
|