Chapter 6 Customizing the Generated Code

Code Generator allows you to customize the generated code for different languages by changing the provided templates. This version does not allow you to create new output files.

You can load new templates using the following command in an existing template, where <pathToTemplate> is the absolute path to the folder with the customized template:

#parse(“<pathToTemplate>/template.vm”) 

If that template.vm file contains macros, you can use it within the original template. If template.vm contains just plain text without macros, that text will be included directly in the original file.

You can also load new templates that contain macros by adding these templates to a folder named “macros,” as follows:

<NDDSHOME>/resource/app/app_support/rtiddsgen/templates/<lang>/macros/

All the templates must have the extension .vm. The velocity engine will load them. These macros can then be called from the templates in <NDDSHOME>/resource/app/app_support/rtiddsgen/templates/<lang>/.

For example, if you have a template that contains macros to generate Python code called myMacros.vm, you could move this template to the following path:

<NDDSHOME>/resource/app/app_support/rtiddsgen/templates/py/macros/myMacros.vm

You can now use all the macros defined in myMacros.vm from the templates type.vm, typeMacros.vm, and utils.vm located in <NDDSHOME>/resource/app/app_support/rtiddsgen/templates/py/.

You can customize the behavior of a template by using the predefined set of variables provided with Code Generator. For more information, see the tables in RTI_rtiddsgen_template_variables.xlsx.

This file contains two different sheets: Language-Templates and Template variables. The Language-Template sheet shows the correspondence between the Velocity Templates used and the generated files for each language. If, for example, we want to add a method in C in the Hello.c file, we would need to modify the template typeBody.vm under the templates/c directory.

The scope of a template can be:

  • type: If we generate a file with that template for each type in the IDL file. For example in Java, where we generate a TypeSupport file for each type in the IDL.
  • file: If we generate a file with that template for each IDL file. For example in C, we generate a single plugin file containing all the types Plugin information.
  • lastTopLevelType: If we generate a file with that template for the last top-level type in the IDL file. This is commonly used for the publisher/subscriber examples.
  • module: If we generate a file with that template for each module in the IDL file. This is used in Ada, where there are files that contain all the types of a module.
  • topLevelType: if we generate a file with that template for each type in the idl file. This is used in ADA where the publisher/subscriber files are only generated for top level types

The table also shows the top_level variables that can be used for that templates. These variables are explained in the sheet Template variables. For example in Java, the main unit of variables are the constructMap which is a hashMap of variables that represent a type. In C, we will have as the main unit the constructMapList, which is a List of constructMap. In the Template variables sheet, we can see which variables are contained in each constructMap, the constructKind or type that it is applicable to and the value that it contains depending on the language we use.

One important variable that contains the constructMap for a type is the memberFieldMapList. This list represent the members contained within the type. Each member is also represented as a hashMap whose variables are also described in the Template variables sheet.

Apart from that there are environmental or general variables that are not related with the types that are defined within a hashMap called envMap.

Let’s see how to use these variables with an example. Suppose we want to generate a method in C that prints the members for a structure and, if it is an array or sequence, its corresponding size. For this IDL:

module Mymodule{  
	struct MyStruct{    
		int32 longMember;
		int32 arrayMember [2][100];
		sequence<char,2> sequenceMember;
		sequence <int32, 5> arrayOfSequenceMember[28];    
	};
};

We want to generate this:

void MyModule_MyStruct_specialPrint(){
   printf(" longMember  \n");
   printf(" arrayMember  is an array  [2, 100]  \n ");
   printf(" sequenceMember   is a sequence <2> \n");
   printf(" arrayOfSequenceMember  is an array  [28]  is a sequence <5> ");
}

The code in the template would look like this:

## We go through all the list of types
#foreach ($node in $constructMapList)
##We only want the method for structs
#*--*##if ($node.constructKind.equals(“struct”))
void ${node. nativeFQName}_specialPrint(){
##We go through all the members and call to the macros that check if they are array or sequences
#*----*##foreach($member in $node.memberFieldMapList)
print("$member.name #isAnArray($member) #isASeq($member) \n");
#*----*##end
}
#*--*##end
#end

The isAnArray macro checks if the member is an array (i.e, has the variable dimensionList) and in that case, prints it:

#macro (isAnArray $member)
#if($member.dimensionList) is an array $member.dimensionList #end
#end

The isASeq macro checks if the member is a sequence (i.e, has the variable seqSize) and in that case, prints it:

#macro (isASeq $member)
#if($member.seqSize) is a sequence <$member.seqSize> #end
#end

You can add new variables to the templates using the -V <name> [=<value>] command-line option when starting Code Generator. This variable will be added to the userVarList hashMap. You can refer to it in the template as $userVarList.name or $userVarList.name.equals(value).

For more information on velocity templates, see https://velocity.apache.org/engine/1.5/user-guide.html.