Creating derived topic types using Dynamic Data

2 posts / 0 new
Last post
Offline
Last seen: 5 months 1 day ago
Joined: 10/23/2013
Posts: 43
Creating derived topic types using Dynamic Data

Hello,

How do you use the Dynamic Data C API to create and register a type defined with IDL inheritance?  For example, given the following IDL, how is the type Square created and registered so that DDS knows that it is derived from type Shape?

 

struct Shape {

float area;

}

struct Square: Shape {

float side_length;

}

Organization:
gianpiero's picture
Offline
Last seen: 9 months 3 weeks ago
Joined: 06/02/2010
Posts: 177

Hello,

in order to do that you have to use DDS_TypeCodeFactory_create_value_tc that gets the base class as a parameter. 

First thing to do is to create the type code for the type:

DDS_TypeCode *MyType_create() {
    struct DDS_TypeCodeFactory * factory = NULL;
    struct DDS_TypeCode * shapeTc = NULL;   /* Typecode for 'base class Shape'     */
    struct DDS_TypeCode * squareTc = NULL;     /* Typecode for Square      */
    DDS_ExceptionCode_t ex;
    struct DDS_StructMemberSeq memberSeq = DDS_SEQUENCE_INITIALIZER;
    struct DDS_ValueMemberSeq valueMemberSeq = DDS_SEQUENCE_INITIALIZER;
    
    factory = DDS_TypeCodeFactory_get_instance();
    if (factory == NULL) {
        fprintf(stderr, "! Unable to get type code factory singleton\n");
        goto done;
    }
    
    shapeTc = DDS_TypeCodeFactory_create_struct_tc(
                                                    factory,
                                                    "Shape",
                                                    &memberSeq,
                                                    &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to create struct typecode, error=%d\n", ex);
        goto done;
    }
    
    DDS_TypeCode_add_member(shapeTc,
                            "area",
                            DDS_MEMBER_ID_INVALID,
                            DDS_TypeCodeFactory_get_primitive_tc(factory,
                                                                 DDS_TK_FLOAT),
                            DDS_TYPECODE_NONKEY_REQUIRED_MEMBER,
                            &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to add member to struct typecode, error=%d\n",
                ex);
        goto done;
    }
    
    squareTc = DDS_TypeCodeFactory_create_value_tc(
                                                   factory,
                                                   "Square",
 													DDS_VM_NONE,
												shapeTc,
                                                   &valueMemberSeq,
                                                   &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to create struct typecode, error=%d\n", ex);
        goto done;
    }
    
    DDS_TypeCode_add_member(squareTc,
                            "side_length",
                            1,
                            DDS_TypeCodeFactory_get_primitive_tc(factory,
                                                                 DDS_TK_FLOAT),
                            DDS_TYPECODE_NONKEY_REQUIRED_MEMBER,
                            &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to add member to struct typecode, error=%d\n",
                ex);
        goto done;
    }
    
    return squareTc;
    
done:
    
    if (shapeTc != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, shapeTc, &ex);
    }
    if (squareTc != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, squareTc, &ex);
    }
    return NULL;
}

To verify that the type is what you want you can call the print_IDL function:

DDS_TypeCode *squareTc = MyType_create(); DDS_TypeCode_print_IDL(squareTc,4,NULL);

You shold get:

            struct Square : Shape {
               public float side_length;
               //@ID 1
            }; //@Extensibility EXTENSIBLE_EXTENSIBILITY

Now you can proceed as usual registering the type and creating the topic

    /* Create the Dynamic data type support object
     */
    typeSupport = DDS_DynamicDataTypeSupport_new(squareTc, 
                        &props);
    if (typeSupport == NULL) {
        fprintf(stderr, "! Unable to create dynamic data type support\n");
        goto exitFn;
    }

    /* Register type before creating topic */
    rc = DDS_DynamicDataTypeSupport_register_type(typeSupport, 
                        participant, 
                        typeName);
    if (rc != DDS_RETCODE_OK) {
        fprintf(stderr,"! Unable to register dynamic type\n");
        goto exitFn;
    }

Let me know if this answers your question.

Best,
   Gianpiero