Skip to content

Commit

Permalink
Refs 17138. API update fixes .. part 3
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Barro <[email protected]>
  • Loading branch information
Miguel Barro committed Jun 20, 2023
1 parent b21a6c6 commit e958673
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 234 deletions.
8 changes: 8 additions & 0 deletions include/fastrtps/types/DynamicPubSubType.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,21 @@ 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);

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
Expand Down
3 changes: 0 additions & 3 deletions include/fastrtps/types/TypesBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -926,9 +926,6 @@ namespace v1_3 {
class DynamicType;
class DynamicTypeBuilder;

template<class T>
std::function<void(const T*)> dynamic_object_deleter(const T*);

using namespace xtypes_names;

constexpr uint32_t LENGTH_UNLIMITED = std::numeric_limits<uint32_t>::max();
Expand Down
4 changes: 3 additions & 1 deletion include/fastrtps/types/v1_3/DynamicType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class DynamicType final
explicit use_the_create_method() = default;
};

friend std::function<void(const DynamicType*)> 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:

Expand Down
7 changes: 6 additions & 1 deletion include/fastrtps/types/v1_3/DynamicTypeBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(const DynamicTypeBuilder*)> 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};

Expand Down
13 changes: 12 additions & 1 deletion include/fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`
Expand Down
7 changes: 3 additions & 4 deletions include/fastrtps/types/v1_3/DynamicTypeBuilderPtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ namespace fastrtps {
namespace types {
namespace v1_3 {

template<>
std::function<void(const DynamicTypeBuilder*)> dynamic_object_deleter(const DynamicTypeBuilder*);
RTPS_DllAPI void (*dynamic_object_deleter(const DynamicTypeBuilder* ))(const DynamicTypeBuilder*);

} // namespace v1_3
} // namespace types
Expand Down Expand Up @@ -57,7 +56,7 @@ class shared_ptr<const eprosima::fastrtps::types::v1_3::DynamicTypeBuilder>
shared_ptr(const shared_ptr<Y>& r, element_type* ptr) noexcept
: base(r, ptr) {}

template <class T, enable_if_t<is_convertible<T*, element_type*>::value, int> = 0>
template <class T, typename enable_if<is_convertible<T*, element_type*>::value, int>::type = 0>
explicit shared_ptr(const weak_ptr<T>& r)
: base(r) {}

Expand Down Expand Up @@ -121,7 +120,7 @@ class shared_ptr<eprosima::fastrtps::types::v1_3::DynamicTypeBuilder>
shared_ptr(const shared_ptr<Y>& r, element_type* ptr) noexcept
: base(r, ptr) {}

template <class T, enable_if_t<is_convertible<T*, element_type*>::value, int> = 0>
template <class T, typename enable_if<is_convertible<T*, element_type*>::value, int>::type = 0>
explicit shared_ptr(const weak_ptr<T>& r)
: base(r) {}

Expand Down
7 changes: 3 additions & 4 deletions include/fastrtps/types/v1_3/DynamicTypePtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ namespace fastrtps {
namespace types {
namespace v1_3 {

template<>
std::function<void(const DynamicType*)> dynamic_object_deleter(const DynamicType*);
RTPS_DllAPI void (*dynamic_object_deleter(const DynamicType* ))(const DynamicType*);

} // namespace v1_3
} // namespace types
Expand Down Expand Up @@ -57,7 +56,7 @@ class shared_ptr<const eprosima::fastrtps::types::v1_3::DynamicType>
shared_ptr(const shared_ptr<Y>& r, element_type* ptr) noexcept
: base(r, ptr) {}

template <class T, enable_if_t<is_convertible<T*, element_type*>::value, int> = 0>
template <class T, typename enable_if<is_convertible<T*, element_type*>::value, int>::type = 0>
explicit shared_ptr(const weak_ptr<T>& r)
: base(r) {}

Expand Down Expand Up @@ -121,7 +120,7 @@ class shared_ptr<eprosima::fastrtps::types::v1_3::DynamicType>
shared_ptr(const shared_ptr<Y>& r, element_type* ptr) noexcept
: base(r, ptr) {}

template <class T, enable_if_t<is_convertible<T*, element_type*>::value, int> = 0>
template <class T, typename enable_if<is_convertible<T*, element_type*>::value, int>::type = 0>
explicit shared_ptr(const weak_ptr<T>& r)
: base(r) {}

Expand Down
11 changes: 7 additions & 4 deletions include/fastrtps/xmlparser/XMLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion include/fastrtps/xmlparser/XMLProfileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 11 additions & 9 deletions src/cpp/dynamic-types/v1_3/DynamicPubSubType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <fastcdr/Cdr.h>

#include <fastdds/dds/log/Log.hpp>
#include <fastdds/rtps/common/InstanceHandle.h>
#include <fastdds/rtps/common/SerializedPayload.h>
#include <fastrtps/types/v1_3/DynamicData.hpp>
#include <fastrtps/types/v1_3/DynamicDataFactory.hpp>
#include <fastrtps/types/v1_3/DynamicPubSubType.hpp>
#include <fastrtps/types/v1_3/DynamicType.hpp>
#include <fastrtps/types/v1_3/DynamicTypeBuilderFactory.hpp>
#include <fastrtps/types/v1_3/DynamicTypeMember.hpp>
#include <fastrtps/types/v1_3/DynamicDataFactory.hpp>
#include <fastrtps/types/v1_3/DynamicData.hpp>
#include <fastdds/rtps/common/SerializedPayload.h>
#include <fastdds/rtps/common/InstanceHandle.h>
#include <fastdds/dds/log/Log.hpp>
#include <fastcdr/Cdr.h>

using namespace eprosima::fastrtps::types::v1_3;

DynamicPubSubType::DynamicPubSubType(
const DynamicType& type)
: dynamic_type_(&type)
: dynamic_type_(DynamicTypeBuilderFactory::get_instance().create_copy(type))
{
UpdateDynamicTypeInfo();
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
32 changes: 21 additions & 11 deletions src/cpp/dynamic-types/v1_3/DynamicType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1894,25 +1894,35 @@ void DynamicType::serialize_empty_data(
}
}

std::function<void(const DynamicType*)> 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<const DynamicType> 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<DynamicType*>(pDT)->release();
}
};
return DynamicType::external_dynamic_object_deleter;
}
else
{
// This is an internal object
return std::default_delete<const DynamicType>();
return DynamicType::internal_dynamic_object_deleter;
}
}

Expand Down
65 changes: 41 additions & 24 deletions src/cpp/dynamic-types/v1_3/DynamicTypeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,40 +309,57 @@ bool DynamicTypeBuilder::equals(
return *this == other;
}

std::function<void(const DynamicTypeBuilder*)> 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<const DynamicTypeBuilder> 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<DynamicTypeBuilder*>(pDT)->release();
}
};
return DynamicTypeBuilder::external_dynamic_object_deleter;
}
else
{
// This is an internal object
return std::default_delete<const DynamicTypeBuilder>();
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;
}
8 changes: 7 additions & 1 deletion src/cpp/dynamic-types/v1_3/DynamicTypeBuilderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit e958673

Please sign in to comment.