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

[19236] Fix Bitmask code generation #205

Closed
wants to merge 6 commits into from
Closed
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
93 changes: 91 additions & 2 deletions src/main/java/com/eprosima/fastcdr/idl/templates/TypesHeader.stg
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,81 @@ bitmask_type(ctx, parent, bitmask) ::= <<
* @brief This class represents the bitmask $bitmask.name$ defined by the user in the IDL file.
* @ingroup $ctx.trimfilename$
*/
enum $bitmask.name$$bitmask.boundType$
class $bitmask.name$
{
$bitmask.members:{ member | $member.name$ = 0x01ull << $member.position$}; separator=",\n"$
public:

/*!
* @brief Default constructor.
*/
eProsima_user_DllExport $bitmask.name$();

/*!
* @brief Default destructor.
*/
eProsima_user_DllExport ~$bitmask.name$();

/*!
* @brief Copy constructor.
*
* @param x Reference to the object $bitmask.scopedname$ that will be copied.
*/
eProsima_user_DllExport $bitmask.name$(
const $bitmask.name$& x);

/*!
* @brief Move constructor.
*
* @param x Reference to the object $bitmask.scopedname$ that will be copied.
*/
eProsima_user_DllExport $bitmask.name$(
$bitmask.name$&& x) noexcept;

/*!
* @brief Copy assignment.
*
* @param x Reference to the object $bitmask.scopedname$ that will be copied.
*/
eProsima_user_DllExport $bitmask.name$& operator =(
const $bitmask.name$& x);

/*!
* @brief Move assignment.
*
* @param x Reference to the object $bitmask.scopedname$ that will be copied.
*/
eProsima_user_DllExport $bitmask.name$& operator =(
$bitmask.name$&& x) noexcept;

/*!
* @brief Comparison operator.
*
* @param x $bitmask.scopedname$ object to compare.
*/
eProsima_user_DllExport bool operator ==(
const $bitmask.name$& x) const;

/*!
* @brief Comparison operator.
*
* @param x $bitmask.scopedname$ object to compare.
*/
eProsima_user_DllExport bool operator !=(
const $bitmask.name$& x) const;

$bitmask.members:{ it | $public_bitflag_declaration(it)$}; separator="\n"$

eProsima_user_DllExport const std::bitset<$bitmask.bitBound$>& bitmask() const;

eProsima_user_DllExport std::bitset<$bitmask.bitBound$>& bitmask();

eProsima_user_DllExport void bitmask(const std::bitset<$bitmask.bitBound$>&);

$serialization_functions(bitmask)$

private:

std::bitset<$bitmask.bitBound$> m_bitmask;
};
>>

Expand Down Expand Up @@ -619,6 +691,23 @@ eProsima_user_DllExport const $member.typecode.cppTypename$& $member.name$() con
eProsima_user_DllExport $member.typecode.cppTypename$& $member.name$();
>>

public_bitflag_declaration(member) ::= <<
/*!
* @brief This function sets a value in flag $member.name$
*
* @param _$member.name$ New value for flag $member.name$
*/
eProsima_user_DllExport void $member.name$(
bool _$member.name$);

/*!
* @brief This function returns the value of flag $member.name$
*
* @return Value of flag $member.name$
*/
eProsima_user_DllExport bool $member.name$() const;
>>

member_default_init(member) ::= <%
$if(!member.typecode.forwarded)$
$if(member.annotationDefault)$
Expand Down
101 changes: 100 additions & 1 deletion src/main/java/com/eprosima/fastcdr/idl/templates/TypesSource.stg
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,106 @@ $endif$

enum_type(ctx, parent, enum) ::= <<>>

bitmask_type(ctx, parent, bitmask) ::= <<>>
bitmask_type(ctx, parent, bitmask) ::= <<
$bitmask.scopedname$::$bitmask.name$()
{
$if(ctx.generateTypeObject)$
// Just to register all known types
register$ctx.filename$Types();
$endif$
}

$bitmask.scopedname$::~$bitmask.name$()
{
}

$bitmask.scopedname$::$bitmask.name$(
const $bitmask.name$& x)
{
m_bitmask = x.m_bitmask;
}

$bitmask.scopedname$::$bitmask.name$(
$bitmask.name$&& x) noexcept
{
m_bitmask = x.m_bitmask;
}

$bitmask.scopedname$& $bitmask.scopedname$::operator =(
const $bitmask.name$& x)
{
m_bitmask = x.m_bitmask;

return *this;
}

