How to use generated enums with rtiddsgen (modern c++) in code

7 posts / 0 new
Last post
Offline
Last seen: 11 months 3 weeks ago
Joined: 08/13/2014
Posts: 55
How to use generated enums with rtiddsgen (modern c++) in code

Hi

I want to know whether it is possible to use enums generated from "rtiddsgen" command directly in my code or not. For example, I use 

enum PrimitiveEnum { ENUM1, ENUM2, ENUM3 };

 in my IDL and also I want to use 

PrimitiveEnum::ENUM1 or ENUM1 

in my code. I don't want to use them like below:

PrimitiveEnum_def myEnum

 myEnum.ENUM1 

Thanks in advance for your help.

Bonjefir

Offline
Last seen: 4 days 17 hours ago
Joined: 04/02/2013
Posts: 145

Hi,

That's exactly the way you should use your enums:

PrimitiveEnum my_enum = PrimitiveEnum::ENUM1;

 You should ignore PrimitiveEnum_def—it's just an implementation detail.

Did you see any compilation error when using PrimitiveEnum?

Alex

Offline
Last seen: 11 months 3 weeks ago
Joined: 08/13/2014
Posts: 55

Hi Alex

Let me clear the case. Here is my scenario: I have a file called "projectEnums.idl" which use from its enums in all other IDL files by including "projectEnums.idl" in them. Also I want to use the enums in "projectEnums.idl" directly in my code. So I decide to include "projectEnums.hpp", generated by "rtiddsgen", in my code and use the enums within it. But I cannot use this way and get the following compile error: "using typedef-name after ..." I don't know exactly why "rtiddsgen" command changes enum to a enum in a structure and what should I do to solve this probelm? I know that I can define another header file exactly same as "projectEnums.idl" but I don't want to do that (until I have to!!!) because of the possible changes in my enums and possible double changes in the related files.

Another possible solution is that to include only one file called "projectEnums.h" in all other IDL files without translation by "rtiddsgen" command, but this solution is also bad because I think this will be problematic in case of recording data by "Recording Service" and this service will not be able to recognize the type of IDL (I am not sure about this)

I hope this will clarify the problem.  Thanks in advance for your help.

Bonjefir

Offline
Last seen: 4 days 17 hours ago
Joined: 04/02/2013
Posts: 145

Can you paste here the code where you get that compilation error?

I just tested this IDL:

enum MyEnum { VALUE1, VALUE2, VALUE3};

And this C++ code:

MyEnum my_enum = MyEnum::VALUE2;

And it compiles fine.

Offline
Last seen: 11 months 3 weeks ago
Joined: 08/13/2014
Posts: 55

Dear Alex

Sorry for my late response. The following code is absolutely correct and working fine:

enum MyEnum { VALUE1, VALUE2, VALUE3};

MyEnum my_enum = MyEnum::VALUE2;

But I want to have a code like below:

MyEnum my_enum = VALUE2;

I think "rtiddsgen" convert "enums" in IDL to "enum classes" and that's why I cannot use the above code. I used from -language option in rtiddsgen but it  did not work. Is there anyway to solve this?

Thanks in advance for your help.

Bonjefir

Offline
Last seen: 4 days 17 hours ago
Joined: 04/02/2013
Posts: 145

Hi,

I understand now. The Modern C++ API (-language C++03 and -language C++11), as opposed to the Traditional API (-language C++), explicitly discourages what you're trying to do. It avoids name conflicts by declaring enumeration inside a type and not in the global namespace.

I don't think there's any workaround.  Why do you prefer using VALUE1 instead of MyEnum::VALUE1?

Alex

 

Offline
Last seen: 11 months 3 weeks ago
Joined: 08/13/2014
Posts: 55

Hi Alex,

I have a lot of old codes which use enums in this way and I want to use them in my new application which using DDS. Now I have to change all of them :( 

Anyway thanks for your nice answers. 

Best regards,

Bonjefir