Enums with negative value (-1) initialization

2 posts / 0 new
Last post
Offline
Last seen: 5 years 5 months ago
Joined: 03/25/2015
Posts: 33
Enums with negative value (-1) initialization

Hi,

Due to some reasons, I need to have the enum value start with -1.

But, this is giving me compilation errors this way:

/vobs/product-soup/common/dds/rti-dds/5.0.0/x64Linux/ndds.5.0.0/scripts/rtiddsgen -language C++ -replace -namespace -d . MyIdl.idl
Running rtiddsgen version 5.0.0.12, please wait ...
MyIdl.idl:20:19:unexpected token: -
Done (failures)

Line #20 in my IDL is where I defined by negative enum value:
VALUE_NONE = -1

Is there a way to overcome this compilation error?

Uday

Organization:
Gerardo Pardo's picture
Offline
Last seen: 1 week 2 days ago
Joined: 06/02/2010
Posts: 602

Hello Uday,

IDL union do not support negative values. In fact in classic OMG IDL itself the values cannot be specified. The assignment of explicit values is an RTI extension, which has been incorporated into the just released OMG IDL 4.0 specifiation.

However negative values would break some of the language mappings. In Java, for example, negative integers are not supported. So for this reason the enumerated values are currently restricted to being non-nengative ints.

As an alternative you can define a set of constants and put them into a module. For example you could define the IDL like this:

module MyEnum {
    const long ONE = 1;
    const long TWO = 2;
    const long NEG_THREE = -3;
};

struct Foo {
    long enum_val;
    string<255> string_data;
};

 Then in your code if you have an object of type Foo. You can set the value as follows:

Foo foo_data;

foo_data.enum_val = MyEnum::NEG_THREE;  

Which has reasonable syntax in C++. In C This would be mapped to something like:

foo_data.enum_val = MyEnum_NEG_THREE; 

-Gerardo