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 _IntegerFieldClass(_FieldClass
):
55 def field_value_range(self
):
56 size
= native_bt
.field_class_integer_get_field_value_range(self
._ptr
)
60 def _field_value_range(self
, size
):
61 if size
< 1 or size
> 64:
62 raise ValueError("Value is outside valid range [1, 64] ({})".format(size
))
63 native_bt
.field_class_integer_set_field_value_range(self
._ptr
, size
)
65 _field_value_range
= property(fset
=_field_value_range
)
68 def preferred_display_base(self
):
69 base
= native_bt
.field_class_integer_get_preferred_display_base(self
._ptr
)
73 def _preferred_display_base(self
, base
):
74 utils
._check
_uint
64(base
)
77 IntegerDisplayBase
.BINARY
,
78 IntegerDisplayBase
.OCTAL
,
79 IntegerDisplayBase
.DECIMAL
,
80 IntegerDisplayBase
.HEXADECIMAL
,
82 raise ValueError("Display base is not a valid IntegerDisplayBase value")
84 native_bt
.field_class_integer_set_preferred_display_base(self
._ptr
, base
)
86 _preferred_display_base
= property(fset
=_preferred_display_base
)
89 class _UnsignedIntegerFieldClass(_IntegerFieldClass
):
90 _NAME
= 'Unsigned integer'
93 class _SignedIntegerFieldClass(_IntegerFieldClass
):
94 _NAME
= 'Signed integer'
97 class _RealFieldClass(_FieldClass
):
101 def is_single_precision(self
):
102 return native_bt
.field_class_real_is_single_precision(self
._ptr
)
104 def _is_single_precision(self
, is_single_precision
):
105 utils
._check
_bool
(is_single_precision
)
106 native_bt
.field_class_real_set_is_single_precision(
107 self
._ptr
, is_single_precision
110 _is_single_precision
= property(fset
=_is_single_precision
)
113 # an enumeration field class mapping does not have a reference count, so
114 # we copy the properties here to avoid eventual memory access errors.
115 class _EnumerationFieldClassMapping
:
116 def __init__(self
, mapping_ptr
):
117 base_mapping_ptr
= self
._as
_enumeration
_field
_class
_mapping
_ptr
(mapping_ptr
)
118 self
._label
= native_bt
.field_class_enumeration_mapping_get_label(
121 assert self
._label
is not None
122 ranges_ptr
= self
._mapping
_borrow
_ranges
_ptr
(mapping_ptr
)
123 assert ranges_ptr
is not None
124 self
._ranges
= self
._ranges
_type
._create
_from
_ptr
_and
_get
_ref
(ranges_ptr
)
135 class _UnsignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping
):
136 _ranges_type
= bt2_integer_range_set
.UnsignedIntegerRangeSet
137 _as_enumeration_field_class_mapping_ptr
= staticmethod(
138 native_bt
.field_class_enumeration_unsigned_mapping_as_mapping_const
140 _mapping_borrow_ranges_ptr
= staticmethod(
141 native_bt
.field_class_enumeration_unsigned_mapping_borrow_ranges_const
145 class _SignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping
):
146 _ranges_type
= bt2_integer_range_set
.SignedIntegerRangeSet
147 _as_enumeration_field_class_mapping_ptr
= staticmethod(
148 native_bt
.field_class_enumeration_signed_mapping_as_mapping_const
150 _mapping_borrow_ranges_ptr
= staticmethod(
151 native_bt
.field_class_enumeration_signed_mapping_borrow_ranges_const
155 class _EnumerationFieldClass(_IntegerFieldClass
, collections
.abc
.Mapping
):
157 count
= native_bt
.field_class_enumeration_get_mapping_count(self
._ptr
)
161 def add_mapping(self
, label
, ranges
):
162 utils
._check
_str
(label
)
163 utils
._check
_type
(ranges
, self
._range
_set
_type
)
166 raise ValueError("duplicate mapping label '{}'".format(label
))
168 status
= self
._add
_mapping
(self
._ptr
, label
, ranges
._ptr
)
169 utils
._handle
_func
_status
(
170 status
, 'cannot add mapping to enumeration field class object'
173 def mappings_for_value(self
, value
):
174 status
, labels
= self
._get
_mapping
_labels
_for
_value
(self
._ptr
, value
)
175 utils
._handle
_func
_status
(
176 status
, 'cannot get mapping labels for value {}'.format(value
)
178 return [self
[label
] for label
in labels
]
181 for idx
in range(len(self
)):
182 mapping
= self
._get
_mapping
_by
_index
(self
._ptr
, idx
)
185 def __getitem__(self
, label
):
186 utils
._check
_str
(label
)
187 mapping
= self
._get
_mapping
_by
_label
(self
._ptr
, label
)
190 raise KeyError(label
)
194 def __iadd__(self
, mappings
):
195 for label
, ranges
in mappings
:
196 self
.add_mapping(label
, ranges
)
201 class _UnsignedEnumerationFieldClass(
202 _EnumerationFieldClass
, _UnsignedIntegerFieldClass
204 _NAME
= 'Unsigned enumeration'
205 _range_set_type
= bt2_integer_range_set
.UnsignedIntegerRangeSet
206 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_unsigned_add_mapping
)
209 def _get_mapping_by_index(enum_ptr
, index
):
210 mapping_ptr
= native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_index_const(
213 assert mapping_ptr
is not None
214 return _UnsignedEnumerationFieldClassMapping(mapping_ptr
)
217 def _get_mapping_by_label(enum_ptr
, label
):
218 mapping_ptr
= native_bt
.field_class_enumeration_unsigned_borrow_mapping_by_label_const(
222 if mapping_ptr
is None:
225 return _UnsignedEnumerationFieldClassMapping(mapping_ptr
)
228 def _get_mapping_labels_for_value(enum_ptr
, value
):
229 utils
._check
_uint
64(value
)
230 return native_bt
.field_class_enumeration_unsigned_get_mapping_labels_for_value(
235 class _SignedEnumerationFieldClass(_EnumerationFieldClass
, _SignedIntegerFieldClass
):
236 _NAME
= 'Signed enumeration'
237 _range_set_type
= bt2_integer_range_set
.SignedIntegerRangeSet
238 _add_mapping
= staticmethod(native_bt
.field_class_enumeration_signed_add_mapping
)
241 def _get_mapping_by_index(enum_ptr
, index
):
242 mapping_ptr
= native_bt
.field_class_enumeration_signed_borrow_mapping_by_index_const(
245 assert mapping_ptr
is not None
246 return _SignedEnumerationFieldClassMapping(mapping_ptr
)
249 def _get_mapping_by_label(enum_ptr
, label
):
250 mapping_ptr
= native_bt
.field_class_enumeration_signed_borrow_mapping_by_label_const(
254 if mapping_ptr
is None:
257 return _SignedEnumerationFieldClassMapping(mapping_ptr
)
260 def _get_mapping_labels_for_value(enum_ptr
, value
):
261 utils
._check
_int
64(value
)
262 return native_bt
.field_class_enumeration_signed_get_mapping_labels_for_value(
267 class _StringFieldClass(_FieldClass
):
271 class _StructureFieldClassMember
:
272 def __init__(self
, name
, field_class
):
274 self
._field
_class
= field_class
281 def field_class(self
):
282 return self
._field
_class
285 class _StructureFieldClass(_FieldClass
, collections
.abc
.Mapping
):
288 def append_member(self
, name
, field_class
):
289 utils
._check
_str
(name
)
290 utils
._check
_type
(field_class
, _FieldClass
)
293 raise ValueError("duplicate member name '{}'".format(name
))
295 status
= native_bt
.field_class_structure_append_member(
296 self
._ptr
, name
, field_class
._ptr
298 utils
._handle
_func
_status
(
299 status
, 'cannot append member to structure field class object'
303 count
= native_bt
.field_class_structure_get_member_count(self
._ptr
)
308 def _create_member_from_ptr(member_ptr
):
309 name
= native_bt
.field_class_structure_member_get_name(member_ptr
)
310 assert name
is not None
311 fc_ptr
= native_bt
.field_class_structure_member_borrow_field_class_const(
314 assert fc_ptr
is not None
315 fc
= _create_field_class_from_ptr_and_get_ref(fc_ptr
)
316 return _StructureFieldClassMember(name
, fc
)
318 def __getitem__(self
, key
):
319 if not isinstance(key
, str):
321 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
324 member_ptr
= native_bt
.field_class_structure_borrow_member_by_name_const(
328 if member_ptr
is None:
331 return self
._create
_member
_from
_ptr
(member_ptr
)
334 for idx
in range(len(self
)):
335 member_ptr
= native_bt
.field_class_structure_borrow_member_by_index_const(
338 assert member_ptr
is not None
339 yield native_bt
.field_class_structure_member_get_name(member_ptr
)
341 def __iadd__(self
, members
):
342 for name
, field_class
in members
:
343 self
.append_member(name
, field_class
)
347 def member_at_index(self
, index
):
348 utils
._check
_uint
64(index
)
350 if index
>= len(self
):
353 member_ptr
= native_bt
.field_class_structure_borrow_member_by_index_const(
356 assert member_ptr
is not None
357 return self
._create
_member
_from
_ptr
(member_ptr
)
360 class _VariantFieldClassOption
:
361 def __init__(self
, name
, field_class
):
363 self
._field
_class
= field_class
370 def field_class(self
):
371 return self
._field
_class
374 class _VariantFieldClassWithSelectorOption(_VariantFieldClassOption
):
375 def __init__(self
, name
, field_class
, ranges
):
376 super().__init
__(name
, field_class
)
377 self
._ranges
= ranges
384 class _VariantFieldClass(_FieldClass
, collections
.abc
.Mapping
):
386 _borrow_option_by_name_ptr
= staticmethod(
387 native_bt
.field_class_variant_borrow_option_by_name_const
389 _borrow_member_by_index_ptr
= staticmethod(
390 native_bt
.field_class_variant_borrow_option_by_index_const
394 def _as_option_ptr(opt_ptr
):
397 def _create_option_from_ptr(self
, opt_ptr
):
398 name
= native_bt
.field_class_variant_option_get_name(opt_ptr
)
399 assert name
is not None
400 fc_ptr
= native_bt
.field_class_variant_option_borrow_field_class_const(opt_ptr
)
401 assert fc_ptr
is not None
402 fc
= _create_field_class_from_ptr_and_get_ref(fc_ptr
)
403 return _VariantFieldClassOption(name
, fc
)
406 count
= native_bt
.field_class_variant_get_option_count(self
._ptr
)
410 def __getitem__(self
, key
):
411 if not isinstance(key
, str):
413 "key must be a 'str' object, got '{}'".format(key
.__class
__.__name
__)
416 opt_ptr
= self
._borrow
_option
_by
_name
_ptr
(self
._ptr
, key
)
421 return self
._create
_option
_from
_ptr
(opt_ptr
)
424 for idx
in range(len(self
)):
425 opt_ptr
= self
._borrow
_member
_by
_index
_ptr
(self
._ptr
, idx
)
426 assert opt_ptr
is not None
427 base_opt_ptr
= self
._as
_option
_ptr
(opt_ptr
)
428 yield native_bt
.field_class_variant_option_get_name(base_opt_ptr
)
430 def option_at_index(self
, index
):
431 utils
._check
_uint
64(index
)
433 if index
>= len(self
):
436 opt_ptr
= self
._borrow
_member
_by
_index
_ptr
(self
._ptr
, index
)
437 assert opt_ptr
is not None
438 return self
._create
_option
_from
_ptr
(opt_ptr
)
441 class _VariantFieldClassWithoutSelector(_VariantFieldClass
):
442 _NAME
= 'Variant (without selector)'
444 def append_option(self
, name
, field_class
):
445 utils
._check
_str
(name
)
446 utils
._check
_type
(field_class
, _FieldClass
)
449 raise ValueError("duplicate option name '{}'".format(name
))
451 status
= native_bt
.field_class_variant_without_selector_append_option(
452 self
._ptr
, name
, field_class
._ptr
454 utils
._handle
_func
_status
(
455 status
, 'cannot append option to variant field class object'
458 def __iadd__(self
, options
):
459 for name
, field_class
in options
:
460 self
.append_option(name
, field_class
)
465 class _VariantFieldClassWithSelector(_VariantFieldClass
):
466 _NAME
= 'Variant (with selector)'
468 def _create_option_from_ptr(self
, opt_ptr
):
469 base_opt_ptr
= self
._as
_option
_ptr
(opt_ptr
)
470 name
= native_bt
.field_class_variant_option_get_name(base_opt_ptr
)
471 assert name
is not None
472 fc_ptr
= native_bt
.field_class_variant_option_borrow_field_class_const(
475 assert fc_ptr
is not None
476 fc
= _create_field_class_from_ptr_and_get_ref(fc_ptr
)
477 range_set_ptr
= self
._option
_borrow
_ranges
_ptr
(opt_ptr
)
478 assert range_set_ptr
is not None
479 range_set
= self
._range
_set
_type
._create
_from
_ptr
_and
_get
_ref
(range_set_ptr
)
480 return _VariantFieldClassWithSelectorOption(name
, fc
, range_set
)
483 def selector_field_path(self
):
484 ptr
= native_bt
.field_class_variant_with_selector_borrow_selector_field_path_const(
491 return bt2_field_path
._FieldPath
._create
_from
_ptr
_and
_get
_ref
(ptr
)
493 def append_option(self
, name
, field_class
, ranges
):
494 utils
._check
_str
(name
)
495 utils
._check
_type
(field_class
, _FieldClass
)
496 utils
._check
_type
(ranges
, self
._range
_set
_type
)
499 raise ValueError("duplicate option name '{}'".format(name
))
502 raise ValueError('range set is empty')
504 # TODO: check overlaps (precondition of self._append_option())
506 status
= self
._append
_option
(self
._ptr
, name
, field_class
._ptr
, ranges
._ptr
)
507 utils
._handle
_func
_status
(
508 status
, 'cannot append option to variant field class object'
511 def __iadd__(self
, options
):
512 for name
, field_class
, ranges
in options
:
513 self
.append_option(name
, field_class
, ranges
)
518 class _VariantFieldClassWithUnsignedSelector(_VariantFieldClassWithSelector
):
519 _NAME
= 'Variant (with unsigned selector)'
520 _borrow_option_by_name_ptr
= staticmethod(
521 native_bt
.field_class_variant_with_selector_unsigned_borrow_option_by_name_const
523 _borrow_member_by_index_ptr
= staticmethod(
524 native_bt
.field_class_variant_with_selector_unsigned_borrow_option_by_index_const
526 _as_option_ptr
= staticmethod(
527 native_bt
.field_class_variant_with_selector_unsigned_option_as_option_const
529 _append_option
= staticmethod(
530 native_bt
.field_class_variant_with_selector_unsigned_append_option
532 _option_borrow_ranges_ptr
= staticmethod(
533 native_bt
.field_class_variant_with_selector_unsigned_option_borrow_ranges_const
535 _range_set_type
= bt2_integer_range_set
.UnsignedIntegerRangeSet
538 class _VariantFieldClassWithSignedSelector(_VariantFieldClassWithSelector
):
539 _NAME
= 'Variant (with signed selector)'
540 _borrow_option_by_name_ptr
= staticmethod(
541 native_bt
.field_class_variant_with_selector_signed_borrow_option_by_name_const
543 _borrow_member_by_index_ptr
= staticmethod(
544 native_bt
.field_class_variant_with_selector_signed_borrow_option_by_index_const
546 _as_option_ptr
= staticmethod(
547 native_bt
.field_class_variant_with_selector_signed_option_as_option_const
549 _append_option
= staticmethod(
550 native_bt
.field_class_variant_with_selector_signed_append_option
552 _option_borrow_ranges_ptr
= staticmethod(
553 native_bt
.field_class_variant_with_selector_signed_option_borrow_ranges_const
555 _range_set_type
= bt2_integer_range_set
.SignedIntegerRangeSet
558 class _ArrayFieldClass(_FieldClass
):
560 def element_field_class(self
):
561 elem_fc_ptr
= native_bt
.field_class_array_borrow_element_field_class_const(
564 return _create_field_class_from_ptr_and_get_ref(elem_fc_ptr
)
567 class _StaticArrayFieldClass(_ArrayFieldClass
):
570 return native_bt
.field_class_array_static_get_length(self
._ptr
)
573 class _DynamicArrayFieldClass(_ArrayFieldClass
):
575 def length_field_path(self
):
576 ptr
= native_bt
.field_class_array_dynamic_borrow_length_field_path_const(
582 return bt2_field_path
._FieldPath
._create
_from
_ptr
_and
_get
_ref
(ptr
)
585 _FIELD_CLASS_TYPE_TO_OBJ
= {
586 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerFieldClass
,
587 native_bt
.FIELD_CLASS_TYPE_SIGNED_INTEGER
: _SignedIntegerFieldClass
,
588 native_bt
.FIELD_CLASS_TYPE_REAL
: _RealFieldClass
,
589 native_bt
.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
: _UnsignedEnumerationFieldClass
,
590 native_bt
.FIELD_CLASS_TYPE_SIGNED_ENUMERATION
: _SignedEnumerationFieldClass
,
591 native_bt
.FIELD_CLASS_TYPE_STRING
: _StringFieldClass
,
592 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClass
,
593 native_bt
.FIELD_CLASS_TYPE_STATIC_ARRAY
: _StaticArrayFieldClass
,
594 native_bt
.FIELD_CLASS_TYPE_DYNAMIC_ARRAY
: _DynamicArrayFieldClass
,
595 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
: _VariantFieldClassWithoutSelector
,
596 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
: _VariantFieldClassWithUnsignedSelector
,
597 native_bt
.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
: _VariantFieldClassWithSignedSelector
,