| 1 |
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify it under |
| 4 |
# the terms of the GNU Lesser General Public License as published by the Free |
| 5 |
# Software Foundation; either version 2 of the License, or (at your option) any |
| 6 |
# later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 10 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 11 |
# details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser General Public License along |
| 14 |
# with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 |
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 |
# |
| 17 |
# Copyright (C) 2012 Lanedo GmbH |
| 18 |
# Copyright (C) 2012-2022 Aleksander Morgado <[email protected]> |
| 19 |
# Copyright (c) 2022 Qualcomm Innovation Center, Inc. |
| 20 |
# |
| 21 |
|
| 22 |
import string |
| 23 |
import utils |
| 24 |
from Variable import Variable |
| 25 |
import VariableFactory |
| 26 |
|
| 27 |
""" |
| 28 |
Variable type for Arrays ('array' format) |
| 29 |
""" |
| 30 |
class VariableArray(Variable): |
| 31 |
|
| 32 |
def __init__(self, service, dictionary, array_element_type, container_type): |
| 33 |
|
| 34 |
# Call the parent constructor |
| 35 |
Variable.__init__(self, service, dictionary) |
| 36 |
|
| 37 |
self.fixed_size = 0 |
| 38 |
self.name = dictionary['name'] |
| 39 |
|
| 40 |
# The array and its contents need to get disposed |
| 41 |
self.needs_dispose = True |
| 42 |
|
| 43 |
# We need to know whether the variable comes in an Input container or in |
| 44 |
# an Output container, as we should not dump the element clear() helper method |
| 45 |
# if the variable is from an Input container. |
| 46 |
self.container_type = container_type |
| 47 |
|
| 48 |
# Disallow arrays of arrays, always use an intermediate struct |
| 49 |
if dictionary['array-element']['format'] == 'array': |
| 50 |
raise ValueError('Arrays of arrays not allowed in %s array: use an intermediate struct instead' % self.name) |
| 51 |
|
| 52 |
# Load variable type of this array |
| 53 |
if 'name' in dictionary['array-element']: |
| 54 |
self.array_element = VariableFactory.create_variable(self.service, |
| 55 |
dictionary['array-element'], |
| 56 |
array_element_type + ' ' + dictionary['array-element']['name'], |
| 57 |
self.container_type) |
| 58 |
else: |
| 59 |
self.array_element = VariableFactory.create_variable(self.service, |
| 60 |
dictionary['array-element'], |
| 61 |
'', |
| 62 |
self.container_type) |
| 63 |
|
| 64 |
if 'size-prefix-format' in dictionary and 'fixed-size' in dictionary: |
| 65 |
raise ValueError('Cannot give \'size-prefix-format\' and \'fixed-size\' in %s array at the same time' % self.name) |
| 66 |
elif 'size-prefix-format' in dictionary: |
| 67 |
# Load variable type for the array size prefix |
| 68 |
# We do NOT allow 64-bit types as array sizes (GArray won't support them) |
| 69 |
if dictionary['size-prefix-format'] not in [ 'guint8', 'guint16', 'guint32' ]: |
| 70 |
raise ValueError('Invalid size prefix format (%s) in %s array: not guint8 or guint16 or guint32' % |
| 71 |
(dictionary['size-prefix-format'], self.name)) |
| 72 |
default_array_size = { 'format' : dictionary['size-prefix-format'] } |
| 73 |
self.array_size_element = VariableFactory.create_variable(self.service, default_array_size, '', self.container_type) |
| 74 |
# Load variable type for the sequence prefix, if any |
| 75 |
if 'sequence-prefix-format' in dictionary: |
| 76 |
sequence = { 'format' : dictionary['sequence-prefix-format'] } |
| 77 |
self.array_sequence_element = VariableFactory.create_variable(self.service, sequence, '', self.container_type) |
| 78 |
else: |
| 79 |
self.array_sequence_element = '' |
| 80 |
|
| 81 |
elif 'fixed-size' in dictionary: |
| 82 |
# fixed-size arrays have no size element, obviously |
| 83 |
self.fixed_size = dictionary['fixed-size'] |
| 84 |
if int(self.fixed_size) == 0 or int(self.fixed_size) > 512: |
| 85 |
raise ValueError('Fixed array size %s out of bounds (not between 0 and 512)' % self.fixed_size) |
| 86 |
if 'sequence-prefix-format' in dictionary: |
| 87 |
raise ValueError('\'sequence-prefix-format\' is not supported along with \'fixed-size\' in %s array' % self.name) |
| 88 |
else: |
| 89 |
self.array_sequence_element = '' |
| 90 |
else: |
| 91 |
raise ValueError('Missing \'size-prefix-format\' or \'fixed-size\' in %s array' % self.name) |
| 92 |
|
| 93 |
# Arrays need compat GIR support if the array element needs compat GIR support |
| 94 |
self.needs_compat_gir = self.array_element.needs_compat_gir |
| 95 |
|
| 96 |
# Arrays contain personal info if the array element contains personal info or they themselves are personal info |
| 97 |
self.contains_personal_info = self.contains_personal_info or self.array_element.contains_personal_info |
| 98 |
|
| 99 |
|
| 100 |
def emit_types(self, hfile, cfile, since, static): |
| 101 |
self.array_element.emit_types(hfile, cfile, since, static) |
| 102 |
|
| 103 |
|
| 104 |
def emit_types_gir(self, hfile, cfile, since): |
| 105 |
if self.array_element.needs_compat_gir: |
| 106 |
self.array_element.emit_types_gir(hfile, cfile, since) |
| 107 |
|
| 108 |
|
| 109 |
def emit_buffer_read(self, f, line_prefix, tlv_out, error, variable_name): |
| 110 |
common_var_prefix = utils.build_underscore_name(self.name) |
| 111 |
translations = { 'lp' : line_prefix, |
| 112 |
'variable_name' : variable_name, |
| 113 |
'private_format' : self.private_format, |
| 114 |
'array_element_public_format' : self.array_element.public_format, |
| 115 |
'array_element_clear_method' : self.array_element.clear_method, |
| 116 |
'common_var_prefix' : common_var_prefix } |
| 117 |
|
| 118 |
template = ( |
| 119 |
'${lp}{\n' |
| 120 |
'${lp} guint ${common_var_prefix}_i;\n') |
| 121 |
f.write(string.Template(template).substitute(translations)) |
| 122 |
|
| 123 |
if self.fixed_size: |
| 124 |
translations['fixed_size'] = self.fixed_size |
| 125 |
|
| 126 |
template = ( |
| 127 |
'${lp} guint16 ${common_var_prefix}_n_items = ${fixed_size};\n' |
| 128 |
'\n') |
| 129 |
f.write(string.Template(template).substitute(translations)) |
| 130 |
else: |
| 131 |
translations['array_size_element_format'] = self.array_size_element.public_format |
| 132 |
template = ( |
| 133 |
'${lp} ${array_size_element_format} ${common_var_prefix}_n_items;\n') |
| 134 |
|
| 135 |
if self.array_sequence_element != '': |
| 136 |
translations['array_sequence_element_format'] = self.array_sequence_element.public_format |
| 137 |
template += ( |
| 138 |
'${lp} ${array_sequence_element_format} ${common_var_prefix}_sequence;\n') |
| 139 |
|
| 140 |
template += ( |
| 141 |
'\n' |
| 142 |
'${lp} /* Read number of items in the array */\n') |
| 143 |
f.write(string.Template(template).substitute(translations)) |
| 144 |
self.array_size_element.emit_buffer_read(f, line_prefix + ' ', tlv_out, error, common_var_prefix + '_n_items') |
| 145 |
|
| 146 |
if self.array_sequence_element != '': |
| 147 |
template = ( |
| 148 |
'\n' |
| 149 |
'${lp} /* Read sequence in the array */\n') |
| 150 |
f.write(string.Template(template).substitute(translations)) |
| 151 |
self.array_size_element.emit_buffer_read(f, line_prefix + ' ', tlv_out, error, common_var_prefix + '_sequence') |
| 152 |
|
| 153 |
template = ( |
| 154 |
'\n' |
| 155 |
'${lp} ${variable_name}_sequence = ${common_var_prefix}_sequence;\n') |
| 156 |
f.write(string.Template(template).substitute(translations)) |
| 157 |
|
| 158 |
template = ( |
| 159 |
'\n' |
| 160 |
'${lp} ${variable_name} = g_array_sized_new (\n' |
| 161 |
'${lp} FALSE,\n' |
| 162 |
'${lp} FALSE,\n' |
| 163 |
'${lp} sizeof (${array_element_public_format}),\n' |
| 164 |
'${lp} (guint)${common_var_prefix}_n_items);\n' |
| 165 |
'\n') |
| 166 |
|
| 167 |
if self.array_element.needs_dispose: |
| 168 |
template += ( |
| 169 |
'${lp} g_array_set_clear_func (${variable_name}, (GDestroyNotify)${array_element_clear_method});\n' |
| 170 |
'\n') |
| 171 |
|
| 172 |
template += ( |
| 173 |
'${lp} for (${common_var_prefix}_i = 0; ${common_var_prefix}_i < ${common_var_prefix}_n_items; ${common_var_prefix}_i++) {\n' |
| 174 |
'${lp} ${array_element_public_format} ${common_var_prefix}_aux;\n' |
| 175 |
'\n') |
| 176 |
f.write(string.Template(template).substitute(translations)) |
| 177 |
|
| 178 |
self.array_element.emit_buffer_read(f, line_prefix + ' ', tlv_out, error, common_var_prefix + '_aux') |
| 179 |
|
| 180 |
template = ( |
| 181 |
'${lp} g_array_insert_val (${variable_name}, ${common_var_prefix}_i, ${common_var_prefix}_aux);\n' |
| 182 |
'${lp} }\n' |
| 183 |
'${lp}}\n') |
| 184 |
f.write(string.Template(template).substitute(translations)) |
| 185 |
|
| 186 |
|
| 187 |
def emit_buffer_write(self, f, line_prefix, tlv_name, variable_name): |
| 188 |
common_var_prefix = utils.build_underscore_name(self.name) |
| 189 |
translations = { 'lp' : line_prefix, |
| 190 |
'variable_name' : variable_name, |
| 191 |
'common_var_prefix' : common_var_prefix } |
| 192 |
|
| 193 |
template = ( |
| 194 |
'${lp}{\n' |
| 195 |
'${lp} guint ${common_var_prefix}_i;\n') |
| 196 |
f.write(string.Template(template).substitute(translations)) |
| 197 |
|
| 198 |
if self.fixed_size == 0: |
| 199 |
translations['array_size_element_format'] = self.array_size_element.private_format |
| 200 |
|
| 201 |
template = ( |
| 202 |
'${lp} ${array_size_element_format} ${common_var_prefix}_n_items;\n' |
| 203 |
'\n' |
| 204 |
'${lp} /* Write the number of items in the array first */\n' |
| 205 |
'${lp} ${common_var_prefix}_n_items = (${array_size_element_format}) ${variable_name}->len;\n') |
| 206 |
f.write(string.Template(template).substitute(translations)) |
| 207 |
|
| 208 |
self.array_size_element.emit_buffer_write(f, line_prefix + ' ', tlv_name, common_var_prefix + '_n_items') |
| 209 |
|
| 210 |
if self.array_sequence_element != '': |
| 211 |
self.array_sequence_element.emit_buffer_write(f, line_prefix + ' ', tlv_name, variable_name + '_sequence') |
| 212 |
|
| 213 |
|
| 214 |
template = ( |
| 215 |
'\n' |
| 216 |
'${lp} for (${common_var_prefix}_i = 0; ${common_var_prefix}_i < ${variable_name}->len; ${common_var_prefix}_i++) {\n') |
| 217 |
f.write(string.Template(template).substitute(translations)) |
| 218 |
|
| 219 |
self.array_element.emit_buffer_write(f, line_prefix + ' ', tlv_name, 'g_array_index (' + variable_name + ', ' + self.array_element.public_format + ',' + common_var_prefix + '_i)') |
| 220 |
|
| 221 |
template = ( |
| 222 |
'${lp} }\n' |
| 223 |
'${lp}}\n') |
| 224 |
f.write(string.Template(template).substitute(translations)) |
| 225 |
|
| 226 |
|
| 227 |
def emit_get_printable(self, f, line_prefix, is_personal): |
| 228 |
common_var_prefix = utils.build_underscore_name(self.name) |
| 229 |
translations = { 'lp' : line_prefix, |
| 230 |
'common_var_prefix' : common_var_prefix } |
| 231 |
|
| 232 |
template = ( |
| 233 |
'${lp}{\n' |
| 234 |
'${lp} guint ${common_var_prefix}_i;\n') |
| 235 |
f.write(string.Template(template).substitute(translations)) |
| 236 |
|
| 237 |
if self.fixed_size: |
| 238 |
translations['fixed_size'] = self.fixed_size |
| 239 |
|
| 240 |
template = ( |
| 241 |
'${lp} guint16 ${common_var_prefix}_n_items = ${fixed_size};\n' |
| 242 |
'\n') |
| 243 |
f.write(string.Template(template).substitute(translations)) |
| 244 |
else: |
| 245 |
translations['array_size_element_format'] = self.array_size_element.public_format |
| 246 |
template = ( |
| 247 |
'${lp} ${array_size_element_format} ${common_var_prefix}_n_items;\n') |
| 248 |
|
| 249 |
if self.array_sequence_element != '': |
| 250 |
translations['array_sequence_element_format'] = self.array_sequence_element.public_format |
| 251 |
template += ( |
| 252 |
'${lp} ${array_sequence_element_format} ${common_var_prefix}_sequence;\n') |
| 253 |
|
| 254 |
template += ( |
| 255 |
'\n' |
| 256 |
'${lp} /* Read number of items in the array */\n') |
| 257 |
f.write(string.Template(template).substitute(translations)) |
| 258 |
self.array_size_element.emit_buffer_read(f, line_prefix + ' ', 'out', '&error', common_var_prefix + '_n_items') |
| 259 |
|
| 260 |
if self.array_sequence_element != '': |
| 261 |
template = ( |
| 262 |
'\n' |
| 263 |
'${lp} /* Read sequence */\n') |
| 264 |
f.write(string.Template(template).substitute(translations)) |
| 265 |
self.array_sequence_element.emit_buffer_read(f, line_prefix + ' ', 'out', '&error', common_var_prefix + '_sequence') |
| 266 |
template = ( |
| 267 |
'\n' |
| 268 |
'${lp} g_string_append_printf (printable, "[[Seq:%u]] ", ${common_var_prefix}_sequence);\n') |
| 269 |
f.write(string.Template(template).substitute(translations)) |
| 270 |
|
| 271 |
template = ( |
| 272 |
'\n' |
| 273 |
'${lp} g_string_append (printable, "{");\n' |
| 274 |
'\n' |
| 275 |
'${lp} for (${common_var_prefix}_i = 0; ${common_var_prefix}_i < ${common_var_prefix}_n_items; ${common_var_prefix}_i++) {\n' |
| 276 |
'${lp} g_string_append_printf (printable, " [%u] = \'", ${common_var_prefix}_i);\n') |
| 277 |
f.write(string.Template(template).substitute(translations)) |
| 278 |
|
| 279 |
self.array_element.emit_get_printable(f, line_prefix + ' ', self.personal_info or is_personal); |
| 280 |
|
| 281 |
template = ( |
| 282 |
'${lp} g_string_append (printable, "\'");\n' |
| 283 |
'${lp} }\n' |
| 284 |
'\n' |
| 285 |
'${lp} g_string_append (printable, "}");\n' |
| 286 |
'${lp}}') |
| 287 |
f.write(string.Template(template).substitute(translations)) |
| 288 |
|
| 289 |
|
| 290 |
""" |
| 291 |
We need to include SEQUENCE + GARRAY |
| 292 |
""" |
| 293 |
def build_variable_declaration(self, line_prefix, variable_name): |
| 294 |
translations = { 'lp' : line_prefix, |
| 295 |
'name' : variable_name } |
| 296 |
|
| 297 |
template = '' |
| 298 |
if self.array_sequence_element != '': |
| 299 |
translations['array_sequence_element_format'] = self.array_sequence_element.public_format |
| 300 |
template += ( |
| 301 |
'${lp}${array_sequence_element_format} ${name}_sequence;\n') |
| 302 |
|
| 303 |
template += ( |
| 304 |
'${lp}GArray *${name};\n') |
| 305 |
return string.Template(template).substitute(translations) |
| 306 |
|
| 307 |
|
| 308 |
""" |
| 309 |
We need to include GPTRARRAY |
| 310 |
""" |
| 311 |
def build_variable_declaration_gir(self, line_prefix, variable_name): |
| 312 |
if not self.array_element.needs_compat_gir: |
| 313 |
return '' |
| 314 |
|
| 315 |
translations = { 'lp' : line_prefix, |
| 316 |
'name' : variable_name } |
| 317 |
|
| 318 |
template = ( |
| 319 |
'${lp}GPtrArray *${name}_ptr;\n') |
| 320 |
return string.Template(template).substitute(translations) |
| 321 |
|
| 322 |
|
| 323 |
""" |
| 324 |
We need to include SEQUENCE + GARRAY |
| 325 |
""" |
| 326 |
def build_struct_field_declaration(self, line_prefix, variable_name): |
| 327 |
return self.build_variable_declaration(line_prefix, variable_name) |
| 328 |
|
| 329 |
|
| 330 |
""" |
| 331 |
We need to include SEQUENCE + GPTRARRAY |
| 332 |
""" |
| 333 |
def build_struct_field_declaration_gir(self, line_prefix, variable_name): |
| 334 |
if not self.array_element.needs_compat_gir: |
| 335 |
return self.build_struct_field_declaration(line_prefix, variable_name) |
| 336 |
|
| 337 |
translations = { 'lp' : line_prefix, |
| 338 |
'name' : variable_name } |
| 339 |
|
| 340 |
template = '' |
| 341 |
|
| 342 |
if self.array_sequence_element != '': |
| 343 |
translations['array_sequence_element_format'] = self.array_sequence_element.public_format |
| 344 |
template += ( |
| 345 |
'${lp}${array_sequence_element_format} ${name}_sequence;\n') |
| 346 |
|
| 347 |
template += ( |
| 348 |
'${lp}GPtrArray *${name};\n') |
| 349 |
return string.Template(template).substitute(translations) |
| 350 |
|
| 351 |
|
| 352 |
def build_getter_declaration(self, line_prefix, variable_name): |
| 353 |
if not self.visible: |
| 354 |
return '' |
| 355 |
|
| 356 |
translations = { 'lp' : line_prefix, |
| 357 |
'name' : variable_name } |
| 358 |
|
| 359 |
template = '' |
| 360 |
if self.array_sequence_element != '': |
| 361 |
translations['array_sequence_element_format'] = self.array_sequence_element.public_format |
| 362 |
template += ( |
| 363 |
'${lp}${array_sequence_element_format} *${name}_sequence,\n') |
| 364 |
|
| 365 |
template += ( |
| 366 |
'${lp}GArray **${name},\n') |
| 367 |
return string.Template(template).substitute(translations) |
| 368 |
|
| 369 |
|
| 370 |
def build_getter_declaration_gir(self, line_prefix, variable_name): |
| 371 |
if not self.array_element.needs_compat_gir: |
| 372 |
return self.build_getter_declaration(line_prefix, variable_name) |
| 373 |
|
| 374 |
if not self.visible: |
| 375 |
return '' |
| 376 |
|
| 377 |
translations = { 'lp' : line_prefix, |
| 378 |
'name' : variable_name } |
| 379 |
|
| 380 |
template = '' |
| 381 |
if self.array_sequence_element != '': |
| 382 |
translations['array_sequence_element_format'] = self.array_sequence_element.public_format |
| 383 |
template += ( |
| 384 |
'${lp}${array_sequence_element_format} *${name}_sequence,\n') |
| 385 |
|
| 386 |
template += ( |
| 387 |
'${lp}GPtrArray **${name}_ptr,\n') |
| 388 |
return string.Template(template).substitute(translations) |
| 389 |
|
| 390 |
|
| 391 |
def build_getter_documentation(self, line_prefix, variable_name): |
| 392 |
if not self.visible: |
| 393 |
return '' |
| 394 |
|
| 395 |
translations = { 'lp' : line_prefix, |
| 396 |
'array_element_public_format' : self.array_element.public_format, |
| 397 |
'array_element_element_type' : self.array_element.element_type, |
| 398 |
'name' : variable_name } |
| 399 |
|
| 400 |
template = '' |
| 401 |
if self.array_sequence_element != '': |
| 402 |
template += ( |
| 403 |
'${lp}@${name}_sequence: (out)(optional): a placeholder for the output sequence number, or %NULL if not required.\n') |
| 404 |
|
| 405 |
template += ( |
| 406 |
'${lp}@${name}: (out)(optional)(element-type ${array_element_element_type})(transfer none): a placeholder for the output #GArray of #${array_element_public_format} elements, or %NULL if not required. Do not free it, it is owned by @self.\n') |
| 407 |
return string.Template(template).substitute(translations) |
| 408 |
|
| 409 |
|
| 410 |
def build_getter_documentation_gir(self, line_prefix, variable_name): |
| 411 |
if not self.array_element.needs_compat_gir: |
| 412 |
return self.build_getter_documentation(line_prefix, variable_name) |
| 413 |
if not self.visible: |
| 414 |
return '' |
| 415 |
|
| 416 |
translations = { 'lp' : line_prefix, |
| 417 |
'array_element_public_format' : self.array_element.public_format, |
| 418 |
'array_element_element_type_gir' : self.array_element.element_type_gir, |
| 419 |
'name' : variable_name } |
| 420 |
|
| 421 |
template = '' |
| 422 |
if self.array_sequence_element != '': |
| 423 |
template += ( |
| 424 |
'${lp}@${name}_sequence: (out)(optional): a placeholder for the output sequence number, or %NULL if not required.\n') |
| 425 |
|
| 426 |
template += ( |
| 427 |
'${lp}@${name}_ptr: (out)(optional)(element-type ${array_element_element_type_gir})(transfer none): a placeholder for the output array of #${array_element_public_format} elements, or %NULL if not required. Do not free or modify it, it is owned by @self.\n') |
| 428 |
return string.Template(template).substitute(translations) |
| 429 |
|
| 430 |
|
| 431 |
def build_getter_implementation(self, line_prefix, variable_name_from, variable_name_to): |
| 432 |
if not self.visible: |
| 433 |
return '' |
| 434 |
|
| 435 |
translations = { 'lp' : line_prefix, |
| 436 |
'from' : variable_name_from, |
| 437 |
'to' : variable_name_to } |
| 438 |
|
| 439 |
template = '' |
| 440 |
if self.array_sequence_element != '': |
| 441 |
template += ( |
| 442 |
'${lp}if (${to}_sequence)\n' |
| 443 |
'${lp} *${to}_sequence = ${from}_sequence;\n') |
| 444 |
|
| 445 |
template += ( |
| 446 |
'${lp}if (${to})\n' |
| 447 |
'${lp} *${to} = ${from};\n') |
| 448 |
|
| 449 |
return string.Template(template).substitute(translations) |
| 450 |
|
| 451 |
|
| 452 |
def build_getter_implementation_gir(self, line_prefix, variable_name_from, variable_name_to): |
| 453 |
if not self.array_element.needs_compat_gir: |
| 454 |
return self.build_getter_implementation(line_prefix, variable_name_from, variable_name_to) |
| 455 |
if not self.visible: |
| 456 |
return '' |
| 457 |
|
| 458 |
common_var_prefix = utils.build_underscore_name(self.name) |
| 459 |
translations = { 'lp' : line_prefix, |
| 460 |
'from' : variable_name_from, |
| 461 |
'to' : variable_name_to } |
| 462 |
|
| 463 |
template = '' |
| 464 |
if self.array_sequence_element != '': |
| 465 |
template += ( |
| 466 |
'${lp}if (${to}_sequence)\n' |
| 467 |
'${lp} *${to}_sequence = ${from}_sequence;\n') |
| 468 |
|
| 469 |
template += ( |
| 470 |
'${lp}if (${to}_ptr) {\n' |
| 471 |
'${lp} if (!${from}_ptr) {\n') |
| 472 |
|
| 473 |
template += self.build_copy_to_gir(line_prefix + ' ', |
| 474 |
variable_name_from, |
| 475 |
variable_name_from + '_ptr') |
| 476 |
|
| 477 |
template += ( |
| 478 |
'${lp} }\n' |
| 479 |
'${lp} *${to}_ptr = ${from}_ptr;\n' |
| 480 |
'${lp}}') |
| 481 |
|
| 482 |
return string.Template(template).substitute(translations) |
| 483 |
|
| 484 |
|
| 485 |
def build_setter_declaration(self, line_prefix, variable_name): |
| 486 |
if not self.visible: |
| 487 |
return '' |
| 488 |
|
| 489 |
translations = { 'lp' : line_prefix, |
| 490 |
'name' : variable_name } |
| 491 |
|
| 492 |
template = '' |
| 493 |
if self.array_sequence_element != '': |
| 494 |
translations['array_sequence_element_format'] = self.array_sequence_element.public_format |
| 495 |
template += ( |
| 496 |
'${lp}${array_sequence_element_format} ${name}_sequence,\n') |
| 497 |
|
| 498 |
template += ( |
| 499 |
'${lp}GArray *${name},\n') |
| 500 |
return string.Template(template).substitute(translations) |
| 501 |
|
| 502 |
|
| 503 |
def build_setter_declaration_gir(self, line_prefix, variable_name): |
| 504 |
if not self.array_element.needs_compat_gir: |
| 505 |
return self.build_setter_declaration(line_prefix, variable_name) |
| 506 |
if not self.visible: |
| 507 |
return '' |
| 508 |
|
| 509 |
translations = { 'lp' : line_prefix, |
| 510 |
'name' : variable_name } |
| 511 |
|
| 512 |
template = '' |
| 513 |
if self.array_sequence_element != '': |
| 514 |
translations['array_sequence_element_format'] = self.array_sequence_element.public_format |
| 515 |
template += ( |
| 516 |
'${lp}${array_sequence_element_format} ${name}_sequence,\n') |
| 517 |
|
| 518 |
template += ( |
| 519 |
'${lp}GPtrArray *${name}_ptr,\n') |
| 520 |
return string.Template(template).substitute(translations) |
| 521 |
|
| 522 |
|
| 523 |
def build_setter_documentation(self, line_prefix, variable_name): |
| 524 |
if not self.visible: |
| 525 |
return "" |
| 526 |
|
| 527 |
translations = { 'lp' : line_prefix, |
| 528 |
'array_element_public_format' : self.array_element.public_format, |
| 529 |
'array_element_element_type' : self.array_element.element_type, |
| 530 |
'name' : variable_name } |
| 531 |
|
| 532 |
template = '' |
| 533 |
if self.array_sequence_element != '': |
| 534 |
template += ( |
| 535 |
'${lp}@${name}_sequence: the sequence number.\n') |
| 536 |
|
| 537 |
template += ( |
| 538 |
'${lp}@${name}: (in)(element-type ${array_element_element_type})(transfer none): a #GArray of #${array_element_public_format} elements. A new reference to @${name} will be taken, so the caller must make sure the array was created with the correct #GDestroyNotify as clear function for each element in the array.\n') |
| 539 |
return string.Template(template).substitute(translations) |
| 540 |
|
| 541 |
|
| 542 |
def build_setter_documentation_gir(self, line_prefix, variable_name): |
| 543 |
if not self.array_element.needs_compat_gir: |
| 544 |
return self.build_setter_documentation(line_prefix, variable_name) |
| 545 |
if not self.visible: |
| 546 |
return "" |
| 547 |
|
| 548 |
translations = { 'lp' : line_prefix, |
| 549 |
'array_element_public_format' : self.array_element.public_format, |
| 550 |
'array_element_element_type_gir' : self.array_element.element_type_gir, |
| 551 |
'name' : variable_name } |
| 552 |
|
| 553 |
template = '' |
| 554 |
if self.array_sequence_element != '': |
| 555 |
template += ( |
| 556 |
'${lp}@${name}_sequence: the sequence number.\n') |
| 557 |
|
| 558 |
template += ( |
| 559 |
'${lp}@${name}_ptr: (in)(element-type ${array_element_element_type_gir})(transfer none): array of #${array_element_public_format} elements. The contents of the given array will be copied, the #GPtrArray will not increase its reference count.\n') |
| 560 |
return string.Template(template).substitute(translations) |
| 561 |
|
| 562 |
|
| 563 |
def build_setter_implementation(self, line_prefix, variable_name_from, variable_name_to): |
| 564 |
if not self.visible: |
| 565 |
return "" |
| 566 |
|
| 567 |
translations = { 'lp' : line_prefix, |
| 568 |
'from' : variable_name_from, |
| 569 |
'to' : variable_name_to } |
| 570 |
|
| 571 |
template = '' |
| 572 |
if self.array_sequence_element != '': |
| 573 |
template += ( |
| 574 |
'${lp}${to}_sequence = ${from}_sequence;\n') |
| 575 |
|
| 576 |
template += self.build_dispose(line_prefix, variable_name_to) |
| 577 |
template += self.build_dispose_gir(line_prefix, variable_name_to) |
| 578 |
|
| 579 |
template +=( |
| 580 |
'${lp}${to} = g_array_ref (${from});\n') |
| 581 |
return string.Template(template).substitute(translations) |
| 582 |
|
| 583 |
|
| 584 |
def build_setter_implementation_gir(self, line_prefix, variable_name_from, variable_name_to): |
| 585 |
if not self.array_element.needs_compat_gir: |
| 586 |
return self.build_setter_implementation(line_prefix, variable_name_from, variable_name_to) |
| 587 |
if not self.visible: |
| 588 |
return "" |
| 589 |
|
| 590 |
common_var_prefix = utils.build_underscore_name(self.name) |
| 591 |
translations = { 'lp' : line_prefix, |
| 592 |
'from' : variable_name_from, |
| 593 |
'to' : variable_name_to } |
| 594 |
|
| 595 |
template = '' |
| 596 |
if self.array_sequence_element != '': |
| 597 |
template += ( |
| 598 |
'${lp}${to}_sequence = ${from}_sequence;\n') |
| 599 |
|
| 600 |
template += self.build_dispose(line_prefix, variable_name_to) |
| 601 |
template += self.build_dispose_gir(line_prefix, variable_name_to + '_ptr') |
| 602 |
template += self.build_copy_from_gir(line_prefix, variable_name_from + '_ptr', variable_name_to) |
| 603 |
|
| 604 |
return string.Template(template).substitute(translations) |
| 605 |
|
| 606 |
|
| 607 |
def build_struct_field_documentation(self, line_prefix, variable_name): |
| 608 |
translations = { 'lp' : line_prefix, |
| 609 |
'array_element_public_format' : self.array_element.public_format, |
| 610 |
'name' : variable_name } |
| 611 |
|
| 612 |
template = '' |
| 613 |
if self.array_sequence_element != '': |
| 614 |
template += ( |
| 615 |
'${lp}@${name}_sequence: the sequence number.\n') |
| 616 |
|
| 617 |
template += ( |
| 618 |
'${lp}@${name}: a #GArray of #${array_element_public_format} elements.\n') |
| 619 |
return string.Template(template).substitute(translations) |
| 620 |
|
| 621 |
|
| 622 |
def build_struct_field_documentation_gir(self, line_prefix, variable_name): |
| 623 |
if not self.array_element.needs_compat_gir: |
| 624 |
return self.build_struct_field_documentation(line_prefix, variable_name) |
| 625 |
|
| 626 |
translations = { 'lp' : line_prefix, |
| 627 |
'array_element_public_format' : self.array_element.public_format, |
| 628 |
'array_element_element_type' : self.array_element.element_type, |
| 629 |
'name' : variable_name } |
| 630 |
|
| 631 |
template = '' |
| 632 |
if self.array_sequence_element != '': |
| 633 |
template += ( |
| 634 |
'${lp}@${name}_sequence: the sequence number.\n') |
| 635 |
|
| 636 |
template += ( |
| 637 |
'${lp}@${name}: (element-type ${array_element_element_type}): an array of #${array_element_public_format} elements.\n') |
| 638 |
return string.Template(template).substitute(translations) |
| 639 |
|
| 640 |
|
| 641 |
def build_dispose(self, line_prefix, variable_name): |
| 642 |
translations = { 'lp' : line_prefix, |
| 643 |
'variable_name' : variable_name } |
| 644 |
|
| 645 |
template = ( |
| 646 |
'${lp}g_clear_pointer (&${variable_name}, (GDestroyNotify)g_array_unref);\n') |
| 647 |
return string.Template(template).substitute(translations) |
| 648 |
|
| 649 |
|
| 650 |
def build_dispose_gir(self, line_prefix, variable_name): |
| 651 |
if not self.array_element.needs_compat_gir: |
| 652 |
self.build_dispose(line_prefix, variable_name) |
| 653 |
|
| 654 |
translations = { 'lp' : line_prefix, |
| 655 |
'variable_name' : variable_name } |
| 656 |
|
| 657 |
template = ( |
| 658 |
'${lp}g_clear_pointer (&${variable_name}, (GDestroyNotify)g_ptr_array_unref);\n') |
| 659 |
return string.Template(template).substitute(translations) |
| 660 |
|
| 661 |
|
| 662 |
def build_copy_to_gir(self, line_prefix, variable_name_from, variable_name_to): |
| 663 |
common_var_prefix = utils.build_underscore_name(self.name) |
| 664 |
translations = { 'lp' : line_prefix, |
| 665 |
'to' : variable_name_to, |
| 666 |
'from' : variable_name_from } |
| 667 |
|
| 668 |
if self.array_element.needs_compat_gir: |
| 669 |
|
| 670 |
translations['common_var_prefix'] = common_var_prefix |
| 671 |
translations['array_element_public_format'] = self.array_element.public_format |
| 672 |
translations['array_element_public_format_gir'] = self.array_element.public_format_gir |
| 673 |
translations['array_element_element_type_gir'] = self.array_element.element_type_gir |
| 674 |
translations['array_element_free_method_gir'] = self.array_element.free_method_gir |
| 675 |
translations['array_element_new_method_gir'] = self.array_element.new_method_gir |
| 676 |
|
| 677 |
template = ( |
| 678 |
'${lp}{\n' |
| 679 |
'${lp} guint ${common_var_prefix}_i;\n' |
| 680 |
'\n' |
| 681 |
'${lp} ${to} = g_ptr_array_new_full (${from}->len, (GDestroyNotify)${array_element_free_method_gir});\n' |
| 682 |
'${lp} for (${common_var_prefix}_i = 0; ${common_var_prefix}_i < ${from}->len; ${common_var_prefix}_i++) {\n' |
| 683 |
'${lp} ${array_element_public_format} *${common_var_prefix}_aux_from;\n' |
| 684 |
'${lp} ${array_element_public_format_gir} *${common_var_prefix}_aux_to;\n' |
| 685 |
'\n' |
| 686 |
'${lp} ${common_var_prefix}_aux_from = &g_array_index (${from}, ${array_element_public_format}, ${common_var_prefix}_i);\n' |
| 687 |
'\n' |
| 688 |
'${lp} ${common_var_prefix}_aux_to = ${array_element_new_method_gir} ();\n') |
| 689 |
|
| 690 |
template += self.array_element.build_copy_to_gir(line_prefix + ' ', |
| 691 |
'(*${common_var_prefix}_aux_from)', |
| 692 |
'${common_var_prefix}_aux_to') |
| 693 |
|
| 694 |
template += ( |
| 695 |
'\n' |
| 696 |
'${lp} g_ptr_array_add (${to}, ${common_var_prefix}_aux_to);\n' |
| 697 |
'${lp} }\n' |
| 698 |
'${lp}}\n') |
| 699 |
else: |
| 700 |
template = ( |
| 701 |
'${lp}${to} = g_array_ref (${from});\n') |
| 702 |
return string.Template(template).substitute(translations) |
| 703 |
|
| 704 |
|
| 705 |
def build_copy_from_gir(self, line_prefix, variable_name_from, variable_name_to): |
| 706 |
translations = { 'lp' : line_prefix, |
| 707 |
'to' : variable_name_to, |
| 708 |
'from' : variable_name_from } |
| 709 |
|
| 710 |
if self.array_element.needs_compat_gir: |
| 711 |
common_var_prefix = utils.build_underscore_name(self.name) |
| 712 |
translations['common_var_prefix'] = common_var_prefix |
| 713 |
translations['array_element_public_format'] = self.array_element.public_format |
| 714 |
translations['array_element_public_format_gir'] = self.array_element.public_format_gir |
| 715 |
|
| 716 |
template = ( |
| 717 |
'${lp}{\n' |
| 718 |
'${lp} guint ${common_var_prefix}_i;\n' |
| 719 |
'\n' |
| 720 |
'${lp} ${to} = g_array_sized_new (FALSE, FALSE, sizeof (${array_element_public_format}), ${from}->len);\n' |
| 721 |
'${lp} for (${common_var_prefix}_i = 0; ${common_var_prefix}_i < ${from}->len; ${common_var_prefix}_i++) {\n' |
| 722 |
'${lp} ${array_element_public_format} ${common_var_prefix}_aux_to;\n' |
| 723 |
'${lp} ${array_element_public_format_gir} *${common_var_prefix}_aux_from;\n' |
| 724 |
'\n' |
| 725 |
'${lp} ${common_var_prefix}_aux_from = g_ptr_array_index (${from}, ${common_var_prefix}_i);\n' |
| 726 |
'\n') |
| 727 |
|
| 728 |
template += self.array_element.build_copy_from_gir(line_prefix + ' ', |
| 729 |
'${common_var_prefix}_aux_from', |
| 730 |
'${common_var_prefix}_aux_to') |
| 731 |
|
| 732 |
template += ( |
| 733 |
'\n' |
| 734 |
'${lp} g_array_append_val (${to}, ${common_var_prefix}_aux_to);\n' |
| 735 |
'${lp} }\n' |
| 736 |
'${lp}}\n') |
| 737 |
else: |
| 738 |
template = ( |
| 739 |
'${lp}${to} = g_array_ref (${from});\n') |
| 740 |
return string.Template(template).substitute(translations) |
| 741 |
|
| 742 |
|
| 743 |
def build_copy_gir(self, line_prefix, variable_name_from, variable_name_to): |
| 744 |
common_var_prefix = utils.build_underscore_name(self.name) |
| 745 |
translations = { 'lp' : line_prefix, |
| 746 |
'to' : variable_name_to, |
| 747 |
'from' : variable_name_from } |
| 748 |
|
| 749 |
# our GPtrArrays and GArrays are immutable, we can just ref the array as a copy |
| 750 |
if self.array_element.needs_compat_gir: |
| 751 |
template = ( |
| 752 |
'${lp}${to} = g_ptr_array_ref (${from});\n') |
| 753 |
else: |
| 754 |
template = ( |
| 755 |
'${lp}${to} = g_array_ref (${from});\n') |
| 756 |
return string.Template(template).substitute(translations) |
| 757 |
|
| 758 |
|
| 759 |
def add_sections(self, sections): |
| 760 |
self.array_element.add_sections(sections) |