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

Simplify code in CDRMessage (backport #3855) #3997

Merged
merged 1 commit into from
Dec 12, 2023
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
15 changes: 15 additions & 0 deletions include/fastdds/rtps/messages/CDRMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ inline bool appendMsg(
*/
/// @{

template<typename T>
inline bool addPrimitive(
CDRMessage_t* msg,
T value);

inline bool hasSpace(
CDRMessage_t* msg,
const uint32_t length);

inline void copyToBuffer(
CDRMessage_t* msg,
const octet* data,
const uint32_t length,
bool reverse = false);

inline bool addData(
CDRMessage_t*,
const octet*,
Expand Down
184 changes: 50 additions & 134 deletions include/fastdds/rtps/messages/CDRMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,29 +470,44 @@ inline bool CDRMessage::readString(
return valid;
}

inline bool CDRMessage::addData(
inline bool CDRMessage::hasSpace(
CDRMessage_t* msg,
const octet* data,
const uint32_t length)
{
if (msg == nullptr)
return msg && (msg->pos + length <= msg->max_size);
}

inline void CDRMessage::copyToBuffer(
CDRMessage_t* msg,
const octet* data,
const uint32_t length,
bool reverse)
{
if (reverse)
{
return false;
for (uint32_t i = 0; i < length; i++)
{
msg->buffer[msg->pos + i] = *(data + length - 1 - i);
}
}
if (msg->pos + length > msg->max_size)
else
{
return false;
memcpy(&msg->buffer[msg->pos], data, length);
}
if (length > 0)
msg->pos += length;
msg->length += length;
}

inline bool CDRMessage::addData(
CDRMessage_t* msg,
const octet* data,
const uint32_t length)
{
if (!hasSpace(msg, length) || (length > 0 && !data))
{
if (data == nullptr)
{
return false;
}
memcpy(&msg->buffer[msg->pos], data, length);
msg->pos += length;
msg->length += length;
return false;
}
copyToBuffer(msg, data, length);
return true;
}

Expand All @@ -501,168 +516,69 @@ inline bool CDRMessage::addDataReversed(
const octet* data,
const uint32_t length)
{
if (msg->pos + length > msg->max_size)
if (!hasSpace(msg, length))
{
return false;
}
for (uint32_t i = 0; i < length; i++)
{
msg->buffer[msg->pos + i] = *(data + length - 1 - i);
}
msg->pos += length;
msg->length += length;
copyToBuffer(msg, data, length, true);
return true;
}

inline bool CDRMessage::addOctet(
template<typename T>
inline bool CDRMessage::addPrimitive(
CDRMessage_t* msg,
octet O)
T value)
{
if (msg->pos + 1 > msg->max_size)
const uint32_t size = sizeof(T);
if (!hasSpace(msg, size))
{
return false;
}
//const void* d = (void*)&O;
msg->buffer[msg->pos] = O;
msg->pos++;
msg->length++;
bool reverse = (msg->msg_endian != DEFAULT_ENDIAN);
copyToBuffer(msg, (octet*)&value, size, reverse);
return true;
}

inline bool CDRMessage::addOctet(
CDRMessage_t* msg,
octet O)
{
return addPrimitive(msg, O);
}

inline bool CDRMessage::addUInt16(
CDRMessage_t* msg,
uint16_t us)
{
if (msg->pos + 2 > msg->max_size)
{
return false;
}
octet* o = (octet*)&us;
if (msg->msg_endian == DEFAULT_ENDIAN)
{
msg->buffer[msg->pos] = *(o);
msg->buffer[msg->pos + 1] = *(o + 1);
}
else
{
msg->buffer[msg->pos] = *(o + 1);
msg->buffer[msg->pos + 1] = *(o);
}
msg->pos += 2;
msg->length += 2;
return true;
return addPrimitive(msg, us);
}

inline bool CDRMessage::addInt32(
CDRMessage_t* msg,
int32_t lo)
{
octet* o = (octet*)&lo;
if (msg->pos + 4 > msg->max_size)
{
return false;
}
if (msg->msg_endian == DEFAULT_ENDIAN)
{
for (uint8_t i = 0; i < 4; i++)
{
msg->buffer[msg->pos + i] = *(o + i);
}
}
else
{
for (uint8_t i = 0; i < 4; i++)
{
msg->buffer[msg->pos + i] = *(o + 3 - i);
}
}
msg->pos += 4;
msg->length += 4;
return true;
return addPrimitive(msg, lo);
}

inline bool CDRMessage::addUInt32(
CDRMessage_t* msg,
uint32_t ulo)
{
octet* o = (octet*)&ulo;
if (msg->pos + 4 > msg->max_size)
{
return false;
}
if (msg->msg_endian == DEFAULT_ENDIAN)
{
for (uint8_t i = 0; i < 4; i++)
{
msg->buffer[msg->pos + i] = *(o + i);
}
}
else
{
for (uint8_t i = 0; i < 4; i++)
{
msg->buffer[msg->pos + i] = *(o + 3 - i);
}
}
msg->pos += 4;
msg->length += 4;
return true;
return addPrimitive(msg, ulo);
}

inline bool CDRMessage::addInt64(
CDRMessage_t* msg,
int64_t lolo)
{
octet* o = (octet*)&lolo;
if (msg->pos + 8 > msg->max_size)
{
return false;
}
if (msg->msg_endian == DEFAULT_ENDIAN)
{
for (uint8_t i = 0; i < 8; i++)
{
msg->buffer[msg->pos + i] = *(o + i);
}
}
else
{
for (uint8_t i = 0; i < 8; i++)
{
msg->buffer[msg->pos + i] = *(o + 7 - i);
}
}
msg->pos += 8;
msg->length += 8;
return true;
return addPrimitive(msg, lolo);
}

inline bool CDRMessage::addUInt64(
CDRMessage_t* msg,
uint64_t ulolo)
{
octet* o = (octet*)&ulolo;
if (msg->pos + 8 > msg->max_size)
{
return false;
}
if (msg->msg_endian == DEFAULT_ENDIAN)
{
for (uint8_t i = 0; i < 8; i++)
{
msg->buffer[msg->pos + i] = *(o + i);
}
}
else
{
for (uint8_t i = 0; i < 8; i++)
{
msg->buffer[msg->pos + i] = *(o + 7 - i);
}
}
msg->pos += 8;
msg->length += 8;
return true;
return addPrimitive(msg, ulolo);
}

inline bool CDRMessage::addOctetVector(
Expand Down
Loading