Skip to content

Commit

Permalink
implement operator== for feature
Browse files Browse the repository at this point in the history
  • Loading branch information
louwers committed Oct 17, 2024
1 parent d3aa775 commit d7e8b41
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
46 changes: 17 additions & 29 deletions include/maplibre/feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,6 @@
namespace maplibre {
namespace feature {

// comparator functors
struct equal_comp_shared_ptr
{

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
template <typename T>
bool operator()(T const& lhs, T const& rhs) const
{
return lhs == rhs;
}
#pragma GCC diagnostic pop

template <typename T>
bool operator()(std::shared_ptr<T> const& lhs, std::shared_ptr<T> const& rhs) const
{
if (lhs == rhs)
{
return true;
}
return *lhs == *rhs;
}
};

struct value;

struct null_value_t
Expand Down Expand Up @@ -104,10 +80,24 @@ struct value : public value_base
value(object_type object) : value_base(std::make_shared<object_type>(std::forward<object_type>(object))) {}
value(object_ptr_type object) : value_base(object) {}

explicit operator bool() const { return !std::holds_alternative<null_value_t>(variant()); }
bool operator==(const value& rhs) const
{
if (index() != rhs.index()) return false;

if (std::holds_alternative<value::array_ptr_type>(*this))
{
return *std::get<value::array_ptr_type>(*this) == *std::get<value::array_ptr_type>(rhs);
}

// TODO: allow comparing values held by shared ptrs
friend bool operator==(const value&, const value&) = default;
if (std::holds_alternative<value::object_ptr_type>(*this))
{
return *std::get<value::object_ptr_type>(*this) == *std::get<value::object_ptr_type>(rhs);
}

return *static_cast<const value_base*>(this) == rhs;
}

explicit operator bool() const { return !std::holds_alternative<null_value_t>(variant()); }

array_ptr_type getArray() noexcept
{
Expand Down Expand Up @@ -209,8 +199,6 @@ struct value : public value_base
}
};

#undef DECLARE_VALUE_TYPE_ACCESOR

using property_map = value::object_type;

// The same considerations and requirement for numeric types apply as for `value_base`.
Expand Down
19 changes: 19 additions & 0 deletions test/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ TEST_CASE("test value")
CHECK_FALSE(intV.getString());
}

TEST_CASE("test value operator==")
{
value true1 = true;
value true2 = true;
value false1 = false;
CHECK(true1 == true2);
CHECK(true1 != false1);

value::array_type vec = {1, 2, 3};
value arr1 = std::make_shared<value::array_type>(vec);
value arr2 = std::make_shared<value::array_type>(vec);
CHECK(arr1 == arr2);

value::object_type obj = {{"hello", 1}, {"world", true}};
value obj1 = std::make_shared<value::object_type>(obj);
value obj2 = std::make_shared<value::object_type>(obj);
CHECK(obj1 == obj2);
}

TEST_CASE("test feature")
{
feature<int64_t> pf{point<int64_t>()};
Expand Down

0 comments on commit d7e8b41

Please sign in to comment.