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
.field_class_get_type_id(ptr
)
32 return _TYPE_ID_TO_OBJ
[typeid
]._create
_from
_ptr
(ptr
)
35 class _FieldClass(object._SharedObject
, 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
.field_class_compare(self
._ptr
, other
._ptr
)
48 utils
._handle
_ret
(ret
, "cannot compare field classes")
51 def _check_create_status(self
, ptr
):
53 raise bt2
.CreationError('cannot create {} field class object'.format(self
._NAME
.lower()))
56 ptr
= native_bt
.field_class_copy(self
._ptr
)
57 utils
._handle
_ptr
(ptr
, 'cannot copy {} field class 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
.field_create(self
._ptr
)
69 raise bt2
.CreationError('cannot create {} field object'.format(self
._NAME
.lower()))
71 field
= bt2
.field
._create
_from
_ptr
(field_ptr
)
74 if not isinstance(field
, (bt2
.field
._IntegerField
, bt2
.field
._FloatingPointNumberField
, bt2
.field
._StringField
)):
75 raise bt2
.Error('cannot assign an initial value to a {} field object'.format(field
._NAME
))
85 alignment
= native_bt
.field_class_get_alignment(self
._ptr
)
86 assert(alignment
>= 0)
90 def alignment(self
, alignment
):
91 utils
._check
_alignment
(alignment
)
92 ret
= native_bt
.field_class_set_alignment(self
._ptr
, alignment
)
93 utils
._handle
_ret
(ret
, "cannot set field class object's alignment")
99 bo
= native_bt
.field_class_get_byte_order(self
._ptr
)
104 def byte_order(self
, byte_order
):
105 utils
._check
_int
(byte_order
)
106 ret
= native_bt
.field_class_set_byte_order(self
._ptr
, byte_order
)
107 utils
._handle
_ret
(ret
, "cannot set field class object's byte order")
110 class IntegerFieldClass(_FieldClass
, _AlignmentProp
, _ByteOrderProp
):
113 def __init__(self
, size
, alignment
=None, byte_order
=None, is_signed
=None,
114 base
=None, encoding
=None, mapped_clock_class
=None):
115 utils
._check
_uint
64(size
)
118 raise ValueError('size is 0 bits')
120 ptr
= native_bt
.field_class_integer_create(size
)
121 self
._check
_create
_status
(ptr
)
122 super().__init
__(ptr
)
124 if alignment
is not None:
125 self
.alignment
= alignment
127 if byte_order
is not None:
128 self
.byte_order
= byte_order
130 if is_signed
is not None:
131 self
.is_signed
= is_signed
136 if encoding
is not None:
137 self
.encoding
= encoding
139 if mapped_clock_class
is not None:
140 self
.mapped_clock_class
= mapped_clock_class
144 size
= native_bt
.field_class_integer_get_size(self
._ptr
)
150 is_signed
= native_bt
.field_class_integer_is_signed(self
._ptr
)
151 assert(is_signed
>= 0)
155 def is_signed(self
, is_signed
):
156 utils
._check
_bool
(is_signed
)
157 ret
= native_bt
.field_class_integer_set_is_signed(self
._ptr
, int(is_signed
))
158 utils
._handle
_ret
(ret
, "cannot set integer field class object's signedness")
162 base
= native_bt
.field_class_integer_get_base(self
._ptr
)
167 def base(self
, base
):
168 utils
._check
_int
(base
)
169 ret
= native_bt
.field_class_integer_set_base(self
._ptr
, base
)
170 utils
._handle
_ret
(ret
, "cannot set integer field class object's base")
174 encoding
= native_bt
.field_class_integer_get_encoding(self
._ptr
)
175 assert(encoding
>= 0)
179 def encoding(self
, encoding
):
180 utils
._check
_int
(encoding
)
181 ret
= native_bt
.field_class_integer_set_encoding(self
._ptr
, encoding
)
182 utils
._handle
_ret
(ret
, "cannot set integer field class object's encoding")
185 def mapped_clock_class(self
):
186 ptr
= native_bt
.field_class_integer_get_mapped_clock_class(self
._ptr
)
191 return bt2
.ClockClass
._create
_from
_ptr
(ptr
)
193 @mapped_clock_class.setter
194 def mapped_clock_class(self
, clock_class
):
195 utils
._check
_type
(clock_class
, bt2
.ClockClass
)
196 ret
= native_bt
.field_class_integer_set_mapped_clock_class(self
._ptr
, clock_class
._ptr
)
197 utils
._handle
_ret
(ret
, "cannot set integer field class object's mapped clock class")
200 class FloatingPointNumberFieldClass(_FieldClass
, _AlignmentProp
, _ByteOrderProp
):
201 _NAME
= 'Floating point number'
203 def __init__(self
, alignment
=None, byte_order
=None, exponent_size
=None,
205 ptr
= native_bt
.field_class_floating_point_create()
206 self
._check
_create
_status
(ptr
)
207 super().__init
__(ptr
)
209 if alignment
is not None:
210 self
.alignment
= alignment
212 if byte_order
is not None:
213 self
.byte_order
= byte_order
215 if exponent_size
is not None:
216 self
.exponent_size
= exponent_size
218 if mantissa_size
is not None:
219 self
.mantissa_size
= mantissa_size
222 def exponent_size(self
):
223 exp_size
= native_bt
.field_class_floating_point_get_exponent_digits(self
._ptr
)
224 assert(exp_size
>= 0)
227 @exponent_size.setter
228 def exponent_size(self
, exponent_size
):
229 utils
._check
_uint
64(exponent_size
)
230 ret
= native_bt
.field_class_floating_point_set_exponent_digits(self
._ptr
, exponent_size
)
231 utils
._handle
_ret
(ret
, "cannot set floating point number field class object's exponent size")
234 def mantissa_size(self
):
235 mant_size
= native_bt
.field_class_floating_point_get_mantissa_digits(self
._ptr
)
236 assert(mant_size
>= 0)
239 @mantissa_size.setter
240 def mantissa_size(self
, mantissa_size
):
241 utils
._check
_uint
64(mantissa_size
)
242 ret
= native_bt
.field_class_floating_point_set_mantissa_digits(self
._ptr
, mantissa_size
)
243 utils
._handle
_ret
(ret
, "cannot set floating point number field class object's mantissa size")
246 class _EnumerationFieldClassMapping
:
247 def __init__(self
, name
, lower
, upper
):
264 def __eq__(self
, other
):
265 if type(other
) is not self
.__class
__:
268 return (self
.name
, self
.lower
, self
.upper
) == (other
.name
, other
.lower
, other
.upper
)
271 class _EnumerationFieldClassMappingIterator(object._SharedObject
,
272 collections
.abc
.Iterator
):
273 def __init__(self
, iter_ptr
, is_signed
):
274 super().__init
__(iter_ptr
)
275 self
._is
_signed
= is_signed
276 self
._done
= (iter_ptr
is None)
282 ret
= native_bt
.field_class_enumeration_mapping_iterator_next(self
._ptr
)
288 ret
, name
, lower
, upper
= native_bt
.field_class_enumeration_mapping_iterator_get_signed(self
._ptr
)
290 ret
, name
, lower
, upper
= native_bt
.field_class_enumeration_mapping_iterator_get_unsigned(self
._ptr
)
293 mapping
= _EnumerationFieldClassMapping(name
, lower
, upper
)
298 class EnumerationFieldClass(IntegerFieldClass
, collections
.abc
.Sequence
):
299 _NAME
= 'Enumeration'
301 def __init__(self
, int_field_class
=None, size
=None, alignment
=None,
302 byte_order
=None, is_signed
=None, base
=None, encoding
=None,
303 mapped_clock_class
=None):
304 if int_field_class
is None:
305 int_field_class
= IntegerFieldClass(size
=size
, alignment
=alignment
,
306 byte_order
=byte_order
,
307 is_signed
=is_signed
, base
=base
,
309 mapped_clock_class
=mapped_clock_class
)
311 utils
._check
_type
(int_field_class
, IntegerFieldClass
)
312 ptr
= native_bt
.field_class_enumeration_create(int_field_class
._ptr
)
313 self
._check
_create
_status
(ptr
)
314 _FieldClass
.__init
__(self
, ptr
)
317 def integer_field_class(self
):
318 ptr
= native_bt
.field_class_enumeration_get_container_type(self
._ptr
)
320 return _create_from_ptr(ptr
)
324 return self
.integer_field_class
.size
328 return self
.integer_field_class
.alignment
331 def alignment(self
, alignment
):
332 self
.integer_field_class
.alignment
= alignment
335 def byte_order(self
):
336 return self
.integer_field_class
.byte_order
339 def byte_order(self
, byte_order
):
340 self
.integer_field_class
.byte_order
= byte_order
344 return self
.integer_field_class
.is_signed
347 def is_signed(self
, is_signed
):
348 self
.integer_field_class
.is_signed
= is_signed
352 return self
.integer_field_class
.base
355 def base(self
, base
):
356 self
.integer_field_class
.base
= base
360 return self
.integer_field_class
.encoding
363 def encoding(self
, encoding
):
364 self
.integer_field_class
.encoding
= encoding
367 def mapped_clock_class(self
):
368 return self
.integer_field_class
.mapped_clock_class
370 @mapped_clock_class.setter
371 def mapped_clock_class(self
, mapped_clock_class
):
372 self
.integer_field_class
.mapped_clock_class
= mapped_clock_class
375 count
= native_bt
.field_class_enumeration_get_mapping_count(self
._ptr
)
379 def __getitem__(self
, index
):
380 utils
._check
_uint
64(index
)
382 if index
>= len(self
):
386 get_fn
= native_bt
.field_class_enumeration_get_mapping_signed
388 get_fn
= native_bt
.field_class_enumeration_get_mapping_unsigned
390 ret
, name
, lower
, upper
= get_fn(self
._ptr
, index
)
392 return _EnumerationFieldClassMapping(name
, lower
, upper
)
394 def _get_mapping_iter(self
, iter_ptr
):
395 return _EnumerationFieldClassMappingIterator(iter_ptr
, self
.is_signed
)
397 def mappings_by_name(self
, name
):
398 utils
._check
_str
(name
)
399 iter_ptr
= native_bt
.field_class_enumeration_find_mappings_by_name(self
._ptr
, name
)
400 print('iter_ptr', iter_ptr
)
401 return self
._get
_mapping
_iter
(iter_ptr
)
403 def mappings_by_value(self
, value
):
405 utils
._check
_int
64(value
)
406 iter_ptr
= native_bt
.field_class_enumeration_find_mappings_by_signed_value(self
._ptr
, value
)
408 utils
._check
_uint
64(value
)
409 iter_ptr
= native_bt
.field_class_enumeration_find_mappings_by_unsigned_value(self
._ptr
, value
)
411 return self
._get
_mapping
_iter
(iter_ptr
)
413 def add_mapping(self
, name
, lower
, upper
=None):
414 utils
._check
_str
(name
)
420 add_fn
= native_bt
.field_class_enumeration_add_mapping_signed
421 utils
._check
_int
64(lower
)
422 utils
._check
_int
64(upper
)
424 add_fn
= native_bt
.field_class_enumeration_add_mapping_unsigned
425 utils
._check
_uint
64(lower
)
426 utils
._check
_uint
64(upper
)
428 ret
= add_fn(self
._ptr
, name
, lower
, upper
)
429 utils
._handle
_ret
(ret
, "cannot add mapping to enumeration field class object")
431 def __iadd__(self
, mappings
):
432 for mapping
in mappings
:
433 self
.add_mapping(mapping
.name
, mapping
.lower
, mapping
.upper
)
438 class StringFieldClass(_FieldClass
):
441 def __init__(self
, encoding
=None):
442 ptr
= native_bt
.field_class_string_create()
443 self
._check
_create
_status
(ptr
)
444 super().__init
__(ptr
)
446 if encoding
is not None:
447 self
.encoding
= encoding
451 encoding
= native_bt
.field_class_string_get_encoding(self
._ptr
)
452 assert(encoding
>= 0)
456 def encoding(self
, encoding
):
457 utils
._check
_int
(encoding
)
458 ret
= native_bt
.field_class_string_set_encoding(self
._ptr
, encoding
)
459 utils
._handle
_ret
(ret
, "cannot set string field class object's encoding")
462 class _FieldContainer(collections
.abc
.Mapping
):
464 count
= self
._count
()
468 def __getitem__(self
, key
):
469 if not isinstance(key
, str):
470 raise TypeError("'{}' is not a 'str' object".format(key
.__class
__.__name
__))
472 ptr
= self
._get
_field
_by
_name
(key
)
477 return _create_from_ptr(ptr
)
480 return self
._ITER
_CLS
(self
)
482 def append_field(self
, name
, field_class
):
483 utils
._check
_str
(name
)
484 utils
._check
_type
(field_class
, _FieldClass
)
485 ret
= self
._add
_field
(field_class
._ptr
, name
)
486 utils
._handle
_ret
(ret
, "cannot add field to {} field class object".format(self
._NAME
.lower()))
488 def __iadd__(self
, fields
):
489 for name
, field_class
in fields
.items():
490 self
.append_field(name
, field_class
)
494 def at_index(self
, index
):
495 utils
._check
_uint
64(index
)
496 return self
._at
(index
)
499 class _StructureFieldClassFieldIterator(collections
.abc
.Iterator
):
500 def __init__(self
, struct_field_class
):
501 self
._struct
_field
_class
= struct_field_class
505 if self
._at
== len(self
._struct
_field
_class
):
508 get_fc_by_index
= native_bt
.field_class_structure_get_field_by_index
509 ret
, name
, field_class_ptr
= get_fc_by_index(self
._struct
_field
_class
._ptr
,
512 native_bt
.put(field_class_ptr
)
517 class StructureFieldClass(_FieldClass
, _FieldContainer
, _AlignmentProp
):
519 _ITER_CLS
= _StructureFieldClassFieldIterator
521 def __init__(self
, min_alignment
=None):
522 ptr
= native_bt
.field_class_structure_create()
523 self
._check
_create
_status
(ptr
)
524 super().__init
__(ptr
)
526 if min_alignment
is not None:
527 self
.min_alignment
= min_alignment
530 return native_bt
.field_class_structure_get_field_count(self
._ptr
)
532 def _get_field_by_name(self
, key
):
533 return native_bt
.field_class_structure_get_field_class_by_name(self
._ptr
, key
)
535 def _add_field(self
, ptr
, name
):
536 return native_bt
.field_class_structure_add_field(self
._ptr
, ptr
,
539 def _at(self
, index
):
540 if index
< 0 or index
>= len(self
):
543 ret
, name
, field_class_ptr
= native_bt
.field_class_structure_get_field_by_index(self
._ptr
, index
)
545 return _create_from_ptr(field_class_ptr
)
548 StructureFieldClass
.min_alignment
= property(fset
=StructureFieldClass
.alignment
.fset
)
549 StructureFieldClass
.alignment
= property(fget
=StructureFieldClass
.alignment
.fget
)
552 class _VariantFieldClassFieldIterator(collections
.abc
.Iterator
):
553 def __init__(self
, variant_field_class
):
554 self
._variant
_field
_class
= variant_field_class
558 if self
._at
== len(self
._variant
_field
_class
):
561 ret
, name
, field_class_ptr
= native_bt
.field_class_variant_get_field_by_index(self
._variant
_field
_class
._ptr
,
564 native_bt
.put(field_class_ptr
)
569 class VariantFieldClass(_FieldClass
, _FieldContainer
, _AlignmentProp
):
571 _ITER_CLS
= _VariantFieldClassFieldIterator
573 def __init__(self
, tag_name
, tag_field_class
=None):
574 utils
._check
_str
(tag_name
)
576 if tag_field_class
is None:
579 utils
._check
_type
(tag_field_class
, EnumerationFieldClass
)
580 tag_fc_ptr
= tag_field_class
._ptr
582 ptr
= native_bt
.field_class_variant_create(tag_fc_ptr
,
584 self
._check
_create
_status
(ptr
)
585 super().__init
__(ptr
)
589 tag_name
= native_bt
.field_class_variant_get_tag_name(self
._ptr
)
590 assert(tag_name
is not None)
594 def tag_name(self
, tag_name
):
595 utils
._check
_str
(tag_name
)
596 ret
= native_bt
.field_class_variant_set_tag_name(self
._ptr
, tag_name
)
597 utils
._handle
_ret
(ret
, "cannot set variant field class object's tag name")
600 def tag_field_class(self
):
601 fc_ptr
= native_bt
.field_class_variant_get_tag_type(self
._ptr
)
606 return _create_from_ptr(fc_ptr
)
609 return native_bt
.field_class_variant_get_field_count(self
._ptr
)
611 def _get_field_by_name(self
, key
):
612 return native_bt
.field_class_variant_get_field_class_by_name(self
._ptr
, key
)
614 def _add_field(self
, ptr
, name
):
615 return native_bt
.field_class_variant_add_field(self
._ptr
, ptr
, name
)
617 def _at(self
, index
):
618 if index
< 0 or index
>= len(self
):
621 ret
, name
, field_class_ptr
= native_bt
.field_class_variant_get_field_by_index(self
._ptr
, index
)
623 return _create_from_ptr(field_class_ptr
)
626 class ArrayFieldClass(_FieldClass
):
629 def __init__(self
, element_field_class
, length
):
630 utils
._check
_type
(element_field_class
, _FieldClass
)
631 utils
._check
_uint
64(length
)
632 ptr
= native_bt
.field_class_array_create(element_field_class
._ptr
, length
)
633 self
._check
_create
_status
(ptr
)
634 super().__init
__(ptr
)
638 length
= native_bt
.field_class_array_get_length(self
._ptr
)
643 def element_field_class(self
):
644 ptr
= native_bt
.field_class_array_get_element_type(self
._ptr
)
646 return _create_from_ptr(ptr
)
649 class SequenceFieldClass(_FieldClass
):
652 def __init__(self
, element_field_class
, length_name
):
653 utils
._check
_type
(element_field_class
, _FieldClass
)
654 utils
._check
_str
(length_name
)
655 ptr
= native_bt
.field_class_sequence_create(element_field_class
._ptr
,
657 self
._check
_create
_status
(ptr
)
658 super().__init
__(ptr
)
661 def length_name(self
):
662 length_name
= native_bt
.field_class_sequence_get_length_field_name(self
._ptr
)
663 assert(length_name
is not None)
667 def element_field_class(self
):
668 ptr
= native_bt
.field_class_sequence_get_element_type(self
._ptr
)
670 return _create_from_ptr(ptr
)