Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
191 lines (171 sloc) 7.45 KB
import Foundation
// Bridging of structs
{% for type in types.implementing.AutoObjCBridgeable|struct %}
@objc(XYZ{{type.name}})
class _ObjC{{type.name}} : NSObject {
private (set) var {{type.name|lowerFirstWord}}: {{type.name}}
// Initializer to be used from Swift code
init({{type.name|lowerFirstWord}}: {{type.name}}) {
self.{{type.name|lowerFirstWord}} = {{type.name|lowerFirstWord}}
}
// Initializer to be used from ObjC code
{% if type.variables.count == 0 %}override {%endif%}
init({% for variable in type.variables %}
{% if variable.type.kind == "enum"%}
{{variable.name}}: Any{%if variable.isOptional %}?{%endif%}{%if not forloop.last %}, {%endif%}
{% else %}
{{variable.name}}: {{variable.typeName}}{%if not forloop.last %}, {%endif%}{% endif %}
{% endfor %}){
{% for variable in type.variables %}
{% if variable.type.kind == "enum" %}
// Unwrapping of enumeration
guard let enumeration{{forloop.counter}} = {{variable.name}} as? _ObjC{{variable.type.name|upperFirst}} else {
preconditionFailure("Type of enumeration not valid for {{variable.name}}")
}
{% endif %}
{% endfor %}
self.{{type.name|lowerFirstWord}} = {{type.name}}(
{% for variable in type.variables %}
{% if variable.type.kind == "enum" %}
{{variable.name}}: enumeration{{forloop.counter}}.{{variable.type.name|lowerFirstWord}}{%if not forloop.last %},{%endif%}
{% else %}
{{variable.name}}: {{variable.name}}{%if not forloop.last %},{%endif%}
{% endif %}
{% endfor %})
}
{% for variable in type.variables %}
{% if variable.type.kind == "enum" %}
// Computed property for enums
var {{variable.name}} : Any{% if variable.isOptional %}?{%endif%} {
{% if variable.isMutable %}
get {
{% endif %}
{% if variable.isOptional %}
guard let value = self.{{type.name|lowerFirstWord}}.{{variable.name}} else {
return nil;
}
{% else %}
let value = self.{{type.name|lowerFirstWord}}.{{variable.name}}
{% endif %}
switch value {
{% for case in variable.type.cases %}
{% if case.hasAssociatedValue %}
case .{{ case.name }}({%for associatedValue in case.associatedValues %}let value{{forloop.counter}}{% endfor %}):
return _ObjC{{variable.type.name}}{{case.name|upperFirst}}({%for associatedValue in case.associatedValues %}value{{forloop.counter}} : value{{forloop.counter}}{%if not forloop.last %},{% endif %}{% endfor%})
{% else %} case .{{ case.name }}:
return _ObjC{{variable.type.name}}()
{% endif %}
{% endfor %}
}
{% if variable.isMutable %}
}
{% endif %}
{% if variable.isMutable %}
set {
{% for case in variable.type.cases %}
if let caseValue = newValue as? _ObjC{{variable.name|upperFirst}}{{case.name|upperFirst}} {
{% if not case.hasAssociatedValue %}
self.{{type.name|lowerFirstWord}}.{{variable.name}} = .{{case.name}}
{% else %}
self.{{type.name|lowerFirstWord}}.{{variable.name}} = .{{case.name}}({% for associatedValue in case.associatedValues %}{% if associatedValue.localName == nil %}caseValue.value{{forloop.counter}}{% else %}{{associatedValue.localName}}: caseValue.value{{forloop.counter}}{% endif %}{% endfor %})
{% endif %}
}
{% endfor %}
}
{% endif %}
}
{% endif %}
{% if variable.type.kind == "struct" %}
var {{variable.name}} : _ObjC{{variable.type.name}} {
{% if variable.isMutable %}
get {
{% endif %}
{% if variable.isOptional %}
guard let value = self.{{type.name|lowerFirstWord}}.{{variable.name}} else {
return nil;
}
{% else %}
let value = self.{{type.name|lowerFirstWord}}.{{variable.name}}
{% endif %}
return _ObjC{{variable.type.name}}({{variable.type.name|lowerFirstWord}}: value)
{% if variable.isMutable %}
}
{% endif %}
{% if variable.isMutable %}
set {
self.{{type.name|lowerFirstWord}}.{{variable.name}} = newValue.{{variable.type.name|lowerFirstWord}}
}
{% endif %}
}
{% endif %}
{% if variable.type.kind != "struct" and variable.type.kind != "enum" %}
// Forwarding property for native types
var {{variable.name}} : {{variable.typeName}}
{
get {
return self.{{type.name|lowerFirstWord}}.{{variable.name}}
}
{% if variable.isMutable %}
set {
self.{{type.name|lowerFirstWord}}.{{variable.name}} = newValue
}
{% endif %}
}
{% endif %}
{% endfor %}
}
{% endfor %}
// Bridging of enums
{% macro bridgeStructOrNative associatedValue caseValue %}
{% if associatedValue.localName == nil %}
caseValue.value{{forloop.counter}}{%if associatedValue.type.kind == "struct" %}.{{associatedValue.type.name|lowerFirstWord}}{%endif%}
{% else %}
{{associatedValue.localName}}: caseValue.value{{forloop.counter}}
{% endif %}
{% endmacro %}
{% for enum in types.implementing.AutoObjCBridgeable|enum %}
@objc(XYZ{{enum.name}})
class _ObjC{{enum.name}} : NSObject {
private (set) var {{enum.name|lowerFirstWord}}: {{enum.name}}
// Initializer for Swift code
init(value: {{enum.name}}) {
self.{{enum.name|lowerFirstWord}} = value
}
// Initializer for Objective-C code
init(caseValue: Any) {
{% for case in enum.cases %}
{%if forloop.first %}if let{%else %} else if let{%endif%} {% if case.hasAssociatedValue %}caseValue{%else%}_{%endif%} = caseValue as? _ObjC{{enum.name|upperFirst}}{{case.name|upperFirst}} {
{% if not case.hasAssociatedValue %}
self.{{enum.name|lowerFirstWord}} = .{{case.name}}
{% else %}
self.{{enum.name|lowerFirstWord}} = .{{case.name}}({% for associatedValue in case.associatedValues %}{% call bridgeStructOrNative associatedValue caseValue %}{% endfor %})
{% endif %}
}
{% endfor %}
else {
preconditionFailure("Value \(caseValue) is not compatible with cases of {{enum.name}}")
}
}
}
{% for case in enum.cases %}
// A case of {{enum.name}}
@objc(XYZ{{enum.name}}{{case.name|upperFirst}})
class _ObjC{{enum.name}}{{case.name|upperFirst}} : NSObject {
{% for associatedValue in case.associatedValues %}
{% if associatedValue.type.kind == "struct" or associatedValue.type.kind == "enum" %}
let value{{forloop.counter}} : _ObjC{{associatedValue.type.name}}
{% else %}
let value{{forloop.counter}} : {{associatedValue.typeName}}
{% endif %}
{% endfor %}
{% if not case.associatedValues.count == 0 %}
init({% for associatedValue in case.associatedValues %}{% if associatedValue.type.kind == "struct" or associatedValue.type.kind == "enum" %}value{{forloop.counter}} : _ObjC{{associatedValue.type.name}}{% else %}value{{forloop.counter}} : {{associatedValue.typeName}} {% endif %}{% if not forloop.last %},{% endif %}{% endfor %})
{
{% for associatedValue in case.associatedValues %}
self.value{{forloop.counter}} = value{{forloop.counter}}
{% endfor %}
}
{% endif %}
}
{% endfor %}
{% endfor %}