Fortran target for capnpc, meant for interoperable data structures

22 views
Skip to first unread message

Fabian Hachenberg

unread,
Feb 13, 2020, 5:57:35 PM2/13/20
to Cap'n Proto
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?


Kenton Varda

unread,
Feb 14, 2020, 5:39:19 PM2/14/20
to Fabian Hachenberg, Cap'n Proto
Hi Fabian,

One thing you might want to consider: Cap'n Proto works well for structures that you build all-at-once, but does not work very well for data structures that are long-lived and mutated over time. So if you were imagining writing messages to pass between the languages, great. But if you were imagining using it to share long-lived data structures that change over time, it probably won't work for that.

Otherwise, this sounds like an interesting idea, with the caveat that I don't know anything about Fortran. :)

-Kenton

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/603690f3-558d-450b-8f2e-a95f24c7f4dd%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages