Files
swift-mirror/utils/gyb_foundation_support.py
Hugh Bellamy ad4b338062 Fix python lint failures now not excluded as we provide custom exclusions
Looks like flake8 enables other rules when you add something to the
exclusion list. We added W291
2017-03-27 12:31:56 +07:00

86 lines
2.8 KiB
Python

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}()
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}()
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}()
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)