-# _c_unserialize()
-
-def _c_sizeof_helper(self):
- _h_setlevel(1)
- _c_setlevel(1)
-
- # _unserialize()
- _hc('')
- # _unserialize() returns the buffer size as well
- _hc('int')
-
- variable_size_fields = 0
- # maximum space required for type definition of function arguments
- maxtypelen = 0
- param_fields, wire_fields, params = get_serialize_params('sizeof', self)
-
- # determine N(variable_fields)
- for field in param_fields:
- # if self.is_switch, treat all fields as if they are variable sized
- if not field.type.fixed_size() or self.is_switch:
- variable_size_fields += 1
- # determine maxtypelen
- for p in params:
- maxtypelen = max(maxtypelen, len(p[0]) + len(p[1]))
-
- # write to .c/.h
- for idx, p in enumerate(params):
- line = ""
- typespec, pointerspec, field_name = p
- indent = ' '*(len(self.c_sizeof_name)+2)
- # p==0: function declaration
- if 0==idx:
- line = "%s (" % self.c_sizeof_name
- indent = ''
- spacing = ' '*(maxtypelen-len(typespec)-len(pointerspec))
- line += "%s%s%s %s%s /**< */" % (indent, typespec, spacing, pointerspec, field_name)
- if idx < len(params)-1:
- _hc("%s," % line)
- else:
- _h("%s);" % line)
- _c("%s)" % line)
-
- _c('{')
-
- param_names = [p[2] for p in params]
- if 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))
- elif self.var_followed_by_fixed_fields:
- # special case: call _unserialize()
- _c(' %s *_aux = NULL;', self.c_type)
- _c(' return %s(%s, &_aux);', self.c_unserialize_name, reduce(lambda x,y: "%s, %s" % (x, y), param_names))
- # otherwise: evaluate parameters directly
- else:
- code_lines = []
- temp_vars = []
- _c_serialize_helper('sizeof', self,
- code_lines, temp_vars)
- _c(' char *xcb_tmp = (char *)_buffer;')
- if len(filter(lambda x: x.find('_aux')!=-1, code_lines))>0:
- if not self.var_followed_by_fixed_fields:
- _c(' const %s *_aux = (%s *)_buffer;', self.c_type, self.c_type)
- else:
- _c(' %s *_aux = malloc(sizeof(%s));', self.c_type, self.c_type)
- _c(' unsigned int xcb_buffer_len = 0;')
- _c(' unsigned int xcb_block_len = 0;')
- _c(' unsigned int xcb_pad = 0;')
- for t in temp_vars:
- _c(t)
- _c('')
-
- for l in code_lines:
- _c(l)
- _c('')
- _c(' return xcb_buffer_len;')
- _c('}')
-# _c_sizeof_helper()