1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
def ObjectiveCBridgeableImplementationForNSValue(Type):
return """
extension {Type}: _ObjectiveCBridgeable {{
public func _bridgeToObjectiveC() -> NSValue {{
var myself = self
return NSValue(bytes: &myself, objCType: _getObjCTypeEncoding({Type}.self))
}}
public static func _forceBridgeFromObjectiveC(_ source: NSValue,
result: inout {Type}?) {{
precondition(strcmp(source.objCType,
_getObjCTypeEncoding({Type}.self)) == 0,
"NSValue does not contain the right type to bridge to {Type}")
result = {Type}()
if #available(OSX 13.0, iOS 16.0, tvOS 16.0, watchOS 6.0, *) {{
source.getValue(&result!, size: MemoryLayout<{Type}>.size)
}} else {{
source.getValue(&result!)
}}
}}
public static func _conditionallyBridgeFromObjectiveC(_ source: NSValue,
result: inout {Type}?)
-> Bool {{
if strcmp(source.objCType, _getObjCTypeEncoding({Type}.self)) != 0 {{
result = nil
return false
}}
result = {Type}()
if #available(OSX 13.0, iOS 16.0, tvOS 16.0, watchOS 6.0, *) {{
source.getValue(&result!, size: MemoryLayout<{Type}>.size)
}} else {{
source.getValue(&result!)
}}
return true
}}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSValue?)
-> {Type} {{
let unwrappedSource = source!
precondition(strcmp(unwrappedSource.objCType,
_getObjCTypeEncoding({Type}.self)) == 0,
"NSValue does not contain the right type to bridge to {Type}")
var result = {Type}()
if #available(OSX 13.0, iOS 16.0, tvOS 16.0, watchOS 6.0, *) {{
unwrappedSource.getValue(&result, size: MemoryLayout<{Type}>.size)
}} else {{
unwrappedSource.getValue(&result)
}}
return result
}}
}}
""".format(Type=Type)
def ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods(
Type,
initializer,
getter,
objCType="_getObjCTypeEncoding"
):
return """
extension {Type}: _ObjectiveCBridgeable {{
public func _bridgeToObjectiveC() -> NSValue {{
return {initializer}(self)
}}
public static func _forceBridgeFromObjectiveC(_ source: NSValue,
result: inout {Type}?) {{
precondition(strcmp(source.objCType,
{objCType}({Type}.self)) == 0,
"NSValue does not contain the right type to bridge to {Type}")
result = {getter}(source)
}}
public static func _conditionallyBridgeFromObjectiveC(_ source: NSValue,
result: inout {Type}?)
-> Bool {{
if strcmp(source.objCType, {objCType}({Type}.self)) != 0 {{
result = nil
return false
}}
result = {getter}(source)
return true
}}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSValue?)
-> {Type} {{
let unwrappedSource = source!
precondition(strcmp(unwrappedSource.objCType,
{objCType}({Type}.self)) == 0,
"NSValue does not contain the right type to bridge to {Type}")
return {getter}(unwrappedSource)
}}
}}
""".format(Type=Type, initializer=initializer,
getter=getter, objCType=objCType)
|