1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 import collections
.abc
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
)
35 class _FieldType(object._Object
, metaclass
=abc
.ABCMeta
):
36 def __init__(self
, ptr
):
39 def __eq__(self
, other
):
40 if not isinstance(other
, self
.__class
__):
41 # not comparing apples to apples
44 if self
.addr
== other
.addr
:
47 ret
= native_bt
.ctf_field_type_compare(self
._ptr
, other
._ptr
)
48 utils
._handle
_ret
(ret
, "cannot compare field types")
51 def _check_create_status(self
, ptr
):
53 raise bt2
.CreationError('cannot create {} field type object'.format(self
._NAME
.lower()))
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
)
60 def __deepcopy__(self
, memo
):
65 def __call__(self
, value
=None):
66 field_ptr
= native_bt
.ctf_field_create(self
._ptr
)
69 raise bt2
.CreationError('cannot create {} field object'.format(self
._NAME
.lower()))
71 field
= bt2
.fields
._create
_from
_ptr
(field_ptr
)
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
))
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
90 NONE
= native_bt
.CTF_STRING_ENCODING_NONE
91 UTF8
= native_bt
.CTF_STRING_ENCODING_UTF8
92 ASCII
= native_bt
.CTF_STRING_ENCODING_ASCII
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
102 class _AlignmentProp
:
105 alignment
= native_bt
.ctf_field_type_get_alignment(self
._ptr
)
106 assert(alignment
>= 0)
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")
116 class _ByteOrderProp
:
118 def byte_order(self
):
119 bo
= native_bt
.ctf_field_type_get_byte_order(self
._ptr
)
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")
130 class IntegerFieldType(_FieldType
, _AlignmentProp
, _ByteOrderProp
):
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
_uint
64(size
)
138 raise ValueError('size is 0 bits')
140 ptr
= native_bt
.ctf_field_type_integer_create(size
)
141 self
._check
_create
_status
(ptr
)
142 super().__init
__(ptr
)
144 if alignment
is not None:
145 self
.alignment
= alignment
147 if byte_order
is not None:
148 self
.byte_order
= byte_order
150 if is_signed
is not None:
151 self
.is_signed
= is_signed
156 if encoding
is not None:
157 self
.encoding
= encoding
159 if mapped_clock_class
is not None:
160 self
.mapped_clock_class
= mapped_clock_class
164 size
= native_bt
.ctf_field_type_integer_get_size(self
._ptr
)
170 is_signed
= native_bt
.ctf_field_type_integer_is_signed(self
._ptr
)
171 assert(is_signed
>= 0)
175 def is_signed(self
, is_signed
):
176 utils
._check
_bool
(is_signed
)
177 ret
= native_bt
.ctf_field_type_integer_set_is_signed(self
._ptr
, int(is_signed
))
178 utils
._handle
_ret
(ret
, "cannot set integer field type object's signedness")
182 base
= native_bt
.ctf_field_type_integer_get_base(self
._ptr
)
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")
194 encoding
= native_bt
.ctf_field_type_integer_get_encoding(self
._ptr
)
195 assert(encoding
>= 0)
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")
205 def mapped_clock_class(self
):
206 ptr
= native_bt
.ctf_field_type_integer_get_mapped_clock_class(self
._ptr
)
211 return bt2
.ClockClass
._create
_from
_ptr
(ptr
)
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")
220 class FloatingPointNumberFieldType(_FieldType
, _AlignmentProp
, _ByteOrderProp
):
221 _NAME
= 'Floating point number'
223 def __init__(self
, alignment
=None, byte_order
=None, exponent_size
=None,
225 ptr
= native_bt
.ctf_field_type_floating_point_create()
226 self
._check
_create
_status
(ptr
)
227 super().__init
__(ptr
)
229 if alignment
is not None:
230 self
.alignment
= alignment
232 if byte_order
is not None:
233 self
.byte_order
= byte_order
235 if exponent_size
is not None:
236 self
.exponent_size
= exponent_size
238 if mantissa_size
is not None:
239 self
.mantissa_size
= mantissa_size
242 def exponent_size(self
):
243 exp_size
= native_bt
.ctf_field_type_floating_point_get_exponent_digits(self
._ptr
)
244 assert(exp_size
>= 0)
247 @exponent_size.setter
248 def exponent_size(self
, exponent_size
):
249 utils
._check
_uint
64(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")
254 def mantissa_size(self
):
255 mant_size
= native_bt
.ctf_field_type_floating_point_get_mantissa_digits(self
._ptr
)
256 assert(mant_size
>= 0)
259 @mantissa_size.setter
260 def mantissa_size(self
, mantissa_size
):
261 utils
._check
_uint
64(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")
266 class _EnumerationFieldTypeMapping
:
267 def __init__(self
, name
, lower
, upper
):
284 def __eq__(self
, other
):
285 if type(other
) is not self
.__class
__:
288 return (self
.name
, self
.lower
, self
.upper
) == (other
.name
, other
.lower
, other
.upper
)
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)
303 ret
, name
, lower
, upper
= native_bt
.ctf_field_type_enumeration_mapping_iterator_get_signed(self
._ptr
)
305 ret
, name
, lower
, upper
= native_bt
.ctf_field_type_enumeration_mapping_iterator_get_unsigned(self
._ptr
)
308 mapping
= _EnumerationFieldTypeMapping(name
, lower
, upper
)
309 ret
= native_bt
.ctf_field_type_enumeration_mapping_iterator_next(self
._ptr
)
317 class EnumerationFieldType(IntegerFieldType
, collections
.abc
.Sequence
):
318 _NAME
= 'Enumeration'
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
,
328 mapped_clock_class
=mapped_clock_class
)
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
)
336 def integer_field_type(self
):
337 ptr
= native_bt
.ctf_field_type_enumeration_get_container_type(self
._ptr
)
339 return _create_from_ptr(ptr
)
343 return self
.integer_field_type
.size
347 return self
.integer_field_type
.alignment
350 def alignment(self
, alignment
):
351 self
.integer_field_type
.alignment
= alignment
354 def byte_order(self
):
355 return self
.integer_field_type
.byte_order
358 def byte_order(self
, byte_order
):
359 self
.integer_field_type
.byte_order
= byte_order
363 return self
.integer_field_type
.is_signed
366 def is_signed(self
, is_signed
):
367 self
.integer_field_type
.is_signed
= is_signed
371 return self
.integer_field_type
.base
374 def base(self
, base
):
375 self
.integer_field_type
.base
= base
379 return self
.integer_field_type
.encoding
382 def encoding(self
, encoding
):
383 self
.integer_field_type
.encoding
= encoding
386 def mapped_clock_class(self
):
387 return self
.integer_field_type
.mapped_clock_class
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
394 count
= native_bt
.ctf_field_type_enumeration_get_mapping_count(self
._ptr
)
398 def __getitem__(self
, index
):
399 utils
._check
_uint
64(index
)
401 if index
>= len(self
):
405 get_fn
= native_bt
.ctf_field_type_enumeration_get_mapping_signed
407 get_fn
= native_bt
.ctf_field_type_enumeration_get_mapping_unsigned
409 ret
, name
, lower
, upper
= get_fn(self
._ptr
, index
)
411 return _EnumerationFieldTypeMapping(name
, lower
, upper
)
413 def _get_mapping_iter(self
, iter_ptr
):
414 return _EnumerationFieldTypeMappingIterator(iter_ptr
, self
.is_signed
)
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
)
421 def mappings_by_value(self
, value
):
423 utils
._check
_int
64(value
)
424 iter_ptr
= native_bt
.ctf_field_type_enumeration_find_mappings_by_signed_value(self
._ptr
, value
)
426 utils
._check
_uint
64(value
)
427 iter_ptr
= native_bt
.ctf_field_type_enumeration_find_mappings_by_unsigned_value(self
._ptr
, value
)
429 return self
._get
_mapping
_iter
(iter_ptr
)
431 def append_mapping(self
, name
, lower
, upper
=None):
432 utils
._check
_str
(name
)
438 add_fn
= native_bt
.ctf_field_type_enumeration_add_mapping_signed
439 utils
._check
_int
64(lower
)
440 utils
._check
_int
64(upper
)
442 add_fn
= native_bt
.ctf_field_type_enumeration_add_mapping_unsigned
443 utils
._check
_uint
64(lower
)
444 utils
._check
_uint
64(upper
)
446 ret
= add_fn(self
._ptr
, name
, lower
, upper
)
447 utils
._handle
_ret
(ret
, "cannot add mapping to enumeration field type object")
449 def __iadd__(self
, mappings
):
450 for mapping
in mappings
:
451 self
.append_mapping(mapping
.name
, mapping
.lower
, mapping
.upper
)
456 class StringFieldType(_FieldType
):
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
)
464 if encoding
is not None:
465 self
.encoding
= encoding
469 encoding
= native_bt
.ctf_field_type_string_get_encoding(self
._ptr
)
470 assert(encoding
>= 0)
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")
480 class _FieldContainer(collections
.abc
.Mapping
):
482 count
= self
._count
()
486 def __getitem__(self
, key
):
487 if not isinstance(key
, str):
488 raise TypeError("'{}' is not a 'str' object".format(key
.__class
__.__name
__))
490 ptr
= self
._get
_field
_by
_name
(key
)
495 return _create_from_ptr(ptr
)
498 return self
._ITER
_CLS
(self
)
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()))
506 def __iadd__(self
, fields
):
507 for name
, field_type
in fields
.items():
508 self
.append_field(name
, field_type
)
512 def at_index(self
, index
):
513 utils
._check
_uint
64(index
)
514 return self
._at
(index
)
517 class _StructureFieldTypeFieldIterator(collections
.abc
.Iterator
):
518 def __init__(self
, struct_field_type
):
519 self
._struct
_field
_type
= struct_field_type
523 if self
._at
== len(self
._struct
_field
_type
):
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
,
530 native_bt
.put(field_type_ptr
)
535 class StructureFieldType(_FieldType
, _FieldContainer
, _AlignmentProp
):
537 _ITER_CLS
= _StructureFieldTypeFieldIterator
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
)
544 if min_alignment
is not None:
545 self
.min_alignment
= min_alignment
548 return native_bt
.ctf_field_type_structure_get_field_count(self
._ptr
)
550 def _get_field_by_name(self
, key
):
551 return native_bt
.ctf_field_type_structure_get_field_type_by_name(self
._ptr
, key
)
553 def _add_field(self
, ptr
, name
):
554 return native_bt
.ctf_field_type_structure_add_field(self
._ptr
, ptr
,
557 def _at(self
, index
):
558 if index
< 0 or index
>= len(self
):
561 ret
, name
, field_type_ptr
= native_bt
.ctf_field_type_structure_get_field_by_index(self
._ptr
, index
)
563 return _create_from_ptr(field_type_ptr
)
566 StructureFieldType
.min_alignment
= property(fset
=StructureFieldType
.alignment
.fset
)
567 StructureFieldType
.alignment
= property(fget
=StructureFieldType
.alignment
.fget
)
570 class _VariantFieldTypeFieldIterator(collections
.abc
.Iterator
):
571 def __init__(self
, variant_field_type
):
572 self
._variant
_field
_type
= variant_field_type
576 if self
._at
== len(self
._variant
_field
_type
):
579 ret
, name
, field_type_ptr
= native_bt
.ctf_field_type_variant_get_field_by_index(self
._variant
_field
_type
._ptr
,
582 native_bt
.put(field_type_ptr
)
587 class VariantFieldType(_FieldType
, _FieldContainer
, _AlignmentProp
):
589 _ITER_CLS
= _VariantFieldTypeFieldIterator
591 def __init__(self
, tag_name
, tag_field_type
=None):
592 utils
._check
_str
(tag_name
)
594 if tag_field_type
is None:
597 utils
._check
_type
(tag_field_type
, EnumerationFieldType
)
598 tag_ft_ptr
= tag_field_type
._ptr
600 ptr
= native_bt
.ctf_field_type_variant_create(tag_ft_ptr
,
602 self
._check
_create
_status
(ptr
)
603 super().__init
__(ptr
)
607 tag_name
= native_bt
.ctf_field_type_variant_get_tag_name(self
._ptr
)
608 assert(tag_name
is not None)
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")
618 def tag_field_type(self
):
619 ft_ptr
= native_bt
.ctf_field_type_variant_get_tag_type(self
._ptr
)
624 return _create_from_ptr(ft_ptr
)
627 return native_bt
.ctf_field_type_variant_get_field_count(self
._ptr
)
629 def _get_field_by_name(self
, key
):
630 return native_bt
.ctf_field_type_variant_get_field_type_by_name(self
._ptr
, key
)
632 def _add_field(self
, ptr
, name
):
633 return native_bt
.ctf_field_type_variant_add_field(self
._ptr
, ptr
, name
)
635 def _at(self
, index
):
636 if index
< 0 or index
>= len(self
):
639 ret
, name
, field_type_ptr
= native_bt
.ctf_field_type_variant_get_field_by_index(self
._ptr
, index
)
641 return _create_from_ptr(field_type_ptr
)
644 class ArrayFieldType(_FieldType
):
647 def __init__(self
, element_field_type
, length
):
648 utils
._check
_type
(element_field_type
, _FieldType
)
649 utils
._check
_uint
64(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
)
656 length
= native_bt
.ctf_field_type_array_get_length(self
._ptr
)
661 def element_field_type(self
):
662 ptr
= native_bt
.ctf_field_type_array_get_element_type(self
._ptr
)
664 return _create_from_ptr(ptr
)
667 class SequenceFieldType(_FieldType
):
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
,
675 self
._check
_create
_status
(ptr
)
676 super().__init
__(ptr
)
679 def length_name(self
):
680 length_name
= native_bt
.ctf_field_type_sequence_get_length_field_name(self
._ptr
)
681 assert(length_name
is not None)
685 def element_field_type(self
):
686 ptr
= native_bt
.ctf_field_type_sequence_get_element_type(self
._ptr
)
688 return _create_from_ptr(ptr
)
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
,