comp.lang.ada
 help / color / mirror / Atom feed
* Generic 'access-to-subprogram' formal
@ 2010-01-09 12:20 xorque
  2010-01-09 17:03 ` AdaMagica
  0 siblings, 1 reply; 4+ messages in thread
From: xorque @ 2010-01-09 12:20 UTC (permalink / raw)


Hello.

I'm attempting to write a generic wrapper around a C function
that acts similarly to dlopen().

What's the correct way to specify that a generic takes an
access-to-subprogram type as a formal parameter?



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generic 'access-to-subprogram' formal
  2010-01-09 12:20 Generic 'access-to-subprogram' formal xorque
@ 2010-01-09 17:03 ` AdaMagica
  2010-01-09 18:08   ` xorque
  0 siblings, 1 reply; 4+ messages in thread
From: AdaMagica @ 2010-01-09 17:03 UTC (permalink / raw)


On 9 Jan., 13:20, xorque <xorquew...@googlemail.com> wrote:
> Hello.
>
> I'm attempting to write a generic wrapper around a C function
> that acts similarly to dlopen().
>
> What's the correct way to specify that a generic takes an
> access-to-subprogram type as a formal parameter?

Ada Reference Manual

12.5.4: formal_access_type_definition ::= access_type_definition
3.10: access_type_definition ::= [null_exclusion]
access_to_object_definition
                               | [null_exclusion]
access_to_subprogram_definition
3.10: access_to_subprogram_definition ::= access [protected] procedure
parameter_profile
                                        | access [protected] function
parameter_and_result_profile

For Instance:

generic
  type Some_Type is private;
  type Acc_Proc  is access procedure (A: Some_Type);



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generic 'access-to-subprogram' formal
  2010-01-09 17:03 ` AdaMagica
@ 2010-01-09 18:08   ` xorque
  2010-01-09 18:52     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 4+ messages in thread
From: xorque @ 2010-01-09 18:08 UTC (permalink / raw)


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...




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generic 'access-to-subprogram' formal
  2010-01-09 18:08   ` xorque
@ 2010-01-09 18:52     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 4+ messages in thread
From: Dmitry A. Kazakov @ 2010-01-09 18:52 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-01-09 18:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox