C++ class structure mapping

Alastair Reid alastair@reid-consulting-uk.ltd.uk
Tue, 17 Jun 2003 09:07:06 +0100


On Tuesday 17 June 2003 2:33 am, Abraham Egnor wrote:
> I'd like to make a haskell binding for a C++ library.  Most of the tools
> out there seem oriented towards c bindings, so it looks like I'll be
> writing the FFI code generator myself.  While I'm at it I figure I might
> as well just make a general C++ binding tool.  I've been thinking about
> this, and I'd like some feedback on the ideas I've had.

All previous interfaces to C++ libraries that I know of have been created by 
writing a bunch of wrapper functions with C-style types like these wrapper 
for a virtual function 'foo'.

  void foo_T(S x) { x->foo(); }
  void foo_T(T x) { x->foo(); }
  void foo_T(U x) { x->foo(); }

[Note that these are C++ functions but that their type is one that C programs 
can call.]

People have shied away from trying to create a more direct interface because:

1) C++ is a huge language (with the complexity mostly in types,  classes and 
templates where you can't easily avoid them) and it seems that to be able to 
cope with any reasonable number of C++ libraries, you'd have to implement a 
large part of that language.

2) C++'s type/ class system is quite different from Haskell's class system.  
Any mapping of on to the other is likely to fall apart rather quickly. 

That said, I think H/Direct (http://haskell.org/hdirect/) is able to help a 
little with C++ and, even if it doesn't, it seems likely that an approach 
based on having Haskell and C++ interface to a third intermediate language 
(i.e., IDL) is most likely to work.

--
Alastair Reid