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 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!newshub.sdsu.edu!elnk-nf2-pas!elnk-pas-nf1!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread2.news.pas.earthlink.net.POSTED!a6202946!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko/20030624 MSIE X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Interfacing with C: access/out parameters References: <2ph6fjFkpsahU1@uni-berlin.de> <_UKYc.2897$w%6.1362@newsread1.news.pas.earthlink.net> <2pivcgFl9kl2U1@uni-berlin.de> In-Reply-To: <2pivcgFl9kl2U1@uni-berlin.de> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Tue, 31 Aug 2004 18:26:33 GMT NNTP-Posting-Host: 63.184.8.236 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.news.pas.earthlink.net 1093976793 63.184.8.236 (Tue, 31 Aug 2004 11:26:33 PDT) NNTP-Posting-Date: Tue, 31 Aug 2004 11:26:33 PDT Xref: g2news1.google.com comp.lang.ada:3225 Date: 2004-08-31T18:26:33+00:00 List-Id: Alex R. Mosteo wrote: > It's for these reasons that I would want to have the optimal high level > solution, and eliminating the copy of the returned data seems a sensible > desire. There is that mentioned Import_Valued_Procedure in Gnat which > addresses this Ada shortcoming, but talking about a binding, I want it > to be totally portable if possible. > > As per the C convention types all around the place, my intention is to > build (if time permits) a thick binding over this thin binding, so that > can be isolated later. Conversion to native Ada types will probably add assignment anyway. > Further ideas? Is there anything definitely unadvisable in my original > solution? (The one using limited/tagged types and 'Address). I would not advise using an overlay if you want to insure portability. For a convention C function, Ada automatically converts parameters of composite types to pointers (see Annex B), so you can "cheat" and use this fact to pass a pointer to your record. I've done a similar thing in a binding to fgets: procedure Get (Item : out String; Last : out Natural; File : in File_Handle := Standard_Input) is Buffer : Char_Array (1 .. Item'Length + 1); -- May get a warning about Buffer never assigned a value Result : File_Handle; -- Dummy pointer function C_Get (Item : Char_Array; Length : Int; File : File_Handle) return File_Handle; pragma Import (C, C_Get, "fgets"); -- Item should be "out", but since Import (C) will pass a pointer, -- this works. begin -- Get Result := C_Get (Buffer, Buffer'Length, File); -- This fills Buffer, but Ada doesn't know that. if Result = null then raise EOF_Breached; end if; Extract : declare Value : constant String := To_Ada (Buffer); begin -- Extract if Value (Value'Last) = LF then -- Remove line terminator Last := Item'First + Value'Length - 2; Item (Item'First .. Last) := Value (Value'First .. Value'Last - 1); else Last := Item'First + Value'Length - 1; Item (Item'First .. Last) := Value; end if; end Extract; end Get; -- Jeff Carter "I'm particularly glad that these lovely children were here today to hear that speech. Not only was it authentic frontier gibberish, it expressed a courage little seen in this day and age." Blazing Saddles 88