繁体   English   中英

Swig java 进程 std::pair 与来自 C++ 的类

[英]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 包含自定义对象时都会存在一些问题:

  1. 默认情况下解析的类 Result 无法识别,也未用于配对包装器代码生成,因此在 ResultStringPair 中,我看到 SWIGTYPE_p_Result 而不是 Result:
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);
  }
  1. 有一些奇怪的配对类,它是在 java 代码中默认生成和使用的。 例如类 SWIGTYPE_p_std__pairT_lib__Result_std__string_t 被创建和使用,尽管 ResultStringPair 被定义和生成。
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM