lib: make plugin API const-correct
[babeltrace.git] / lib / graph / query-executor.c
CommitLineData
c7eee084
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
d94d92ac 26#include <babeltrace/graph/private-query-executor.h>
c7eee084
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>
40f4ba76 31#include <babeltrace/values.h>
05e21286 32#include <babeltrace/values-const.h>
c7eee084 33#include <babeltrace/object-internal.h>
d94d92ac 34#include <babeltrace/object.h>
c7eee084 35#include <babeltrace/compiler-internal.h>
d94d92ac
PP
36#include <babeltrace/assert-internal.h>
37#include <babeltrace/assert-pre-internal.h>
c7eee084
PP
38
39static
40void bt_query_executor_destroy(struct bt_object *obj)
41{
42 struct bt_query_executor *query_exec =
43 container_of(obj, struct bt_query_executor, base);
44
d94d92ac 45 BT_LOGD("Destroying query executor: addr=%p", query_exec);
c7eee084
PP
46 g_free(query_exec);
47}
48
d94d92ac 49struct bt_private_query_executor *bt_private_query_executor_create(void)
c7eee084
PP
50{
51 struct bt_query_executor *query_exec;
52
53 BT_LOGD_STR("Creating query executor.");
54 query_exec = g_new0(struct bt_query_executor, 1);
55 if (!query_exec) {
56 BT_LOGE_STR("Failed to allocate one query executor.");
57 goto end;
58 }
59
3fea54f6
PP
60 bt_object_init_shared(&query_exec->base,
61 bt_query_executor_destroy);
c7eee084
PP
62 BT_LOGD("Created query executor: addr=%p", query_exec);
63
64end:
d94d92ac 65 return (void *) query_exec;
c7eee084
PP
66}
67
d72535e6 68enum bt_query_executor_status bt_private_query_executor_query(
d94d92ac
PP
69 struct bt_private_query_executor *priv_query_exec,
70 struct bt_component_class *comp_cls,
05e21286
PP
71 const char *object, const struct bt_value *params,
72 const struct bt_value **user_result)
c7eee084 73{
d94d92ac 74 typedef enum bt_query_status (*method_t)(void *, void *,
05e21286 75 const void *, const void *, void *);
c7eee084 76
d94d92ac
PP
77 struct bt_query_executor *query_exec = (void *) priv_query_exec;
78 enum bt_query_status status;
d72535e6 79 enum bt_query_executor_status exec_status;
d94d92ac 80 method_t method = NULL;
c7eee084 81
d94d92ac
PP
82 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
83 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
84 BT_ASSERT_PRE_NON_NULL(object, "Object");
85 BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)");
86 BT_ASSERT_PRE(!query_exec->canceled, "Query executor is canceled.");
87
88 if (!params) {
89 params = bt_value_null;
c7eee084
PP
90 }
91
d94d92ac
PP
92 switch (comp_cls->type) {
93 case BT_COMPONENT_CLASS_TYPE_SOURCE:
94 {
95 struct bt_component_class_source *src_cc = (void *) comp_cls;
96
97 method = (method_t) src_cc->methods.query;
98 break;
c7eee084 99 }
d94d92ac
PP
100 case BT_COMPONENT_CLASS_TYPE_FILTER:
101 {
102 struct bt_component_class_filter *flt_cc = (void *) comp_cls;
c7eee084 103
d94d92ac
PP
104 method = (method_t) flt_cc->methods.query;
105 break;
c7eee084 106 }
d94d92ac
PP
107 case BT_COMPONENT_CLASS_TYPE_SINK:
108 {
109 struct bt_component_class_sink *sink_cc = (void *) comp_cls;
c7eee084 110
d94d92ac
PP
111 method = (method_t) sink_cc->methods.query;
112 break;
113 }
114 default:
115 abort();
c7eee084
PP
116 }
117
d94d92ac 118 if (!method) {
c7eee084 119 /* Not an error: nothing to query */
d94d92ac
PP
120 BT_LIB_LOGD("Component class has no registered query method: "
121 "%!+C", comp_cls);
d72535e6 122 exec_status = BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED;
c7eee084
PP
123 goto end;
124 }
125
d94d92ac
PP
126 BT_LIB_LOGD("Calling user's query method: "
127 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v",
128 query_exec, comp_cls, object, params);
129 *user_result = NULL;
130 status = method(comp_cls, query_exec, object, params, user_result);
131 BT_LIB_LOGD("User method returned: status=%s, %![res-]+v",
132 bt_query_status_string(status), *user_result);
d94d92ac
PP
133 BT_ASSERT_PRE(status != BT_QUERY_STATUS_OK || *user_result,
134 "User method returned `BT_QUERY_STATUS_OK` without a result.");
d72535e6 135 exec_status = (int) status;
c7eee084 136 if (query_exec->canceled) {
d94d92ac 137 BT_OBJECT_PUT_REF_AND_RESET(*user_result);
d72535e6 138 status = BT_QUERY_EXECUTOR_STATUS_CANCELED;
c7eee084 139 goto end;
c7eee084
PP
140 }
141
142end:
d72535e6 143 return exec_status;
c7eee084
PP
144}
145
d72535e6 146enum bt_query_executor_status bt_private_query_executor_cancel(
d94d92ac 147 struct bt_private_query_executor *priv_query_exec)
c7eee084 148{
d94d92ac 149 struct bt_query_executor *query_exec = (void *) priv_query_exec;
c7eee084 150
d94d92ac 151 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
c7eee084
PP
152 query_exec->canceled = BT_TRUE;
153 BT_LOGV("Canceled query executor: addr=%p", query_exec);
d72535e6 154 return BT_QUERY_EXECUTOR_STATUS_OK;
c7eee084
PP
155}
156
157bt_bool bt_query_executor_is_canceled(struct bt_query_executor *query_exec)
158{
d94d92ac
PP
159 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
160 return query_exec->canceled;
c7eee084 161}
This page took 0.032224 seconds and 4 git commands to generate.