Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
811644b8 | 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
81447b5b PP |
4 | # |
5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | # of this software and associated documentation files (the "Software"), to deal | |
7 | # in the Software without restriction, including without limitation the rights | |
8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | # copies of the Software, and to permit persons to whom the Software is | |
10 | # furnished to do so, subject to the following conditions: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | # THE SOFTWARE. | |
22 | ||
23 | from bt2 import native_bt, object, utils | |
24 | import collections.abc | |
25 | import bt2.fields | |
26 | import abc | |
27 | import bt2 | |
28 | ||
29 | ||
30 | def _create_from_ptr(ptr): | |
50842bdc | 31 | typeid = native_bt.field_type_get_type_id(ptr) |
81447b5b PP |
32 | return _TYPE_ID_TO_OBJ[typeid]._create_from_ptr(ptr) |
33 | ||
34 | ||
35 | class _FieldType(object._Object, metaclass=abc.ABCMeta): | |
36 | def __init__(self, ptr): | |
37 | super().__init__(ptr) | |
38 | ||
39 | def __eq__(self, other): | |
40 | if not isinstance(other, self.__class__): | |
41 | # not comparing apples to apples | |
42 | return False | |
43 | ||
44 | if self.addr == other.addr: | |
45 | return True | |
46 | ||
50842bdc | 47 | ret = native_bt.field_type_compare(self._ptr, other._ptr) |
81447b5b PP |
48 | utils._handle_ret(ret, "cannot compare field types") |
49 | return ret == 0 | |
50 | ||
51 | def _check_create_status(self, ptr): | |
52 | if ptr is None: | |
53 | raise bt2.CreationError('cannot create {} field type object'.format(self._NAME.lower())) | |
54 | ||
55 | def __copy__(self): | |
50842bdc | 56 | ptr = native_bt.field_type_copy(self._ptr) |
81447b5b PP |
57 | utils._handle_ptr(ptr, 'cannot copy {} field type object'.format(self._NAME.lower())) |
58 | return _create_from_ptr(ptr) | |
59 | ||
60 | def __deepcopy__(self, memo): | |
61 | cpy = self.__copy__() | |
62 | memo[id(self)] = cpy | |
63 | return cpy | |
64 | ||
65 | def __call__(self, value=None): | |
50842bdc | 66 | field_ptr = native_bt.field_create(self._ptr) |
81447b5b PP |
67 | |
68 | if field_ptr is None: | |
69 | raise bt2.CreationError('cannot create {} field object'.format(self._NAME.lower())) | |
70 | ||
71 | field = bt2.fields._create_from_ptr(field_ptr) | |
72 | ||
73 | if value is not None: | |
74 | if not isinstance(field, (bt2.fields._IntegerField, bt2.fields._FloatingPointNumberField, bt2.fields._StringField)): | |
75 | raise bt2.Error('cannot assign an initial value to a {} field object'.format(field._NAME)) | |
76 | ||
77 | field.value = value | |
78 | ||
79 | return field | |
80 | ||
81 | ||
81447b5b PP |
82 | class _AlignmentProp: |
83 | @property | |
84 | def alignment(self): | |
50842bdc | 85 | alignment = native_bt.field_type_get_alignment(self._ptr) |
811644b8 | 86 | assert(alignment >= 0) |
81447b5b PP |
87 | return alignment |
88 | ||
89 | @alignment.setter | |
90 | def alignment(self, alignment): | |
91 | utils._check_alignment(alignment) | |
50842bdc | 92 | ret = native_bt.field_type_set_alignment(self._ptr, alignment) |
81447b5b PP |
93 | utils._handle_ret(ret, "cannot set field type object's alignment") |
94 | ||
95 | ||
96 | class _ByteOrderProp: | |
97 | @property | |
98 | def byte_order(self): | |
50842bdc | 99 | bo = native_bt.field_type_get_byte_order(self._ptr) |
811644b8 | 100 | assert(bo >= 0) |
81447b5b PP |
101 | return bo |
102 | ||
103 | @byte_order.setter | |
104 | def byte_order(self, byte_order): | |
105 | utils._check_int(byte_order) | |
50842bdc | 106 | ret = native_bt.field_type_set_byte_order(self._ptr, byte_order) |
81447b5b PP |
107 | utils._handle_ret(ret, "cannot set field type object's byte order") |
108 | ||
109 | ||
110 | class IntegerFieldType(_FieldType, _AlignmentProp, _ByteOrderProp): | |
111 | _NAME = 'Integer' | |
112 | ||
113 | def __init__(self, size, alignment=None, byte_order=None, is_signed=None, | |
114 | base=None, encoding=None, mapped_clock_class=None): | |
115 | utils._check_uint64(size) | |
116 | ||
117 | if size == 0: | |
118 | raise ValueError('size is 0 bits') | |
119 | ||
50842bdc | 120 | ptr = native_bt.field_type_integer_create(size) |
81447b5b PP |
121 | self._check_create_status(ptr) |
122 | super().__init__(ptr) | |
123 | ||
124 | if alignment is not None: | |
125 | self.alignment = alignment | |
126 | ||
127 | if byte_order is not None: | |
128 | self.byte_order = byte_order | |
129 | ||
130 | if is_signed is not None: | |
131 | self.is_signed = is_signed | |
132 | ||
133 | if base is not None: | |
134 | self.base = base | |
135 | ||
136 | if encoding is not None: | |
137 | self.encoding = encoding | |
138 | ||
139 | if mapped_clock_class is not None: | |
140 | self.mapped_clock_class = mapped_clock_class | |
141 | ||
142 | @property | |
143 | def size(self): | |
50842bdc | 144 | size = native_bt.field_type_integer_get_size(self._ptr) |
811644b8 | 145 | assert(size >= 1) |
81447b5b PP |
146 | return size |
147 | ||
148 | @property | |
149 | def is_signed(self): | |
50842bdc | 150 | is_signed = native_bt.field_type_integer_is_signed(self._ptr) |
811644b8 | 151 | assert(is_signed >= 0) |
81447b5b PP |
152 | return is_signed > 0 |
153 | ||
154 | @is_signed.setter | |
155 | def is_signed(self, is_signed): | |
156 | utils._check_bool(is_signed) | |
50842bdc | 157 | ret = native_bt.field_type_integer_set_is_signed(self._ptr, int(is_signed)) |
81447b5b PP |
158 | utils._handle_ret(ret, "cannot set integer field type object's signedness") |
159 | ||
160 | @property | |
161 | def base(self): | |
50842bdc | 162 | base = native_bt.field_type_integer_get_base(self._ptr) |
811644b8 | 163 | assert(base >= 0) |
81447b5b PP |
164 | return base |
165 | ||
166 | @base.setter | |
167 | def base(self, base): | |
168 | utils._check_int(base) | |
50842bdc | 169 | ret = native_bt.field_type_integer_set_base(self._ptr, base) |
81447b5b PP |
170 | utils._handle_ret(ret, "cannot set integer field type object's base") |
171 | ||
172 | @property | |
173 | def encoding(self): | |
50842bdc | 174 | encoding = native_bt.field_type_integer_get_encoding(self._ptr) |
811644b8 | 175 | assert(encoding >= 0) |
81447b5b PP |
176 | return encoding |
177 | ||
178 | @encoding.setter | |
179 | def encoding(self, encoding): | |
180 | utils._check_int(encoding) | |
50842bdc | 181 | ret = native_bt.field_type_integer_set_encoding(self._ptr, encoding) |
81447b5b PP |
182 | utils._handle_ret(ret, "cannot set integer field type object's encoding") |
183 | ||
184 | @property | |
185 | def mapped_clock_class(self): | |
50842bdc | 186 | ptr = native_bt.field_type_integer_get_mapped_clock_class(self._ptr) |
811644b8 PP |
187 | |
188 | if ptr is None: | |
189 | return | |
190 | ||
81447b5b PP |
191 | return bt2.ClockClass._create_from_ptr(ptr) |
192 | ||
193 | @mapped_clock_class.setter | |
194 | def mapped_clock_class(self, clock_class): | |
195 | utils._check_type(clock_class, bt2.ClockClass) | |
50842bdc | 196 | ret = native_bt.field_type_integer_set_mapped_clock_class(self._ptr, clock_class._ptr) |
81447b5b PP |
197 | utils._handle_ret(ret, "cannot set integer field type object's mapped clock class") |
198 | ||
199 | ||
200 | class FloatingPointNumberFieldType(_FieldType, _AlignmentProp, _ByteOrderProp): | |
201 | _NAME = 'Floating point number' | |
202 | ||
203 | def __init__(self, alignment=None, byte_order=None, exponent_size=None, | |
204 | mantissa_size=None): | |
50842bdc | 205 | ptr = native_bt.field_type_floating_point_create() |
81447b5b PP |
206 | self._check_create_status(ptr) |
207 | super().__init__(ptr) | |
208 | ||
209 | if alignment is not None: | |
210 | self.alignment = alignment | |
211 | ||
212 | if byte_order is not None: | |
213 | self.byte_order = byte_order | |
214 | ||
215 | if exponent_size is not None: | |
216 | self.exponent_size = exponent_size | |
217 | ||
218 | if mantissa_size is not None: | |
219 | self.mantissa_size = mantissa_size | |
220 | ||
221 | @property | |
222 | def exponent_size(self): | |
50842bdc | 223 | exp_size = native_bt.field_type_floating_point_get_exponent_digits(self._ptr) |
811644b8 | 224 | assert(exp_size >= 0) |
81447b5b PP |
225 | return exp_size |
226 | ||
227 | @exponent_size.setter | |
228 | def exponent_size(self, exponent_size): | |
229 | utils._check_uint64(exponent_size) | |
50842bdc | 230 | ret = native_bt.field_type_floating_point_set_exponent_digits(self._ptr, exponent_size) |
81447b5b PP |
231 | utils._handle_ret(ret, "cannot set floating point number field type object's exponent size") |
232 | ||
233 | @property | |
234 | def mantissa_size(self): | |
50842bdc | 235 | mant_size = native_bt.field_type_floating_point_get_mantissa_digits(self._ptr) |
811644b8 PP |
236 | assert(mant_size >= 0) |
237 | return mant_size | |
81447b5b PP |
238 | |
239 | @mantissa_size.setter | |
240 | def mantissa_size(self, mantissa_size): | |
241 | utils._check_uint64(mantissa_size) | |
50842bdc | 242 | ret = native_bt.field_type_floating_point_set_mantissa_digits(self._ptr, mantissa_size) |
81447b5b PP |
243 | utils._handle_ret(ret, "cannot set floating point number field type object's mantissa size") |
244 | ||
245 | ||
246 | class _EnumerationFieldTypeMapping: | |
247 | def __init__(self, name, lower, upper): | |
248 | self._name = name | |
249 | self._lower = lower | |
250 | self._upper = upper | |
251 | ||
252 | @property | |
253 | def name(self): | |
254 | return self._name | |
255 | ||
256 | @property | |
257 | def lower(self): | |
258 | return self._lower | |
259 | ||
260 | @property | |
261 | def upper(self): | |
262 | return self._upper | |
263 | ||
264 | def __eq__(self, other): | |
265 | if type(other) is not self.__class__: | |
266 | return False | |
267 | ||
268 | return (self.name, self.lower, self.upper) == (other.name, other.lower, other.upper) | |
269 | ||
270 | ||
271 | class _EnumerationFieldTypeMappingIterator(object._Object, | |
272 | collections.abc.Iterator): | |
273 | def __init__(self, iter_ptr, is_signed): | |
274 | super().__init__(iter_ptr) | |
275 | self._is_signed = is_signed | |
276 | self._done = (iter_ptr is None) | |
277 | ||
278 | def __next__(self): | |
279 | if self._done: | |
280 | raise StopIteration | |
281 | ||
50842bdc | 282 | ret = native_bt.field_type_enumeration_mapping_iterator_next(self._ptr) |
74fb0452 JG |
283 | if ret < 0: |
284 | self._done = True | |
285 | raise StopIteration | |
286 | ||
81447b5b | 287 | if self._is_signed: |
50842bdc | 288 | ret, name, lower, upper = native_bt.field_type_enumeration_mapping_iterator_get_signed(self._ptr) |
81447b5b | 289 | else: |
50842bdc | 290 | ret, name, lower, upper = native_bt.field_type_enumeration_mapping_iterator_get_unsigned(self._ptr) |
81447b5b | 291 | |
811644b8 | 292 | assert(ret == 0) |
81447b5b | 293 | mapping = _EnumerationFieldTypeMapping(name, lower, upper) |
81447b5b PP |
294 | |
295 | return mapping | |
296 | ||
297 | ||
298 | class EnumerationFieldType(IntegerFieldType, collections.abc.Sequence): | |
299 | _NAME = 'Enumeration' | |
300 | ||
301 | def __init__(self, int_field_type=None, size=None, alignment=None, | |
302 | byte_order=None, is_signed=None, base=None, encoding=None, | |
303 | mapped_clock_class=None): | |
304 | if int_field_type is None: | |
305 | int_field_type = IntegerFieldType(size=size, alignment=alignment, | |
306 | byte_order=byte_order, | |
307 | is_signed=is_signed, base=base, | |
308 | encoding=encoding, | |
309 | mapped_clock_class=mapped_clock_class) | |
310 | ||
311 | utils._check_type(int_field_type, IntegerFieldType) | |
50842bdc | 312 | ptr = native_bt.field_type_enumeration_create(int_field_type._ptr) |
81447b5b PP |
313 | self._check_create_status(ptr) |
314 | _FieldType.__init__(self, ptr) | |
315 | ||
316 | @property | |
317 | def integer_field_type(self): | |
50842bdc | 318 | ptr = native_bt.field_type_enumeration_get_container_type(self._ptr) |
811644b8 | 319 | assert(ptr) |
81447b5b PP |
320 | return _create_from_ptr(ptr) |
321 | ||
322 | @property | |
323 | def size(self): | |
324 | return self.integer_field_type.size | |
325 | ||
326 | @property | |
327 | def alignment(self): | |
328 | return self.integer_field_type.alignment | |
329 | ||
330 | @alignment.setter | |
331 | def alignment(self, alignment): | |
332 | self.integer_field_type.alignment = alignment | |
333 | ||
334 | @property | |
335 | def byte_order(self): | |
336 | return self.integer_field_type.byte_order | |
337 | ||
338 | @byte_order.setter | |
339 | def byte_order(self, byte_order): | |
340 | self.integer_field_type.byte_order = byte_order | |
341 | ||
342 | @property | |
343 | def is_signed(self): | |
344 | return self.integer_field_type.is_signed | |
345 | ||
346 | @is_signed.setter | |
347 | def is_signed(self, is_signed): | |
348 | self.integer_field_type.is_signed = is_signed | |
349 | ||
350 | @property | |
351 | def base(self): | |
352 | return self.integer_field_type.base | |
353 | ||
354 | @base.setter | |
355 | def base(self, base): | |
356 | self.integer_field_type.base = base | |
357 | ||
358 | @property | |
359 | def encoding(self): | |
360 | return self.integer_field_type.encoding | |
361 | ||
362 | @encoding.setter | |
363 | def encoding(self, encoding): | |
364 | self.integer_field_type.encoding = encoding | |
365 | ||
366 | @property | |
367 | def mapped_clock_class(self): | |
368 | return self.integer_field_type.mapped_clock_class | |
369 | ||
370 | @mapped_clock_class.setter | |
371 | def mapped_clock_class(self, mapped_clock_class): | |
372 | self.integer_field_type.mapped_clock_class = mapped_clock_class | |
373 | ||
374 | def __len__(self): | |
50842bdc | 375 | count = native_bt.field_type_enumeration_get_mapping_count(self._ptr) |
811644b8 | 376 | assert(count >= 0) |
81447b5b PP |
377 | return count |
378 | ||
379 | def __getitem__(self, index): | |
380 | utils._check_uint64(index) | |
381 | ||
382 | if index >= len(self): | |
383 | raise IndexError | |
384 | ||
385 | if self.is_signed: | |
50842bdc | 386 | get_fn = native_bt.field_type_enumeration_get_mapping_signed |
81447b5b | 387 | else: |
50842bdc | 388 | get_fn = native_bt.field_type_enumeration_get_mapping_unsigned |
81447b5b PP |
389 | |
390 | ret, name, lower, upper = get_fn(self._ptr, index) | |
811644b8 | 391 | assert(ret == 0) |
81447b5b PP |
392 | return _EnumerationFieldTypeMapping(name, lower, upper) |
393 | ||
394 | def _get_mapping_iter(self, iter_ptr): | |
395 | return _EnumerationFieldTypeMappingIterator(iter_ptr, self.is_signed) | |
396 | ||
397 | def mappings_by_name(self, name): | |
398 | utils._check_str(name) | |
50842bdc | 399 | iter_ptr = native_bt.field_type_enumeration_find_mappings_by_name(self._ptr, name) |
74fb0452 | 400 | print('iter_ptr', iter_ptr) |
81447b5b PP |
401 | return self._get_mapping_iter(iter_ptr) |
402 | ||
403 | def mappings_by_value(self, value): | |
404 | if self.is_signed: | |
405 | utils._check_int64(value) | |
50842bdc | 406 | iter_ptr = native_bt.field_type_enumeration_find_mappings_by_signed_value(self._ptr, value) |
81447b5b PP |
407 | else: |
408 | utils._check_uint64(value) | |
50842bdc | 409 | iter_ptr = native_bt.field_type_enumeration_find_mappings_by_unsigned_value(self._ptr, value) |
81447b5b PP |
410 | |
411 | return self._get_mapping_iter(iter_ptr) | |
412 | ||
e4d04867 | 413 | def add_mapping(self, name, lower, upper=None): |
81447b5b PP |
414 | utils._check_str(name) |
415 | ||
416 | if upper is None: | |
417 | upper = lower | |
418 | ||
419 | if self.is_signed: | |
50842bdc | 420 | add_fn = native_bt.field_type_enumeration_add_mapping_signed |
81447b5b PP |
421 | utils._check_int64(lower) |
422 | utils._check_int64(upper) | |
423 | else: | |
50842bdc | 424 | add_fn = native_bt.field_type_enumeration_add_mapping_unsigned |
81447b5b PP |
425 | utils._check_uint64(lower) |
426 | utils._check_uint64(upper) | |
427 | ||
428 | ret = add_fn(self._ptr, name, lower, upper) | |
429 | utils._handle_ret(ret, "cannot add mapping to enumeration field type object") | |
430 | ||
431 | def __iadd__(self, mappings): | |
432 | for mapping in mappings: | |
e4d04867 | 433 | self.add_mapping(mapping.name, mapping.lower, mapping.upper) |
81447b5b PP |
434 | |
435 | return self | |
436 | ||
437 | ||
438 | class StringFieldType(_FieldType): | |
439 | _NAME = 'String' | |
440 | ||
441 | def __init__(self, encoding=None): | |
50842bdc | 442 | ptr = native_bt.field_type_string_create() |
81447b5b PP |
443 | self._check_create_status(ptr) |
444 | super().__init__(ptr) | |
445 | ||
446 | if encoding is not None: | |
447 | self.encoding = encoding | |
448 | ||
449 | @property | |
450 | def encoding(self): | |
50842bdc | 451 | encoding = native_bt.field_type_string_get_encoding(self._ptr) |
811644b8 | 452 | assert(encoding >= 0) |
81447b5b PP |
453 | return encoding |
454 | ||
455 | @encoding.setter | |
456 | def encoding(self, encoding): | |
457 | utils._check_int(encoding) | |
50842bdc | 458 | ret = native_bt.field_type_string_set_encoding(self._ptr, encoding) |
81447b5b PP |
459 | utils._handle_ret(ret, "cannot set string field type object's encoding") |
460 | ||
461 | ||
462 | class _FieldContainer(collections.abc.Mapping): | |
463 | def __len__(self): | |
464 | count = self._count() | |
811644b8 | 465 | assert(count >= 0) |
81447b5b PP |
466 | return count |
467 | ||
468 | def __getitem__(self, key): | |
469 | if not isinstance(key, str): | |
470 | raise TypeError("'{}' is not a 'str' object".format(key.__class__.__name__)) | |
471 | ||
472 | ptr = self._get_field_by_name(key) | |
473 | ||
474 | if ptr is None: | |
475 | raise KeyError(key) | |
476 | ||
477 | return _create_from_ptr(ptr) | |
478 | ||
479 | def __iter__(self): | |
480 | return self._ITER_CLS(self) | |
481 | ||
482 | def append_field(self, name, field_type): | |
483 | utils._check_str(name) | |
484 | utils._check_type(field_type, _FieldType) | |
485 | ret = self._add_field(field_type._ptr, name) | |
486 | utils._handle_ret(ret, "cannot add field to {} field type object".format(self._NAME.lower())) | |
487 | ||
488 | def __iadd__(self, fields): | |
489 | for name, field_type in fields.items(): | |
490 | self.append_field(name, field_type) | |
491 | ||
492 | return self | |
493 | ||
494 | def at_index(self, index): | |
495 | utils._check_uint64(index) | |
496 | return self._at(index) | |
497 | ||
498 | ||
499 | class _StructureFieldTypeFieldIterator(collections.abc.Iterator): | |
500 | def __init__(self, struct_field_type): | |
501 | self._struct_field_type = struct_field_type | |
502 | self._at = 0 | |
503 | ||
504 | def __next__(self): | |
505 | if self._at == len(self._struct_field_type): | |
506 | raise StopIteration | |
507 | ||
50842bdc | 508 | get_ft_by_index = native_bt.field_type_structure_get_field_by_index |
811644b8 PP |
509 | ret, name, field_type_ptr = get_ft_by_index(self._struct_field_type._ptr, |
510 | self._at) | |
511 | assert(ret == 0) | |
81447b5b PP |
512 | native_bt.put(field_type_ptr) |
513 | self._at += 1 | |
514 | return name | |
515 | ||
516 | ||
517 | class StructureFieldType(_FieldType, _FieldContainer, _AlignmentProp): | |
518 | _NAME = 'Structure' | |
519 | _ITER_CLS = _StructureFieldTypeFieldIterator | |
520 | ||
521 | def __init__(self, min_alignment=None): | |
50842bdc | 522 | ptr = native_bt.field_type_structure_create() |
81447b5b PP |
523 | self._check_create_status(ptr) |
524 | super().__init__(ptr) | |
525 | ||
526 | if min_alignment is not None: | |
527 | self.min_alignment = min_alignment | |
528 | ||
529 | def _count(self): | |
50842bdc | 530 | return native_bt.field_type_structure_get_field_count(self._ptr) |
81447b5b PP |
531 | |
532 | def _get_field_by_name(self, key): | |
50842bdc | 533 | return native_bt.field_type_structure_get_field_type_by_name(self._ptr, key) |
81447b5b PP |
534 | |
535 | def _add_field(self, ptr, name): | |
50842bdc | 536 | return native_bt.field_type_structure_add_field(self._ptr, ptr, |
0b03f63e | 537 | name) |
81447b5b PP |
538 | |
539 | def _at(self, index): | |
811644b8 PP |
540 | if index < 0 or index >= len(self): |
541 | raise IndexError | |
542 | ||
50842bdc | 543 | ret, name, field_type_ptr = native_bt.field_type_structure_get_field_by_index(self._ptr, index) |
811644b8 | 544 | assert(ret == 0) |
81447b5b PP |
545 | return _create_from_ptr(field_type_ptr) |
546 | ||
547 | ||
548 | StructureFieldType.min_alignment = property(fset=StructureFieldType.alignment.fset) | |
549 | StructureFieldType.alignment = property(fget=StructureFieldType.alignment.fget) | |
550 | ||
551 | ||
552 | class _VariantFieldTypeFieldIterator(collections.abc.Iterator): | |
553 | def __init__(self, variant_field_type): | |
554 | self._variant_field_type = variant_field_type | |
555 | self._at = 0 | |
556 | ||
557 | def __next__(self): | |
558 | if self._at == len(self._variant_field_type): | |
559 | raise StopIteration | |
560 | ||
50842bdc | 561 | ret, name, field_type_ptr = native_bt.field_type_variant_get_field_by_index(self._variant_field_type._ptr, |
0b03f63e | 562 | self._at) |
811644b8 | 563 | assert(ret == 0) |
81447b5b PP |
564 | native_bt.put(field_type_ptr) |
565 | self._at += 1 | |
566 | return name | |
567 | ||
568 | ||
569 | class VariantFieldType(_FieldType, _FieldContainer, _AlignmentProp): | |
570 | _NAME = 'Variant' | |
571 | _ITER_CLS = _VariantFieldTypeFieldIterator | |
572 | ||
811644b8 | 573 | def __init__(self, tag_name, tag_field_type=None): |
81447b5b | 574 | utils._check_str(tag_name) |
811644b8 PP |
575 | |
576 | if tag_field_type is None: | |
577 | tag_ft_ptr = None | |
578 | else: | |
579 | utils._check_type(tag_field_type, EnumerationFieldType) | |
580 | tag_ft_ptr = tag_field_type._ptr | |
581 | ||
50842bdc | 582 | ptr = native_bt.field_type_variant_create(tag_ft_ptr, |
0b03f63e | 583 | tag_name) |
81447b5b PP |
584 | self._check_create_status(ptr) |
585 | super().__init__(ptr) | |
586 | ||
587 | @property | |
588 | def tag_name(self): | |
50842bdc | 589 | tag_name = native_bt.field_type_variant_get_tag_name(self._ptr) |
811644b8 | 590 | assert(tag_name is not None) |
81447b5b PP |
591 | return tag_name |
592 | ||
593 | @tag_name.setter | |
594 | def tag_name(self, tag_name): | |
595 | utils._check_str(tag_name) | |
50842bdc | 596 | ret = native_bt.field_type_variant_set_tag_name(self._ptr, tag_name) |
81447b5b PP |
597 | utils._handle_ret(ret, "cannot set variant field type object's tag name") |
598 | ||
811644b8 PP |
599 | @property |
600 | def tag_field_type(self): | |
50842bdc | 601 | ft_ptr = native_bt.field_type_variant_get_tag_type(self._ptr) |
811644b8 PP |
602 | |
603 | if ft_ptr is None: | |
604 | return | |
605 | ||
606 | return _create_from_ptr(ft_ptr) | |
607 | ||
81447b5b | 608 | def _count(self): |
50842bdc | 609 | return native_bt.field_type_variant_get_field_count(self._ptr) |
81447b5b PP |
610 | |
611 | def _get_field_by_name(self, key): | |
50842bdc | 612 | return native_bt.field_type_variant_get_field_type_by_name(self._ptr, key) |
81447b5b PP |
613 | |
614 | def _add_field(self, ptr, name): | |
50842bdc | 615 | return native_bt.field_type_variant_add_field(self._ptr, ptr, name) |
81447b5b PP |
616 | |
617 | def _at(self, index): | |
811644b8 PP |
618 | if index < 0 or index >= len(self): |
619 | raise IndexError | |
620 | ||
50842bdc | 621 | ret, name, field_type_ptr = native_bt.field_type_variant_get_field_by_index(self._ptr, index) |
811644b8 | 622 | assert(ret == 0) |
81447b5b PP |
623 | return _create_from_ptr(field_type_ptr) |
624 | ||
625 | ||
626 | class ArrayFieldType(_FieldType): | |
627 | _NAME = 'Array' | |
628 | ||
629 | def __init__(self, element_field_type, length): | |
630 | utils._check_type(element_field_type, _FieldType) | |
631 | utils._check_uint64(length) | |
50842bdc | 632 | ptr = native_bt.field_type_array_create(element_field_type._ptr, length) |
81447b5b PP |
633 | self._check_create_status(ptr) |
634 | super().__init__(ptr) | |
635 | ||
636 | @property | |
637 | def length(self): | |
50842bdc | 638 | length = native_bt.field_type_array_get_length(self._ptr) |
811644b8 | 639 | assert(length >= 0) |
81447b5b PP |
640 | return length |
641 | ||
642 | @property | |
643 | def element_field_type(self): | |
50842bdc | 644 | ptr = native_bt.field_type_array_get_element_type(self._ptr) |
811644b8 | 645 | assert(ptr) |
81447b5b PP |
646 | return _create_from_ptr(ptr) |
647 | ||
648 | ||
649 | class SequenceFieldType(_FieldType): | |
650 | _NAME = 'Sequence' | |
651 | ||
652 | def __init__(self, element_field_type, length_name): | |
653 | utils._check_type(element_field_type, _FieldType) | |
654 | utils._check_str(length_name) | |
50842bdc | 655 | ptr = native_bt.field_type_sequence_create(element_field_type._ptr, |
0b03f63e | 656 | length_name) |
81447b5b PP |
657 | self._check_create_status(ptr) |
658 | super().__init__(ptr) | |
659 | ||
660 | @property | |
661 | def length_name(self): | |
50842bdc | 662 | length_name = native_bt.field_type_sequence_get_length_field_name(self._ptr) |
811644b8 | 663 | assert(length_name is not None) |
81447b5b PP |
664 | return length_name |
665 | ||
666 | @property | |
667 | def element_field_type(self): | |
50842bdc | 668 | ptr = native_bt.field_type_sequence_get_element_type(self._ptr) |
811644b8 | 669 | assert(ptr) |
81447b5b PP |
670 | return _create_from_ptr(ptr) |
671 | ||
672 | ||
673 | _TYPE_ID_TO_OBJ = { | |
81447b5b | 674 | } |