comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: Behavior of Stream Attributes On Access Types.
Date: Tue, 11 Jun 2002 22:44:13 -0500
Date: 2002-06-11T22:44:13-05:00	[thread overview]
Message-ID: <mailman.1023853382.7723.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: ae2v7f$r12$1@nh.pace.co.uk


----- Original Message -----
From: "Marin David Condic ]" <dont.bother.mcondic.auntie.spam@[acm.org>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: June 10, 2002 2:38 PM
Subject: Behavior of Stream Attributes On Access Types.


> I'm checking section 13.13.2 of the ARM to discover the default behavior of
> the Stream 'Read, 'Write, 'Input and 'Output when they encounter an access
> type. I'm not finding any clear indication of what gets put to the stream -
> although there is a mention in paragraph 35 of raising Constraint_Error on
> input if the value is not of its subtype. Can someone clarify the behavior?
>
> My inclination is to think that the sensible thing would be to call the
> 'Read or 'Write for the thing pointed to by the access type, but this has
> implications for dynamically allocated objects. Will it write/read a
> (totally useless) access value?
>
Here is an example of how I do this in a number of ADT packages I have
developed.

The complete file with appropriate copyright notice, and ratioonale, etc.,
is available at
ftp.ada95.com/pub/access_object_stream_attributes.tgz or
ftp.ada95.com/pub/access_object_stream_attributes.tar.gz

with Ada.Streams.Stream_IO;
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
procedure Test_Access_Object_Stream_Attributes
is
   type Node;

   type Node_Access is access all Node;

   procedure Node_Access_Read
      (Stream : access Ada.Streams.Root_Stream_Type'Class;
       Item   :    out Node_Access);

   procedure Node_Access_Write
      (Stream : access Ada.Streams.Root_Stream_Type'Class;
       Item   : in     Node_Access);

   type Node is record
      Item : Integer;
      Next : Node_Access;
   end record;

   for Node_Access'Read use Node_Access_Read;

   for Node_Access'Write use Node_Access_Write;

   procedure Node_Access_Read
     (Stream : access Ada.Streams.Root_Stream_Type'Class;
      Item   :    out Node_Access)
   is
      Is_Non_Null : Boolean;
   begin
      Boolean'Read (Stream, Is_Non_Null);
      if Is_Non_Null then
         Item := new Node'(Node'Input (Stream));
      else
         Item := null;
      end if;
   end Node_Access_Read;

   procedure Node_Access_Write
     (Stream : access Ada.Streams.Root_Stream_Type'Class;
      Item   : in     Node_Access)
   is
   begin
      Boolean'Write (Stream, Item /= null);
      if Item /= null then
         Node'Output (Stream, Item.all);
      end if;
   end Node_Access_Write;

   --   NOTE:  Since the Read procedure is an allocator, it is the
   --   responsibility of the caller to deallocate the memory.
   --   To this end, a Deallocate procedure is provided.
   procedure Deallocate (Item : in out Node_Access)
   is
      procedure Free is new Ada.Unchecked_Deallocation
        (Object => Node,
         Name   => Node_Access);
   begin
      if Item.Next /= null then
         -- Not a leaf node; first deallocate node's children,
         -- then deallocate this node
         Deallocate (Item.Next);
         Deallocate (Item);
      else
         -- Leaf node; free the storage
         Free (Item);
      end if;
   end Deallocate;

   Head : Node_Access;
   Tail : Node_Access;
   Next : Node_Access;

   File   : Ada.Streams.Stream_IO.File_Type;
   Stream : Ada.Streams.Stream_IO.Stream_Access;

begin
   -- Link together a few nodes
   for N in 1 .. 10 loop
      if N = 1 then
         Tail := new Node;
         Head := Tail;
      else
         Tail.Next := new Node;
         Tail := Tail.Next;
      end if;
      Tail.Item := N;
   end loop;

   -- Verify the list
   Ada.Text_IO.Put_Line ("Original populated list:");
   Next := Head;
   while next  /= null loop
      Ada.Text_IO.Put_Line (Next.Item'Img);
      Next := Next.Next;
   end loop;

   -- Write the list to a stream file

   -- Create the file
   Ada.Streams.Stream_IO.Create
     (File => File,
      Mode => Ada.Streams.Stream_IO.Out_File,
      Name => "nodes.dat");

   -- Associate a stream with the file
   Stream := Ada.Streams.Stream_IO.Stream (File);

   -- Write the list
   Node_Access'Output (Stream, Head);

   -- Close the file
   Ada.Streams.Stream_IO.Close (File);

   -- Destroy the list
   Deallocate (Head);

   -- Verify the list
   Ada.Text_IO.Put_Line
     ("List after deallocation (should be empty):");
   Next := Head;
   while next  /= null loop
      Ada.Text_IO.Put_Line (Next.Item'Img);
      Next := Next.Next;
   end loop;

   -- Read the list from a stream file

   -- Open the file
   Ada.Streams.Stream_IO.Open
     (File => File,
      Mode => Ada.Streams.Stream_IO.In_File,
      Name => "nodes.dat");

   -- Associate a stream with the file
   Stream := Ada.Streams.Stream_IO.Stream (File);

   -- Read the list
   Head := Node_Access'Input (Stream);

   -- Close the file
   Ada.Streams.Stream_IO.Close (File);

   -- Verify the list
   Ada.Text_IO.Put_Line
     ("List after reading file:");
   Next := Head;
   while next  /= null loop
      Ada.Text_IO.Put_Line (Next.Item'Img);
      Next := Next.Next;
   end loop;

end Test_Access_Object_Stream_Attributes;









      parent reply	other threads:[~2002-06-12  3:44 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-10 19:38 Behavior of Stream Attributes On Access Types Marin David Condic
2002-06-11  5:33 ` R. Tim Coslet
2002-06-11 14:15   ` Marin David Condic
2002-06-11 13:47 ` Ted Dennison
2002-06-11 14:27   ` Marin David Condic
2002-06-11 14:37     ` Marin David Condic
2002-06-12 14:19       ` David C. Hoos
2002-06-12 15:18         ` Marin David Condic
2002-06-13  3:00           ` David C. Hoos, Sr.
2002-06-14 18:27           ` Simon Wright
2002-06-14 18:53             ` Marin David Condic
2002-06-15 14:56               ` Simon Wright
2002-06-16  2:27                 ` Randy Brukardt
2002-06-17 14:31                   ` Marin David Condic
2002-06-18 19:30                     ` Randy Brukardt
2002-06-12 19:39       ` Randy Brukardt
2002-06-12 13:31     ` Ted Dennison
2002-06-11 21:56   ` Randy Brukardt
2002-06-12  3:44 ` David C. Hoos, Sr. [this message]
replies disabled

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