From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: ADTs C to Ada
Date: Mon, 5 Mar 2012 07:23:55 -0800 (PST)
Date: 2012-03-05T07:23:55-08:00 [thread overview]
Message-ID: <15634690.1963.1330961035534.JavaMail.geo-discussion-forums@vbas10> (raw)
In-Reply-To: <28640131.3602.1330956581432.JavaMail.geo-discussion-forums@vbw15>
Patrick wrote on comp.lang.ada:
> I am studying Ada and making progress. The C interface seems straight forward enough for functions, strings pointers and such but I am having trouble finding any information on how to interface with stucts that contain functions.
structs cannot contain functions; they can only contain pointers to functions. You can call the pointed-to functions from Ada like this:
-*- C -*-
typedef char (*function_pointer_t) (int);
struct struct_t {
function_pointer_t f;
};
void foo(struct struct_t s) {
char result = s.f(42);
}
-*- Ada -*-
type Function_Pointer is access function (Param : in Integer) return Character;
pragma Convention (C, Function_Pointer);
type Struct is record
F : Function_Pointer;
end record;
pragma Convention (C, Struct);
procedure Foo (S : in Struct) is
Result : Character := S.F.all (Param => 42);
begin
null;
end Foo;
If you are really interfacing with C++, where structs can contain "member functions", then you need to delve into the C++ interfacing which is specific to GNAT. The compiler must learn about the vtable of the imported structs, about their constructors and destructors, etc.
HTH
--
Ludovic Brenta.
next prev parent reply other threads:[~2012-03-05 15:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-05 14:09 ADTs C to Ada Patrick
2012-03-05 15:23 ` Ludovic Brenta [this message]
2012-03-05 16:03 ` Patrick
2012-03-05 16:08 ` Simon Wright
2012-03-05 16:58 ` Georg Bauhaus
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox