Move component iterator creation to base component class
[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 };
45
46 static
47 enum bt_component_status (* const component_validation_funcs[])(
48 struct bt_component *) = {
49 [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_validate,
50 [BT_COMPONENT_TYPE_SINK] = bt_component_sink_validate,
51 };
52
53 static
54 void bt_component_destroy(struct bt_object *obj)
55 {
56 struct bt_component *component = NULL;
57 struct bt_component_class *component_class = NULL;
58
59 if (!obj) {
60 return;
61 }
62
63 component = container_of(obj, struct bt_component, base);
64 component_class = component->class;
65
66 /*
67 * User data is destroyed first, followed by the concrete component
68 * instance.
69 */
70 if (component->user_destroy) {
71 component->user_destroy(component);
72 }
73
74 if (component->destroy) {
75 component->destroy(component);
76 }
77
78 g_string_free(component->name, TRUE);
79 bt_put(component_class);
80 g_free(component);
81 }
82
83 BT_HIDDEN
84 enum bt_component_status bt_component_init(struct bt_component *component,
85 bt_component_destroy_cb destroy)
86 {
87 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
88
89 if (!component) {
90 ret = BT_COMPONENT_STATUS_INVALID;
91 goto end;
92 }
93
94 component->destroy = destroy;
95 end:
96 return ret;
97 }
98
99 BT_HIDDEN
100 enum bt_component_type bt_component_get_type(struct bt_component *component)
101 {
102 return component ? component->class->type : BT_COMPONENT_TYPE_UNKNOWN;
103 }
104
105 BT_HIDDEN
106 struct bt_notification_iterator *bt_component_create_iterator(
107 struct bt_component *component)
108 {
109 enum bt_notification_iterator_status ret_iterator;
110 enum bt_component_type component_type;
111 struct bt_notification_iterator *iterator = NULL;
112
113 if (!component) {
114 goto error;
115 }
116
117 component_type = bt_component_get_type(component);
118 if (component_type != BT_COMPONENT_TYPE_SOURCE &&
119 component_type != BT_COMPONENT_TYPE_FILTER) {
120 /* Unsupported operation. */
121 goto error;
122 }
123
124 iterator = bt_notification_iterator_create(component);
125 if (!iterator) {
126 goto error;
127 }
128
129 switch (component_type) {
130 case BT_COMPONENT_TYPE_SOURCE:
131 {
132 struct bt_component_source *source;
133 enum bt_component_status ret_component;
134
135 source = container_of(component, struct bt_component_source, parent);
136 assert(source->init_iterator);
137 ret_component = source->init_iterator(component, iterator);
138 if (ret_component != BT_COMPONENT_STATUS_OK) {
139 goto error;
140 }
141 break;
142
143 break;
144 }
145 case BT_COMPONENT_TYPE_FILTER:
146 {
147 struct bt_component_filter *filter;
148 enum bt_component_status ret_component;
149
150 filter = container_of(component, struct bt_component_filter, parent);
151 assert(filter->init_iterator);
152 ret_component = filter->init_iterator(component, iterator);
153 if (ret_component != BT_COMPONENT_STATUS_OK) {
154 goto error;
155 }
156 break;
157 }
158 default:
159 /* Unreachable. */
160 assert(0);
161 }
162
163 ret_iterator = bt_notification_iterator_validate(iterator);
164 if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
165 goto error;
166 }
167
168 return iterator;
169 error:
170 BT_PUT(iterator);
171 return iterator;
172 }
173
174 struct bt_component *bt_component_create(
175 struct bt_component_class *component_class, const char *name,
176 struct bt_value *params)
177 {
178 int ret;
179 struct bt_component *component = NULL;
180 enum bt_component_type type;
181
182 if (!component_class) {
183 goto end;
184 }
185
186 type = bt_component_class_get_type(component_class);
187 if (type <= BT_COMPONENT_TYPE_UNKNOWN ||
188 type >= BT_COMPONENT_TYPE_FILTER) {
189 /* Filter components are not supported yet. */
190 goto end;
191 }
192
193 component = component_create_funcs[type](component_class, params);
194 if (!component) {
195 goto end;
196 }
197
198 bt_object_init(component, bt_component_destroy);
199 component->name = g_string_new(name);
200 if (!component->name) {
201 BT_PUT(component);
202 goto end;
203 }
204
205 component->initializing = true;
206 component_class->init(component, params);
207 component->initializing = false;
208 ret = component_validation_funcs[type](component);
209 if (ret != BT_COMPONENT_STATUS_OK) {
210 BT_PUT(component);
211 goto end;
212 }
213 end:
214 return component;
215 }
216
217 const char *bt_component_get_name(struct bt_component *component)
218 {
219 const char *ret = NULL;
220
221 if (!component) {
222 goto end;
223 }
224
225 ret = component->name->str;
226 end:
227 return ret;
228 }
229
230 enum bt_component_status bt_component_set_name(struct bt_component *component,
231 const char *name)
232 {
233 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
234
235 if (!component || !name || name[0] == '\0') {
236 ret = BT_COMPONENT_STATUS_INVALID;
237 goto end;
238 }
239
240 g_string_assign(component->name, name);
241 end:
242 return ret;
243 }
244
245 struct bt_component_class *bt_component_get_class(
246 struct bt_component *component)
247 {
248 return component ? bt_get(component->class) : NULL;
249 }
250
251 void *bt_component_get_private_data(struct bt_component *component)
252 {
253 return component ? component->user_data : NULL;
254 }
255
256 enum bt_component_status
257 bt_component_set_private_data(struct bt_component *component,
258 void *data)
259 {
260 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
261
262 if (!component || !component->initializing) {
263 ret = BT_COMPONENT_STATUS_INVALID;
264 goto end;
265 }
266
267 component->user_data = data;
268 end:
269 return ret;
270 }
271
272 enum bt_component_status bt_component_set_destroy_cb(
273 struct bt_component *component, bt_component_destroy_cb destroy)
274 {
275 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
276
277 if (!component || !component->initializing) {
278 ret = BT_COMPONENT_STATUS_INVALID;
279 goto end;
280 }
281
282 component->user_destroy = destroy;
283 end:
284 return ret;
285 }
This page took 0.035875 seconds and 5 git commands to generate.