Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[21376] Refactor TopicDataType #97

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 62 additions & 75 deletions examples/HelloWorldExampleDS/HelloWorldPubSubTypes.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,42 @@ using DataRepresentationId_t = eprosima::fastdds::dds::DataRepresentationId_t;

HelloWorldPubSubType::HelloWorldPubSubType()
{
setName("HelloWorld");
uint32_t type_size =
#if FASTCDR_VERSION_MAJOR == 1
static_cast<uint32_t>(HelloWorld::getMaxCdrSerializedSize());
#else
HelloWorld_max_cdr_typesize;
#endif
set_name("HelloWorld");
uint32_t type_size = HelloWorld_max_cdr_typesize;
type_size += static_cast<uint32_t>(eprosima::fastcdr::Cdr::alignment(type_size, 4)); /* possible submessage alignment */
m_typeSize = type_size + 4; /*encapsulation*/
m_isGetKeyDefined = false;
uint32_t keyLength = HelloWorld_max_key_cdr_typesize > 16 ? HelloWorld_max_key_cdr_typesize : 16;
m_keyBuffer = reinterpret_cast<unsigned char*>(malloc(keyLength));
memset(m_keyBuffer, 0, keyLength);
max_serialized_type_size = type_size + 4; /*encapsulation*/
is_compute_key_provided = false;
uint32_t key_length = HelloWorld_max_key_cdr_typesize > 16 ? HelloWorld_max_key_cdr_typesize : 16;
key_buffer_ = reinterpret_cast<unsigned char*>(malloc(key_length));
memset(key_buffer_, 0, key_length);
}

HelloWorldPubSubType::~HelloWorldPubSubType()
{
if (m_keyBuffer != nullptr)
if (key_buffer_ != nullptr)
{
free(m_keyBuffer);
free(key_buffer_);
}
}

bool HelloWorldPubSubType::serialize(
const void* const data,
SerializedPayload_t* payload,
SerializedPayload_t& payload,
DataRepresentationId_t data_representation)
{
const HelloWorld* p_type = static_cast<const HelloWorld*>(data);

// Object that manages the raw buffer.
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size);
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(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 ?
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR :
eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2);
#endif // FASTCDR_VERSION_MAJOR > 1

try
{
Expand All @@ -88,16 +81,12 @@ bool HelloWorldPubSubType::serialize(
}

// Get the serialized length
#if FASTCDR_VERSION_MAJOR == 1
payload->length = static_cast<uint32_t>(ser.getSerializedDataLength());
#else
payload->length = static_cast<uint32_t>(ser.get_serialized_data_length());
#endif // FASTCDR_VERSION_MAJOR == 1
payload.length = static_cast<uint32_t>(ser.get_serialized_data_length());
return true;
}

bool HelloWorldPubSubType::deserialize(
SerializedPayload_t* payload,
SerializedPayload_t& payload,
void* data)
{
try
Expand All @@ -106,18 +95,14 @@ bool HelloWorldPubSubType::deserialize(
HelloWorld* p_type = static_cast<HelloWorld*>(data);

// Object that manages the raw buffer.
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->length);
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(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;
Expand All @@ -130,88 +115,90 @@ bool HelloWorldPubSubType::deserialize(
return true;
}

std::function<uint32_t()> HelloWorldPubSubType::getSerializedSizeProvider(
uint32_t HelloWorldPubSubType::calculate_serialized_size(
const void* const data,
DataRepresentationId_t data_representation)
{
return [data, data_representation]() -> uint32_t
{
#if FASTCDR_VERSION_MAJOR == 1
static_cast<void>(data_representation);
return static_cast<uint32_t>(type::getCdrSerializedSize(*static_cast<HelloWorld*>(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<uint32_t>(calculator.calculate_serialized_size(
*static_cast<const HelloWorld*>(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<uint32_t>(calculator.calculate_serialized_size(
*static_cast<const HelloWorld*>(data), current_alignment)) +
4u /*encapsulation*/;
}
catch (eprosima::fastcdr::exception::Exception& /*exception*/)
{
return 0;
}
}

void* HelloWorldPubSubType::createData()
void* HelloWorldPubSubType::create_data()
{
return reinterpret_cast<void*>(new HelloWorld());
}

void HelloWorldPubSubType::deleteData(
void HelloWorldPubSubType::delete_data(
void* data)
{
delete(reinterpret_cast<HelloWorld*>(data));
}

bool HelloWorldPubSubType::getKey(
bool HelloWorldPubSubType::compute_key(
SerializedPayload_t& payload,
InstanceHandle_t& handle,
bool force_md5)
{
if (!is_compute_key_provided)
{
return false;
}

HelloWorld data;
if (deserialize(payload, static_cast<void*>(&data)))
{
return compute_key(static_cast<void*>(&data), handle, force_md5);
}

return false;
}

bool HelloWorldPubSubType::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;
}

const HelloWorld* p_type = static_cast<const HelloWorld*>(data);

// Object that manages the raw buffer.
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(m_keyBuffer),
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(key_buffer_),
HelloWorld_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 || HelloWorld_max_key_cdr_typesize > 16)
{
m_md5.init();
#if FASTCDR_VERSION_MAJOR == 1
m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.getSerializedDataLength()));
#else
m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.get_serialized_data_length()));
#endif // FASTCDR_VERSION_MAJOR == 1
m_md5.finalize();
md5_.init();
md5_.update(key_buffer_, static_cast<unsigned int>(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;
Expand Down
46 changes: 18 additions & 28 deletions examples/HelloWorldExampleDS/HelloWorldPubSubTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
#include "HelloWorld.hpp"


#if !defined(GEN_API_VER) || (GEN_API_VER != 2)
#if !defined(FASTDDS_GEN_API_VER) || (FASTDDS_GEN_API_VER != 3)
#error \
Generated HelloWorld is not compatible with current installed Fast DDS. Please, regenerate it with fastddsgen.
#endif // GEN_API_VER
#endif // FASTDDS_GEN_API_VER


/*!
Expand All @@ -54,38 +54,30 @@ class HelloWorldPubSubType : public eprosima::fastdds::dds::TopicDataType

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<uint32_t()> getSerializedSizeProvider(
const void* const data) override
{
return getSerializedSizeProvider(data, eprosima::fastdds::dds::DEFAULT_DATA_REPRESENTATION);
}

eProsima_user_DllExport std::function<uint32_t()> 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
Expand All @@ -100,10 +92,6 @@ class HelloWorldPubSubType : public eprosima::fastdds::dds::TopicDataType
#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 false;
}

eProsima_user_DllExport inline bool is_plain(
eprosima::fastdds::dds::DataRepresentationId_t data_representation) const override
Expand All @@ -124,8 +112,10 @@ class HelloWorldPubSubType : public eprosima::fastdds::dds::TopicDataType

#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_;

};

Expand Down
Loading
Loading