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/babeltrace-internal.h>
32 #include <babeltrace/compiler.h>
34 static void bt_component_destroy(struct bt_ref
*ref
);
36 const char *bt_component_get_name(struct bt_component
*component
)
38 const char *ret
= NULL
;
44 ret
= component
->name
->str
;
49 enum bt_component_status
bt_component_set_name(struct bt_component
*component
,
52 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
54 if (!component
|| !name
|| name
[0] == '\0') {
55 ret
= BT_COMPONENT_STATUS_INVAL
;
59 g_string_assign(component
->name
, name
);
64 enum bt_component_type
bt_component_get_type(struct bt_component
*component
)
66 enum bt_component_type type
= BT_COMPONENT_TYPE_UNKNOWN
;
72 type
= component
->class->type
;
77 enum bt_component_status
bt_component_set_error_stream(
78 struct bt_component
*component
, FILE *stream
)
80 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
83 ret
= BT_COMPONENT_STATUS_INVAL
;
87 component
->error_stream
= stream
;
92 void bt_component_get(struct bt_component
*component
)
98 bt_ref_get(&component
->ref
);
101 void bt_component_put(struct bt_component
*component
)
107 bt_ref_put(&component
->ref
);
111 enum bt_component_status
bt_component_init(struct bt_component
*component
,
112 struct bt_component_class
*class, const char *name
,
113 bt_component_destroy_cb destroy
)
115 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
117 if (!component
|| !class || !name
|| name
[0] == '\0' || destroy
) {
118 ret
= BT_COMPONENT_STATUS_INVAL
;
122 bt_ref_init(&component
->ref
, bt_component_destroy
);
123 bt_component_class_get(class);
124 component
->class = class;
125 component
->name
= g_string_new(name
);
126 if (!component
->name
) {
127 ret
= BT_COMPONENT_STATUS_NOMEM
;
130 component
->destroy
= destroy
;
135 void *bt_component_get_private_data(struct bt_component
*component
)
143 ret
= component
->user_data
;
148 enum bt_component_status
149 bt_component_set_private_data(struct bt_component
*component
,
152 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
155 ret
= BT_COMPONENT_STATUS_INVAL
;
159 component
->user_data
= data
;
165 void bt_component_destroy(struct bt_ref
*ref
)
167 struct bt_component
*component
= NULL
;
173 component
= container_of(ref
, struct bt_component
, ref
);
176 * User data is destroyed first, followed by the concrete component
179 assert(!component
->user_data
|| component
->user_destroy
);
180 component
->user_destroy(component
->user_data
);
182 g_string_free(component
->name
, TRUE
);
184 assert(component
->destroy
);
185 component
->destroy(component
);
186 bt_component_class_put(component
->class);