DDS_TypeCode *
sequence_element_get_typecode(DDS_TypeCodeFactory *tcf) {
    static DDS_TypeCode * tc = NULL;
    struct DDS_StructMemberSeq members = DDS_SEQUENCE_INITIALIZER;
    DDS_ExceptionCode_t err;

    /* First, we create the typeCode for the simple struct */
    tc = DDS_TypeCodeFactory_create_struct_tc(tcf, "SimpleStruct", &members,
            &err);
    if (err != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to create sequence TC\n");
        goto fail;
    }

    DDS_TypeCode_add_member(tc, "a_member", DDS_TYPECODE_MEMBER_ID_INVALID,
            DDS_TypeCodeFactory_get_primitive_tc(tcf, DDS_TK_LONG),
            DDS_TYPECODE_NONKEY_MEMBER, &err);
    if (err != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to add a_member to SimpleStruct\n");
        goto fail;
    }

    return tc;

    fail:
    if (tc != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, tc, &err);
    }
    return NULL;
}
DDS_TypeCode * outer_struct_get_typecode(struct DDS_TypeCodeFactory *tcf) {
    static DDS_TypeCode *tc = NULL;
    DDS_TypeCode *innerTC = NULL;
    struct DDS_StructMemberSeq members = DDS_SEQUENCE_INITIALIZER;
    DDS_ExceptionCode_t ex;

    /* First, we create the typeCode for a struct */
    tc = DDS_TypeCodeFactory_create_struct_tc(tcf, "OuterStruct", &members,
            &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to create struct TC\n");
        goto fail;
    }

    innerTC = inner_struct_get_typecode(tcf);
    if (innerTC == NULL) {
        fprintf(stderr, "! Unable to create struct TC\n");
        goto fail;
    }
    /* Member 1 of outer struct will be a struct of type inner_struct
     * called inner*/
    DDS_TypeCode_add_member(tc, "inner", DDS_TYPECODE_MEMBER_ID_INVALID,
            innerTC, DDS_TYPECODE_NONKEY_REQUIRED_MEMBER, &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to add member x\n");
        goto fail;
    }

    if (innerTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, innerTC, NULL);
    }

    DDS_StructMemberSeq_finalize(&members);
    return tc;

    fail:
    if (tc != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, tc, &ex);
    }

    if (innerTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, innerTC, NULL);
    }

    DDS_StructMemberSeq_finalize(&members);
    return NULL;
}
struct DDS_TypeCode *create_type_code(struct DDS_TypeCodeFactory *factory) {
    struct DDS_TypeCode *unionTC = NULL;
    DDS_ExceptionCode_t ex;

    /* First, we create the typeCode for a union */
    /* Default-member index refers to which member of the union
     * will be the default one. In this example index = 1 refers
     * to the the member 'aLong'. Take into account that index
     * starts in 0 */
    unionTC = DDS_TypeCodeFactory_create_union_tc(factory, "Foo",
            DDS_TypeCodeFactory_get_primitive_tc(factory, DDS_TK_LONG),
            1, /* default-member index */
            NULL, /* For now, it does not have any member */
            &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to create union TC\n");
        goto fail;
    }

    /* Case 1 will be a short named aShort */
    DDS_TypeCode_add_member(unionTC, "aShort",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED,
            DDS_TypeCodeFactory_get_primitive_tc(factory, DDS_TK_SHORT),
            DDS_TYPECODE_NONKEY_MEMBER, &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to add member aShort\n");
        goto fail;
    }

    /* Case 2, the default, will be a long named aLong */
    DDS_TypeCode_add_member(unionTC, "aLong", 2,
            DDS_TypeCodeFactory_get_primitive_tc(factory, DDS_TK_LONG),
            DDS_TYPECODE_NONKEY_MEMBER, &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to add member aLong\n");
        goto fail;
    }

    /* Case 3 will be a double named aDouble */
    DDS_TypeCode_add_member(unionTC, "aDouble", 3,
            DDS_TypeCodeFactory_get_primitive_tc(factory, DDS_TK_DOUBLE),
            DDS_TYPECODE_NONKEY_MEMBER, &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to add member aDouble\n");
        goto fail;
    }

    return unionTC;

