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
30 def _create_field_class_from_ptr_and_get_ref(ptr
):
31 typeid
= native_bt
.field_class_get_type(ptr
)
32 return _FIELD_CLASS_TYPE_TO_OBJ
[typeid
]._create
_from
_ptr
_and
_get
_ref
(ptr
)
35 class IntegerDisplayBase
:
36 BINARY
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
37 OCTAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
38 DECIMAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
39 HEXADECIMAL
= native_bt
.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
42 class _FieldClass(object._SharedObject
):
43 _get_ref
= staticmethod(native_bt
.field_class_get_ref
)
44 _put_ref
= staticmethod(native_bt
.field_class_put_ref
)
46 def _check_create_status(self
, ptr
):
48 raise bt2
._MemoryError(
49 'cannot create {} field class object'.format(self
._NAME
.lower())
53 class _BoolFieldClass(_FieldClass
):
57 class _IntegerFieldClass(_FieldClass
):
59 def field_value_range(self
):
60 size
= native_bt
.field_class_integer_get_field_value_range(self
._ptr
)
64 def _field_value_range(self
, size
):
65 if size
< 1 or size
> 64:
66 raise ValueError("Value is outside valid range [1, 64] ({})".format(size
))
67 native_bt
.field_class_integer_set_field_value_range(self
._ptr
, size
)
69 _field_value_range
= property(fset
=_field_value_range
)
72 def preferred_display_base(self
):
73 base
= native_bt
.field_class_integer_get_preferred_display_base(self
._ptr
)
77 def _preferred_display_base(self
, base
):
78 utils
._check
_uint
64(base
)
81 IntegerDisplayBase
.BINARY
,
82 IntegerDisplayBase
.OCTAL
,
83 IntegerDisplayBase
.DECIMAL
,
84 IntegerDisplayBase
.HEXADECIMAL
,
86 raise ValueError("Display base is not a valid IntegerDisplayBase value")
88 native_bt
.field_class_integer_set_preferred_display_base(self
._ptr
, base
)
90 _preferred_display_base
= property(fset
=_preferred_display_base
)
93 class _UnsignedIntegerFieldClass(_IntegerFieldClass
):
94 _NAME
= 'Unsigned integer'
97 class _SignedIntegerFieldClass(_IntegerFieldClass
):
98 _NAME
= 'Signed integer'
101 class _RealFieldClass(_FieldClass
):
105 def is_single_precision(self
):
106 return native_bt
.field_class_real_is_single_precision(self
._ptr
)
108 def _is_single_precision(self
, is_single_precision
):
109 utils
._check
_bool
(is_single_precision
)
110 native_bt
.field_class_real_set_is_single_precision(
111 self
._ptr
, is_single_precision
114 _is_single_precision
= property(fset
=_is_single_precision
)
117 # an enumeration field class mapping does not have a reference count, so
118 # we copy the properties here to avoid eventual memory access errors.
119 class _EnumerationFieldClassMapping
:
120 def __init__(self
, mapping_ptr
):
121 base_mapping_ptr
= self
._as
_enumeration
_field
_class
_mapping
_ptr
(mapping_ptr
)
122 self
._label
= native_bt
.field_class_enumeration_mapping_get_label(
125 assert self
._label
is not None
126 ranges_ptr
= self
._mapping
_borrow
_ranges
_ptr
(mapping_ptr
)
127 assert ranges_ptr
is not None
128 self
._ranges
= self
._ranges
_type
._create
_from
_ptr
_and
_get
_ref
(ranges_ptr
)
139 class _UnsignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping
):
140 _ranges_type
= bt2_integer_range_set
.UnsignedIntegerRangeSet
141 _as_enumeration_field_class_mapping_ptr
= staticmethod(
142 native_bt
.field_class_enumeration_unsigned_mapping_as_mapping_const
144 _mapping_borrow_ranges_ptr
= staticmethod(
145 native_bt
.field_class_enumeration_unsigned_mapping_borrow_ranges_const
149 class _SignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping
):
150 _ranges_type
= bt2_integer_range_set
.SignedIntegerRangeSet
151 _as_enumeration_field_class_mapping_ptr
= staticmethod(
152 native_bt
.field_class_enumeration_signed_mapping_as_mapping_const
154 _mapping_borrow_ranges_ptr
= staticmethod(
155 native_bt
.field_class_enumeration_signed_mapping_borrow_ranges_const
159 class _EnumerationFieldClass(_IntegerFieldClass
, collections
.abc
.Mapping
):
161 count
= native_bt
.field_class_enumeration_get_mapping_count(self
._ptr
)
165 def add_mapping(self
, label
, ranges
):
166 utils
._check
_str
(label
)
167 utils
._check
_type
(ranges
, self
._range
_set
_type
)
170 raise ValueError("duplicate mapping label '{}'".format(label
))
172 status
= self
._add
_mapping
(self
._ptr
, label
, ranges
._ptr
)
173 utils
._handle
_func
_status
(
174 status
, 'cannot add mapping to enumeration field class object'
177 def mappings_for_value(self
, value
):
178 status
, labels
= self
._get
_mapping
_labels
_for
_value
(self
._ptr
, value
)
179 utils
._handle
_func
_status
(
180 status
, 'cannot get mapping labels for value {}'.format(value
)
182 return [self
[label
] for label
in labels
]
185 for idx
in range(len(self
)):
186 mapping
= self
._get
_mapping
_by
_index
(self
._ptr
, idx
)
189 def __getitem__(self
, label
):
190 utils
._check
_str
(label
)
191 mapping
= self
._get
_mapping
_by
_label
(self
._ptr
, label
)
194 raise KeyError(label
)
198 def __iadd__(self
, mappings
):
199 for label
, ranges
in mappings
:
200 self
.add_mapping(label
, ranges
)
205 class _UnsignedEnumerationFieldClass(
206 _EnumerationFieldClass
, _UnsignedIntegerFieldClass
208 _NAME
= 'Unsigned enumeration'
209 _range_set_type
= bt2_integer_range_set
.UnsignedIntegerRangeSet
210 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_unsigned_add_mapping
)
213 def _get_mapping_by_index(enum_ptr
, index
):
214 mapping_ptr
= native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_index_const(
217 assert mapping_ptr
is not None
218 return _UnsignedEnumerationFieldClassMapping(mapping_ptr
)
221 def _get_mapping_by_label(enum_ptr
, label
):
222 mapping_ptr
= native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_label_const(
226 if mapping_ptr
is None:
229 return _UnsignedEnumerationFieldClassMapping(mapping_ptr
)
232 def _get_mapping_labels_for_value(enum_ptr
, value
):
233 utils
._check
_uint
64(value
)
234 return native_bt
.field_class_enumeration_unsigned_get_mapping_labels_for_value(
239 class _SignedEnumerationFieldClass(_EnumerationFieldClass
, _SignedIntegerFieldClass
):
240 _NAME
= 'Signed enumeration'
241 _range_set_type
= bt2_integer_range_set
.SignedIntegerRangeSet
242 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_signed_add_mapping
)
245 def _get_mapping_by_index(enum_ptr
, index
):
246 mapping_ptr
= native_bt
.field_class_enumeration_signed_borrow_mapping_by_index_const(
249 assert mapping_ptr
is not None
250 return _SignedEnumerationFieldClassMapping(mapping_ptr
)
253 def _get_mapping_by_label(enum_ptr
, label
):
254 mapping_ptr
= native_bt
.field_class_enumeration_signed_borrow_mapping_by_label_const(
258 if mapping_ptr
is None:
261 return _SignedEnumerationFieldClassMapping(mapping_ptr
)
264 def _get_mapping_labels_for_value(enum_ptr
, value
):
265 utils
._check
_int
64(value
)
266 return native_bt
.field_class_enumeration_signed_get_mapping_labels_for_value(
271 class _StringFieldClass(_FieldClass
):
275 class _StructureFieldClassMember
:
276 def __init__(self
, name
, field_class
):
278 self
._field
_class
= field_class
285 def field_class(self
):
286 return self
._field
_class
289 class _StructureFieldClass(_FieldClass
, collections
.abc
.Mapping
):
292 def append_member(self
, name
, field_class
):
293 utils
._check
_str
(name
)
294 utils
._check
_type
(field_class
, _FieldClass
)
297 raise ValueError("duplicate member name '{}'".format(name
))
299 status
= native_bt
.field_class_structure_append_member(
300 self
._ptr
, name
, field_class
._ptr
302 utils
._handle
_func
_status
(
303 status
, 'cannot append member to structure field class object'
307 count
= native_bt
.field_class_structure_get_member_count(self
._ptr
)
312 def _create_member_from_ptr(member_ptr
):
313 name
= native_bt
.field_class_structure_member_get_name(member_ptr
)
314 assert name
is not None
315 fc_ptr
= native_bt
.field_class_structure_member_borrow_field_class_const(
318 assert fc_ptr
is not None
319 fc
= _create_field_class_from_ptr_and_get_ref(fc_ptr
)
320 return _StructureFieldClassMember(name
, fc
)
322 def __getitem__(self
, key
):
323 if not isinstance(key
, str):
325 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
328 member_ptr
= native_bt
.field_class_structure_borrow_member_by_name_const(
332 if member_ptr
is None:
335 return self
._create
_member
_from
_ptr
(member_ptr
)
338 for idx
in range(len(self
)):
339 member_ptr
= native_bt
.field_class_structure_borrow_member_by_index_const(
342 assert member_ptr
is not None
343 yield native_bt
.field_class_structure_member_get_name(member_ptr
)
345 def __iadd__(self
, members
):
346 for name
, field_class
in members
:
347 self
.append_member(name
, field_class
)
351 def member_at_index(self
, index
):
352 utils
._check
_uint
64(index
)
354 if index
>= len(self
):
357 member_ptr
= native_bt
.field_class_structure_borrow_member_by_index_const(
360 assert member_ptr
is not None
361 return self
._create
_member
_from
_ptr
(member_ptr
)
364 class _VariantFieldClassOption
:
365 def __init__(self
, name
, field_class
):
367 self
._field
_class
= field_class
374 def field_class(self
):
375 return self
._field
_class
378 class _VariantFieldClassWithSelectorOption(_VariantFieldClassOption
):
379 def __init__(self
, name
, field_class
, ranges
):
380 super().__init
__(name
, field_class
)
381 self
._ranges
= ranges
388 class _VariantFieldClass(_FieldClass
, collections
.abc
.Mapping
):
390 _borrow_option_by_name_ptr
= staticmethod(
391 native_bt
.field_class_variant_borrow_option_by_name_const
393 _borrow_member_by_index_ptr
= staticmethod(
394 native_bt
.field_class_variant_borrow_option_by_index_const
398 def _as_option_ptr(opt_ptr
):
401 def _create_option_from_ptr(self
, opt_ptr
):
402 name
= native_bt
.field_class_variant_option_get_name(opt_ptr
)
403 assert name
is not None
404 fc_ptr
= native_bt
.field_class_variant_option_borrow_field_class_const(opt_ptr
)
405 assert fc_ptr
is not None
406 fc
= _create_field_class_from_ptr_and_get_ref(fc_ptr
)
407 return _VariantFieldClassOption(name
, fc
)
410 count
= native_bt
.field_class_variant_get_option_count(self
._ptr
)
414 def __getitem__(self
, key
):
415 if not isinstance(key
, str):
417 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
420 opt_ptr
= self
._borrow
_option
_by
_name
_ptr
(self
._ptr
, key
)
425 return self
._create
_option
_from
_ptr
(opt_ptr
)
428 for idx
in range(len(self
)):
429 opt_ptr
= self
._borrow
_member
_by
_index
_ptr
(self
._ptr
, idx
)
430 assert opt_ptr
is not None
431 base_opt_ptr
= self
._as
_option
_ptr
(opt_ptr
)
432 yield native_bt
.field_class_variant_option_get_name(base_opt_ptr
)
434 def option_at_index(self
, index
):
435 utils
._check
_uint
64(index
)
437 if index
>= len(self
):
440 opt_ptr
= self
._borrow
_member
_by
_index
_ptr
(self
._ptr
, index
)
441 assert opt_ptr
is not None
442 return self
._create
_option
_from
_ptr
(opt_ptr
)
445 class _VariantFieldClassWithoutSelector(_VariantFieldClass
):
446 _NAME
= 'Variant (without selector)'
448 def append_option(self
, name
, field_class
):
449 utils
._check
_str
(name
)
450 utils
._check
_type
(field_class
, _FieldClass
)
453 raise ValueError("duplicate option name '{}'".format(name
))
455 status
= native_bt
.field_class_variant_without_selector_append_option(
456 self
._ptr
, name
, field_class
._ptr
458 utils
._handle
_func
_status
(
459 status
, 'cannot append option to variant field class object'
462 def __iadd__(self
, options
):
463 for name
, field_class
in options
:
464 self
.append_option(name
, field_class
)
469 class _VariantFieldClassWithSelector(_VariantFieldClass
):
470 _NAME
= 'Variant (with selector)'
472 def _create_option_from_ptr(self
, opt_ptr
):
473 base_opt_ptr
= self
._as
_option
_ptr
(opt_ptr
)
474 name
= native_bt
.field_class_variant_option_get_name(base_opt_ptr
)
475 assert name
is not None
476 fc_ptr
= native_bt
.field_class_variant_option_borrow_field_class_const(
479 assert fc_ptr
is not None
480 fc
= _create_field_class_from_ptr_and_get_ref(fc_ptr
)
481 range_set_ptr
= self
._option
_borrow
_ranges
_ptr
(opt_ptr
)
482 assert range_set_ptr
is not None
483 range_set
= self
._range
_set
_type
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
484 return _VariantFieldClassWithSelectorOption(name
, fc
, range_set
)
487 def selector_field_path(self
):
488 ptr
= native_bt
.field_class_variant_with_selector_borrow_selector_field_path_const(
495 return bt2_field_path
._FieldPath
._create
_from
_ptr
_and
_get
_ref
(ptr
)
497 def append_option(self
, name
, field_class
, ranges
):
498 utils
._check
_str
(name
)
499 utils
._check
_type
(field_class
, _FieldClass
)
500 utils
._check
_type
(ranges
, self
._range
_set
_type
)
503 raise ValueError("duplicate option name '{}'".format(name
))
506 raise ValueError('range set is empty')
508 # TODO: check overlaps (precondition of self._append_option())
510 status
= self
._append
_option
(self
._ptr
, name
, field_class
._ptr
, ranges
._ptr
)
511 utils
._handle
_func
_status
(
512 status
, 'cannot append option to variant field class object'
515 def __iadd__(self
, options
):
516 for name
, field_class
, ranges
in options
:
517 self
.append_option(name
, field_class
, ranges
)
522 class _VariantFieldClassWithUnsignedSelector(_VariantFieldClassWithSelector
):
523 _NAME
= 'Variant (with unsigned selector)'
524 _borrow_option_by_name_ptr
= staticmethod(
525 native_bt
.field_class_variant_with_selector_unsigned_borrow_option_by_name_const
527 _borrow_member_by_index_ptr
= staticmethod(
528 native_bt
.field_class_variant_with_selector_unsigned_borrow_option_by_index_const
530 _as_option_ptr
= staticmethod(
531 native_bt
.field_class_variant_with_selector_unsigned_option_as_option_const
533 _append_option
= staticmethod(
534 native_bt
.field_class_variant_with_selector_unsigned_append_option
536 _option_borrow_ranges_ptr
= staticmethod(
537 native_bt
.field_class_variant_with_selector_unsigned_option_borrow_ranges_const
539 _range_set_type
= bt2_integer_range_set
.UnsignedIntegerRangeSet
542 class _VariantFieldClassWithSignedSelector(_VariantFieldClassWithSelector
):
543 _NAME
= 'Variant (with signed selector)'
544 _borrow_option_by_name_ptr
= staticmethod(
545 native_bt
.field_class_variant_with_selector_signed_borrow_option_by_name_const
547 _borrow_member_by_index_ptr
= staticmethod(
548 native_bt
.field_class_variant_with_selector_signed_borrow_option_by_index_const
550 _as_option_ptr
= staticmethod(
551 native_bt
.field_class_variant_with_selector_signed_option_as_option_const
553 _append_option
= staticmethod(
554 native_bt
.field_class_variant_with_selector_signed_append_option
556 _option_borrow_ranges_ptr
= staticmethod(
557 native_bt
.field_class_variant_with_selector_signed_option_borrow_ranges_const
559 _range_set_type
= bt2_integer_range_set
.SignedIntegerRangeSet
562 class _ArrayFieldClass(_FieldClass
):
564 def element_field_class(self
):
565 elem_fc_ptr
= native_bt
.field_class_array_borrow_element_field_class_const(
568 return _create_field_class_from_ptr_and_get_ref(elem_fc_ptr
)
571 class _StaticArrayFieldClass(_ArrayFieldClass
):
574 return native_bt
.field_class_array_static_get_length(self
._ptr
)
577 class _DynamicArrayFieldClass(_ArrayFieldClass
):
579 def length_field_path(self
):
580 ptr
= native_bt
.field_class_array_dynamic_borrow_length_field_path_const(
586 return bt2_field_path
._FieldPath
._create
_from
_ptr
_and
_get
_ref
(ptr
)
589 _FIELD_CLASS_TYPE_TO_OBJ
= {
590 native_bt
.FIELD_CLASS_TYPE_BOOL
: _BoolFieldClass
,
591 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClass
,
592 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClass
,
593 native_bt
.FIELD_CLASS_TYPE_REAL
: _RealFieldClass
,
594 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClass
,
595 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClass
,
596 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClass
,
597 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClass
,
598 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClass
,
599 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY
: _DynamicArrayFieldClass
,
600 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
: _VariantFieldClassWithoutSelector
,
601 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
: _VariantFieldClassWithUnsignedSelector
,
602 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
: _VariantFieldClassWithSignedSelector
,