4 * Babeltrace Plugin Component
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/plugin/component.h>
30 #include <babeltrace/plugin/component-internal.h>
31 #include <babeltrace/plugin/source-internal.h>
32 #include <babeltrace/plugin/sink-internal.h>
33 #include <babeltrace/babeltrace-internal.h>
34 #include <babeltrace/compiler.h>
35 #include <babeltrace/ref.h>
38 void bt_component_destroy(struct bt_object
*obj
)
40 struct bt_component
*component
= NULL
;
41 struct bt_component_class
*component_class
= NULL
;
47 component
= container_of(obj
, struct bt_component
, base
);
50 * User data is destroyed first, followed by the concrete component
53 assert(!component
->user_data
|| component
->user_destroy
);
54 component
->user_destroy(component
->user_data
);
56 g_string_free(component
->name
, TRUE
);
58 assert(component
->destroy
);
59 component_class
= component
->class;
61 /* Frees the component, which becomes invalid */
62 component
->destroy(component
);
65 bt_put(component_class
);
69 enum 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
)
73 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
75 bt_object_init(component
, bt_component_destroy
);
76 if (!component
|| !class || !name
|| name
[0] == '\0' || !destroy
) {
77 ret
= BT_COMPONENT_STATUS_INVAL
;
81 component
->class = bt_get(class);
82 component
->name
= g_string_new(name
);
83 if (!component
->name
) {
84 ret
= BT_COMPONENT_STATUS_NOMEM
;
87 component
->destroy
= destroy
;
93 enum bt_component_type
bt_component_get_type(struct bt_component
*component
)
95 return component
? component
->class->type
: BT_COMPONENT_TYPE_UNKNOWN
;
98 struct bt_component
*bt_component_create(
99 struct bt_component_class
*component_class
, const char *name
)
101 struct bt_component
*component
= NULL
;
103 if (!component_class
) {
107 switch (bt_component_class_get_type(component_class
))
109 case BT_COMPONENT_TYPE_SOURCE
:
110 component
= bt_component_source_create(component_class
, name
);
112 case BT_COMPONENT_TYPE_SINK
:
113 component
= bt_component_sink_create(component_class
, name
);
122 const char *bt_component_get_name(struct bt_component
*component
)
124 const char *ret
= NULL
;
130 ret
= component
->name
->str
;
135 enum bt_component_status
bt_component_set_name(struct bt_component
*component
,
138 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
140 if (!component
|| !name
|| name
[0] == '\0') {
141 ret
= BT_COMPONENT_STATUS_INVAL
;
145 g_string_assign(component
->name
, name
);
150 struct bt_component_class
*bt_component_get_class(
151 struct bt_component
*component
)
153 return component
? bt_get(component
->class) : NULL
;
156 enum bt_component_status
bt_component_set_error_stream(
157 struct bt_component
*component
, FILE *stream
)
159 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
162 ret
= BT_COMPONENT_STATUS_INVAL
;
166 component
->error_stream
= stream
;
171 void *bt_component_get_private_data(struct bt_component
*component
)
173 return component
? component
->user_data
: NULL
;
176 enum bt_component_status
177 bt_component_set_private_data(struct bt_component
*component
,
180 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
183 ret
= BT_COMPONENT_STATUS_INVAL
;
187 component
->user_data
= data
;