Commit 7e2bbe2b authored by DanmeiChen's avatar DanmeiChen

wrapper swift: set some enums inside of class

parent 71f37ecd
......@@ -238,7 +238,10 @@ class SphinxPart(object):
class EnumPart(SphinxPart):
def __init__(self, enum, lang, langs, namespace=None):
SphinxPart.__init__(self, lang, langs)
self.name = enum.name.translate(self.lang.nameTranslator)
if lang.langCode == 'Swift' and type(enum.parent) is abstractapi.Class :
self.name = enum.name.translate(self.lang.nameTranslator,recursive=True, topAncestor=namespace)
else:
self.name = enum.name.translate(self.lang.nameTranslator)
self.fullName = enum.name.translate(self.lang.nameTranslator, recursive=True)
self.briefDesc = enum.briefDescription.translate(self.docTranslator)
self.enumerators = [self._translate_enumerator(enumerator) for enumerator in enum.enumerators]
......
......@@ -1284,21 +1284,15 @@ class SwiftLangTranslator(CLikeLangTranslator):
self.falseConstantToken = 'false'
self.trueConstantToken = 'true'
def translate_base_type(self, _type, dllImport=True, namespace=None):
def translate_base_type(self, _type, namespace=None):
if _type.name == 'void':
if _type.isref:
return 'UnsafeMutableRawPointer'
return 'void'
if _type.isref:
return 'UnsafeMutableRawPointer'
return 'void'
elif _type.name == 'status':
if dllImport:
return 'int'
else:
return 'void'
return 'void'
elif _type.name == 'boolean':
if dllImport:
res = 'char'
else:
res = 'Bool'
return 'Bool'
elif _type.name == 'integer':
if _type.size is None:
res = 'Int'
......@@ -1308,92 +1302,69 @@ class SwiftLangTranslator(CLikeLangTranslator):
res = 'U' + res
if _type.isref:
res = 'UnsafePointer<' + res + '>'
return res
elif _type.name == 'string':
if dllImport:
if type(_type.parent) is Argument:
return 'String'
else:
res = 'IntPtr' # TODO check
else:
return 'String'
return 'String'
elif _type.name == 'character':
if _type.isUnsigned:
res = 'byte'
return 'byte'
else:
res = 'CChar'
return 'CChar'
elif _type.name == 'time':
res = 'Int'
return 'Int'
elif _type.name == 'size':
res = 'Int'
return 'Int'
elif _type.name == 'floatant':
if _type.size is not None and _type.isref:
return 'UnsafeMutablePointer<Float>'
else:
return 'Float'
elif _type.name == 'string_array':
if dllImport or type(_type.parent) is Argument:
return 'IntPtr' # TODO:check
else:
return '[String]'
return '[String]'
else:
raise TranslationError('\'{0}\' is not a base abstract type'.format(_type.name))
return res
def translate_enum_type(self, _type, dllImport=True, namespace=None):
if dllImport and type(_type.parent) is Argument:
return 'int'
else:
return _type.desc.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace))
def translate_enum_type(self, _type, namespace=None):
return _type.desc.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace))
def translate_class_type(self, _type, dllImport=True, namespace=None):
def translate_class_type(self, _type, namespace=None):
return _type.desc.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace))
def translate_list_type(self, _type, dllImport=True, namespace=None):
if dllImport:
return 'IntPtr'
def translate_list_type(self, _type, namespace=None):
if type(_type.containedTypeDesc) is BaseType:
if _type.containedTypeDesc.name == 'string':
return '[String]'
else:
raise TranslationError('translation of bctbx_list_t of basic C types is not supported')
elif type(_type.containedTypeDesc) is ClassType:
ptrType = _type.containedTypeDesc.desc.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace))
return '[' + ptrType + ']'
else:
if type(_type.containedTypeDesc) is BaseType:
if _type.containedTypeDesc.name == 'string':
return '[String]'
else:
raise TranslationError('translation of bctbx_list_t of basic C types is not supported')
elif type(_type.containedTypeDesc) is ClassType:
ptrType = _type.containedTypeDesc.desc.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace))
return '[' + ptrType + ']'
if _type.containedTypeDesc:
raise TranslationError('translation of bctbx_list_t of enums')
else:
if _type.containedTypeDesc:
raise TranslationError('translation of bctbx_list_t of enums')
else:
raise TranslationError('translation of bctbx_list_t of unknow type !')
raise TranslationError('translation of bctbx_list_t of unknow type !')
def translate_argument(self, arg, dllImport=True, namespace=None):
return '{0} {1}'.format(
arg.type.translate(self, dllImport=dllImport, namespace=None),
arg.name.translate(self.nameTranslator)
)
def translate_argument(self, arg, namespace=None):
argType = arg.type.translate(self, namespace=None)
if argType == 'UnsafePointer<Int>' and not arg.type.isconst:
argType = 'UnsafeMutablePointer<Int32>'
elif argType == 'UnsafeMutableRawPointer':
argType = 'UnsafeMutableRawPointer?'
return '{0}: {1}'.format(arg.name.translate(self.nameTranslator), argType)
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideArgTypes=False, hideReturnType=False, stripDeclarators=False, namespace=None):
params = []
if not hideArguments:
for arg in method.args:
argType = arg.type.translate(self, dllImport=False, namespace=None)
argName = arg.name.translate(self.nameTranslator)
if argType.endswith('Listener'):
argType = argType[0:len(argType)-8] + "Delegate"
argName = "delegate"
elif argType == "UnsafePointer<Int>" and not arg.type.isconst:
argType = "UnsafeMutablePointer<Int32>"
elif argType == "UnsafeMutableRawPointer":
argType = "UnsafeMutableRawPointer?"
params.append(argName + ":" + argType if not hideArgTypes else argName + ":")
returnType = method.returnType.translate(self, dllImport=False)
returnValue = ""
if returnType != "void":
returnValue = " -> " + returnType + '?' if type(method.returnType) is ClassType and 'create' in method.name.to_word_list() else " -> " + returnType
params.append(arg.translate(self, namespace=namespace) if not hideArgTypes else argName + ':')
returnType = method.returnType.translate(self)
returnValue = ''
if returnType != 'void':
returnValue = ' -> ' + returnType + ('?' if type(method.returnType) is ClassType and 'create' in method.name.to_word_list() else '')
return '{name}({params}){returnValue}'.format(
name = method.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace)),
......
......@@ -547,10 +547,45 @@ class DoxygenTranslator(Translator):
class JavaDocTranslator(DoxygenTranslator):
def __init__(self):
DoxygenTranslator.__init__(self, 'C')
def _tag_as_brief(self, lines):
pass
class SwiftDocTranslator(JavaDocTranslator):
def __init__(self):
DoxygenTranslator.__init__(self, 'Swift')
def translate_class_reference(self, ref, **kargs):
if isinstance(ref.relatedObject, (abstractapi.Class, abstractapi.Enum)):
return '`{0}`'.format(Translator.translate_reference(self, ref))
else:
raise ReferenceTranslationError(ref.cname)
def _translate_section(self, section):
if section.kind == 'return':
section.kind = 'Returns'
elif section.kind == 'warning':
section.kind = 'Warning'
elif section.kind == 'note':
section.kind = 'Note'
elif section.kind == 'see':
section.kind = 'See also'
else:
logging.warning('doc translate section pointing on an unknown object ({0})'.format(section.kind))
return '- {0}: {1}'.format(
section.kind,
self._translate_paragraph(section.paragraph)
)
def _translate_parameter_list(self, parameterList):
text = ''
for paramDesc in parameterList.parameters:
if self.displaySelfParam or not paramDesc.is_self_parameter():
desc = self._translate_description(paramDesc.desc)
desc = desc[0] if len(desc) > 0 else ''
text += ('- Parameter {0}: {1}\n'.format(paramDesc.name.translate(self.nameTranslator), desc))
return text
class SphinxTranslator(Translator):
def __init__(self, langCode):
......@@ -626,13 +661,11 @@ class SphinxTranslator(Translator):
def translate_class_reference(self, ref, label=None, namespace=None):
refStr = Translator.translate_reference(self, ref, absName=True)
tag = self._sphinx_ref_tag(ref)
if self.domain == 'swift':
refStr = tag.split(':')[1] + " " + refStr
return ':{tag}:`{label} <{ref}>`'.format(
tag=tag,
label=label if label is not None else Translator.translate_reference(self, ref, namespace=namespace),
ref= refStr
ref=tag.split(':')[1] + " " + refStr if self.domain == 'swift' else refStr
)
def translate_function_reference(self, ref, label=None, useNamespace=True, namespace=None):
......
......@@ -335,13 +335,11 @@ class SwiftTranslator(JavaTranslator):
self.keyWordEscapes = {'protocol' : 'proto'}
def translate_enum_name(self, name, recursive=False, topAncestor=None):
namespace = name
while namespace is not None and type(namespace) is not NamespaceName:
namespace = namespace.prev
nameCopy = name.copy()
if namespace is not None:
nameCopy.delete_prefix(namespace)
return nameCopy.to_camel_case(fullName=True)
if name.prev is None or not recursive or name.prev is topAncestor:
return name.to_camel_case()
else:
params = {'recursive': recursive, 'topAncestor': topAncestor}
return name.prev.translate(self, **params) + self.nsSep + name.to_camel_case()
def translate_interface_name(self, name, **params):
name = self.translate_class_name(name, **params)
......
This diff is collapsed.
......@@ -167,6 +167,16 @@ open class {{interfaceName}} : LinphoneObjectDelegate
{{#methods}}
{{#delegate}}
{{#doc}}
{{#lines}}
/// {{{line}}}
{{/lines}}
{{/doc}}
{{#detailedDoc}}
{{#lines}}
/// {{{line}}}
{{/lines}}
{{/detailedDoc}}
open func {{cb_name}}({{{params_public}}}){{#return_class}} -> {{type}}? {return nil}{{/return_class}}{{^return_class}} {}{{/return_class}}
{{/delegate}}
{{/methods}}
......@@ -182,6 +192,11 @@ open class {{interfaceName}} : LinphoneObjectDelegate
/// {{{line}}}
{{/lines}}
{{/doc}}
{{#detailedDoc}}
{{#lines}}
/// {{{line}}}
{{/lines}}
{{/detailedDoc}}
public class {{className}} : LinphoneObject
{
static func getSobject(cObject:OpaquePointer) -> {{className}} {
......@@ -225,7 +240,47 @@ public class {{className}} : LinphoneObject
}
}
{{/isLinphoneCore}}
{{#dllImports}}
{{#classEnums}}
{{#doc}}
{{#lines}}
///{{{line}}}
{{/lines}}
{{/doc}}
{{#isFlag}}
public struct {{enumName}}:OptionSet
{
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}
{{#values}}
{{#doc}}
{{#lines}}
/// {{{line}}}
{{/lines}}
{{/doc}}
public static let {{name}} = {{enumName}}(rawValue: {{{value}}})
{{/values}}
}
{{/isFlag}}
{{^isFlag}}
public enum {{enumName}}:Int
{
{{#values}}
{{#doc}}
{{#lines}}
/// {{{line}}}
{{/lines}}
{{/doc}}
case {{name}} = {{{value}}}
{{/values}}
}
{{/isFlag}}
{{/classEnums}}
{{#properties}}
{{#has_property}}
{{#doc}}
......@@ -233,6 +288,11 @@ public class {{className}} : LinphoneObject
/// {{{line}}}
{{/lines}}
{{/doc}}
{{#detailedDoc}}
{{#lines}}
/// {{{line}}}
{{/lines}}
{{/detailedDoc}}
{{static}}public var {{property_name}}: {{{return}}}{{{return_default}}}
{
{{#has_getter}}
......@@ -318,7 +378,7 @@ public class {{className}} : LinphoneObject
{{setter_c_name}}(cPtr, newValue?.cPtr)
{{/is_class}}
{{#is_enum}}
{{setter_c_name}}(cPtr, Linphone{{{return}}}(rawValue: {{enum_type}}(newValue{{^has_getter}}!{{/has_getter}}.rawValue)))
{{setter_c_name}}(cPtr, {{returnCType}}(rawValue: {{enum_type}}(newValue{{^has_getter}}!{{/has_getter}}.rawValue)))
{{/is_enum}}
{{#is_int}}
{{setter_c_name}}(cPtr, {{int_method}}(newValue))
......@@ -360,7 +420,7 @@ public class {{className}} : LinphoneObject
let exception_result = {{setter_c_name}}(cPtr, newValue.cPtr)
{{/is_class}}
{{#is_enum}}
let exception_result = {{setter_c_name}}(cPtr, Linphone{{{return}}}(rawValue: {{enum_type}}(newValue{{^has_getter}}!{{/has_getter}}.rawValue)))
let exception_result = {{setter_c_name}}(cPtr, {{returnCType}}(rawValue: {{enum_type}}(newValue{{^has_getter}}!{{/has_getter}}.rawValue)))
{{/is_enum}}
{{#is_int}}
let exception_result = {{setter_c_name}}(cPtr, {{int_method}}(newValue))
......@@ -415,6 +475,11 @@ public class {{className}} : LinphoneObject
/// {{{line}}}
{{/lines}}
{{/doc}}
{{#detailedDoc}}
{{#lines}}
/// {{{line}}}
{{/lines}}
{{/detailedDoc}}
{{static}}public func {{name}}({{{args}}}){{throw_default}} {{#return}}-> {{{type}}}{{return_default}}{{/return}}
{
{{#is_string}}
......@@ -475,7 +540,7 @@ public class {{className}} : LinphoneObject
{{/is_class_list}}
}
{{/impl}}
{{/dllImports}}
{{/properties}}
}
{{/_class}}
{{/classes}}
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