| 1 | # The MIT License (MIT) |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 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 |
| 25 | import bt2.field |
| 26 | import abc |
| 27 | import bt2 |
| 28 | |
| 29 | |
| 30 | def _create_from_ptr(ptr): |
| 31 | typeid = native_bt.field_class_get_type_id(ptr) |
| 32 | return _TYPE_ID_TO_OBJ[typeid]._create_from_ptr(ptr) |
| 33 | |
| 34 | |
| 35 | class _FieldClass(object._Object, metaclass=abc.ABCMeta): |
| 36 | def __init__(self, ptr): |
| 37 | super().__init__(ptr) |
| 38 | |
| 39 | def __eq__(self, other): |
| 40 | if not isinstance(other, self.__class__): |
| 41 | # not comparing apples to apples |
| 42 | return False |
| 43 | |
| 44 | if self.addr == other.addr: |
| 45 | return True |
| 46 | |
| 47 | ret = native_bt.field_class_compare(self._ptr, other._ptr) |
| 48 | utils._handle_ret(ret, "cannot compare field classes") |
| 49 | return ret == 0 |
| 50 | |
| 51 | def _check_create_status(self, ptr): |
| 52 | if ptr is None: |
| 53 | raise bt2.CreationError('cannot create {} field class object'.format(self._NAME.lower())) |
| 54 | |
| 55 | def __copy__(self): |
| 56 | ptr = native_bt.field_class_copy(self._ptr) |
| 57 | utils._handle_ptr(ptr, 'cannot copy {} field class object'.format(self._NAME.lower())) |
| 58 | return _create_from_ptr(ptr) |
| 59 | |
| 60 | def __deepcopy__(self, memo): |
| 61 | cpy = self.__copy__() |
| 62 | memo[id(self)] = cpy |
| 63 | return cpy |
| 64 | |
| 65 | def __call__(self, value=None): |
| 66 | field_ptr = native_bt.field_create(self._ptr) |
| 67 | |
| 68 | if field_ptr is None: |
| 69 | raise bt2.CreationError('cannot create {} field object'.format(self._NAME.lower())) |
| 70 | |
| 71 | field = bt2.field._create_from_ptr(field_ptr) |
| 72 | |
| 73 | if value is not None: |
| 74 | if not isinstance(field, (bt2.field._IntegerField, bt2.field._FloatingPointNumberField, bt2.field._StringField)): |
| 75 | raise bt2.Error('cannot assign an initial value to a {} field object'.format(field._NAME)) |
| 76 | |
| 77 | field.value = value |
| 78 | |
| 79 | return field |
| 80 | |
| 81 | |
| 82 | class _AlignmentProp: |
| 83 | @property |
| 84 | def alignment(self): |
| 85 | alignment = native_bt.field_class_get_alignment(self._ptr) |
| 86 | assert(alignment >= 0) |
| 87 | return alignment |
| 88 | |
| 89 | @alignment.setter |
| 90 | def alignment(self, alignment): |
| 91 | utils._check_alignment(alignment) |
| 92 | ret = native_bt.field_class_set_alignment(self._ptr, alignment) |
| 93 | utils._handle_ret(ret, "cannot set field class object's alignment") |
| 94 | |
| 95 | |
| 96 | class _ByteOrderProp: |
| 97 | @property |
| 98 | def byte_order(self): |
| 99 | bo = native_bt.field_class_get_byte_order(self._ptr) |
| 100 | assert(bo >= 0) |
| 101 | return bo |
| 102 | |
| 103 | @byte_order.setter |
| 104 | def byte_order(self, byte_order): |
| 105 | utils._check_int(byte_order) |
| 106 | ret = native_bt.field_class_set_byte_order(self._ptr, byte_order) |
| 107 | utils._handle_ret(ret, "cannot set field class object's byte order") |
| 108 | |
| 109 | |
| 110 | class IntegerFieldClass(_FieldClass, _AlignmentProp, _ByteOrderProp): |
| 111 | _NAME = 'Integer' |
| 112 | |
| 113 | def __init__(self, size, alignment=None, byte_order=None, is_signed=None, |
| 114 | base=None, encoding=None, mapped_clock_class=None): |
| 115 | utils._check_uint64(size) |
| 116 | |
| 117 | if size == 0: |
| 118 | raise ValueError('size is 0 bits') |
| 119 | |
| 120 | ptr = native_bt.field_class_integer_create(size) |
| 121 | self._check_create_status(ptr) |
| 122 | super().__init__(ptr) |
| 123 | |
| 124 | if alignment is not None: |
| 125 | self.alignment = alignment |
| 126 | |
| 127 | if byte_order is not None: |
| 128 | self.byte_order = byte_order |
| 129 | |
| 130 | if is_signed is not None: |
| 131 | self.is_signed = is_signed |
| 132 | |
| 133 | if base is not None: |
| 134 | self.base = base |
| 135 | |
| 136 | if encoding is not None: |
| 137 | self.encoding = encoding |
| 138 | |
| 139 | if mapped_clock_class is not None: |
| 140 | self.mapped_clock_class = mapped_clock_class |
| 141 | |
| 142 | @property |
| 143 | def size(self): |
| 144 | size = native_bt.field_class_integer_get_size(self._ptr) |
| 145 | assert(size >= 1) |
| 146 | return size |
| 147 | |
| 148 | @property |
| 149 | def is_signed(self): |
| 150 | is_signed = native_bt.field_class_integer_is_signed(self._ptr) |
| 151 | assert(is_signed >= 0) |
| 152 | return is_signed > 0 |
| 153 | |
| 154 | @is_signed.setter |
| 155 | def is_signed(self, is_signed): |
| 156 | utils._check_bool(is_signed) |
| 157 | ret = native_bt.field_class_integer_set_is_signed(self._ptr, int(is_signed)) |
| 158 | utils._handle_ret(ret, "cannot set integer field class object's signedness") |
| 159 | |
| 160 | @property |
| 161 | def base(self): |
| 162 | base = native_bt.field_class_integer_get_base(self._ptr) |
| 163 | assert(base >= 0) |
| 164 | return base |
| 165 | |
| 166 | @base.setter |
| 167 | def base(self, base): |
| 168 | utils._check_int(base) |
| 169 | ret = native_bt.field_class_integer_set_base(self._ptr, base) |
| 170 | utils._handle_ret(ret, "cannot set integer field class object's base") |
| 171 | |
| 172 | @property |
| 173 | def encoding(self): |
| 174 | encoding = native_bt.field_class_integer_get_encoding(self._ptr) |
| 175 | assert(encoding >= 0) |
| 176 | return encoding |
| 177 | |
| 178 | @encoding.setter |
| 179 | def encoding(self, encoding): |
| 180 | utils._check_int(encoding) |
| 181 | ret = native_bt.field_class_integer_set_encoding(self._ptr, encoding) |
| 182 | utils._handle_ret(ret, "cannot set integer field class object's encoding") |
| 183 | |
| 184 | @property |
| 185 | def mapped_clock_class(self): |
| 186 | ptr = native_bt.field_class_integer_get_mapped_clock_class(self._ptr) |
| 187 | |
| 188 | if ptr is None: |
| 189 | return |
| 190 | |
| 191 | return bt2.ClockClass._create_from_ptr(ptr) |
| 192 | |
| 193 | @mapped_clock_class.setter |
| 194 | def mapped_clock_class(self, clock_class): |
| 195 | utils._check_type(clock_class, bt2.ClockClass) |
| 196 | ret = native_bt.field_class_integer_set_mapped_clock_class(self._ptr, clock_class._ptr) |
| 197 | utils._handle_ret(ret, "cannot set integer field class object's mapped clock class") |
| 198 | |
| 199 | |
| 200 | class FloatingPointNumberFieldClass(_FieldClass, _AlignmentProp, _ByteOrderProp): |
| 201 | _NAME = 'Floating point number' |
| 202 | |
| 203 | def __init__(self, alignment=None, byte_order=None, exponent_size=None, |
| 204 | mantissa_size=None): |
| 205 | ptr = native_bt.field_class_floating_point_create() |
| 206 | self._check_create_status(ptr) |
| 207 | super().__init__(ptr) |
| 208 | |
| 209 | if alignment is not None: |
| 210 | self.alignment = alignment |
| 211 | |
| 212 | if byte_order is not None: |
| 213 | self.byte_order = byte_order |
| 214 | |
| 215 | if exponent_size is not None: |
| 216 | self.exponent_size = exponent_size |
| 217 | |
| 218 | if mantissa_size is not None: |
| 219 | self.mantissa_size = mantissa_size |
| 220 | |
| 221 | @property |
| 222 | def exponent_size(self): |
| 223 | exp_size = native_bt.field_class_floating_point_get_exponent_digits(self._ptr) |
| 224 | assert(exp_size >= 0) |
| 225 | return exp_size |
| 226 | |
| 227 | @exponent_size.setter |
| 228 | def exponent_size(self, exponent_size): |
| 229 | utils._check_uint64(exponent_size) |
| 230 | ret = native_bt.field_class_floating_point_set_exponent_digits(self._ptr, exponent_size) |
| 231 | utils._handle_ret(ret, "cannot set floating point number field class object's exponent size") |
| 232 | |
| 233 | @property |
| 234 | def mantissa_size(self): |
| 235 | mant_size = native_bt.field_class_floating_point_get_mantissa_digits(self._ptr) |
| 236 | assert(mant_size >= 0) |
| 237 | return mant_size |
| 238 | |
| 239 | @mantissa_size.setter |
| 240 | def mantissa_size(self, mantissa_size): |
| 241 | utils._check_uint64(mantissa_size) |
| 242 | ret = native_bt.field_class_floating_point_set_mantissa_digits(self._ptr, mantissa_size) |
| 243 | utils._handle_ret(ret, "cannot set floating point number field class object's mantissa size") |
| 244 | |
| 245 | |
| 246 | class _EnumerationFieldClassMapping: |
| 247 | def __init__(self, name, lower, upper): |
| 248 | self._name = name |
| 249 | self._lower = lower |
| 250 | self._upper = upper |
| 251 | |
| 252 | @property |
| 253 | def name(self): |
| 254 | return self._name |
| 255 | |
| 256 | @property |
| 257 | def lower(self): |
| 258 | return self._lower |
| 259 | |
| 260 | @property |
| 261 | def upper(self): |
| 262 | return self._upper |
| 263 | |
| 264 | def __eq__(self, other): |
| 265 | if type(other) is not self.__class__: |
| 266 | return False |
| 267 | |
| 268 | return (self.name, self.lower, self.upper) == (other.name, other.lower, other.upper) |
| 269 | |
| 270 | |
| 271 | class _EnumerationFieldClassMappingIterator(object._Object, |
| 272 | collections.abc.Iterator): |
| 273 | def __init__(self, iter_ptr, is_signed): |
| 274 | super().__init__(iter_ptr) |
| 275 | self._is_signed = is_signed |
| 276 | self._done = (iter_ptr is None) |
| 277 | |
| 278 | def __next__(self): |
| 279 | if self._done: |
| 280 | raise StopIteration |
| 281 | |
| 282 | ret = native_bt.field_class_enumeration_mapping_iterator_next(self._ptr) |
| 283 | if ret < 0: |
| 284 | self._done = True |
| 285 | raise StopIteration |
| 286 | |
| 287 | if self._is_signed: |
| 288 | ret, name, lower, upper = native_bt.field_class_enumeration_mapping_iterator_get_signed(self._ptr) |
| 289 | else: |
| 290 | ret, name, lower, upper = native_bt.field_class_enumeration_mapping_iterator_get_unsigned(self._ptr) |
| 291 | |
| 292 | assert(ret == 0) |
| 293 | mapping = _EnumerationFieldClassMapping(name, lower, upper) |
| 294 | |
| 295 | return mapping |
| 296 | |
| 297 | |
| 298 | class EnumerationFieldClass(IntegerFieldClass, collections.abc.Sequence): |
| 299 | _NAME = 'Enumeration' |
| 300 | |
| 301 | def __init__(self, int_field_class=None, size=None, alignment=None, |
| 302 | byte_order=None, is_signed=None, base=None, encoding=None, |
| 303 | mapped_clock_class=None): |
| 304 | if int_field_class is None: |
| 305 | int_field_class = IntegerFieldClass(size=size, alignment=alignment, |
| 306 | byte_order=byte_order, |
| 307 | is_signed=is_signed, base=base, |
| 308 | encoding=encoding, |
| 309 | mapped_clock_class=mapped_clock_class) |
| 310 | |
| 311 | utils._check_type(int_field_class, IntegerFieldClass) |
| 312 | ptr = native_bt.field_class_enumeration_create(int_field_class._ptr) |
| 313 | self._check_create_status(ptr) |
| 314 | _FieldClass.__init__(self, ptr) |
| 315 | |
| 316 | @property |
| 317 | def integer_field_class(self): |
| 318 | ptr = native_bt.field_class_enumeration_get_container_type(self._ptr) |
| 319 | assert(ptr) |
| 320 | return _create_from_ptr(ptr) |
| 321 | |
| 322 | @property |
| 323 | def size(self): |
| 324 | return self.integer_field_class.size |
| 325 | |
| 326 | @property |
| 327 | def alignment(self): |
| 328 | return self.integer_field_class.alignment |
| 329 | |
| 330 | @alignment.setter |
| 331 | def alignment(self, alignment): |
| 332 | self.integer_field_class.alignment = alignment |
| 333 | |
| 334 | @property |
| 335 | def byte_order(self): |
| 336 | return self.integer_field_class.byte_order |
| 337 | |
| 338 | @byte_order.setter |
| 339 | def byte_order(self, byte_order): |
| 340 | self.integer_field_class.byte_order = byte_order |
| 341 | |
| 342 | @property |
| 343 | def is_signed(self): |
| 344 | return self.integer_field_class.is_signed |
| 345 | |
| 346 | @is_signed.setter |
| 347 | def is_signed(self, is_signed): |
| 348 | self.integer_field_class.is_signed = is_signed |
| 349 | |
| 350 | @property |
| 351 | def base(self): |
| 352 | return self.integer_field_class.base |
| 353 | |
| 354 | @base.setter |
| 355 | def base(self, base): |
| 356 | self.integer_field_class.base = base |
| 357 | |
| 358 | @property |
| 359 | def encoding(self): |
| 360 | return self.integer_field_class.encoding |
| 361 | |
| 362 | @encoding.setter |
| 363 | def encoding(self, encoding): |
| 364 | self.integer_field_class.encoding = encoding |
| 365 | |
| 366 | @property |
| 367 | def mapped_clock_class(self): |
| 368 | return self.integer_field_class.mapped_clock_class |
| 369 | |
| 370 | @mapped_clock_class.setter |
| 371 | def mapped_clock_class(self, mapped_clock_class): |
| 372 | self.integer_field_class.mapped_clock_class = mapped_clock_class |
| 373 | |
| 374 | def __len__(self): |
| 375 | count = native_bt.field_class_enumeration_get_mapping_count(self._ptr) |
| 376 | assert(count >= 0) |
| 377 | return count |
| 378 | |
| 379 | def __getitem__(self, index): |
| 380 | utils._check_uint64(index) |
| 381 | |
| 382 | if index >= len(self): |
| 383 | raise IndexError |
| 384 | |
| 385 | if self.is_signed: |
| 386 | get_fn = native_bt.field_class_enumeration_get_mapping_signed |
| 387 | else: |
| 388 | get_fn = native_bt.field_class_enumeration_get_mapping_unsigned |
| 389 | |
| 390 | ret, name, lower, upper = get_fn(self._ptr, index) |
| 391 | assert(ret == 0) |
| 392 | return _EnumerationFieldClassMapping(name, lower, upper) |
| 393 | |
| 394 | def _get_mapping_iter(self, iter_ptr): |
| 395 | return _EnumerationFieldClassMappingIterator(iter_ptr, self.is_signed) |
| 396 | |
| 397 | def mappings_by_name(self, name): |
| 398 | utils._check_str(name) |
| 399 | iter_ptr = native_bt.field_class_enumeration_find_mappings_by_name(self._ptr, name) |
| 400 | print('iter_ptr', iter_ptr) |
| 401 | return self._get_mapping_iter(iter_ptr) |
| 402 | |
| 403 | def mappings_by_value(self, value): |
| 404 | if self.is_signed: |
| 405 | utils._check_int64(value) |
| 406 | iter_ptr = native_bt.field_class_enumeration_find_mappings_by_signed_value(self._ptr, value) |
| 407 | else: |
| 408 | utils._check_uint64(value) |
| 409 | iter_ptr = native_bt.field_class_enumeration_find_mappings_by_unsigned_value(self._ptr, value) |
| 410 | |
| 411 | return self._get_mapping_iter(iter_ptr) |
| 412 | |
| 413 | def add_mapping(self, name, lower, upper=None): |
| 414 | utils._check_str(name) |
| 415 | |
| 416 | if upper is None: |
| 417 | upper = lower |
| 418 | |
| 419 | if self.is_signed: |
| 420 | add_fn = native_bt.field_class_enumeration_add_mapping_signed |
| 421 | utils._check_int64(lower) |
| 422 | utils._check_int64(upper) |
| 423 | else: |
| 424 | add_fn = native_bt.field_class_enumeration_add_mapping_unsigned |
| 425 | utils._check_uint64(lower) |
| 426 | utils._check_uint64(upper) |
| 427 | |
| 428 | ret = add_fn(self._ptr, name, lower, upper) |
| 429 | utils._handle_ret(ret, "cannot add mapping to enumeration field class object") |
| 430 | |
| 431 | def __iadd__(self, mappings): |
| 432 | for mapping in mappings: |
| 433 | self.add_mapping(mapping.name, mapping.lower, mapping.upper) |
| 434 | |
| 435 | return self |
| 436 | |
| 437 | |
| 438 | class StringFieldClass(_FieldClass): |
| 439 | _NAME = 'String' |
| 440 | |
| 441 | def __init__(self, encoding=None): |
| 442 | ptr = native_bt.field_class_string_create() |
| 443 | self._check_create_status(ptr) |
| 444 | super().__init__(ptr) |
| 445 | |
| 446 | if encoding is not None: |
| 447 | self.encoding = encoding |
| 448 | |
| 449 | @property |
| 450 | def encoding(self): |
| 451 | encoding = native_bt.field_class_string_get_encoding(self._ptr) |
| 452 | assert(encoding >= 0) |
| 453 | return encoding |
| 454 | |
| 455 | @encoding.setter |
| 456 | def encoding(self, encoding): |
| 457 | utils._check_int(encoding) |
| 458 | ret = native_bt.field_class_string_set_encoding(self._ptr, encoding) |
| 459 | utils._handle_ret(ret, "cannot set string field class object's encoding") |
| 460 | |
| 461 | |
| 462 | class _FieldContainer(collections.abc.Mapping): |
| 463 | def __len__(self): |
| 464 | count = self._count() |
| 465 | assert(count >= 0) |
| 466 | return count |
| 467 | |
| 468 | def __getitem__(self, key): |
| 469 | if not isinstance(key, str): |
| 470 | raise TypeError("'{}' is not a 'str' object".format(key.__class__.__name__)) |
| 471 | |
| 472 | ptr = self._get_field_by_name(key) |
| 473 | |
| 474 | if ptr is None: |
| 475 | raise KeyError(key) |
| 476 | |
| 477 | return _create_from_ptr(ptr) |
| 478 | |
| 479 | def __iter__(self): |
| 480 | return self._ITER_CLS(self) |
| 481 | |
| 482 | def append_field(self, name, field_class): |
| 483 | utils._check_str(name) |
| 484 | utils._check_type(field_class, _FieldClass) |
| 485 | ret = self._add_field(field_class._ptr, name) |
| 486 | utils._handle_ret(ret, "cannot add field to {} field class object".format(self._NAME.lower())) |
| 487 | |
| 488 | def __iadd__(self, fields): |
| 489 | for name, field_class in fields.items(): |
| 490 | self.append_field(name, field_class) |
| 491 | |
| 492 | return self |
| 493 | |
| 494 | def at_index(self, index): |
| 495 | utils._check_uint64(index) |
| 496 | return self._at(index) |
| 497 | |
| 498 | |
| 499 | class _StructureFieldClassFieldIterator(collections.abc.Iterator): |
| 500 | def __init__(self, struct_field_class): |
| 501 | self._struct_field_class = struct_field_class |
| 502 | self._at = 0 |
| 503 | |
| 504 | def __next__(self): |
| 505 | if self._at == len(self._struct_field_class): |
| 506 | raise StopIteration |
| 507 | |
| 508 | get_fc_by_index = native_bt.field_class_structure_get_field_by_index |
| 509 | ret, name, field_class_ptr = get_fc_by_index(self._struct_field_class._ptr, |
| 510 | self._at) |
| 511 | assert(ret == 0) |
| 512 | native_bt.put(field_class_ptr) |
| 513 | self._at += 1 |
| 514 | return name |
| 515 | |
| 516 | |
| 517 | class StructureFieldClass(_FieldClass, _FieldContainer, _AlignmentProp): |
| 518 | _NAME = 'Structure' |
| 519 | _ITER_CLS = _StructureFieldClassFieldIterator |
| 520 | |
| 521 | def __init__(self, min_alignment=None): |
| 522 | ptr = native_bt.field_class_structure_create() |
| 523 | self._check_create_status(ptr) |
| 524 | super().__init__(ptr) |
| 525 | |
| 526 | if min_alignment is not None: |
| 527 | self.min_alignment = min_alignment |
| 528 | |
| 529 | def _count(self): |
| 530 | return native_bt.field_class_structure_get_field_count(self._ptr) |
| 531 | |
| 532 | def _get_field_by_name(self, key): |
| 533 | return native_bt.field_class_structure_get_field_class_by_name(self._ptr, key) |
| 534 | |
| 535 | def _add_field(self, ptr, name): |
| 536 | return native_bt.field_class_structure_add_field(self._ptr, ptr, |
| 537 | name) |
| 538 | |
| 539 | def _at(self, index): |
| 540 | if index < 0 or index >= len(self): |
| 541 | raise IndexError |
| 542 | |
| 543 | ret, name, field_class_ptr = native_bt.field_class_structure_get_field_by_index(self._ptr, index) |
| 544 | assert(ret == 0) |
| 545 | return _create_from_ptr(field_class_ptr) |
| 546 | |
| 547 | |
| 548 | StructureFieldClass.min_alignment = property(fset=StructureFieldClass.alignment.fset) |
| 549 | StructureFieldClass.alignment = property(fget=StructureFieldClass.alignment.fget) |
| 550 | |
| 551 | |
| 552 | class _VariantFieldClassFieldIterator(collections.abc.Iterator): |
| 553 | def __init__(self, variant_field_class): |
| 554 | self._variant_field_class = variant_field_class |
| 555 | self._at = 0 |
| 556 | |
| 557 | def __next__(self): |
| 558 | if self._at == len(self._variant_field_class): |
| 559 | raise StopIteration |
| 560 | |
| 561 | ret, name, field_class_ptr = native_bt.field_class_variant_get_field_by_index(self._variant_field_class._ptr, |
| 562 | self._at) |
| 563 | assert(ret == 0) |
| 564 | native_bt.put(field_class_ptr) |
| 565 | self._at += 1 |
| 566 | return name |
| 567 | |
| 568 | |
| 569 | class VariantFieldClass(_FieldClass, _FieldContainer, _AlignmentProp): |
| 570 | _NAME = 'Variant' |
| 571 | _ITER_CLS = _VariantFieldClassFieldIterator |
| 572 | |
| 573 | def __init__(self, tag_name, tag_field_class=None): |
| 574 | utils._check_str(tag_name) |
| 575 | |
| 576 | if tag_field_class is None: |
| 577 | tag_fc_ptr = None |
| 578 | else: |
| 579 | utils._check_type(tag_field_class, EnumerationFieldClass) |
| 580 | tag_fc_ptr = tag_field_class._ptr |
| 581 | |
| 582 | ptr = native_bt.field_class_variant_create(tag_fc_ptr, |
| 583 | tag_name) |
| 584 | self._check_create_status(ptr) |
| 585 | super().__init__(ptr) |
| 586 | |
| 587 | @property |
| 588 | def tag_name(self): |
| 589 | tag_name = native_bt.field_class_variant_get_tag_name(self._ptr) |
| 590 | assert(tag_name is not None) |
| 591 | return tag_name |
| 592 | |
| 593 | @tag_name.setter |
| 594 | def tag_name(self, tag_name): |
| 595 | utils._check_str(tag_name) |
| 596 | ret = native_bt.field_class_variant_set_tag_name(self._ptr, tag_name) |
| 597 | utils._handle_ret(ret, "cannot set variant field class object's tag name") |
| 598 | |
| 599 | @property |
| 600 | def tag_field_class(self): |
| 601 | fc_ptr = native_bt.field_class_variant_get_tag_type(self._ptr) |
| 602 | |
| 603 | if fc_ptr is None: |
| 604 | return |
| 605 | |
| 606 | return _create_from_ptr(fc_ptr) |
| 607 | |
| 608 | def _count(self): |
| 609 | return native_bt.field_class_variant_get_field_count(self._ptr) |
| 610 | |
| 611 | def _get_field_by_name(self, key): |
| 612 | return native_bt.field_class_variant_get_field_class_by_name(self._ptr, key) |
| 613 | |
| 614 | def _add_field(self, ptr, name): |
| 615 | return native_bt.field_class_variant_add_field(self._ptr, ptr, name) |
| 616 | |
| 617 | def _at(self, index): |
| 618 | if index < 0 or index >= len(self): |
| 619 | raise IndexError |
| 620 | |
| 621 | ret, name, field_class_ptr = native_bt.field_class_variant_get_field_by_index(self._ptr, index) |
| 622 | assert(ret == 0) |
| 623 | return _create_from_ptr(field_class_ptr) |
| 624 | |
| 625 | |
| 626 | class ArrayFieldClass(_FieldClass): |
| 627 | _NAME = 'Array' |
| 628 | |
| 629 | def __init__(self, element_field_class, length): |
| 630 | utils._check_type(element_field_class, _FieldClass) |
| 631 | utils._check_uint64(length) |
| 632 | ptr = native_bt.field_class_array_create(element_field_class._ptr, length) |
| 633 | self._check_create_status(ptr) |
| 634 | super().__init__(ptr) |
| 635 | |
| 636 | @property |
| 637 | def length(self): |
| 638 | length = native_bt.field_class_array_get_length(self._ptr) |
| 639 | assert(length >= 0) |
| 640 | return length |
| 641 | |
| 642 | @property |
| 643 | def element_field_class(self): |
| 644 | ptr = native_bt.field_class_array_get_element_type(self._ptr) |
| 645 | assert(ptr) |
| 646 | return _create_from_ptr(ptr) |
| 647 | |
| 648 | |
| 649 | class SequenceFieldClass(_FieldClass): |
| 650 | _NAME = 'Sequence' |
| 651 | |
| 652 | def __init__(self, element_field_class, length_name): |
| 653 | utils._check_type(element_field_class, _FieldClass) |
| 654 | utils._check_str(length_name) |
| 655 | ptr = native_bt.field_class_sequence_create(element_field_class._ptr, |
| 656 | length_name) |
| 657 | self._check_create_status(ptr) |
| 658 | super().__init__(ptr) |
| 659 | |
| 660 | @property |
| 661 | def length_name(self): |
| 662 | length_name = native_bt.field_class_sequence_get_length_field_name(self._ptr) |
| 663 | assert(length_name is not None) |
| 664 | return length_name |
| 665 | |
| 666 | @property |
| 667 | def element_field_class(self): |
| 668 | ptr = native_bt.field_class_sequence_get_element_type(self._ptr) |
| 669 | assert(ptr) |
| 670 | return _create_from_ptr(ptr) |
| 671 | |
| 672 | |
| 673 | _TYPE_ID_TO_OBJ = { |
| 674 | } |