Skip to content

Commit

Permalink
Improve operator== implementation for Message
Browse files Browse the repository at this point in the history
  • Loading branch information
tuokri committed Nov 21, 2023
1 parent 80267c6 commit ebf02af
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
10 changes: 10 additions & 0 deletions include/umb/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ class Message

// TODO: reconsider this API.
[[nodiscard]] constexpr virtual uint16_t type() const noexcept = 0;

protected:
[[nodiscard]] virtual bool is_equal(const Message& msg) const = 0;

friend bool operator==(const Message&, const Message&);
};

bool operator==(const Message& lhs, const Message& rhs)
{
return typeid(lhs) == typeid(rhs) && lhs.is_equal(rhs);
}

}

#endif // USCRIPT_MSGBUF_MESSAGE_HPP
3 changes: 2 additions & 1 deletion templates/cpp_header.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public:
[[nodiscard]] {{ cpp_type(field.type) }} {{ field.name }}() const;
void set_{{ field.name }}({{ cpp_type_arg(field.type) }});
{% endfor %}
friend bool operator==(const {{ message.name }}&, const {{ message.name }}&);
[[nodiscard]] constexpr uint16_t type() const noexcept override
{
return static_cast<uint16_t>(message_type());
Expand All @@ -63,6 +62,8 @@ public:
{
return MessageType::{{ message.name }};
}
protected:
[[nodiscard]] bool is_equal(const ::umb::Message& msg) const override;

private:
{% for field in message.fields %}
Expand Down
11 changes: 6 additions & 5 deletions templates/cpp_source.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,18 @@ void {{ message.name }}::set_{{ field.name }}({{ cpp_type_arg(field.type) }} val
m_{{ field.name }} = value;
}

{% endfor %}
{% endfor -%}

bool operator==(const {{ message.name }} & lhs, const {{ message.name }}& rhs)
bool {{ message.name}}::is_equal(const ::umb::Message& msg) const
{
const auto& m = static_cast<const {{ message.name }}&>(msg);
return (
{% for field in message.fields %}
{% if field.type == "float" %}
::umb::internal::Float{lhs.{{ field.name }}()}.AlmostEquals(
::umb::internal::Float{rhs.{{ field.name }}()})
::umb::internal::Float{ {{ field.name }}() }.AlmostEquals(
::umb::internal::Float{m.{{ field.name }}()})
{% else %}
(lhs.{{ field.name }}() == rhs.{{ field.name }}())
({{ field.name }}() == m.{{ field.name }}())
{% endif %}
{% if not loop.is_last %} && {% endif %}
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions tests/test_randomized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ TEST_CASE("test TestMessages messages with randomized data")
CHECK(ok);
const auto bytes2 = m2->to_bytes();
CHECK_MESSAGE(bytes == bytes2, byte_cmp_msg(bytes, bytes2));
CHECK_EQ(*message, *m2);
// TODO: meta comparison functions?

const auto mt1 = message->type();
Expand Down

0 comments on commit ebf02af

Please sign in to comment.