lib: bt_object_{get,put}_ref(): accept a `const` parameter
[babeltrace.git] / lib / graph / query-executor.c
CommitLineData
1286dcbb
PP
1/*
2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#define BT_LOG_TAG "QUERY-EXECUTOR"
24#include <babeltrace/lib-logging-internal.h>
25
834e9996 26#include <babeltrace/graph/private-query-executor.h>
1286dcbb
PP
27#include <babeltrace/graph/query-executor.h>
28#include <babeltrace/graph/query-executor-internal.h>
29#include <babeltrace/graph/component-class.h>
30#include <babeltrace/graph/component-class-internal.h>
31#include <babeltrace/values.h>
32#include <babeltrace/object-internal.h>
834e9996 33#include <babeltrace/object.h>
1286dcbb 34#include <babeltrace/compiler-internal.h>
834e9996
PP
35#include <babeltrace/assert-internal.h>
36#include <babeltrace/assert-pre-internal.h>
1286dcbb
PP
37
38static
39void bt_query_executor_destroy(struct bt_object *obj)
40{
41 struct bt_query_executor *query_exec =
42 container_of(obj, struct bt_query_executor, base);
43
834e9996 44 BT_LOGD("Destroying query executor: addr=%p", query_exec);
1286dcbb
PP
45 g_free(query_exec);
46}
47
834e9996 48struct bt_private_query_executor *bt_private_query_executor_create(void)
1286dcbb
PP
49{
50 struct bt_query_executor *query_exec;
51
52 BT_LOGD_STR("Creating query executor.");
53 query_exec = g_new0(struct bt_query_executor, 1);
54 if (!query_exec) {
55 BT_LOGE_STR("Failed to allocate one query executor.");
56 goto end;
57 }
58
1d7bf349
PP
59 bt_object_init_shared(&query_exec->base,
60 bt_query_executor_destroy);
1286dcbb
PP
61 BT_LOGD("Created query executor: addr=%p", query_exec);
62
63end:
834e9996 64 return (void *) query_exec;
1286dcbb
PP
65}
66
79400927 67enum bt_query_executor_status bt_private_query_executor_query(
834e9996
PP
68 struct bt_private_query_executor *priv_query_exec,
69 struct bt_component_class *comp_cls,
1286dcbb
PP
70 const char *object, struct bt_value *params,
71 struct bt_value **user_result)
72{
834e9996
PP
73 typedef enum bt_query_status (*method_t)(void *, void *,
74 const void *, void *, void *);
1286dcbb 75
834e9996
PP
76 struct bt_query_executor *query_exec = (void *) priv_query_exec;
77 enum bt_query_status status;
79400927 78 enum bt_query_executor_status exec_status;
834e9996 79 method_t method = NULL;
1286dcbb 80
834e9996
PP
81 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
82 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
83 BT_ASSERT_PRE_NON_NULL(object, "Object");
84 BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)");
85 BT_ASSERT_PRE(!query_exec->canceled, "Query executor is canceled.");
86
87 if (!params) {
88 params = bt_value_null;
1286dcbb
PP
89 }
90
834e9996
PP
91 switch (comp_cls->type) {
92 case BT_COMPONENT_CLASS_TYPE_SOURCE:
93 {
94 struct bt_component_class_source *src_cc = (void *) comp_cls;
95
96 method = (method_t) src_cc->methods.query;
97 break;
1286dcbb 98 }
834e9996
PP
99 case BT_COMPONENT_CLASS_TYPE_FILTER:
100 {
101 struct bt_component_class_filter *flt_cc = (void *) comp_cls;
1286dcbb 102
834e9996
PP
103 method = (method_t) flt_cc->methods.query;
104 break;
1286dcbb 105 }
834e9996
PP
106 case BT_COMPONENT_CLASS_TYPE_SINK:
107 {
108 struct bt_component_class_sink *sink_cc = (void *) comp_cls;
1286dcbb 109
834e9996
PP
110 method = (method_t) sink_cc->methods.query;
111 break;
112 }
113 default:
114 abort();
1286dcbb
PP
115 }
116
834e9996 117 if (!method) {
1286dcbb 118 /* Not an error: nothing to query */
834e9996
PP
119 BT_LIB_LOGD("Component class has no registered query method: "
120 "%!+C", comp_cls);
79400927 121 exec_status = BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED;
1286dcbb
PP
122 goto end;
123 }
124
834e9996
PP
125 BT_LIB_LOGD("Calling user's query method: "
126 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v",
127 query_exec, comp_cls, object, params);
128 *user_result = NULL;
129 status = method(comp_cls, query_exec, object, params, user_result);
130 BT_LIB_LOGD("User method returned: status=%s, %![res-]+v",
131 bt_query_status_string(status), *user_result);
834e9996
PP
132 BT_ASSERT_PRE(status != BT_QUERY_STATUS_OK || *user_result,
133 "User method returned `BT_QUERY_STATUS_OK` without a result.");
79400927 134 exec_status = (int) status;
1286dcbb 135 if (query_exec->canceled) {
834e9996 136 BT_OBJECT_PUT_REF_AND_RESET(*user_result);
79400927 137 status = BT_QUERY_EXECUTOR_STATUS_CANCELED;
1286dcbb 138 goto end;
1286dcbb
PP
139 }
140
141end:
79400927 142 return exec_status;
1286dcbb
PP
143}
144
79400927 145enum bt_query_executor_status bt_private_query_executor_cancel(
834e9996 146 struct bt_private_query_executor *priv_query_exec)
1286dcbb 147{
834e9996 148 struct bt_query_executor *query_exec = (void *) priv_query_exec;
1286dcbb 149
834e9996 150 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
1286dcbb
PP
151 query_exec->canceled = BT_TRUE;
152 BT_LOGV("Canceled query executor: addr=%p", query_exec);
79400927 153 return BT_QUERY_EXECUTOR_STATUS_OK;
1286dcbb
PP
154}
155
156bt_bool bt_query_executor_is_canceled(struct bt_query_executor *query_exec)
157{
834e9996
PP
158 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
159 return query_exec->canceled;
1286dcbb 160}
This page took 0.031576 seconds and 4 git commands to generate.