diff --git a/include/fastrtps/types/TypesBase.h b/include/fastrtps/types/TypesBase.h index 620801b4cf8..84cc303bae4 100644 --- a/include/fastrtps/types/TypesBase.h +++ b/include/fastrtps/types/TypesBase.h @@ -952,7 +952,7 @@ namespace fastrtps { namespace types { namespace v1_3 { -using DynamicType_ptr = std::shared_ptr; +using DynamicType_ptr = std::shared_ptr; using DynamicTypeBuilder_ptr = std::shared_ptr; using DynamicTypeBuilder_cptr = std::shared_ptr; diff --git a/include/fastrtps/types/v1_3/DynamicPubSubType.hpp b/include/fastrtps/types/v1_3/DynamicPubSubType.hpp index b1e3f13476d..90c2978e662 100644 --- a/include/fastrtps/types/v1_3/DynamicPubSubType.hpp +++ b/include/fastrtps/types/v1_3/DynamicPubSubType.hpp @@ -41,7 +41,7 @@ class DynamicPubSubType : public virtual eprosima::fastdds::dds::TopicDataType RTPS_DllAPI DynamicPubSubType() = default; RTPS_DllAPI DynamicPubSubType( - v1_3::DynamicType& type); + const v1_3::DynamicType& type); RTPS_DllAPI virtual ~DynamicPubSubType(); @@ -74,7 +74,7 @@ class DynamicPubSubType : public virtual eprosima::fastdds::dds::TopicDataType const v1_3::DynamicData& data); RTPS_DllAPI ReturnCode_t SetDynamicType( - v1_3::DynamicType& type); + const v1_3::DynamicType& type); }; } // namespace v1_3 diff --git a/include/fastrtps/types/v1_3/DynamicTypeBuilder.hpp b/include/fastrtps/types/v1_3/DynamicTypeBuilder.hpp index 416f8438d1b..07bb35da81e 100644 --- a/include/fastrtps/types/v1_3/DynamicTypeBuilder.hpp +++ b/include/fastrtps/types/v1_3/DynamicTypeBuilder.hpp @@ -45,6 +45,9 @@ class DynamicTypeBuilder final static void before_destruction( DynamicType* b); + static ReturnCode_t delete_type( + const DynamicType* type) noexcept; + // Only create objects from the associated factory struct use_the_create_method { @@ -171,7 +174,7 @@ class DynamicTypeBuilder final * non-const method usage. * @return new @ref DynamicType object reference */ - RTPS_DllAPI DynamicType* build() const; + RTPS_DllAPI const DynamicType* build() const; RTPS_DllAPI ReturnCode_t copy_from( const DynamicTypeBuilder* other); diff --git a/include/fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp b/include/fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp index 16ffde7f81e..4da727e8e38 100644 --- a/include/fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp +++ b/include/fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp @@ -280,7 +280,7 @@ class DynamicTypeBuilderFactory final * @param[in] kind type identifying the primitive type to retrieve * @return @ref DynamicType object */ - RTPS_DllAPI DynamicType* get_primitive_type( + RTPS_DllAPI const DynamicType* get_primitive_type( TypeKind kind) noexcept; /** @@ -470,7 +470,7 @@ class DynamicTypeBuilderFactory final * Creates a new @ref DynamicTypeBuilder object representing an enum * @return new @ref DynamicTypeBuilder object */ - RTPS_DllAPI DynamicTypeBuilder* create_enum_type(); + RTPS_DllAPI const DynamicTypeBuilder* create_enum_type(); /** * Returns a @ref DynamicTypeBuilder associated with a `TypeKind::TK_STRUCTURE` @@ -499,59 +499,59 @@ class DynamicTypeBuilderFactory final * @param[in] name string annotation identifier * @return new @ref DynamicTypeBuilder object */ - RTPS_DllAPI DynamicType* create_annotation_primitive( + RTPS_DllAPI const DynamicType* create_annotation_primitive( const std::string& name); //! returns type instantiation of the @ref DynamicTypeBuilderFactory::create_alias_type builder - RTPS_DllAPI DynamicType* get_alias_type( + RTPS_DllAPI const DynamicType* get_alias_type( const DynamicType& base_type, const std::string& sName); //! returns the cache type associated to create_int16_type() - RTPS_DllAPI DynamicType* get_int16_type(); + RTPS_DllAPI const DynamicType* get_int16_type(); //! returns the cache type associated to create_uint16_type() - RTPS_DllAPI DynamicType* get_uint16_type(); + RTPS_DllAPI const DynamicType* get_uint16_type(); //! returns the cache type associated to create_int32_type() - RTPS_DllAPI DynamicType* get_int32_type(); + RTPS_DllAPI const DynamicType* get_int32_type(); //! returns the cache type associated to create_uint32_type() - RTPS_DllAPI DynamicType* get_uint32_type(); + RTPS_DllAPI const DynamicType* get_uint32_type(); //! returns the cache type associated to create_int64_type() - RTPS_DllAPI DynamicType* get_int64_type(); + RTPS_DllAPI const DynamicType* get_int64_type(); //! returns the cache type associated to create_uint64_type() - RTPS_DllAPI DynamicType* get_uint64_type(); + RTPS_DllAPI const DynamicType* get_uint64_type(); //! returns the cache type associated to create_float32_type() - RTPS_DllAPI DynamicType* get_float32_type(); + RTPS_DllAPI const DynamicType* get_float32_type(); //! returns the cache type associated to create_float64_type() - RTPS_DllAPI DynamicType* get_float64_type(); + RTPS_DllAPI const DynamicType* get_float64_type(); //! returns the cache type associated to create_float128_type() - RTPS_DllAPI DynamicType* get_float128_type(); + RTPS_DllAPI const DynamicType* get_float128_type(); //! returns the cache type associated to create_char8_type() - RTPS_DllAPI DynamicType* get_char8_type(); + RTPS_DllAPI const DynamicType* get_char8_type(); //! returns the cache type associated to create_char16_type() - RTPS_DllAPI DynamicType* get_char16_type(); + RTPS_DllAPI const DynamicType* get_char16_type(); //! returns the cache type associated to create_bool_type() - RTPS_DllAPI DynamicType* get_bool_type(); + RTPS_DllAPI const DynamicType* get_bool_type(); //! returns the cache type associated to get_byte_type() - RTPS_DllAPI DynamicType* get_byte_type(); + RTPS_DllAPI const DynamicType* get_byte_type(); //! returns the cache type associated to create_string_type() - RTPS_DllAPI DynamicType* get_string_type( + RTPS_DllAPI const DynamicType* get_string_type( uint32_t bound = LENGTH_UNLIMITED) noexcept; //! returns the cache type associated to create_wstring_type() - RTPS_DllAPI DynamicType* get_wstring_type( + RTPS_DllAPI const DynamicType* get_wstring_type( uint32_t bound = LENGTH_UNLIMITED) noexcept; /** @@ -559,7 +559,7 @@ class DynamicTypeBuilderFactory final * @param[in] bound `uint32_t` representing the maximum number of elements that may be stored. * @return new @ref DynamicTypeBuilder object */ - RTPS_DllAPI DynamicType* get_bitset_type( + RTPS_DllAPI const DynamicType* get_bitset_type( uint32_t bound); RTPS_DllAPI void build_type_identifier( diff --git a/include/fastrtps/utils/custom_allocators.hpp b/include/fastrtps/utils/custom_allocators.hpp index ebd2c80a173..df7946badeb 100644 --- a/include/fastrtps/utils/custom_allocators.hpp +++ b/include/fastrtps/utils/custom_allocators.hpp @@ -206,12 +206,12 @@ namespace detail { template class external_reference_counting - : public std::enable_shared_from_this + : public std::enable_shared_from_this { //! Keeps the object alive is external references are held - std::shared_ptr external_lock_; + mutable std::shared_ptr external_lock_; //! External references tracker - std::atomic_long counter_ = {0l}; + mutable std::atomic_long counter_ = {0l}; protected: @@ -231,7 +231,7 @@ class external_reference_counting return counter_; } - long add_ref() + long add_ref() const { long former = counter_; @@ -248,8 +248,8 @@ class external_reference_counting } // sync with other threads release() operations - std::shared_ptr empty; - std::shared_ptr inner = this->shared_from_this(); + std::shared_ptr empty; + std::shared_ptr inner = this->shared_from_this(); while (!std::atomic_compare_exchange_strong( &external_lock_, @@ -281,7 +281,7 @@ class external_reference_counting } } - long release() + long release() const { long former = counter_; @@ -301,13 +301,13 @@ class external_reference_counting } // sync with other threads add_ref() operations - std::shared_ptr inner = this->shared_from_this(), + std::shared_ptr inner = this->shared_from_this(), cmp = inner; while (!std::atomic_compare_exchange_strong( &external_lock_, &cmp, - std::shared_ptr{})) + std::shared_ptr{})) { if (!cmp) { diff --git a/src/cpp/dynamic-types/v1_3/DynamicDataFactory.cpp b/src/cpp/dynamic-types/v1_3/DynamicDataFactory.cpp index d93abe2d135..156424bffd0 100644 --- a/src/cpp/dynamic-types/v1_3/DynamicDataFactory.cpp +++ b/src/cpp/dynamic-types/v1_3/DynamicDataFactory.cpp @@ -88,7 +88,7 @@ DynamicData* DynamicDataFactory::create_data( { if (pBuilder != nullptr && pBuilder->is_consistent()) { - DynamicType_ptr pType = pBuilder->build(); + DynamicType_ptr pType{pBuilder->build()}; return create_data(pType); } else diff --git a/src/cpp/dynamic-types/v1_3/DynamicPubSubType.cpp b/src/cpp/dynamic-types/v1_3/DynamicPubSubType.cpp index 6f76d9dd40b..bb7df632e05 100644 --- a/src/cpp/dynamic-types/v1_3/DynamicPubSubType.cpp +++ b/src/cpp/dynamic-types/v1_3/DynamicPubSubType.cpp @@ -25,7 +25,7 @@ using namespace eprosima::fastrtps::types::v1_3; DynamicPubSubType::DynamicPubSubType( - DynamicType& type) + const DynamicType& type) : dynamic_type_(&type) { UpdateDynamicTypeInfo(); @@ -66,7 +66,7 @@ ReturnCode_t DynamicPubSubType::SetDynamicType( } ReturnCode_t DynamicPubSubType::SetDynamicType( - DynamicType& type) + const DynamicType& type) { if (!dynamic_type_) { diff --git a/src/cpp/dynamic-types/v1_3/DynamicTypeBuilder.cpp b/src/cpp/dynamic-types/v1_3/DynamicTypeBuilder.cpp index ce578619e1c..c0be9f37016 100644 --- a/src/cpp/dynamic-types/v1_3/DynamicTypeBuilder.cpp +++ b/src/cpp/dynamic-types/v1_3/DynamicTypeBuilder.cpp @@ -224,7 +224,7 @@ ReturnCode_t DynamicTypeBuilder::add_member( return ReturnCode_t::RETCODE_OK; } -DynamicType* DynamicTypeBuilder::build() const +const DynamicType* DynamicTypeBuilder::build() const { // check if an instance is already available // and is still valid @@ -334,3 +334,15 @@ std::function eprosima::fastrtps::types::v1_3:: 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 3d7e4d02b9e..d8edd8df3ef 100644 --- a/src/cpp/dynamic-types/v1_3/DynamicTypeBuilderFactory.cpp +++ b/src/cpp/dynamic-types/v1_3/DynamicTypeBuilderFactory.cpp @@ -448,7 +448,7 @@ DynamicTypeBuilder* DynamicTypeBuilderFactory::create_bitmask_type( descriptor.set_kind(TypeKind::TK_BITMASK); // TODO review on implementation for IDL descriptor.set_name(GenerateTypeName(get_type_name(TypeKind::TK_BITMASK))); - descriptor.element_type_ = get_bool_type(); + descriptor.element_type_.reset(get_bool_type()); descriptor.bound_.push_back(bound); return create_type(descriptor); @@ -513,7 +513,7 @@ DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::new_unlimited_string_builder( TypeDescriptor descriptor; descriptor.set_kind(large ? TypeKind::TK_STRING16 : TypeKind::TK_STRING8); descriptor.set_name(TypeNamesGenerator::get_string_type_name(MAX_STRING_LENGTH, large, true)); - descriptor.element_type_ = large ? get_char16_type() : get_char8_type(); + descriptor.element_type_.reset(large ? get_char16_type() : get_char8_type()); descriptor.bound_.push_back(MAX_STRING_LENGTH); try @@ -556,16 +556,16 @@ const DynamicTypeBuilder* DynamicTypeBuilderFactory::create_char16_type() noexce return create_primitive_type(); } -DynamicType_ptr DynamicTypeBuilderFactory::create_annotation_primitive( +const DynamicType* DynamicTypeBuilderFactory::create_annotation_primitive( const std::string& name) { TypeDescriptor descriptor; descriptor.set_kind(TypeKind::TK_ANNOTATION); descriptor.set_name(name); - return create_type(descriptor)->build(); + return DynamicTypeBuilder_ptr{create_type(descriptor)}->build(); } -DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::create_enum_type() +const DynamicTypeBuilder* DynamicTypeBuilderFactory::create_enum_type() { TypeDescriptor pEnumDescriptor; pEnumDescriptor.set_kind(TypeKind::TK_ENUM); @@ -604,7 +604,7 @@ const DynamicTypeBuilder* DynamicTypeBuilderFactory::create_int64_type() noexcep return create_primitive_type(); } -DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::create_map_type( +DynamicTypeBuilder* DynamicTypeBuilderFactory::create_map_type( const DynamicType& key_type, const DynamicType& value_type, uint32_t bound /* = LENGTH_UNLIMITED */) noexcept @@ -627,7 +627,7 @@ DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::create_map_type( return create_type(descriptor); } -DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::create_sequence_type( +DynamicTypeBuilder* DynamicTypeBuilderFactory::create_sequence_type( const DynamicType& type, uint32_t bound /* = LENGTH_UNLIMITED */) noexcept { @@ -664,7 +664,8 @@ const DynamicTypeBuilder* DynamicTypeBuilderFactory::create_string_type( else if ( LENGTH_UNLIMITED == bound ) { // TODO:Barro refactor unbounded to be unbounded and not 256 - return unlimited_builder; + unlimited_builder->add_ref(); + return unlimited_builder.get(); } // otherwise allocate one on the heap @@ -674,7 +675,7 @@ const DynamicTypeBuilder* DynamicTypeBuilderFactory::create_string_type( return create_type(descriptor); } -DynamicTypeBuilder * DynamicTypeBuilderFactory::create_wstring_type( +const DynamicTypeBuilder* DynamicTypeBuilderFactory::create_wstring_type( uint32_t bound /* = LENGTH_UNLIMITED */) noexcept { // C++11 compiler uses double-checked locking pattern to avoid concurrency issues @@ -693,7 +694,8 @@ DynamicTypeBuilder * DynamicTypeBuilderFactory::create_wstring_type( else if ( LENGTH_UNLIMITED == bound ) { // TODO:Barro refactor unbounded to be unbounded and not 256 - return unlimited_builder; + unlimited_builder->add_ref(); + return unlimited_builder.get(); } // otherwise allocate one on the heap @@ -703,7 +705,7 @@ DynamicTypeBuilder * DynamicTypeBuilderFactory::create_wstring_type( return create_type(descriptor); } -DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::create_child_struct_type( +DynamicTypeBuilder* DynamicTypeBuilderFactory::create_child_struct_type( const DynamicType& parent_type) { auto kind = parent_type.get_kind(); @@ -738,7 +740,7 @@ DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::create_child_struct_type( } } -DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::create_struct_type() noexcept +DynamicTypeBuilder* DynamicTypeBuilderFactory::create_struct_type() noexcept { TypeDescriptor descriptor; descriptor.set_kind(TypeKind::TK_STRUCTURE); @@ -762,7 +764,7 @@ const DynamicTypeBuilder* DynamicTypeBuilderFactory::create_uint64_type() noexce return create_primitive_type(); } -DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::create_union_type( +DynamicTypeBuilder* DynamicTypeBuilderFactory::create_union_type( const DynamicType& discriminator_type) { if (discriminator_type.is_discriminator_type()) @@ -782,13 +784,7 @@ DynamicTypeBuilder_ptr DynamicTypeBuilderFactory::create_union_type( ReturnCode_t DynamicTypeBuilderFactory::delete_type( const DynamicType* type) noexcept { - if (type != nullptr) - { - type->release(); - return ReturnCode_t::RETCODE_OK; - } - - return ReturnCode_t::RETCODE_PRECONDITION_NOT_MET; + return DynamicTypeBuilder::delete_type(type); } ReturnCode_t DynamicTypeBuilderFactory::delete_type( @@ -840,10 +836,10 @@ const DynamicTypeBuilder* DynamicTypeBuilderFactory::create_primitive_type( } } -DynamicType* DynamicTypeBuilderFactory::get_primitive_type( +const DynamicType* DynamicTypeBuilderFactory::get_primitive_type( TypeKind kind) noexcept { - const DynamicTypeBuilder* builder = create_primitive_type(kind); + DynamicTypeBuilder_cptr builder{create_primitive_type(kind)}; if (builder) { return builder->build(); @@ -2583,99 +2579,99 @@ void DynamicTypeBuilderFactory::set_annotation_default_value( } } -DynamicType* DynamicTypeBuilderFactory::get_alias_type( +const DynamicType* DynamicTypeBuilderFactory::get_alias_type( const DynamicType& base_type, const std::string& sName) { - DynamicTypeBuilder_ptr builder = create_alias_type(base_type, sName); + DynamicTypeBuilder_cptr builder{create_alias_type(base_type, sName)}; return builder ? builder->build() : nullptr; } -DynamicType* DynamicTypeBuilderFactory::get_int32_type() +const DynamicType* DynamicTypeBuilderFactory::get_int32_type() { - return create_int32_type()->build(); + return DynamicTypeBuilder_cptr{create_int32_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_uint32_type() +const DynamicType* DynamicTypeBuilderFactory::get_uint32_type() { - return create_uint32_type()->build(); + return DynamicTypeBuilder_cptr{create_uint32_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_int16_type() +const DynamicType* DynamicTypeBuilderFactory::get_int16_type() { - return create_int16_type()->build(); + return DynamicTypeBuilder_cptr{create_int16_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_uint16_type() +const DynamicType* DynamicTypeBuilderFactory::get_uint16_type() { - return create_uint16_type()->build(); + return DynamicTypeBuilder_cptr{create_uint16_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_int64_type() +const DynamicType* DynamicTypeBuilderFactory::get_int64_type() { - return create_int64_type()->build(); + return DynamicTypeBuilder_cptr{create_int64_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_uint64_type() +const DynamicType* DynamicTypeBuilderFactory::get_uint64_type() { - return create_uint64_type()->build(); + return DynamicTypeBuilder_cptr{create_uint64_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_float32_type() +const DynamicType* DynamicTypeBuilderFactory::get_float32_type() { - return create_float32_type()->build(); + return DynamicTypeBuilder_cptr{create_float32_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_float64_type() +const DynamicType* DynamicTypeBuilderFactory::get_float64_type() { - return create_float64_type()->build(); + return DynamicTypeBuilder_cptr{create_float64_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_float128_type() +const DynamicType* DynamicTypeBuilderFactory::get_float128_type() { - return create_float128_type()->build(); + return DynamicTypeBuilder_cptr{create_float128_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_char8_type() +const DynamicType* DynamicTypeBuilderFactory::get_char8_type() { - return create_char8_type()->build(); + return DynamicTypeBuilder_cptr{create_char8_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_char16_type() +const DynamicType* DynamicTypeBuilderFactory::get_char16_type() { - return create_char16_type()->build(); + return DynamicTypeBuilder_cptr{create_char16_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_bool_type() +const DynamicType* DynamicTypeBuilderFactory::get_bool_type() { - return create_bool_type()->build(); + return DynamicTypeBuilder_cptr{create_bool_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_byte_type() +const DynamicType* DynamicTypeBuilderFactory::get_byte_type() { - return create_byte_type()->build(); + return DynamicTypeBuilder_cptr{create_byte_type()}->build(); } -DynamicType* DynamicTypeBuilderFactory::get_string_type( +const DynamicType* DynamicTypeBuilderFactory::get_string_type( uint32_t bound /* = LENGTH_UNLIMITED */) noexcept { - DynamicTypeBuilder_cptr builder = bound == LENGTH_UNLIMITED - ? create_string_type() : create_string_type(bound); + DynamicTypeBuilder_cptr builder { + bound == LENGTH_UNLIMITED ? create_string_type() : create_string_type(bound)}; return builder ? builder->build() : nullptr; } -DynamicType* DynamicTypeBuilderFactory::get_wstring_type( +const DynamicType* DynamicTypeBuilderFactory::get_wstring_type( uint32_t bound /* = LENGTH_UNLIMITED */) noexcept { - DynamicTypeBuilder_cptr builder = bound == LENGTH_UNLIMITED - ? create_wstring_type() : create_wstring_type(bound); + DynamicTypeBuilder_cptr builder { + bound == LENGTH_UNLIMITED ? create_wstring_type() : create_wstring_type(bound)}; return builder ? builder->build() : nullptr; } -DynamicType* DynamicTypeBuilderFactory::get_bitset_type( +const DynamicType* DynamicTypeBuilderFactory::get_bitset_type( uint32_t bound) { - DynamicTypeBuilder_ptr builder = create_bitset_type(bound); + DynamicTypeBuilder_ptr builder{create_bitset_type(bound)}; return builder ? builder->build() : nullptr; }