I'm not sure on how to best bind C++ code to D. D can handle some parts of the C++ ABI, like classes and virtual functions. dlang.org contains some information:
Now extern(C++) interface allow to access to virtual and non-virtual (with final annotation) methods, static methods. After my pull for posix and fix windows mangling (in work) static variables also be allowed.
I've idea, how we can get access to fields:

//C++
class Foo
{
  public:
    int a;
    int b;
  //virtual methods and other stuff
};

//Glue C++ (automatically generated)

int& __accessor_Foo_a(Foo* f){return f->a;}
int& __accessor_Foo_b(Foo* f){return f->b;}
//also we can get access to private/protected methods with some header hack if in needed

//D header

extern(C++)
{
  interface Foo
  {
     //we can implement final methods in interface
     @property final ref int a() {return __accessor_Foo_a(this);}
     @property final ref int b() {return __accessor_Foo_b(this);}
     //other stuff
  }
  ref int __accessor_Foo_a(Foo);
  ref int __accessor_Foo_b(Foo);
}

for get access to different special functions (like constructors, destructors, operators) we can use special methods + pragma(mangle)

interface Foo
{
  pragma(mangle, getCPPOperatorMangle!(Foo.__operator_idx, "[]"))
int& __operator_idx(size_t); //get access to int& Foo::operator[](size_t)
}

There is small trouble with inlined functions:
//C++
class Foo
{
  protected:
int foo(){return 5;} //this function is inline and there's a possibility that this function won't be written into object file.
};

In this case, we must to do some actions to force write foo into object file.


This is my ideas about binding C++ to D :)
If I'll do all as I want, we'll get a simple interface to access to most of C++ code (except templates).

Reply via email to