Usage of Ada.Streams 
Author Message
 Usage of Ada.Streams

Hello there,

I don't quite know how to use Ada.Streams to read Objects using
dispatching.
To be specific, I have a private tagged type, say Data, and want to
read/write Objects derived from that type. Writing works fine like this:
Data_Pointer: PData; -- an access all Data'class
Str: Stream_Access;
...
Data'Class'Write(Str, Data.all);
But I can't find a solution to read the objects equally nice. From the
Ada95 Rationale I gather I should use 'Input and 'Output somehow, but I'm
just missing an example. Could anyone help me?

Joerg
--
"Quoth the raven: Nevermore!" -- E.A.Poe



Sat, 06 Jul 2002 03:00:00 GMT  
 Usage of Ada.Streams



Quote:
> Hello there,

> I don't quite know how to use Ada.Streams to read Objects using
> dispatching.
> To be specific, I have a private tagged type, say Data, and want to
> read/write Objects derived from that type. Writing works fine like this:
> Data_Pointer: PData; -- an access all Data'class
> Str: Stream_Access;
> ...
> Data'Class'Write(Str, Data.all);
> But I can't find a solution to read the objects equally nice. From the
> Ada95 Rationale I gather I should use 'Input and 'Output somehow, but I'm
> just missing an example. Could anyone help me?

If you want to have the tags in the stream (so that the specific
types can be distinguished when reading back), you need to use:
Data'Class'Output(Str, Data_Pointer.all);

Then, to read them back, you would say:
Data_Pointer := new Data'Class'(Data'Class'Input (str));

That statement allocates memory for the object, and initializes it
with the value read from the stream.



Sun, 07 Jul 2002 03:00:00 GMT  
 Usage of Ada.Streams
David C. Hoos, Sr. a crit dans le message ...

Quote:
>If you want to have the tags in the stream (so that the specific
>types can be distinguished when reading back), you need to use:
>Data'Class'Output(Str, Data_Pointer.all);

>Then, to read them back, you would say:
>Data_Pointer := new Data'Class'(Data'Class'Input (str));

>That statement allocates memory for the object, and initializes it
>with the value read from the stream.

That's correct, but note that it is not necessary to use pointer under
Ada95.

Data'Class'Output (Str, Object);

(with Object whatever instance in the class heriachy).

New_Object : constant Data'Class := Data'Class'Input (Str);

Pascal.

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://ourworld.compuserve.com/homepages/pascal_obry
--|
--| "The best way to travel is by means of imagination"



Sun, 07 Jul 2002 03:00:00 GMT  
 Usage of Ada.Streams


Quote:
> David C. Hoos, Sr. a crit dans le message ...
> >If you want to have the tags in the stream (so that the specific
> >types can be distinguished when reading back), you need to use:
> >Data'Class'Output(Str, Data_Pointer.all);

> >Then, to read them back, you would say:
> >Data_Pointer := new Data'Class'(Data'Class'Input (str));

> >That statement allocates memory for the object, and initializes it
> >with the value read from the stream.

> That's correct, but note that it is not necessary to use pointer under
> Ada95.

Agreed.  I only used an access type because the original poster was
writing from a dereferenced access object.

Also, local declare blocks, of course, are more restricted in scope than
dynamically-allocated objects.

Which to use depends on other considerations, but I would probably use
a storage pool if dynamically allocating, especially in a long-running
program reading from, say, a network stream.



Sun, 07 Jul 2002 03:00:00 GMT  
 Usage of Ada.Streams

David C. Hoos, Sr. a crit dans le message ...

Quote:
>Agreed.  I only used an access type because the original poster was
>writing from a dereferenced access object.

Sure, It was just side note that was more an answer to the original poster.

Quote:
>Also, local declare blocks, of course, are more restricted in scope than
>dynamically-allocated objects.

>Which to use depends on other considerations, but I would probably use
>a storage pool if dynamically allocating, especially in a long-running
>program reading from, say, a network stream.

Certainly something to have in mind.

Pascal.

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://ourworld.compuserve.com/homepages/pascal_obry
--|
--| "The best way to travel is by means of imagination"



Sun, 07 Jul 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Stream IO - Correct usage of Set_Index

2. Examples on Usage of Ada-ODBC and Ada-Win32 Bindings

3. Examples on Usage of Ada-ODBC and Ada-Win32 Bindings

4. Ada, Ada,Ada,Ada,Ada,Ada,Ada,Ada Development

5. Stream management in Ada

6. Ada Streams representation

7. Ada.Streams.Read Length Paramenter is not IN?

8. Streams in Ada

9. Ann: unzip-ada, v.06 (streams)

10. Ada.Streams examples

11. Ada.Streams

12. Ada.Streams.Stream_IO - mode Inout_File missing?

 

 
Powered by phpBB® Forum Software