Skip to content

Commit a186ed6

Browse files
authored
Merge pull request #203 from kurdybacha/master
Problem: message_t could be easier to construct and is missing equal not equal operators
2 parents 3185dd1 + f518a64 commit a186ed6

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fetch_googletest(
2222

2323
add_executable(
2424
unit_tests
25+
message.cpp
2526
context.cpp
2627
socket.cpp
2728
poller.cpp

tests/message.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <gtest/gtest.h>
2+
#include <zmq.hpp>
3+
4+
TEST (message, create_destroy)
5+
{
6+
zmq::message_t message;
7+
}
8+
9+
TEST (message, constructors)
10+
{
11+
const std::string hi ("Hi");
12+
zmq::message_t hi_msg_a (hi.begin (), hi.end ());
13+
ASSERT_EQ (hi_msg_a.size (), hi.size ());
14+
zmq::message_t hi_msg_b (hi.data (), hi.size ());
15+
ASSERT_EQ (hi_msg_b.size (), hi.size ());
16+
ASSERT_EQ (hi_msg_a, hi_msg_b);
17+
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
18+
zmq::message_t hello_msg_a ("Hello");
19+
ASSERT_NE (hi_msg_a, hello_msg_a);
20+
ASSERT_NE (hi_msg_b, hello_msg_a);
21+
zmq::message_t hi_msg_c (hi);
22+
ASSERT_EQ (hi_msg_c, hi_msg_a);
23+
ASSERT_EQ (hi_msg_c, hi_msg_b);
24+
ASSERT_NE (hi_msg_c, hello_msg_a);
25+
#endif
26+
#ifdef ZMQ_HAS_RVALUE_REFS
27+
zmq::message_t hello_msg_b(zmq::message_t("Hello"));
28+
ASSERT_EQ (hello_msg_a, hello_msg_b);
29+
#endif
30+
}

tests/poller.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,7 @@ TEST(poller, poll_basic)
167167
zmq::socket_t sink{context, zmq::socket_type::pull};
168168
ASSERT_NO_THROW(sink.connect(endpoint));
169169

170-
const std::string message = "H";
171-
172-
// TODO: send overload for string would be handy.
173-
ASSERT_NO_THROW(vent.send(std::begin(message), std::end(message)));
170+
ASSERT_NO_THROW(vent.send("Hi"));
174171

175172
zmq::poller_t poller;
176173
bool message_received = false;
@@ -217,7 +214,7 @@ TEST(poller, client_server)
217214
// Setup client and send message
218215
zmq::socket_t client{context, zmq::socket_type::dealer};
219216
ASSERT_NO_THROW(client.connect(endpoint));
220-
ASSERT_NO_THROW(client.send(std::begin(send_msg), std::end(send_msg)));
217+
ASSERT_NO_THROW(client.send(send_msg));
221218

222219
ASSERT_NO_THROW(poller.wait(std::chrono::milliseconds{-1}));
223220
ASSERT_TRUE(got_pollin);

zmq.hpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ namespace zmq
271271
throw error_t ();
272272
}
273273

274+
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
275+
template<typename T> message_t (const T &msg_)
276+
: message_t (std::begin (msg_), std::end (msg_))
277+
{}
278+
#endif
279+
274280
#ifdef ZMQ_HAS_RVALUE_REFS
275281
inline message_t (message_t &&rhs): msg (rhs.msg)
276282
{
@@ -379,15 +385,30 @@ namespace zmq
379385
return static_cast<T const*>( data() );
380386
}
381387

388+
ZMQ_DEPRECATED("from 4.3.0, use operator== instead")
382389
inline bool equal(const message_t* other) const ZMQ_NOTHROW
383390
{
384391
if (size() != other->size())
385392
return false;
386-
std::string a(data<char>(), size());
387-
std::string b(other->data<char>(), other->size());
393+
const std::string a(data<char>(), size());
394+
const std::string b(other->data<char>(), other->size());
395+
return a == b;
396+
}
397+
398+
inline bool operator==(const message_t &other) const ZMQ_NOTHROW
399+
{
400+
if (size () != other.size ())
401+
return false;
402+
const std::string a(data<char>(), size());
403+
const std::string b(other.data<char>(), other.size());
388404
return a == b;
389405
}
390406

407+
inline bool operator!=(const message_t &other) const ZMQ_NOTHROW
408+
{
409+
return !(*this == other);
410+
}
411+
391412
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
392413
inline const char* gets(const char *property_)
393414
{

0 commit comments

Comments
 (0)