mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Along the way created bridging utilities for ObjectiveC functions that take "out" arguments and added an fmap for operating on Optionals Swift SVN r11123
28 lines
815 B
Swift
28 lines
815 B
Swift
/// \brief A means of accepting "out" parameters from Objective-C
|
|
/// functions taking the parameters by pointer
|
|
func withOutArgument<T, Result>(
|
|
arg: @inout T,
|
|
f: (UnsafePointer<T>)->Result
|
|
) -> Result
|
|
{
|
|
return f(UnsafePointer<T>(Builtin.addressof(&arg)))
|
|
}
|
|
|
|
/// \brief A means of accepting @autorelease "out" parameters from
|
|
/// Objective-C functions, which can be tricky because they are
|
|
/// not handed to us at +1 like other objects.
|
|
func withAutoReleasedOutArgument<T, Result>(
|
|
arg: @inout T?,
|
|
f: (UnsafePointer<T>)->Result
|
|
) -> Result
|
|
{
|
|
var buffer: Builtin.RawPointer = Builtin.inttoptr_Int64(0.value)
|
|
var address = UnsafePointer<T>(Builtin.addressof(&buffer))
|
|
var result = f(address)
|
|
|
|
arg = Int64(Builtin.ptrtoint_Int64(buffer)) == 0
|
|
? .None : .Some(address.get())
|
|
|
|
return result
|
|
}
|