a-ev / DACE.jl

https://a-ev.github.io/DACE.jl/
MIT License
3 stars 1 forks source link

How to access public attributes on C++ class e.g. Monomial #3

Open chrisdjscott opened 5 months ago

chrisdjscott commented 5 months ago

We could look at map_type instead of add_type

Could also add a method that takes monomial and returns the value in dace_julia.cxx

afossa commented 5 months ago

For "trivial" layouts, CxxWrap assumes that the exposed class is mirrored to an already defined Julia mutable struct. See Breaking changes in v0.9 in the repository README.

If you try to add_type a simple C++ class or struct like DACE::Interval, you'll encounter this error which can be solved by adding this line at the top of dace_julia.cxx

template<> struct jlcxx::IsMirroredType<DACE::Interval> : std::false_type { };

this will let you add the class with the "limitations" of add_type, i.e. no public attributes exposed to Julia

If instead you conform to the defaults, you can map_type (aka mirror) the simple class to a predefined mutable struct in DACE.jl

mutable struct Interval
  m_lb::Float64
  m_ub::Float64
end

this will expose public attributes to Julia, but we need to check how to expose other methods of the wrapped class. See also this discussion

afossa commented 5 months ago

I implemented DACE.bounds() using map_type to map the Interval object defined in DACE. You can check the code here ade179b62e681d77c6bd5e5bed573376199a2c60 and here https://github.com/afossa/dace/commit/bc594ab2d558b72ab4527239f4541593f2f3eae9

If you find a more suitable solution, notably for Monomial, feel free to update the implementation to reflect your.

jackyarndley commented 5 months ago

I'm not sure that more complicated classes will be possible to add, so we might need to add some get/set methods.

See https://github.com/JuliaInterop/CxxWrap.jl/issues/418