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 |
81447b5b PP |
27 | import bt2 |
28 | ||
29 | ||
3cdfbaea SM |
30 | def _create_field_class_from_ptr_and_get_ref(ptr): |
31 | typeid = native_bt.field_class_get_type(ptr) | |
32 | return _FIELD_CLASS_TYPE_TO_OBJ[typeid]._create_from_ptr_and_get_ref(ptr) | |
81447b5b PP |
33 | |
34 | ||
d47b87ac SM |
35 | class IntegerDisplayBase: |
36 | BINARY = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY | |
37 | OCTAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL | |
38 | DECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL | |
39 | HEXADECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL | |
81447b5b | 40 | |
81447b5b | 41 | |
d47b87ac SM |
42 | class _FieldClass(object._SharedObject): |
43 | _get_ref = staticmethod(native_bt.field_class_get_ref) | |
44 | _put_ref = staticmethod(native_bt.field_class_put_ref) | |
81447b5b PP |
45 | |
46 | def _check_create_status(self, ptr): | |
47 | if ptr is None: | |
b4f45851 | 48 | raise bt2.CreationError('cannot create {} field class object'.format(self._NAME.lower())) |
81447b5b | 49 | |
81447b5b | 50 | |
af4bbfc7 | 51 | class _IntegerFieldClass(_FieldClass): |
81447b5b | 52 | @property |
d47b87ac SM |
53 | def field_value_range(self): |
54 | size = native_bt.field_class_integer_get_field_value_range(self._ptr) | |
811644b8 | 55 | assert(size >= 1) |
81447b5b PP |
56 | return size |
57 | ||
d47b87ac SM |
58 | def _field_value_range(self, size): |
59 | if size < 1 or size > 64: | |
60 | raise ValueError("Value is outside valid range [1, 64] ({})".format(size)) | |
61 | native_bt.field_class_integer_set_field_value_range(self._ptr, size) | |
81447b5b | 62 | |
d47b87ac | 63 | _field_value_range = property(fset=_field_value_range) |
81447b5b PP |
64 | |
65 | @property | |
d47b87ac SM |
66 | def preferred_display_base(self): |
67 | base = native_bt.field_class_integer_get_preferred_display_base( | |
68 | self._ptr) | |
811644b8 | 69 | assert(base >= 0) |
81447b5b PP |
70 | return base |
71 | ||
d47b87ac SM |
72 | def _preferred_display_base(self, base): |
73 | utils._check_uint64(base) | |
81447b5b | 74 | |
d47b87ac SM |
75 | if base not in (IntegerDisplayBase.BINARY, |
76 | IntegerDisplayBase.OCTAL, | |
77 | IntegerDisplayBase.DECIMAL, | |
78 | IntegerDisplayBase.HEXADECIMAL): | |
79 | raise ValueError("Display base is not a valid IntegerDisplayBase value") | |
81447b5b | 80 | |
d47b87ac SM |
81 | native_bt.field_class_integer_set_preferred_display_base( |
82 | self._ptr, base) | |
81447b5b | 83 | |
d47b87ac | 84 | _preferred_display_base = property(fset=_preferred_display_base) |
81447b5b PP |
85 | |
86 | ||
2ae9f48c | 87 | class _UnsignedIntegerFieldClass(_IntegerFieldClass): |
d47b87ac | 88 | _NAME = 'Unsigned integer' |
2ae9f48c SM |
89 | |
90 | ||
af4bbfc7 | 91 | class _SignedIntegerFieldClass(_IntegerFieldClass): |
d47b87ac | 92 | _NAME = 'Signed integer' |
2ae9f48c | 93 | |
af4bbfc7 | 94 | |
d47b87ac | 95 | class _RealFieldClass(_FieldClass): |
2ae9f48c | 96 | _NAME = 'Real' |
81447b5b | 97 | |
81447b5b | 98 | @property |
d47b87ac SM |
99 | def is_single_precision(self): |
100 | return native_bt.field_class_real_is_single_precision(self._ptr) | |
81447b5b | 101 | |
d47b87ac SM |
102 | def _is_single_precision(self, is_single_precision): |
103 | utils._check_bool(is_single_precision) | |
104 | native_bt.field_class_real_set_is_single_precision( | |
105 | self._ptr, is_single_precision) | |
81447b5b | 106 | |
d47b87ac | 107 | _is_single_precision = property(fset=_is_single_precision) |
81447b5b PP |
108 | |
109 | ||
d47b87ac SM |
110 | class _EnumerationFieldClassMappingRange: |
111 | def __init__(self, lower, upper): | |
81447b5b PP |
112 | self._lower = lower |
113 | self._upper = upper | |
114 | ||
81447b5b PP |
115 | @property |
116 | def lower(self): | |
117 | return self._lower | |
118 | ||
119 | @property | |
120 | def upper(self): | |
121 | return self._upper | |
122 | ||
123 | def __eq__(self, other): | |
d47b87ac | 124 | return self.lower == other.lower and self.upper == other.upper |
81447b5b | 125 | |
81447b5b | 126 | |
d47b87ac SM |
127 | class _EnumerationFieldClassMapping(collections.abc.Set): |
128 | def __init__(self, mapping_ptr): | |
129 | self._mapping_ptr = mapping_ptr | |
81447b5b PP |
130 | |
131 | @property | |
d47b87ac SM |
132 | def label(self): |
133 | mapping_ptr = self._as_enumeration_field_class_mapping_ptr(self._mapping_ptr) | |
134 | label = native_bt.field_class_enumeration_mapping_get_label(mapping_ptr) | |
135 | assert label is not None | |
136 | return label | |
81447b5b | 137 | |
d47b87ac SM |
138 | def __len__(self): |
139 | mapping_ptr = self._as_enumeration_field_class_mapping_ptr(self._mapping_ptr) | |
140 | return native_bt.field_class_enumeration_mapping_get_range_count(mapping_ptr) | |
81447b5b | 141 | |
d47b87ac SM |
142 | def __contains__(self, other_range): |
143 | for curr_range in self: | |
144 | if curr_range == other_range: | |
145 | return True | |
146 | return False | |
81447b5b | 147 | |
d47b87ac SM |
148 | def __iter__(self): |
149 | for idx in range(len(self)): | |
150 | lower, upper = self._get_range_by_index(self._mapping_ptr, idx) | |
151 | yield _EnumerationFieldClassMappingRange(lower, upper) | |
81447b5b | 152 | |
81447b5b | 153 | |
d47b87ac SM |
154 | class _UnsignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping): |
155 | _as_enumeration_field_class_mapping_ptr = staticmethod(native_bt.field_class_unsigned_enumeration_mapping_as_mapping_const) | |
156 | _get_range_by_index = staticmethod(native_bt.field_class_unsigned_enumeration_mapping_get_range_by_index) | |
81447b5b | 157 | |
81447b5b | 158 | |
d47b87ac SM |
159 | class _SignedEnumerationFieldClassMapping(_EnumerationFieldClassMapping): |
160 | _as_enumeration_field_class_mapping_ptr = staticmethod(native_bt.field_class_signed_enumeration_mapping_as_mapping_const) | |
161 | _get_range_by_index = staticmethod(native_bt.field_class_signed_enumeration_mapping_get_range_by_index) | |
81447b5b | 162 | |
81447b5b | 163 | |
d47b87ac | 164 | class _EnumerationFieldClass(_IntegerFieldClass, collections.abc.Mapping): |
81447b5b | 165 | def __len__(self): |
b4f45851 | 166 | count = native_bt.field_class_enumeration_get_mapping_count(self._ptr) |
811644b8 | 167 | assert(count >= 0) |
81447b5b PP |
168 | return count |
169 | ||
d47b87ac SM |
170 | def map_range(self, label, lower, upper=None): |
171 | utils._check_str(label) | |
81447b5b | 172 | |
d47b87ac SM |
173 | if upper is None: |
174 | upper = lower | |
81447b5b | 175 | |
d24d5663 PP |
176 | status = self._map_range(self._ptr, label, lower, upper) |
177 | utils._handle_func_status(status, | |
178 | "cannot add mapping to enumeration field class object") | |
81447b5b | 179 | |
d47b87ac | 180 | def labels_by_value(self, value): |
d24d5663 PP |
181 | status, labels = self._get_mapping_labels_by_value(self._ptr, value) |
182 | utils._handle_func_status(status, "cannot get mapping labels") | |
d47b87ac | 183 | return labels |
81447b5b | 184 | |
d47b87ac SM |
185 | def __iter__(self): |
186 | for idx in range(len(self)): | |
187 | mapping = self._get_mapping_by_index(self._ptr, idx) | |
188 | yield mapping.label | |
81447b5b | 189 | |
d47b87ac SM |
190 | def __getitem__(self, key): |
191 | utils._check_str(key) | |
192 | for idx in range(len(self)): | |
193 | mapping = self._get_mapping_by_index(self._ptr, idx) | |
194 | if mapping.label == key: | |
195 | return mapping | |
81447b5b | 196 | |
d47b87ac | 197 | raise KeyError(key) |
81447b5b | 198 | |
d47b87ac SM |
199 | def __iadd__(self, mappings): |
200 | for mapping in mappings.values(): | |
201 | for range in mapping: | |
202 | self.map_range(mapping.label, range.lower, range.upper) | |
81447b5b | 203 | |
d47b87ac | 204 | return self |
81447b5b | 205 | |
81447b5b | 206 | |
d47b87ac SM |
207 | class _UnsignedEnumerationFieldClass(_EnumerationFieldClass, _UnsignedIntegerFieldClass): |
208 | _NAME = 'Unsigned enumeration' | |
81447b5b | 209 | |
d47b87ac SM |
210 | @staticmethod |
211 | def _get_mapping_by_index(enum_ptr, index): | |
212 | mapping_ptr = native_bt.field_class_unsigned_enumeration_borrow_mapping_by_index_const(enum_ptr, index) | |
213 | assert mapping_ptr is not None | |
214 | return _UnsignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 215 | |
d47b87ac SM |
216 | @staticmethod |
217 | def _map_range(enum_ptr, label, lower, upper): | |
218 | utils._check_uint64(lower) | |
219 | utils._check_uint64(upper) | |
220 | return native_bt.field_class_unsigned_enumeration_map_range(enum_ptr, label, lower, upper) | |
81447b5b | 221 | |
d47b87ac SM |
222 | @staticmethod |
223 | def _get_mapping_labels_by_value(enum_ptr, value): | |
224 | utils._check_uint64(value) | |
225 | return native_bt.field_class_unsigned_enumeration_get_mapping_labels_by_value(enum_ptr, value) | |
81447b5b PP |
226 | |
227 | ||
d47b87ac SM |
228 | class _SignedEnumerationFieldClass(_EnumerationFieldClass, _SignedIntegerFieldClass): |
229 | _NAME = 'Signed enumeration' | |
81447b5b | 230 | |
d47b87ac SM |
231 | @staticmethod |
232 | def _get_mapping_by_index(enum_ptr, index): | |
233 | mapping_ptr = native_bt.field_class_signed_enumeration_borrow_mapping_by_index_const(enum_ptr, index) | |
234 | assert mapping_ptr is not None | |
235 | return _SignedEnumerationFieldClassMapping(mapping_ptr) | |
81447b5b | 236 | |
d47b87ac SM |
237 | @staticmethod |
238 | def _map_range(enum_ptr, label, lower, upper): | |
239 | utils._check_int64(lower) | |
240 | utils._check_int64(upper) | |
241 | return native_bt.field_class_signed_enumeration_map_range(enum_ptr, label, lower, upper) | |
81447b5b | 242 | |
d47b87ac SM |
243 | @staticmethod |
244 | def _get_mapping_labels_by_value(enum_ptr, value): | |
245 | utils._check_int64(value) | |
246 | return native_bt.field_class_signed_enumeration_get_mapping_labels_by_value(enum_ptr, value) | |
81447b5b | 247 | |
d47b87ac SM |
248 | |
249 | class _StringFieldClass(_FieldClass): | |
250 | _NAME = 'String' | |
81447b5b PP |
251 | |
252 | ||
253 | class _FieldContainer(collections.abc.Mapping): | |
254 | def __len__(self): | |
d47b87ac SM |
255 | count = self._get_element_count(self._ptr) |
256 | assert count >= 0 | |
81447b5b PP |
257 | return count |
258 | ||
259 | def __getitem__(self, key): | |
260 | if not isinstance(key, str): | |
d47b87ac | 261 | raise TypeError("key should be a 'str' object, got {}".format(key.__class__.__name__)) |
81447b5b | 262 | |
d47b87ac | 263 | ptr = self._borrow_field_class_ptr_by_name(key) |
81447b5b PP |
264 | |
265 | if ptr is None: | |
266 | raise KeyError(key) | |
267 | ||
d47b87ac SM |
268 | return _create_field_class_from_ptr_and_get_ref(ptr) |
269 | ||
270 | def _borrow_field_class_ptr_by_name(self, key): | |
271 | element_ptr = self._borrow_element_by_name(self._ptr, key) | |
272 | if element_ptr is None: | |
273 | return | |
274 | ||
275 | return self._element_borrow_field_class(element_ptr) | |
81447b5b PP |
276 | |
277 | def __iter__(self): | |
d47b87ac SM |
278 | for idx in range(len(self)): |
279 | element_ptr = self._borrow_element_by_index(self._ptr, idx) | |
280 | assert element_ptr is not None | |
81447b5b | 281 | |
d47b87ac SM |
282 | yield self._element_get_name(element_ptr) |
283 | ||
284 | def _append_element_common(self, name, field_class): | |
81447b5b | 285 | utils._check_str(name) |
b4f45851 | 286 | utils._check_type(field_class, _FieldClass) |
d24d5663 PP |
287 | status = self._append_element(self._ptr, name, field_class._ptr) |
288 | utils._handle_func_status(status, | |
289 | "cannot add field to {} field class object".format(self._NAME.lower())) | |
81447b5b PP |
290 | |
291 | def __iadd__(self, fields): | |
b4f45851 | 292 | for name, field_class in fields.items(): |
d47b87ac | 293 | self._append_element_common(name, field_class) |
81447b5b PP |
294 | |
295 | return self | |
296 | ||
d47b87ac | 297 | def _at_index(self, index): |
81447b5b | 298 | utils._check_uint64(index) |
81447b5b | 299 | |
811644b8 PP |
300 | if index < 0 or index >= len(self): |
301 | raise IndexError | |
302 | ||
d47b87ac SM |
303 | element_ptr = self._borrow_element_by_index(self._ptr, index) |
304 | assert element_ptr is not None | |
81447b5b | 305 | |
d47b87ac | 306 | field_class_ptr = self._element_borrow_field_class(element_ptr) |
81447b5b | 307 | |
d47b87ac | 308 | return _create_field_class_from_ptr_and_get_ref(field_class_ptr) |
81447b5b PP |
309 | |
310 | ||
d47b87ac SM |
311 | class _StructureFieldClass(_FieldClass, _FieldContainer): |
312 | _NAME = 'Structure' | |
313 | _borrow_element_by_index = staticmethod(native_bt.field_class_structure_borrow_member_by_index_const) | |
314 | _borrow_element_by_name = staticmethod(native_bt.field_class_structure_borrow_member_by_name_const) | |
315 | _element_get_name = staticmethod(native_bt.field_class_structure_member_get_name) | |
316 | _element_borrow_field_class = staticmethod(native_bt.field_class_structure_member_borrow_field_class_const) | |
317 | _get_element_count = staticmethod(native_bt.field_class_structure_get_member_count) | |
318 | _append_element = staticmethod(native_bt.field_class_structure_append_member) | |
81447b5b | 319 | |
d47b87ac SM |
320 | def append_member(self, name, field_class): |
321 | return self._append_element_common(name, field_class) | |
81447b5b | 322 | |
d47b87ac SM |
323 | def member_at_index(self, index): |
324 | return self._at_index(index) | |
81447b5b PP |
325 | |
326 | ||
d47b87ac | 327 | class _VariantFieldClass(_FieldClass, _FieldContainer): |
81447b5b | 328 | _NAME = 'Variant' |
d47b87ac SM |
329 | _borrow_element_by_index = staticmethod(native_bt.field_class_variant_borrow_option_by_index_const) |
330 | _borrow_element_by_name = staticmethod(native_bt.field_class_variant_borrow_option_by_name_const) | |
331 | _element_get_name = staticmethod(native_bt.field_class_variant_option_get_name) | |
332 | _element_borrow_field_class = staticmethod(native_bt.field_class_variant_option_borrow_field_class_const) | |
333 | _get_element_count = staticmethod(native_bt.field_class_variant_get_option_count) | |
334 | _append_element = staticmethod(native_bt.field_class_variant_append_option) | |
811644b8 | 335 | |
d47b87ac SM |
336 | def append_option(self, name, field_class): |
337 | return self._append_element_common(name, field_class) | |
811644b8 | 338 | |
d47b87ac SM |
339 | def option_at_index(self, index): |
340 | return self._at_index(index) | |
81447b5b PP |
341 | |
342 | @property | |
d47b87ac SM |
343 | def selector_field_path(self): |
344 | ptr = native_bt.field_class_variant_borrow_selector_field_path_const(self._ptr) | |
345 | if ptr is None: | |
811644b8 PP |
346 | return |
347 | ||
d47b87ac | 348 | return bt2.field_path._FieldPath._create_from_ptr_and_get_ref(ptr) |
81447b5b | 349 | |
d47b87ac SM |
350 | def _set_selector_field_class(self, selector_fc): |
351 | utils._check_type(selector_fc, bt2.field_class._EnumerationFieldClass) | |
d24d5663 PP |
352 | status = native_bt.field_class_variant_set_selector_field_class(self._ptr, selector_fc._ptr) |
353 | utils._handle_func_status(status, | |
354 | "cannot set variant selector field type") | |
81447b5b | 355 | |
d47b87ac | 356 | _selector_field_class = property(fset=_set_selector_field_class) |
811644b8 | 357 | |
81447b5b | 358 | |
d47b87ac SM |
359 | class _ArrayFieldClass(_FieldClass): |
360 | @property | |
361 | def element_field_class(self): | |
362 | elem_fc_ptr = native_bt.field_class_array_borrow_element_field_class_const(self._ptr) | |
363 | return _create_field_class_from_ptr_and_get_ref(elem_fc_ptr) | |
81447b5b | 364 | |
81447b5b | 365 | |
d47b87ac | 366 | class _StaticArrayFieldClass(_ArrayFieldClass): |
81447b5b PP |
367 | @property |
368 | def length(self): | |
d47b87ac | 369 | return native_bt.field_class_static_array_get_length(self._ptr) |
81447b5b PP |
370 | |
371 | ||
d47b87ac SM |
372 | class _DynamicArrayFieldClass(_ArrayFieldClass): |
373 | @property | |
374 | def length_field_path(self): | |
375 | ptr = native_bt.field_class_dynamic_array_borrow_length_field_path_const(self._ptr) | |
376 | if ptr is None: | |
377 | return | |
81447b5b | 378 | |
d47b87ac | 379 | return bt2.field_path._FieldPath._create_from_ptr_and_get_ref(ptr) |
81447b5b | 380 | |
d47b87ac SM |
381 | def _set_length_field_class(self, length_fc): |
382 | utils._check_type(length_fc, _UnsignedIntegerFieldClass) | |
d24d5663 PP |
383 | status = native_bt.field_class_dynamic_array_set_length_field_class(self._ptr, length_fc._ptr) |
384 | utils._handle_func_status(status, | |
385 | "cannot set dynamic array length field type") | |
81447b5b | 386 | |
d47b87ac | 387 | _length_field_class = property(fset=_set_length_field_class) |
81447b5b PP |
388 | |
389 | ||
3cdfbaea | 390 | _FIELD_CLASS_TYPE_TO_OBJ = { |
d47b87ac SM |
391 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClass, |
392 | native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClass, | |
393 | native_bt.FIELD_CLASS_TYPE_REAL: _RealFieldClass, | |
394 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationFieldClass, | |
395 | native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationFieldClass, | |
396 | native_bt.FIELD_CLASS_TYPE_STRING: _StringFieldClass, | |
3cdfbaea | 397 | native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldClass, |
d47b87ac SM |
398 | native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldClass, |
399 | native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY: _DynamicArrayFieldClass, | |
400 | native_bt.FIELD_CLASS_TYPE_VARIANT: _VariantFieldClass, | |
81447b5b | 401 | } |