Rationale for Ada 2005

John Barnes
Contents   Index   References   Search   Previous   Next 

7.8 Streams

Important improvements to the control of streams were described in the chapter on the object oriented model where we discussed the new package Ada.Tags.Generic_Dispatching_Constructor (see 2.6) and various changes to the parent package Ada.Tags itself. In this section we mention two other changes.
There is a problem with the existing stream attributes such as (see RM 13.13.2)
procedure S'Write(Stream: access Root_Stream_Type'Class; Item: in T);
where S is a subtype of T. Note that for the parameter Item, its type T is in italic and so has to be interpreted according to the kind of type. In the case of integer and enumeration types it means that the parameter Item has type T'Base.
Given a declaration such as 
type Index is range 1 .. 10;
different implementations might use different representations for Index'Base – some might use 8 bits others might use 32 bits and so on.
Now stream elements themselves are typically 8 bits and so with an 8-bit base, there will be one value of Index per stream element whereas with a 32-bit base each value of Index will take 4 stream elements. Clearly a stream written by the 8-bit implementation cannot be read by the 32-bit one.
This problem is overcome in Ada 2005 by the introduction of a new attribute Stream_Size. The universal integer value S'Stream_Size gives the number of bits used in the stream for values of the subtype S. We are guaranteed that it is a multiple of Stream_Element'Size. So the number of stream elements required will be 
S'Stream_Size / Stream_Element'Size
We can set the attribute in the usual way provided that the value given is a static multiple of Stream_Element'Size. So in the case above we can write 
for Index'Stream_Size use 8;
and portability is then assured. That is provided that Stream_Element_Size is 8 anyway and that the implementation accepts the attribute definition clause (which it should).
A minor change is that the parameter Stream of the various attributes now has a null exclusion so that S'Write is in fact 
procedure S'Write(Stream: not null access Root_Stream_Type'Class; Item: in T);
This does not introduce any incompatibilities since in Ada 95 passing null raises Constraint_Error anyway and so this change just clarifies the situation.
On this dullish but important topic here endeth the Rationale for Ada 2005 apart from various exciting appendices and the extensive Chapter 8 on containers.

Contents   Index   References   Search   Previous   Next 
© 2005, 2006, 2007 John Barnes Informatics.
Sponsored in part by:
The Ada Resource Association and its member companies: ARA Members AdaCore Polyspace Technologies Praxis Critical Systems IBM Rational Sofcheck and   Ada-Europe: