www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Accessing more of C++ directly in D

reply Robert Clipsham <robert octarineparrot.com> writes:
Hey all,

I've recently worked on some code allowing direct access to class fields 
and non-virtual class members in C++, and was wondering if anyone else 
would find it useful? If so, would it be worth making a pull request for 
phobos? Should that be the case, where should it go?

Example usage (requires 
https://github.com/D-Programming-Language/dmd/pull/131 ):
----
// someFile.cpp
class A
{
     int someInt;
     void* ptr;
     void myFunc(){}
     int add(int a, int b)
     {
         return a + b;
     }
     virtual void someVirtFunc();
}

// someFile.d
extern(C++)
interface A
{
     mixin CppFields!(A,
         int, "someInt",
         void*, "ptr"
     );
     mixin CppMembers!(A,
         "myFunc", void,
         "add", int, int, int
     );
     void someVirtFunc();
     final void myDMethod()
     {
         myFunc();
         someVirtFunc();
         writefln("someInt: %s; ptr: %s; 1 + 1: %s", someInt,
                                                     ptr,
                                                     add(1, 1)));
     }
}
mixin(A.cppMethods);
----

I also have plans to add support for constructors/destructors if I get 
chance/it's not too difficult.

-- 
Robert
http://octarineparrot.com/
Jun 19 2011
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Robert Clipsham" <robert octarineparrot.com> wrote in message 
news:itl7b4$2m18$1 digitalmars.com...
 Hey all,

 I've recently worked on some code allowing direct access to class fields 
 and non-virtual class members in C++, and was wondering if anyone else 
 would find it useful? If so, would it be worth making a pull request for 
 phobos? Should that be the case, where should it go?
I'd find it useful. Better compatability with C++ can only be a good thing for D!
Jun 20 2011