Commit | Line | Data |
---|---|---|
de713ce0 JG |
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 | ||
33b34c43 PP |
29 | #include <babeltrace/component/component.h> |
30 | #include <babeltrace/component/component-internal.h> | |
d3e4dcd8 | 31 | #include <babeltrace/component/component-class-internal.h> |
d71dcf2c PP |
32 | #include <babeltrace/component/component-source-internal.h> |
33 | #include <babeltrace/component/component-filter-internal.h> | |
34 | #include <babeltrace/component/component-sink-internal.h> | |
33b34c43 | 35 | #include <babeltrace/component/notification/iterator-internal.h> |
de713ce0 JG |
36 | #include <babeltrace/babeltrace-internal.h> |
37 | #include <babeltrace/compiler.h> | |
b8a06801 | 38 | #include <babeltrace/ref.h> |
de713ce0 | 39 | |
7c7c0433 JG |
40 | static |
41 | struct bt_component * (* const component_create_funcs[])( | |
42 | struct bt_component_class *, struct bt_value *) = { | |
d3e4dcd8 PP |
43 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create, |
44 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create, | |
45 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create, | |
7c7c0433 JG |
46 | }; |
47 | ||
48 | static | |
49 | enum bt_component_status (* const component_validation_funcs[])( | |
50 | struct bt_component *) = { | |
d3e4dcd8 PP |
51 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_validate, |
52 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_validate, | |
53 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_validate, | |
7c7c0433 JG |
54 | }; |
55 | ||
b8a06801 JG |
56 | static |
57 | void bt_component_destroy(struct bt_object *obj) | |
58 | { | |
59 | struct bt_component *component = NULL; | |
60 | struct bt_component_class *component_class = NULL; | |
61 | ||
62 | if (!obj) { | |
63 | return; | |
64 | } | |
65 | ||
66 | component = container_of(obj, struct bt_component, base); | |
7c7c0433 JG |
67 | component_class = component->class; |
68 | ||
69 | /* | |
b8a06801 JG |
70 | * User data is destroyed first, followed by the concrete component |
71 | * instance. | |
72 | */ | |
d3e4dcd8 PP |
73 | if (component->class->methods.destroy) { |
74 | component->class->methods.destroy(component); | |
7c7c0433 | 75 | } |
b8a06801 | 76 | |
ab09f844 JG |
77 | if (component->destroy) { |
78 | component->destroy(component); | |
79 | } | |
80 | ||
7c7c0433 | 81 | g_string_free(component->name, TRUE); |
b8a06801 | 82 | bt_put(component_class); |
7c7c0433 | 83 | g_free(component); |
b8a06801 JG |
84 | } |
85 | ||
86 | BT_HIDDEN | |
87 | enum bt_component_status bt_component_init(struct bt_component *component, | |
d3e4dcd8 | 88 | bt_component_class_destroy_method destroy) |
b8a06801 JG |
89 | { |
90 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
91 | ||
ab09f844 | 92 | if (!component) { |
30d619df | 93 | ret = BT_COMPONENT_STATUS_INVALID; |
b8a06801 JG |
94 | goto end; |
95 | } | |
96 | ||
b8a06801 JG |
97 | component->destroy = destroy; |
98 | end: | |
99 | return ret; | |
100 | } | |
de713ce0 | 101 | |
d3e4dcd8 PP |
102 | enum bt_component_class_type bt_component_get_class_type( |
103 | struct bt_component *component) | |
38b48196 | 104 | { |
d3e4dcd8 | 105 | return component ? component->class->type : BT_COMPONENT_CLASS_TYPE_UNKNOWN; |
38b48196 JG |
106 | } |
107 | ||
5645cd95 JG |
108 | BT_HIDDEN |
109 | struct bt_notification_iterator *bt_component_create_iterator( | |
110 | struct bt_component *component) | |
111 | { | |
112 | enum bt_notification_iterator_status ret_iterator; | |
d3e4dcd8 | 113 | enum bt_component_class_type type; |
5645cd95 | 114 | struct bt_notification_iterator *iterator = NULL; |
d3e4dcd8 | 115 | struct bt_component_class *class = component->class; |
5645cd95 JG |
116 | |
117 | if (!component) { | |
118 | goto error; | |
119 | } | |
120 | ||
d3e4dcd8 PP |
121 | type = bt_component_get_class_type(component); |
122 | if (type != BT_COMPONENT_CLASS_TYPE_SOURCE && | |
123 | type != BT_COMPONENT_CLASS_TYPE_FILTER) { | |
5645cd95 JG |
124 | /* Unsupported operation. */ |
125 | goto error; | |
126 | } | |
127 | ||
128 | iterator = bt_notification_iterator_create(component); | |
129 | if (!iterator) { | |
130 | goto error; | |
131 | } | |
132 | ||
d3e4dcd8 PP |
133 | switch (type) { |
134 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
5645cd95 | 135 | { |
d3e4dcd8 | 136 | struct bt_component_class_source *source_class; |
d3eb6e8f | 137 | enum bt_notification_iterator_status status; |
5645cd95 | 138 | |
d3e4dcd8 | 139 | source_class = container_of(class, struct bt_component_class_source, parent); |
d3eb6e8f PP |
140 | assert(source_class->methods.iterator.init); |
141 | status = source_class->methods.iterator.init(component, | |
142 | iterator); | |
143 | if (status < 0) { | |
5645cd95 JG |
144 | goto error; |
145 | } | |
146 | break; | |
5645cd95 | 147 | } |
d3e4dcd8 | 148 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
5645cd95 | 149 | { |
d3e4dcd8 | 150 | struct bt_component_class_filter *filter_class; |
d3eb6e8f | 151 | enum bt_notification_iterator_status status; |
5645cd95 | 152 | |
d3e4dcd8 | 153 | filter_class = container_of(class, struct bt_component_class_filter, parent); |
d3eb6e8f PP |
154 | assert(filter_class->methods.iterator.init); |
155 | status = filter_class->methods.iterator.init(component, | |
156 | iterator); | |
157 | if (status < 0) { | |
5645cd95 JG |
158 | goto error; |
159 | } | |
160 | break; | |
161 | } | |
162 | default: | |
163 | /* Unreachable. */ | |
164 | assert(0); | |
165 | } | |
166 | ||
167 | ret_iterator = bt_notification_iterator_validate(iterator); | |
168 | if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
169 | goto error; | |
170 | } | |
171 | ||
172 | return iterator; | |
173 | error: | |
174 | BT_PUT(iterator); | |
175 | return iterator; | |
176 | } | |
177 | ||
6358c163 | 178 | struct bt_component *bt_component_create_with_init_method_data( |
7c7c0433 | 179 | struct bt_component_class *component_class, const char *name, |
6358c163 | 180 | struct bt_value *params, void *init_method_data) |
38b48196 | 181 | { |
7c7c0433 | 182 | int ret; |
38b48196 | 183 | struct bt_component *component = NULL; |
d3e4dcd8 | 184 | enum bt_component_class_type type; |
38b48196 JG |
185 | |
186 | if (!component_class) { | |
187 | goto end; | |
188 | } | |
189 | ||
7c7c0433 | 190 | type = bt_component_class_get_type(component_class); |
d3e4dcd8 PP |
191 | if (type <= BT_COMPONENT_CLASS_TYPE_UNKNOWN || |
192 | type > BT_COMPONENT_CLASS_TYPE_FILTER) { | |
7c7c0433 JG |
193 | goto end; |
194 | } | |
195 | ||
196 | component = component_create_funcs[type](component_class, params); | |
197 | if (!component) { | |
198 | goto end; | |
199 | } | |
200 | ||
201 | bt_object_init(component, bt_component_destroy); | |
7c7c0433 | 202 | component->name = g_string_new(name); |
4b70dd83 | 203 | if (!component->name) { |
7c7c0433 JG |
204 | BT_PUT(component); |
205 | goto end; | |
206 | } | |
207 | ||
fec2a9f2 | 208 | component->initializing = true; |
d3e4dcd8 PP |
209 | |
210 | if (component_class->methods.init) { | |
6358c163 PP |
211 | ret = component_class->methods.init(component, params, |
212 | init_method_data); | |
d3e4dcd8 PP |
213 | component->initializing = false; |
214 | if (ret != BT_COMPONENT_STATUS_OK) { | |
215 | BT_PUT(component); | |
216 | goto end; | |
217 | } | |
528debdf | 218 | } |
d3e4dcd8 PP |
219 | |
220 | component->initializing = false; | |
7c7c0433 | 221 | ret = component_validation_funcs[type](component); |
692b38d2 | 222 | if (ret != BT_COMPONENT_STATUS_OK) { |
7c7c0433 | 223 | BT_PUT(component); |
38b48196 JG |
224 | goto end; |
225 | } | |
1e4d8103 PP |
226 | |
227 | bt_component_class_freeze(component->class); | |
38b48196 JG |
228 | end: |
229 | return component; | |
230 | } | |
231 | ||
6358c163 PP |
232 | struct bt_component *bt_component_create( |
233 | struct bt_component_class *component_class, const char *name, | |
234 | struct bt_value *params) | |
235 | { | |
236 | return bt_component_create_with_init_method_data(component_class, name, | |
237 | params, NULL); | |
238 | } | |
239 | ||
de713ce0 JG |
240 | const char *bt_component_get_name(struct bt_component *component) |
241 | { | |
242 | const char *ret = NULL; | |
243 | ||
244 | if (!component) { | |
245 | goto end; | |
246 | } | |
247 | ||
248 | ret = component->name->str; | |
249 | end: | |
250 | return ret; | |
251 | } | |
252 | ||
253 | enum bt_component_status bt_component_set_name(struct bt_component *component, | |
254 | const char *name) | |
255 | { | |
256 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
257 | ||
258 | if (!component || !name || name[0] == '\0') { | |
30d619df | 259 | ret = BT_COMPONENT_STATUS_INVALID; |
de713ce0 JG |
260 | goto end; |
261 | } | |
262 | ||
263 | g_string_assign(component->name, name); | |
264 | end: | |
265 | return ret; | |
266 | } | |
267 | ||
38b48196 JG |
268 | struct bt_component_class *bt_component_get_class( |
269 | struct bt_component *component) | |
de713ce0 | 270 | { |
38b48196 | 271 | return component ? bt_get(component->class) : NULL; |
de713ce0 JG |
272 | } |
273 | ||
de713ce0 JG |
274 | void *bt_component_get_private_data(struct bt_component *component) |
275 | { | |
38b48196 | 276 | return component ? component->user_data : NULL; |
de713ce0 JG |
277 | } |
278 | ||
279 | enum bt_component_status | |
280 | bt_component_set_private_data(struct bt_component *component, | |
281 | void *data) | |
282 | { | |
283 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
284 | ||
fec2a9f2 | 285 | if (!component || !component->initializing) { |
30d619df | 286 | ret = BT_COMPONENT_STATUS_INVALID; |
de713ce0 JG |
287 | goto end; |
288 | } | |
289 | ||
290 | component->user_data = data; | |
291 | end: | |
292 | return ret; | |
293 | } |