Prefix {source,filter,sink}*.h file names with component-
[babeltrace.git] / lib / component / component-class.c
CommitLineData
fb2dcc52
JG
1/*
2 * component-class.c
3 *
4 * Babeltrace Plugin Component Class
33b34c43 5 *
3310b217 6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
fb2dcc52
JG
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/compiler.h>
33b34c43 30#include <babeltrace/component/component-class-internal.h>
b8a06801 31#include <babeltrace/ref.h>
fb2dcc52
JG
32#include <glib.h>
33
34static
b8a06801 35void bt_component_class_destroy(struct bt_object *obj)
fb2dcc52
JG
36{
37 struct bt_component_class *class;
33b34c43 38 int i;
fb2dcc52 39
b8a06801
JG
40 assert(obj);
41 class = container_of(obj, struct bt_component_class, base);
33b34c43
PP
42
43 /* Call destroy listeners in reverse registration order */
44 for (i = class->destroy_listeners->len - 1; i >= 0; i--) {
d3e4dcd8 45 struct bt_component_class_destroy_listener *listener =
33b34c43 46 &g_array_index(class->destroy_listeners,
d3e4dcd8 47 struct bt_component_class_destroy_listener,
33b34c43
PP
48 i);
49
50 listener->func(class, listener->data);
51 }
52
fb2dcc52
JG
53 if (class->name) {
54 g_string_free(class->name, TRUE);
55 }
7c7c0433
JG
56 if (class->description) {
57 g_string_free(class->description, TRUE);
58 }
33b34c43
PP
59 if (class->destroy_listeners) {
60 g_array_free(class->destroy_listeners, TRUE);
61 }
b8a06801 62
fb2dcc52
JG
63 g_free(class);
64}
65
d3e4dcd8
PP
66static
67int bt_component_class_init(struct bt_component_class *class,
68 enum bt_component_class_type type, const char *name)
fb2dcc52 69{
d3e4dcd8
PP
70 int ret = 0;
71
72 bt_object_init(class, bt_component_class_destroy);
73 class->type = type;
74 class->name = g_string_new(name);
75 if (!class->name) {
76 goto error;
77 }
78
79 class->description = g_string_new(NULL);
80 if (!class->description) {
81 goto error;
82 }
83
84 class->destroy_listeners = g_array_new(FALSE, TRUE,
85 sizeof(struct bt_component_class_destroy_listener));
86 if (!class->destroy_listeners) {
87 goto error;
88 }
89
90 goto end;
6ba0b073 91
d3e4dcd8
PP
92error:
93 BT_PUT(class);
94 ret = -1;
95
96end:
97 return ret;
98}
99
100struct bt_component_class *bt_component_class_source_create(const char *name,
101 bt_component_class_source_init_iterator_method init_iterator_method)
102{
103 struct bt_component_class_source *source_class = NULL;
104 int ret;
105
106 if (!name || !init_iterator_method) {
6ba0b073
PP
107 goto end;
108 }
fb2dcc52 109
d3e4dcd8
PP
110 source_class = g_new0(struct bt_component_class_source, 1);
111 if (!source_class) {
fb2dcc52
JG
112 goto end;
113 }
114
d3e4dcd8
PP
115 ret = bt_component_class_init(&source_class->parent,
116 BT_COMPONENT_CLASS_TYPE_SOURCE, name);
117 if (ret) {
118 /*
119 * If bt_component_class_init() fails, the component
120 * class is put, therefore its memory is already
121 * freed.
122 */
123 source_class = NULL;
124 goto end;
125 }
126
127 source_class->methods.init_iterator = init_iterator_method;
128
129end:
130 return &source_class->parent;
131}
132
133struct bt_component_class *bt_component_class_filter_create(const char *name,
134 bt_component_class_filter_init_iterator_method init_iterator_method)
135{
136 struct bt_component_class_filter *filter_class = NULL;
137 int ret;
138
139 if (!name || !init_iterator_method) {
fb2dcc52
JG
140 goto end;
141 }
6ba0b073 142
d3e4dcd8
PP
143 filter_class = g_new0(struct bt_component_class_filter, 1);
144 if (!filter_class) {
145 goto end;
6ba0b073
PP
146 }
147
d3e4dcd8
PP
148 ret = bt_component_class_init(&filter_class->parent,
149 BT_COMPONENT_CLASS_TYPE_FILTER, name);
150 if (ret) {
151 /*
152 * If bt_component_class_init() fails, the component
153 * class is put, therefore its memory is already
154 * freed.
155 */
156 filter_class = NULL;
33b34c43
PP
157 goto end;
158 }
d3e4dcd8
PP
159
160 filter_class->methods.init_iterator = init_iterator_method;
161
fb2dcc52 162end:
d3e4dcd8
PP
163 return &filter_class->parent;
164}
165
166struct bt_component_class *bt_component_class_sink_create(const char *name,
167 bt_component_class_sink_consume_method consume_method)
168{
169 struct bt_component_class_sink *sink_class = NULL;
170 int ret;
171
172 if (!name || !consume_method) {
173 goto end;
174 }
175
176 sink_class = g_new0(struct bt_component_class_sink, 1);
177 if (!sink_class) {
178 goto end;
179 }
180
181 ret = bt_component_class_init(&sink_class->parent,
182 BT_COMPONENT_CLASS_TYPE_SINK, name);
183 if (ret) {
184 /*
185 * If bt_component_class_init() fails, the component
186 * class is put, therefore its memory is already
187 * freed.
188 */
189 sink_class = NULL;
190 goto end;
191 }
192
193 sink_class->methods.consume = consume_method;
194
195end:
196 return &sink_class->parent;
197}
198
199int bt_component_class_set_init_method(
200 struct bt_component_class *component_class,
201 bt_component_class_init_method init_method)
202{
203 int ret = 0;
204
205 if (!component_class || !init_method) {
206 ret = -1;
207 goto end;
208 }
209
210 component_class->methods.init = init_method;
211
212end:
213 return ret;
214}
215
216int bt_component_class_set_destroy_method(
217 struct bt_component_class *component_class,
218 bt_component_class_destroy_method destroy_method)
219{
220 int ret = 0;
221
222 if (!component_class || !destroy_method) {
223 ret = -1;
224 goto end;
225 }
226
227 component_class->methods.destroy = destroy_method;
228
229end:
230 return ret;
231}
232
233extern int bt_component_class_set_description(
234 struct bt_component_class *component_class,
235 const char *description)
236{
237 int ret = 0;
238
239 if (!component_class || !description) {
240 ret = -1;
241 goto end;
242 }
243
244 g_string_assign(component_class->description, description);
245
246end:
247 return ret;
fb2dcc52 248}
38b48196
JG
249
250const char *bt_component_class_get_name(
251 struct bt_component_class *component_class)
252{
253 return component_class ? component_class->name->str : NULL;
254}
255
d3e4dcd8 256enum bt_component_class_type bt_component_class_get_type(
38b48196
JG
257 struct bt_component_class *component_class)
258{
259 return component_class ? component_class->type :
d3e4dcd8 260 BT_COMPONENT_CLASS_TYPE_UNKNOWN;
38b48196
JG
261}
262
33b34c43 263const char *bt_component_class_get_description(
38b48196
JG
264 struct bt_component_class *component_class)
265{
6ba0b073
PP
266 return component_class && component_class->description ?
267 component_class->description->str : NULL;
38b48196 268}
7c7c0433 269
33b34c43
PP
270BT_HIDDEN
271int bt_component_class_add_destroy_listener(struct bt_component_class *class,
272 bt_component_class_destroy_listener_func func, void *data)
7c7c0433 273{
33b34c43 274 int ret = 0;
d3e4dcd8 275 struct bt_component_class_destroy_listener listener;
33b34c43
PP
276
277 if (!class || !func) {
278 ret = -1;
279 goto end;
280 }
281
282 listener.func = func;
283 listener.data = data;
284 g_array_append_val(class->destroy_listeners, listener);
285
286end:
287 return ret;
7c7c0433 288}
d3e4dcd8
PP
289
290extern int bt_component_class_sink_set_add_iterator_method(
291 struct bt_component_class *component_class,
292 bt_component_class_sink_add_iterator_method add_iterator_method)
293{
294 struct bt_component_class_sink *sink_class;
295 int ret = 0;
296
297 if (!component_class || !add_iterator_method ||
298 component_class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
299 ret = -1;
300 goto end;
301 }
302
303 sink_class = container_of(component_class,
304 struct bt_component_class_sink, parent);
305 sink_class->methods.add_iterator = add_iterator_method;
306
307end:
308 return ret;
309}
310
311extern int bt_component_class_filter_set_add_iterator_method(
312 struct bt_component_class *component_class,
313 bt_component_class_filter_add_iterator_method add_iterator_method)
314{
315 struct bt_component_class_filter *filter_class;
316 int ret = 0;
317
318 if (!component_class || !add_iterator_method ||
319 component_class->type !=
320 BT_COMPONENT_CLASS_TYPE_FILTER) {
321 ret = -1;
322 goto end;
323 }
324
325 filter_class = container_of(component_class,
326 struct bt_component_class_filter, parent);
327 filter_class->methods.add_iterator = add_iterator_method;
328
329end:
330 return ret;
331}
This page took 0.039321 seconds and 4 git commands to generate.