lib: strictly type function return status enumerations
[babeltrace.git] / src / lib / graph / component-class-sink-colander.c
1 /*
2 * Copyright 2017-2018 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 "LIB/COLANDER"
24 #include "lib/logging.h"
25
26 #include "common/assert.h"
27 #include "lib/assert-pre.h"
28 #include "lib/object.h"
29 #include <babeltrace2/graph/component-class-sink.h>
30 #include <babeltrace2/graph/self-component-sink.h>
31 #include <babeltrace2/graph/self-component-port.h>
32 #include <babeltrace2/graph/self-component-port-input-message-iterator.h>
33 #include <babeltrace2/graph/self-component.h>
34 #include <glib.h>
35
36 #include "component-class-sink-colander.h"
37 #include "lib/func-status.h"
38
39 static
40 struct bt_component_class_sink *colander_comp_cls;
41
42 static
43 enum bt_component_class_init_method_status colander_init(
44 struct bt_self_component_sink *self_comp,
45 const struct bt_value *params, void *init_method_data)
46 {
47 enum bt_component_class_init_method_status status =
48 BT_FUNC_STATUS_OK;
49 struct bt_component_class_sink_colander_priv_data *colander_data = NULL;
50 struct bt_component_class_sink_colander_data *user_provided_data =
51 init_method_data;
52
53 BT_ASSERT(init_method_data);
54 colander_data = g_new0(
55 struct bt_component_class_sink_colander_priv_data, 1);
56 if (!colander_data) {
57 BT_LOGE_STR("Failed to allocate colander data.");
58 status = BT_FUNC_STATUS_MEMORY_ERROR;
59 goto end;
60 }
61
62 colander_data->msgs = user_provided_data->msgs;
63 colander_data->count_addr = user_provided_data->count_addr;
64 status = bt_self_component_sink_add_input_port(self_comp, "in",
65 NULL, NULL);
66 if (status != BT_FUNC_STATUS_OK) {
67 BT_LOGE_STR("Cannot add input port.");
68 goto end;
69 }
70
71 bt_self_component_set_data(
72 bt_self_component_sink_as_self_component(self_comp),
73 colander_data);
74
75 end:
76 return status;
77 }
78
79 static
80 void colander_finalize(struct bt_self_component_sink *self_comp)
81 {
82 struct bt_component_class_sink_colander_priv_data *colander_data =
83 bt_self_component_get_data(
84 bt_self_component_sink_as_self_component(self_comp));
85
86 if (!colander_data) {
87 return;
88 }
89
90 BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
91 g_free(colander_data);
92 }
93
94 static
95 enum bt_component_class_sink_graph_is_configured_method_status
96 colander_graph_is_configured(
97 bt_self_component_sink *self_comp)
98 {
99 enum bt_component_class_sink_graph_is_configured_method_status status =
100 BT_FUNC_STATUS_OK;
101 struct bt_component_class_sink_colander_priv_data *colander_data =
102 bt_self_component_get_data(
103 bt_self_component_sink_as_self_component(self_comp));
104
105 struct bt_self_component_port_input *self_port =
106 bt_self_component_sink_borrow_input_port_by_name(self_comp, "in");
107 BT_ASSERT(self_port);
108
109 BT_ASSERT(colander_data);
110 BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
111 colander_data->msg_iter =
112 bt_self_component_port_input_message_iterator_create(
113 self_port);
114 if (!colander_data->msg_iter) {
115 BT_LIB_LOGE("Cannot create message iterator on "
116 "self component input port: %![port-]+p",
117 self_port);
118 status = BT_FUNC_STATUS_MEMORY_ERROR;
119 goto end;
120 }
121
122 end:
123 return status;
124 }
125
126 static
127 enum bt_component_class_sink_consume_method_status colander_consume(
128 struct bt_self_component_sink *self_comp)
129 {
130 enum bt_component_class_sink_consume_method_status status =
131 BT_FUNC_STATUS_OK;
132 enum bt_message_iterator_next_status next_status;
133 struct bt_component_class_sink_colander_priv_data *colander_data =
134 bt_self_component_get_data(
135 bt_self_component_sink_as_self_component(self_comp));
136 bt_message_array_const msgs;
137
138 BT_ASSERT(colander_data);
139 BT_ASSERT(colander_data->msg_iter);
140 next_status = bt_self_component_port_input_message_iterator_next(
141 colander_data->msg_iter, &msgs,
142 colander_data->count_addr);
143 switch (next_status) {
144 case BT_FUNC_STATUS_AGAIN:
145 status = BT_FUNC_STATUS_AGAIN;
146 goto end;
147 case BT_FUNC_STATUS_END:
148 status = BT_FUNC_STATUS_END;
149 goto end;
150 case BT_FUNC_STATUS_OK:
151 /* Move messages to user (count already set) */
152 memcpy(colander_data->msgs, msgs,
153 sizeof(*msgs) * *colander_data->count_addr);
154 break;
155 default:
156 status = BT_FUNC_STATUS_ERROR;
157 goto end;
158 }
159
160 end:
161 return status;
162 }
163
164 struct bt_component_class_sink *bt_component_class_sink_colander_get(void)
165 {
166 if (colander_comp_cls) {
167 goto end;
168 }
169
170 colander_comp_cls = bt_component_class_sink_create("colander",
171 colander_consume);
172 if (!colander_comp_cls) {
173 BT_LOGE_STR("Cannot create sink colander component class.");
174 goto end;
175 }
176
177 (void) bt_component_class_sink_set_init_method(
178 colander_comp_cls, colander_init);
179 (void) bt_component_class_sink_set_finalize_method(
180 colander_comp_cls, colander_finalize);
181 (void) bt_component_class_sink_set_graph_is_configured_method(
182 colander_comp_cls, colander_graph_is_configured);
183
184 end:
185 bt_object_get_ref(colander_comp_cls);
186 return colander_comp_cls;
187 }
188
189 __attribute__((destructor)) static
190 void put_colander(void) {
191 BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls);
192 }
This page took 0.032768 seconds and 4 git commands to generate.