X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fbindings%2Fpython%2Fbt2%2Fbt2%2Ffield_class.py;h=03e6e4e3569c405dea401410da5e78f1a482a9fa;hb=f0a42b33ac3951cd5cb2ee0f66ac04437a681621;hp=27a37d96baeccedebc9c7de81c9ef2d0d84a6299;hpb=578e048b5debf169e286e5b5cc747b5d6c16886d;p=babeltrace.git diff --git a/src/bindings/python/bt2/bt2/field_class.py b/src/bindings/python/bt2/bt2/field_class.py index 27a37d96..03e6e4e3 100644 --- a/src/bindings/python/bt2/bt2/field_class.py +++ b/src/bindings/python/bt2/bt2/field_class.py @@ -22,14 +22,27 @@ from bt2 import native_bt, object, utils import collections.abc -import bt2.field -import bt2.field_path +from bt2 import field_path as bt2_field_path +from bt2 import integer_range_set as bt2_integer_range_set +from bt2 import value as bt2_value import bt2 -def _create_field_class_from_ptr_and_get_ref(ptr): +def _create_field_class_from_ptr_and_get_ref_template(type_map, ptr): typeid = native_bt.field_class_get_type(ptr) - return _FIELD_CLASS_TYPE_TO_OBJ[typeid]._create_from_ptr_and_get_ref(ptr) + return type_map[typeid]._create_from_ptr_and_get_ref(ptr) + + +def _create_field_class_from_ptr_and_get_ref(ptr): + return _create_field_class_from_ptr_and_get_ref_template( + _FIELD_CLASS_TYPE_TO_OBJ, ptr + ) + + +def _create_field_class_from_const_ptr_and_get_ref(ptr): + return _create_field_class_from_ptr_and_get_ref_template( + _FIELD_CLASS_TYPE_TO_CONST_OBJ, ptr + ) class IntegerDisplayBase: @@ -39,22 +52,82 @@ class IntegerDisplayBase: HEXADECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL -class _FieldClass(object._SharedObject): +class _FieldClassConst(object._SharedObject): _get_ref = staticmethod(native_bt.field_class_get_ref) _put_ref = staticmethod(native_bt.field_class_put_ref) + _borrow_user_attributes_ptr = staticmethod( + native_bt.field_class_borrow_user_attributes_const + ) + _create_value_from_ptr_and_get_ref = staticmethod( + bt2_value._create_from_const_ptr_and_get_ref + ) def _check_create_status(self, ptr): if ptr is None: - raise bt2.CreationError('cannot create {} field class object'.format(self._NAME.lower())) + raise bt2._MemoryError( + 'cannot create {} field class object'.format(self._NAME.lower()) + ) + + @property + def user_attributes(self): + ptr = self._borrow_user_attributes_ptr(self._ptr) + assert ptr is not None + return self._create_value_from_ptr_and_get_ref(ptr) + + +class _FieldClass(_FieldClassConst): + _borrow_user_attributes_ptr = staticmethod( + native_bt.field_class_borrow_user_attributes + ) + _create_value_from_ptr_and_get_ref = staticmethod( + bt2_value._create_from_ptr_and_get_ref + ) + + def _user_attributes(self, user_attributes): + value = bt2_value.create_value(user_attributes) + utils._check_type(value, bt2_value.MapValue) + native_bt.field_class_set_user_attributes(self._ptr, value._ptr) + _user_attributes = property(fset=_user_attributes) + + +class _BoolFieldClassConst(_FieldClassConst): + _NAME = 'Const Boolean' + + +class _BoolFieldClass(_BoolFieldClassConst, _FieldClass): + _NAME = 'Boolean' + + +class _BitArrayFieldClassConst(_FieldClassConst): + _NAME = 'Const Bit array' -class _IntegerFieldClass(_FieldClass): + @property + def length(self): + length = native_bt.field_class_bit_array_get_length(self._ptr) + assert length >= 1 + return length + + +class _BitArrayFieldClass(_BitArrayFieldClassConst, _FieldClass): + _NAME = 'Bit array' + + +class _IntegerFieldClassConst(_FieldClassConst): @property def field_value_range(self): size = native_bt.field_class_integer_get_field_value_range(self._ptr) - assert(size >= 1) + assert size >= 1 return size + @property + def preferred_display_base(self): + base = native_bt.field_class_integer_get_preferred_display_base(self._ptr) + assert base >= 0 + return base + + +class _IntegerFieldClass(_FieldClass, _IntegerFieldClassConst): def _field_value_range(self, size): if size < 1 or size > 64: raise ValueError("Value is outside valid range [1, 64] ({})".format(size)) @@ -62,328 +135,794 @@ class _IntegerFieldClass(_FieldClass): _field_value_range = property(fset=_field_value_range) - @property - def preferred_display_base(self): - base = native_bt.field_class_integer_get_preferred_display_base( - self._ptr) - assert(base >= 0) - return base - def _preferred_display_base(self, base): utils._check_uint64(base) - if base not in (IntegerDisplayBase.BINARY, - IntegerDisplayBase.OCTAL, - IntegerDisplayBase.DECIMAL, - IntegerDisplayBase.HEXADECIMAL): + if base not in ( + IntegerDisplayBase.BINARY, + IntegerDisplayBase.OCTAL, + IntegerDisplayBase.DECIMAL, + IntegerDisplayBase.HEXADECIMAL, + ): raise ValueError("Display base is not a valid IntegerDisplayBase value") - native_bt.field_class_integer_set_preferred_display_base( - self._ptr, base) + native_bt.field_class_integer_set_preferred_display_base(self._ptr, base) _preferred_display_base = property(fset=_preferred_display_base) -class _UnsignedIntegerFieldClass(_IntegerFieldClass): +class _UnsignedIntegerFieldClassConst(_IntegerFieldClassConst, _FieldClassConst): + _NAME = 'Const unsigned integer' + + +class _UnsignedIntegerFieldClass( + _UnsignedIntegerFieldClassConst, _IntegerFieldClass, _FieldClass +): _NAME = 'Unsigned integer' -class _SignedIntegerFieldClass(_IntegerFieldClass): +class _SignedIntegerFieldClassConst(_IntegerFieldClassConst, _FieldClassConst): + _NAME = 'Const signed integer' + + +class _SignedIntegerFieldClass( + _SignedIntegerFieldClassConst, _IntegerFieldClass, _FieldClass +): _NAME = 'Signed integer' -class _RealFieldClass(_FieldClass): - _NAME = 'Real' +class _RealFieldClassConst(_FieldClassConst): + _NAME = 'Const real' @property def is_single_precision(self): return native_bt.field_class_real_is_single_precision(self._ptr) + +class _RealFieldClass(_FieldClass, _RealFieldClassConst): + _NAME = 'Real' + def _is_single_precision(self, is_single_precision): utils._check_bool(is_single_precision) native_bt.field_class_real_set_is_single_precision( - self._ptr, is_single_precision) + self._ptr, is_single_precision + ) _is_single_precision = property(fset=_is_single_precision) -class _EnumerationFieldClassMappingRange: - def __init__(self, lower, upper): - self._lower = lower - self._upper = upper +# an enumeration field class mapping does not have a reference count, so +# we copy the properties here to avoid eventual memory access errors. +class _EnumerationFieldClassMapping: + def __init__(self, mapping_ptr): + base_mapping_ptr = self._as_enumeration_field_class_mapping_ptr(mapping_ptr) + self._label = native_bt.field_class_enumeration_mapping_get_label( + base_mapping_ptr + ) + assert self._label is not None + ranges_ptr = self._mapping_borrow_ranges_ptr(mapping_ptr) + assert ranges_ptr is not None + self._ranges = self._ranges_type._create_from_ptr_and_get_ref(ranges_ptr) @property - def lower(self): - return self._lower + def label(self): + return self._label @property - def upper(self): - return self._upper + def ranges(self): + return self._ranges - def __eq__(self, other): - return self.lower == other.lower and self.upper == other.upper +class _UnsignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping): + _ranges_type = bt2_integer_range_set.UnsignedIntegerRangeSet + _as_enumeration_field_class_mapping_ptr = staticmethod( + native_bt.field_class_enumeration_unsigned_mapping_as_mapping_const + ) + _mapping_borrow_ranges_ptr = staticmethod( + native_bt.field_class_enumeration_unsigned_mapping_borrow_ranges_const + ) -class _EnumerationFieldClassMapping(collections.abc.Set): - def __init__(self, mapping_ptr): - self._mapping_ptr = mapping_ptr - @property - def label(self): - mapping_ptr = self._as_enumeration_field_class_mapping_ptr(self._mapping_ptr) - label = native_bt.field_class_enumeration_mapping_get_label(mapping_ptr) - assert label is not None - return label +class _SignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping): + _ranges_type = bt2_integer_range_set.SignedIntegerRangeSet + _as_enumeration_field_class_mapping_ptr = staticmethod( + native_bt.field_class_enumeration_signed_mapping_as_mapping_const + ) + _mapping_borrow_ranges_ptr = staticmethod( + native_bt.field_class_enumeration_signed_mapping_borrow_ranges_const + ) + +class _EnumerationFieldClassConst(_IntegerFieldClassConst, collections.abc.Mapping): def __len__(self): - mapping_ptr = self._as_enumeration_field_class_mapping_ptr(self._mapping_ptr) - return native_bt.field_class_enumeration_mapping_get_range_count(mapping_ptr) + count = native_bt.field_class_enumeration_get_mapping_count(self._ptr) + assert count >= 0 + return count + + def mappings_for_value(self, value): + self._check_int_type(value) - def __contains__(self, other_range): - for curr_range in self: - if curr_range == other_range: - return True - return False + status, labels = self._get_mapping_labels_for_value(self._ptr, value) + utils._handle_func_status( + status, 'cannot get mapping labels for value {}'.format(value) + ) + return [self[label] for label in labels] def __iter__(self): for idx in range(len(self)): - lower, upper = self._get_range_by_index(self._mapping_ptr, idx) - yield _EnumerationFieldClassMappingRange(lower, upper) + mapping_ptr = self._borrow_mapping_ptr_by_index(self._ptr, idx) + yield self._mapping_pycls(mapping_ptr).label + def __getitem__(self, label): + utils._check_str(label) + mapping_ptr = self._borrow_mapping_ptr_by_label(self._ptr, label) -class _UnsignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping): - _as_enumeration_field_class_mapping_ptr = staticmethod(native_bt.field_class_unsigned_enumeration_mapping_as_mapping_const) - _get_range_by_index = staticmethod(native_bt.field_class_unsigned_enumeration_mapping_get_range_by_index) + if mapping_ptr is None: + raise KeyError(label) + return self._mapping_pycls(mapping_ptr) -class _SignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping): - _as_enumeration_field_class_mapping_ptr = staticmethod(native_bt.field_class_signed_enumeration_mapping_as_mapping_const) - _get_range_by_index = staticmethod(native_bt.field_class_signed_enumeration_mapping_get_range_by_index) +class _EnumerationFieldClass(_EnumerationFieldClassConst, _IntegerFieldClass): + def add_mapping(self, label, ranges): + utils._check_str(label) + utils._check_type(ranges, self._range_set_type) + + if label in self: + raise ValueError("duplicate mapping label '{}'".format(label)) + + status = self._add_mapping(self._ptr, label, ranges._ptr) + utils._handle_func_status( + status, 'cannot add mapping to enumeration field class object' + ) + + def __iadd__(self, mappings): + for label, ranges in mappings: + self.add_mapping(label, ranges) + + return self + + +class _UnsignedEnumerationFieldClassConst( + _EnumerationFieldClassConst, _UnsignedIntegerFieldClassConst +): + _NAME = 'Const nsigned enumeration' + _range_set_type = bt2_integer_range_set.UnsignedIntegerRangeSet + _borrow_mapping_ptr_by_label = staticmethod( + native_bt.field_class_enumeration_unsigned_borrow_mapping_by_label_const + ) + _borrow_mapping_ptr_by_index = staticmethod( + native_bt.field_class_enumeration_unsigned_borrow_mapping_by_index_const + ) + _mapping_pycls = property(lambda _: _UnsignedEnumerationFieldClassMappingConst) + _get_mapping_labels_for_value = staticmethod( + native_bt.field_class_enumeration_unsigned_get_mapping_labels_for_value + ) + _check_int_type = staticmethod(utils._check_uint64) + + +class _UnsignedEnumerationFieldClass( + _UnsignedEnumerationFieldClassConst, + _EnumerationFieldClass, + _UnsignedIntegerFieldClass, +): + _NAME = 'Unsigned enumeration' + _add_mapping = staticmethod(native_bt.field_class_enumeration_unsigned_add_mapping) + + +class _SignedEnumerationFieldClassConst( + _EnumerationFieldClassConst, _SignedIntegerFieldClassConst +): + _NAME = 'Const signed enumeration' + _range_set_type = bt2_integer_range_set.SignedIntegerRangeSet + _borrow_mapping_ptr_by_label = staticmethod( + native_bt.field_class_enumeration_signed_borrow_mapping_by_label_const + ) + _borrow_mapping_ptr_by_index = staticmethod( + native_bt.field_class_enumeration_signed_borrow_mapping_by_index_const + ) + _mapping_pycls = property(lambda _: _SignedEnumerationFieldClassMappingConst) + _get_mapping_labels_for_value = staticmethod( + native_bt.field_class_enumeration_signed_get_mapping_labels_for_value + ) + _check_int_type = staticmethod(utils._check_int64) + + +class _SignedEnumerationFieldClass( + _SignedEnumerationFieldClassConst, _EnumerationFieldClass, _SignedIntegerFieldClass +): + _NAME = 'Signed enumeration' + _add_mapping = staticmethod(native_bt.field_class_enumeration_signed_add_mapping) + + +class _StringFieldClassConst(_FieldClassConst): + _NAME = 'Const string' + + +class _StringFieldClass(_StringFieldClassConst, _FieldClass): + _NAME = 'String' + + +class _StructureFieldClassMemberConst: + _create_field_class_from_ptr_and_get_ref = staticmethod( + _create_field_class_from_const_ptr_and_get_ref + ) + _borrow_field_class_ptr = staticmethod( + native_bt.field_class_structure_member_borrow_field_class_const + ) + _borrow_user_attributes_ptr = staticmethod( + native_bt.field_class_structure_member_borrow_user_attributes_const + ) + _create_value_from_ptr_and_get_ref = staticmethod( + bt2_value._create_from_const_ptr_and_get_ref + ) + + def __init__(self, owning_struct_fc, member_ptr): + # this field class owns the member; keeping it here maintains + # the member alive as members are not shared objects + self._owning_struct_fc = owning_struct_fc + self._ptr = member_ptr + + @property + def name(self): + name = native_bt.field_class_structure_member_get_name(self._ptr) + assert name is not None + return name + + @property + def field_class(self): + fc_ptr = self._borrow_field_class_ptr(self._ptr) + assert fc_ptr is not None + return self._create_field_class_from_ptr_and_get_ref(fc_ptr) + + @property + def user_attributes(self): + ptr = self._borrow_user_attributes_ptr(self._ptr) + assert ptr is not None + return self._create_value_from_ptr_and_get_ref(ptr) + + +class _StructureFieldClassMember(_StructureFieldClassMemberConst): + _borrow_field_class_ptr = staticmethod( + native_bt.field_class_structure_member_borrow_field_class + ) + _borrow_user_attributes_ptr = staticmethod( + native_bt.field_class_structure_member_borrow_user_attributes + ) + _create_field_class_from_ptr_and_get_ref = staticmethod( + _create_field_class_from_ptr_and_get_ref + ) + _create_value_from_ptr_and_get_ref = staticmethod( + bt2_value._create_from_ptr_and_get_ref + ) + + def _user_attributes(self, user_attributes): + value = bt2_value.create_value(user_attributes) + utils._check_type(value, bt2_value.MapValue) + native_bt.field_class_structure_member_set_user_attributes( + self._ptr, value._ptr + ) + + _user_attributes = property(fset=_user_attributes) + + +class _StructureFieldClassConst(_FieldClassConst, collections.abc.Mapping): + _NAME = 'Const structure' + _borrow_member_ptr_by_index = staticmethod( + native_bt.field_class_structure_borrow_member_by_index_const + ) + _borrow_member_ptr_by_name = staticmethod( + native_bt.field_class_structure_borrow_member_by_name_const + ) + _structure_member_field_class_pycls = property( + lambda _: _StructureFieldClassMemberConst + ) -class _EnumerationFieldClass(_IntegerFieldClass, collections.abc.Mapping): def __len__(self): - count = native_bt.field_class_enumeration_get_mapping_count(self._ptr) - assert(count >= 0) + count = native_bt.field_class_structure_get_member_count(self._ptr) + assert count >= 0 return count - def map_range(self, label, lower, upper=None): - utils._check_str(label) + def __getitem__(self, key): + if not isinstance(key, str): + raise TypeError( + "key must be a 'str' object, got '{}'".format(key.__class__.__name__) + ) - if upper is None: - upper = lower + member_ptr = self._borrow_member_ptr_by_name(self._ptr, key) - ret = self._map_range(self._ptr, label, lower, upper) - utils._handle_ret(ret, "cannot add mapping to enumeration field class object") + if member_ptr is None: + raise KeyError(key) - def labels_by_value(self, value): - ret, labels = self._get_mapping_labels_by_value(self._ptr, value) - utils._handle_ret(ret, "cannot get mapping labels") - return labels + return self._structure_member_field_class_pycls(self, member_ptr) def __iter__(self): for idx in range(len(self)): - mapping = self._get_mapping_by_index(self._ptr, idx) - yield mapping.label + member_ptr = self._borrow_member_ptr_by_index(self._ptr, idx) + assert member_ptr is not None + yield native_bt.field_class_structure_member_get_name(member_ptr) - def __getitem__(self, key): - utils._check_str(key) - for idx in range(len(self)): - mapping = self._get_mapping_by_index(self._ptr, idx) - if mapping.label == key: - return mapping + def member_at_index(self, index): + utils._check_uint64(index) - raise KeyError(key) + if index >= len(self): + raise IndexError - def __iadd__(self, mappings): - for mapping in mappings.values(): - for range in mapping: - self.map_range(mapping.label, range.lower, range.upper) + member_ptr = self._borrow_member_ptr_by_index(self._ptr, index) + assert member_ptr is not None + return self._structure_member_field_class_pycls(self, member_ptr) - return self +class _StructureFieldClass(_StructureFieldClassConst, _FieldClass): + _NAME = 'Structure' + _borrow_member_by_index = staticmethod( + native_bt.field_class_structure_borrow_member_by_index + ) + _borrow_member_ptr_by_name = staticmethod( + native_bt.field_class_structure_borrow_member_by_name + ) + _structure_member_field_class_pycls = property(lambda _: _StructureFieldClassMember) + + def append_member(self, name, field_class, user_attributes=None): + utils._check_str(name) + utils._check_type(field_class, _FieldClass) -class _UnsignedEnumerationFieldClass(_EnumerationFieldClass, _UnsignedIntegerFieldClass): - _NAME = 'Unsigned enumeration' + if name in self: + raise ValueError("duplicate member name '{}'".format(name)) - @staticmethod - def _get_mapping_by_index(enum_ptr, index): - mapping_ptr = native_bt.field_class_unsigned_enumeration_borrow_mapping_by_index_const(enum_ptr, index) - assert mapping_ptr is not None - return _UnsignedEnumerationFieldClassMapping(mapping_ptr) + user_attributes_value = None - @staticmethod - def _map_range(enum_ptr, label, lower, upper): - utils._check_uint64(lower) - utils._check_uint64(upper) - return native_bt.field_class_unsigned_enumeration_map_range(enum_ptr, label, lower, upper) + if user_attributes is not None: + # check now that user attributes are valid + user_attributes_value = bt2.create_value(user_attributes) - @staticmethod - def _get_mapping_labels_by_value(enum_ptr, value): - utils._check_uint64(value) - return native_bt.field_class_unsigned_enumeration_get_mapping_labels_by_value(enum_ptr, value) + status = native_bt.field_class_structure_append_member( + self._ptr, name, field_class._ptr + ) + utils._handle_func_status( + status, 'cannot append member to structure field class object' + ) + if user_attributes is not None: + self[name]._user_attributes = user_attributes_value -class _SignedEnumerationFieldClass(_EnumerationFieldClass, _SignedIntegerFieldClass): - _NAME = 'Signed enumeration' + def __iadd__(self, members): + for name, field_class in members: + self.append_member(name, field_class) - @staticmethod - def _get_mapping_by_index(enum_ptr, index): - mapping_ptr = native_bt.field_class_signed_enumeration_borrow_mapping_by_index_const(enum_ptr, index) - assert mapping_ptr is not None - return _SignedEnumerationFieldClassMapping(mapping_ptr) + return self - @staticmethod - def _map_range(enum_ptr, label, lower, upper): - utils._check_int64(lower) - utils._check_int64(upper) - return native_bt.field_class_signed_enumeration_map_range(enum_ptr, label, lower, upper) - @staticmethod - def _get_mapping_labels_by_value(enum_ptr, value): - utils._check_int64(value) - return native_bt.field_class_signed_enumeration_get_mapping_labels_by_value(enum_ptr, value) +class _OptionFieldClassConst(_FieldClassConst): + _NAME = 'Const Option' + _create_field_class_from_ptr_and_get_ref = staticmethod( + _create_field_class_from_const_ptr_and_get_ref + ) + _borrow_field_class_ptr = staticmethod( + native_bt.field_class_option_borrow_field_class_const + ) + _borrow_selector_field_path = staticmethod( + native_bt.field_class_option_borrow_selector_field_path_const + ) + @property + def field_class(self): + elem_fc_ptr = self._borrow_field_class_ptr(self._ptr) + return self._create_field_class_from_ptr_and_get_ref(elem_fc_ptr) -class _StringFieldClass(_FieldClass): - _NAME = 'String' + @property + def selector_field_path(self): + ptr = self._borrow_selector_field_path(self._ptr) + if ptr is None: + return + + return bt2_field_path._FieldPath._create_from_ptr_and_get_ref(ptr) + + +class _OptionFieldClass(_OptionFieldClassConst, _FieldClass): + _NAME = 'Option' + _borrow_field_class_ptr = staticmethod( + native_bt.field_class_option_borrow_field_class + ) + _create_field_class_from_ptr_and_get_ref = staticmethod( + _create_field_class_from_ptr_and_get_ref + ) + + +class _VariantFieldClassOptionConst: + _create_field_class_from_ptr_and_get_ref = staticmethod( + _create_field_class_from_const_ptr_and_get_ref + ) + _borrow_field_class_ptr = staticmethod( + native_bt.field_class_variant_option_borrow_field_class_const + ) + _borrow_user_attributes_ptr = staticmethod( + native_bt.field_class_variant_option_borrow_user_attributes_const + ) + _create_value_from_ptr_and_get_ref = staticmethod( + bt2_value._create_from_const_ptr_and_get_ref + ) + + def __init__(self, owning_var_fc, option_ptr): + # this field class owns the option; keeping it here maintains + # the option alive as options are not shared objects + self._owning_var_fc = owning_var_fc + self._ptr = option_ptr + + @property + def name(self): + name = native_bt.field_class_variant_option_get_name(self._ptr) + assert name is not None + return name + + @property + def field_class(self): + fc_ptr = self._borrow_field_class_ptr(self._ptr) + assert fc_ptr is not None + return self._create_field_class_from_ptr_and_get_ref(fc_ptr) + + @property + def user_attributes(self): + ptr = self._borrow_user_attributes_ptr(self._ptr) + assert ptr is not None + return self._create_value_from_ptr_and_get_ref(ptr) + + +class _VariantFieldClassOption(_VariantFieldClassOptionConst): + _create_field_class_from_ptr_and_get_ref = staticmethod( + _create_field_class_from_ptr_and_get_ref + ) + _borrow_field_class_ptr = staticmethod( + native_bt.field_class_variant_option_borrow_field_class + ) + _borrow_user_attributes_ptr = staticmethod( + native_bt.field_class_variant_option_borrow_user_attributes_const + ) + _create_value_from_ptr_and_get_ref = staticmethod( + bt2_value._create_from_ptr_and_get_ref + ) + + def _user_attributes(self, user_attributes): + value = bt2_value.create_value(user_attributes) + utils._check_type(value, bt2_value.MapValue) + native_bt.field_class_variant_option_set_user_attributes(self._ptr, value._ptr) + + _user_attributes = property(fset=_user_attributes) + + +class _VariantFieldClassWithSelectorOptionConst(_VariantFieldClassOptionConst): + def __init__(self, owning_var_fc, spec_opt_ptr): + self._spec_ptr = spec_opt_ptr + super().__init__(owning_var_fc, self._as_option_ptr(spec_opt_ptr)) + @property + def ranges(self): + range_set_ptr = self._borrow_ranges_ptr(self._spec_ptr) + assert range_set_ptr is not None + return self._range_set_type._create_from_ptr_and_get_ref(range_set_ptr) + + +class _VariantFieldClassWithSelectorOption( + _VariantFieldClassWithSelectorOptionConst, _VariantFieldClassOption +): + pass + + +class _VariantFieldClassWithSignedSelectorOptionConst( + _VariantFieldClassWithSelectorOptionConst +): + _as_option_ptr = staticmethod( + native_bt.field_class_variant_with_selector_signed_option_as_option_const + ) + _borrow_ranges_ptr = staticmethod( + native_bt.field_class_variant_with_selector_signed_option_borrow_ranges_const + ) + _range_set_type = bt2_integer_range_set.SignedIntegerRangeSet + + +class _VariantFieldClassWithSignedSelectorOption( + _VariantFieldClassWithSignedSelectorOptionConst, + _VariantFieldClassWithSelectorOption, +): + pass + + +class _VariantFieldClassWithUnsignedSelectorOptionConst( + _VariantFieldClassWithSelectorOptionConst +): + _as_option_ptr = staticmethod( + native_bt.field_class_variant_with_selector_unsigned_option_as_option_const + ) + _borrow_ranges_ptr = staticmethod( + native_bt.field_class_variant_with_selector_unsigned_option_borrow_ranges_const + ) + _range_set_type = bt2_integer_range_set.UnsignedIntegerRangeSet + + +class _VariantFieldClassWithUnsignedSelectorOption( + _VariantFieldClassWithUnsignedSelectorOptionConst, + _VariantFieldClassWithSelectorOption, +): + pass + + +class _VariantFieldClassConst(_FieldClassConst, collections.abc.Mapping): + _NAME = 'Const Variant' + _borrow_option_ptr_by_name = staticmethod( + native_bt.field_class_variant_borrow_option_by_name_const + ) + _borrow_option_ptr_by_index = staticmethod( + native_bt.field_class_variant_borrow_option_by_index_const + ) + _variant_option_pycls = _VariantFieldClassOptionConst + + @staticmethod + def _as_option_ptr(opt_ptr): + return opt_ptr + + def _create_option_from_ptr(self, opt_ptr): + return self._variant_option_pycls(self, opt_ptr) -class _FieldContainer(collections.abc.Mapping): def __len__(self): - count = self._get_element_count(self._ptr) + count = native_bt.field_class_variant_get_option_count(self._ptr) assert count >= 0 return count def __getitem__(self, key): if not isinstance(key, str): - raise TypeError("key should be a 'str' object, got {}".format(key.__class__.__name__)) + raise TypeError( + "key must be a 'str' object, got '{}'".format(key.__class__.__name__) + ) - ptr = self._borrow_field_class_ptr_by_name(key) + opt_ptr = self._borrow_option_ptr_by_name(self._ptr, key) - if ptr is None: + if opt_ptr is None: raise KeyError(key) - return _create_field_class_from_ptr_and_get_ref(ptr) - - def _borrow_field_class_ptr_by_name(self, key): - element_ptr = self._borrow_element_by_name(self._ptr, key) - if element_ptr is None: - return - - return self._element_borrow_field_class(element_ptr) + return self._create_option_from_ptr(opt_ptr) def __iter__(self): for idx in range(len(self)): - element_ptr = self._borrow_element_by_index(self._ptr, idx) - assert element_ptr is not None + opt_ptr = self._borrow_option_ptr_by_index(self._ptr, idx) + assert opt_ptr is not None + base_opt_ptr = self._as_option_ptr(opt_ptr) + yield native_bt.field_class_variant_option_get_name(base_opt_ptr) + + def option_at_index(self, index): + utils._check_uint64(index) - yield self._element_get_name(element_ptr) + if index >= len(self): + raise IndexError - def _append_element_common(self, name, field_class): - utils._check_str(name) - utils._check_type(field_class, _FieldClass) - ret = self._append_element(self._ptr, name, field_class._ptr) - utils._handle_ret(ret, "cannot add field to {} field class object".format(self._NAME.lower())) + opt_ptr = self._borrow_option_ptr_by_index(self._ptr, index) + assert opt_ptr is not None + return self._create_option_from_ptr(opt_ptr) - def __iadd__(self, fields): - for name, field_class in fields.items(): - self._append_element_common(name, field_class) - return self +class _VariantFieldClass(_VariantFieldClassConst, _FieldClass, collections.abc.Mapping): + _NAME = 'Variant' + _borrow_option_ptr_by_name = staticmethod( + native_bt.field_class_variant_borrow_option_by_name + ) + _borrow_option_ptr_by_index = staticmethod( + native_bt.field_class_variant_borrow_option_by_index + ) + _variant_option_pycls = _VariantFieldClassOption - def _at_index(self, index): - utils._check_uint64(index) - if index < 0 or index >= len(self): - raise IndexError +class _VariantFieldClassWithoutSelectorConst(_VariantFieldClassConst): + _NAME = 'Const Variant (without selector)' - element_ptr = self._borrow_element_by_index(self._ptr, index) - assert element_ptr is not None - field_class_ptr = self._element_borrow_field_class(element_ptr) +class _VariantFieldClassWithoutSelector( + _VariantFieldClassWithoutSelectorConst, _VariantFieldClass +): + _NAME = 'Variant (without selector)' - return _create_field_class_from_ptr_and_get_ref(field_class_ptr) + def append_option(self, name, field_class, user_attributes=None): + utils._check_str(name) + utils._check_type(field_class, _FieldClass) + if name in self: + raise ValueError("duplicate option name '{}'".format(name)) -class _StructureFieldClass(_FieldClass, _FieldContainer): - _NAME = 'Structure' - _borrow_element_by_index = staticmethod(native_bt.field_class_structure_borrow_member_by_index_const) - _borrow_element_by_name = staticmethod(native_bt.field_class_structure_borrow_member_by_name_const) - _element_get_name = staticmethod(native_bt.field_class_structure_member_get_name) - _element_borrow_field_class = staticmethod(native_bt.field_class_structure_member_borrow_field_class_const) - _get_element_count = staticmethod(native_bt.field_class_structure_get_member_count) - _append_element = staticmethod(native_bt.field_class_structure_append_member) + user_attributes_value = None - def append_member(self, name, field_class): - return self._append_element_common(name, field_class) + if user_attributes is not None: + # check now that user attributes are valid + user_attributes_value = bt2.create_value(user_attributes) - def member_at_index(self, index): - return self._at_index(index) + status = native_bt.field_class_variant_without_selector_append_option( + self._ptr, name, field_class._ptr + ) + utils._handle_func_status( + status, 'cannot append option to variant field class object' + ) + if user_attributes is not None: + self[name]._user_attributes = user_attributes_value -class _VariantFieldClass(_FieldClass, _FieldContainer): - _NAME = 'Variant' - _borrow_element_by_index = staticmethod(native_bt.field_class_variant_borrow_option_by_index_const) - _borrow_element_by_name = staticmethod(native_bt.field_class_variant_borrow_option_by_name_const) - _element_get_name = staticmethod(native_bt.field_class_variant_option_get_name) - _element_borrow_field_class = staticmethod(native_bt.field_class_variant_option_borrow_field_class_const) - _get_element_count = staticmethod(native_bt.field_class_variant_get_option_count) - _append_element = staticmethod(native_bt.field_class_variant_append_option) + def __iadd__(self, options): + for name, field_class in options: + self.append_option(name, field_class) - def append_option(self, name, field_class): - return self._append_element_common(name, field_class) + return self - def option_at_index(self, index): - return self._at_index(index) + +class _VariantFieldClassWithSelectorConst(_VariantFieldClassConst): + _NAME = 'Const Variant (with selector)' @property def selector_field_path(self): - ptr = native_bt.field_class_variant_borrow_selector_field_path_const(self._ptr) + ptr = native_bt.field_class_variant_with_selector_borrow_selector_field_path_const( + self._ptr + ) + if ptr is None: return - return bt2.field_path._FieldPath._create_from_ptr_and_get_ref(ptr) + return bt2_field_path._FieldPath._create_from_ptr_and_get_ref(ptr) + + +class _VariantFieldClassWithSelector( + _VariantFieldClassWithSelectorConst, _VariantFieldClass +): + _NAME = 'Variant (with selector)' - def _set_selector_field_class(self, selector_fc): - utils._check_type(selector_fc, bt2.field_class._EnumerationFieldClass) - ret = native_bt.field_class_variant_set_selector_field_class(self._ptr, selector_fc._ptr) - utils._handle_ret(ret, "cannot set variant selector field type") + def append_option(self, name, field_class, ranges, user_attributes=None): + utils._check_str(name) + utils._check_type(field_class, _FieldClass) + utils._check_type(ranges, self._variant_option_pycls._range_set_type) - _selector_field_class = property(fset=_set_selector_field_class) + if name in self: + raise ValueError("duplicate option name '{}'".format(name)) + if len(ranges) == 0: + raise ValueError('range set is empty') + + user_attributes_value = None + + if user_attributes is not None: + # check now that user attributes are valid + user_attributes_value = bt2.create_value(user_attributes) + + # TODO: check overlaps (precondition of self._append_option()) + + status = self._append_option(self._ptr, name, field_class._ptr, ranges._ptr) + utils._handle_func_status( + status, 'cannot append option to variant field class object' + ) + + if user_attributes is not None: + self[name]._user_attributes = user_attributes_value + + def __iadd__(self, options): + for name, field_class, ranges in options: + self.append_option(name, field_class, ranges) + + return self + + +class _VariantFieldClassWithUnsignedSelectorConst(_VariantFieldClassWithSelectorConst): + _NAME = 'Const Variant (with unsigned selector)' + _borrow_option_ptr_by_name = staticmethod( + native_bt.field_class_variant_with_selector_unsigned_borrow_option_by_name_const + ) + _borrow_option_ptr_by_index = staticmethod( + native_bt.field_class_variant_with_selector_unsigned_borrow_option_by_index_const + ) + _append_option = staticmethod( + native_bt.field_class_variant_with_selector_unsigned_append_option + ) + _variant_option_pycls = _VariantFieldClassWithUnsignedSelectorOptionConst + _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr) + + +class _VariantFieldClassWithUnsignedSelector( + _VariantFieldClassWithUnsignedSelectorConst, _VariantFieldClassWithSelector +): + _NAME = 'Variant (with unsigned selector)' + _variant_option_pycls = _VariantFieldClassWithUnsignedSelectorOption + _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr) + + +class _VariantFieldClassWithSignedSelectorConst(_VariantFieldClassWithSelectorConst): + _NAME = 'Const Variant (with signed selector)' + _borrow_option_ptr_by_name = staticmethod( + native_bt.field_class_variant_with_selector_signed_borrow_option_by_name_const + ) + _borrow_option_ptr_by_index = staticmethod( + native_bt.field_class_variant_with_selector_signed_borrow_option_by_index_const + ) + _append_option = staticmethod( + native_bt.field_class_variant_with_selector_signed_append_option + ) + _variant_option_pycls = _VariantFieldClassWithSignedSelectorOptionConst + _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr) + + +class _VariantFieldClassWithSignedSelector( + _VariantFieldClassWithSignedSelectorConst, _VariantFieldClassWithSelector +): + _NAME = 'Variant (with signed selector)' + _variant_option_pycls = _VariantFieldClassWithSignedSelectorOption + _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr) + + +class _ArrayFieldClassConst(_FieldClassConst): + _create_field_class_from_ptr_and_get_ref = staticmethod( + _create_field_class_from_const_ptr_and_get_ref + ) + _borrow_element_field_class = staticmethod( + native_bt.field_class_array_borrow_element_field_class_const + ) -class _ArrayFieldClass(_FieldClass): @property def element_field_class(self): - elem_fc_ptr = native_bt.field_class_array_borrow_element_field_class_const(self._ptr) - return _create_field_class_from_ptr_and_get_ref(elem_fc_ptr) + elem_fc_ptr = self._borrow_element_field_class(self._ptr) + return self._create_field_class_from_ptr_and_get_ref(elem_fc_ptr) -class _StaticArrayFieldClass(_ArrayFieldClass): +class _ArrayFieldClass(_ArrayFieldClassConst, _FieldClass): + _create_field_class_from_ptr_and_get_ref = staticmethod( + _create_field_class_from_ptr_and_get_ref + ) + _borrow_element_field_class = staticmethod( + native_bt.field_class_array_borrow_element_field_class + ) + + +class _StaticArrayFieldClassConst(_ArrayFieldClassConst): + _NAME = 'Const static array' + @property def length(self): - return native_bt.field_class_static_array_get_length(self._ptr) + return native_bt.field_class_array_static_get_length(self._ptr) + + +class _StaticArrayFieldClass(_StaticArrayFieldClassConst, _ArrayFieldClass): + _NAME = 'Static array' -class _DynamicArrayFieldClass(_ArrayFieldClass): +class _DynamicArrayFieldClassConst(_ArrayFieldClassConst): + _NAME = 'Const dynamic array' + @property def length_field_path(self): - ptr = native_bt.field_class_dynamic_array_borrow_length_field_path_const(self._ptr) + ptr = native_bt.field_class_array_dynamic_borrow_length_field_path_const( + self._ptr + ) if ptr is None: return - return bt2.field_path._FieldPath._create_from_ptr_and_get_ref(ptr) - - def _set_length_field_class(self, length_fc): - utils._check_type(length_fc, _UnsignedIntegerFieldClass) - ret = native_bt.field_class_dynamic_array_set_length_field_class(self._ptr, length_fc._ptr) - utils._handle_ret(ret, "cannot set dynamic array length field type") - - _length_field_class = property(fset=_set_length_field_class) - + return bt2_field_path._FieldPath._create_from_ptr_and_get_ref(ptr) + + +class _DynamicArrayFieldClass(_DynamicArrayFieldClassConst, _ArrayFieldClass): + _NAME = 'Dynamic Array' + + +_FIELD_CLASS_TYPE_TO_CONST_OBJ = { + native_bt.FIELD_CLASS_TYPE_BOOL: _BoolFieldClassConst, + native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayFieldClassConst, + native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClassConst, + native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClassConst, + native_bt.FIELD_CLASS_TYPE_REAL: _RealFieldClassConst, + native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationFieldClassConst, + native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationFieldClassConst, + native_bt.FIELD_CLASS_TYPE_STRING: _StringFieldClassConst, + native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldClassConst, + native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldClassConst, + native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY: _DynamicArrayFieldClassConst, + native_bt.FIELD_CLASS_TYPE_OPTION: _OptionFieldClassConst, + native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: _VariantFieldClassWithoutSelectorConst, + native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: _VariantFieldClassWithUnsignedSelectorConst, + native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: _VariantFieldClassWithSignedSelectorConst, +} _FIELD_CLASS_TYPE_TO_OBJ = { + native_bt.FIELD_CLASS_TYPE_BOOL: _BoolFieldClass, + native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayFieldClass, native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClass, native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClass, native_bt.FIELD_CLASS_TYPE_REAL: _RealFieldClass, @@ -393,5 +932,8 @@ _FIELD_CLASS_TYPE_TO_OBJ = { native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldClass, native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldClass, native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY: _DynamicArrayFieldClass, - native_bt.FIELD_CLASS_TYPE_VARIANT: _VariantFieldClass, + native_bt.FIELD_CLASS_TYPE_OPTION: _OptionFieldClass, + native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: _VariantFieldClassWithoutSelector, + native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: _VariantFieldClassWithUnsignedSelector, + native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: _VariantFieldClassWithSignedSelector, }