Skip to content

Commit

Permalink
Refs #20165: Fix for custom annotations name hash.
Browse files Browse the repository at this point in the history
Signed-off-by: adriancampo <adriancampo@eprosima.com>
  • Loading branch information
adriancampo committed Mar 4, 2024
1 parent bc1442c commit 09f6c70
Show file tree
Hide file tree
Showing 15 changed files with 2,476 additions and 1,794 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,10 @@ traits<DynamicTypeBuilder>::ref_type DynamicTypeBuilderFactoryImpl::create_annot
break;
}
member_descriptor->type(type);
member_descriptor->name(parameter.name());
// Name hashes are used because the full name is not present in AppliedAnnotationParameter
// and can not be used in apply_custom_annotations when adding the members
xtypes::NameHash parameter_name_hash = xtypes::TypeObjectUtils::name_hash(parameter.name().to_string());
member_descriptor->name(get_string_from_name_hash(parameter_name_hash));
member_descriptor->default_value(get_annotation_parameter_value(parameter.default_value()));
if (RETCODE_OK != ret_val->add_member(member_descriptor))
{
Expand Down Expand Up @@ -1413,7 +1416,15 @@ bool DynamicTypeBuilderFactoryImpl::apply_custom_annotations(
break;
}
traits<AnnotationDescriptor>::ref_type annotation_descriptor {traits<AnnotationDescriptor>::make_shared()};
annotation_descriptor->type(create_type_w_type_object(annotation_type_object)->build());
auto type = create_type_w_type_object(annotation_type_object);
if (!type)
{
EPROSIMA_LOG_ERROR(DYN_TYPES, "Type could not be created from TypeObject");
ret_val.reset();
break;
}

annotation_descriptor->type(type->build());
if (annotation.param_seq().has_value())
{
for (const xtypes::AppliedAnnotationParameter& parameter : annotation.param_seq().value())
Expand All @@ -1422,6 +1433,7 @@ bool DynamicTypeBuilderFactoryImpl::apply_custom_annotations(
get_annotation_parameter_value(parameter.value()));
}
}

if (member_id != MEMBER_ID_INVALID &&
ret_val && RETCODE_OK != ret_val->apply_annotation_to_member(member_id, annotation_descriptor))
{
Expand Down
124 changes: 124 additions & 0 deletions test/dds-types-test/annotations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,130 @@ class EmptyAnnotatedStruct
private:


};

/*!
* @brief This class represents the structure BasicAnnotationsStruct defined by the user in the IDL file.
* @ingroup annotations
*/
class BasicAnnotationsStruct
{
public:

/*!
* @brief Default constructor.
*/
eProsima_user_DllExport BasicAnnotationsStruct()
{
}

/*!
* @brief Default destructor.
*/
eProsima_user_DllExport ~BasicAnnotationsStruct()
{
}

/*!
* @brief Copy constructor.
* @param x Reference to the object BasicAnnotationsStruct that will be copied.
*/
eProsima_user_DllExport BasicAnnotationsStruct(
const BasicAnnotationsStruct& x)
{
m_basic_annotations_member = x.m_basic_annotations_member;

}

/*!
* @brief Move constructor.
* @param x Reference to the object BasicAnnotationsStruct that will be copied.
*/
eProsima_user_DllExport BasicAnnotationsStruct(
BasicAnnotationsStruct&& x) noexcept
{
m_basic_annotations_member = x.m_basic_annotations_member;
}

/*!
* @brief Copy assignment.
* @param x Reference to the object BasicAnnotationsStruct that will be copied.
*/
eProsima_user_DllExport BasicAnnotationsStruct& operator =(
const BasicAnnotationsStruct& x)
{

m_basic_annotations_member = x.m_basic_annotations_member;

return *this;
}

/*!
* @brief Move assignment.
* @param x Reference to the object BasicAnnotationsStruct that will be copied.
*/
eProsima_user_DllExport BasicAnnotationsStruct& operator =(
BasicAnnotationsStruct&& x) noexcept
{

m_basic_annotations_member = x.m_basic_annotations_member;
return *this;
}

/*!
* @brief Comparison operator.
* @param x BasicAnnotationsStruct object to compare.
*/
eProsima_user_DllExport bool operator ==(
const BasicAnnotationsStruct& x) const
{
return (m_basic_annotations_member == x.m_basic_annotations_member);
}

/*!
* @brief Comparison operator.
* @param x BasicAnnotationsStruct object to compare.
*/
eProsima_user_DllExport bool operator !=(
const BasicAnnotationsStruct& x) const
{
return !(*this == x);
}

/*!
* @brief This function sets a value in member basic_annotations_member
* @param _basic_annotations_member New value for member basic_annotations_member
*/
eProsima_user_DllExport void basic_annotations_member(
int16_t _basic_annotations_member)
{
m_basic_annotations_member = _basic_annotations_member;
}

/*!
* @brief This function returns the value of member basic_annotations_member
* @return Value of member basic_annotations_member
*/
eProsima_user_DllExport int16_t basic_annotations_member() const
{
return m_basic_annotations_member;
}

/*!
* @brief This function returns a reference to member basic_annotations_member
* @return Reference to member basic_annotations_member
*/
eProsima_user_DllExport int16_t& basic_annotations_member()
{
return m_basic_annotations_member;
}



private:

int16_t m_basic_annotations_member{0};

};

