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