Using rtiddsgen template files

4 posts / 0 new
Last post
Offline
Last seen: 10 months 1 week ago
Joined: 09/07/2018
Posts: 10
Using rtiddsgen template files

Hi,

I am trying to change the default CPP behavior of rtiddsgen for:

file.idl:  const string str_name = "my string";

The default is to create the following code in the header file:

file.h:  static const DDS_Char * str_name= "my string";

But this creates a new static string in every object the header file is included and also gives warnings in every object that it's not used.

The way it should be done, is to create a single const string in the body with a corresponding extern statement in the header so there is only a single instance of the string within the executable.

file.cpp: const DDS_Char * str_name= "my string";

file.h: extern const DDS_Char * str_name;

I read in the Code Generator manual that the rtiddsgen templates can be modified to accomplish this. So I modified typeBody.vm and typeHeader.vm in the resource/app/app_support/rtiddsgen/templates/cpp03 directory but when I run rtiddsgen, it doesn't seem to use my modified .vm files.  Is there something I'm missing?

Any help would be appreciated.

Thanks,

Chris

 

Offline
Last seen: 10 months 1 week ago
Joined: 09/07/2018
Posts: 10

I should add that I'm using RTI DDS Ver 5.2.3

ajimenez's picture
Offline
Last seen: 2 years 6 months ago
Joined: 09/29/2017
Posts: 21

Hi Chris,

It is a known issue, with ID CODEGENII-873, you will see it in the following release.

You are correct, const string is not generated correctly.
According to the OMG specification "C++ Language Mapping" the mapping of "const string" from IDL to C++ should be:

 // IDL
 const string name = "testing";

  // C++
 static const char *const name = "testing";
 

 In the case that you want to modify the template in the meantime. You will have to do the following changes in resource/app/app_support/rtiddsgen/templates/c/typeMacros.vm:

 ##------------------------------------------------------------------------------
 ## Macro to define constanst
 ##------------------------------------------------------------------------------
 #macro(declareConstant $member)
 #*--*##if($envMap.language=="C")
 ${envMap.POUND_CHAR}define ${member.nativeFQNameInModule} (${member.value})
 #*--*##elseif($envMap.language.equals("Cpp"))
 #*----*##if($member.nativeTypeFQName == "DDS_Char *")
 static const $member.nativeTypeFQName const ${member.nativeFQNameInModule}= ${member.value};
 #*----*##elseif($member.nativeTypeFQName == "DDS_Wchar *")
 static const DDS_Wchar ${member.nativeFQNameInModule}[] = ${member.value};
 #*----*##else
 static const $member.nativeTypeFQName ${member.nativeFQNameInModule}= ${member.value};
 #*----*##end
 #*--*##end
 #end

Please let me know if it works for you.

Best,
Antonio

Offline
Last seen: 10 months 1 week ago
Joined: 09/07/2018
Posts: 10

Hi Antonio,

I made the change you suggested but unfortunately it doesn't fix the warning issue, and I didn't expect it to.

However, by changing "static" to "extern" in the typeMacros.vm file and removing the =$() part, I got half of my fix, meaning the header now has what I want.

File.h: extern const DDS_Char * str_name;

Now I just need a way to add the corresponding declaration to the body (file.cpp) so that it contains the following line:

File.cpp: const DDS_Char * str_name= "my string";

Can you help me with this?

Thanks,

Chris