Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com> | |
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): | |
31 | typeid = native_bt.ctf_field_type_get_type_id(ptr) | |
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 | ||
47 | ret = native_bt.ctf_field_type_compare(self._ptr, other._ptr) | |
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): | |
56 | ptr = native_bt.ctf_field_type_copy(self._ptr) | |
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): | |
66 | field_ptr = native_bt.ctf_field_create(self._ptr) | |
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: | |
83 | NATIVE = native_bt.CTF_BYTE_ORDER_NATIVE | |
84 | LITTLE_ENDIAN = native_bt.CTF_BYTE_ORDER_LITTLE_ENDIAN | |
85 | BIG_ENDIAN = native_bt.CTF_BYTE_ORDER_BIG_ENDIAN | |
86 | NETWORK = native_bt.CTF_BYTE_ORDER_NETWORK | |
87 | ||
88 | ||
89 | class Encoding: | |
90 | NONE = native_bt.CTF_STRING_ENCODING_NONE | |
91 | UTF8 = native_bt.CTF_STRING_ENCODING_UTF8 | |
92 | ASCII = native_bt.CTF_STRING_ENCODING_ASCII | |
93 | ||
94 | ||
95 | class Base: | |
96 | BINARY = native_bt.CTF_INTEGER_BASE_BINARY | |
97 | OCTAL = native_bt.CTF_INTEGER_BASE_OCTAL | |
98 | DECIMAL = native_bt.CTF_INTEGER_BASE_DECIMAL | |
99 | HEXADECIMAL = native_bt.CTF_INTEGER_BASE_HEXADECIMAL | |
100 | ||
101 | ||
102 | class _AlignmentProp: | |
103 | @property | |
104 | def alignment(self): | |
105 | alignment = native_bt.ctf_field_type_get_alignment(self._ptr) | |
106 | utils._handle_ret(alignment, "cannot get field type object's alignment") | |
107 | return alignment | |
108 | ||
109 | @alignment.setter | |
110 | def alignment(self, alignment): | |
111 | utils._check_alignment(alignment) | |
112 | ret = native_bt.ctf_field_type_set_alignment(self._ptr, alignment) | |
113 | utils._handle_ret(ret, "cannot set field type object's alignment") | |
114 | ||
115 | ||
116 | class _ByteOrderProp: | |
117 | @property | |
118 | def byte_order(self): | |
119 | bo = native_bt.ctf_field_type_get_byte_order(self._ptr) | |
120 | utils._handle_ret(bo, "cannot get field type object's byte order") | |
121 | return bo | |
122 | ||
123 | @byte_order.setter | |
124 | def byte_order(self, byte_order): | |
125 | utils._check_int(byte_order) | |
126 | ret = native_bt.ctf_field_type_set_byte_order(self._ptr, byte_order) | |
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 | ||
140 | ptr = native_bt.ctf_field_type_integer_create(size) | |
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): | |
164 | size = native_bt.ctf_field_type_integer_get_size(self._ptr) | |
165 | utils._handle_ret(size, "cannot get integer field type object's size") | |
166 | return size | |
167 | ||
168 | @property | |
169 | def is_signed(self): | |
170 | is_signed = native_bt.ctf_field_type_integer_get_signed(self._ptr) | |
171 | utils._handle_ret(is_signed, | |
172 | "cannot get integer field type object's signedness") | |
173 | return is_signed > 0 | |
174 | ||
175 | @is_signed.setter | |
176 | def is_signed(self, is_signed): | |
177 | utils._check_bool(is_signed) | |
178 | ret = native_bt.ctf_field_type_integer_set_signed(self._ptr, int(is_signed)) | |
179 | utils._handle_ret(ret, "cannot set integer field type object's signedness") | |
180 | ||
181 | @property | |
182 | def base(self): | |
183 | base = native_bt.ctf_field_type_integer_get_base(self._ptr) | |
184 | utils._handle_ret(base, "cannot get integer field type object's base") | |
185 | return base | |
186 | ||
187 | @base.setter | |
188 | def base(self, base): | |
189 | utils._check_int(base) | |
190 | ret = native_bt.ctf_field_type_integer_set_base(self._ptr, base) | |
191 | utils._handle_ret(ret, "cannot set integer field type object's base") | |
192 | ||
193 | @property | |
194 | def encoding(self): | |
195 | encoding = native_bt.ctf_field_type_integer_get_encoding(self._ptr) | |
196 | utils._handle_ret(encoding, "cannot get integer field type object's encoding") | |
197 | return encoding | |
198 | ||
199 | @encoding.setter | |
200 | def encoding(self, encoding): | |
201 | utils._check_int(encoding) | |
202 | ret = native_bt.ctf_field_type_integer_set_encoding(self._ptr, encoding) | |
203 | utils._handle_ret(ret, "cannot set integer field type object's encoding") | |
204 | ||
205 | @property | |
206 | def mapped_clock_class(self): | |
207 | ptr = native_bt.ctf_field_type_integer_get_mapped_clock_class(self._ptr) | |
208 | utils._handle_ptr(ptr, "cannot get integer field type object's mapped clock class") | |
209 | return bt2.ClockClass._create_from_ptr(ptr) | |
210 | ||
211 | @mapped_clock_class.setter | |
212 | def mapped_clock_class(self, clock_class): | |
213 | utils._check_type(clock_class, bt2.ClockClass) | |
214 | ret = native_bt.ctf_field_type_integer_set_mapped_clock_class(self._ptr, clock_class._ptr) | |
215 | utils._handle_ret(ret, "cannot set integer field type object's mapped clock class") | |
216 | ||
217 | ||
218 | class FloatingPointNumberFieldType(_FieldType, _AlignmentProp, _ByteOrderProp): | |
219 | _NAME = 'Floating point number' | |
220 | ||
221 | def __init__(self, alignment=None, byte_order=None, exponent_size=None, | |
222 | mantissa_size=None): | |
223 | ptr = native_bt.ctf_field_type_floating_point_create() | |
224 | self._check_create_status(ptr) | |
225 | super().__init__(ptr) | |
226 | ||
227 | if alignment is not None: | |
228 | self.alignment = alignment | |
229 | ||
230 | if byte_order is not None: | |
231 | self.byte_order = byte_order | |
232 | ||
233 | if exponent_size is not None: | |
234 | self.exponent_size = exponent_size | |
235 | ||
236 | if mantissa_size is not None: | |
237 | self.mantissa_size = mantissa_size | |
238 | ||
239 | @property | |
240 | def exponent_size(self): | |
241 | exp_size = native_bt.ctf_field_type_floating_point_get_exponent_digits(self._ptr) | |
242 | utils._handle_ret(exp_size, "cannot get floating point number field type object's exponent size") | |
243 | return exp_size | |
244 | ||
245 | @exponent_size.setter | |
246 | def exponent_size(self, exponent_size): | |
247 | utils._check_uint64(exponent_size) | |
248 | ret = native_bt.ctf_field_type_floating_point_set_exponent_digits(self._ptr, exponent_size) | |
249 | utils._handle_ret(ret, "cannot set floating point number field type object's exponent size") | |
250 | ||
251 | @property | |
252 | def mantissa_size(self): | |
253 | exp_size = native_bt.ctf_field_type_floating_point_get_mantissa_digits(self._ptr) | |
254 | utils._handle_ret(exp_size, "cannot get floating point number field type object's mantissa size") | |
255 | return exp_size | |
256 | ||
257 | @mantissa_size.setter | |
258 | def mantissa_size(self, mantissa_size): | |
259 | utils._check_uint64(mantissa_size) | |
260 | ret = native_bt.ctf_field_type_floating_point_set_mantissa_digits(self._ptr, mantissa_size) | |
261 | utils._handle_ret(ret, "cannot set floating point number field type object's mantissa size") | |
262 | ||
263 | ||
264 | class _EnumerationFieldTypeMapping: | |
265 | def __init__(self, name, lower, upper): | |
266 | self._name = name | |
267 | self._lower = lower | |
268 | self._upper = upper | |
269 | ||
270 | @property | |
271 | def name(self): | |
272 | return self._name | |
273 | ||
274 | @property | |
275 | def lower(self): | |
276 | return self._lower | |
277 | ||
278 | @property | |
279 | def upper(self): | |
280 | return self._upper | |
281 | ||
282 | def __eq__(self, other): | |
283 | if type(other) is not self.__class__: | |
284 | return False | |
285 | ||
286 | return (self.name, self.lower, self.upper) == (other.name, other.lower, other.upper) | |
287 | ||
288 | ||
289 | class _EnumerationFieldTypeMappingIterator(object._Object, | |
290 | collections.abc.Iterator): | |
291 | def __init__(self, iter_ptr, is_signed): | |
292 | super().__init__(iter_ptr) | |
293 | self._is_signed = is_signed | |
294 | self._done = (iter_ptr is None) | |
295 | ||
296 | def __next__(self): | |
297 | if self._done: | |
298 | raise StopIteration | |
299 | ||
300 | if self._is_signed: | |
301 | ret, name, lower, upper = native_bt.ctf_field_type_enumeration_mapping_iterator_get_signed(self._ptr) | |
302 | else: | |
303 | ret, name, lower, upper = native_bt.ctf_field_type_enumeration_mapping_iterator_get_unsigned(self._ptr) | |
304 | ||
305 | utils._handle_ret(ret, "cannot get enumeration field type mapping iterator object's current mapping") | |
306 | mapping = _EnumerationFieldTypeMapping(name, lower, upper) | |
307 | ret = native_bt.ctf_field_type_enumeration_mapping_iterator_next(self._ptr) | |
308 | ||
309 | if ret < 0: | |
310 | self._done = True | |
311 | ||
312 | return mapping | |
313 | ||
314 | ||
315 | class EnumerationFieldType(IntegerFieldType, collections.abc.Sequence): | |
316 | _NAME = 'Enumeration' | |
317 | ||
318 | def __init__(self, int_field_type=None, size=None, alignment=None, | |
319 | byte_order=None, is_signed=None, base=None, encoding=None, | |
320 | mapped_clock_class=None): | |
321 | if int_field_type is None: | |
322 | int_field_type = IntegerFieldType(size=size, alignment=alignment, | |
323 | byte_order=byte_order, | |
324 | is_signed=is_signed, base=base, | |
325 | encoding=encoding, | |
326 | mapped_clock_class=mapped_clock_class) | |
327 | ||
328 | utils._check_type(int_field_type, IntegerFieldType) | |
329 | ptr = native_bt.ctf_field_type_enumeration_create(int_field_type._ptr) | |
330 | self._check_create_status(ptr) | |
331 | _FieldType.__init__(self, ptr) | |
332 | ||
333 | @property | |
334 | def integer_field_type(self): | |
335 | ptr = native_bt.ctf_field_type_enumeration_get_container_type(self._ptr) | |
336 | utils._handle_ptr(ptr, "cannot get enumeration field type object's integer field type") | |
337 | return _create_from_ptr(ptr) | |
338 | ||
339 | @property | |
340 | def size(self): | |
341 | return self.integer_field_type.size | |
342 | ||
343 | @property | |
344 | def alignment(self): | |
345 | return self.integer_field_type.alignment | |
346 | ||
347 | @alignment.setter | |
348 | def alignment(self, alignment): | |
349 | self.integer_field_type.alignment = alignment | |
350 | ||
351 | @property | |
352 | def byte_order(self): | |
353 | return self.integer_field_type.byte_order | |
354 | ||
355 | @byte_order.setter | |
356 | def byte_order(self, byte_order): | |
357 | self.integer_field_type.byte_order = byte_order | |
358 | ||
359 | @property | |
360 | def is_signed(self): | |
361 | return self.integer_field_type.is_signed | |
362 | ||
363 | @is_signed.setter | |
364 | def is_signed(self, is_signed): | |
365 | self.integer_field_type.is_signed = is_signed | |
366 | ||
367 | @property | |
368 | def base(self): | |
369 | return self.integer_field_type.base | |
370 | ||
371 | @base.setter | |
372 | def base(self, base): | |
373 | self.integer_field_type.base = base | |
374 | ||
375 | @property | |
376 | def encoding(self): | |
377 | return self.integer_field_type.encoding | |
378 | ||
379 | @encoding.setter | |
380 | def encoding(self, encoding): | |
381 | self.integer_field_type.encoding = encoding | |
382 | ||
383 | @property | |
384 | def mapped_clock_class(self): | |
385 | return self.integer_field_type.mapped_clock_class | |
386 | ||
387 | @mapped_clock_class.setter | |
388 | def mapped_clock_class(self, mapped_clock_class): | |
389 | self.integer_field_type.mapped_clock_class = mapped_clock_class | |
390 | ||
391 | def __len__(self): | |
392 | count = native_bt.ctf_field_type_enumeration_get_mapping_count(self._ptr) | |
393 | utils._handle_ret(count, "cannot get enumeration field type object's mapping count") | |
394 | return count | |
395 | ||
396 | def __getitem__(self, index): | |
397 | utils._check_uint64(index) | |
398 | ||
399 | if index >= len(self): | |
400 | raise IndexError | |
401 | ||
402 | if self.is_signed: | |
403 | get_fn = native_bt.ctf_field_type_enumeration_get_mapping_signed | |
404 | else: | |
405 | get_fn = native_bt.ctf_field_type_enumeration_get_mapping_unsigned | |
406 | ||
407 | ret, name, lower, upper = get_fn(self._ptr, index) | |
408 | utils._handle_ret(ret, "cannot get enumeration field type object's mapping") | |
409 | return _EnumerationFieldTypeMapping(name, lower, upper) | |
410 | ||
411 | def _get_mapping_iter(self, iter_ptr): | |
412 | return _EnumerationFieldTypeMappingIterator(iter_ptr, self.is_signed) | |
413 | ||
414 | def mappings_by_name(self, name): | |
415 | utils._check_str(name) | |
416 | iter_ptr = native_bt.ctf_field_type_enumeration_find_mappings_by_name(self._ptr, name) | |
417 | return self._get_mapping_iter(iter_ptr) | |
418 | ||
419 | def mappings_by_value(self, value): | |
420 | if self.is_signed: | |
421 | utils._check_int64(value) | |
422 | iter_ptr = native_bt.ctf_field_type_enumeration_find_mappings_by_signed_value(self._ptr, value) | |
423 | else: | |
424 | utils._check_uint64(value) | |
425 | iter_ptr = native_bt.ctf_field_type_enumeration_find_mappings_by_unsigned_value(self._ptr, value) | |
426 | ||
427 | return self._get_mapping_iter(iter_ptr) | |
428 | ||
429 | def append_mapping(self, name, lower, upper=None): | |
430 | utils._check_str(name) | |
431 | ||
432 | if upper is None: | |
433 | upper = lower | |
434 | ||
435 | if self.is_signed: | |
436 | add_fn = native_bt.ctf_field_type_enumeration_add_mapping | |
437 | utils._check_int64(lower) | |
438 | utils._check_int64(upper) | |
439 | else: | |
440 | add_fn = native_bt.ctf_field_type_enumeration_add_mapping_unsigned | |
441 | utils._check_uint64(lower) | |
442 | utils._check_uint64(upper) | |
443 | ||
444 | ret = add_fn(self._ptr, name, lower, upper) | |
445 | utils._handle_ret(ret, "cannot add mapping to enumeration field type object") | |
446 | ||
447 | def __iadd__(self, mappings): | |
448 | for mapping in mappings: | |
449 | self.append_mapping(mapping.name, mapping.lower, mapping.upper) | |
450 | ||
451 | return self | |
452 | ||
453 | ||
454 | class StringFieldType(_FieldType): | |
455 | _NAME = 'String' | |
456 | ||
457 | def __init__(self, encoding=None): | |
458 | ptr = native_bt.ctf_field_type_string_create() | |
459 | self._check_create_status(ptr) | |
460 | super().__init__(ptr) | |
461 | ||
462 | if encoding is not None: | |
463 | self.encoding = encoding | |
464 | ||
465 | @property | |
466 | def encoding(self): | |
467 | encoding = native_bt.ctf_field_type_string_get_encoding(self._ptr) | |
468 | utils._handle_ret(encoding, "cannot get string field type object's encoding") | |
469 | return encoding | |
470 | ||
471 | @encoding.setter | |
472 | def encoding(self, encoding): | |
473 | utils._check_int(encoding) | |
474 | ret = native_bt.ctf_field_type_string_set_encoding(self._ptr, encoding) | |
475 | utils._handle_ret(ret, "cannot set string field type object's encoding") | |
476 | ||
477 | ||
478 | class _FieldContainer(collections.abc.Mapping): | |
479 | def __len__(self): | |
480 | count = self._count() | |
481 | utils._handle_ret(count, "cannot get {} field type object's field count".format(self._NAME.lower())) | |
482 | return count | |
483 | ||
484 | def __getitem__(self, key): | |
485 | if not isinstance(key, str): | |
486 | raise TypeError("'{}' is not a 'str' object".format(key.__class__.__name__)) | |
487 | ||
488 | ptr = self._get_field_by_name(key) | |
489 | ||
490 | if ptr is None: | |
491 | raise KeyError(key) | |
492 | ||
493 | return _create_from_ptr(ptr) | |
494 | ||
495 | def __iter__(self): | |
496 | return self._ITER_CLS(self) | |
497 | ||
498 | def append_field(self, name, field_type): | |
499 | utils._check_str(name) | |
500 | utils._check_type(field_type, _FieldType) | |
501 | ret = self._add_field(field_type._ptr, name) | |
502 | utils._handle_ret(ret, "cannot add field to {} field type object".format(self._NAME.lower())) | |
503 | ||
504 | def __iadd__(self, fields): | |
505 | for name, field_type in fields.items(): | |
506 | self.append_field(name, field_type) | |
507 | ||
508 | return self | |
509 | ||
510 | def at_index(self, index): | |
511 | utils._check_uint64(index) | |
512 | return self._at(index) | |
513 | ||
514 | ||
515 | class _StructureFieldTypeFieldIterator(collections.abc.Iterator): | |
516 | def __init__(self, struct_field_type): | |
517 | self._struct_field_type = struct_field_type | |
518 | self._at = 0 | |
519 | ||
520 | def __next__(self): | |
521 | if self._at == len(self._struct_field_type): | |
522 | raise StopIteration | |
523 | ||
524 | ret, name, field_type_ptr = native_bt.ctf_field_type_structure_get_field(self._struct_field_type._ptr, self._at) | |
525 | utils._handle_ret(ret, "cannot get structure field type object's field") | |
526 | native_bt.put(field_type_ptr) | |
527 | self._at += 1 | |
528 | return name | |
529 | ||
530 | ||
531 | class StructureFieldType(_FieldType, _FieldContainer, _AlignmentProp): | |
532 | _NAME = 'Structure' | |
533 | _ITER_CLS = _StructureFieldTypeFieldIterator | |
534 | ||
535 | def __init__(self, min_alignment=None): | |
536 | ptr = native_bt.ctf_field_type_structure_create() | |
537 | self._check_create_status(ptr) | |
538 | super().__init__(ptr) | |
539 | ||
540 | if min_alignment is not None: | |
541 | self.min_alignment = min_alignment | |
542 | ||
543 | def _count(self): | |
544 | return native_bt.ctf_field_type_structure_get_field_count(self._ptr) | |
545 | ||
546 | def _get_field_by_name(self, key): | |
547 | return native_bt.ctf_field_type_structure_get_field_type_by_name(self._ptr, key) | |
548 | ||
549 | def _add_field(self, ptr, name): | |
550 | return native_bt.ctf_field_type_structure_add_field(self._ptr, ptr, | |
551 | name) | |
552 | ||
553 | def _at(self, index): | |
554 | ret, name, field_type_ptr = native_bt.ctf_field_type_structure_get_field(self._ptr, index) | |
555 | utils._handle_ret(ret, "cannot get structure field type object's field") | |
556 | return _create_from_ptr(field_type_ptr) | |
557 | ||
558 | ||
559 | StructureFieldType.min_alignment = property(fset=StructureFieldType.alignment.fset) | |
560 | StructureFieldType.alignment = property(fget=StructureFieldType.alignment.fget) | |
561 | ||
562 | ||
563 | class _VariantFieldTypeFieldIterator(collections.abc.Iterator): | |
564 | def __init__(self, variant_field_type): | |
565 | self._variant_field_type = variant_field_type | |
566 | self._at = 0 | |
567 | ||
568 | def __next__(self): | |
569 | if self._at == len(self._variant_field_type): | |
570 | raise StopIteration | |
571 | ||
572 | ret, name, field_type_ptr = native_bt.ctf_field_type_variant_get_field(self._variant_field_type._ptr, self._at) | |
573 | utils._handle_ret(ret, "cannot get variant field type object's field") | |
574 | native_bt.put(field_type_ptr) | |
575 | self._at += 1 | |
576 | return name | |
577 | ||
578 | ||
579 | class VariantFieldType(_FieldType, _FieldContainer, _AlignmentProp): | |
580 | _NAME = 'Variant' | |
581 | _ITER_CLS = _VariantFieldTypeFieldIterator | |
582 | ||
583 | def __init__(self, tag_name): | |
584 | utils._check_str(tag_name) | |
585 | ptr = native_bt.ctf_field_type_variant_create(None, tag_name) | |
586 | self._check_create_status(ptr) | |
587 | super().__init__(ptr) | |
588 | ||
589 | @property | |
590 | def tag_name(self): | |
591 | tag_name = native_bt.ctf_field_type_variant_get_tag_name(self._ptr) | |
592 | utils._handle_ptr(tag_name, "cannot get variant field type object's tag name") | |
593 | return tag_name | |
594 | ||
595 | @tag_name.setter | |
596 | def tag_name(self, tag_name): | |
597 | utils._check_str(tag_name) | |
598 | ret = native_bt.ctf_field_type_variant_set_tag_name(self._ptr, tag_name) | |
599 | utils._handle_ret(ret, "cannot set variant field type object's tag name") | |
600 | ||
601 | def _count(self): | |
602 | return native_bt.ctf_field_type_variant_get_field_count(self._ptr) | |
603 | ||
604 | def _get_field_by_name(self, key): | |
605 | return native_bt.ctf_field_type_variant_get_field_type_by_name(self._ptr, key) | |
606 | ||
607 | def _add_field(self, ptr, name): | |
608 | return native_bt.ctf_field_type_variant_add_field(self._ptr, ptr, name) | |
609 | ||
610 | def _at(self, index): | |
611 | ret, name, field_type_ptr = native_bt.ctf_field_type_variant_get_field(self._ptr, index) | |
612 | utils._handle_ret(ret, "cannot get variant field type object's field") | |
613 | return _create_from_ptr(field_type_ptr) | |
614 | ||
615 | ||
616 | class ArrayFieldType(_FieldType): | |
617 | _NAME = 'Array' | |
618 | ||
619 | def __init__(self, element_field_type, length): | |
620 | utils._check_type(element_field_type, _FieldType) | |
621 | utils._check_uint64(length) | |
622 | ptr = native_bt.ctf_field_type_array_create(element_field_type._ptr, length) | |
623 | self._check_create_status(ptr) | |
624 | super().__init__(ptr) | |
625 | ||
626 | @property | |
627 | def length(self): | |
628 | length = native_bt.ctf_field_type_array_get_length(self._ptr) | |
629 | utils._handle_ret(length, "cannot get array field type object's length") | |
630 | return length | |
631 | ||
632 | @property | |
633 | def element_field_type(self): | |
634 | ptr = native_bt.ctf_field_type_array_get_element_type(self._ptr) | |
635 | utils._handle_ptr(ptr, "cannot get array field type object's element field type") | |
636 | return _create_from_ptr(ptr) | |
637 | ||
638 | ||
639 | class SequenceFieldType(_FieldType): | |
640 | _NAME = 'Sequence' | |
641 | ||
642 | def __init__(self, element_field_type, length_name): | |
643 | utils._check_type(element_field_type, _FieldType) | |
644 | utils._check_str(length_name) | |
645 | ptr = native_bt.ctf_field_type_sequence_create(element_field_type._ptr, | |
646 | length_name) | |
647 | self._check_create_status(ptr) | |
648 | super().__init__(ptr) | |
649 | ||
650 | @property | |
651 | def length_name(self): | |
652 | length_name = native_bt.ctf_field_type_sequence_get_length_field_name(self._ptr) | |
653 | utils._handle_ptr(length_name, "cannot get sequence field type object's length name") | |
654 | return length_name | |
655 | ||
656 | @property | |
657 | def element_field_type(self): | |
658 | ptr = native_bt.ctf_field_type_sequence_get_element_type(self._ptr) | |
659 | utils._handle_ptr(ptr, "cannot get sequence field type object's element field type") | |
660 | return _create_from_ptr(ptr) | |
661 | ||
662 | ||
663 | _TYPE_ID_TO_OBJ = { | |
664 | native_bt.CTF_TYPE_ID_INTEGER: IntegerFieldType, | |
665 | native_bt.CTF_TYPE_ID_FLOAT: FloatingPointNumberFieldType, | |
666 | native_bt.CTF_TYPE_ID_ENUM: EnumerationFieldType, | |
667 | native_bt.CTF_TYPE_ID_STRING: StringFieldType, | |
668 | native_bt.CTF_TYPE_ID_STRUCT: StructureFieldType, | |
669 | native_bt.CTF_TYPE_ID_ARRAY: ArrayFieldType, | |
670 | native_bt.CTF_TYPE_ID_SEQUENCE: SequenceFieldType, | |
671 | native_bt.CTF_TYPE_ID_VARIANT: VariantFieldType, | |
672 | native_bt.CTF_TYPE_ID_UNTAGGED_VARIANT: VariantFieldType, | |
673 | } |