I often use std::shared_ptr
with types that should not be copied.
For example, I have a heavy type from a different library (ArrayBuffer) that can (or should) only be used inside a std::shared_ptr
.
When passing this to Swift to call methods on it, I am currently forced to make multiple copies of that type:
func doSum(val: shared_ptr_array_buffer) {
val.pointee.calculate()
val.pointee.calculateAgain()
val.pointee.andCalculateAgain()
// 3x copy + destructor of ArrayBuffer
}
It would be great to somehow access .pointee
without actually copying it - i.e. get const ArrayBuffer&
(or borrowing ArrayBuffer
/inout ArrayBuffer
) instead of ArrayBuffer
from Swift.
Is this even possible?
I currently only see two workarounds:
- Make
ArrayBuffer
SWIFT_NONCOPYABLE
- but this doesn't work if it's an external type - Add an
ArrayBufferHolder
type that holdsstd::shared_ptr<ArrayBuffer>
and exposes wrapper methods for each method onArrayBuffer
- lots of work.