Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
811644b8 | 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 | ||
23 | from bt2 import native_bt, object, utils | |
24 | import collections.abc | |
3fb99a22 PP |
25 | from bt2 import field_path as bt2_field_path |
26 | from bt2 import integer_range_set as bt2_integer_range_set | |
81447b5b PP |
27 | import bt2 |
28 | ||
29 | ||
3cdfbaea SM |
30 | def _create_field_class_from_ptr_and_get_ref(ptr): |
31 | typeid = native_bt.field_class_get_type(ptr) | |
32 | return _FIELD_CLASS_TYPE_TO_OBJ[typeid]._create_from_ptr_and_get_ref(ptr) | |
81447b5b PP |
33 | |
34 | ||
d47b87ac SM |
35 | class IntegerDisplayBase: |
36 | BINARY = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY | |
37 | OCTAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL | |
38 | DECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL | |
39 | HEXADECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL | |
81447b5b | 40 | |
81447b5b | 41 | |
d47b87ac SM |
42 | class _FieldClass(object._SharedObject): |
43 | _get_ref = staticmethod(native_bt.field_class_get_ref) | |
44 | _put_ref = staticmethod(native_bt.field_class_put_ref) | |
81447b5b PP |
45 | |
46 | def _check_create_status(self, ptr): | |
47 | if ptr is None: | |
694c792b | 48 | raise bt2._MemoryError( |
cfbd7cf3 FD |
49 | 'cannot create {} field class object'.format(self._NAME.lower()) |
50 | ) | |
81447b5b | 51 | |
81447b5b | 52 | |
aae30e61 PP |
53 | class _BoolFieldClass(_FieldClass): |
54 | _NAME = 'Boolean' | |
55 | ||
56 | ||
ead8c3d4 PP |
57 | class _BitArrayFieldClass(_FieldClass): |
58 | _NAME = 'Bit array' | |
59 | ||
60 | @property | |
61 | def length(self): | |
62 | length = native_bt.field_class_bit_array_get_length(self._ptr) | |
63 | assert length >= 1 | |
64 | return length | |
65 | ||
66 | ||
af4bbfc7 | 67 | class _IntegerFieldClass(_FieldClass): |
81447b5b | 68 | @property |
d47b87ac SM |
69 | def field_value_range(self): |
70 | size = native_bt.field_class_integer_get_field_value_range(self._ptr) | |
cfbd7cf3 | 71 | assert size >= 1 |
81447b5b PP |
72 | return size |
73 | ||
d47b87ac SM |
74 | def _field_value_range(self, size): |
75 | if size < 1 or size > 64: | |
76 | raise ValueError("Value is outside valid range [1, 64] ({})".format(size)) | |
77 | native_bt.field_class_integer_set_field_value_range(self._ptr, size) | |
81447b5b | 78 | |
d47b87ac | 79 | _field_value_range = property(fset=_field_value_range) |
81447b5b PP |
80 | |
81 | @property | |
d47b87ac | 82 | def preferred_display_base(self): |
cfbd7cf3 FD |
83 | base = native_bt.field_class_integer_get_preferred_display_base(self._ptr) |
84 | assert base >= 0 | |
81447b5b PP |
85 | return base |
86 | ||
d47b87ac SM |
87 | def _preferred_display_base(self, base): |
88 | utils._check_uint64(base) | |
81447b5b | 89 | |
cfbd7cf3 FD |
90 | if base not in ( |
91 | IntegerDisplayBase.BINARY, | |
92 | IntegerDisplayBase.OCTAL, | |
93 | IntegerDisplayBase.DECIMAL, | |
94 | IntegerDisplayBase.HEXADECIMAL, | |
95 | ): | |
d47b87ac | 96 | raise ValueError("Display base is not a valid IntegerDisplayBase value") |
81447b5b | 97 | |
cfbd7cf3 | 98 | native_bt.field_class_integer_set_preferred_display_base(self._ptr, base) |
81447b5b | 99 | |
d47b87ac | 100 | _preferred_display_base = property(fset=_preferred_display_base) |
81447b5b PP |
101 | |
102 | ||
2ae9f48c | 103 | class _UnsignedIntegerFieldClass(_IntegerFieldClass): |
d47b87ac | 104 | _NAME = 'Unsigned integer' |
2ae9f48c SM |
105 | |
106 | ||
af4bbfc7 | 107 | class _SignedIntegerFieldClass(_IntegerFieldClass): |
d47b87ac | 108 | _NAME = 'Signed integer' |
2ae9f48c | 109 | |
af4bbfc7 | 110 | |
d47b87ac | 111 | class _RealFieldClass(_FieldClass): |
2ae9f48c | 112 | _NAME = 'Real' |
81447b5b | 113 | |
81447b5b | 114 | @property |
d47b87ac SM |
115 | def is_single_precision(self): |
116 | return native_bt.field_class_real_is_single_precision(self._ptr) | |
81447b5b | 117 | |
d47b87ac SM |
118 | def _is_single_precision(self, is_single_precision): |
119 | utils._check_bool(is_single_precision) | |
120 | native_bt.field_class_real_set_is_single_precision( | |
cfbd7cf3 FD |
121 | self._ptr, is_single_precision |
122 | ) | |
81447b5b | 123 | |
d47b87ac | 124 | _is_single_precision = property(fset=_is_single_precision) |
81447b5b PP |
125 | |
126 | ||
45c51519 PP |
127 | # an enumeration field class mapping does not have a reference count, so |
128 | # we copy the properties here to avoid eventual memory access errors. | |
129 | class _EnumerationFieldClassMapping: | |
d47b87ac | 130 | def __init__(self, mapping_ptr): |
45c51519 | 131 | base_mapping_ptr = self._as_enumeration_field_class_mapping_ptr(mapping_ptr) |
cfbd7cf3 FD |
132 | self._label = native_bt.field_class_enumeration_mapping_get_label( |
133 | base_mapping_ptr | |
134 | ) | |
45c51519 PP |
135 | assert self._label is not None |
136 | ranges_ptr = self._mapping_borrow_ranges_ptr(mapping_ptr) | |
137 | assert ranges_ptr is not None | |
138 | self._ranges = self._ranges_type._create_from_ptr_and_get_ref(ranges_ptr) | |
81447b5b PP |
139 | |
140 | @property | |
d47b87ac | 141 | def label(self): |
45c51519 | 142 | return self._label |
81447b5b | 143 | |
45c51519 PP |
144 | @property |
145 | def ranges(self): | |
146 | return self._ranges | |
81447b5b | 147 | |
81447b5b | 148 | |
d47b87ac | 149 | class _UnsignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping): |
3fb99a22 | 150 | _ranges_type = bt2_integer_range_set.UnsignedIntegerRangeSet |
cfbd7cf3 | 151 | _as_enumeration_field_class_mapping_ptr = staticmethod( |
9c08c816 | 152 | native_bt.field_class_enumeration_unsigned_mapping_as_mapping_const |
cfbd7cf3 FD |
153 | ) |
154 | _mapping_borrow_ranges_ptr = staticmethod( | |
9c08c816 | 155 | native_bt.field_class_enumeration_unsigned_mapping_borrow_ranges_const |
cfbd7cf3 | 156 | ) |
81447b5b | 157 | |
81447b5b | 158 | |
d47b87ac | 159 | class _SignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping): |
3fb99a22 | 160 | _ranges_type = bt2_integer_range_set.SignedIntegerRangeSet |
cfbd7cf3 | 161 | _as_enumeration_field_class_mapping_ptr = staticmethod( |
9c08c816 | 162 | native_bt.field_class_enumeration_signed_mapping_as_mapping_const |
cfbd7cf3 FD |
163 | ) |
164 | _mapping_borrow_ranges_ptr = staticmethod( | |
9c08c816 | 165 | native_bt.field_class_enumeration_signed_mapping_borrow_ranges_const |
cfbd7cf3 | 166 | ) |
81447b5b | 167 | |
81447b5b | 168 | |
d47b87ac | 169 | class _EnumerationFieldClass(_IntegerFieldClass, collections.abc.Mapping): |
81447b5b | 170 | def __len__(self): |
b4f45851 | 171 | count = native_bt.field_class_enumeration_get_mapping_count(self._ptr) |
45c51519 | 172 | assert count >= 0 |
81447b5b PP |
173 | return count |
174 | ||
45c51519 | 175 | def add_mapping(self, label, ranges): |
d47b87ac | 176 | utils._check_str(label) |
45c51519 | 177 | utils._check_type(ranges, self._range_set_type) |
81447b5b | 178 | |
45c51519 | 179 | if label in self: |
ce4923b0 | 180 | raise ValueError("duplicate mapping label '{}'".format(label)) |
81447b5b | 181 | |
45c51519 | 182 | status = self._add_mapping(self._ptr, label, ranges._ptr) |
cfbd7cf3 FD |
183 | utils._handle_func_status( |
184 | status, 'cannot add mapping to enumeration field class object' | |
185 | ) | |
81447b5b | 186 | |
45c51519 | 187 | def mappings_for_value(self, value): |
185ecf64 | 188 | status, labels = self._get_mapping_labels_for_value(self._ptr, value) |
cfbd7cf3 FD |
189 | utils._handle_func_status( |
190 | status, 'cannot get mapping labels for value {}'.format(value) | |
191 | ) | |
45c51519 | 192 | return [self[label] for label in labels] |
81447b5b | 193 | |
d47b87ac SM |
194 | def __iter__(self): |
195 | for idx in range(len(self)): | |
196 | mapping = self._get_mapping_by_index(self._ptr, idx) | |
197 | yield mapping.label | |
81447b5b | 198 | |
45c51519 PP |
199 | def __getitem__(self, label): |
200 | utils._check_str(label) | |
201 | mapping = self._get_mapping_by_label(self._ptr, label) | |
81447b5b | 202 | |
45c51519 PP |
203 | if mapping is None: |
204 | raise KeyError(label) | |
205 | ||
206 | return mapping | |
81447b5b | 207 | |
d47b87ac | 208 | def __iadd__(self, mappings): |
45c51519 PP |
209 | for label, ranges in mappings: |
210 | self.add_mapping(label, ranges) | |
81447b5b | 211 | |
d47b87ac | 212 | return self |
81447b5b | 213 | |
81447b5b | 214 | |
cfbd7cf3 FD |
215 | class _UnsignedEnumerationFieldClass( |
216 | _EnumerationFieldClass, _UnsignedIntegerFieldClass | |
217 | ): | |
d47b87ac | 218 | _NAME = 'Unsigned enumeration' |
3fb99a22 | 219 | _range_set_type = bt2_integer_range_set.UnsignedIntegerRangeSet |
9c08c816 | 220 | _add_mapping = staticmethod(native_bt.field_class_enumeration_unsigned_add_mapping) |
81447b5b | 221 | |
d47b87ac SM |
222 | @staticmethod |
223 | def _get_mapping_by_index(enum_ptr, index): | |
9c08c816 | 224 | mapping_ptr = native_bt.field_class_enumeration_unsigned_borrow_mapping_by_index_const( |
cfbd7cf3 FD |
225 | enum_ptr, index |
226 | ) | |
d47b87ac SM |
227 | assert mapping_ptr is not None |
228 | return _UnsignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 229 | |
d47b87ac | 230 | @staticmethod |
45c51519 | 231 | def _get_mapping_by_label(enum_ptr, label): |
9c08c816 | 232 | mapping_ptr = native_bt.field_class_enumeration_unsigned_borrow_mapping_by_label_const( |
cfbd7cf3 FD |
233 | enum_ptr, label |
234 | ) | |
45c51519 PP |
235 | |
236 | if mapping_ptr is None: | |
237 | return | |
238 | ||
239 | return _UnsignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 240 | |
d47b87ac | 241 | @staticmethod |
185ecf64 | 242 | def _get_mapping_labels_for_value(enum_ptr, value): |
d47b87ac | 243 | utils._check_uint64(value) |
9c08c816 | 244 | return native_bt.field_class_enumeration_unsigned_get_mapping_labels_for_value( |
cfbd7cf3 FD |
245 | enum_ptr, value |
246 | ) | |
81447b5b PP |
247 | |
248 | ||
d47b87ac SM |
249 | class _SignedEnumerationFieldClass(_EnumerationFieldClass, _SignedIntegerFieldClass): |
250 | _NAME = 'Signed enumeration' | |
3fb99a22 | 251 | _range_set_type = bt2_integer_range_set.SignedIntegerRangeSet |
9c08c816 | 252 | _add_mapping = staticmethod(native_bt.field_class_enumeration_signed_add_mapping) |
81447b5b | 253 | |
d47b87ac SM |
254 | @staticmethod |
255 | def _get_mapping_by_index(enum_ptr, index): | |
9c08c816 | 256 | mapping_ptr = native_bt.field_class_enumeration_signed_borrow_mapping_by_index_const( |
cfbd7cf3 FD |
257 | enum_ptr, index |
258 | ) | |
d47b87ac SM |
259 | assert mapping_ptr is not None |
260 | return _SignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 261 | |
d47b87ac | 262 | @staticmethod |
45c51519 | 263 | def _get_mapping_by_label(enum_ptr, label): |
9c08c816 | 264 | mapping_ptr = native_bt.field_class_enumeration_signed_borrow_mapping_by_label_const( |
cfbd7cf3 FD |
265 | enum_ptr, label |
266 | ) | |
45c51519 PP |
267 | |
268 | if mapping_ptr is None: | |
269 | return | |
270 | ||
271 | return _SignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 272 | |
d47b87ac | 273 | @staticmethod |
185ecf64 | 274 | def _get_mapping_labels_for_value(enum_ptr, value): |
d47b87ac | 275 | utils._check_int64(value) |
9c08c816 | 276 | return native_bt.field_class_enumeration_signed_get_mapping_labels_for_value( |
cfbd7cf3 FD |
277 | enum_ptr, value |
278 | ) | |
81447b5b | 279 | |
d47b87ac SM |
280 | |
281 | class _StringFieldClass(_FieldClass): | |
282 | _NAME = 'String' | |
81447b5b PP |
283 | |
284 | ||
45c51519 PP |
285 | class _StructureFieldClassMember: |
286 | def __init__(self, name, field_class): | |
287 | self._name = name | |
288 | self._field_class = field_class | |
289 | ||
290 | @property | |
291 | def name(self): | |
292 | return self._name | |
293 | ||
294 | @property | |
295 | def field_class(self): | |
296 | return self._field_class | |
297 | ||
298 | ||
299 | class _StructureFieldClass(_FieldClass, collections.abc.Mapping): | |
300 | _NAME = 'Structure' | |
301 | ||
302 | def append_member(self, name, field_class): | |
303 | utils._check_str(name) | |
304 | utils._check_type(field_class, _FieldClass) | |
305 | ||
306 | if name in self: | |
ce4923b0 | 307 | raise ValueError("duplicate member name '{}'".format(name)) |
45c51519 | 308 | |
cfbd7cf3 FD |
309 | status = native_bt.field_class_structure_append_member( |
310 | self._ptr, name, field_class._ptr | |
311 | ) | |
312 | utils._handle_func_status( | |
313 | status, 'cannot append member to structure field class object' | |
314 | ) | |
45c51519 | 315 | |
81447b5b | 316 | def __len__(self): |
45c51519 | 317 | count = native_bt.field_class_structure_get_member_count(self._ptr) |
d47b87ac | 318 | assert count >= 0 |
81447b5b PP |
319 | return count |
320 | ||
45c51519 PP |
321 | @staticmethod |
322 | def _create_member_from_ptr(member_ptr): | |
323 | name = native_bt.field_class_structure_member_get_name(member_ptr) | |
324 | assert name is not None | |
cfbd7cf3 FD |
325 | fc_ptr = native_bt.field_class_structure_member_borrow_field_class_const( |
326 | member_ptr | |
327 | ) | |
45c51519 PP |
328 | assert fc_ptr is not None |
329 | fc = _create_field_class_from_ptr_and_get_ref(fc_ptr) | |
330 | return _StructureFieldClassMember(name, fc) | |
331 | ||
81447b5b PP |
332 | def __getitem__(self, key): |
333 | if not isinstance(key, str): | |
cfbd7cf3 FD |
334 | raise TypeError( |
335 | "key must be a 'str' object, got '{}'".format(key.__class__.__name__) | |
336 | ) | |
81447b5b | 337 | |
cfbd7cf3 FD |
338 | member_ptr = native_bt.field_class_structure_borrow_member_by_name_const( |
339 | self._ptr, key | |
340 | ) | |
81447b5b | 341 | |
45c51519 | 342 | if member_ptr is None: |
81447b5b PP |
343 | raise KeyError(key) |
344 | ||
45c51519 | 345 | return self._create_member_from_ptr(member_ptr) |
81447b5b PP |
346 | |
347 | def __iter__(self): | |
d47b87ac | 348 | for idx in range(len(self)): |
cfbd7cf3 FD |
349 | member_ptr = native_bt.field_class_structure_borrow_member_by_index_const( |
350 | self._ptr, idx | |
351 | ) | |
45c51519 PP |
352 | assert member_ptr is not None |
353 | yield native_bt.field_class_structure_member_get_name(member_ptr) | |
81447b5b | 354 | |
45c51519 PP |
355 | def __iadd__(self, members): |
356 | for name, field_class in members: | |
357 | self.append_member(name, field_class) | |
81447b5b PP |
358 | |
359 | return self | |
360 | ||
45c51519 | 361 | def member_at_index(self, index): |
81447b5b | 362 | utils._check_uint64(index) |
81447b5b | 363 | |
45c51519 | 364 | if index >= len(self): |
811644b8 PP |
365 | raise IndexError |
366 | ||
cfbd7cf3 FD |
367 | member_ptr = native_bt.field_class_structure_borrow_member_by_index_const( |
368 | self._ptr, index | |
369 | ) | |
45c51519 PP |
370 | assert member_ptr is not None |
371 | return self._create_member_from_ptr(member_ptr) | |
372 | ||
81447b5b | 373 | |
cec0261d PP |
374 | class _OptionFieldClass(_FieldClass): |
375 | @property | |
376 | def field_class(self): | |
377 | elem_fc_ptr = native_bt.field_class_option_borrow_field_class_const(self._ptr) | |
378 | return _create_field_class_from_ptr_and_get_ref(elem_fc_ptr) | |
379 | ||
380 | @property | |
381 | def selector_field_path(self): | |
382 | ptr = native_bt.field_class_option_borrow_selector_field_path_const(self._ptr) | |
383 | if ptr is None: | |
384 | return | |
385 | ||
386 | return bt2_field_path._FieldPath._create_from_ptr_and_get_ref(ptr) | |
387 | ||
388 | ||
45c51519 PP |
389 | class _VariantFieldClassOption: |
390 | def __init__(self, name, field_class): | |
391 | self._name = name | |
392 | self._field_class = field_class | |
81447b5b | 393 | |
45c51519 PP |
394 | @property |
395 | def name(self): | |
396 | return self._name | |
81447b5b | 397 | |
45c51519 PP |
398 | @property |
399 | def field_class(self): | |
400 | return self._field_class | |
81447b5b | 401 | |
81447b5b | 402 | |
45c51519 PP |
403 | class _VariantFieldClassWithSelectorOption(_VariantFieldClassOption): |
404 | def __init__(self, name, field_class, ranges): | |
405 | super().__init__(name, field_class) | |
406 | self._ranges = ranges | |
81447b5b | 407 | |
45c51519 PP |
408 | @property |
409 | def ranges(self): | |
410 | return self._ranges | |
81447b5b PP |
411 | |
412 | ||
45c51519 | 413 | class _VariantFieldClass(_FieldClass, collections.abc.Mapping): |
81447b5b | 414 | _NAME = 'Variant' |
cfbd7cf3 FD |
415 | _borrow_option_by_name_ptr = staticmethod( |
416 | native_bt.field_class_variant_borrow_option_by_name_const | |
417 | ) | |
418 | _borrow_member_by_index_ptr = staticmethod( | |
419 | native_bt.field_class_variant_borrow_option_by_index_const | |
420 | ) | |
811644b8 | 421 | |
45c51519 PP |
422 | @staticmethod |
423 | def _as_option_ptr(opt_ptr): | |
424 | return opt_ptr | |
425 | ||
426 | def _create_option_from_ptr(self, opt_ptr): | |
427 | name = native_bt.field_class_variant_option_get_name(opt_ptr) | |
428 | assert name is not None | |
429 | fc_ptr = native_bt.field_class_variant_option_borrow_field_class_const(opt_ptr) | |
430 | assert fc_ptr is not None | |
431 | fc = _create_field_class_from_ptr_and_get_ref(fc_ptr) | |
432 | return _VariantFieldClassOption(name, fc) | |
433 | ||
434 | def __len__(self): | |
435 | count = native_bt.field_class_variant_get_option_count(self._ptr) | |
436 | assert count >= 0 | |
437 | return count | |
438 | ||
439 | def __getitem__(self, key): | |
440 | if not isinstance(key, str): | |
cfbd7cf3 FD |
441 | raise TypeError( |
442 | "key must be a 'str' object, got '{}'".format(key.__class__.__name__) | |
443 | ) | |
45c51519 PP |
444 | |
445 | opt_ptr = self._borrow_option_by_name_ptr(self._ptr, key) | |
446 | ||
447 | if opt_ptr is None: | |
448 | raise KeyError(key) | |
449 | ||
450 | return self._create_option_from_ptr(opt_ptr) | |
451 | ||
452 | def __iter__(self): | |
453 | for idx in range(len(self)): | |
454 | opt_ptr = self._borrow_member_by_index_ptr(self._ptr, idx) | |
455 | assert opt_ptr is not None | |
456 | base_opt_ptr = self._as_option_ptr(opt_ptr) | |
457 | yield native_bt.field_class_variant_option_get_name(base_opt_ptr) | |
811644b8 | 458 | |
d47b87ac | 459 | def option_at_index(self, index): |
45c51519 PP |
460 | utils._check_uint64(index) |
461 | ||
462 | if index >= len(self): | |
463 | raise IndexError | |
464 | ||
465 | opt_ptr = self._borrow_member_by_index_ptr(self._ptr, index) | |
466 | assert opt_ptr is not None | |
467 | return self._create_option_from_ptr(opt_ptr) | |
468 | ||
469 | ||
470 | class _VariantFieldClassWithoutSelector(_VariantFieldClass): | |
471 | _NAME = 'Variant (without selector)' | |
472 | ||
473 | def append_option(self, name, field_class): | |
474 | utils._check_str(name) | |
475 | utils._check_type(field_class, _FieldClass) | |
476 | ||
477 | if name in self: | |
ce4923b0 | 478 | raise ValueError("duplicate option name '{}'".format(name)) |
45c51519 | 479 | |
cfbd7cf3 FD |
480 | status = native_bt.field_class_variant_without_selector_append_option( |
481 | self._ptr, name, field_class._ptr | |
482 | ) | |
483 | utils._handle_func_status( | |
484 | status, 'cannot append option to variant field class object' | |
485 | ) | |
45c51519 PP |
486 | |
487 | def __iadd__(self, options): | |
488 | for name, field_class in options: | |
489 | self.append_option(name, field_class) | |
490 | ||
491 | return self | |
492 | ||
493 | ||
494 | class _VariantFieldClassWithSelector(_VariantFieldClass): | |
495 | _NAME = 'Variant (with selector)' | |
496 | ||
497 | def _create_option_from_ptr(self, opt_ptr): | |
498 | base_opt_ptr = self._as_option_ptr(opt_ptr) | |
499 | name = native_bt.field_class_variant_option_get_name(base_opt_ptr) | |
500 | assert name is not None | |
cfbd7cf3 FD |
501 | fc_ptr = native_bt.field_class_variant_option_borrow_field_class_const( |
502 | base_opt_ptr | |
503 | ) | |
45c51519 PP |
504 | assert fc_ptr is not None |
505 | fc = _create_field_class_from_ptr_and_get_ref(fc_ptr) | |
506 | range_set_ptr = self._option_borrow_ranges_ptr(opt_ptr) | |
507 | assert range_set_ptr is not None | |
508 | range_set = self._range_set_type._create_from_ptr_and_get_ref(range_set_ptr) | |
509 | return _VariantFieldClassWithSelectorOption(name, fc, range_set) | |
81447b5b PP |
510 | |
511 | @property | |
d47b87ac | 512 | def selector_field_path(self): |
cfbd7cf3 FD |
513 | ptr = native_bt.field_class_variant_with_selector_borrow_selector_field_path_const( |
514 | self._ptr | |
515 | ) | |
45c51519 | 516 | |
d47b87ac | 517 | if ptr is None: |
811644b8 PP |
518 | return |
519 | ||
3fb99a22 | 520 | return bt2_field_path._FieldPath._create_from_ptr_and_get_ref(ptr) |
81447b5b | 521 | |
45c51519 PP |
522 | def append_option(self, name, field_class, ranges): |
523 | utils._check_str(name) | |
524 | utils._check_type(field_class, _FieldClass) | |
525 | utils._check_type(ranges, self._range_set_type) | |
526 | ||
527 | if name in self: | |
ce4923b0 | 528 | raise ValueError("duplicate option name '{}'".format(name)) |
45c51519 PP |
529 | |
530 | if len(ranges) == 0: | |
531 | raise ValueError('range set is empty') | |
532 | ||
533 | # TODO: check overlaps (precondition of self._append_option()) | |
534 | ||
535 | status = self._append_option(self._ptr, name, field_class._ptr, ranges._ptr) | |
cfbd7cf3 FD |
536 | utils._handle_func_status( |
537 | status, 'cannot append option to variant field class object' | |
538 | ) | |
45c51519 PP |
539 | |
540 | def __iadd__(self, options): | |
541 | for name, field_class, ranges in options: | |
542 | self.append_option(name, field_class, ranges) | |
543 | ||
544 | return self | |
545 | ||
546 | ||
547 | class _VariantFieldClassWithUnsignedSelector(_VariantFieldClassWithSelector): | |
548 | _NAME = 'Variant (with unsigned selector)' | |
cfbd7cf3 | 549 | _borrow_option_by_name_ptr = staticmethod( |
9c08c816 | 550 | native_bt.field_class_variant_with_selector_unsigned_borrow_option_by_name_const |
cfbd7cf3 FD |
551 | ) |
552 | _borrow_member_by_index_ptr = staticmethod( | |
9c08c816 | 553 | native_bt.field_class_variant_with_selector_unsigned_borrow_option_by_index_const |
cfbd7cf3 FD |
554 | ) |
555 | _as_option_ptr = staticmethod( | |
9c08c816 | 556 | native_bt.field_class_variant_with_selector_unsigned_option_as_option_const |
cfbd7cf3 FD |
557 | ) |
558 | _append_option = staticmethod( | |
9c08c816 | 559 | native_bt.field_class_variant_with_selector_unsigned_append_option |
cfbd7cf3 FD |
560 | ) |
561 | _option_borrow_ranges_ptr = staticmethod( | |
9c08c816 | 562 | native_bt.field_class_variant_with_selector_unsigned_option_borrow_ranges_const |
cfbd7cf3 | 563 | ) |
3fb99a22 | 564 | _range_set_type = bt2_integer_range_set.UnsignedIntegerRangeSet |
45c51519 | 565 | |
81447b5b | 566 | |
45c51519 PP |
567 | class _VariantFieldClassWithSignedSelector(_VariantFieldClassWithSelector): |
568 | _NAME = 'Variant (with signed selector)' | |
cfbd7cf3 | 569 | _borrow_option_by_name_ptr = staticmethod( |
9c08c816 | 570 | native_bt.field_class_variant_with_selector_signed_borrow_option_by_name_const |
cfbd7cf3 FD |
571 | ) |
572 | _borrow_member_by_index_ptr = staticmethod( | |
9c08c816 | 573 | native_bt.field_class_variant_with_selector_signed_borrow_option_by_index_const |
cfbd7cf3 FD |
574 | ) |
575 | _as_option_ptr = staticmethod( | |
9c08c816 | 576 | native_bt.field_class_variant_with_selector_signed_option_as_option_const |
cfbd7cf3 FD |
577 | ) |
578 | _append_option = staticmethod( | |
9c08c816 | 579 | native_bt.field_class_variant_with_selector_signed_append_option |
cfbd7cf3 FD |
580 | ) |
581 | _option_borrow_ranges_ptr = staticmethod( | |
9c08c816 | 582 | native_bt.field_class_variant_with_selector_signed_option_borrow_ranges_const |
cfbd7cf3 | 583 | ) |
3fb99a22 | 584 | _range_set_type = bt2_integer_range_set.SignedIntegerRangeSet |
811644b8 | 585 | |
81447b5b | 586 | |
d47b87ac SM |
587 | class _ArrayFieldClass(_FieldClass): |
588 | @property | |
589 | def element_field_class(self): | |
cfbd7cf3 FD |
590 | elem_fc_ptr = native_bt.field_class_array_borrow_element_field_class_const( |
591 | self._ptr | |
592 | ) | |
d47b87ac | 593 | return _create_field_class_from_ptr_and_get_ref(elem_fc_ptr) |
81447b5b | 594 | |
81447b5b | 595 | |
d47b87ac | 596 | class _StaticArrayFieldClass(_ArrayFieldClass): |
81447b5b PP |
597 | @property |
598 | def length(self): | |
9c08c816 | 599 | return native_bt.field_class_array_static_get_length(self._ptr) |
81447b5b PP |
600 | |
601 | ||
d47b87ac SM |
602 | class _DynamicArrayFieldClass(_ArrayFieldClass): |
603 | @property | |
604 | def length_field_path(self): | |
9c08c816 | 605 | ptr = native_bt.field_class_array_dynamic_borrow_length_field_path_const( |
cfbd7cf3 FD |
606 | self._ptr |
607 | ) | |
d47b87ac SM |
608 | if ptr is None: |
609 | return | |
81447b5b | 610 | |
3fb99a22 | 611 | return bt2_field_path._FieldPath._create_from_ptr_and_get_ref(ptr) |
81447b5b | 612 | |
81447b5b | 613 | |
3cdfbaea | 614 | _FIELD_CLASS_TYPE_TO_OBJ = { |
aae30e61 | 615 | native_bt.FIELD_CLASS_TYPE_BOOL: _BoolFieldClass, |
ead8c3d4 | 616 | native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayFieldClass, |
d47b87ac SM |
617 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClass, |
618 | native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClass, | |
619 | native_bt.FIELD_CLASS_TYPE_REAL: _RealFieldClass, | |
620 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationFieldClass, | |
621 | native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationFieldClass, | |
622 | native_bt.FIELD_CLASS_TYPE_STRING: _StringFieldClass, | |
3cdfbaea | 623 | native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldClass, |
d47b87ac SM |
624 | native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldClass, |
625 | native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY: _DynamicArrayFieldClass, | |
cec0261d | 626 | native_bt.FIELD_CLASS_TYPE_OPTION: _OptionFieldClass, |
45c51519 PP |
627 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: _VariantFieldClassWithoutSelector, |
628 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: _VariantFieldClassWithUnsignedSelector, | |
629 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: _VariantFieldClassWithSignedSelector, | |
81447b5b | 630 | } |