1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
7 from bt2
import error
as bt2_error
8 from bt2
import utils
as bt2_utils
9 from bt2
import value
as bt2_value
10 from bt2
import object as bt2_object
11 from bt2
import native_bt
12 from bt2
import field_path
as bt2_field_path
13 from bt2
import integer_range_set
as bt2_integer_range_set
16 def _obj_type_from_field_class_ptr_template(type_map
, ptr
):
17 typeid
= native_bt
.field_class_get_type(ptr
)
18 return type_map
[typeid
]
21 def _obj_type_from_field_class_ptr(ptr
):
22 return _obj_type_from_field_class_ptr_template(_FIELD_CLASS_TYPE_TO_OBJ
, ptr
)
25 def _create_field_class_from_ptr_and_get_ref_template(type_map
, ptr
):
26 return _obj_type_from_field_class_ptr_template(
28 )._create
_from
_ptr
_and
_get
_ref
(ptr
)
31 def _create_field_class_from_ptr_and_get_ref(ptr
):
32 return _create_field_class_from_ptr_and_get_ref_template(
33 _FIELD_CLASS_TYPE_TO_OBJ
, ptr
37 def _create_field_class_from_const_ptr_and_get_ref(ptr
):
38 return _create_field_class_from_ptr_and_get_ref_template(
39 _FIELD_CLASS_TYPE_TO_CONST_OBJ
, ptr
43 class IntegerDisplayBase
:
44 BINARY
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
45 OCTAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
46 DECIMAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
47 HEXADECIMAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
50 class _FieldClassConst(bt2_object
._SharedObject
):
53 native_bt
.field_class_get_ref(ptr
)
57 native_bt
.field_class_put_ref(ptr
)
59 _borrow_user_attributes_ptr
= staticmethod(
60 native_bt
.field_class_borrow_user_attributes_const
62 _create_value_from_ptr_and_get_ref
= staticmethod(
63 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
66 def _check_create_status(self
, ptr
):
68 raise bt2_error
._MemoryError
(
69 "cannot create {} field class object".format(self
._NAME
.lower())
73 def user_attributes(self
):
74 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
75 assert ptr
is not None
76 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
79 class _FieldClass(_FieldClassConst
):
80 _borrow_user_attributes_ptr
= staticmethod(
81 native_bt
.field_class_borrow_user_attributes
83 _create_value_from_ptr_and_get_ref
= staticmethod(
84 bt2_value
._create
_from
_ptr
_and
_get
_ref
87 def _user_attributes(self
, user_attributes
):
88 value
= bt2_value
.create_value(user_attributes
)
89 bt2_utils
._check
_type
(value
, bt2_value
.MapValue
)
90 native_bt
.field_class_set_user_attributes(self
._ptr
, value
._ptr
)
92 _user_attributes
= property(fset
=_user_attributes
)
95 class _BoolFieldClassConst(_FieldClassConst
):
96 _NAME
= "Const boolean"
99 class _BoolFieldClass(_BoolFieldClassConst
, _FieldClass
):
103 class _BitArrayFieldClassConst(_FieldClassConst
):
104 _NAME
= "Const bit array"
108 length
= native_bt
.field_class_bit_array_get_length(self
._ptr
)
113 class _BitArrayFieldClass(_BitArrayFieldClassConst
, _FieldClass
):
117 class _IntegerFieldClassConst(_FieldClassConst
):
119 def field_value_range(self
):
120 size
= native_bt
.field_class_integer_get_field_value_range(self
._ptr
)
125 def preferred_display_base(self
):
126 base
= native_bt
.field_class_integer_get_preferred_display_base(self
._ptr
)
131 class _IntegerFieldClass(_FieldClass
, _IntegerFieldClassConst
):
132 def _field_value_range(self
, size
):
133 if size
< 1 or size
> 64:
134 raise ValueError("Value is outside valid range [1, 64] ({})".format(size
))
135 native_bt
.field_class_integer_set_field_value_range(self
._ptr
, size
)
137 _field_value_range
= property(fset
=_field_value_range
)
139 def _preferred_display_base(self
, base
):
140 bt2_utils
._check
_uint
64(base
)
143 IntegerDisplayBase
.BINARY
,
144 IntegerDisplayBase
.OCTAL
,
145 IntegerDisplayBase
.DECIMAL
,
146 IntegerDisplayBase
.HEXADECIMAL
,
148 raise ValueError("Display base is not a valid IntegerDisplayBase value")
150 native_bt
.field_class_integer_set_preferred_display_base(self
._ptr
, base
)
152 _preferred_display_base
= property(fset
=_preferred_display_base
)
155 class _UnsignedIntegerFieldClassConst(_IntegerFieldClassConst
, _FieldClassConst
):
156 _NAME
= "Const unsigned integer"
159 class _UnsignedIntegerFieldClass(
160 _UnsignedIntegerFieldClassConst
, _IntegerFieldClass
, _FieldClass
162 _NAME
= "Unsigned integer"
165 class _SignedIntegerFieldClassConst(_IntegerFieldClassConst
, _FieldClassConst
):
166 _NAME
= "Const signed integer"
169 class _SignedIntegerFieldClass(
170 _SignedIntegerFieldClassConst
, _IntegerFieldClass
, _FieldClass
172 _NAME
= "Signed integer"
175 class _RealFieldClassConst(_FieldClassConst
):
179 class _SinglePrecisionRealFieldClassConst(_RealFieldClassConst
):
180 _NAME
= "Const single-precision real"
183 class _DoublePrecisionRealFieldClassConst(_RealFieldClassConst
):
184 _NAME
= "Const double-precision real"
187 class _RealFieldClass(_FieldClass
, _RealFieldClassConst
):
191 class _SinglePrecisionRealFieldClass(_RealFieldClass
):
192 _NAME
= "Single-precision real"
195 class _DoublePrecisionRealFieldClass(_RealFieldClass
):
196 _NAME
= "Double-precision real"
199 # an enumeration field class mapping does not have a reference count, so
200 # we copy the properties here to avoid eventual memory access errors.
201 class _EnumerationFieldClassMapping
:
202 def __init__(self
, mapping_ptr
):
203 base_mapping_ptr
= self
._as
_enumeration
_field
_class
_mapping
_ptr
(mapping_ptr
)
204 self
._label
= native_bt
.field_class_enumeration_mapping_get_label(
207 assert self
._label
is not None
208 ranges_ptr
= self
._mapping
_borrow
_ranges
_ptr
(mapping_ptr
)
209 assert ranges_ptr
is not None
210 self
._ranges
= self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(ranges_ptr
)
221 class _UnsignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping
):
222 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
223 _as_enumeration_field_class_mapping_ptr
= staticmethod(
224 native_bt
.field_class_enumeration_unsigned_mapping_as_mapping_const
226 _mapping_borrow_ranges_ptr
= staticmethod(
227 native_bt
.field_class_enumeration_unsigned_mapping_borrow_ranges_const
231 class _SignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping
):
232 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
233 _as_enumeration_field_class_mapping_ptr
= staticmethod(
234 native_bt
.field_class_enumeration_signed_mapping_as_mapping_const
236 _mapping_borrow_ranges_ptr
= staticmethod(
237 native_bt
.field_class_enumeration_signed_mapping_borrow_ranges_const
241 class _EnumerationFieldClassConst(_IntegerFieldClassConst
, collections
.abc
.Mapping
):
243 count
= native_bt
.field_class_enumeration_get_mapping_count(self
._ptr
)
247 def mappings_for_value(self
, value
):
248 self
._check
_int
_type
(value
)
250 status
, labels
= self
._get
_mapping
_labels
_for
_value
(self
._ptr
, value
)
251 bt2_utils
._handle
_func
_status
(
252 status
, "cannot get mapping labels for value {}".format(value
)
254 return [self
[label
] for label
in labels
]
257 for idx
in range(len(self
)):
258 mapping_ptr
= self
._borrow
_mapping
_ptr
_by
_index
(self
._ptr
, idx
)
259 yield self
._mapping
_pycls
(mapping_ptr
).label
261 def __getitem__(self
, label
):
262 bt2_utils
._check
_str
(label
)
263 mapping_ptr
= self
._borrow
_mapping
_ptr
_by
_label
(self
._ptr
, label
)
265 if mapping_ptr
is None:
266 raise KeyError(label
)
268 return self
._mapping
_pycls
(mapping_ptr
)
271 class _EnumerationFieldClass(_EnumerationFieldClassConst
, _IntegerFieldClass
):
272 def add_mapping(self
, label
, ranges
):
273 bt2_utils
._check
_str
(label
)
274 bt2_utils
._check
_type
(ranges
, self
._range
_set
_pycls
)
277 raise ValueError("duplicate mapping label '{}'".format(label
))
279 status
= self
._add
_mapping
(self
._ptr
, label
, ranges
._ptr
)
280 bt2_utils
._handle
_func
_status
(
281 status
, "cannot add mapping to enumeration field class object"
284 def __iadd__(self
, mappings
):
285 for label
, ranges
in mappings
:
286 self
.add_mapping(label
, ranges
)
291 class _UnsignedEnumerationFieldClassConst(
292 _EnumerationFieldClassConst
, _UnsignedIntegerFieldClassConst
294 _NAME
= "Const unsigned enumeration"
295 _borrow_mapping_ptr_by_label
= staticmethod(
296 native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_label_const
298 _borrow_mapping_ptr_by_index
= staticmethod(
299 native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_index_const
301 _mapping_pycls
= property(lambda _
: _UnsignedEnumerationFieldClassMappingConst
)
302 _get_mapping_labels_for_value
= staticmethod(
303 native_bt
.field_class_enumeration_unsigned_get_mapping_labels_for_value
305 _check_int_type
= staticmethod(bt2_utils
._check
_uint
64)
308 class _UnsignedEnumerationFieldClass(
309 _UnsignedEnumerationFieldClassConst
,
310 _EnumerationFieldClass
,
311 _UnsignedIntegerFieldClass
,
313 _NAME
= "Unsigned enumeration"
314 _range_set_pycls
= bt2_integer_range_set
.UnsignedIntegerRangeSet
315 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_unsigned_add_mapping
)
318 class _SignedEnumerationFieldClassConst(
319 _EnumerationFieldClassConst
, _SignedIntegerFieldClassConst
321 _NAME
= "Const signed enumeration"
322 _borrow_mapping_ptr_by_label
= staticmethod(
323 native_bt
.field_class_enumeration_signed_borrow_mapping_by_label_const
325 _borrow_mapping_ptr_by_index
= staticmethod(
326 native_bt
.field_class_enumeration_signed_borrow_mapping_by_index_const
328 _mapping_pycls
= property(lambda _
: _SignedEnumerationFieldClassMappingConst
)
329 _get_mapping_labels_for_value
= staticmethod(
330 native_bt
.field_class_enumeration_signed_get_mapping_labels_for_value
332 _check_int_type
= staticmethod(bt2_utils
._check
_int
64)
335 class _SignedEnumerationFieldClass(
336 _SignedEnumerationFieldClassConst
, _EnumerationFieldClass
, _SignedIntegerFieldClass
338 _NAME
= "Signed enumeration"
339 _range_set_pycls
= bt2_integer_range_set
.SignedIntegerRangeSet
340 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_signed_add_mapping
)
343 class _StringFieldClassConst(_FieldClassConst
):
344 _NAME
= "Const string"
347 class _StringFieldClass(_StringFieldClassConst
, _FieldClass
):
351 class _StructureFieldClassMemberConst
:
352 _create_field_class_from_ptr_and_get_ref
= staticmethod(
353 _create_field_class_from_const_ptr_and_get_ref
355 _borrow_field_class_ptr
= staticmethod(
356 native_bt
.field_class_structure_member_borrow_field_class_const
358 _borrow_user_attributes_ptr
= staticmethod(
359 native_bt
.field_class_structure_member_borrow_user_attributes_const
361 _create_value_from_ptr_and_get_ref
= staticmethod(
362 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
365 def __init__(self
, owning_struct_fc
, member_ptr
):
366 # this field class owns the member; keeping it here maintains
367 # the member alive as members are not shared objects
368 self
._owning
_struct
_fc
= owning_struct_fc
369 self
._ptr
= member_ptr
373 name
= native_bt
.field_class_structure_member_get_name(self
._ptr
)
374 assert name
is not None
378 def field_class(self
):
379 fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
380 assert fc_ptr
is not None
381 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
384 def user_attributes(self
):
385 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
386 assert ptr
is not None
387 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
390 class _StructureFieldClassMember(_StructureFieldClassMemberConst
):
391 _borrow_field_class_ptr
= staticmethod(
392 native_bt
.field_class_structure_member_borrow_field_class
394 _borrow_user_attributes_ptr
= staticmethod(
395 native_bt
.field_class_structure_member_borrow_user_attributes
397 _create_field_class_from_ptr_and_get_ref
= staticmethod(
398 _create_field_class_from_ptr_and_get_ref
400 _create_value_from_ptr_and_get_ref
= staticmethod(
401 bt2_value
._create
_from
_ptr
_and
_get
_ref
404 def _user_attributes(self
, user_attributes
):
405 value
= bt2_value
.create_value(user_attributes
)
406 bt2_utils
._check
_type
(value
, bt2_value
.MapValue
)
407 native_bt
.field_class_structure_member_set_user_attributes(
408 self
._ptr
, value
._ptr
411 _user_attributes
= property(fset
=_user_attributes
)
414 class _StructureFieldClassConst(_FieldClassConst
, collections
.abc
.Mapping
):
415 _NAME
= "Const structure"
416 _borrow_member_ptr_by_index
= staticmethod(
417 native_bt
.field_class_structure_borrow_member_by_index_const
419 _borrow_member_ptr_by_name
= staticmethod(
420 native_bt
.field_class_structure_borrow_member_by_name_const
422 _structure_member_field_class_pycls
= property(
423 lambda _
: _StructureFieldClassMemberConst
427 count
= native_bt
.field_class_structure_get_member_count(self
._ptr
)
431 def __getitem__(self
, key
):
432 if not isinstance(key
, str):
434 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
437 member_ptr
= self
._borrow
_member
_ptr
_by
_name
(self
._ptr
, key
)
439 if member_ptr
is None:
442 return self
._structure
_member
_field
_class
_pycls
(self
, member_ptr
)
445 for idx
in range(len(self
)):
446 member_ptr
= self
._borrow
_member
_ptr
_by
_index
(self
._ptr
, idx
)
447 assert member_ptr
is not None
448 yield native_bt
.field_class_structure_member_get_name(member_ptr
)
450 def member_at_index(self
, index
):
451 bt2_utils
._check
_uint
64(index
)
453 if index
>= len(self
):
456 member_ptr
= self
._borrow
_member
_ptr
_by
_index
(self
._ptr
, index
)
457 assert member_ptr
is not None
458 return self
._structure
_member
_field
_class
_pycls
(self
, member_ptr
)
461 class _StructureFieldClass(_StructureFieldClassConst
, _FieldClass
):
463 _borrow_member_by_index
= staticmethod(
464 native_bt
.field_class_structure_borrow_member_by_index
466 _borrow_member_ptr_by_name
= staticmethod(
467 native_bt
.field_class_structure_borrow_member_by_name
469 _structure_member_field_class_pycls
= property(lambda _
: _StructureFieldClassMember
)
471 def append_member(self
, name
, field_class
, user_attributes
=None):
472 bt2_utils
._check
_str
(name
)
473 bt2_utils
._check
_type
(field_class
, _FieldClass
)
476 raise ValueError("duplicate member name '{}'".format(name
))
478 user_attributes_value
= None
480 if user_attributes
is not None:
481 # check now that user attributes are valid
482 user_attributes_value
= bt2_value
.create_value(user_attributes
)
484 status
= native_bt
.field_class_structure_append_member(
485 self
._ptr
, name
, field_class
._ptr
487 bt2_utils
._handle
_func
_status
(
488 status
, "cannot append member to structure field class object"
491 if user_attributes
is not None:
492 self
[name
]._user
_attributes
= user_attributes_value
494 def __iadd__(self
, members
):
495 for name
, field_class
in members
:
496 self
.append_member(name
, field_class
)
501 class _OptionFieldClassConst(_FieldClassConst
):
502 _NAME
= "Const option"
503 _create_field_class_from_ptr_and_get_ref
= staticmethod(
504 _create_field_class_from_const_ptr_and_get_ref
506 _borrow_field_class_ptr
= staticmethod(
507 native_bt
.field_class_option_borrow_field_class_const
511 def field_class(self
):
512 elem_fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
513 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(elem_fc_ptr
)
516 class _OptionWithSelectorFieldClassConst(_OptionFieldClassConst
):
517 _NAME
= "Const option (with selector)"
520 def selector_field_path(self
):
521 ptr
= native_bt
.field_class_option_with_selector_field_borrow_selector_field_path_const(
527 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
530 class _OptionWithBoolSelectorFieldClassConst(_OptionWithSelectorFieldClassConst
):
531 _NAME
= "Const option (with boolean selector)"
534 def selector_is_reversed(self
):
536 native_bt
.field_class_option_with_selector_field_bool_selector_is_reversed(
542 class _OptionWithIntegerSelectorFieldClassConst(_OptionWithSelectorFieldClassConst
):
543 _NAME
= "Const option (with integer selector)"
547 range_set_ptr
= self
._borrow
_selector
_ranges
_ptr
(self
._ptr
)
548 assert range_set_ptr
is not None
549 return self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
552 class _OptionWithUnsignedIntegerSelectorFieldClassConst(
553 _OptionWithIntegerSelectorFieldClassConst
555 _NAME
= "Const option (with unsigned integer selector)"
556 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
557 _borrow_selector_ranges_ptr
= staticmethod(
558 native_bt
.field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const
562 class _OptionWithSignedIntegerSelectorFieldClassConst(
563 _OptionWithIntegerSelectorFieldClassConst
565 _NAME
= "Const option (with signed integer selector)"
566 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
567 _borrow_selector_ranges_ptr
= staticmethod(
568 native_bt
.field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const
572 class _OptionFieldClass(_OptionFieldClassConst
, _FieldClass
):
574 _borrow_field_class_ptr
= staticmethod(
575 native_bt
.field_class_option_borrow_field_class
577 _create_field_class_from_ptr_and_get_ref
= staticmethod(
578 _create_field_class_from_ptr_and_get_ref
582 class _OptionWithSelectorFieldClass(
583 _OptionWithSelectorFieldClassConst
, _OptionFieldClass
585 _NAME
= "Option (with selector)"
588 class _OptionWithBoolSelectorFieldClass(
589 _OptionWithBoolSelectorFieldClassConst
, _OptionWithSelectorFieldClass
591 _NAME
= "Option (with boolean selector)"
593 def _selector_is_reversed(self
, selector_is_reversed
):
594 bt2_utils
._check
_bool
(selector_is_reversed
)
595 native_bt
.field_class_option_with_selector_field_bool_set_selector_is_reversed(
596 self
._ptr
, selector_is_reversed
599 _selector_is_reversed
= property(fset
=_selector_is_reversed
)
602 class _OptionWithIntegerSelectorFieldClass(
603 _OptionWithIntegerSelectorFieldClassConst
, _OptionWithSelectorFieldClass
605 _NAME
= "Option (with integer selector)"
608 class _OptionWithUnsignedIntegerSelectorFieldClass(
609 _OptionWithUnsignedIntegerSelectorFieldClassConst
,
610 _OptionWithIntegerSelectorFieldClass
,
612 _NAME
= "Option (with unsigned integer selector)"
615 class _OptionWithSignedIntegerSelectorFieldClass(
616 _OptionWithSignedIntegerSelectorFieldClassConst
,
617 _OptionWithIntegerSelectorFieldClass
,
619 _NAME
= "Option (with signed integer selector)"
622 class _VariantFieldClassOptionConst
:
623 _create_field_class_from_ptr_and_get_ref
= staticmethod(
624 _create_field_class_from_const_ptr_and_get_ref
626 _borrow_field_class_ptr
= staticmethod(
627 native_bt
.field_class_variant_option_borrow_field_class_const
629 _borrow_user_attributes_ptr
= staticmethod(
630 native_bt
.field_class_variant_option_borrow_user_attributes_const
632 _create_value_from_ptr_and_get_ref
= staticmethod(
633 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
636 def __init__(self
, owning_var_fc
, option_ptr
):
637 # this field class owns the option; keeping it here maintains
638 # the option alive as options are not shared objects
639 self
._owning
_var
_fc
= owning_var_fc
640 self
._ptr
= option_ptr
644 name
= native_bt
.field_class_variant_option_get_name(self
._ptr
)
645 assert name
is not None
649 def field_class(self
):
650 fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
651 assert fc_ptr
is not None
652 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
655 def user_attributes(self
):
656 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
657 assert ptr
is not None
658 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
661 class _VariantFieldClassOption(_VariantFieldClassOptionConst
):
662 _create_field_class_from_ptr_and_get_ref
= staticmethod(
663 _create_field_class_from_ptr_and_get_ref
665 _borrow_field_class_ptr
= staticmethod(
666 native_bt
.field_class_variant_option_borrow_field_class
668 _borrow_user_attributes_ptr
= staticmethod(
669 native_bt
.field_class_variant_option_borrow_user_attributes
671 _create_value_from_ptr_and_get_ref
= staticmethod(
672 bt2_value
._create
_from
_ptr
_and
_get
_ref
675 def _user_attributes(self
, user_attributes
):
676 value
= bt2_value
.create_value(user_attributes
)
677 bt2_utils
._check
_type
(value
, bt2_value
.MapValue
)
678 native_bt
.field_class_variant_option_set_user_attributes(self
._ptr
, value
._ptr
)
680 _user_attributes
= property(fset
=_user_attributes
)
683 class _VariantFieldClassWithIntegerSelectorOptionConst(_VariantFieldClassOptionConst
):
684 def __init__(self
, owning_var_fc
, spec_opt_ptr
):
685 self
._spec
_ptr
= spec_opt_ptr
686 super().__init
__(owning_var_fc
, self
._as
_option
_ptr
(spec_opt_ptr
))
690 range_set_ptr
= self
._borrow
_ranges
_ptr
(self
._spec
_ptr
)
691 assert range_set_ptr
is not None
692 return self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
695 class _VariantFieldClassWithIntegerSelectorOption(
696 _VariantFieldClassWithIntegerSelectorOptionConst
, _VariantFieldClassOption
701 class _VariantFieldClassWithSignedIntegerSelectorOptionConst(
702 _VariantFieldClassWithIntegerSelectorOptionConst
704 _as_option_ptr
= staticmethod(
705 native_bt
.field_class_variant_with_selector_field_integer_signed_option_as_option_const
707 _borrow_ranges_ptr
= staticmethod(
708 native_bt
.field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const
710 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
713 class _VariantFieldClassWithSignedIntegerSelectorOption(
714 _VariantFieldClassWithSignedIntegerSelectorOptionConst
,
715 _VariantFieldClassWithIntegerSelectorOption
,
720 class _VariantFieldClassWithUnsignedIntegerSelectorOptionConst(
721 _VariantFieldClassWithIntegerSelectorOptionConst
723 _as_option_ptr
= staticmethod(
724 native_bt
.field_class_variant_with_selector_field_integer_unsigned_option_as_option_const
726 _borrow_ranges_ptr
= staticmethod(
727 native_bt
.field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const
729 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
732 class _VariantFieldClassWithUnsignedIntegerSelectorOption(
733 _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
,
734 _VariantFieldClassWithIntegerSelectorOption
,
739 class _VariantFieldClassConst(_FieldClassConst
, collections
.abc
.Mapping
):
740 _NAME
= "Const variant"
741 _borrow_option_ptr_by_name
= staticmethod(
742 native_bt
.field_class_variant_borrow_option_by_name_const
744 _borrow_option_ptr_by_index
= staticmethod(
745 native_bt
.field_class_variant_borrow_option_by_index_const
747 _variant_option_pycls
= _VariantFieldClassOptionConst
750 def _as_option_ptr(opt_ptr
):
753 def _create_option_from_ptr(self
, opt_ptr
):
754 return self
._variant
_option
_pycls
(self
, opt_ptr
)
757 count
= native_bt
.field_class_variant_get_option_count(self
._ptr
)
761 def __getitem__(self
, key
):
762 if not isinstance(key
, str):
764 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
767 opt_ptr
= self
._borrow
_option
_ptr
_by
_name
(self
._ptr
, key
)
772 return self
._create
_option
_from
_ptr
(opt_ptr
)
775 for idx
in range(len(self
)):
776 opt_ptr
= self
._borrow
_option
_ptr
_by
_index
(self
._ptr
, idx
)
777 assert opt_ptr
is not None
778 base_opt_ptr
= self
._as
_option
_ptr
(opt_ptr
)
779 yield native_bt
.field_class_variant_option_get_name(base_opt_ptr
)
781 def option_at_index(self
, index
):
782 bt2_utils
._check
_uint
64(index
)
784 if index
>= len(self
):
787 opt_ptr
= self
._borrow
_option
_ptr
_by
_index
(self
._ptr
, index
)
788 assert opt_ptr
is not None
789 return self
._create
_option
_from
_ptr
(opt_ptr
)
792 class _VariantFieldClass(_VariantFieldClassConst
, _FieldClass
, collections
.abc
.Mapping
):
794 _borrow_option_ptr_by_name
= staticmethod(
795 native_bt
.field_class_variant_borrow_option_by_name
797 _borrow_option_ptr_by_index
= staticmethod(
798 native_bt
.field_class_variant_borrow_option_by_index
800 _variant_option_pycls
= _VariantFieldClassOption
803 class _VariantFieldClassWithoutSelectorConst(_VariantFieldClassConst
):
804 _NAME
= "Const variant (without selector)"
807 class _VariantFieldClassWithoutSelector(
808 _VariantFieldClassWithoutSelectorConst
, _VariantFieldClass
810 _NAME
= "Variant (without selector)"
812 def append_option(self
, name
, field_class
, user_attributes
=None):
813 bt2_utils
._check
_str
(name
)
814 bt2_utils
._check
_type
(field_class
, _FieldClass
)
817 raise ValueError("duplicate option name '{}'".format(name
))
819 user_attributes_value
= None
821 if user_attributes
is not None:
822 # check now that user attributes are valid
823 user_attributes_value
= bt2_value
.create_value(user_attributes
)
825 status
= native_bt
.field_class_variant_without_selector_append_option(
826 self
._ptr
, name
, field_class
._ptr
828 bt2_utils
._handle
_func
_status
(
829 status
, "cannot append option to variant field class object"
832 if user_attributes
is not None:
833 self
[name
]._user
_attributes
= user_attributes_value
835 def __iadd__(self
, options
):
836 for name
, field_class
in options
:
837 self
.append_option(name
, field_class
)
842 class _VariantFieldClassWithIntegerSelectorConst(_VariantFieldClassConst
):
843 _NAME
= "Const variant (with selector)"
846 def selector_field_path(self
):
847 ptr
= native_bt
.field_class_variant_with_selector_field_borrow_selector_field_path_const(
854 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
857 class _VariantFieldClassWithIntegerSelector(
858 _VariantFieldClassWithIntegerSelectorConst
, _VariantFieldClass
860 _NAME
= "Variant (with selector)"
862 def append_option(self
, name
, field_class
, ranges
, user_attributes
=None):
863 bt2_utils
._check
_str
(name
)
864 bt2_utils
._check
_type
(field_class
, _FieldClass
)
865 bt2_utils
._check
_type
(ranges
, self
._variant
_option
_pycls
._range
_set
_pycls
)
868 raise ValueError("duplicate option name '{}'".format(name
))
871 raise ValueError("range set is empty")
873 user_attributes_value
= None
875 if user_attributes
is not None:
876 # check now that user attributes are valid
877 user_attributes_value
= bt2_value
.create_value(user_attributes
)
879 # TODO: check overlaps (precondition of self._append_option())
881 status
= self
._append
_option
(self
._ptr
, name
, field_class
._ptr
, ranges
._ptr
)
882 bt2_utils
._handle
_func
_status
(
883 status
, "cannot append option to variant field class object"
886 if user_attributes
is not None:
887 self
[name
]._user
_attributes
= user_attributes_value
889 def __iadd__(self
, options
):
890 for name
, field_class
, ranges
in options
:
891 self
.append_option(name
, field_class
, ranges
)
896 class _VariantFieldClassWithUnsignedIntegerSelectorConst(
897 _VariantFieldClassWithIntegerSelectorConst
899 _NAME
= "Const variant (with unsigned integer selector)"
900 _borrow_option_ptr_by_name
= staticmethod(
901 native_bt
.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_name_const
903 _borrow_option_ptr_by_index
= staticmethod(
904 native_bt
.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const
906 _variant_option_pycls
= _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
907 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
910 class _VariantFieldClassWithUnsignedIntegerSelector(
911 _VariantFieldClassWithUnsignedIntegerSelectorConst
,
912 _VariantFieldClassWithIntegerSelector
,
914 _NAME
= "Variant (with unsigned integer selector)"
915 _variant_option_pycls
= _VariantFieldClassWithUnsignedIntegerSelectorOption
916 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
917 _append_option
= staticmethod(
918 native_bt
.field_class_variant_with_selector_field_integer_unsigned_append_option
922 class _VariantFieldClassWithSignedIntegerSelectorConst(
923 _VariantFieldClassWithIntegerSelectorConst
925 _NAME
= "Const variant (with signed integer selector)"
926 _borrow_option_ptr_by_name
= staticmethod(
927 native_bt
.field_class_variant_with_selector_field_integer_signed_borrow_option_by_name_const
929 _borrow_option_ptr_by_index
= staticmethod(
930 native_bt
.field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const
932 _variant_option_pycls
= _VariantFieldClassWithSignedIntegerSelectorOptionConst
933 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
936 class _VariantFieldClassWithSignedIntegerSelector(
937 _VariantFieldClassWithSignedIntegerSelectorConst
,
938 _VariantFieldClassWithIntegerSelector
,
940 _NAME
= "Variant (with signed integer selector)"
941 _variant_option_pycls
= _VariantFieldClassWithSignedIntegerSelectorOption
942 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
943 _append_option
= staticmethod(
944 native_bt
.field_class_variant_with_selector_field_integer_signed_append_option
948 class _ArrayFieldClassConst(_FieldClassConst
):
949 _create_field_class_from_ptr_and_get_ref
= staticmethod(
950 _create_field_class_from_const_ptr_and_get_ref
952 _borrow_element_field_class
= staticmethod(
953 native_bt
.field_class_array_borrow_element_field_class_const
957 def element_field_class(self
):
958 elem_fc_ptr
= self
._borrow
_element
_field
_class
(self
._ptr
)
959 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(elem_fc_ptr
)
962 class _ArrayFieldClass(_ArrayFieldClassConst
, _FieldClass
):
963 _create_field_class_from_ptr_and_get_ref
= staticmethod(
964 _create_field_class_from_ptr_and_get_ref
966 _borrow_element_field_class
= staticmethod(
967 native_bt
.field_class_array_borrow_element_field_class
971 class _StaticArrayFieldClassConst(_ArrayFieldClassConst
):
972 _NAME
= "Const static array"
976 return native_bt
.field_class_array_static_get_length(self
._ptr
)
979 class _StaticArrayFieldClass(_StaticArrayFieldClassConst
, _ArrayFieldClass
):
980 _NAME
= "Static array"
983 class _DynamicArrayFieldClassConst(_ArrayFieldClassConst
):
984 _NAME
= "Const dynamic array"
987 class _DynamicArrayWithLengthFieldFieldClassConst(_DynamicArrayFieldClassConst
):
988 _NAME
= "Const dynamic array (with length field)"
991 def length_field_path(self
):
992 ptr
= native_bt
.field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
998 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
1001 class _DynamicArrayFieldClass(_DynamicArrayFieldClassConst
, _ArrayFieldClass
):
1002 _NAME
= "Dynamic array"
1005 class _DynamicArrayWithLengthFieldFieldClass(
1006 _DynamicArrayWithLengthFieldFieldClassConst
, _DynamicArrayFieldClass
1008 _NAME
= "Dynamic array (with length field)"
1011 _FIELD_CLASS_TYPE_TO_CONST_OBJ
= {
1012 native_bt
.FIELD_CLASS_TYPE_BOOL
: _BoolFieldClassConst
,
1013 native_bt
.FIELD_CLASS_TYPE_BIT_ARRAY
: _BitArrayFieldClassConst
,
1014 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClassConst
,
1015 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClassConst
,
1016 native_bt
.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
: _SinglePrecisionRealFieldClassConst
,
1017 native_bt
.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
: _DoublePrecisionRealFieldClassConst
,
1018 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClassConst
,
1019 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClassConst
,
1020 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClassConst
,
1021 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClassConst
,
1022 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClassConst
,
1023 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
: _DynamicArrayFieldClassConst
,
1024 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
: _DynamicArrayWithLengthFieldFieldClassConst
,
1025 native_bt
.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
: _OptionFieldClassConst
,
1026 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
: _OptionWithBoolSelectorFieldClassConst
,
1027 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithUnsignedIntegerSelectorFieldClassConst
,
1028 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithSignedIntegerSelectorFieldClassConst
,
1029 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
: _VariantFieldClassWithoutSelectorConst
,
1030 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithUnsignedIntegerSelectorConst
,
1031 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithSignedIntegerSelectorConst
,
1034 _FIELD_CLASS_TYPE_TO_OBJ
= {
1035 native_bt
.FIELD_CLASS_TYPE_BOOL
: _BoolFieldClass
,
1036 native_bt
.FIELD_CLASS_TYPE_BIT_ARRAY
: _BitArrayFieldClass
,
1037 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClass
,
1038 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClass
,
1039 native_bt
.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
: _SinglePrecisionRealFieldClass
,
1040 native_bt
.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
: _DoublePrecisionRealFieldClass
,
1041 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClass
,
1042 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClass
,
1043 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClass
,
1044 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClass
,
1045 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClass
,
1046 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
: _DynamicArrayFieldClass
,
1047 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
: _DynamicArrayWithLengthFieldFieldClass
,
1048 native_bt
.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
: _OptionFieldClass
,
1049 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
: _OptionWithBoolSelectorFieldClass
,
1050 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithUnsignedIntegerSelectorFieldClass
,
1051 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _OptionWithSignedIntegerSelectorFieldClass
,
1052 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
: _VariantFieldClassWithoutSelector
,
1053 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithUnsignedIntegerSelector
,
1054 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
: _VariantFieldClassWithSignedIntegerSelector
,