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