lib: split real FC/field into single and double prec FC/field
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_class.py
CommitLineData
fbbe9302
SM
1# The MIT License (MIT)
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>
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23# THE SOFTWARE.
24
fbbe9302 25from bt2 import native_bt, utils, object
3fb99a22
PP
26from bt2 import stream_class as bt2_stream_class
27from bt2 import field_class as bt2_field_class
28from bt2 import trace as bt2_trace
5783664e 29from bt2 import value as bt2_value
fbbe9302
SM
30import collections.abc
31import functools
f0a42b33 32import bt2
fbbe9302
SM
33
34
fbbe9302 35def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr):
f0a42b33 36 trace_class = _TraceClass._create_from_ptr_and_get_ref(trace_class_ptr)
fbbe9302
SM
37 user_listener(trace_class)
38
39
f0a42b33 40class _TraceClassConst(object._SharedObject, collections.abc.Mapping):
fbbe9302
SM
41 _get_ref = staticmethod(native_bt.trace_class_get_ref)
42 _put_ref = staticmethod(native_bt.trace_class_put_ref)
f0a42b33
FD
43 _borrow_stream_class_ptr_by_index = staticmethod(
44 native_bt.trace_class_borrow_stream_class_by_index_const
45 )
46 _borrow_stream_class_ptr_by_id = staticmethod(
47 native_bt.trace_class_borrow_stream_class_by_id_const
48 )
2550c437
SM
49 _borrow_user_attributes_ptr = staticmethod(
50 native_bt.trace_class_borrow_user_attributes_const
51 )
f0a42b33
FD
52 _stream_class_pycls = bt2_stream_class._StreamClassConst
53 _create_value_from_ptr_and_get_ref = staticmethod(
54 bt2_value._create_from_const_ptr_and_get_ref
55 )
fbbe9302 56
f0a42b33
FD
57 @property
58 def user_attributes(self):
2550c437 59 ptr = self._borrow_user_attributes_ptr(self._ptr)
f0a42b33
FD
60 assert ptr is not None
61 return self._create_value_from_ptr_and_get_ref(ptr)
8c2367b8 62
fbbe9302
SM
63 # Number of stream classes in this trace class.
64
65 def __len__(self):
66 count = native_bt.trace_class_get_stream_class_count(self._ptr)
67 assert count >= 0
68 return count
69
70 # Get a stream class by stream id.
71
72 def __getitem__(self, key):
73 utils._check_uint64(key)
74
f0a42b33 75 sc_ptr = self._borrow_stream_class_ptr_by_id(self._ptr, key)
fbbe9302
SM
76 if sc_ptr is None:
77 raise KeyError(key)
78
f0a42b33 79 return self._stream_class_pycls._create_from_ptr_and_get_ref(sc_ptr)
fbbe9302
SM
80
81 def __iter__(self):
82 for idx in range(len(self)):
f0a42b33 83 sc_ptr = self._borrow_stream_class_ptr_by_index(self._ptr, idx)
fbbe9302
SM
84 assert sc_ptr is not None
85
86 id = native_bt.stream_class_get_id(sc_ptr)
87 assert id >= 0
88
89 yield id
90
f0a42b33
FD
91 @property
92 def assigns_automatic_stream_class_id(self):
93 return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr)
94
95 # Add a listener to be called when the trace class is destroyed.
96
97 def add_destruction_listener(self, listener):
98
99 if not callable(listener):
100 raise TypeError("'listener' parameter is not callable")
101
102 fn = native_bt.bt2_trace_class_add_destruction_listener
103 listener_from_native = functools.partial(
104 _trace_class_destruction_listener_from_native, listener
105 )
106
107 status, listener_id = fn(self._ptr, listener_from_native)
108 utils._handle_func_status(
109 status, 'cannot add destruction listener to trace class object'
110 )
111
112 return utils._ListenerHandle(listener_id, self)
113
1114a7d5
SM
114 def remove_destruction_listener(self, listener_handle):
115 utils._check_type(listener_handle, utils._ListenerHandle)
116
117 if listener_handle._obj.addr != self.addr:
118 raise ValueError(
119 'This trace class destruction listener does not match the trace object.'
120 )
121
122 if listener_handle._listener_id is None:
123 raise ValueError(
124 'This trace class destruction listener was already removed.'
125 )
126
127 status = native_bt.trace_class_remove_destruction_listener(
128 self._ptr, listener_handle._listener_id
129 )
130 utils._handle_func_status(status)
131 listener_handle._listener_id = None
132
f0a42b33
FD
133
134class _TraceClass(_TraceClassConst):
135 _borrow_stream_class_ptr_by_index = staticmethod(
136 native_bt.trace_class_borrow_stream_class_by_index
137 )
138 _borrow_stream_class_ptr_by_id = staticmethod(
139 native_bt.trace_class_borrow_stream_class_by_id
140 )
2550c437
SM
141 _borrow_user_attributes_ptr = staticmethod(
142 native_bt.trace_class_borrow_user_attributes
143 )
f0a42b33
FD
144 _stream_class_pycls = bt2_stream_class._StreamClass
145 _create_value_from_ptr_and_get_ref = staticmethod(
146 bt2_value._create_from_ptr_and_get_ref
147 )
148
149 # Instantiate a trace of this class.
150
151 def __call__(self, name=None, user_attributes=None, uuid=None, environment=None):
152 trace_ptr = native_bt.trace_create(self._ptr)
153
154 if trace_ptr is None:
155 raise bt2._MemoryError('cannot create trace class object')
156
157 trace = bt2_trace._Trace._create_from_ptr(trace_ptr)
158
159 if name is not None:
160 trace._name = name
161
162 if user_attributes is not None:
163 trace._user_attributes = user_attributes
164
165 if uuid is not None:
166 trace._uuid = uuid
167
168 if environment is not None:
169 for key, value in environment.items():
170 trace.environment[key] = value
171
172 return trace
173
cfbd7cf3
FD
174 def create_stream_class(
175 self,
176 id=None,
177 name=None,
5783664e 178 user_attributes=None,
cfbd7cf3
FD
179 packet_context_field_class=None,
180 event_common_context_field_class=None,
181 default_clock_class=None,
182 assigns_automatic_event_class_id=True,
183 assigns_automatic_stream_id=True,
184 supports_packets=False,
185 packets_have_beginning_default_clock_snapshot=False,
186 packets_have_end_default_clock_snapshot=False,
187 supports_discarded_events=False,
188 discarded_events_have_default_clock_snapshots=False,
189 supports_discarded_packets=False,
190 discarded_packets_have_default_clock_snapshots=False,
191 ):
fbbe9302
SM
192
193 if self.assigns_automatic_stream_class_id:
194 if id is not None:
cfbd7cf3
FD
195 raise ValueError(
196 'id provided, but trace class assigns automatic stream class ids'
197 )
fbbe9302
SM
198
199 sc_ptr = native_bt.stream_class_create(self._ptr)
200 else:
201 if id is None:
cfbd7cf3
FD
202 raise ValueError(
203 'id not provided, but trace class does not assign automatic stream class ids'
204 )
fbbe9302
SM
205
206 utils._check_uint64(id)
207 sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id)
208
3fb99a22 209 sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr)
8c2367b8 210
3cdfbaea
SM
211 if name is not None:
212 sc._name = name
213
5783664e
PP
214 if user_attributes is not None:
215 sc._user_attributes = user_attributes
216
3cdfbaea
SM
217 if event_common_context_field_class is not None:
218 sc._event_common_context_field_class = event_common_context_field_class
219
220 if default_clock_class is not None:
221 sc._default_clock_class = default_clock_class
222
26fc5aed
PP
223 # call after `sc._default_clock_class` because, if
224 # `packets_have_beginning_default_clock_snapshot` or
225 # `packets_have_end_default_clock_snapshot` is true, then this
226 # stream class needs a default clock class already.
cfbd7cf3
FD
227 sc._set_supports_packets(
228 supports_packets,
229 packets_have_beginning_default_clock_snapshot,
230 packets_have_end_default_clock_snapshot,
231 )
26fc5aed
PP
232
233 # call after sc._set_supports_packets() because, if
234 # `packet_context_field_class` is not `None`, then this stream
235 # class needs to support packets already.
236 if packet_context_field_class is not None:
237 sc._packet_context_field_class = packet_context_field_class
238
3cdfbaea 239 sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
8c2367b8 240 sc._assigns_automatic_stream_id = assigns_automatic_stream_id
cfbd7cf3
FD
241 sc._set_supports_discarded_events(
242 supports_discarded_events, discarded_events_have_default_clock_snapshots
243 )
244 sc._set_supports_discarded_packets(
245 supports_discarded_packets, discarded_packets_have_default_clock_snapshots
246 )
8c2367b8 247 return sc
fbbe9302 248
5783664e
PP
249 def _user_attributes(self, user_attributes):
250 value = bt2_value.create_value(user_attributes)
251 utils._check_type(value, bt2_value.MapValue)
252 native_bt.trace_class_set_user_attributes(self._ptr, value._ptr)
253
254 _user_attributes = property(fset=_user_attributes)
255
fbbe9302
SM
256 def _assigns_automatic_stream_class_id(self, auto_id):
257 utils._check_bool(auto_id)
cfbd7cf3
FD
258 return native_bt.trace_class_set_assigns_automatic_stream_class_id(
259 self._ptr, auto_id
260 )
fbbe9302 261
cfbd7cf3
FD
262 _assigns_automatic_stream_class_id = property(
263 fset=_assigns_automatic_stream_class_id
264 )
fbbe9302 265
3cdfbaea
SM
266 # Field class creation methods.
267
aae30e61 268 def _check_field_class_create_status(self, ptr, type_name):
3cdfbaea 269 if ptr is None:
694c792b 270 raise bt2._MemoryError('cannot create {} field class'.format(type_name))
3cdfbaea 271
5783664e
PP
272 @staticmethod
273 def _set_field_class_user_attrs(fc, user_attributes):
274 if user_attributes is not None:
275 fc._user_attributes = user_attributes
276
277 def create_bool_field_class(self, user_attributes=None):
aae30e61
PP
278 field_class_ptr = native_bt.field_class_bool_create(self._ptr)
279 self._check_field_class_create_status(field_class_ptr, 'boolean')
5783664e
PP
280 fc = bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr)
281 self._set_field_class_user_attrs(fc, user_attributes)
282 return fc
aae30e61 283
5783664e 284 def create_bit_array_field_class(self, length, user_attributes=None):
ead8c3d4
PP
285 utils._check_uint64(length)
286
287 if length < 1 or length > 64:
288 raise ValueError(
289 'invalid length {}: expecting a value in the [1, 64] range'.format(
290 length
291 )
292 )
293
294 field_class_ptr = native_bt.field_class_bit_array_create(self._ptr, length)
295 self._check_field_class_create_status(field_class_ptr, 'bit array')
5783664e
PP
296 fc = bt2_field_class._BitArrayFieldClass._create_from_ptr(field_class_ptr)
297 self._set_field_class_user_attrs(fc, user_attributes)
298 return fc
ead8c3d4 299
cfbd7cf3 300 def _create_integer_field_class(
5783664e
PP
301 self,
302 create_func,
303 py_cls,
304 type_name,
305 field_value_range,
306 preferred_display_base,
307 user_attributes,
cfbd7cf3 308 ):
af4bbfc7 309 field_class_ptr = create_func(self._ptr)
aae30e61 310 self._check_field_class_create_status(field_class_ptr, type_name)
af4bbfc7
SM
311
312 field_class = py_cls._create_from_ptr(field_class_ptr)
313
d47b87ac
SM
314 if field_value_range is not None:
315 field_class._field_value_range = field_value_range
af4bbfc7 316
d47b87ac
SM
317 if preferred_display_base is not None:
318 field_class._preferred_display_base = preferred_display_base
af4bbfc7 319
5783664e 320 self._set_field_class_user_attrs(field_class, user_attributes)
af4bbfc7
SM
321 return field_class
322
cfbd7cf3 323 def create_signed_integer_field_class(
5783664e 324 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
325 ):
326 return self._create_integer_field_class(
9c08c816 327 native_bt.field_class_integer_signed_create,
3fb99a22 328 bt2_field_class._SignedIntegerFieldClass,
cfbd7cf3
FD
329 'signed integer',
330 field_value_range,
331 preferred_display_base,
5783664e 332 user_attributes,
cfbd7cf3
FD
333 )
334
335 def create_unsigned_integer_field_class(
5783664e 336 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
337 ):
338 return self._create_integer_field_class(
9c08c816 339 native_bt.field_class_integer_unsigned_create,
3fb99a22 340 bt2_field_class._UnsignedIntegerFieldClass,
cfbd7cf3
FD
341 'unsigned integer',
342 field_value_range,
343 preferred_display_base,
5783664e 344 user_attributes,
cfbd7cf3
FD
345 )
346
347 def create_signed_enumeration_field_class(
5783664e 348 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
349 ):
350 return self._create_integer_field_class(
9c08c816 351 native_bt.field_class_enumeration_signed_create,
3fb99a22 352 bt2_field_class._SignedEnumerationFieldClass,
cfbd7cf3
FD
353 'signed enumeration',
354 field_value_range,
355 preferred_display_base,
5783664e 356 user_attributes,
cfbd7cf3
FD
357 )
358
359 def create_unsigned_enumeration_field_class(
5783664e 360 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
361 ):
362 return self._create_integer_field_class(
9c08c816 363 native_bt.field_class_enumeration_unsigned_create,
3fb99a22 364 bt2_field_class._UnsignedEnumerationFieldClass,
cfbd7cf3
FD
365 'unsigned enumeration',
366 field_value_range,
367 preferred_display_base,
5783664e 368 user_attributes,
cfbd7cf3 369 )
2ae9f48c 370
fe4df857
FD
371 def create_single_precision_real_field_class(self, user_attributes=None):
372 field_class_ptr = native_bt.field_class_real_single_precision_create(self._ptr)
373 self._check_field_class_create_status(field_class_ptr, 'single-precision real')
2ae9f48c 374
fe4df857
FD
375 field_class = bt2_field_class._SinglePrecisionRealFieldClass._create_from_ptr(
376 field_class_ptr
377 )
378
379 self._set_field_class_user_attrs(field_class, user_attributes)
380
381 return field_class
382
383 def create_double_precision_real_field_class(self, user_attributes=None):
384 field_class_ptr = native_bt.field_class_real_double_precision_create(self._ptr)
385 self._check_field_class_create_status(field_class_ptr, 'double-precision real')
386
387 field_class = bt2_field_class._DoublePrecisionRealFieldClass._create_from_ptr(
388 field_class_ptr
389 )
2ae9f48c 390
5783664e 391 self._set_field_class_user_attrs(field_class, user_attributes)
2ae9f48c
SM
392
393 return field_class
394
5783664e 395 def create_structure_field_class(self, user_attributes=None):
3cdfbaea 396 field_class_ptr = native_bt.field_class_structure_create(self._ptr)
aae30e61 397 self._check_field_class_create_status(field_class_ptr, 'structure')
5783664e
PP
398 fc = bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr)
399 self._set_field_class_user_attrs(fc, user_attributes)
400 return fc
3cdfbaea 401
5783664e 402 def create_string_field_class(self, user_attributes=None):
3cdfbaea 403 field_class_ptr = native_bt.field_class_string_create(self._ptr)
aae30e61 404 self._check_field_class_create_status(field_class_ptr, 'string')
5783664e
PP
405 fc = bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr)
406 self._set_field_class_user_attrs(fc, user_attributes)
407 return fc
3cdfbaea 408
5783664e 409 def create_static_array_field_class(self, elem_fc, length, user_attributes=None):
3fb99a22 410 utils._check_type(elem_fc, bt2_field_class._FieldClass)
d47b87ac 411 utils._check_uint64(length)
9c08c816 412 ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length)
aae30e61 413 self._check_field_class_create_status(ptr, 'static array')
5783664e
PP
414 fc = bt2_field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr)
415 self._set_field_class_user_attrs(fc, user_attributes)
416 return fc
d47b87ac 417
5783664e
PP
418 def create_dynamic_array_field_class(
419 self, elem_fc, length_fc=None, user_attributes=None
420 ):
3fb99a22 421 utils._check_type(elem_fc, bt2_field_class._FieldClass)
1367bc7c 422 length_fc_ptr = None
d47b87ac
SM
423
424 if length_fc is not None:
3fb99a22 425 utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass)
1367bc7c 426 length_fc_ptr = length_fc._ptr
d47b87ac 427
9c08c816 428 ptr = native_bt.field_class_array_dynamic_create(
cfbd7cf3
FD
429 self._ptr, elem_fc._ptr, length_fc_ptr
430 )
aae30e61 431 self._check_field_class_create_status(ptr, 'dynamic array')
5783664e
PP
432 fc = bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr)
433 self._set_field_class_user_attrs(fc, user_attributes)
434 return fc
d47b87ac 435
5783664e
PP
436 def create_option_field_class(
437 self, content_fc, selector_fc=None, user_attributes=None
438 ):
cec0261d
PP
439 utils._check_type(content_fc, bt2_field_class._FieldClass)
440
441 selector_fc_ptr = None
442
443 if selector_fc is not None:
444 utils._check_type(selector_fc, bt2_field_class._BoolFieldClass)
445 selector_fc_ptr = selector_fc._ptr
446
447 ptr = native_bt.field_class_option_create(
448 self._ptr, content_fc._ptr, selector_fc_ptr
449 )
450 self._check_field_class_create_status(ptr, 'option')
5783664e
PP
451 fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
452 self._set_field_class_user_attrs(fc, user_attributes)
453 return fc
cec0261d 454
5783664e 455 def create_variant_field_class(self, selector_fc=None, user_attributes=None):
45c51519 456 selector_fc_ptr = None
d47b87ac
SM
457
458 if selector_fc is not None:
3fb99a22 459 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
45c51519 460 selector_fc_ptr = selector_fc._ptr
d47b87ac 461
45c51519 462 ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr)
aae30e61 463 self._check_field_class_create_status(ptr, 'variant')
5783664e
PP
464 fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
465 self._set_field_class_user_attrs(fc, user_attributes)
466 return fc
This page took 0.056221 seconds and 4 git commands to generate.