JuliaInterop / libcxxwrap-julia

C++ library for backing CxxWrap.jl
Other
84 stars 43 forks source link

How to create a struct which is interoperable: #114

Closed sunayana closed 1 year ago

sunayana commented 1 year ago

Hello, I am currently stuck with making a C++ struct inter-operable from within Julia, here is my example: The C++ struct:

struct TestDepression
{
    uint32_t pit_cell = 0;
    float pit_elev = std::numeric_limits<float>::infinity();
};

the corresponding wrapper code in libcxxjulia-wrap

JLCXX_MODULE define_depressions_module(jlcxx::Module &mod)
{
    mod.add_type<TestDepression>("TestDepression", jlcxx::julia_type("TestDepression"));
}

followed by this julia code:

module RichDEM

__precompile__(false)

module depressions
    using CxxWrap
    mutable struct TestDepression
        pit_cell::UInt32
        pit_elev::Float32
    end
    export TestDepression
    @wrapmodule("/workspaces/richdem/build/lib/libjlrichdem.so", :define_depressions_module)

    function __init__()
        @initcxx
    end
end #depressions

end # module richdem

The C++ code compiles fine but when I try it out from Julia REPL, I get the following error:

julia> using RichDEM
[ Info: Precompiling RichDEM [db566695-6f60-45f3-9b37-d3ff769e9aca]
[ Info: Skipping precompilation since __precompile__(false). Importing RichDEM [db566695-6f60-45f3-9b37-d3ff769e9aca].
Warning: Type i already had a mapped type set as Int32 using hash 6253375586064260614 and const-ref indicator 0
Warning: Type a already had a mapped type set as Int8 using hash 4993892634952068459 and const-ref indicator 0
Warning: Type j already had a mapped type set as UInt32 using hash 10485857595211860659 and const-ref indicator 0
C++ exception while wrapping module depressions: invalid subtyping in definition of TestDepression with supertype TestDepression
ERROR: LoadError: invalid subtyping in definition of TestDepression with supertype TestDepression
in expression starting at /workspaces/richdem/wrappers/RichDEM.jl/src/RichDEM.jl:1

I am not sure whether the way I define the struct in Julia is a problem. Any help is much appreciated.

Thanks!

sunayana commented 1 year ago

I was able to resolve this at my end.