//===----------------------------------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2016 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, @noescape _ f: () throws -> Result ) rethrows -> Result { defer { _fixLifetime(x) } return try f() } /// Evaluate `f(x)` and return its result, ensuring that `x` is not /// destroyed before f returns. public func withExtendedLifetime( x: T, @noescape _ f: (T) throws -> Result ) rethrows -> Result { defer { _fixLifetime(x) } return try f(x) } 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( @noescape f: (UnsafePointer) throws -> Result ) rethrows -> Result { return try self.nulTerminatedUTF8.withUnsafeBufferPointer { try 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(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( arg: inout T, @noescape _ body: (UnsafeMutablePointer) throws -> Result ) rethrows -> Result { return try body(UnsafeMutablePointer(Builtin.addressof(&arg))) } /// Like `withUnsafeMutablePointer`, but passes pointers to `arg0` and `arg1`. public func withUnsafeMutablePointers( arg0: inout A0, _ arg1: inout A1, @noescape _ body: ( UnsafeMutablePointer, UnsafeMutablePointer) throws -> Result ) rethrows -> Result { return try body( UnsafeMutablePointer(Builtin.addressof(&arg0)), UnsafeMutablePointer(Builtin.addressof(&arg1))) } /// Like `withUnsafeMutablePointer`, but passes pointers to `arg0`, `arg1`, /// and `arg2`. public func withUnsafeMutablePointers( arg0: inout A0, _ arg1: inout A1, _ arg2: inout A2, @noescape _ body: ( UnsafeMutablePointer, UnsafeMutablePointer, UnsafeMutablePointer ) throws -> Result ) rethrows -> Result { return try body( UnsafeMutablePointer(Builtin.addressof(&arg0)), UnsafeMutablePointer(Builtin.addressof(&arg1)), UnsafeMutablePointer(Builtin.addressof(&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( arg: inout T, @noescape _ body: (UnsafePointer) throws -> Result ) rethrows -> Result { return try body(UnsafePointer(Builtin.addressof(&arg))) } /// Like `withUnsafePointer`, but passes pointers to `arg0` and `arg1`. public func withUnsafePointers( arg0: inout A0, _ arg1: inout A1, @noescape _ body: (UnsafePointer, UnsafePointer) throws -> Result ) rethrows -> Result { return try body( UnsafePointer(Builtin.addressof(&arg0)), UnsafePointer(Builtin.addressof(&arg1))) } /// Like `withUnsafePointer`, but passes pointers to `arg0`, `arg1`, /// and `arg2`. public func withUnsafePointers( arg0: inout A0, _ arg1: inout A1, _ arg2: inout A2, @noescape _ body: ( UnsafePointer, UnsafePointer, UnsafePointer ) throws -> Result ) rethrows -> Result { return try body( UnsafePointer(Builtin.addressof(&arg0)), UnsafePointer(Builtin.addressof(&arg1)), UnsafePointer(Builtin.addressof(&arg2))) }