[Xcb] [PATCH lib] c_client.py: Handle sizeof expression

Daniel Martin consume.noise at gmail.com
Sun Dec 23 06:29:24 PST 2012


If a container has a sizeof expression (atm. struct with sizeof tag),
make sure that the generated sizeof function handles it.

Signed-off-by: Daniel Martin <consume.noise at gmail.com>
---
 src/c_client.py | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/c_client.py b/src/c_client.py
index 1ec4b9e..6b7f5c2 100644
--- a/src/c_client.py
+++ b/src/c_client.py
@@ -334,6 +334,9 @@ def _c_type_setup(self, name, postfix):
         prev_varsized_offset = 0
         first_field_after_varsized = None
 
+        if hasattr(self, 'sizeof_expr') and self.sizeof_expr != None:
+            self.need_sizeof = True
+
         for field in self.fields:
             _c_type_setup(field.type, field.field_type, ())
             if field.type.is_list:
@@ -1153,7 +1156,14 @@ def _c_serialize(context, self):
 
     elif 'sizeof' == context:
         param_names = [p[2] for p in params]
-        if self.is_switch:
+        if hasattr(self, 'sizeof_expr') and self.sizeof_expr != None:
+            fields = {}
+            fields.update(_c_helper_field_mapping(self.sizeof_expr.parent, [('_aux', '->', self.sizeof_expr.parent)], flat=True))
+            _c('    const %s *_aux = (%s *)_buffer;', self.c_type, self.c_type)
+            _c('    return %s;', _c_accessor_get_expr(self.sizeof_expr, fields))
+            _c('}')
+            return
+        elif self.is_switch:
             # switch: call _unpack()
             _c('    %s _aux;', self.c_type)
             _c('    return %s(%s, &_aux);', self.c_unpack_name, reduce(lambda x,y: "%s, %s" % (x, y), param_names))
-- 
1.8.0.2



More information about the Xcb mailing list