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
):
48 _get_ref
= staticmethod(native_bt
.field_class_get_ref
)
49 _put_ref
= staticmethod(native_bt
.field_class_put_ref
)
50 _borrow_user_attributes_ptr
= staticmethod(
51 native_bt
.field_class_borrow_user_attributes_const
53 _create_value_from_ptr_and_get_ref
= staticmethod(
54 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
57 def _check_create_status(self
, ptr
):
59 raise bt2
._MemoryError(
60 "cannot create {} field class object".format(self
._NAME
.lower())
64 def user_attributes(self
):
65 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
66 assert ptr
is not None
67 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
70 class _FieldClass(_FieldClassConst
):
71 _borrow_user_attributes_ptr
= staticmethod(
72 native_bt
.field_class_borrow_user_attributes
74 _create_value_from_ptr_and_get_ref
= staticmethod(
75 bt2_value
._create
_from
_ptr
_and
_get
_ref
78 def _user_attributes(self
, user_attributes
):
79 value
= bt2_value
.create_value(user_attributes
)
80 utils
._check
_type
(value
, bt2_value
.MapValue
)
81 native_bt
.field_class_set_user_attributes(self
._ptr
, value
._ptr
)
83 _user_attributes
= property(fset
=_user_attributes
)
86 class _BoolFieldClassConst(_FieldClassConst
):
87 _NAME
= "Const boolean"
90 class _BoolFieldClass(_BoolFieldClassConst
, _FieldClass
):
94 class _BitArrayFieldClassConst(_FieldClassConst
):
95 _NAME
= "Const bit array"
99 length
= native_bt
.field_class_bit_array_get_length(self
._ptr
)
104 class _BitArrayFieldClass(_BitArrayFieldClassConst
, _FieldClass
):
108 class _IntegerFieldClassConst(_FieldClassConst
):
110 def field_value_range(self
):
111 size
= native_bt
.field_class_integer_get_field_value_range(self
._ptr
)
116 def preferred_display_base(self
):
117 base
= native_bt
.field_class_integer_get_preferred_display_base(self
._ptr
)
122 class _IntegerFieldClass(_FieldClass
, _IntegerFieldClassConst
):
123 def _field_value_range(self
, size
):
124 if size
< 1 or size
> 64:
125 raise ValueError("Value is outside valid range [1, 64] ({})".format(size
))
126 native_bt
.field_class_integer_set_field_value_range(self
._ptr
, size
)
128 _field_value_range
= property(fset
=_field_value_range
)
130 def _preferred_display_base(self
, base
):
131 utils
._check
_uint
64(base
)
134 IntegerDisplayBase
.BINARY
,
135 IntegerDisplayBase
.OCTAL
,
136 IntegerDisplayBase
.DECIMAL
,
137 IntegerDisplayBase
.HEXADECIMAL
,
139 raise ValueError("Display base is not a valid IntegerDisplayBase value")
141 native_bt
.field_class_integer_set_preferred_display_base(self
._ptr
, base
)
143 _preferred_display_base
= property(fset
=_preferred_display_base
)
146 class _UnsignedIntegerFieldClassConst(_IntegerFieldClassConst
, _FieldClassConst
):
147 _NAME
= "Const unsigned integer"
150 class _UnsignedIntegerFieldClass(
151 _UnsignedIntegerFieldClassConst
, _IntegerFieldClass
, _FieldClass
153 _NAME
= "Unsigned integer"
156 class _SignedIntegerFieldClassConst(_IntegerFieldClassConst
, _FieldClassConst
):
157 _NAME
= "Const signed integer"
160 class _SignedIntegerFieldClass(
161 _SignedIntegerFieldClassConst
, _IntegerFieldClass
, _FieldClass
163 _NAME
= "Signed integer"
166 class _RealFieldClassConst(_FieldClassConst
):
170 class _SinglePrecisionRealFieldClassConst(_RealFieldClassConst
):
171 _NAME
= "Const single-precision real"
174 class _DoublePrecisionRealFieldClassConst(_RealFieldClassConst
):
175 _NAME
= "Const double-precision real"
178 class _RealFieldClass(_FieldClass
, _RealFieldClassConst
):
182 class _SinglePrecisionRealFieldClass(_RealFieldClass
):
183 _NAME
= "Single-precision real"
186 class _DoublePrecisionRealFieldClass(_RealFieldClass
):
187 _NAME
= "Double-precision real"
190 # an enumeration field class mapping does not have a reference count, so
191 # we copy the properties here to avoid eventual memory access errors.
192 class _EnumerationFieldClassMapping
:
193 def __init__(self
, mapping_ptr
):
194 base_mapping_ptr
= self
._as
_enumeration
_field
_class
_mapping
_ptr
(mapping_ptr
)
195 self
._label
= native_bt
.field_class_enumeration_mapping_get_label(
198 assert self
._label
is not None
199 ranges_ptr
= self
._mapping
_borrow
_ranges
_ptr
(mapping_ptr
)
200 assert ranges_ptr
is not None
201 self
._ranges
= self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(ranges_ptr
)
212 class _UnsignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping
):
213 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
214 _as_enumeration_field_class_mapping_ptr
= staticmethod(
215 native_bt
.field_class_enumeration_unsigned_mapping_as_mapping_const
217 _mapping_borrow_ranges_ptr
= staticmethod(
218 native_bt
.field_class_enumeration_unsigned_mapping_borrow_ranges_const
222 class _SignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping
):
223 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
224 _as_enumeration_field_class_mapping_ptr
= staticmethod(
225 native_bt
.field_class_enumeration_signed_mapping_as_mapping_const
227 _mapping_borrow_ranges_ptr
= staticmethod(
228 native_bt
.field_class_enumeration_signed_mapping_borrow_ranges_const
232 class _EnumerationFieldClassConst(_IntegerFieldClassConst
, collections
.abc
.Mapping
):
234 count
= native_bt
.field_class_enumeration_get_mapping_count(self
._ptr
)
238 def mappings_for_value(self
, value
):
239 self
._check
_int
_type
(value
)
241 status
, labels
= self
._get
_mapping
_labels
_for
_value
(self
._ptr
, value
)
242 utils
._handle
_func
_status
(
243 status
, "cannot get mapping labels for value {}".format(value
)
245 return [self
[label
] for label
in labels
]
248 for idx
in range(len(self
)):
249 mapping_ptr
= self
._borrow
_mapping
_ptr
_by
_index
(self
._ptr
, idx
)
250 yield self
._mapping
_pycls
(mapping_ptr
).label
252 def __getitem__(self
, label
):
253 utils
._check
_str
(label
)
254 mapping_ptr
= self
._borrow
_mapping
_ptr
_by
_label
(self
._ptr
, label
)
256 if mapping_ptr
is None:
257 raise KeyError(label
)
259 return self
._mapping
_pycls
(mapping_ptr
)
262 class _EnumerationFieldClass(_EnumerationFieldClassConst
, _IntegerFieldClass
):
263 def add_mapping(self
, label
, ranges
):
264 utils
._check
_str
(label
)
265 utils
._check
_type
(ranges
, self
._range
_set
_pycls
)
268 raise ValueError("duplicate mapping label '{}'".format(label
))
270 status
= self
._add
_mapping
(self
._ptr
, label
, ranges
._ptr
)
271 utils
._handle
_func
_status
(
272 status
, "cannot add mapping to enumeration field class object"
275 def __iadd__(self
, mappings
):
276 for label
, ranges
in mappings
:
277 self
.add_mapping(label
, ranges
)
282 class _UnsignedEnumerationFieldClassConst(
283 _EnumerationFieldClassConst
, _UnsignedIntegerFieldClassConst
285 _NAME
= "Const unsigned enumeration"
286 _borrow_mapping_ptr_by_label
= staticmethod(
287 native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_label_const
289 _borrow_mapping_ptr_by_index
= staticmethod(
290 native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_index_const
292 _mapping_pycls
= property(lambda _
: _UnsignedEnumerationFieldClassMappingConst
)
293 _get_mapping_labels_for_value
= staticmethod(
294 native_bt
.field_class_enumeration_unsigned_get_mapping_labels_for_value
296 _check_int_type
= staticmethod(utils
._check
_uint
64)
299 class _UnsignedEnumerationFieldClass(
300 _UnsignedEnumerationFieldClassConst
,
301 _EnumerationFieldClass
,
302 _UnsignedIntegerFieldClass
,
304 _NAME
= "Unsigned enumeration"
305 _range_set_pycls
= bt2_integer_range_set
.UnsignedIntegerRangeSet
306 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_unsigned_add_mapping
)
309 class _SignedEnumerationFieldClassConst(
310 _EnumerationFieldClassConst
, _SignedIntegerFieldClassConst
312 _NAME
= "Const signed enumeration"
313 _borrow_mapping_ptr_by_label
= staticmethod(
314 native_bt
.field_class_enumeration_signed_borrow_mapping_by_label_const
316 _borrow_mapping_ptr_by_index
= staticmethod(
317 native_bt
.field_class_enumeration_signed_borrow_mapping_by_index_const
319 _mapping_pycls
= property(lambda _
: _SignedEnumerationFieldClassMappingConst
)
320 _get_mapping_labels_for_value
= staticmethod(
321 native_bt
.field_class_enumeration_signed_get_mapping_labels_for_value
323 _check_int_type
= staticmethod(utils
._check
_int
64)
326 class _SignedEnumerationFieldClass(
327 _SignedEnumerationFieldClassConst
, _EnumerationFieldClass
, _SignedIntegerFieldClass
329 _NAME
= "Signed enumeration"
330 _range_set_pycls
= bt2_integer_range_set
.SignedIntegerRangeSet
331 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_signed_add_mapping
)
334 class _StringFieldClassConst(_FieldClassConst
):
335 _NAME
= "Const string"
338 class _StringFieldClass(_StringFieldClassConst
, _FieldClass
):
342 class _StructureFieldClassMemberConst
:
343 _create_field_class_from_ptr_and_get_ref
= staticmethod(
344 _create_field_class_from_const_ptr_and_get_ref
346 _borrow_field_class_ptr
= staticmethod(
347 native_bt
.field_class_structure_member_borrow_field_class_const
349 _borrow_user_attributes_ptr
= staticmethod(
350 native_bt
.field_class_structure_member_borrow_user_attributes_const
352 _create_value_from_ptr_and_get_ref
= staticmethod(
353 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
356 def __init__(self
, owning_struct_fc
, member_ptr
):
357 # this field class owns the member; keeping it here maintains
358 # the member alive as members are not shared objects
359 self
._owning
_struct
_fc
= owning_struct_fc
360 self
._ptr
= member_ptr
364 name
= native_bt
.field_class_structure_member_get_name(self
._ptr
)
365 assert name
is not None
369 def field_class(self
):
370 fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
371 assert fc_ptr
is not None
372 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
375 def user_attributes(self
):
376 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
377 assert ptr
is not None
378 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
381 class _StructureFieldClassMember(_StructureFieldClassMemberConst
):
382 _borrow_field_class_ptr
= staticmethod(
383 native_bt
.field_class_structure_member_borrow_field_class
385 _borrow_user_attributes_ptr
= staticmethod(
386 native_bt
.field_class_structure_member_borrow_user_attributes
388 _create_field_class_from_ptr_and_get_ref
= staticmethod(
389 _create_field_class_from_ptr_and_get_ref
391 _create_value_from_ptr_and_get_ref
= staticmethod(
392 bt2_value
._create
_from
_ptr
_and
_get
_ref
395 def _user_attributes(self
, user_attributes
):
396 value
= bt2_value
.create_value(user_attributes
)
397 utils
._check
_type
(value
, bt2_value
.MapValue
)
398 native_bt
.field_class_structure_member_set_user_attributes(
399 self
._ptr
, value
._ptr
402 _user_attributes
= property(fset
=_user_attributes
)
405 class _StructureFieldClassConst(_FieldClassConst
, collections
.abc
.Mapping
):
406 _NAME
= "Const structure"
407 _borrow_member_ptr_by_index
= staticmethod(
408 native_bt
.field_class_structure_borrow_member_by_index_const
410 _borrow_member_ptr_by_name
= staticmethod(
411 native_bt
.field_class_structure_borrow_member_by_name_const
413 _structure_member_field_class_pycls
= property(
414 lambda _
: _StructureFieldClassMemberConst
418 count
= native_bt
.field_class_structure_get_member_count(self
._ptr
)
422 def __getitem__(self
, key
):
423 if not isinstance(key
, str):
425 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
428 member_ptr
= self
._borrow
_member
_ptr
_by
_name
(self
._ptr
, key
)
430 if member_ptr
is None:
433 return self
._structure
_member
_field
_class
_pycls
(self
, member_ptr
)
436 for idx
in range(len(self
)):
437 member_ptr
= self
._borrow
_member
_ptr
_by
_index
(self
._ptr
, idx
)
438 assert member_ptr
is not None
439 yield native_bt
.field_class_structure_member_get_name(member_ptr
)
441 def member_at_index(self
, index
):
442 utils
._check
_uint
64(index
)
444 if index
>= len(self
):
447 member_ptr
= self
._borrow
_member
_ptr
_by
_index
(self
._ptr
, index
)
448 assert member_ptr
is not None
449 return self
._structure
_member
_field
_class
_pycls
(self
, member_ptr
)
452 class _StructureFieldClass(_StructureFieldClassConst
, _FieldClass
):
454 _borrow_member_by_index
= staticmethod(
455 native_bt
.field_class_structure_borrow_member_by_index
457 _borrow_member_ptr_by_name
= staticmethod(
458 native_bt
.field_class_structure_borrow_member_by_name
460 _structure_member_field_class_pycls
= property(lambda _
: _StructureFieldClassMember
)
462 def append_member(self
, name
, field_class
, user_attributes
=None):
463 utils
._check
_str
(name
)
464 utils
._check
_type
(field_class
, _FieldClass
)
467 raise ValueError("duplicate member name '{}'".format(name
))
469 user_attributes_value
= None
471 if user_attributes
is not None:
472 # check now that user attributes are valid
473 user_attributes_value
= bt2
.create_value(user_attributes
)
475 status
= native_bt
.field_class_structure_append_member(
476 self
._ptr
, name
, field_class
._ptr
478 utils
._handle
_func
_status
(
479 status
, "cannot append member to structure field class object"
482 if user_attributes
is not None:
483 self
[name
]._user
_attributes
= user_attributes_value
485 def __iadd__(self
, members
):
486 for name
, field_class
in members
:
487 self
.append_member(name
, field_class
)
492 class _OptionFieldClassConst(_FieldClassConst
):
493 _NAME
= "Const option"
494 _create_field_class_from_ptr_and_get_ref
= staticmethod(
495 _create_field_class_from_const_ptr_and_get_ref
497 _borrow_field_class_ptr
= staticmethod(
498 native_bt
.field_class_option_borrow_field_class_const
502 def field_class(self
):
503 elem_fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
504 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(elem_fc_ptr
)
507 class _OptionWithSelectorFieldClassConst(_OptionFieldClassConst
):
508 _NAME
= "Const option (with selector)"
511 def selector_field_path(self
):
512 ptr
= native_bt
.field_class_option_with_selector_field_borrow_selector_field_path_const(
518 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
521 class _OptionWithBoolSelectorFieldClassConst(_OptionWithSelectorFieldClassConst
):
522 _NAME
= "Const option (with boolean selector)"
525 def selector_is_reversed(self
):
527 native_bt
.field_class_option_with_selector_field_bool_selector_is_reversed(
533 class _OptionWithIntegerSelectorFieldClassConst(_OptionWithSelectorFieldClassConst
):
534 _NAME
= "Const option (with integer selector)"
538 range_set_ptr
= self
._borrow
_selector
_ranges
_ptr
(self
._ptr
)
539 assert range_set_ptr
is not None
540 return self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
543 class _OptionWithUnsignedIntegerSelectorFieldClassConst(
544 _OptionWithIntegerSelectorFieldClassConst
546 _NAME
= "Const option (with unsigned integer selector)"
547 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
548 _borrow_selector_ranges_ptr
= staticmethod(
549 native_bt
.field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const
553 class _OptionWithSignedIntegerSelectorFieldClassConst(
554 _OptionWithIntegerSelectorFieldClassConst
556 _NAME
= "Const option (with signed integer selector)"
557 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
558 _borrow_selector_ranges_ptr
= staticmethod(
559 native_bt
.field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const
563 class _OptionFieldClass(_OptionFieldClassConst
, _FieldClass
):
565 _borrow_field_class_ptr
= staticmethod(
566 native_bt
.field_class_option_borrow_field_class
568 _create_field_class_from_ptr_and_get_ref
= staticmethod(
569 _create_field_class_from_ptr_and_get_ref
573 class _OptionWithSelectorFieldClass(
574 _OptionWithSelectorFieldClassConst
, _OptionFieldClass
576 _NAME
= "Option (with selector)"
579 class _OptionWithBoolSelectorFieldClass(
580 _OptionWithBoolSelectorFieldClassConst
, _OptionWithSelectorFieldClass
582 _NAME
= "Option (with boolean selector)"
584 def _selector_is_reversed(self
, selector_is_reversed
):
585 utils
._check
_bool
(selector_is_reversed
)
586 native_bt
.field_class_option_with_selector_field_bool_set_selector_is_reversed(
587 self
._ptr
, selector_is_reversed
590 _selector_is_reversed
= property(fset
=_selector_is_reversed
)
593 class _OptionWithIntegerSelectorFieldClass(
594 _OptionWithIntegerSelectorFieldClassConst
, _OptionWithSelectorFieldClass
596 _NAME
= "Option (with integer selector)"
599 class _OptionWithUnsignedIntegerSelectorFieldClass(
600 _OptionWithUnsignedIntegerSelectorFieldClassConst
,
601 _OptionWithIntegerSelectorFieldClass
,
603 _NAME
= "Option (with unsigned integer selector)"
606 class _OptionWithSignedIntegerSelectorFieldClass(
607 _OptionWithSignedIntegerSelectorFieldClassConst
,
608 _OptionWithIntegerSelectorFieldClass
,
610 _NAME
= "Option (with signed integer selector)"
613 class _VariantFieldClassOptionConst
:
614 _create_field_class_from_ptr_and_get_ref
= staticmethod(
615 _create_field_class_from_const_ptr_and_get_ref
617 _borrow_field_class_ptr
= staticmethod(
618 native_bt
.field_class_variant_option_borrow_field_class_const
620 _borrow_user_attributes_ptr
= staticmethod(
621 native_bt
.field_class_variant_option_borrow_user_attributes_const
623 _create_value_from_ptr_and_get_ref
= staticmethod(
624 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
627 def __init__(self
, owning_var_fc
, option_ptr
):
628 # this field class owns the option; keeping it here maintains
629 # the option alive as options are not shared objects
630 self
._owning
_var
_fc
= owning_var_fc
631 self
._ptr
= option_ptr
635 name
= native_bt
.field_class_variant_option_get_name(self
._ptr
)
636 assert name
is not None
640 def field_class(self
):
641 fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
642 assert fc_ptr
is not None
643 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
646 def user_attributes(self
):
647 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
648 assert ptr
is not None
649 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
652 class _VariantFieldClassOption(_VariantFieldClassOptionConst
):
653 _create_field_class_from_ptr_and_get_ref
= staticmethod(
654 _create_field_class_from_ptr_and_get_ref
656 _borrow_field_class_ptr
= staticmethod(
657 native_bt
.field_class_variant_option_borrow_field_class
659 _borrow_user_attributes_ptr
= staticmethod(
660 native_bt
.field_class_variant_option_borrow_user_attributes
662 _create_value_from_ptr_and_get_ref
= staticmethod(
663 bt2_value
._create
_from
_ptr
_and
_get
_ref
666 def _user_attributes(self
, user_attributes
):
667 value
= bt2_value
.create_value(user_attributes
)
668 utils
._check
_type
(value
, bt2_value
.MapValue
)
669 native_bt
.field_class_variant_option_set_user_attributes(self
._ptr
, value
._ptr
)
671 _user_attributes
= property(fset
=_user_attributes
)
674 class _VariantFieldClassWithIntegerSelectorOptionConst(_VariantFieldClassOptionConst
):
675 def __init__(self
, owning_var_fc
, spec_opt_ptr
):
676 self
._spec
_ptr
= spec_opt_ptr
677 super().__init
__(owning_var_fc
, self
._as
_option
_ptr
(spec_opt_ptr
))
681 range_set_ptr
= self
._borrow
_ranges
_ptr
(self
._spec
_ptr
)
682 assert range_set_ptr
is not None
683 return self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
686 class _VariantFieldClassWithIntegerSelectorOption(
687 _VariantFieldClassWithIntegerSelectorOptionConst
, _VariantFieldClassOption
692 class _VariantFieldClassWithSignedIntegerSelectorOptionConst(
693 _VariantFieldClassWithIntegerSelectorOptionConst
695 _as_option_ptr
= staticmethod(
696 native_bt
.field_class_variant_with_selector_field_integer_signed_option_as_option_const
698 _borrow_ranges_ptr
= staticmethod(
699 native_bt
.field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const
701 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
704 class _VariantFieldClassWithSignedIntegerSelectorOption(
705 _VariantFieldClassWithSignedIntegerSelectorOptionConst
,
706 _VariantFieldClassWithIntegerSelectorOption
,
711 class _VariantFieldClassWithUnsignedIntegerSelectorOptionConst(
712 _VariantFieldClassWithIntegerSelectorOptionConst
714 _as_option_ptr
= staticmethod(
715 native_bt
.field_class_variant_with_selector_field_integer_unsigned_option_as_option_const
717 _borrow_ranges_ptr
= staticmethod(
718 native_bt
.field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const
720 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
723 class _VariantFieldClassWithUnsignedIntegerSelectorOption(
724 _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
,
725 _VariantFieldClassWithIntegerSelectorOption
,
730 class _VariantFieldClassConst(_FieldClassConst
, collections
.abc
.Mapping
):
731 _NAME
= "Const variant"
732 _borrow_option_ptr_by_name
= staticmethod(
733 native_bt
.field_class_variant_borrow_option_by_name_const
735 _borrow_option_ptr_by_index
= staticmethod(
736 native_bt
.field_class_variant_borrow_option_by_index_const
738 _variant_option_pycls
= _VariantFieldClassOptionConst
741 def _as_option_ptr(opt_ptr
):
744 def _create_option_from_ptr(self
, opt_ptr
):
745 return self
._variant
_option
_pycls
(self
, opt_ptr
)
748 count
= native_bt
.field_class_variant_get_option_count(self
._ptr
)
752 def __getitem__(self
, key
):
753 if not isinstance(key
, str):
755 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
758 opt_ptr
= self
._borrow
_option
_ptr
_by
_name
(self
._ptr
, key
)
763 return self
._create
_option
_from
_ptr
(opt_ptr
)
766 for idx
in range(len(self
)):
767 opt_ptr
= self
._borrow
_option
_ptr
_by
_index
(self
._ptr
, idx
)
768 assert opt_ptr
is not None
769 base_opt_ptr
= self
._as
_option
_ptr
(opt_ptr
)
770 yield native_bt
.field_class_variant_option_get_name(base_opt_ptr
)
772 def option_at_index(self
, index
):
773 utils
._check
_uint
64(index
)
775 if index
>= len(self
):
778 opt_ptr
= self
._borrow
_option
_ptr
_by
_index
(self
._ptr
, index
)
779 assert opt_ptr
is not None
780 return self
._create
_option
_from
_ptr
(opt_ptr
)
783 class _VariantFieldClass(_VariantFieldClassConst
, _FieldClass
, collections
.abc
.Mapping
):
785 _borrow_option_ptr_by_name
= staticmethod(
786 native_bt
.field_class_variant_borrow_option_by_name
788 _borrow_option_ptr_by_index
= staticmethod(
789 native_bt
.field_class_variant_borrow_option_by_index
791 _variant_option_pycls
= _VariantFieldClassOption
794 class _VariantFieldClassWithoutSelectorConst(_VariantFieldClassConst
):
795 _NAME
= "Const variant (without selector)"
798 class _VariantFieldClassWithoutSelector(
799 _VariantFieldClassWithoutSelectorConst
, _VariantFieldClass
801 _NAME
= "Variant (without selector)"
803 def append_option(self
, name
, field_class
, user_attributes
=None):
804 utils
._check
_str
(name
)
805 utils
._check
_type
(field_class
, _FieldClass
)
808 raise ValueError("duplicate option name '{}'".format(name
))
810 user_attributes_value
= None
812 if user_attributes
is not None:
813 # check now that user attributes are valid
814 user_attributes_value
= bt2
.create_value(user_attributes
)
816 status
= native_bt
.field_class_variant_without_selector_append_option(
817 self
._ptr
, name
, field_class
._ptr
819 utils
._handle
_func
_status
(
820 status
, "cannot append option to variant field class object"
823 if user_attributes
is not None:
824 self
[name
]._user
_attributes
= user_attributes_value
826 def __iadd__(self
, options
):
827 for name
, field_class
in options
:
828 self
.append_option(name
, field_class
)
833 class _VariantFieldClassWithIntegerSelectorConst(_VariantFieldClassConst
):
834 _NAME
= "Const variant (with selector)"
837 def selector_field_path(self
):
838 ptr
= native_bt
.field_class_variant_with_selector_field_borrow_selector_field_path_const(
845 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
848 class _VariantFieldClassWithIntegerSelector(
849 _VariantFieldClassWithIntegerSelectorConst
, _VariantFieldClass
851 _NAME
= "Variant (with selector)"
853 def append_option(self
, name
, field_class
, ranges
, user_attributes
=None):
854 utils
._check
_str
(name
)
855 utils
._check
_type
(field_class
, _FieldClass
)
856 utils
._check
_type
(ranges
, self
._variant
_option
_pycls
._range
_set
_pycls
)
859 raise ValueError("duplicate option name '{}'".format(name
))
862 raise ValueError("range set is empty")
864 user_attributes_value
= None
866 if user_attributes
is not None:
867 # check now that user attributes are valid
868 user_attributes_value
= bt2
.create_value(user_attributes
)
870 # TODO: check overlaps (precondition of self._append_option())
872 status
= self
._append
_option
(self
._ptr
, name
, field_class
._ptr
, ranges
._ptr
)
873 utils
._handle
_func
_status
(
874 status
, "cannot append option to variant field class object"
877 if user_attributes
is not None:
878 self
[name
]._user
_attributes
= user_attributes_value
880 def __iadd__(self
, options
):
881 for name
, field_class
, ranges
in options
:
882 self
.append_option(name
, field_class
, ranges
)
887 class _VariantFieldClassWithUnsignedIntegerSelectorConst(
888 _VariantFieldClassWithIntegerSelectorConst
890 _NAME
= "Const variant (with unsigned integer selector)"
891 _borrow_option_ptr_by_name
= staticmethod(
892 native_bt
.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_name_const
894 _borrow_option_ptr_by_index
= staticmethod(
895 native_bt
.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const
897 _variant_option_pycls
= _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
898 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
901 class _VariantFieldClassWithUnsignedIntegerSelector(
902 _VariantFieldClassWithUnsignedIntegerSelectorConst
,
903 _VariantFieldClassWithIntegerSelector
,
905 _NAME
= "Variant (with unsigned integer selector)"
906 _variant_option_pycls
= _VariantFieldClassWithUnsignedIntegerSelectorOption
907 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
908 _append_option
= staticmethod(
909 native_bt
.field_class_variant_with_selector_field_integer_unsigned_append_option
913 class _VariantFieldClassWithSignedIntegerSelectorConst(
914 _VariantFieldClassWithIntegerSelectorConst
916 _NAME
= "Const variant (with signed integer selector)"
917 _borrow_option_ptr_by_name
= staticmethod(
918 native_bt
.field_class_variant_with_selector_field_integer_signed_borrow_option_by_name_const
920 _borrow_option_ptr_by_index
= staticmethod(
921 native_bt
.field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const
923 _variant_option_pycls
= _VariantFieldClassWithSignedIntegerSelectorOptionConst
924 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
927 class _VariantFieldClassWithSignedIntegerSelector(
928 _VariantFieldClassWithSignedIntegerSelectorConst
,
929 _VariantFieldClassWithIntegerSelector
,
931 _NAME
= "Variant (with signed integer selector)"
932 _variant_option_pycls
= _VariantFieldClassWithSignedIntegerSelectorOption
933 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
934 _append_option
= staticmethod(
935 native_bt
.field_class_variant_with_selector_field_integer_signed_append_option
939 class _ArrayFieldClassConst(_FieldClassConst
):
940 _create_field_class_from_ptr_and_get_ref
= staticmethod(
941 _create_field_class_from_const_ptr_and_get_ref
943 _borrow_element_field_class
= staticmethod(
944 native_bt
.field_class_array_borrow_element_field_class_const
948 def element_field_class(self
):
949 elem_fc_ptr
= self
._borrow
_element
_field
_class
(self
._ptr
)
950 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(elem_fc_ptr
)
953 class _ArrayFieldClass(_ArrayFieldClassConst
, _FieldClass
):
954 _create_field_class_from_ptr_and_get_ref
= staticmethod(
955 _create_field_class_from_ptr_and_get_ref
957 _borrow_element_field_class
= staticmethod(
958 native_bt
.field_class_array_borrow_element_field_class
962 class _StaticArrayFieldClassConst(_ArrayFieldClassConst
):
963 _NAME
= "Const static array"
967 return native_bt
.field_class_array_static_get_length(self
._ptr
)
970 class _StaticArrayFieldClass(_StaticArrayFieldClassConst
, _ArrayFieldClass
):
971 _NAME
= "Static array"
974 class _DynamicArrayFieldClassConst(_ArrayFieldClassConst
):
975 _NAME
= "Const dynamic array"
978 class _DynamicArrayWithLengthFieldFieldClassConst(_DynamicArrayFieldClassConst
):
979 _NAME
= "Const dynamic array (with length field)"
982 def length_field_path(self
):
983 ptr
= native_bt
.field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
989 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
992 class _DynamicArrayFieldClass(_DynamicArrayFieldClassConst
, _ArrayFieldClass
):
993 _NAME
= "Dynamic array"
996 class _DynamicArrayWithLengthFieldFieldClass(
997 _DynamicArrayWithLengthFieldFieldClassConst
, _DynamicArrayFieldClass
999 _NAME
= "Dynamic array (with length field)"
1002 _FIELD_CLASS_TYPE_TO_CONST_OBJ
= {
1003 native_bt
.FIELD_CLASS_TYPE_BOOL
: _BoolFieldClassConst
,
1004 native_bt
.FIELD_CLASS_TYPE_BIT_ARRAY
: _BitArrayFieldClassConst
,
1005 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClassConst
,
1006 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClassConst
,
1007 native_bt
.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
: _SinglePrecisionRealFieldClassConst
,
1008 native_bt
.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
: _DoublePrecisionRealFieldClassConst
,
1009 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClassConst
,
1010 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClassConst
,
1011 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClassConst
,
1012 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClassConst
,
1013 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClassConst
,
1014 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
: _DynamicArrayFieldClassConst
,
1015 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
: _DynamicArrayWithLengthFieldFieldClassConst
,
1016 native_bt
.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
: _OptionFieldClassConst
,
1017 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
: _OptionWithBoolSelectorFieldClassConst
,
1018 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithUnsignedIntegerSelectorFieldClassConst
,
1019 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithSignedIntegerSelectorFieldClassConst
,
1020 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
: _VariantFieldClassWithoutSelectorConst
,
1021 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithUnsignedIntegerSelectorConst
,
1022 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithSignedIntegerSelectorConst
,
1025 _FIELD_CLASS_TYPE_TO_OBJ
= {
1026 native_bt
.FIELD_CLASS_TYPE_BOOL
: _BoolFieldClass
,
1027 native_bt
.FIELD_CLASS_TYPE_BIT_ARRAY
: _BitArrayFieldClass
,
1028 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClass
,
1029 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClass
,
1030 native_bt
.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
: _SinglePrecisionRealFieldClass
,
1031 native_bt
.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
: _DoublePrecisionRealFieldClass
,
1032 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClass
,
1033 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClass
,
1034 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClass
,
1035 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClass
,
1036 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClass
,
1037 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
: _DynamicArrayFieldClass
,
1038 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
: _DynamicArrayWithLengthFieldFieldClass
,
1039 native_bt
.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
: _OptionFieldClass
,
1040 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
: _OptionWithBoolSelectorFieldClass
,
1041 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithUnsignedIntegerSelectorFieldClass
,
1042 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithSignedIntegerSelectorFieldClass
,
1043 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
: _VariantFieldClassWithoutSelector
,
1044 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithUnsignedIntegerSelector
,
1045 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithSignedIntegerSelector
,