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,73975695cdfcb86f X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.227.230 with SMTP id sd6mr4244053pbc.8.1333659986087; Thu, 05 Apr 2012 14:06:26 -0700 (PDT) MIME-Version: 1.0 Path: r9ni22681pbh.0!nntp.google.com!news1.google.com!goblin3!goblin.stu.neva.ru!gandalf.srv.welterde.de!news.jacob-sparre.dk!munin.jacob-sparre.dk!pnx.dk!.POSTED!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Dispatching callback handed over to C Date: Thu, 5 Apr 2012 16:06:22 -0500 Organization: Jacob Sparre Andersen Research & Innovation Message-ID: References: <6fb83c74-b2a4-4ae8-9c89-d755b737198f@v22g2000vby.googlegroups.com> <85d1ad51-c02b-44fa-87b6-02aa1d8ba1b2@x17g2000vba.googlegroups.com> <62246369-6b9f-4a96-9c67-fd1774c02e78@k24g2000yqe.googlegroups.com> NNTP-Posting-Host: static-69-95-181-76.mad.choiceone.net X-Trace: munin.nbi.dk 1333659985 15464 69.95.181.76 (5 Apr 2012 21:06:25 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Thu, 5 Apr 2012 21:06:25 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 Date: 2012-04-05T16:06:22-05:00 List-Id: Your code looks OK to me. (But I didn't try to compiler or use it, so there may be something I missed.) Randy. "Natasha Kerensikova" wrote in message news:slrnjnqoga.1lme.lithiumcat@sigil.instinctive.eu... > On 2012-04-04, Randy Brukardt wrote: >> For instance, that's how we find the Ada object in Claw; we >> unchecked-convert access-to-class-wide to a DWord in Windows, and then >> when >> we need to use it, we unchecked-convert back. If I needed to make this >> totally portable and had sufficient control over the API, I'd replace the >> DWord by a locally-declared modular type of which I could control the >> size >> to match the target. But there is little point in going further than >> that. >> >> System.Address is the wrong type to use for a "bucket-of-bits". I agree >> that >> it would be nice to be able to directly put the access-to-class-wide in >> the >> C convention record (and most compilers will in fact allow this, after >> giving some warnings), but it doesn't seem to make that much difference. > > So for my binding, what about something like : > > type Opaque_Data is null record; > pragma Convention (C, Opaque_Data); > > type Opaque_Pointer is access all Opaque_Data; > pragma Convention (C, Opaque_Pointer); > > type Callback_Access is access all Event_Callback'Class; > pragma Convention (C, Callback_Access); > > procedure Set_C_Callback (; > Data : Opaque_Pointer); > pragma Import (C, Set_C_Callback, "set_callback"); > > > procedure Called_From_C (; > Data : Opaque_Pointer) > is > package To_Ada is new Ada.Unchecked_Conversion > (Source => Opaque_Pointer, Target => Callback_Access); > > Callback : Callback_Access := To_Ada (Data); > begin > Callback.all.Handle (<...>); > end Called_From_C; > > pragma Convention (C, Called_From_C); > > > procedure Set_Callback (; > Callback : in out Event_Callback'Class) > is > package To_C is new Ada.Unchecked_Conversion > (Source => Callback_Access, Target => Opaque_Pointer); > begin > Set_C_Callback (<...>, To_C (Callback'Access)); > end Set_Callback; > > As far as I cen tell, Opaque_Pointer refers only to types compatibles > with C, so the imports goes well and without warning, while > Opaque_Pointer and Callback_Access, being both access types with the > same convention, ensures they can be safely (in terms of keeping the > bit pattern intact, of course not type-safety) converted back and forth. > > Or is there some trap in the above code that I'm missing? > > > Thanks for your help, > Natasha