[英]How to return std::vector<std::pair<std::string, int> > from java to c++ using swig typemaps
[英]Swig java process std::pair with a class from c++
我正在嘗試使用頭文件 lib.h 從 C++ 處理到 java DLL
enum class Code : uint32_t
{
ok = 0,
cancelled = 1,
};
struct Result
{
Result(): m_code(Code::ok) {}
Result(Code code, const std::string& t = std::string()) :m_code(code), m_text(t) {}
Code code() const { return m_code; }
const std::string& text() const { return m_text; }
private:
Code m_code;
std::string m_text;
};
class IApp
{
public:
virtual std::pair<std::uint8_t, std::uint8_t> systemModeInt() = 0;
virtual std::pair<Result, std::uint8_t> systemMode() = 0;
virtual std::pair<Result, std::string> objectName() = 0;
virtual std::pair<Result,std::vector<uint8_t>> readParameters() = 0;
}
我的 swig 腳本,它處理 std::pairs 如下:
%include <std_pair.i>
#include "lib.h"
%template(ShortPair) std::pair<std::uint8_t, std::uint8_t>;
%template(ResultStringPair) std::pair<Result, std::string>;
%template(ResultShortPair) std::pair<Result, std::uint8_t>;
%template(ResultVectorPair) std::pair<Result,std::vector<uint8_t>>;
我所看到的,那個 swig 為 Result 和 ShortPair (std::pair) 類生成 java 代碼沒有任何問題。 但在所有情況下,pair 包含自定義對象時都會存在一些問題:
public class ResultStringPair {
private transient long swigCPtr;
protected transient boolean swigCMemOwn;
public ResultStringPair() {
this(vselibJNI.new_ResultStringPair__SWIG_0(), true);
}
public ResultStringPair(SWIGTYPE_p_Result first, String second) { this(vselibJNI.new_ResultStringPair__SWIG_1(SWIGTYPE_p_Result.getCPtr(first), second), true);
}
public SWIGTYPE_p_std__pairT_lib__Result_std__string_t objectName() {
return new ...
}
public class SWIGTYPE_p_std__pairT_lib__Result_std__string_t {
private transient long swigCPtr;
protected SWIGTYPE_p_std__pairT_lib__Result_std__string_t(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
swigCPtr = cPtr;
}
protected SWIGTYPE_p_std__pairT_lib__Result_std__string_t() {
swigCPtr = 0;
}
protected static long getCPtr(SWIGTYPE_p_std__pairT_lib__Result_std__string_t obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
}
如何為帶有自定義對象的 std::pair 生成正確的 java-wrappers 並避免自動生成 SWIGTYPE_p_Result、SWIGTYPE_p_std__pairT_lib__Result_std__string_t?
除了 lib.h 文件中缺少分號之外,您還需要對 SWIG .i 文件進行以下更改,我已經對它們進行了注釋:
%include <std_pair.i>
%include <std_vector.i> // Missing for vector template
%include <std_string.i> // One of your interface functions had a std::string
%include <stdint.i> // This is needed for uint8_t, uint32_t etc.
%include "lib.h" // This is the most important change - in order to make SWIG read the lib.h file you need to use %include
%template(CharVector) std::vector<uint8_t>; // This was missing and resulted in a SWIGTYPE_ for the last pair
%template(ShortPair) std::pair<std::uint8_t, std::uint8_t>;
%template(ResultStringPair) std::pair<Result, std::string>;
%template(ResultShortPair) std::pair<Result, std::uint8_t>;
%template(ResultVectorPair) std::pair<Result,std::vector<uint8_t>>;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.