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): | |
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) | |
811644b8 | 106 | assert(alignment >= 0) |
81447b5b PP |
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) | |
811644b8 | 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) | |
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) | |
811644b8 | 165 | assert(size >= 1) |
81447b5b PP |
166 | return size |
167 | ||
168 | @property | |
169 | def is_signed(self): | |
811644b8 PP |
170 | is_signed = native_bt.ctf_field_type_integer_is_signed(self._ptr) |
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) | |
811644b8 | 177 | ret = native_bt.ctf_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): | |
182 | base = native_bt.ctf_field_type_integer_get_base(self._ptr) | |
811644b8 | 183 | assert(base >= 0) |
81447b5b PP |
184 | return base |
185 | ||
186 | @base.setter | |
187 | def base(self, base): | |
188 | utils._check_int(base) | |
189 | ret = native_bt.ctf_field_type_integer_set_base(self._ptr, base) | |
190 | utils._handle_ret(ret, "cannot set integer field type object's base") | |
191 | ||
192 | @property | |
193 | def encoding(self): | |
194 | encoding = native_bt.ctf_field_type_integer_get_encoding(self._ptr) | |
811644b8 | 195 | assert(encoding >= 0) |
81447b5b PP |
196 | return encoding |
197 | ||
198 | @encoding.setter | |
199 | def encoding(self, encoding): | |
200 | utils._check_int(encoding) | |
201 | ret = native_bt.ctf_field_type_integer_set_encoding(self._ptr, encoding) | |
202 | utils._handle_ret(ret, "cannot set integer field type object's encoding") | |
203 | ||
204 | @property | |
205 | def mapped_clock_class(self): | |
206 | ptr = native_bt.ctf_field_type_integer_get_mapped_clock_class(self._ptr) | |
811644b8 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) | |
216 | ret = native_bt.ctf_field_type_integer_set_mapped_clock_class(self._ptr, clock_class._ptr) | |
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): | |
225 | ptr = native_bt.ctf_field_type_floating_point_create() | |
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): | |
243 | exp_size = native_bt.ctf_field_type_floating_point_get_exponent_digits(self._ptr) | |
811644b8 | 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) | |
250 | ret = native_bt.ctf_field_type_floating_point_set_exponent_digits(self._ptr, exponent_size) | |
251 | utils._handle_ret(ret, "cannot set floating point number field type object's exponent size") | |
252 | ||
253 | @property | |
254 | def mantissa_size(self): | |
811644b8 PP |
255 | mant_size = native_bt.ctf_field_type_floating_point_get_mantissa_digits(self._ptr) |
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) | |
262 | ret = native_bt.ctf_field_type_floating_point_set_mantissa_digits(self._ptr, mantissa_size) | |
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 | ||
302 | if self._is_signed: | |
303 | ret, name, lower, upper = native_bt.ctf_field_type_enumeration_mapping_iterator_get_signed(self._ptr) | |
304 | else: | |
305 | ret, name, lower, upper = native_bt.ctf_field_type_enumeration_mapping_iterator_get_unsigned(self._ptr) | |
306 | ||
811644b8 | 307 | assert(ret == 0) |
81447b5b PP |
308 | mapping = _EnumerationFieldTypeMapping(name, lower, upper) |
309 | ret = native_bt.ctf_field_type_enumeration_mapping_iterator_next(self._ptr) | |
310 | ||
311 | if ret < 0: | |
312 | self._done = True | |
313 | ||
314 | return mapping | |
315 | ||
316 | ||
317 | class EnumerationFieldType(IntegerFieldType, collections.abc.Sequence): | |
318 | _NAME = 'Enumeration' | |
319 | ||
320 | def __init__(self, int_field_type=None, size=None, alignment=None, | |
321 | byte_order=None, is_signed=None, base=None, encoding=None, | |
322 | mapped_clock_class=None): | |
323 | if int_field_type is None: | |
324 | int_field_type = IntegerFieldType(size=size, alignment=alignment, | |
325 | byte_order=byte_order, | |
326 | is_signed=is_signed, base=base, | |
327 | encoding=encoding, | |
328 | mapped_clock_class=mapped_clock_class) | |
329 | ||
330 | utils._check_type(int_field_type, IntegerFieldType) | |
331 | ptr = native_bt.ctf_field_type_enumeration_create(int_field_type._ptr) | |
332 | self._check_create_status(ptr) | |
333 | _FieldType.__init__(self, ptr) | |
334 | ||
335 | @property | |
336 | def integer_field_type(self): | |
337 | ptr = native_bt.ctf_field_type_enumeration_get_container_type(self._ptr) | |
811644b8 | 338 | assert(ptr) |
81447b5b PP |
339 | return _create_from_ptr(ptr) |
340 | ||
341 | @property | |
342 | def size(self): | |
343 | return self.integer_field_type.size | |
344 | ||
345 | @property | |
346 | def alignment(self): | |
347 | return self.integer_field_type.alignment | |
348 | ||
349 | @alignment.setter | |
350 | def alignment(self, alignment): | |
351 | self.integer_field_type.alignment = alignment | |
352 | ||
353 | @property | |
354 | def byte_order(self): | |
355 | return self.integer_field_type.byte_order | |
356 | ||
357 | @byte_order.setter | |
358 | def byte_order(self, byte_order): | |
359 | self.integer_field_type.byte_order = byte_order | |
360 | ||
361 | @property | |
362 | def is_signed(self): | |
363 | return self.integer_field_type.is_signed | |
364 | ||
365 | @is_signed.setter | |
366 | def is_signed(self, is_signed): | |
367 | self.integer_field_type.is_signed = is_signed | |
368 | ||
369 | @property | |
370 | def base(self): | |
371 | return self.integer_field_type.base | |
372 | ||
373 | @base.setter | |
374 | def base(self, base): | |
375 | self.integer_field_type.base = base | |
376 | ||
377 | @property | |
378 | def encoding(self): | |
379 | return self.integer_field_type.encoding | |
380 | ||
381 | @encoding.setter | |
382 | def encoding(self, encoding): | |
383 | self.integer_field_type.encoding = encoding | |
384 | ||
385 | @property | |
386 | def mapped_clock_class(self): | |
387 | return self.integer_field_type.mapped_clock_class | |
388 | ||
389 | @mapped_clock_class.setter | |
390 | def mapped_clock_class(self, mapped_clock_class): | |
391 | self.integer_field_type.mapped_clock_class = mapped_clock_class | |
392 | ||
393 | def __len__(self): | |
394 | count = native_bt.ctf_field_type_enumeration_get_mapping_count(self._ptr) | |
811644b8 | 395 | assert(count >= 0) |
81447b5b PP |
396 | return count |
397 | ||
398 | def __getitem__(self, index): | |
399 | utils._check_uint64(index) | |
400 | ||
401 | if index >= len(self): | |
402 | raise IndexError | |
403 | ||
404 | if self.is_signed: | |
405 | get_fn = native_bt.ctf_field_type_enumeration_get_mapping_signed | |
406 | else: | |
407 | get_fn = native_bt.ctf_field_type_enumeration_get_mapping_unsigned | |
408 | ||
409 | ret, name, lower, upper = get_fn(self._ptr, index) | |
811644b8 | 410 | assert(ret == 0) |
81447b5b PP |
411 | return _EnumerationFieldTypeMapping(name, lower, upper) |
412 | ||
413 | def _get_mapping_iter(self, iter_ptr): | |
414 | return _EnumerationFieldTypeMappingIterator(iter_ptr, self.is_signed) | |
415 | ||
416 | def mappings_by_name(self, name): | |
417 | utils._check_str(name) | |
418 | iter_ptr = native_bt.ctf_field_type_enumeration_find_mappings_by_name(self._ptr, name) | |
419 | return self._get_mapping_iter(iter_ptr) | |
420 | ||
421 | def mappings_by_value(self, value): | |
422 | if self.is_signed: | |
423 | utils._check_int64(value) | |
424 | iter_ptr = native_bt.ctf_field_type_enumeration_find_mappings_by_signed_value(self._ptr, value) | |
425 | else: | |
426 | utils._check_uint64(value) | |
427 | iter_ptr = native_bt.ctf_field_type_enumeration_find_mappings_by_unsigned_value(self._ptr, value) | |
428 | ||
429 | return self._get_mapping_iter(iter_ptr) | |
430 | ||
431 | def append_mapping(self, name, lower, upper=None): | |
432 | utils._check_str(name) | |
433 | ||
434 | if upper is None: | |
435 | upper = lower | |
436 | ||
437 | if self.is_signed: | |
811644b8 | 438 | add_fn = native_bt.ctf_field_type_enumeration_add_mapping_signed |
81447b5b PP |
439 | utils._check_int64(lower) |
440 | utils._check_int64(upper) | |
441 | else: | |
442 | add_fn = native_bt.ctf_field_type_enumeration_add_mapping_unsigned | |
443 | utils._check_uint64(lower) | |
444 | utils._check_uint64(upper) | |
445 | ||
446 | ret = add_fn(self._ptr, name, lower, upper) | |
447 | utils._handle_ret(ret, "cannot add mapping to enumeration field type object") | |
448 | ||
449 | def __iadd__(self, mappings): | |
450 | for mapping in mappings: | |
451 | self.append_mapping(mapping.name, mapping.lower, mapping.upper) | |
452 | ||
453 | return self | |
454 | ||
455 | ||
456 | class StringFieldType(_FieldType): | |
457 | _NAME = 'String' | |
458 | ||
459 | def __init__(self, encoding=None): | |
460 | ptr = native_bt.ctf_field_type_string_create() | |
461 | self._check_create_status(ptr) | |
462 | super().__init__(ptr) | |
463 | ||
464 | if encoding is not None: | |
465 | self.encoding = encoding | |
466 | ||
467 | @property | |
468 | def encoding(self): | |
469 | encoding = native_bt.ctf_field_type_string_get_encoding(self._ptr) | |
811644b8 | 470 | assert(encoding >= 0) |
81447b5b PP |
471 | return encoding |
472 | ||
473 | @encoding.setter | |
474 | def encoding(self, encoding): | |
475 | utils._check_int(encoding) | |
476 | ret = native_bt.ctf_field_type_string_set_encoding(self._ptr, encoding) | |
477 | utils._handle_ret(ret, "cannot set string field type object's encoding") | |
478 | ||
479 | ||
480 | class _FieldContainer(collections.abc.Mapping): | |
481 | def __len__(self): | |
482 | count = self._count() | |
811644b8 | 483 | assert(count >= 0) |
81447b5b PP |
484 | return count |
485 | ||
486 | def __getitem__(self, key): | |
487 | if not isinstance(key, str): | |
488 | raise TypeError("'{}' is not a 'str' object".format(key.__class__.__name__)) | |
489 | ||
490 | ptr = self._get_field_by_name(key) | |
491 | ||
492 | if ptr is None: | |
493 | raise KeyError(key) | |
494 | ||
495 | return _create_from_ptr(ptr) | |
496 | ||
497 | def __iter__(self): | |
498 | return self._ITER_CLS(self) | |
499 | ||
500 | def append_field(self, name, field_type): | |
501 | utils._check_str(name) | |
502 | utils._check_type(field_type, _FieldType) | |
503 | ret = self._add_field(field_type._ptr, name) | |
504 | utils._handle_ret(ret, "cannot add field to {} field type object".format(self._NAME.lower())) | |
505 | ||
506 | def __iadd__(self, fields): | |
507 | for name, field_type in fields.items(): | |
508 | self.append_field(name, field_type) | |
509 | ||
510 | return self | |
511 | ||
512 | def at_index(self, index): | |
513 | utils._check_uint64(index) | |
514 | return self._at(index) | |
515 | ||
516 | ||
517 | class _StructureFieldTypeFieldIterator(collections.abc.Iterator): | |
518 | def __init__(self, struct_field_type): | |
519 | self._struct_field_type = struct_field_type | |
520 | self._at = 0 | |
521 | ||
522 | def __next__(self): | |
523 | if self._at == len(self._struct_field_type): | |
524 | raise StopIteration | |
525 | ||
811644b8 PP |
526 | get_ft_by_index = native_bt.ctf_field_type_structure_get_field_by_index |
527 | ret, name, field_type_ptr = get_ft_by_index(self._struct_field_type._ptr, | |
528 | self._at) | |
529 | assert(ret == 0) | |
81447b5b PP |
530 | native_bt.put(field_type_ptr) |
531 | self._at += 1 | |
532 | return name | |
533 | ||
534 | ||
535 | class StructureFieldType(_FieldType, _FieldContainer, _AlignmentProp): | |
536 | _NAME = 'Structure' | |
537 | _ITER_CLS = _StructureFieldTypeFieldIterator | |
538 | ||
539 | def __init__(self, min_alignment=None): | |
540 | ptr = native_bt.ctf_field_type_structure_create() | |
541 | self._check_create_status(ptr) | |
542 | super().__init__(ptr) | |
543 | ||
544 | if min_alignment is not None: | |
545 | self.min_alignment = min_alignment | |
546 | ||
547 | def _count(self): | |
548 | return native_bt.ctf_field_type_structure_get_field_count(self._ptr) | |
549 | ||
550 | def _get_field_by_name(self, key): | |
551 | return native_bt.ctf_field_type_structure_get_field_type_by_name(self._ptr, key) | |
552 | ||
553 | def _add_field(self, ptr, name): | |
554 | return native_bt.ctf_field_type_structure_add_field(self._ptr, ptr, | |
555 | name) | |
556 | ||
557 | def _at(self, index): | |
811644b8 PP |
558 | if index < 0 or index >= len(self): |
559 | raise IndexError | |
560 | ||
561 | ret, name, field_type_ptr = native_bt.ctf_field_type_structure_get_field_by_index(self._ptr, index) | |
562 | assert(ret == 0) | |
81447b5b PP |
563 | return _create_from_ptr(field_type_ptr) |
564 | ||
565 | ||
566 | StructureFieldType.min_alignment = property(fset=StructureFieldType.alignment.fset) | |
567 | StructureFieldType.alignment = property(fget=StructureFieldType.alignment.fget) | |
568 | ||
569 | ||
570 | class _VariantFieldTypeFieldIterator(collections.abc.Iterator): | |
571 | def __init__(self, variant_field_type): | |
572 | self._variant_field_type = variant_field_type | |
573 | self._at = 0 | |
574 | ||
575 | def __next__(self): | |
576 | if self._at == len(self._variant_field_type): | |
577 | raise StopIteration | |
578 | ||
811644b8 PP |
579 | ret, name, field_type_ptr = native_bt.ctf_field_type_variant_get_field_by_index(self._variant_field_type._ptr, |
580 | self._at) | |
581 | assert(ret == 0) | |
81447b5b PP |
582 | native_bt.put(field_type_ptr) |
583 | self._at += 1 | |
584 | return name | |
585 | ||
586 | ||
587 | class VariantFieldType(_FieldType, _FieldContainer, _AlignmentProp): | |
588 | _NAME = 'Variant' | |
589 | _ITER_CLS = _VariantFieldTypeFieldIterator | |
590 | ||
811644b8 | 591 | def __init__(self, tag_name, tag_field_type=None): |
81447b5b | 592 | utils._check_str(tag_name) |
811644b8 PP |
593 | |
594 | if tag_field_type is None: | |
595 | tag_ft_ptr = None | |
596 | else: | |
597 | utils._check_type(tag_field_type, EnumerationFieldType) | |
598 | tag_ft_ptr = tag_field_type._ptr | |
599 | ||
600 | ptr = native_bt.ctf_field_type_variant_create(tag_ft_ptr, | |
601 | tag_name) | |
81447b5b PP |
602 | self._check_create_status(ptr) |
603 | super().__init__(ptr) | |
604 | ||
605 | @property | |
606 | def tag_name(self): | |
607 | tag_name = native_bt.ctf_field_type_variant_get_tag_name(self._ptr) | |
811644b8 | 608 | assert(tag_name is not None) |
81447b5b PP |
609 | return tag_name |
610 | ||
611 | @tag_name.setter | |
612 | def tag_name(self, tag_name): | |
613 | utils._check_str(tag_name) | |
614 | ret = native_bt.ctf_field_type_variant_set_tag_name(self._ptr, tag_name) | |
615 | utils._handle_ret(ret, "cannot set variant field type object's tag name") | |
616 | ||
811644b8 PP |
617 | @property |
618 | def tag_field_type(self): | |
619 | ft_ptr = native_bt.ctf_field_type_variant_get_tag_type(self._ptr) | |
620 | ||
621 | if ft_ptr is None: | |
622 | return | |
623 | ||
624 | return _create_from_ptr(ft_ptr) | |
625 | ||
81447b5b PP |
626 | def _count(self): |
627 | return native_bt.ctf_field_type_variant_get_field_count(self._ptr) | |
628 | ||
629 | def _get_field_by_name(self, key): | |
630 | return native_bt.ctf_field_type_variant_get_field_type_by_name(self._ptr, key) | |
631 | ||
632 | def _add_field(self, ptr, name): | |
633 | return native_bt.ctf_field_type_variant_add_field(self._ptr, ptr, name) | |
634 | ||
635 | def _at(self, index): | |
811644b8 PP |
636 | if index < 0 or index >= len(self): |
637 | raise IndexError | |
638 | ||
639 | ret, name, field_type_ptr = native_bt.ctf_field_type_variant_get_field_by_index(self._ptr, index) | |
640 | assert(ret == 0) | |
81447b5b PP |
641 | return _create_from_ptr(field_type_ptr) |
642 | ||
643 | ||
644 | class ArrayFieldType(_FieldType): | |
645 | _NAME = 'Array' | |
646 | ||
647 | def __init__(self, element_field_type, length): | |
648 | utils._check_type(element_field_type, _FieldType) | |
649 | utils._check_uint64(length) | |
650 | ptr = native_bt.ctf_field_type_array_create(element_field_type._ptr, length) | |
651 | self._check_create_status(ptr) | |
652 | super().__init__(ptr) | |
653 | ||
654 | @property | |
655 | def length(self): | |
656 | length = native_bt.ctf_field_type_array_get_length(self._ptr) | |
811644b8 | 657 | assert(length >= 0) |
81447b5b PP |
658 | return length |
659 | ||
660 | @property | |
661 | def element_field_type(self): | |
662 | ptr = native_bt.ctf_field_type_array_get_element_type(self._ptr) | |
811644b8 | 663 | assert(ptr) |
81447b5b PP |
664 | return _create_from_ptr(ptr) |
665 | ||
666 | ||
667 | class SequenceFieldType(_FieldType): | |
668 | _NAME = 'Sequence' | |
669 | ||
670 | def __init__(self, element_field_type, length_name): | |
671 | utils._check_type(element_field_type, _FieldType) | |
672 | utils._check_str(length_name) | |
673 | ptr = native_bt.ctf_field_type_sequence_create(element_field_type._ptr, | |
674 | length_name) | |
675 | self._check_create_status(ptr) | |
676 | super().__init__(ptr) | |
677 | ||
678 | @property | |
679 | def length_name(self): | |
680 | length_name = native_bt.ctf_field_type_sequence_get_length_field_name(self._ptr) | |
811644b8 | 681 | assert(length_name is not None) |
81447b5b PP |
682 | return length_name |
683 | ||
684 | @property | |
685 | def element_field_type(self): | |
686 | ptr = native_bt.ctf_field_type_sequence_get_element_type(self._ptr) | |
811644b8 | 687 | assert(ptr) |
81447b5b PP |
688 | return _create_from_ptr(ptr) |
689 | ||
690 | ||
691 | _TYPE_ID_TO_OBJ = { | |
811644b8 PP |
692 | native_bt.CTF_FIELD_TYPE_ID_INTEGER: IntegerFieldType, |
693 | native_bt.CTF_FIELD_TYPE_ID_FLOAT: FloatingPointNumberFieldType, | |
694 | native_bt.CTF_FIELD_TYPE_ID_ENUM: EnumerationFieldType, | |
695 | native_bt.CTF_FIELD_TYPE_ID_STRING: StringFieldType, | |
696 | native_bt.CTF_FIELD_TYPE_ID_STRUCT: StructureFieldType, | |
697 | native_bt.CTF_FIELD_TYPE_ID_ARRAY: ArrayFieldType, | |
698 | native_bt.CTF_FIELD_TYPE_ID_SEQUENCE: SequenceFieldType, | |
699 | native_bt.CTF_FIELD_TYPE_ID_VARIANT: VariantFieldType, | |
81447b5b | 700 | } |