Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace improper static_variant operator overloads with comparators #154

Merged
merged 2 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions include/fc/reflect/typename.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fc/container/flat_fwd.hpp>

namespace fc {
template<typename...> class static_variant;
class value;
class exception;
namespace ip { class address; }
Expand Down Expand Up @@ -41,10 +42,19 @@ namespace fc {
return n.c_str();
}
};
template<typename T> struct get_typename<flat_set<T>>
template<typename T> struct get_typename<flat_set<T>>
{
static const char* name() {
static std::string n = std::string("flat_set<") + get_typename<T>::name() + ">";
return n.c_str();
}
};
template<typename... Ts>
struct get_typename<flat_set<static_variant<Ts...>, typename static_variant<Ts...>::type_lt>>
{
static const char* name() {
static std::string n = std::string("flat_set<") + get_typename<T>::name() + ">";
using TN = get_typename<static_variant<Ts...>>;
static std::string n = std::string("flat_set<") + TN::name() + ", " + TN::name() + "::type_lt>";
return n.c_str();
}
};
Expand Down
16 changes: 8 additions & 8 deletions include/fc/static_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ class static_variant {
static constexpr int value = impl::position<X, Types...>::pos;
};

struct type_lt {
bool operator()(const static_variant& a, const static_variant& b) const { return a.which() < b.which(); }
};
struct type_eq {
bool operator()(const static_variant& a, const static_variant& b) const { return a.which() == b.which(); }
};
using flat_set_type = flat_set<static_variant, type_lt>;

static_variant()
{
init_from_tag(0);
Expand Down Expand Up @@ -352,14 +360,6 @@ class static_variant {
v.visit( impl::move_construct<static_variant>(*this) );
return *this;
}
friend bool operator == ( const static_variant& a, const static_variant& b )
{
return a.which() == b.which();
}
friend bool operator < ( const static_variant& a, const static_variant& b )
{
return a.which() < b.which();
}

template<typename X, typename = type_in_typelist<X>>
X& get() {
Expand Down
7 changes: 5 additions & 2 deletions tests/variant_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ BOOST_AUTO_TEST_CASE( nested_objects_test )

from_variant( v, sv1, nested_levels + 2 );

BOOST_CHECK( sv == sv1 );
auto sv_equal = [](const fc::static_variant<fc::test::item>& v1, const fc::static_variant<fc::test::item>& v2) {
return v1.get<fc::test::item>() == v2.get<fc::test::item>();
};
BOOST_CHECK( sv_equal(sv, sv1) );

// both log and dump should never throw
BOOST_TEST_MESSAGE( "========== About to log static_variant. ==========" );
Expand Down Expand Up @@ -215,7 +218,7 @@ BOOST_AUTO_TEST_CASE( nested_objects_test )

from_variant( v, vec1, nested_levels + 3 );

BOOST_CHECK( vec == vec1 );
BOOST_CHECK( std::equal(vec.begin(), vec.end(), vec1.begin(), sv_equal) );

// both log and dump should never throw
BOOST_TEST_MESSAGE( "========== About to log vector. ==========" );
Expand Down