java - Swig java 进程 std::pair 与 c++ 中的类

标签 java c++ swig std-pair

我正在尝试使用头文件从 C++ 处理到 Java DLL 库.h

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);
  }
  • 有一些奇怪的pair类,它是在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;
      }
    }
    

    如何使用 swig 为 std::pair 与自定义对象生成正确的 java 包装器,并避免自动生成 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>>;
    

    关于java - Swig java 进程 std::pair 与 c++ 中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55785974/

    相关文章:

    c++ - 创建一个编译时字符串重复一个字符 n 次

    c++ - 如何高效转置非方阵?

    c++ - Swig 在命名空间别名上给出 "unknown namespace"错误

    python - 痛饮 python : inject pointer on construction

    java - 用于在检查异常和非检查异常之间做出决定的清晰简单的规则

    java - PHP 是否像 Java 一样允许 *.properties 文件?

    java - Eclipse Checkstyle 禁用 "Lline has trailing spaces"

    java - 如果我在 Java 中的 TIMED_WAIT 中有 3000 个线程,对 CPU 有什么影响?

    c++ - 基本的c++之后是什么?

    c++ - 如何使用 shared_ptr 和 SWIG 避免内存泄漏