From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,90b27c91e19c3731 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!news4.google.com!news2.volia.net!solnet.ch!solnet.ch!news-zh.switch.ch!switch.ch!cernne03.cern.ch!not-for-mail From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Re: I/O streaming with custom data transport Date: Wed, 22 Nov 2006 10:16:56 +0100 Organization: CERN News Message-ID: References: <4sgsgoFvn3caU1@mid.individual.net> NNTP-Posting-Host: abpc10883.cern.ch Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: cernne03.cern.ch 1164187016 20496 137.138.37.241 (22 Nov 2006 09:16:56 GMT) X-Complaints-To: news@@cern.ch NNTP-Posting-Date: Wed, 22 Nov 2006 09:16:56 +0000 (UTC) User-Agent: Thunderbird 1.5.0.8 (X11/20061113) In-Reply-To: <4sgsgoFvn3caU1@mid.individual.net> Xref: g2news2.google.com comp.lang.ada:7634 Date: 2006-11-22T10:16:56+01:00 List-Id: 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/