Dear experts,
assume the module below:
module PointsModule
struct Point
x::Cdouble
y::Cdouble
z::Cdouble
end
struct Points
n_of_points::Cint
all_points::Vector{Point}
end
function sum_points(my_points::Points)::Cdouble
println("Received ", my_points.n_of_points)
println("x[1] = ", my_points.all_points[1].x, " x[2] = ", my_points.all_points[2].x)
return my_points.all_points[1].x + my_points.all_points[2].x
end
function show_point(my_point::Point)
println("x = ", my_point.x)
println("y = ", my_point.y)
println("z = ", my_point.z)
end
if abspath(PROGRAM_FILE) == @__FILE__
a_p = Point(1.0,2.0,3.0)
b_p = Point(4.0,5.0,6.0)
c_p = Point(7.0,8.0,9.0)
show_point(a_p)
my_points = Points(3,[a_p, b_p, c_p])
res = sum_points(my_points)
println("Res is: ", res)
end
end
I would like to call the sum_points
from my C++ code.
Here is my C++ code:
#include <iostream>
#include <string>
#include <unistd.h>
#include <julia.h>
JULIA_DEFINE_FAST_TLS()
typedef struct
{
double x;
double y;
double z;
}Point;
typedef struct
{
size_t nr_of_points;
Point* all_points;
}Points;
void fill_struct(Points* my_points)
{
const size_t nr_of_points(600*600);
my_points->nr_of_points = nr_of_points;
my_points->all_points = new Point[nr_of_points];
for(size_t i=0; i<nr_of_points; i++)
{
my_points->all_points[i].x = 1.5;
my_points->all_points[i].y = 2.7;
my_points->all_points[i].z = 3.9;
}
}
int main()
{
jl_options.image_file = JULIAC_PROGRAM_LIBNAME;
julia_init(JL_IMAGE_JULIA_HOME);
Points my_points;
fill_struct(&my_points);
jl_value_t* mod = (jl_value_t*) jl_eval_string("PointsModule");
jl_value_t** jl_point_ptr = (jl_value_t**) malloc (my_points.nr_of_points * sizeof( (jl_value_t*) jl_get_function((jl_module_t*)mod, "Point") ));
for(size_t i=0; i<my_points.nr_of_points; i++)
{
jl_value_t* jl_x = NULL;
jl_value_t* jl_y = NULL;
jl_value_t* jl_z = NULL;
JL_GC_PUSH3(jl_x, jl_y, jl_z);
jl_x = jl_box_float64(my_points.all_points[i].x);
jl_y = jl_box_float64(my_points.all_points[i].y);
jl_z = jl_box_float64(my_points.all_points[i].z);
jl_point_ptr[i] = jl_new_struct((jl_datatype_t*)jl_get_function((jl_module_t*)mod, "Point"),
jl_x,
jl_y,
jl_z);
}
jl_function_t* jl_show_point = jl_get_function((jl_module_t*)mod, "show_point");
jl_call1(jl_show_point, jl_point_ptr[0]);
jl_call1(jl_show_point, jl_point_ptr[1]);
jl_value_t* jl_nr_of_points = NULL;
jl_array_t* jl_point_array = NULL;
JL_GC_PUSH2(jl_nr_of_points, jl_point_array);
jl_nr_of_points = jl_box_int64(my_points.nr_of_points);
jl_point_array = jl_ptr_to_array_1d(jl_apply_array_type((jl_value_t*)jl_get_function((jl_module_t*)mod, "Point"), 1),
jl_point_ptr,
my_points.nr_of_points,
0);
jl_value_t* jl_my_points = jl_new_struct((jl_datatype_t*)jl_get_function((jl_module_t*)mod, "Points"),
jl_nr_of_points,
jl_point_array);
jl_function_t* jl_sum_points = jl_get_function((jl_module_t*)mod, "sum_points");
jl_value_t* res = jl_call1(jl_sum_points, jl_my_points);
std::cout << "Res is: " << jl_unbox_float64(res) << std::endl;
JL_GC_POP();
jl_atexit_hook(0);
return 0;
}
On the above example, I created the jl_point_ptr
which is an array of jl_new_struct
objects ( = jl_value_t*
).
Then, inside the for loop I am assigning each element of the jl_point_pt
r to an allocation jl_new_struct
object.
Once all the points are created, I am creating a “pointer to an array” as I would do if I had an array of primitive types.
Although the jl_point_array
is created fine, when I pass it in the jl_sum_points
function, all the elements are empty.
Could you help me please?