From: William S F. <ws...@fu...> - 2007-08-14 20:17:03
|
David Piepgrass wrote: > Suppose I get my shapes from a ShapeProvider, which is some sort of > collection: > > class Shape { > virtual const char* Name() { return "Shape"; } > }; > class Square : public Shape { > virtual const char* Name() { return "Square"; } > int SideLength() { return 10; } > }; > class Circle : public Shape { > virtual const char* Name() { return "Square"; } > int Radius() { return 10; } > }; > class ShapeProvider { > private: > Square s; > Circle c; > public: > Shape* Get(int index) { > if (index == 0) return &s; else return &c; > } > }; > > Unfortunately, when Get() is called, the SWIG creates a Shape wrapper > rather than a wrapper for the actual type of the object (C# example): > > public Shape Get(int index) { > IntPtr cPtr = NaviPINVOKE.ShapeProvider_Get(swigCPtr, index); > Shape ret = (cPtr == IntPtr.Zero) ? null : new Shape(cPtr, false); > return ret; > } > > What can I do to get the correct wrapper created? Preferably with only > one P/Invoke call... > See the discussion of downcasts in http://www.swig.org/Doc1.3/Java.html#adding_downcasts. This is mostly relevant to C#. You'd have to replace the JNI code in the final Vehicle *vehicle_factory out typemap with something more elaborate using delegates. Or have just a csout typemap for Vehicle *vehicle_factory which calls into unmanaged code to cycle through make the different possible C++ dynamic_cast calls. William |