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: Richard D Riehle Subject: Re: Ada.Streams examples Date: 1999/03/11 Message-ID: <7c753p$3t7@dfw-ixnews3.ix.netcom.com>#1/1 X-Deja-AN: 453614274 References: <36e61db4.50430543@news.pacbell.net> <7c64kl$r8s$1@nnrp1.dejanews.com> <36e6cd2c.3377267@news.pacbell.net> Organization: Netcom X-NETCOM-Date: Wed Mar 10 7:09:13 PM CST 1999 Newsgroups: comp.lang.ada Date: 1999-03-10T19:09:13-06:00 List-Id: There is an interesting example of how to use Streams in the GNAT compiler for Ada.Exceptions. I post a fragment from my book, Object Technology in Ada that steals liberally from the GNAT package. In the package specification for Ada.Exceptions the designer includes a context clause for Ada.Streams. The limited private type Exception_Occurrence has the full definiton as limited record. type Exception_Occurrence (Max_Length : Nat := SSL.Exception_Msg_Max) is limited record Id : Exception_Id; Msg_Length : Natural; Msg : String (1 .. Max_Length); end record; Elsewhere in the private part of the specification, two procedures are declared to provide Read and Write for Exception_Occurrence. It should be noted that 'Input and 'Output cannot be written for a limited private type since assignment is not available. It is possible to write those functions for Exception_Occurrence_Access which is already defined in the Ada.Exceptions package. For this discussion, it should be enough to cover Read and Write. procedure Exception_Occurrence_Read (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : out Exception_Occurrence); procedure Exception_Occurrence_Write (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : in Exception_Occurrence); for Exception_Occurrence'Read use Exception_Occurrence_Read; for Exception_Occurrence'Write use Exception_Occurrence_Write; Note that Exception_Occurrence is a record. Therfore, it is necessary to create Read and Write operations for it since none currently exist. Also, the creation of Read and Write procedures is not the same as overloading existing subprograms. You create a procedure with a name and the associate it with an attribute of 'Read or 'Write through a representation specification. Implementing code for Execption_Occurrence_Read and Execption_Occurrence_Write looks like this. procedure Exception_Occurrence_Read (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : out Exception_Occurrence) is Name : String := String'Input(Stream); Len : Natural := Natural'Input(Stream); Msg : String := String'Input(Stream); begin Item.Id := Exception_Id (Internal_Exception (Name)); if Msg'Length /= Len then raise Data_Error; end if; -- Silently truncate message if it does not fit Item.Msg_Length := Natural'Min (Len, Item.Max_Length); Item.Msg (1 .. Item.Msg_Length) := Msg (1 .. Item.Msg_Length); end Exception_Occurrence_Read; The reader should notice that the implementation of the Read operation takes advantage of the "Input attribute already available for Standard types. Now we show the code for implementing the 'Write. It is even easier that the code for the 'Read. procedure Exception_Occurrence_Write (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : in Exception_Occurrence) is begin String'Output (Stream, Exception_Name (Item.Id)); Natural'Output (Stream, Item.Msg_Length); String'Output (Stream, Item.Msg (1 .. Item.Msg_Length)); end Exception_Occurrence_Write; Hope this is helpful, Richard Riehle richard@adaworks.com http://www.adaworks.com