【问题标题】:How to wrap std::vector of custom structure with ffi?如何用 ffi 包装自定义结构的 std::vector?
【发布时间】:2015-10-02 09:36:17
【问题描述】:

我正在尝试为处理图像处理的动态 *.so 库创建一个 Ruby 包装器。 convert_x_to_z 函数之一有一个自定义 typedef 输入参数。如果我映射了A 结构,如何传入A 对象的向量

typedef struct image {
    uint16_t image_width;
    uint16_t image_height;
    uint16_t image_depth;
    uint8_t* data;
} A;

像这样变成FFI:Structure

 class A  < FFI::Struct
    layout :image_width , :int,
           :image_height, :int,
           :image_depth, :int,
           :data, :pointer
  end

假设我有一个变量single,它是A 类的一个实例。我怎样才能将它包装到B 类的数组中,它将代表A 类的向量/数组并将其作为参数@ 987654329@到函数int32_t convert_x_to_z

int32_t convert_x_to_z(
    const B &x,
    uint32_t &t,
    uint8_t* z);

这是A 类的B 向量或数组结构。

typedef std::vector<A> B;

【问题讨论】:

    标签: c++ ruby vector ffi


    【解决方案1】:

    你需要这样做:

    int32_t convert_x_to_z_aux(const A &a, uint32_t &t, uint8_t* z) {
        std::vector<A> b(1, a); // create a vector with 1 element
        return convert_x_to_z(b, t, z);
    }
    

    【讨论】:

    • 谢谢。我无法访问 C++ 代码,它是编译的动态库,我没有源文件。所以这真的行不通。我需要在 Ruby 级别上执行此操作。
    • @adamliesko,只需在旧库的基础上创建自己的库即可。
    • 谢谢。您能否指出我的某种指南/教程,我该怎么做?这不是stackoverflow.com/a/915181/1212336 和这里的障碍(如果我没有源文件)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多