comp.lang.ada
 help / color / mirror / Atom feed
* Calling C++ from Ada
@ 2006-05-13 14:48 Yves Bailly
  2006-05-13 17:02 ` Jeffrey Creem
  0 siblings, 1 reply; 20+ messages in thread
From: Yves Bailly @ 2006-05-13 14:48 UTC (permalink / raw)


Hello all,

I'm trying to use C++ classes from an Ada program. Searching around, I
found some examples, but none implying a non-default constructor.
I tried some advices found in
http://www.pegasoft.ca/resources/boblap/19.html#19.5, which work well,
but it seems one can only use default constructors. Using various versions
of GNAT (including the one coming with the latest GCC 4.2.0), when trying
to use the pragma CPP_Constructor with a function taking more than zero
parameters, I get this message :
imported.ads:36:27: non-default constructors not implemented

Does anyone have any clue about this ?

Thanks and best regards,

    Yves Bailly




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

* Re: Calling C++ from Ada
  2006-05-13 14:48 Calling C++ from Ada Yves Bailly
@ 2006-05-13 17:02 ` Jeffrey Creem
  2006-05-13 19:08   ` Yves Bailly
  0 siblings, 1 reply; 20+ messages in thread
From: Jeffrey Creem @ 2006-05-13 17:02 UTC (permalink / raw)


Yves Bailly wrote:
> Hello all,
> 
> I'm trying to use C++ classes from an Ada program. Searching around, I
> found some examples, but none implying a non-default constructor.
> I tried some advices found in
> http://www.pegasoft.ca/resources/boblap/19.html#19.5, which work well,
> but it seems one can only use default constructors. Using various versions
> of GNAT (including the one coming with the latest GCC 4.2.0), when trying
> to use the pragma CPP_Constructor with a function taking more than zero
> parameters, I get this message :
> imported.ads:36:27: non-default constructors not implemented
> 
> Does anyone have any clue about this ?
> 
> Thanks and best regards,
> 
>     Yves Bailly
> 

Direct interface to C++ from Ada is outside the scope of the Ada (at 
least 95) LRM. GNAT has some vendor specific pragmas that can be used 
but I have never heard of any widespread use of these pragmas in any 
publically avaialble code.

There is a paper on the topic being presented at the GCC summit this summer

http://www.gccsummit.org/2006/view_abstract.php?content_key=26

In any case, outside of something heavy like an ORB or vendor specific 
such as GNATCOM I have not seen anything that looks to promising in this 
area yet.

You probably need to interface to it the same way a C programmer would - 
with non classed based externs.



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

* Re: Calling C++ from Ada
  2006-05-13 17:02 ` Jeffrey Creem
@ 2006-05-13 19:08   ` Yves Bailly
  2006-05-13 19:55     ` Jeffrey Creem
  2006-05-13 22:38     ` Craig Carey
  0 siblings, 2 replies; 20+ messages in thread
From: Yves Bailly @ 2006-05-13 19:08 UTC (permalink / raw)


On Sat, 13 May 2006 13:02:33 -0400, Jeffrey Creem wrote:
> Yves Bailly wrote:
>> I'm trying to use C++ classes from an Ada program. Searching around, I
>> found some examples, but none implying a non-default constructor.
> 
> Direct interface to C++ from Ada is outside the scope of the Ada (at 
> least 95) LRM. 

I'm aware of this. It's not much better either in the new 2005(6?7?)
standard.

> GNAT has some vendor specific pragmas that can be used
> but I have never heard of any widespread use of these pragmas in any
> publically avaialble code.

I tried to use them, with some success on simple things. At least for now,
these vendor-specific pragmas are not a real issue, I'm just experimenting.

> You probably need to interface to it the same way a C programmer would -
> with non classed based externs.

I guess I'll end doing this... 

To paint a little of the whole story, I was looking for a tool allowing
to automagically generate an Ada thin binding for a C++-based library.
To many classes and methods to create it "by hand". However I didn't find
any, so I was thinking on creating one myself.

Thanks anyway for your lights.

Best regards,

      Yves Bailly




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

* Re: Calling C++ from Ada
  2006-05-13 19:08   ` Yves Bailly
@ 2006-05-13 19:55     ` Jeffrey Creem
  2006-05-14  8:59       ` Yves Bailly
  2006-05-15 15:24       ` rodkay
  2006-05-13 22:38     ` Craig Carey
  1 sibling, 2 replies; 20+ messages in thread
From: Jeffrey Creem @ 2006-05-13 19:55 UTC (permalink / raw)


Yves Bailly wrote:

> 
> I guess I'll end doing this... 
> 
> To paint a little of the whole story, I was looking for a tool allowing
> to automagically generate an Ada thin binding for a C++-based library.
> To many classes and methods to create it "by hand". However I didn't find
> any, so I was thinking on creating one myself.
> 
> Thanks anyway for your lights.
> 
> Best regards,
> 
>       Yves Bailly
> 

If you are thinking of doing something yourself perhaps you should look 
at adding an Ada output module for SWIG.

Looking at some of the other language output modules it appears that it 
is a reasonably small project.



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

* Re: Calling C++ from Ada
  2006-05-13 19:08   ` Yves Bailly
  2006-05-13 19:55     ` Jeffrey Creem
@ 2006-05-13 22:38     ` Craig Carey
  1 sibling, 0 replies; 20+ messages in thread
From: Craig Carey @ 2006-05-13 22:38 UTC (permalink / raw)


On Sat, 13 May 2006 21:08:33 +0200, Yves Bailly wrote:
>On Sat, 13 May 2006 13:02:33 -0400, Jeffrey Creem wrote:
>> Yves Bailly wrote:
>>> I'm trying to use C++ classes from an Ada program. Searching around, I
>>> found some examples, but none implying a non-default constructor.

The SGI company had a tool (for Unix?) that inserted C++ symbols into
GNAT pragmas. Then SGI went bankrupt (at least, inside of USA. I don't
know what the cost was, before and after).

(I happen to now regard wXwindows as seeming less credible than GCC 2.8
Ada over Microsoft MFC of Visual C 6++).

There is a webpage on how Microsoft mangles C++ symbols, here:
    http://www.kegel.com/mangle.html

I am expect to port an eMule using Visual Studio MFC C++ to
MinGW so that it can use the future OpenGNAT for Windows, the modified
GCC (2.x) compiler based on the AdaCore 2002 program. I expect to have
Mr Brukardt doing some of the coding, though he is possibly too busy
anonymizing votes for IBM's Pascal Leroy (eg. AI-302, the C++
containers re-learning class for AdA; but is this too indiscreet?)

I missed out the word "better" in my last message here (re the
Asian Indian, Anath The Boss, CLA grade ... avionics expert).


Craig Carey




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

* Re: Calling C++ from Ada
  2006-05-13 19:55     ` Jeffrey Creem
@ 2006-05-14  8:59       ` Yves Bailly
  2006-05-14 13:39         ` Jeffrey Creem
  2006-05-14 18:38         ` Jeffrey R. Carter
  2006-05-15 15:24       ` rodkay
  1 sibling, 2 replies; 20+ messages in thread
From: Yves Bailly @ 2006-05-14  8:59 UTC (permalink / raw)


On Sat, 13 May 2006 15:55:38 -0400, Jeffrey Creem wrote:
> If you are thinking of doing something yourself perhaps you should look 
> at adding an Ada output module for SWIG.
> Looking at some of the other language output modules it appears that it 
> is a reasonably small project.

Interesting. I'll dig into this, it seems promising.

Sorry if I'm boring everyone here, but I would like to submit some
thoughts on a way to call C++ from Ada, through a C wrapper, avoiding
too much dynamic memory allocations.

