www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Using C++ classes in D?

reply Jan <Jan_member pathlink.com> writes:
I have a set of some C++ classes which I want to use with D too. It would be a
lot of work to port them all to D, and some of them I have only as a DLL-file,
without sourcecode.

So my question is short and simple :)
Is there any way to use C++ classes in D?
I know, D classes are different from C++ classes, but maybe there's a trick? ;)

Thx,
Jan
Jun 14 2004
next sibling parent reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Jan" <Jan_member pathlink.com> wrote in message
news:caldc7$2p69$1 digitaldaemon.com...
 I have a set of some C++ classes which I want to use with D too. It would be a
 lot of work to port them all to D, and some of them I have only as a DLL-file,
 without sourcecode.

 So my question is short and simple :)
 Is there any way to use C++ classes in D?

Only via C. You can provide an API by which your C++ classes are represented as handles to the D side, and methods are represented as free functions taking the instance handle as an argument, inside which it is cast back to the C++ type and the method called. If you're C++ is nicely laid out, you should be able to script this with Perl/Python/Ruby.
 I know, D classes are different from C++ classes, but maybe there's a trick? ;)

 Thx,
 Jan

Jun 14 2004
next sibling parent reply John Fletcher <J.P.Fletcher aston.ac.uk> writes:
Matthew wrote:

 "Jan" <Jan_member pathlink.com> wrote in message
 news:caldc7$2p69$1 digitaldaemon.com...
 I have a set of some C++ classes which I want to use with D too. It would be a
 lot of work to port them all to D, and some of them I have only as a DLL-file,
 without sourcecode.

 So my question is short and simple :)
 Is there any way to use C++ classes in D?

Only via C. You can provide an API by which your C++ classes are represented as handles to the D side, and methods are represented as free functions taking the instance handle as an argument, inside which it is cast back to the C++ type and the method called. If you're C++ is nicely laid out, you should be able to script this with Perl/Python/Ruby.
 I know, D classes are different from C++ classes, but maybe there's a trick? ;)

 Thx,
 Jan


Hi The good news is that there is a means of generating the interface code automatically. Andy Friesen has adapted SWIG so that it works to produce an interface for D. I have been using that and tackling some of the problems which arise when wanting to interface a set of C++ classes to D. My progress so far is reported on the D wiki at the reference below. http://www.prowiki.org/wiki4d/wiki.cgi?DwithSwig I have solved some problems and am still tackling some others. The end result will be a revised version of Andy Friesen's work, which I have yet to report back to him. You will find an uptodate reference to his work on the wiki page. The one on the D links page on Walter's web site is out of date. If you get a copy of Andy Friesens's code I can let you have a modified version and some examples for D, which are not in his package. SWIG works on WIndows and Linux. I am mainly using it on Linux at the moment. If you have not used SWIG before do feel free to contact me. The documentation for SWIG is good, except for anything D specific which has still to be written. Cheers John Fletcher
Jun 15 2004
parent reply Trejkaz Xaoza <trejkaz xaoza.net> writes:
Rats.  Sorry about my post stating the same info, I would have noticed you
already mentioned SWIG if I had access to my normal threaded newsreader.  My
bad. :-(

In article <40CEBB1E.E38DF81E aston.ac.uk>, John Fletcher says...
The good news is that there is a means of generating the interface code
automatically.  Andy Friesen has adapted SWIG so that it works to produce an
interface for D.  I have been using that and tackling some of the problems which
arise when wanting to interface a set of C++ classes to D.  My progress so far
is
reported on the D wiki at the reference below.

http://www.prowiki.org/wiki4d/wiki.cgi?DwithSwig

Jun 15 2004
parent John Fletcher <J.P.Fletcher aston.ac.uk> writes:
Trejkaz Xaoza wrote:

 Rats.  Sorry about my post stating the same info, I would have noticed you
 already mentioned SWIG if I had access to my normal threaded newsreader.  My
 bad. :-(

 In article <40CEBB1E.E38DF81E aston.ac.uk>, John Fletcher says...
The good news is that there is a means of generating the interface code
automatically.  Andy Friesen has adapted SWIG so that it works to produce an
interface for D.  I have been using that and tackling some of the problems which
arise when wanting to interface a set of C++ classes to D.  My progress so far
is
reported on the D wiki at the reference below.

http://www.prowiki.org/wiki4d/wiki.cgi?DwithSwig


No problem. I have been working on making it easier to wrap C++ classes using SWIG, and some of the results are in an update of the wiki page I reference above. I hope very soon to mke available a version which will make it easy to wrap quite complex sets of C++ classes. John
Jun 17 2004
prev sibling parent Trejkaz Xaoza <trejkaz xaoza.net> writes:
In article <caldlf$2pl5$1 digitaldaemon.com>, Matthew says...
"Jan" <Jan_member pathlink.com> wrote in message
news:caldc7$2p69$1 digitaldaemon.com...
 I have a set of some C++ classes which I want to use with D too. It would be
 a lot of work to port them all to D, and some of them I have only as a
 DLL-file, without sourcecode.

 So my question is short and simple :)
 Is there any way to use C++ classes in D?

Only via C. You can provide an API by which your C++ classes are represented as handles to the D side, and methods are represented as free functions taking the instance handle as an argument, inside which it is cast back to the C++ type and the method called. If you're C++ is nicely laid out, you should be able to script this with Perl/Python/Ruby.

There is also SWIG for this sort of purpose, although SWIG is quite daunting IMO. But at least someone has integrated DMD support into SWIG... just not into the official release. A google search will pull it up though. [I was looking into this heavily a couple of weeks ago when I started lamenting the lack of Qt in D. Two weeks later, I still lament the lack of Qt in D, but am fairly comfortable with the fact that writing the wrapper is beyond my abilities. I think the only way to get the damn thing is to convince someone in the KDE project that D is cool enough to go into kdebindings. :-)] TX
Jun 15 2004
prev sibling parent John Fletcher <J.P.Fletcher aston.ac.uk> writes:
Jan wrote:

 I have a set of some C++ classes which I want to use with D too. It would be a
 lot of work to port them all to D, and some of them I have only as a DLL-file,
 without sourcecode.

 So my question is short and simple :)
 Is there any way to use C++ classes in D?
 I know, D classes are different from C++ classes, but maybe there's a trick? ;)

 Thx,
 Jan

Jan To use SWIG as I have suggested in my other message, you will need access to two things I have not mentioned (1) The header files for your C++ code (2) The relevant C++ compiler to compile some C++ wrapping code which will be generated. Also the D compiler and a text editor, which I assume you will have. I tried to Email you a direct copy of my other posting but it has been returned. John
Jun 15 2004