Commit 4e504716 authored by François Grisez's avatar François Grisez

Store the declared values of enumerations in the abstract API

parent b8d8eac3
......@@ -34,7 +34,9 @@ class CObject:
class CEnumValue(CObject):
pass
def __init__(self, name):
CObject.__init__(self, name)
self.value = None
class CEnum(CObject):
......@@ -360,8 +362,21 @@ class Project:
c.addMethod(f)
break
def __parseCEnumValueInitializer(self, initializer):
initializer = initializer.strip()
if not initializer.startswith('='):
return None
initializer = initializer[1:]
initializer.strip()
return initializer
def __parseCEnumValue(self, node):
ev = CEnumValue(node.find('./name').text)
initializerNode = node.find('./initializer')
if initializerNode is not None:
ev.value = self.__parseCEnumValueInitializer(initializerNode.text)
deprecatedNode = node.find(".//xrefsect[xreftitle='Deprecated']")
if deprecatedNode is not None:
ev.deprecated = True
......
......@@ -14,15 +14,19 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import re
import genapixml as CApi
class Error(RuntimeError):
pass
class BlacklistedException(Error):
pass
class Name(object):
camelCaseParsingRegex = re.compile('[A-Z][a-z0-9]*')
lowerCamelCaseSplitingRegex = re.compile('([a-z][a-z0-9]*)([A-Z][a-z0-9]*)')
......@@ -277,8 +281,22 @@ class Namespace(DocumentableObject):
child.parent = self
class Flag:
def __init__(self, position):
self.position = position
class EnumValue(DocumentableObject):
pass
def __init__(self, name):
DocumentableObject.__init__(self, name)
self.value = None
def value_from_string(self, stringValue):
m = re.match('^1\s*<<\s*([0-9]+)$', stringValue)
if m is not None:
self.value = Flag(int(m.group(1)))
else:
self.value = int(stringValue, base=0)
class Enum(DocumentableObject):
......@@ -478,7 +496,11 @@ class CParser(object):
def parse_all(self):
for enum in self.cProject.enums:
self.parse_enum(enum)
try:
self.parse_enum(enum)
except Error as e:
print('Could not parse \'{0}\' enum: {1}'.format(enum.name, e.args[0]))
for _class in self.cProject.classes:
try:
self.parse_class(_class)
......@@ -570,6 +592,11 @@ class CParser(object):
valueName = EnumValueName()
valueName.from_camel_case(cEnumValue.name, namespace=name)
aEnumValue = EnumValue(valueName)
if cEnumValue.value is not None:
try:
aEnumValue.value_from_string(cEnumValue.value)
except ValueError:
raise Error('{0} enum value has an invalid definition ({1})'.format(cEnumValue.name, cEnumValue.value))
enum.add_value(aEnumValue)
self.enumsIndex[nameStr] = enum
......
......@@ -24,7 +24,7 @@ namespace linphone {
{{#enums}}
enum {{name}} {
{{#values}}
{{name}}{{#notLast}},{{/notLast}}
{{name}}{{#value}} = {{{value}}}{{/value}}{{#notLast}},{{/notLast}}
{{/values}}
};
......
......@@ -55,6 +55,12 @@ class CppTranslator(object):
def translate_enum_value(enumValue):
enumValueDict = {}
enumValueDict['name'] = CppTranslator.translate_enum_value_name(enumValue.name)
if type(enumValue.value) is int:
enumValueDict['value'] = str(enumValue.value)
elif type(enumValue.value) is AbsApi.Flag:
enumValueDict['value'] = '1<<' + str(enumValue.value.position)
else:
enumValueDict['value'] = None
return enumValueDict
def translate_class(self, _class):
......@@ -429,7 +435,7 @@ class CppTranslator(object):
raise AbsApi.Error('{0} has been escaped'.format(_type.name))
if _type.desc is None:
raise AbsApi.Error('{0} has not been fixed'.format(_type.name.to_camel_case(fullName=True)))
raise AbsApi.Error('{0} has not been fixed'.format(_type.name))
if 'namespace' in params:
nsName = params['namespace'].name if params['namespace'] is not None else None
......
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