Skip to content

Commit

Permalink
Refs #19760. Coexist fastcdr v1 and v2
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
  • Loading branch information
richiware committed Nov 2, 2023
1 parent 7df0be7 commit 6ad37f5
Show file tree
Hide file tree
Showing 5 changed files with 581 additions and 50 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/eprosima/fastcdr/idl/templates/TypesHeader.stg
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ $fileHeader(ctx=ctx, file=[ctx.filename, ".h"], description=["This header file c
#include <string>
#include <vector>

#include <fastcdr/config.h>
#if FASTCDR_VERSION_MAJOR == 1
#include <fastdds/rtps/common/CdrSerialization.hpp>
#else
#include <fastcdr/cdr/fixed_size_string.hpp>
#include <fastcdr/xcdr/optional.hpp>
#endif // FASTCDR_VERSION_MAJOR == 1

$if(ctx.printexception)$
#include <$ctx.product$/exceptions/UserException.h>
Expand Down Expand Up @@ -225,6 +230,7 @@ public:
const $struct.name$& x) const;

$struct.members:{ member | $public_member_declaration(member)$}; separator="\n"$

$serialization_functions(struct)$
$extensions : { extension | $extension$}; separator="\n"$
private:
Expand Down Expand Up @@ -635,6 +641,23 @@ $endif$
%>

serialization_functions(type) ::= <<
$if(ctx.cdr)$
/*!
* @brief This function serializes an object using CDR serialization.
* @param cdr CDR serialization object.
*/
eProsima_user_DllExport void serialize(
eprosima::fastcdr::Cdr& cdr) const;

/*!
* @brief This function deserializes an object using CDR serialization.
* @param cdr CDR serialization object.
*/
eProsima_user_DllExport void deserialize(
eprosima::fastcdr::Cdr& cdr);

$endif$

$if(ctx.fastcdr)$
/*!
* @brief This function serializes an object using FastCDR serialization.
Expand Down
64 changes: 51 additions & 13 deletions src/main/java/com/eprosima/fastcdr/idl/templates/TypesSource.stg
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,24 @@ $if(ctx.generateTypeObject)$
#include "$ctx.filename$TypeObject.h"
$endif$

$if(ctx.cdr)$
#include <fastcdr/Cdr.h>

$endif$
#include <fastdds/rtps/common/CdrSerialization.hpp>

$if(ctx.fastcdr)$
#include <fastcdr/FastCdr.h>
#include <utility>

$if(ctx.thereIsStructOrUnion)$
// Include auxiliary functions like for serializing/deserializing.
#include "$ctx.filename$CdrAux.ipp"
$endif$

#include <fastcdr/exceptions/BadParamException.h>
using namespace eprosima::fastcdr::exception;

#include <utility>

$extensions : { extension | $extension$}; separator="\n"$

$if(ctx.generateTypesC)$
$ctx.typeCodesToDefine : { type | $sequences_definition(type.value)$ }; separator="\n"$
$endif$

$definitions; separator="\n"$
$if(ctx.thereIsStructOrUnion)$
// Include auxiliary functions like for serializing/deserializing.
#include "$ctx.filename$CdrAux.ipp"
$endif$
>>

module(ctx, parent, module, definition_list) ::= <<
Expand Down Expand Up @@ -216,6 +208,21 @@ bool $struct.scopedname$::operator !=(
return !(*this == x);
}

$if(ctx.cdr)$
void $struct.scopedname$::serialize(
eprosima::fastcdr::Cdr& scdr) const
{
eprosima::fastcdr::serialize(scdr, *this);
}

void $struct.scopedname$::deserialize(
eprosima::fastcdr::Cdr& dcdr)
{
eprosima::fastcdr::deserialize(dcdr, *this);
}

$endif$

$if(ctx.fastcdr)$
void $struct.scopedname$::serialize(
eprosima::fastcdr::FastCdr &scdr) const
Expand Down Expand Up @@ -354,6 +361,21 @@ void $bitset.scopedname$::bitset(
m_bitset = std::bitset<$bitset.bitSize$>(str_value.substr(str_value.length() - base_diff, last_post));
}

$if(ctx.cdr)$
void $bitset.scopedname$::serialize(
eprosima::fastcdr::Cdr& scdr) const
{
eprosima::fastcdr::serialize(scdr, *this);
}

void $bitset.scopedname$::deserialize(
eprosima::fastcdr::Cdr& dcdr)
{
eprosima::fastcdr::deserialize(dcdr, *this);
}

$endif$

>>

public_bitfield_definition(class, member) ::= <<
Expand Down Expand Up @@ -517,6 +539,22 @@ $union.discriminator.cppTypename$& $union.scopedname$::_d()
}

$union.members:{ member | $public_unionmember_declaration(class=union.scopedname, member=member, defaultvalue=union.defaultvalue, totallabels=union.totallabels)$}; separator="\n"$

$if(ctx.cdr)$
void $union.scopedname$::serialize(
eprosima::fastcdr::Cdr& scdr) const
{
eprosima::fastcdr::serialize(scdr, *this);
}

void $union.scopedname$::deserialize(
eprosima::fastcdr::Cdr& dcdr)
{
eprosima::fastcdr::deserialize(dcdr, *this);
}

$endif$

$if(ctx.fastcdr)$
void $union.scopedname$::serialize(
eprosima::fastcdr::FastCdr& scdr) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import "eprosima.stg"
main(ctx, definitions) ::= <<
$fileHeader(ctx=ctx, file=[ctx.filename, "PubSubTypes.cpp"], description=["This header file contains the implementation of the serialization functions."])$


#include <fastcdr/FastBuffer.h>
#include <fastcdr/Cdr.h>
#include <fastcdr/CdrSizeCalculator.hpp>
#include <fastdds/rtps/common/CdrSerialization.hpp>

#include "$ctx.filename$PubSubTypes.h"
#include "$ctx.filename$CdrAux.hpp"
Expand Down Expand Up @@ -107,10 +104,12 @@ bool $if (parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::seri
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
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
{
Expand All @@ -125,7 +124,11 @@ bool $if (parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::seri
}

// 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
return true;
}

Expand All @@ -142,7 +145,11 @@ bool $if (parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::dese
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);
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
);

// Deserialize encapsulation.
deser.read_encapsulation();
Expand Down Expand Up @@ -203,12 +210,16 @@ bool $if (parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::getK
$struct.cScopedname$_max_key_cdr_typesize);

// Object that serializes the data.
eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS);
eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS, eprosima::fastcdr::CdrVersion::XCDRv1);
eprosima::fastcdr::serialize_key(ser, *p_type);
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<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();
for (uint8_t i = 0; i < 16; ++i)
{
Expand Down
Loading

0 comments on commit 6ad37f5

Please sign in to comment.