bt2: Mass clock_value -> clock_snapshot rename
[babeltrace.git] / bindings / python / bt2 / bt2 / field_class.py
CommitLineData
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
23from bt2 import native_bt, object, utils
24import collections.abc
25import bt2.fields
26import abc
27import bt2
28
29
30def _create_from_ptr(ptr):
b4f45851 31 typeid = native_bt.field_class_get_type_id(ptr)
81447b5b
PP
32 return _TYPE_ID_TO_OBJ[typeid]._create_from_ptr(ptr)
33
34
b4f45851 35class _FieldClass(object._Object, metaclass=abc.ABCMeta):
81447b5b
PP
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
b4f45851
SM
47 ret = native_bt.field_class_compare(self._ptr, other._ptr)
48 utils._handle_ret(ret, "cannot compare field classes")
81447b5b
PP
49 return ret == 0
50
51 def _check_create_status(self, ptr):
52 if ptr is None:
b4f45851 53 raise bt2.CreationError('cannot create {} field class object'.format(self._NAME.lower()))
81447b5b
PP
54
55 def __copy__(self):
b4f45851
SM
56 ptr = native_bt.field_class_copy(self._ptr)
57 utils._handle_ptr(ptr, 'cannot copy {} field class object'.format(self._NAME.lower()))
81447b5b
PP
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):
50842bdc 66 field_ptr = native_bt.field_create(self._ptr)
81447b5b
PP
67
68 if field_ptr is None:
69 raise bt2.CreationError('cannot create {} field object'.format(self._NAME.lower()))
70
71 field = bt2.fields._create_from_ptr(field_ptr)
72
73 if value is not None:
74 if not isinstance(field, (bt2.fields._IntegerField, bt2.fields._FloatingPointNumberField, bt2.fields._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
81447b5b
PP
82class _AlignmentProp:
83 @property
84 def alignment(self):
b4f45851 85 alignment = native_bt.field_class_get_alignment(self._ptr)
811644b8 86 assert(alignment >= 0)
81447b5b
PP
87 return alignment
88
89 @alignment.setter
90 def alignment(self, alignment):
91 utils._check_alignment(alignment)
b4f45851
SM
92 ret = native_bt.field_class_set_alignment(self._ptr, alignment)
93 utils._handle_ret(ret, "cannot set field class object's alignment")
81447b5b
PP
94
95
96class _ByteOrderProp:
97 @property
98 def byte_order(self):
b4f45851 99 bo = native_bt.field_class_get_byte_order(self._ptr)
811644b8 100 assert(bo >= 0)
81447b5b
PP
101 return bo
102
103 @byte_order.setter
104 def byte_order(self, byte_order):
105 utils._check_int(byte_order)
b4f45851
SM
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")
81447b5b
PP
108
109
b4f45851 110class IntegerFieldClass(_FieldClass, _AlignmentProp, _ByteOrderProp):
81447b5b
PP
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
b4f45851 120 ptr = native_bt.field_class_integer_create(size)
81447b5b
PP
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):
b4f45851 144 size = native_bt.field_class_integer_get_size(self._ptr)
811644b8 145 assert(size >= 1)
81447b5b
PP
146 return size
147
148 @property
149 def is_signed(self):
b4f45851 150 is_signed = native_bt.field_class_integer_is_signed(self._ptr)
811644b8 151 assert(is_signed >= 0)
81447b5b
PP
152 return is_signed > 0
153
154 @is_signed.setter
155 def is_signed(self, is_signed):
156 utils._check_bool(is_signed)
b4f45851
SM
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")
81447b5b
PP
159
160 @property
161 def base(self):
b4f45851 162 base = native_bt.field_class_integer_get_base(self._ptr)
811644b8 163 assert(base >= 0)
81447b5b
PP
164 return base
165
166 @base.setter
167 def base(self, base):
168 utils._check_int(base)
b4f45851
SM
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")
81447b5b
PP
171
172 @property
173 def encoding(self):
b4f45851 174 encoding = native_bt.field_class_integer_get_encoding(self._ptr)
811644b8 175 assert(encoding >= 0)
81447b5b
PP
176 return encoding
177
178 @encoding.setter
179 def encoding(self, encoding):
180 utils._check_int(encoding)
b4f45851
SM
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")
81447b5b
PP
183
184 @property
185 def mapped_clock_class(self):
b4f45851 186 ptr = native_bt.field_class_integer_get_mapped_clock_class(self._ptr)
811644b8
PP
187
188 if ptr is None:
189 return
190
81447b5b
PP
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)
b4f45851
SM
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")
81447b5b
PP
198
199
b4f45851 200class FloatingPointNumberFieldClass(_FieldClass, _AlignmentProp, _ByteOrderProp):
81447b5b
PP
201 _NAME = 'Floating point number'
202
203 def __init__(self, alignment=None, byte_order=None, exponent_size=None,
204 mantissa_size=None):
b4f45851 205 ptr = native_bt.field_class_floating_point_create()
81447b5b
PP
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):
b4f45851 223 exp_size = native_bt.field_class_floating_point_get_exponent_digits(self._ptr)
811644b8 224 assert(exp_size >= 0)
81447b5b
PP
225 return exp_size
226
227 @exponent_size.setter
228 def exponent_size(self, exponent_size):
229 utils._check_uint64(exponent_size)
b4f45851
SM
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")
81447b5b
PP
232
233 @property
234 def mantissa_size(self):
b4f45851 235 mant_size = native_bt.field_class_floating_point_get_mantissa_digits(self._ptr)
811644b8
PP
236 assert(mant_size >= 0)
237 return mant_size
81447b5b
PP
238
239 @mantissa_size.setter
240 def mantissa_size(self, mantissa_size):
241 utils._check_uint64(mantissa_size)
b4f45851
SM
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")
81447b5b
PP
244
245
b4f45851 246class _EnumerationFieldClassMapping:
81447b5b
PP
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
b4f45851 271class _EnumerationFieldClassMappingIterator(object._Object,
81447b5b
PP
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
b4f45851 282 ret = native_bt.field_class_enumeration_mapping_iterator_next(self._ptr)
74fb0452
JG
283 if ret < 0:
284 self._done = True
285 raise StopIteration
286
81447b5b 287 if self._is_signed:
b4f45851 288 ret, name, lower, upper = native_bt.field_class_enumeration_mapping_iterator_get_signed(self._ptr)
81447b5b 289 else:
b4f45851 290 ret, name, lower, upper = native_bt.field_class_enumeration_mapping_iterator_get_unsigned(self._ptr)
81447b5b 291
811644b8 292 assert(ret == 0)
b4f45851 293 mapping = _EnumerationFieldClassMapping(name, lower, upper)
81447b5b
PP
294
295 return mapping
296
297
b4f45851 298class EnumerationFieldClass(IntegerFieldClass, collections.abc.Sequence):
81447b5b
PP
299 _NAME = 'Enumeration'
300
b4f45851 301 def __init__(self, int_field_class=None, size=None, alignment=None,
81447b5b
PP
302 byte_order=None, is_signed=None, base=None, encoding=None,
303 mapped_clock_class=None):
b4f45851
SM
304 if int_field_class is None:
305 int_field_class = IntegerFieldClass(size=size, alignment=alignment,
81447b5b
PP
306 byte_order=byte_order,
307 is_signed=is_signed, base=base,
308 encoding=encoding,
309 mapped_clock_class=mapped_clock_class)
310
b4f45851
SM
311 utils._check_type(int_field_class, IntegerFieldClass)
312 ptr = native_bt.field_class_enumeration_create(int_field_class._ptr)
81447b5b 313 self._check_create_status(ptr)
b4f45851 314 _FieldClass.__init__(self, ptr)
81447b5b
PP
315
316 @property
b4f45851
SM
317 def integer_field_class(self):
318 ptr = native_bt.field_class_enumeration_get_container_type(self._ptr)
811644b8 319 assert(ptr)
81447b5b
PP
320 return _create_from_ptr(ptr)
321
322 @property
323 def size(self):
b4f45851 324 return self.integer_field_class.size
81447b5b
PP
325
326 @property
327 def alignment(self):
b4f45851 328 return self.integer_field_class.alignment
81447b5b
PP
329
330 @alignment.setter
331 def alignment(self, alignment):
b4f45851 332 self.integer_field_class.alignment = alignment
81447b5b
PP
333
334 @property
335 def byte_order(self):
b4f45851 336 return self.integer_field_class.byte_order
81447b5b
PP
337
338 @byte_order.setter
339 def byte_order(self, byte_order):
b4f45851 340 self.integer_field_class.byte_order = byte_order
81447b5b
PP
341
342 @property
343 def is_signed(self):
b4f45851 344 return self.integer_field_class.is_signed
81447b5b
PP
345
346 @is_signed.setter
347 def is_signed(self, is_signed):
b4f45851 348 self.integer_field_class.is_signed = is_signed
81447b5b
PP
349
350 @property
351 def base(self):
b4f45851 352 return self.integer_field_class.base
81447b5b
PP
353
354 @base.setter
355 def base(self, base):
b4f45851 356 self.integer_field_class.base = base
81447b5b
PP
357
358 @property
359 def encoding(self):
b4f45851 360 return self.integer_field_class.encoding
81447b5b
PP
361
362 @encoding.setter
363 def encoding(self, encoding):
b4f45851 364 self.integer_field_class.encoding = encoding
81447b5b
PP
365
366 @property
367 def mapped_clock_class(self):
b4f45851 368 return self.integer_field_class.mapped_clock_class
81447b5b
PP
369
370 @mapped_clock_class.setter
371 def mapped_clock_class(self, mapped_clock_class):
b4f45851 372 self.integer_field_class.mapped_clock_class = mapped_clock_class
81447b5b
PP
373
374 def __len__(self):
b4f45851 375 count = native_bt.field_class_enumeration_get_mapping_count(self._ptr)
811644b8 376 assert(count >= 0)
81447b5b
PP
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:
b4f45851 386 get_fn = native_bt.field_class_enumeration_get_mapping_signed
81447b5b 387 else:
b4f45851 388 get_fn = native_bt.field_class_enumeration_get_mapping_unsigned
81447b5b
PP
389
390 ret, name, lower, upper = get_fn(self._ptr, index)
811644b8 391 assert(ret == 0)
b4f45851 392 return _EnumerationFieldClassMapping(name, lower, upper)
81447b5b
PP
393
394 def _get_mapping_iter(self, iter_ptr):
b4f45851 395 return _EnumerationFieldClassMappingIterator(iter_ptr, self.is_signed)
81447b5b
PP
396
397 def mappings_by_name(self, name):
398 utils._check_str(name)
b4f45851 399 iter_ptr = native_bt.field_class_enumeration_find_mappings_by_name(self._ptr, name)
74fb0452 400 print('iter_ptr', iter_ptr)
81447b5b
PP
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)
b4f45851 406 iter_ptr = native_bt.field_class_enumeration_find_mappings_by_signed_value(self._ptr, value)
81447b5b
PP
407 else:
408 utils._check_uint64(value)
b4f45851 409 iter_ptr = native_bt.field_class_enumeration_find_mappings_by_unsigned_value(self._ptr, value)
81447b5b
PP
410
411 return self._get_mapping_iter(iter_ptr)
412
e4d04867 413 def add_mapping(self, name, lower, upper=None):
81447b5b
PP
414 utils._check_str(name)
415
416 if upper is None:
417 upper = lower
418
419 if self.is_signed:
b4f45851 420 add_fn = native_bt.field_class_enumeration_add_mapping_signed
81447b5b
PP
421 utils._check_int64(lower)
422 utils._check_int64(upper)
423 else:
b4f45851 424 add_fn = native_bt.field_class_enumeration_add_mapping_unsigned
81447b5b
PP
425 utils._check_uint64(lower)
426 utils._check_uint64(upper)
427
428 ret = add_fn(self._ptr, name, lower, upper)
b4f45851 429 utils._handle_ret(ret, "cannot add mapping to enumeration field class object")
81447b5b
PP
430
431 def __iadd__(self, mappings):
432 for mapping in mappings:
e4d04867 433 self.add_mapping(mapping.name, mapping.lower, mapping.upper)
81447b5b
PP
434
435 return self
436
437
b4f45851 438class StringFieldClass(_FieldClass):
81447b5b
PP
439 _NAME = 'String'
440
441 def __init__(self, encoding=None):
b4f45851 442 ptr = native_bt.field_class_string_create()
81447b5b
PP
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):
b4f45851 451 encoding = native_bt.field_class_string_get_encoding(self._ptr)
811644b8 452 assert(encoding >= 0)
81447b5b
PP
453 return encoding
454
455 @encoding.setter
456 def encoding(self, encoding):
457 utils._check_int(encoding)
b4f45851
SM
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")
81447b5b
PP
460
461
462class _FieldContainer(collections.abc.Mapping):
463 def __len__(self):
464 count = self._count()
811644b8 465 assert(count >= 0)
81447b5b
PP
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
b4f45851 482 def append_field(self, name, field_class):
81447b5b 483 utils._check_str(name)
b4f45851
SM
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()))
81447b5b
PP
487
488 def __iadd__(self, fields):
b4f45851
SM
489 for name, field_class in fields.items():
490 self.append_field(name, field_class)
81447b5b
PP
491
492 return self
493
494 def at_index(self, index):
495 utils._check_uint64(index)
496 return self._at(index)
497
498
b4f45851
SM
499class _StructureFieldClassFieldIterator(collections.abc.Iterator):
500 def __init__(self, struct_field_class):
501 self._struct_field_class = struct_field_class
81447b5b
PP
502 self._at = 0
503
504 def __next__(self):
b4f45851 505 if self._at == len(self._struct_field_class):
81447b5b
PP
506 raise StopIteration
507
b4f45851
SM
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,
811644b8
PP
510 self._at)
511 assert(ret == 0)
b4f45851 512 native_bt.put(field_class_ptr)
81447b5b
PP
513 self._at += 1
514 return name
515
516
b4f45851 517class StructureFieldClass(_FieldClass, _FieldContainer, _AlignmentProp):
81447b5b 518 _NAME = 'Structure'
b4f45851 519 _ITER_CLS = _StructureFieldClassFieldIterator
81447b5b
PP
520
521 def __init__(self, min_alignment=None):
b4f45851 522 ptr = native_bt.field_class_structure_create()
81447b5b
PP
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):
b4f45851 530 return native_bt.field_class_structure_get_field_count(self._ptr)
81447b5b
PP
531
532 def _get_field_by_name(self, key):
b4f45851 533 return native_bt.field_class_structure_get_field_class_by_name(self._ptr, key)
81447b5b
PP
534
535 def _add_field(self, ptr, name):
b4f45851 536 return native_bt.field_class_structure_add_field(self._ptr, ptr,
0b03f63e 537 name)
81447b5b
PP
538
539 def _at(self, index):
811644b8
PP
540 if index < 0 or index >= len(self):
541 raise IndexError
542
b4f45851 543 ret, name, field_class_ptr = native_bt.field_class_structure_get_field_by_index(self._ptr, index)
811644b8 544 assert(ret == 0)
b4f45851 545 return _create_from_ptr(field_class_ptr)
81447b5b
PP
546
547
b4f45851
SM
548StructureFieldClass.min_alignment = property(fset=StructureFieldClass.alignment.fset)
549StructureFieldClass.alignment = property(fget=StructureFieldClass.alignment.fget)
81447b5b
PP
550
551
b4f45851
SM
552class _VariantFieldClassFieldIterator(collections.abc.Iterator):
553 def __init__(self, variant_field_class):
554 self._variant_field_class = variant_field_class
81447b5b
PP
555 self._at = 0
556
557 def __next__(self):
b4f45851 558 if self._at == len(self._variant_field_class):
81447b5b
PP
559 raise StopIteration
560
b4f45851 561 ret, name, field_class_ptr = native_bt.field_class_variant_get_field_by_index(self._variant_field_class._ptr,
0b03f63e 562 self._at)
811644b8 563 assert(ret == 0)
b4f45851 564 native_bt.put(field_class_ptr)
81447b5b
PP
565 self._at += 1
566 return name
567
568
b4f45851 569class VariantFieldClass(_FieldClass, _FieldContainer, _AlignmentProp):
81447b5b 570 _NAME = 'Variant'
b4f45851 571 _ITER_CLS = _VariantFieldClassFieldIterator
81447b5b 572
b4f45851 573 def __init__(self, tag_name, tag_field_class=None):
81447b5b 574 utils._check_str(tag_name)
811644b8 575
b4f45851
SM
576 if tag_field_class is None:
577 tag_fc_ptr = None
811644b8 578 else:
b4f45851
SM
579 utils._check_type(tag_field_class, EnumerationFieldClass)
580 tag_fc_ptr = tag_field_class._ptr
811644b8 581
b4f45851 582 ptr = native_bt.field_class_variant_create(tag_fc_ptr,
0b03f63e 583 tag_name)
81447b5b
PP
584 self._check_create_status(ptr)
585 super().__init__(ptr)
586
587 @property
588 def tag_name(self):
b4f45851 589 tag_name = native_bt.field_class_variant_get_tag_name(self._ptr)
811644b8 590 assert(tag_name is not None)
81447b5b
PP
591 return tag_name
592
593 @tag_name.setter
594 def tag_name(self, tag_name):
595 utils._check_str(tag_name)
b4f45851
SM
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")
81447b5b 598
811644b8 599 @property
b4f45851
SM
600 def tag_field_class(self):
601 fc_ptr = native_bt.field_class_variant_get_tag_type(self._ptr)
811644b8 602
b4f45851 603 if fc_ptr is None:
811644b8
PP
604 return
605
b4f45851 606 return _create_from_ptr(fc_ptr)
811644b8 607
81447b5b 608 def _count(self):
b4f45851 609 return native_bt.field_class_variant_get_field_count(self._ptr)
81447b5b
PP
610
611 def _get_field_by_name(self, key):
b4f45851 612 return native_bt.field_class_variant_get_field_class_by_name(self._ptr, key)
81447b5b
PP
613
614 def _add_field(self, ptr, name):
b4f45851 615 return native_bt.field_class_variant_add_field(self._ptr, ptr, name)
81447b5b
PP
616
617 def _at(self, index):
811644b8
PP
618 if index < 0 or index >= len(self):
619 raise IndexError
620
b4f45851 621 ret, name, field_class_ptr = native_bt.field_class_variant_get_field_by_index(self._ptr, index)
811644b8 622 assert(ret == 0)
b4f45851 623 return _create_from_ptr(field_class_ptr)
81447b5b
PP
624
625
b4f45851 626class ArrayFieldClass(_FieldClass):
81447b5b
PP
627 _NAME = 'Array'
628
b4f45851
SM
629 def __init__(self, element_field_class, length):
630 utils._check_type(element_field_class, _FieldClass)
81447b5b 631 utils._check_uint64(length)
b4f45851 632 ptr = native_bt.field_class_array_create(element_field_class._ptr, length)
81447b5b
PP
633 self._check_create_status(ptr)
634 super().__init__(ptr)
635
636 @property
637 def length(self):
b4f45851 638 length = native_bt.field_class_array_get_length(self._ptr)
811644b8 639 assert(length >= 0)
81447b5b
PP
640 return length
641
642 @property
b4f45851
SM
643 def element_field_class(self):
644 ptr = native_bt.field_class_array_get_element_type(self._ptr)
811644b8 645 assert(ptr)
81447b5b
PP
646 return _create_from_ptr(ptr)
647
648
b4f45851 649class SequenceFieldClass(_FieldClass):
81447b5b
PP
650 _NAME = 'Sequence'
651
b4f45851
SM
652 def __init__(self, element_field_class, length_name):
653 utils._check_type(element_field_class, _FieldClass)
81447b5b 654 utils._check_str(length_name)
b4f45851 655 ptr = native_bt.field_class_sequence_create(element_field_class._ptr,
0b03f63e 656 length_name)
81447b5b
PP
657 self._check_create_status(ptr)
658 super().__init__(ptr)
659
660 @property
661 def length_name(self):
b4f45851 662 length_name = native_bt.field_class_sequence_get_length_field_name(self._ptr)
811644b8 663 assert(length_name is not None)
81447b5b
PP
664 return length_name
665
666 @property
b4f45851
SM
667 def element_field_class(self):
668 ptr = native_bt.field_class_sequence_get_element_type(self._ptr)
811644b8 669 assert(ptr)
81447b5b
PP
670 return _create_from_ptr(ptr)
671
672
673_TYPE_ID_TO_OBJ = {
81447b5b 674}
This page took 0.066129 seconds and 4 git commands to generate.