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 | ||
47e5a032 | 29 | #include <babeltrace/plugin/component.h> |
de713ce0 | 30 | #include <babeltrace/plugin/component-internal.h> |
38b48196 JG |
31 | #include <babeltrace/plugin/source-internal.h> |
32 | #include <babeltrace/plugin/sink-internal.h> | |
de713ce0 JG |
33 | #include <babeltrace/babeltrace-internal.h> |
34 | #include <babeltrace/compiler.h> | |
b8a06801 | 35 | #include <babeltrace/ref.h> |
de713ce0 | 36 | |
7c7c0433 JG |
37 | static |
38 | struct bt_component * (* const component_create_funcs[])( | |
39 | struct bt_component_class *, struct bt_value *) = { | |
40 | [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_create, | |
41 | [BT_COMPONENT_TYPE_SINK] = bt_component_sink_create, | |
42 | }; | |
43 | ||
44 | static | |
45 | enum bt_component_status (* const component_validation_funcs[])( | |
46 | struct bt_component *) = { | |
47 | [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_validate, | |
48 | [BT_COMPONENT_TYPE_SINK] = bt_component_sink_validate, | |
49 | }; | |
50 | ||
b8a06801 JG |
51 | static |
52 | void bt_component_destroy(struct bt_object *obj) | |
53 | { | |
54 | struct bt_component *component = NULL; | |
55 | struct bt_component_class *component_class = NULL; | |
56 | ||
57 | if (!obj) { | |
58 | return; | |
59 | } | |
60 | ||
61 | component = container_of(obj, struct bt_component, base); | |
7c7c0433 JG |
62 | component_class = component->class; |
63 | ||
64 | /* | |
b8a06801 JG |
65 | * User data is destroyed first, followed by the concrete component |
66 | * instance. | |
67 | */ | |
7c7c0433 | 68 | if (component->user_destroy) { |
760051fa | 69 | component->user_destroy(component); |
7c7c0433 | 70 | } |
b8a06801 | 71 | |
ab09f844 JG |
72 | if (component->destroy) { |
73 | component->destroy(component); | |
74 | } | |
75 | ||
7c7c0433 | 76 | g_string_free(component->name, TRUE); |
b8a06801 | 77 | bt_put(component_class); |
7c7c0433 | 78 | g_free(component); |
b8a06801 JG |
79 | } |
80 | ||
81 | BT_HIDDEN | |
82 | enum bt_component_status bt_component_init(struct bt_component *component, | |
b8a06801 JG |
83 | bt_component_destroy_cb destroy) |
84 | { | |
85 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
86 | ||
ab09f844 | 87 | if (!component) { |
30d619df | 88 | ret = BT_COMPONENT_STATUS_INVALID; |
b8a06801 JG |
89 | goto end; |
90 | } | |
91 | ||
b8a06801 JG |
92 | component->destroy = destroy; |
93 | end: | |
94 | return ret; | |
95 | } | |
de713ce0 | 96 | |
38b48196 JG |
97 | BT_HIDDEN |
98 | enum bt_component_type bt_component_get_type(struct bt_component *component) | |
99 | { | |
100 | return component ? component->class->type : BT_COMPONENT_TYPE_UNKNOWN; | |
101 | } | |
102 | ||
103 | struct bt_component *bt_component_create( | |
7c7c0433 JG |
104 | struct bt_component_class *component_class, const char *name, |
105 | struct bt_value *params) | |
38b48196 | 106 | { |
7c7c0433 | 107 | int ret; |
38b48196 | 108 | struct bt_component *component = NULL; |
7c7c0433 | 109 | enum bt_component_type type; |
38b48196 JG |
110 | |
111 | if (!component_class) { | |
112 | goto end; | |
113 | } | |
114 | ||
7c7c0433 JG |
115 | type = bt_component_class_get_type(component_class); |
116 | if (type <= BT_COMPONENT_TYPE_UNKNOWN || | |
117 | type >= BT_COMPONENT_TYPE_FILTER) { | |
118 | /* Filter components are not supported yet. */ | |
119 | goto end; | |
120 | } | |
121 | ||
122 | component = component_create_funcs[type](component_class, params); | |
123 | if (!component) { | |
124 | goto end; | |
125 | } | |
126 | ||
127 | bt_object_init(component, bt_component_destroy); | |
7c7c0433 | 128 | component->name = g_string_new(name); |
4b70dd83 | 129 | if (!component->name) { |
7c7c0433 JG |
130 | BT_PUT(component); |
131 | goto end; | |
132 | } | |
133 | ||
fec2a9f2 | 134 | component->initializing = true; |
7c7c0433 | 135 | component_class->init(component, params); |
fec2a9f2 | 136 | component->initializing = false; |
7c7c0433 | 137 | ret = component_validation_funcs[type](component); |
692b38d2 | 138 | if (ret != BT_COMPONENT_STATUS_OK) { |
7c7c0433 | 139 | BT_PUT(component); |
38b48196 JG |
140 | goto end; |
141 | } | |
142 | end: | |
143 | return component; | |
144 | } | |
145 | ||
de713ce0 JG |
146 | const char *bt_component_get_name(struct bt_component *component) |
147 | { | |
148 | const char *ret = NULL; | |
149 | ||
150 | if (!component) { | |
151 | goto end; | |
152 | } | |
153 | ||
154 | ret = component->name->str; | |
155 | end: | |
156 | return ret; | |
157 | } | |
158 | ||
159 | enum bt_component_status bt_component_set_name(struct bt_component *component, | |
160 | const char *name) | |
161 | { | |
162 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
163 | ||
164 | if (!component || !name || name[0] == '\0') { | |
30d619df | 165 | ret = BT_COMPONENT_STATUS_INVALID; |
de713ce0 JG |
166 | goto end; |
167 | } | |
168 | ||
169 | g_string_assign(component->name, name); | |
170 | end: | |
171 | return ret; | |
172 | } | |
173 | ||
38b48196 JG |
174 | struct bt_component_class *bt_component_get_class( |
175 | struct bt_component *component) | |
de713ce0 | 176 | { |
38b48196 | 177 | return component ? bt_get(component->class) : NULL; |
de713ce0 JG |
178 | } |
179 | ||
de713ce0 JG |
180 | void *bt_component_get_private_data(struct bt_component *component) |
181 | { | |
38b48196 | 182 | return component ? component->user_data : NULL; |
de713ce0 JG |
183 | } |
184 | ||
185 | enum bt_component_status | |
186 | bt_component_set_private_data(struct bt_component *component, | |
187 | void *data) | |
188 | { | |
189 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
190 | ||
fec2a9f2 | 191 | if (!component || !component->initializing) { |
30d619df | 192 | ret = BT_COMPONENT_STATUS_INVALID; |
de713ce0 JG |
193 | goto end; |
194 | } | |
195 | ||
196 | component->user_data = data; | |
197 | end: | |
198 | return ret; | |
199 | } | |
42d7dce5 JG |
200 | |
201 | enum bt_component_status bt_component_set_destroy_cb( | |
202 | struct bt_component *component, bt_component_destroy_cb destroy) | |
203 | { | |
204 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
205 | ||
fec2a9f2 | 206 | if (!component || !component->initializing) { |
42d7dce5 JG |
207 | ret = BT_COMPONENT_STATUS_INVALID; |
208 | goto end; | |
209 | } | |
210 | ||
211 | component->user_destroy = destroy; | |
212 | end: | |
213 | return ret; | |
214 | } |