Re: I/O streaming with custom data transport
- From: Maciej Sobczak <no.spam@xxxxxxxxxxx>
- Date: Wed, 22 Nov 2006 10:16:56 +0100
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/
.
- Follow-Ups:
- Re: I/O streaming with custom data transport
- From: Robert A Duff
- Re: I/O streaming with custom data transport
- From: Alex R. Mosteo
- Re: I/O streaming with custom data transport
- References:
- I/O streaming with custom data transport
- From: Maciej Sobczak
- Re: I/O streaming with custom data transport
- From: Alex R. Mosteo
- I/O streaming with custom data transport
- Prev by Date: Re: Building GNAT/GCC 4.2.0
- Next by Date: Re: I/O streaming with custom data transport
- Previous by thread: Re: I/O streaming with custom data transport
- Next by thread: Re: I/O streaming with custom data transport
- Index(es):
Relevant Pages
|