Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
f6a5e476 | 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
81447b5b PP |
4 | # |
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: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
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 | |
21 | # THE SOFTWARE. | |
22 | ||
23 | from bt2 import native_bt, object, utils | |
24 | import collections.abc | |
ae0bfae8 | 25 | import bt2.field |
c8820b76 | 26 | import bt2.field_path |
02b61fe0 | 27 | import bt2.integer_range_set |
81447b5b PP |
28 | import bt2 |
29 | ||
30 | ||
060aee52 SM |
31 | def _create_field_class_from_ptr_and_get_ref(ptr): |
32 | typeid = native_bt.field_class_get_type(ptr) | |
33 | return _FIELD_CLASS_TYPE_TO_OBJ[typeid]._create_from_ptr_and_get_ref(ptr) | |
81447b5b PP |
34 | |
35 | ||
c8820b76 SM |
36 | class IntegerDisplayBase: |
37 | BINARY = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY | |
38 | OCTAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL | |
39 | DECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL | |
40 | HEXADECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL | |
81447b5b | 41 | |
81447b5b | 42 | |
c8820b76 SM |
43 | class _FieldClass(object._SharedObject): |
44 | _get_ref = staticmethod(native_bt.field_class_get_ref) | |
45 | _put_ref = staticmethod(native_bt.field_class_put_ref) | |
81447b5b PP |
46 | |
47 | def _check_create_status(self, ptr): | |
48 | if ptr is None: | |
32656995 | 49 | raise bt2.CreationError('cannot create {} field class object'.format(self._NAME.lower())) |
81447b5b | 50 | |
81447b5b | 51 | |
a728f6c3 | 52 | class _IntegerFieldClass(_FieldClass): |
81447b5b | 53 | @property |
c8820b76 SM |
54 | def field_value_range(self): |
55 | size = native_bt.field_class_integer_get_field_value_range(self._ptr) | |
f6a5e476 | 56 | assert(size >= 1) |
81447b5b PP |
57 | return size |
58 | ||
c8820b76 SM |
59 | def _field_value_range(self, size): |
60 | if size < 1 or size > 64: | |
61 | raise ValueError("Value is outside valid range [1, 64] ({})".format(size)) | |
62 | native_bt.field_class_integer_set_field_value_range(self._ptr, size) | |
81447b5b | 63 | |
c8820b76 | 64 | _field_value_range = property(fset=_field_value_range) |
81447b5b PP |
65 | |
66 | @property | |
c8820b76 SM |
67 | def preferred_display_base(self): |
68 | base = native_bt.field_class_integer_get_preferred_display_base( | |
69 | self._ptr) | |
f6a5e476 | 70 | assert(base >= 0) |
81447b5b PP |
71 | return base |
72 | ||
c8820b76 SM |
73 | def _preferred_display_base(self, base): |
74 | utils._check_uint64(base) | |
81447b5b | 75 | |
c8820b76 SM |
76 | if base not in (IntegerDisplayBase.BINARY, |
77 | IntegerDisplayBase.OCTAL, | |
78 | IntegerDisplayBase.DECIMAL, | |
79 | IntegerDisplayBase.HEXADECIMAL): | |
80 | raise ValueError("Display base is not a valid IntegerDisplayBase value") | |
81447b5b | 81 | |
c8820b76 SM |
82 | native_bt.field_class_integer_set_preferred_display_base( |
83 | self._ptr, base) | |
81447b5b | 84 | |
c8820b76 | 85 | _preferred_display_base = property(fset=_preferred_display_base) |
81447b5b PP |
86 | |
87 | ||
27d97a3f | 88 | class _UnsignedIntegerFieldClass(_IntegerFieldClass): |
c8820b76 | 89 | _NAME = 'Unsigned integer' |
27d97a3f SM |
90 | |
91 | ||
a728f6c3 | 92 | class _SignedIntegerFieldClass(_IntegerFieldClass): |
c8820b76 | 93 | _NAME = 'Signed integer' |
27d97a3f | 94 | |
a728f6c3 | 95 | |
c8820b76 | 96 | class _RealFieldClass(_FieldClass): |
27d97a3f | 97 | _NAME = 'Real' |
81447b5b | 98 | |
81447b5b | 99 | @property |
c8820b76 SM |
100 | def is_single_precision(self): |
101 | return native_bt.field_class_real_is_single_precision(self._ptr) | |
81447b5b | 102 | |
c8820b76 SM |
103 | def _is_single_precision(self, is_single_precision): |
104 | utils._check_bool(is_single_precision) | |
105 | native_bt.field_class_real_set_is_single_precision( | |
106 | self._ptr, is_single_precision) | |
81447b5b | 107 | |
c8820b76 | 108 | _is_single_precision = property(fset=_is_single_precision) |
81447b5b PP |
109 | |
110 | ||
02b61fe0 PP |
111 | # an enumeration field class mapping does not have a reference count, so |
112 | # we copy the properties here to avoid eventual memory access errors. | |
113 | class _EnumerationFieldClassMapping: | |
c8820b76 | 114 | def __init__(self, mapping_ptr): |
02b61fe0 PP |
115 | base_mapping_ptr = self._as_enumeration_field_class_mapping_ptr(mapping_ptr) |
116 | self._label = native_bt.field_class_enumeration_mapping_get_label(base_mapping_ptr) | |
117 | assert self._label is not None | |
118 | ranges_ptr = self._mapping_borrow_ranges_ptr(mapping_ptr) | |
119 | assert ranges_ptr is not None | |
120 | self._ranges = self._ranges_type._create_from_ptr_and_get_ref(ranges_ptr) | |
81447b5b PP |
121 | |
122 | @property | |
c8820b76 | 123 | def label(self): |
02b61fe0 | 124 | return self._label |
81447b5b | 125 | |
02b61fe0 PP |
126 | @property |
127 | def ranges(self): | |
128 | return self._ranges | |
81447b5b | 129 | |
81447b5b | 130 | |
c8820b76 | 131 | class _UnsignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping): |
02b61fe0 | 132 | _ranges_type = bt2.integer_range_set.UnsignedIntegerRangeSet |
c8820b76 | 133 | _as_enumeration_field_class_mapping_ptr = staticmethod(native_bt.field_class_unsigned_enumeration_mapping_as_mapping_const) |
02b61fe0 | 134 | _mapping_borrow_ranges_ptr = staticmethod(native_bt.field_class_unsigned_enumeration_mapping_borrow_ranges_const) |
81447b5b | 135 | |
81447b5b | 136 | |
c8820b76 | 137 | class _SignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping): |
02b61fe0 | 138 | _ranges_type = bt2.integer_range_set.SignedIntegerRangeSet |
c8820b76 | 139 | _as_enumeration_field_class_mapping_ptr = staticmethod(native_bt.field_class_signed_enumeration_mapping_as_mapping_const) |
02b61fe0 | 140 | _mapping_borrow_ranges_ptr = staticmethod(native_bt.field_class_signed_enumeration_mapping_borrow_ranges_const) |
81447b5b | 141 | |
81447b5b | 142 | |
c8820b76 | 143 | class _EnumerationFieldClass(_IntegerFieldClass, collections.abc.Mapping): |
81447b5b | 144 | def __len__(self): |
32656995 | 145 | count = native_bt.field_class_enumeration_get_mapping_count(self._ptr) |
02b61fe0 | 146 | assert count >= 0 |
81447b5b PP |
147 | return count |
148 | ||
02b61fe0 | 149 | def add_mapping(self, label, ranges): |
c8820b76 | 150 | utils._check_str(label) |
02b61fe0 | 151 | utils._check_type(ranges, self._range_set_type) |
81447b5b | 152 | |
02b61fe0 PP |
153 | if label in self: |
154 | raise bt2.Error("duplicate mapping label '{}'".format(label)) | |
81447b5b | 155 | |
02b61fe0 | 156 | status = self._add_mapping(self._ptr, label, ranges._ptr) |
fb25b9e3 | 157 | utils._handle_func_status(status, |
02b61fe0 | 158 | 'cannot add mapping to enumeration field class object') |
81447b5b | 159 | |
02b61fe0 | 160 | def mappings_for_value(self, value): |
1091bb60 | 161 | status, labels = self._get_mapping_labels_for_value(self._ptr, value) |
02b61fe0 PP |
162 | utils._handle_func_status(status, 'cannot get mapping labels for value {}'.format(value)) |
163 | return [self[label] for label in labels] | |
81447b5b | 164 | |
c8820b76 SM |
165 | def __iter__(self): |
166 | for idx in range(len(self)): | |
167 | mapping = self._get_mapping_by_index(self._ptr, idx) | |
168 | yield mapping.label | |
81447b5b | 169 | |
02b61fe0 PP |
170 | def __getitem__(self, label): |
171 | utils._check_str(label) | |
172 | mapping = self._get_mapping_by_label(self._ptr, label) | |
81447b5b | 173 | |
02b61fe0 PP |
174 | if mapping is None: |
175 | raise KeyError(label) | |
176 | ||
177 | return mapping | |
81447b5b | 178 | |
c8820b76 | 179 | def __iadd__(self, mappings): |
02b61fe0 PP |
180 | for label, ranges in mappings: |
181 | self.add_mapping(label, ranges) | |
81447b5b | 182 | |
c8820b76 | 183 | return self |
81447b5b | 184 | |
81447b5b | 185 | |
c8820b76 SM |
186 | class _UnsignedEnumerationFieldClass(_EnumerationFieldClass, _UnsignedIntegerFieldClass): |
187 | _NAME = 'Unsigned enumeration' | |
02b61fe0 PP |
188 | _range_set_type = bt2.integer_range_set.UnsignedIntegerRangeSet |
189 | _add_mapping = staticmethod(native_bt.field_class_unsigned_enumeration_add_mapping) | |
81447b5b | 190 | |
c8820b76 SM |
191 | @staticmethod |
192 | def _get_mapping_by_index(enum_ptr, index): | |
193 | mapping_ptr = native_bt.field_class_unsigned_enumeration_borrow_mapping_by_index_const(enum_ptr, index) | |
194 | assert mapping_ptr is not None | |
195 | return _UnsignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 196 | |
c8820b76 | 197 | @staticmethod |
02b61fe0 PP |
198 | def _get_mapping_by_label(enum_ptr, label): |
199 | mapping_ptr = native_bt.field_class_unsigned_enumeration_borrow_mapping_by_label_const(enum_ptr, label) | |
200 | ||
201 | if mapping_ptr is None: | |
202 | return | |
203 | ||
204 | return _UnsignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 205 | |
c8820b76 | 206 | @staticmethod |
1091bb60 | 207 | def _get_mapping_labels_for_value(enum_ptr, value): |
c8820b76 | 208 | utils._check_uint64(value) |
1091bb60 | 209 | return native_bt.field_class_unsigned_enumeration_get_mapping_labels_for_value(enum_ptr, value) |
81447b5b PP |
210 | |
211 | ||
c8820b76 SM |
212 | class _SignedEnumerationFieldClass(_EnumerationFieldClass, _SignedIntegerFieldClass): |
213 | _NAME = 'Signed enumeration' | |
02b61fe0 PP |
214 | _range_set_type = bt2.integer_range_set.SignedIntegerRangeSet |
215 | _add_mapping = staticmethod(native_bt.field_class_signed_enumeration_add_mapping) | |
81447b5b | 216 | |
c8820b76 SM |
217 | @staticmethod |
218 | def _get_mapping_by_index(enum_ptr, index): | |
219 | mapping_ptr = native_bt.field_class_signed_enumeration_borrow_mapping_by_index_const(enum_ptr, index) | |
220 | assert mapping_ptr is not None | |
221 | return _SignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 222 | |
c8820b76 | 223 | @staticmethod |
02b61fe0 PP |
224 | def _get_mapping_by_label(enum_ptr, label): |
225 | mapping_ptr = native_bt.field_class_signed_enumeration_borrow_mapping_by_label_const(enum_ptr, label) | |
226 | ||
227 | if mapping_ptr is None: | |
228 | return | |
229 | ||
230 | return _SignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 231 | |
c8820b76 | 232 | @staticmethod |
1091bb60 | 233 | def _get_mapping_labels_for_value(enum_ptr, value): |
c8820b76 | 234 | utils._check_int64(value) |
1091bb60 | 235 | return native_bt.field_class_signed_enumeration_get_mapping_labels_for_value(enum_ptr, value) |
81447b5b | 236 | |
c8820b76 SM |
237 | |
238 | class _StringFieldClass(_FieldClass): | |
239 | _NAME = 'String' | |
81447b5b PP |
240 | |
241 | ||
02b61fe0 PP |
242 | class _StructureFieldClassMember: |
243 | def __init__(self, name, field_class): | |
244 | self._name = name | |
245 | self._field_class = field_class | |
246 | ||
247 | @property | |
248 | def name(self): | |
249 | return self._name | |
250 | ||
251 | @property | |
252 | def field_class(self): | |
253 | return self._field_class | |
254 | ||
255 | ||
256 | class _StructureFieldClass(_FieldClass, collections.abc.Mapping): | |
257 | _NAME = 'Structure' | |
258 | ||
259 | def append_member(self, name, field_class): | |
260 | utils._check_str(name) | |
261 | utils._check_type(field_class, _FieldClass) | |
262 | ||
263 | if name in self: | |
264 | raise bt2.Error("duplicate member name '{}'".format(name)) | |
265 | ||
266 | status = native_bt.field_class_structure_append_member(self._ptr, name, field_class._ptr) | |
267 | utils._handle_func_status(status, | |
268 | 'cannot append member to structure field class object') | |
269 | ||
81447b5b | 270 | def __len__(self): |
02b61fe0 | 271 | count = native_bt.field_class_structure_get_member_count(self._ptr) |
c8820b76 | 272 | assert count >= 0 |
81447b5b PP |
273 | return count |
274 | ||
02b61fe0 PP |
275 | @staticmethod |
276 | def _create_member_from_ptr(member_ptr): | |
277 | name = native_bt.field_class_structure_member_get_name(member_ptr) | |
278 | assert name is not None | |
279 | fc_ptr = native_bt.field_class_structure_member_borrow_field_class_const(member_ptr) | |
280 | assert fc_ptr is not None | |
281 | fc = _create_field_class_from_ptr_and_get_ref(fc_ptr) | |
282 | return _StructureFieldClassMember(name, fc) | |
283 | ||
81447b5b PP |
284 | def __getitem__(self, key): |
285 | if not isinstance(key, str): | |
02b61fe0 | 286 | raise TypeError("key must be a 'str' object, got '{}'".format(key.__class__.__name__)) |
81447b5b | 287 | |
02b61fe0 | 288 | member_ptr = native_bt.field_class_structure_borrow_member_by_name_const(self._ptr, key) |
81447b5b | 289 | |
02b61fe0 | 290 | if member_ptr is None: |
81447b5b PP |
291 | raise KeyError(key) |
292 | ||
02b61fe0 | 293 | return self._create_member_from_ptr(member_ptr) |
81447b5b PP |
294 | |
295 | def __iter__(self): | |
c8820b76 | 296 | for idx in range(len(self)): |
02b61fe0 PP |
297 | member_ptr = native_bt.field_class_structure_borrow_member_by_index_const(self._ptr, idx) |
298 | assert member_ptr is not None | |
299 | yield native_bt.field_class_structure_member_get_name(member_ptr) | |
81447b5b | 300 | |
02b61fe0 PP |
301 | def __iadd__(self, members): |
302 | for name, field_class in members: | |
303 | self.append_member(name, field_class) | |
81447b5b PP |
304 | |
305 | return self | |
306 | ||
02b61fe0 | 307 | def member_at_index(self, index): |
81447b5b | 308 | utils._check_uint64(index) |
81447b5b | 309 | |
02b61fe0 | 310 | if index >= len(self): |
f6a5e476 PP |
311 | raise IndexError |
312 | ||
02b61fe0 PP |
313 | member_ptr = native_bt.field_class_structure_borrow_member_by_index_const(self._ptr, index) |
314 | assert member_ptr is not None | |
315 | return self._create_member_from_ptr(member_ptr) | |
316 | ||
81447b5b | 317 | |
02b61fe0 PP |
318 | class _VariantFieldClassOption: |
319 | def __init__(self, name, field_class): | |
320 | self._name = name | |
321 | self._field_class = field_class | |
81447b5b | 322 | |
02b61fe0 PP |
323 | @property |
324 | def name(self): | |
325 | return self._name | |
81447b5b | 326 | |
02b61fe0 PP |
327 | @property |
328 | def field_class(self): | |
329 | return self._field_class | |
81447b5b | 330 | |
81447b5b | 331 | |
02b61fe0 PP |
332 | class _VariantFieldClassWithSelectorOption(_VariantFieldClassOption): |
333 | def __init__(self, name, field_class, ranges): | |
334 | super().__init__(name, field_class) | |
335 | self._ranges = ranges | |
81447b5b | 336 | |
02b61fe0 PP |
337 | @property |
338 | def ranges(self): | |
339 | return self._ranges | |
81447b5b PP |
340 | |
341 | ||
02b61fe0 | 342 | class _VariantFieldClass(_FieldClass, collections.abc.Mapping): |
81447b5b | 343 | _NAME = 'Variant' |
02b61fe0 PP |
344 | _borrow_option_by_name_ptr = staticmethod(native_bt.field_class_variant_borrow_option_by_name_const) |
345 | _borrow_member_by_index_ptr = staticmethod(native_bt.field_class_variant_borrow_option_by_index_const) | |
f6a5e476 | 346 | |
02b61fe0 PP |
347 | @staticmethod |
348 | def _as_option_ptr(opt_ptr): | |
349 | return opt_ptr | |
350 | ||
351 | def _create_option_from_ptr(self, opt_ptr): | |
352 | name = native_bt.field_class_variant_option_get_name(opt_ptr) | |
353 | assert name is not None | |
354 | fc_ptr = native_bt.field_class_variant_option_borrow_field_class_const(opt_ptr) | |
355 | assert fc_ptr is not None | |
356 | fc = _create_field_class_from_ptr_and_get_ref(fc_ptr) | |
357 | return _VariantFieldClassOption(name, fc) | |
358 | ||
359 | def __len__(self): | |
360 | count = native_bt.field_class_variant_get_option_count(self._ptr) | |
361 | assert count >= 0 | |
362 | return count | |
363 | ||
364 | def __getitem__(self, key): | |
365 | if not isinstance(key, str): | |
366 | raise TypeError("key must be a 'str' object, got '{}'".format(key.__class__.__name__)) | |
367 | ||
368 | opt_ptr = self._borrow_option_by_name_ptr(self._ptr, key) | |
369 | ||
370 | if opt_ptr is None: | |
371 | raise KeyError(key) | |
372 | ||
373 | return self._create_option_from_ptr(opt_ptr) | |
374 | ||
375 | def __iter__(self): | |
376 | for idx in range(len(self)): | |
377 | opt_ptr = self._borrow_member_by_index_ptr(self._ptr, idx) | |
378 | assert opt_ptr is not None | |
379 | base_opt_ptr = self._as_option_ptr(opt_ptr) | |
380 | yield native_bt.field_class_variant_option_get_name(base_opt_ptr) | |
f6a5e476 | 381 | |
c8820b76 | 382 | def option_at_index(self, index): |
02b61fe0 PP |
383 | utils._check_uint64(index) |
384 | ||
385 | if index >= len(self): | |
386 | raise IndexError | |
387 | ||
388 | opt_ptr = self._borrow_member_by_index_ptr(self._ptr, index) | |
389 | assert opt_ptr is not None | |
390 | return self._create_option_from_ptr(opt_ptr) | |
391 | ||
392 | ||
393 | class _VariantFieldClassWithoutSelector(_VariantFieldClass): | |
394 | _NAME = 'Variant (without selector)' | |
395 | ||
396 | def append_option(self, name, field_class): | |
397 | utils._check_str(name) | |
398 | utils._check_type(field_class, _FieldClass) | |
399 | ||
400 | if name in self: | |
401 | raise bt2.Error("duplicate option name '{}'".format(name)) | |
402 | ||
403 | status = native_bt.field_class_variant_without_selector_append_option(self._ptr, name, field_class._ptr) | |
404 | utils._handle_func_status(status, | |
405 | 'cannot append option to variant field class object') | |
406 | ||
407 | def __iadd__(self, options): | |
408 | for name, field_class in options: | |
409 | self.append_option(name, field_class) | |
410 | ||
411 | return self | |
412 | ||
413 | ||
414 | class _VariantFieldClassWithSelector(_VariantFieldClass): | |
415 | _NAME = 'Variant (with selector)' | |
416 | ||
417 | def _create_option_from_ptr(self, opt_ptr): | |
418 | base_opt_ptr = self._as_option_ptr(opt_ptr) | |
419 | name = native_bt.field_class_variant_option_get_name(base_opt_ptr) | |
420 | assert name is not None | |
421 | fc_ptr = native_bt.field_class_variant_option_borrow_field_class_const(base_opt_ptr) | |
422 | assert fc_ptr is not None | |
423 | fc = _create_field_class_from_ptr_and_get_ref(fc_ptr) | |
424 | range_set_ptr = self._option_borrow_ranges_ptr(opt_ptr) | |
425 | assert range_set_ptr is not None | |
426 | range_set = self._range_set_type._create_from_ptr_and_get_ref(range_set_ptr) | |
427 | return _VariantFieldClassWithSelectorOption(name, fc, range_set) | |
81447b5b PP |
428 | |
429 | @property | |
c8820b76 | 430 | def selector_field_path(self): |
02b61fe0 PP |
431 | ptr = native_bt.field_class_variant_with_selector_borrow_selector_field_path_const(self._ptr) |
432 | ||
c8820b76 | 433 | if ptr is None: |
f6a5e476 PP |
434 | return |
435 | ||
c8820b76 | 436 | return bt2.field_path._FieldPath._create_from_ptr_and_get_ref(ptr) |
81447b5b | 437 | |
02b61fe0 PP |
438 | def append_option(self, name, field_class, ranges): |
439 | utils._check_str(name) | |
440 | utils._check_type(field_class, _FieldClass) | |
441 | utils._check_type(ranges, self._range_set_type) | |
442 | ||
443 | if name in self: | |
444 | raise bt2.Error("duplicate option name '{}'".format(name)) | |
445 | ||
446 | if len(ranges) == 0: | |
447 | raise ValueError('range set is empty') | |
448 | ||
449 | # TODO: check overlaps (precondition of self._append_option()) | |
450 | ||
451 | status = self._append_option(self._ptr, name, field_class._ptr, ranges._ptr) | |
fb25b9e3 | 452 | utils._handle_func_status(status, |
02b61fe0 PP |
453 | 'cannot append option to variant field class object') |
454 | ||
455 | def __iadd__(self, options): | |
456 | for name, field_class, ranges in options: | |
457 | self.append_option(name, field_class, ranges) | |
458 | ||
459 | return self | |
460 | ||
461 | ||
462 | class _VariantFieldClassWithUnsignedSelector(_VariantFieldClassWithSelector): | |
463 | _NAME = 'Variant (with unsigned selector)' | |
464 | _borrow_option_by_name_ptr = staticmethod(native_bt.field_class_variant_with_unsigned_selector_borrow_option_by_name_const) | |
465 | _borrow_member_by_index_ptr = staticmethod(native_bt.field_class_variant_with_unsigned_selector_borrow_option_by_index_const) | |
466 | _as_option_ptr = staticmethod(native_bt.field_class_variant_with_unsigned_selector_option_as_option_const) | |
467 | _append_option = staticmethod(native_bt.field_class_variant_with_unsigned_selector_append_option) | |
468 | _option_borrow_ranges_ptr = staticmethod(native_bt.field_class_variant_with_unsigned_selector_option_borrow_ranges_const) | |
469 | _range_set_type = bt2.integer_range_set.UnsignedIntegerRangeSet | |
470 | ||
81447b5b | 471 | |
02b61fe0 PP |
472 | class _VariantFieldClassWithSignedSelector(_VariantFieldClassWithSelector): |
473 | _NAME = 'Variant (with signed selector)' | |
474 | _borrow_option_by_name_ptr = staticmethod(native_bt.field_class_variant_with_signed_selector_borrow_option_by_name_const) | |
475 | _borrow_member_by_index_ptr = staticmethod(native_bt.field_class_variant_with_signed_selector_borrow_option_by_index_const) | |
476 | _as_option_ptr = staticmethod(native_bt.field_class_variant_with_signed_selector_option_as_option_const) | |
477 | _append_option = staticmethod(native_bt.field_class_variant_with_signed_selector_append_option) | |
478 | _option_borrow_ranges_ptr = staticmethod(native_bt.field_class_variant_with_signed_selector_option_borrow_ranges_const) | |
479 | _range_set_type = bt2.integer_range_set.SignedIntegerRangeSet | |
f6a5e476 | 480 | |
81447b5b | 481 | |
c8820b76 SM |
482 | class _ArrayFieldClass(_FieldClass): |
483 | @property | |
484 | def element_field_class(self): | |
485 | elem_fc_ptr = native_bt.field_class_array_borrow_element_field_class_const(self._ptr) | |
486 | return _create_field_class_from_ptr_and_get_ref(elem_fc_ptr) | |
81447b5b | 487 | |
81447b5b | 488 | |
c8820b76 | 489 | class _StaticArrayFieldClass(_ArrayFieldClass): |
81447b5b PP |
490 | @property |
491 | def length(self): | |
c8820b76 | 492 | return native_bt.field_class_static_array_get_length(self._ptr) |
81447b5b PP |
493 | |
494 | ||
c8820b76 SM |
495 | class _DynamicArrayFieldClass(_ArrayFieldClass): |
496 | @property | |
497 | def length_field_path(self): | |
498 | ptr = native_bt.field_class_dynamic_array_borrow_length_field_path_const(self._ptr) | |
499 | if ptr is None: | |
500 | return | |
81447b5b | 501 | |
c8820b76 | 502 | return bt2.field_path._FieldPath._create_from_ptr_and_get_ref(ptr) |
81447b5b | 503 | |
81447b5b | 504 | |
060aee52 | 505 | _FIELD_CLASS_TYPE_TO_OBJ = { |
c8820b76 SM |
506 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClass, |
507 | native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClass, | |
508 | native_bt.FIELD_CLASS_TYPE_REAL: _RealFieldClass, | |
509 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationFieldClass, | |
510 | native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationFieldClass, | |
511 | native_bt.FIELD_CLASS_TYPE_STRING: _StringFieldClass, | |
060aee52 | 512 | native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldClass, |
c8820b76 SM |
513 | native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldClass, |
514 | native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY: _DynamicArrayFieldClass, | |
02b61fe0 PP |
515 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: _VariantFieldClassWithoutSelector, |
516 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: _VariantFieldClassWithUnsignedSelector, | |
517 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: _VariantFieldClassWithSignedSelector, | |
81447b5b | 518 | } |