1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
, object, utils
7 from bt2
import field_path
as bt2_field_path
8 from bt2
import integer_range_set
as bt2_integer_range_set
9 from bt2
import value
as bt2_value
13 def _obj_type_from_field_class_ptr_template(type_map
, ptr
):
14 typeid
= native_bt
.field_class_get_type(ptr
)
15 return type_map
[typeid
]
18 def _obj_type_from_field_class_ptr(ptr
):
19 return _obj_type_from_field_class_ptr_template(_FIELD_CLASS_TYPE_TO_OBJ
, ptr
)
22 def _create_field_class_from_ptr_and_get_ref_template(type_map
, ptr
):
23 return _obj_type_from_field_class_ptr_template(
25 )._create
_from
_ptr
_and
_get
_ref
(ptr
)
28 def _create_field_class_from_ptr_and_get_ref(ptr
):
29 return _create_field_class_from_ptr_and_get_ref_template(
30 _FIELD_CLASS_TYPE_TO_OBJ
, ptr
34 def _create_field_class_from_const_ptr_and_get_ref(ptr
):
35 return _create_field_class_from_ptr_and_get_ref_template(
36 _FIELD_CLASS_TYPE_TO_CONST_OBJ
, ptr
40 class IntegerDisplayBase
:
41 BINARY
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
42 OCTAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
43 DECIMAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
44 HEXADECIMAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
47 class _FieldClassConst(object._SharedObject
):
50 native_bt
.field_class_get_ref(ptr
)
54 native_bt
.field_class_put_ref(ptr
)
56 _borrow_user_attributes_ptr
= staticmethod(
57 native_bt
.field_class_borrow_user_attributes_const
59 _create_value_from_ptr_and_get_ref
= staticmethod(
60 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
63 def _check_create_status(self
, ptr
):
65 raise bt2
._MemoryError(
66 "cannot create {} field class object".format(self
._NAME
.lower())
70 def user_attributes(self
):
71 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
72 assert ptr
is not None
73 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
76 class _FieldClass(_FieldClassConst
):
77 _borrow_user_attributes_ptr
= staticmethod(
78 native_bt
.field_class_borrow_user_attributes
80 _create_value_from_ptr_and_get_ref
= staticmethod(
81 bt2_value
._create
_from
_ptr
_and
_get
_ref
84 def _user_attributes(self
, user_attributes
):
85 value
= bt2_value
.create_value(user_attributes
)
86 utils
._check
_type
(value
, bt2_value
.MapValue
)
87 native_bt
.field_class_set_user_attributes(self
._ptr
, value
._ptr
)
89 _user_attributes
= property(fset
=_user_attributes
)
92 class _BoolFieldClassConst(_FieldClassConst
):
93 _NAME
= "Const boolean"
96 class _BoolFieldClass(_BoolFieldClassConst
, _FieldClass
):
100 class _BitArrayFieldClassConst(_FieldClassConst
):
101 _NAME
= "Const bit array"
105 length
= native_bt
.field_class_bit_array_get_length(self
._ptr
)
110 class _BitArrayFieldClass(_BitArrayFieldClassConst
, _FieldClass
):
114 class _IntegerFieldClassConst(_FieldClassConst
):
116 def field_value_range(self
):
117 size
= native_bt
.field_class_integer_get_field_value_range(self
._ptr
)
122 def preferred_display_base(self
):
123 base
= native_bt
.field_class_integer_get_preferred_display_base(self
._ptr
)
128 class _IntegerFieldClass(_FieldClass
, _IntegerFieldClassConst
):
129 def _field_value_range(self
, size
):
130 if size
< 1 or size
> 64:
131 raise ValueError("Value is outside valid range [1, 64] ({})".format(size
))
132 native_bt
.field_class_integer_set_field_value_range(self
._ptr
, size
)
134 _field_value_range
= property(fset
=_field_value_range
)
136 def _preferred_display_base(self
, base
):
137 utils
._check
_uint
64(base
)
140 IntegerDisplayBase
.BINARY
,
141 IntegerDisplayBase
.OCTAL
,
142 IntegerDisplayBase
.DECIMAL
,
143 IntegerDisplayBase
.HEXADECIMAL
,
145 raise ValueError("Display base is not a valid IntegerDisplayBase value")
147 native_bt
.field_class_integer_set_preferred_display_base(self
._ptr
, base
)
149 _preferred_display_base
= property(fset
=_preferred_display_base
)
152 class _UnsignedIntegerFieldClassConst(_IntegerFieldClassConst
, _FieldClassConst
):
153 _NAME
= "Const unsigned integer"
156 class _UnsignedIntegerFieldClass(
157 _UnsignedIntegerFieldClassConst
, _IntegerFieldClass
, _FieldClass
159 _NAME
= "Unsigned integer"
162 class _SignedIntegerFieldClassConst(_IntegerFieldClassConst
, _FieldClassConst
):
163 _NAME
= "Const signed integer"
166 class _SignedIntegerFieldClass(
167 _SignedIntegerFieldClassConst
, _IntegerFieldClass
, _FieldClass
169 _NAME
= "Signed integer"
172 class _RealFieldClassConst(_FieldClassConst
):
176 class _SinglePrecisionRealFieldClassConst(_RealFieldClassConst
):
177 _NAME
= "Const single-precision real"
180 class _DoublePrecisionRealFieldClassConst(_RealFieldClassConst
):
181 _NAME
= "Const double-precision real"
184 class _RealFieldClass(_FieldClass
, _RealFieldClassConst
):
188 class _SinglePrecisionRealFieldClass(_RealFieldClass
):
189 _NAME
= "Single-precision real"
192 class _DoublePrecisionRealFieldClass(_RealFieldClass
):
193 _NAME
= "Double-precision real"
196 # an enumeration field class mapping does not have a reference count, so
197 # we copy the properties here to avoid eventual memory access errors.
198 class _EnumerationFieldClassMapping
:
199 def __init__(self
, mapping_ptr
):
200 base_mapping_ptr
= self
._as
_enumeration
_field
_class
_mapping
_ptr
(mapping_ptr
)
201 self
._label
= native_bt
.field_class_enumeration_mapping_get_label(
204 assert self
._label
is not None
205 ranges_ptr
= self
._mapping
_borrow
_ranges
_ptr
(mapping_ptr
)
206 assert ranges_ptr
is not None
207 self
._ranges
= self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(ranges_ptr
)
218 class _UnsignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping
):
219 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
220 _as_enumeration_field_class_mapping_ptr
= staticmethod(
221 native_bt
.field_class_enumeration_unsigned_mapping_as_mapping_const
223 _mapping_borrow_ranges_ptr
= staticmethod(
224 native_bt
.field_class_enumeration_unsigned_mapping_borrow_ranges_const
228 class _SignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping
):
229 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
230 _as_enumeration_field_class_mapping_ptr
= staticmethod(
231 native_bt
.field_class_enumeration_signed_mapping_as_mapping_const
233 _mapping_borrow_ranges_ptr
= staticmethod(
234 native_bt
.field_class_enumeration_signed_mapping_borrow_ranges_const
238 class _EnumerationFieldClassConst(_IntegerFieldClassConst
, collections
.abc
.Mapping
):
240 count
= native_bt
.field_class_enumeration_get_mapping_count(self
._ptr
)
244 def mappings_for_value(self
, value
):
245 self
._check
_int
_type
(value
)
247 status
, labels
= self
._get
_mapping
_labels
_for
_value
(self
._ptr
, value
)
248 utils
._handle
_func
_status
(
249 status
, "cannot get mapping labels for value {}".format(value
)
251 return [self
[label
] for label
in labels
]
254 for idx
in range(len(self
)):
255 mapping_ptr
= self
._borrow
_mapping
_ptr
_by
_index
(self
._ptr
, idx
)
256 yield self
._mapping
_pycls
(mapping_ptr
).label
258 def __getitem__(self
, label
):
259 utils
._check
_str
(label
)
260 mapping_ptr
= self
._borrow
_mapping
_ptr
_by
_label
(self
._ptr
, label
)
262 if mapping_ptr
is None:
263 raise KeyError(label
)
265 return self
._mapping
_pycls
(mapping_ptr
)
268 class _EnumerationFieldClass(_EnumerationFieldClassConst
, _IntegerFieldClass
):
269 def add_mapping(self
, label
, ranges
):
270 utils
._check
_str
(label
)
271 utils
._check
_type
(ranges
, self
._range
_set
_pycls
)
274 raise ValueError("duplicate mapping label '{}'".format(label
))
276 status
= self
._add
_mapping
(self
._ptr
, label
, ranges
._ptr
)
277 utils
._handle
_func
_status
(
278 status
, "cannot add mapping to enumeration field class object"
281 def __iadd__(self
, mappings
):
282 for label
, ranges
in mappings
:
283 self
.add_mapping(label
, ranges
)
288 class _UnsignedEnumerationFieldClassConst(
289 _EnumerationFieldClassConst
, _UnsignedIntegerFieldClassConst
291 _NAME
= "Const unsigned enumeration"
292 _borrow_mapping_ptr_by_label
= staticmethod(
293 native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_label_const
295 _borrow_mapping_ptr_by_index
= staticmethod(
296 native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_index_const
298 _mapping_pycls
= property(lambda _
: _UnsignedEnumerationFieldClassMappingConst
)
299 _get_mapping_labels_for_value
= staticmethod(
300 native_bt
.field_class_enumeration_unsigned_get_mapping_labels_for_value
302 _check_int_type
= staticmethod(utils
._check
_uint
64)
305 class _UnsignedEnumerationFieldClass(
306 _UnsignedEnumerationFieldClassConst
,
307 _EnumerationFieldClass
,
308 _UnsignedIntegerFieldClass
,
310 _NAME
= "Unsigned enumeration"
311 _range_set_pycls
= bt2_integer_range_set
.UnsignedIntegerRangeSet
312 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_unsigned_add_mapping
)
315 class _SignedEnumerationFieldClassConst(
316 _EnumerationFieldClassConst
, _SignedIntegerFieldClassConst
318 _NAME
= "Const signed enumeration"
319 _borrow_mapping_ptr_by_label
= staticmethod(
320 native_bt
.field_class_enumeration_signed_borrow_mapping_by_label_const
322 _borrow_mapping_ptr_by_index
= staticmethod(
323 native_bt
.field_class_enumeration_signed_borrow_mapping_by_index_const
325 _mapping_pycls
= property(lambda _
: _SignedEnumerationFieldClassMappingConst
)
326 _get_mapping_labels_for_value
= staticmethod(
327 native_bt
.field_class_enumeration_signed_get_mapping_labels_for_value
329 _check_int_type
= staticmethod(utils
._check
_int
64)
332 class _SignedEnumerationFieldClass(
333 _SignedEnumerationFieldClassConst
, _EnumerationFieldClass
, _SignedIntegerFieldClass
335 _NAME
= "Signed enumeration"
336 _range_set_pycls
= bt2_integer_range_set
.SignedIntegerRangeSet
337 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_signed_add_mapping
)
340 class _StringFieldClassConst(_FieldClassConst
):
341 _NAME
= "Const string"
344 class _StringFieldClass(_StringFieldClassConst
, _FieldClass
):
348 class _StructureFieldClassMemberConst
:
349 _create_field_class_from_ptr_and_get_ref
= staticmethod(
350 _create_field_class_from_const_ptr_and_get_ref
352 _borrow_field_class_ptr
= staticmethod(
353 native_bt
.field_class_structure_member_borrow_field_class_const
355 _borrow_user_attributes_ptr
= staticmethod(
356 native_bt
.field_class_structure_member_borrow_user_attributes_const
358 _create_value_from_ptr_and_get_ref
= staticmethod(
359 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
362 def __init__(self
, owning_struct_fc
, member_ptr
):
363 # this field class owns the member; keeping it here maintains
364 # the member alive as members are not shared objects
365 self
._owning
_struct
_fc
= owning_struct_fc
366 self
._ptr
= member_ptr
370 name
= native_bt
.field_class_structure_member_get_name(self
._ptr
)
371 assert name
is not None
375 def field_class(self
):
376 fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
377 assert fc_ptr
is not None
378 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
381 def user_attributes(self
):
382 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
383 assert ptr
is not None
384 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
387 class _StructureFieldClassMember(_StructureFieldClassMemberConst
):
388 _borrow_field_class_ptr
= staticmethod(
389 native_bt
.field_class_structure_member_borrow_field_class
391 _borrow_user_attributes_ptr
= staticmethod(
392 native_bt
.field_class_structure_member_borrow_user_attributes
394 _create_field_class_from_ptr_and_get_ref
= staticmethod(
395 _create_field_class_from_ptr_and_get_ref
397 _create_value_from_ptr_and_get_ref
= staticmethod(
398 bt2_value
._create
_from
_ptr
_and
_get
_ref
401 def _user_attributes(self
, user_attributes
):
402 value
= bt2_value
.create_value(user_attributes
)
403 utils
._check
_type
(value
, bt2_value
.MapValue
)
404 native_bt
.field_class_structure_member_set_user_attributes(
405 self
._ptr
, value
._ptr
408 _user_attributes
= property(fset
=_user_attributes
)
411 class _StructureFieldClassConst(_FieldClassConst
, collections
.abc
.Mapping
):
412 _NAME
= "Const structure"
413 _borrow_member_ptr_by_index
= staticmethod(
414 native_bt
.field_class_structure_borrow_member_by_index_const
416 _borrow_member_ptr_by_name
= staticmethod(
417 native_bt
.field_class_structure_borrow_member_by_name_const
419 _structure_member_field_class_pycls
= property(
420 lambda _
: _StructureFieldClassMemberConst
424 count
= native_bt
.field_class_structure_get_member_count(self
._ptr
)
428 def __getitem__(self
, key
):
429 if not isinstance(key
, str):
431 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
434 member_ptr
= self
._borrow
_member
_ptr
_by
_name
(self
._ptr
, key
)
436 if member_ptr
is None:
439 return self
._structure
_member
_field
_class
_pycls
(self
, member_ptr
)
442 for idx
in range(len(self
)):
443 member_ptr
= self
._borrow
_member
_ptr
_by
_index
(self
._ptr
, idx
)
444 assert member_ptr
is not None
445 yield native_bt
.field_class_structure_member_get_name(member_ptr
)
447 def member_at_index(self
, index
):
448 utils
._check
_uint
64(index
)
450 if index
>= len(self
):
453 member_ptr
= self
._borrow
_member
_ptr
_by
_index
(self
._ptr
, index
)
454 assert member_ptr
is not None
455 return self
._structure
_member
_field
_class
_pycls
(self
, member_ptr
)
458 class _StructureFieldClass(_StructureFieldClassConst
, _FieldClass
):
460 _borrow_member_by_index
= staticmethod(
461 native_bt
.field_class_structure_borrow_member_by_index
463 _borrow_member_ptr_by_name
= staticmethod(
464 native_bt
.field_class_structure_borrow_member_by_name
466 _structure_member_field_class_pycls
= property(lambda _
: _StructureFieldClassMember
)
468 def append_member(self
, name
, field_class
, user_attributes
=None):
469 utils
._check
_str
(name
)
470 utils
._check
_type
(field_class
, _FieldClass
)
473 raise ValueError("duplicate member name '{}'".format(name
))
475 user_attributes_value
= None
477 if user_attributes
is not None:
478 # check now that user attributes are valid
479 user_attributes_value
= bt2
.create_value(user_attributes
)
481 status
= native_bt
.field_class_structure_append_member(
482 self
._ptr
, name
, field_class
._ptr
484 utils
._handle
_func
_status
(
485 status
, "cannot append member to structure field class object"
488 if user_attributes
is not None:
489 self
[name
]._user
_attributes
= user_attributes_value
491 def __iadd__(self
, members
):
492 for name
, field_class
in members
:
493 self
.append_member(name
, field_class
)
498 class _OptionFieldClassConst(_FieldClassConst
):
499 _NAME
= "Const option"
500 _create_field_class_from_ptr_and_get_ref
= staticmethod(
501 _create_field_class_from_const_ptr_and_get_ref
503 _borrow_field_class_ptr
= staticmethod(
504 native_bt
.field_class_option_borrow_field_class_const
508 def field_class(self
):
509 elem_fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
510 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(elem_fc_ptr
)
513 class _OptionWithSelectorFieldClassConst(_OptionFieldClassConst
):
514 _NAME
= "Const option (with selector)"
517 def selector_field_path(self
):
518 ptr
= native_bt
.field_class_option_with_selector_field_borrow_selector_field_path_const(
524 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
527 class _OptionWithBoolSelectorFieldClassConst(_OptionWithSelectorFieldClassConst
):
528 _NAME
= "Const option (with boolean selector)"
531 def selector_is_reversed(self
):
533 native_bt
.field_class_option_with_selector_field_bool_selector_is_reversed(
539 class _OptionWithIntegerSelectorFieldClassConst(_OptionWithSelectorFieldClassConst
):
540 _NAME
= "Const option (with integer selector)"
544 range_set_ptr
= self
._borrow
_selector
_ranges
_ptr
(self
._ptr
)
545 assert range_set_ptr
is not None
546 return self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
549 class _OptionWithUnsignedIntegerSelectorFieldClassConst(
550 _OptionWithIntegerSelectorFieldClassConst
552 _NAME
= "Const option (with unsigned integer selector)"
553 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
554 _borrow_selector_ranges_ptr
= staticmethod(
555 native_bt
.field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const
559 class _OptionWithSignedIntegerSelectorFieldClassConst(
560 _OptionWithIntegerSelectorFieldClassConst
562 _NAME
= "Const option (with signed integer selector)"
563 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
564 _borrow_selector_ranges_ptr
= staticmethod(
565 native_bt
.field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const
569 class _OptionFieldClass(_OptionFieldClassConst
, _FieldClass
):
571 _borrow_field_class_ptr
= staticmethod(
572 native_bt
.field_class_option_borrow_field_class
574 _create_field_class_from_ptr_and_get_ref
= staticmethod(
575 _create_field_class_from_ptr_and_get_ref
579 class _OptionWithSelectorFieldClass(
580 _OptionWithSelectorFieldClassConst
, _OptionFieldClass
582 _NAME
= "Option (with selector)"
585 class _OptionWithBoolSelectorFieldClass(
586 _OptionWithBoolSelectorFieldClassConst
, _OptionWithSelectorFieldClass
588 _NAME
= "Option (with boolean selector)"
590 def _selector_is_reversed(self
, selector_is_reversed
):
591 utils
._check
_bool
(selector_is_reversed
)
592 native_bt
.field_class_option_with_selector_field_bool_set_selector_is_reversed(
593 self
._ptr
, selector_is_reversed
596 _selector_is_reversed
= property(fset
=_selector_is_reversed
)
599 class _OptionWithIntegerSelectorFieldClass(
600 _OptionWithIntegerSelectorFieldClassConst
, _OptionWithSelectorFieldClass
602 _NAME
= "Option (with integer selector)"
605 class _OptionWithUnsignedIntegerSelectorFieldClass(
606 _OptionWithUnsignedIntegerSelectorFieldClassConst
,
607 _OptionWithIntegerSelectorFieldClass
,
609 _NAME
= "Option (with unsigned integer selector)"
612 class _OptionWithSignedIntegerSelectorFieldClass(
613 _OptionWithSignedIntegerSelectorFieldClassConst
,
614 _OptionWithIntegerSelectorFieldClass
,
616 _NAME
= "Option (with signed integer selector)"
619 class _VariantFieldClassOptionConst
:
620 _create_field_class_from_ptr_and_get_ref
= staticmethod(
621 _create_field_class_from_const_ptr_and_get_ref
623 _borrow_field_class_ptr
= staticmethod(
624 native_bt
.field_class_variant_option_borrow_field_class_const
626 _borrow_user_attributes_ptr
= staticmethod(
627 native_bt
.field_class_variant_option_borrow_user_attributes_const
629 _create_value_from_ptr_and_get_ref
= staticmethod(
630 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
633 def __init__(self
, owning_var_fc
, option_ptr
):
634 # this field class owns the option; keeping it here maintains
635 # the option alive as options are not shared objects
636 self
._owning
_var
_fc
= owning_var_fc
637 self
._ptr
= option_ptr
641 name
= native_bt
.field_class_variant_option_get_name(self
._ptr
)
642 assert name
is not None
646 def field_class(self
):
647 fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
648 assert fc_ptr
is not None
649 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
652 def user_attributes(self
):
653 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
654 assert ptr
is not None
655 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
658 class _VariantFieldClassOption(_VariantFieldClassOptionConst
):
659 _create_field_class_from_ptr_and_get_ref
= staticmethod(
660 _create_field_class_from_ptr_and_get_ref
662 _borrow_field_class_ptr
= staticmethod(
663 native_bt
.field_class_variant_option_borrow_field_class
665 _borrow_user_attributes_ptr
= staticmethod(
666 native_bt
.field_class_variant_option_borrow_user_attributes
668 _create_value_from_ptr_and_get_ref
= staticmethod(
669 bt2_value
._create
_from
_ptr
_and
_get
_ref
672 def _user_attributes(self
, user_attributes
):
673 value
= bt2_value
.create_value(user_attributes
)
674 utils
._check
_type
(value
, bt2_value
.MapValue
)
675 native_bt
.field_class_variant_option_set_user_attributes(self
._ptr
, value
._ptr
)
677 _user_attributes
= property(fset
=_user_attributes
)
680 class _VariantFieldClassWithIntegerSelectorOptionConst(_VariantFieldClassOptionConst
):
681 def __init__(self
, owning_var_fc
, spec_opt_ptr
):
682 self
._spec
_ptr
= spec_opt_ptr
683 super().__init
__(owning_var_fc
, self
._as
_option
_ptr
(spec_opt_ptr
))
687 range_set_ptr
= self
._borrow
_ranges
_ptr
(self
._spec
_ptr
)
688 assert range_set_ptr
is not None
689 return self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
692 class _VariantFieldClassWithIntegerSelectorOption(
693 _VariantFieldClassWithIntegerSelectorOptionConst
, _VariantFieldClassOption
698 class _VariantFieldClassWithSignedIntegerSelectorOptionConst(
699 _VariantFieldClassWithIntegerSelectorOptionConst
701 _as_option_ptr
= staticmethod(
702 native_bt
.field_class_variant_with_selector_field_integer_signed_option_as_option_const
704 _borrow_ranges_ptr
= staticmethod(
705 native_bt
.field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const
707 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
710 class _VariantFieldClassWithSignedIntegerSelectorOption(
711 _VariantFieldClassWithSignedIntegerSelectorOptionConst
,
712 _VariantFieldClassWithIntegerSelectorOption
,
717 class _VariantFieldClassWithUnsignedIntegerSelectorOptionConst(
718 _VariantFieldClassWithIntegerSelectorOptionConst
720 _as_option_ptr
= staticmethod(
721 native_bt
.field_class_variant_with_selector_field_integer_unsigned_option_as_option_const
723 _borrow_ranges_ptr
= staticmethod(
724 native_bt
.field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const
726 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
729 class _VariantFieldClassWithUnsignedIntegerSelectorOption(
730 _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
,
731 _VariantFieldClassWithIntegerSelectorOption
,
736 class _VariantFieldClassConst(_FieldClassConst
, collections
.abc
.Mapping
):
737 _NAME
= "Const variant"
738 _borrow_option_ptr_by_name
= staticmethod(
739 native_bt
.field_class_variant_borrow_option_by_name_const
741 _borrow_option_ptr_by_index
= staticmethod(
742 native_bt
.field_class_variant_borrow_option_by_index_const
744 _variant_option_pycls
= _VariantFieldClassOptionConst
747 def _as_option_ptr(opt_ptr
):
750 def _create_option_from_ptr(self
, opt_ptr
):
751 return self
._variant
_option
_pycls
(self
, opt_ptr
)
754 count
= native_bt
.field_class_variant_get_option_count(self
._ptr
)
758 def __getitem__(self
, key
):
759 if not isinstance(key
, str):
761 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
764 opt_ptr
= self
._borrow
_option
_ptr
_by
_name
(self
._ptr
, key
)
769 return self
._create
_option
_from
_ptr
(opt_ptr
)
772 for idx
in range(len(self
)):
773 opt_ptr
= self
._borrow
_option
_ptr
_by
_index
(self
._ptr
, idx
)
774 assert opt_ptr
is not None
775 base_opt_ptr
= self
._as
_option
_ptr
(opt_ptr
)
776 yield native_bt
.field_class_variant_option_get_name(base_opt_ptr
)
778 def option_at_index(self
, index
):
779 utils
._check
_uint
64(index
)
781 if index
>= len(self
):
784 opt_ptr
= self
._borrow
_option
_ptr
_by
_index
(self
._ptr
, index
)
785 assert opt_ptr
is not None
786 return self
._create
_option
_from
_ptr
(opt_ptr
)
789 class _VariantFieldClass(_VariantFieldClassConst
, _FieldClass
, collections
.abc
.Mapping
):
791 _borrow_option_ptr_by_name
= staticmethod(
792 native_bt
.field_class_variant_borrow_option_by_name
794 _borrow_option_ptr_by_index
= staticmethod(
795 native_bt
.field_class_variant_borrow_option_by_index
797 _variant_option_pycls
= _VariantFieldClassOption
800 class _VariantFieldClassWithoutSelectorConst(_VariantFieldClassConst
):
801 _NAME
= "Const variant (without selector)"
804 class _VariantFieldClassWithoutSelector(
805 _VariantFieldClassWithoutSelectorConst
, _VariantFieldClass
807 _NAME
= "Variant (without selector)"
809 def append_option(self
, name
, field_class
, user_attributes
=None):
810 utils
._check
_str
(name
)
811 utils
._check
_type
(field_class
, _FieldClass
)
814 raise ValueError("duplicate option name '{}'".format(name
))
816 user_attributes_value
= None
818 if user_attributes
is not None:
819 # check now that user attributes are valid
820 user_attributes_value
= bt2
.create_value(user_attributes
)
822 status
= native_bt
.field_class_variant_without_selector_append_option(
823 self
._ptr
, name
, field_class
._ptr
825 utils
._handle
_func
_status
(
826 status
, "cannot append option to variant field class object"
829 if user_attributes
is not None:
830 self
[name
]._user
_attributes
= user_attributes_value
832 def __iadd__(self
, options
):
833 for name
, field_class
in options
:
834 self
.append_option(name
, field_class
)
839 class _VariantFieldClassWithIntegerSelectorConst(_VariantFieldClassConst
):
840 _NAME
= "Const variant (with selector)"
843 def selector_field_path(self
):
844 ptr
= native_bt
.field_class_variant_with_selector_field_borrow_selector_field_path_const(
851 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
854 class _VariantFieldClassWithIntegerSelector(
855 _VariantFieldClassWithIntegerSelectorConst
, _VariantFieldClass
857 _NAME
= "Variant (with selector)"
859 def append_option(self
, name
, field_class
, ranges
, user_attributes
=None):
860 utils
._check
_str
(name
)
861 utils
._check
_type
(field_class
, _FieldClass
)
862 utils
._check
_type
(ranges
, self
._variant
_option
_pycls
._range
_set
_pycls
)
865 raise ValueError("duplicate option name '{}'".format(name
))
868 raise ValueError("range set is empty")
870 user_attributes_value
= None
872 if user_attributes
is not None:
873 # check now that user attributes are valid
874 user_attributes_value
= bt2
.create_value(user_attributes
)
876 # TODO: check overlaps (precondition of self._append_option())
878 status
= self
._append
_option
(self
._ptr
, name
, field_class
._ptr
, ranges
._ptr
)
879 utils
._handle
_func
_status
(
880 status
, "cannot append option to variant field class object"
883 if user_attributes
is not None:
884 self
[name
]._user
_attributes
= user_attributes_value
886 def __iadd__(self
, options
):
887 for name
, field_class
, ranges
in options
:
888 self
.append_option(name
, field_class
, ranges
)
893 class _VariantFieldClassWithUnsignedIntegerSelectorConst(
894 _VariantFieldClassWithIntegerSelectorConst
896 _NAME
= "Const variant (with unsigned integer selector)"
897 _borrow_option_ptr_by_name
= staticmethod(
898 native_bt
.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_name_const
900 _borrow_option_ptr_by_index
= staticmethod(
901 native_bt
.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const
903 _variant_option_pycls
= _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
904 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
907 class _VariantFieldClassWithUnsignedIntegerSelector(
908 _VariantFieldClassWithUnsignedIntegerSelectorConst
,
909 _VariantFieldClassWithIntegerSelector
,
911 _NAME
= "Variant (with unsigned integer selector)"
912 _variant_option_pycls
= _VariantFieldClassWithUnsignedIntegerSelectorOption
913 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
914 _append_option
= staticmethod(
915 native_bt
.field_class_variant_with_selector_field_integer_unsigned_append_option
919 class _VariantFieldClassWithSignedIntegerSelectorConst(
920 _VariantFieldClassWithIntegerSelectorConst
922 _NAME
= "Const variant (with signed integer selector)"
923 _borrow_option_ptr_by_name
= staticmethod(
924 native_bt
.field_class_variant_with_selector_field_integer_signed_borrow_option_by_name_const
926 _borrow_option_ptr_by_index
= staticmethod(
927 native_bt
.field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const
929 _variant_option_pycls
= _VariantFieldClassWithSignedIntegerSelectorOptionConst
930 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
933 class _VariantFieldClassWithSignedIntegerSelector(
934 _VariantFieldClassWithSignedIntegerSelectorConst
,
935 _VariantFieldClassWithIntegerSelector
,
937 _NAME
= "Variant (with signed integer selector)"
938 _variant_option_pycls
= _VariantFieldClassWithSignedIntegerSelectorOption
939 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
940 _append_option
= staticmethod(
941 native_bt
.field_class_variant_with_selector_field_integer_signed_append_option
945 class _ArrayFieldClassConst(_FieldClassConst
):
946 _create_field_class_from_ptr_and_get_ref
= staticmethod(
947 _create_field_class_from_const_ptr_and_get_ref
949 _borrow_element_field_class
= staticmethod(
950 native_bt
.field_class_array_borrow_element_field_class_const
954 def element_field_class(self
):
955 elem_fc_ptr
= self
._borrow
_element
_field
_class
(self
._ptr
)
956 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(elem_fc_ptr
)
959 class _ArrayFieldClass(_ArrayFieldClassConst
, _FieldClass
):
960 _create_field_class_from_ptr_and_get_ref
= staticmethod(
961 _create_field_class_from_ptr_and_get_ref
963 _borrow_element_field_class
= staticmethod(
964 native_bt
.field_class_array_borrow_element_field_class
968 class _StaticArrayFieldClassConst(_ArrayFieldClassConst
):
969 _NAME
= "Const static array"
973 return native_bt
.field_class_array_static_get_length(self
._ptr
)
976 class _StaticArrayFieldClass(_StaticArrayFieldClassConst
, _ArrayFieldClass
):
977 _NAME
= "Static array"
980 class _DynamicArrayFieldClassConst(_ArrayFieldClassConst
):
981 _NAME
= "Const dynamic array"
984 class _DynamicArrayWithLengthFieldFieldClassConst(_DynamicArrayFieldClassConst
):
985 _NAME
= "Const dynamic array (with length field)"
988 def length_field_path(self
):
989 ptr
= native_bt
.field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
995 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
998 class _DynamicArrayFieldClass(_DynamicArrayFieldClassConst
, _ArrayFieldClass
):
999 _NAME
= "Dynamic array"
1002 class _DynamicArrayWithLengthFieldFieldClass(
1003 _DynamicArrayWithLengthFieldFieldClassConst
, _DynamicArrayFieldClass
1005 _NAME
= "Dynamic array (with length field)"
1008 _FIELD_CLASS_TYPE_TO_CONST_OBJ
= {
1009 native_bt
.FIELD_CLASS_TYPE_BOOL
: _BoolFieldClassConst
,
1010 native_bt
.FIELD_CLASS_TYPE_BIT_ARRAY
: _BitArrayFieldClassConst
,
1011 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClassConst
,
1012 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClassConst
,
1013 native_bt
.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
: _SinglePrecisionRealFieldClassConst
,
1014 native_bt
.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
: _DoublePrecisionRealFieldClassConst
,
1015 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClassConst
,
1016 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClassConst
,
1017 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClassConst
,
1018 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClassConst
,
1019 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClassConst
,
1020 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
: _DynamicArrayFieldClassConst
,
1021 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
: _DynamicArrayWithLengthFieldFieldClassConst
,
1022 native_bt
.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
: _OptionFieldClassConst
,
1023 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
: _OptionWithBoolSelectorFieldClassConst
,
1024 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithUnsignedIntegerSelectorFieldClassConst
,
1025 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithSignedIntegerSelectorFieldClassConst
,
1026 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
: _VariantFieldClassWithoutSelectorConst
,
1027 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithUnsignedIntegerSelectorConst
,
1028 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithSignedIntegerSelectorConst
,
1031 _FIELD_CLASS_TYPE_TO_OBJ
= {
1032 native_bt
.FIELD_CLASS_TYPE_BOOL
: _BoolFieldClass
,
1033 native_bt
.FIELD_CLASS_TYPE_BIT_ARRAY
: _BitArrayFieldClass
,
1034 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClass
,
1035 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClass
,
1036 native_bt
.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
: _SinglePrecisionRealFieldClass
,
1037 native_bt
.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
: _DoublePrecisionRealFieldClass
,
1038 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClass
,
1039 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClass
,
1040 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClass
,
1041 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClass
,
1042 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClass
,
1043 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
: _DynamicArrayFieldClass
,
1044 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
: _DynamicArrayWithLengthFieldFieldClass
,
1045 native_bt
.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
: _OptionFieldClass
,
1046 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
: _OptionWithBoolSelectorFieldClass
,
1047 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithUnsignedIntegerSelectorFieldClass
,
1048 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithSignedIntegerSelectorFieldClass
,
1049 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
: _VariantFieldClassWithoutSelector
,
1050 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithUnsignedIntegerSelector
,
1051 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithSignedIntegerSelector
,