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