From: tmoran@acm.org
Subject: Re: C Interface example
Date: Sat, 03 Feb 2007 13:48:21 -0600
Date: 2007-02-03T13:48:21-06:00 [thread overview]
Message-ID: <ncKdnZXwW7GYelnYnZ2dnUVZ_oSnnZ2d@comcast.com> (raw)
In-Reply-To: 1170526166.831811.237660@h3g2000cwc.googlegroups.com
> 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);
next prev parent reply other threads:[~2007-02-03 19:48 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-02-03 15:20 C Interface example artifact.one
2007-02-03 16:15 ` Ludovic Brenta
2007-02-03 17:15 ` Simon Wright
2007-02-03 19:43 ` Ludovic Brenta
2007-02-03 17:39 ` artifact.one
2007-02-04 14:34 ` Stephen Leake
2007-02-03 16:19 ` Cesar Rabak
2007-02-03 17:40 ` artifact.one
2007-02-03 19:59 ` Cesar Rabak
2007-02-03 17:48 ` Gautier
2007-02-03 18:09 ` artifact.one
2007-02-03 19:17 ` Georg Bauhaus
2007-02-03 19:53 ` artifact.one
2007-02-03 20:02 ` Gautier
2007-02-03 21:36 ` Georg Bauhaus
2007-02-03 19:48 ` tmoran [this message]
2007-02-03 19:55 ` artifact.one
2007-02-04 14:37 ` Stephen Leake
2007-02-03 20:42 ` Pascal Obry
2007-02-03 20:52 ` Jeffrey R. Carter
2007-02-03 20:57 ` artifact.one
2007-02-04 4:05 ` Jeffrey R. Carter
2007-02-04 11:52 ` Simon Wright
2007-02-04 20:59 ` Jeffrey R. Carter
2007-02-05 8:56 ` Maciej Sobczak
2007-02-05 18:12 ` Jeffrey R. Carter
2007-02-06 1:48 ` Randy Brukardt
2007-02-06 8:20 ` Maciej Sobczak
2007-02-06 19:18 ` Jeffrey R. Carter
2007-02-04 14:32 ` Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox