1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 from bt2
import native_bt
, object, utils
24 import collections
.abc
25 from bt2
import field_path
as bt2_field_path
26 from bt2
import integer_range_set
as bt2_integer_range_set
27 from bt2
import value
as bt2_value
31 def _create_field_class_from_ptr_and_get_ref_template(type_map
, ptr
):
32 typeid
= native_bt
.field_class_get_type(ptr
)
33 return type_map
[typeid
]._create
_from
_ptr
_and
_get
_ref
(ptr
)
36 def _create_field_class_from_ptr_and_get_ref(ptr
):
37 return _create_field_class_from_ptr_and_get_ref_template(
38 _FIELD_CLASS_TYPE_TO_OBJ
, ptr
42 def _create_field_class_from_const_ptr_and_get_ref(ptr
):
43 return _create_field_class_from_ptr_and_get_ref_template(
44 _FIELD_CLASS_TYPE_TO_CONST_OBJ
, ptr
48 class IntegerDisplayBase
:
49 BINARY
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
50 OCTAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
51 DECIMAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
52 HEXADECIMAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
55 class _FieldClassConst(object._SharedObject
):
56 _get_ref
= staticmethod(native_bt
.field_class_get_ref
)
57 _put_ref
= staticmethod(native_bt
.field_class_put_ref
)
58 _borrow_user_attributes_ptr
= staticmethod(
59 native_bt
.field_class_borrow_user_attributes_const
61 _create_value_from_ptr_and_get_ref
= staticmethod(
62 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
65 def _check_create_status(self
, ptr
):
67 raise bt2
._MemoryError(
68 'cannot create {} field class object'.format(self
._NAME
.lower())
72 def user_attributes(self
):
73 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
74 assert ptr
is not None
75 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
78 class _FieldClass(_FieldClassConst
):
79 _borrow_user_attributes_ptr
= staticmethod(
80 native_bt
.field_class_borrow_user_attributes
82 _create_value_from_ptr_and_get_ref
= staticmethod(
83 bt2_value
._create
_from
_ptr
_and
_get
_ref
86 def _user_attributes(self
, user_attributes
):
87 value
= bt2_value
.create_value(user_attributes
)
88 utils
._check
_type
(value
, bt2_value
.MapValue
)
89 native_bt
.field_class_set_user_attributes(self
._ptr
, value
._ptr
)
91 _user_attributes
= property(fset
=_user_attributes
)
94 class _BoolFieldClassConst(_FieldClassConst
):
95 _NAME
= 'Const Boolean'
98 class _BoolFieldClass(_BoolFieldClassConst
, _FieldClass
):
102 class _BitArrayFieldClassConst(_FieldClassConst
):
103 _NAME
= 'Const Bit array'
107 length
= native_bt
.field_class_bit_array_get_length(self
._ptr
)
112 class _BitArrayFieldClass(_BitArrayFieldClassConst
, _FieldClass
):
116 class _IntegerFieldClassConst(_FieldClassConst
):
118 def field_value_range(self
):
119 size
= native_bt
.field_class_integer_get_field_value_range(self
._ptr
)
124 def preferred_display_base(self
):
125 base
= native_bt
.field_class_integer_get_preferred_display_base(self
._ptr
)
130 class _IntegerFieldClass(_FieldClass
, _IntegerFieldClassConst
):
131 def _field_value_range(self
, size
):
132 if size
< 1 or size
> 64:
133 raise ValueError("Value is outside valid range [1, 64] ({})".format(size
))
134 native_bt
.field_class_integer_set_field_value_range(self
._ptr
, size
)
136 _field_value_range
= property(fset
=_field_value_range
)
138 def _preferred_display_base(self
, base
):
139 utils
._check
_uint
64(base
)
142 IntegerDisplayBase
.BINARY
,
143 IntegerDisplayBase
.OCTAL
,
144 IntegerDisplayBase
.DECIMAL
,
145 IntegerDisplayBase
.HEXADECIMAL
,
147 raise ValueError("Display base is not a valid IntegerDisplayBase value")
149 native_bt
.field_class_integer_set_preferred_display_base(self
._ptr
, base
)
151 _preferred_display_base
= property(fset
=_preferred_display_base
)
154 class _UnsignedIntegerFieldClassConst(_IntegerFieldClassConst
, _FieldClassConst
):
155 _NAME
= 'Const unsigned integer'
158 class _UnsignedIntegerFieldClass(
159 _UnsignedIntegerFieldClassConst
, _IntegerFieldClass
, _FieldClass
161 _NAME
= 'Unsigned integer'
164 class _SignedIntegerFieldClassConst(_IntegerFieldClassConst
, _FieldClassConst
):
165 _NAME
= 'Const signed integer'
168 class _SignedIntegerFieldClass(
169 _SignedIntegerFieldClassConst
, _IntegerFieldClass
, _FieldClass
171 _NAME
= 'Signed integer'
174 class _RealFieldClassConst(_FieldClassConst
):
178 class _SinglePrecisionRealFieldClassConst(_RealFieldClassConst
):
179 _NAME
= 'Const single-precision real'
182 class _DoublePrecisionRealFieldClassConst(_RealFieldClassConst
):
183 _NAME
= 'Const double-precision real'
186 class _RealFieldClass(_FieldClass
, _RealFieldClassConst
):
190 class _SinglePrecisionRealFieldClass(_RealFieldClass
):
191 _NAME
= 'Single-precision real'
194 class _DoublePrecisionRealFieldClass(_RealFieldClass
):
195 _NAME
= 'Double-precision real'
198 # an enumeration field class mapping does not have a reference count, so
199 # we copy the properties here to avoid eventual memory access errors.
200 class _EnumerationFieldClassMapping
:
201 def __init__(self
, mapping_ptr
):
202 base_mapping_ptr
= self
._as
_enumeration
_field
_class
_mapping
_ptr
(mapping_ptr
)
203 self
._label
= native_bt
.field_class_enumeration_mapping_get_label(
206 assert self
._label
is not None
207 ranges_ptr
= self
._mapping
_borrow
_ranges
_ptr
(mapping_ptr
)
208 assert ranges_ptr
is not None
209 self
._ranges
= self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(ranges_ptr
)
220 class _UnsignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping
):
221 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
222 _as_enumeration_field_class_mapping_ptr
= staticmethod(
223 native_bt
.field_class_enumeration_unsigned_mapping_as_mapping_const
225 _mapping_borrow_ranges_ptr
= staticmethod(
226 native_bt
.field_class_enumeration_unsigned_mapping_borrow_ranges_const
230 class _SignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping
):
231 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
232 _as_enumeration_field_class_mapping_ptr
= staticmethod(
233 native_bt
.field_class_enumeration_signed_mapping_as_mapping_const
235 _mapping_borrow_ranges_ptr
= staticmethod(
236 native_bt
.field_class_enumeration_signed_mapping_borrow_ranges_const
240 class _EnumerationFieldClassConst(_IntegerFieldClassConst
, collections
.abc
.Mapping
):
242 count
= native_bt
.field_class_enumeration_get_mapping_count(self
._ptr
)
246 def mappings_for_value(self
, value
):
247 self
._check
_int
_type
(value
)
249 status
, labels
= self
._get
_mapping
_labels
_for
_value
(self
._ptr
, value
)
250 utils
._handle
_func
_status
(
251 status
, 'cannot get mapping labels for value {}'.format(value
)
253 return [self
[label
] for label
in labels
]
256 for idx
in range(len(self
)):
257 mapping_ptr
= self
._borrow
_mapping
_ptr
_by
_index
(self
._ptr
, idx
)
258 yield self
._mapping
_pycls
(mapping_ptr
).label
260 def __getitem__(self
, label
):
261 utils
._check
_str
(label
)
262 mapping_ptr
= self
._borrow
_mapping
_ptr
_by
_label
(self
._ptr
, label
)
264 if mapping_ptr
is None:
265 raise KeyError(label
)
267 return self
._mapping
_pycls
(mapping_ptr
)
270 class _EnumerationFieldClass(_EnumerationFieldClassConst
, _IntegerFieldClass
):
271 def add_mapping(self
, label
, ranges
):
272 utils
._check
_str
(label
)
273 utils
._check
_type
(ranges
, self
._range
_set
_pycls
)
276 raise ValueError("duplicate mapping label '{}'".format(label
))
278 status
= self
._add
_mapping
(self
._ptr
, label
, ranges
._ptr
)
279 utils
._handle
_func
_status
(
280 status
, 'cannot add mapping to enumeration field class object'
283 def __iadd__(self
, mappings
):
284 for label
, ranges
in mappings
:
285 self
.add_mapping(label
, ranges
)
290 class _UnsignedEnumerationFieldClassConst(
291 _EnumerationFieldClassConst
, _UnsignedIntegerFieldClassConst
293 _NAME
= 'Const unsigned enumeration'
294 _borrow_mapping_ptr_by_label
= staticmethod(
295 native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_label_const
297 _borrow_mapping_ptr_by_index
= staticmethod(
298 native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_index_const
300 _mapping_pycls
= property(lambda _
: _UnsignedEnumerationFieldClassMappingConst
)
301 _get_mapping_labels_for_value
= staticmethod(
302 native_bt
.field_class_enumeration_unsigned_get_mapping_labels_for_value
304 _check_int_type
= staticmethod(utils
._check
_uint
64)
307 class _UnsignedEnumerationFieldClass(
308 _UnsignedEnumerationFieldClassConst
,
309 _EnumerationFieldClass
,
310 _UnsignedIntegerFieldClass
,
312 _NAME
= 'Unsigned enumeration'
313 _range_set_pycls
= bt2_integer_range_set
.UnsignedIntegerRangeSet
314 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_unsigned_add_mapping
)
317 class _SignedEnumerationFieldClassConst(
318 _EnumerationFieldClassConst
, _SignedIntegerFieldClassConst
320 _NAME
= 'Const signed enumeration'
321 _borrow_mapping_ptr_by_label
= staticmethod(
322 native_bt
.field_class_enumeration_signed_borrow_mapping_by_label_const
324 _borrow_mapping_ptr_by_index
= staticmethod(
325 native_bt
.field_class_enumeration_signed_borrow_mapping_by_index_const
327 _mapping_pycls
= property(lambda _
: _SignedEnumerationFieldClassMappingConst
)
328 _get_mapping_labels_for_value
= staticmethod(
329 native_bt
.field_class_enumeration_signed_get_mapping_labels_for_value
331 _check_int_type
= staticmethod(utils
._check
_int
64)
334 class _SignedEnumerationFieldClass(
335 _SignedEnumerationFieldClassConst
, _EnumerationFieldClass
, _SignedIntegerFieldClass
337 _NAME
= 'Signed enumeration'
338 _range_set_pycls
= bt2_integer_range_set
.SignedIntegerRangeSet
339 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_signed_add_mapping
)
342 class _StringFieldClassConst(_FieldClassConst
):
343 _NAME
= 'Const string'
346 class _StringFieldClass(_StringFieldClassConst
, _FieldClass
):
350 class _StructureFieldClassMemberConst
:
351 _create_field_class_from_ptr_and_get_ref
= staticmethod(
352 _create_field_class_from_const_ptr_and_get_ref
354 _borrow_field_class_ptr
= staticmethod(
355 native_bt
.field_class_structure_member_borrow_field_class_const
357 _borrow_user_attributes_ptr
= staticmethod(
358 native_bt
.field_class_structure_member_borrow_user_attributes_const
360 _create_value_from_ptr_and_get_ref
= staticmethod(
361 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
364 def __init__(self
, owning_struct_fc
, member_ptr
):
365 # this field class owns the member; keeping it here maintains
366 # the member alive as members are not shared objects
367 self
._owning
_struct
_fc
= owning_struct_fc
368 self
._ptr
= member_ptr
372 name
= native_bt
.field_class_structure_member_get_name(self
._ptr
)
373 assert name
is not None
377 def field_class(self
):
378 fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
379 assert fc_ptr
is not None
380 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
383 def user_attributes(self
):
384 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
385 assert ptr
is not None
386 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
389 class _StructureFieldClassMember(_StructureFieldClassMemberConst
):
390 _borrow_field_class_ptr
= staticmethod(
391 native_bt
.field_class_structure_member_borrow_field_class
393 _borrow_user_attributes_ptr
= staticmethod(
394 native_bt
.field_class_structure_member_borrow_user_attributes
396 _create_field_class_from_ptr_and_get_ref
= staticmethod(
397 _create_field_class_from_ptr_and_get_ref
399 _create_value_from_ptr_and_get_ref
= staticmethod(
400 bt2_value
._create
_from
_ptr
_and
_get
_ref
403 def _user_attributes(self
, user_attributes
):
404 value
= bt2_value
.create_value(user_attributes
)
405 utils
._check
_type
(value
, bt2_value
.MapValue
)
406 native_bt
.field_class_structure_member_set_user_attributes(
407 self
._ptr
, value
._ptr
410 _user_attributes
= property(fset
=_user_attributes
)
413 class _StructureFieldClassConst(_FieldClassConst
, collections
.abc
.Mapping
):
414 _NAME
= 'Const structure'
415 _borrow_member_ptr_by_index
= staticmethod(
416 native_bt
.field_class_structure_borrow_member_by_index_const
418 _borrow_member_ptr_by_name
= staticmethod(
419 native_bt
.field_class_structure_borrow_member_by_name_const
421 _structure_member_field_class_pycls
= property(
422 lambda _
: _StructureFieldClassMemberConst
426 count
= native_bt
.field_class_structure_get_member_count(self
._ptr
)
430 def __getitem__(self
, key
):
431 if not isinstance(key
, str):
433 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
436 member_ptr
= self
._borrow
_member
_ptr
_by
_name
(self
._ptr
, key
)
438 if member_ptr
is None:
441 return self
._structure
_member
_field
_class
_pycls
(self
, member_ptr
)
444 for idx
in range(len(self
)):
445 member_ptr
= self
._borrow
_member
_ptr
_by
_index
(self
._ptr
, idx
)
446 assert member_ptr
is not None
447 yield native_bt
.field_class_structure_member_get_name(member_ptr
)
449 def member_at_index(self
, index
):
450 utils
._check
_uint
64(index
)
452 if index
>= len(self
):
455 member_ptr
= self
._borrow
_member
_ptr
_by
_index
(self
._ptr
, index
)
456 assert member_ptr
is not None
457 return self
._structure
_member
_field
_class
_pycls
(self
, member_ptr
)
460 class _StructureFieldClass(_StructureFieldClassConst
, _FieldClass
):
462 _borrow_member_by_index
= staticmethod(
463 native_bt
.field_class_structure_borrow_member_by_index
465 _borrow_member_ptr_by_name
= staticmethod(
466 native_bt
.field_class_structure_borrow_member_by_name
468 _structure_member_field_class_pycls
= property(lambda _
: _StructureFieldClassMember
)
470 def append_member(self
, name
, field_class
, user_attributes
=None):
471 utils
._check
_str
(name
)
472 utils
._check
_type
(field_class
, _FieldClass
)
475 raise ValueError("duplicate member name '{}'".format(name
))
477 user_attributes_value
= None
479 if user_attributes
is not None:
480 # check now that user attributes are valid
481 user_attributes_value
= bt2
.create_value(user_attributes
)
483 status
= native_bt
.field_class_structure_append_member(
484 self
._ptr
, name
, field_class
._ptr
486 utils
._handle
_func
_status
(
487 status
, 'cannot append member to structure field class object'
490 if user_attributes
is not None:
491 self
[name
]._user
_attributes
= user_attributes_value
493 def __iadd__(self
, members
):
494 for name
, field_class
in members
:
495 self
.append_member(name
, field_class
)
500 class _OptionFieldClassConst(_FieldClassConst
):
501 _NAME
= 'Const Option'
502 _create_field_class_from_ptr_and_get_ref
= staticmethod(
503 _create_field_class_from_const_ptr_and_get_ref
505 _borrow_field_class_ptr
= staticmethod(
506 native_bt
.field_class_option_borrow_field_class_const
510 def field_class(self
):
511 elem_fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
512 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(elem_fc_ptr
)
515 class _OptionWithSelectorFieldClassConst(_OptionFieldClassConst
):
516 _NAME
= 'Const Option (with selector)'
519 def selector_field_path(self
):
520 ptr
= native_bt
.field_class_option_with_selector_borrow_selector_field_path_const(
526 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
529 class _OptionWithBoolSelectorFieldClassConst(_OptionWithSelectorFieldClassConst
):
530 _NAME
= 'Const Option (with boolean selector)'
533 def selector_is_reversed(self
):
535 native_bt
.field_class_option_with_selector_bool_selector_is_reversed(
541 class _OptionWithIntegerSelectorFieldClassConst(_OptionWithSelectorFieldClassConst
):
542 _NAME
= 'Const Option (with integer selector)'
546 range_set_ptr
= self
._borrow
_selector
_ranges
_ptr
(self
._ptr
)
547 assert range_set_ptr
is not None
548 return self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
551 class _OptionWithUnsignedIntegerSelectorFieldClassConst(
552 _OptionWithIntegerSelectorFieldClassConst
554 _NAME
= 'Const Option (with unsigned integer selector)'
555 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
556 _borrow_selector_ranges_ptr
= staticmethod(
557 native_bt
.field_class_option_with_selector_integer_unsigned_borrow_selector_ranges_const
561 class _OptionWithSignedIntegerSelectorFieldClassConst(
562 _OptionWithIntegerSelectorFieldClassConst
564 _NAME
= 'Const Option (with signed integer selector)'
565 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
566 _borrow_selector_ranges_ptr
= staticmethod(
567 native_bt
.field_class_option_with_selector_integer_signed_borrow_selector_ranges_const
571 class _OptionFieldClass(_OptionFieldClassConst
, _FieldClass
):
573 _borrow_field_class_ptr
= staticmethod(
574 native_bt
.field_class_option_borrow_field_class
576 _create_field_class_from_ptr_and_get_ref
= staticmethod(
577 _create_field_class_from_ptr_and_get_ref
581 class _OptionWithSelectorFieldClass(
582 _OptionWithSelectorFieldClassConst
, _OptionFieldClass
584 _NAME
= 'Option (with selector)'
587 class _OptionWithBoolSelectorFieldClass(
588 _OptionWithBoolSelectorFieldClassConst
, _OptionWithSelectorFieldClass
590 _NAME
= 'Option (with boolean selector)'
592 def _selector_is_reversed(self
, selector_is_reversed
):
593 utils
._check
_bool
(selector_is_reversed
)
594 native_bt
.field_class_option_with_selector_bool_set_selector_is_reversed(
595 self
._ptr
, selector_is_reversed
598 _selector_is_reversed
= property(fset
=_selector_is_reversed
)
601 class _OptionWithIntegerSelectorFieldClass(
602 _OptionWithIntegerSelectorFieldClassConst
, _OptionWithSelectorFieldClass
604 _NAME
= 'Option (with integer selector)'
607 class _OptionWithUnsignedIntegerSelectorFieldClass(
608 _OptionWithUnsignedIntegerSelectorFieldClassConst
,
609 _OptionWithIntegerSelectorFieldClass
,
611 _NAME
= 'Option (with unsigned integer selector)'
614 class _OptionWithSignedIntegerSelectorFieldClass(
615 _OptionWithSignedIntegerSelectorFieldClassConst
,
616 _OptionWithIntegerSelectorFieldClass
,
618 _NAME
= 'Option (with signed integer selector)'
621 class _VariantFieldClassOptionConst
:
622 _create_field_class_from_ptr_and_get_ref
= staticmethod(
623 _create_field_class_from_const_ptr_and_get_ref
625 _borrow_field_class_ptr
= staticmethod(
626 native_bt
.field_class_variant_option_borrow_field_class_const
628 _borrow_user_attributes_ptr
= staticmethod(
629 native_bt
.field_class_variant_option_borrow_user_attributes_const
631 _create_value_from_ptr_and_get_ref
= staticmethod(
632 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
635 def __init__(self
, owning_var_fc
, option_ptr
):
636 # this field class owns the option; keeping it here maintains
637 # the option alive as options are not shared objects
638 self
._owning
_var
_fc
= owning_var_fc
639 self
._ptr
= option_ptr
643 name
= native_bt
.field_class_variant_option_get_name(self
._ptr
)
644 assert name
is not None
648 def field_class(self
):
649 fc_ptr
= self
._borrow
_field
_class
_ptr
(self
._ptr
)
650 assert fc_ptr
is not None
651 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
654 def user_attributes(self
):
655 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
656 assert ptr
is not None
657 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
660 class _VariantFieldClassOption(_VariantFieldClassOptionConst
):
661 _create_field_class_from_ptr_and_get_ref
= staticmethod(
662 _create_field_class_from_ptr_and_get_ref
664 _borrow_field_class_ptr
= staticmethod(
665 native_bt
.field_class_variant_option_borrow_field_class
667 _borrow_user_attributes_ptr
= staticmethod(
668 native_bt
.field_class_variant_option_borrow_user_attributes
670 _create_value_from_ptr_and_get_ref
= staticmethod(
671 bt2_value
._create
_from
_ptr
_and
_get
_ref
674 def _user_attributes(self
, user_attributes
):
675 value
= bt2_value
.create_value(user_attributes
)
676 utils
._check
_type
(value
, bt2_value
.MapValue
)
677 native_bt
.field_class_variant_option_set_user_attributes(self
._ptr
, value
._ptr
)
679 _user_attributes
= property(fset
=_user_attributes
)
682 class _VariantFieldClassWithIntegerSelectorOptionConst(_VariantFieldClassOptionConst
):
683 def __init__(self
, owning_var_fc
, spec_opt_ptr
):
684 self
._spec
_ptr
= spec_opt_ptr
685 super().__init
__(owning_var_fc
, self
._as
_option
_ptr
(spec_opt_ptr
))
689 range_set_ptr
= self
._borrow
_ranges
_ptr
(self
._spec
_ptr
)
690 assert range_set_ptr
is not None
691 return self
._range
_set
_pycls
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
694 class _VariantFieldClassWithIntegerSelectorOption(
695 _VariantFieldClassWithIntegerSelectorOptionConst
, _VariantFieldClassOption
700 class _VariantFieldClassWithSignedIntegerSelectorOptionConst(
701 _VariantFieldClassWithIntegerSelectorOptionConst
703 _as_option_ptr
= staticmethod(
704 native_bt
.field_class_variant_with_selector_integer_signed_option_as_option_const
706 _borrow_ranges_ptr
= staticmethod(
707 native_bt
.field_class_variant_with_selector_integer_signed_option_borrow_ranges_const
709 _range_set_pycls
= bt2_integer_range_set
._SignedIntegerRangeSetConst
712 class _VariantFieldClassWithSignedIntegerSelectorOption(
713 _VariantFieldClassWithSignedIntegerSelectorOptionConst
,
714 _VariantFieldClassWithIntegerSelectorOption
,
719 class _VariantFieldClassWithUnsignedIntegerSelectorOptionConst(
720 _VariantFieldClassWithIntegerSelectorOptionConst
722 _as_option_ptr
= staticmethod(
723 native_bt
.field_class_variant_with_selector_integer_unsigned_option_as_option_const
725 _borrow_ranges_ptr
= staticmethod(
726 native_bt
.field_class_variant_with_selector_integer_unsigned_option_borrow_ranges_const
728 _range_set_pycls
= bt2_integer_range_set
._UnsignedIntegerRangeSetConst
731 class _VariantFieldClassWithUnsignedIntegerSelectorOption(
732 _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
,
733 _VariantFieldClassWithIntegerSelectorOption
,
738 class _VariantFieldClassConst(_FieldClassConst
, collections
.abc
.Mapping
):
739 _NAME
= 'Const Variant'
740 _borrow_option_ptr_by_name
= staticmethod(
741 native_bt
.field_class_variant_borrow_option_by_name_const
743 _borrow_option_ptr_by_index
= staticmethod(
744 native_bt
.field_class_variant_borrow_option_by_index_const
746 _variant_option_pycls
= _VariantFieldClassOptionConst
749 def _as_option_ptr(opt_ptr
):
752 def _create_option_from_ptr(self
, opt_ptr
):
753 return self
._variant
_option
_pycls
(self
, opt_ptr
)
756 count
= native_bt
.field_class_variant_get_option_count(self
._ptr
)
760 def __getitem__(self
, key
):
761 if not isinstance(key
, str):
763 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
766 opt_ptr
= self
._borrow
_option
_ptr
_by
_name
(self
._ptr
, key
)
771 return self
._create
_option
_from
_ptr
(opt_ptr
)
774 for idx
in range(len(self
)):
775 opt_ptr
= self
._borrow
_option
_ptr
_by
_index
(self
._ptr
, idx
)
776 assert opt_ptr
is not None
777 base_opt_ptr
= self
._as
_option
_ptr
(opt_ptr
)
778 yield native_bt
.field_class_variant_option_get_name(base_opt_ptr
)
780 def option_at_index(self
, index
):
781 utils
._check
_uint
64(index
)
783 if index
>= len(self
):
786 opt_ptr
= self
._borrow
_option
_ptr
_by
_index
(self
._ptr
, index
)
787 assert opt_ptr
is not None
788 return self
._create
_option
_from
_ptr
(opt_ptr
)
791 class _VariantFieldClass(_VariantFieldClassConst
, _FieldClass
, collections
.abc
.Mapping
):
793 _borrow_option_ptr_by_name
= staticmethod(
794 native_bt
.field_class_variant_borrow_option_by_name
796 _borrow_option_ptr_by_index
= staticmethod(
797 native_bt
.field_class_variant_borrow_option_by_index
799 _variant_option_pycls
= _VariantFieldClassOption
802 class _VariantFieldClassWithoutSelectorConst(_VariantFieldClassConst
):
803 _NAME
= 'Const Variant (without selector)'
806 class _VariantFieldClassWithoutSelector(
807 _VariantFieldClassWithoutSelectorConst
, _VariantFieldClass
809 _NAME
= 'Variant (without selector)'
811 def append_option(self
, name
, field_class
, user_attributes
=None):
812 utils
._check
_str
(name
)
813 utils
._check
_type
(field_class
, _FieldClass
)
816 raise ValueError("duplicate option name '{}'".format(name
))
818 user_attributes_value
= None
820 if user_attributes
is not None:
821 # check now that user attributes are valid
822 user_attributes_value
= bt2
.create_value(user_attributes
)
824 status
= native_bt
.field_class_variant_without_selector_append_option(
825 self
._ptr
, name
, field_class
._ptr
827 utils
._handle
_func
_status
(
828 status
, 'cannot append option to variant field class object'
831 if user_attributes
is not None:
832 self
[name
]._user
_attributes
= user_attributes_value
834 def __iadd__(self
, options
):
835 for name
, field_class
in options
:
836 self
.append_option(name
, field_class
)
841 class _VariantFieldClassWithIntegerSelectorConst(_VariantFieldClassConst
):
842 _NAME
= 'Const Variant (with selector)'
845 def selector_field_path(self
):
846 ptr
= native_bt
.field_class_variant_with_selector_borrow_selector_field_path_const(
853 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
856 class _VariantFieldClassWithIntegerSelector(
857 _VariantFieldClassWithIntegerSelectorConst
, _VariantFieldClass
859 _NAME
= 'Variant (with selector)'
861 def append_option(self
, name
, field_class
, ranges
, user_attributes
=None):
862 utils
._check
_str
(name
)
863 utils
._check
_type
(field_class
, _FieldClass
)
864 utils
._check
_type
(ranges
, self
._variant
_option
_pycls
._range
_set
_pycls
)
867 raise ValueError("duplicate option name '{}'".format(name
))
870 raise ValueError('range set is empty')
872 user_attributes_value
= None
874 if user_attributes
is not None:
875 # check now that user attributes are valid
876 user_attributes_value
= bt2
.create_value(user_attributes
)
878 # TODO: check overlaps (precondition of self._append_option())
880 status
= self
._append
_option
(self
._ptr
, name
, field_class
._ptr
, ranges
._ptr
)
881 utils
._handle
_func
_status
(
882 status
, 'cannot append option to variant field class object'
885 if user_attributes
is not None:
886 self
[name
]._user
_attributes
= user_attributes_value
888 def __iadd__(self
, options
):
889 for name
, field_class
, ranges
in options
:
890 self
.append_option(name
, field_class
, ranges
)
895 class _VariantFieldClassWithUnsignedIntegerSelectorConst(
896 _VariantFieldClassWithIntegerSelectorConst
898 _NAME
= 'Const Variant (with unsigned integer selector)'
899 _borrow_option_ptr_by_name
= staticmethod(
900 native_bt
.field_class_variant_with_selector_integer_unsigned_borrow_option_by_name_const
902 _borrow_option_ptr_by_index
= staticmethod(
903 native_bt
.field_class_variant_with_selector_integer_unsigned_borrow_option_by_index_const
905 _variant_option_pycls
= _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
906 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
909 class _VariantFieldClassWithUnsignedIntegerSelector(
910 _VariantFieldClassWithUnsignedIntegerSelectorConst
,
911 _VariantFieldClassWithIntegerSelector
,
913 _NAME
= 'Variant (with unsigned integer selector)'
914 _variant_option_pycls
= _VariantFieldClassWithUnsignedIntegerSelectorOption
915 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
916 _append_option
= staticmethod(
917 native_bt
.field_class_variant_with_selector_integer_unsigned_append_option
921 class _VariantFieldClassWithSignedIntegerSelectorConst(
922 _VariantFieldClassWithIntegerSelectorConst
924 _NAME
= 'Const Variant (with signed integer selector)'
925 _borrow_option_ptr_by_name
= staticmethod(
926 native_bt
.field_class_variant_with_selector_integer_signed_borrow_option_by_name_const
928 _borrow_option_ptr_by_index
= staticmethod(
929 native_bt
.field_class_variant_with_selector_integer_signed_borrow_option_by_index_const
931 _variant_option_pycls
= _VariantFieldClassWithSignedIntegerSelectorOptionConst
932 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
935 class _VariantFieldClassWithSignedIntegerSelector(
936 _VariantFieldClassWithSignedIntegerSelectorConst
,
937 _VariantFieldClassWithIntegerSelector
,
939 _NAME
= 'Variant (with signed integer selector)'
940 _variant_option_pycls
= _VariantFieldClassWithSignedIntegerSelectorOption
941 _as_option_ptr
= staticmethod(_variant_option_pycls
._as
_option
_ptr
)
942 _append_option
= staticmethod(
943 native_bt
.field_class_variant_with_selector_integer_signed_append_option
947 class _ArrayFieldClassConst(_FieldClassConst
):
948 _create_field_class_from_ptr_and_get_ref
= staticmethod(
949 _create_field_class_from_const_ptr_and_get_ref
951 _borrow_element_field_class
= staticmethod(
952 native_bt
.field_class_array_borrow_element_field_class_const
956 def element_field_class(self
):
957 elem_fc_ptr
= self
._borrow
_element
_field
_class
(self
._ptr
)
958 return self
._create
_field
_class
_from
_ptr
_and
_get
_ref
(elem_fc_ptr
)
961 class _ArrayFieldClass(_ArrayFieldClassConst
, _FieldClass
):
962 _create_field_class_from_ptr_and_get_ref
= staticmethod(
963 _create_field_class_from_ptr_and_get_ref
965 _borrow_element_field_class
= staticmethod(
966 native_bt
.field_class_array_borrow_element_field_class
970 class _StaticArrayFieldClassConst(_ArrayFieldClassConst
):
971 _NAME
= 'Const static array'
975 return native_bt
.field_class_array_static_get_length(self
._ptr
)
978 class _StaticArrayFieldClass(_StaticArrayFieldClassConst
, _ArrayFieldClass
):
979 _NAME
= 'Static array'
982 class _DynamicArrayFieldClassConst(_ArrayFieldClassConst
):
983 _NAME
= 'Const dynamic array'
986 def length_field_path(self
):
987 ptr
= native_bt
.field_class_array_dynamic_borrow_length_field_path_const(
993 return bt2_field_path
._FieldPathConst
._create
_from
_ptr
_and
_get
_ref
(ptr
)
996 class _DynamicArrayFieldClass(_DynamicArrayFieldClassConst
, _ArrayFieldClass
):
997 _NAME
= 'Dynamic Array'
1000 _FIELD_CLASS_TYPE_TO_CONST_OBJ
= {
1001 native_bt
.FIELD_CLASS_TYPE_BOOL
: _BoolFieldClassConst
,
1002 native_bt
.FIELD_CLASS_TYPE_BIT_ARRAY
: _BitArrayFieldClassConst
,
1003 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClassConst
,
1004 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClassConst
,
1005 native_bt
.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
: _SinglePrecisionRealFieldClassConst
,
1006 native_bt
.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
: _DoublePrecisionRealFieldClassConst
,
1007 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClassConst
,
1008 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClassConst
,
1009 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClassConst
,
1010 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClassConst
,
1011 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClassConst
,
1012 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY
: _DynamicArrayFieldClassConst
,
1013 native_bt
.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR
: _OptionFieldClassConst
,
1014 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR
: _OptionWithBoolSelectorFieldClassConst
,
1015 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR
: _OptionWithUnsignedIntegerSelectorFieldClassConst
,
1016 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR
: _OptionWithSignedIntegerSelectorFieldClassConst
,
1017 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
: _VariantFieldClassWithoutSelectorConst
,
1018 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR
: _VariantFieldClassWithUnsignedIntegerSelectorConst
,
1019 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR
: _VariantFieldClassWithSignedIntegerSelectorConst
,
1022 _FIELD_CLASS_TYPE_TO_OBJ
= {
1023 native_bt
.FIELD_CLASS_TYPE_BOOL
: _BoolFieldClass
,
1024 native_bt
.FIELD_CLASS_TYPE_BIT_ARRAY
: _BitArrayFieldClass
,
1025 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClass
,
1026 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClass
,
1027 native_bt
.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
: _SinglePrecisionRealFieldClass
,
1028 native_bt
.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
: _DoublePrecisionRealFieldClass
,
1029 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClass
,
1030 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClass
,
1031 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClass
,
1032 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClass
,
1033 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClass
,
1034 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY
: _DynamicArrayFieldClass
,
1035 native_bt
.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR
: _OptionFieldClass
,
1036 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR
: _OptionWithBoolSelectorFieldClass
,
1037 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR
: _OptionWithUnsignedIntegerSelectorFieldClass
,
1038 native_bt
.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR
: _OptionWithSignedIntegerSelectorFieldClass
,
1039 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
: _VariantFieldClassWithoutSelector
,
1040 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR
: _VariantFieldClassWithUnsignedIntegerSelector
,
1041 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR
: _VariantFieldClassWithSignedIntegerSelector
,