Add bt_plugin test
[babeltrace.git] / lib / component / component.c
1 /*
2 * component.c
3 *
4 * Babeltrace Plugin Component
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/component/component.h>
30 #include <babeltrace/component/component-internal.h>
31 #include <babeltrace/component/source-internal.h>
32 #include <babeltrace/component/filter-internal.h>
33 #include <babeltrace/component/notification/iterator-internal.h>
34 #include <babeltrace/component/sink-internal.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/compiler.h>
37 #include <babeltrace/ref.h>
38
39 static
40 struct bt_component * (* const component_create_funcs[])(
41 struct bt_component_class *, struct bt_value *) = {
42 [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_create,
43 [BT_COMPONENT_TYPE_SINK] = bt_component_sink_create,
44 [BT_COMPONENT_TYPE_FILTER] = bt_component_filter_create,
45 };
46
47 static
48 enum bt_component_status (* const component_validation_funcs[])(
49 struct bt_component *) = {
50 [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_validate,
51 [BT_COMPONENT_TYPE_SINK] = bt_component_sink_validate,
52 [BT_COMPONENT_TYPE_FILTER] = bt_component_filter_validate,
53 };
54
55 static
56 void bt_component_destroy(struct bt_object *obj)
57 {
58 struct bt_component *component = NULL;
59 struct bt_component_class *component_class = NULL;
60
61 if (!obj) {
62 return;
63 }
64
65 component = container_of(obj, struct bt_component, base);
66 component_class = component->class;
67
68 /*
69 * User data is destroyed first, followed by the concrete component
70 * instance.
71 */
72 if (component->user_destroy) {
73 component->user_destroy(component);
74 }
75
76 if (component->destroy) {
77 component->destroy(component);
78 }
79
80 g_string_free(component->name, TRUE);
81 bt_put(component_class);
82 g_free(component);
83 }
84
85 BT_HIDDEN
86 enum bt_component_status bt_component_init(struct bt_component *component,
87 bt_component_destroy_cb destroy)
88 {
89 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
90
91 if (!component) {
92 ret = BT_COMPONENT_STATUS_INVALID;
93 goto end;
94 }
95
96 component->destroy = destroy;
97 end:
98 return ret;
99 }
100
101 BT_HIDDEN
102 enum bt_component_type bt_component_get_type(struct bt_component *component)
103 {
104 return component ? component->class->type : BT_COMPONENT_TYPE_UNKNOWN;
105 }
106
107 BT_HIDDEN
108 struct bt_notification_iterator *bt_component_create_iterator(
109 struct bt_component *component)
110 {
111 enum bt_notification_iterator_status ret_iterator;
112 enum bt_component_type component_type;
113 struct bt_notification_iterator *iterator = NULL;
114
115 if (!component) {
116 goto error;
117 }
118
119 component_type = bt_component_get_type(component);
120 if (component_type != BT_COMPONENT_TYPE_SOURCE &&
121 component_type != BT_COMPONENT_TYPE_FILTER) {
122 /* Unsupported operation. */
123 goto error;
124 }
125
126 iterator = bt_notification_iterator_create(component);
127 if (!iterator) {
128 goto error;
129 }
130
131 switch (component_type) {
132 case BT_COMPONENT_TYPE_SOURCE:
133 {
134 struct bt_component_source *source;
135 enum bt_component_status ret_component;
136
137 source = container_of(component, struct bt_component_source, parent);
138 assert(source->init_iterator);
139 ret_component = source->init_iterator(component, iterator);
140 if (ret_component != BT_COMPONENT_STATUS_OK) {
141 goto error;
142 }
143 break;
144
145 break;
146 }
147 case BT_COMPONENT_TYPE_FILTER:
148 {
149 struct bt_component_filter *filter;
150 enum bt_component_status ret_component;
151
152 filter = container_of(component, struct bt_component_filter, parent);
153 assert(filter->init_iterator);
154 ret_component = filter->init_iterator(component, iterator);
155 if (ret_component != BT_COMPONENT_STATUS_OK) {
156 goto error;
157 }
158 break;
159 }
160 default:
161 /* Unreachable. */
162 assert(0);
163 }
164
165 ret_iterator = bt_notification_iterator_validate(iterator);
166 if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
167 goto error;
168 }
169
170 return iterator;
171 error:
172 BT_PUT(iterator);
173 return iterator;
174 }
175
176 struct bt_component *bt_component_create(
177 struct bt_component_class *component_class, const char *name,
178 struct bt_value *params)
179 {
180 int ret;
181 struct bt_component *component = NULL;
182 enum bt_component_type type;
183
184 if (!component_class) {
185 goto end;
186 }
187
188 type = bt_component_class_get_type(component_class);
189 if (type <= BT_COMPONENT_TYPE_UNKNOWN ||
190 type > BT_COMPONENT_TYPE_FILTER) {
191 goto end;
192 }
193
194 component = component_create_funcs[type](component_class, params);
195 if (!component) {
196 goto end;
197 }
198
199 bt_object_init(component, bt_component_destroy);
200 component->name = g_string_new(name);
201 if (!component->name) {
202 BT_PUT(component);
203 goto end;
204 }
205
206 component->initializing = true;
207 ret = component_class->init(component, params);
208 component->initializing = false;
209 if (ret != BT_COMPONENT_STATUS_OK) {
210 BT_PUT(component);
211 goto end;
212 }
213 ret = component_validation_funcs[type](component);
214 if (ret != BT_COMPONENT_STATUS_OK) {
215 BT_PUT(component);
216 goto end;
217 }
218 end:
219 return component;
220 }
221
222 const char *bt_component_get_name(struct bt_component *component)
223 {
224 const char *ret = NULL;
225
226 if (!component) {
227 goto end;
228 }
229
230 ret = component->name->str;
231 end:
232 return ret;
233 }
234
235 enum bt_component_status bt_component_set_name(struct bt_component *component,
236 const char *name)
237 {
238 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
239
240 if (!component || !name || name[0] == '\0') {
241 ret = BT_COMPONENT_STATUS_INVALID;
242 goto end;
243 }
244
245 g_string_assign(component->name, name);
246 end:
247 return ret;
248 }
249
250 struct bt_component_class *bt_component_get_class(
251 struct bt_component *component)
252 {
253 return component ? bt_get(component->class) : NULL;
254 }
255
256 void *bt_component_get_private_data(struct bt_component *component)
257 {
258 return component ? component->user_data : NULL;
259 }
260
261 enum bt_component_status
262 bt_component_set_private_data(struct bt_component *component,
263 void *data)
264 {
265 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
266
267 if (!component || !component->initializing) {
268 ret = BT_COMPONENT_STATUS_INVALID;
269 goto end;
270 }
271
272 component->user_data = data;
273 end:
274 return ret;
275 }
276
277 enum bt_component_status bt_component_set_destroy_cb(
278 struct bt_component *component, bt_component_destroy_cb destroy)
279 {
280 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
281
282 if (!component || !component->initializing) {
283 ret = BT_COMPONENT_STATUS_INVALID;
284 goto end;
285 }
286
287 component->user_destroy = destroy;
288 end:
289 return ret;
290 }
This page took 0.038177 seconds and 4 git commands to generate.