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