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 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;
}
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;
}
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;
}