【问题标题】:SWIG2 template return type namespace not detected未检测到 SWIG2 模板返回类型命名空间
【发布时间】: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


    【解决方案1】:

    现在我通过添加修复了第一个版本

    %typemap(javaimports) ns::C "
    import mypack.Ivec;
    "
    

    对...

    [ ... ]
    %nspace ns::C;
    // ... here
    namespace ns
    {
    [ ... ]
    

    现在名称将由 java 编译器解析。

    干杯。

    【讨论】:

      猜你喜欢
      • 2013-04-22
      • 2015-04-13
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 2014-11-14
      相关资源
      最近更新 更多