Skip to content

Commit

Permalink
Simplify code in CDRMessage (#3855)
Browse files Browse the repository at this point in the history
* for test
Signed-off-by: Parsifal <51481718+paxifaer@users.noreply.github.com>

* 1。Removed the macro and replaced it with a template function addPrimitive to handle the addition of primitive types.
2.Each specific function (addOctet, addUInt16, etc.) now calls the addPrimitive function.

Signed-off-by: Parsifal <51481718+paxifaer@users.noreply.github.com>

* fix little bug.I put an extra A in a certain line

Signed-off-by: Parsifal <51481718+paxifaer@users.noreply.github.com>

* change the style

Signed-off-by: Parsifal <51481718+paxifaer@users.noreply.github.com>

* accept MiguelCompany comment

Signed-off-by: Parsifal <51481718+paxifaer@users.noreply.github.com>

---------

Signed-off-by: Parsifal <51481718+paxifaer@users.noreply.github.com>
  • Loading branch information
paxifaer authored Nov 8, 2023
1 parent e318c5b commit 7b6b362
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 134 deletions.
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

0 comments on commit 7b6b362

Please sign in to comment.