Refine Swift API for C++?

I’m working in a code base that uses both Metal and Metal-cpp. Metal-cpp is a header provided by Apple that provides C++ bindings to the Metal framework. Metal-cpp has its own pointer compatible types. They can be cast to/from the Obj-C Metal types because its types are just thin shims who just call objc_msgsend on the underlying pointer.

This causes some problems for Swift/C++ interop though. I need to return the C++ Metal types from Swift, but I can’t use those types in Swift because they’re reference types. (Which would require messing with the header.) That means I need to use the Obj-C Metal types in my Swift code but return the C++ Metal types across the language boundary.

Is there a feature to refine functions for C++ like there is for Obj-C? I’d like to be able to change the return type. I know I can manually generate the header and then modify it. But I’m not sure that’s sustainable - I’m pretty sure that header changes every compilation.

3 Likes

Not sure if you tried this or if it will work, but you may want to look into API Notes.

Sorry, I had a bit of the post holiday rush happen. Just getting back to this thread now.

I’ll look into API notes. Have those been used to refine a Swift generated C++ header within a target? I’ve seen them used to refine a stable header from a different module. But never a generated header in the same module.

Honestly, I’m not sure. I haven’t experimented with generated headers before. Theoretically, if the generated headers are being built as part of a module and the API Notes are being applied after the fact, I don’t see how this would be different from hand-written ones.

Looking forward to hearing how your experiment went! :slight_smile: