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 JG |
30 | #include <babeltrace/plugin/component-internal.h> |
31 | #include <babeltrace/babeltrace-internal.h> | |
32 | #include <babeltrace/compiler.h> | |
b8a06801 | 33 | #include <babeltrace/ref.h> |
de713ce0 | 34 | |
b8a06801 JG |
35 | static |
36 | void bt_component_destroy(struct bt_object *obj) | |
37 | { | |
38 | struct bt_component *component = NULL; | |
39 | struct bt_component_class *component_class = NULL; | |
40 | ||
41 | if (!obj) { | |
42 | return; | |
43 | } | |
44 | ||
45 | component = container_of(obj, struct bt_component, base); | |
46 | ||
47 | /** | |
48 | * User data is destroyed first, followed by the concrete component | |
49 | * instance. | |
50 | */ | |
51 | assert(!component->user_data || component->user_destroy); | |
52 | component->user_destroy(component->user_data); | |
53 | ||
54 | g_string_free(component->name, TRUE); | |
55 | ||
56 | assert(component->destroy); | |
57 | component_class = component->class; | |
58 | ||
59 | /* Frees the component, which becomes invalid */ | |
60 | component->destroy(component); | |
61 | component = NULL; | |
62 | ||
63 | bt_put(component_class); | |
64 | } | |
65 | ||
66 | BT_HIDDEN | |
67 | enum bt_component_status bt_component_init(struct bt_component *component, | |
68 | struct bt_component_class *class, const char *name, | |
69 | bt_component_destroy_cb destroy) | |
70 | { | |
71 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
72 | ||
73 | if (!component || !class || !name || name[0] == '\0' || !destroy) { | |
74 | ret = BT_COMPONENT_STATUS_INVAL; | |
75 | goto end; | |
76 | } | |
77 | ||
78 | bt_object_init(component, bt_component_destroy); | |
4647b93a | 79 | component->class = bt_get(class); |
b8a06801 JG |
80 | component->name = g_string_new(name); |
81 | if (!component->name) { | |
82 | ret = BT_COMPONENT_STATUS_NOMEM; | |
83 | goto end; | |
84 | } | |
85 | component->destroy = destroy; | |
86 | end: | |
87 | return ret; | |
88 | } | |
de713ce0 JG |
89 | |
90 | const char *bt_component_get_name(struct bt_component *component) | |
91 | { | |
92 | const char *ret = NULL; | |
93 | ||
94 | if (!component) { | |
95 | goto end; | |
96 | } | |
97 | ||
98 | ret = component->name->str; | |
99 | end: | |
100 | return ret; | |
101 | } | |
102 | ||
103 | enum bt_component_status bt_component_set_name(struct bt_component *component, | |
104 | const char *name) | |
105 | { | |
106 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
107 | ||
108 | if (!component || !name || name[0] == '\0') { | |
109 | ret = BT_COMPONENT_STATUS_INVAL; | |
110 | goto end; | |
111 | } | |
112 | ||
113 | g_string_assign(component->name, name); | |
114 | end: | |
115 | return ret; | |
116 | } | |
117 | ||
118 | enum bt_component_type bt_component_get_type(struct bt_component *component) | |
119 | { | |
120 | enum bt_component_type type = BT_COMPONENT_TYPE_UNKNOWN; | |
121 | ||
122 | if (!component) { | |
123 | goto end; | |
124 | } | |
125 | ||
fb2dcc52 | 126 | type = component->class->type; |
de713ce0 JG |
127 | end: |
128 | return type; | |
129 | } | |
130 | ||
131 | enum bt_component_status bt_component_set_error_stream( | |
132 | struct bt_component *component, FILE *stream) | |
133 | { | |
134 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
135 | ||
136 | if (!component) { | |
137 | ret = BT_COMPONENT_STATUS_INVAL; | |
138 | goto end; | |
139 | } | |
140 | ||
141 | component->error_stream = stream; | |
142 | end: | |
143 | return ret; | |
144 | } | |
145 | ||
de713ce0 JG |
146 | void *bt_component_get_private_data(struct bt_component *component) |
147 | { | |
0d884c50 | 148 | void *ret = NULL; |
de713ce0 JG |
149 | |
150 | if (!component) { | |
151 | goto end; | |
152 | } | |
153 | ||
154 | ret = component->user_data; | |
155 | end: | |
156 | return ret; | |
157 | } | |
158 | ||
159 | enum bt_component_status | |
160 | bt_component_set_private_data(struct bt_component *component, | |
161 | void *data) | |
162 | { | |
163 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
164 | ||
165 | if (!component) { | |
166 | ret = BT_COMPONENT_STATUS_INVAL; | |
167 | goto end; | |
168 | } | |
169 | ||
170 | component->user_data = data; | |
171 | end: | |
172 | return ret; | |
173 | } |