comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Ada to C interfacing with access on unconstrained array
Date: Wed, 21 Oct 2009 14:09:51 +0200
Date: 2009-10-21T14:09:52+02:00	[thread overview]
Message-ID: <1lisu857l2qte$.1trga8b0qw0fq$.dlg@40tude.net> (raw)
In-Reply-To: ba084f6c-41e2-402b-87df-caa490793430@m13g2000vbf.googlegroups.com

On Wed, 21 Oct 2009 02:25:52 -0700 (PDT), dhenry wrote:

> On 20 oct, 18:11, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> wrote:
>> BTW, why
>>
>> � �type Coco_Type is record
>> � � � X : Integer;
>> � � � Y : Integer;
>> � � � Nuts : Nut_Array_Access;
>> � �end record;
>>
>> and not
>>
>> � �type Coco_Type (Size : Natural) is record
>> � � � X : Integer;
>> � � � Y : Integer;
>> � � � Nuts : Nut_Array_Type (1..Size);
>> � �end record;
> 
> Well, I quickly tested the discriminant version of Coco_Type with my
> application needs, and indeed I could use a discriminant to replace
> the access. So I may reconsider my Coco_Type definition.
> 
> Would the discriminant version help me for my Ada->C interfacing?

No. It could only if the types you use would have C convention of the
parameters you are dealing with.

The problem is not how Nut_Array_Type is constrained, but what are its
elements. For example:

   type Nut_Type is record
      Diameter : int;
      Weight   : int;
      Age      : int;
   end record;
   pragma Convention (C, Nut_Type);

   type Nut_Type_Ptr is access all Nut_Type;
   pragma Convention (C, Nut_Type_Ptr);
   
   type Nut_Array_Type is array (Positive range <>) of aliased Nut_Type;
   pragma Convention (C, Nut_Array_Type);
   
   procedure Climb_C
             (  X    : int;
                Y    : int;
                Nuts : Nut_Type_Ptr;
                Len  : unsigned
             );
   pragma Export (C, Climb_C);

This sort of Climb_C can be called from C. Inside it you can call Climb
passing the array "as-is", provided Climb used the type Nut_Array_Type:

   procedure Climb (X, Y : int; Nuts : Nut_Array_Type);

   procedure Climb_C
             (  X    : int;
                Y    : int;
                Nuts : Nut_Type_Ptr;
                Len  : unsigned
             )  is
      subtype Actual_Array_Type is Nut_Array_Type (1..Natural (Len));
      Actual_Array : Actual_Array_Type;
      for Actual_Array'Address use Nuts.all'Address;
   begin
      Climb (X, Y, Actual_Array);
         -- Normally, this should not copy Actual_Array upon subtype
         -- conversion (from Actual_Array_Type to Nut_Array_Type)
   end Climb_C;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2009-10-21 12:09 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-20 13:07 Ada to C interfacing with access on unconstrained array dhenry
2009-10-20 16:11 ` Dmitry A. Kazakov
2009-10-21  9:25   ` dhenry
2009-10-21 12:09     ` Dmitry A. Kazakov [this message]
2009-10-21 12:14       ` Dmitry A. Kazakov
2009-10-20 16:24 ` Adam Beneschan
2009-10-20 18:40 ` tmoran
2009-10-21  3:29   ` John B. Matthews
2009-10-21  9:29     ` dhenry
2009-10-21 14:16       ` John B. Matthews
replies disabled

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