Search code examples
common-lispzeromqffi

Nested structs and unions with Common Lisp CFFI


I want to write a small wrapper library for a C library (ZeroMQ v3, to be more specific).

I wonder, however, how to declare foreign nested structs and unions in CFFI.

The C header file containing the respective struct/union combination can be found here.

CFFI's documentation couldn't help so far and I also couldn't find similar questions online yet.

I'll be glad for any help!


Solution

  • defcstruct can be used in this case. Also, in the docs you'll find examples of defining a foreign struct, and also that the two kinds of slots possible are simple (types such as :int) and aggregate (the name of other struct), so nothing prevents you to be defining the different structures that form that big union and then compose the union (there is also a defcunion construct).

    The own CFFI implementation has examples and tests for structs like this:

    (defcstruct s-short
      (a-char :char)
      (another-char :char)
      (a-short :short))
    
    (defcstruct s-s-short
      (yet-another-char :char)
      (a-s-short s-short))
    

    Note that a-s-short is of type s-short, defined above.