bt2: make can_seek_beginning a method instead of a property
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i.h
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"
26 #include "compat/glib.h"
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
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
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
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);
102 bt_cc_ptr_to_py_cls = NULL;
103 }
104 }
105
106 static inline
107 int py_exc_to_status(bt_self_component_class *self_component_class,
108 bt_self_component *self_component,
109 bt_self_message_iterator *self_message_iterator,
110 const char *module_name, int active_log_level)
111 {
112 int status = __BT_FUNC_STATUS_OK;
113 PyObject *exc = PyErr_Occurred();
114
115 if (!exc) {
116 goto end;
117 }
118
119 if (PyErr_GivenExceptionMatches(exc,
120 py_mod_bt2_exc_try_again_type)) {
121 status = __BT_FUNC_STATUS_AGAIN;
122 } else if (PyErr_GivenExceptionMatches(exc,
123 py_mod_bt2_exc_stop_type)) {
124 status = __BT_FUNC_STATUS_END;
125 } else if (PyErr_GivenExceptionMatches(exc,
126 py_mod_bt2_exc_unknown_object_type)) {
127 status = __BT_FUNC_STATUS_UNKNOWN_OBJECT;
128 } else {
129 /*
130 * Unknown exception: convert to general error.
131 *
132 * Because we only want to fetch the log level when
133 * we actually get an exception, and not systematically
134 * when we call py_exc_to_status() (as py_exc_to_status()
135 * can return `__BT_FUNC_STATUS_OK`), we get it here
136 * depending on the actor's type.
137 */
138 if (self_component) {
139 active_log_level = get_self_component_log_level(
140 self_component);
141 } else if (self_message_iterator) {
142 active_log_level = get_self_message_iterator_log_level(
143 self_message_iterator);
144 }
145
146 BT_ASSERT(active_log_level != -1);
147 log_exception_and_maybe_append_error(BT_LOG_WARNING,
148 active_log_level, true,
149 self_component_class, self_component,
150 self_message_iterator, module_name);
151
152 if (PyErr_GivenExceptionMatches(exc,
153 py_mod_bt2_exc_memory_error)) {
154 status = __BT_FUNC_STATUS_MEMORY_ERROR;
155 } else {
156 status = __BT_FUNC_STATUS_ERROR;
157 }
158 }
159
160 end:
161 PyErr_Clear();
162 return status;
163 }
164
165 static
166 int py_exc_to_status_component_class(
167 bt_self_component_class *self_component_class,
168 int active_log_level)
169 {
170 return py_exc_to_status(self_component_class, NULL, NULL, NULL,
171 active_log_level);
172 }
173
174 static
175 int py_exc_to_status_component(bt_self_component *self_component)
176 {
177 return py_exc_to_status(NULL, self_component, NULL, NULL, -1);
178 }
179
180 static
181 int py_exc_to_status_message_iterator(
182 bt_self_message_iterator *self_message_iterator)
183 {
184 return py_exc_to_status(NULL, NULL, self_message_iterator, NULL, -1);
185 }
186
187 static
188 bool bt_bt2_is_python_component_class(const bt_component_class *comp_cls)
189 {
190 return bt_g_hash_table_contains(bt_cc_ptr_to_py_cls, comp_cls);
191 }
192
193 /* Component class proxy methods (delegate to the attached Python object) */
194
195 static
196 bt_component_class_init_method_status component_class_init(
197 bt_self_component *self_component,
198 void *self_component_v,
199 swig_type_info *self_comp_cls_type_swig_type,
200 const bt_value *params,
201 void *init_method_data)
202 {
203 const bt_component *component = bt_self_component_as_component(self_component);
204 const bt_component_class *component_class = bt_component_borrow_class_const(component);
205 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
206 PyObject *py_cls = NULL;
207 PyObject *py_comp = NULL;
208 PyObject *py_params_ptr = NULL;
209 PyObject *py_comp_ptr = NULL;
210 bt_logging_level log_level = get_self_component_log_level(
211 self_component);
212
213 BT_ASSERT(self_component);
214 BT_ASSERT(self_component_v);
215 BT_ASSERT(self_comp_cls_type_swig_type);
216
217 /*
218 * Get the user-defined Python class which created this
219 * component's class in the first place (borrowed
220 * reference).
221 */
222 py_cls = lookup_cc_ptr_to_py_cls(component_class);
223 if (!py_cls) {
224 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
225 "Cannot find Python class associated to native component class: "
226 "comp-cls-addr=%p", component_class);
227 goto error;
228 }
229
230 /* Parameters pointer -> SWIG pointer Python object */
231 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
232 SWIGTYPE_p_bt_value, 0);
233 if (!py_params_ptr) {
234 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
235 "Failed to create a SWIG pointer object.");
236 goto error;
237 }
238
239 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
240 self_comp_cls_type_swig_type, 0);
241 if (!py_comp_ptr) {
242 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
243 "Failed to create a SWIG pointer object.");
244 goto error;
245 }
246
247 /*
248 * Do the equivalent of this:
249 *
250 * py_comp = py_cls._bt_init_from_native(py_comp_ptr,
251 * py_params_ptr, init_method_data ? init_method_data : Py_None)
252 *
253 * _UserComponentType._bt_init_from_native() calls the Python
254 * component object's __init__() function.
255 *
256 * We don't take any reference on `init_method_data` which, if
257 * not `NULL`, is assumed to be a `PyObject *`: the user's
258 * __init__() function will eventually take a reference if
259 * needed. If `init_method_data` is `NULL`, then we pass
260 * `Py_None` as the initialization's Python object.
261 */
262 py_comp = PyObject_CallMethod(py_cls,
263 "_bt_init_from_native", "(OOO)", py_comp_ptr, py_params_ptr,
264 init_method_data ? init_method_data : Py_None);
265 if (!py_comp) {
266 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component,
267 "Failed to call Python class's _bt_init_from_native() method: "
268 "py-cls-addr=%p", py_cls);
269 status = py_exc_to_status_component(self_component);
270 goto end;
271 }
272
273 /*
274 * Our user Python component object is now fully created and
275 * initialized by the user. Since we just created it, this
276 * native component is its only (persistent) owner.
277 */
278 bt_self_component_set_data(self_component, py_comp);
279 py_comp = NULL;
280 goto end;
281
282 error:
283 status = __BT_FUNC_STATUS_ERROR;
284
285 /*
286 * Clear any exception: we're returning a bad status anyway. If
287 * this call originated from Python (creation from a plugin's
288 * component class, for example), then the user gets an
289 * appropriate creation error.
290 */
291 PyErr_Clear();
292
293 end:
294 Py_XDECREF(py_comp);
295 Py_XDECREF(py_params_ptr);
296 Py_XDECREF(py_comp_ptr);
297 return status;
298 }
299
300 static
301 bt_component_class_get_supported_mip_versions_method_status
302 component_class_get_supported_mip_versions(
303 const bt_component_class *component_class,
304 bt_self_component_class *self_component_class,
305 const bt_value *params, void *init_method_data,
306 bt_logging_level log_level,
307 bt_integer_range_set_unsigned *supported_versions)
308 {
309 uint64_t i;
310 PyObject *py_cls = NULL;
311 PyObject *py_params_ptr = NULL;
312 PyObject *py_range_set_addr = NULL;
313 bt_integer_range_set_unsigned *ret_range_set = NULL;
314 bt_component_class_get_supported_mip_versions_method_status status =
315 __BT_FUNC_STATUS_OK;
316
317 py_cls = lookup_cc_ptr_to_py_cls(component_class);
318 if (!py_cls) {
319 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
320 "Cannot find Python class associated to native component class: "
321 "comp-cls-addr=%p", component_class);
322 goto error;
323 }
324
325 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
326 SWIGTYPE_p_bt_value, 0);
327 if (!py_params_ptr) {
328 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
329 "Failed to create a SWIG pointer object.");
330 goto error;
331 }
332
333 /*
334 * We don't take any reference on `init_method_data` which, if
335 * not `NULL`, is assumed to be a `PyObject *`: the user's
336 * _user_get_supported_mip_versions() function will eventually
337 * take a reference if needed. If `init_method_data` is `NULL`,
338 * then we pass `Py_None` as the initialization's Python object.
339 */
340 py_range_set_addr = PyObject_CallMethod(py_cls,
341 "_bt_get_supported_mip_versions_from_native", "(OOi)",
342 py_params_ptr, init_method_data ? init_method_data : Py_None,
343 (int) log_level);
344 if (!py_range_set_addr) {
345 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
346 "Failed to call Python class's _bt_get_supported_mip_versions_from_native() method: "
347 "py-cls-addr=%p", py_cls);
348 status = py_exc_to_status_component_class(self_component_class,
349 log_level);
350 goto end;
351 }
352
353 /*
354 * The returned object, on success, is an integer object
355 * (PyLong) containing the address of a BT unsigned integer
356 * range set object (new reference).
357 */
358 ret_range_set = PyLong_AsVoidPtr(py_range_set_addr);
359 BT_ASSERT(!PyErr_Occurred());
360 BT_ASSERT(ret_range_set);
361
362 /* Copy returned ranges to input range set */
363 for (i = 0; i < bt_integer_range_set_get_range_count(
364 bt_integer_range_set_unsigned_as_range_set_const(ret_range_set));
365 i++) {
366 const bt_integer_range_unsigned *range =
367 bt_integer_range_set_unsigned_borrow_range_by_index_const(
368 ret_range_set, i);
369 bt_integer_range_set_add_range_status add_range_status;
370
371 add_range_status = bt_integer_range_set_unsigned_add_range(
372 supported_versions,
373 bt_integer_range_unsigned_get_lower(range),
374 bt_integer_range_unsigned_get_upper(range));
375 if (add_range_status) {
376 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
377 "Failed to add range to supported MIP versions range set.");
378 goto error;
379 }
380 }
381
382 goto end;
383
384 error:
385 PyErr_Clear();
386 status = __BT_FUNC_STATUS_ERROR;
387
388 end:
389 Py_XDECREF(py_params_ptr);
390 Py_XDECREF(py_range_set_addr);
391 bt_integer_range_set_unsigned_put_ref(ret_range_set);
392 return status;
393 }
394
395 static
396 bt_component_class_get_supported_mip_versions_method_status
397 component_class_source_get_supported_mip_versions(
398 bt_self_component_class_source *self_component_class_source,
399 const bt_value *params, void *init_method_data,
400 bt_logging_level log_level,
401 bt_integer_range_set_unsigned *supported_versions)
402 {
403 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
404 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
405 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
406
407 return component_class_get_supported_mip_versions(
408 component_class, self_component_class,
409 params, init_method_data, log_level, supported_versions);
410 }
411
412 static
413 bt_component_class_get_supported_mip_versions_method_status
414 component_class_filter_get_supported_mip_versions(
415 bt_self_component_class_filter *self_component_class_filter,
416 const bt_value *params, void *init_method_data,
417 bt_logging_level log_level,
418 bt_integer_range_set_unsigned *supported_versions)
419 {
420 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
421 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
422 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
423
424 return component_class_get_supported_mip_versions(
425 component_class, self_component_class,
426 params, init_method_data, log_level, supported_versions);
427 }
428
429 static
430 bt_component_class_get_supported_mip_versions_method_status
431 component_class_sink_get_supported_mip_versions(
432 bt_self_component_class_sink *self_component_class_sink,
433 const bt_value *params, void *init_method_data,
434 bt_logging_level log_level,
435 bt_integer_range_set_unsigned *supported_versions)
436 {
437 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
438 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
439 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
440
441 return component_class_get_supported_mip_versions(
442 component_class, self_component_class,
443 params, init_method_data, log_level, supported_versions);
444 }
445
446 /*
447 * Method of bt_component_class_source to initialize a bt_self_component_source
448 * of that class.
449 */
450
451 static
452 bt_component_class_init_method_status component_class_source_init(
453 bt_self_component_source *self_component_source,
454 const bt_value *params, void *init_method_data)
455 {
456 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
457 return component_class_init(
458 self_component,
459 self_component_source,
460 SWIGTYPE_p_bt_self_component_source,
461 params, init_method_data);
462 }
463
464 static
465 bt_component_class_init_method_status component_class_filter_init(
466 bt_self_component_filter *self_component_filter,
467 const bt_value *params, void *init_method_data)
468 {
469 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
470 return component_class_init(
471 self_component,
472 self_component_filter,
473 SWIGTYPE_p_bt_self_component_filter,
474 params, init_method_data);
475 }
476
477 static
478 bt_component_class_init_method_status component_class_sink_init(
479 bt_self_component_sink *self_component_sink,
480 const bt_value *params, void *init_method_data)
481 {
482 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
483 return component_class_init(
484 self_component,
485 self_component_sink,
486 SWIGTYPE_p_bt_self_component_sink,
487 params, init_method_data);
488 }
489
490 static
491 void component_class_finalize(bt_self_component *self_component)
492 {
493 PyObject *py_comp = bt_self_component_get_data(self_component);
494 BT_ASSERT(py_comp);
495
496 /* Call user's _user_finalize() method */
497 PyObject *py_method_result = PyObject_CallMethod(py_comp,
498 "_user_finalize", NULL);
499
500 if (PyErr_Occurred()) {
501 bt_logging_level log_level = get_self_component_log_level(
502 self_component);
503
504 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component,
505 "User component's _user_finalize() method raised an exception: ignoring:");
506 logw_exception(log_level);
507 }
508
509 /*
510 * Ignore any exception raised by the _user_finalize() method
511 * because it won't change anything at this point: the component
512 * is being destroyed anyway.
513 */
514 PyErr_Clear();
515 Py_XDECREF(py_method_result);
516 Py_DECREF(py_comp);
517 }
518
519 static
520 void component_class_source_finalize(bt_self_component_source *self_component_source)
521 {
522 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
523 component_class_finalize(self_component);
524 }
525
526 static
527 void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
528 {
529 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
530 component_class_finalize(self_component);
531 }
532
533 static
534 void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
535 {
536 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
537 component_class_finalize(self_component);
538 }
539
540 static
541 bt_component_class_message_iterator_can_seek_beginning_method_status
542 component_class_can_seek_beginning(
543 bt_self_message_iterator *self_message_iterator, bt_bool *can_seek)
544 {
545 PyObject *py_iter;
546 PyObject *py_result = NULL;
547 bt_component_class_message_iterator_can_seek_beginning_method_status status;
548 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
549 BT_ASSERT(py_iter);
550
551 py_result = PyObject_CallMethod(py_iter,
552 "_bt_can_seek_beginning_from_native", NULL);
553
554 BT_ASSERT(!py_result || PyBool_Check(py_result));
555
556 if (py_result) {
557 *can_seek = PyObject_IsTrue(py_result);
558 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
559 } else {
560 status = py_exc_to_status_message_iterator(self_message_iterator);
561 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
562 }
563
564 Py_XDECREF(py_result);
565
566 return status;
567 }
568
569 static
570 bt_component_class_message_iterator_seek_beginning_method_status
571 component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
572 {
573 PyObject *py_iter;
574 PyObject *py_result;
575 bt_component_class_message_iterator_seek_beginning_method_status status;
576
577 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
578 BT_ASSERT(py_iter);
579 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
580 NULL);
581 BT_ASSERT(!py_result || py_result == Py_None);
582 status = py_exc_to_status_message_iterator(self_message_iterator);
583 Py_XDECREF(py_result);
584 return status;
585 }
586
587 static
588 bt_component_class_port_connected_method_status component_class_port_connected(
589 bt_self_component *self_component,
590 void *self_component_port,
591 swig_type_info *self_component_port_swig_type,
592 bt_port_type self_component_port_type,
593 const void *other_port,
594 swig_type_info *other_port_swig_type)
595 {
596 bt_component_class_port_connected_method_status status;
597 PyObject *py_comp = NULL;
598 PyObject *py_self_port_ptr = NULL;
599 PyObject *py_other_port_ptr = NULL;
600 PyObject *py_method_result = NULL;
601 bt_logging_level log_level = get_self_component_log_level(
602 self_component);
603
604 py_comp = bt_self_component_get_data(self_component);
605 BT_ASSERT(py_comp);
606 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
607 self_component_port_swig_type, 0);
608 if (!py_self_port_ptr) {
609 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
610 "Failed to create a SWIG pointer object.");
611 status = __BT_FUNC_STATUS_MEMORY_ERROR;
612 goto end;
613 }
614
615 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
616 other_port_swig_type, 0);
617 if (!py_other_port_ptr) {
618 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
619 "Failed to create a SWIG pointer object.");
620 status = __BT_FUNC_STATUS_MEMORY_ERROR;
621 goto end; }
622
623 py_method_result = PyObject_CallMethod(py_comp,
624 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
625 self_component_port_type, py_other_port_ptr);
626 BT_ASSERT(!py_method_result || py_method_result == Py_None);
627 status = py_exc_to_status_component(self_component);
628
629 end:
630 Py_XDECREF(py_self_port_ptr);
631 Py_XDECREF(py_other_port_ptr);
632 Py_XDECREF(py_method_result);
633 return status;
634 }
635
636 static
637 bt_component_class_port_connected_method_status
638 component_class_source_output_port_connected(
639 bt_self_component_source *self_component_source,
640 bt_self_component_port_output *self_component_port_output,
641 const bt_port_input *other_port_input)
642 {
643 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
644
645 return component_class_port_connected(
646 self_component,
647 self_component_port_output,
648 SWIGTYPE_p_bt_self_component_port_output,
649 BT_PORT_TYPE_OUTPUT,
650 other_port_input,
651 SWIGTYPE_p_bt_port_input);
652 }
653
654 static
655 bt_component_class_port_connected_method_status
656 component_class_filter_input_port_connected(
657 bt_self_component_filter *self_component_filter,
658 bt_self_component_port_input *self_component_port_input,
659 const bt_port_output *other_port_output)
660 {
661 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
662
663 return component_class_port_connected(
664 self_component,
665 self_component_port_input,
666 SWIGTYPE_p_bt_self_component_port_input,
667 BT_PORT_TYPE_INPUT,
668 other_port_output,
669 SWIGTYPE_p_bt_port_output);
670 }
671
672 static
673 bt_component_class_port_connected_method_status
674 component_class_filter_output_port_connected(
675 bt_self_component_filter *self_component_filter,
676 bt_self_component_port_output *self_component_port_output,
677 const bt_port_input *other_port_input)
678 {
679 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
680
681 return component_class_port_connected(
682 self_component,
683 self_component_port_output,
684 SWIGTYPE_p_bt_self_component_port_output,
685 BT_PORT_TYPE_OUTPUT,
686 other_port_input,
687 SWIGTYPE_p_bt_port_input);
688 }
689
690 static
691 bt_component_class_port_connected_method_status
692 component_class_sink_input_port_connected(
693 bt_self_component_sink *self_component_sink,
694 bt_self_component_port_input *self_component_port_input,
695 const bt_port_output *other_port_output)
696 {
697 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
698
699 return component_class_port_connected(
700 self_component,
701 self_component_port_input,
702 SWIGTYPE_p_bt_self_component_port_input,
703 BT_PORT_TYPE_INPUT,
704 other_port_output,
705 SWIGTYPE_p_bt_port_output);
706 }
707
708 static
709 bt_component_class_sink_graph_is_configured_method_status
710 component_class_sink_graph_is_configured(
711 bt_self_component_sink *self_component_sink)
712 {
713 PyObject *py_comp = NULL;
714 PyObject *py_method_result = NULL;
715 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
716 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
717
718 py_comp = bt_self_component_get_data(self_component);
719 py_method_result = PyObject_CallMethod(py_comp,
720 "_bt_graph_is_configured_from_native", NULL);
721 BT_ASSERT(!py_method_result || py_method_result == Py_None);
722 status = py_exc_to_status_component(self_component);
723 Py_XDECREF(py_method_result);
724 return status;
725 }
726
727 static
728 bt_component_class_query_method_status component_class_query(
729 const bt_component_class *component_class,
730 bt_self_component_class *self_component_class,
731 bt_private_query_executor *priv_query_executor,
732 const char *object, const bt_value *params, void *method_data,
733 const bt_value **result)
734 {
735 PyObject *py_cls = NULL;
736 PyObject *py_params_ptr = NULL;
737 PyObject *py_priv_query_exec_ptr = NULL;
738 PyObject *py_query_func = NULL;
739 PyObject *py_object = NULL;
740 PyObject *py_results_addr = NULL;
741 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
742 const bt_query_executor *query_exec =
743 bt_private_query_executor_as_query_executor_const(
744 priv_query_executor);
745 bt_logging_level log_level =
746 bt_query_executor_get_logging_level(query_exec);
747
748 /*
749 * If there's any `method_data`, assume this component class is
750 * getting queried from Python, so that `method_data` is a
751 * Python object to pass to the user's _user_query() method.
752 */
753 BT_ASSERT(!method_data ||
754 bt_bt2_is_python_component_class(component_class));
755
756 py_cls = lookup_cc_ptr_to_py_cls(component_class);
757 if (!py_cls) {
758 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
759 "Cannot find Python class associated to native component class: "
760 "comp-cls-addr=%p", component_class);
761 goto error;
762 }
763
764 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
765 SWIGTYPE_p_bt_value, 0);
766 if (!py_params_ptr) {
767 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
768 "Failed to create a SWIG pointer object.");
769 goto error;
770 }
771
772 py_priv_query_exec_ptr = SWIG_NewPointerObj(
773 SWIG_as_voidptr(priv_query_executor),
774 SWIGTYPE_p_bt_private_query_executor, 0);
775 if (!py_priv_query_exec_ptr) {
776 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
777 "Failed to create a SWIG pointer object.");
778 goto error;
779 }
780
781 py_object = SWIG_FromCharPtr(object);
782 if (!py_object) {
783 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
784 "Failed to create a Python string.");
785 goto error;
786 }
787
788 /*
789 * We don't take any reference on `method_data` which, if not
790 * `NULL`, is assumed to be a `PyObject *`: the user's
791 * _user_query() function will eventually take a reference if
792 * needed. If `method_data` is `NULL`, then we pass `Py_None` as
793 * the initialization's Python object.
794 */
795 py_results_addr = PyObject_CallMethod(py_cls,
796 "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr,
797 py_object, py_params_ptr,
798 method_data ? method_data : Py_None);
799 if (!py_results_addr) {
800 status = py_exc_to_status_component_class(self_component_class,
801 log_level);
802 if (status < 0) {
803 static const char *fmt =
804 "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p";
805 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
806 fmt, py_cls);
807 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
808 self_component_class, fmt, py_cls);
809 }
810 goto end;
811 }
812
813 /*
814 * The returned object, on success, is an integer object
815 * (PyLong) containing the address of a BT value object (new
816 * reference).
817 */
818 *result = PyLong_AsVoidPtr(py_results_addr);
819 BT_ASSERT(!PyErr_Occurred());
820 BT_ASSERT(*result);
821 goto end;
822
823 error:
824 PyErr_Clear();
825 status = __BT_FUNC_STATUS_ERROR;
826
827 end:
828 Py_XDECREF(py_params_ptr);
829 Py_XDECREF(py_priv_query_exec_ptr);
830 Py_XDECREF(py_query_func);
831 Py_XDECREF(py_object);
832 Py_XDECREF(py_results_addr);
833 return status;
834 }
835
836 static
837 bt_component_class_query_method_status component_class_source_query(
838 bt_self_component_class_source *self_component_class_source,
839 bt_private_query_executor *priv_query_executor,
840 const char *object, const bt_value *params, void *method_data,
841 const bt_value **result)
842 {
843 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
844 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
845 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
846
847 return component_class_query(component_class, self_component_class,
848 priv_query_executor, object, params, method_data, result);
849 }
850
851 static
852 bt_component_class_query_method_status component_class_filter_query(
853 bt_self_component_class_filter *self_component_class_filter,
854 bt_private_query_executor *priv_query_executor,
855 const char *object, const bt_value *params, void *method_data,
856 const bt_value **result)
857 {
858 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
859 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
860 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
861
862 return component_class_query(component_class, self_component_class,
863 priv_query_executor, object, params, method_data, result);
864 }
865
866 static
867 bt_component_class_query_method_status component_class_sink_query(
868 bt_self_component_class_sink *self_component_class_sink,
869 bt_private_query_executor *priv_query_executor,
870 const char *object, const bt_value *params, void *method_data,
871 const bt_value **result)
872 {
873 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
874 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
875 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
876
877 return component_class_query(component_class, self_component_class,
878 priv_query_executor, object, params, method_data, result);
879 }
880
881 static
882 bt_component_class_message_iterator_init_method_status
883 component_class_message_iterator_init(
884 bt_self_message_iterator *self_message_iterator,
885 bt_self_component *self_component,
886 bt_self_component_port_output *self_component_port_output)
887 {
888 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
889 PyObject *py_comp_cls = NULL;
890 PyObject *py_iter_cls = NULL;
891 PyObject *py_iter_ptr = NULL;
892 PyObject *py_component_port_output_ptr = NULL;
893 PyObject *py_init_method_result = NULL;
894 PyObject *py_iter = NULL;
895 PyObject *py_comp;
896 bt_logging_level log_level = get_self_component_log_level(
897 self_component);
898
899 py_comp = bt_self_component_get_data(self_component);
900
901 /* Find user's Python message iterator class */
902 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
903 if (!py_comp_cls) {
904 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
905 "Cannot get Python object's `__class__` attribute.");
906 goto python_error;
907 }
908
909 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
910 if (!py_iter_cls) {
911 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
912 "Cannot get Python class's `_iter_cls` attribute.");
913 goto python_error;
914 }
915
916 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
917 SWIGTYPE_p_bt_self_message_iterator, 0);
918 if (!py_iter_ptr) {
919 const char *err = "Failed to create a SWIG pointer object.";
920
921 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
922 "%s", err);
923 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
924 self_message_iterator, err);
925 goto error;
926 }
927
928 /*
929 * Create object with borrowed native message iterator
930 * reference:
931 *
932 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
933 */
934 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
935 "(OO)", py_iter_cls, py_iter_ptr);
936 if (!py_iter) {
937 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
938 "Failed to call Python class's __new__() method: "
939 "py-cls-addr=%p", py_iter_cls);
940 goto python_error;
941 }
942
943 /*
944 * Initialize object:
945 *
946 * py_iter.__init__(self_output_port)
947 *
948 * through the _init_for_native helper static method.
949 *
950 * At this point, py_iter._ptr is set, so this initialization
951 * function has access to self._component (which gives it the
952 * user Python component object from which the iterator was
953 * created).
954 */
955 py_component_port_output_ptr = SWIG_NewPointerObj(
956 SWIG_as_voidptr(self_component_port_output),
957 SWIGTYPE_p_bt_self_component_port_output, 0);
958 if (!py_component_port_output_ptr) {
959 const char *err = "Failed to create a SWIG pointer object.";
960
961 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
962 "%s", err);
963 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
964 self_message_iterator, err);
965 goto error;
966 }
967
968 py_init_method_result = PyObject_CallMethod(py_iter,
969 "_bt_init_from_native", "O", py_component_port_output_ptr);
970 if (!py_init_method_result) {
971 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
972 "User's __init__() method failed:");
973 goto python_error;
974 }
975
976 /*
977 * Since the Python code can never instantiate a user-defined
978 * message iterator class, the native message iterator
979 * object does NOT belong to a user Python message iterator
980 * object (borrowed reference). However this Python object is
981 * owned by this native message iterator object.
982 *
983 * In the Python world, the lifetime of the native message
984 * iterator is managed by a _GenericMessageIterator
985 * instance:
986 *
987 * _GenericMessageIterator instance:
988 * owns a native bt_message_iterator object (iter)
989 * owns a _UserMessageIterator instance (py_iter)
990 * self._ptr is a borrowed reference to the
991 * native bt_private_connection_private_message_iterator
992 * object (iter)
993 */
994 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
995 py_iter = NULL;
996 goto end;
997
998 python_error:
999 /* Handling of errors that cause a Python exception to be set. */
1000 status = py_exc_to_status_message_iterator(self_message_iterator);
1001 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
1002 goto end;
1003
1004 error:
1005 /* Handling of errors that don't cause a Python exception to be set. */
1006 status = __BT_FUNC_STATUS_ERROR;
1007
1008 end:
1009 BT_ASSERT(!PyErr_Occurred());
1010
1011 Py_XDECREF(py_comp_cls);
1012 Py_XDECREF(py_iter_cls);
1013 Py_XDECREF(py_iter_ptr);
1014 Py_XDECREF(py_component_port_output_ptr);
1015 Py_XDECREF(py_init_method_result);
1016 Py_XDECREF(py_iter);
1017 return status;
1018 }
1019
1020 static
1021 bt_component_class_message_iterator_init_method_status
1022 component_class_source_message_iterator_init(
1023 bt_self_message_iterator *self_message_iterator,
1024 bt_self_component_source *self_component_source,
1025 bt_self_component_port_output *self_component_port_output)
1026 {
1027 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
1028
1029 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1030 }
1031
1032 static
1033 bt_component_class_message_iterator_init_method_status
1034 component_class_filter_message_iterator_init(
1035 bt_self_message_iterator *self_message_iterator,
1036 bt_self_component_filter *self_component_filter,
1037 bt_self_component_port_output *self_component_port_output)
1038 {
1039 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
1040
1041 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1042 }
1043
1044 static
1045 void component_class_message_iterator_finalize(
1046 bt_self_message_iterator *message_iterator)
1047 {
1048 PyObject *py_message_iter = bt_self_message_iterator_get_data(
1049 message_iterator);
1050 PyObject *py_method_result = NULL;
1051
1052 BT_ASSERT(py_message_iter);
1053
1054 /* Call user's _user_finalize() method */
1055 py_method_result = PyObject_CallMethod(py_message_iter,
1056 "_user_finalize", NULL);
1057
1058 if (PyErr_Occurred()) {
1059 bt_self_component *self_comp =
1060 bt_self_message_iterator_borrow_component(
1061 message_iterator);
1062 bt_logging_level log_level = get_self_component_log_level(
1063 self_comp);
1064
1065 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp,
1066 "User's _user_finalize() method raised an exception: ignoring:");
1067 logw_exception(get_self_message_iterator_log_level(
1068 message_iterator));
1069 }
1070
1071 /*
1072 * Ignore any exception raised by the _user_finalize() method
1073 * because it won't change anything at this point: the component
1074 * is being destroyed anyway.
1075 */
1076 PyErr_Clear();
1077 Py_XDECREF(py_method_result);
1078 Py_DECREF(py_message_iter);
1079 }
1080
1081 /* Valid for both sources and filters. */
1082
1083 static
1084 bt_component_class_message_iterator_next_method_status
1085 component_class_message_iterator_next(
1086 bt_self_message_iterator *message_iterator,
1087 bt_message_array_const msgs, uint64_t capacity,
1088 uint64_t *count)
1089 {
1090 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
1091 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1092 PyObject *py_method_result = NULL;
1093
1094 BT_ASSERT(py_message_iter);
1095 py_method_result = PyObject_CallMethod(py_message_iter,
1096 "_bt_next_from_native", NULL);
1097 if (!py_method_result) {
1098 status = py_exc_to_status_message_iterator(message_iterator);
1099 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
1100 goto end;
1101 }
1102
1103 /*
1104 * The returned object, on success, is an integer object
1105 * (PyLong) containing the address of a native message
1106 * object (which is now ours).
1107 */
1108 msgs[0] = PyLong_AsVoidPtr(py_method_result);
1109 *count = 1;
1110
1111 /* Clear potential overflow error; should never happen */
1112 BT_ASSERT(!PyErr_Occurred());
1113 goto end;
1114
1115 end:
1116 Py_XDECREF(py_method_result);
1117 return status;
1118 }
1119
1120 static
1121 bt_component_class_sink_consume_method_status
1122 component_class_sink_consume(bt_self_component_sink *self_component_sink)
1123 {
1124 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1125 PyObject *py_comp = bt_self_component_get_data(self_component);
1126 PyObject *py_method_result = NULL;
1127 bt_component_class_sink_consume_method_status status;
1128
1129 BT_ASSERT(py_comp);
1130 py_method_result = PyObject_CallMethod(py_comp,
1131 "_user_consume", NULL);
1132 status = py_exc_to_status_component(self_component);
1133 BT_ASSERT(py_method_result || status != __BT_FUNC_STATUS_OK);
1134 Py_XDECREF(py_method_result);
1135 return status;
1136 }
1137
1138 static
1139 int component_class_set_help_and_desc(
1140 bt_component_class *component_class,
1141 const char *description, const char *help)
1142 {
1143 int ret;
1144
1145 if (description) {
1146 ret = bt_component_class_set_description(component_class, description);
1147 if (ret) {
1148 BT_LOGE("Cannot set component class's description: "
1149 "comp-cls-addr=%p", component_class);
1150 goto end;
1151 }
1152 }
1153
1154 if (help) {
1155 ret = bt_component_class_set_help(component_class, help);
1156 if (ret) {
1157 BT_LOGE("Cannot set component class's help text: "
1158 "comp-cls-addr=%p", component_class);
1159 goto end;
1160 }
1161 }
1162
1163 ret = 0;
1164
1165 end:
1166 return ret;
1167 }
1168
1169 static
1170 bt_component_class_source *bt_bt2_component_class_source_create(
1171 PyObject *py_cls, const char *name, const char *description,
1172 const char *help)
1173 {
1174 bt_component_class_source *component_class_source;
1175 bt_component_class *component_class;
1176 int ret;
1177
1178 BT_ASSERT(py_cls);
1179 component_class_source = bt_component_class_source_create(name,
1180 component_class_message_iterator_next);
1181 if (!component_class_source) {
1182 BT_LOGE_STR("Cannot create source component class.");
1183 goto end;
1184 }
1185
1186 component_class = bt_component_class_source_as_component_class(component_class_source);
1187
1188 if (component_class_set_help_and_desc(component_class, description, help)) {
1189 goto end;
1190 }
1191
1192 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
1193 BT_ASSERT(ret == 0);
1194 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1195 BT_ASSERT(ret == 0);
1196 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
1197 component_class_can_seek_beginning);
1198 BT_ASSERT(ret == 0);
1199 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
1200 component_class_seek_beginning);
1201 BT_ASSERT(ret == 0);
1202 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1203 component_class_source_output_port_connected);
1204 BT_ASSERT(ret == 0);
1205 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1206 BT_ASSERT(ret == 0);
1207 ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions);
1208 BT_ASSERT(ret == 0);
1209 ret = bt_component_class_source_set_message_iterator_init_method(
1210 component_class_source, component_class_source_message_iterator_init);
1211 BT_ASSERT(ret == 0);
1212 ret = bt_component_class_source_set_message_iterator_finalize_method(
1213 component_class_source, component_class_message_iterator_finalize);
1214 BT_ASSERT(ret == 0);
1215 register_cc_ptr_to_py_cls(component_class, py_cls);
1216
1217 end:
1218 return component_class_source;
1219 }
1220
1221 static
1222 bt_component_class_filter *bt_bt2_component_class_filter_create(
1223 PyObject *py_cls, const char *name, const char *description,
1224 const char *help)
1225 {
1226 bt_component_class *component_class;
1227 bt_component_class_filter *component_class_filter;
1228 int ret;
1229
1230 BT_ASSERT(py_cls);
1231 component_class_filter = bt_component_class_filter_create(name,
1232 component_class_message_iterator_next);
1233 if (!component_class_filter) {
1234 BT_LOGE_STR("Cannot create filter component class.");
1235 goto end;
1236 }
1237
1238 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1239
1240 if (component_class_set_help_and_desc(component_class, description, help)) {
1241 goto end;
1242 }
1243
1244 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
1245 BT_ASSERT(ret == 0);
1246 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1247 BT_ASSERT(ret == 0);
1248 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
1249 component_class_can_seek_beginning);
1250 BT_ASSERT(ret == 0);
1251 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
1252 component_class_seek_beginning);
1253 BT_ASSERT(ret == 0);
1254 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1255 component_class_filter_input_port_connected);
1256 BT_ASSERT(ret == 0);
1257 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1258 component_class_filter_output_port_connected);
1259 BT_ASSERT(ret == 0);
1260 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1261 BT_ASSERT(ret == 0);
1262 ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions);
1263 BT_ASSERT(ret == 0);
1264 ret = bt_component_class_filter_set_message_iterator_init_method(
1265 component_class_filter, component_class_filter_message_iterator_init);
1266 BT_ASSERT(ret == 0);
1267 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1268 component_class_filter, component_class_message_iterator_finalize);
1269 BT_ASSERT(ret == 0);
1270 register_cc_ptr_to_py_cls(component_class, py_cls);
1271
1272 end:
1273 return component_class_filter;
1274 }
1275
1276 static
1277 bt_component_class_sink *bt_bt2_component_class_sink_create(
1278 PyObject *py_cls, const char *name, const char *description,
1279 const char *help)
1280 {
1281 bt_component_class_sink *component_class_sink;
1282 bt_component_class *component_class;
1283 int ret;
1284
1285 BT_ASSERT(py_cls);
1286 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1287
1288 if (!component_class_sink) {
1289 BT_LOGE_STR("Cannot create sink component class.");
1290 goto end;
1291 }
1292
1293 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1294
1295 if (component_class_set_help_and_desc(component_class, description, help)) {
1296 goto end;
1297 }
1298
1299 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
1300 BT_ASSERT(ret == 0);
1301 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1302 BT_ASSERT(ret == 0);
1303 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1304 component_class_sink_input_port_connected);
1305 BT_ASSERT(ret == 0);
1306 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1307 component_class_sink_graph_is_configured);
1308 BT_ASSERT(ret == 0);
1309 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1310 BT_ASSERT(ret == 0);
1311 ret = bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink, component_class_sink_get_supported_mip_versions);
1312 BT_ASSERT(ret == 0);
1313 register_cc_ptr_to_py_cls(component_class, py_cls);
1314
1315 end:
1316 return component_class_sink;
1317 }
This page took 0.090426 seconds and 5 git commands to generate.