The basic idea is to reserve the needed space on the Ada side, then to
use the placement "new" on the C++ side. 

A generic holder is declared this way :
--8<-----8<-----8<-----8<-----8<---
   package C renames Interfaces.C ;
   type Cpp_Holder(Size: C.size_t) is tagged limited
   record
      holder: C.char_array(1..Size) ;
   end record ;
--8<-----8<-----8<-----8<-----8<---

Now assume you want to use the standard std::string class from Ada.
This could be done like this :
--8<-----8<-----8<-----8<-----8<---
   Std_String_Size: constant C.size_t ; -- imported needed size
   pragma Import(C, Std_String_Size, "Std_String_Size") ;
   type Std_String is 
      new Cpp_Holder(Size=>Std_String_Size) with null record;
   -- imported C function to create an empty string
   procedure Std_String_String(where: System.Address) ;
   pragma Import(C, 
                 Std_String_String, 
                 "Std_String_String") ;
   -- Ada-side creation procedure
   procedure Create(ss: in out Std_String) is
   begin
      Std_String_String(ss.holder(1)'Address) ;
   end Create ;
--8<-----8<-----8<-----8<-----8<---

The C wrapper would be :

--8<-----8<-----8<-----8<-----8<---
   // std_string_wrapper.h
   #include <string>
   extern "C"
   {
      extern const size_t Std_String_Size ;
      void Std_String_String(std::string* where) ;
   }

   // std_string_wrapper.cpp
   #include "std_string_wrapper.h"
   extern "C"
   {
      const size_t Std_String_Size = sizeof(std::string) ;
      void Std_String_String(std::string* where)
      {
         new (where) std::string ;
      }
   }
--8<-----8<-----8<-----8<-----8<---

If you're interested and willing to give advices or comments, I've
build a more detailed example :
http://kafka-fr.hd.free.fr/~yves/cpp_holder.tar.bz2

It can be build using GNAT with
gprmake -P std_string.gpr
...tested using GCC 4.2.0 as of 20060512. The trivial test program
works fine, as far as I can tell for now.

Is this approach too nasty ? Is it more or less portable ?

Best regards,

           Yves Bailly




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

* Re: Calling C++ from Ada
  2006-05-14  8:59       ` Yves Bailly
@ 2006-05-14 13:39         ` Jeffrey Creem
  2006-05-14 18:38         ` Jeffrey R. Carter
  1 sibling, 0 replies; 20+ messages in thread
From: Jeffrey Creem @ 2006-05-14 13:39 UTC (permalink / raw)


Yves Bailly wrote:
> On Sat, 13 May 2006 15:55:38 -0400, Jeffrey Creem wrote:
> 
>>If you are thinking of doing something yourself perhaps you should look 
>>at adding an Ada output module for SWIG.
>>Looking at some of the other language output modules it appears that it 
>>is a reasonably small project.
> 
> 
> Interesting. I'll dig into this, it seems promising.
> 
> Sorry if I'm boring everyone here, but I would like to submit some
> thoughts on a way to call C++ from Ada, through a C wrapper, avoiding
> too much dynamic memory allocations.


I would not worry about boring anyone. This has been an area where there 
has always been interest since the early days of comp.lang.ada.

I think most here are interested in the topic.

> 
> The basic idea is to reserve the needed space on the Ada side, then to
> use the placement "new" on the C++ side. 
> 

The biggest problem I see with th is is ithat it is not clear that this 
really is portable. The problem is that the code is making assumptions 
about the layout of the std::string class and assumptions about what the 
compiler will do with respect to dope vectors. While nesting the string 
in the record probably makes it so that the compiler does not need to 
generate a dope vector, I am not sure if it requires that the compiler 
not do so. I suspect there are also other issues with respect to the 
class, tags, and other layout issues that are not guaranteed to be portable.

Having said that, it is worth pointing out that more "useful" 
implementations have been sacrificed on the alter of future portability 
in this newsgroup than I'd like to count. I hate to think of the number 
of approaches that were rejected due to portability and then replaced 
with something equally non-portable from a different language.





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

* Re: Calling C++ from Ada
  2006-05-14  8:59       ` Yves Bailly
  2006-05-14 13:39         ` Jeffrey Creem
@ 2006-05-14 18:38         ` Jeffrey R. Carter
  1 sibling, 0 replies; 20+ messages in thread
From: Jeffrey R. Carter @ 2006-05-14 18:38 UTC (permalink / raw)


Yves Bailly wrote:
> 
> Is this approach too nasty ? Is it more or less portable ?

Your assumption that System.Address is equivalent to a C pointer is not 
portable among compilers. A better approach might be to have your 
imported C creation subprogram take an Interfaces.C.Strings.Chars_Ptr, 
and pass an 'Unchecked_Access to Interfaces.C.Strings.To_Chars_Ptr 
instead of 'Address.

-- 
Jeff Carter
"Perfidious English mouse-dropping hoarders."
Monty Python & the Holy Grail
10



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

* Re: Calling C++ from Ada
  2006-05-13 19:55     ` Jeffrey Creem
  2006-05-14  8:59       ` Yves Bailly
@ 2006-05-15 15:24       ` rodkay
  2006-05-15 16:23         ` Jeffrey Creem
  2006-05-16  2:14         ` Gene
  1 sibling, 2 replies; 20+ messages in thread
From: rodkay @ 2006-05-15 15:24 UTC (permalink / raw)


   I'm trying to build an Ada module for SWIG, at the moment. The
current prototype produces rough Ada bindings for several of the SWIG
testsuite cases, and seems to handle some of the simpler C/C++ librarys
(such as ODE dynamics engine).

   If there is sufficient interest, I can try to have the module placed
into SWIG cvs.




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

* Re: Calling C++ from Ada
  2006-05-15 15:24       ` rodkay
@ 2006-05-15 16:23         ` Jeffrey Creem
  2006-05-16  2:14         ` Gene
  1 sibling, 0 replies; 20+ messages in thread
From: Jeffrey Creem @ 2006-05-15 16:23 UTC (permalink / raw)


rodkay@mullum.com.au wrote:
>    I'm trying to build an Ada module for SWIG, at the moment. The
> current prototype produces rough Ada bindings for several of the SWIG
> testsuite cases, and seems to handle some of the simpler C/C++ librarys
> (such as ODE dynamics engine).
> 
>    If there is sufficient interest, I can try to have the module placed
> into SWIG cvs.
> 

Even if it were not 100% complete, getting it into CVS as a starting 
point would be great. There was at least one ther effort a while ago 
where someone worked on it and then walked away and nothing ever got saved.

Keep us informed.



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

* Re: Calling C++ from Ada
  2006-05-15 15:24       ` rodkay
  2006-05-15 16:23         ` Jeffrey Creem
@ 2006-05-16  2:14         ` Gene
  2006-05-16  4:31           ` rodkay
  1 sibling, 1 reply; 20+ messages in thread
From: Gene @ 2006-05-16  2:14 UTC (permalink / raw)



rodkay@mullum.com.au wrote:
> I'm trying to build an Ada module for SWIG, at the moment. The
> current prototype produces rough Ada bindings for several of the SWIG
> testsuite cases, and seems to handle some of the simpler C/C++ librarys
> (such as ODE dynamics engine).
>
>    If there is sufficient interest, I can try to have the module placed
> into SWIG cvs.

Please!  Yes!  Put it in CVS.  I'll help if I can get some time...




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

* Re: Calling C++ from Ada
  2006-05-16  2:14         ` Gene
@ 2006-05-16  4:31           ` rodkay
  2006-05-16 11:47             ` Jeffrey Creem
  0 siblings, 1 reply; 20+ messages in thread
From: rodkay @ 2006-05-16  4:31 UTC (permalink / raw)


   Ok, I'll try to get the module into cvs, asap.

    Current work is on 'directors' which manage the upcall from C++
virtual functions into Ada. When that is complete, the move into cvs
will hopefully take place (which seems to involve writing an Ada
chapter for the SWIG docs, hooking the Ada module into the formal SWIG
testsuite, and a general tidying of the actual module code).

   I'll let you know should any progress be made.




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

* Re: Calling C++ from Ada
  2006-05-16  4:31           ` rodkay
@ 2006-05-16 11:47             ` Jeffrey Creem
  2006-05-17  4:53               ` rodkay
  0 siblings, 1 reply; 20+ messages in thread
From: Jeffrey Creem @ 2006-05-16 11:47 UTC (permalink / raw)


rodkay@mullum.com.au wrote:
>    Ok, I'll try to get the module into cvs, asap.
> 
>     Current work is on 'directors' which manage the upcall from C++
> virtual functions into Ada. When that is complete, the move into cvs
> will hopefully take place (which seems to involve writing an Ada
> chapter for the SWIG docs, hooking the Ada module into the formal SWIG
> testsuite, and a general tidying of the actual module code).
> 
>    I'll let you know should any progress be made.
> 

If you'd like we could setup a temporary subdirectory in the GNUAda SVN 
area where you can dump a local copy and perhaps get some collaberation 
on things like the test suite and documentation.

If you are days away from getting CVS write access then this is probably 
not worth it. If you are weeks away, then it would probably make sense.



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

* Re: Calling C++ from Ada
  2006-05-16 11:47             ` Jeffrey Creem
@ 2006-05-17  4:53               ` rodkay
  2006-05-17  6:05                 ` Martin Krischik
  0 siblings, 1 reply; 20+ messages in thread
From: rodkay @ 2006-05-17  4:53 UTC (permalink / raw)


   A temporary subdirectory in GNUAda SVN sounds ideal. I hope to
finish the initial work on directors, within the next day or two. Add a
few days to clean up the code a little, and perhaps it might be ready
for svn at the end of next week.

   Thanks for providing this option. I think the code is probably still
too immature for the SWIG repository. Allowing community access via
GNUAda svn should prove a great help.




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

* Re: Calling C++ from Ada
  2006-05-17  4:53               ` rodkay
@ 2006-05-17  6:05                 ` Martin Krischik
  2006-05-25  5:34                   ` rodkay
  0 siblings, 1 reply; 20+ messages in thread
From: Martin Krischik @ 2006-05-17  6:05 UTC (permalink / raw)


Send a support request [1] with your sourceforge name to the gnuada
project [2] and one of the four administrators will take care of it.

Martin

[1] http://sourceforge.net/tracker/?group_id=12974&atid=212974
[2] http://sourceforge.net/projects/gnuada/




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

* Re: Calling C++ from Ada
  2006-05-17  6:05                 ` Martin Krischik
@ 2006-05-25  5:34                   ` rodkay
  2006-05-25 11:29                     ` Alex R. Mosteo
  0 siblings, 1 reply; 20+ messages in thread
From: rodkay @ 2006-05-25  5:34 UTC (permalink / raw)


   The code, such as it is,  has been placed in svn and is available
via:

   svn co
https://svn.sourceforge.net/svnroot/gnuada/trunk/projects/swig-1.3.29

   To build and install, the usual './configure, make, make install'
method applies. You may also need to run './autogen.sh', before
'./configure'

   The SWIG manual is very helpful. There is also a brief README.Ada in
the top level directory.




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

* Re: Calling C++ from Ada
  2006-05-25  5:34                   ` rodkay
@ 2006-05-25 11:29                     ` Alex R. Mosteo
  2006-05-29  3:43                       ` Gene
  0 siblings, 1 reply; 20+ messages in thread
From: Alex R. Mosteo @ 2006-05-25 11:29 UTC (permalink / raw)


rodkay@mullum.com.au wrote:

>    The code, such as it is,  has been placed in svn and is available
> via:
> 
>    svn co
> https://svn.sourceforge.net/svnroot/gnuada/trunk/projects/swig-1.3.29
> 
>    To build and install, the usual './configure, make, make install'
> method applies. You may also need to run './autogen.sh', before
> './configure'
> 
>    The SWIG manual is very helpful. There is also a brief README.Ada in
> the top level directory.

I have just tested the enum example. Thanks for your effort, I really think
that getting Ada into SWIG could be a great leap forward for Ada,
furthermore if it also supports C++.

Best luck,

Alex.




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

* Re: Calling C++ from Ada
  2006-05-25 11:29                     ` Alex R. Mosteo
@ 2006-05-29  3:43                       ` Gene
  2006-05-29 14:53                         ` rodkay
  2006-05-29 17:48                         ` Martin Krischik
  0 siblings, 2 replies; 20+ messages in thread
From: Gene @ 2006-05-29  3:43 UTC (permalink / raw)


Alex R. Mosteo wrote:
> rodkay@mullum.com.au wrote:
>
> >    The code, such as it is,  has been placed in svn and is available
> > via:
> >
> >    svn co
> > https://svn.sourceforge.net/svnroot/gnuada/trunk/projects/swig-1.3.29
> >

THANKS!  This is great!  Terrific start in a short time.

FWIW I have built successfully under MINGW/MSYS.

I have a couple of bug fixes.  How would you like to handle them?

Gene




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

* Re: Calling C++ from Ada
  2006-05-29  3:43                       ` Gene
@ 2006-05-29 14:53                         ` rodkay
  2006-05-29 17:48                         ` Martin Krischik
  1 sibling, 0 replies; 20+ messages in thread
From: rodkay @ 2006-05-29 14:53 UTC (permalink / raw)


   Thanks for the bug fixes and trying out the MINGW/MSYS build.

   For the bug fixes, can you obtain svn write access ? If not, perhaps
you could send an email (contact details are at the bottom of
'README.Ada') ?




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

* Re: Calling C++ from Ada
  2006-05-29  3:43                       ` Gene
  2006-05-29 14:53                         ` rodkay
@ 2006-05-29 17:48                         ` Martin Krischik
  1 sibling, 0 replies; 20+ messages in thread
From: Martin Krischik @ 2006-05-29 17:48 UTC (permalink / raw)


Gene wrote:

> I have a couple of bug fixes. ï¿œHow would you like to handle them?

Make a support request on SourceForge for write access to svn. I might not
allways have the time but there are 4 administrators so it should not take
to long even then.

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



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

end of thread, other threads:[~2006-05-29 17:48 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-13 14:48 Calling C++ from Ada Yves Bailly
2006-05-13 17:02 ` Jeffrey Creem
2006-05-13 19:08   ` Yves Bailly
2006-05-13 19:55     ` Jeffrey Creem
2006-05-14  8:59       ` Yves Bailly
2006-05-14 13:39         ` Jeffrey Creem
2006-05-14 18:38         ` Jeffrey R. Carter
2006-05-15 15:24       ` rodkay
2006-05-15 16:23         ` Jeffrey Creem
2006-05-16  2:14         ` Gene
2006-05-16  4:31           ` rodkay
2006-05-16 11:47             ` Jeffrey Creem
2006-05-17  4:53               ` rodkay
2006-05-17  6:05                 ` Martin Krischik
2006-05-25  5:34                   ` rodkay
2006-05-25 11:29                     ` Alex R. Mosteo
2006-05-29  3:43                       ` Gene
2006-05-29 14:53                         ` rodkay
2006-05-29 17:48                         ` Martin Krischik
2006-05-13 22:38     ` Craig Carey

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