Prev: Direct control of NXT mindstorms
Next: GNAT requires body of generic unit to be present at build?
From: xorque on 9 Jan 2010 07:20 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?
From: AdaMagica on 9 Jan 2010 12:03 On 9 Jan., 13:20, xorque <xorquew...(a)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);
From: xorque on 9 Jan 2010 13:08 On Jan 9, 5:03 pm, AdaMagica <christoph.gr...(a)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...
From: Dmitry A. Kazakov on 9 Jan 2010 13:52 On Sat, 9 Jan 2010 10:08:20 -0800 (PST), xorque wrote: > On Jan 9, 5:03�pm, AdaMagica <christoph.gr...(a)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
|
Pages: 1 Prev: Direct control of NXT mindstorms Next: GNAT requires body of generic unit to be present at build? |