From ef8919942e0cc425afca4040c262a51c028e7a78 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 10 Jul 2015 14:53:21 +0300 Subject: [PATCH] dsdlc: Union comparison operators --- .../data_type_template.tmpl | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/libuavcan/dsdl_compiler/libuavcan_dsdl_compiler/data_type_template.tmpl b/libuavcan/dsdl_compiler/libuavcan_dsdl_compiler/data_type_template.tmpl index c9963fbc98..93401a4ea7 100644 --- a/libuavcan/dsdl_compiler/libuavcan_dsdl_compiler/data_type_template.tmpl +++ b/libuavcan/dsdl_compiler/libuavcan_dsdl_compiler/data_type_template.tmpl @@ -181,33 +181,63 @@ private: /* * Out of line struct method definitions */ - #! scope_prefix, fields + #! scope_prefix, fields, union template bool ${scope_prefix}<_tmpl>::operator==(ParameterType rhs) const { - % if fields: - return + % if union: + if (_tag_ != rhs._tag_) + { + return false; + } % for idx,a in enumerate(fields): - ${a.name} == rhs.${a.name}${' &&' if (idx + 1) < len(fields) else ';'} + if (_tag_ == ${idx}) + { + return ${a.name} == rhs.${a.name}; + } % endfor + UAVCAN_ASSERT(0); // Invalid tag + return false; % else: + % if fields: + return + % for idx,a in enumerate(fields): + ${a.name} == rhs.${a.name}${' &&' if (idx + 1) < len(fields) else ';'} + % endfor + % else: (void)rhs; return true; + % endif % endif } template bool ${scope_prefix}<_tmpl>::isClose(ParameterType rhs) const { - % if fields: - return + % if union: + if (_tag_ != rhs._tag_) + { + return false; + } % for idx,a in enumerate(fields): - ::uavcan::areClose(${a.name}, rhs.${a.name})${' &&' if (idx + 1) < len(fields) else ';'} + if (_tag_ == ${idx}) + { + return ::uavcan::areClose(${a.name}, rhs.${a.name}); + } % endfor + UAVCAN_ASSERT(0); // Invalid tag + return false; % else: + % if fields: + return + % for idx,a in enumerate(fields): + ::uavcan::areClose(${a.name}, rhs.${a.name})${' &&' if (idx + 1) < len(fields) else ';'} + % endfor + % else: (void)rhs; return true; + % endif % endif } @@ -238,10 +268,10 @@ ${generate_codec_calls_per_field(call_name='decode', self_parameter_type='Refere % if t.kind == t.KIND_SERVICE: -${define_out_of_line_struct_methods(scope_prefix=t.cpp_type_name + '::Request_', fields=t.request_fields)} -${define_out_of_line_struct_methods(scope_prefix=t.cpp_type_name + '::Response_', fields=t.response_fields)} +${define_out_of_line_struct_methods(scope_prefix=t.cpp_type_name + '::Request_', fields=t.request_fields, union=t.request_union)} +${define_out_of_line_struct_methods(scope_prefix=t.cpp_type_name + '::Response_', fields=t.response_fields, union=t.response_union)} % else: -${define_out_of_line_struct_methods(scope_prefix=t.cpp_type_name, fields=t.fields)} +${define_out_of_line_struct_methods(scope_prefix=t.cpp_type_name, fields=t.fields, union=t.union)} % endif /*