Fix header guard
[babeltrace.git] / plugins / component.c
CommitLineData
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
b8a06801
JG
37static
38void bt_component_destroy(struct bt_object *obj)
39{
40 struct bt_component *component = NULL;
41 struct bt_component_class *component_class = NULL;
42
43 if (!obj) {
44 return;
45 }
46
47 component = container_of(obj, struct bt_component, base);
48
49 /**
50 * User data is destroyed first, followed by the concrete component
51 * instance.
52 */
53 assert(!component->user_data || component->user_destroy);
54 component->user_destroy(component->user_data);
55
56 g_string_free(component->name, TRUE);
57
58 assert(component->destroy);
59 component_class = component->class;
60
61 /* Frees the component, which becomes invalid */
62 component->destroy(component);
63 component = NULL;
64
65 bt_put(component_class);
66}
67
68BT_HIDDEN
69enum bt_component_status bt_component_init(struct bt_component *component,
70 struct bt_component_class *class, const char *name,
71 bt_component_destroy_cb destroy)
72{
73 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
74
38b48196 75 bt_object_init(component, bt_component_destroy);
b8a06801
JG
76 if (!component || !class || !name || name[0] == '\0' || !destroy) {
77 ret = BT_COMPONENT_STATUS_INVAL;
78 goto end;
79 }
80
4647b93a 81 component->class = bt_get(class);
b8a06801
JG
82 component->name = g_string_new(name);
83 if (!component->name) {
84 ret = BT_COMPONENT_STATUS_NOMEM;
85 goto end;
86 }
87 component->destroy = destroy;
88end:
89 return ret;
90}
de713ce0 91
38b48196
JG
92BT_HIDDEN
93enum bt_component_type bt_component_get_type(struct bt_component *component)
94{
95 return component ? component->class->type : BT_COMPONENT_TYPE_UNKNOWN;
96}
97
98struct bt_component *bt_component_create(
99 struct bt_component_class *component_class, const char *name)
100{
101 struct bt_component *component = NULL;
102
103 if (!component_class) {
104 goto end;
105 }
106
107 switch (bt_component_class_get_type(component_class))
108 {
109 case BT_COMPONENT_TYPE_SOURCE:
110 component = bt_component_source_create(component_class, name);
111 break;
112 case BT_COMPONENT_TYPE_SINK:
113 component = bt_component_sink_create(component_class, name);
114 break;
115 default:
116 goto end;
117 }
118end:
119 return component;
120}
121
de713ce0
JG
122const char *bt_component_get_name(struct bt_component *component)
123{
124 const char *ret = NULL;
125
126 if (!component) {
127 goto end;
128 }
129
130 ret = component->name->str;
131end:
132 return ret;
133}
134
135enum bt_component_status bt_component_set_name(struct bt_component *component,
136 const char *name)
137{
138 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
139
140 if (!component || !name || name[0] == '\0') {
141 ret = BT_COMPONENT_STATUS_INVAL;
142 goto end;
143 }
144
145 g_string_assign(component->name, name);
146end:
147 return ret;
148}
149
38b48196
JG
150struct bt_component_class *bt_component_get_class(
151 struct bt_component *component)
de713ce0 152{
38b48196 153 return component ? bt_get(component->class) : NULL;
de713ce0
JG
154}
155
156enum bt_component_status bt_component_set_error_stream(
157 struct bt_component *component, FILE *stream)
158{
159 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
160
161 if (!component) {
162 ret = BT_COMPONENT_STATUS_INVAL;
163 goto end;
164 }
165
166 component->error_stream = stream;
167end:
168 return ret;
169}
170
de713ce0
JG
171void *bt_component_get_private_data(struct bt_component *component)
172{
38b48196 173 return component ? component->user_data : NULL;
de713ce0
JG
174}
175
176enum bt_component_status
177bt_component_set_private_data(struct bt_component *component,
178 void *data)
179{
180 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
181
182 if (!component) {
183 ret = BT_COMPONENT_STATUS_INVAL;
184 goto end;
185 }
186
187 component->user_data = data;
188end:
189 return ret;
190}
This page took 0.030903 seconds and 4 git commands to generate.