comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <brad.moore@shaw.ca>
Subject: Re: Access to procedure and generics
Date: Tue, 28 Aug 2012 21:52:42 -0600
Date: 2012-08-28T21:52:42-06:00	[thread overview]
Message-ID: <503D920A.9010809@shaw.ca> (raw)
In-Reply-To: <647e1a9e-e8be-4ad2-a9d4-21c8ae6f52f2@googlegroups.com>

On 28/08/2012 9:57 AM, Adam Beneschan wrote:
> If you want a portable solution, you can use tagged types instead of access-to-procedure:
>
> package Callback is
>      type Callback_Object is interface;
>      type Callback_T is access all Callback_Object'Class;
>      procedure Handler (Obj : in out Callback_Object) is abstract;
>      procedure Register_Callback(Callback : Callback_T);
> end Callback;
>
> -- if Z is a registered Callback_T, Z.Handler will call the handler
>
> generic
> package USE_CALLBACK is
>      procedure HANDLER;
> end USE_CALLBACK;
> --
> with CALLBACK;
> package body USE_CALLBACK
> is
>      procedure HANDLER is
>      begin
>         ...
>      end;
>
>      type Call_Handler is new Callback.Callback_Object with null record;
>      overriding
>      procedure Handler (Obj : in out Call_Handler) is
>      begin
>          Handler; -- of course, this doesn't *have* to have the same name
>      end Handler;
>
>      X : aliased Call_Handler;
>
> begin
>      CALLBACK.REGISTER_CALLBACK(X'Unchecked_Access);
> end USE_CALLBACK;
>
> Now you can instantiate Use_Callback inside or outside a subprogram, and it should still work on any Ada implementation.
 >
 > (Credit to Brad Moore who came up with essentially this solution to a 
similar problem.)

I have to give credit where credit is due. It was Steve Baird who 
suggested to me that I should try to use tagged types as an approach to 
get around this issue.

Brad



  parent reply	other threads:[~2012-08-29  3:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-28  9:46 Access to procedure and generics Markus Schöpflin
2012-08-28 10:20 ` Egil Høvik
2012-08-28 10:22   ` Egil Høvik
2012-08-28 10:32     ` Markus Schöpflin
2012-08-31 14:14       ` Robert A Duff
2012-08-28 10:41 ` Georg Bauhaus
2012-08-28 10:47   ` Markus Schöpflin
2012-08-28 15:57 ` Adam Beneschan
2012-08-28 16:01   ` Adam Beneschan
2012-08-29  3:52   ` Brad Moore [this message]
2012-08-29 11:25   ` Markus Schöpflin
replies disabled

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