如何使用其他程序包中的SWIG Java代理类?

时间:2018-08-01 14:40:38

标签: java c++ swig

我正在研究一个C ++项目,该项目具有一些抽象类(我们称它们为接口)和许多扩展某些接口的“普通”类。这些类是独立的,需要进行编译,并且要分别生成绑定。在C ++中,这不是问题,但是在Java中,接口必须与实现类放在单独的程序包中,否则每个类将具有自己的接口,并且您将无法使用ArrayList。

问题源于某些接口具有返回std :: vector的方法的事实,因此我们使用代理类将vector绑定到AbstractList。生成的代理类包含在接口包(jar)中,并导入到类java文件中。问题在于代理类具有我们需要作为私有生成的构造函数,而不能将其用于主类。

这是C ++接口的示例:

#include <vector>

class Interface
{
    public:
        virtual ~Interface() {}

        virtual std::vector<float> getValues() = 0;
};

Swig为其生成以下Java类:

package interfaces;

import java.util.AbstractList;
import java.lang.Float;

public interface Interface {
  long Interface_GetInterfaceCPtr();
  AbstractList<Float> getValues();
}

和此代理类:

package interfaces;

import java.util.AbstractList;
import java.lang.Float;

public class floatVector extends java.util.AbstractList<Float> {
  private transient long swigCPtr;
  protected transient boolean swigCMemOwn;

  protected floatVector(long cPtr, boolean cMemoryOwn) {
    swigCMemOwn = cMemoryOwn;
    swigCPtr = cPtr;
  }

  protected static long getCPtr(floatVector obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        javaupm_new_interfacesJNI.delete_floatVector(swigCPtr);
      }
      swigCPtr = 0;
    }
  }

    floatVector(java.util.Collection<Float> e) {
        this.reserve(e.size());
        for(Float value : e) {
            this.push_back(value);
        }
    }
    public Float get(int idx) {
        return get_impl(idx);
    }

  public floatVector() {
    this(javaupm_new_interfacesJNI.new_floatVector__SWIG_0(), true);
  }

  public floatVector(long n) {
    this(javaupm_new_interfacesJNI.new_floatVector__SWIG_1(n), true);
  }

  public floatVector(floatVector o) {
    this(javaupm_new_interfacesJNI.new_floatVector__SWIG_2(floatVector.getCPtr(o), o), true);
  }

  public long capacity() {
    return javaupm_new_interfacesJNI.floatVector_capacity(swigCPtr, this);
  }

  public void reserve(long n) {
    javaupm_new_interfacesJNI.floatVector_reserve(swigCPtr, this, n);
  }

  public boolean isEmpty() {
    return javaupm_new_interfacesJNI.floatVector_isEmpty(swigCPtr, this);
  }

  public void clear() {
    javaupm_new_interfacesJNI.floatVector_clear(swigCPtr, this);
  }

  public void push_back(float x) {
    javaupm_new_interfacesJNI.floatVector_push_back(swigCPtr, this, x);
  }

  public float get_impl(int i) {
    return javaupm_new_interfacesJNI.floatVector_get_impl(swigCPtr, this, i);
  }

  public float set(int i, float VECTOR_VALUE_IN) {
    return javaupm_new_interfacesJNI.floatVector_set(swigCPtr, this, i, VECTOR_VALUE_IN);
  }

  public int size() {
    return javaupm_new_interfacesJNI.floatVector_size(swigCPtr, this);
  }

  public void removeRange(int from, int to) {
    javaupm_new_interfacesJNI.floatVector_removeRange(swigCPtr, this, from, to);
  }

}

主类swig文件(.i)导入接口swig文件,因此它也使用 floatVector 。 问题在于主类需要 floatVector(long cPtr,boolean cMemoryOwn)构造函数public且不受保护,因为它位于不同的程序包中。实际错误是

constructor floatVector.floatVector(Collection<Float>) is not applicable
  (actual and formal argument lists differ in length)
constructor floatVector.floatVector() is not applicable
  (actual and formal argument lists differ in length)
constructor floatVector.floatVector(long) is not applicable
  (actual and formal argument lists differ in length)
constructor floatVector.floatVector(floatVector) is not applicable
  (actual and formal argument lists differ in length)
MyClass.java:72: error: no suitable constructor found for floatVector(long,boolean)
    return (AbstractList<Float>)(new floatVector(myclassJNI.MyClass_getValues(swigCPtr, this), true));

我没有附加swig接口文件,因为它们很多,有些很大,我想我解释了我是如何使用它们的。让我知道您是否也需要添加它们。

我乐于接受任何建议,它的工作要比看起来漂亮或以“正确”的方式做更为重要。预先感谢!

0 个答案:

没有答案