comp.lang.ada
 help / color / mirror / Atom feed
From: Jano <notelacreas@porfavor.no>
Subject: Interfacing with C: access/out parameters
Date: Mon, 30 Aug 2004 19:30:37 +0200
Date: 2004-08-30T19:30:37+02:00	[thread overview]
Message-ID: <2ph6fjFkpsahU1@uni-berlin.de> (raw)

Hi,

I'm doing some C interfacing and I have a doubt about what's the proper 
way to do something "the Ada Way(tm)".

If I'm not mistaken, reading the relevant ARM section I see that C 
pointer parameters can be mapped either with access and out parameters.

I'm interfacing to some C functions which fill a data structure passed 
via pointer, and which return an error code I don't want to discard. So 
Firstly, I define a function like:

function Blah (Datum : access Data) return C.Int;
pragma Import (C, Blah);

since I can't use out mode in functions. Data is a record defined 
elsewhere with C convention.

Now, I would want to have a wrapper procedure more Ada-fied like:

Procedure Ada_Blah (Datum : out Data);
-- May raise some exception

And my doubt, finally, is: can I ensure somehow that Datum is passed by 
reference?, so I can do inside Ada_Blah [1]:

Helper : aliased Datum;
For Helper'Address use Datum'Address;
if Thin_Package.Blah (Helper'Access) = Error_Value then
    raise My_Error;
end if;

I know that tagged types are always passed by reference, but I fear that 
the tag will ruin the record structure. (My tests show indeed this is 
happening). It seems my only solution is to use a limited modifier. Am I 
right in that this would work properly in any compliant compiler? I'm 
ready to lose the copy operation.

I'd want to not use a wrapper type for the types implicated in this kind 
of functions... nor explicitely do a local copy, because the function 
may appear in code called at high frequency rates. Is this possible?

Thanks in advance,

A. Mosteo.

[1] This works with Gnat, but is it portable by some reason I ignore, or 
simply because Gnat uses reference passing for out record parameters?

P.s: A quite old thread here says:

"Simon says

   Yes, I quite understand that; the problem is that the C subprogram is
   a function and therefore can't be declared with an out parameter. I
   guess I could use the DEC pragma (valued_subprogram??), but that would
   be a tad implementation-dependent!

Deward says

Sure, it would be a bit implementation dependent. At the moment it would
only work on GNAT and DEC, though I would like to see this particular
pragma (Import_Valued_Procedure) implemented more widely, since it is
very useful in interfacing.

It is indeed the sad consequence of the rule forbidding out parameters 
in functions that the horrible copy in your suggested solution is the 
only truly portable way of doing things."

The last paragraph is discouraging. n
:(



             reply	other threads:[~2004-08-30 17:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-30 17:30 Jano [this message]
2004-08-30 19:06 ` Interfacing with C: access/out parameters Jeffrey Carter
2004-08-31  9:41   ` Alex R. Mosteo
2004-08-31  9:48     ` Alex R. Mosteo
2004-08-31 18:26     ` Jeffrey Carter
2004-09-01  9:46       ` Alex R. Mosteo
2004-08-31  7:54 ` Egil H. H�vik
2004-08-31  9:11   ` Egil H. H�vik
replies disabled

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