I am facing an issue in SWIG.
As explained in the doc here, SWIG does not support downcasts in Java and C#. I followed the advices of the doc, and create the correct typemaps for the factory of objects. I can know create an object A and downcast it to B.
However, I also have a method returning a vector of pointers to object A. I wrote some typemaps in order to use the factory I made. But then, I can't downcast any element of the vector.
Here is an example:
class A { public:
string name;
void someMethod();
... }
class B : public A {
specialBMethod();
... }
class C : public A {
specialCMethod();
... }
std::vector<std::shared_ptr<A>> someRandomMethod();
And now, I want to this in C# like:
A tmp = someFactoryMethod("B") // will return a B class;
(B)tmp.specialBMethod(); // works fine;
A_vector test = someRandomMethod();
if (test[0].name == "B")
(B)tmp.specialBMethod(); // InvalidCastException
The funniest part, is that if I make a copy of the vector, using CopyTo, and put it in an array for example, it works.
A_vector tmp = someRandomMethod();
A[] test = tmp.ToArray(); // imagine the ToArray method was implemented
if (test[0].name == "B")
(B)tmp.specialBMethod(); // works fine
I think there is a problem when the vector return a const reference of the object. It losts the inheritance and becomes impossible to downcast.
I am pretty lost in this situation. I also opened an issue on the SWIG repo. Any help would be great;
EDIT : as Flexo asked for, here a minimal, complete example.
example.hpp
class A {
string name;
public:
void someMethod();
string getName() { return name; }
... }
class B : public A {
specialBMethod();
... }
class C : public A {
specialCMethod();
... }
std::vector<std::shared_ptr<A>> someRandomMethod();
example.i
%{
#include "example.hpp"
%}
%pragma(csharp) imclasscode=%{
public static System.Collections.Generic.Dictionary<string, System.Type> aDictionary;
public static System.Collections.Generic.Dictionary<string, System.Type> createDictionary<T>() where T : class
{
System.Collections.Generic.Dictionary<string, System.Type> dictionary = new System.Collections.Generic.Dictionary<string, System.Type>();
foreach (System.Type type in
System.Reflection.Assembly.GetAssembly(typeof(T)).GetTypes())
{
if (type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(T)))
{
string tmp = type.ToString().Split('.')[type.ToString().Split('.').Length - 1].Substring(0, type.ToString().Split('.')[type.ToString().Split('.').Length - 1].IndexOf(typeof(T).Name));
dictionary.Add(tmp, type);
}
}
return dictionary;
}
public static A createA(System.IntPtr cPtr, bool owner)
{
A ret = null;
if (cPtr == System.IntPtr.Zero) {
return ret;
}
string ct = ($imclassname.A_getName(new System.Runtime.InteropServices.HandleRef(null, cPtr)));
if (aDictionary == null)
aDictionary = createDictionary<A>();
if (aDictionary.ContainsKey(ct))
{
System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.CreateInstance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
ret = (A)System.Activator.CreateInstance(aDictionary[ct], flags, null, new object[] { cPtr, owner }, null);
}
else
{
ret = new A(cPtr, owner);
}
return ret;
}
%}
%typemap(csout, excode=SWIGEXCODE)
A*, std::shared_ptr<A> {
System.IntPtr cPtr = $imcall;
Chip ret = liblogicalaccess_examplePINVOKE.createA(cPtr, $owner);$excode
return ret;
}
%include "example.hpp"
test.cs
A tmp = someFactoryMethod("B") // will return a B class;
(B)tmp.specialBMethod(); // works fine;
A_vector test = someRandomMethod();
if (test[0].name == "B")
(B)tmp.specialBMethod(); // InvalidCastException
A_vector tmp = someRandomMethod();
A[] test = new A[tmp.Count];
tmp.CopyTo(test);
if (test[0].name == "B")
(B)tmp.specialBMethod(); // works fine
THE SOLUTION:
https://github.com/swig/swig/issues/1007
I found the answer on the SWIG issues list. It seems I simply forgot a typemap... Because the getitem method from the std::vector wrapping to C# return a reference to T, so I needed to add :
%typemap(csout, excode=SWIGEXCODE)
std::shared_ptr<A>&, // <- ANSWER HERE
A*, std::shared_ptr<A> {
System.IntPtr cPtr = $imcall;
Chip ret = liblogicalaccess_examplePINVOKE.createA(cPtr, $owner);$excode
return ret;
}