From 4236aa916e036cb7a496fcf68ad96a808644ec65 Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Tue, 22 Feb 2022 16:00:18 +0100 Subject: build-aux,codegen: use per-type clear methods We create new clear methods for each struct type, plus additional generic clear methods for GArrays and strings. --- build-aux/qmi-codegen/Variable.py | 6 +++++ build-aux/qmi-codegen/VariableArray.py | 39 +++------------------------------ build-aux/qmi-codegen/VariableString.py | 1 + build-aux/qmi-codegen/VariableStruct.py | 18 +++++++++++++++ 4 files changed, 28 insertions(+), 36 deletions(-) (limited to 'build-aux') diff --git a/build-aux/qmi-codegen/Variable.py b/build-aux/qmi-codegen/Variable.py index 69a24fa3..9ae9c8f9 100644 --- a/build-aux/qmi-codegen/Variable.py +++ b/build-aux/qmi-codegen/Variable.py @@ -58,6 +58,12 @@ class Variable: """ self.needs_dispose = False + """ + Variables that get allocated in heap need to have a clear method so that it + can be used as part of an array of this variable type. + """ + self.clear_method = '' + self.endian = "QMI_ENDIAN_LITTLE" if 'endian' in dictionary: endian = dictionary['endian'] diff --git a/build-aux/qmi-codegen/VariableArray.py b/build-aux/qmi-codegen/VariableArray.py index c26d8225..12dd3e4f 100644 --- a/build-aux/qmi-codegen/VariableArray.py +++ b/build-aux/qmi-codegen/VariableArray.py @@ -44,6 +44,7 @@ class VariableArray(Variable): # The array and its contents need to get disposed self.needs_dispose = True + self.clear_method = 'qmi_helpers_clear_array' # We need to know whether the variable comes in an Input container or in # an Output container, as we should not dump the element clear() helper method @@ -92,45 +93,12 @@ class VariableArray(Variable): self.array_element.emit_types(hfile, cfile, since, static) - """ - Constructs the name of the array clear function - """ - def clear_func_name(self): - # element public format might be a base type like 'gchar *' rather - # than a structure name like QmiFooBar - elt_name = self.array_element.public_format.replace('*', 'pointer') - return utils.build_underscore_name(self.name) + \ - '_' + \ - utils.build_underscore_name_from_camelcase(utils.build_camelcase_name(elt_name)) - - """ Emits the code to clear the element of the array """ def emit_helper_methods(self, hfile, cfile): self.array_element.emit_helper_methods(hfile, cfile) - # No need for the clear func if no need to dispose the contents - if self.array_element.needs_dispose == False: - return - - # No need for the clear func if we were not the ones who created - # the array - if self.container_type == "Input": - return - - translations = { 'element_format' : self.array_element.public_format, - 'underscore' : self.clear_func_name(), - 'dispose_contents' : self.array_element.build_dispose(' ', '(*p)') } - - template = ( - '\n' - 'static void\n' - '${underscore}_clear (${element_format} *p)\n' - '{\n' - '$dispose_contents' - '}\n') - cfile.write(string.Template(template).substitute(translations)) """ @@ -143,7 +111,7 @@ class VariableArray(Variable): 'variable_name' : variable_name, 'private_format' : self.private_format, 'public_array_element_format' : self.array_element.public_format, - 'underscore' : self.clear_func_name(), + 'array_element_clear_method' : self.array_element.clear_method, 'common_var_prefix' : common_var_prefix } template = ( @@ -197,8 +165,7 @@ class VariableArray(Variable): if self.array_element.needs_dispose == True: template += ( - '${lp} g_array_set_clear_func (${variable_name},\n' - '${lp} (GDestroyNotify)${underscore}_clear);\n' + '${lp} g_array_set_clear_func (${variable_name}, (GDestroyNotify)${array_element_clear_method});\n' '\n') template += ( diff --git a/build-aux/qmi-codegen/VariableString.py b/build-aux/qmi-codegen/VariableString.py index 48d6d6b7..07d0a960 100644 --- a/build-aux/qmi-codegen/VariableString.py +++ b/build-aux/qmi-codegen/VariableString.py @@ -52,6 +52,7 @@ class VariableString(Variable): self.fixed_size = '-1' # Variable-length strings in heap self.needs_dispose = True + self.clear_method = 'qmi_helpers_clear_string' if 'size-prefix-format' in dictionary: if dictionary['size-prefix-format'] == 'guint8': self.length_prefix_size = 8 diff --git a/build-aux/qmi-codegen/VariableStruct.py b/build-aux/qmi-codegen/VariableStruct.py index d81526f5..4e658083 100644 --- a/build-aux/qmi-codegen/VariableStruct.py +++ b/build-aux/qmi-codegen/VariableStruct.py @@ -40,6 +40,7 @@ class VariableStruct(Variable): # The public format of the struct is built directly from the suggested # struct type name + self.struct_type_name = struct_type_name self.public_format = utils.build_camelcase_name(struct_type_name) self.private_format = self.public_format self.element_type = self.public_format @@ -59,6 +60,8 @@ class VariableStruct(Variable): for member in self.members: if member['object'].needs_dispose == True: self.needs_dispose = True + self.clear_method = '__' + utils.build_underscore_name(self.struct_type_name) + '_clear' + break """ @@ -100,6 +103,21 @@ class VariableStruct(Variable): template = ('} ${format};\n') hfile.write(string.Template(template).substitute(translations)) + # No need for the clear func if no need to dispose the contents, + # or if we were not the ones who created the array + if self.needs_dispose and self.container_type != "Input": + translations['clear_method'] = self.clear_method + template = ( + '\n' + 'static void\n' + '${clear_method} (${format} *value)\n' + '{\n') + for member in self.members: + template += member['object'].build_dispose(' ', 'value->' + member['name']) + template += ( + '}\n') + cfile.write(string.Template(template).substitute(translations)) + """ Emit helper methods for all types in the struct -- cgit v1.2.1