vector 返回 const 引用,不可能向下转型
vector returning const reference, impossible to downcast
我在 SWIG 中遇到问题。
如文档 here 中所述,SWIG 不支持 Java 和 C# 中的向下转型。我听从了文档的建议,并为对象工厂创建了正确的类型映射。我可以知道创建一个对象 A 并将其向下转换为 B。
不过,我还有一个方法 returning 一个指向对象 A 的指针向量。我写了一些类型映射来使用我创建的工厂。但是,我不能向下转换向量的任何元素。
这是一个例子:
class A { public:
string name;
void someMethod();
... }
class B : public A {
specialBMethod();
... }
class C : public A {
specialCMethod();
... }
std::vector<std::shared_ptr<A>> someRandomMethod();
现在,我想在 C# 中像这样:
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
最有趣的部分是,如果我使用 CopyTo 制作矢量的副本,并将其放入数组中,例如,它就可以工作。
A_vector tmp = someRandomMethod();
A[] test = tmp.ToArray(); // imagine the ToArray method was implemented
if (test[0].name == "B")
(B)tmp.specialBMethod(); // works fine
我认为当 vector return 对象的 const 引用时有问题。它失去了继承性,变得无法向下转型。
在这种情况下我很迷茫。我还在 SWIG 存储库上打开了一个 issue。
任何帮助都会很棒;
编辑 :正如 Flexo 要求的那样,这里是一个最小的完整示例。
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
编辑:随着问题的改变,我的回答不再有任何贡献。
尝试 std::unique_ptr 的向量,因为对象不是多态的。
解决方案:
https://github.com/swig/swig/issues/1007
我在 SWIG 问题列表中找到了答案。看来我只是忘记了一个类型映射...因为 std::vector 中的 getitem 方法包装到 C# return 对 T 的引用,所以我需要添加:
%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;
}
我在 SWIG 中遇到问题。
如文档 here 中所述,SWIG 不支持 Java 和 C# 中的向下转型。我听从了文档的建议,并为对象工厂创建了正确的类型映射。我可以知道创建一个对象 A 并将其向下转换为 B。
不过,我还有一个方法 returning 一个指向对象 A 的指针向量。我写了一些类型映射来使用我创建的工厂。但是,我不能向下转换向量的任何元素。
这是一个例子:
class A { public:
string name;
void someMethod();
... }
class B : public A {
specialBMethod();
... }
class C : public A {
specialCMethod();
... }
std::vector<std::shared_ptr<A>> someRandomMethod();
现在,我想在 C# 中像这样:
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
最有趣的部分是,如果我使用 CopyTo 制作矢量的副本,并将其放入数组中,例如,它就可以工作。
A_vector tmp = someRandomMethod();
A[] test = tmp.ToArray(); // imagine the ToArray method was implemented
if (test[0].name == "B")
(B)tmp.specialBMethod(); // works fine
我认为当 vector return 对象的 const 引用时有问题。它失去了继承性,变得无法向下转型。
在这种情况下我很迷茫。我还在 SWIG 存储库上打开了一个 issue。 任何帮助都会很棒;
编辑 :正如 Flexo 要求的那样,这里是一个最小的完整示例。
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
编辑:随着问题的改变,我的回答不再有任何贡献。
尝试 std::unique_ptr 的向量,因为对象不是多态的。
解决方案:
https://github.com/swig/swig/issues/1007
我在 SWIG 问题列表中找到了答案。看来我只是忘记了一个类型映射...因为 std::vector 中的 getitem 方法包装到 C# return 对 T 的引用,所以我需要添加:
%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;
}