From 05f7c16cffe95c38c9398c7da11b5c2f03ced67a Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 10 Jul 2015 17:20:53 +0300 Subject: [PATCH] dsdlc: union bit lengths --- .../data_type_template.tmpl | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 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 0e477c6bc4..ed690a098a 100644 --- a/libuavcan/dsdl_compiler/libuavcan_dsdl_compiler/data_type_template.tmpl +++ b/libuavcan/dsdl_compiler/libuavcan_dsdl_compiler/data_type_template.tmpl @@ -58,18 +58,45 @@ struct UAVCAN_EXPORT ${t.cpp_type_name} ${expand_attr_types(group_name='ConstantTypes', attrs=constants)} ${expand_attr_types(group_name='FieldTypes', attrs=fields)} - #! enum_name + % if union: + + typedef IntegerSpec< ::uavcan::IntegerBitLen< ${len(fields)} >::Result, + ::uavcan::SignednessUnsigned, ::uavcan::CastModeTruncate > TagType; + + #! enum_name, enum_comparator + enum + { + ${enum_name} = TagType::BitLen + + % for idx,a in enumerate(fields): + % if idx < (len(fields) - 1): + ::uavcan::${enum_comparator}::Result' * (len(fields) - 1)} + % endif + % endfor + }; + + + ${expand_enum_per_field(enum_name='MinBitLen', enum_comparator='EnumMin')} + ${expand_enum_per_field(enum_name='MaxBitLen', enum_comparator='EnumMax')} + + % else: + + #! enum_name enum { ${enum_name} - % for idx,a in enumerate(fields): + % for idx,a in enumerate(fields): ${'=' if idx == 0 else '+'} FieldTypes::${a.name}::${enum_name} - % endfor + % endfor }; - + + ${expand_enum_per_field(enum_name='MinBitLen')} ${expand_enum_per_field(enum_name='MaxBitLen')} + % endif + // Constants % for a in constants: static const typename ::uavcan::StorageType< typename ConstantTypes::${a.name} >::Type ${a.name}; // ${a.init_expression} @@ -81,11 +108,8 @@ struct UAVCAN_EXPORT ${t.cpp_type_name} % endfor % if union: -private: // Union tag definition - typedef IntegerSpec< ::uavcan::IntegerBitLen< ${len(fields)} >::Result, - ::uavcan::SignednessUnsigned, ::uavcan::CastModeTruncate > TagType; - - typename ::uavcan::StorageType< TagType >::Type _tag_; // The name is mangled to avoid clashing with fields +private: + typename ::uavcan::StorageType< TagType >::Type _tag_; // The name is mangled to avoid clashing with fields public: % endif