我正在尝试使用头文件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代码没有任何问题。但在所有情况下,对包含自定义对象时都存在一些问题:
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 swig生成正确的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>>;