comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: C bindings, Interfaces.C.Pointers etc.
Date: 12 May 2004 05:50:03 +0100
Date: 2004-05-12T05:50:03+01:00	[thread overview]
Message-ID: <x7vd65adzb8.fsf@smaug.pushface.org> (raw)
In-Reply-To: te9oc.16468$Hs1.1407@newsread2.news.pas.earthlink.net

Jeffrey Carter <spam@spam.com> writes:

> Dr. Adrian Wrigley wrote:
> 
> > Hi folks!
> > I have a problem creating a binding to a C library.
> > The library makes extensive use of arrays of arrays of structs.
> > Both levels of arrays are of variable length.
> >  struct node { int index; double value; };
> >  struct problem { node **x; };
> > When I attempt to define Ada types for this type of thing,
> > I find I can't use access types to the arrays with the
> > necessary C convention.
> > "warning: this access type does not correspond to C pointer"
> > is the compiler error.
> 
>     -- struct node { int index; double value; };
>     type Node_Info is record
>        Index : Interfaces.C.Int;
>        Value : Interfaces.C.Double;
>     end record;
>     pragma Convention (C, Node_Info);
> 
>     -- struct problem { node **x; };
>     type Node_Ptr     is access all Node_Info;
>     pragma Convention (C, Node_Ptr);
>     type Node_Ptr_Ptr is access all Node_Ptr;
>     pragma Convention (C, Node_Ptr_Ptr);
>     type Problem_Info is record
>        X : Node_Ptr_Ptr;
>     end record;
>     pragma Convention (C, Problem_Info);
> 
> This compiles fine. This is an exact duplicate of the C
> declarations.

But not, I think, of the implications. Where is the equivalent of 

  problem p;
  node *np = *p.x;
  np++;               // <<<<

I've used constrained subtypes in the past:

  type Node_Array is array (Integer range <>) of Node_Info;
  
then in a subprogram taking a Node_Array parameter Nodes,

  subtype Specific_Node_Array is Node_Array (Nodes'First .. Nodes'Last);
  type Node_Array_Access is access Specific_Node_Array;
  pragma Convention (C, Node_Array_Access);

then declare a local binding to the C operation.

Same again for Node_Ptr_Array, though the Ada side of this is going to
be rather difficult since the Node_Ptrs in this are really
Node_Array_Accesses and the Node_Arrays are not all of the same
length.

Perhaps the simplest way would be to use System.Address:

   (Arr (Arr'First))'Address

>               To deal with this using a higher level Ada
> abstraction, you'd wrap something around this to provide that
> abstraction.



-- 
Simon Wright                               100% Ada, no bugs.



  reply	other threads:[~2004-05-12  4:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-11 13:14 C bindings, Interfaces.C.Pointers etc Dr. Adrian Wrigley
2004-05-11 18:17 ` tmoran
2004-05-11 18:48 ` Jeffrey Carter
2004-05-12  4:50   ` Simon Wright [this message]
2004-05-13 15:26   ` Dr. Adrian Wrigley
2004-05-12  6:30 ` Martin Krischik
2004-05-13 15:56   ` Dr. Adrian Wrigley
2004-05-13 17:37     ` Martin Krischik
2004-05-13 22:42     ` Jeffrey Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox