Commit | Line | Data |
---|---|---|
5fa16feb PP |
1 | /* |
2 | * The MIT License (MIT) | |
3 | * | |
4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "logging/comp-logging.h" | |
b20382e2 | 26 | #include "compat/glib.h" |
5fa16feb PP |
27 | |
28 | /* | |
29 | * This hash table associates a BT component class object address to a | |
30 | * user-defined Python class (PyObject *). The keys and values are NOT | |
31 | * owned by this hash table. The Python class objects are owned by the | |
32 | * Python module, which should not be unloaded until it is not possible | |
33 | * to create a user Python component anyway. | |
34 | * | |
35 | * This hash table is written to when a user-defined Python component | |
36 | * class is created by one of the bt_bt2_component_class_*_create() | |
37 | * functions. | |
38 | * | |
39 | * This function is read from when a user calls bt_component_create() | |
40 | * with a component class pointer created by one of the functions above. | |
41 | * In this case, the original Python class needs to be found to | |
42 | * instantiate it and associate the created Python component object with | |
43 | * a BT component object instance. | |
44 | */ | |
45 | ||
46 | static GHashTable *bt_cc_ptr_to_py_cls; | |
47 | ||
774b7f28 PP |
48 | static |
49 | void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class *comp_cls) | |
50 | { | |
51 | gboolean existed; | |
52 | ||
53 | if (!bt_cc_ptr_to_py_cls) { | |
54 | return; | |
55 | } | |
56 | ||
57 | existed = g_hash_table_remove(bt_cc_ptr_to_py_cls, comp_cls); | |
58 | BT_ASSERT(existed); | |
59 | } | |
60 | ||
5fa16feb PP |
61 | static |
62 | void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, | |
63 | PyObject *py_cls) | |
64 | { | |
65 | if (!bt_cc_ptr_to_py_cls) { | |
66 | /* | |
67 | * Lazy-initializing this GHashTable because GLib | |
68 | * might not be initialized yet and it needs to be | |
69 | * before we call g_hash_table_new() | |
70 | */ | |
71 | BT_LOGD_STR("Creating native component class to Python component class hash table."); | |
72 | bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal); | |
73 | BT_ASSERT(bt_cc_ptr_to_py_cls); | |
74 | } | |
75 | ||
76 | g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc, | |
77 | (gpointer) py_cls); | |
78 | } | |
79 | ||
80 | static | |
81 | PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc) | |
82 | { | |
83 | if (!bt_cc_ptr_to_py_cls) { | |
84 | BT_LOGW("Cannot look up Python component class because hash table is NULL: " | |
85 | "comp-cls-addr=%p", bt_cc); | |
86 | return NULL; | |
87 | } | |
88 | ||
89 | return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls, | |
90 | (gconstpointer) bt_cc); | |
91 | } | |
92 | ||
5fa16feb PP |
93 | /* Library destructor */ |
94 | ||
95 | __attribute__((destructor)) | |
96 | static | |
97 | void native_comp_class_dtor(void) { | |
98 | /* Destroy component class association hash table */ | |
99 | if (bt_cc_ptr_to_py_cls) { | |
100 | BT_LOGD_STR("Destroying native component class to Python component class hash table."); | |
101 | g_hash_table_destroy(bt_cc_ptr_to_py_cls); | |
774b7f28 | 102 | bt_cc_ptr_to_py_cls = NULL; |
5fa16feb PP |
103 | } |
104 | } | |
105 | ||
5fa16feb | 106 | static inline |
27ba49f1 SM |
107 | int py_exc_to_status_clear( |
108 | bt_self_component_class *self_component_class, | |
5fa16feb PP |
109 | bt_self_component *self_component, |
110 | bt_self_message_iterator *self_message_iterator, | |
111 | const char *module_name, int active_log_level) | |
112 | { | |
7f3ee93d | 113 | int status; |
5fa16feb PP |
114 | PyObject *exc = PyErr_Occurred(); |
115 | ||
116 | if (!exc) { | |
7f3ee93d | 117 | status = __BT_FUNC_STATUS_OK; |
5fa16feb PP |
118 | goto end; |
119 | } | |
120 | ||
121 | if (PyErr_GivenExceptionMatches(exc, | |
122 | py_mod_bt2_exc_try_again_type)) { | |
123 | status = __BT_FUNC_STATUS_AGAIN; | |
124 | } else if (PyErr_GivenExceptionMatches(exc, | |
125 | py_mod_bt2_exc_stop_type)) { | |
126 | status = __BT_FUNC_STATUS_END; | |
127 | } else if (PyErr_GivenExceptionMatches(exc, | |
128 | py_mod_bt2_exc_unknown_object_type)) { | |
129 | status = __BT_FUNC_STATUS_UNKNOWN_OBJECT; | |
130 | } else { | |
131 | /* | |
132 | * Unknown exception: convert to general error. | |
133 | * | |
134 | * Because we only want to fetch the log level when | |
135 | * we actually get an exception, and not systematically | |
136 | * when we call py_exc_to_status() (as py_exc_to_status() | |
137 | * can return `__BT_FUNC_STATUS_OK`), we get it here | |
138 | * depending on the actor's type. | |
139 | */ | |
140 | if (self_component) { | |
141 | active_log_level = get_self_component_log_level( | |
142 | self_component); | |
143 | } else if (self_message_iterator) { | |
144 | active_log_level = get_self_message_iterator_log_level( | |
145 | self_message_iterator); | |
146 | } | |
147 | ||
148 | BT_ASSERT(active_log_level != -1); | |
27ba49f1 | 149 | log_exception_and_maybe_append_cause(BT_LOG_WARNING, |
5fa16feb PP |
150 | active_log_level, true, |
151 | self_component_class, self_component, | |
152 | self_message_iterator, module_name); | |
153 | ||
154 | if (PyErr_GivenExceptionMatches(exc, | |
155 | py_mod_bt2_exc_memory_error)) { | |
156 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
157 | } else { | |
158 | status = __BT_FUNC_STATUS_ERROR; | |
159 | } | |
160 | } | |
161 | ||
162 | end: | |
163 | PyErr_Clear(); | |
164 | return status; | |
165 | } | |
166 | ||
167 | static | |
27ba49f1 | 168 | int py_exc_to_status_component_class_clear( |
5fa16feb PP |
169 | bt_self_component_class *self_component_class, |
170 | int active_log_level) | |
171 | { | |
27ba49f1 | 172 | return py_exc_to_status_clear(self_component_class, NULL, NULL, NULL, |
5fa16feb PP |
173 | active_log_level); |
174 | } | |
175 | ||
176 | static | |
27ba49f1 | 177 | int py_exc_to_status_component_clear(bt_self_component *self_component) |
5fa16feb | 178 | { |
27ba49f1 | 179 | return py_exc_to_status_clear(NULL, self_component, NULL, NULL, -1); |
5fa16feb PP |
180 | } |
181 | ||
182 | static | |
27ba49f1 | 183 | int py_exc_to_status_message_iterator_clear( |
5fa16feb PP |
184 | bt_self_message_iterator *self_message_iterator) |
185 | { | |
27ba49f1 | 186 | return py_exc_to_status_clear(NULL, NULL, self_message_iterator, NULL, -1); |
5fa16feb PP |
187 | } |
188 | ||
b20382e2 PP |
189 | static |
190 | bool bt_bt2_is_python_component_class(const bt_component_class *comp_cls) | |
191 | { | |
192 | return bt_g_hash_table_contains(bt_cc_ptr_to_py_cls, comp_cls); | |
193 | } | |
194 | ||
5fa16feb PP |
195 | /* Component class proxy methods (delegate to the attached Python object) */ |
196 | ||
197 | static | |
4175c1d5 | 198 | bt_component_class_initialize_method_status component_class_init( |
5fa16feb PP |
199 | bt_self_component *self_component, |
200 | void *self_component_v, | |
201 | swig_type_info *self_comp_cls_type_swig_type, | |
202 | const bt_value *params, | |
203 | void *init_method_data) | |
204 | { | |
205 | const bt_component *component = bt_self_component_as_component(self_component); | |
206 | const bt_component_class *component_class = bt_component_borrow_class_const(component); | |
7f3ee93d | 207 | bt_component_class_initialize_method_status status; |
5fa16feb PP |
208 | PyObject *py_cls = NULL; |
209 | PyObject *py_comp = NULL; | |
210 | PyObject *py_params_ptr = NULL; | |
211 | PyObject *py_comp_ptr = NULL; | |
212 | bt_logging_level log_level = get_self_component_log_level( | |
213 | self_component); | |
214 | ||
5fa16feb PP |
215 | BT_ASSERT(self_component); |
216 | BT_ASSERT(self_component_v); | |
217 | BT_ASSERT(self_comp_cls_type_swig_type); | |
218 | ||
219 | /* | |
220 | * Get the user-defined Python class which created this | |
221 | * component's class in the first place (borrowed | |
222 | * reference). | |
223 | */ | |
224 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
225 | if (!py_cls) { | |
226 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
227 | "Cannot find Python class associated to native component class: " | |
228 | "comp-cls-addr=%p", component_class); | |
229 | goto error; | |
230 | } | |
231 | ||
232 | /* Parameters pointer -> SWIG pointer Python object */ | |
233 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
234 | SWIGTYPE_p_bt_value, 0); | |
235 | if (!py_params_ptr) { | |
236 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
237 | "Failed to create a SWIG pointer object."); | |
238 | goto error; | |
239 | } | |
240 | ||
241 | py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), | |
242 | self_comp_cls_type_swig_type, 0); | |
243 | if (!py_comp_ptr) { | |
244 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
245 | "Failed to create a SWIG pointer object."); | |
246 | goto error; | |
247 | } | |
248 | ||
249 | /* | |
250 | * Do the equivalent of this: | |
251 | * | |
b20382e2 PP |
252 | * py_comp = py_cls._bt_init_from_native(py_comp_ptr, |
253 | * py_params_ptr, init_method_data ? init_method_data : Py_None) | |
5fa16feb PP |
254 | * |
255 | * _UserComponentType._bt_init_from_native() calls the Python | |
256 | * component object's __init__() function. | |
b20382e2 PP |
257 | * |
258 | * We don't take any reference on `init_method_data` which, if | |
259 | * not `NULL`, is assumed to be a `PyObject *`: the user's | |
260 | * __init__() function will eventually take a reference if | |
261 | * needed. If `init_method_data` is `NULL`, then we pass | |
262 | * `Py_None` as the initialization's Python object. | |
5fa16feb PP |
263 | */ |
264 | py_comp = PyObject_CallMethod(py_cls, | |
b20382e2 PP |
265 | "_bt_init_from_native", "(OOO)", py_comp_ptr, py_params_ptr, |
266 | init_method_data ? init_method_data : Py_None); | |
5fa16feb PP |
267 | if (!py_comp) { |
268 | BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, | |
269 | "Failed to call Python class's _bt_init_from_native() method: " | |
270 | "py-cls-addr=%p", py_cls); | |
27ba49f1 | 271 | status = py_exc_to_status_component_clear(self_component); |
5fa16feb PP |
272 | goto end; |
273 | } | |
274 | ||
275 | /* | |
276 | * Our user Python component object is now fully created and | |
277 | * initialized by the user. Since we just created it, this | |
278 | * native component is its only (persistent) owner. | |
279 | */ | |
280 | bt_self_component_set_data(self_component, py_comp); | |
281 | py_comp = NULL; | |
7f3ee93d SM |
282 | |
283 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
284 | ||
5fa16feb PP |
285 | goto end; |
286 | ||
287 | error: | |
7f3ee93d SM |
288 | /* This error path is for non-Python errors only. */ |
289 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
5fa16feb PP |
290 | |
291 | end: | |
7f3ee93d | 292 | BT_ASSERT(!PyErr_Occurred()); |
5fa16feb PP |
293 | Py_XDECREF(py_comp); |
294 | Py_XDECREF(py_params_ptr); | |
295 | Py_XDECREF(py_comp_ptr); | |
296 | return status; | |
297 | } | |
298 | ||
ed68d910 PP |
299 | static |
300 | bt_component_class_get_supported_mip_versions_method_status | |
301 | component_class_get_supported_mip_versions( | |
302 | const bt_component_class *component_class, | |
303 | bt_self_component_class *self_component_class, | |
304 | const bt_value *params, void *init_method_data, | |
305 | bt_logging_level log_level, | |
306 | bt_integer_range_set_unsigned *supported_versions) | |
307 | { | |
308 | uint64_t i; | |
309 | PyObject *py_cls = NULL; | |
310 | PyObject *py_params_ptr = NULL; | |
311 | PyObject *py_range_set_addr = NULL; | |
312 | bt_integer_range_set_unsigned *ret_range_set = NULL; | |
7f3ee93d | 313 | bt_component_class_get_supported_mip_versions_method_status status; |
ed68d910 PP |
314 | |
315 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
316 | if (!py_cls) { | |
317 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
318 | "Cannot find Python class associated to native component class: " | |
319 | "comp-cls-addr=%p", component_class); | |
320 | goto error; | |
321 | } | |
322 | ||
323 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
324 | SWIGTYPE_p_bt_value, 0); | |
325 | if (!py_params_ptr) { | |
326 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
327 | "Failed to create a SWIG pointer object."); | |
328 | goto error; | |
329 | } | |
330 | ||
331 | /* | |
332 | * We don't take any reference on `init_method_data` which, if | |
333 | * not `NULL`, is assumed to be a `PyObject *`: the user's | |
334 | * _user_get_supported_mip_versions() function will eventually | |
335 | * take a reference if needed. If `init_method_data` is `NULL`, | |
336 | * then we pass `Py_None` as the initialization's Python object. | |
337 | */ | |
338 | py_range_set_addr = PyObject_CallMethod(py_cls, | |
339 | "_bt_get_supported_mip_versions_from_native", "(OOi)", | |
340 | py_params_ptr, init_method_data ? init_method_data : Py_None, | |
341 | (int) log_level); | |
342 | if (!py_range_set_addr) { | |
343 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG, | |
344 | "Failed to call Python class's _bt_get_supported_mip_versions_from_native() method: " | |
345 | "py-cls-addr=%p", py_cls); | |
27ba49f1 | 346 | status = py_exc_to_status_component_class_clear(self_component_class, |
ed68d910 PP |
347 | log_level); |
348 | goto end; | |
349 | } | |
350 | ||
351 | /* | |
352 | * The returned object, on success, is an integer object | |
353 | * (PyLong) containing the address of a BT unsigned integer | |
354 | * range set object (new reference). | |
355 | */ | |
356 | ret_range_set = PyLong_AsVoidPtr(py_range_set_addr); | |
357 | BT_ASSERT(!PyErr_Occurred()); | |
358 | BT_ASSERT(ret_range_set); | |
359 | ||
360 | /* Copy returned ranges to input range set */ | |
361 | for (i = 0; i < bt_integer_range_set_get_range_count( | |
362 | bt_integer_range_set_unsigned_as_range_set_const(ret_range_set)); | |
363 | i++) { | |
364 | const bt_integer_range_unsigned *range = | |
365 | bt_integer_range_set_unsigned_borrow_range_by_index_const( | |
366 | ret_range_set, i); | |
367 | bt_integer_range_set_add_range_status add_range_status; | |
368 | ||
369 | add_range_status = bt_integer_range_set_unsigned_add_range( | |
370 | supported_versions, | |
371 | bt_integer_range_unsigned_get_lower(range), | |
372 | bt_integer_range_unsigned_get_upper(range)); | |
373 | if (add_range_status) { | |
374 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
375 | "Failed to add range to supported MIP versions range set."); | |
376 | goto error; | |
377 | } | |
378 | } | |
379 | ||
7f3ee93d SM |
380 | status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK; |
381 | ||
ed68d910 PP |
382 | goto end; |
383 | ||
384 | error: | |
7f3ee93d SM |
385 | /* This error path is for non-Python errors only. */ |
386 | status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR; | |
ed68d910 PP |
387 | |
388 | end: | |
7f3ee93d | 389 | BT_ASSERT(!PyErr_Occurred()); |
ed68d910 PP |
390 | Py_XDECREF(py_params_ptr); |
391 | Py_XDECREF(py_range_set_addr); | |
392 | bt_integer_range_set_unsigned_put_ref(ret_range_set); | |
393 | return status; | |
394 | } | |
395 | ||
396 | static | |
397 | bt_component_class_get_supported_mip_versions_method_status | |
398 | component_class_source_get_supported_mip_versions( | |
399 | bt_self_component_class_source *self_component_class_source, | |
400 | const bt_value *params, void *init_method_data, | |
401 | bt_logging_level log_level, | |
402 | bt_integer_range_set_unsigned *supported_versions) | |
403 | { | |
404 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); | |
405 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); | |
406 | bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source); | |
407 | ||
408 | return component_class_get_supported_mip_versions( | |
409 | component_class, self_component_class, | |
410 | params, init_method_data, log_level, supported_versions); | |
411 | } | |
412 | ||
413 | static | |
414 | bt_component_class_get_supported_mip_versions_method_status | |
415 | component_class_filter_get_supported_mip_versions( | |
416 | bt_self_component_class_filter *self_component_class_filter, | |
417 | const bt_value *params, void *init_method_data, | |
418 | bt_logging_level log_level, | |
419 | bt_integer_range_set_unsigned *supported_versions) | |
420 | { | |
421 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); | |
422 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); | |
423 | bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter); | |
424 | ||
425 | return component_class_get_supported_mip_versions( | |
426 | component_class, self_component_class, | |
427 | params, init_method_data, log_level, supported_versions); | |
428 | } | |
429 | ||
430 | static | |
431 | bt_component_class_get_supported_mip_versions_method_status | |
432 | component_class_sink_get_supported_mip_versions( | |
433 | bt_self_component_class_sink *self_component_class_sink, | |
434 | const bt_value *params, void *init_method_data, | |
435 | bt_logging_level log_level, | |
436 | bt_integer_range_set_unsigned *supported_versions) | |
437 | { | |
438 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); | |
439 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); | |
440 | bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink); | |
441 | ||
442 | return component_class_get_supported_mip_versions( | |
443 | component_class, self_component_class, | |
444 | params, init_method_data, log_level, supported_versions); | |
445 | } | |
446 | ||
5fa16feb PP |
447 | /* |
448 | * Method of bt_component_class_source to initialize a bt_self_component_source | |
449 | * of that class. | |
450 | */ | |
451 | ||
452 | static | |
4175c1d5 | 453 | bt_component_class_initialize_method_status component_class_source_init( |
5fa16feb | 454 | bt_self_component_source *self_component_source, |
e3250e61 | 455 | bt_self_component_source_configuration *config, |
5fa16feb PP |
456 | const bt_value *params, void *init_method_data) |
457 | { | |
458 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
459 | return component_class_init( | |
460 | self_component, | |
461 | self_component_source, | |
462 | SWIGTYPE_p_bt_self_component_source, | |
463 | params, init_method_data); | |
464 | } | |
465 | ||
466 | static | |
4175c1d5 | 467 | bt_component_class_initialize_method_status component_class_filter_init( |
5fa16feb | 468 | bt_self_component_filter *self_component_filter, |
e3250e61 | 469 | bt_self_component_filter_configuration *config, |
5fa16feb PP |
470 | const bt_value *params, void *init_method_data) |
471 | { | |
472 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
473 | return component_class_init( | |
474 | self_component, | |
475 | self_component_filter, | |
476 | SWIGTYPE_p_bt_self_component_filter, | |
477 | params, init_method_data); | |
478 | } | |
479 | ||
480 | static | |
4175c1d5 | 481 | bt_component_class_initialize_method_status component_class_sink_init( |
5fa16feb | 482 | bt_self_component_sink *self_component_sink, |
e3250e61 | 483 | bt_self_component_sink_configuration *config, |
5fa16feb PP |
484 | const bt_value *params, void *init_method_data) |
485 | { | |
486 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
487 | return component_class_init( | |
488 | self_component, | |
489 | self_component_sink, | |
490 | SWIGTYPE_p_bt_self_component_sink, | |
491 | params, init_method_data); | |
492 | } | |
493 | ||
494 | static | |
495 | void component_class_finalize(bt_self_component *self_component) | |
496 | { | |
497 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
7f3ee93d SM |
498 | PyObject *py_method_result; |
499 | ||
5fa16feb PP |
500 | BT_ASSERT(py_comp); |
501 | ||
502 | /* Call user's _user_finalize() method */ | |
7f3ee93d SM |
503 | py_method_result = PyObject_CallMethod(py_comp, "_user_finalize", NULL); |
504 | if (!py_method_result) { | |
5fa16feb PP |
505 | bt_logging_level log_level = get_self_component_log_level( |
506 | self_component); | |
507 | ||
7f3ee93d SM |
508 | /* |
509 | * Ignore any exception raised by the _user_finalize() method | |
510 | * because it won't change anything at this point: the component | |
511 | * is being destroyed anyway. | |
512 | */ | |
c7cc9a2a SM |
513 | BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, |
514 | "User component's _user_finalize() method raised an exception: ignoring:"); | |
515 | logw_exception_clear(log_level); | |
516 | ||
7f3ee93d | 517 | goto end; |
5fa16feb PP |
518 | } |
519 | ||
7f3ee93d SM |
520 | BT_ASSERT(py_method_result == Py_None); |
521 | ||
522 | end: | |
5fa16feb PP |
523 | Py_XDECREF(py_method_result); |
524 | Py_DECREF(py_comp); | |
525 | } | |
526 | ||
f002c045 SM |
527 | /* Decref the Python object in the user data associated to `port`. */ |
528 | ||
529 | static | |
530 | void delete_port_user_data(bt_self_component_port *port) | |
531 | { | |
532 | Py_DECREF(bt_self_component_port_get_data(port)); | |
533 | } | |
534 | ||
535 | static | |
536 | void delete_port_input_user_data(bt_self_component_port_input *port_input) | |
537 | { | |
538 | bt_self_component_port *port = | |
539 | bt_self_component_port_input_as_self_component_port(port_input); | |
540 | ||
541 | delete_port_user_data(port); | |
542 | } | |
543 | ||
544 | static | |
545 | void delete_port_output_user_data(bt_self_component_port_output *port_output) | |
546 | { | |
547 | bt_self_component_port *port = | |
548 | bt_self_component_port_output_as_self_component_port(port_output); | |
549 | ||
550 | delete_port_user_data(port); | |
551 | } | |
552 | ||
5fa16feb PP |
553 | static |
554 | void component_class_source_finalize(bt_self_component_source *self_component_source) | |
555 | { | |
f002c045 SM |
556 | uint64_t i; |
557 | bt_self_component *self_component; | |
558 | const bt_component_source *component_source; | |
559 | ||
560 | self_component = bt_self_component_source_as_self_component( | |
561 | self_component_source); | |
562 | component_source = bt_self_component_source_as_component_source( | |
563 | self_component_source); | |
564 | ||
5fa16feb | 565 | component_class_finalize(self_component); |
f002c045 SM |
566 | |
567 | /* | |
568 | * Free the user data Python object attached to the port. The | |
569 | * corresponding incref was done by the `void *` typemap in | |
570 | * native_bt_port.i. | |
571 | */ | |
572 | for (i = 0; i < bt_component_source_get_output_port_count(component_source); i++) { | |
573 | bt_self_component_port_output *port_output; | |
574 | ||
575 | port_output = bt_self_component_source_borrow_output_port_by_index( | |
576 | self_component_source, i); | |
577 | ||
578 | delete_port_output_user_data(port_output); | |
579 | } | |
5fa16feb PP |
580 | } |
581 | ||
582 | static | |
583 | void component_class_filter_finalize(bt_self_component_filter *self_component_filter) | |
584 | { | |
f002c045 SM |
585 | uint64_t i; |
586 | bt_self_component *self_component; | |
587 | const bt_component_filter *component_filter; | |
588 | ||
589 | self_component = bt_self_component_filter_as_self_component( | |
590 | self_component_filter); | |
591 | component_filter = bt_self_component_filter_as_component_filter( | |
592 | self_component_filter); | |
593 | ||
5fa16feb | 594 | component_class_finalize(self_component); |
f002c045 SM |
595 | |
596 | /* | |
597 | * Free the user data Python object attached to the port. The | |
598 | * corresponding incref was done by the `void *` typemap in | |
599 | * native_bt_port.i. | |
600 | */ | |
601 | for (i = 0; i < bt_component_filter_get_input_port_count(component_filter); i++) { | |
602 | bt_self_component_port_input *port_input; | |
603 | ||
604 | port_input = bt_self_component_filter_borrow_input_port_by_index( | |
605 | self_component_filter, i); | |
606 | ||
607 | delete_port_input_user_data(port_input); | |
608 | } | |
609 | ||
610 | for (i = 0; i < bt_component_filter_get_output_port_count(component_filter); i++) { | |
611 | bt_self_component_port_output *port_output; | |
612 | ||
613 | port_output = bt_self_component_filter_borrow_output_port_by_index( | |
614 | self_component_filter, i); | |
615 | ||
616 | delete_port_output_user_data(port_output); | |
617 | } | |
5fa16feb PP |
618 | } |
619 | ||
620 | static | |
621 | void component_class_sink_finalize(bt_self_component_sink *self_component_sink) | |
622 | { | |
f002c045 SM |
623 | uint64_t i; |
624 | bt_self_component *self_component; | |
625 | const bt_component_sink *component_sink; | |
626 | ||
627 | self_component = bt_self_component_sink_as_self_component( | |
628 | self_component_sink); | |
629 | component_sink = bt_self_component_sink_as_component_sink( | |
630 | self_component_sink); | |
631 | ||
5fa16feb | 632 | component_class_finalize(self_component); |
f002c045 SM |
633 | |
634 | /* | |
635 | * Free the user data Python object attached to the port. The | |
636 | * corresponding incref was done by the `void *` typemap in | |
637 | * native_bt_port.i. | |
638 | */ | |
639 | for (i = 0; i < bt_component_sink_get_input_port_count(component_sink); i++) { | |
640 | bt_self_component_port_input *port_input; | |
641 | ||
642 | port_input = bt_self_component_sink_borrow_input_port_by_index( | |
643 | self_component_sink, i); | |
644 | ||
645 | delete_port_input_user_data(port_input); | |
646 | } | |
5fa16feb PP |
647 | } |
648 | ||
649 | static | |
68e2deed | 650 | bt_message_iterator_class_can_seek_beginning_method_status |
9e8e8b43 SM |
651 | component_class_can_seek_beginning( |
652 | bt_self_message_iterator *self_message_iterator, bt_bool *can_seek) | |
5fa16feb PP |
653 | { |
654 | PyObject *py_iter; | |
655 | PyObject *py_result = NULL; | |
68e2deed | 656 | bt_message_iterator_class_can_seek_beginning_method_status status; |
5fa16feb | 657 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); |
7f3ee93d | 658 | |
5fa16feb PP |
659 | BT_ASSERT(py_iter); |
660 | ||
5c836b95 SM |
661 | py_result = PyObject_CallMethod(py_iter, |
662 | "_bt_can_seek_beginning_from_native", NULL); | |
7f3ee93d | 663 | if (!py_result) { |
27ba49f1 | 664 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); |
7f3ee93d | 665 | goto end; |
5fa16feb PP |
666 | } |
667 | ||
7f3ee93d SM |
668 | BT_ASSERT(PyBool_Check(py_result)); |
669 | *can_seek = PyObject_IsTrue(py_result); | |
670 | ||
68e2deed | 671 | status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; |
7f3ee93d SM |
672 | |
673 | end: | |
5fa16feb PP |
674 | Py_XDECREF(py_result); |
675 | ||
9e8e8b43 | 676 | return status; |
5fa16feb PP |
677 | } |
678 | ||
679 | static | |
68e2deed | 680 | bt_message_iterator_class_seek_beginning_method_status |
5fa16feb PP |
681 | component_class_seek_beginning(bt_self_message_iterator *self_message_iterator) |
682 | { | |
683 | PyObject *py_iter; | |
684 | PyObject *py_result; | |
68e2deed | 685 | bt_message_iterator_class_seek_beginning_method_status status; |
5fa16feb PP |
686 | |
687 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); | |
688 | BT_ASSERT(py_iter); | |
7f3ee93d SM |
689 | |
690 | py_result = PyObject_CallMethod(py_iter, | |
691 | "_bt_seek_beginning_from_native", | |
5fa16feb | 692 | NULL); |
7f3ee93d SM |
693 | if (!py_result) { |
694 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); | |
695 | goto end; | |
696 | } | |
697 | ||
698 | BT_ASSERT(py_result == Py_None); | |
699 | ||
68e2deed | 700 | status = BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK; |
7f3ee93d SM |
701 | |
702 | end: | |
5fa16feb | 703 | Py_XDECREF(py_result); |
7f3ee93d | 704 | |
5fa16feb PP |
705 | return status; |
706 | } | |
707 | ||
24a68958 | 708 | static |
68e2deed | 709 | bt_message_iterator_class_can_seek_ns_from_origin_method_status |
24a68958 SM |
710 | component_class_can_seek_ns_from_origin( |
711 | bt_self_message_iterator *self_message_iterator, | |
712 | int64_t ns_from_origin, bt_bool *can_seek) | |
713 | { | |
714 | PyObject *py_iter; | |
715 | PyObject *py_result = NULL; | |
68e2deed | 716 | bt_message_iterator_class_can_seek_ns_from_origin_method_status status; |
7f3ee93d | 717 | |
24a68958 SM |
718 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); |
719 | BT_ASSERT(py_iter); | |
720 | ||
721 | py_result = PyObject_CallMethod(py_iter, | |
722 | "_bt_can_seek_ns_from_origin_from_native", "L", ns_from_origin); | |
7f3ee93d | 723 | if (!py_result) { |
27ba49f1 | 724 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); |
7f3ee93d | 725 | goto end; |
24a68958 SM |
726 | } |
727 | ||
7f3ee93d SM |
728 | BT_ASSERT(PyBool_Check(py_result)); |
729 | *can_seek = PyObject_IsTrue(py_result); | |
730 | ||
68e2deed | 731 | status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; |
7f3ee93d SM |
732 | |
733 | end: | |
24a68958 SM |
734 | Py_XDECREF(py_result); |
735 | ||
736 | return status; | |
737 | } | |
738 | ||
739 | static | |
68e2deed | 740 | bt_message_iterator_class_seek_ns_from_origin_method_status |
24a68958 SM |
741 | component_class_seek_ns_from_origin( |
742 | bt_self_message_iterator *self_message_iterator, | |
743 | int64_t ns_from_origin) | |
744 | { | |
745 | PyObject *py_iter; | |
746 | PyObject *py_result; | |
68e2deed | 747 | bt_message_iterator_class_seek_ns_from_origin_method_status status; |
24a68958 SM |
748 | |
749 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); | |
750 | BT_ASSERT(py_iter); | |
7f3ee93d | 751 | |
24a68958 SM |
752 | py_result = PyObject_CallMethod(py_iter, |
753 | "_bt_seek_ns_from_origin_from_native", "L", ns_from_origin); | |
7f3ee93d SM |
754 | if (!py_result) { |
755 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); | |
756 | goto end; | |
757 | } | |
758 | ||
759 | ||
760 | BT_ASSERT(py_result == Py_None); | |
761 | ||
68e2deed | 762 | status = BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; |
7f3ee93d SM |
763 | |
764 | end: | |
24a68958 | 765 | Py_XDECREF(py_result); |
7f3ee93d | 766 | |
24a68958 SM |
767 | return status; |
768 | } | |
769 | ||
5fa16feb PP |
770 | static |
771 | bt_component_class_port_connected_method_status component_class_port_connected( | |
772 | bt_self_component *self_component, | |
773 | void *self_component_port, | |
774 | swig_type_info *self_component_port_swig_type, | |
775 | bt_port_type self_component_port_type, | |
776 | const void *other_port, | |
777 | swig_type_info *other_port_swig_type) | |
778 | { | |
779 | bt_component_class_port_connected_method_status status; | |
780 | PyObject *py_comp = NULL; | |
781 | PyObject *py_self_port_ptr = NULL; | |
782 | PyObject *py_other_port_ptr = NULL; | |
783 | PyObject *py_method_result = NULL; | |
784 | bt_logging_level log_level = get_self_component_log_level( | |
785 | self_component); | |
786 | ||
787 | py_comp = bt_self_component_get_data(self_component); | |
788 | BT_ASSERT(py_comp); | |
789 | py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), | |
790 | self_component_port_swig_type, 0); | |
791 | if (!py_self_port_ptr) { | |
792 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
793 | "Failed to create a SWIG pointer object."); | |
794 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
795 | goto end; | |
796 | } | |
797 | ||
798 | py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), | |
799 | other_port_swig_type, 0); | |
800 | if (!py_other_port_ptr) { | |
801 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
802 | "Failed to create a SWIG pointer object."); | |
803 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
7f3ee93d SM |
804 | goto end; |
805 | } | |
5fa16feb PP |
806 | |
807 | py_method_result = PyObject_CallMethod(py_comp, | |
808 | "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr, | |
809 | self_component_port_type, py_other_port_ptr); | |
7f3ee93d SM |
810 | if (!py_method_result) { |
811 | status = py_exc_to_status_component_clear(self_component); | |
812 | goto end; | |
813 | } | |
814 | ||
815 | BT_ASSERT(py_method_result == Py_None); | |
816 | ||
817 | status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; | |
5fa16feb PP |
818 | |
819 | end: | |
820 | Py_XDECREF(py_self_port_ptr); | |
821 | Py_XDECREF(py_other_port_ptr); | |
822 | Py_XDECREF(py_method_result); | |
7f3ee93d | 823 | |
5fa16feb PP |
824 | return status; |
825 | } | |
826 | ||
827 | static | |
828 | bt_component_class_port_connected_method_status | |
829 | component_class_source_output_port_connected( | |
830 | bt_self_component_source *self_component_source, | |
831 | bt_self_component_port_output *self_component_port_output, | |
832 | const bt_port_input *other_port_input) | |
833 | { | |
834 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
835 | ||
836 | return component_class_port_connected( | |
837 | self_component, | |
838 | self_component_port_output, | |
839 | SWIGTYPE_p_bt_self_component_port_output, | |
840 | BT_PORT_TYPE_OUTPUT, | |
841 | other_port_input, | |
842 | SWIGTYPE_p_bt_port_input); | |
843 | } | |
844 | ||
845 | static | |
846 | bt_component_class_port_connected_method_status | |
847 | component_class_filter_input_port_connected( | |
848 | bt_self_component_filter *self_component_filter, | |
849 | bt_self_component_port_input *self_component_port_input, | |
850 | const bt_port_output *other_port_output) | |
851 | { | |
852 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
853 | ||
854 | return component_class_port_connected( | |
855 | self_component, | |
856 | self_component_port_input, | |
857 | SWIGTYPE_p_bt_self_component_port_input, | |
858 | BT_PORT_TYPE_INPUT, | |
859 | other_port_output, | |
860 | SWIGTYPE_p_bt_port_output); | |
861 | } | |
862 | ||
863 | static | |
864 | bt_component_class_port_connected_method_status | |
865 | component_class_filter_output_port_connected( | |
866 | bt_self_component_filter *self_component_filter, | |
867 | bt_self_component_port_output *self_component_port_output, | |
868 | const bt_port_input *other_port_input) | |
869 | { | |
870 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
871 | ||
872 | return component_class_port_connected( | |
873 | self_component, | |
874 | self_component_port_output, | |
875 | SWIGTYPE_p_bt_self_component_port_output, | |
876 | BT_PORT_TYPE_OUTPUT, | |
877 | other_port_input, | |
878 | SWIGTYPE_p_bt_port_input); | |
879 | } | |
880 | ||
881 | static | |
882 | bt_component_class_port_connected_method_status | |
883 | component_class_sink_input_port_connected( | |
884 | bt_self_component_sink *self_component_sink, | |
885 | bt_self_component_port_input *self_component_port_input, | |
886 | const bt_port_output *other_port_output) | |
887 | { | |
888 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
889 | ||
890 | return component_class_port_connected( | |
891 | self_component, | |
892 | self_component_port_input, | |
893 | SWIGTYPE_p_bt_self_component_port_input, | |
894 | BT_PORT_TYPE_INPUT, | |
895 | other_port_output, | |
896 | SWIGTYPE_p_bt_port_output); | |
897 | } | |
898 | ||
899 | static | |
900 | bt_component_class_sink_graph_is_configured_method_status | |
901 | component_class_sink_graph_is_configured( | |
902 | bt_self_component_sink *self_component_sink) | |
903 | { | |
904 | PyObject *py_comp = NULL; | |
905 | PyObject *py_method_result = NULL; | |
7f3ee93d | 906 | bt_component_class_sink_graph_is_configured_method_status status; |
5fa16feb PP |
907 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
908 | ||
909 | py_comp = bt_self_component_get_data(self_component); | |
7f3ee93d | 910 | |
5fa16feb PP |
911 | py_method_result = PyObject_CallMethod(py_comp, |
912 | "_bt_graph_is_configured_from_native", NULL); | |
7f3ee93d SM |
913 | if (!py_method_result) { |
914 | status = py_exc_to_status_component_clear(self_component); | |
915 | goto end; | |
916 | } | |
917 | ||
918 | BT_ASSERT(py_method_result == Py_None); | |
919 | ||
920 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;; | |
921 | ||
922 | end: | |
5fa16feb PP |
923 | Py_XDECREF(py_method_result); |
924 | return status; | |
925 | } | |
926 | ||
927 | static | |
928 | bt_component_class_query_method_status component_class_query( | |
929 | const bt_component_class *component_class, | |
930 | bt_self_component_class *self_component_class, | |
931 | bt_private_query_executor *priv_query_executor, | |
f6e305ce | 932 | const char *object, const bt_value *params, void *method_data, |
5fa16feb PP |
933 | const bt_value **result) |
934 | { | |
935 | PyObject *py_cls = NULL; | |
936 | PyObject *py_params_ptr = NULL; | |
937 | PyObject *py_priv_query_exec_ptr = NULL; | |
938 | PyObject *py_query_func = NULL; | |
939 | PyObject *py_object = NULL; | |
940 | PyObject *py_results_addr = NULL; | |
941 | bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK; | |
942 | const bt_query_executor *query_exec = | |
943 | bt_private_query_executor_as_query_executor_const( | |
944 | priv_query_executor); | |
945 | bt_logging_level log_level = | |
946 | bt_query_executor_get_logging_level(query_exec); | |
947 | ||
f6e305ce PP |
948 | /* |
949 | * If there's any `method_data`, assume this component class is | |
950 | * getting queried from Python, so that `method_data` is a | |
951 | * Python object to pass to the user's _user_query() method. | |
952 | */ | |
953 | BT_ASSERT(!method_data || | |
954 | bt_bt2_is_python_component_class(component_class)); | |
955 | ||
5fa16feb PP |
956 | py_cls = lookup_cc_ptr_to_py_cls(component_class); |
957 | if (!py_cls) { | |
958 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
959 | "Cannot find Python class associated to native component class: " | |
960 | "comp-cls-addr=%p", component_class); | |
961 | goto error; | |
962 | } | |
963 | ||
964 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
965 | SWIGTYPE_p_bt_value, 0); | |
966 | if (!py_params_ptr) { | |
967 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
968 | "Failed to create a SWIG pointer object."); | |
969 | goto error; | |
970 | } | |
971 | ||
972 | py_priv_query_exec_ptr = SWIG_NewPointerObj( | |
973 | SWIG_as_voidptr(priv_query_executor), | |
974 | SWIGTYPE_p_bt_private_query_executor, 0); | |
975 | if (!py_priv_query_exec_ptr) { | |
976 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
977 | "Failed to create a SWIG pointer object."); | |
978 | goto error; | |
979 | } | |
980 | ||
981 | py_object = SWIG_FromCharPtr(object); | |
982 | if (!py_object) { | |
983 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
984 | "Failed to create a Python string."); | |
985 | goto error; | |
986 | } | |
987 | ||
f6e305ce PP |
988 | /* |
989 | * We don't take any reference on `method_data` which, if not | |
990 | * `NULL`, is assumed to be a `PyObject *`: the user's | |
991 | * _user_query() function will eventually take a reference if | |
992 | * needed. If `method_data` is `NULL`, then we pass `Py_None` as | |
993 | * the initialization's Python object. | |
994 | */ | |
5fa16feb | 995 | py_results_addr = PyObject_CallMethod(py_cls, |
f6e305ce PP |
996 | "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr, |
997 | py_object, py_params_ptr, | |
998 | method_data ? method_data : Py_None); | |
5fa16feb | 999 | if (!py_results_addr) { |
27ba49f1 | 1000 | status = py_exc_to_status_component_class_clear(self_component_class, |
5fa16feb | 1001 | log_level); |
afe3f8ce SM |
1002 | if (status < 0) { |
1003 | static const char *fmt = | |
1004 | "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p"; | |
1005 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG, | |
1006 | fmt, py_cls); | |
1007 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( | |
1008 | self_component_class, fmt, py_cls); | |
1009 | } | |
5fa16feb PP |
1010 | goto end; |
1011 | } | |
1012 | ||
1013 | /* | |
1014 | * The returned object, on success, is an integer object | |
1015 | * (PyLong) containing the address of a BT value object (new | |
1016 | * reference). | |
1017 | */ | |
1018 | *result = PyLong_AsVoidPtr(py_results_addr); | |
1019 | BT_ASSERT(!PyErr_Occurred()); | |
1020 | BT_ASSERT(*result); | |
1021 | goto end; | |
1022 | ||
1023 | error: | |
1024 | PyErr_Clear(); | |
1025 | status = __BT_FUNC_STATUS_ERROR; | |
1026 | ||
1027 | end: | |
1028 | Py_XDECREF(py_params_ptr); | |
1029 | Py_XDECREF(py_priv_query_exec_ptr); | |
1030 | Py_XDECREF(py_query_func); | |
1031 | Py_XDECREF(py_object); | |
1032 | Py_XDECREF(py_results_addr); | |
1033 | return status; | |
1034 | } | |
1035 | ||
1036 | static | |
1037 | bt_component_class_query_method_status component_class_source_query( | |
1038 | bt_self_component_class_source *self_component_class_source, | |
1039 | bt_private_query_executor *priv_query_executor, | |
f6e305ce | 1040 | const char *object, const bt_value *params, void *method_data, |
5fa16feb PP |
1041 | const bt_value **result) |
1042 | { | |
1043 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); | |
1044 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); | |
1045 | bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source); | |
1046 | ||
1047 | return component_class_query(component_class, self_component_class, | |
f6e305ce | 1048 | priv_query_executor, object, params, method_data, result); |
5fa16feb PP |
1049 | } |
1050 | ||
1051 | static | |
1052 | bt_component_class_query_method_status component_class_filter_query( | |
1053 | bt_self_component_class_filter *self_component_class_filter, | |
1054 | bt_private_query_executor *priv_query_executor, | |
f6e305ce | 1055 | const char *object, const bt_value *params, void *method_data, |
5fa16feb PP |
1056 | const bt_value **result) |
1057 | { | |
1058 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); | |
1059 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); | |
1060 | bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter); | |
1061 | ||
1062 | return component_class_query(component_class, self_component_class, | |
f6e305ce | 1063 | priv_query_executor, object, params, method_data, result); |
5fa16feb PP |
1064 | } |
1065 | ||
1066 | static | |
1067 | bt_component_class_query_method_status component_class_sink_query( | |
1068 | bt_self_component_class_sink *self_component_class_sink, | |
1069 | bt_private_query_executor *priv_query_executor, | |
f6e305ce | 1070 | const char *object, const bt_value *params, void *method_data, |
5fa16feb PP |
1071 | const bt_value **result) |
1072 | { | |
1073 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); | |
1074 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); | |
1075 | bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink); | |
1076 | ||
1077 | return component_class_query(component_class, self_component_class, | |
f6e305ce | 1078 | priv_query_executor, object, params, method_data, result); |
5fa16feb PP |
1079 | } |
1080 | ||
1081 | static | |
68e2deed | 1082 | bt_message_iterator_class_initialize_method_status |
5fa16feb PP |
1083 | component_class_message_iterator_init( |
1084 | bt_self_message_iterator *self_message_iterator, | |
9415de1c | 1085 | bt_self_message_iterator_configuration *config, |
5fa16feb PP |
1086 | bt_self_component_port_output *self_component_port_output) |
1087 | { | |
68e2deed | 1088 | bt_message_iterator_class_initialize_method_status status = __BT_FUNC_STATUS_OK; |
5fa16feb PP |
1089 | PyObject *py_comp_cls = NULL; |
1090 | PyObject *py_iter_cls = NULL; | |
1091 | PyObject *py_iter_ptr = NULL; | |
9415de1c | 1092 | PyObject *py_config_ptr = NULL; |
5fa16feb PP |
1093 | PyObject *py_component_port_output_ptr = NULL; |
1094 | PyObject *py_init_method_result = NULL; | |
1095 | PyObject *py_iter = NULL; | |
1096 | PyObject *py_comp; | |
e1b15e55 PP |
1097 | bt_self_component *self_component = |
1098 | bt_self_message_iterator_borrow_component( | |
1099 | self_message_iterator); | |
5fa16feb PP |
1100 | bt_logging_level log_level = get_self_component_log_level( |
1101 | self_component); | |
1102 | ||
1103 | py_comp = bt_self_component_get_data(self_component); | |
1104 | ||
1105 | /* Find user's Python message iterator class */ | |
1106 | py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); | |
1107 | if (!py_comp_cls) { | |
1108 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1109 | "Cannot get Python object's `__class__` attribute."); | |
1110 | goto python_error; | |
1111 | } | |
1112 | ||
1113 | py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); | |
1114 | if (!py_iter_cls) { | |
1115 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1116 | "Cannot get Python class's `_iter_cls` attribute."); | |
1117 | goto python_error; | |
1118 | } | |
1119 | ||
1120 | py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), | |
1121 | SWIGTYPE_p_bt_self_message_iterator, 0); | |
1122 | if (!py_iter_ptr) { | |
1123 | const char *err = "Failed to create a SWIG pointer object."; | |
1124 | ||
1125 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1126 | "%s", err); | |
1127 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( | |
1128 | self_message_iterator, err); | |
1129 | goto error; | |
1130 | } | |
1131 | ||
1132 | /* | |
1133 | * Create object with borrowed native message iterator | |
1134 | * reference: | |
1135 | * | |
1136 | * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) | |
1137 | */ | |
1138 | py_iter = PyObject_CallMethod(py_iter_cls, "__new__", | |
1139 | "(OO)", py_iter_cls, py_iter_ptr); | |
1140 | if (!py_iter) { | |
1141 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1142 | "Failed to call Python class's __new__() method: " | |
1143 | "py-cls-addr=%p", py_iter_cls); | |
1144 | goto python_error; | |
1145 | } | |
1146 | ||
1147 | /* | |
1148 | * Initialize object: | |
1149 | * | |
9415de1c | 1150 | * py_iter.__init__(config, self_output_port) |
5fa16feb | 1151 | * |
9415de1c | 1152 | * through the _init_from_native helper static method. |
5fa16feb PP |
1153 | * |
1154 | * At this point, py_iter._ptr is set, so this initialization | |
1155 | * function has access to self._component (which gives it the | |
1156 | * user Python component object from which the iterator was | |
1157 | * created). | |
1158 | */ | |
9415de1c SM |
1159 | py_config_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(config), |
1160 | SWIGTYPE_p_bt_self_message_iterator_configuration, 0); | |
1161 | if (!py_config_ptr) { | |
1162 | const char *err = "Failed to create a SWIG pointer object"; | |
1163 | ||
1164 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1165 | "%s", err); | |
1166 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( | |
1167 | self_message_iterator, err); | |
1168 | goto error; | |
1169 | } | |
1170 | ||
5fa16feb PP |
1171 | py_component_port_output_ptr = SWIG_NewPointerObj( |
1172 | SWIG_as_voidptr(self_component_port_output), | |
1173 | SWIGTYPE_p_bt_self_component_port_output, 0); | |
1174 | if (!py_component_port_output_ptr) { | |
1175 | const char *err = "Failed to create a SWIG pointer object."; | |
1176 | ||
1177 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1178 | "%s", err); | |
1179 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( | |
1180 | self_message_iterator, err); | |
1181 | goto error; | |
1182 | } | |
1183 | ||
1184 | py_init_method_result = PyObject_CallMethod(py_iter, | |
9415de1c SM |
1185 | "_bt_init_from_native", "OO", py_config_ptr, |
1186 | py_component_port_output_ptr); | |
5fa16feb PP |
1187 | if (!py_init_method_result) { |
1188 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1189 | "User's __init__() method failed:"); | |
1190 | goto python_error; | |
1191 | } | |
1192 | ||
1193 | /* | |
1194 | * Since the Python code can never instantiate a user-defined | |
1195 | * message iterator class, the native message iterator | |
1196 | * object does NOT belong to a user Python message iterator | |
1197 | * object (borrowed reference). However this Python object is | |
1198 | * owned by this native message iterator object. | |
1199 | * | |
1200 | * In the Python world, the lifetime of the native message | |
1201 | * iterator is managed by a _GenericMessageIterator | |
1202 | * instance: | |
1203 | * | |
1204 | * _GenericMessageIterator instance: | |
1205 | * owns a native bt_message_iterator object (iter) | |
1206 | * owns a _UserMessageIterator instance (py_iter) | |
1207 | * self._ptr is a borrowed reference to the | |
1208 | * native bt_private_connection_private_message_iterator | |
1209 | * object (iter) | |
1210 | */ | |
1211 | bt_self_message_iterator_set_data(self_message_iterator, py_iter); | |
1212 | py_iter = NULL; | |
1213 | goto end; | |
1214 | ||
1215 | python_error: | |
1216 | /* Handling of errors that cause a Python exception to be set. */ | |
27ba49f1 | 1217 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); |
5fa16feb PP |
1218 | goto end; |
1219 | ||
1220 | error: | |
1221 | /* Handling of errors that don't cause a Python exception to be set. */ | |
1222 | status = __BT_FUNC_STATUS_ERROR; | |
1223 | ||
1224 | end: | |
1225 | BT_ASSERT(!PyErr_Occurred()); | |
1226 | ||
1227 | Py_XDECREF(py_comp_cls); | |
1228 | Py_XDECREF(py_iter_cls); | |
1229 | Py_XDECREF(py_iter_ptr); | |
1230 | Py_XDECREF(py_component_port_output_ptr); | |
1231 | Py_XDECREF(py_init_method_result); | |
1232 | Py_XDECREF(py_iter); | |
1233 | return status; | |
1234 | } | |
1235 | ||
5fa16feb PP |
1236 | static |
1237 | void component_class_message_iterator_finalize( | |
1238 | bt_self_message_iterator *message_iterator) | |
1239 | { | |
1240 | PyObject *py_message_iter = bt_self_message_iterator_get_data( | |
1241 | message_iterator); | |
1242 | PyObject *py_method_result = NULL; | |
1243 | ||
1244 | BT_ASSERT(py_message_iter); | |
1245 | ||
1246 | /* Call user's _user_finalize() method */ | |
1247 | py_method_result = PyObject_CallMethod(py_message_iter, | |
1248 | "_user_finalize", NULL); | |
7f3ee93d | 1249 | if (!py_method_result) { |
5fa16feb PP |
1250 | bt_self_component *self_comp = |
1251 | bt_self_message_iterator_borrow_component( | |
1252 | message_iterator); | |
1253 | bt_logging_level log_level = get_self_component_log_level( | |
1254 | self_comp); | |
1255 | ||
7f3ee93d SM |
1256 | /* |
1257 | * Ignore any exception raised by the _user_finalize() method | |
1258 | * because it won't change anything at this point: the component | |
1259 | * is being destroyed anyway. | |
1260 | */ | |
c7cc9a2a SM |
1261 | BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp, |
1262 | "User's _user_finalize() method raised an exception: ignoring:"); | |
1263 | logw_exception_clear(get_self_message_iterator_log_level( | |
1264 | message_iterator)); | |
5fa16feb PP |
1265 | } |
1266 | ||
5fa16feb PP |
1267 | Py_XDECREF(py_method_result); |
1268 | Py_DECREF(py_message_iter); | |
1269 | } | |
1270 | ||
1271 | /* Valid for both sources and filters. */ | |
1272 | ||
1273 | static | |
68e2deed | 1274 | bt_message_iterator_class_next_method_status |
5fa16feb PP |
1275 | component_class_message_iterator_next( |
1276 | bt_self_message_iterator *message_iterator, | |
1277 | bt_message_array_const msgs, uint64_t capacity, | |
1278 | uint64_t *count) | |
1279 | { | |
68e2deed | 1280 | bt_message_iterator_class_next_method_status status; |
5fa16feb PP |
1281 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); |
1282 | PyObject *py_method_result = NULL; | |
1283 | ||
ec4a3354 | 1284 | BT_ASSERT_DBG(py_message_iter); |
5fa16feb PP |
1285 | py_method_result = PyObject_CallMethod(py_message_iter, |
1286 | "_bt_next_from_native", NULL); | |
1287 | if (!py_method_result) { | |
27ba49f1 | 1288 | status = py_exc_to_status_message_iterator_clear(message_iterator); |
5fa16feb PP |
1289 | goto end; |
1290 | } | |
1291 | ||
1292 | /* | |
1293 | * The returned object, on success, is an integer object | |
1294 | * (PyLong) containing the address of a native message | |
1295 | * object (which is now ours). | |
1296 | */ | |
1297 | msgs[0] = PyLong_AsVoidPtr(py_method_result); | |
1298 | *count = 1; | |
1299 | ||
7f3ee93d | 1300 | /* Overflow errors should never happen. */ |
ec4a3354 | 1301 | BT_ASSERT_DBG(!PyErr_Occurred()); |
7f3ee93d | 1302 | |
68e2deed | 1303 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; |
5fa16feb PP |
1304 | |
1305 | end: | |
1306 | Py_XDECREF(py_method_result); | |
1307 | return status; | |
1308 | } | |
1309 | ||
1310 | static | |
1311 | bt_component_class_sink_consume_method_status | |
1312 | component_class_sink_consume(bt_self_component_sink *self_component_sink) | |
1313 | { | |
1314 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
1315 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
1316 | PyObject *py_method_result = NULL; | |
1317 | bt_component_class_sink_consume_method_status status; | |
1318 | ||
ec4a3354 | 1319 | BT_ASSERT_DBG(py_comp); |
7f3ee93d | 1320 | |
5fa16feb PP |
1321 | py_method_result = PyObject_CallMethod(py_comp, |
1322 | "_user_consume", NULL); | |
7f3ee93d SM |
1323 | if (!py_method_result) { |
1324 | status = py_exc_to_status_component_clear(self_component); | |
1325 | goto end; | |
1326 | } | |
1327 | ||
1328 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; | |
1329 | ||
1330 | end: | |
5fa16feb PP |
1331 | Py_XDECREF(py_method_result); |
1332 | return status; | |
1333 | } | |
1334 | ||
1335 | static | |
1336 | int component_class_set_help_and_desc( | |
1337 | bt_component_class *component_class, | |
1338 | const char *description, const char *help) | |
1339 | { | |
1340 | int ret; | |
1341 | ||
1342 | if (description) { | |
1343 | ret = bt_component_class_set_description(component_class, description); | |
1344 | if (ret) { | |
1345 | BT_LOGE("Cannot set component class's description: " | |
1346 | "comp-cls-addr=%p", component_class); | |
1347 | goto end; | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | if (help) { | |
1352 | ret = bt_component_class_set_help(component_class, help); | |
1353 | if (ret) { | |
1354 | BT_LOGE("Cannot set component class's help text: " | |
1355 | "comp-cls-addr=%p", component_class); | |
1356 | goto end; | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | ret = 0; | |
1361 | ||
1362 | end: | |
1363 | return ret; | |
1364 | } | |
1365 | ||
68e2deed SM |
1366 | static |
1367 | bt_message_iterator_class *create_message_iterator_class(void) | |
1368 | { | |
1369 | bt_message_iterator_class *message_iterator_class; | |
1370 | bt_message_iterator_class_set_method_status ret; | |
1371 | ||
1372 | message_iterator_class = bt_message_iterator_class_create( | |
1373 | component_class_message_iterator_next); | |
1374 | if (!message_iterator_class) { | |
1375 | BT_LOGE_STR("Cannot create message iterator class."); | |
1376 | goto end; | |
1377 | } | |
1378 | ||
1379 | ret = bt_message_iterator_class_set_seek_beginning_methods( | |
1380 | message_iterator_class, component_class_seek_beginning, | |
1381 | component_class_can_seek_beginning); | |
1382 | BT_ASSERT(ret == 0); | |
1383 | ret = bt_message_iterator_class_set_seek_ns_from_origin_methods( | |
1384 | message_iterator_class, component_class_seek_ns_from_origin, | |
1385 | component_class_can_seek_ns_from_origin); | |
1386 | BT_ASSERT(ret == 0); | |
1387 | ret = bt_message_iterator_class_set_initialize_method( | |
1388 | message_iterator_class, component_class_message_iterator_init); | |
1389 | BT_ASSERT(ret == 0); | |
1390 | ret = bt_message_iterator_class_set_finalize_method( | |
1391 | message_iterator_class, component_class_message_iterator_finalize); | |
1392 | BT_ASSERT(ret == 0); | |
1393 | ||
1394 | end: | |
1395 | return message_iterator_class; | |
1396 | } | |
1397 | ||
5fa16feb PP |
1398 | static |
1399 | bt_component_class_source *bt_bt2_component_class_source_create( | |
1400 | PyObject *py_cls, const char *name, const char *description, | |
1401 | const char *help) | |
1402 | { | |
68e2deed SM |
1403 | bt_component_class_source *component_class_source = NULL; |
1404 | bt_message_iterator_class *message_iterator_class; | |
5fa16feb PP |
1405 | bt_component_class *component_class; |
1406 | int ret; | |
1407 | ||
1408 | BT_ASSERT(py_cls); | |
68e2deed SM |
1409 | |
1410 | message_iterator_class = create_message_iterator_class(); | |
1411 | if (!message_iterator_class) { | |
1412 | goto end; | |
1413 | } | |
1414 | ||
5fa16feb | 1415 | component_class_source = bt_component_class_source_create(name, |
68e2deed | 1416 | message_iterator_class); |
5fa16feb PP |
1417 | if (!component_class_source) { |
1418 | BT_LOGE_STR("Cannot create source component class."); | |
1419 | goto end; | |
1420 | } | |
1421 | ||
1422 | component_class = bt_component_class_source_as_component_class(component_class_source); | |
1423 | ||
1424 | if (component_class_set_help_and_desc(component_class, description, help)) { | |
1425 | goto end; | |
1426 | } | |
1427 | ||
4175c1d5 | 1428 | ret = bt_component_class_source_set_initialize_method(component_class_source, component_class_source_init); |
5fa16feb PP |
1429 | BT_ASSERT(ret == 0); |
1430 | ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize); | |
1431 | BT_ASSERT(ret == 0); | |
5fa16feb PP |
1432 | ret = bt_component_class_source_set_output_port_connected_method(component_class_source, |
1433 | component_class_source_output_port_connected); | |
1434 | BT_ASSERT(ret == 0); | |
1435 | ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query); | |
1436 | BT_ASSERT(ret == 0); | |
ed68d910 PP |
1437 | ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions); |
1438 | BT_ASSERT(ret == 0); | |
5fa16feb PP |
1439 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1440 | ||
1441 | end: | |
68e2deed | 1442 | bt_message_iterator_class_put_ref(message_iterator_class); |
5fa16feb PP |
1443 | return component_class_source; |
1444 | } | |
1445 | ||
1446 | static | |
1447 | bt_component_class_filter *bt_bt2_component_class_filter_create( | |
1448 | PyObject *py_cls, const char *name, const char *description, | |
1449 | const char *help) | |
1450 | { | |
68e2deed SM |
1451 | bt_component_class_filter *component_class_filter = NULL; |
1452 | bt_message_iterator_class *message_iterator_class; | |
5fa16feb | 1453 | bt_component_class *component_class; |
5fa16feb PP |
1454 | int ret; |
1455 | ||
1456 | BT_ASSERT(py_cls); | |
68e2deed SM |
1457 | |
1458 | message_iterator_class = create_message_iterator_class(); | |
1459 | if (!message_iterator_class) { | |
1460 | goto end; | |
1461 | } | |
1462 | ||
5fa16feb | 1463 | component_class_filter = bt_component_class_filter_create(name, |
68e2deed | 1464 | message_iterator_class); |
5fa16feb PP |
1465 | if (!component_class_filter) { |
1466 | BT_LOGE_STR("Cannot create filter component class."); | |
1467 | goto end; | |
1468 | } | |
1469 | ||
1470 | component_class = bt_component_class_filter_as_component_class(component_class_filter); | |
1471 | ||
1472 | if (component_class_set_help_and_desc(component_class, description, help)) { | |
1473 | goto end; | |
1474 | } | |
1475 | ||
4175c1d5 | 1476 | ret = bt_component_class_filter_set_initialize_method(component_class_filter, component_class_filter_init); |
5fa16feb PP |
1477 | BT_ASSERT(ret == 0); |
1478 | ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize); | |
1479 | BT_ASSERT(ret == 0); | |
5fa16feb PP |
1480 | ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, |
1481 | component_class_filter_input_port_connected); | |
1482 | BT_ASSERT(ret == 0); | |
1483 | ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter, | |
1484 | component_class_filter_output_port_connected); | |
1485 | BT_ASSERT(ret == 0); | |
1486 | ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query); | |
1487 | BT_ASSERT(ret == 0); | |
ed68d910 PP |
1488 | ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions); |
1489 | BT_ASSERT(ret == 0); | |
5fa16feb PP |
1490 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1491 | ||
1492 | end: | |
68e2deed | 1493 | bt_message_iterator_class_put_ref(message_iterator_class); |
5fa16feb PP |
1494 | return component_class_filter; |
1495 | } | |
1496 | ||
1497 | static | |
1498 | bt_component_class_sink *bt_bt2_component_class_sink_create( | |
1499 | PyObject *py_cls, const char *name, const char *description, | |
1500 | const char *help) | |
1501 | { | |
1502 | bt_component_class_sink *component_class_sink; | |
1503 | bt_component_class *component_class; | |
1504 | int ret; | |
1505 | ||
1506 | BT_ASSERT(py_cls); | |
1507 | component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume); | |
1508 | ||
1509 | if (!component_class_sink) { | |
1510 | BT_LOGE_STR("Cannot create sink component class."); | |
1511 | goto end; | |
1512 | } | |
1513 | ||
1514 | component_class = bt_component_class_sink_as_component_class(component_class_sink); | |
1515 | ||
1516 | if (component_class_set_help_and_desc(component_class, description, help)) { | |
1517 | goto end; | |
1518 | } | |
1519 | ||
4175c1d5 | 1520 | ret = bt_component_class_sink_set_initialize_method(component_class_sink, component_class_sink_init); |
5fa16feb PP |
1521 | BT_ASSERT(ret == 0); |
1522 | ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize); | |
1523 | BT_ASSERT(ret == 0); | |
1524 | ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink, | |
1525 | component_class_sink_input_port_connected); | |
1526 | BT_ASSERT(ret == 0); | |
1527 | ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink, | |
1528 | component_class_sink_graph_is_configured); | |
1529 | BT_ASSERT(ret == 0); | |
1530 | ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query); | |
1531 | BT_ASSERT(ret == 0); | |
ed68d910 PP |
1532 | ret = bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink, component_class_sink_get_supported_mip_versions); |
1533 | BT_ASSERT(ret == 0); | |
5fa16feb PP |
1534 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1535 | ||
1536 | end: | |
1537 | return component_class_sink; | |
1538 | } |