$bitmask.scopedname$& $bitmask.scopedname$::operator =(
$bitmask.name$&& x) noexcept
{
m_bitmask = x.m_bitmask;

return *this;
}

bool $bitmask.scopedname$::operator ==(
const $bitmask.name$& x) const
{
return m_bitmask == x.m_bitmask;
}

bool $bitmask.scopedname$::operator !=(
const $bitmask.name$& x) const
{
return !(*this == x);
}

$if(ctx.fastcdr)$
void $bitmask.scopedname$::serialize(
eprosima::fastcdr::FastCdr& scdr) const
{
scdr << m_bitmask;
}

void $bitmask.scopedname$::deserialize(
eprosima::fastcdr::FastCdr& dcdr)
{
dcdr \>> m_bitmask;
}

$endif$


$bitmask.members:{ it | $public_bitmask_definition(it)$}; separator="\n"$

const std::bitset<$bitmask.bitBound$>& $bitmask.scopedname$::bitmask() const
{
return m_bitmask;
}

std::bitset<$bitmask.bitBound$>& $bitmask.scopedname$::bitmask()
{
return m_bitmask;
}

void $bitmask.scopedname$::bitmask(const std::bitset<$bitmask.bitBound$>& bitmask)
{
m_bitmask = bitmask;
}

>>

public_bitmask_definition(member) ::= <<
void $bitmask.scopedname$::$member.name$(
bool _$member.name$)
{
m_bitmask.set($member.position$, _$member.name$);
}

bool $bitmask.scopedname$::$member.name$() const
{
return m_bitmask.test($member.position$);
}
>>

/***** Utils *****/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,18 @@ enum_type(ctx, parent, enum) ::= <<>>

typedef_decl(ctx, parent, typedefs) ::= <<>>

bitmask_type(ctx, parent, bitmask) ::= <<>>
bitmask_type(ctx, parent, bitmask) ::= <<
void print$bitmask.name$(
$bitmask.name$* topic);

void initialize$bitmask.name$(
$bitmask.name$* topic);

int compare$bitmask.name$(
$bitmask.name$* a,
$bitmask.name$* b);

>>

bitset_type(ctx, parent, bitset) ::= <<
void print$bitset.name$(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,29 @@ int $if(union.hasScope)$$union.scope$::$endif$compare$union.name$(

enum_type(ctx, parent, enum) ::= <<>>

bitmask_type(ctx, parent, bitmask) ::= <<>>
bitmask_type(ctx, parent, bitmask) ::= <<
void $if(bitmask.hasScope)$$bitmask.scope$::$endif$print$bitmask.name$(
$bitmask.name$* topic)
{
printf("$bitmask.name$: { \n");
$bitmask.members:{ it | printf("$it.name$: %s\n", topic->$it.name$() ? "true" : "false");}; separator="\n"$
}

void $if(bitmask.hasScope)$$bitmask.scope$::$endif$initialize$bitmask.name$(
$bitmask.name$* topic)
{
$bitmask.members:{ it | topic->$it.name$(static_cast<bool>(rand()%2==1));}; separator="\n"$
}

int $if(bitmask.hasScope)$$bitmask.scope$::$endif$compare$bitmask.name$(
$bitmask.name$* topic_a,
$bitmask.name$* topic_b)
{
$bitmask.members:{ it | if(topic_a->$it.name$() != topic_b->$it.name$()) return 0;}; separator="\n"$
return 1;
}

>>

typedef_decl(ctx, parent, typedefs) ::= <<>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,35 @@ $endif$

enum_type(ctx, parent, enum) ::= <<>>

bitmask_type(ctx, parent, bitmask) ::= <<>>
bitmask_type(ctx, parent, bitmask) ::= <<
$if(ctx.anyCdr)$
template<>
eProsima_user_DllExport size_t calculate_serialized_size(
eprosima::fastcdr::CdrSizeCalculator& calculator,
const $bitmask.scopedname$& data,
size_t& current_alignment)
{
return calculator.calculate_serialized_size(data.bitmask(), current_alignment);
}
$endif$

$if(ctx.cdr)$
template<>
eProsima_user_DllExport void serialize(
eprosima::fastcdr::Cdr& scdr,
const $bitmask.scopedname$& data)
{
scdr << data.bitmask();
}

template<>
eProsima_user_DllExport void deserialize(
eprosima::fastcdr::Cdr& dcdr,
$bitmask.scopedname$& data)
{
std::bitset<$bitmask.bitBound$> bitmask;
dcdr \>> bitmask;
data.bitmask(bitmask);
}
$endif$
>>
Loading