[Xcb] [lib 3/5] c_client.py: Prefer bit shifts over values in enum

Daniel Martin consume.noise at gmail.com
Fri Nov 22 14:27:30 PST 2013


- When generating enum items prefer a bit shift over decimal values.
- Align the equal signs.
- Add an 'u' suffix to all values to prevent warnings with long values.
  (See bug #71837.)

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=71837
Signed-off-by: Daniel Martin <consume.noise at gmail.com>
---
 src/c_client.py | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/c_client.py b/src/c_client.py
index 99fd307..2bca0c3 100644
--- a/src/c_client.py
+++ b/src/c_client.py
@@ -268,16 +268,26 @@ def c_enum(self, name):
     _h('')
     _h('typedef enum %s {', tname)
 
-    count = len(self.values)
-
+    bit_values = dict(self.bits)
+    items = []
     for (enam, eval) in self.values:
-        count = count - 1
-        equals = ' = ' if eval != '' else ''
-        comma = ',' if count > 0 else ''
-        doc = ''
-        if hasattr(self, "doc") and self.doc and enam in self.doc.fields:
-            doc = '\n/**< %s */\n' % self.doc.fields[enam]
-        _h('    %s%s%s%s%s', _n(name + (enam,)).upper(), equals, eval, comma, doc)
+        item_name = _n(name + (enam,)).upper()
+        item_value = "(1u << %su)" % bit_values[enam] \
+                if enam in bit_values else "%su" % eval
+        item_doc = self.doc.fields.get(enam, None) \
+                if self.doc is not None else None
+        items.append((item_name, item_value, item_doc))
+
+    last_item = len(items) - 1
+    max_enam_len = max(len(enam) for (enam, *tmp) in items)
+
+    for (idx, (enam, eval, doc)) in enumerate(items):
+        comma = ',' if idx < last_item else ''
+        spaces = ' ' * (max_enam_len - len(enam))
+
+        _h('    %s%s = %s%s', enam, spaces, eval, comma)
+        if doc is not None:
+            _h('/**< %s */', doc)
 
     _h('} %s;', tname)
 
-- 
1.8.4.2



More information about the Xcb mailing list