[Xcb] [PATCH] c_client.py: Prefer bit shifts over values in enum

Daniel Martin consume.noise at gmail.com
Thu Jun 6 15:00:39 PDT 2013


When generating enum structures, prefer bit shifts over values. And
align the equal signs.

Signed-off-by: Daniel Martin <consume.noise at gmail.com>
---

An example of the result is:

--- orig/glx.h	2013-06-06 22:31:27.470024926 +0200
+++ src/glx.h	2013-06-06 23:38:38.564460965 +0200
@@ -476,27 +476,27 @@ typedef struct xcb_glx_copy_context_requ
 } xcb_glx_copy_context_request_t;
 
 typedef enum xcb_glx_gc_t {
-    XCB_GLX_GC_GL_CURRENT_BIT = 1,
-    XCB_GLX_GC_GL_POINT_BIT = 2,
-    XCB_GLX_GC_GL_LINE_BIT = 4,
-    XCB_GLX_GC_GL_POLYGON_BIT = 8,
-    XCB_GLX_GC_GL_POLYGON_STIPPLE_BIT = 16,
-    XCB_GLX_GC_GL_PIXEL_MODE_BIT = 32,
-    XCB_GLX_GC_GL_LIGHTING_BIT = 64,
-    XCB_GLX_GC_GL_FOG_BIT = 128,
-    XCB_GLX_GC_GL_DEPTH_BUFFER_BIT = 256,
-    XCB_GLX_GC_GL_ACCUM_BUFFER_BIT = 512,
-    XCB_GLX_GC_GL_STENCIL_BUFFER_BIT = 1024,
-    XCB_GLX_GC_GL_VIEWPORT_BIT = 2048,
-    XCB_GLX_GC_GL_TRANSFORM_BIT = 4096,
-    XCB_GLX_GC_GL_ENABLE_BIT = 8192,
-    XCB_GLX_GC_GL_COLOR_BUFFER_BIT = 16384,
-    XCB_GLX_GC_GL_HINT_BIT = 32768,
-    XCB_GLX_GC_GL_EVAL_BIT = 65536,
-    XCB_GLX_GC_GL_LIST_BIT = 131072,
-    XCB_GLX_GC_GL_TEXTURE_BIT = 262144,
-    XCB_GLX_GC_GL_SCISSOR_BIT = 524288,
-    XCB_GLX_GC_GL_ALL_ATTRIB_BITS = 16777215
+    XCB_GLX_GC_GL_CURRENT_BIT         = (1 << 0),
+    XCB_GLX_GC_GL_POINT_BIT           = (1 << 1),
+    XCB_GLX_GC_GL_LINE_BIT            = (1 << 2),
+    XCB_GLX_GC_GL_POLYGON_BIT         = (1 << 3),
+    XCB_GLX_GC_GL_POLYGON_STIPPLE_BIT = (1 << 4),
+    XCB_GLX_GC_GL_PIXEL_MODE_BIT      = (1 << 5),
+    XCB_GLX_GC_GL_LIGHTING_BIT        = (1 << 6),
+    XCB_GLX_GC_GL_FOG_BIT             = (1 << 7),
+    XCB_GLX_GC_GL_DEPTH_BUFFER_BIT    = (1 << 8),
+    XCB_GLX_GC_GL_ACCUM_BUFFER_BIT    = (1 << 9),
+    XCB_GLX_GC_GL_STENCIL_BUFFER_BIT  = (1 << 10),
+    XCB_GLX_GC_GL_VIEWPORT_BIT        = (1 << 11),
+    XCB_GLX_GC_GL_TRANSFORM_BIT       = (1 << 12),
+    XCB_GLX_GC_GL_ENABLE_BIT          = (1 << 13),
+    XCB_GLX_GC_GL_COLOR_BUFFER_BIT    = (1 << 14),
+    XCB_GLX_GC_GL_HINT_BIT            = (1 << 15),
+    XCB_GLX_GC_GL_EVAL_BIT            = (1 << 16),
+    XCB_GLX_GC_GL_LIST_BIT            = (1 << 17),
+    XCB_GLX_GC_GL_TEXTURE_BIT         = (1 << 18),
+    XCB_GLX_GC_GL_SCISSOR_BIT         = (1 << 19),
+    XCB_GLX_GC_GL_ALL_ATTRIB_BITS     = 16777215
 } xcb_glx_gc_t;

 src/c_client.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/c_client.py b/src/c_client.py
index 942e78a..a1982c9 100644
--- a/src/c_client.py
+++ b/src/c_client.py
@@ -269,15 +269,26 @@ def c_enum(self, name):
     _h('typedef enum %s {', tname)
 
     count = len(self.values)
+    # convert list of simple tuples (name, value) to dictionary for easier
+    # look up later
+    bits = dict(self.bits)
+    # max_name_len is used to indent the equal sign
+    max_name_len = max( [ len(_n(name + (enam,))) for (enam, val) in self.values ] )
 
     for (enam, eval) in self.values:
         count = count - 1
+        spaces = ' ' * (max_name_len - len(_n(name + (enam,)))) if eval != '' else ''
         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)
+
+        # if we have a bit value for this entry, we prefer it (override eval)
+        bit_eval = bits.get(enam, None) # look up in dictionary, return bit value or None
+        if bit_eval != None:
+            eval = '(1 << %s)' % bit_eval
+        _h('    %s%s%s%s%s%s', _n(name + (enam,)).upper(), spaces, equals, eval, comma, doc)
 
     _h('} %s;', tname)
 
-- 
1.8.3



More information about the Xcb mailing list