Skip to content

Commit

Permalink
Merge pull request #325 from gummif/gfa/msg-range-ctor
Browse files Browse the repository at this point in the history
Problem: message_t ctor for ranges too greedy
  • Loading branch information
sigiesec authored Jun 1, 2019
2 parents 0672e31 + f412ea9 commit 4e37816
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 7 deletions.
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ add_executable(
poller.cpp
active_poller.cpp
multipart.cpp
monitor.cpp
monitor.cpp
utilities.cpp
)

add_dependencies(unit_tests catch)
Expand Down
6 changes: 6 additions & 0 deletions tests/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ TEST_CASE("message constructor with iterators", "[message]")
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}

TEST_CASE("message constructor with size", "[message]")
{
const zmq::message_t msg(5);
CHECK(msg.size() == 5);
}

TEST_CASE("message constructor with buffer and size", "[message]")
{
const std::string hi(data);
Expand Down
2 changes: 1 addition & 1 deletion tests/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ TEST_CASE("socket send recv message_t", "[socket]")
s2.bind("inproc://test");
s.connect("inproc://test");

zmq::message_t smsg(size_t{10});
zmq::message_t smsg(10);
const auto res_send = s2.send(smsg, zmq::send_flags::none);
CHECK(res_send);
CHECK(*res_send == 10);
Expand Down
96 changes: 96 additions & 0 deletions tests/utilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <catch.hpp>
#include <zmq.hpp>

#ifdef ZMQ_CPP11

namespace test_ns
{
struct T_nr
{
};
struct T_mr
{
void *begin() const noexcept { return nullptr; }
void *end() const noexcept { return nullptr; }
};
struct T_fr
{
};
void *begin(const T_fr &) noexcept
{
return nullptr;
}
void *end(const T_fr &) noexcept
{
return nullptr;
}
struct T_mfr
{
void *begin() const noexcept { return nullptr; }
void *end() const noexcept { return nullptr; }
};
void *begin(const T_mfr &) noexcept
{
return nullptr;
}
void *end(const T_mfr &) noexcept
{
return nullptr;
}

// types with associated namespace std
struct T_assoc_ns_nr : std::exception
{
};
struct T_assoc_ns_mr : std::exception
{
void *begin() const noexcept { return nullptr; }
void *end() const noexcept { return nullptr; }
};
struct T_assoc_ns_fr : std::exception
{
};
void *begin(const T_assoc_ns_fr &) noexcept
{
return nullptr;
}
void *end(const T_assoc_ns_fr &) noexcept
{
return nullptr;
}
struct T_assoc_ns_mfr : std::exception
{
void *begin() const noexcept { return nullptr; }
void *end() const noexcept { return nullptr; }
};
void *begin(const T_assoc_ns_mfr &) noexcept
{
return nullptr;
}
void *end(const T_assoc_ns_mfr &) noexcept
{
return nullptr;
}
} // namespace test_ns

TEST_CASE("range SFINAE", "[utilities]")
{
CHECK(!zmq::detail::is_range<int>::value);
CHECK(zmq::detail::is_range<std::string>::value);
CHECK(zmq::detail::is_range<std::string &>::value);
CHECK(zmq::detail::is_range<const std::string &>::value);
CHECK(zmq::detail::is_range<decltype("hello")>::value);
CHECK(zmq::detail::is_range<std::initializer_list<int>>::value);

CHECK(!zmq::detail::is_range<test_ns::T_nr>::value);
CHECK(zmq::detail::is_range<test_ns::T_mr>::value);
CHECK(zmq::detail::is_range<test_ns::T_fr>::value);
CHECK(zmq::detail::is_range<test_ns::T_mfr>::value);

CHECK(!zmq::detail::is_range<test_ns::T_assoc_ns_nr>::value);
CHECK(zmq::detail::is_range<test_ns::T_assoc_ns_mr>::value);
CHECK(zmq::detail::is_range<test_ns::T_assoc_ns_fr>::value);
CHECK(zmq::detail::is_range<test_ns::T_assoc_ns_mfr>::value);
}

#endif
62 changes: 57 additions & 5 deletions zmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,55 @@ typedef struct

namespace zmq
{

#ifdef ZMQ_CPP11
namespace detail
{
namespace ranges
{
using std::begin;
using std::end;
template<class T>
auto begin(T&& r) -> decltype(begin(std::forward<T>(r)))
{
return begin(std::forward<T>(r));
}
template<class T>
auto end(T&& r) -> decltype(end(std::forward<T>(r)))
{
return end(std::forward<T>(r));
}
} // namespace ranges

template<class T> using void_t = void;

template<class Iter>
using iter_value_t = typename std::iterator_traits<Iter>::value_type;

template<class Range>
using range_iter_t = decltype(
ranges::begin(std::declval<typename std::remove_reference<Range>::type &>()));

template<class Range>
using range_value_t = iter_value_t<range_iter_t<Range>>;

template<class T, class = void> struct is_range : std::false_type
{
};

template<class T>
struct is_range<
T,
void_t<decltype(
ranges::begin(std::declval<typename std::remove_reference<T>::type &>())
== ranges::end(std::declval<typename std::remove_reference<T>::type &>()))>>
: std::true_type
{
};

} // namespace detail
#endif

typedef zmq_free_fn free_fn;
typedef zmq_pollitem_t pollitem_t;

Expand Down Expand Up @@ -278,11 +327,14 @@ class message_t
throw error_t();
}

#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
// TODO: this function is too greedy, must add
// SFINAE for begin and end support.
template<typename T>
explicit message_t(const T &msg_) : message_t(std::begin(msg_), std::end(msg_))
#ifdef ZMQ_CPP11
template<class Range,
typename = typename std::enable_if<
detail::is_range<Range>::value
&& std::is_trivially_copyable<detail::range_value_t<Range>>::value
&& !std::is_same<Range, message_t>::value>::type>
explicit message_t(const Range &rng) :
message_t(detail::ranges::begin(rng), detail::ranges::end(rng))
{
}
#endif
Expand Down

0 comments on commit 4e37816

Please sign in to comment.