//===----------------------------------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// /// Evaluate `f()` and return its result, ensuring that `x` is not /// destroyed before f returns. public func withExtendedLifetime( x: T, f: ()->Result ) -> Result { let result = f() _fixLifetime(x) return result } /// Evaluate `f(x)` and return its result, ensuring that `x` is not /// destroyed before f returns. public func withExtendedLifetime( x: T, f: (T)->Result ) -> Result { let result = f(x) _fixLifetime(x) return result } extension String { /// Invoke `f` on the contents of this string, represented as /// a nul-terminated array of char, ensuring that the array's /// lifetime extends through the execution of `f`. public func withCString( f: (UnsafePointer)->Result ) -> Result { return self.nulTerminatedUTF8.withUnsafeBufferPointer { f(UnsafePointer($0.baseAddress)) } } } // Fix the lifetime of the given instruction so that the ARC optimizer does not // shorten the lifetime of x to be before this point. @transparent public func _fixLifetime(var x: T) { Builtin.fixLifetime(x) } /// Invokes `body` with an `UnsafeMutablePointer` to `arg` and returns the /// result. Useful for calling Objective-C APIs that take "in/out" /// parameters (and default-constructible "out" parameters) by pointer public func withUnsafeMutablePointer( inout arg: T, body: (UnsafeMutablePointer)->Result ) -> Result { return body(UnsafeMutablePointer(Builtin.addressof(&arg))) } /// Like `withUnsafeMutablePointer`, but passes pointers to `arg0` and `arg1`. public func withUnsafeMutablePointers( inout arg0: A0, inout arg1: A1, body: (UnsafeMutablePointer, UnsafeMutablePointer)->Result ) -> Result { return withUnsafeMutablePointer(&arg0) { arg0 in withUnsafeMutablePointer(&arg1) { arg1 in body(arg0, arg1) } } } /// Like `withUnsafeMutablePointer`, but passes pointers to `arg0`, `arg1`, /// and `arg2`. public func withUnsafeMutablePointers( inout arg0: A0, inout arg1: A1, inout arg2: A2, body: ( UnsafeMutablePointer, UnsafeMutablePointer, UnsafeMutablePointer )->Result ) -> Result { return withUnsafeMutablePointer(&arg0) { arg0 in withUnsafeMutablePointer(&arg1) { arg1 in withUnsafeMutablePointer(&arg2) { arg2 in body(arg0, arg1, arg2) } } } } /// Invokes `body` with an `UnsafePointer` to `arg` and returns the /// result. Useful for calling Objective-C APIs that take "in/out" /// parameters (and default-constructible "out" parameters) by pointer public func withUnsafePointer( inout arg: T, body: (UnsafePointer)->Result ) -> Result { return body(UnsafePointer(Builtin.addressof(&arg))) } /// Like `withUnsafePointer`, but passes pointers to `arg0` and `arg1`. public func withUnsafePointers( inout arg0: A0, inout arg1: A1, body: (UnsafePointer, UnsafePointer)->Result ) -> Result { return withUnsafePointer(&arg0) { arg0 in withUnsafePointer(&arg1) { arg1 in body(arg0, arg1) } } } /// Like `withUnsafePointer`, but passes pointers to `arg0`, `arg1`, /// and `arg2`. public func withUnsafePointers( inout arg0: A0, inout arg1: A1, inout arg2: A2, body: ( UnsafePointer, UnsafePointer, UnsafePointer )->Result ) -> Result { return withUnsafePointer(&arg0) { arg0 in withUnsafePointer(&arg1) { arg1 in withUnsafePointer(&arg2) { arg2 in body(arg0, arg1, arg2) } } } }