Improved range detection supporting ADL

This commit is contained in:
Gudmundur Adalsteinsson 2019-05-31 12:09:20 +00:00
parent 2d496cb296
commit f412ea9e34
4 changed files with 119 additions and 16 deletions

View File

@ -25,7 +25,8 @@ add_executable(
poller.cpp
active_poller.cpp
multipart.cpp
monitor.cpp
monitor.cpp
utilities.cpp
)
add_dependencies(unit_tests catch)

View File

@ -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;

96
tests/utilities.cpp Normal file
View File

@ -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
zmq.hpp
View File

@ -156,6 +156,22 @@ 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>
@ -163,7 +179,7 @@ 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>>;
@ -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
{
};
@ -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