Ada Programming/Attributes/'Enum Rep

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

Ada. Time-tested, safe and secure.
Ada. Time-tested, safe and secure.

Description[edit | edit source]

This language feature has been introduced in Ada 2022. 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 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.

Note that this attribute is now standard in Ada 2022. In earlier Ada versions, it is available in GNAT as an implementation defined attribute, but the standard and portable way to get the internal representation was using an instantiation of Unchecked_Conversion.

Example[edit | edit source]

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 | edit source]

Wikibook[edit | edit source]

Ada Reference Manual[edit | edit source]

Ada 2022 Overview[edit | edit source]

External links[edit | edit source]

GNAT Reference Manual > Implementation Defined Attributes > Enum_Rep