[我试图在C#中为C ++库生成包装器,并使用生成的C3包装器来开发客户端应用程序。
C ++类:
namespace ns {
class ISampleInterface {
public:
virtual ~ISampleInterface() {}
virtual void fn(int cnt) = 0;
};
}
namespace ns {
class ISampleInterface2 {
public:
virtual ~ISampleInterface() {}
virtual void fn2(int cnt) = 0;
};
}
namespace ns {
class SampleClass {
public:
SampleClass() {}
~SampleClass() {}
void InputFunction(int count, ISampleInterface sampleInterface, ISampleInterface2& sampleInterface2){
}
ISampleInterface& OutputFunction(void) {
return null;
}
};
}
预期的C#代码:
namespace csNS {
public class SampleClass {
public void InputFunction(int count, out SampleInterface sampleInterface, out SampleInterface2 sampleInterface2) {
// some code
}
public SampleInterface OutputFunction() {
// some code
}
}
}
Swig接口声明:
%interface_custom("SampleInterfaceImpl", "SampleInterface", ns::ISampleInterface);
%interface_custom("SampleInterface2Impl", "SampleInterface2", ns::ISampleInterface2);
%Outparams(ns:ISampleInterface, SampleInterfaceImpl)
%Outparams(ns:ISampleInterface2, SampleInterface2Impl)
在以上C#代码中,InputFunction接受3个参数。但是,为SampleInterface和SampleInterface2生成的实现不包含默认构造函数。因此,我无法为这些接口创建对象。由于我无法在C#端实例化接口,因此我想将out作为参数传递给CS函数,然后在C ++端进行初始化。因此,要修改在我已定义的typemap下的swig编译期间生成的cs文件。
Typemap声明:
%define %Outparams(TYPE, InterfaceImpl)
%typemap (cstype) TYPE,
TYPE &,
TYPE *,
TYPE *& "out TYPE"
%enddef
使用上面的typemap,我可以更改输入参数,但是,它也在更改函数的返回值,因此我无法控制它。
生成的实际cs代码:
namespace csNS {
public class SampleClass {
public void InputFunction(int count, out SampleInterface sampleInterface, out SampleInterface2 sampleInterface2) {
// some code
}
public out SampleInterface OutputFunction() {
// some code
}
}
}
我做错了什么?
我更改了typemap声明,成功了。
对于班级:
%typemap (cstype, out="TYPE") TYPE,
TYPE &,
TYPE *,
TYPE *& "out TYPE"
对于接口:
%typemap (cstype, out="InterfaceImpl") TYPE,
TYPE &,
TYPE *,
TYPE *& "out InterfaceImpl"
CS文件:
namespace csNS {
public class SampleClass {
public void InputFunction(int count, out SampleInterface sampleInterface, out SampleInterface2 sampleInterface2) {
// some code
}
public SampleInterfaceImpl OutputFunction() {
// some code
}
}
}