Thursday, July 3, 2008

Wrapping structure

Decided on the wrapping solution. Will use a constructor that builds managed objects from native handles, the classes that are used through handles will have a protected native handle, the others an IntPtr (both of these accessed through a Handle property). Will have to add also some = operator overload and a copy constructor.

The trick for declaring the managed class hierarchy is the following:
class ManagedA
// The pointer/handle that will hold a reference to the unmanaged class.
NativeA* na;

// dummy constructor used to avoid the calls to the default constructor
// from the derived classes, used to avoid allocating na in the
// base and derived classes
ManagedA(ManagedA*) { };

ManagedA() { na = new NativeA(); }; // constructor with no parameters
ManagedA(int i) { na = new NativeA(); }; // constructor with parameters
// The pointer is released in the base class even if
// allocated in the child classes
virtual ~ManagedA() { delete na; };
int MFooA() {return na->NFooA(); }; // Base class method call

class ManagedB : public ManagedA
ManagedB (ManagedB *) : ManagedA((ManagedA*)0) { };

ManagedB () : ManagedA(0) { na = new NativeB(); };
ManagedB (int i) : ManagedA((ManagedA*)0) { na = new NativeB(); };
~ManagedB () { };
int MFooB() {return ((NativeB*)na)->NFooB(); };

It took a lot of time to adjust the code generator to create this type of structure and didn't finish integrating the project with a C# application. Suceeded to generate two classes from the Standard package that build with no manual modifications.

The highest priority now is to build a dll with the gp package and the other 4 packages that it depends on and use them in a C# project to detect any other possible adjustments needed.

There is no exception system yet. Will have to think also at this.

No comments: