Commit 41839213 authored by Dominik Charousset's avatar Dominik Charousset

Remove non-standard operator== for variant<...>

parent 63052d26
......@@ -193,7 +193,7 @@ public:
template <int Pos>
bool is(std::integral_constant<int, Pos>) const {
return type_ == Pos;
return type_ == static_cast<size_t>(Pos);
}
template <class T>
......@@ -348,19 +348,6 @@ bool operator==(const variant<Ts...>& x, const variant<Ts...>& y) {
return visit(f, y);
}
/// @relates variant
template <class T, class... Ts>
bool operator==(const T& x, const variant<Ts...>& y) {
variant_compare_helper<variant<Ts...>, std::equal_to> f{y};
return f(x);
}
/// @relates variant
template <class T, class... Ts>
bool operator==(const variant<Ts...>& x, const T& y) {
return y == x;
}
/// @relates variant
template <class... Ts>
bool operator<(const variant<Ts...>& x, const variant<Ts...>& y) {
......
......@@ -138,6 +138,18 @@ struct test_visitor {
} // namespace <anonymous>
CAF_TEST(constructors) {
variant<int, string> a{42};
variant<string, atom_value> b{atom("foo")};
variant<float, int, string> c{string{"bar"}};
variant<int, string, double> d{123};
CAF_CHECK_EQUAL(a, 42);
CAF_CHECK_EQUAL(b, atom("foo"));
CAF_CHECK_EQUAL(d, 123);
CAF_CHECK_EQUAL(d, 123.0); // the unit test framework supports fuzzy compare
CAF_CHECK_NOT_EQUAL(d, std::string{"123"});
}
CAF_TEST(n_ary_visit) {
variant<int, string> a{42};
variant<string, atom_value> b{atom("foo")};
......
......@@ -490,7 +490,7 @@ void basp_broker_state::cleanup(connection_handle hdl) {
auto i = ctx_tcp.find(hdl);
if (i != ctx_tcp.end()) {
auto& ref = i->second;
CAF_ASSERT(i->first == ref.hdl);
CAF_ASSERT(i->first == get<connection_handle>(ref.hdl));
if (ref.callback) {
CAF_LOG_DEBUG("connection closed during handshake");
ref.callback->deliver(sec::disconnect_during_handshake);
......@@ -513,7 +513,7 @@ void basp_broker_state::cleanup(datagram_handle hdl) {
auto i = ctx_udp.find(hdl);
if (i != ctx_udp.end()) {
auto& ref = i->second;
CAF_ASSERT(i->first == ref.hdl);
CAF_ASSERT(i->first == get<datagram_handle>(ref.hdl));
if (ref.callback) {
CAF_LOG_DEBUG("connection closed during handshake");
ref.callback->deliver(sec::disconnect_during_handshake);
......
......@@ -43,6 +43,16 @@ struct anything { };
anything any_vals;
template <class T>
bool operator==(anything, const T&) {
return true;
}
template <class T>
bool operator==(const T&, anything) {
return true;
}
template <class T>
using maybe = caf::variant<anything, T>;
......@@ -76,16 +86,6 @@ ostream& operator<<(ostream& out, const maybe<T>& x) {
namespace caf {
template <class T, class U>
bool operator==(const maybe<T>& x, const U& y) {
return get_if<anything>(&x) != nullptr || get<T>(x) == y;
}
template <class T, class U>
bool operator==(const T& x, const maybe<U>& y) {
return (y == x);
}
template <class T>
std::string to_string(const maybe<T>& x) {
return !get_if<anything>(&x) ? std::string{"*"} : deep_to_string(get<T>(x));
......
......@@ -47,6 +47,16 @@ struct anything { };
anything any_vals;
template <class T>
bool operator==(anything, const T&) {
return true;
}
template <class T>
bool operator==(const T&, anything) {
return true;
}
template <class T>
using maybe = caf::variant<anything, T>;
......@@ -62,16 +72,6 @@ constexpr auto config_serv_atom = caf::atom("ConfigServ");
namespace caf {
template <class T, class U>
bool operator==(const maybe<T>& x, const U& y) {
return get_if<anything>(&x) != nullptr || get<T>(x) == y;
}
template <class T, class U>
bool operator==(const T& x, const maybe<U>& y) {
return (y == x);
}
template <class T>
std::string to_string(const maybe<T>& x) {
return !get_if<anything>(&x) ? std::string{"*"} : deep_to_string(get<T>(x));
......
......@@ -34,6 +34,7 @@
#include "caf/logger.hpp"
#include "caf/optional.hpp"
#include "caf/term.hpp"
#include "caf/variant.hpp"
#include "caf/detail/arg_wrapper.hpp"
......@@ -51,12 +52,24 @@ struct negated {
}
};
struct equal_to {
template <class T, class Comparator>
struct compare_visitor {
const T& rhs;
template <class U>
bool operator()(const U& lhs) const {
Comparator f;
return f(lhs, rhs);
}
};
struct equality_operator {
template <class T, class U,
typename std::enable_if<std::is_floating_point<T>::value
|| std::is_floating_point<U>::value,
typename std::enable_if<(std::is_floating_point<T>::value
|| std::is_floating_point<U>::value)
&& detail::is_comparable<T, U>::value,
int>::type = 0>
bool operator()(const T& t, const U& u) {
bool operator()(const T& t, const U& u) const {
auto x = static_cast<long double>(t);
auto y = static_cast<long double>(u);
auto max = std::max(std::abs(x), std::abs(y));
......@@ -66,22 +79,80 @@ struct equal_to {
template <class T, class U,
typename std::enable_if<!std::is_floating_point<T>::value
&& !std::is_floating_point<U>::value,
&& !std::is_floating_point<U>::value
&& detail::is_comparable<T, U>::value,
int>::type = 0>
bool operator()(const T& x, const U& y) {
bool operator()(const T& x, const U& y) const {
return x == y;
}
template <class T, class U,
typename std::enable_if<!detail::is_comparable<T, U>::value,
int>::type = 0>
bool operator()(const T&, const U&) const {
return false;
}
};
// note: we could use negated<equal_to>, but that would give us `!(x == y)`
// instead of `x != y` and thus messes with coverage testing
struct not_equal_to {
template <class T, class U>
bool operator()(const T& x, const U& y) {
struct inequality_operator {
template <class T, class U,
typename std::enable_if<(std::is_floating_point<T>::value
|| std::is_floating_point<U>::value)
&& detail::is_comparable<T, U>::value,
int>::type = 0>
bool operator()(const T& x, const U& y) const {
equality_operator f;
return !f(x, y);
}
template <class T, class U,
typename std::enable_if<!std::is_floating_point<T>::value
&& !std::is_floating_point<U>::value
&& detail::is_comparable<T, U>::value,
int>::type = 0>
bool operator()(const T& x, const U& y) const {
return x != y;
}
template <class T, class U,
typename std::enable_if<!detail::is_comparable<T, U>::value,
int>::type = 0>
bool operator()(const T&, const U&) const {
return true;
}
};
template <class Operator>
struct comparison {
// -- default case -----------------------------------------------------------
template <class T, class U>
detail::enable_if_t<!SumType<T>() && !SumType<U>(), bool>
operator()(const T& x, const U& y) {
Operator f;
return f(x, y);
}
// -- automagic unboxing of sum types ----------------------------------------
template <class T, class U>
detail::enable_if_t<SumType<T>(), bool>
operator()(const T& lhs, const U& rhs) const {
compare_visitor<U, comparison> f{rhs};
return visit(f, lhs);
}
template <class T, class U>
detail::enable_if_t<!SumType<T>() && SumType<U>(), bool>
operator()(const T& lhs, const U& rhs) const {
return (*this)(rhs, lhs);
}
};
using equal_to = comparison<equality_operator>;
using not_equal_to = comparison<inequality_operator>;
struct less_than {
template <class T, class U>
bool operator()(const T& x, const U& y) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment