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,FREEMAIL_FROM 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!postnews.google.com!a75g2000cwd.googlegroups.com!not-for-mail From: artifact.one@googlemail.com Newsgroups: comp.lang.ada Subject: Re: C Interface example Date: 3 Feb 2007 11:55:03 -0800 Organization: http://groups.google.com Message-ID: <1170532503.164182.253710@a75g2000cwd.googlegroups.com> References: <1170526166.831811.237660@h3g2000cwc.googlegroups.com> NNTP-Posting-Host: 81.179.242.178 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1170532514 3393 127.0.0.1 (3 Feb 2007 19:55:14 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 3 Feb 2007 19:55:14 +0000 (UTC) In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy),gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: a75g2000cwd.googlegroups.com; posting-host=81.179.242.178; posting-account=3t6ROQ0AAABpbovmj4Qol-vmGur4qb-2 Xref: g2news2.google.com comp.lang.ada:8895 Date: 2007-02-03T11:55:03-08:00 List-Id: On Feb 3, 7:48 pm, tmo...@acm.org wrote: > > float *vec_addNf(float *, float *, unsigned int); > > Now what would be the most simple and portable way to write > > a vector,ads and vector.adb file? (Specification and body, in case > > reading all that just for an example on interfacing... > > If you want to call an existing C routine, not rewriting it in Ada, > then the literal translation of the calling interface is: > > type p_float is access all interfaces.c.c_float; > > function vec_addNf(va, vb : in p_float; n : in interfaces.c.unsigned) > return p_float; > pragma import(C, vec_addNf, "vec_addNf"); > -- Note: Do this if you really have a C calling convention. > -- If you are running on MS Windows, you probably want "StdCall" > -- instead of "C" > > and then use it like this > type vectors is array(interfaces.c.unsigned range <>) > of aliased interfaces.c.c_float; > va, vb : vectors(1 .. 100); > trash : p_float; > ... > trash := vec_addNf(va(va'first)'unchecked_access, > vb(vb'first)'unchecked_access, > va'length); > > If you were going to be doing a lot of complex calls on va, vb, vc, and vd > you might want instead > > N : interfaces.c.unsigned := 123; > va_data, vb_data, vc_data, vd_data : vectors(1 .. N); > va : constant p_float := va_data(va_data'first)'unchecked_access; > vb : constant p_float := vb_data(vb_data'first)'unchecked_access; > vc : constant p_float := vc_data(vc_data'first)'unchecked_access; > vd : constant p_float := vd_data(vd_data'first)'unchecked_access; > > so you could write things like > > trash := vec_addNf(vec_addNf(va,vb,N),vec_addNf(vc,vd,N), N); Thanks. I will end up using some sort of preprocessor for all this stuff, but I like to know how it works beforehand. MC