#endif // _FAST_DDS_GENERATED_ANNOTATIONS_HPP_
Expand Down
8 changes: 8 additions & 0 deletions test/dds-types-test/annotationsCdrAux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ constexpr uint32_t EmptyAnnotatedStruct_max_cdr_typesize {4UL};
constexpr uint32_t EmptyAnnotatedStruct_max_key_cdr_typesize {0UL};


constexpr uint32_t BasicAnnotationsStruct_max_cdr_typesize {6UL};
constexpr uint32_t BasicAnnotationsStruct_max_key_cdr_typesize {0UL};




Expand All @@ -61,6 +64,11 @@ eProsima_user_DllExport void serialize_key(
const EmptyAnnotatedStruct& data);


eProsima_user_DllExport void serialize_key(
eprosima::fastcdr::Cdr& scdr,
const BasicAnnotationsStruct& data);


} // namespace fastcdr
} // namespace eprosima

Expand Down
77 changes: 77 additions & 0 deletions test/dds-types-test/annotationsCdrAux.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,83 @@ void serialize_key(



template<>
eProsima_user_DllExport size_t calculate_serialized_size(
eprosima::fastcdr::CdrSizeCalculator& calculator,
const BasicAnnotationsStruct& data,
size_t& current_alignment)
{
static_cast<void>(data);

eprosima::fastcdr::EncodingAlgorithmFlag previous_encoding = calculator.get_encoding();
size_t calculated_size {calculator.begin_calculate_type_serialized_size(
eprosima::fastcdr::CdrVersion::XCDRv2 == calculator.get_cdr_version() ?
eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2 :
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR,
current_alignment)};


calculated_size += calculator.calculate_member_serialized_size(eprosima::fastcdr::MemberId(0),
data.basic_annotations_member(), current_alignment);


calculated_size += calculator.end_calculate_type_serialized_size(previous_encoding, current_alignment);

return calculated_size;
}

template<>
eProsima_user_DllExport void serialize(
eprosima::fastcdr::Cdr& scdr,
const BasicAnnotationsStruct& data)
{
eprosima::fastcdr::Cdr::state current_state(scdr);
scdr.begin_serialize_type(current_state,
eprosima::fastcdr::CdrVersion::XCDRv2 == scdr.get_cdr_version() ?
eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2 :
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR);

scdr
<< eprosima::fastcdr::MemberId(0) << data.basic_annotations_member()
;
scdr.end_serialize_type(current_state);
}

template<>
eProsima_user_DllExport void deserialize(
eprosima::fastcdr::Cdr& cdr,
BasicAnnotationsStruct& data)
{
cdr.deserialize_type(eprosima::fastcdr::CdrVersion::XCDRv2 == cdr.get_cdr_version() ?
eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2 :
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR,
[&data](eprosima::fastcdr::Cdr& dcdr, const eprosima::fastcdr::MemberId& mid) -> bool
{
bool ret_value = true;
switch (mid.id)
{
case 0:
dcdr >> data.basic_annotations_member();
break;

default:
ret_value = false;
break;
}
return ret_value;
});
}

void serialize_key(
eprosima::fastcdr::Cdr& scdr,
const BasicAnnotationsStruct& data)
{
static_cast<void>(scdr);
static_cast<void>(data);
}



} // namespace fastcdr
} // namespace eprosima

Expand Down
Loading

0 comments on commit 09f6c70

Please sign in to comment.