python: make all _get_ref/_put_ref proper static methods
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_class.py
CommitLineData
0235b0db 1# SPDX-License-Identifier: MIT
fbbe9302
SM
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4# Copyright (c) 2018 Francis Deslauriers <francis.deslauriers@efficios.com>
5# Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com>
fbbe9302 6
fbbe9302 7from bt2 import native_bt, utils, object
3fb99a22
PP
8from bt2 import stream_class as bt2_stream_class
9from bt2 import field_class as bt2_field_class
0aa006b7 10from bt2 import integer_range_set as bt2_integer_range_set
3fb99a22 11from bt2 import trace as bt2_trace
5783664e 12from bt2 import value as bt2_value
fbbe9302
SM
13import collections.abc
14import functools
f0a42b33 15import bt2
fbbe9302
SM
16
17
67de22ba
SM
18def _trace_class_destruction_listener_from_native(
19 user_listener, handle, trace_class_ptr
20):
f0a42b33 21 trace_class = _TraceClass._create_from_ptr_and_get_ref(trace_class_ptr)
fbbe9302 22 user_listener(trace_class)
67de22ba 23 handle._invalidate()
fbbe9302
SM
24
25
f0a42b33 26class _TraceClassConst(object._SharedObject, collections.abc.Mapping):
9dee90bd
SM
27 @staticmethod
28 def _get_ref(ptr):
29 native_bt.trace_class_get_ref(ptr)
30
31 @staticmethod
32 def _put_ref(ptr):
33 native_bt.trace_class_put_ref(ptr)
34
f0a42b33
FD
35 _borrow_stream_class_ptr_by_index = staticmethod(
36 native_bt.trace_class_borrow_stream_class_by_index_const
37 )
38 _borrow_stream_class_ptr_by_id = staticmethod(
39 native_bt.trace_class_borrow_stream_class_by_id_const
40 )
2550c437
SM
41 _borrow_user_attributes_ptr = staticmethod(
42 native_bt.trace_class_borrow_user_attributes_const
43 )
f0a42b33
FD
44 _stream_class_pycls = bt2_stream_class._StreamClassConst
45 _create_value_from_ptr_and_get_ref = staticmethod(
46 bt2_value._create_from_const_ptr_and_get_ref
47 )
fbbe9302 48
f0a42b33
FD
49 @property
50 def user_attributes(self):
2550c437 51 ptr = self._borrow_user_attributes_ptr(self._ptr)
f0a42b33
FD
52 assert ptr is not None
53 return self._create_value_from_ptr_and_get_ref(ptr)
8c2367b8 54
fbbe9302
SM
55 # Number of stream classes in this trace class.
56
57 def __len__(self):
58 count = native_bt.trace_class_get_stream_class_count(self._ptr)
59 assert count >= 0
60 return count
61
62 # Get a stream class by stream id.
63
64 def __getitem__(self, key):
65 utils._check_uint64(key)
66
f0a42b33 67 sc_ptr = self._borrow_stream_class_ptr_by_id(self._ptr, key)
fbbe9302
SM
68 if sc_ptr is None:
69 raise KeyError(key)
70
f0a42b33 71 return self._stream_class_pycls._create_from_ptr_and_get_ref(sc_ptr)
fbbe9302
SM
72
73 def __iter__(self):
74 for idx in range(len(self)):
f0a42b33 75 sc_ptr = self._borrow_stream_class_ptr_by_index(self._ptr, idx)
fbbe9302
SM
76 assert sc_ptr is not None
77
78 id = native_bt.stream_class_get_id(sc_ptr)
79 assert id >= 0
80
81 yield id
82
f0a42b33
FD
83 @property
84 def assigns_automatic_stream_class_id(self):
85 return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr)
86
87 # Add a listener to be called when the trace class is destroyed.
88
89 def add_destruction_listener(self, listener):
90
91 if not callable(listener):
92 raise TypeError("'listener' parameter is not callable")
93
67de22ba
SM
94 handle = utils._ListenerHandle(self.addr)
95
f0a42b33 96 listener_from_native = functools.partial(
67de22ba 97 _trace_class_destruction_listener_from_native, listener, handle
f0a42b33
FD
98 )
99
67de22ba 100 fn = native_bt.bt2_trace_class_add_destruction_listener
f0a42b33
FD
101 status, listener_id = fn(self._ptr, listener_from_native)
102 utils._handle_func_status(
f5567ea8 103 status, "cannot add destruction listener to trace class object"
f0a42b33
FD
104 )
105
67de22ba
SM
106 handle._set_listener_id(listener_id)
107
108 return handle
f0a42b33 109
1114a7d5
SM
110 def remove_destruction_listener(self, listener_handle):
111 utils._check_type(listener_handle, utils._ListenerHandle)
112
67de22ba 113 if listener_handle._addr != self.addr:
1114a7d5 114 raise ValueError(
f5567ea8 115 "This trace class destruction listener does not match the trace class object."
1114a7d5
SM
116 )
117
118 if listener_handle._listener_id is None:
119 raise ValueError(
f5567ea8 120 "This trace class destruction listener was already removed."
1114a7d5
SM
121 )
122
123 status = native_bt.trace_class_remove_destruction_listener(
124 self._ptr, listener_handle._listener_id
125 )
126 utils._handle_func_status(status)
67de22ba 127 listener_handle._invalidate()
1114a7d5 128
f0a42b33
FD
129
130class _TraceClass(_TraceClassConst):
131 _borrow_stream_class_ptr_by_index = staticmethod(
132 native_bt.trace_class_borrow_stream_class_by_index
133 )
134 _borrow_stream_class_ptr_by_id = staticmethod(
135 native_bt.trace_class_borrow_stream_class_by_id
136 )
2550c437
SM
137 _borrow_user_attributes_ptr = staticmethod(
138 native_bt.trace_class_borrow_user_attributes
139 )
f0a42b33
FD
140 _stream_class_pycls = bt2_stream_class._StreamClass
141 _create_value_from_ptr_and_get_ref = staticmethod(
142 bt2_value._create_from_ptr_and_get_ref
143 )
144
145 # Instantiate a trace of this class.
146
147 def __call__(self, name=None, user_attributes=None, uuid=None, environment=None):
148 trace_ptr = native_bt.trace_create(self._ptr)
149
150 if trace_ptr is None:
f5567ea8 151 raise bt2._MemoryError("cannot create trace class object")
f0a42b33
FD
152
153 trace = bt2_trace._Trace._create_from_ptr(trace_ptr)
154
155 if name is not None:
156 trace._name = name
157
158 if user_attributes is not None:
159 trace._user_attributes = user_attributes
160
161 if uuid is not None:
162 trace._uuid = uuid
163
164 if environment is not None:
165 for key, value in environment.items():
166 trace.environment[key] = value
167
168 return trace
169
cfbd7cf3
FD
170 def create_stream_class(
171 self,
172 id=None,
173 name=None,
5783664e 174 user_attributes=None,
cfbd7cf3
FD
175 packet_context_field_class=None,
176 event_common_context_field_class=None,
177 default_clock_class=None,
178 assigns_automatic_event_class_id=True,
179 assigns_automatic_stream_id=True,
180 supports_packets=False,
181 packets_have_beginning_default_clock_snapshot=False,
182 packets_have_end_default_clock_snapshot=False,
183 supports_discarded_events=False,
184 discarded_events_have_default_clock_snapshots=False,
185 supports_discarded_packets=False,
186 discarded_packets_have_default_clock_snapshots=False,
187 ):
d3bf1370
SM
188 # Validate parameters before we create the object.
189 bt2_stream_class._StreamClass._validate_create_params(
190 name,
191 user_attributes,
192 packet_context_field_class,
193 event_common_context_field_class,
194 default_clock_class,
195 assigns_automatic_event_class_id,
196 assigns_automatic_stream_id,
197 supports_packets,
198 packets_have_beginning_default_clock_snapshot,
199 packets_have_end_default_clock_snapshot,
200 supports_discarded_events,
201 discarded_events_have_default_clock_snapshots,
202 supports_discarded_packets,
203 discarded_packets_have_default_clock_snapshots,
204 )
fbbe9302
SM
205
206 if self.assigns_automatic_stream_class_id:
207 if id is not None:
cfbd7cf3 208 raise ValueError(
f5567ea8 209 "id provided, but trace class assigns automatic stream class ids"
cfbd7cf3 210 )
fbbe9302
SM
211
212 sc_ptr = native_bt.stream_class_create(self._ptr)
213 else:
214 if id is None:
cfbd7cf3 215 raise ValueError(
f5567ea8 216 "id not provided, but trace class does not assign automatic stream class ids"
cfbd7cf3 217 )
fbbe9302
SM
218
219 utils._check_uint64(id)
220 sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id)
221
3fb99a22 222 sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr)
8c2367b8 223
3cdfbaea
SM
224 if name is not None:
225 sc._name = name
226
5783664e
PP
227 if user_attributes is not None:
228 sc._user_attributes = user_attributes
229
3cdfbaea
SM
230 if event_common_context_field_class is not None:
231 sc._event_common_context_field_class = event_common_context_field_class
232
233 if default_clock_class is not None:
234 sc._default_clock_class = default_clock_class
235
26fc5aed
PP
236 # call after `sc._default_clock_class` because, if
237 # `packets_have_beginning_default_clock_snapshot` or
238 # `packets_have_end_default_clock_snapshot` is true, then this
239 # stream class needs a default clock class already.
cfbd7cf3
FD
240 sc._set_supports_packets(
241 supports_packets,
242 packets_have_beginning_default_clock_snapshot,
243 packets_have_end_default_clock_snapshot,
244 )
26fc5aed
PP
245
246 # call after sc._set_supports_packets() because, if
247 # `packet_context_field_class` is not `None`, then this stream
248 # class needs to support packets already.
249 if packet_context_field_class is not None:
250 sc._packet_context_field_class = packet_context_field_class
251
3cdfbaea 252 sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
8c2367b8 253 sc._assigns_automatic_stream_id = assigns_automatic_stream_id
cfbd7cf3
FD
254 sc._set_supports_discarded_events(
255 supports_discarded_events, discarded_events_have_default_clock_snapshots
256 )
257 sc._set_supports_discarded_packets(
258 supports_discarded_packets, discarded_packets_have_default_clock_snapshots
259 )
8c2367b8 260 return sc
fbbe9302 261
5783664e
PP
262 def _user_attributes(self, user_attributes):
263 value = bt2_value.create_value(user_attributes)
264 utils._check_type(value, bt2_value.MapValue)
265 native_bt.trace_class_set_user_attributes(self._ptr, value._ptr)
266
267 _user_attributes = property(fset=_user_attributes)
268
fbbe9302
SM
269 def _assigns_automatic_stream_class_id(self, auto_id):
270 utils._check_bool(auto_id)
cfbd7cf3
FD
271 return native_bt.trace_class_set_assigns_automatic_stream_class_id(
272 self._ptr, auto_id
273 )
fbbe9302 274
cfbd7cf3
FD
275 _assigns_automatic_stream_class_id = property(
276 fset=_assigns_automatic_stream_class_id
277 )
fbbe9302 278
3cdfbaea
SM
279 # Field class creation methods.
280
aae30e61 281 def _check_field_class_create_status(self, ptr, type_name):
3cdfbaea 282 if ptr is None:
f5567ea8 283 raise bt2._MemoryError("cannot create {} field class".format(type_name))
3cdfbaea 284
5783664e
PP
285 @staticmethod
286 def _set_field_class_user_attrs(fc, user_attributes):
287 if user_attributes is not None:
288 fc._user_attributes = user_attributes
289
290 def create_bool_field_class(self, user_attributes=None):
aae30e61 291 field_class_ptr = native_bt.field_class_bool_create(self._ptr)
f5567ea8 292 self._check_field_class_create_status(field_class_ptr, "boolean")
5783664e
PP
293 fc = bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr)
294 self._set_field_class_user_attrs(fc, user_attributes)
295 return fc
aae30e61 296
5783664e 297 def create_bit_array_field_class(self, length, user_attributes=None):
ead8c3d4
PP
298 utils._check_uint64(length)
299
300 if length < 1 or length > 64:
301 raise ValueError(
f5567ea8 302 "invalid length {}: expecting a value in the [1, 64] range".format(
ead8c3d4
PP
303 length
304 )
305 )
306
307 field_class_ptr = native_bt.field_class_bit_array_create(self._ptr, length)
f5567ea8 308 self._check_field_class_create_status(field_class_ptr, "bit array")
5783664e
PP
309 fc = bt2_field_class._BitArrayFieldClass._create_from_ptr(field_class_ptr)
310 self._set_field_class_user_attrs(fc, user_attributes)
311 return fc
ead8c3d4 312
cfbd7cf3 313 def _create_integer_field_class(
5783664e
PP
314 self,
315 create_func,
316 py_cls,
317 type_name,
318 field_value_range,
319 preferred_display_base,
320 user_attributes,
cfbd7cf3 321 ):
af4bbfc7 322 field_class_ptr = create_func(self._ptr)
aae30e61 323 self._check_field_class_create_status(field_class_ptr, type_name)
af4bbfc7
SM
324
325 field_class = py_cls._create_from_ptr(field_class_ptr)
326
d47b87ac
SM
327 if field_value_range is not None:
328 field_class._field_value_range = field_value_range
af4bbfc7 329
d47b87ac
SM
330 if preferred_display_base is not None:
331 field_class._preferred_display_base = preferred_display_base
af4bbfc7 332
5783664e 333 self._set_field_class_user_attrs(field_class, user_attributes)
af4bbfc7
SM
334 return field_class
335
cfbd7cf3 336 def create_signed_integer_field_class(
5783664e 337 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
338 ):
339 return self._create_integer_field_class(
9c08c816 340 native_bt.field_class_integer_signed_create,
3fb99a22 341 bt2_field_class._SignedIntegerFieldClass,
f5567ea8 342 "signed integer",
cfbd7cf3
FD
343 field_value_range,
344 preferred_display_base,
5783664e 345 user_attributes,
cfbd7cf3
FD
346 )
347
348 def create_unsigned_integer_field_class(
5783664e 349 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
350 ):
351 return self._create_integer_field_class(
9c08c816 352 native_bt.field_class_integer_unsigned_create,
3fb99a22 353 bt2_field_class._UnsignedIntegerFieldClass,
f5567ea8 354 "unsigned integer",
cfbd7cf3
FD
355 field_value_range,
356 preferred_display_base,
5783664e 357 user_attributes,
cfbd7cf3
FD
358 )
359
360 def create_signed_enumeration_field_class(
5783664e 361 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
362 ):
363 return self._create_integer_field_class(
9c08c816 364 native_bt.field_class_enumeration_signed_create,
3fb99a22 365 bt2_field_class._SignedEnumerationFieldClass,
f5567ea8 366 "signed enumeration",
cfbd7cf3
FD
367 field_value_range,
368 preferred_display_base,
5783664e 369 user_attributes,
cfbd7cf3
FD
370 )
371
372 def create_unsigned_enumeration_field_class(
5783664e 373 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
374 ):
375 return self._create_integer_field_class(
9c08c816 376 native_bt.field_class_enumeration_unsigned_create,
3fb99a22 377 bt2_field_class._UnsignedEnumerationFieldClass,
f5567ea8 378 "unsigned enumeration",
cfbd7cf3
FD
379 field_value_range,
380 preferred_display_base,
5783664e 381 user_attributes,
cfbd7cf3 382 )
2ae9f48c 383
fe4df857
FD
384 def create_single_precision_real_field_class(self, user_attributes=None):
385 field_class_ptr = native_bt.field_class_real_single_precision_create(self._ptr)
f5567ea8 386 self._check_field_class_create_status(field_class_ptr, "single-precision real")
2ae9f48c 387
fe4df857
FD
388 field_class = bt2_field_class._SinglePrecisionRealFieldClass._create_from_ptr(
389 field_class_ptr
390 )
391
392 self._set_field_class_user_attrs(field_class, user_attributes)
393
394 return field_class
395
396 def create_double_precision_real_field_class(self, user_attributes=None):
397 field_class_ptr = native_bt.field_class_real_double_precision_create(self._ptr)
f5567ea8 398 self._check_field_class_create_status(field_class_ptr, "double-precision real")
fe4df857
FD
399
400 field_class = bt2_field_class._DoublePrecisionRealFieldClass._create_from_ptr(
401 field_class_ptr
402 )
2ae9f48c 403
5783664e 404 self._set_field_class_user_attrs(field_class, user_attributes)
2ae9f48c
SM
405
406 return field_class
407
5783664e 408 def create_structure_field_class(self, user_attributes=None):
3cdfbaea 409 field_class_ptr = native_bt.field_class_structure_create(self._ptr)
f5567ea8 410 self._check_field_class_create_status(field_class_ptr, "structure")
5783664e
PP
411 fc = bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr)
412 self._set_field_class_user_attrs(fc, user_attributes)
413 return fc
3cdfbaea 414
5783664e 415 def create_string_field_class(self, user_attributes=None):
3cdfbaea 416 field_class_ptr = native_bt.field_class_string_create(self._ptr)
f5567ea8 417 self._check_field_class_create_status(field_class_ptr, "string")
5783664e
PP
418 fc = bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr)
419 self._set_field_class_user_attrs(fc, user_attributes)
420 return fc
3cdfbaea 421
5783664e 422 def create_static_array_field_class(self, elem_fc, length, user_attributes=None):
3fb99a22 423 utils._check_type(elem_fc, bt2_field_class._FieldClass)
d47b87ac 424 utils._check_uint64(length)
9c08c816 425 ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length)
f5567ea8 426 self._check_field_class_create_status(ptr, "static array")
2bebdd7f 427 fc = bt2_field_class._StaticArrayFieldClass._create_from_ptr(ptr)
5783664e
PP
428 self._set_field_class_user_attrs(fc, user_attributes)
429 return fc
d47b87ac 430
5783664e
PP
431 def create_dynamic_array_field_class(
432 self, elem_fc, length_fc=None, user_attributes=None
433 ):
3fb99a22 434 utils._check_type(elem_fc, bt2_field_class._FieldClass)
1367bc7c 435 length_fc_ptr = None
d47b87ac
SM
436
437 if length_fc is not None:
3fb99a22 438 utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass)
1367bc7c 439 length_fc_ptr = length_fc._ptr
d47b87ac 440
9c08c816 441 ptr = native_bt.field_class_array_dynamic_create(
cfbd7cf3
FD
442 self._ptr, elem_fc._ptr, length_fc_ptr
443 )
f5567ea8 444 self._check_field_class_create_status(ptr, "dynamic array")
2bebdd7f 445 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
5783664e
PP
446 self._set_field_class_user_attrs(fc, user_attributes)
447 return fc
d47b87ac 448
0aa006b7
PP
449 def create_option_without_selector_field_class(
450 self, content_fc, user_attributes=None
5783664e 451 ):
cec0261d 452 utils._check_type(content_fc, bt2_field_class._FieldClass)
0aa006b7
PP
453 ptr = native_bt.field_class_option_without_selector_create(
454 self._ptr, content_fc._ptr
455 )
f5567ea8 456 self._check_field_class_create_status(ptr, "option")
2bebdd7f 457 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
0aa006b7
PP
458 self._set_field_class_user_attrs(fc, user_attributes)
459 return fc
cec0261d 460
0aa006b7
PP
461 def create_option_with_bool_selector_field_class(
462 self, content_fc, selector_fc, selector_is_reversed=False, user_attributes=None
463 ):
464 utils._check_type(content_fc, bt2_field_class._FieldClass)
465 utils._check_bool(selector_is_reversed)
466 utils._check_type(selector_fc, bt2_field_class._BoolFieldClass)
de821fe5 467 ptr = native_bt.field_class_option_with_selector_field_bool_create(
0aa006b7
PP
468 self._ptr, content_fc._ptr, selector_fc._ptr
469 )
f5567ea8 470 self._check_field_class_create_status(ptr, "option")
2bebdd7f 471 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
0aa006b7
PP
472 self._set_field_class_user_attrs(fc, user_attributes)
473 fc._selector_is_reversed = selector_is_reversed
474 return fc
cec0261d 475
0aa006b7
PP
476 def create_option_with_integer_selector_field_class(
477 self, content_fc, selector_fc, ranges, user_attributes=None
478 ):
479 utils._check_type(content_fc, bt2_field_class._FieldClass)
480 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
481
482 if len(ranges) == 0:
f5567ea8 483 raise ValueError("integer range set is empty")
0aa006b7
PP
484
485 if isinstance(selector_fc, bt2_field_class._UnsignedIntegerFieldClass):
486 utils._check_type(ranges, bt2_integer_range_set.UnsignedIntegerRangeSet)
de821fe5 487 ptr = native_bt.field_class_option_with_selector_field_integer_unsigned_create(
0aa006b7
PP
488 self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr
489 )
490 else:
491 utils._check_type(ranges, bt2_integer_range_set.SignedIntegerRangeSet)
776a2a25
PP
492 ptr = (
493 native_bt.field_class_option_with_selector_field_integer_signed_create(
494 self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr
495 )
0aa006b7 496 )
cec0261d 497
f5567ea8 498 self._check_field_class_create_status(ptr, "option")
2bebdd7f 499 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
5783664e
PP
500 self._set_field_class_user_attrs(fc, user_attributes)
501 return fc
cec0261d 502
5783664e 503 def create_variant_field_class(self, selector_fc=None, user_attributes=None):
45c51519 504 selector_fc_ptr = None
d47b87ac
SM
505
506 if selector_fc is not None:
3fb99a22 507 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
45c51519 508 selector_fc_ptr = selector_fc._ptr
d47b87ac 509
45c51519 510 ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr)
f5567ea8 511 self._check_field_class_create_status(ptr, "variant")
2bebdd7f 512 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
5783664e
PP
513 self._set_field_class_user_attrs(fc, user_attributes)
514 return fc
This page took 0.097605 seconds and 4 git commands to generate.