2 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
3 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "ATTRS"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/object.h>
28 #include <babeltrace/babeltrace-internal.h>
29 #include <babeltrace/values.h>
30 #include <babeltrace/private-values.h>
31 #include <babeltrace/values-internal.h>
33 #include <babeltrace/compat/string-internal.h>
34 #include <babeltrace/assert-internal.h>
36 #define BT_ATTR_NAME_INDEX 0
37 #define BT_ATTR_VALUE_INDEX 1
40 struct bt_private_value
*bt_attributes_create(void)
42 struct bt_private_value
*attr_obj
;
45 * Attributes: array value object of array value objects, each one
46 * containing two entries: a string value object (attributes
47 * field name), and a value object (attributes field value).
49 * Example (JSON representation):
52 * ["hostname", "eeppdesk"],
53 * ["sysname", "Linux"],
54 * ["tracer_major", 2],
58 BT_LOGD_STR("Creating attributes object.");
59 attr_obj
= bt_private_value_array_create();
61 BT_LOGE_STR("Failed to create array value.");
63 BT_LOGD("Created attributes object: addr=%p",
71 void bt_attributes_destroy(struct bt_private_value
*attr_obj
)
73 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
74 BT_OBJECT_PUT_REF_AND_RESET(attr_obj
);
78 int64_t bt_attributes_get_count(struct bt_private_value
*attr_obj
)
80 return bt_value_array_get_size(bt_private_value_as_value(attr_obj
));
84 const char *bt_attributes_get_field_name(struct bt_private_value
*attr_obj
,
87 const char *ret
= NULL
;
88 struct bt_private_value
*attr_field_obj
= NULL
;
89 struct bt_private_value
*attr_field_name_obj
= NULL
;
92 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
96 if (index
>= bt_value_array_get_size(
97 bt_private_value_as_value(attr_obj
))) {
98 BT_LOGW("Invalid parameter: index is out of bounds: "
99 "index=%" PRIu64
", count=%" PRId64
,
100 index
, bt_value_array_get_size(
101 bt_private_value_as_value(attr_obj
)));
105 attr_field_obj
= bt_private_value_array_borrow_element_by_index(
107 if (!attr_field_obj
) {
108 BT_LOGE("Cannot get attributes object's array value's element by index: "
109 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
113 attr_field_name_obj
=
114 bt_private_value_array_borrow_element_by_index(attr_field_obj
,
116 if (!attr_field_name_obj
) {
117 BT_LOGE("Cannot get attribute array value's element by index: "
118 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
119 (uint64_t) BT_ATTR_NAME_INDEX
);
123 ret
= bt_value_string_get(
124 bt_private_value_as_value(attr_field_name_obj
));
131 struct bt_private_value
*bt_attributes_borrow_field_value(
132 struct bt_private_value
*attr_obj
, uint64_t index
)
134 struct bt_private_value
*value_obj
= NULL
;
135 struct bt_private_value
*attr_field_obj
= NULL
;
138 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
142 if (index
>= bt_value_array_get_size(bt_private_value_as_value(attr_obj
))) {
143 BT_LOGW("Invalid parameter: index is out of bounds: "
144 "index=%" PRIu64
", count=%" PRId64
,
145 index
, bt_value_array_get_size(
146 bt_private_value_as_value(attr_obj
)));
151 bt_private_value_array_borrow_element_by_index(attr_obj
, index
);
152 if (!attr_field_obj
) {
153 BT_LOGE("Cannot get attributes object's array value's element by index: "
154 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
158 value_obj
= bt_private_value_array_borrow_element_by_index(
159 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
161 BT_LOGE("Cannot get attribute array value's element by index: "
162 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
163 (uint64_t) BT_ATTR_VALUE_INDEX
);
171 struct bt_private_value
*bt_attributes_borrow_field_by_name(
172 struct bt_private_value
*attr_obj
, const char *name
)
176 struct bt_private_value
*value_obj
= NULL
;
177 struct bt_private_value
*attr_field_name_obj
= NULL
;
179 attr_size
= bt_value_array_get_size(
180 bt_private_value_as_value(attr_obj
));
182 BT_LOGE("Cannot get array value's size: value-addr=%p",
187 for (i
= 0; i
< attr_size
; ++i
) {
188 const char *field_name
;
190 value_obj
= bt_private_value_array_borrow_element_by_index(
193 BT_LOGE("Cannot get attributes object's array value's element by index: "
194 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
198 attr_field_name_obj
=
199 bt_private_value_array_borrow_element_by_index(
200 value_obj
, BT_ATTR_NAME_INDEX
);
201 if (!attr_field_name_obj
) {
202 BT_LOGE("Cannot get attribute array value's element by index: "
203 "value-addr=%p, index=%" PRIu64
,
204 value_obj
, (int64_t) BT_ATTR_NAME_INDEX
);
208 field_name
= bt_value_string_get(
209 bt_private_value_as_value(attr_field_name_obj
));
211 if (!strcmp(field_name
, name
)) {
226 int bt_attributes_set_field_value(struct bt_private_value
*attr_obj
,
227 const char *name
, struct bt_private_value
*value_obj
)
230 struct bt_private_value
*attr_field_obj
= NULL
;
232 if (!attr_obj
|| !name
|| !value_obj
) {
233 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
234 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
235 attr_obj
, name
, value_obj
);
240 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
241 if (attr_field_obj
) {
242 ret
= bt_private_value_array_set_element_by_index(
243 attr_field_obj
, BT_ATTR_VALUE_INDEX
,
244 bt_private_value_as_value(value_obj
));
245 attr_field_obj
= NULL
;
249 attr_field_obj
= bt_private_value_array_create();
250 if (!attr_field_obj
) {
251 BT_LOGE_STR("Failed to create empty array value.");
256 ret
= bt_private_value_array_append_string_element(attr_field_obj
,
258 ret
|= bt_private_value_array_append_element(attr_field_obj
,
259 bt_private_value_as_value(value_obj
));
261 BT_LOGE("Cannot append elements to array value: addr=%p",
266 ret
= bt_private_value_array_append_element(attr_obj
,
267 bt_private_value_as_value(attr_field_obj
));
269 BT_LOGE("Cannot append element to array value: "
270 "array-value-addr=%p, element-value-addr=%p",
271 attr_obj
, attr_field_obj
);
275 bt_object_put_ref(attr_field_obj
);
280 struct bt_private_value
*bt_attributes_borrow_field_value_by_name(
281 struct bt_private_value
*attr_obj
, const char *name
)
283 struct bt_private_value
*value_obj
= NULL
;
284 struct bt_private_value
*attr_field_obj
= NULL
;
286 if (!attr_obj
|| !name
) {
287 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
288 "value-addr=%p, name-addr=%p", attr_obj
, name
);
292 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
293 if (!attr_field_obj
) {
294 BT_LOGD("Cannot find attributes object's field by name: "
295 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
299 value_obj
= bt_private_value_array_borrow_element_by_index(
300 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
302 BT_LOGE("Cannot get attribute array value's element by index: "
303 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
304 (uint64_t) BT_ATTR_VALUE_INDEX
);
312 int bt_attributes_freeze(struct bt_private_value
*attr_obj
)
319 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
324 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
325 count
= bt_value_array_get_size(bt_private_value_as_value(attr_obj
));
326 BT_ASSERT(count
>= 0);
329 * We do not freeze the array value object itself here, since
330 * internal stuff could need to modify/add attributes. Each
331 * attribute is frozen one by one.
333 for (i
= 0; i
< count
; ++i
) {
334 struct bt_private_value
*obj
= NULL
;
336 obj
= bt_attributes_borrow_field_value(attr_obj
, i
);
338 BT_LOGE("Cannot get attributes object's field value by index: "
339 "value-addr=%p, index=%" PRIu64
,
345 bt_value_freeze(bt_private_value_as_value(obj
));
This page took 0.037101 seconds and 4 git commands to generate.