C++ class structure mapping

Derek Elkins ddarius@hotpop.com
Tue, 17 Jun 2003 06:33:02 -0400


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

> 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.]

I'm assuming you don't want overloading here as these aren't functions
that can be called in C (or C99?).

> 
> 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. 

If the code you are interfacing to doesn't make too much use of
templates then you can do what I did and hack together a SWIG module to
flatten the C++ to C (I had hoped that that would already be available
as it effectively does that anyways but I couldn't find it).