[Spice-devel] [PATCH v5 34/41] codegen: Reuse enumerator constructor

Frediano Ziglio fziglio at redhat.com
Fri Aug 7 08:01:03 PDT 2015


EnumType.__init__ and FlagsType.__init__ was equivalents, merge into
the base class EnumBaseType.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 python_modules/ptypes.py | 87 ++++++++++++++++--------------------------------
 1 file changed, 29 insertions(+), 58 deletions(-)

diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
index 9436e36..3f278a2 100644
--- a/python_modules/ptypes.py
+++ b/python_modules/ptypes.py
@@ -319,6 +319,35 @@ class TypeAlias(Type):
         return self.name
 
 class EnumBaseType(Type):
+    def __init__(self, bits, name, enums, attribute_list):
+        Type.__init__(self)
+        self.bits = bits
+        self.name = name
+
+        last = -1
+        names = {}
+        values = {}
+        descs = {}
+        for v in enums:
+            name = v[0]
+            desc = v[1][1]
+            if len(v) > 2:
+                value = v[2]
+            else:
+                value = last + 1
+            last = value
+
+            assert value not in names
+            names[value] = name
+            descs[value] = desc
+            values[name] = value
+
+        self.names = names
+        self.values = values
+        self.descs = descs
+
+        self.attributes = fix_attributes(attribute_list)
+
     def is_enum(self):
         return isinstance(self, EnumType)
 
@@ -363,35 +392,6 @@ class EnumBaseType(Type):
 
 
 class EnumType(EnumBaseType):
-    def __init__(self, bits, name, enums, attribute_list):
-        Type.__init__(self)
-        self.bits = bits
-        self.name = name
-
-        last = -1
-        names = {}
-        values = {}
-        descs = {}
-        for v in enums:
-            name = v[0]
-            desc = v[1][1]
-            if len(v) > 2:
-                value = v[2]
-            else:
-                value = last + 1
-            last = value
-
-            assert value not in names
-            names[value] = name
-            descs[value] = desc
-            values[name] = value
-
-        self.names = names
-        self.values = values
-        self.descs = descs
-
-        self.attributes = fix_attributes(attribute_list)
-
     def __str__(self):
         return "enum %s" % self.name
 
@@ -420,35 +420,6 @@ class EnumType(EnumBaseType):
         writer.newline()
 
 class FlagsType(EnumBaseType):
-    def __init__(self, bits, name, flags, attribute_list):
-        Type.__init__(self)
-        self.bits = bits
-        self.name = name
-
-        last = -1
-        names = {}
-        values = {}
-        descs = {}
-        for v in flags:
-            name = v[0]
-            desc = v[1][1]
-            if len(v) > 2:
-                value = v[2]
-            else:
-                value = last + 1
-            last = value
-
-            assert value not in names
-            names[value] = name
-            descs[value] = desc
-            values[name] = value
-
-        self.names = names
-        self.values = values
-        self.descs = descs
-
-        self.attributes = fix_attributes(attribute_list)
-
     def __str__(self):
         return "flags %s" % self.name
 
-- 
2.1.0



More information about the Spice-devel mailing list