未检测到 SWIG2 模板返回类型命名空间
我正在转向 Java。我正在尝试获取一个模板化成员函数来使用一些模板化返回类型,当然我必须给出一个名称(因为否则 SWIG 将不会创建所需的源)。
test.i 就像:
%module Test
%{
#include <vector>
namespace ns
{
class C
{
public:
template <class T>
void doit(const std::vector<T>& v) {}; // will need std:vector<int> version of that
};
}
%}
%include "std_vector.i"
%template(Ivec) std::vector<int>; // here I'm defining an std::vector<int> to use ...
%nspace ns::C;
namespace ns
{
class C
{
public:
template <class T>
void doit(const std::vector<T>& v);
};
}
%extend ns::C
{
%template(Idoit) doit<int>; // ... here
}
调用时:
swig -c++ -java -outdir mypack -package mypack test.i
mypack/ns/C.java 看起来像:
package mypack.ns;
public class C {
private long swigCPtr;
protected boolean swigCMemOwn;
public C(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}
public static long getCPtr(C obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
protected void finalize() {
delete();
}
public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
mypack.TestJNI.delete_ns_C(swigCPtr);
}
swigCPtr = 0;
}
}
public void Idoit(Ivec v) { // OK, Ivec is beeing used ... but not with its fqn
mypack.TestJNI.ns_C_Idoit(swigCPtr, this, Ivec.getCPtr(v), v);
}
public C() {
this(mypack.TestJNI.new_ns_C(), true);
}
}
这很好,但是 Ivec 是在 mypack/Ivec.java 中定义的,即在“全局”包中,因此编译源失败。我怎样才能让 SWIG 使用 Ivec 的完整限定名!?
我还尝试将 Ivec 推入与 C 相同的命名空间,例如:
%module Test
%{
#include <vector>
namespace ns
{
class C
{
public:
template <class T>
void doit(const std::vector<T>& v) {}; // will need std:vector<int> version of that
};
}
%}
%include "std_vector.i"
%nspace ns::C;
%nspace ns::Ivec;
namespace ns
{
%template("ns.Ivec") std::vector<int>;
class C
{
public:
template <class T>
void doit(const std::vector<T>& v);
};
}
%extend ns::C
{
%template(Idoit) doit<int>;
}
但这会导致 Ivec 仍然位于 mypack 中,而 mypack/ns/C.java 现在是:
package mypack.ns;
public class C {
private long swigCPtr;
protected boolean swigCMemOwn;
public C(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}
public static long getCPtr(C obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
protected void finalize() {
delete();
}
public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
mypack.TestJNI.delete_ns_C(swigCPtr);
}
swigCPtr = 0;
}
}
public void Idoit(SWIGTYPE_p_ns__std__vectorT_int_t v) { // aaaaaaaaah
mypack.TestJNI.ns_C_Idoit(swigCPtr, this, SWIGTYPE_p_ns__std__vectorT_int_t.getCPtr(v));
}
public C() {
this(mypack.TestJNI.new_ns_C(), true);
}
}
现在 SWIG 甚至无法识别很酷的 Ivec : (
有没有人经历过类似的困难并给我一些提示?
BIGTHX bbb
I'm swigging to Java. I'm trying to get a templated member func to use some templated return type, which i have to give a name of course (since otherwise SWIG would not create the needed source).
test.i is like:
%module Test
%{
#include <vector>
namespace ns
{
class C
{
public:
template <class T>
void doit(const std::vector<T>& v) {}; // will need std:vector<int> version of that
};
}
%}
%include "std_vector.i"
%template(Ivec) std::vector<int>; // here I'm defining an std::vector<int> to use ...
%nspace ns::C;
namespace ns
{
class C
{
public:
template <class T>
void doit(const std::vector<T>& v);
};
}
%extend ns::C
{
%template(Idoit) doit<int>; // ... here
}
When calling:
swig -c++ -java -outdir mypack -package mypack test.i
mypack/ns/C.java looks like:
package mypack.ns;
public class C {
private long swigCPtr;
protected boolean swigCMemOwn;
public C(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}
public static long getCPtr(C obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
protected void finalize() {
delete();
}
public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
mypack.TestJNI.delete_ns_C(swigCPtr);
}
swigCPtr = 0;
}
}
public void Idoit(Ivec v) { // OK, Ivec is beeing used ... but not with its fqn
mypack.TestJNI.ns_C_Idoit(swigCPtr, this, Ivec.getCPtr(v), v);
}
public C() {
this(mypack.TestJNI.new_ns_C(), true);
}
}
which is fine but Ivec is defined in mypack/Ivec.java, that is in the 'global' package and thus compiling the source fails. How can I get SWIG to use Ivec's full qualified name!?
I have also tried to push Ivec into the same namespace as C like:
%module Test
%{
#include <vector>
namespace ns
{
class C
{
public:
template <class T>
void doit(const std::vector<T>& v) {}; // will need std:vector<int> version of that
};
}
%}
%include "std_vector.i"
%nspace ns::C;
%nspace ns::Ivec;
namespace ns
{
%template("ns.Ivec") std::vector<int>;
class C
{
public:
template <class T>
void doit(const std::vector<T>& v);
};
}
%extend ns::C
{
%template(Idoit) doit<int>;
}
but that has the effect that Ivec is still located in mypack and mypack/ns/C.java is now:
package mypack.ns;
public class C {
private long swigCPtr;
protected boolean swigCMemOwn;
public C(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}
public static long getCPtr(C obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
protected void finalize() {
delete();
}
public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
mypack.TestJNI.delete_ns_C(swigCPtr);
}
swigCPtr = 0;
}
}
public void Idoit(SWIGTYPE_p_ns__std__vectorT_int_t v) { // aaaaaaaaah
mypack.TestJNI.ns_C_Idoit(swigCPtr, this, SWIGTYPE_p_ns__std__vectorT_int_t.getCPtr(v));
}
public C() {
this(mypack.TestJNI.new_ns_C(), true);
}
}
and now SWIG doesn't even recognize the cool Ivec :(
Has anybody experienced similar difficulties and give me some hint?
B I G T H X
bbb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止,我通过添加
right ...
修复了第一个版本。现在该名称将由 java 编译器解析。
干杯。
By now I fixed the first version this by adding
right ...
Now the name will be resolved by the java compiler.
Cheers.