JuliaInterop / Cxx.jl

The Julia C++ Interface
Other
755 stars 107 forks source link

Advice for Cxx Cpp to Julia "object conversion" #408

Open gstavrinos opened 5 years ago

gstavrinos commented 5 years ago

Hello everyone,

Thanks for the cool package.

I am trying to create a Julia wrapper of a Cpp library that uses arbitrary Cpp objects, meaning that the users can include additional header files, thus creating new object types. This makes it impossible to create a Julia struct for every possible Cpp object the users might need.

So, what is the most elegant way of converting from Cpp to Julia "objects" and vice versa? I have tried doing it using meta-programming (eval etc) but this seem to inefficient.

Thanks for any kind of advice.

George

Roger-luo commented 5 years ago

If you want to create a Julia type, the only way is to eval an expression (but only once), this is similar to the compile process of C++, and there's no way to avoid it. But if you mean create the instance of a C++ object wrapped by Julia. Simply define the corresponding constructor in Julia, and do the construction.

Regarding to C++ class, I think only pointers can be "moved" to Julia's object without copying, for other type of value, you will have to copy it inside Julia.

Gnimuc commented 5 years ago

what is the most elegant way of converting from Cpp to Julia "objects" and vice versa?

isn't this what Cxx did for us?

gstavrinos commented 5 years ago

Thanks for your answers @Roger-luo and @Gnimuc . I want to provide my users the ability to write for example:

j_object = customCppObject()
j_object.data = 1

What I do now internally, is create the corresponding julia type (by using eval). I am ok with this solution, since it can happen only once. My main concern is, that I have tackled the second line (the assignment) with the same rationale. I generate the appropriate Cxx expression and then evaluate it, adding overhead to a seemingly simple operation.

Given the fact that each time the user runs the code provides the Cpp header files needed, would it be appropriate to generate structs for all classes? In my mind this is the most elegant solution since it provides the user with full Julia integration, but how am I going to pass my custom structs to Cpp functions that expect the equivalent Cpp object as a parameter?

Sorry for the wall of text. Hope you will have the time to help. If not, it's still cool!

Thanks again, George

Gnimuc commented 5 years ago

IIUC, you are looking for something like Clang.jl's wrapper auto-generator? That's possible but someone needs to invest time to implement it :P

gstavrinos commented 5 years ago

Interesting pointer, @Gnimuc , but as you suggest, it requires a lot of time and effort, and unfortunately my available time for extra (but fun!) projects is very close to absolute zero. I appreciate your input though.

Throw any other solutions and suggestions, if you please!

Thanks,

George