第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

int[] 到 List<int> 的類型映射

int[] 到 List<int> 的類型映射

喵喵時光機(jī) 2023-05-10 16:28:26
我設(shè)法從我的 C++ 實(shí)現(xiàn)中生成了一個 Java 類。為此,我有一個SubwordEncoder.i:/* File : example.i */%module encoder%{#include "SubwordEncoder.h"%}/* Let's just grab the original header file here */%include "SubwordEncoder.h"界面看起來像這樣:class SubwordEncoder {public:    int* encode(char* decoded);    char* decode(int* encoded);};生成的代碼是這樣的:public class SubwordEncoder {  private transient long swigCPtr;  protected transient boolean swigCMemOwn;  protected SubwordEncoder(long cPtr, boolean cMemoryOwn) {    swigCMemOwn = cMemoryOwn;    swigCPtr = cPtr;  }  protected static long getCPtr(SubwordEncoder obj) {    return (obj == null) ? 0 : obj.swigCPtr;  }  protected void finalize() {    delete();  }  public synchronized void delete() {    if (swigCPtr != 0) {      if (swigCMemOwn) {        swigCMemOwn = false;        encoderJNI.delete_SubwordEncoder(swigCPtr);      }      swigCPtr = 0;    }  }  public SWIGTYPE_p_int encode(String decoded) {    long cPtr = encoderJNI.SubwordEncoder_encode(swigCPtr, this, decoded);    return (cPtr == 0) ? null : new SWIGTYPE_p_int(cPtr, false);  }  public String decode(SWIGTYPE_p_int encoded) {    return encoderJNI.SubwordEncoder_decode(swigCPtr, this, SWIGTYPE_p_int.getCPtr(encoded));  }  public SubwordEncoder() {    this(encoderJNI.new_SubwordEncoder(), true);  }}但是是否也可以從 SWIG獲得List<Integer>、ArrayList<int>或類似的東西?Iterable<int>char*已經(jīng)轉(zhuǎn)換為 Java String(來自文檔)但是擴(kuò)展這些映射的最簡單方法是什么?SWIG 版本為 4.0.0 (Ubuntu)
查看完整描述

1 回答

?
守候你守候我

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個贊

我會更改此接口并使用 C++ 容器(或迭代器/范圍,但在 SWIG 中不太支持)。從 SWIG 3.1(或者可能是 4.x?)開始,兩者std::vector都std::list應(yīng)該正確地實(shí)現(xiàn)合理的 Java 接口和自動裝箱原語。所以你的界面可能變成這樣:


class SubwordEncoder {

public:

    std::vector<int> encode(const std::vector<char>& decoded);

    std::vector<char> decode(const std::vector<int>& encoded);

};

然后你可以用這個包裝:


/* File : example.i */

%module encoder


%include <std_vector.i>


%{

#include "SubwordEncoder.h"

%}


%template(IntVector) std::vector<int>;

%template(CharVector) std::vector<char>;


/* Let's just grab the original header file here */

%include "SubwordEncoder.h"

這有兩件事。首先,它引入了 SWIG 庫對std::vector. 其次,它用于%template告訴 SWIG 使用兩種類型顯式實(shí)例化和包裝向量模板。這些在 Java 中被賦予了合理的名稱。


有了它,安全地實(shí)現(xiàn)您在這里嘗試做的事情應(yīng)該非常簡單。


需要注意的是,從byte[]或int[]另一個 Java 集合的自動轉(zhuǎn)換不會自動發(fā)生在函數(shù)輸入上。如果該行為對您很重要/有用,則可以創(chuàng)建一個接口來執(zhí)行此操作,但它需要更多的類型映射和 JNI 調(diào)用。


查看完整回答
反對 回復(fù) 2023-05-10
  • 1 回答
  • 0 關(guān)注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號