Contents Index Previous Next
A.10.10 Input-Output for Enumeration Types
Static Semantics
1
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).
2
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.
3
type Type_Set is (Lower_Case, Upper_Case);
4
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:
5
Default_Width : Field := 0;
Default_Setting : Type_Set := Upper_Case;
6
The following procedures
are provided:
7
procedure Get(File : in File_Type; Item : out Enum);
procedure Get(Item : out Enum);
8
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.
9
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.
10
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);
11
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.
11.a
Discussion: For a character
type, the literal might be a Wide_Character or a control character. Whatever
Image does for these things is appropriate here, too.
12
procedure Get(From : in String; Item : out Enum; Last : out Positive);
13
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.
14
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.
14.a
To be honest: For a character
type, it is permissible for the implementation to make Get do the inverse
of what Put does, in the case of wide character_literals
and control characters.
15
procedure Put(To : out String;
Item : in Enum;
Set : in Type_Set := Default_Setting);
16
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.
17/1
{
8652/0054}
Although the specification of the generic package Enumeration_IO would allow
instantiation for an
integerfloat type, this is not the intended
purpose of this generic package, and the effect of such instantiations is not
defined by the language.
18
32 There is a difference
between Put defined for characters, and for enumeration values. Thus
19
Ada.Text_IO.Put('A'); -- outputs the character A
20
package Char_IO is new Ada.Text_IO.Enumeration_IO(Character);
Char_IO.Put('A'); -- outputs the character 'A', between apostrophes
21
33 The type Boolean is an
enumeration type, hence Enumeration_IO can be instantiated for this type.
Contents Index Previous Next Legal