Hi,
I’ve been wrapping a C++ library with C++ wrap, and some of the methods in it return vectors, either of simple items like floats, and ints, or sometimes vectors of classes.
I’ve come up with a way to give a std::vector to julia by doing the following:
.method("get_fw_nodes", [](SequenceGraph& sg, sgNodeID_t n) {
std::vector<sgNodeID_t> fw_nodes = sg.get_fw_nodes(n);
auto *nodes = new sgNodeID_t[fw_nodes.size()];
std::copy(fw_nodes.begin(), fw_nodes.end(), nodes);
auto jlarr = jlcxx::make_julia_array(nodes, fw_nodes.size());
return jlarr;
})
So I call the method, get the vector, allocate a new array, copy vector contents to the array, wrap that up in an ArrayRef and pass that to julia. This works great for this case as sgNodeID_t
is just int64_t
.
The use of a new without a paired delete scares me, but I’m trusting julia gc to take care of that, as I believe jccxx::make_julia_array
ensures the resulting Array is owned by julia and so should be gc’d.
I’d like to do something similar, passing an array of a C++ class. The class is wrapped in CxxWrap too:
mod.add_type<Link>("Link")
.constructor()
.constructor<sgNodeID_t, sgNodeID_t, int32_t>();
However trying to do something like this:
.method("get_fw_links", [](SequenceGraph& sg, sgNodeID_t n){
std::vector<Link> fw_links = sg.get_fw_links(n);
auto *links = new Link[fw_links.size()];
std::copy(fw_links.begin(), fw_links.end(), links);
auto jlarr = jlcxx::make_julia_array(links, fw_links.size());
return jlarr;
})
Doesn’t work. Is it at all possible using CxxWrap and any other julia c/c++ interfacing to pass to julia an array of C++ objects (which are themselves CxxWrapped)? I know an alternative route is to wrap std::vector.