5.5 Enumerations

Another top level type that eTrice introduces is the Enumeration. Enumerations are well known from many programming languages. They basically are a lists of literals, each of which is a pair consisting of a name and an integer constant.

The assignment of integer constants is optional in eTrice. If no value is specified then the value is that of the predecessor plus one or 0 if there is no predecessor.

The default type of the enumeration is an int and depends on the target platform. But it is also possible to explicitly associate a PrimitiveType (of integer type of course) with the enumeration.

In the following listing we show a couple of examples for enumerations.

 
RoomModel EnumExample { 
 
 PrimitiveType int16: ptInteger -> short(Short) default "0" 
 PrimitiveType char: ptCharacter -> char(Char) default "" 
 
 Enumeration FirstEnum { 
   zero,// 0 
   one,// 1 
   two,// 2 
   three// 3 
 } 
 
 Enumeration SecondEnum { 
   one = 1,// 1 
   two,// 2 
   three// 3 
 } 
 
 Enumeration ThirdEnum { 
   one = 1,// 1 
   two,// 2 
   five = 5// 5 
 } 
 
 Enumeration FourthEnum { 
   one = 1,// 1 
   three = 3,// 3 
   sixtyfive = 0x41// 0x41 or 65 
 } 
 
 Enumeration FifthEnum of int16 { 
   f1 = 0x1,// 0x1 or 1 
   f2 = 0x2,// 0x2 or 2 
   f3 = 0x4,// 0x4 or 4 
   f4 = 0x8// 0x8 or 8 
 } 
 
 Enumeration WrongType of char /* <- ERROR: no integer primitive type */ { 
   c 
 } 
 
 Enumeration EmptyEnum { 
   // ERROR: no literals defined 
 } 
 
}
Listing 5.2: ROOM example code