简体   繁体   中英

How to wrap std::vector of custom structure with ffi?

I am trying to create a Ruby wrapper for dynamic *.so library which deals with image processing. One of the functions convert_x_to_z has a custom typedef input parameter. How can I pass in the vector of A objects if I mapped the A struct

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

into a FFI:Structure like this

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

Suppose I have a variable single which is a instance of class A. How can I wrap this into an array of class B, which will represent the vector/array of classes A and pass it as a parameter const B &x to the function int32_t convert_x_to_z ?

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

This is the B vector or array struct of A class.

typedef std::vector<A> B;

You need to do it this way:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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