The situation: I'm developing an iOS Metal App in Objective-C. I need to use a 3rd-party library VkFFT that provides Metal FFTs based on Metal-Cpp.
What I need: pass a id<MTLCommandQueue> instance from Objective-C world to C++ world as a MTL::CommandQueue object so that I can push compute commands to it. I don't want to create a seperate commmand queue in Metal-Cpp because using a single command queue can let me keep pushing all commands to it without waitUntilCompleted.
Bridged casts
A bridged cast is a C-style cast annotated with one of three keywords:
- (__bridge T) op casts the operand to the destination type T. If T is a retainable object pointer type, then op must have a non-retainable pointer type. If T is a non-retainable pointer type, then op must have a retainable object pointer type. Otherwise the cast is ill-formed. There is no transfer of ownership, and ARC inserts no retain operations.
- (__bridge_retained T) op casts the operand, which must have retainable object pointer type, to the destination type, which must be a non-retainable pointer type. ARC retains the value, subject to the usual optimizations on local values, and the recipient is responsible for balancing that +1.
- (__bridge_transfer T) op casts the operand, which must have non-retainable pointer type, to the destination type, which must be a retainable object pointer type. ARC will release the value at the end of the enclosing full-expression, subject to the usual optimizations on local values. These casts are required in order to transfer objects in and out of ARC control; see the rationale in the section on conversion of retainable object pointers.
Using a __bridge_retained or __bridge_transfer cast purely to convince ARC > to emit an unbalanced retain or release, respectively, is poor form.
id<MTLCommandQueue> objcQueue = /* Your Objective-c queue */;
MTL::CommandQueue* queue = (__bridge MTL::CommandQueue*)objcQueue;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With