Skip to content

Commit 7d9e5cb

Browse files
committed
Add user-defined string literals to create buffers
1 parent ab588fb commit 7d9e5cb

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

tests/buffer.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,38 @@ TEST_CASE("const_buffer creation with str_buffer", "[buffer]")
250250
CHECK(std::string(static_cast<const char*>(b2.data()), b2.size()) == "hello");
251251
}
252252

253+
TEST_CASE("const_buffer creation with zbuf string literal char", "[buffer]")
254+
{
255+
using namespace zmq::literals;
256+
constexpr zmq::const_buffer b = "hello"_zbuf;
257+
CHECK(b.size() == 5);
258+
CHECK(std::memcmp(b.data(), "hello", b.size()) == 0);
259+
}
260+
261+
TEST_CASE("const_buffer creation with zbuf string literal wchar_t", "[buffer]")
262+
{
263+
using namespace zmq::literals;
264+
constexpr zmq::const_buffer b = L"hello"_zbuf;
265+
CHECK(b.size() == 5 * sizeof(wchar_t));
266+
CHECK(std::memcmp(b.data(), L"hello", b.size()) == 0);
267+
}
268+
269+
TEST_CASE("const_buffer creation with zbuf string literal char16_t", "[buffer]")
270+
{
271+
using namespace zmq::literals;
272+
constexpr zmq::const_buffer b = u"hello"_zbuf;
273+
CHECK(b.size() == 5 * sizeof(char16_t));
274+
CHECK(std::memcmp(b.data(), u"hello", b.size()) == 0);
275+
}
276+
277+
TEST_CASE("const_buffer creation with zbuf string literal char32_t", "[buffer]")
278+
{
279+
using namespace zmq::literals;
280+
constexpr zmq::const_buffer b = U"hello"_zbuf;
281+
CHECK(b.size() == 5 * sizeof(char32_t));
282+
CHECK(std::memcmp(b.data(), U"hello", b.size()) == 0);
283+
}
284+
253285
TEST_CASE("buffer of structs", "[buffer]")
254286
{
255287
struct some_pod

zmq.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,26 @@ constexpr const_buffer str_buffer(const Char (&data)[N]) noexcept
11241124
(N - 1) * sizeof(Char));
11251125
}
11261126

1127+
namespace literals
1128+
{
1129+
constexpr const_buffer operator"" _zbuf(const char* str, size_t len) noexcept
1130+
{
1131+
return const_buffer(str, len * sizeof(char));
1132+
}
1133+
constexpr const_buffer operator"" _zbuf(const wchar_t* str, size_t len) noexcept
1134+
{
1135+
return const_buffer(str, len * sizeof(wchar_t));
1136+
}
1137+
constexpr const_buffer operator"" _zbuf(const char16_t* str, size_t len) noexcept
1138+
{
1139+
return const_buffer(str, len * sizeof(char16_t));
1140+
}
1141+
constexpr const_buffer operator"" _zbuf(const char32_t* str, size_t len) noexcept
1142+
{
1143+
return const_buffer(str, len * sizeof(char32_t));
1144+
}
1145+
}
1146+
11271147
#endif // ZMQ_CPP11
11281148

11291149
namespace detail

0 commit comments

Comments
 (0)