From dd8aa30535681b8de113599989756c38e203dd51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez?= Date: Thu, 18 Jul 2024 12:39:21 +0200 Subject: [PATCH] Refactor `TopicDataType` (#375) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refs #21349. Remove old functions Signed-off-by: Ricardo González Moreno * Refs #21349. Apply suggestions: Update TopicDataType Signed-off-by: Ricardo González Moreno * Refs #21349. Rename GEN_API_VER Signed-off-by: Ricardo González Moreno * Refs #21349. Apply suggestion of new compute_key Signed-off-by: Ricardo González Moreno * Refs #21349. Apply suggestions Signed-off-by: Ricardo González Moreno * Refs #21349. Making private two members Signed-off-by: Ricardo González Moreno * Refs #21340. Fix tests Signed-off-by: Ricardo González Moreno --------- Signed-off-by: Ricardo González Moreno --- .../idl/templates/DDSPubSubTypeHeader.stg | 47 +++--- .../idl/templates/DDSPubSubTypeSource.stg | 137 ++++++++---------- .../templates/DDSPubSubTypeSwigInterface.stg | 2 +- .../idl/templates/SerializationTestSource.stg | 16 +- 4 files changed, 88 insertions(+), 114 deletions(-) diff --git a/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeHeader.stg b/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeHeader.stg index 4d2149cd..214aedb3 100644 --- a/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeHeader.stg +++ b/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeHeader.stg @@ -33,10 +33,10 @@ $fileHeader(ctx=ctx, file=[ctx.filename, "PubSubTypes.hpp"], description=["This $ctx.directIncludeDependencies : {include | #include "$include$PubSubTypes.hpp"}; separator="\n"$ -#if !defined(GEN_API_VER) || (GEN_API_VER != 2) +#if !defined(FASTDDS_GEN_API_VER) || (FASTDDS_GEN_API_VER != 3) #error \ Generated $ctx.filename$ is not compatible with current installed Fast DDS. Please, regenerate it with fastddsgen. -#endif // GEN_API_VER +#endif // FASTDDS_GEN_API_VER $definitions; separator="\n"$ @@ -113,38 +113,30 @@ public: eProsima_user_DllExport bool serialize( const void* const data, - eprosima::fastdds::rtps::SerializedPayload_t* payload) override - { - return serialize(data, payload, eprosima::fastdds::dds::DEFAULT_DATA_REPRESENTATION); - } - - eProsima_user_DllExport bool serialize( - const void* const data, - eprosima::fastdds::rtps::SerializedPayload_t* payload, + eprosima::fastdds::rtps::SerializedPayload_t& payload, eprosima::fastdds::dds::DataRepresentationId_t data_representation) override; eProsima_user_DllExport bool deserialize( - eprosima::fastdds::rtps::SerializedPayload_t* payload, + eprosima::fastdds::rtps::SerializedPayload_t& payload, void* data) override; - eProsima_user_DllExport std::function getSerializedSizeProvider( - const void* const data) override - { - return getSerializedSizeProvider(data, eprosima::fastdds::dds::DEFAULT_DATA_REPRESENTATION); - } - - eProsima_user_DllExport std::function getSerializedSizeProvider( + eProsima_user_DllExport uint32_t calculate_serialized_size( const void* const data, eprosima::fastdds::dds::DataRepresentationId_t data_representation) override; - eProsima_user_DllExport bool getKey( + eProsima_user_DllExport bool compute_key( + eprosima::fastdds::rtps::SerializedPayload_t& payload, + eprosima::fastdds::rtps::InstanceHandle_t& ihandle, + bool force_md5 = false) override; + + eProsima_user_DllExport bool compute_key( const void* const data, - eprosima::fastdds::rtps::InstanceHandle_t* ihandle, + eprosima::fastdds::rtps::InstanceHandle_t& ihandle, bool force_md5 = false) override; - eProsima_user_DllExport void* createData() override; + eProsima_user_DllExport void* create_data() override; - eProsima_user_DllExport void deleteData( + eProsima_user_DllExport void delete_data( void* data) override; //Register TypeObject representation in Fast DDS TypeObjectRegistry @@ -159,10 +151,6 @@ public: #endif // TOPIC_DATA_TYPE_API_HAS_IS_BOUNDED #ifdef TOPIC_DATA_TYPE_API_HAS_IS_PLAIN - eProsima_user_DllExport inline bool is_plain() const override - { - return $if(struct.isPlain)$is_plain_xcdrv1_impl()$else$false$endif$; - } eProsima_user_DllExport inline bool is_plain( eprosima::fastdds::dds::DataRepresentationId_t data_representation) const override @@ -194,11 +182,12 @@ public: #endif // TOPIC_DATA_TYPE_API_HAS_CONSTRUCT_SAMPLE - eprosima::fastdds::MD5 m_md5; - unsigned char* m_keyBuffer; +private: + + eprosima::fastdds::MD5 md5_; + unsigned char* key_buffer_; $if(struct.isPlain)$ -private: static constexpr bool is_plain_xcdrv1_impl() { 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 40509350..4b81f7eb 100644 --- a/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSource.stg +++ b/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSource.stg @@ -53,49 +53,42 @@ struct_type(ctx, parent, struct, member_list) ::= << $member_list$ $struct.name$PubSubType::$struct.name$PubSubType() { - $if(ctx.GenerateTypesROS2)$setName("$struct.ROS2Scopedname$");$else$setName("$struct.scopedname$");$endif$ - uint32_t type_size = -#if FASTCDR_VERSION_MAJOR == 1 - static_cast($struct.name$::getMaxCdrSerializedSize()); -#else - $struct.cScopedname$_max_cdr_typesize; -#endif + $if(ctx.GenerateTypesROS2)$set_name("$struct.ROS2Scopedname$");$else$set_name("$struct.scopedname$");$endif$ + uint32_t type_size = $struct.cScopedname$_max_cdr_typesize; type_size += static_cast(eprosima::fastcdr::Cdr::alignment(type_size, 4)); /* possible submessage alignment */ - m_typeSize = type_size + 4; /*encapsulation*/ - m_isGetKeyDefined = $if(struct.hasKey)$true$else$false$endif$; - uint32_t keyLength = $struct.cScopedname$_max_key_cdr_typesize > 16 ? $struct.cScopedname$_max_key_cdr_typesize : 16; - m_keyBuffer = reinterpret_cast(malloc(keyLength)); - memset(m_keyBuffer, 0, keyLength); + max_serialized_type_size = type_size + 4; /*encapsulation*/ + is_compute_key_provided = $if(struct.hasKey)$true$else$false$endif$; + uint32_t key_length = $struct.cScopedname$_max_key_cdr_typesize > 16 ? $struct.cScopedname$_max_key_cdr_typesize : 16; + key_buffer_ = reinterpret_cast(malloc(key_length)); + memset(key_buffer_, 0, key_length); } $struct.name$PubSubType::~$struct.name$PubSubType() { - if (m_keyBuffer != nullptr) + if (key_buffer_ != nullptr) { - free(m_keyBuffer); + free(key_buffer_); } } bool $struct.name$PubSubType::serialize( const void* const data, - SerializedPayload_t* payload, + SerializedPayload_t& payload, DataRepresentationId_t data_representation) { const $struct.name$* p_type = static_cast(data); // Object that manages the raw buffer. - eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast(payload->data), payload->max_size); + eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast(payload.data), payload.max_size); // Object that serializes the data. eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, data_representation == DataRepresentationId_t::XCDR_DATA_REPRESENTATION ? eprosima::fastcdr::CdrVersion::XCDRv1 : eprosima::fastcdr::CdrVersion::XCDRv2); - payload->encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE; -#if FASTCDR_VERSION_MAJOR > 1 + payload.encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE; ser.set_encoding_flag( data_representation == DataRepresentationId_t::XCDR_DATA_REPRESENTATION ? $if(struct.annotationFinal || struct.annotationAppendable)$eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR $elseif(struct.annotationMutable)$eprosima::fastcdr::EncodingAlgorithmFlag::PL_CDR$endif$ : $if(struct.annotationFinal)$eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR2$elseif(struct.annotationAppendable)$eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2$elseif(struct.annotationMutable)$eprosima::fastcdr::EncodingAlgorithmFlag::PL_CDR2$endif$); -#endif // FASTCDR_VERSION_MAJOR > 1 try { @@ -110,16 +103,12 @@ bool $struct.name$PubSubType::serialize( } // Get the serialized length -#if FASTCDR_VERSION_MAJOR == 1 - payload->length = static_cast(ser.getSerializedDataLength()); -#else - payload->length = static_cast(ser.get_serialized_data_length()); -#endif // FASTCDR_VERSION_MAJOR == 1 + payload.length = static_cast(ser.get_serialized_data_length()); return true; } bool $struct.name$PubSubType::deserialize( - SerializedPayload_t* payload, + SerializedPayload_t& payload, void* data) { try @@ -128,18 +117,14 @@ bool $struct.name$PubSubType::deserialize( $struct.name$* p_type = static_cast<$struct.name$*>(data); // Object that manages the raw buffer. - eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast(payload->data), payload->length); + eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast(payload.data), payload.length); // Object that deserializes the data. - eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN -#if FASTCDR_VERSION_MAJOR == 1 - , eprosima::fastcdr::Cdr::CdrType::DDS_CDR -#endif // FASTCDR_VERSION_MAJOR == 1 - ); + eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN); // Deserialize encapsulation. deser.read_encapsulation(); - payload->encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE; + payload.encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE; // Deserialize the object. deser \>> *p_type; @@ -152,52 +137,62 @@ bool $struct.name$PubSubType::deserialize( return true; } -std::function $struct.name$PubSubType::getSerializedSizeProvider( +uint32_t $struct.name$PubSubType::calculate_serialized_size( const void* const data, DataRepresentationId_t data_representation) { - return [data, data_representation]() -> uint32_t - { -#if FASTCDR_VERSION_MAJOR == 1 - static_cast(data_representation); - return static_cast(type::getCdrSerializedSize(*static_cast<$struct.name$*>(data))) + - 4u /*encapsulation*/; -#else - try - { - eprosima::fastcdr::CdrSizeCalculator calculator( - data_representation == DataRepresentationId_t::XCDR_DATA_REPRESENTATION ? - eprosima::fastcdr::CdrVersion::XCDRv1 :eprosima::fastcdr::CdrVersion::XCDRv2); - size_t current_alignment {0}; - return static_cast(calculator.calculate_serialized_size( - *static_cast(data), current_alignment)) + - 4u /*encapsulation*/; - } - catch (eprosima::fastcdr::exception::Exception& /*exception*/) - { - return 0; - } -#endif // FASTCDR_VERSION_MAJOR == 1 - }; + try + { + eprosima::fastcdr::CdrSizeCalculator calculator( + data_representation == DataRepresentationId_t::XCDR_DATA_REPRESENTATION ? + eprosima::fastcdr::CdrVersion::XCDRv1 :eprosima::fastcdr::CdrVersion::XCDRv2); + size_t current_alignment {0}; + return static_cast(calculator.calculate_serialized_size( + *static_cast(data), current_alignment)) + + 4u /*encapsulation*/; + } + catch (eprosima::fastcdr::exception::Exception& /*exception*/) + { + return 0; + } } -void* $struct.name$PubSubType::createData() +void* $struct.name$PubSubType::create_data() { return reinterpret_cast(new $struct.name$()); } -void $struct.name$PubSubType::deleteData( +void $struct.name$PubSubType::delete_data( void* data) { delete(reinterpret_cast<$struct.name$*>(data)); } -bool $struct.name$PubSubType::getKey( +bool $struct.name$PubSubType::compute_key( + SerializedPayload_t& payload, + InstanceHandle_t& handle, + bool force_md5) +{ + if (!is_compute_key_provided) + { + return false; + } + + $struct.name$ data; + if (deserialize(payload, static_cast(&data))) + { + return compute_key(static_cast(&data), handle, force_md5); + } + + return false; +} + +bool $struct.name$PubSubType::compute_key( const void* const data, - InstanceHandle_t* handle, + InstanceHandle_t& handle, bool force_md5) { - if (!m_isGetKeyDefined) + if (!is_compute_key_provided) { return false; } @@ -205,35 +200,27 @@ bool $struct.name$PubSubType::getKey( const $struct.name$* p_type = static_cast(data); // Object that manages the raw buffer. - eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast(m_keyBuffer), + eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast(key_buffer_), $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); -#if FASTCDR_VERSION_MAJOR == 1 - p_type->serializeKey(ser); -#else eprosima::fastcdr::serialize_key(ser, *p_type); -#endif // FASTCDR_VERSION_MAJOR == 1 if (force_md5 || $struct.cScopedname$_max_key_cdr_typesize > 16) { - m_md5.init(); -#if FASTCDR_VERSION_MAJOR == 1 - m_md5.update(m_keyBuffer, static_cast(ser.getSerializedDataLength())); -#else - m_md5.update(m_keyBuffer, static_cast(ser.get_serialized_data_length())); -#endif // FASTCDR_VERSION_MAJOR == 1 - m_md5.finalize(); + md5_.init(); + md5_.update(key_buffer_, static_cast(ser.get_serialized_data_length())); + md5_.finalize(); for (uint8_t i = 0; i < 16; ++i) { - handle->value[i] = m_md5.digest[i]; + handle.value[i] = md5_.digest[i]; } } else { for (uint8_t i = 0; i < 16; ++i) { - handle->value[i] = m_keyBuffer[i]; + handle.value[i] = key_buffer_[i]; } } return true; diff --git a/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSwigInterface.stg b/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSwigInterface.stg index c8c4c575..5762b96b 100644 --- a/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSwigInterface.stg +++ b/src/main/java/com/eprosima/fastdds/idl/templates/DDSPubSubTypeSwigInterface.stg @@ -25,7 +25,7 @@ $fileHeader(ctx=ctx, file=[ctx.filename, "PubSubTypes.i"], description=["This h #include "$ctx.filename$PubSubTypes.hpp" %} -#define GEN_API_VER 2 +#define FASTDDS_GEN_API_VER 3 %include "$ctx.filename$PubSubTypes.hpp" diff --git a/src/main/java/com/eprosima/fastdds/idl/templates/SerializationTestSource.stg b/src/main/java/com/eprosima/fastdds/idl/templates/SerializationTestSource.stg index 295e0e14..c951dcdc 100644 --- a/src/main/java/com/eprosima/fastdds/idl/templates/SerializationTestSource.stg +++ b/src/main/java/com/eprosima/fastdds/idl/templates/SerializationTestSource.stg @@ -64,30 +64,28 @@ TEST_P($struct.formatedCppTypename$Test, encoding) if (test_null_optional || !test_empty_external) { - uint32_t payloadSize = static_cast(pst.getSerializedSizeProvider(&$struct.name$_serialization_topic, - cdr_version)()); + uint32_t payloadSize = pst.calculate_serialized_size(&$struct.name$_serialization_topic, cdr_version); SerializedPayload_t payload(payloadSize); - ASSERT_TRUE(pst.serialize(&$struct.name$_serialization_topic, &payload, cdr_version)); + ASSERT_TRUE(pst.serialize(&$struct.name$_serialization_topic, payload, cdr_version)); - ASSERT_TRUE(pst.deserialize(&payload, &$struct.name$_deserialization_topic)); + ASSERT_TRUE(pst.deserialize(payload, &$struct.name$_deserialization_topic)); - uint32_t payloadOutSize = static_cast(pst.getSerializedSizeProvider(&$struct.name$_deserialization_topic, - cdr_version)()); + uint32_t payloadOutSize = pst.calculate_serialized_size(&$struct.name$_deserialization_topic, cdr_version); ASSERT_GT(compare$struct.name$(&$struct.name$_serialization_topic, &$struct.name$_deserialization_topic), 0); ASSERT_EQ(payloadOutSize, payloadSize); - ASSERT_GE($struct.name$_type_support.m_typeSize + 4, payloadSize); + ASSERT_GE($struct.name$_type_support.max_serialized_type_size + 4, payloadSize); ASSERT_EQ(payload.length, payloadSize); } else { - ASSERT_EQ(0, pst.getSerializedSizeProvider(&$struct.name$_serialization_topic, cdr_version)()); + ASSERT_EQ(0, pst.calculate_serialized_size(&$struct.name$_serialization_topic, cdr_version)); SerializedPayload_t payload(1000); - ASSERT_FALSE(pst.serialize(&$struct.name$_serialization_topic, &payload, cdr_version)); + ASSERT_FALSE(pst.serialize(&$struct.name$_serialization_topic, payload, cdr_version)); } $if((ctx.generateTypesC))$