comp.lang.ada
 help / color / mirror / Atom feed
* ADTs C to Ada
@ 2012-03-05 14:09 Patrick
  2012-03-05 15:23 ` Ludovic Brenta
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Patrick @ 2012-03-05 14:09 UTC (permalink / raw)


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.

I am not really a programmer and master of neither C or Ada but I don't mind writing a little C to initialize a library. I was hoping to then control this ADT from Ada.

Is there any documentation on how to do this? Do I need to write a full thin binding to a library and interface each data type and function seperately? I am interested in gstreamer right now about there are several C libraries I would like to interface with. At the moment I would like too create a gstreamer instance and then play, pause and seek an audio recording.

Thanks for reading-Patrick



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ADTs C to Ada
  2012-03-05 14:09 ADTs C to Ada Patrick
@ 2012-03-05 15:23 ` Ludovic Brenta
  2012-03-05 16:03 ` Patrick
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ludovic Brenta @ 2012-03-05 15:23 UTC (permalink / raw)


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.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ADTs C to Ada
  2012-03-05 14:09 ADTs C to Ada Patrick
  2012-03-05 15:23 ` Ludovic Brenta
@ 2012-03-05 16:03 ` Patrick
  2012-03-05 16:08 ` Simon Wright
  2012-03-05 16:58 ` Georg Bauhaus
  3 siblings, 0 replies; 5+ messages in thread
From: Patrick @ 2012-03-05 16:03 UTC (permalink / raw)


Thanks once again Ludovic

I'll study this and give it a try. I'm betting that I will have to replace the main c file with an Ada file that will import the various pieces from C to provide it to Ada.

Thanks again



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ADTs C to Ada
  2012-03-05 14:09 ADTs C to Ada Patrick
  2012-03-05 15:23 ` Ludovic Brenta
  2012-03-05 16:03 ` Patrick
@ 2012-03-05 16:08 ` Simon Wright
  2012-03-05 16:58 ` Georg Bauhaus
  3 siblings, 0 replies; 5+ messages in thread
From: Simon Wright @ 2012-03-05 16:08 UTC (permalink / raw)


Patrick <patrick@spellingbeewinnars.org> writes:

> 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.

The thing to be careful of here is what the library does to the
pointer-to-subprogram. It's most likely to store it away somwehere and
call it in a different context, which would mean that the subprogram
might get called without the Ada environment it expects. See my response
in this thread:
https://groups.google.com/forum/?fromgroups#!searchin/comp.lang.ada/.threads/comp.lang.ada/Kl9hmcPMDos/-H_XoJYQvDkJ



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ADTs C to Ada
  2012-03-05 14:09 ADTs C to Ada Patrick
                   ` (2 preceding siblings ...)
  2012-03-05 16:08 ` Simon Wright
@ 2012-03-05 16:58 ` Georg Bauhaus
  3 siblings, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2012-03-05 16:58 UTC (permalink / raw)


On 05.03.12 15:09, Patrick wrote:
> 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.

> Is there any documentation on how to do this? Do I need to write a full thin binding to a library and interface each data type and function seperately? I am interested in gstreamer right now about there are several C libraries I would like to interface with. At the moment I would like too create a gstreamer instance and then play, pause and seek an audio recording.

In case you want to map Ada's O-O model to that of gstreamer,
which seems to be based on glib, a look at GtkAda can show
a way of how to do it, I think.

Side note:
One could imagine the O-O design of the GNU Network *Object* Model
Environment, or whatever modernization it has turned into,
to use an O-O data model / ABI that is easily used with O-O
languages.
Instead, we are charged with working around home brew style
look-I-can-write-fnatastic-O-O-in-C-too, just like in real ADT
languages...




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-03-05 16:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-05 14:09 ADTs C to Ada Patrick
2012-03-05 15:23 ` Ludovic Brenta
2012-03-05 16:03 ` Patrick
2012-03-05 16:08 ` Simon Wright
2012-03-05 16:58 ` Georg Bauhaus

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