如何在Java中使用SWIG Generated C结构作为通过SWIG / JNI输入C函数 [英] How to use SWIG Generated C structures in Java as input to C functions via SWIG/JNI

查看:657
本文介绍了如何在Java中使用SWIG Generated C结构作为通过SWIG / JNI输入C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SWIG接口文件,它将一些C函数(通过JNI)公开给我的Java应用程序,这些C结构用作C函数的输入(通过SWIG / JNI)。 SWIG将结构生成为Java类,但我不确定如何设置结构属性,因为setter采用SWIG生成的类型。我需要在将结构属性作为输入传递到我的Java类的C函数之前设置它们。 example_location_id_t_是我需要传递的类,但 Id Phy_idx 的setter采用以下SWIG类型。如何填充 SWIGTYPE_p_unsigned_char SWIGTYPE_p_uint32_t 以便我可以设置 Id Phy_idx SWIGTYPE_p_uint32_t 类的属性?

I have a SWIG interface file that exposes some C functions (via JNI) to my Java application and these C structures are used as input into the C function (via SWIG/JNI). SWIG generates the structure as a Java class, but I'm unsure how to set the structures properties as the setters take a SWIG generated type. I need to set the structures properties before passing it as input into the C function from my Java class. example_location_id_t_ is the class I need to pass, but the setters for Id and Phy_idx take the below SWIG types. How do I populate the SWIGTYPE_p_unsigned_char and SWIGTYPE_p_uint32_t so that I can set the Id and Phy_idx properties of the SWIGTYPE_p_uint32_t class?

setId(SWIGTYPE_p_unsigned_char value) setPhy_idx(SWIGTYPE_p_uint32_t value)

package com.test.jni;

public class SWIGTYPE_p_unsigned_char {
  private long swigCPtr;

  protected SWIGTYPE_p_unsigned_char(long cPtr, boolean futureUse) {
    swigCPtr = cPtr;
  }

  protected SWIGTYPE_p_unsigned_char() {
    swigCPtr = 0;
  }

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


package com.test.jni;

public class SWIGTYPE_p_uint32_t {
  private long swigCPtr;

  protected SWIGTYPE_p_uint32_t(long cPtr, boolean futureUse) {
    swigCPtr = cPtr;
  }

  protected SWIGTYPE_p_uint32_t() {
    swigCPtr = 0;
  }

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

package com.test.jni;

public class example_location_id_t_ {
  private long swigCPtr;
  protected boolean swigCMemOwn;

  public example_location_id_t_ (long cPtr, boolean cMemoryOwn) {
    swigCMemOwn = cMemoryOwn;
    swigCPtr = cPtr;
  }

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

  protected void finalize() {
    delete();
  }

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

  public void setId(SWIGTYPE_p_unsigned_char value) {
      ExampleJNI.example_location_id_t__id_set(swigCPtr, this, SWIGTYPE_p_unsigned_char.getCPtr(value));
  }

  public SWIGTYPE_p_unsigned_char getId() {
    long cPtr = ExampleJNI.example_location_id_t__id_get(swigCPtr, this);
    return (cPtr == 0) ? null : new SWIGTYPE_p_unsigned_char(cPtr, false);
  }

  public void setPhy_idx(SWIGTYPE_p_uint32_t value) {
    ExampleJNI.example_location_id_t__phy_idx_set(swigCPtr, this, SWIGTYPE_p_uint32_t.getCPtr(value));
  }

  public SWIGTYPE_p_uint32_t getPhy_idx() {
    return new SWIGTYPE_p_uint32_t(ExampleJNI.example_location_id_t__phy_idx_get(swigCPtr, this), true);
  }

  public example_location_id_t_() {
    this(ExampleJNI.new_example_location_id_t_(), true);
  }

}


推荐答案

在没有给出任何额外信息的情况下,SWIG默认假设您希望将事物包装为可以返回并从一个函数传递到另一个函数的类型,即使它无法在目标语言中有意义地包装。

Without being given any extra information SWIG defaults to assuming you want things wrapped as a type that can be returned and passed from one function to another, even if it can't be wrapped meaningfully in the target language.

每当你看到一个类型开始 SWIGTYPE _... 时,这意味着SWIG不知道如何生成更好的包装器这是它设法提出的最好的。

Every time you see a type that begins SWIGTYPE_... it means SWIG didn't know how to generate a better wrapper and this was the best it managed to come up with.

SWIG确实提供了默认的文字图,可以帮助你:

SWIG does provide default typemaps that can help you here though:


  • 对于 setPhy_idx(uint32_t value); 您需要在界面中添加:

  • For setPhy_idx(uint32_t value); all you need to add in your interface is:

%include "stdint.i"

void setPhy_idx(uint32_t value);

以及可代表 uint32_t 的类型将用于目标语言。

and a type which can represent a uint32_t will be used in the target language.

对于 setId(unsigned char * value); 它取决于实际上是什么 - 如果它是 NULL 终止字符串,你可以做类似的事情:

For setId(unsigned char *value); it depends quite what value actually is - if it's a NULL terminated string you can do something like:

%apply char * { unsigned char * };

void setId(unsigned char *value);

以及 String 将用于您的目标语言。

and a String will be used in your target language.

如果你想将指针作为整数类型传递,你可以使用类似的东西:

If you wanted to pass the pointer as an integer type you could use something like:

%apply unsigned long { unsigned char * };

void setId(unsigned char *value);

而不是。

如果 unsigned char * value 是指向您可以执行的单个 unsigned char 的指针:

If unsigned char *value is a pointer to a single unsigned char you could do:

%include "typemaps.i"
%apply unsigned char *INPUT { unsigned char *value };

void setId(unsigned char *value);

指示SWIG将poitner视为单个指针。 (这可以应用于 uint32_t 如果它也是一个指针)

which instructs SWIG to treat the poitner as a single pointer. (This could be applied for the uint32_t if it was a pointer too)

这篇关于如何在Java中使用SWIG Generated C结构作为通过SWIG / JNI输入C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