Skip to content

Commit

Permalink
Improved range detection supporting ADL
Browse files Browse the repository at this point in the history
  • Loading branch information
gummif committed May 31, 2019
1 parent 2d496cb commit f412ea9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 16 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
11 changes: 0 additions & 11 deletions tests/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ static_assert(std::is_nothrow_swappable<zmq::message_t>::value,
"message_t should be nothrow swappable");
#endif

#ifdef ZMQ_CPP11
TEST_CASE("range SFINAE", "[message]")
{
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);
}
#endif

TEST_CASE("message default constructed", "[message]")
{
const zmq::message_t message;
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
25 changes: 21 additions & 4 deletions zmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,30 @@ 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(
std::begin(std::declval<typename std::remove_reference<Range>::type &>()));
ranges::begin(std::declval<typename std::remove_reference<Range>::type &>()));

template<class Range>
using range_value_t = iter_value_t<range_iter_t<Range>>;
Expand All @@ -176,8 +192,8 @@ template<class T>
struct is_range<
T,
void_t<decltype(
std::begin(std::declval<typename std::remove_reference<T>::type &>())
== std::end(std::declval<typename std::remove_reference<T>::type &>()))>>
ranges::begin(std::declval<typename std::remove_reference<T>::type &>())
== ranges::end(std::declval<typename std::remove_reference<T>::type &>()))>>
: std::true_type
{
};
Expand Down Expand Up @@ -316,7 +332,8 @@ class message_t
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(std::begin(rng), std::end(rng))
explicit message_t(const Range &rng) :
message_t(detail::ranges::begin(rng), detail::ranges::end(rng))
{
}
#endif
Expand Down

0 comments on commit f412ea9

Please sign in to comment.