diff --git a/libuavcan/dsdl_compiler/data_type_template.hpp b/libuavcan/dsdl_compiler/data_type_template.hpp index 69f9be1367..a0bf0c426c 100644 --- a/libuavcan/dsdl_compiler/data_type_template.hpp +++ b/libuavcan/dsdl_compiler/data_type_template.hpp @@ -105,6 +105,19 @@ struct ${t.cpp_type_name} #endif } + bool operator!=(const ${type_name}& rhs) const { return !operator==(rhs); } + bool operator==(const ${type_name}& rhs) const + { +% if fields: + return + % for idx,a in enumerate(fields): + ${a.name} == rhs.${a.name} ${'&&' if (idx + 1) < len(fields) else ';'} + % endfor +% else: + return true; +% endif + } + <%def name="generate_codec_calls_per_field(call_name, self_parameter_type)"> static int ${call_name}(${self_parameter_type} self, ::uavcan::ScalarCodec& codec, ::uavcan::TailArrayOptimizationMode tao_mode = ::uavcan::TailArrayOptEnabled) diff --git a/libuavcan/test/dsdl_test/dsdl_test.cpp b/libuavcan/test/dsdl_test/dsdl_test.cpp index 267469d342..6869878345 100644 --- a/libuavcan/test/dsdl_test/dsdl_test.cpp +++ b/libuavcan/test/dsdl_test/dsdl_test.cpp @@ -55,6 +55,25 @@ TEST(Dsdl, Signature) ASSERT_EQ(uavcan::DataTypeKindMessage, root_ns_a::NestedMessage::DataTypeKind); } + +TEST(Dsdl, Operators) +{ + { + root_ns_a::EmptyService::Request a, b; + ASSERT_TRUE(a == b); + ASSERT_FALSE(a != b); + } + { + root_ns_a::NestedMessage c, d; + ASSERT_TRUE(c == d); + ASSERT_FALSE(c != d); + + c.field = 1; + ASSERT_FALSE(c == d); + ASSERT_TRUE(c != d); + } +} + /* * This test assumes that it will be executed before other GDTR tests; otherwise it fails. * TODO: Probably it needs to be called directly from main() diff --git a/libuavcan/test/subscriber.cpp b/libuavcan/test/subscriber.cpp index 89dfe2147c..7d70836174 100644 --- a/libuavcan/test/subscriber.cpp +++ b/libuavcan/test/subscriber.cpp @@ -55,17 +55,6 @@ struct SubscriptionListener SimpleBinder bindSimple() { return SimpleBinder(this, &SelfType::receiveSimple); } }; -// TODO: add autogenerated comparison operators, then remove this -static bool operator==(const uavcan::mavlink::Message& a, const uavcan::mavlink::Message& b) -{ - return - a.seq == b.seq && - a.sysid == b.sysid && - a.compid == b.compid && - a.msgid == b.msgid && - a.payload == b.payload; -} - TEST(Subscriber, Basic) {