Ada Programming/Attributes/'Enum Rep

From Wikibooks, open books for an open world
Jump to navigation Jump to search

float

Description[edit]

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 from 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.

Example[edit]

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

See also[edit]

Wikibook[edit]