From: "John B. Matthews" <nospam@nospam.invalid>
Subject: Re: Ada to C interfacing with access on unconstrained array
Date: Wed, 21 Oct 2009 10:16:52 -0400
Date: 2009-10-21T10:16:52-04:00 [thread overview]
Message-ID: <nospam-00B28A.10165221102009@news.aioe.org> (raw)
In-Reply-To: 6f97c919-9535-4fa6-939d-9ec2cb927ed5@e34g2000vbc.googlegroups.com
In article
<6f97c919-9535-4fa6-939d-9ec2cb927ed5@e34g2000vbc.googlegroups.com>,
dhenry <tfc.duke@gmail.com> wrote:
> On 21 oct, 05:29, "John B. Matthews" <nos...@nospam.invalid> wrote:
> > David: GNAT implements a convenient pragma that's especially
> > helpful for this approach. Given a procedure such as
> >
> > procedure Climb (Coco_Rec : Coco_Type; Length : Positive);
> >
> > One can export the procedure
> >
> > pragma Convention(C, Climb);
> > pragma Export_Procedure(
> > Internal => Climb,
> > External => climb,
> > Parameter_Types => (Coco_Type, Positive),
> > Mechanism => (Value, Value));
>
> Interesting. But how does this pragma would cope with the access on
> unconstrained array?
It doesn't use access to unconstrained array; Dmitry A. Kazakov's
example is more apropos to that context. Instead, the approach relies on
access to constrained array having a large constraint, as tmoran
suggested. The former uses 'Address and the latter uses renames to match
the C array/length pair to an Ada array.
Here is an example of the latter using type Natural as the large
constraint:
package Callee is
type Nut_Type is
record
Diameter : Integer;
Weight : Integer;
Age : Integer;
end record;
type Nut_Array_Type is array (Natural) of Nut_Type;
type Nut_Array_Access is access Nut_Array_Type;
type Coco_Type is
record
X : Integer;
Y : Integer;
Nuts : Nut_Array_Access;
end record;
procedure Climb(Coco_Rec : Coco_Type; Length : Positive);
pragma Convention(C, Climb);
pragma Export_Procedure(
Internal => Climb,
External => climb,
Parameter_Types => (Coco_Type, Positive),
Mechanism => (Value, Value));
end Callee;
with Text_IO;
package body Callee is
procedure Climb(Coco_Rec : Coco_Type; Length : Positive) is
Coco_Nuts : Nut_Array_Type renames Coco_Rec.Nuts.all(0 .. Length - 1);
begin
Text_IO.Put_Line("Viewed from Ada.");
Text_IO.Put_Line("( X, Y )" & Coco_Rec.X'Img & Coco_Rec.Y'Img);
for I in Coco_Nuts'Range loop
Text_IO.Put_Line("(" & I'Img & " )"
& Coco_Nuts(I).Diameter'Img
& Coco_Nuts(I).Weight'Img
& Coco_Nuts(I).Age'Img);
end loop;
end Climb;
end Callee;
/* caller.c */
#include <stdio.h>
#define MAX_NUTS 10
struct Nut_Type {
int Diameter;
int Weight;
int Age;
};
struct Nut_Type Nuts[MAX_NUTS];
struct Coco_Type {
int X;
int Y;
struct Nut_Type *Nuts;
} Coco_Rec = { 1, 2, &Nuts[0] };
int i;
main() {
/* Initialize Ada */
adainit();
/* Process */
printf("Initializing %i records in C.\n", MAX_NUTS);
for (i = 0 ; i < MAX_NUTS; i++) {
Coco_Rec.Nuts[i].Diameter = i;
Coco_Rec.Nuts[i].Weight = i;
Coco_Rec.Nuts[i].Age = i;
}
climb(Coco_Rec, MAX_NUTS);
/* Finalize Ada */
adafinal();
}
Dependencies:
caller: callee.ali caller.o
gnatbind -n callee.ali
gnatlink -o caller callee.ali caller.o
callee.ali: callee.adb callee.ads
gnatmake -c callee
caller.o: caller.c
gcc -c caller.c
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
prev parent reply other threads:[~2009-10-21 14:16 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
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 [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox