comp.lang.ada
 help / color / mirror / Atom feed
* C++ from Ada, again
@ 2006-05-18 22:08 Yves Bailly
  2006-05-19  9:19 ` news.hinet.net
  2006-05-20 15:12 ` Yves Bailly
  0 siblings, 2 replies; 4+ messages in thread
From: Yves Bailly @ 2006-05-18 22:08 UTC (permalink / raw)


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 | //\
\_/ |                                      | \_/`




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

* Re: C++ from Ada, again
  2006-05-18 22:08 C++ from Ada, again Yves Bailly
@ 2006-05-19  9:19 ` news.hinet.net
  2006-05-19 10:50   ` Yves Bailly
  2006-05-20 15:12 ` Yves Bailly
  1 sibling, 1 reply; 4+ messages in thread
From: news.hinet.net @ 2006-05-19  9:19 UTC (permalink / raw)


hello ,all:


    there are some articles discussing about "how to write a COM in plain 
c".
we know COM and C++ share almost the same memory layout structure.
and ada has a c interface.
It make you make a interface for ada.

 http://www.codeproject.com/com/com_in_c1.asp
http://www.codeproject.com/com/com_in_c2.asp
http://www.codeproject.com/com/com_in_c3.asp
http://www.codeproject.com/useritems/com_in__c4.asp

 I never try it but I think it should be work. 





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

* Re: C++ from Ada, again
  2006-05-19  9:19 ` news.hinet.net
@ 2006-05-19 10:50   ` Yves Bailly
  0 siblings, 0 replies; 4+ messages in thread
From: Yves Bailly @ 2006-05-19 10:50 UTC (permalink / raw)


news.hinet.net wrote:
>     there are some articles discussing about "how to write a COM in plain
> c".
> we know COM and C++ share almost the same memory layout structure.
> and ada has a c interface.
> It make you make a interface for ada.

However, it seems it works only for Windows, right ? I'm looking for
a portable solution...

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



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

* Re: C++ from Ada, again
  2006-05-18 22:08 C++ from Ada, again Yves Bailly
  2006-05-19  9:19 ` news.hinet.net
@ 2006-05-20 15:12 ` Yves Bailly
  1 sibling, 0 replies; 4+ messages in thread
From: Yves Bailly @ 2006-05-20 15:12 UTC (permalink / raw)


Hello all,

I've refined my last try to call C++ from Ada. Grab it there:
http://kafka-fr.hd.free.fr/~yves/cpp_from_ada.tar.bz2

Now memory is dynamically allocated, which I wanted to avoid. But
I didn't find another way to keep the inheritance semantic. So basically,
given a C++ class "Base" and a derived class "Derived":

class Base 
{ ... };
class Derived: public Base 
{ ... };

...now you keep this model into Ada:

type Base is tagged ... -- details omitted here
type Derived is new Base with ... -- details omitted here

So the C++ hierarchy is reflected in Ada.

If you create new derived type into Ada, overriding virtual methods,
the dispatching occures as you would expect. More precisely, the C++
class contains:

class Base
{ public:
    void method()
    { 
       // calls the virtual method
       virtual_method(); 
    }
    virtual void virtual_method()
    { /* ...something...*/ }
};
(actually the code contains many std::cout to keep track of the flow)

This is translated in Ada by:

type Base is tagged ... -- details omitted here
not overriding procedure Method(b: in Base'Class);
not overriding procedure Virtual_Method(b: in Base);

Now suppose you derive from Base:

type Sub is new Base with null record;
overriding procedure Virtual_Method(s: in Sub);

Then create an instance:
s: Sub;

...when you invoke Method(s), the Virtual_Method() called is the
overriden one.

I tried to avoid as much as possible any compiler- or platform-specific
features. However I don't have access to anything else than Gnat and
a Linux/i386 platform. Thanks to point me anything not portable.

Any comments welcome ! I'm not that experienced in those things...

Best regards,

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



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

end of thread, other threads:[~2006-05-20 15:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-18 22:08 C++ from Ada, again Yves Bailly
2006-05-19  9:19 ` news.hinet.net
2006-05-19 10:50   ` Yves Bailly
2006-05-20 15:12 ` Yves Bailly

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