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

[21374] Refactor TopicDataType #166

Merged
merged 3 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
20 changes: 10 additions & 10 deletions include/eprosimashapesdemo/shapesdemo/ShapeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,25 @@ class ColorInstanceHandle
{
Shape shape;
shape.define(SD_PURPLE);
m_topic.getKey((void*)&shape.m_shape, &PurpleIH.second);
m_topic.compute_key((void*)&shape.m_shape, PurpleIH.second);
shape.define(SD_BLUE);
m_topic.getKey((void*)&shape.m_shape, &BlueIH.second);
m_topic.compute_key((void*)&shape.m_shape, BlueIH.second);
shape.define(SD_RED);
m_topic.getKey((void*)&shape.m_shape, &RedIH.second);
m_topic.compute_key((void*)&shape.m_shape, RedIH.second);
shape.define(SD_GREEN);
m_topic.getKey((void*)&shape.m_shape, &GreenIH.second);
m_topic.compute_key((void*)&shape.m_shape, GreenIH.second);
shape.define(SD_YELLOW);
m_topic.getKey((void*)&shape.m_shape, &YellowIH.second);
m_topic.compute_key((void*)&shape.m_shape, YellowIH.second);
shape.define(SD_CYAN);
m_topic.getKey((void*)&shape.m_shape, &CyanIH.second);
m_topic.compute_key((void*)&shape.m_shape, CyanIH.second);
shape.define(SD_MAGENTA);
m_topic.getKey((void*)&shape.m_shape, &MagentaIH.second);
m_topic.compute_key((void*)&shape.m_shape, MagentaIH.second);
shape.define(SD_ORANGE);
m_topic.getKey((void*)&shape.m_shape, &OrangeIH.second);
m_topic.compute_key((void*)&shape.m_shape, OrangeIH.second);
shape.define(SD_GRAY);
m_topic.getKey((void*)&shape.m_shape, &GrayIH.second);
m_topic.compute_key((void*)&shape.m_shape, GrayIH.second);
shape.define(SD_BLACK);
m_topic.getKey((void*)&shape.m_shape, &BlackIH.second);
m_topic.compute_key((void*)&shape.m_shape, BlackIH.second);
// cout << PurpleIH.second << endl;
// cout << BlueIH.second << endl;
// cout << RedIH.second<<endl;
Expand Down
2 changes: 1 addition & 1 deletion src/shapesdemo/ShapePublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ShapePublisher::~ShapePublisher()
{
Duration_t wait_time(1, 0);
rtps::InstanceHandle_t handle;
if (mp_datawriter->get_type()->m_isGetKeyDefined)
if (mp_datawriter->get_type()->is_compute_key_provided)
{
mp_datawriter->dispose((void*)&this->m_shape.m_shape, handle);
}
Expand Down
137 changes: 62 additions & 75 deletions types/KeylessShapeTypePubSubTypes.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,42 @@ namespace shapes_demo_typesupport {
namespace idl {
KeylessShapeTypePubSubType::KeylessShapeTypePubSubType()
{
setName("shapes_demo_typesupport::idl::KeylessShapeType");
uint32_t type_size =
#if FASTCDR_VERSION_MAJOR == 1
static_cast<uint32_t>(KeylessShapeType::getMaxCdrSerializedSize());
#else
shapes_demo_typesupport_idl_KeylessShapeType_max_cdr_typesize;
#endif
set_name("shapes_demo_typesupport::idl::KeylessShapeType");
uint32_t type_size = shapes_demo_typesupport_idl_KeylessShapeType_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 = shapes_demo_typesupport_idl_KeylessShapeType_max_key_cdr_typesize > 16 ? shapes_demo_typesupport_idl_KeylessShapeType_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 = shapes_demo_typesupport_idl_KeylessShapeType_max_key_cdr_typesize > 16 ? shapes_demo_typesupport_idl_KeylessShapeType_max_key_cdr_typesize : 16;
key_buffer_ = reinterpret_cast<unsigned char*>(malloc(key_length));
memset(key_buffer_, 0, key_length);
}

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

bool KeylessShapeTypePubSubType::serialize(
const void* const data,
SerializedPayload_t* payload,
SerializedPayload_t& payload,
DataRepresentationId_t data_representation)
{
const KeylessShapeType* p_type = static_cast<const KeylessShapeType*>(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 @@ -92,16 +85,12 @@ namespace shapes_demo_typesupport {
}

// 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 KeylessShapeTypePubSubType::deserialize(
SerializedPayload_t* payload,
SerializedPayload_t& payload,
void* data)
{
try
Expand All @@ -110,18 +99,14 @@ namespace shapes_demo_typesupport {
KeylessShapeType* p_type = static_cast<KeylessShapeType*>(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 @@ -134,88 +119,90 @@ namespace shapes_demo_typesupport {
return true;
}

std::function<uint32_t()> KeylessShapeTypePubSubType::getSerializedSizeProvider(
uint32_t KeylessShapeTypePubSubType::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<KeylessShapeType*>(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 KeylessShapeType*>(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 KeylessShapeType*>(data), current_alignment)) +
4u /*encapsulation*/;
}
catch (eprosima::fastcdr::exception::Exception& /*exception*/)
{
return 0;
}
}

void* KeylessShapeTypePubSubType::createData()
void* KeylessShapeTypePubSubType::create_data()
{
return reinterpret_cast<void*>(new KeylessShapeType());
}

void KeylessShapeTypePubSubType::deleteData(
void KeylessShapeTypePubSubType::delete_data(
void* data)
{
delete(reinterpret_cast<KeylessShapeType*>(data));
}

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

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

return false;
}

bool KeylessShapeTypePubSubType::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 KeylessShapeType* p_type = static_cast<const KeylessShapeType*>(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_),
shapes_demo_typesupport_idl_KeylessShapeType_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 || shapes_demo_typesupport_idl_KeylessShapeType_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 types/KeylessShapeTypePubSubTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
#include "KeylessShapeType.hpp"


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

namespace shapes_demo_typesupport
{
Expand All @@ -58,38 +58,30 @@ namespace shapes_demo_typesupport

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 @@ -104,10 +96,6 @@ namespace shapes_demo_typesupport
#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 @@ -128,8 +116,10 @@ namespace shapes_demo_typesupport

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

};
} // namespace idl
Expand Down
Loading
Loading