From: "Jeffrey R. Carter" <spam.not.jrcarter@acm.not.spam.org>
Subject: Re: How to use Annex G.3 Vectors and Matrices in bindings to C arrays
Date: Mon, 13 Nov 2006 19:09:10 GMT
Date: 2006-11-13T19:09:10+00:00 [thread overview]
Message-ID: <qp36h.278689$1i1.164466@attbi_s72> (raw)
In-Reply-To: <1163416802.221321.170270@f16g2000cwb.googlegroups.com>
Jerry wrote:
>>
> I'm afraid I'll have to apologize for not being accurate in my initial
> post. Indeed, I am not having any problems (as I erroneously claimed)
> with 1D arrays. The problem arises with 2D arrays, about which I
> pestered the list earlier. With further apologies for posting a bit
> more code that I would like, the following is a typical binding that I
> made in which it was necessary to gain access to 2D C arrays.
OK.
> z : in out Long_Float_Array_2D; -- height of surface
> at definition points
I don't see why this is in out.
> package PL_Float_Pointers_Local is new Interfaces.C.Pointers
> (Index => Integer,
> Element => Long_Float,
> Element_Array => Long_Float_Array_1D,
> Default_Terminator => 0.0);
> use type PL_Float_Pointers_Local.Pointer; -- as in RM B.3.2
> type PL_Float_Pointer_Array_Local is array (Integer range <>) of
> PL_Float_Pointers_Local.Pointer; -- array of pointers to Long_Floats
> which represent the first element of each row of z in C-land
>
> Index_Of_First_Column : Integer := z'First(2);
> z_As_Pointers : PL_Float_Pointer_Array_Local (z'range(1));
>
> procedure
> plmeshc_local(x : PL_Float_Array; y : PL_Float_Array; z :
> PL_Float_Pointer_Array_Local; nx : Integer; ny : Integer; opt :
> Integer; clevel : PL_Float_Array; nlevel : PLINT);
> pragma Import(C, plmeshc_local, "c_plmeshc");
>
> begin
> for Index in z'range(1) loop
> z_As_Pointers(Index) := z(Index, Index_Of_First_Column)'access;
To do this, Z must have aliased components. If you want to use the
language-defined matrix type for this, you'll have to copy the values
from Z into a local array. I'd probably do something like
type Row_Of_Z is new Vector (Z'range (2) );
pragma Convention (C, Row_Of_Z);
type Row_Ptr is access all Row_Of_Z;
pragma Convention (C, Row_Ptr);
type Z_For_C is array (Z'range (1) ) of Row_Ptr;
pragma Convention (C, Z_For_C);
Z_Ptr : Z_For_C;
for I in Z_Ptr'range loop
Z_Ptr := new Row_Of_Z;
for J in Z'Ptr (I)'range loop
Z_Ptr (J) := Z (I, J);
end loop;
end loop;
-- Call C with Z_Ptr
-- Free Z_Ptr
You could, of course, put the rows into an array of aliased Row_Of_Z and
use the 'access of its elements to fill Z_Ptr. That would eliminate the
need to free the pointers.
A messy way, that doesn't involve copying the elements, is to
instantiate System.Address_To_Access_Conversions, and derive a
convention-C access type from the access type defined in the
instantiation. Then you take 'Address, convert it to an access type
using To_Pointer, then convert that to your convention-C access type and
store it in your array of pointers. (This is equivalent to using GNAT's
'Unrestricted_Access, but portable.)
> The related array definitions are:
> type Long_Float_Array_1D is array (Integer range <>) of aliased
> Long_Float;
> type Long_Float_Array_2D is array (Integer range <>, Integer range
> <>) of aliased Long_Float;
>
> The "thin" binding Ada procedure plmeshc is defined:
> procedure plmeshc(x : PL_Float_Array; y : PL_Float_Array; z :
> PL_Float_Array_2D; nx : PLINT; ny : PLINT; opt : PLINT;
> clevel : PL_Float_Array; nlevel : PLINT);
> pragma Import(C, plmeshc, "c_plmeshc");
The declarations of the PL_... types would be useful, too, though we can
make a good guess from the C.
--
Jeff Carter
"Now go away or I shall taunt you a second time."
Monty Python & the Holy Grail
07
next prev parent reply other threads:[~2006-11-13 19:09 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-11-12 3:45 How to use Annex G.3 Vectors and Matrices in bindings to C arrays Jerry
2006-11-12 8:45 ` Yves Bailly
2006-11-13 10:48 ` Jerry
2006-11-12 19:39 ` Jeffrey R. Carter
2006-11-13 7:09 ` tmoran
2006-11-13 11:20 ` Jerry
2006-11-13 17:42 ` Yves Bailly
2006-11-13 19:50 ` Gautier
2006-11-14 5:28 ` Jerry
2006-11-14 5:18 ` Jerry
2006-11-13 19:09 ` Jeffrey R. Carter [this message]
2006-11-14 7:31 ` Jerry
2006-11-14 7:54 ` Jerry
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox