lib: split real FC/field into single and double prec FC/field
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_class.py
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
25 from bt2 import native_bt, utils, object
26 from bt2 import stream_class as bt2_stream_class
27 from bt2 import field_class as bt2_field_class
28 from bt2 import trace as bt2_trace
29 from bt2 import value as bt2_value
30 import collections.abc
31 import functools
32 import bt2
33
34
35 def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr):
36 trace_class = _TraceClass._create_from_ptr_and_get_ref(trace_class_ptr)
37 user_listener(trace_class)
38
39
40 class _TraceClassConst(object._SharedObject, collections.abc.Mapping):
41 _get_ref = staticmethod(native_bt.trace_class_get_ref)
42 _put_ref = staticmethod(native_bt.trace_class_put_ref)
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 )
49 _borrow_user_attributes_ptr = staticmethod(
50 native_bt.trace_class_borrow_user_attributes_const
51 )
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 )
56
57 @property
58 def user_attributes(self):
59 ptr = self._borrow_user_attributes_ptr(self._ptr)
60 assert ptr is not None
61 return self._create_value_from_ptr_and_get_ref(ptr)
62
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
75 sc_ptr = self._borrow_stream_class_ptr_by_id(self._ptr, key)
76 if sc_ptr is None:
77 raise KeyError(key)
78
79 return self._stream_class_pycls._create_from_ptr_and_get_ref(sc_ptr)
80
81 def __iter__(self):
82 for idx in range(len(self)):
83 sc_ptr = self._borrow_stream_class_ptr_by_index(self._ptr, idx)
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
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
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
133
134 class _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 )
141 _borrow_user_attributes_ptr = staticmethod(
142 native_bt.trace_class_borrow_user_attributes
143 )
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
174 def create_stream_class(
175 self,
176 id=None,
177 name=None,
178 user_attributes=None,
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 ):
192
193 if self.assigns_automatic_stream_class_id:
194 if id is not None:
195 raise ValueError(
196 'id provided, but trace class assigns automatic stream class ids'
197 )
198
199 sc_ptr = native_bt.stream_class_create(self._ptr)
200 else:
201 if id is None:
202 raise ValueError(
203 'id not provided, but trace class does not assign automatic stream class ids'
204 )
205
206 utils._check_uint64(id)
207 sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id)
208
209 sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr)
210
211 if name is not None:
212 sc._name = name
213
214 if user_attributes is not None:
215 sc._user_attributes = user_attributes
216
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
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.
227 sc._set_supports_packets(
228 supports_packets,
229 packets_have_beginning_default_clock_snapshot,
230 packets_have_end_default_clock_snapshot,
231 )
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
239 sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
240 sc._assigns_automatic_stream_id = assigns_automatic_stream_id
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 )
247 return sc
248
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
256 def _assigns_automatic_stream_class_id(self, auto_id):
257 utils._check_bool(auto_id)
258 return native_bt.trace_class_set_assigns_automatic_stream_class_id(
259 self._ptr, auto_id
260 )
261
262 _assigns_automatic_stream_class_id = property(
263 fset=_assigns_automatic_stream_class_id
264 )
265
266 # Field class creation methods.
267
268 def _check_field_class_create_status(self, ptr, type_name):
269 if ptr is None:
270 raise bt2._MemoryError('cannot create {} field class'.format(type_name))
271
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):
278 field_class_ptr = native_bt.field_class_bool_create(self._ptr)
279 self._check_field_class_create_status(field_class_ptr, 'boolean')
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
283
284 def create_bit_array_field_class(self, length, user_attributes=None):
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')
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
299
300 def _create_integer_field_class(
301 self,
302 create_func,
303 py_cls,
304 type_name,
305 field_value_range,
306 preferred_display_base,
307 user_attributes,
308 ):
309 field_class_ptr = create_func(self._ptr)
310 self._check_field_class_create_status(field_class_ptr, type_name)
311
312 field_class = py_cls._create_from_ptr(field_class_ptr)
313
314 if field_value_range is not None:
315 field_class._field_value_range = field_value_range
316
317 if preferred_display_base is not None:
318 field_class._preferred_display_base = preferred_display_base
319
320 self._set_field_class_user_attrs(field_class, user_attributes)
321 return field_class
322
323 def create_signed_integer_field_class(
324 self, field_value_range=None, preferred_display_base=None, user_attributes=None
325 ):
326 return self._create_integer_field_class(
327 native_bt.field_class_integer_signed_create,
328 bt2_field_class._SignedIntegerFieldClass,
329 'signed integer',
330 field_value_range,
331 preferred_display_base,
332 user_attributes,
333 )
334
335 def create_unsigned_integer_field_class(
336 self, field_value_range=None, preferred_display_base=None, user_attributes=None
337 ):
338 return self._create_integer_field_class(
339 native_bt.field_class_integer_unsigned_create,
340 bt2_field_class._UnsignedIntegerFieldClass,
341 'unsigned integer',
342 field_value_range,
343 preferred_display_base,
344 user_attributes,
345 )
346
347 def create_signed_enumeration_field_class(
348 self, field_value_range=None, preferred_display_base=None, user_attributes=None
349 ):
350 return self._create_integer_field_class(
351 native_bt.field_class_enumeration_signed_create,
352 bt2_field_class._SignedEnumerationFieldClass,
353 'signed enumeration',
354 field_value_range,
355 preferred_display_base,
356 user_attributes,
357 )
358
359 def create_unsigned_enumeration_field_class(
360 self, field_value_range=None, preferred_display_base=None, user_attributes=None
361 ):
362 return self._create_integer_field_class(
363 native_bt.field_class_enumeration_unsigned_create,
364 bt2_field_class._UnsignedEnumerationFieldClass,
365 'unsigned enumeration',
366 field_value_range,
367 preferred_display_base,
368 user_attributes,
369 )
370
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')
374
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 )
390
391 self._set_field_class_user_attrs(field_class, user_attributes)
392
393 return field_class
394
395 def create_structure_field_class(self, user_attributes=None):
396 field_class_ptr = native_bt.field_class_structure_create(self._ptr)
397 self._check_field_class_create_status(field_class_ptr, 'structure')
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
401
402 def create_string_field_class(self, user_attributes=None):
403 field_class_ptr = native_bt.field_class_string_create(self._ptr)
404 self._check_field_class_create_status(field_class_ptr, 'string')
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
408
409 def create_static_array_field_class(self, elem_fc, length, user_attributes=None):
410 utils._check_type(elem_fc, bt2_field_class._FieldClass)
411 utils._check_uint64(length)
412 ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length)
413 self._check_field_class_create_status(ptr, 'static array')
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
417
418 def create_dynamic_array_field_class(
419 self, elem_fc, length_fc=None, user_attributes=None
420 ):
421 utils._check_type(elem_fc, bt2_field_class._FieldClass)
422 length_fc_ptr = None
423
424 if length_fc is not None:
425 utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass)
426 length_fc_ptr = length_fc._ptr
427
428 ptr = native_bt.field_class_array_dynamic_create(
429 self._ptr, elem_fc._ptr, length_fc_ptr
430 )
431 self._check_field_class_create_status(ptr, 'dynamic array')
432 fc = bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr)
433 self._set_field_class_user_attrs(fc, user_attributes)
434 return fc
435
436 def create_option_field_class(
437 self, content_fc, selector_fc=None, user_attributes=None
438 ):
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')
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
454
455 def create_variant_field_class(self, selector_fc=None, user_attributes=None):
456 selector_fc_ptr = None
457
458 if selector_fc is not None:
459 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
460 selector_fc_ptr = selector_fc._ptr
461
462 ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr)
463 self._check_field_class_create_status(ptr, 'variant')
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.039816 seconds and 4 git commands to generate.