comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Generic 'access-to-subprogram' formal
Date: Sat, 9 Jan 2010 19:52:38 +0100
Date: 2010-01-09T19:52:34+01:00	[thread overview]
Message-ID: <1p7zqgrrgydvs.14xsj4cicf826$.dlg@40tude.net> (raw)
In-Reply-To: c79ed4f4-6f42-4309-a9e8-4476001f326b@k17g2000yqh.googlegroups.com

On Sat, 9 Jan 2010 10:08:20 -0800 (PST), xorque wrote:

> On Jan 9, 5:03�pm, AdaMagica <christoph.gr...@eurocopter.com> wrote:
>>
>> generic
>> � type Some_Type is private;
>> � type Acc_Proc �is access procedure (A: Some_Type);
> 
> Thanks.
> 
> I suppose it's not possible to be less specific?
> 
> dlopen()-like functions might return a pointer to any type
> of subprogram, so I'd want to do something like:
> 
> generic
>   type Subprogram_Access_Type is access private;
> 
> function Load_Subprogram (Name : in String) return
> Subprogram_Access_Type;
> 
> I don't see anything in the RM that might accommodate this.
> 
> The Load_Subprogram doesn't need to know anything about
> the type other than the fact that it's an access to something.
>
> I have a feeling I'm going to be unsafely converting a
> System.Address...

Sure, if you cannot say anything about the result. The only property of the
result is to be something. The conversion is unsafe anyway, because the
linker cannot prove the profile. You could do it like this:

   function Get_Address (External_Name : String) return Address is
   begin
      ... -- loading the external procedure from a DLL
   end Get_Address;

Later on, for some procedure:

   procedure DB_Open (Name : char_array);
   for DB_Open'Address use Get_Address ("dbopen");
   pragma Import (C, DB_Open);

However, there is no much use in such wrappers, because for C bindings you
have to convert some parameters and results. Make procedures functions,
where C is unable to return an object on the stack. Wrap procedures into
controlled types (e.g. data base connection handle). Drop return codes
converting them to exceptions. And so on.

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



      reply	other threads:[~2010-01-09 18:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-09 12:20 Generic 'access-to-subprogram' formal xorque
2010-01-09 17:03 ` AdaMagica
2010-01-09 18:08   ` xorque
2010-01-09 18:52     ` Dmitry A. Kazakov [this message]
replies disabled

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