From b3ae5dccfacadd99d3c7e226fc04f9da02cbd973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez?= Date: Mon, 29 Jul 2024 13:57:08 +0200 Subject: [PATCH] Refactor key hash calculation according to DDS X-Types 1.3 (#381) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refs #21385. New key hash calculation Signed-off-by: Ricardo González Moreno * Refs #21385. Fix Signed-off-by: Ricardo González Moreno * Refs #21385. Fix with inheritance Signed-off-by: Ricardo González Moreno * Refs #21385. Apply suggestion Signed-off-by: Ricardo González Moreno * Refs #21385. Update submodule Signed-off-by: Ricardo González Moreno * Refs #21385. Update submodule Signed-off-by: Ricardo González Moreno * Refs #21385. Fix tests Signed-off-by: Ricardo González Moreno --------- Signed-off-by: Ricardo González Moreno --- .../idl/templates/DDSPubSubTypeSource.stg | 3 +- .../templates/TypeObjectTestingTestSource.stg | 54 +++---------------- .../idl/templates/TypesCdrAuxHeaderImpl.stg | 49 +++++++++++++---- thirdparty/dds-types-test | 2 +- thirdparty/idl-parser | 2 +- 5 files changed, 51 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSource.stg b/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSource.stg index 4b81f7eb..6475e9ef 100644 --- a/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSource.stg +++ b/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSource.stg @@ -204,7 +204,8 @@ bool $struct.name$PubSubType::compute_key( $struct.cScopedname$_max_key_cdr_typesize); // Object that serializes the data. - eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS, eprosima::fastcdr::CdrVersion::XCDRv1); + eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS, eprosima::fastcdr::CdrVersion::XCDRv2); + ser.set_encoding_flag(eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR2); eprosima::fastcdr::serialize_key(ser, *p_type); if (force_md5 || $struct.cScopedname$_max_key_cdr_typesize > 16) { diff --git a/src/main/java/com/eprosima/fastdds/idl/templates/TypeObjectTestingTestSource.stg b/src/main/java/com/eprosima/fastdds/idl/templates/TypeObjectTestingTestSource.stg index 1df6c1a6..ea041708 100644 --- a/src/main/java/com/eprosima/fastdds/idl/templates/TypeObjectTestingTestSource.stg +++ b/src/main/java/com/eprosima/fastdds/idl/templates/TypeObjectTestingTestSource.stg @@ -176,20 +176,10 @@ TEST(TypeObjectTests, TestStructureTypeObject_$struct.cScopedname$) $if (struct.inheritance)$ member_id = $struct.firstMember.id$; $endif$ - $struct.members: { member | $check_struct_member(member=member, parent=struct)$}; separator="\n"$ + $struct.members: { member | $check_struct_member(member=member, parent=struct, pos=i0)$}; separator="\n"$ $endif$ ASSERT_EQ($struct.membersSize$, type_objects.minimal_type_object.minimal().struct_type().member_seq().size()); - for (size_t i = 1; i < type_objects.minimal_type_object.minimal().struct_type().member_seq().size(); ++i) - { - EXPECT_LT(type_objects.minimal_type_object.minimal().struct_type().member_seq()[i-1].common().member_id(), - type_objects.minimal_type_object.minimal().struct_type().member_seq()[i].common().member_id()); - } ASSERT_EQ($struct.membersSize$, type_objects.complete_type_object.complete().struct_type().member_seq().size()); - for (size_t i = 1; i < type_objects.complete_type_object.complete().struct_type().member_seq().size(); ++i) - { - EXPECT_LT(type_objects.complete_type_object.complete().struct_type().member_seq()[i-1].common().member_id(), - type_objects.complete_type_object.complete().struct_type().member_seq()[i].common().member_id()); - } } >> @@ -242,19 +232,9 @@ TEST(TypeObjectTests, TestUnionTypeObject_$union.name$) } $check_type_detail_annotations(object=union.discriminator, type="union_type().discriminator()")$ MemberId member_id = 1; - $union.members: { member | $check_union_member(member=member, parent=union)$}; separator="\n"$ + $union.members: { member | $check_union_member(member=member, parent=union, pos=i0)$}; separator="\n"$ ASSERT_EQ($union.membersSize$, type_objects.minimal_type_object.minimal().union_type().member_seq().size()); - for (size_t i = 1; i < type_objects.minimal_type_object.minimal().union_type().member_seq().size(); ++i) - { - EXPECT_LT(type_objects.minimal_type_object.minimal().union_type().member_seq()[i-1].common().member_id(), - type_objects.minimal_type_object.minimal().union_type().member_seq()[i].common().member_id()); - } ASSERT_EQ($union.membersSize$, type_objects.complete_type_object.complete().union_type().member_seq().size()); - for (size_t i = 1; i < type_objects.complete_type_object.complete().union_type().member_seq().size(); ++i) - { - EXPECT_LT(type_objects.complete_type_object.complete().union_type().member_seq()[i-1].common().member_id(), - type_objects.complete_type_object.complete().union_type().member_seq()[i].common().member_id()); - } } >> @@ -803,19 +783,10 @@ $object.name$_expected_flags |= TypeFlagBits::IS_AUTOID_HASH; $endif$ >> -check_struct_member(member, parent) ::= << +check_struct_member(member, parent, pos) ::= << { - size_t pos = 0; - bool found = false; - for (; pos < type_objects.complete_type_object.complete().struct_type().member_seq().size(); ++pos) - { - if (strcmp("$member.name$", type_objects.complete_type_object.complete().struct_type().member_seq()[pos].detail().name()) == 0) - { - found = true; - break; - } - } - ASSERT_TRUE(found); + size_t pos = $pos$; + ASSERT_EQ(0, strcmp("$member.name$", type_objects.complete_type_object.complete().struct_type().member_seq()[pos].detail().name())); NameHash member_name_hashed = TypeObjectUtils::name_hash("$member.name$"); $if(member.annotationId)$ member_id = $member.annotationIdValue$; @@ -862,19 +833,10 @@ check_struct_member(member, parent) ::= << } >> -check_union_member(member, parent) ::= << +check_union_member(member, parent, pos) ::= << { - size_t pos = 0; - bool found = false; - for (; pos < type_objects.complete_type_object.complete().union_type().member_seq().size(); ++pos) - { - if (strcmp("$member.name$", type_objects.complete_type_object.complete().union_type().member_seq()[pos].detail().name()) == 0) - { - found = true; - break; - } - } - ASSERT_TRUE(found); + size_t pos = $pos$; + ASSERT_EQ(0, strcmp("$member.name$", type_objects.complete_type_object.complete().union_type().member_seq()[pos].detail().name())); NameHash member_name_hashed = TypeObjectUtils::name_hash("$member.name$"); $if(member.annotationId)$ member_id = $member.annotationIdValue$; diff --git a/src/main/java/com/eprosima/fastdds/idl/templates/TypesCdrAuxHeaderImpl.stg b/src/main/java/com/eprosima/fastdds/idl/templates/TypesCdrAuxHeaderImpl.stg index 3ae2ec4d..42086cd8 100644 --- a/src/main/java/com/eprosima/fastdds/idl/templates/TypesCdrAuxHeaderImpl.stg +++ b/src/main/java/com/eprosima/fastdds/idl/templates/TypesCdrAuxHeaderImpl.stg @@ -151,26 +151,55 @@ void serialize_key( eprosima::fastcdr::Cdr& scdr, const $struct.scopedname$& data) { + $if(!struct.inheritance)$ $if(!struct.scope.empty)$ using namespace $struct.scope$; $endif$ + $struct.membersById : { member | + $if(member.typecode.isStructType)$ + extern void serialize_key( + Cdr& scdr, + const $member.typecode.scopedname$& data); + $endif$ + }; separator="\n"$ static_cast(scdr); static_cast(data); $if(struct.hasKey)$ - $struct.members : { member | + $struct.membersById : { member | $if(member.annotationKey)$ - $if(member.typecode.isStructType)$ - $if(member.typecode.hasKey)$ - serialize_key(scdr, data.$member.name$()); - $else$ - scdr << data.$member.name$(); - $endif$ - $else$ - scdr << data.$member.name$(); - $endif$ + $if(member.typecode.isStructType)$ + serialize_key(scdr, data.$member.name$()); + $else$ + scdr << data.$member.name$(); + $endif$ $endif$ }; separator="\n"$ + $else$ + $struct.membersById : { member | + $if(member.annotationOptional)$ + if (data.$member.name$().has_value()) + { + $if(member.typecode.isStructType)$ + serialize_key(scdr, $if(member.annotationExternal)$*$endif$data.$member.name$().value()); + $else$ + scdr << data.$member.name$().value(); + $endif$ + \} + $else$ + $if(member.typecode.isStructType)$ + serialize_key(scdr, $if(member.annotationExternal)$*$endif$data.$member.name$()); + $else$ + scdr << data.$member.name$(); + $endif$ + $endif$ + }; separator="\n"$ + $endif$ + $else$ + extern void serialize_key( + Cdr& scdr, + const $struct.inheritance.scopedname$& data); + serialize_key(scdr, static_cast(data)); $endif$ } diff --git a/thirdparty/dds-types-test b/thirdparty/dds-types-test index bb807e1f..1c87a16d 160000 --- a/thirdparty/dds-types-test +++ b/thirdparty/dds-types-test @@ -1 +1 @@ -Subproject commit bb807e1fb804368ea237a67800fa92e23cf1f8b6 +Subproject commit 1c87a16d2be220a65474499743e3759ef553b030 diff --git a/thirdparty/idl-parser b/thirdparty/idl-parser index 5c830efd..0e8ce7ee 160000 --- a/thirdparty/idl-parser +++ b/thirdparty/idl-parser @@ -1 +1 @@ -Subproject commit 5c830efd144c63c60852d9a07aeee689b2bce218 +Subproject commit 0e8ce7ee38ffa9bca1faabf7ba866f7fcdd74c09