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,c370342fe303517b X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns14feed!worldnet.att.net!attbi_s72.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: C Interface example References: <1170516037.435847.326440@p10g2000cwp.googlegroups.com> <1170536268.531049.70610@p10g2000cwp.googlegroups.com> In-Reply-To: <1170536268.531049.70610@p10g2000cwp.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 12.201.97.213 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s72 1170561946 12.201.97.213 (Sun, 04 Feb 2007 04:05:46 GMT) NNTP-Posting-Date: Sun, 04 Feb 2007 04:05:46 GMT Organization: AT&T ASP.att.net Date: Sun, 04 Feb 2007 04:05:46 GMT Xref: g2news2.google.com comp.lang.ada:8904 Date: 2007-02-04T04:05:46+00:00 List-Id: artifact.one@googlemail.com wrote: > On Feb 3, 8:52 pm, "Jeffrey R. Carter" wrote: >> >> with Interfaces.C; >> package C_Vectors is >> type Vector is array (Positive range <>) of Interfaces.C.C_Float; >> pragma Convention (C, Vector); >> >> procedure Add >> (To : in out Vector; Using : in Vector; Length : in Natural); >> end C_Vectors; >> >> package body C_Vectors is >> procedure Add >> (To : in out Vector; Using : in Vector; Length : in Natural) >> is >> type C_Ptr is access all Interfaces.C.C_Float; >> pragma Convention (C, C_Ptr); >> >> function C_Add >> (To : Vector; Using : Vector: Length : Interfaces.C.Unsigned) >> return C_Ptr; >> pragma Import (C, C_Add, "vec_addNf"); >> >> Result : C_Ptr; >> begin -- Add >> Result := C_Add (To, Using, Interfaces.C.Unsigned (Length) ); >> end Add; >> end C_Vectors; >> > > This is compiler-agnostic, yes? Should be. Everything that interacts with C is either from Interfaces.C or declared Convention C, so you're not relying on a compiler using the same representation as C types, even though that's usually the case. The C function returns a pointer, so this imports it that way, and provides a place for the returned value (which it ignores). C allows you to call a function as a procedure (in Ada terms), but Ada doesn't, and you can't be sure what will happen if you call a C function as a procedure from Ada. Now that I look at it, you can leave the Length parameter off Add, and pass To'Length to C_Add: procedure Add (To : in out Vector; Using : Vector); Result := C_Add (To, Using, To'Length); No conversion to Unsigned is needed, because 'Length is Universal_Integer, and implicitly converted as needed. Of course, you have to be sure To'Length = Using'Length (or you could put this check in Add), just as in C. Also, you might get a warning that you never modify To, depending on your compiler. To will be passed to C_Add as a float*, so C_Add can modify it, but Ada doesn't know that C_Add modifies it. -- Jeff Carter "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement." Drew Olbrich 51