summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@lanedo.com>2012-07-03 15:09:30 +0200
committerAleksander Morgado <aleksander@lanedo.com>2012-07-03 16:08:58 +0200
commit43440015a81b1be1a3dbc1ab2e6edaf3f4cf95c5 (patch)
tree9693162f80f77646500ff9a3c6e2ffe277950693
parent442af9d9b67a5d0338a7fc863c574c4b94ed16fc (diff)
qmi-codegen: use `g_array_set_clear_func()' to clear array contents
Bumped required glib version to 2.32. Actually... there is no real issue with keeping 2.28 as minimum required version, as for now there's no array element which requires clearing, but anyway, given that QMI is to be used in quite recent kernels, it shouldn't be a big issue to require a recent glib as well.
-rw-r--r--build-aux/qmi-codegen/Field.py1
-rw-r--r--build-aux/qmi-codegen/Variable.py8
-rw-r--r--build-aux/qmi-codegen/VariableArray.py56
-rw-r--r--build-aux/qmi-codegen/utils.py10
-rw-r--r--configure.ac2
5 files changed, 53 insertions, 24 deletions
diff --git a/build-aux/qmi-codegen/Field.py b/build-aux/qmi-codegen/Field.py
index 4f670dd..37ac526 100644
--- a/build-aux/qmi-codegen/Field.py
+++ b/build-aux/qmi-codegen/Field.py
@@ -84,6 +84,7 @@ class Field:
if TypeFactory.is_type_emitted(self.fullname) is False:
TypeFactory.set_type_emitted(self.fullname)
self.variable.emit_types(hfile)
+ self.variable.emit_helper_methods(hfile, cfile)
"""
diff --git a/build-aux/qmi-codegen/Variable.py b/build-aux/qmi-codegen/Variable.py
index a592d23..8232e48 100644
--- a/build-aux/qmi-codegen/Variable.py
+++ b/build-aux/qmi-codegen/Variable.py
@@ -53,6 +53,14 @@ class Variable:
"""
+ Emits the code to custom helper methods needed by this variable.
+ They are emitted as early as possible.
+ """
+ def emit_helper_methods(self, hfile, cfile):
+ pass
+
+
+ """
Emits the code involved in reading the variable from the raw byte stream
into the specific private format.
"""
diff --git a/build-aux/qmi-codegen/VariableArray.py b/build-aux/qmi-codegen/VariableArray.py
index 3ffc9ad..402dc10 100644
--- a/build-aux/qmi-codegen/VariableArray.py
+++ b/build-aux/qmi-codegen/VariableArray.py
@@ -55,6 +55,27 @@ class VariableArray(Variable):
def emit_types(self, f):
self.array_element.emit_types(f)
+ """
+ Emits the code to clear the element of the array
+ """
+ def emit_helper_methods(self, hfile, cfile):
+ # No need for the clear func if no need to dispose the contents
+ if self.array_element.needs_dispose == False:
+ return
+
+ translations = { 'element_format' : self.array_element.public_format,
+ 'underscore' : utils.build_underscore_name_from_camelcase(self.array_element.public_format),
+ '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))
+
"""
Reading an array from the raw byte buffer is just about providing a loop to
@@ -64,6 +85,7 @@ class VariableArray(Variable):
translations = { 'lp' : line_prefix,
'private_format' : self.private_format,
'public_array_element_format' : self.array_element.public_format,
+ 'underscore' : utils.build_underscore_name_from_camelcase(self.array_element.public_format),
'variable_name' : variable_name,
'buffer_name' : buffer_name,
'buffer_len' : buffer_len }
@@ -83,7 +105,15 @@ class VariableArray(Variable):
'${lp} FALSE,\n'
'${lp} sizeof (${public_array_element_format}),\n'
'${lp} n_items);\n'
- '\n'
+ '\n')
+
+ if self.array_element.needs_dispose == True:
+ template += (
+ '${lp} g_array_set_clear_func (${variable_name},\n'
+ '${lp} (GDestroyNotify)${underscore}_clear);\n'
+ '\n')
+
+ template += (
'${lp} for (i = 0; i < n_items; i++) {\n'
'${lp} ${public_array_element_format} aux;\n'
'\n')
@@ -240,32 +270,12 @@ class VariableArray(Variable):
"""
- FIXME: we should only dispose the members of the array if the refcount of
- the array reached zero. Use g_array_set_clear_func() for that.
+ Dispose the array just with an unref
"""
def build_dispose(self, line_prefix, variable_name):
translations = { 'lp' : line_prefix,
'variable_name' : variable_name }
- built = ''
-
- if self.array_element.needs_dispose == True:
- template = (
- '${lp}{\n'
- '${lp} guint i;\n'
- '\n'
- '${lp} for (i = 0; i < ${variable_name}->len; i++) {\n')
- built += string.Template(template).substitute(translations)
-
- built += self.array_element.build_dispose(line_prefix, 'g_array_index (' + variable_name + ',' + self.array_element.public_format + ', i)')
-
- template = (
- '${lp} }\n'
- '${lp}}')
- built += string.Template(template).substitute(translations)
-
-
template = (
'${lp}g_array_unref (${variable_name});\n')
- built += string.Template(template).substitute(translations)
- return built
+ return string.Template(template).substitute(translations)
diff --git a/build-aux/qmi-codegen/utils.py b/build-aux/qmi-codegen/utils.py
index fe36c06..a6e2363 100644
--- a/build-aux/qmi-codegen/utils.py
+++ b/build-aux/qmi-codegen/utils.py
@@ -19,6 +19,7 @@
#
import string
+import re
"""
Add the common copyright header to the given file
@@ -131,6 +132,15 @@ def build_underscore_name(name):
"""
+Build an underscore name from the given camelcase name
+e.g.: "ThisIsAMessage" --> "this_is_a_message"
+"""
+def build_underscore_name_from_camelcase(camelcase):
+ s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', camelcase)
+ return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
+
+
+"""
Build a camelcase name from the given full name
e.g.: "This is a message" --> "ThisIsAMessage"
"""
diff --git a/configure.ac b/configure.ac
index 4baf88f..afef048 100644
--- a/configure.ac
+++ b/configure.ac
@@ -28,7 +28,7 @@ LIBQMI_GLIB_COMPILER_WARNINGS
dnl General dependencies
PKG_CHECK_MODULES(LIBQMI_GLIB,
- glib-2.0 >= 2.28
+ glib-2.0 >= 2.32
gobject-2.0
gio-2.0)
AC_SUBST(LIBQMI_GLIB_CFLAGS)