C++ from Ada, again



Hello all,

Here's a new try to call C++ from Ada, while keeping as most as possible
the OOP semantic. The code:
http://kafka-fr.hd.free.fr/~yves/cpp_ada.tar.bz2

Now a bit of explanation. Two classes are declared in C++, "Base" and
"Derived", containing a normal method and a virtual method. The virtual
method is overdefined into "Derived".

The trick is to create a kind of "double-wrapper". The class "Wrapper"
contains only a "void*" pointer, to receive the address of an Ada object
(Jeffrey told System.Address are not equivalent to C pointer, but I saw
it too late - sorry, I'll do better next time).

Then a class "Base_Wrapper" is created, derived from both "Wrapper" and
"Base". It overdefine the virtual method, to call an exported Ada
procedure.

Then C functions wrappers are created for each of the method of
"Wrapper_Base", just forwarding the call to the corresponding method
of "Base". Those functions take "Wrapper_Base" pointers.

The Ada procedure calls another *imported* procedure, which itself
corresponds to the C wrapper just mentioned.

Why so much trouble ? Here's the nice part. If I create a new *Ada* type
derived from the "Base" type, then thanks to polymorphism in both Ada
and C++ the overdefined virtual procedure is actually called.

Sorry if I'm not clear, english is not my native language. Please, have
a quick look at the code, and tell me. For what I can tell, it uses only
standard features, and therefore should be portable among compilers
(when System.Address are changed to something always equivalent to C
pointers).

Thanks for your patience and best regards,

--
(o< | Yves Bailly  : http://kafka-fr.net   | -o)
//\ | Linux Dijon  : http://www.coagul.org | //\
\_/ |                                      | \_/`

.



Relevant Pages

  • Ada access vs C/C++ pointers and references
    ... In the "ADA Popularity Discussion Request" thread, ... of C/C++ pointers and Ada access types came up. ... It seems to me that Ada access types are more akin to C++ ... above, pointer arithmetic is partly ...
    (comp.lang.ada)
  • Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > in Ada. ... > it at hand anyway, but the gist of it is that, in Ada, loop variables ... I would expect a programmer to know the precedence ... > * pointer types cannot be converted to one another. ...
    (comp.lang.ada)
  • Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > in Ada. ... > it at hand anyway, but the gist of it is that, in Ada, loop variables ... I would expect a programmer to know the precedence ... > * pointer types cannot be converted to one another. ...
    (comp.lang.cpp)
  • Re: Ada Pointer Problem
    ... > Ada as a Second Language has a whole chapter describing access ... inability to find a 'pointer'. ... If I used a brass hammer I would be required to ...
    (comp.lang.ada)
  • Re: ADA Popularity Discussion Request
    ... > Theoretically Ada has better support for optimizations than C. ... This assumes that compiler is ... In Ada an object has to be ... In C99 you can declare a pointer 'restrict' meaning the compiler may ...
    (comp.lang.ada)