Swig:如何键入结构的两个字段?

时间:2014-03-03 12:11:42

标签: c++ swig

我正在使用Swig将C / C ++包装成Java。

我有这个结构:

struct score {
    void* goals;
    uint32_t goals_number; 
}

我需要在目标和目标之间进行奇偶校验。

这种类型映射仅在函数参数奇偶校验的情况下适用于我:

%apply (unsigned int *OUTPUT, size_t LENGTH) { (void* goals, uint32_t goals_number) };

如何在结构域上应用奇偶校验?怎么做?

非常感谢你!

编辑

我需要在Java端以这种方式获取getter和setter的签名:

public class Score {

    // ... other methods

    void setGoals( int goals[] ){
        //...
    }

    int[] getGoals(){
        //...
        // goals.length must be equal to "goals_number"
    }
}

而不是:

  public void setGoals(SWIGTYPE_p_void value) {
    MyModuleJNI.Score_goals_set(swigCPtr, this, SWIGTYPE_p_void.getCPtr(value));
  }

  public SWIGTYPE_p_void getGoals() {
    long cPtr = MyModuleJNI.Score_goals_get(swigCPtr, this);
    return (cPtr == 0) ? null : new SWIGTYPE_p_void(cPtr, false);
  }

  public void setGoals_number(long value) {
    MyModuleJNI.Score_goals_number_set(swigCPtr, this, value);
  }

  public long getGoals_number() {
    return MyModuleJNI.Score_goals_number_get(swigCPtr, this);
  }

1 个答案:

答案 0 :(得分:0)

尝试%extend指令:

// score.i:
%{
#include "Score.h"
%}

%include "Score.h"

%extend Score  %{
    void setGoals( int goals[] ){
        //... use $self->goals_number and $self->goals to populate
        // goals array
    }

    int* getGoals(){
        //...
        int* swigGoals = new int[$self->goals_number]; 
        // populate swigGoals, then
        return swigGoals; 
    }
%}

如果您希望能够修改返回的数组,可能需要按摩一下。

您可能希望隐藏这两个公共属性,以便从Java中看不到它们(使用%ignore Score::goals等)。