无法弄清楚如何让 SWIG/Java 强制代理类实现接口
我正在使用 SWIG 将 C++ 类导出到 Java,但在尝试强制代理对象实现接口时遇到了问题。
我搜索了 SWIG 文档,发现您可以使用“%pragma(java) jniclassinterfaces=x”让 JNI 类实现给定的接口,并使用“%pragma(java) moduleinterfaces=x”让模块实现任何给定的接口,但没有实际代理对象的相应编译指示。
我更喜欢让 SWIG 生成“实现 X”代码,因为稍后尝试添加该实现被证明是困难的。例如,如果我尝试对 SWIG 代理进行子类化,然后实现该接口,我会遇到问题,因为我也在使用泛型:
interface IVector<VectorType> {
VectorType add(VectorType other);
...
}
所以类似这样的事情会失败:
class MyVector extends MyProxyVector implements IVector<MyVector> {
MyVector add(MyVector other) {
return (MyVector) super.add(other);
}
}
因为它需要将父类强制转换为子类。
我能想出的解决这个问题的唯一其他方法是创建一个包装类或使用复制构造函数。两者似乎都有点低效,因为它们的全部目的都是实现一个接口。
I'm using SWIG to export a c++ class to Java, but ran into a problem trying to force the proxy object to implement an interface.
I scoured the SWIG documentation and found you could use "%pragma(java) jniclassinterfaces=x" to have the JNI class implement a given interface, and use "%pragma(java) moduleinterfaces=x" to have the module implement any given interface, but no corresponding pragma for the actual proxy object.
I would prefer having SWIG generate the 'implements X' code, as trying to add that implementation later on is proving difficult. For example, if I try to subclass the SWIG proxy and then implement the interface, I run into issues because I'm also using generics:
interface IVector<VectorType> {
VectorType add(VectorType other);
...
}
So something like this fails:
class MyVector extends MyProxyVector implements IVector<MyVector> {
MyVector add(MyVector other) {
return (MyVector) super.add(other);
}
}
because it would require casting a parent to a child class.
The only other ways I can come up with getting around this problem is to either create a wrapper class or use a copy constructor. Both seem somewhat inefficient as their entire purpose to to implement the one interface.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该通过类型映射机制来处理。
以下代码:
设置
Foo
的基础和接口,如下所示:在生成的 Java 代理类中
This should be handled via the typemaps mechanism.
The following code:
Sets the bases and interfaces for
Foo
, like so:in the generated Java proxy class.