comp.lang.ada
 help / color / mirror / Atom feed
* Actual stream of a certain type
@ 2006-07-26 17:27 Marius Amado-Alves
  2006-07-26 18:25 ` tmoran
  0 siblings, 1 reply; 8+ messages in thread
From: Marius Amado-Alves @ 2006-07-26 17:27 UTC (permalink / raw)
  To: comp.lang.ada

I want to write a library that exports a type T whose stream  
operations 'Write and 'Read require that the actual stream be of a  
certain type, also provided by the library, and visible and usable in  
the body of the operations. Any idea how to accomplish his? I've been  
at it for hours now. Thanks a lot.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Actual stream of a certain type
       [not found] <03AF72A2-6759-4DE5-9D52-4E8266C19C43@amado-alves.info>
@ 2006-07-26 18:04 ` Marius Amado-Alves
  0 siblings, 0 replies; 8+ messages in thread
From: Marius Amado-Alves @ 2006-07-26 18:04 UTC (permalink / raw)
  To: comp.lang.ada

> I want to write a library that exports a type T whose stream
> operations 'Write and 'Read require that the actual stream be of a
> certain type, also provided by the library, and visible and usable in
> the body of the operations.

Correction: the provided stream type is an add-on, or mix-in, to  
whatever stream type the user needs (and provides himself). (As  
opposed to a stream type entirely defined in the library, as my first  
description could suggest.) Thanks.






^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Actual stream of a certain type
  2006-07-26 17:27 Actual stream of a certain type Marius Amado-Alves
@ 2006-07-26 18:25 ` tmoran
  2006-07-26 23:47   ` Marius Amado-Alves
       [not found]   ` <71E9A504-B016-49E2-AB85-34CCDF0AEEC8@amado-alves.info>
  0 siblings, 2 replies; 8+ messages in thread
From: tmoran @ 2006-07-26 18:25 UTC (permalink / raw)


> I want to write a library that exports a type T whose stream
> operations 'Write and 'Read require that the actual stream be of a
> certain type, also provided by the library, and visible and usable in
   You can write your own 'Read and 'Write for T and within those check
if not Stream.all in T_Kind_of_Stream then raise ...



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Actual stream of a certain type
  2006-07-26 18:25 ` tmoran
@ 2006-07-26 23:47   ` Marius Amado-Alves
  2006-07-27  8:05     ` Dmitry A. Kazakov
       [not found]   ` <71E9A504-B016-49E2-AB85-34CCDF0AEEC8@amado-alves.info>
  1 sibling, 1 reply; 8+ messages in thread
From: Marius Amado-Alves @ 2006-07-26 23:47 UTC (permalink / raw)
  To: comp.lang.ada

>    You can write your own 'Read and 'Write for T and within those  
> check
> if not Stream.all in T_Kind_of_Stream then raise ...

Thanks, I know, I tried that, but I don't seem to succeed combining  
that with providing T_Stream as a mix-in. I also tried Ada 2005  
interfaces with GNAT. I thought I saw a real case for interfaces and  
that interested me because all examples I have seen so far are  
academic, not compelling. But failed. Every time Ada got in the way.  
It's really a love-hate relation with her. Shall I post the code here?



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Actual stream of a certain type
       [not found]   ` <71E9A504-B016-49E2-AB85-34CCDF0AEEC8@amado-alves.info>
@ 2006-07-27  0:25     ` Marius Amado-Alves
  0 siblings, 0 replies; 8+ messages in thread
From: Marius Amado-Alves @ 2006-07-27  0:25 UTC (permalink / raw)
  To: comp.lang.ada

Ok, here it goes. Of all idioms I tried maybe the least unsuccessful  
(?) ones are those that raise Program_Error with message  
"accessibility check failed" (GNAT) for line marked HERE below. The  
problem here is: how to use the stream visible on that line, as the  
good T_Stream object he is, pass it as a variable, etc? Thanks a  
great lot.

    type T_Stream_Class_Access is access all T_Stream'Class;

    procedure Write_T
      (Stream : T_Stream_Class_Access;
       Item : in T) is
    begin
       Foobar (Stream.all, Item);
    end;

    procedure Write
      (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
       Item : in T) is
    begin
       if Stream.all not in T_Stream'Class then
          raise Actual_Stream_Must_Be_A_T_Stream;
       else
          Write_T (T_Stream_Class_Access (Stream), Item); -- <== HERE
       end if;
    end;





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Actual stream of a certain type
  2006-07-26 23:47   ` Marius Amado-Alves
@ 2006-07-27  8:05     ` Dmitry A. Kazakov
  2006-07-27  8:45       ` Marius Amado-Alves
       [not found]       ` <2AA0FC04-5130-42BF-A6C9-03C3D6F4C642@amado-alves.info>
  0 siblings, 2 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2006-07-27  8:05 UTC (permalink / raw)


On Thu, 27 Jul 2006 00:47:00 +0100, Marius Amado-Alves wrote:

> Thanks, I know, I tried that, but I don't seem to succeed combining  
> that with providing T_Stream as a mix-in. I also tried Ada 2005  
> interfaces with GNAT. I thought I saw a real case for interfaces and  
> that interested me because all examples I have seen so far are  
> academic, not compelling. But failed. Every time Ada got in the way.  
> It's really a love-hate relation with her.

You have a "parallel types hierarchy" problem, which in general has no
solution in Ada.

On Thu, 27 Jul 2006 01:25:04 +0100, in comp.lang.ada you wrote:

> Ok, here it goes. Of all idioms I tried maybe the least unsuccessful  
> (?) ones are those that raise Program_Error with message  
> "accessibility check failed" (GNAT) for line marked HERE below. The  
> problem here is: how to use the stream visible on that line, as the  
> good T_Stream object he is, pass it as a variable, etc? Thanks a  
> great lot.
> 
>     type T_Stream_Class_Access is access all T_Stream'Class;
> 
>     procedure Write_T
>       (Stream : T_Stream_Class_Access;
>        Item : in T) is
>     begin
>        Foobar (Stream.all, Item);
>     end;
> 
>     procedure Write
>       (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
>        Item : in T) is
>     begin
>        if Stream.all not in T_Stream'Class then
>           raise Actual_Stream_Must_Be_A_T_Stream;
>        else
>           Write_T (T_Stream_Class_Access (Stream), Item); -- <== HERE
>        end if;
>     end;

I would use (double-dispatch emulation):

   procedure Write_T (Stream : in out T_Stream; Item : T'Class) is
   begin
      Foobar (Stream, Item);
   end Write_T;

   procedure Write
             (  Stream : access Ada.Streams.Root_Stream_Type'Class;
                Item   : T
             )  is
   begin
      if Stream.all not in T_Stream'Class then
         raise Actual_Stream_Must_Be_A_T_Stream;
      else
         Write_T (T_Stream'Class (Stream.all), Item);
      end if;
   end Write;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Actual stream of a certain type
  2006-07-27  8:05     ` Dmitry A. Kazakov
@ 2006-07-27  8:45       ` Marius Amado-Alves
       [not found]       ` <2AA0FC04-5130-42BF-A6C9-03C3D6F4C642@amado-alves.info>
  1 sibling, 0 replies; 8+ messages in thread
From: Marius Amado-Alves @ 2006-07-27  8:45 UTC (permalink / raw)
  To: comp.lang.ada

>    procedure Write_T (Stream : in out T_Stream; Item : T'Class) is
>    begin
>       Foobar (Stream, Item);
>    end Write_T;
>
>    procedure Write
>              (  Stream : access Ada.Streams.Root_Stream_Type'Class;
>                 Item   : T
>              )  is
>    begin
>       if Stream.all not in T_Stream'Class then
>          raise Actual_Stream_Must_Be_A_T_Stream;
>       else
>          Write_T (T_Stream'Class (Stream.all), Item);
>       end if;
>    end Write;

Thanks a lot! I'll try this. I did not try it before because I  
thought the call to Write_T on Write would not pass Stream as a  
variable, i.e. the conversion would create a read-only view. But if  
you wrote it, it must be one of those Ada idiosyncrasies. Thanks a lot.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Actual stream of a certain type
       [not found]       ` <2AA0FC04-5130-42BF-A6C9-03C3D6F4C642@amado-alves.info>
@ 2006-07-27 10:47         ` Marius Amado-Alves
  0 siblings, 0 replies; 8+ messages in thread
From: Marius Amado-Alves @ 2006-07-27 10:47 UTC (permalink / raw)
  To: comp.lang.ada

> ... it must be one of those Ada idiosyncrasies...

Ok, I'm laughable, it's the view conversion of course, I can't  
believe I forgot it. Thanks a lot. All is well now.



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-07-27 10:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-26 17:27 Actual stream of a certain type Marius Amado-Alves
2006-07-26 18:25 ` tmoran
2006-07-26 23:47   ` Marius Amado-Alves
2006-07-27  8:05     ` Dmitry A. Kazakov
2006-07-27  8:45       ` Marius Amado-Alves
     [not found]       ` <2AA0FC04-5130-42BF-A6C9-03C3D6F4C642@amado-alves.info>
2006-07-27 10:47         ` Marius Amado-Alves
     [not found]   ` <71E9A504-B016-49E2-AB85-34CCDF0AEEC8@amado-alves.info>
2006-07-27  0:25     ` Marius Amado-Alves
     [not found] <03AF72A2-6759-4DE5-9D52-4E8266C19C43@amado-alves.info>
2006-07-26 18:04 ` Marius Amado-Alves

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