From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,fce935e9f3aa1da8,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Jano Newsgroups: comp.lang.ada Subject: Interfacing with C: access/out parameters Date: Mon, 30 Aug 2004 19:30:37 +0200 Message-ID: <2ph6fjFkpsahU1@uni-berlin.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de AwbOoGft87aiw7jlSh5kkg+E+hPCatokBRsDMmfHBXrYDJUQ0= User-Agent: Mozilla Thunderbird 0.7.1 (X11/20040626) X-Accept-Language: en-us, en Xref: g2news1.google.com comp.lang.ada:3172 Date: 2004-08-30T19:30:37+02:00 List-Id: 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 :(