lib: strictly type function return status enumerations
[babeltrace.git] / src / lib / graph / query-executor.c
CommitLineData
1286dcbb 1/*
f2b0325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
1286dcbb
PP
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
b03487ab 23#define BT_LOG_TAG "LIB/QUERY-EXECUTOR"
1633ef46 24#include "lib/logging.h"
1286dcbb 25
57952005 26#include "common/assert.h"
83da519a 27#include "common/common.h"
57952005 28#include "lib/assert-pre.h"
3b7d3ccc 29#include "lib/assert-post.h"
71c5da58
MJ
30#include <babeltrace2/graph/query-executor-const.h>
31#include <babeltrace2/graph/query-executor.h>
71c5da58 32#include <babeltrace2/graph/component-class.h>
71c5da58
MJ
33#include <babeltrace2/value.h>
34#include <babeltrace2/value-const.h>
57952005
MJ
35#include "lib/object.h"
36#include "compat/compiler.h"
37
38#include "component-class.h"
39#include "query-executor.h"
fb25b9e3 40#include "lib/func-status.h"
1286dcbb
PP
41
42static
43void bt_query_executor_destroy(struct bt_object *obj)
44{
45 struct bt_query_executor *query_exec =
46 container_of(obj, struct bt_query_executor, base);
47
834e9996 48 BT_LOGD("Destroying query executor: addr=%p", query_exec);
1286dcbb
PP
49 g_free(query_exec);
50}
51
7b53201c 52struct bt_query_executor *bt_query_executor_create(void)
1286dcbb
PP
53{
54 struct bt_query_executor *query_exec;
55
56 BT_LOGD_STR("Creating query executor.");
57 query_exec = g_new0(struct bt_query_executor, 1);
58 if (!query_exec) {
59 BT_LOGE_STR("Failed to allocate one query executor.");
60 goto end;
61 }
62
1d7bf349
PP
63 bt_object_init_shared(&query_exec->base,
64 bt_query_executor_destroy);
1286dcbb
PP
65 BT_LOGD("Created query executor: addr=%p", query_exec);
66
67end:
834e9996 68 return (void *) query_exec;
1286dcbb
PP
69}
70
fb25b9e3 71enum bt_query_executor_query_status bt_query_executor_query(
7b53201c
PP
72 struct bt_query_executor *query_exec,
73 const struct bt_component_class *comp_cls,
ce141536 74 const char *object, const struct bt_value *params,
fb25b9e3 75 enum bt_logging_level log_level,
ce141536 76 const struct bt_value **user_result)
1286dcbb 77{
fb25b9e3
PP
78 typedef enum bt_component_class_query_method_status (*method_t)(void *,
79 const void *, const void *, const void *, enum bt_logging_level,
80 const void *);
1286dcbb 81
fb25b9e3
PP
82 enum bt_query_executor_query_status status;
83 enum bt_component_class_query_method_status query_status;
834e9996 84 method_t method = NULL;
1286dcbb 85
834e9996
PP
86 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
87 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
88 BT_ASSERT_PRE_NON_NULL(object, "Object");
89 BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)");
90 BT_ASSERT_PRE(!query_exec->canceled, "Query executor is canceled.");
91
92 if (!params) {
93 params = bt_value_null;
1286dcbb
PP
94 }
95
834e9996
PP
96 switch (comp_cls->type) {
97 case BT_COMPONENT_CLASS_TYPE_SOURCE:
98 {
99 struct bt_component_class_source *src_cc = (void *) comp_cls;
100
101 method = (method_t) src_cc->methods.query;
102 break;
1286dcbb 103 }
834e9996
PP
104 case BT_COMPONENT_CLASS_TYPE_FILTER:
105 {
106 struct bt_component_class_filter *flt_cc = (void *) comp_cls;
1286dcbb 107
834e9996
PP
108 method = (method_t) flt_cc->methods.query;
109 break;
1286dcbb 110 }
834e9996
PP
111 case BT_COMPONENT_CLASS_TYPE_SINK:
112 {
113 struct bt_component_class_sink *sink_cc = (void *) comp_cls;
1286dcbb 114
834e9996
PP
115 method = (method_t) sink_cc->methods.query;
116 break;
117 }
118 default:
119 abort();
1286dcbb
PP
120 }
121
834e9996 122 if (!method) {
1286dcbb 123 /* Not an error: nothing to query */
834e9996
PP
124 BT_LIB_LOGD("Component class has no registered query method: "
125 "%!+C", comp_cls);
fb25b9e3 126 status = BT_FUNC_STATUS_UNSUPPORTED;
1286dcbb
PP
127 goto end;
128 }
129
834e9996 130 BT_LIB_LOGD("Calling user's query method: "
83da519a
PP
131 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
132 "log-level=%s",
133 query_exec, comp_cls, object, params,
134 bt_common_logging_level_string(log_level));
834e9996 135 *user_result = NULL;
fb25b9e3 136 query_status = method((void *) comp_cls, query_exec, object, params,
83da519a 137 log_level, user_result);
834e9996 138 BT_LIB_LOGD("User method returned: status=%s, %![res-]+v",
fb25b9e3
PP
139 bt_common_func_status_string(query_status), *user_result);
140 BT_ASSERT_POST(query_status != BT_FUNC_STATUS_OK || *user_result,
141 "User method returned `BT_FUNC_STATUS_OK` without a result.");
142 status = (int) query_status;
1286dcbb 143 if (query_exec->canceled) {
834e9996 144 BT_OBJECT_PUT_REF_AND_RESET(*user_result);
fb25b9e3 145 status = BT_FUNC_STATUS_CANCELED;
1286dcbb 146 goto end;
1286dcbb
PP
147 }
148
149end:
fb25b9e3 150 return status;
1286dcbb
PP
151}
152
fb25b9e3 153enum bt_query_executor_cancel_status bt_query_executor_cancel(
7b53201c 154 struct bt_query_executor *query_exec)
1286dcbb 155{
834e9996 156 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
1286dcbb 157 query_exec->canceled = BT_TRUE;
a684a357 158 BT_LOGI("Canceled query executor: addr=%p", query_exec);
fb25b9e3 159 return BT_FUNC_STATUS_OK;
1286dcbb
PP
160}
161
7b53201c 162bt_bool bt_query_executor_is_canceled(const struct bt_query_executor *query_exec)
1286dcbb 163{
834e9996
PP
164 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
165 return query_exec->canceled;
1286dcbb 166}
8c6884d9
PP
167
168void bt_query_executor_get_ref(const struct bt_query_executor *query_executor)
169{
170 bt_object_get_ref(query_executor);
171}
172
173void bt_query_executor_put_ref(const struct bt_query_executor *query_executor)
174{
175 bt_object_put_ref(query_executor);
176}
This page took 0.040296 seconds and 4 git commands to generate.