/******************************************************************************* (c) 2005-2014 Copyright, Real-Time Innovations, Inc. All rights reserved. RTI grants Licensee a license to use, modify, compile, and create derivative works of the Software. Licensee has the right to distribute object form only for use with RTI products. The Software is provided "as is", with no warranty of any type, including any warranty for fitness for any purpose. RTI is under no obligation to maintain or support the Software. RTI shall not be liable for any incidental or consequential damages arising out of the use or inability to use the software. ******************************************************************************/ /* dynamic_data_enum.cxx This example - creates the type code of for a enum with values {Foo = 0; Bar = 1} - creates the type code of for a struct which contains the enum - creates a DynamicData instance - prints the representation of the created DynamicData Example: To run the example application: */ #include #include using namespace std; DDS_TypeCode *create_enum_type_code(DDS_TypeCodeFactory *tcf) { static DDS_TypeCode *enumTC = NULL; struct DDS_EnumMemberSeq members; DDS_ExceptionCode_t err; /* First, we create the typeCode for the enum */ enumTC = tcf->create_enum_tc("FooEnum", members, err); if (err != DDS_NO_EXCEPTION_CODE) { cerr << "! Unable to create typecode: " << err << endl; goto fail; } /* Case 1: We add the member Foo with value 0 */ enumTC->add_member_to_enum("Foo", 0, err); if (err != DDS_NO_EXCEPTION_CODE) { cerr << "! Unable to add member Foo: " << err << endl; goto fail; } /* Case 2: We add the member Bar with value 1 */ enumTC->add_member_to_enum("Bar", 1, err); if (err != DDS_NO_EXCEPTION_CODE) { cerr << "! Unable to add member Bar: " << err << endl; goto fail; } return enumTC; fail: if (enumTC != NULL) { tcf->delete_tc(enumTC, err); } return NULL; } DDS_TypeCode * outer_struct_get_typecode(DDS_TypeCodeFactory * tcf) { static DDS_TypeCode *tc = NULL; DDS_TypeCode *enumTC = NULL; struct DDS_StructMemberSeq members; DDS_ExceptionCode_t err; /* First, we create the typeCode for a struct */ tc = tcf->create_struct_tc("OuterStruct", members, err); if (err != DDS_NO_EXCEPTION_CODE) { cerr << "! Unable to create struct TC" << endl; goto fail; } enumTC = create_enum_type_code(tcf); if (enumTC == NULL) { cerr << "! Unable to create innerTC" << endl; goto fail; } /* Member 1 of outer struct will be a enum of type FooEnum */ tc->add_member("enum_inside_struct", DDS_TYPECODE_MEMBER_ID_INVALID, enumTC, DDS_TYPECODE_NONKEY_REQUIRED_MEMBER, err); if (err != DDS_NO_EXCEPTION_CODE) { cerr << "! Unable to add member inner struct" << endl; goto fail; } if (enumTC != NULL) { tcf->delete_tc(enumTC, err); } DDS_StructMemberSeq_finalize(&members); return tc; fail: if (tc != NULL) { tcf->delete_tc(tc, err); } DDS_StructMemberSeq_finalize(&members); if (enumTC != NULL) { tcf->delete_tc(enumTC, err); } return NULL; } int example() { DDS_TypeCodeFactory *tcf = NULL; tcf = DDS_TypeCodeFactory::get_instance(); if (tcf == NULL) { cerr << "! Unable to get type code factory singleton" << endl; return -1; } /* Creating the enum & struct typeCode */ struct DDS_TypeCode *enumTC = create_enum_type_code(tcf); struct DDS_TypeCode *structTC = outer_struct_get_typecode(tcf); int ret = -1; DDS_ExceptionCode_t err; struct DDS_DynamicDataProperty_t myProperty = DDS_DYNAMIC_DATA_PROPERTY_DEFAULT; /* Creating a new dynamicData instance based on the struct type code */ DDS_DynamicData data(structTC, myProperty); if (!data.is_valid()) { cerr << "! Unable to create dynamicData" << endl; goto fail; } enumTC->print_IDL(0,err); structTC->print_IDL(0,err); ret = 0; fail: if (enumTC != NULL) { tcf->delete_tc(enumTC, err); } return ret; } int main(int argc, char *argv[]) { return example(); }