【发布时间】:2010-08-11 13:44:45
【问题描述】:
我喜欢 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 :(
有没有人遇到过类似的困难并给我一些提示?
B I G T H X bbb
【问题讨论】:
标签: java templates namespaces return-value swig