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