From e9586738d0c1a1abe71bfaa2d4ba2e9647f33abe Mon Sep 17 00:00:00 2001 From: Miguel Barro Date: Mon, 19 Jun 2023 17:38:06 +0200 Subject: [PATCH] Refs 17138. API update fixes .. part 3 Signed-off-by: Miguel Barro --- include/fastrtps/types/DynamicPubSubType.h | 8 + include/fastrtps/types/TypesBase.h | 3 - include/fastrtps/types/v1_3/DynamicType.hpp | 4 +- .../types/v1_3/DynamicTypeBuilder.hpp | 7 +- .../types/v1_3/DynamicTypeBuilderFactory.hpp | 13 +- .../types/v1_3/DynamicTypeBuilderPtr.hpp | 7 +- .../fastrtps/types/v1_3/DynamicTypePtr.hpp | 7 +- include/fastrtps/xmlparser/XMLParser.h | 11 +- .../fastrtps/xmlparser/XMLProfileManager.h | 2 +- .../dynamic-types/v1_3/DynamicPubSubType.cpp | 20 +- src/cpp/dynamic-types/v1_3/DynamicType.cpp | 32 +- .../dynamic-types/v1_3/DynamicTypeBuilder.cpp | 65 ++-- .../v1_3/DynamicTypeBuilderFactory.cpp | 8 +- src/cpp/rtps/xmlparser/XMLDynamicParser.cpp | 355 +++++++++--------- 14 files changed, 308 insertions(+), 234 deletions(-) diff --git a/include/fastrtps/types/DynamicPubSubType.h b/include/fastrtps/types/DynamicPubSubType.h index df8f166d308..cb6358807d2 100644 --- a/include/fastrtps/types/DynamicPubSubType.h +++ b/include/fastrtps/types/DynamicPubSubType.h @@ -45,6 +45,11 @@ class DynamicPubSubType RTPS_DllAPI DynamicPubSubType( v1_3::DynamicType_ptr pDynamicType); + RTPS_DllAPI DynamicPubSubType( + const v1_3::DynamicType& type) + : v1_3::DynamicPubSubType(type) + {} + FASTRTPS_DEPRECATED("Dynamic types version 1.1 will be disposed. Upgrade to version 1.3.") RTPS_DllAPI ReturnCode_t SetDynamicType( v1_1::DynamicType_ptr pType); @@ -52,6 +57,9 @@ class DynamicPubSubType RTPS_DllAPI ReturnCode_t SetDynamicType( v1_3::DynamicType_ptr pType); + RTPS_DllAPI ReturnCode_t SetDynamicType( + const v1_3::DynamicType& type); + RTPS_DllAPI void CleanDynamicType(); RTPS_DllAPI version GetDynamicTypeVersion() const diff --git a/include/fastrtps/types/TypesBase.h b/include/fastrtps/types/TypesBase.h index 84cc303bae4..7883f115694 100644 --- a/include/fastrtps/types/TypesBase.h +++ b/include/fastrtps/types/TypesBase.h @@ -926,9 +926,6 @@ namespace v1_3 { class DynamicType; class DynamicTypeBuilder; -template -std::function dynamic_object_deleter(const T*); - using namespace xtypes_names; constexpr uint32_t LENGTH_UNLIMITED = std::numeric_limits::max(); diff --git a/include/fastrtps/types/v1_3/DynamicType.hpp b/include/fastrtps/types/v1_3/DynamicType.hpp index b8cdd89d191..67a7224490b 100644 --- a/include/fastrtps/types/v1_3/DynamicType.hpp +++ b/include/fastrtps/types/v1_3/DynamicType.hpp @@ -47,7 +47,9 @@ class DynamicType final explicit use_the_create_method() = default; }; - friend std::function dynamic_object_deleter(const DynamicType*); + static void external_dynamic_object_deleter(const DynamicType*); + static void internal_dynamic_object_deleter(const DynamicType*); + friend void (*dynamic_object_deleter(const DynamicType* ))(const DynamicType*); public: diff --git a/include/fastrtps/types/v1_3/DynamicTypeBuilder.hpp b/include/fastrtps/types/v1_3/DynamicTypeBuilder.hpp index 07bb35da81e..f9b1fdb83d8 100644 --- a/include/fastrtps/types/v1_3/DynamicTypeBuilder.hpp +++ b/include/fastrtps/types/v1_3/DynamicTypeBuilder.hpp @@ -48,13 +48,18 @@ class DynamicTypeBuilder final static ReturnCode_t delete_type( const DynamicType* type) noexcept; + static const DynamicType* create_copy( + const DynamicType& type) noexcept; + // Only create objects from the associated factory struct use_the_create_method { explicit use_the_create_method() = default; }; - friend std::function dynamic_object_deleter(const DynamicTypeBuilder*); + static void external_dynamic_object_deleter(const DynamicTypeBuilder*); + static void internal_dynamic_object_deleter(const DynamicTypeBuilder*); + friend void (*dynamic_object_deleter(const DynamicTypeBuilder* ))(const DynamicTypeBuilder*); MemberId current_member_id_{0}; diff --git a/include/fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp b/include/fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp index 4da727e8e38..a1e392491fc 100644 --- a/include/fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp +++ b/include/fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp @@ -274,6 +274,17 @@ class DynamicTypeBuilderFactory final RTPS_DllAPI DynamicTypeBuilder* create_type_copy( const DynamicType& type) noexcept; + /** + * Create a new @ref DynamicType object based on the given @ref DynamicType object. + * @remark This method is thread-safe. + * @remark This method will always create a new object. In order to access primitive static allocated + * ones and avoid heap overhead use the `get_xxxx_type()` methods. + * @param[in] type @ref DynamicType object + * @return new @ref DynamicType object copy + */ + RTPS_DllAPI const DynamicType* create_copy( + const DynamicType& type) noexcept; + /** * Retrieve the cached @ref DynamicType object associated to a given primitive * @remark This method is thread-safe. @@ -470,7 +481,7 @@ class DynamicTypeBuilderFactory final * Creates a new @ref DynamicTypeBuilder object representing an enum * @return new @ref DynamicTypeBuilder object */ - RTPS_DllAPI const DynamicTypeBuilder* create_enum_type(); + RTPS_DllAPI DynamicTypeBuilder* create_enum_type(); /** * Returns a @ref DynamicTypeBuilder associated with a `TypeKind::TK_STRUCTURE` diff --git a/include/fastrtps/types/v1_3/DynamicTypeBuilderPtr.hpp b/include/fastrtps/types/v1_3/DynamicTypeBuilderPtr.hpp index 6679c0db496..91d5ba0399a 100644 --- a/include/fastrtps/types/v1_3/DynamicTypeBuilderPtr.hpp +++ b/include/fastrtps/types/v1_3/DynamicTypeBuilderPtr.hpp @@ -22,8 +22,7 @@ namespace fastrtps { namespace types { namespace v1_3 { -template<> -std::function dynamic_object_deleter(const DynamicTypeBuilder*); +RTPS_DllAPI void (*dynamic_object_deleter(const DynamicTypeBuilder* ))(const DynamicTypeBuilder*); } // namespace v1_3 } // namespace types @@ -57,7 +56,7 @@ class shared_ptr shared_ptr(const shared_ptr& r, element_type* ptr) noexcept : base(r, ptr) {} - template ::value, int> = 0> + template ::value, int>::type = 0> explicit shared_ptr(const weak_ptr& r) : base(r) {} @@ -121,7 +120,7 @@ class shared_ptr shared_ptr(const shared_ptr& r, element_type* ptr) noexcept : base(r, ptr) {} - template ::value, int> = 0> + template ::value, int>::type = 0> explicit shared_ptr(const weak_ptr& r) : base(r) {} diff --git a/include/fastrtps/types/v1_3/DynamicTypePtr.hpp b/include/fastrtps/types/v1_3/DynamicTypePtr.hpp index fe79ad0f2e4..de55a272f75 100644 --- a/include/fastrtps/types/v1_3/DynamicTypePtr.hpp +++ b/include/fastrtps/types/v1_3/DynamicTypePtr.hpp @@ -22,8 +22,7 @@ namespace fastrtps { namespace types { namespace v1_3 { -template<> -std::function dynamic_object_deleter(const DynamicType*); +RTPS_DllAPI void (*dynamic_object_deleter(const DynamicType* ))(const DynamicType*); } // namespace v1_3 } // namespace types @@ -57,7 +56,7 @@ class shared_ptr shared_ptr(const shared_ptr& r, element_type* ptr) noexcept : base(r, ptr) {} - template ::value, int> = 0> + template ::value, int>::type = 0> explicit shared_ptr(const weak_ptr& r) : base(r) {} @@ -121,7 +120,7 @@ class shared_ptr shared_ptr(const shared_ptr& r, element_type* ptr) noexcept : base(r, ptr) {} - template ::value, int> = 0> + template ::value, int>::type = 0> explicit shared_ptr(const weak_ptr& r) : base(r) {} diff --git a/include/fastrtps/xmlparser/XMLParser.h b/include/fastrtps/xmlparser/XMLParser.h index a87386be64a..91484f66f4f 100644 --- a/include/fastrtps/xmlparser/XMLParser.h +++ b/include/fastrtps/xmlparser/XMLParser.h @@ -263,23 +263,26 @@ class XMLParser RTPS_DllAPI static types::v1_3::DynamicTypeBuilder_cptr parseXMLBitfieldDynamicType( tinyxml2::XMLElement* p_root, - types::v1_3::DynamicTypeBuilder* p_dynamictype, + types::v1_3::DynamicTypeBuilder& builder, types::v1_3::MemberId mId, uint16_t& position); RTPS_DllAPI static XMLP_ret parseXMLBitvalueDynamicType( tinyxml2::XMLElement* p_root, - types::v1_3::DynamicTypeBuilder* p_dynamictype, + types::v1_3::DynamicTypeBuilder& builder, uint16_t& position); + RTPS_DllAPI static types::v1_3::DynamicTypeBuilder_cptr parseXMLMemberDynamicType( + tinyxml2::XMLElement* p_root); + RTPS_DllAPI static types::v1_3::DynamicTypeBuilder_cptr parseXMLMemberDynamicType( tinyxml2::XMLElement* p_root, - types::v1_3::DynamicTypeBuilder* p_dynamictype, + types::v1_3::DynamicTypeBuilder& p_dynamictype, types::v1_3::MemberId mId); RTPS_DllAPI static types::v1_3::DynamicTypeBuilder_cptr parseXMLMemberDynamicType( tinyxml2::XMLElement* p_root, - types::v1_3::DynamicTypeBuilder* p_dynamictype, + types::v1_3::DynamicTypeBuilder& p_dynamictype, types::v1_3::MemberId mId, const std::string& values); diff --git a/include/fastrtps/xmlparser/XMLProfileManager.h b/include/fastrtps/xmlparser/XMLProfileManager.h index 0a67d71c943..5212782076a 100644 --- a/include/fastrtps/xmlparser/XMLProfileManager.h +++ b/include/fastrtps/xmlparser/XMLProfileManager.h @@ -256,7 +256,7 @@ class XMLProfileManager { if (dynamic_types_.find(type_name) != dynamic_types_.end()) { - return new types::DynamicPubSubType(dynamic_types_[type_name]->build()); + return new types::DynamicPubSubType(*dynamic_types_[type_name]->build()); } return nullptr; } diff --git a/src/cpp/dynamic-types/v1_3/DynamicPubSubType.cpp b/src/cpp/dynamic-types/v1_3/DynamicPubSubType.cpp index bb7df632e05..c38f251483e 100644 --- a/src/cpp/dynamic-types/v1_3/DynamicPubSubType.cpp +++ b/src/cpp/dynamic-types/v1_3/DynamicPubSubType.cpp @@ -12,21 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + +#include +#include +#include +#include +#include #include #include +#include #include -#include -#include -#include -#include -#include -#include using namespace eprosima::fastrtps::types::v1_3; DynamicPubSubType::DynamicPubSubType( const DynamicType& type) - : dynamic_type_(&type) + : dynamic_type_(DynamicTypeBuilderFactory::get_instance().create_copy(type)) { UpdateDynamicTypeInfo(); } @@ -70,7 +72,7 @@ ReturnCode_t DynamicPubSubType::SetDynamicType( { if (!dynamic_type_) { - dynamic_type_.reset(&type); + dynamic_type_.reset(DynamicTypeBuilderFactory::get_instance().create_copy(type)); UpdateDynamicTypeInfo(); return ReturnCode_t::RETCODE_OK; } @@ -119,7 +121,7 @@ bool DynamicPubSubType::getKey( eprosima::fastrtps::rtps::InstanceHandle_t* handle, bool force_md5) { - if (dynamic_type_ == nullptr || !m_isGetKeyDefined) + if (!dynamic_type_ || !m_isGetKeyDefined) { return false; } diff --git a/src/cpp/dynamic-types/v1_3/DynamicType.cpp b/src/cpp/dynamic-types/v1_3/DynamicType.cpp index 7e5ee2a1b4d..6f372cb9d10 100644 --- a/src/cpp/dynamic-types/v1_3/DynamicType.cpp +++ b/src/cpp/dynamic-types/v1_3/DynamicType.cpp @@ -1894,25 +1894,35 @@ void DynamicType::serialize_empty_data( } } -std::function eprosima::fastrtps::types::v1_3::dynamic_object_deleter(const DynamicType* pDT) +void DynamicType::external_dynamic_object_deleter(const DynamicType* pDT) +{ + if (pDT) + { + pDT->release(); + } +} + +void DynamicType::internal_dynamic_object_deleter(const DynamicType* pDT) +{ + if (pDT) + { + std::default_delete del; + del(pDT); + } +} + +void (*eprosima::fastrtps::types::v1_3::dynamic_object_deleter( + const DynamicType* pDT))(const DynamicType*) { if ( pDT != nullptr) { if (pDT->use_count()) { - // This is an external object - return [](const DynamicType* pDT) - { - if (pDT) - { - const_cast(pDT)->release(); - } - }; + return DynamicType::external_dynamic_object_deleter; } else { - // This is an internal object - return std::default_delete(); + return DynamicType::internal_dynamic_object_deleter; } } diff --git a/src/cpp/dynamic-types/v1_3/DynamicTypeBuilder.cpp b/src/cpp/dynamic-types/v1_3/DynamicTypeBuilder.cpp index c0be9f37016..09a2c574bfe 100644 --- a/src/cpp/dynamic-types/v1_3/DynamicTypeBuilder.cpp +++ b/src/cpp/dynamic-types/v1_3/DynamicTypeBuilder.cpp @@ -309,40 +309,57 @@ bool DynamicTypeBuilder::equals( return *this == other; } -std::function eprosima::fastrtps::types::v1_3::dynamic_object_deleter( - const DynamicTypeBuilder* pDT) +ReturnCode_t DynamicTypeBuilder::delete_type( + const DynamicType* type) noexcept +{ + if (type != nullptr) + { + type->release(); + return ReturnCode_t::RETCODE_OK; + } + + return ReturnCode_t::RETCODE_PRECONDITION_NOT_MET; +} + +const DynamicType* DynamicTypeBuilder::create_copy( + const DynamicType& type) noexcept +{ + // increase external reference counting + type.add_ref(); + return &type; +} + +void DynamicTypeBuilder::external_dynamic_object_deleter(const DynamicTypeBuilder* pDT) +{ + if (pDT) + { + pDT->release(); + } +} + +void DynamicTypeBuilder::internal_dynamic_object_deleter(const DynamicTypeBuilder* pDT) +{ + if (pDT) + { + std::default_delete del; + del(pDT); + } +} + +void (*eprosima::fastrtps::types::v1_3::dynamic_object_deleter( + const DynamicTypeBuilder* pDT))(const DynamicTypeBuilder*) { if ( pDT != nullptr) { if (pDT->use_count()) { - // This is an external object - return [](const DynamicTypeBuilder* pDT) - { - if (pDT) - { - const_cast(pDT)->release(); - } - }; + return DynamicTypeBuilder::external_dynamic_object_deleter; } else { - // This is an internal object - return std::default_delete(); + return DynamicTypeBuilder::internal_dynamic_object_deleter; } } return nullptr; } - -ReturnCode_t DynamicTypeBuilder::delete_type( - const DynamicType* type) noexcept -{ - if (type != nullptr) - { - type->release(); - return ReturnCode_t::RETCODE_OK; - } - - return ReturnCode_t::RETCODE_PRECONDITION_NOT_MET; -} diff --git a/src/cpp/dynamic-types/v1_3/DynamicTypeBuilderFactory.cpp b/src/cpp/dynamic-types/v1_3/DynamicTypeBuilderFactory.cpp index d8edd8df3ef..bae1e77bbb6 100644 --- a/src/cpp/dynamic-types/v1_3/DynamicTypeBuilderFactory.cpp +++ b/src/cpp/dynamic-types/v1_3/DynamicTypeBuilderFactory.cpp @@ -399,6 +399,12 @@ DynamicTypeBuilder* DynamicTypeBuilderFactory::create_type( return {}; } +const DynamicType* DynamicTypeBuilderFactory::create_copy( + const DynamicType& type) noexcept +{ + return DynamicTypeBuilder::create_copy(type); +} + DynamicTypeBuilder* DynamicTypeBuilderFactory::create_type_copy( const DynamicType& type) noexcept { @@ -565,7 +571,7 @@ const DynamicType* DynamicTypeBuilderFactory::create_annotation_primitive( return DynamicTypeBuilder_ptr{create_type(descriptor)}->build(); } -const DynamicTypeBuilder* DynamicTypeBuilderFactory::create_enum_type() +DynamicTypeBuilder* DynamicTypeBuilderFactory::create_enum_type() { TypeDescriptor pEnumDescriptor; pEnumDescriptor.set_kind(TypeKind::TK_ENUM); diff --git a/src/cpp/rtps/xmlparser/XMLDynamicParser.cpp b/src/cpp/rtps/xmlparser/XMLDynamicParser.cpp index 83c7cfb4cb8..fb97e6aa73c 100644 --- a/src/cpp/rtps/xmlparser/XMLDynamicParser.cpp +++ b/src/cpp/rtps/xmlparser/XMLDynamicParser.cpp @@ -158,7 +158,7 @@ XMLP_ret XMLParser::parseXMLTypes( XMLP_ret XMLParser::parseXMLBitvalueDynamicType( tinyxml2::XMLElement* p_root, - types::v1_3::DynamicTypeBuilder* p_dynamictype, + types::v1_3::DynamicTypeBuilder& builder, uint16_t& field_position) { /* @@ -189,15 +189,15 @@ XMLP_ret XMLParser::parseXMLBitvalueDynamicType( } } - if (memberName == nullptr && p_dynamictype != nullptr) + if (memberName == nullptr) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing bit_value name: Not found."); return XMLP_ret::XML_ERROR; } - p_dynamictype->add_member(MemberId{field_position}, memberName); - //p_dynamictype->apply_annotation_to_member( - // p_dynamictype->get_member_id_by_name(memberName), ANNOTATION_POSITION_ID, "value", position); + builder.add_member(MemberId{field_position}, memberName); + //builder.apply_annotation_to_member( + // builder.get_member_id_by_name(memberName), ANNOTATION_POSITION_ID, "value", position); ++field_position; return XMLP_ret::XML_OK; @@ -216,66 +216,66 @@ static DynamicTypeBuilder_cptr getDiscriminatorTypeBuilder( DynamicTypeBuilderFactory& factory = DynamicTypeBuilderFactory::get_instance(); if (disc.compare(BOOLEAN) == 0) { - return factory.create_bool_type(); + return DynamicTypeBuilder_cptr{factory.create_bool_type()}; } else if (disc.compare(TBYTE) == 0 || disc.compare(OCTET) == 0 || disc.compare(INT8) == 0 || disc.compare(UINT8) == 0) { - return factory.create_byte_type(); + return DynamicTypeBuilder_cptr{factory.create_byte_type()}; } else if (disc.compare(SHORT) == 0) { - return factory.create_int16_type(); + return DynamicTypeBuilder_cptr{factory.create_int16_type()}; } else if (disc.compare(LONG) == 0) { - return factory.create_int32_type(); + return DynamicTypeBuilder_cptr{factory.create_int32_type()}; } else if (disc.compare(LONGLONG) == 0) { - return factory.create_int64_type(); + return DynamicTypeBuilder_cptr{factory.create_int64_type()}; } else if (disc.compare(USHORT) == 0) { - return factory.create_uint16_type(); + return DynamicTypeBuilder_cptr{factory.create_uint16_type()}; } else if (disc.compare(ULONG) == 0) { - return factory.create_uint32_type(); + return DynamicTypeBuilder_cptr{factory.create_uint32_type()}; } else if (disc.compare(ULONGLONG) == 0) { - return factory.create_uint64_type(); + return DynamicTypeBuilder_cptr{factory.create_uint64_type()}; } else if (disc.compare(FLOAT) == 0) { - return factory.create_float32_type(); + return DynamicTypeBuilder_cptr{factory.create_float32_type()}; } else if (disc.compare(DOUBLE) == 0) { - return factory.create_float64_type(); + return DynamicTypeBuilder_cptr{factory.create_float64_type()}; } else if (disc.compare(LONGDOUBLE) == 0) { - return factory.create_float128_type(); + return DynamicTypeBuilder_cptr{factory.create_float128_type()}; } else if (disc.compare(CHAR) == 0) { - return factory.create_char8_type(); + return DynamicTypeBuilder_cptr{factory.create_char8_type()}; } else if (disc.compare(WCHAR) == 0) { - return factory.create_char16_type(); + return DynamicTypeBuilder_cptr{factory.create_char16_type()}; } else if (disc.compare(STRING) == 0) { - return bound == 0 ? factory.create_string_type() : factory.create_string_type(bound); + return DynamicTypeBuilder_cptr{bound == 0 ? factory.create_string_type() : factory.create_string_type(bound)}; } else if (disc.compare(WSTRING) == 0) { - return bound == 0 ? factory.create_wstring_type() : factory.create_wstring_type(bound); + return DynamicTypeBuilder_cptr{bound == 0 ? factory.create_wstring_type() : factory.create_wstring_type(bound)}; } DynamicTypeBuilder_ptr ret; @@ -325,7 +325,7 @@ XMLP_ret XMLParser::parseXMLAliasDynamicType( (p_root->Attribute(SEQ_MAXLENGTH) != nullptr) || (p_root->Attribute(MAP_MAXLENGTH) != nullptr)) { - valueBuilder = parseXMLMemberDynamicType(p_root, nullptr, MEMBER_ID_INVALID); + valueBuilder = parseXMLMemberDynamicType(p_root); } else { @@ -343,8 +343,9 @@ XMLP_ret XMLParser::parseXMLAliasDynamicType( const char* name = p_root->Attribute(NAME); if (name != nullptr) { - DynamicTypeBuilder_ptr typeBuilder = - DynamicTypeBuilderFactory::get_instance().create_alias_type(*valueBuilder->build(), name); + DynamicType_ptr value_type{valueBuilder->build()}; + DynamicTypeBuilder_ptr typeBuilder{ + DynamicTypeBuilderFactory::get_instance().create_alias_type(*value_type, name)}; XMLProfileManager::insertDynamicTypeByName(name, std::move(typeBuilder)); } else @@ -403,11 +404,12 @@ XMLP_ret XMLParser::parseXMLBitsetDynamicType( const char* baseType = p_root->Attribute(BASE_TYPE); if (baseType != nullptr) { - types::v1_3::DynamicTypeBuilder_ptr parentType; + DynamicTypeBuilder_ptr parentType; XMLProfileManager::getDynamicTypeByName(parentType, baseType); if (parentType && parentType->get_kind() == TypeKind::TK_BITSET) { - typeBuilder = DynamicTypeBuilderFactory::get_instance().create_child_struct_type(*parentType->build()); + DynamicType_ptr parent_type{parentType->build()}; + typeBuilder.reset(DynamicTypeBuilderFactory::get_instance().create_child_struct_type(*parent_type)); } else { @@ -417,7 +419,7 @@ XMLP_ret XMLParser::parseXMLBitsetDynamicType( } else { - typeBuilder = DynamicTypeBuilderFactory::get_instance().create_bitset_type(); + typeBuilder.reset(DynamicTypeBuilderFactory::get_instance().create_bitset_type()); } typeBuilder->set_name(name); @@ -429,7 +431,7 @@ XMLP_ret XMLParser::parseXMLBitsetDynamicType( element_name = p_element->Name(); if (strcmp(element_name, BITFIELD) == 0) { - if (!parseXMLBitfieldDynamicType(p_element, typeBuilder.get(), mId++, position)) + if (!parseXMLBitfieldDynamicType(p_element, *typeBuilder, mId++, position)) { return XMLP_ret::XML_ERROR; } @@ -447,7 +449,7 @@ XMLP_ret XMLParser::parseXMLBitsetDynamicType( DynamicTypeBuilder_cptr XMLParser::parseXMLBitfieldDynamicType( tinyxml2::XMLElement* p_root, - types::v1_3::DynamicTypeBuilder* p_dynamictype, + types::v1_3::DynamicTypeBuilder& builder, MemberId mId, uint16_t& position) { @@ -461,17 +463,17 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLBitfieldDynamicType( if (p_root == nullptr) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing bitfield: Node not found."); - return nullptr; + return {}; } const char* memberType = p_root->Attribute(TYPE); const char* memberName = p_root->Attribute(NAME); const char* bit_bound = p_root->Attribute(BIT_BOUND); - if (bit_bound == nullptr && p_dynamictype != nullptr) + if (bit_bound == nullptr) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing bitfield bit_bound: Not found."); - return nullptr; + return {}; } if (memberName == nullptr) @@ -510,59 +512,59 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLBitfieldDynamicType( else { EPROSIMA_LOG_ERROR(XMLPARSER, "Failed creating bitfield, size too big: " << bit_bound); - return nullptr; + return {}; } } catch (...) { EPROSIMA_LOG_ERROR(XMLPARSER, "Failed creating bitfield, invalid bit_bound (must be an unsigned short): " << bit_bound); - return nullptr; + return {}; } } if (strncmp(memberType, BOOLEAN, 8) == 0) { - memberBuilder = factory.create_bool_type(); + memberBuilder.reset(factory.create_bool_type()); } else if (strncmp(memberType, CHAR, 5) == 0) { - memberBuilder = factory.create_char8_type(); + memberBuilder.reset(factory.create_char8_type()); } else if (strncmp(memberType, WCHAR, 6) == 0) { - memberBuilder = factory.create_char16_type(); + memberBuilder.reset(factory.create_char16_type()); } else if (strncmp(memberType, TBYTE, 6) == 0 || strncmp(memberType, OCTET, 6) == 0 || strncmp(memberType, UINT8, 6) == 0 || strncmp(memberType, INT8, 5) == 0) { - memberBuilder = factory.create_byte_type(); + memberBuilder.reset(factory.create_byte_type()); } else if (strncmp(memberType, SHORT, 6) == 0) { - memberBuilder = factory.create_int16_type(); + memberBuilder.reset(factory.create_int16_type()); } else if (strncmp(memberType, LONG, 5) == 0) { - memberBuilder = factory.create_int32_type(); + memberBuilder.reset(factory.create_int32_type()); } else if (strncmp(memberType, ULONG, 13) == 0) { - memberBuilder = factory.create_uint32_type(); + memberBuilder.reset(factory.create_uint32_type()); } else if (strncmp(memberType, USHORT, 14) == 0) { - memberBuilder = factory.create_uint16_type(); + memberBuilder.reset(factory.create_uint16_type()); } else if (strncmp(memberType, LONGLONG, 9) == 0) { - memberBuilder = factory.create_int64_type(); + memberBuilder.reset(factory.create_int64_type()); } else if (strncmp(memberType, ULONGLONG, 17) == 0) { - memberBuilder = factory.create_uint64_type(); + memberBuilder.reset(factory.create_uint64_type()); } else // Unsupported type? { @@ -570,24 +572,20 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLBitfieldDynamicType( "Failed creating bitfield " << memberName << ": Type " << memberType << " unsupported."); } - if (!memberBuilder) { EPROSIMA_LOG_ERROR(XMLPARSER, "Failed creating " << memberType << ": " << memberName); } - if (p_dynamictype != nullptr) + builder.add_member(mId, memberName, memberBuilder->build()); + if (!std::string(memberName).empty()) { - p_dynamictype->add_member(mId, memberName, memberBuilder->build()); - if (!std::string(memberName).empty()) - { - p_dynamictype->apply_annotation_to_member(mId, ANNOTATION_BIT_BOUND_ID, "value", bit_bound); - //position += static_cast(mId); - p_dynamictype->apply_annotation_to_member(mId, ANNOTATION_POSITION_ID, "value", - std::to_string(position)); - } - position += static_cast(atoi(bit_bound)); + builder.apply_annotation_to_member(mId, ANNOTATION_BIT_BOUND_ID, "value", bit_bound); + //position += static_cast(mId); + builder.apply_annotation_to_member(mId, ANNOTATION_POSITION_ID, "value", + std::to_string(position)); } + position += static_cast(atoi(bit_bound)); return memberBuilder; } @@ -624,8 +622,8 @@ XMLP_ret XMLParser::parseXMLBitmaskDynamicType( { return XMLP_ret::XML_ERROR; } - DynamicTypeBuilder_ptr typeBuilder = - DynamicTypeBuilderFactory::get_instance().create_bitmask_type(bit_bound); + DynamicTypeBuilder_ptr typeBuilder{ + DynamicTypeBuilderFactory::get_instance().create_bitmask_type(bit_bound)}; typeBuilder->set_name(name); uint16_t position = 0; @@ -636,7 +634,7 @@ XMLP_ret XMLParser::parseXMLBitmaskDynamicType( element_name = p_element->Name(); if (strcmp(element_name, BIT_VALUE) == 0) { - if (parseXMLBitvalueDynamicType(p_element, typeBuilder.get(), position) != XMLP_ret::XML_OK) + if (parseXMLBitvalueDynamicType(p_element, *typeBuilder, position) != XMLP_ret::XML_OK) { return XMLP_ret::XML_ERROR; } @@ -679,7 +677,7 @@ XMLP_ret XMLParser::parseXMLEnumDynamicType( return XMLP_ret::XML_ERROR; } - DynamicTypeBuilder_ptr typeBuilder = DynamicTypeBuilderFactory::get_instance().create_enum_type(); + DynamicTypeBuilder_ptr typeBuilder{DynamicTypeBuilderFactory::get_instance().create_enum_type()}; typeBuilder->set_name(enumName); uint32_t currValue = 0; @@ -728,11 +726,12 @@ XMLP_ret XMLParser::parseXMLStructDynamicType( const char* baseType = p_root->Attribute(BASE_TYPE); if (baseType != nullptr) { - types::v1_3::DynamicTypeBuilder_ptr parentType; + DynamicTypeBuilder_ptr parentType; XMLProfileManager::getDynamicTypeByName(parentType, baseType); if (parentType && parentType->get_kind() == TypeKind::TK_STRUCTURE) { - typeBuilder = DynamicTypeBuilderFactory::get_instance().create_child_struct_type(*parentType->build()); + DynamicType_ptr parent_type{parentType->build()}; + typeBuilder.reset(DynamicTypeBuilderFactory::get_instance().create_child_struct_type(*parent_type)); } else { @@ -742,7 +741,7 @@ XMLP_ret XMLParser::parseXMLStructDynamicType( } else { - typeBuilder = DynamicTypeBuilderFactory::get_instance().create_struct_type(); + typeBuilder.reset(DynamicTypeBuilderFactory::get_instance().create_struct_type()); } typeBuilder->set_name(name); @@ -753,7 +752,7 @@ XMLP_ret XMLParser::parseXMLStructDynamicType( element_name = p_element->Name(); if (strcmp(element_name, MEMBER) == 0) { - if (!parseXMLMemberDynamicType(p_element, typeBuilder.get(), mId++)) + if (!parseXMLMemberDynamicType(p_element, *typeBuilder, mId++)) { return XMLP_ret::XML_ERROR; } @@ -809,8 +808,8 @@ XMLP_ret XMLParser::parseXMLUnionDynamicType( } else { - DynamicTypeBuilder_ptr typeBuilder = DynamicTypeBuilderFactory::get_instance().create_union_type( - *discriminator->build()); + DynamicType_ptr disc_type{discriminator->build()}; + DynamicTypeBuilder_ptr typeBuilder{DynamicTypeBuilderFactory::get_instance().create_union_type(*disc_type)}; typeBuilder->set_name(name); MemberId mId{0}; @@ -845,7 +844,7 @@ XMLP_ret XMLParser::parseXMLUnionDynamicType( } if (caseElement != nullptr) { - if (!parseXMLMemberDynamicType(caseElement, typeBuilder.get(), mId++, valuesStr)) + if (!parseXMLMemberDynamicType(caseElement, *typeBuilder, mId++, valuesStr)) { return XMLP_ret::XML_ERROR; } @@ -909,18 +908,7 @@ static bool dimensionsToLabels( } DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( - tinyxml2::XMLElement* p_root, - types::v1_3::DynamicTypeBuilder* p_dynamictype, - MemberId mId) -{ - return parseXMLMemberDynamicType(p_root, p_dynamictype, mId, ""); -} - -DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( - tinyxml2::XMLElement* p_root, - types::v1_3::DynamicTypeBuilder* p_dynamictype, - MemberId mId, - const std::string& values) + tinyxml2::XMLElement* p_root) { /* @@ -938,23 +926,23 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( if (p_root == nullptr) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing member: Node not found."); - return nullptr; + return {}; } const char* memberType = p_root->Attribute(TYPE); const char* memberName = p_root->Attribute(NAME); bool isArray = false; - if (memberName == nullptr && p_dynamictype != nullptr) + if (memberName == nullptr) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing member name: Not found."); - return nullptr; + return {}; } if (memberType == nullptr) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing member type: Not found."); - return nullptr; + return {}; } const char* memberArray = p_root->Attribute(ARRAY_DIMENSIONS); @@ -973,7 +961,7 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( else { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing member type: Not found."); - return nullptr; + return {}; } } @@ -995,7 +983,7 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( if (!contentType) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing sequence element type: Cannot be recognized: " << memberType); - return nullptr; + return {}; } const char* lengthStr = p_root->Attribute(SEQ_MAXLENGTH); @@ -1009,20 +997,23 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( catch (const std::exception&) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing member sequence length in line " << p_root->GetLineNum()); - return nullptr; + return {}; } } if (!isArray) { - memberBuilder = factory.create_sequence_type(*contentType->build(), length); + DynamicType_ptr type{contentType->build()}; + memberBuilder.reset(factory.create_sequence_type(*type, length)); } else { - DynamicTypeBuilder_ptr innerBuilder = factory.create_sequence_type(*contentType->build(), length); + DynamicType_ptr type{contentType->build()}; + DynamicTypeBuilder_ptr innerBuilder{factory.create_sequence_type(*type, length)}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + DynamicType_ptr innertype{innerBuilder->build()}; + memberBuilder.reset(factory.create_array_type(*innertype, bounds)); } } else if (p_root->Attribute(MAP_MAXLENGTH) != nullptr) @@ -1055,13 +1046,13 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( if (!keyTypeBuilder) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing map's key element type: Cannot be recognized."); - return nullptr; + return {}; } } else { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing key_type element: Not found."); - return nullptr; + return {}; } // Parse value @@ -1072,13 +1063,13 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( if (!valueTypeBuilder) { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing map's value element type: Cannot be recognized."); - return nullptr; + return {}; } } else { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing value_value element: Not found."); - return nullptr; + return {}; } const char* lengthStr = p_root->Attribute(MAP_MAXLENGTH); @@ -1093,65 +1084,68 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( { EPROSIMA_LOG_ERROR(XMLPARSER, "Error parsing map member sequence length in line " << p_root->GetLineNum()); - return nullptr; + return {}; } } assert(keyTypeBuilder && valueTypeBuilder); + DynamicType_ptr key_type{keyTypeBuilder->build()}; + DynamicType_ptr value_type{valueTypeBuilder->build()}; + if (!isArray) { - memberBuilder = factory.create_map_type(*keyTypeBuilder->build(), *valueTypeBuilder->build(), length); + memberBuilder.reset(factory.create_map_type(*key_type, *value_type, length)); } else { - DynamicTypeBuilder_ptr innerBuilder = - factory.create_map_type(*keyTypeBuilder->build(), *valueTypeBuilder->build(), length); + DynamicTypeBuilder_ptr innerBuilder{factory.create_map_type(*key_type, *value_type, length)}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + DynamicType_ptr inner_type{innerBuilder->build()}; + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, BOOLEAN, 8) == 0) { if (!isArray) { - memberBuilder = factory.create_bool_type(); + memberBuilder.reset(factory.create_bool_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_bool_type(); + DynamicType_ptr inner_type { factory.get_bool_type() }; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, CHAR, 5) == 0) { if (!isArray) { - memberBuilder = factory.create_char8_type(); + memberBuilder.reset(factory.create_char8_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_char8_type(); + DynamicType_ptr inner_type { factory.get_char8_type() }; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, WCHAR, 6) == 0) { if (!isArray) { - memberBuilder = factory.create_char16_type(); + memberBuilder.reset(factory.create_char16_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_char16_type(); + DynamicType_ptr inner_type{factory.get_char16_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, TBYTE, 6) == 0 @@ -1161,145 +1155,145 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( { if (!isArray) { - memberBuilder = factory.create_byte_type(); + memberBuilder.reset(factory.create_byte_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_byte_type(); + DynamicType_ptr inner_type{factory.get_byte_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, SHORT, 6) == 0) { if (!isArray) { - memberBuilder = factory.create_int16_type(); + memberBuilder.reset(factory.create_int16_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_int16_type(); + DynamicType_ptr inner_type{factory.get_int16_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, LONG, 5) == 0) { if (!isArray) { - memberBuilder = factory.create_int32_type(); + memberBuilder.reset(factory.create_int32_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_int32_type(); + DynamicType_ptr inner_type{factory.get_int32_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, ULONG, 13) == 0) { if (!isArray) { - memberBuilder = factory.create_uint32_type(); + memberBuilder.reset(factory.create_uint32_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_uint32_type(); + DynamicType_ptr inner_type{factory.get_uint32_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, USHORT, 14) == 0) { if (!isArray) { - memberBuilder = factory.create_uint16_type(); + memberBuilder.reset(factory.create_uint16_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_uint16_type(); + DynamicType_ptr inner_type{factory.get_uint16_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, LONGLONG, 9) == 0) { if (!isArray) { - memberBuilder = factory.create_int64_type(); + memberBuilder.reset(factory.create_int64_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_int64_type(); + DynamicType_ptr inner_type{factory.get_int64_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, ULONGLONG, 17) == 0) { if (!isArray) { - memberBuilder = factory.create_uint64_type(); + memberBuilder.reset(factory.create_uint64_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_uint64_type(); + DynamicType_ptr inner_type{factory.get_uint64_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, FLOAT, 6) == 0) { if (!isArray) { - memberBuilder = factory.create_float32_type(); + memberBuilder.reset(factory.create_float32_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_float32_type(); + DynamicType_ptr inner_type{factory.get_float32_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, DOUBLE, 7) == 0) { if (!isArray) { - memberBuilder = factory.create_float64_type(); + memberBuilder.reset(factory.create_float64_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_float64_type(); + DynamicType_ptr inner_type{factory.get_float64_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, LONGDOUBLE, 11) == 0) { if (!isArray) { - memberBuilder = factory.create_float128_type(); + memberBuilder.reset(factory.create_float128_type()); } else { - DynamicTypeBuilder_cptr innerBuilder = factory.create_float128_type(); + DynamicType_ptr inner_type{factory.get_float128_type()}; std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*innerBuilder->build(), bounds); + memberBuilder.reset(factory.create_array_type(*inner_type, bounds)); } } else if (strncmp(memberType, STRING, 7) == 0) { - auto string_builder = factory.create_string_type(); + DynamicTypeBuilder_cptr string_builder{factory.create_string_type()}; const char* boundStr = p_root->Attribute(STR_MAXLENGTH); if (nullptr != boundStr) @@ -1307,7 +1301,7 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( uint32_t bound = static_cast(std::atoi(boundStr)); if ( bound > 0 ) { - string_builder = factory.create_string_type(bound); + string_builder.reset(factory.create_string_type(bound)); } } @@ -1317,14 +1311,15 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( } else { + DynamicType_ptr string_type {string_builder->build()}; std::vector boundsArray; dimensionsToArrayBounds(memberArray, boundsArray); - memberBuilder = factory.create_array_type(*string_builder->build(), boundsArray); + memberBuilder.reset(factory.create_array_type(*string_type, boundsArray)); } } else if (strncmp(memberType, WSTRING, 8) == 0) { - auto string_builder = factory.create_wstring_type(); + DynamicTypeBuilder_cptr string_builder {factory.create_wstring_type()}; const char* boundStr = p_root->Attribute(STR_MAXLENGTH); if (nullptr != boundStr) @@ -1332,7 +1327,7 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( uint32_t bound = static_cast(std::atoi(boundStr)); if ( bound > 0 ) { - string_builder = factory.create_wstring_type(bound); + string_builder.reset(factory.create_wstring_type(bound)); } } @@ -1342,9 +1337,10 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( } else { + DynamicType_ptr string_type {string_builder->build()}; std::vector boundsArray; dimensionsToArrayBounds(memberArray, boundsArray); - memberBuilder = factory.create_array_type(*string_builder->build(), boundsArray); + memberBuilder.reset(factory.create_array_type(*string_type, boundsArray)); } } else // Complex type? @@ -1353,17 +1349,17 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( XMLProfileManager::getDynamicTypeByName(typePtr, memberType); if (!isArray) { - memberBuilder = typePtr->shared_from_this(); + memberBuilder = typePtr; } else if (typePtr) { std::vector bounds; dimensionsToArrayBounds(memberArray, bounds); - memberBuilder = factory.create_array_type(*typePtr->build(), bounds); + DynamicType_ptr type {typePtr->build()}; + memberBuilder.reset(factory.create_array_type(*type, bounds)); } } - if (!memberBuilder) { if (!isArray) @@ -1375,49 +1371,68 @@ DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( EPROSIMA_LOG_ERROR(XMLPARSER, "Failed creating " << memberType << " array: " << (memberName ? memberName : "")); } - return nullptr; + return {}; } const char* memberTopicKey = p_root->Attribute(KEY); + if (memberTopicKey != nullptr) { if (strncmp(memberTopicKey, "true", 5) == 0) { - DynamicTypeBuilder_ptr builder; - // check if its primitive if (memberBuilder->is_primitive()) { // copy if modification is required - builder = factory.create_type(*memberBuilder); - } - else - { - // otherwise cast - builder = std::const_pointer_cast(memberBuilder); + DynamicTypeBuilder_ptr mutable_builder {factory.create_type(*memberBuilder)}; + mutable_builder->apply_annotation(types::ANNOTATION_KEY_ID, "value", "true"); + return mutable_builder; } + } + } - builder->apply_annotation(ANNOTATION_KEY_ID, "value", "true"); + return memberBuilder; +} - if (p_dynamictype != nullptr) - { - p_dynamictype->apply_annotation(ANNOTATION_KEY_ID, "value", "true"); - } - } +DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( + tinyxml2::XMLElement* p_root, + types::v1_3::DynamicTypeBuilder& builder, + MemberId mId) +{ + return parseXMLMemberDynamicType(p_root, builder, mId, ""); +} + +DynamicTypeBuilder_cptr XMLParser::parseXMLMemberDynamicType( + tinyxml2::XMLElement* p_root, + types::v1_3::DynamicTypeBuilder& builder, + MemberId mId, + const std::string& values) +{ + DynamicTypeBuilder_cptr memberBuilder = parseXMLMemberDynamicType(p_root); + + if(!memberBuilder) + { + return {}; + } + + const char* memberTopicKey = p_root->Attribute(KEY); + + if (memberTopicKey != nullptr && strncmp(memberTopicKey, "true", 5) == 0) + { + builder.apply_annotation(ANNOTATION_KEY_ID, "value", "true"); } - if (p_dynamictype != nullptr) + const char* memberName = p_root->Attribute(NAME); + + if (memberName != nullptr && !values.empty()) { - if (!values.empty()) - { - std::vector labels; - bool defaultLabel = dimensionsToLabels(values, labels); - p_dynamictype->add_member(mId, memberName, memberBuilder->build(), "", labels, defaultLabel); - } - else - { - p_dynamictype->add_member(mId, memberName, memberBuilder->build()); - } + std::vector labels; + bool defaultLabel = dimensionsToLabels(values, labels); + builder.add_member(mId, memberName, memberBuilder->build(), "", labels, defaultLabel); + } + else + { + builder.add_member(mId, memberName, memberBuilder->build()); } return memberBuilder;