340e8066475e5c540a18908e631ce0efca67b1d8
[babeltrace.git] / lib / plugin-system / 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/plugin/component.h>
30 #include <babeltrace/plugin/component-internal.h>
31 #include <babeltrace/plugin/source-internal.h>
32 #include <babeltrace/plugin/filter-internal.h>
33 #include <babeltrace/plugin/notification/iterator-internal.h>
34 #include <babeltrace/plugin/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 component_class->init(component, params);
208 component->initializing = false;
209 ret = component_validation_funcs[type](component);
210 if (ret != BT_COMPONENT_STATUS_OK) {
211 BT_PUT(component);
212 goto end;
213 }
214 end:
215 return component;
216 }
217
218 const char *bt_component_get_name(struct bt_component *component)
219 {
220 const char *ret = NULL;
221
222 if (!component) {
223 goto end;
224 }
225
226 ret = component->name->str;
227 end:
228 return ret;
229 }
230
231 enum bt_component_status bt_component_set_name(struct bt_component *component,
232 const char *name)
233 {
234 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
235
236 if (!component || !name || name[0] == '\0') {
237 ret = BT_COMPONENT_STATUS_INVALID;
238 goto end;
239 }
240
241 g_string_assign(component->name, name);
242 end:
243 return ret;
244 }
245
246 struct bt_component_class *bt_component_get_class(
247 struct bt_component *component)
248 {
249 return component ? bt_get(component->class) : NULL;
250 }
251
252 void *bt_component_get_private_data(struct bt_component *component)
253 {
254 return component ? component->user_data : NULL;
255 }
256
257 enum bt_component_status
258 bt_component_set_private_data(struct bt_component *component,
259 void *data)
260 {
261 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
262
263 if (!component || !component->initializing) {
264 ret = BT_COMPONENT_STATUS_INVALID;
265 goto end;
266 }
267
268 component->user_data = data;
269 end:
270 return ret;
271 }
272
273 enum bt_component_status bt_component_set_destroy_cb(
274 struct bt_component *component, bt_component_destroy_cb destroy)
275 {
276 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
277
278 if (!component || !component->initializing) {
279 ret = BT_COMPONENT_STATUS_INVALID;
280 goto end;
281 }
282
283 component->user_destroy = destroy;
284 end:
285 return ret;
286 }
This page took 0.033733 seconds and 3 git commands to generate.