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