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