Skip to content

Commit

Permalink
Bitsets according to standard IDL to C++11 2021 (#345)
Browse files Browse the repository at this point in the history
* Refs #20853. TypeObject retrives all bitfields

Signed-off-by: Ricardo González Moreno <[email protected]>

* Refs #21026. Generate bitsets according IDL to C++11 2021

Signed-off-by: Ricardo González Moreno <[email protected]>

* Refs #21026. Fix warning

Signed-off-by: Ricardo González Moreno <[email protected]>

---------

Signed-off-by: Ricardo González Moreno <[email protected]>
  • Loading branch information
richiware authored May 21, 2024
1 parent 2580c5b commit 5efa58d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ $if(member.annotationDefault)$
$elseif(!member.annotationOptional && !member.annotationExternal)$
$if(member.typecode.initialValue)$
{$member.typecode.initialValue$}
$elseif(member.typecode.isBitsetType)$
{}
$endif$
$endif$
%>
175 changes: 6 additions & 169 deletions src/main/java/com/eprosima/fastcdr/idl/templates/TypesHeader.stg
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ $fileHeader(ctx=ctx, file=[ctx.filename, ".hpp"], description=["This header file
$if(ctx.thereIsArray)$
#include <array>
$endif$
$if(ctx.thereIsBitset)$
#include <bitset>
$endif$
#include <cstdint>
$if(ctx.thereIsUnion)$
#include <functional>
Expand Down Expand Up @@ -528,83 +525,14 @@ private:

bitset_type(ctx, parent, bitset, extensions) ::= <<
/*!
* @brief This class represents the structure $bitset.name$ defined by the user in the IDL file.
* @brief This structure represents the bitset $bitset.name$ defined by the user in the IDL file.
* @ingroup $ctx.trimfilename$
*/
class $bitset.name$$if(bitset.inheritance)$ : public $public_bitset_inheritances(bitset.inheritance)$$endif$
struct $bitset.name$
{
public:

/*!
* @brief Default constructor.
*/
eProsima_user_DllExport $bitset.name$()
$if(bitset.inheritance)$
: $struct_inherit_default_init(bitset.inheritance)$
$endif$
{
}

/*!
* @brief Default destructor.
*/
eProsima_user_DllExport ~$bitset.name$()
{
}

/*!
* @brief Copy constructor.
* @param x Reference to the object $bitset.scopedname$ that will be copied.
*/
eProsima_user_DllExport $bitset.name$(
const $bitset.name$& x)
$if(bitset.inheritance)$
: $struct_inherit_copy_init(bitset.inheritance)$
$endif$
{
m_bitset = x.m_bitset;
}

/*!
* @brief Move constructor.
* @param x Reference to the object $bitset.scopedname$ that will be copied.
*/
eProsima_user_DllExport $bitset.name$(
$bitset.name$&& x) noexcept
$if(bitset.inheritance)$
: $struct_inherit_move_init(bitset.inheritance)$
$endif$
{
m_bitset = x.m_bitset;
}

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

m_bitset = x.m_bitset;

return *this;
}

/*!
* @brief Move assignment.
* @param x Reference to the object $bitset.scopedname$ that will be copied.
*/
eProsima_user_DllExport $bitset.name$& operator =(
$bitset.name$&& x) noexcept
{
$if(bitset.inheritance)$ $bitset.inheritance.scopedname$::operator =(std::move(x));$endif$

m_bitset = x.m_bitset;

return *this;
}
$bitset.bitfields:{ bitfield |
$bitfield.spec.cppTypename$ $bitfield.name$ : $bitfield.spec.bitSize$;
}; separator="\n"$

/*!
* @brief Comparison operator.
Expand All @@ -613,9 +541,7 @@ public:
eProsima_user_DllExport bool operator ==(
const $bitset.name$& x) const
{
$if(bitset.inheritance)$ if ($bitset.inheritance.scopedname$::operator !=(x)) return false;$endif$

return m_bitset == x.m_bitset;
return ($bitset.definedBitfields : { bitfield | $bitfield.name$ == x.$bitfield.name$}; separator=" &&\n "$);
}

/*!
Expand All @@ -627,47 +553,6 @@ public:
{
return !(*this == x);
}

$bitset.bitfields:{ bitset | $public_bitfield_declaration(bitset)$}; separator="\n"$

eProsima_user_DllExport std::bitset<$bitset.fullBitSize$> bitset() const
{
std::string str_value;

$if(bitset.inheritance)$
str_value = static_cast<const $bitset.inheritance.scopedname$*>(this)->bitset().to_string() + str_value;
$endif$

str_value = m_bitset.to_string() + str_value;

return std::bitset<$bitset.fullBitSize$>(str_value);
}

eProsima_user_DllExport void bitset(
const std::bitset<$bitset.fullBitSize$>& bitset)
{
std::string str_value {bitset.to_string()};
size_t base_diff {0};
size_t last_post {std::string::npos};

$if(bitset.inheritance)$
{
base_diff += $bitset.inheritance.fullBitSize$;
std::bitset<$bitset.inheritance.fullBitSize$> internal_bitset(str_value.substr(str_value.length() - base_diff, last_post));
static_cast<$bitset.inheritance.scopedname$*>(this)->bitset(internal_bitset);
last_post = base_diff;
}
$endif$

base_diff += $bitset.bitSize$;
m_bitset = std::bitset<$bitset.bitSize$>(str_value.substr(str_value.length() - base_diff, last_post));
}

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

private:

std::bitset<$bitset.bitSize$> m_bitset;
};
>>

Expand Down Expand Up @@ -703,8 +588,6 @@ class $type.name$;

public_struct_inheritances(parent) ::= <<$parent.scopedname$>>

public_bitset_inheritances(parent) ::= <<$parent.scopedname$>>

public_member_declaration(member) ::= <<
$if(member.annotationOptional || member.annotationExternal)$
$public_member_common_declaration(member=member)$
Expand Down Expand Up @@ -785,52 +668,6 @@ eProsima_user_DllExport $member_type_declaration(member)$& $member.name$()
}
>>

public_bitfield_declaration(member) ::= <<
$if(member.name)$
/*!
* @brief This function sets a value in member $member.name$
* @param _$member.name$ New value for member $member.name$
*/
eProsima_user_DllExport void $member.name$(
$member.spec.cppTypename$ _$member.name$)
{
int base = $member.basePosition$;
$if(member.spec.typecode.isType_7)$
m_bitset.set(base, _$member.name$);
$else$
int size = $member.spec.bitSize$;
for (int i = base; i < base + size; ++i)
{
m_bitset.set(i, !!(_$member.name$ & 0x01));
_$member.name$ = _$member.name$ \>> 1;
}
$endif$

}

/*!
* @brief This function returns the value of member $member.name$
* @return Value of member $member.name$
*/
eProsima_user_DllExport $member.spec.cppTypename$ $member.name$() const
{
int base = $member.basePosition$;
$if(member.spec.typecode.isType_7)$
return m_bitset.test(base);
$else$
int size = $member.spec.bitSize$;
std::bitset<$member.spec.bitSize$> aux;
for (int i = 0; i < size; ++i)
{
aux.set(i, m_bitset.test(i + base));
}
return static_cast<$member.spec.cppTypename$>(aux.to_ullong());
$endif$

}
$endif$
>>

private_member_declaration(member) ::= <<
$member_type_declaration(member)$ m_$member.name$$member_default_init(member)$;
>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public long maxPlainTypeSerializedSize(
{
long initial_alignment = current_alignment;

int full_bit_size = getFullBitSize();
int full_bit_size = getBitSize();

if (9 > full_bit_size)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,26 +250,27 @@ void $if(bitset.hasScope)$$bitset.scope$::$endif$print$bitset.name$(
$bitset.name$* topic)
{
printf("$bitset.name$: { \n");
$bitset.allBitfields:{ bitfield |
$if(bitfield.name)$printf("$bitfield.name$: 0x%" PRIx64 "\n", (uint64_t)topic->$bitfield.name$());$endif$
}; separator="\n"$
$bitset.definedBitfields:{ bitfield | printf("$bitfield.name$: 0x%" PRIx64 "\n", (uint64_t)topic->$bitfield.name$); }; separator="\n"$
printf("} \n");
}

void $if(bitset.hasScope)$$bitset.scope$::$endif$initialize$bitset.name$(
$bitset.name$* topic)
{
$bitset.allBitfields:{ bitfield |
$if(bitfield.name)$$if(bitfield.spec.typecode.isType_7)$topic->$bitfield.name$(static_cast<$bitfield.spec.typecode.cppTypename$>(rand()%2==1));$elseif(bitfield.spec.typecode.primitive)$topic->$bitfield.name$(static_cast<$bitfield.spec.typecode.cppTypename$>(rand()));$endif$$endif$
$bitset.definedBitfields:{ bitfield |
$if(bitfield.spec.typecode.isType_7)$
topic->$bitfield.name$ = static_cast<$bitfield.spec.typecode.cppTypename$>(rand()%2==1);
$elseif(bitfield.spec.typecode.primitive)$
topic->$bitfield.name$ = static_cast<$bitfield.spec.typecode.cppTypename$>(rand());
$endif$
}; separator="\n"$
}

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

Expand Down Expand Up @@ -619,7 +620,7 @@ $endif$
$if(typecode.primitive)$
printf("$name$: 0x%" PRIx64 "\n", (uint64_t)topic->$name$()$if(optional)$.value()$endif$);
$elseif(typecode.isBitsetType)$
printf("$name$: %s\n", topic->$name$()$if(optional)$.value()$endif$.bitset().to_string().c_str());
printf("$name$: \n"); $if(typecode.hasScope)$$typecode.scope$::$endif$print$typecode.name$(&topic->$name$()$if(optional)$.value()$endif$);
$elseif(typecode.isType_d)$
$if(typecode.isWStringType)$
printf("$name$: %ls\n", topic->$name$()$if(optional)$.value()$endif$$if(external)$->$else$.$endif$c_str());
Expand Down Expand Up @@ -656,7 +657,7 @@ array_member_print(ctx, typecode, name, originName, loopvar) ::= <<
$if(typecode.primitive)$
printf("$name$: 0x%" PRIx64 "\n", (uint64_t)topic->$name$);
$elseif(typecode.isBitsetType)$
printf("$name$: %s\n", topic->$name$.bitset().to_string().c_str());
printf("$name$: \n"); $if(typecode.hasScope)$$typecode.scope$::$endif$print$typecode.name$(&topic->$name$);
$elseif(typecode.isType_d)$
$if(typecode.isWStringType)$
printf("$name$: %ls\n", topic->$name$.c_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ TEST(TypeObjectTests, TestStructureTypeObject_$struct.cScopedname$)
$if (!struct.scope.empty)$$struct.scope$::$endif$register_$struct.CScopedname$_type_identifier(type_id);
EXPECT_NE(TypeIdentifier(), type_id);

$check_struct_type(struct=struct)$
$check_struct_type(struct=struct)$
}
>>

Expand Down Expand Up @@ -658,7 +658,7 @@ TEST(TypeObjectTests, TestUnionTypeObject_$union.name$)
$if (!union.scope.empty)$$union.scope$::$endif$register_$union.CScopedname$_type_identifier(type_id);
EXPECT_NE(TypeIdentifier(), type_id);

$check_union_type(union=union)$
$check_union_type(union=union)$
}
>>

Expand Down Expand Up @@ -806,7 +806,7 @@ TEST(TypeObjectTests, TestEnumTypeObject_$enum.name$)
// Enum Types does not have a specific register function: register every type in IDL file.
register_$ctx.filename$_type_objects();

$check_enum_type(enum=enum)$
$check_enum_type(enum=enum)$
}
>>

Expand Down Expand Up @@ -878,7 +878,7 @@ TEST(TypeObjectTests, TestBitmaskTypeObject_$bitmask.name$)
// Bitmask Types does not have a specific register function: register every type in IDL file.
register_$ctx.filename$_type_objects();

$check_bitmask_type(bitmask=bitmask)$
$check_bitmask_type(bitmask=bitmask)$
}
>>

Expand Down Expand Up @@ -941,7 +941,7 @@ TEST(TypeObjectTests, TestBitsetTypeObject_$bitset.name$)
// Bitset Types does not have a specific register function: register every type in IDL file.
register_$ctx.filename$_type_objects();

$check_bitset_type(bitset=bitset)$
$check_bitset_type(bitset=bitset)$
}
>>

Expand All @@ -958,7 +958,7 @@ EXPECT_EQ(0u, type_objects.minimal_type_object.minimal().bitset_type().bitset_fl
EXPECT_EQ(0u, type_objects.complete_type_object.complete().bitset_type().bitset_flags());
$check_type_detail_annotations(object=bitset, type="bitset_type().header().detail()")$
EXPECT_EQ("$bitset.scopedname$", type_objects.complete_type_object.complete().bitset_type().header().detail().type_name().to_string());
$bitset.members: { member | $check_bitfield(bitfield=member, parent=bitset)$}; separator="\n"$
$bitset.definedBitfields: { bitfield | $check_bitfield(bitfield=bitfield, parent=bitset)$}; separator="\n"$
ASSERT_EQ($bitset.membersSize$, type_objects.minimal_type_object.minimal().bitset_type().field_seq().size());
for (size_t i = 1; i < type_objects.minimal_type_object.minimal().bitset_type().field_seq().size(); ++i)
{
Expand All @@ -975,7 +975,6 @@ for (size_t i = 1; i < type_objects.complete_type_object.complete().bitset_type(

check_bitfield(bitfield, parent) ::= <<
{
$if(bitfield.name)$
size_t pos = 0;
bool found = false;
for (; pos < type_objects.complete_type_object.complete().bitset_type().field_seq().size(); ++pos)
Expand All @@ -998,7 +997,6 @@ check_bitfield(bitfield, parent) ::= <<
EXPECT_EQ("$bitfield.name$", type_objects.complete_type_object.complete().bitset_type().field_seq()[pos].detail().name().to_string());
EXPECT_EQ(TypeObjectUtils::name_hash("$bitfield.name$"), type_objects.minimal_type_object.minimal().bitset_type().field_seq()[pos].name_hash());
$check_member_detail_annotations(member=bitfield, type="bitset_type().field_seq()[pos].detail()", parent=parent)$
$endif$
}
>>

Expand Down
Loading

0 comments on commit 5efa58d

Please sign in to comment.