bt2: add boolean field class and field support
[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 __all__ = ['_TraceClass']
26
27 import bt2
28 from bt2 import native_bt, utils, object
29 from bt2 import stream_class as bt2_stream_class
30 from bt2 import field_class as bt2_field_class
31 from bt2 import trace as bt2_trace
32 from bt2 import trace_class as bt2_trace_class
33 import collections.abc
34 import functools
35
36
37 def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr):
38 trace_class = bt2_trace_class._TraceClass._create_from_ptr_and_get_ref(
39 trace_class_ptr
40 )
41 user_listener(trace_class)
42
43
44 class _TraceClass(object._SharedObject, collections.abc.Mapping):
45 _get_ref = staticmethod(native_bt.trace_class_get_ref)
46 _put_ref = staticmethod(native_bt.trace_class_put_ref)
47
48 # Instantiate a trace of this class.
49
50 def __call__(self, name=None, uuid=None, env=None):
51 trace_ptr = native_bt.trace_create(self._ptr)
52
53 if trace_ptr is None:
54 raise bt2._MemoryError('cannot create trace class object')
55
56 trace = bt2_trace._Trace._create_from_ptr(trace_ptr)
57
58 if name is not None:
59 trace._name = name
60
61 if uuid is not None:
62 trace._uuid = uuid
63
64 if env is not None:
65 for key, value in env.items():
66 trace.env[key] = value
67
68 return trace
69
70 # Number of stream classes in this trace class.
71
72 def __len__(self):
73 count = native_bt.trace_class_get_stream_class_count(self._ptr)
74 assert count >= 0
75 return count
76
77 # Get a stream class by stream id.
78
79 def __getitem__(self, key):
80 utils._check_uint64(key)
81
82 sc_ptr = native_bt.trace_class_borrow_stream_class_by_id_const(self._ptr, key)
83 if sc_ptr is None:
84 raise KeyError(key)
85
86 return bt2_stream_class._StreamClass._create_from_ptr_and_get_ref(sc_ptr)
87
88 def __iter__(self):
89 for idx in range(len(self)):
90 sc_ptr = native_bt.trace_class_borrow_stream_class_by_index_const(
91 self._ptr, idx
92 )
93 assert sc_ptr is not None
94
95 id = native_bt.stream_class_get_id(sc_ptr)
96 assert id >= 0
97
98 yield id
99
100 def create_stream_class(
101 self,
102 id=None,
103 name=None,
104 packet_context_field_class=None,
105 event_common_context_field_class=None,
106 default_clock_class=None,
107 assigns_automatic_event_class_id=True,
108 assigns_automatic_stream_id=True,
109 supports_packets=False,
110 packets_have_beginning_default_clock_snapshot=False,
111 packets_have_end_default_clock_snapshot=False,
112 supports_discarded_events=False,
113 discarded_events_have_default_clock_snapshots=False,
114 supports_discarded_packets=False,
115 discarded_packets_have_default_clock_snapshots=False,
116 ):
117
118 if self.assigns_automatic_stream_class_id:
119 if id is not None:
120 raise ValueError(
121 'id provided, but trace class assigns automatic stream class ids'
122 )
123
124 sc_ptr = native_bt.stream_class_create(self._ptr)
125 else:
126 if id is None:
127 raise ValueError(
128 'id not provided, but trace class does not assign automatic stream class ids'
129 )
130
131 utils._check_uint64(id)
132 sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id)
133
134 sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr)
135
136 if name is not None:
137 sc._name = name
138
139 if event_common_context_field_class is not None:
140 sc._event_common_context_field_class = event_common_context_field_class
141
142 if default_clock_class is not None:
143 sc._default_clock_class = default_clock_class
144
145 # call after `sc._default_clock_class` because, if
146 # `packets_have_beginning_default_clock_snapshot` or
147 # `packets_have_end_default_clock_snapshot` is true, then this
148 # stream class needs a default clock class already.
149 sc._set_supports_packets(
150 supports_packets,
151 packets_have_beginning_default_clock_snapshot,
152 packets_have_end_default_clock_snapshot,
153 )
154
155 # call after sc._set_supports_packets() because, if
156 # `packet_context_field_class` is not `None`, then this stream
157 # class needs to support packets already.
158 if packet_context_field_class is not None:
159 sc._packet_context_field_class = packet_context_field_class
160
161 sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
162 sc._assigns_automatic_stream_id = assigns_automatic_stream_id
163 sc._set_supports_discarded_events(
164 supports_discarded_events, discarded_events_have_default_clock_snapshots
165 )
166 sc._set_supports_discarded_packets(
167 supports_discarded_packets, discarded_packets_have_default_clock_snapshots
168 )
169 return sc
170
171 @property
172 def assigns_automatic_stream_class_id(self):
173 return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr)
174
175 def _assigns_automatic_stream_class_id(self, auto_id):
176 utils._check_bool(auto_id)
177 return native_bt.trace_class_set_assigns_automatic_stream_class_id(
178 self._ptr, auto_id
179 )
180
181 _assigns_automatic_stream_class_id = property(
182 fset=_assigns_automatic_stream_class_id
183 )
184
185 # Field class creation methods.
186
187 def _check_field_class_create_status(self, ptr, type_name):
188 if ptr is None:
189 raise bt2._MemoryError('cannot create {} field class'.format(type_name))
190
191 def create_bool_field_class(self):
192 field_class_ptr = native_bt.field_class_bool_create(self._ptr)
193 self._check_field_class_create_status(field_class_ptr, 'boolean')
194
195 return bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr)
196
197 def _create_integer_field_class(
198 self, create_func, py_cls, type_name, field_value_range, preferred_display_base
199 ):
200 field_class_ptr = create_func(self._ptr)
201 self._check_field_class_create_status(field_class_ptr, type_name)
202
203 field_class = py_cls._create_from_ptr(field_class_ptr)
204
205 if field_value_range is not None:
206 field_class._field_value_range = field_value_range
207
208 if preferred_display_base is not None:
209 field_class._preferred_display_base = preferred_display_base
210
211 return field_class
212
213 def create_signed_integer_field_class(
214 self, field_value_range=None, preferred_display_base=None
215 ):
216 return self._create_integer_field_class(
217 native_bt.field_class_integer_signed_create,
218 bt2_field_class._SignedIntegerFieldClass,
219 'signed integer',
220 field_value_range,
221 preferred_display_base,
222 )
223
224 def create_unsigned_integer_field_class(
225 self, field_value_range=None, preferred_display_base=None
226 ):
227 return self._create_integer_field_class(
228 native_bt.field_class_integer_unsigned_create,
229 bt2_field_class._UnsignedIntegerFieldClass,
230 'unsigned integer',
231 field_value_range,
232 preferred_display_base,
233 )
234
235 def create_signed_enumeration_field_class(
236 self, field_value_range=None, preferred_display_base=None
237 ):
238 return self._create_integer_field_class(
239 native_bt.field_class_enumeration_signed_create,
240 bt2_field_class._SignedEnumerationFieldClass,
241 'signed enumeration',
242 field_value_range,
243 preferred_display_base,
244 )
245
246 def create_unsigned_enumeration_field_class(
247 self, field_value_range=None, preferred_display_base=None
248 ):
249 return self._create_integer_field_class(
250 native_bt.field_class_enumeration_unsigned_create,
251 bt2_field_class._UnsignedEnumerationFieldClass,
252 'unsigned enumeration',
253 field_value_range,
254 preferred_display_base,
255 )
256
257 def create_real_field_class(self, is_single_precision=False):
258 field_class_ptr = native_bt.field_class_real_create(self._ptr)
259 self._check_field_class_create_status(field_class_ptr, 'real')
260
261 field_class = bt2_field_class._RealFieldClass._create_from_ptr(field_class_ptr)
262
263 field_class._is_single_precision = is_single_precision
264
265 return field_class
266
267 def create_structure_field_class(self):
268 field_class_ptr = native_bt.field_class_structure_create(self._ptr)
269 self._check_field_class_create_status(field_class_ptr, 'structure')
270
271 return bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr)
272
273 def create_string_field_class(self):
274 field_class_ptr = native_bt.field_class_string_create(self._ptr)
275 self._check_field_class_create_status(field_class_ptr, 'string')
276
277 return bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr)
278
279 def create_static_array_field_class(self, elem_fc, length):
280 utils._check_type(elem_fc, bt2_field_class._FieldClass)
281 utils._check_uint64(length)
282 ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length)
283 self._check_field_class_create_status(ptr, 'static array')
284
285 return bt2_field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr)
286
287 def create_dynamic_array_field_class(self, elem_fc, length_fc=None):
288 utils._check_type(elem_fc, bt2_field_class._FieldClass)
289 length_fc_ptr = None
290
291 if length_fc is not None:
292 utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass)
293 length_fc_ptr = length_fc._ptr
294
295 ptr = native_bt.field_class_array_dynamic_create(
296 self._ptr, elem_fc._ptr, length_fc_ptr
297 )
298 self._check_field_class_create_status(ptr, 'dynamic array')
299 return bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr)
300
301 def create_variant_field_class(self, selector_fc=None):
302 selector_fc_ptr = None
303
304 if selector_fc is not None:
305 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
306 selector_fc_ptr = selector_fc._ptr
307
308 ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr)
309 self._check_field_class_create_status(ptr, 'variant')
310 return bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
311
312 # Add a listener to be called when the trace class is destroyed.
313
314 def add_destruction_listener(self, listener):
315
316 if not callable(listener):
317 raise TypeError("'listener' parameter is not callable")
318
319 fn = native_bt.bt2_trace_class_add_destruction_listener
320 listener_from_native = functools.partial(
321 _trace_class_destruction_listener_from_native, listener
322 )
323
324 status, listener_id = fn(self._ptr, listener_from_native)
325 utils._handle_func_status(
326 status, 'cannot add destruction listener to trace class object'
327 )
328
329 return utils._ListenerHandle(listener_id, self)
This page took 0.036985 seconds and 5 git commands to generate.