Contains three useful files:
FFI-Bindings.rkt
lets you bind FFI functions using Racket'sffi/unsafe
module, but extends the Racket notion ofcpointer
types to includec++pointer
types, which support subtyping and upcasts.Cxx-Types.rkt
lets you automatically definec++pointer
types for the entire type hierarchy of some API, by parsing JSON descriptions of inheritance relations emitted by this library, to discover symbol names of functions implementing the necessary upcasts (a C++ file containing the necessary functions can be emitted using the same Python utilities).private/interactive-helpers.rkt
allows you to easily preload a set of libraries into the Racket address space, so thatFFI-Bindings.rkt
doesn't need to know about specific path to libraries.
An example usage, based on a hypothetical FFI for Cxx-TEDSL
might look like this:
(define-c++ makeFloat
(_fun _float -> _TEDSL::Float))
(define-c++ makePlus
(_fun _TEDSL::Expr<TEDSL::Number>++
_TEDSL::Expr<TEDSL::Number>++ -> _TEDSL::Plus))
(makePlus (makeFloat 1.0) (makeFloat 3.0))
(In this example Float
is a subtype of Expr<Float>
, Number
, and Expr<Number>
, so the FFI wrapper for makePlus
, would automatically be able to find the necessary upcasts).
It should be possible to reliably track ownership of these pointers, using the allocator
, deallocator
and retainer
wrappers in ffi/unsafe/alloc
, but the documentation for these functions is dense.