    fail: if (unionTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, unionTC, &ex);
    }
    return NULL;
}
DDS_TypeCode *
type_w_sequence_get_typecode(DDS_TypeCodeFactory *tcf) {
    static DDS_TypeCode * tc = NULL;
    struct DDS_TypeCode * sequenceTC = NULL;
    struct DDS_StructMemberSeq members = DDS_SEQUENCE_INITIALIZER;
    DDS_ExceptionCode_t err;

    sequenceTC = sequence_get_typecode(tcf);
    if (sequenceTC == NULL) {
        fprintf(stderr, "! Unable to get sequenceTC\n");
        goto fail;
    }

    tc = DDS_TypeCodeFactory_create_struct_tc(tcf, "TypeWithSequence", &members,
            &err);
    if (tc == NULL) {
        fprintf(stderr, "! Unable to create Type with sequence TC\n");
        goto fail;
    }

    DDS_TypeCode_add_member(tc, "sequence_member",
            DDS_TYPECODE_MEMBER_ID_INVALID, sequenceTC,
            DDS_TYPECODE_NONKEY_MEMBER, &err);

    if (sequenceTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, sequenceTC, &err);
    }

    DDS_StructMemberSeq_finalize(&members);
    return tc;

    fail:
    if (tc != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, tc, &err);
    }
    if (sequenceTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, sequenceTC, &err);
    }
    DDS_StructMemberSeq_finalize(&members);
    return NULL;
}
DDS_TypeCode *
sequence_get_typecode(DDS_TypeCodeFactory *tcf) {
    static DDS_TypeCode * tc = NULL;
    DDS_TypeCode * seqElementTC = NULL;
    DDS_ExceptionCode_t err;

    seqElementTC = sequence_element_get_typecode(tcf);
    if (seqElementTC == NULL) {
        fprintf(stderr, "! Unable to create typeCode\n");
        goto fail;
    }

    /* We create the typeCode for the sequence */
    tc = DDS_TypeCodeFactory_create_sequence_tc(tcf, MAX_SEQ_LEN, seqElementTC,
            &err);
    if (err != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to create the sequence TC\n");
        goto fail;
    }

    if (seqElementTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, seqElementTC, &err);
    }
    return tc;

    fail:
    if (seqElementTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, seqElementTC, &err);
    }

    if (tc != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, tc, &err);
    }

    return NULL;
}
DDS_TypeCode *
inner_struct_get_typecode(struct DDS_TypeCodeFactory *tcf) {
    static DDS_TypeCode *tc = NULL;
    struct DDS_StructMemberSeq members = DDS_SEQUENCE_INITIALIZER;
    DDS_ExceptionCode_t ex;

    /* First, we create the typeCode for a struct */
    tc = DDS_TypeCodeFactory_create_struct_tc(tcf, "InnerStruct", &members,
            &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to create struct TC\n");
        goto fail;
    }

    /* Member 1 will be a double named x */
    DDS_TypeCode_add_member(tc, "x", DDS_TYPECODE_MEMBER_ID_INVALID,
            DDS_TypeCodeFactory_get_primitive_tc(tcf, DDS_TK_DOUBLE),
            DDS_TYPECODE_NONKEY_REQUIRED_MEMBER, &ex);

    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to add member x\n");
        goto fail;
    }

    /* Member 2 will be a double named y */
    DDS_TypeCode_add_member(tc, "y", DDS_TYPECODE_MEMBER_ID_INVALID,
            DDS_TypeCodeFactory_get_primitive_tc(tcf, DDS_TK_DOUBLE),
            DDS_TYPECODE_NONKEY_REQUIRED_MEMBER, &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "! Unable to add member y\n");
        goto fail;
    }

    DDS_StructMemberSeq_finalize(&members);
    return tc;

    fail:
    if (tc != NULL) {
        DDS_TypeCodeFactory_delete_tc(tcf, tc, &ex);
    }
    DDS_StructMemberSeq_finalize(&members);
    return NULL;

}
void RTI_RoutingServiceFileAdapter_delete_type_code(DDS_TypeCode * type_code)
{
    DDS_TypeCodeFactory * factory = NULL;
    DDS_ExceptionCode_t ex = DDS_NO_EXCEPTION_CODE;

    factory = DDS_TypeCodeFactory_get_instance();
    if (factory == NULL) {
        fprintf(stdout,
                "ERROR getting instance DDS_TypeCodeFactory deleting typecode");
        return;
    } else {
        if (type_code != NULL) {
            DDS_TypeCodeFactory_delete_tc(factory,type_code,&ex);
        }
        if (ex != DDS_NO_EXCEPTION_CODE) {
            fprintf(stderr, "Unable to delete typecode\n");
            return;
        }
    }
}
int main() {
    DDS_DynamicData sample;
    DDS_TypeCodeFactory *factory = NULL;
    DDS_TypeCode *wSequenceTC = NULL;
    DDS_Boolean dynamicDataIsInitialized = DDS_BOOLEAN_FALSE;

    factory = DDS_TypeCodeFactory_get_instance();
    if (factory == NULL) {
        fprintf(stderr, "! Unable to get type code factory singleton\n");
        goto fail;
    }

    wSequenceTC = type_w_sequence_get_typecode(factory);
    if (wSequenceTC == NULL) {
        fprintf(stderr, "! Unable to create wSequence Type Code\n");
        goto fail;
    }

    dynamicDataIsInitialized = DDS_DynamicData_initialize(&sample, wSequenceTC,
            &DDS_DYNAMIC_DATA_PROPERTY_DEFAULT);
    if (!dynamicDataIsInitialized) {
        fprintf(stderr, "! Unable to create dynamicData\n");
        goto fail;
    }
    printf("***** Writing a sample *****\n");

    write_data(&sample, factory);

    printf("***** Reading a sample *****\n");
    read_data(&sample, factory);

    fail:
    if (wSequenceTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, wSequenceTC, NULL);
    }
    if (dynamicDataIsInitialized) {
        DDS_DynamicData_finalize(&sample);
    }
    return 0;
}
void read_data(DDS_DynamicData *sample, DDS_TypeCodeFactory *factory) {
    struct DDS_TypeCode *sequenceTC = NULL;

    DDS_DynamicData seqmember;
    DDS_DynamicData seqelement;
    DDS_Boolean seqMemberIsInitialized = DDS_BOOLEAN_FALSE;
    DDS_Boolean seqElementIsInitialized = DDS_BOOLEAN_FALSE;

    DDS_Long value;
    struct DDS_DynamicDataInfo info;
    int i, seqlen;
    DDS_ReturnCode_t retcode;
    DDS_ExceptionCode_t err;

    sequenceTC = sequence_get_typecode(factory);
    if (sequenceTC == NULL) {
        fprintf(stderr, "! Unable to get sequence type code\n");
        goto fail;
    }
    seqMemberIsInitialized = DDS_DynamicData_initialize(&seqmember, sequenceTC,
            &DDS_DYNAMIC_DATA_PROPERTY_DEFAULT);
    if (!seqMemberIsInitialized) {
        fprintf(stderr, "! Unable to initialize seqMember\n");
        goto fail;
    }

    seqElementIsInitialized = DDS_DynamicData_initialize(&seqelement, NULL,
            &DDS_DYNAMIC_DATA_PROPERTY_DEFAULT);
    if (!seqElementIsInitialized) {
        fprintf(stderr, "! Unable to initialize seqElement\n");
        goto fail;
    }

    retcode = DDS_DynamicData_get_complex_member(sample, &seqmember,
            "sequence_member", DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to get complex member from seq member\n");
        goto fail;
    }

    /* Now we get the total amount of elements contained in the sequence
     * by accessing the dynamic data info
     */
    printf("* Getting sequence member info....\n");

    DDS_DynamicData_get_info(&seqmember, &info);
    seqlen = info.member_count;

    printf("* Sequence contains %d elements\n", seqlen);

    for (i = 0; i < seqlen; ++i) {
        /*
         * The same results can be obtained using
         * bind_complex_member method. The main difference, is that
         * in that case the members are not copied, just referenced
         */
#ifdef USE_BIND_API
        retcode = DDS_DynamicData_bind_complex_member(&seqmember, &seqelement,
                NULL, i + 1);
        if (retcode != DDS_RETCODE_OK) {
            fprintf(stderr,"! Unable to bind complex member\n");
            goto fail;
        }
#else
        retcode = DDS_DynamicData_get_complex_member(&seqmember, &seqelement,
                NULL, i + 1);
        if (retcode != DDS_RETCODE_OK) {
            fprintf(stderr, "! Unable to get complex member\n");
            goto fail;
        }
#endif

        retcode = DDS_DynamicData_get_long(&seqelement, &value, "a_member",
                DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED);
        if (retcode != DDS_RETCODE_OK) {
            fprintf(stderr, "! Unable to get long member from seq element\n");
            goto fail;
        }

        printf("Reading sequence element #%d : \n", i + 1);
        DDS_DynamicData_print(&seqelement, stdout, 1);

#ifdef USE_BIND_API
        retcode = DDS_DynamicData_unbind_complex_member(&seqmember, &seqelement);
        if (retcode != DDS_RETCODE_OK) {
            fprintf(stderr, "! Unable to unbind complex member\n");
        }
#endif

    }
    fail: if (sequenceTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, sequenceTC, &err);
    }

    if (seqMemberIsInitialized) {
        DDS_DynamicData_finalize(&seqmember);
    }

    if (seqElementIsInitialized) {
        DDS_DynamicData_finalize(&seqelement);
    }

    return;
}
void write_data(DDS_DynamicData *sample, DDS_TypeCodeFactory *factory) {
    DDS_DynamicData seqmember;
    DDS_DynamicData seqelement;

    DDS_Boolean seqMemberIsInitialized = DDS_BOOLEAN_FALSE;
    DDS_Boolean seqElementIsInitialized = DDS_BOOLEAN_FALSE;

    DDS_TypeCode *sequenceTC = NULL;
    DDS_TypeCode *sequenceElementTC = NULL;

    DDS_ExceptionCode_t err;
    DDS_ReturnCode_t retcode;

    int i = 0;

    sequenceTC = sequence_get_typecode(factory);
    if (sequenceTC == NULL) {
        fprintf(stderr, "! Unable to create typeCode\n");
        goto fail;
    }

    sequenceElementTC = sequence_element_get_typecode(factory);
    if (sequenceElementTC == NULL) {
        fprintf(stderr, "! Unable to create typeCode\n");
        goto fail;
    }

    seqMemberIsInitialized = DDS_DynamicData_initialize(&seqmember, sequenceTC,
            &DDS_DYNAMIC_DATA_PROPERTY_DEFAULT);
    if (!seqMemberIsInitialized) {
        fprintf(stderr, "! Unable to initialize seqMember\n");
        goto fail;
    }

    seqElementIsInitialized = DDS_DynamicData_initialize(&seqelement,
            sequenceElementTC, &DDS_DYNAMIC_DATA_PROPERTY_DEFAULT);
    if (!seqElementIsInitialized) {
        fprintf(stderr, "! Unable to initialize seqElement\n");
        goto fail;
    }

    for (i = 0; i < MAX_SEQ_LEN; ++i) {

        /* To access the elements of a sequence it is necessary
         * to use their id. This parameter allows accessing to every element
         * of the sequence using a 1-based index.
         * There are two ways of doing this: bind API and get API.
         * See the NestedStructExample for further details about the 
         * differences between these two APIs. */

#ifdef USE_BIND_API
        retcode = DDS_DynamicData_bind_complex_member(&seqmember, &seqelement,
                NULL, i + 1);
        if (retcode != DDS_RETCODE_OK) {
            fprintf(stderr,"! Unable to bind complex member\n");
            goto fail;
        }
        retcode = DDS_DynamicData_set_long(&seqelement, "a_member",
                DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, i);
        if (retcode != DDS_RETCODE_OK) {
            fprintf(stderr,"! Unable to set a_member long\n");
            goto fail;
        }
        printf("Writing sequence element #%d : \n", i + 1);
        DDS_DynamicData_print(&seqelement, stdout, 1);
        retcode = DDS_DynamicData_unbind_complex_member(&seqmember, &seqelement);
        if (retcode != DDS_RETCODE_OK) {
            fprintf(stderr,"! Unable to unbind complex member\n");
            goto fail;
        }
#else
        retcode = DDS_DynamicData_set_long(&seqelement, "a_member",
                DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, i);
        if (retcode != DDS_RETCODE_OK) {
            fprintf(stderr, "! Unable to set a_member long\n");
            goto fail;
        }
        printf("Writing sequence element #%d : \n", i + 1);
        DDS_DynamicData_print(&seqelement, stdout, 1);
        retcode = DDS_DynamicData_set_complex_member(&seqmember, NULL, i + 1,
                &seqelement);
        if (retcode != DDS_RETCODE_OK) {
            fprintf(stderr, "! Unable to set complex member\n");
            goto fail;
        }
#endif
    }

    retcode = DDS_DynamicData_set_complex_member(sample, "sequence_member",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, &seqmember);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to set complex member\n");
        goto fail;
    }

    fail:
    if (sequenceTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, sequenceTC, &err);
    }

    if (sequenceElementTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, sequenceElementTC, &err);
    }

    if (seqMemberIsInitialized) {
        DDS_DynamicData_finalize(&seqmember);
    }

    if (seqElementIsInitialized) {
        DDS_DynamicData_finalize(&seqelement);
    }
    return;
}
int example() {
    struct DDS_TypeCode *unionTC = NULL;
    struct DDS_DynamicData data;
    DDS_ReturnCode_t retcode;
    struct DDS_DynamicDataMemberInfo info;
    struct DDS_TypeCodeFactory *factory = NULL;
    int ret = -1;
    struct DDS_DynamicDataProperty_t myProperty =
            DDS_DYNAMIC_DATA_PROPERTY_DEFAULT;
    DDS_Short valueTestShort;
    DDS_Boolean dynamicDataIsInitialized = DDS_BOOLEAN_FALSE;

    /* Getting a reference to the type code factory */
    factory = DDS_TypeCodeFactory_get_instance();
    if (factory == NULL) {
        fprintf(stderr, "! Unable to get type code factory singleton\n");
        goto fail;
    }

    /* Creating the union typeCode */
    unionTC = create_type_code(factory);
    if (unionTC == NULL) {
        fprintf(stderr, "! Unable to create typeCode\n");
        goto fail;
    }

    /* Creating a new dynamicData instance based on the union type code */
    dynamicDataIsInitialized = DDS_DynamicData_initialize(&data, unionTC,
            &myProperty);
    if (!dynamicDataIsInitialized) {
        fprintf(stderr, "! Unable to create dynamicData\n");
        goto fail;
    }

    /* If we get the current discriminator, it will be the default one
     (not the first added) */
    retcode = DDS_DynamicData_get_member_info_by_index(&data, &info, 0);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to get member info\n");
        goto fail;
    }

    printf("The member selected is %s\n", info.member_name);

    /* When we set a value in one of the members of the union,
     * the discriminator updates automatically to point that member.  */
    retcode = DDS_DynamicData_set_short(&data, "aShort",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, 42);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to set value to aShort member\n");
        goto fail;
    }

    /* The discriminator should now reflect the new situation */
    retcode = DDS_DynamicData_get_member_info_by_index(&data, &info, 0);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to get member info\n");
        goto fail;
    }

    printf("The member selected is %s\n", info.member_name);

    /* Getting the value of the target member */
    retcode = DDS_DynamicData_get_short(&data, &valueTestShort, "aShort",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to get member value\n");
        goto fail;
    }

    printf("The member selected is %s with value: %d\n", info.member_name,
            valueTestShort);

    ret = 1;

    fail: 
    if (unionTC != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, unionTC, NULL);
    }

    if (dynamicDataIsInitialized) {
        DDS_DynamicData_finalize(&data);
    }

    return ret;
}
/* This function creates the typecode */
DDS_TypeCode * RTI_RoutingServiceFileAdapter_create_type_code()
{

    struct DDS_TypeCodeFactory * factory = NULL;
    struct DDS_TypeCode * sequence_tc = NULL; /* type code for octet */
    struct DDS_TypeCode * struct_tc = NULL; /* Top-level typecode */
    struct DDS_StructMemberSeq member_seq = DDS_SEQUENCE_INITIALIZER;
    DDS_ExceptionCode_t ex = DDS_NO_EXCEPTION_CODE;

    /* we get the instance of the type code factory */
    factory = DDS_TypeCodeFactory_get_instance();
    if (factory == NULL) {
        fprintf(stderr, "ERROR: Unable to get type code factory singleton\n");
        goto done;
    }

    /* create a sequence for DDS_Octet, so we use a general type */
    sequence_tc = DDS_TypeCodeFactory_create_sequence_tc(
            factory,
            MAX_PAYLOAD_SIZE,
            DDS_TypeCodeFactory_get_primitive_tc(factory,
                    DDS_TK_OCTET),
                    &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "ERROR: Unable to create 'payload' sequence typecode:"
                " %d\n", ex);
        goto done;
    }

    /* create top-level typecode */
    struct_tc = DDS_TypeCodeFactory_create_struct_tc(
            factory,
            "TextLine",
            &member_seq,
            &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr, "Unable to create struct typecode, error = %d \n", ex);
        goto done;
    }

    DDS_TypeCode_add_member(struct_tc,
            "value",
            DDS_MEMBER_ID_INVALID,
            sequence_tc,
            DDS_TYPECODE_NONKEY_MEMBER,
            &ex);
    if (ex != DDS_NO_EXCEPTION_CODE) {
        fprintf(stderr,"Error adding member to struct typecode, error=%d\n",
                ex);
        goto done;
    }
    if (sequence_tc !=NULL) {
        DDS_TypeCodeFactory_delete_tc(factory,sequence_tc,&ex);
    }
    return struct_tc;

done:
    if (sequence_tc != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, sequence_tc, &ex);
    }
    if (struct_tc != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, struct_tc, &ex);
    }
    return NULL;
}
int main() {
    struct DDS_TypeCode *inner_tc = NULL;
    struct DDS_TypeCode *outer_tc = NULL;
    struct DDS_TypeCodeFactory *factory = NULL;

    DDS_ExceptionCode_t err;
    DDS_ReturnCode_t retcode;
    int ret = -1;

    DDS_DynamicData *outer_data = NULL;
    DDS_DynamicData *inner_data = NULL;
    DDS_DynamicData *bounded_data = NULL;

    /* Getting a reference to the type code factory */
    factory = DDS_TypeCodeFactory_get_instance();
    if (factory == NULL) {
        fprintf(stderr, "! Unable to get type code factory singleton\n");
        goto fail;
    }

    /* Creating the typeCode of the inner_struct */
    inner_tc = inner_struct_get_typecode(factory);
    if (inner_tc == NULL) {
        fprintf(stderr, "! Unable to create typeCode\n");
        goto fail;
    }

    /* Creating the typeCode of the outer_struct that contains an inner_struct */
    outer_tc = outer_struct_get_typecode(factory);
    if (inner_tc == NULL) {
        fprintf(stderr, "! Unable to create typeCode\n");
        goto fail;
    }

    printf(" Connext Dynamic Data Nested Struct Example \n"
            "--------------------------------------------\n");

    printf(" Data Types\n"
            "------------------\n");
    DDS_TypeCode_print_IDL(inner_tc, 0, &err);
    DDS_TypeCode_print_IDL(outer_tc, 0, &err);

    /* Now, we create a dynamicData instance for each type */
    outer_data = DDS_DynamicData_new(outer_tc,
            &DDS_DYNAMIC_DATA_PROPERTY_DEFAULT);
    if (outer_data == NULL) {
        fprintf(stderr, "! Unable to create outer dynamicData\n");
        goto fail;
    }

    inner_data = DDS_DynamicData_new(inner_tc,
            &DDS_DYNAMIC_DATA_PROPERTY_DEFAULT);
    if (outer_data == NULL) {
        fprintf(stderr, "! Unable to create inner dynamicData\n");
        goto fail;
    }
    /* Setting the inner data */
    retcode = DDS_DynamicData_set_double(inner_data, "x",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, 3.14159);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to set value 'x' in the inner struct \n");
        goto fail;
    }

    retcode = DDS_DynamicData_set_double(inner_data, "y",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, 2.71828);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to set value 'y' in the inner struct\n");
        goto fail;
    }

    printf("\n\n get/set_complex_member API\n"
            "----------------------------\n");
    /* Using set_complex_member, we copy inner_data values in inner_struct of
     * outer_data */
    printf("Setting initial values of outer_data with "
            "set_complex_member()\n");
    retcode = DDS_DynamicData_set_complex_member(outer_data, "inner",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, inner_data);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to set complex struct value "
                "(member inner in the outer struct)\n");
        goto fail;
    }

    DDS_DynamicData_print(outer_data, stdout, 1);

    retcode = DDS_DynamicData_clear_all_members(inner_data);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to clear all member in the inner data\n");
        goto fail;
    }

    printf("\n + get_complex_member() called\n");
    retcode = DDS_DynamicData_get_complex_member(outer_data, inner_data,
            "inner", DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to get complex struct value "
                "(member inner in the outer struct)\n");
        goto fail;
    }

    printf("\n + inner_data value\n");
    DDS_DynamicData_print(inner_data, stdout, 1);

    /* get_complex_member made a copy of the inner_struct. If we modify
     * inner_data values, outer_data inner_struct WILL NOT be modified. */
    printf("\n + setting new values to inner_data\n");
    retcode = DDS_DynamicData_set_double(inner_data, "x",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, 1.00000);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to set value 'x' in the inner struct \n");
        goto fail;
    }

    retcode = DDS_DynamicData_set_double(inner_data, "y",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, 0.00001);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to set value 'y' in the inner struct \n");
        goto fail;
    }

    DDS_DynamicData_print(inner_data, stdout, 1);

    /* Current value of outer_data
     * outer_data:
     * inner:
     *     x: 3.141590
     *     y: 2.718280
     *
     * inner_data:
     *     x: 1.000000
     *     y: 0.000010
     */

    printf("\n + current outer_data value \n");
    DDS_DynamicData_print(outer_data, stdout, 1);

    /* Bind/Unbind member API */
    printf("\n\n bind/unbind API\n"
            "------------------\n");
    printf("Creating a new dynamic data called bounded_data\n");
    bounded_data = DDS_DynamicData_new(NULL,
            &DDS_DYNAMIC_DATA_PROPERTY_DEFAULT);
    if (bounded_data == NULL) {
        fprintf(stderr, "! Unable to create new dynamic data\n");
        goto fail;
    }

    printf("\n + binding bounded_data to outer_data's inner_struct\n");

    /* Using bind_complex_member, we do not copy inner_struct, but bind it.
     * So, if we modify bounded_data, the inner member inside outer_data WILL
     * also be modified */
    retcode = DDS_DynamicData_bind_complex_member(outer_data, bounded_data,
            "inner", DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to bind the structs\n");
        goto fail;
    }

    DDS_DynamicData_print(bounded_data, stdout, 1);

    printf("\n + setting new values to bounded_data\n");
    retcode = DDS_DynamicData_set_double(bounded_data, "x",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, 1.00000);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to set value to 'x' with the bounded data\n");
        goto fail;
    }

    retcode = DDS_DynamicData_set_double(bounded_data, "y",
            DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED, 0.00001);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to set value to 'x' with the bounded data\n");
        goto fail;
    }

    /* Current value of outer data
     * outer:
     * inner:
     *     x: 1.000000
     *     y: 0.000010
     */

    DDS_DynamicData_print(bounded_data, stdout, 1);
    retcode = DDS_DynamicData_unbind_complex_member(outer_data, bounded_data);
    if (retcode != DDS_RETCODE_OK) {
        fprintf(stderr, "! Unable to unbind the data\n");
        goto fail;
    }

    printf("\n + current outer_data value \n");
    DDS_DynamicData_print(outer_data, stdout, 1);

    ret = 1;

    fail:
    if (inner_tc != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, inner_tc, NULL);
    }

    if (outer_tc != NULL) {
        DDS_TypeCodeFactory_delete_tc(factory, outer_tc, NULL);
    }

    if (inner_data != NULL) {
        DDS_DynamicData_delete(inner_data);
    }

    if (outer_data != NULL) {
        DDS_DynamicData_delete(outer_data);
    }

    if (bounded_data != NULL) {
        DDS_DynamicData_delete(bounded_data);
    }

    return ret;
}