javaout typemap不适用于std :: vector

时间:2015-11-25 13:18:37

标签: java c++ java-native-interface swig

当我使用int时,一切正常 -

%immutable S::field;

%typemap(javaout) int S::field {
  //custom code
}

struct S {
  int field;
};

但方法不适用于std::vector<int>

%include <std_vector.i>

%immutable S::field;
%template(vector_int) std::vector<int>;

%typemap(javaout) std::vector<int> S::field {
  //custom code
}

struct S {
  std::vector<int> field;
};

使用swig -java -c++ -module sample sample.i

编译样本

1 个答案:

答案 0 :(得分:1)

请参阅structure data members上的Swig文档。

关键点是:

  

结构成员被包装时,它被作为指针处理,除非使用%naturalvar指令处理它更像C ++引用。

所以你需要定义的类型图是

%typemap(javaout) std::vector<int>* S::field {
    //custom code
}

(如果您使用的是%typemap(javaout) std::vector<int>& S::field,则为%naturalvar