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;

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