SWIG - Polymorphic class as parameter
-
19-10-2022 - |
Question
I have a C++ library that I want to wrap using SWIG to use it with C# and Java. I am having some trouble in writing the interface for methods that make use of polymorphic parameters.
For e.g: In my C++ library code, I have a method that does this.
long FindShapes(vector<tr1::shared_ptr<CShapes> > &fArray, int ShapeType);
This instantiates an appropriate shape (square, circle or polygon) object and fills it into the vector.
In my C# code,
using (ShapesVec shapes = new ShapesVec())
{
Result rs = ShapesLibCS.FindShapes(shapes, 1); // 1 = Circle
for (int i = 0; i < shapes .Count; ++i)
{
Circle circle = shapes[i] as Circle;
}
}
Here, the casting of the Shape object to Circle fails. I came across an article that handles the polymorphic return types. (http://www.voom.net/swig-csharp-java-downcast). Here he instructs SWIG to instantiate the concrete class based on the enum value. But this doesn't help in my case as shapes[i] calls the get_item of the Vector. And in the generated code for that, it always forces the instantiation of the abstract class.
What can I do to solve the problem in my case?
Thanks
No correct solution