From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,b9a378a9a09dab87 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.66.76.201 with SMTP id m9mr52947paw.1.1346212374810; Tue, 28 Aug 2012 20:52:54 -0700 (PDT) Path: t10ni109821995pbh.0!nntp.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe23.iad.POSTED!not-for-mail Message-ID: <503D920A.9010809@shaw.ca> From: Brad Moore User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Access to procedure and generics References: <647e1a9e-e8be-4ad2-a9d4-21c8ae6f52f2@googlegroups.com> In-Reply-To: <647e1a9e-e8be-4ad2-a9d4-21c8ae6f52f2@googlegroups.com> NNTP-Posting-Host: 68.145.219.148 X-Complaints-To: internet.abuse@sjrb.ca X-Trace: 1346212374 68.145.219.148 (Wed, 29 Aug 2012 03:52:54 UTC) NNTP-Posting-Date: Wed, 29 Aug 2012 03:52:54 UTC Date: Tue, 28 Aug 2012 21:52:42 -0600 X-Received-Bytes: 2373 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Date: 2012-08-28T21:52:42-06:00 List-Id: 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