comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Convert between C "void*" pointer and an access
Date: Thu, 12 Oct 2017 09:57:28 +0200
Date: 2017-10-12T09:57:28+02:00	[thread overview]
Message-ID: <orn797$1bic$1@gioia.aioe.org> (raw)
In-Reply-To: orm7v8$1gr$1@gioia.aioe.org

On 12/10/2017 01:03, Victor Porton wrote:
> Randy Brukardt wrote:
> 
>> "Victor Porton" <porton@narod.ru> wrote in message
>> news:orm3qe$1r0c$1@gioia.aioe.org...
>>> What is the right way to convert between C "void*" pointer and an access
>>> to
>>> a tagged or class-wide type?
>>
>> I doubt that there is a *right* way, there just are several possibilities.
>>
>>> Ada.Unchecked_Conversion seems to be what I need, but the access type may
>>> be
>>> "fat" and thus have another format than void*.
>>
>> Right, but not very likely. I'd expect this to work in most cases.
>>
>> But our solution in the Claw libraries was simply to use the appropriate
>> C-convention access type in the interface definitions and avoid making any
>> conversions at all. I believe that on the C side, void* and
>> <anything-else>* have to use the same representation, so C convention
>> should work properly for any pointer type.
>>
>> That is, if the C interface contains a void* parameter, we just used an
>> appropriate C convention access type in its place in the Ada parameter
>> definition. (Using overloading if we needed multiple such pointers - but
>> that was very rare.) There's no counterpart to void* in Ada anyway, so one
>> has to do something like that in the interfacing definitions.
> 
> "- What is your solution for Windows printing problems? - Do not print."
> 
> I NEED to convert between access to tagged records (and access to class-wide
> types) and C void* pointers.
> 
> I need this because it is C's way to pass objects by using void* and I need
> to pass Ada objects to C functions (not just C objects).

What Randy says is that in many cases you can pass an Ada access type 
where void * is expected:

    type T is tagged whatever ...;
--
-- void c_foo (void * userdata);
--
    type T_Ptr is access all T['Class];
    pragma Convention (C, T_Ptr);

    procedure Set_Callback (..., User_Data : T_Ptr);
    pragma Import (C, Set_Callback, "...");

This is the most safe and clean way to pass pointers to Ada objects 
through C library, assuming that C does not touch the object.

In other cases you can simply declare it System.Address and use 
Access_To_Address conversion or the Address pragma/aspect or fake pool 
allocator new. Whatever you like.

System.Address is frowned at because it not guaranteed to work 
everywhere, but chances are high that if that does not work nothing else 
would either. So I would not worry too much about it.

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

  reply	other threads:[~2017-10-12  7:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-11 21:52 Convert between C "void*" pointer and an access Victor Porton
2017-10-11 22:32 ` Randy Brukardt
2017-10-11 23:03   ` Victor Porton
2017-10-12  7:57     ` Dmitry A. Kazakov [this message]
2017-10-12  8:05     ` Simon Wright
2017-10-29 14:50   ` David Thompson
2017-10-11 22:58 ` Victor Porton
2017-10-11 23:12   ` Victor Porton
2017-10-12  1:01     ` Victor Porton
replies disabled

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