I'm part of a team working on a large and old Fortran code base which is increasingly modernized by bits of C++. In the process we've created tools for generating wrapper code to call Fortran routines from C++ and vice versa. This has been manageable for some years.
Now we want to also pass structs between Fortran & C++. This is quite challenging though because the Fortran standard does exclude various important features from interoperable structs: unions and arrays with size defined at runtime ... to name just a few. If you need those, what you are basically left with are opaque pointers pointing to objects in the respective other domain (C++/Fortran) and writing boilerplate code to manage them (create, copy, delete, etc.). We want to avoid that.
My idea now is to use Cap'n Proto serialization as a universal runtime format for our data structures so they can be simply passed by-value between Fortran and C++ and accessed in both domains.
What would be a reasonable way to approach this? Here's my current idea:
There's a C plugin for capnpc (
https://github.com/opensourcerouting/c-capnproto) available. Since Fortran can directly call C functions, we could generate Fortran wrapper code for the generated C code. So the layers of data & code would be:
1. the actual message as a continuous sequence of bytes in memory.
2. The C struct generated by capnpc-c which models the message's outer and inner structure + C functions generated by capnp-c to manage the content of the message
3. Our Fortran derived type (=struct) which resembles the outer structure of the message (= length) and carries type-bound procedures which call into the capnp-c-generated C code for accessing/managing the contents of the message
Does that make sense?