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