cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / bindings / python / bt2 / bt2 / field_class.py
1 # SPDX-License-Identifier: MIT
2 #
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4
5 import collections.abc
6
7 from bt2 import error as bt2_error
8 from bt2 import utils as bt2_utils
9 from bt2 import value as bt2_value
10 from bt2 import object as bt2_object
11 from bt2 import native_bt
12 from bt2 import field_path as bt2_field_path
13 from bt2 import integer_range_set as bt2_integer_range_set
14
15
16 def _obj_type_from_field_class_ptr_template(type_map, ptr):
17 typeid = native_bt.field_class_get_type(ptr)
18 return type_map[typeid]
19
20
21 def _obj_type_from_field_class_ptr(ptr):
22 return _obj_type_from_field_class_ptr_template(_FIELD_CLASS_TYPE_TO_OBJ, ptr)
23
24
25 def _create_field_class_from_ptr_and_get_ref_template(type_map, ptr):
26 return _obj_type_from_field_class_ptr_template(
27 type_map, ptr
28 )._create_from_ptr_and_get_ref(ptr)
29
30
31 def _create_field_class_from_ptr_and_get_ref(ptr):
32 return _create_field_class_from_ptr_and_get_ref_template(
33 _FIELD_CLASS_TYPE_TO_OBJ, ptr
34 )
35
36
37 def _create_field_class_from_const_ptr_and_get_ref(ptr):
38 return _create_field_class_from_ptr_and_get_ref_template(
39 _FIELD_CLASS_TYPE_TO_CONST_OBJ, ptr
40 )
41
42
43 class IntegerDisplayBase:
44 BINARY = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
45 OCTAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
46 DECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
47 HEXADECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
48
49
50 class _FieldClassConst(bt2_object._SharedObject):
51 @staticmethod
52 def _get_ref(ptr):
53 native_bt.field_class_get_ref(ptr)
54
55 @staticmethod
56 def _put_ref(ptr):
57 native_bt.field_class_put_ref(ptr)
58
59 _borrow_user_attributes_ptr = staticmethod(
60 native_bt.field_class_borrow_user_attributes_const
61 )
62 _create_value_from_ptr_and_get_ref = staticmethod(
63 bt2_value._create_from_const_ptr_and_get_ref
64 )
65
66 def _check_create_status(self, ptr):
67 if ptr is None:
68 raise bt2_error._MemoryError(
69 "cannot create {} field class object".format(self._NAME.lower())
70 )
71
72 @property
73 def user_attributes(self):
74 ptr = self._borrow_user_attributes_ptr(self._ptr)
75 assert ptr is not None
76 return self._create_value_from_ptr_and_get_ref(ptr)
77
78
79 class _FieldClass(_FieldClassConst):
80 _borrow_user_attributes_ptr = staticmethod(
81 native_bt.field_class_borrow_user_attributes
82 )
83 _create_value_from_ptr_and_get_ref = staticmethod(
84 bt2_value._create_from_ptr_and_get_ref
85 )
86
87 def _user_attributes(self, user_attributes):
88 value = bt2_value.create_value(user_attributes)
89 bt2_utils._check_type(value, bt2_value.MapValue)
90 native_bt.field_class_set_user_attributes(self._ptr, value._ptr)
91
92 _user_attributes = property(fset=_user_attributes)
93
94
95 class _BoolFieldClassConst(_FieldClassConst):
96 _NAME = "Const boolean"
97
98
99 class _BoolFieldClass(_BoolFieldClassConst, _FieldClass):
100 _NAME = "Boolean"
101
102
103 class _BitArrayFieldClassConst(_FieldClassConst):
104 _NAME = "Const bit array"
105
106 @property
107 def length(self):
108 length = native_bt.field_class_bit_array_get_length(self._ptr)
109 assert length >= 1
110 return length
111
112
113 class _BitArrayFieldClass(_BitArrayFieldClassConst, _FieldClass):
114 _NAME = "Bit array"
115
116
117 class _IntegerFieldClassConst(_FieldClassConst):
118 @property
119 def field_value_range(self):
120 size = native_bt.field_class_integer_get_field_value_range(self._ptr)
121 assert size >= 1
122 return size
123
124 @property
125 def preferred_display_base(self):
126 base = native_bt.field_class_integer_get_preferred_display_base(self._ptr)
127 assert base >= 0
128 return base
129
130
131 class _IntegerFieldClass(_FieldClass, _IntegerFieldClassConst):
132 def _field_value_range(self, size):
133 if size < 1 or size > 64:
134 raise ValueError("Value is outside valid range [1, 64] ({})".format(size))
135 native_bt.field_class_integer_set_field_value_range(self._ptr, size)
136
137 _field_value_range = property(fset=_field_value_range)
138
139 def _preferred_display_base(self, base):
140 bt2_utils._check_uint64(base)
141
142 if base not in (
143 IntegerDisplayBase.BINARY,
144 IntegerDisplayBase.OCTAL,
145 IntegerDisplayBase.DECIMAL,
146 IntegerDisplayBase.HEXADECIMAL,
147 ):
148 raise ValueError("Display base is not a valid IntegerDisplayBase value")
149
150 native_bt.field_class_integer_set_preferred_display_base(self._ptr, base)
151
152 _preferred_display_base = property(fset=_preferred_display_base)
153
154
155 class _UnsignedIntegerFieldClassConst(_IntegerFieldClassConst, _FieldClassConst):
156 _NAME = "Const unsigned integer"
157
158
159 class _UnsignedIntegerFieldClass(
160 _UnsignedIntegerFieldClassConst, _IntegerFieldClass, _FieldClass
161 ):
162 _NAME = "Unsigned integer"
163
164
165 class _SignedIntegerFieldClassConst(_IntegerFieldClassConst, _FieldClassConst):
166 _NAME = "Const signed integer"
167
168
169 class _SignedIntegerFieldClass(
170 _SignedIntegerFieldClassConst, _IntegerFieldClass, _FieldClass
171 ):
172 _NAME = "Signed integer"
173
174
175 class _RealFieldClassConst(_FieldClassConst):
176 pass
177
178
179 class _SinglePrecisionRealFieldClassConst(_RealFieldClassConst):
180 _NAME = "Const single-precision real"
181
182
183 class _DoublePrecisionRealFieldClassConst(_RealFieldClassConst):
184 _NAME = "Const double-precision real"
185
186
187 class _RealFieldClass(_FieldClass, _RealFieldClassConst):
188 pass
189
190
191 class _SinglePrecisionRealFieldClass(_RealFieldClass):
192 _NAME = "Single-precision real"
193
194
195 class _DoublePrecisionRealFieldClass(_RealFieldClass):
196 _NAME = "Double-precision real"
197
198
199 # an enumeration field class mapping does not have a reference count, so
200 # we copy the properties here to avoid eventual memory access errors.
201 class _EnumerationFieldClassMapping:
202 def __init__(self, mapping_ptr):
203 base_mapping_ptr = self._as_enumeration_field_class_mapping_ptr(mapping_ptr)
204 self._label = native_bt.field_class_enumeration_mapping_get_label(
205 base_mapping_ptr
206 )
207 assert self._label is not None
208 ranges_ptr = self._mapping_borrow_ranges_ptr(mapping_ptr)
209 assert ranges_ptr is not None
210 self._ranges = self._range_set_pycls._create_from_ptr_and_get_ref(ranges_ptr)
211
212 @property
213 def label(self):
214 return self._label
215
216 @property
217 def ranges(self):
218 return self._ranges
219
220
221 class _UnsignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping):
222 _range_set_pycls = bt2_integer_range_set._UnsignedIntegerRangeSetConst
223 _as_enumeration_field_class_mapping_ptr = staticmethod(
224 native_bt.field_class_enumeration_unsigned_mapping_as_mapping_const
225 )
226 _mapping_borrow_ranges_ptr = staticmethod(
227 native_bt.field_class_enumeration_unsigned_mapping_borrow_ranges_const
228 )
229
230
231 class _SignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping):
232 _range_set_pycls = bt2_integer_range_set._SignedIntegerRangeSetConst
233 _as_enumeration_field_class_mapping_ptr = staticmethod(
234 native_bt.field_class_enumeration_signed_mapping_as_mapping_const
235 )
236 _mapping_borrow_ranges_ptr = staticmethod(
237 native_bt.field_class_enumeration_signed_mapping_borrow_ranges_const
238 )
239
240
241 class _EnumerationFieldClassConst(_IntegerFieldClassConst, collections.abc.Mapping):
242 def __len__(self):
243 count = native_bt.field_class_enumeration_get_mapping_count(self._ptr)
244 assert count >= 0
245 return count
246
247 def mappings_for_value(self, value):
248 self._check_int_type(value)
249
250 status, labels = self._get_mapping_labels_for_value(self._ptr, value)
251 bt2_utils._handle_func_status(
252 status, "cannot get mapping labels for value {}".format(value)
253 )
254 return [self[label] for label in labels]
255
256 def __iter__(self):
257 for idx in range(len(self)):
258 mapping_ptr = self._borrow_mapping_ptr_by_index(self._ptr, idx)
259 yield self._mapping_pycls(mapping_ptr).label
260
261 def __getitem__(self, label):
262 bt2_utils._check_str(label)
263 mapping_ptr = self._borrow_mapping_ptr_by_label(self._ptr, label)
264
265 if mapping_ptr is None:
266 raise KeyError(label)
267
268 return self._mapping_pycls(mapping_ptr)
269
270
271 class _EnumerationFieldClass(_EnumerationFieldClassConst, _IntegerFieldClass):
272 def add_mapping(self, label, ranges):
273 bt2_utils._check_str(label)
274 bt2_utils._check_type(ranges, self._range_set_pycls)
275
276 if label in self:
277 raise ValueError("duplicate mapping label '{}'".format(label))
278
279 status = self._add_mapping(self._ptr, label, ranges._ptr)
280 bt2_utils._handle_func_status(
281 status, "cannot add mapping to enumeration field class object"
282 )
283
284 def __iadd__(self, mappings):
285 for label, ranges in mappings:
286 self.add_mapping(label, ranges)
287
288 return self
289
290
291 class _UnsignedEnumerationFieldClassConst(
292 _EnumerationFieldClassConst, _UnsignedIntegerFieldClassConst
293 ):
294 _NAME = "Const unsigned enumeration"
295 _borrow_mapping_ptr_by_label = staticmethod(
296 native_bt.field_class_enumeration_unsigned_borrow_mapping_by_label_const
297 )
298 _borrow_mapping_ptr_by_index = staticmethod(
299 native_bt.field_class_enumeration_unsigned_borrow_mapping_by_index_const
300 )
301 _mapping_pycls = property(lambda _: _UnsignedEnumerationFieldClassMappingConst)
302 _get_mapping_labels_for_value = staticmethod(
303 native_bt.field_class_enumeration_unsigned_get_mapping_labels_for_value
304 )
305 _check_int_type = staticmethod(bt2_utils._check_uint64)
306
307
308 class _UnsignedEnumerationFieldClass(
309 _UnsignedEnumerationFieldClassConst,
310 _EnumerationFieldClass,
311 _UnsignedIntegerFieldClass,
312 ):
313 _NAME = "Unsigned enumeration"
314 _range_set_pycls = bt2_integer_range_set.UnsignedIntegerRangeSet
315 _add_mapping = staticmethod(native_bt.field_class_enumeration_unsigned_add_mapping)
316
317
318 class _SignedEnumerationFieldClassConst(
319 _EnumerationFieldClassConst, _SignedIntegerFieldClassConst
320 ):
321 _NAME = "Const signed enumeration"
322 _borrow_mapping_ptr_by_label = staticmethod(
323 native_bt.field_class_enumeration_signed_borrow_mapping_by_label_const
324 )
325 _borrow_mapping_ptr_by_index = staticmethod(
326 native_bt.field_class_enumeration_signed_borrow_mapping_by_index_const
327 )
328 _mapping_pycls = property(lambda _: _SignedEnumerationFieldClassMappingConst)
329 _get_mapping_labels_for_value = staticmethod(
330 native_bt.field_class_enumeration_signed_get_mapping_labels_for_value
331 )
332 _check_int_type = staticmethod(bt2_utils._check_int64)
333
334
335 class _SignedEnumerationFieldClass(
336 _SignedEnumerationFieldClassConst, _EnumerationFieldClass, _SignedIntegerFieldClass
337 ):
338 _NAME = "Signed enumeration"
339 _range_set_pycls = bt2_integer_range_set.SignedIntegerRangeSet
340 _add_mapping = staticmethod(native_bt.field_class_enumeration_signed_add_mapping)
341
342
343 class _StringFieldClassConst(_FieldClassConst):
344 _NAME = "Const string"
345
346
347 class _StringFieldClass(_StringFieldClassConst, _FieldClass):
348 _NAME = "String"
349
350
351 class _StructureFieldClassMemberConst:
352 _create_field_class_from_ptr_and_get_ref = staticmethod(
353 _create_field_class_from_const_ptr_and_get_ref
354 )
355 _borrow_field_class_ptr = staticmethod(
356 native_bt.field_class_structure_member_borrow_field_class_const
357 )
358 _borrow_user_attributes_ptr = staticmethod(
359 native_bt.field_class_structure_member_borrow_user_attributes_const
360 )
361 _create_value_from_ptr_and_get_ref = staticmethod(
362 bt2_value._create_from_const_ptr_and_get_ref
363 )
364
365 def __init__(self, owning_struct_fc, member_ptr):
366 # this field class owns the member; keeping it here maintains
367 # the member alive as members are not shared objects
368 self._owning_struct_fc = owning_struct_fc
369 self._ptr = member_ptr
370
371 @property
372 def name(self):
373 name = native_bt.field_class_structure_member_get_name(self._ptr)
374 assert name is not None
375 return name
376
377 @property
378 def field_class(self):
379 fc_ptr = self._borrow_field_class_ptr(self._ptr)
380 assert fc_ptr is not None
381 return self._create_field_class_from_ptr_and_get_ref(fc_ptr)
382
383 @property
384 def user_attributes(self):
385 ptr = self._borrow_user_attributes_ptr(self._ptr)
386 assert ptr is not None
387 return self._create_value_from_ptr_and_get_ref(ptr)
388
389
390 class _StructureFieldClassMember(_StructureFieldClassMemberConst):
391 _borrow_field_class_ptr = staticmethod(
392 native_bt.field_class_structure_member_borrow_field_class
393 )
394 _borrow_user_attributes_ptr = staticmethod(
395 native_bt.field_class_structure_member_borrow_user_attributes
396 )
397 _create_field_class_from_ptr_and_get_ref = staticmethod(
398 _create_field_class_from_ptr_and_get_ref
399 )
400 _create_value_from_ptr_and_get_ref = staticmethod(
401 bt2_value._create_from_ptr_and_get_ref
402 )
403
404 def _user_attributes(self, user_attributes):
405 value = bt2_value.create_value(user_attributes)
406 bt2_utils._check_type(value, bt2_value.MapValue)
407 native_bt.field_class_structure_member_set_user_attributes(
408 self._ptr, value._ptr
409 )
410
411 _user_attributes = property(fset=_user_attributes)
412
413
414 class _StructureFieldClassConst(_FieldClassConst, collections.abc.Mapping):
415 _NAME = "Const structure"
416 _borrow_member_ptr_by_index = staticmethod(
417 native_bt.field_class_structure_borrow_member_by_index_const
418 )
419 _borrow_member_ptr_by_name = staticmethod(
420 native_bt.field_class_structure_borrow_member_by_name_const
421 )
422 _structure_member_field_class_pycls = property(
423 lambda _: _StructureFieldClassMemberConst
424 )
425
426 def __len__(self):
427 count = native_bt.field_class_structure_get_member_count(self._ptr)
428 assert count >= 0
429 return count
430
431 def __getitem__(self, key):
432 if not isinstance(key, str):
433 raise TypeError(
434 "key must be a 'str' object, got '{}'".format(key.__class__.__name__)
435 )
436
437 member_ptr = self._borrow_member_ptr_by_name(self._ptr, key)
438
439 if member_ptr is None:
440 raise KeyError(key)
441
442 return self._structure_member_field_class_pycls(self, member_ptr)
443
444 def __iter__(self):
445 for idx in range(len(self)):
446 member_ptr = self._borrow_member_ptr_by_index(self._ptr, idx)
447 assert member_ptr is not None
448 yield native_bt.field_class_structure_member_get_name(member_ptr)
449
450 def member_at_index(self, index):
451 bt2_utils._check_uint64(index)
452
453 if index >= len(self):
454 raise IndexError
455
456 member_ptr = self._borrow_member_ptr_by_index(self._ptr, index)
457 assert member_ptr is not None
458 return self._structure_member_field_class_pycls(self, member_ptr)
459
460
461 class _StructureFieldClass(_StructureFieldClassConst, _FieldClass):
462 _NAME = "Structure"
463 _borrow_member_by_index = staticmethod(
464 native_bt.field_class_structure_borrow_member_by_index
465 )
466 _borrow_member_ptr_by_name = staticmethod(
467 native_bt.field_class_structure_borrow_member_by_name
468 )
469 _structure_member_field_class_pycls = property(lambda _: _StructureFieldClassMember)
470
471 def append_member(self, name, field_class, user_attributes=None):
472 bt2_utils._check_str(name)
473 bt2_utils._check_type(field_class, _FieldClass)
474
475 if name in self:
476 raise ValueError("duplicate member name '{}'".format(name))
477
478 user_attributes_value = None
479
480 if user_attributes is not None:
481 # check now that user attributes are valid
482 user_attributes_value = bt2_value.create_value(user_attributes)
483
484 status = native_bt.field_class_structure_append_member(
485 self._ptr, name, field_class._ptr
486 )
487 bt2_utils._handle_func_status(
488 status, "cannot append member to structure field class object"
489 )
490
491 if user_attributes is not None:
492 self[name]._user_attributes = user_attributes_value
493
494 def __iadd__(self, members):
495 for name, field_class in members:
496 self.append_member(name, field_class)
497
498 return self
499
500
501 class _OptionFieldClassConst(_FieldClassConst):
502 _NAME = "Const option"
503 _create_field_class_from_ptr_and_get_ref = staticmethod(
504 _create_field_class_from_const_ptr_and_get_ref
505 )
506 _borrow_field_class_ptr = staticmethod(
507 native_bt.field_class_option_borrow_field_class_const
508 )
509
510 @property
511 def field_class(self):
512 elem_fc_ptr = self._borrow_field_class_ptr(self._ptr)
513 return self._create_field_class_from_ptr_and_get_ref(elem_fc_ptr)
514
515
516 class _OptionWithSelectorFieldClassConst(_OptionFieldClassConst):
517 _NAME = "Const option (with selector)"
518
519 @property
520 def selector_field_path(self):
521 ptr = native_bt.field_class_option_with_selector_field_borrow_selector_field_path_const(
522 self._ptr
523 )
524 if ptr is None:
525 return
526
527 return bt2_field_path._FieldPathConst._create_from_ptr_and_get_ref(ptr)
528
529
530 class _OptionWithBoolSelectorFieldClassConst(_OptionWithSelectorFieldClassConst):
531 _NAME = "Const option (with boolean selector)"
532
533 @property
534 def selector_is_reversed(self):
535 return bool(
536 native_bt.field_class_option_with_selector_field_bool_selector_is_reversed(
537 self._ptr
538 )
539 )
540
541
542 class _OptionWithIntegerSelectorFieldClassConst(_OptionWithSelectorFieldClassConst):
543 _NAME = "Const option (with integer selector)"
544
545 @property
546 def ranges(self):
547 range_set_ptr = self._borrow_selector_ranges_ptr(self._ptr)
548 assert range_set_ptr is not None
549 return self._range_set_pycls._create_from_ptr_and_get_ref(range_set_ptr)
550
551
552 class _OptionWithUnsignedIntegerSelectorFieldClassConst(
553 _OptionWithIntegerSelectorFieldClassConst
554 ):
555 _NAME = "Const option (with unsigned integer selector)"
556 _range_set_pycls = bt2_integer_range_set._UnsignedIntegerRangeSetConst
557 _borrow_selector_ranges_ptr = staticmethod(
558 native_bt.field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const
559 )
560
561
562 class _OptionWithSignedIntegerSelectorFieldClassConst(
563 _OptionWithIntegerSelectorFieldClassConst
564 ):
565 _NAME = "Const option (with signed integer selector)"
566 _range_set_pycls = bt2_integer_range_set._SignedIntegerRangeSetConst
567 _borrow_selector_ranges_ptr = staticmethod(
568 native_bt.field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const
569 )
570
571
572 class _OptionFieldClass(_OptionFieldClassConst, _FieldClass):
573 _NAME = "Option"
574 _borrow_field_class_ptr = staticmethod(
575 native_bt.field_class_option_borrow_field_class
576 )
577 _create_field_class_from_ptr_and_get_ref = staticmethod(
578 _create_field_class_from_ptr_and_get_ref
579 )
580
581
582 class _OptionWithSelectorFieldClass(
583 _OptionWithSelectorFieldClassConst, _OptionFieldClass
584 ):
585 _NAME = "Option (with selector)"
586
587
588 class _OptionWithBoolSelectorFieldClass(
589 _OptionWithBoolSelectorFieldClassConst, _OptionWithSelectorFieldClass
590 ):
591 _NAME = "Option (with boolean selector)"
592
593 def _selector_is_reversed(self, selector_is_reversed):
594 bt2_utils._check_bool(selector_is_reversed)
595 native_bt.field_class_option_with_selector_field_bool_set_selector_is_reversed(
596 self._ptr, selector_is_reversed
597 )
598
599 _selector_is_reversed = property(fset=_selector_is_reversed)
600
601
602 class _OptionWithIntegerSelectorFieldClass(
603 _OptionWithIntegerSelectorFieldClassConst, _OptionWithSelectorFieldClass
604 ):
605 _NAME = "Option (with integer selector)"
606
607
608 class _OptionWithUnsignedIntegerSelectorFieldClass(
609 _OptionWithUnsignedIntegerSelectorFieldClassConst,
610 _OptionWithIntegerSelectorFieldClass,
611 ):
612 _NAME = "Option (with unsigned integer selector)"
613
614
615 class _OptionWithSignedIntegerSelectorFieldClass(
616 _OptionWithSignedIntegerSelectorFieldClassConst,
617 _OptionWithIntegerSelectorFieldClass,
618 ):
619 _NAME = "Option (with signed integer selector)"
620
621
622 class _VariantFieldClassOptionConst:
623 _create_field_class_from_ptr_and_get_ref = staticmethod(
624 _create_field_class_from_const_ptr_and_get_ref
625 )
626 _borrow_field_class_ptr = staticmethod(
627 native_bt.field_class_variant_option_borrow_field_class_const
628 )
629 _borrow_user_attributes_ptr = staticmethod(
630 native_bt.field_class_variant_option_borrow_user_attributes_const
631 )
632 _create_value_from_ptr_and_get_ref = staticmethod(
633 bt2_value._create_from_const_ptr_and_get_ref
634 )
635
636 def __init__(self, owning_var_fc, option_ptr):
637 # this field class owns the option; keeping it here maintains
638 # the option alive as options are not shared objects
639 self._owning_var_fc = owning_var_fc
640 self._ptr = option_ptr
641
642 @property
643 def name(self):
644 name = native_bt.field_class_variant_option_get_name(self._ptr)
645 assert name is not None
646 return name
647
648 @property
649 def field_class(self):
650 fc_ptr = self._borrow_field_class_ptr(self._ptr)
651 assert fc_ptr is not None
652 return self._create_field_class_from_ptr_and_get_ref(fc_ptr)
653
654 @property
655 def user_attributes(self):
656 ptr = self._borrow_user_attributes_ptr(self._ptr)
657 assert ptr is not None
658 return self._create_value_from_ptr_and_get_ref(ptr)
659
660
661 class _VariantFieldClassOption(_VariantFieldClassOptionConst):
662 _create_field_class_from_ptr_and_get_ref = staticmethod(
663 _create_field_class_from_ptr_and_get_ref
664 )
665 _borrow_field_class_ptr = staticmethod(
666 native_bt.field_class_variant_option_borrow_field_class
667 )
668 _borrow_user_attributes_ptr = staticmethod(
669 native_bt.field_class_variant_option_borrow_user_attributes
670 )
671 _create_value_from_ptr_and_get_ref = staticmethod(
672 bt2_value._create_from_ptr_and_get_ref
673 )
674
675 def _user_attributes(self, user_attributes):
676 value = bt2_value.create_value(user_attributes)
677 bt2_utils._check_type(value, bt2_value.MapValue)
678 native_bt.field_class_variant_option_set_user_attributes(self._ptr, value._ptr)
679
680 _user_attributes = property(fset=_user_attributes)
681
682
683 class _VariantFieldClassWithIntegerSelectorOptionConst(_VariantFieldClassOptionConst):
684 def __init__(self, owning_var_fc, spec_opt_ptr):
685 self._spec_ptr = spec_opt_ptr
686 super().__init__(owning_var_fc, self._as_option_ptr(spec_opt_ptr))
687
688 @property
689 def ranges(self):
690 range_set_ptr = self._borrow_ranges_ptr(self._spec_ptr)
691 assert range_set_ptr is not None
692 return self._range_set_pycls._create_from_ptr_and_get_ref(range_set_ptr)
693
694
695 class _VariantFieldClassWithIntegerSelectorOption(
696 _VariantFieldClassWithIntegerSelectorOptionConst, _VariantFieldClassOption
697 ):
698 pass
699
700
701 class _VariantFieldClassWithSignedIntegerSelectorOptionConst(
702 _VariantFieldClassWithIntegerSelectorOptionConst
703 ):
704 _as_option_ptr = staticmethod(
705 native_bt.field_class_variant_with_selector_field_integer_signed_option_as_option_const
706 )
707 _borrow_ranges_ptr = staticmethod(
708 native_bt.field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const
709 )
710 _range_set_pycls = bt2_integer_range_set._SignedIntegerRangeSetConst
711
712
713 class _VariantFieldClassWithSignedIntegerSelectorOption(
714 _VariantFieldClassWithSignedIntegerSelectorOptionConst,
715 _VariantFieldClassWithIntegerSelectorOption,
716 ):
717 pass
718
719
720 class _VariantFieldClassWithUnsignedIntegerSelectorOptionConst(
721 _VariantFieldClassWithIntegerSelectorOptionConst
722 ):
723 _as_option_ptr = staticmethod(
724 native_bt.field_class_variant_with_selector_field_integer_unsigned_option_as_option_const
725 )
726 _borrow_ranges_ptr = staticmethod(
727 native_bt.field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const
728 )
729 _range_set_pycls = bt2_integer_range_set._UnsignedIntegerRangeSetConst
730
731
732 class _VariantFieldClassWithUnsignedIntegerSelectorOption(
733 _VariantFieldClassWithUnsignedIntegerSelectorOptionConst,
734 _VariantFieldClassWithIntegerSelectorOption,
735 ):
736 pass
737
738
739 class _VariantFieldClassConst(_FieldClassConst, collections.abc.Mapping):
740 _NAME = "Const variant"
741 _borrow_option_ptr_by_name = staticmethod(
742 native_bt.field_class_variant_borrow_option_by_name_const
743 )
744 _borrow_option_ptr_by_index = staticmethod(
745 native_bt.field_class_variant_borrow_option_by_index_const
746 )
747 _variant_option_pycls = _VariantFieldClassOptionConst
748
749 @staticmethod
750 def _as_option_ptr(opt_ptr):
751 return opt_ptr
752
753 def _create_option_from_ptr(self, opt_ptr):
754 return self._variant_option_pycls(self, opt_ptr)
755
756 def __len__(self):
757 count = native_bt.field_class_variant_get_option_count(self._ptr)
758 assert count >= 0
759 return count
760
761 def __getitem__(self, key):
762 if not isinstance(key, str):
763 raise TypeError(
764 "key must be a 'str' object, got '{}'".format(key.__class__.__name__)
765 )
766
767 opt_ptr = self._borrow_option_ptr_by_name(self._ptr, key)
768
769 if opt_ptr is None:
770 raise KeyError(key)
771
772 return self._create_option_from_ptr(opt_ptr)
773
774 def __iter__(self):
775 for idx in range(len(self)):
776 opt_ptr = self._borrow_option_ptr_by_index(self._ptr, idx)
777 assert opt_ptr is not None
778 base_opt_ptr = self._as_option_ptr(opt_ptr)
779 yield native_bt.field_class_variant_option_get_name(base_opt_ptr)
780
781 def option_at_index(self, index):
782 bt2_utils._check_uint64(index)
783
784 if index >= len(self):
785 raise IndexError
786
787 opt_ptr = self._borrow_option_ptr_by_index(self._ptr, index)
788 assert opt_ptr is not None
789 return self._create_option_from_ptr(opt_ptr)
790
791
792 class _VariantFieldClass(_VariantFieldClassConst, _FieldClass, collections.abc.Mapping):
793 _NAME = "Variant"
794 _borrow_option_ptr_by_name = staticmethod(
795 native_bt.field_class_variant_borrow_option_by_name
796 )
797 _borrow_option_ptr_by_index = staticmethod(
798 native_bt.field_class_variant_borrow_option_by_index
799 )
800 _variant_option_pycls = _VariantFieldClassOption
801
802
803 class _VariantFieldClassWithoutSelectorConst(_VariantFieldClassConst):
804 _NAME = "Const variant (without selector)"
805
806
807 class _VariantFieldClassWithoutSelector(
808 _VariantFieldClassWithoutSelectorConst, _VariantFieldClass
809 ):
810 _NAME = "Variant (without selector)"
811
812 def append_option(self, name, field_class, user_attributes=None):
813 bt2_utils._check_str(name)
814 bt2_utils._check_type(field_class, _FieldClass)
815
816 if name in self:
817 raise ValueError("duplicate option name '{}'".format(name))
818
819 user_attributes_value = None
820
821 if user_attributes is not None:
822 # check now that user attributes are valid
823 user_attributes_value = bt2_value.create_value(user_attributes)
824
825 status = native_bt.field_class_variant_without_selector_append_option(
826 self._ptr, name, field_class._ptr
827 )
828 bt2_utils._handle_func_status(
829 status, "cannot append option to variant field class object"
830 )
831
832 if user_attributes is not None:
833 self[name]._user_attributes = user_attributes_value
834
835 def __iadd__(self, options):
836 for name, field_class in options:
837 self.append_option(name, field_class)
838
839 return self
840
841
842 class _VariantFieldClassWithIntegerSelectorConst(_VariantFieldClassConst):
843 _NAME = "Const variant (with selector)"
844
845 @property
846 def selector_field_path(self):
847 ptr = native_bt.field_class_variant_with_selector_field_borrow_selector_field_path_const(
848 self._ptr
849 )
850
851 if ptr is None:
852 return
853
854 return bt2_field_path._FieldPathConst._create_from_ptr_and_get_ref(ptr)
855
856
857 class _VariantFieldClassWithIntegerSelector(
858 _VariantFieldClassWithIntegerSelectorConst, _VariantFieldClass
859 ):
860 _NAME = "Variant (with selector)"
861
862 def append_option(self, name, field_class, ranges, user_attributes=None):
863 bt2_utils._check_str(name)
864 bt2_utils._check_type(field_class, _FieldClass)
865 bt2_utils._check_type(ranges, self._variant_option_pycls._range_set_pycls)
866
867 if name in self:
868 raise ValueError("duplicate option name '{}'".format(name))
869
870 if len(ranges) == 0:
871 raise ValueError("range set is empty")
872
873 user_attributes_value = None
874
875 if user_attributes is not None:
876 # check now that user attributes are valid
877 user_attributes_value = bt2_value.create_value(user_attributes)
878
879 # TODO: check overlaps (precondition of self._append_option())
880
881 status = self._append_option(self._ptr, name, field_class._ptr, ranges._ptr)
882 bt2_utils._handle_func_status(
883 status, "cannot append option to variant field class object"
884 )
885
886 if user_attributes is not None:
887 self[name]._user_attributes = user_attributes_value
888
889 def __iadd__(self, options):
890 for name, field_class, ranges in options:
891 self.append_option(name, field_class, ranges)
892
893 return self
894
895
896 class _VariantFieldClassWithUnsignedIntegerSelectorConst(
897 _VariantFieldClassWithIntegerSelectorConst
898 ):
899 _NAME = "Const variant (with unsigned integer selector)"
900 _borrow_option_ptr_by_name = staticmethod(
901 native_bt.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_name_const
902 )
903 _borrow_option_ptr_by_index = staticmethod(
904 native_bt.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const
905 )
906 _variant_option_pycls = _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
907 _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr)
908
909
910 class _VariantFieldClassWithUnsignedIntegerSelector(
911 _VariantFieldClassWithUnsignedIntegerSelectorConst,
912 _VariantFieldClassWithIntegerSelector,
913 ):
914 _NAME = "Variant (with unsigned integer selector)"
915 _variant_option_pycls = _VariantFieldClassWithUnsignedIntegerSelectorOption
916 _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr)
917 _append_option = staticmethod(
918 native_bt.field_class_variant_with_selector_field_integer_unsigned_append_option
919 )
920
921
922 class _VariantFieldClassWithSignedIntegerSelectorConst(
923 _VariantFieldClassWithIntegerSelectorConst
924 ):
925 _NAME = "Const variant (with signed integer selector)"
926 _borrow_option_ptr_by_name = staticmethod(
927 native_bt.field_class_variant_with_selector_field_integer_signed_borrow_option_by_name_const
928 )
929 _borrow_option_ptr_by_index = staticmethod(
930 native_bt.field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const
931 )
932 _variant_option_pycls = _VariantFieldClassWithSignedIntegerSelectorOptionConst
933 _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr)
934
935
936 class _VariantFieldClassWithSignedIntegerSelector(
937 _VariantFieldClassWithSignedIntegerSelectorConst,
938 _VariantFieldClassWithIntegerSelector,
939 ):
940 _NAME = "Variant (with signed integer selector)"
941 _variant_option_pycls = _VariantFieldClassWithSignedIntegerSelectorOption
942 _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr)
943 _append_option = staticmethod(
944 native_bt.field_class_variant_with_selector_field_integer_signed_append_option
945 )
946
947
948 class _ArrayFieldClassConst(_FieldClassConst):
949 _create_field_class_from_ptr_and_get_ref = staticmethod(
950 _create_field_class_from_const_ptr_and_get_ref
951 )
952 _borrow_element_field_class = staticmethod(
953 native_bt.field_class_array_borrow_element_field_class_const
954 )
955
956 @property
957 def element_field_class(self):
958 elem_fc_ptr = self._borrow_element_field_class(self._ptr)
959 return self._create_field_class_from_ptr_and_get_ref(elem_fc_ptr)
960
961
962 class _ArrayFieldClass(_ArrayFieldClassConst, _FieldClass):
963 _create_field_class_from_ptr_and_get_ref = staticmethod(
964 _create_field_class_from_ptr_and_get_ref
965 )
966 _borrow_element_field_class = staticmethod(
967 native_bt.field_class_array_borrow_element_field_class
968 )
969
970
971 class _StaticArrayFieldClassConst(_ArrayFieldClassConst):
972 _NAME = "Const static array"
973
974 @property
975 def length(self):
976 return native_bt.field_class_array_static_get_length(self._ptr)
977
978
979 class _StaticArrayFieldClass(_StaticArrayFieldClassConst, _ArrayFieldClass):
980 _NAME = "Static array"
981
982
983 class _DynamicArrayFieldClassConst(_ArrayFieldClassConst):
984 _NAME = "Const dynamic array"
985
986
987 class _DynamicArrayWithLengthFieldFieldClassConst(_DynamicArrayFieldClassConst):
988 _NAME = "Const dynamic array (with length field)"
989
990 @property
991 def length_field_path(self):
992 ptr = native_bt.field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
993 self._ptr
994 )
995 if ptr is None:
996 return
997
998 return bt2_field_path._FieldPathConst._create_from_ptr_and_get_ref(ptr)
999
1000
1001 class _DynamicArrayFieldClass(_DynamicArrayFieldClassConst, _ArrayFieldClass):
1002 _NAME = "Dynamic array"
1003
1004
1005 class _DynamicArrayWithLengthFieldFieldClass(
1006 _DynamicArrayWithLengthFieldFieldClassConst, _DynamicArrayFieldClass
1007 ):
1008 _NAME = "Dynamic array (with length field)"
1009
1010
1011 _FIELD_CLASS_TYPE_TO_CONST_OBJ = {
1012 native_bt.FIELD_CLASS_TYPE_BOOL: _BoolFieldClassConst,
1013 native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayFieldClassConst,
1014 native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClassConst,
1015 native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClassConst,
1016 native_bt.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: _SinglePrecisionRealFieldClassConst,
1017 native_bt.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: _DoublePrecisionRealFieldClassConst,
1018 native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationFieldClassConst,
1019 native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationFieldClassConst,
1020 native_bt.FIELD_CLASS_TYPE_STRING: _StringFieldClassConst,
1021 native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldClassConst,
1022 native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldClassConst,
1023 native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: _DynamicArrayFieldClassConst,
1024 native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: _DynamicArrayWithLengthFieldFieldClassConst,
1025 native_bt.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: _OptionFieldClassConst,
1026 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: _OptionWithBoolSelectorFieldClassConst,
1027 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _OptionWithUnsignedIntegerSelectorFieldClassConst,
1028 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _OptionWithSignedIntegerSelectorFieldClassConst,
1029 native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: _VariantFieldClassWithoutSelectorConst,
1030 native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldClassWithUnsignedIntegerSelectorConst,
1031 native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldClassWithSignedIntegerSelectorConst,
1032 }
1033
1034 _FIELD_CLASS_TYPE_TO_OBJ = {
1035 native_bt.FIELD_CLASS_TYPE_BOOL: _BoolFieldClass,
1036 native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayFieldClass,
1037 native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClass,
1038 native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClass,
1039 native_bt.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: _SinglePrecisionRealFieldClass,
1040 native_bt.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: _DoublePrecisionRealFieldClass,
1041 native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationFieldClass,
1042 native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationFieldClass,
1043 native_bt.FIELD_CLASS_TYPE_STRING: _StringFieldClass,
1044 native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldClass,
1045 native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldClass,
1046 native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: _DynamicArrayFieldClass,
1047 native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: _DynamicArrayWithLengthFieldFieldClass,
1048 native_bt.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: _OptionFieldClass,
1049 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: _OptionWithBoolSelectorFieldClass,
1050 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _OptionWithUnsignedIntegerSelectorFieldClass,
1051 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _OptionWithSignedIntegerSelectorFieldClass,
1052 native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: _VariantFieldClassWithoutSelector,
1053 native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldClassWithUnsignedIntegerSelector,
1054 native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldClassWithSignedIntegerSelector,
1055 }
This page took 0.095359 seconds and 4 git commands to generate.