Skip to content

Commit

Permalink
Add user-defined string literals to create buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
gummif committed Sep 11, 2019
1 parent ab588fb commit 7d9e5cb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,38 @@ TEST_CASE("const_buffer creation with str_buffer", "[buffer]")
CHECK(std::string(static_cast<const char*>(b2.data()), b2.size()) == "hello");
}

TEST_CASE("const_buffer creation with zbuf string literal char", "[buffer]")
{
using namespace zmq::literals;
constexpr zmq::const_buffer b = "hello"_zbuf;
CHECK(b.size() == 5);
CHECK(std::memcmp(b.data(), "hello", b.size()) == 0);
}

TEST_CASE("const_buffer creation with zbuf string literal wchar_t", "[buffer]")
{
using namespace zmq::literals;
constexpr zmq::const_buffer b = L"hello"_zbuf;
CHECK(b.size() == 5 * sizeof(wchar_t));
CHECK(std::memcmp(b.data(), L"hello", b.size()) == 0);
}

TEST_CASE("const_buffer creation with zbuf string literal char16_t", "[buffer]")
{
using namespace zmq::literals;
constexpr zmq::const_buffer b = u"hello"_zbuf;
CHECK(b.size() == 5 * sizeof(char16_t));
CHECK(std::memcmp(b.data(), u"hello", b.size()) == 0);
}

TEST_CASE("const_buffer creation with zbuf string literal char32_t", "[buffer]")
{
using namespace zmq::literals;
constexpr zmq::const_buffer b = U"hello"_zbuf;
CHECK(b.size() == 5 * sizeof(char32_t));
CHECK(std::memcmp(b.data(), U"hello", b.size()) == 0);
}

TEST_CASE("buffer of structs", "[buffer]")
{
struct some_pod
Expand Down
20 changes: 20 additions & 0 deletions zmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,26 @@ constexpr const_buffer str_buffer(const Char (&data)[N]) noexcept
(N - 1) * sizeof(Char));
}

namespace literals
{
constexpr const_buffer operator"" _zbuf(const char* str, size_t len) noexcept
{
return const_buffer(str, len * sizeof(char));
}
constexpr const_buffer operator"" _zbuf(const wchar_t* str, size_t len) noexcept
{
return const_buffer(str, len * sizeof(wchar_t));
}
constexpr const_buffer operator"" _zbuf(const char16_t* str, size_t len) noexcept
{
return const_buffer(str, len * sizeof(char16_t));
}
constexpr const_buffer operator"" _zbuf(const char32_t* str, size_t len) noexcept
{
return const_buffer(str, len * sizeof(char32_t));
}
}

#endif // ZMQ_CPP11

namespace detail
Expand Down

0 comments on commit 7d9e5cb

Please sign in to comment.