Commit 4d7ae6ff authored by DanmeiChen's avatar DanmeiChen

almost complete wrapper swift

parent cc4f60fd
......@@ -327,6 +327,12 @@ class Enum(DocumentableObject):
return False
return True
@property
def isFlag(self):
for enumerator in self.enumerators:
if isinstance(enumerator.value, Flag):
return True
return False
class Argument(DocumentableObject):
def __init__(self, name, argType, optional=False, default=None):
......@@ -1294,14 +1300,15 @@ class SwiftLangTranslator(CLikeLangTranslator):
else:
res = 'Bool'
elif _type.name == 'integer':
if _type.isUnsigned:
if _type.size is not None and _type.isref:
res = 'UnsafePointer<UInt8>'
else:
res = 'UInt'
else:
if _type.size is None:
res = 'Int'
else:
res = 'Int{0}'.format(_type.size)
if _type.isUnsigned:
res = 'U' + res
if _type.isref:
res = 'UnsafePointer<' + res + '>'
elif _type.name == 'string':
if dllImport:
if type(_type.parent) is Argument:
......
......@@ -82,8 +82,6 @@ class SwiftTranslator(object):
methodDict = {}
methodDict['doc'] = method.briefDescription.translate(self.docTranslator, tagAsBrief=True)
# TODO: remove it when abstractapi.py has fixed. We do not need to wrapper get_current_callbacks
methodDict['getCurrentCallbacks'] = True if 'get_current_callbacks' in method.name.to_c() else False
if genImpl:
methodDict['impl'] = {}
......@@ -105,7 +103,7 @@ class SwiftTranslator(object):
methodDict['is_class'] = self.is_linphone_type(method.returnType, False, False) and type(method.returnType) is AbsApi.ClassType
methodDict['is_enum'] = self.is_linphone_type(method.returnType, False, False) and type(method.returnType) is AbsApi.EnumType
methodDict['is_generic'] = self.is_generic(methodDict)
methodDict['isNotConstList'] = not method.returnType.isconst
methodDict['isNotConst'] = not method.returnType.isconst
methodDict['impl']['args'] = ''
methodDict['impl']['c_args'] = ''
......@@ -128,7 +126,7 @@ class SwiftTranslator(object):
methodDict['impl']['c_args'] += "Linphone" + argType + "(rawValue: CUnsignedInt(" + argName + ".rawValue))"
else:
methodDict['impl']['c_args'] += argName
elif arg.type.name == "size":
elif arg.type.name == "size" or arg.type.name == "time":
methodDict['impl']['c_args'] += argName
elif argType == "Int":
methodDict['impl']['c_args'] += "CInt(" + argName + ")"
......@@ -144,6 +142,8 @@ class SwiftTranslator(object):
methodDict['impl']['c_args'] += "ObjectArrayToBctbxList(list: " + argName + ")"
else:
methodDict['impl']['c_args'] += argName
if argType == "UnsafePointer<Int>" and not arg.type.isconst:
argType = "UnsafeMutablePointer<Int32>"
methodDict['impl']['args'] += argName + ":" + argType
......@@ -179,6 +179,7 @@ class SwiftTranslator(object):
listenerDict['delegate']['params_public'] = ""
listenerDict['delegate']['params_private'] = ""
listenerDict['delegate']['params'] = ""
listenerDict['delegate']['classLists'] = {}
for arg in method.args:
dllImportType = arg.type.translate(self.langTranslator, dllImport=True)
normalType = arg.type.translate(self.langTranslator, dllImport=False)
......@@ -189,29 +190,34 @@ class SwiftTranslator(object):
listenerDict['delegate']['params_private'] += ', '
listenerDict['delegate']['params'] += ', '
if normalType == dllImportType:
listenerDict['delegate']['params'] += argName
else:
if normalType == "bool":
listenerDict['delegate']['params'] += argName + " == 0"
elif self.is_linphone_type(arg.type, True, dllImport=False) and type(arg.type) is AbsApi.ClassType:
listenerDict['delegate']['params'] += "fromNativePtr<" + normalType + ">(" + argName + ")"
elif self.is_linphone_type(arg.type, True, dllImport=False) and type(arg.type) is AbsApi.EnumType:
listenerDict['delegate']['params'] += "(" + normalType + ")" + argName + ""
elif isinstance(arg.type, AbsApi.ListType):
if normalType == "string":
listenerDict['delegate']['params'] += "MarshalStringArray(" + argName + ")"
else:
listenerDict['delegate']['params'] += "MarshalBctbxList<" + self.get_class_array_type(normalType) + ">(" + argName + ")"
#if normalType == dllImportType:
# listenerDict['delegate']['params'] += argName + ": " + argName
#else:
if normalType == "Bool":
listenerDict['delegate']['params'] += argName + ": " + argName + ">0"
elif self.is_linphone_type(arg.type, True, dllImport=False) and type(arg.type) is AbsApi.ClassType:
listenerDict['delegate']['params'] += argName + ": " + normalType + ".getSobject(cObject: " + argName + ")!"
elif self.is_linphone_type(arg.type, True, dllImport=False) and type(arg.type) is AbsApi.EnumType:
ends = "" if arg.type.desc.isFlag else "!"
listenerDict['delegate']['params'] += argName + ": " + normalType + "(rawValue: Int(" + argName + ".rawValue))" + ends
elif isinstance(arg.type, AbsApi.ListType):
if normalType == "String":
listenerDict['delegate']['params'] += "BctbxListToStringArray(list: " + argName + ")"
else:
print 'Not supported yet: ' + delegate_name_public
return {}
listenerDict['delegate']['classLists']['classType'] = self.get_class_array_type(normalType)
listenerDict['delegate']['classLists']['argName'] = argName
listenerDict['delegate']['params'] += argName + ": " + argName + "sList"
elif normalType == "String":
listenerDict['delegate']['params'] += argName + ": charArrayToString(charPointer: " + argName +")!"
else:
print 'Not supported yet: ' + delegate_name_public
return {}
else:
listenerDict['delegate']['first_param'] = argName
listenerDict['delegate']['params'] = 'thiz'
listenerDict['delegate']['params'] = argName + ": sObject!"
listenerDict['delegate']['params_public'] += normalType + " " + argName
listenerDict['delegate']['params_private'] += dllImportType + " " + argName
listenerDict['delegate']['params_public'] += argName + ": " + normalType
listenerDict['delegate']['params_private'] += argName
listenerDict['delegate']["c_name_setter"] = c_name_setter
return listenerDict
......@@ -222,6 +228,7 @@ class SwiftTranslator(object):
#methodDict['prototype'] = "static extern IntPtr {c_name}(IntPtr thiz);".format(classname = classname, c_name = c_name)
methodDict['listener'] = True
methodDict['getListener'] = True
#methodDict['has_property'] = FALSE
methodDict['property_static'] = ''
methodDict['property_return'] = classname
......@@ -360,8 +367,9 @@ class SwiftTranslator(object):
interfaceDict = {}
interfaceDict['interfaceName'] = interface.name.translate(self.nameTranslator)
interfaceDict['set_user_data_name'] = interface.listenedClass.name.to_snake_case(fullName=True) + '_cbs_set_user_data'
interfaceDict['get_user_data_name'] = interface.listenedClass.name.to_snake_case(fullName=True) + '_cbs_get_user_data'
if interfaceDict['interfaceName'].endswith('Listener'):
interfaceDict['interfaceName'] = interfaceDict['interfaceName'][0:len(interfaceDict['interfaceName'])-8] + "Delegate"
interfaceDict['create_user_data_name'] = 'linphone_factory_create' + interface.listenedClass.name.to_snake_case(fullName=True).lstrip('linphone') + '_cbs'
interfaceDict['methods'] = []
for method in interface.instanceMethods:
......@@ -378,6 +386,9 @@ class SwiftTranslator(object):
methodDict['has_getter'] = True
methodDict['has_setter'] = False
methodDict['return'] = prop.returnType.translate(self.langTranslator, dllImport=False)
if methodDict['return'].endswith('Listener'):
methodDict['return'] = methodDict['return'][0:len(methodDict['return'])-8] + "Delegate"
methodDict['is_callbacks'] = True
methodDict['exception'] = self.throws_exception(prop.returnType)
methodDict['getter_c_name'] = prop.name.to_c()
......@@ -393,7 +404,8 @@ class SwiftTranslator(object):
methodDict['is_enum'] = self.is_linphone_type(prop.returnType, False, False) and type(prop.returnType) is AbsApi.EnumType
methodDict['is_generic'] = self.is_generic(methodDict)
methodDict['cPtr'] = '' if static else 'cPtr'
methodDict['isNotConstList'] = not prop.returnType.isconst
methodDict['isNotConst'] = not prop.returnType.isconst
methodDict['isFlag'] = methodDict['is_enum'] and prop.returnType.desc.isFlag
if methodDict['is_string'] or methodDict['is_class'] or methodDict['is_void']:
methodDict['return_default'] = "?"
......@@ -419,13 +431,13 @@ class SwiftTranslator(object):
methodDict['is_bool'] = methodDict['return'] == "Bool"
methodDict['is_void'] = methodDict['return'] == "UnsafeMutableRawPointer"
methodDict['is_int'] = methodDict['return'] == "Int" or methodDict['return'] == "UInt"
methodDict['int_method'] = "" if prop.args[0].type.name == "size" else ("CInt" if methodDict['return'] == "Int" else "CUnsignedInt")
methodDict['int_method'] = "" if prop.args[0].type.name == "size" else ("CInt" if methodDict['return'] == "Int" else "CUnsignedInt")
methodDict['is_class'] = self.is_linphone_type(prop.args[0].type, True, False) and type(prop.args[0].type) is AbsApi.ClassType
methodDict['is_enum'] = self.is_linphone_type(prop.args[0].type, True, False) and type(prop.args[0].type) is AbsApi.EnumType
methodDict['enum_type'] = "CUnsignedInt" if methodDict['is_enum'] and prop.args[0].type.desc.isUnsigned else "CInt"
methodDict['is_generic'] = self.is_generic(methodDict)
if methodDict['is_string'] or methodDict['is_class'] or methodDict['is_void'] or methodDict['is_bool']:
if methodDict['is_string'] or methodDict['is_class'] or methodDict['is_void'] or methodDict['is_bool'] or methodDict['is_enum']:
methodDict['return_default'] = "?"
elif methodDict['is_generic'] or methodDict['is_int']:
methodDict['return_default'] = " = 0"
......
......@@ -18,10 +18,9 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import Foundation
import linphone
//////////////////// region enums ////////////////////
{{#enums}}
{{#enum}}
{{#doc}}
......@@ -64,9 +63,9 @@ public enum {{enumName}}:Int
{{/enum}}
{{/enums}}
//////////////////////////////////////////////////////////////////////////////
func linphoneCharToString(charPointer: UnsafePointer<CChar>?) -> String? {
//////////////////// region utils ////////////////////
func charArrayToString(charPointer: UnsafePointer<CChar>?) -> String? {
return charPointer != nil ? String(cString: charPointer!) : nil
}
......@@ -92,6 +91,16 @@ func StringArrayToBctbxList(list: [String]) -> UnsafeMutablePointer<bctbx_list_t
return cList
}
func BctbxListToStringArray(list: UnsafeMutablePointer<bctbx_list_t>) -> [String]? {
var sList = [String]()
var cList = list
while (cList != nil) {
sList.append(String(cString: unsafeBitCast(cList.pointee.data, to: UnsafePointer<CChar>.self)))
cList = UnsafeMutablePointer<bctbx_list_t>(cList.pointee.next)
}
return sList
}
func ObjectArrayToBctbxList<T:LinphoneObject>(list: [T]) -> UnsafeMutablePointer<bctbx_list_t>? {
var cList: UnsafeMutablePointer<bctbx_list_t>? = nil
for data in list {
......@@ -101,25 +110,55 @@ func ObjectArrayToBctbxList<T:LinphoneObject>(list: [T]) -> UnsafeMutablePointer
}
protocol LinphoneObjectDelegate {
var someDate: Int {get set}
var cPtr: OpaquePointer {get set}
}
{{#classes}}
{{#_class}}
{{#hasListener}}
// TODO
class {{className}}Delegate: LinphoneObjectDelegate {
var someDate: Int
//////////////////// region Delegates ////////////////////
{{#interfaces}}
{{#interface}}
public class {{interfaceName}} : LinphoneObjectDelegate
{
var cPtr: OpaquePointer
init() {
self.someDate = Int(Date().timeIntervalSince1970)
self.cPtr = linphone_factory_create_account_creator_cbs(linphone_factory_get())
self.cPtr = {{create_user_data_name}}(linphone_factory_get())
belle_sip_object_data_set(UnsafeMutablePointer(cPtr), "swiftRef", UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()), nil)
{{#methods}}
{{#delegate}}
{{c_name_setter}}(cPtr, { ({{{params_private}}}) -> Void in
let sObject = {{interfaceClassName}}.getSobject(cObject: {{first_param}})
let delegate = sObject?.currentCallbacks
{{#classLists}}
var {{argName}}sList = [{{classType}}]()
var {{argName}}cList = {{argName}}
while ({{argName}}cList != nil) {
let data = unsafeBitCast({{argName}}cList!.pointee.data, to: OpaquePointer.self)
{{argName}}sList.append({{classType}}.getSobject(cObject: data)!)
{{argName}}cList = UnsafePointer<bctbx_list_t>({{argName}}cList!.pointee.next)
}
{{/classLists}}
delegate?.{{cb_name}}({{{params}}})
})
{{/delegate}}
{{/methods}}
}
{{#methods}}
{{#delegate}}
func {{cb_name}}({{{params_public}}}){}
{{/delegate}}
{{/methods}}
}
{{/hasListener}}
{{/interface}}
{{/interfaces}}
//////////////////// region classes ////////////////////
{{#classes}}
{{#_class}}
{{#doc}}
{{#lines}}
......@@ -128,10 +167,6 @@ class {{className}}Delegate: LinphoneObjectDelegate {
{{/doc}}
public class {{className}} : LinphoneObject
{
{{#hasListener}}
var delegates: [{{className}}Delegate]?
{{/hasListener}}
static func getSobject(cObject:OpaquePointer?) -> {{className}}? {
if cObject == nil {
return nil
......@@ -142,7 +177,7 @@ public class {{className}} : LinphoneObject
}
let sObject = {{className}}(cPointer: cObject!)
belle_sip_object_data_set(UnsafeMutablePointer(cObject), "swiftRef", UnsafeMutableRawPointer(Unmanaged.passRetained(sObject).toOpaque()), nil)
belle_sip_object_data_set(UnsafeMutablePointer(cObject), "swiftRef", UnsafeMutableRawPointer(Unmanaged.passUnretained(sObject).toOpaque()), nil)
return sObject
}
......@@ -157,9 +192,9 @@ public class {{className}} : LinphoneObject
}
return Instance!
}
{{/isLinphoneFactory}}
{{#isLinphoneCall}}
/// Get the native window handle of the video window, casted as an unsigned long.
/*public var nativeVideoWindowId: UnsafeMutableRawPointer?
{
......@@ -174,10 +209,9 @@ public class {{className}} : LinphoneObject
}*/
{{/isLinphoneCall}}
{{#isLinphoneCore}}
///static extern IntPtr linphone_core_get_ms_factory(IntPtr thiz);
public MediastreamerFactory MsFactory {
/*public MediastreamerFactory MsFactory {
get
{
IntPtr ptr = linphone_core_get_ms_factory(nativePtr);
......@@ -185,7 +219,7 @@ public class {{className}} : LinphoneObject
factory.nativePtr = ptr;
return factory;
}
}
}*/
/// Get the native window handle of the video window.
public var NativeVideoWindowIdString: String
......@@ -217,7 +251,6 @@ public class {{className}} : LinphoneObject
{{/isLinphoneCore}}
{{#dllImports}}
{{#has_property}}
{{^getCurrentCallbacks}}
{{#doc}}
{{#lines}}
......@@ -231,8 +264,17 @@ public class {{className}} : LinphoneObject
get
{
{{/has_setter}}
{{#is_callbacks}}
let cObject = {{getter_c_name}}(cPtr)
let result = belle_sip_object_data_get(UnsafeMutablePointer(cObject), "swiftRef")
if (result != nil) {
return Unmanaged<{{{return}}}>.fromOpaque(result!).takeUnretainedValue()
}
return nil
{{/is_callbacks}}
{{^is_callbacks}}
{{#is_string}}
return linphoneCharToString(charPointer: {{getter_c_name}}({{cPtr}}))
return charArrayToString(charPointer: {{getter_c_name}}({{cPtr}}))
{{/is_string}}
{{#is_bool}}
return {{getter_c_name}}({{cPtr}})>0
......@@ -241,7 +283,7 @@ public class {{className}} : LinphoneObject
return {{{return}}}.getSobject(cObject:{{getter_c_name}}({{cPtr}}))
{{/is_class}}
{{#is_enum}}
return {{{return}}}(rawValue: Int({{getter_c_name}}({{cPtr}}).rawValue))!
return {{{return}}}(rawValue: Int({{getter_c_name}}({{cPtr}}).rawValue)){{^isFlag}}!{{/isFlag}}
{{/is_enum}}
{{#is_int}}
return {{{return}}}({{getter_c_name}}({{cPtr}}))
......@@ -254,7 +296,7 @@ public class {{className}} : LinphoneObject
var cList = {{getter_c_name}}({{cPtr}})
while (cList != nil) {
swiftList.append(String(cString: unsafeBitCast(cList!.pointee.data, to: UnsafePointer<CChar>.self)))
cList = cList!.pointee.next
cList = Unsafe{{#isNotConst}}Mutable{{/isNotConst}}Pointer<bctbx_list_t>(cList!.pointee.next)
}
return swiftList
{{/is_string_list}}
......@@ -264,10 +306,11 @@ public class {{className}} : LinphoneObject
while (cList != nil) {
let data = unsafeBitCast(cList?.pointee.data, to: OpaquePointer.self)
swiftList.append({{{list_type}}}.getSobject(cObject: data)!)
cList = Unsafe{{#isNotConstList}}Mutable{{/isNotConstList}}Pointer<bctbx_list_t>(cList?.pointee.next)
cList = Unsafe{{#isNotConst}}Mutable{{/isNotConst}}Pointer<bctbx_list_t>(cList?.pointee.next)
}
return swiftList
{{/is_class_list}}
{{/is_callbacks}}
{{#has_setter}}
}
{{/has_setter}}
......@@ -290,7 +333,7 @@ public class {{className}} : LinphoneObject
{{#exception}}let exception_result = {{/exception}}{{setter_c_name}}(cPtr, newValue?.cPtr)
{{/is_class}}
{{#is_enum}}
{{#exception}}let exception_result = {{/exception}}{{setter_c_name}}(cPtr, Linphone{{{return}}}(rawValue: {{enum_type}}(newValue.rawValue)))
{{#exception}}let exception_result = {{/exception}}{{setter_c_name}}(cPtr, Linphone{{{return}}}(rawValue: {{enum_type}}(newValue{{^has_getter}}!{{/has_getter}}.rawValue)))
{{/is_enum}}
{{#is_int}}
{{#exception}}let exception_result = {{/exception}}{{setter_c_name}}(cPtr, {{int_method}}(newValue))
......@@ -309,7 +352,7 @@ public class {{className}} : LinphoneObject
{{#is_class_list}}
var cList: UnsafeMutablePointer<bctbx_list_t>? = nil
for data in newValue {
cList = bctbx_list_append(cList, data.cPtr)
cList = bctbx_list_append(cList, UnsafeMutableRawPointer(data.cPtr))
}
{{#exception}}let exception_result = {{/exception}}{{setter_c_name}}(cPtr, cList)
{{/is_class_list}}
......@@ -321,28 +364,24 @@ public class {{className}} : LinphoneObject
}
{{/has_setter}}
}
{{/getCurrentCallbacks}}
{{/has_property}}
{{#listener}}
{{#impl}}
func {{name}}({{#args}}delegate: {{className}}Delegate{{/args}}){{#return}} -> {{className}}Delegate?{{/return}}
{
{{#has_getter}}
let cbs = {{c_name}}(cPtr)
if let location = delegates?.firstIndex(where: { $0.cPtr == cbs }) {
return delegates![location]
}
{{#getListener}}
let cObject = {{c_name}}(cPtr)
let result = belle_sip_object_data_get(UnsafeMutablePointer(cObject), "swiftRef")
if (result != nil) {
return Unmanaged<{{className}}Delegate>.fromOpaque(result!).takeUnretainedValue()
}
return nil
{{/has_getter}}
{{/getListener}}
{{#addListener}}
delegates?.append(delegate)
{{c_name}}(cPtr, delegate.cPtr)
{{/addListener}}
{{#removeListener}}
if let location = delegates?.firstIndex(where: { $0.someDate == delegate.someDate }) {
{{c_name}}(cPtr, delegate.cPtr)
delegates?.remove(at: location)
}
{{c_name}}(cPtr, delegate.cPtr)
{{/removeListener}}
}
{{/impl}}
......@@ -357,7 +396,7 @@ public class {{className}} : LinphoneObject
{{static}} func {{name}}({{{args}}}) {{#return}}-> {{{type}}}{{#is_string}}?{{/is_string}}{{/return}}
{
{{#is_string}}
{{return}}linphoneCharToString(charPointer: {{c_name}}({{cPtr}}{{{c_args}}}))
{{return}}charArrayToString(charPointer: {{c_name}}({{cPtr}}{{{c_args}}}))
{{/is_string}}
{{#is_bool}}
{{return}}{{c_name}}({{cPtr}}{{{c_args}}})>0
......@@ -404,7 +443,7 @@ public class {{className}} : LinphoneObject
while (cList != nil) {
let data = unsafeBitCast(cList?.pointee.data, to: OpaquePointer.self)
swiftList.append({{{list_type}}}.getSobject(cObject: data)!)
cList = Unsafe{{#isNotConstList}}Mutable{{/isNotConstList}}Pointer<bctbx_list_t>(cList?.pointee.next)
cList = Unsafe{{#isNotConst}}Mutable{{/isNotConst}}Pointer<bctbx_list_t>(cList?.pointee.next)
}
return swiftList
{{/is_class_list}}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment