Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
f6a5e476 | 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): | |
839d52a5 | 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 | ||
839d52a5 | 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): | |
839d52a5 | 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): | |
839d52a5 | 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 | ||
82 | class ByteOrder: | |
839d52a5 PP |
83 | NATIVE = native_bt.BYTE_ORDER_NATIVE |
84 | LITTLE_ENDIAN = native_bt.BYTE_ORDER_LITTLE_ENDIAN | |
85 | BIG_ENDIAN = native_bt.BYTE_ORDER_BIG_ENDIAN | |
86 | NETWORK = native_bt.BYTE_ORDER_NETWORK | |
81447b5b PP |
87 | |
88 | ||
89 | class Encoding: | |
839d52a5 PP |
90 | NONE = native_bt.STRING_ENCODING_NONE |
91 | UTF8 = native_bt.STRING_ENCODING_UTF8 | |
92 | ASCII = native_bt.STRING_ENCODING_ASCII | |
81447b5b PP |
93 | |
94 | ||
95 | class Base: | |
839d52a5 PP |
96 | BINARY = native_bt.INTEGER_BASE_BINARY |
97 | OCTAL = native_bt.INTEGER_BASE_OCTAL | |
98 | DECIMAL = native_bt.INTEGER_BASE_DECIMAL | |
99 | HEXADECIMAL = native_bt.INTEGER_BASE_HEXADECIMAL | |
81447b5b PP |
100 | |
101 | ||
102 | class _AlignmentProp: | |
103 | @property | |
104 | def alignment(self): | |
839d52a5 | 105 | alignment = native_bt.field_type_get_alignment(self._ptr) |
f6a5e476 | 106 | assert(alignment >= 0) |
81447b5b PP |
107 | return alignment |
108 | ||
109 | @alignment.setter | |
110 | def alignment(self, alignment): | |
111 | utils._check_alignment(alignment) | |
839d52a5 | 112 | ret = native_bt.field_type_set_alignment(self._ptr, alignment) |
81447b5b PP |
113 | utils._handle_ret(ret, "cannot set field type object's alignment") |
114 | ||
115 | ||
116 | class _ByteOrderProp: | |
117 | @property | |
118 | def byte_order(self): | |
839d52a5 | 119 | bo = native_bt.field_type_get_byte_order(self._ptr) |
f6a5e476 | 120 | assert(bo >= 0) |
81447b5b PP |
121 | return bo |
122 | ||
123 | @byte_order.setter | |
124 | def byte_order(self, byte_order): | |
125 | utils._check_int(byte_order) | |
839d52a5 | 126 | ret = native_bt.field_type_set_byte_order(self._ptr, byte_order) |
81447b5b PP |
127 | utils._handle_ret(ret, "cannot set field type object's byte order") |
128 | ||
129 | ||
130 | class IntegerFieldType(_FieldType, _AlignmentProp, _ByteOrderProp): | |
131 | _NAME = 'Integer' | |
132 | ||
133 | def __init__(self, size, alignment=None, byte_order=None, is_signed=None, | |
134 | base=None, encoding=None, mapped_clock_class=None): | |
135 | utils._check_uint64(size) | |
136 | ||
137 | if size == 0: | |
138 | raise ValueError('size is 0 bits') | |
139 | ||
839d52a5 | 140 | ptr = native_bt.field_type_integer_create(size) |
81447b5b PP |
141 | self._check_create_status(ptr) |
142 | super().__init__(ptr) | |
143 | ||
144 | if alignment is not None: | |
145 | self.alignment = alignment | |
146 | ||
147 | if byte_order is not None: | |
148 | self.byte_order = byte_order | |
149 | ||
150 | if is_signed is not None: | |
151 | self.is_signed = is_signed | |
152 | ||
153 | if base is not None: | |
154 | self.base = base | |
155 | ||
156 | if encoding is not None: | |
157 | self.encoding = encoding | |
158 | ||
159 | if mapped_clock_class is not None: | |
160 | self.mapped_clock_class = mapped_clock_class | |
161 | ||
162 | @property | |
163 | def size(self): | |
839d52a5 | 164 | size = native_bt.field_type_integer_get_size(self._ptr) |
f6a5e476 | 165 | assert(size >= 1) |
81447b5b PP |
166 | return size |
167 | ||
168 | @property | |
169 | def is_signed(self): | |
839d52a5 | 170 | is_signed = native_bt.field_type_integer_is_signed(self._ptr) |
f6a5e476 | 171 | assert(is_signed >= 0) |
81447b5b PP |
172 | return is_signed > 0 |
173 | ||
174 | @is_signed.setter | |
175 | def is_signed(self, is_signed): | |
176 | utils._check_bool(is_signed) | |
839d52a5 | 177 | ret = native_bt.field_type_integer_set_is_signed(self._ptr, int(is_signed)) |
81447b5b PP |
178 | utils._handle_ret(ret, "cannot set integer field type object's signedness") |
179 | ||
180 | @property | |
181 | def base(self): | |
839d52a5 | 182 | base = native_bt.field_type_integer_get_base(self._ptr) |
f6a5e476 | 183 | assert(base >= 0) |
81447b5b PP |
184 | return base |
185 | ||
186 | @base.setter | |
187 | def base(self, base): | |
188 | utils._check_int(base) | |
839d52a5 | 189 | ret = native_bt.field_type_integer_set_base(self._ptr, base) |
81447b5b PP |
190 | utils._handle_ret(ret, "cannot set integer field type object's base") |
191 | ||
192 | @property | |
193 | def encoding(self): | |
839d52a5 | 194 | encoding = native_bt.field_type_integer_get_encoding(self._ptr) |
f6a5e476 | 195 | assert(encoding >= 0) |
81447b5b PP |
196 | return encoding |
197 | ||
198 | @encoding.setter | |
199 | def encoding(self, encoding): | |
200 | utils._check_int(encoding) | |
839d52a5 | 201 | ret = native_bt.field_type_integer_set_encoding(self._ptr, encoding) |
81447b5b PP |
202 | utils._handle_ret(ret, "cannot set integer field type object's encoding") |
203 | ||
204 | @property | |
205 | def mapped_clock_class(self): | |
839d52a5 | 206 | ptr = native_bt.field_type_integer_get_mapped_clock_class(self._ptr) |
f6a5e476 PP |
207 | |
208 | if ptr is None: | |
209 | return | |
210 | ||
81447b5b PP |
211 | return bt2.ClockClass._create_from_ptr(ptr) |
212 | ||
213 | @mapped_clock_class.setter | |
214 | def mapped_clock_class(self, clock_class): | |
215 | utils._check_type(clock_class, bt2.ClockClass) | |
839d52a5 | 216 | ret = native_bt.field_type_integer_set_mapped_clock_class(self._ptr, clock_class._ptr) |
81447b5b PP |
217 | utils._handle_ret(ret, "cannot set integer field type object's mapped clock class") |
218 | ||
219 | ||
220 | class FloatingPointNumberFieldType(_FieldType, _AlignmentProp, _ByteOrderProp): | |
221 | _NAME = 'Floating point number' | |
222 | ||
223 | def __init__(self, alignment=None, byte_order=None, exponent_size=None, | |
224 | mantissa_size=None): | |
839d52a5 | 225 | ptr = native_bt.field_type_floating_point_create() |
81447b5b PP |
226 | self._check_create_status(ptr) |
227 | super().__init__(ptr) | |
228 | ||
229 | if alignment is not None: | |
230 | self.alignment = alignment | |
231 | ||
232 | if byte_order is not None: | |
233 | self.byte_order = byte_order | |
234 | ||
235 | if exponent_size is not None: | |
236 | self.exponent_size = exponent_size | |
237 | ||
238 | if mantissa_size is not None: | |
239 | self.mantissa_size = mantissa_size | |
240 | ||
241 | @property | |
242 | def exponent_size(self): | |
839d52a5 | 243 | exp_size = native_bt.field_type_floating_point_get_exponent_digits(self._ptr) |
f6a5e476 | 244 | assert(exp_size >= 0) |
81447b5b PP |
245 | return exp_size |
246 | ||
247 | @exponent_size.setter | |
248 | def exponent_size(self, exponent_size): | |
249 | utils._check_uint64(exponent_size) | |
839d52a5 | 250 | ret = native_bt.field_type_floating_point_set_exponent_digits(self._ptr, exponent_size) |
81447b5b PP |
251 | utils._handle_ret(ret, "cannot set floating point number field type object's exponent size") |
252 | ||
253 | @property | |
254 | def mantissa_size(self): | |
839d52a5 | 255 | mant_size = native_bt.field_type_floating_point_get_mantissa_digits(self._ptr) |
f6a5e476 PP |
256 | assert(mant_size >= 0) |
257 | return mant_size | |
81447b5b PP |
258 | |
259 | @mantissa_size.setter | |
260 | def mantissa_size(self, mantissa_size): | |
261 | utils._check_uint64(mantissa_size) | |
839d52a5 | 262 | ret = native_bt.field_type_floating_point_set_mantissa_digits(self._ptr, mantissa_size) |
81447b5b PP |
263 | utils._handle_ret(ret, "cannot set floating point number field type object's mantissa size") |
264 | ||
265 | ||
266 | class _EnumerationFieldTypeMapping: | |
267 | def __init__(self, name, lower, upper): | |
268 | self._name = name | |
269 | self._lower = lower | |
270 | self._upper = upper | |
271 | ||
272 | @property | |
273 | def name(self): | |
274 | return self._name | |
275 | ||
276 | @property | |
277 | def lower(self): | |
278 | return self._lower | |
279 | ||
280 | @property | |
281 | def upper(self): | |
282 | return self._upper | |
283 | ||
284 | def __eq__(self, other): | |
285 | if type(other) is not self.__class__: | |
286 | return False | |
287 | ||
288 | return (self.name, self.lower, self.upper) == (other.name, other.lower, other.upper) | |
289 | ||
290 | ||
291 | class _EnumerationFieldTypeMappingIterator(object._Object, | |
292 | collections.abc.Iterator): | |
293 | def __init__(self, iter_ptr, is_signed): | |
294 | super().__init__(iter_ptr) | |
295 | self._is_signed = is_signed | |
296 | self._done = (iter_ptr is None) | |
297 | ||
298 | def __next__(self): | |
299 | if self._done: | |
300 | raise StopIteration | |
301 | ||
839d52a5 | 302 | ret = native_bt.field_type_enumeration_mapping_iterator_next(self._ptr) |
985100c1 JG |
303 | if ret < 0: |
304 | self._done = True | |
305 | raise StopIteration | |
306 | ||
81447b5b | 307 | if self._is_signed: |
839d52a5 | 308 | ret, name, lower, upper = native_bt.field_type_enumeration_mapping_iterator_get_signed(self._ptr) |
81447b5b | 309 | else: |
839d52a5 | 310 | ret, name, lower, upper = native_bt.field_type_enumeration_mapping_iterator_get_unsigned(self._ptr) |
81447b5b | 311 | |
f6a5e476 | 312 | assert(ret == 0) |
81447b5b | 313 | mapping = _EnumerationFieldTypeMapping(name, lower, upper) |
81447b5b PP |
314 | |
315 | return mapping | |
316 | ||
317 | ||
318 | class EnumerationFieldType(IntegerFieldType, collections.abc.Sequence): | |
319 | _NAME = 'Enumeration' | |
320 | ||
321 | def __init__(self, int_field_type=None, size=None, alignment=None, | |
322 | byte_order=None, is_signed=None, base=None, encoding=None, | |
323 | mapped_clock_class=None): | |
324 | if int_field_type is None: | |
325 | int_field_type = IntegerFieldType(size=size, alignment=alignment, | |
326 | byte_order=byte_order, | |
327 | is_signed=is_signed, base=base, | |
328 | encoding=encoding, | |
329 | mapped_clock_class=mapped_clock_class) | |
330 | ||
331 | utils._check_type(int_field_type, IntegerFieldType) | |
839d52a5 | 332 | ptr = native_bt.field_type_enumeration_create(int_field_type._ptr) |
81447b5b PP |
333 | self._check_create_status(ptr) |
334 | _FieldType.__init__(self, ptr) | |
335 | ||
336 | @property | |
337 | def integer_field_type(self): | |
839d52a5 | 338 | ptr = native_bt.field_type_enumeration_get_container_type(self._ptr) |
f6a5e476 | 339 | assert(ptr) |
81447b5b PP |
340 | return _create_from_ptr(ptr) |
341 | ||
342 | @property | |
343 | def size(self): | |
344 | return self.integer_field_type.size | |
345 | ||
346 | @property | |
347 | def alignment(self): | |
348 | return self.integer_field_type.alignment | |
349 | ||
350 | @alignment.setter | |
351 | def alignment(self, alignment): | |
352 | self.integer_field_type.alignment = alignment | |
353 | ||
354 | @property | |
355 | def byte_order(self): | |
356 | return self.integer_field_type.byte_order | |
357 | ||
358 | @byte_order.setter | |
359 | def byte_order(self, byte_order): | |
360 | self.integer_field_type.byte_order = byte_order | |
361 | ||
362 | @property | |
363 | def is_signed(self): | |
364 | return self.integer_field_type.is_signed | |
365 | ||
366 | @is_signed.setter | |
367 | def is_signed(self, is_signed): | |
368 | self.integer_field_type.is_signed = is_signed | |
369 | ||
370 | @property | |
371 | def base(self): | |
372 | return self.integer_field_type.base | |
373 | ||
374 | @base.setter | |
375 | def base(self, base): | |
376 | self.integer_field_type.base = base | |
377 | ||
378 | @property | |
379 | def encoding(self): | |
380 | return self.integer_field_type.encoding | |
381 | ||
382 | @encoding.setter | |
383 | def encoding(self, encoding): | |
384 | self.integer_field_type.encoding = encoding | |
385 | ||
386 | @property | |
387 | def mapped_clock_class(self): | |
388 | return self.integer_field_type.mapped_clock_class | |
389 | ||
390 | @mapped_clock_class.setter | |
391 | def mapped_clock_class(self, mapped_clock_class): | |
392 | self.integer_field_type.mapped_clock_class = mapped_clock_class | |
393 | ||
394 | def __len__(self): | |
839d52a5 | 395 | count = native_bt.field_type_enumeration_get_mapping_count(self._ptr) |
f6a5e476 | 396 | assert(count >= 0) |
81447b5b PP |
397 | return count |
398 | ||
399 | def __getitem__(self, index): | |
400 | utils._check_uint64(index) | |
401 | ||
402 | if index >= len(self): | |
403 | raise IndexError | |
404 | ||
405 | if self.is_signed: | |
839d52a5 | 406 | get_fn = native_bt.field_type_enumeration_get_mapping_signed |
81447b5b | 407 | else: |
839d52a5 | 408 | get_fn = native_bt.field_type_enumeration_get_mapping_unsigned |
81447b5b PP |
409 | |
410 | ret, name, lower, upper = get_fn(self._ptr, index) | |
f6a5e476 | 411 | assert(ret == 0) |
81447b5b PP |
412 | return _EnumerationFieldTypeMapping(name, lower, upper) |
413 | ||
414 | def _get_mapping_iter(self, iter_ptr): | |
415 | return _EnumerationFieldTypeMappingIterator(iter_ptr, self.is_signed) | |
416 | ||
417 | def mappings_by_name(self, name): | |
418 | utils._check_str(name) | |
839d52a5 | 419 | iter_ptr = native_bt.field_type_enumeration_find_mappings_by_name(self._ptr, name) |
985100c1 | 420 | print('iter_ptr', iter_ptr) |
81447b5b PP |
421 | return self._get_mapping_iter(iter_ptr) |
422 | ||
423 | def mappings_by_value(self, value): | |
424 | if self.is_signed: | |
425 | utils._check_int64(value) | |
839d52a5 | 426 | iter_ptr = native_bt.field_type_enumeration_find_mappings_by_signed_value(self._ptr, value) |
81447b5b PP |
427 | else: |
428 | utils._check_uint64(value) | |
839d52a5 | 429 | iter_ptr = native_bt.field_type_enumeration_find_mappings_by_unsigned_value(self._ptr, value) |
81447b5b PP |
430 | |
431 | return self._get_mapping_iter(iter_ptr) | |
432 | ||
433 | def append_mapping(self, name, lower, upper=None): | |
434 | utils._check_str(name) | |
435 | ||
436 | if upper is None: | |
437 | upper = lower | |
438 | ||
439 | if self.is_signed: | |
839d52a5 | 440 | add_fn = native_bt.field_type_enumeration_add_mapping_signed |
81447b5b PP |
441 | utils._check_int64(lower) |
442 | utils._check_int64(upper) | |
443 | else: | |
839d52a5 | 444 | add_fn = native_bt.field_type_enumeration_add_mapping_unsigned |
81447b5b PP |
445 | utils._check_uint64(lower) |
446 | utils._check_uint64(upper) | |
447 | ||
448 | ret = add_fn(self._ptr, name, lower, upper) | |
449 | utils._handle_ret(ret, "cannot add mapping to enumeration field type object") | |
450 | ||
451 | def __iadd__(self, mappings): | |
452 | for mapping in mappings: | |
453 | self.append_mapping(mapping.name, mapping.lower, mapping.upper) | |
454 | ||
455 | return self | |
456 | ||
457 | ||
458 | class StringFieldType(_FieldType): | |
459 | _NAME = 'String' | |
460 | ||
461 | def __init__(self, encoding=None): | |
839d52a5 | 462 | ptr = native_bt.field_type_string_create() |
81447b5b PP |
463 | self._check_create_status(ptr) |
464 | super().__init__(ptr) | |
465 | ||
466 | if encoding is not None: | |
467 | self.encoding = encoding | |
468 | ||
469 | @property | |
470 | def encoding(self): | |
839d52a5 | 471 | encoding = native_bt.field_type_string_get_encoding(self._ptr) |
f6a5e476 | 472 | assert(encoding >= 0) |
81447b5b PP |
473 | return encoding |
474 | ||
475 | @encoding.setter | |
476 | def encoding(self, encoding): | |
477 | utils._check_int(encoding) | |
839d52a5 | 478 | ret = native_bt.field_type_string_set_encoding(self._ptr, encoding) |
81447b5b PP |
479 | utils._handle_ret(ret, "cannot set string field type object's encoding") |
480 | ||
481 | ||
482 | class _FieldContainer(collections.abc.Mapping): | |
483 | def __len__(self): | |
484 | count = self._count() | |
f6a5e476 | 485 | assert(count >= 0) |
81447b5b PP |
486 | return count |
487 | ||
488 | def __getitem__(self, key): | |
489 | if not isinstance(key, str): | |
490 | raise TypeError("'{}' is not a 'str' object".format(key.__class__.__name__)) | |
491 | ||
492 | ptr = self._get_field_by_name(key) | |
493 | ||
494 | if ptr is None: | |
495 | raise KeyError(key) | |
496 | ||
497 | return _create_from_ptr(ptr) | |
498 | ||
499 | def __iter__(self): | |
500 | return self._ITER_CLS(self) | |
501 | ||
502 | def append_field(self, name, field_type): | |
503 | utils._check_str(name) | |
504 | utils._check_type(field_type, _FieldType) | |
505 | ret = self._add_field(field_type._ptr, name) | |
506 | utils._handle_ret(ret, "cannot add field to {} field type object".format(self._NAME.lower())) | |
507 | ||
508 | def __iadd__(self, fields): | |
509 | for name, field_type in fields.items(): | |
510 | self.append_field(name, field_type) | |
511 | ||
512 | return self | |
513 | ||
514 | def at_index(self, index): | |
515 | utils._check_uint64(index) | |
516 | return self._at(index) | |
517 | ||
518 | ||
519 | class _StructureFieldTypeFieldIterator(collections.abc.Iterator): | |
520 | def __init__(self, struct_field_type): | |
521 | self._struct_field_type = struct_field_type | |
522 | self._at = 0 | |
523 | ||
524 | def __next__(self): | |
525 | if self._at == len(self._struct_field_type): | |
526 | raise StopIteration | |
527 | ||
839d52a5 | 528 | get_ft_by_index = native_bt.field_type_structure_get_field_by_index |
f6a5e476 PP |
529 | ret, name, field_type_ptr = get_ft_by_index(self._struct_field_type._ptr, |
530 | self._at) | |
531 | assert(ret == 0) | |
81447b5b PP |
532 | native_bt.put(field_type_ptr) |
533 | self._at += 1 | |
534 | return name | |
535 | ||
536 | ||
537 | class StructureFieldType(_FieldType, _FieldContainer, _AlignmentProp): | |
538 | _NAME = 'Structure' | |
539 | _ITER_CLS = _StructureFieldTypeFieldIterator | |
540 | ||
541 | def __init__(self, min_alignment=None): | |
839d52a5 | 542 | ptr = native_bt.field_type_structure_create() |
81447b5b PP |
543 | self._check_create_status(ptr) |
544 | super().__init__(ptr) | |
545 | ||
546 | if min_alignment is not None: | |
547 | self.min_alignment = min_alignment | |
548 | ||
549 | def _count(self): | |
839d52a5 | 550 | return native_bt.field_type_structure_get_field_count(self._ptr) |
81447b5b PP |
551 | |
552 | def _get_field_by_name(self, key): | |
839d52a5 | 553 | return native_bt.field_type_structure_get_field_type_by_name(self._ptr, key) |
81447b5b PP |
554 | |
555 | def _add_field(self, ptr, name): | |
839d52a5 | 556 | return native_bt.field_type_structure_add_field(self._ptr, ptr, |
81447b5b PP |
557 | name) |
558 | ||
559 | def _at(self, index): | |
f6a5e476 PP |
560 | if index < 0 or index >= len(self): |
561 | raise IndexError | |
562 | ||
839d52a5 | 563 | ret, name, field_type_ptr = native_bt.field_type_structure_get_field_by_index(self._ptr, index) |
f6a5e476 | 564 | assert(ret == 0) |
81447b5b PP |
565 | return _create_from_ptr(field_type_ptr) |
566 | ||
567 | ||
568 | StructureFieldType.min_alignment = property(fset=StructureFieldType.alignment.fset) | |
569 | StructureFieldType.alignment = property(fget=StructureFieldType.alignment.fget) | |
570 | ||
571 | ||
572 | class _VariantFieldTypeFieldIterator(collections.abc.Iterator): | |
573 | def __init__(self, variant_field_type): | |
574 | self._variant_field_type = variant_field_type | |
575 | self._at = 0 | |
576 | ||
577 | def __next__(self): | |
578 | if self._at == len(self._variant_field_type): | |
579 | raise StopIteration | |
580 | ||
839d52a5 | 581 | ret, name, field_type_ptr = native_bt.field_type_variant_get_field_by_index(self._variant_field_type._ptr, |
f6a5e476 PP |
582 | self._at) |
583 | assert(ret == 0) | |
81447b5b PP |
584 | native_bt.put(field_type_ptr) |
585 | self._at += 1 | |
586 | return name | |
587 | ||
588 | ||
589 | class VariantFieldType(_FieldType, _FieldContainer, _AlignmentProp): | |
590 | _NAME = 'Variant' | |
591 | _ITER_CLS = _VariantFieldTypeFieldIterator | |
592 | ||
f6a5e476 | 593 | def __init__(self, tag_name, tag_field_type=None): |
81447b5b | 594 | utils._check_str(tag_name) |
f6a5e476 PP |
595 | |
596 | if tag_field_type is None: | |
597 | tag_ft_ptr = None | |
598 | else: | |
599 | utils._check_type(tag_field_type, EnumerationFieldType) | |
600 | tag_ft_ptr = tag_field_type._ptr | |
601 | ||
839d52a5 | 602 | ptr = native_bt.field_type_variant_create(tag_ft_ptr, |
f6a5e476 | 603 | tag_name) |
81447b5b PP |
604 | self._check_create_status(ptr) |
605 | super().__init__(ptr) | |
606 | ||
607 | @property | |
608 | def tag_name(self): | |
839d52a5 | 609 | tag_name = native_bt.field_type_variant_get_tag_name(self._ptr) |
f6a5e476 | 610 | assert(tag_name is not None) |
81447b5b PP |
611 | return tag_name |
612 | ||
613 | @tag_name.setter | |
614 | def tag_name(self, tag_name): | |
615 | utils._check_str(tag_name) | |
839d52a5 | 616 | ret = native_bt.field_type_variant_set_tag_name(self._ptr, tag_name) |
81447b5b PP |
617 | utils._handle_ret(ret, "cannot set variant field type object's tag name") |
618 | ||
f6a5e476 PP |
619 | @property |
620 | def tag_field_type(self): | |
839d52a5 | 621 | ft_ptr = native_bt.field_type_variant_get_tag_type(self._ptr) |
f6a5e476 PP |
622 | |
623 | if ft_ptr is None: | |
624 | return | |
625 | ||
626 | return _create_from_ptr(ft_ptr) | |
627 | ||
81447b5b | 628 | def _count(self): |
839d52a5 | 629 | return native_bt.field_type_variant_get_field_count(self._ptr) |
81447b5b PP |
630 | |
631 | def _get_field_by_name(self, key): | |
839d52a5 | 632 | return native_bt.field_type_variant_get_field_type_by_name(self._ptr, key) |
81447b5b PP |
633 | |
634 | def _add_field(self, ptr, name): | |
839d52a5 | 635 | return native_bt.field_type_variant_add_field(self._ptr, ptr, name) |
81447b5b PP |
636 | |
637 | def _at(self, index): | |
f6a5e476 PP |
638 | if index < 0 or index >= len(self): |
639 | raise IndexError | |
640 | ||
839d52a5 | 641 | ret, name, field_type_ptr = native_bt.field_type_variant_get_field_by_index(self._ptr, index) |
f6a5e476 | 642 | assert(ret == 0) |
81447b5b PP |
643 | return _create_from_ptr(field_type_ptr) |
644 | ||
645 | ||
646 | class ArrayFieldType(_FieldType): | |
647 | _NAME = 'Array' | |
648 | ||
649 | def __init__(self, element_field_type, length): | |
650 | utils._check_type(element_field_type, _FieldType) | |
651 | utils._check_uint64(length) | |
839d52a5 | 652 | ptr = native_bt.field_type_array_create(element_field_type._ptr, length) |
81447b5b PP |
653 | self._check_create_status(ptr) |
654 | super().__init__(ptr) | |
655 | ||
656 | @property | |
657 | def length(self): | |
839d52a5 | 658 | length = native_bt.field_type_array_get_length(self._ptr) |
f6a5e476 | 659 | assert(length >= 0) |
81447b5b PP |
660 | return length |
661 | ||
662 | @property | |
663 | def element_field_type(self): | |
839d52a5 | 664 | ptr = native_bt.field_type_array_get_element_type(self._ptr) |
f6a5e476 | 665 | assert(ptr) |
81447b5b PP |
666 | return _create_from_ptr(ptr) |
667 | ||
668 | ||
669 | class SequenceFieldType(_FieldType): | |
670 | _NAME = 'Sequence' | |
671 | ||
672 | def __init__(self, element_field_type, length_name): | |
673 | utils._check_type(element_field_type, _FieldType) | |
674 | utils._check_str(length_name) | |
839d52a5 | 675 | ptr = native_bt.field_type_sequence_create(element_field_type._ptr, |
81447b5b PP |
676 | length_name) |
677 | self._check_create_status(ptr) | |
678 | super().__init__(ptr) | |
679 | ||
680 | @property | |
681 | def length_name(self): | |
839d52a5 | 682 | length_name = native_bt.field_type_sequence_get_length_field_name(self._ptr) |
f6a5e476 | 683 | assert(length_name is not None) |
81447b5b PP |
684 | return length_name |
685 | ||
686 | @property | |
687 | def element_field_type(self): | |
839d52a5 | 688 | ptr = native_bt.field_type_sequence_get_element_type(self._ptr) |
f6a5e476 | 689 | assert(ptr) |
81447b5b PP |
690 | return _create_from_ptr(ptr) |
691 | ||
692 | ||
693 | _TYPE_ID_TO_OBJ = { | |
839d52a5 PP |
694 | native_bt.FIELD_TYPE_ID_INTEGER: IntegerFieldType, |
695 | native_bt.FIELD_TYPE_ID_FLOAT: FloatingPointNumberFieldType, | |
696 | native_bt.FIELD_TYPE_ID_ENUM: EnumerationFieldType, | |
697 | native_bt.FIELD_TYPE_ID_STRING: StringFieldType, | |
698 | native_bt.FIELD_TYPE_ID_STRUCT: StructureFieldType, | |
699 | native_bt.FIELD_TYPE_ID_ARRAY: ArrayFieldType, | |
700 | native_bt.FIELD_TYPE_ID_SEQUENCE: SequenceFieldType, | |
701 | native_bt.FIELD_TYPE_ID_VARIANT: VariantFieldType, | |
81447b5b | 702 | } |