A.10.10 Input-Output for Enumeration Types
Static Semantics
The following procedures are defined in the generic
package Enumeration_IO, which has to be instantiated for the appropriate
enumeration type (indicated by Enum in the specification).
Values are output using either upper or lower case
letters for identifiers. This is specified by the parameter Set, which
is of the enumeration type Type_Set.
type Type_Set is (Lower_Case, Upper_Case);
The format (which includes
any trailing spaces) can be specified by an optional field width parameter.
The default field width and letter case are defined by the following
variables that are declared in the generic package Enumeration_IO:
Default_Width : Field := 0;
Default_Setting : Type_Set := Upper_Case;
The following procedures
are provided:
procedure Get(File : in File_Type; Item : out Enum);
procedure Get(Item : out Enum);
After skipping any leading blanks, line terminators,
or page terminators, reads an identifier according to the syntax of this
lexical element (lower and upper case being considered equivalent), or
a character literal according to the syntax of this lexical element (including
the apostrophes). Returns, in the parameter Item, the value of type Enum
that corresponds to the sequence input.
The exception Data_Error
is propagated if the sequence input does not have the required syntax,
or if the identifier or character literal does not correspond to a value
of the subtype Enum.
procedure Put(File : in File_Type;
Item : in Enum;
Width : in Field := Default_Width;
Set : in Type_Set := Default_Setting);
procedure Put(Item : in Enum;
Width : in Field := Default_Width;
Set : in Type_Set := Default_Setting);
Outputs the value
of the parameter Item as an enumeration literal (either an identifier
or a character literal). The optional parameter Set indicates whether
lower case or upper case is used for identifiers; it has no effect for
character literals. If the sequence of characters produced has fewer
than Width characters, then trailing spaces are finally output to make
up the difference. If Enum is a character type, the sequence of characters
produced is as for Enum'Image(Item), as modified by the Width and Set
parameters.
procedure Get(From : in String; Item : out Enum; Last : out Positive);
Reads an enumeration value from the beginning
of the given string, following the same rule as the Get procedure that
reads an enumeration value from a file, but treating the end of the string
as a file terminator. Returns, in the parameter Item, the value of type
Enum that corresponds to the sequence input. Returns in Last the index
value such that From(Last) is the last character read.
The exception Data_Error
is propagated if the sequence input does not have the required syntax,
or if the identifier or character literal does not correspond to a value
of the subtype Enum.
procedure Put(To : out String;
Item : in Enum;
Set : in Type_Set := Default_Setting);
Outputs the value
of the parameter Item to the given string, following the same rule as
for output to a file, using the length of the given string as the value
for Width.
Although the specification of the generic package
Enumeration_IO would allow instantiation for an integer type, this is
not the intended purpose of this generic package, and the effect of such
instantiations is not defined by the language.
NOTE 1 There is a difference between
Put defined for characters, and for enumeration values. Thus
Ada.Text_IO.Put('A'); -- outputs the character A
package Char_IO is new Ada.Text_IO.Enumeration_IO(Character);
Char_IO.Put('A'); -- outputs the character 'A', between apostrophes
NOTE 2 The type Boolean is an enumeration
type, hence Enumeration_IO can be instantiated for this type.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe