5.2 Moden C++ DynamicType (i.e., TypeCode) Serialization

11 posts / 0 new
Last post
Offline
Last seen: 8 years 6 months ago
Joined: 05/02/2016
Posts: 2
5.2 Moden C++ DynamicType (i.e., TypeCode) Serialization

I'm using the RTI Connext 5.2 Modern C++ API and I'm trying to replicate the functionality that is described within this HOWTO (specifically, "typecode serialization" that's referenced in the link within the uploaded files):

https://community.rti.com/howto/dynamicdata-serialization

Unfortunately, I can't seem to find any analogous functionality to the RTICdrTypeCode_serialize functionality within the Moden C++ API.  Has anyone had success with serializing DynamicTypes in the new API?  Thanks in advance.

Offline
Last seen: 1 month 3 weeks ago
Joined: 04/02/2013
Posts: 196

That article seems outdated. It's actually simpler than that. All you need is to call rti::core::xtypes::to_cdr_buffer. That will serialize a DynamicData object into a std::vector.

Offline
Last seen: 8 years 6 months ago
Joined: 05/02/2016
Posts: 2

Thanks for the input Alejandro.  I understand how to serialize a DynamicData object, the question is how do I serialize (and subsequently deserialize) the DynamicType?  This is referred to as the type_code information under the classic interface but I can't find anything analogous to it in the Modern API.

Offline
Last seen: 5 years 3 weeks ago
Joined: 08/13/2014
Posts: 55

Hi Alejandro and David,

That's my question too. I would be very grateful if anyone can help me.

Bonjefir

Offline
Last seen: 1 month 3 weeks ago
Joined: 04/02/2013
Posts: 196

To serialize the type code—in the modern C++ API it's called DynamicType, as the OMG standard now indicates—you can use the exact same C code you have. You just need to obtain the C "view" of the C++ DynamicType:

dds::core::xtypes::DynamicType cpp_dynamic_type = ...;
const DDS_TypeCode * c_type_code = &cpp_dynamic_type.native();
//
// Use c_type_code as you used to
// 

Please keep in mind that RTICdrTypeCode_serialize and DynamicType::native() are not part of the public API and therefore not officially supported and subject to change.

Alex

Offline
Last seen: 5 years 3 weeks ago
Joined: 08/13/2014
Posts: 55

Dear Alex,

 Thanks a lot for your answer. That was a big help for me. Now I am able to serialize and deserialize typeCode but I cannot convert the returned "DDS_TypeCode" to "dds::core::xtype::DynamicType".

I tried the following code:

dds::core::xtypes::DynamicType *x;
x->native(*(reconstructedTypeCode));

But I have a run-time error (segmentation fault) in 

void native(const native_type& value){ adapter_type::copy(native(), value);}

which is a function in NativeValueType.hpp. 

I would be very grateful if you can help me about this problem. A working example will be wonderful.

Thanks in advance for your help.

Bonjefir

Offline
Last seen: 1 month 3 weeks ago
Joined: 04/02/2013
Posts: 196

Try making x a stack variable:

dds::core::xtypes::DynamicType x;
x.native(*(reconstructedTypeCode));

Alex

Offline
Last seen: 1 month 3 weeks ago
Joined: 04/02/2013
Posts: 196

I just realized that the default constructor of DynamicType is protected, because it shouldn't be instantiated directly. You should pick a subclass. If your type is a struct, use StructType:

dds::core::xtypes::StructType x("MyStruct"); // The name doesn't matter here because we're going to override it in the following line
x.native(*(reconstructedTypeCode));

 

Offline
Last seen: 5 years 3 weeks ago
Joined: 08/13/2014
Posts: 55

Dear Alex,

Thanks a lot for your nice and prompt response. That was a very very big help :) . But I'm afraid of cases in which we don't know the original type. Is there any way to get the type first and then decide which kind of type we need. As you know we have serveral dynamic type including dds::core::xtype::EnumType, dds::core::xtype::UnionType and so on. What should we do in that case?

Best Regards,

Bonjefir

Offline
Last seen: 1 month 3 weeks ago
Joined: 04/02/2013
Posts: 196

In fact, because the classes derived from DynamicType don't add any additional state it is safe to use StructType and then cast, even if the type is not a struct:

dds::core::xtypes::StructType x("MyStruct");
x.native(*(reconstructedTypeCode));
dds::core::xtypes::DynamicType& x_as_dynamic_type = static_cast<dds::core::xtypes::DynamicType&>(x);

Another option (probably safer in the long run) is to first get the type kind from reconstructedTypeCode

DDS_TCKind kind = DDS_TypeCode_kind(reconstructedTypeCode);

And depending on the kind, choose the right subclass of DynamicType.

Offline
Last seen: 5 years 3 weeks ago
Joined: 08/13/2014
Posts: 55

Hi Alex,

Thanks again for your helps. I tried the following code and it's working;

DDS_ExceptionCode_t *ex;

DDS_TCKind kind = DDS_TypeCode_kind(reconstructedTypeCode,ex);

Best Regards,

Bonjefir