Skip to content

Instantly share code, notes, and snippets.

@gianpiero
Last active January 29, 2024 19:29
Show Gist options
  • Save gianpiero/6181ae8f9999bffa117b3dd9d94236b3 to your computer and use it in GitHub Desktop.
Save gianpiero/6181ae8f9999bffa117b3dd9d94236b3 to your computer and use it in GitHub Desktop.
example on how to print a type in IDL or XML
#define PRINT_IDL 0
#define PRINT_XML 1
#define PRINT_COMPLETE_TYPE 0
#define PRINT_TOP_LEVEL_TYPE 1
/*
* This function takes a serialized type object and prints it in either XML or IDL format.
*
* Parameters:
* - ser_type_obj: A pointer to the serialized type object.
* - ser_type_obj_size: The size of the serialized type object.
* - complete: A flag indicating whether to print the complete type or just the top level type.
* Use PRINT_COMPLETE_TYPE to print the complete type and PRINT_TOP_LEVEL_TYPE to print just the top level.
* - xml_or_idl: A flag indicating the format to print the type object in.
* Use PRINT_XML to print in XML format and PRINT_IDL to print in IDL format.
*
* The function first creates a type object from the serialized type object. It then converts this type object to a type code.
* Depending on the 'complete' parameter, it sets the print format to either print the complete type or just the top level type.
* Depending on the 'xml_or_idl' parameter, it sets the print kind to either XML or IDL.
* It then prints the type code in the specified format and kind.
* Finally, it cleans up by deleting the type code, the type object, and the type object factory.
*
* Returns:
* - 0 if the function executed successfully.
* - -1 if there was an error (e.g., failed to create the type object factory, failed to create the type object from the buffer,
* failed to convert the type code to the type object, or an invalid print complete kind or print kind was provided).
*/
int printFromSerializedTypeObject(char * ser_type_obj, unsigned int ser_type_obj_size, int complete, int xml_or_idl) {
DDS_ExceptionCode_t ex;
struct DDS_TypeObjectFactory * type_object_factory = DDS_TypeObjectFactory_new();
if (type_object_factory == NULL) {
#if DEBUG
printf("Failed to create type object factory\n");
#endif
return -1;
}
DDS_TypeObject * type_object = DDS_TypeObjectFactory_create_typeobject_from_serialize_buffer(type_object_factory, ser_type_obj, ser_type_obj_size);
if (type_object == NULL) {
#if DEBUG
printf("Failed to create type object from buffer\n");
#endif
return -1;
}
const DDS_TypeCode * type_code = DDS_TypeObject_convert_to_typecode(type_object);
if (type_code == NULL) {
#if DEBUG
printf("Failed to convert type code to type object\n");
#endif
return -1;
}
DDS_TypeCodePrintFormatProperty format = DDS_TypeCode_PrintFormat_INITIALIZER;
switch (complete) {
case PRINT_COMPLETE_TYPE:
format.print_complete_type = DDS_BOOLEAN_TRUE;
break;
case PRINT_TOP_LEVEL_TYPE:
format.print_complete_type = DDS_BOOLEAN_FALSE;
break;
default:
#if DEBUG
printf("Invalid Print Complete Kind\n");
#endif
return -1;
}
switch (xml_or_idl) {
case PRINT_XML:
format.print_kind = DDS_TYPE_CODE_PRINT_KIND_XML;
break;
case PRINT_IDL:
format.print_kind = DDS_TYPE_CODE_PRINT_KIND_IDL;
break;
default:
#if DEBUG
printf("Invalid Print Complete Kind (NOT XML OR ILD)\n");
#endif
return -1;
}
DDS_TypeCode_print(type_code, &format, &ex);
DDS_TypeCodeFactory_delete_tc(DDS_TypeCodeFactory_get_instance(), (DDS_TypeCode *)type_code, &ex);
DDS_TypeObjectFactory_delete_typeobject(type_object_factory, type_object);
DDS_TypeObjectFactory_delete(type_object_factory);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment