comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <no.spam@no.spam.com>
Subject: Re: I/O streaming with custom data transport
Date: Wed, 22 Nov 2006 10:16:56 +0100
Date: 2006-11-22T10:16:56+01:00	[thread overview]
Message-ID: <ek14i8$k0g$1@cernne03.cern.ch> (raw)
In-Reply-To: <4sgsgoFvn3caU1@mid.individual.net>

Alex R. Mosteo wrote:

>> I'm looking for something like this in Ada.
>>
>> The basic I/O facilities in the standard library don't seem to provide
>> anything like this.
>> I hoped that Ada.Streams allows this by subclassing Root_Stream_Type and
>> providing some overriding operations, but unfortunately I cannot even
>> find the specification of Root_Stream_Type (looks like there isn't any
>> and this type is just a name placeholder in ARM).
> 
> I think you haven't looked right.

Indeed - it's in 13.13.1.

> That's precisely how it's done.

> And you do this just as you say: you extend
> Ada.Streams.Root_Stream_Type and provide the read/write subprograms.

And I actually managed to do this.
Consider the following example of custom stream that for the sake of 
presentation is bound to standard IO and just converts characters to 
uppercase when writing (the point is that if I can do *this*, I can do 
anything else):

-- file my_streams.ads:
with Ada.Streams;
package My_Streams is
    use Ada.Streams;

    type My_Stream is new Root_Stream_Type with null record;

    procedure Read(Stream : in out My_Stream;
                   Item : out Stream_Element_Array;
                   Last : out Stream_Element_Offset);

    procedure Write(Stream : in out My_Stream;
                    Item : in Stream_Element_Array);
end My_Streams;

-- file my_streams.adb:
with Ada.Text_IO.Text_Streams;
with Ada.Characters.Handling;
package body My_Streams is
    use Ada.Text_IO;
    use Ada.Text_IO.Text_Streams;

    Std_Stream : Stream_Access := Stream(Current_Output);

    procedure Read(Stream : in out My_Stream;
                   Item : out Stream_Element_Array;
                   Last : out Stream_Element_Offset) is
    begin
       -- forward to standard streams:
       Read(Std_Stream.all, Item, Last);
    end Read;

    procedure Write(Stream : in out My_Stream;
                    Item : in Stream_Element_Array) is
       Item_Uppercase : Stream_Element_Array := Item;
       C : Character;
    begin
       for I in Item_Uppercase'Range loop
          C := Character'Val(Item_Uppercase(I));
          C := Ada.Characters.Handling.To_Upper(C);
          Item_Uppercase(I) := Stream_Element(Character'Pos(C));
       end loop;

       -- forward to standard streams:
       Write(Std_Stream.all, Item_Uppercase);
    end Write;
end My_Streams;

-- file hello.adb:
with Ada.Text_IO.Text_Streams;
with My_Streams;

procedure Hello is
    use Ada.Text_IO;
    use Ada.Text_IO.Text_Streams;
    use My_Streams;

    procedure Write_Hello(Stream : in out Stream_Access) is
    begin
       String'Write(Stream, "Hello");
    end Write_Hello;

    S1 : Stream_Access := Stream(Current_Output);
    S2 : Stream_Access := new My_Stream;
begin
    Write_Hello(S1);
    New_Line;
    Write_Hello(S2);
    New_Line;
end Hello;

The result is:

$ ./hello
Hello
HELLO
$

The point here is that the Write_Hello procedure can be reused with 
various streams, just like my foo functions in C++ (from initial post).

Is the code above correct? Any traps or problems that I don't see at the 
moment?


-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



  reply	other threads:[~2006-11-22  9:16 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-11-21 15:11 I/O streaming with custom data transport Maciej Sobczak
2006-11-21 15:28 ` gautier_niouzes
2006-11-21 17:51 ` Alex R. Mosteo
2006-11-22  9:16   ` Maciej Sobczak [this message]
2006-11-22 10:01     ` Alex R. Mosteo
2006-11-22 10:39       ` Maciej Sobczak
2006-11-22 16:06         ` Dmitry A. Kazakov
2006-11-22 16:30         ` Alex R. Mosteo
2006-11-23  5:48         ` Simon Wright
2006-11-22 13:50     ` Robert A Duff
2006-11-22 14:37       ` Maciej Sobczak
2006-11-22 16:11         ` Georg Bauhaus
2006-11-21 19:02 ` Dmitry A. Kazakov
2006-11-22  9:21   ` Maciej Sobczak
2006-11-22 10:31     ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox