Skip to content

Commit fef905d

Browse files
authored
Merge pull request #399 from gummif/gfa/message-ctor
Problem: message_t ctor string inconsistency
2 parents e9716fa + ae570b0 commit fef905d

File tree

2 files changed

+85
-16
lines changed

2 files changed

+85
-16
lines changed

tests/message.cpp

+39-6
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,40 @@ TEST_CASE("message constructor with char array", "[message]")
7070
CHECK(0 == memcmp(data, hi_msg.data(), 2));
7171
}
7272

73-
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
74-
TEST_CASE("message constructor with container", "[message]")
73+
#if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
74+
TEST_CASE("message constructor with container - deprecated", "[message]")
7575
{
76-
const std::string hi(data);
77-
zmq::message_t hi_msg(hi);
78-
CHECK(2u == hi_msg.size());
79-
CHECK(0 == memcmp(data, hi_msg.data(), 2));
76+
zmq::message_t hi_msg("Hi"); // deprecated
77+
REQUIRE(3u == hi_msg.size());
78+
CHECK(0 == memcmp(data, hi_msg.data(), 3));
79+
}
80+
81+
TEST_CASE("message constructor with container of trivial data", "[message]")
82+
{
83+
int buf[3] = {1, 2, 3};
84+
zmq::message_t msg(buf);
85+
REQUIRE(sizeof(buf) == msg.size());
86+
CHECK(0 == memcmp(buf, msg.data(), msg.size()));
87+
}
88+
89+
TEST_CASE("message constructor with strings", "[message]")
90+
{
91+
SECTION("string")
92+
{
93+
const std::string hi(data);
94+
zmq::message_t hi_msg(hi);
95+
CHECK(2u == hi_msg.size());
96+
CHECK(0 == memcmp(data, hi_msg.data(), 2));
97+
}
98+
#if CPPZMQ_HAS_STRING_VIEW
99+
SECTION("string_view")
100+
{
101+
const std::string_view hi(data);
102+
zmq::message_t hi_msg(hi);
103+
CHECK(2u == hi_msg.size());
104+
CHECK(0 == memcmp(data, hi_msg.data(), 2));
105+
}
106+
#endif
80107
}
81108
#endif
82109

@@ -162,6 +189,12 @@ TEST_CASE("message to string", "[message]")
162189
CHECK(a.to_string_view() == "");
163190
CHECK(b.to_string_view() == "Foo");
164191
#endif
192+
193+
#if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
194+
const zmq::message_t depr("Foo"); // deprecated
195+
CHECK(depr.to_string() != "Foo");
196+
CHECK(depr.to_string() == std::string("Foo", 4));
197+
#endif
165198
}
166199

167200
#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)

zmq.hpp

+46-10
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ inline int poll(std::vector<zmq_pollitem_t> &items, long timeout_ = -1)
342342
}
343343

344344
template<std::size_t SIZE>
345-
inline int poll(std::array<zmq_pollitem_t, SIZE>& items,
346-
std::chrono::milliseconds timeout)
345+
inline int poll(std::array<zmq_pollitem_t, SIZE> &items,
346+
std::chrono::milliseconds timeout)
347347
{
348348
return poll(items.data(), items.size(), static_cast<long>(timeout.count()));
349349
}
@@ -362,6 +362,20 @@ inline std::tuple<int, int, int> version()
362362
zmq_version(&std::get<0>(v), &std::get<1>(v), &std::get<2>(v));
363363
return v;
364364
}
365+
366+
#if !defined(ZMQ_CPP11_PARTIAL)
367+
namespace detail
368+
{
369+
template<class T> struct is_char_type
370+
{
371+
// true if character type for string literals in C++11
372+
static constexpr bool value =
373+
std::is_same<T, char>::value || std::is_same<T, wchar_t>::value
374+
|| std::is_same<T, char16_t>::value || std::is_same<T, char32_t>::value;
375+
};
376+
}
377+
#endif
378+
365379
#endif
366380

367381
class message_t
@@ -398,8 +412,7 @@ class message_t
398412
int rc = zmq_msg_init_size(&msg, size_);
399413
if (rc != 0)
400414
throw error_t();
401-
if (size_)
402-
{
415+
if (size_) {
403416
// this constructor allows (nullptr, 0),
404417
// memcpy with a null pointer is UB
405418
memcpy(data(), data_, size_);
@@ -413,16 +426,40 @@ class message_t
413426
throw error_t();
414427
}
415428

429+
// overload set of string-like types and generic containers
416430
#if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
431+
// NOTE this constructor will include the null terminator
432+
// when called with a string literal.
433+
// An overload taking const char* can not be added because
434+
// it would be preferred over this function and break compatiblity.
435+
template<
436+
class Char,
437+
size_t N,
438+
typename = typename std::enable_if<detail::is_char_type<Char>::value>::type>
439+
ZMQ_DEPRECATED("from 4.7.0, use constructors taking iterators, (pointer, size) "
440+
"or strings instead")
441+
explicit message_t(const Char (&data)[N]) :
442+
message_t(detail::ranges::begin(data), detail::ranges::end(data))
443+
{
444+
}
445+
417446
template<class Range,
418447
typename = typename std::enable_if<
419448
detail::is_range<Range>::value
420449
&& ZMQ_IS_TRIVIALLY_COPYABLE(detail::range_value_t<Range>)
450+
&& !detail::is_char_type<detail::range_value_t<Range>>::value
421451
&& !std::is_same<Range, message_t>::value>::type>
422452
explicit message_t(const Range &rng) :
423453
message_t(detail::ranges::begin(rng), detail::ranges::end(rng))
424454
{
425455
}
456+
457+
explicit message_t(const std::string &str) : message_t(str.data(), str.size()) {}
458+
459+
#if CPPZMQ_HAS_STRING_VIEW
460+
explicit message_t(std::string_view str) : message_t(str.data(), str.size()) {}
461+
#endif
462+
426463
#endif
427464

428465
#ifdef ZMQ_HAS_RVALUE_REFS
@@ -1337,19 +1374,19 @@ template<int Opt, int NullTerm = 1> struct array_option
13371374

13381375
#define ZMQ_DEFINE_INTEGRAL_OPT(OPT, NAME, TYPE) \
13391376
using NAME##_t = integral_option<OPT, TYPE, false>; \
1340-
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME{}
1377+
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
13411378
#define ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(OPT, NAME, TYPE) \
13421379
using NAME##_t = integral_option<OPT, TYPE, true>; \
1343-
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME{}
1380+
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
13441381
#define ZMQ_DEFINE_ARRAY_OPT(OPT, NAME) \
13451382
using NAME##_t = array_option<OPT>; \
1346-
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME{}
1383+
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
13471384
#define ZMQ_DEFINE_ARRAY_OPT_BINARY(OPT, NAME) \
13481385
using NAME##_t = array_option<OPT, 0>; \
1349-
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME{}
1386+
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
13501387
#define ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(OPT, NAME) \
13511388
using NAME##_t = array_option<OPT, 2>; \
1352-
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME{}
1389+
ZMQ_INLINE_VAR ZMQ_CONSTEXPR_VAR NAME##_t NAME {}
13531390

13541391
// duplicate definition from libzmq 4.3.3
13551392
#if defined _WIN32
@@ -2161,7 +2198,6 @@ class socket_t : public detail::socket_base
21612198
throw error_t();
21622199
if (ctxptr == ZMQ_NULLPTR)
21632200
throw error_t();
2164-
21652201
}
21662202
};
21672203

0 commit comments

Comments
 (0)