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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7876e4356325725b X-Google-Attributes: gid103376,public From: dennison@telepath.com Subject: Re: Ada.Streams examples Date: 1999/03/10 Message-ID: <7c64kl$r8s$1@nnrp1.dejanews.com>#1/1 X-Deja-AN: 453407591 References: <36e61db4.50430543@news.pacbell.net> X-Http-Proxy: 1.0 x15.dejanews.com:80 (Squid/1.1.22) for client 204.48.27.130 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Wed Mar 10 15:55:07 1999 GMT Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.5 [en] (WinNT; I) Date: 1999-03-10T00:00:00+00:00 List-Id: In article <36e61db4.50430543@news.pacbell.net>, tmoran@bix.com (Tom Moran) wrote: > Could someone please point me to a text or tutorial or something on > using Ada.Streams - in particular overriding the default 'Read etc. > Cohen's latest edition of Ada as a Second Language goes over it. Basicly, you just declare a read routine with the following spec: procedure Read (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : out Instance ); where "Instance" is your type, in the same package spec that the type ("Instance in the above case) is declared. Then, after the full declaration of the type you put the following line: for Instance'Read use Read; For the body of Read, you can call the 'Read method for the relevant field types yourself. Another option is to read in a properly sized Ada.Streams.Stream_Element_Array using the stream's Read method then use Unchecked_Conversion to convert the array type(s) you need. For instance: Bytes_Per_Element : constant := (Ada.Streams.Stream_Element'Size + 7) / 8; Bytes_Per_Instance : constant Natural := (Instance'Size + 7) / 8; Elements_Per_Header : constant Ada.Streams.Stream_Element_Offset := Ada.Streams.Stream_Element_Offset( (Bytes_Per_Instance + (Bytes_Per_Element - 1)) / Bytes_Per_Element); subtype Header_Array is Ada.Streams.Stream_Element_Array (1 .. Elements_Per_Header); function To_Instance is new Unchecked_Conversion (Source => Header_Array, Target => Instance); procedure Read (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : out Instance ) is Trash : Ada.Streams.Stream_Element_Offset; Item_Elements : Header_Array; begin Ada.Streams.Read (Stream => Stream.all, Item => Item_Elements, Last => Trash ); Item := To_Instance (Item_Elements); end Read; A wise developer would probably check "Trash". But in my case I happen to know (because I wrote them) that the streams used for this call will raise exceptions rather than return a different Last than Item_Elements'Last. If there's anything else you are curious about, just ask. T.E.D. -----------== Posted via Deja News, The Discussion Network ==---------- http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own