Qs re 'Write & 'Read 
Author Message
 Qs re 'Write & 'Read

Is the default 'Write on a non null access type supposed to write the .all
and a 'Read supposed to do an allocate and read in the .all value?
Same questions for generall access types.


Sat, 26 Nov 2005 14:11:04 GMT  
 Qs re 'Write & 'Read

Quote:

> Is the default 'Write on a non null access type supposed to write the .all
> and a 'Read supposed to do an allocate and read in the .all value?
> Same questions for generall access types.

The answer in any case is, unfortunately, no. It is the value of the access
type itself wich is written.

Rodrigo



Sat, 26 Nov 2005 20:53:05 GMT  
 Qs re 'Write & 'Read

Quote:

> Is the default 'Write on a non null access type supposed to write the .all
> and a 'Read supposed to do an allocate and read in the .all value?
> Same questions for generall access types.

No.  The actual access value is written to the stream -- not very useful.

However, I have been using stream attribute override procedures like these
for years:

   -----------------------------
   -- Translation_Access_Read --
   -----------------------------

   procedure Translation_Access_Read
     (The_Stream_Access : access Ada.Streams.Root_Stream_Type'Class;
      The_Translation_Access : out Translation_Access)
   is
      Is_Non_Null : Boolean;
   begin
      Boolean'Read (The_Stream_Access, Is_Non_Null);
      if Is_Non_Null then
         The_Translation_Access := new Translation'
           (Translation'Input (The_Stream_Access));
      else
         The_Translation_Access := null;
      end if;
   end Translation_Access_Read;

   ------------------------------
   -- Translation_Access_Write --
   ------------------------------

   procedure Translation_Access_Write
     (The_Stream_Access : access Ada.Streams.Root_Stream_Type'Class;
      The_Translation_Access : Translation_Access)
   is
   begin
      if The_Translation_Access /= null then
         Boolean'Write (The_Stream_Access, True);
         Translation'Output
           (The_Stream_Access, The_Translation_Access.all);
      else
         Boolean'Write (The_Stream_Access, False);
      end if;
   end Translation_Access_Write;

The idea is that in a similar fashion to the way that for unconstrained
string types
the string itself is preceded by its contstraint, access types are written
to the
stream with a boolean value preceding the data (if any).

It surely seemed to me like the language standard could have defined
something
like this as the default for access types, but alas, it did not.

I'm not sure why i didn't make these procedures as part of a generic package
to
be instantiated with two parameters -- the data type and its access type,
but
this is what I've been using, lo these several years.

Quote:
> _______________________________________________
> comp.lang.ada mailing list

> http://ada.eu.org/mailman/listinfo/comp.lang.ada



Sun, 27 Nov 2005 01:55:37 GMT  
 Qs re 'Write & 'Read
Quote:
>the string itself is preceded by its contstraint, access types are written
>to the stream with a boolean value preceding the data (if any).

  Sounds right.

Quote:
>It surely seemed to me like the language standard could have defined
>something like this as the default for access types, but alas, it did not.

  I can see there would be difficulties for general access types, but
heap access types seem easy enough.  Unless storage pools cause
difficulties.


Sun, 27 Nov 2005 03:09:48 GMT  
 Qs re 'Write & 'Read

Quote:

> >the string itself is preceded by its contstraint, access types are
written
> >to the stream with a boolean value preceding the data (if any).
>   Sounds right.

> >It surely seemed to me like the language standard could have defined
> >something like this as the default for access types, but alas, it did
not.
>   I can see there would be difficulties for general access types, but
> heap access types seem easy enough.  Unless storage pools cause
> difficulties.

Maybe my mind's just in a fog today, but please explain why there'd be
a problem with any kind of access type -- general, or not, using my
approach.
Quote:
> _______________________________________________
> comp.lang.ada mailing list

> http://ada.eu.org/mailman/listinfo/comp.lang.ada



Sun, 27 Nov 2005 05:30:46 GMT  
 Qs re 'Write & 'Read
Quote:
>Maybe my mind's just in a fog today, but please explain why there'd be
>a problem with any kind of access type -- general, or not, using my
>approach.

  Aliasing.
  type phs is access integer;
  type pgs is access all integer;
  type r is record
    i : aliased integer;
    ph : phs;
    pg : pgs;
  end record;
  x : r;
begin
  x.ph := new integer;
  x.pg := x.i'access;
but on reading it back in, you can tell both x.ph and x.pg are non-null,
but you don't know whether x.pg should allocate from the heap or point
to some existing (stack) object.


Sun, 27 Nov 2005 07:26:11 GMT  
 Qs re 'Write & 'Read

Quote:

> >Maybe my mind's just in a fog today, but please explain why there'd be
> >a problem with any kind of access type -- general, or not, using my
> >approach.
>   Aliasing.
>   type phs is access integer;
>   type pgs is access all integer;
>   type r is record
>     i : aliased integer;
>     ph : phs;
>     pg : pgs;
>   end record;
>   x : r;
> begin
>   x.ph := new integer;
>   x.pg := x.i'access;
> but on reading it back in, you can tell both x.ph and x.pg are non-null,
> but you don't know whether x.pg should allocate from the heap or point
> to some existing (stack) object.

Well, for the life of me, I can't see why anyone would need to do what
your example does.  If, indeed, an access component of a record designates
an aliased component, then it seems to me that one would need an overridden
set of stream attributes for the whole record.  Writing r.pg to the stream
would be meaningless, since it designates data already written to the
stream.

The read attribute would simply make the assignment r.pg = r.i'access, after
having read r.i from the stream.

- Show quoted text -

Quote:
> _______________________________________________
> comp.lang.ada mailing list

> http://ada.eu.org/mailman/listinfo/comp.lang.ada



Sun, 27 Nov 2005 09:31:36 GMT  
 Qs re 'Write & 'Read

Quote:


> > >the string itself is preceded by its contstraint, access types are
> written
> > >to the stream with a boolean value preceding the data (if any).
> >   Sounds right.

> > >It surely seemed to me like the language standard could have defined
> > >something like this as the default for access types, but alas, it did
> not.
> >   I can see there would be difficulties for general access types, but
> > heap access types seem easy enough.  Unless storage pools cause
> > difficulties.
> Maybe my mind's just in a fog today, but please explain why there'd be
> a problem with any kind of access type -- general, or not, using my
> approach.

Cyclic self-referential data structures. It's clearly out of the
standard's business; it's up to your application to define how
a graph (cyclic or not, tree-shaped or not) is to be written to
and read from a stream.

--
-----------------------------------------------------------------



Mon, 28 Nov 2005 03:19:47 GMT  
 Qs re 'Write & 'Read

Quote:



>>>>the string itself is preceded by its contstraint, access types are

>>written

>>>>to the stream with a boolean value preceding the data (if any).

>>>  Sounds right.

>>>>It surely seemed to me like the language standard could have defined
>>>>something like this as the default for access types, but alas, it did

>>not.

>>>  I can see there would be difficulties for general access types, but
>>>heap access types seem easy enough.  Unless storage pools cause
>>>difficulties.

>>Maybe my mind's just in a fog today, but please explain why there'd be
>>a problem with any kind of access type -- general, or not, using my
>>approach.

> Cyclic self-referential data structures. It's clearly out of the
> standard's business; it's up to your application to define how
> a graph (cyclic or not, tree-shaped or not) is to be written to
> and read from a stream.

   I would not say that it is totally outside standard's business... It
clearly adds complexity to the compiler/run-time, but Java does that
automatically (and it detects circularities).

Rodrigo



Mon, 28 Nov 2005 18:12:24 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Pb with 'Write and 'Read consistency

2. Unformatted read's and write's

3. Unformatted read's and write's

4. posix.listdir() and '.'&'..'

5. Reporter Write Can't read Group

6. Newbie Qs: startup time, Module.constants, chdir, Ruby distro's library

7. Writing and reading to/from Com1 under Win'95

8. Can't read and write to a process pipeline

9. Read (no 'into') write (no 'from')

10. Summer '87 Mouse clicks & GET/READ

11. Writing 'TSR' with CW2003

12. Writing 'strange' characters

 

 
Powered by phpBB® Forum Software