开发者

SWIG2 template return type namespace not detected

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&开发者_运维知识库gt;
    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


By now I fixed the first version this by adding

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

right ...

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

Now the name will be resolved by the java compiler.

Cheers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