Ada Programming/Attributes/'Enum Rep


Ada. Time-tested, safe and secure.

DescriptionEdit

User_Enum_Type'Enum_Rep(Instance); where User_Enum_Type is an enumeration type and Instance is an instance of that type will return the underlying representation for that instance of the enumeration. The default representation of an enumeration is based on its position (starting at zero). However, Ada does provide language facilities for specifying the representation independently of the position. The GNAT Enum_Rep allows you to retrieve that representation. Generally in Ada enumerations are their own type and the representation is not important. However, in the interests of cross language compatibility and for possible use in embedded programming the representation can be manipulated. While the core language allows you to change the representation, it doesn't provide a convenient attribute for retrieving it. This extended attribute addresses that need. Using it does require that you know the underlying type used to support the enumeration, since Enum_Rep returns that type. Typically, the standard type Integer is sufficient.

The standard and portable way to get the internal representation is using an instantiation of Unchecked_Conversion.

ExampleEdit

type Enum_Type is (Enum1, Enum2, Enum3);
Enum_Val : Enum_Type  := Enum1;
pragma Assert (Enum_Type'Enum_Rep(Enum_Val) = 0);  -- OK

See alsoEdit

WikibookEdit

External linksEdit

GNAT Reference Manual > Implementation Defined Attributes > Enum_Rep