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