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 "lib/lib-logging.h"
27 #include "common/macros.h"
28 #include <babeltrace2/value.h>
29 #include "lib/assert-pre.h"
30 #include "lib/object.h"
31 #include <babeltrace2/value-const.h>
32 #include "lib/value.h"
33 #include "attributes.h"
35 #include "compat/string.h"
36 #include "common/assert.h"
38 #define BT_ATTR_NAME_INDEX 0
39 #define BT_ATTR_VALUE_INDEX 1
42 struct bt_value
*bt_attributes_create(void)
44 struct bt_value
*attr_obj
;
47 * Attributes: array value object of array value objects, each one
48 * containing two entries: a string value object (attributes
49 * field name), and a value object (attributes field value).
51 * Example (JSON representation):
54 * ["hostname", "eeppdesk"],
55 * ["sysname", "Linux"],
56 * ["tracer_major", 2],
60 BT_LOGD_STR("Creating attributes object.");
61 attr_obj
= bt_value_array_create();
63 BT_LOGE_STR("Failed to create array value.");
65 BT_LOGD("Created attributes object: addr=%p",
73 void bt_attributes_destroy(struct bt_value
*attr_obj
)
75 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
76 BT_OBJECT_PUT_REF_AND_RESET(attr_obj
);
80 int64_t bt_attributes_get_count(const struct bt_value
*attr_obj
)
82 return bt_value_array_get_size(attr_obj
);
86 const char *bt_attributes_get_field_name(const struct bt_value
*attr_obj
,
89 const char *ret
= NULL
;
90 const struct bt_value
*attr_field_obj
= NULL
;
91 const struct bt_value
*attr_field_name_obj
= NULL
;
94 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
98 if (index
>= bt_value_array_get_size(attr_obj
)) {
99 BT_LOGW("Invalid parameter: index is out of bounds: "
100 "index=%" PRIu64
", count=%" PRId64
,
101 index
, bt_value_array_get_size(attr_obj
));
105 attr_field_obj
= bt_value_array_borrow_element_by_index_const(
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_value_array_borrow_element_by_index_const(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(attr_field_name_obj
);
130 struct bt_value
*bt_attributes_borrow_field_value(
131 struct bt_value
*attr_obj
, uint64_t index
)
133 struct bt_value
*value_obj
= NULL
;
134 struct bt_value
*attr_field_obj
= NULL
;
137 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
141 if (index
>= bt_value_array_get_size(attr_obj
)) {
142 BT_LOGW("Invalid parameter: index is out of bounds: "
143 "index=%" PRIu64
", count=%" PRId64
,
144 index
, bt_value_array_get_size(attr_obj
));
149 bt_value_array_borrow_element_by_index(attr_obj
, index
);
150 if (!attr_field_obj
) {
151 BT_LOGE("Cannot get attributes object's array value's element by index: "
152 "value-addr=%p, index=%" PRIu64
, attr_obj
, index
);
156 value_obj
= bt_value_array_borrow_element_by_index(
157 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
159 BT_LOGE("Cannot get attribute array value's element by index: "
160 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
161 (uint64_t) BT_ATTR_VALUE_INDEX
);
169 struct bt_value
*bt_attributes_borrow_field_by_name(
170 struct bt_value
*attr_obj
, const char *name
)
174 struct bt_value
*value_obj
= NULL
;
175 struct bt_value
*attr_field_name_obj
= NULL
;
177 attr_size
= bt_value_array_get_size(attr_obj
);
179 BT_LOGE("Cannot get array value's size: value-addr=%p",
184 for (i
= 0; i
< attr_size
; ++i
) {
185 const char *field_name
;
187 value_obj
= bt_value_array_borrow_element_by_index(
190 BT_LOGE("Cannot get attributes object's array value's element by index: "
191 "value-addr=%p, index=%" PRIu64
, attr_obj
, i
);
195 attr_field_name_obj
=
196 bt_value_array_borrow_element_by_index(
197 value_obj
, BT_ATTR_NAME_INDEX
);
198 if (!attr_field_name_obj
) {
199 BT_LOGE("Cannot get attribute array value's element by index: "
200 "value-addr=%p, index=%" PRIu64
,
201 value_obj
, (int64_t) BT_ATTR_NAME_INDEX
);
205 field_name
= bt_value_string_get(attr_field_name_obj
);
207 if (!strcmp(field_name
, name
)) {
222 int bt_attributes_set_field_value(struct bt_value
*attr_obj
,
223 const char *name
, struct bt_value
*value_obj
)
226 struct bt_value
*attr_field_obj
= NULL
;
228 if (!attr_obj
|| !name
|| !value_obj
) {
229 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
230 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
231 attr_obj
, name
, value_obj
);
236 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
237 if (attr_field_obj
) {
238 ret
= bt_value_array_set_element_by_index(
239 attr_field_obj
, BT_ATTR_VALUE_INDEX
,
241 attr_field_obj
= NULL
;
245 attr_field_obj
= bt_value_array_create();
246 if (!attr_field_obj
) {
247 BT_LOGE_STR("Failed to create empty array value.");
252 ret
= bt_value_array_append_string_element(attr_field_obj
,
254 ret
|= bt_value_array_append_element(attr_field_obj
,
257 BT_LOGE("Cannot append elements to array value: addr=%p",
262 ret
= bt_value_array_append_element(attr_obj
,
265 BT_LOGE("Cannot append element to array value: "
266 "array-value-addr=%p, element-value-addr=%p",
267 attr_obj
, attr_field_obj
);
271 bt_object_put_ref(attr_field_obj
);
276 struct bt_value
*bt_attributes_borrow_field_value_by_name(
277 struct bt_value
*attr_obj
, const char *name
)
279 struct bt_value
*value_obj
= NULL
;
280 struct bt_value
*attr_field_obj
= NULL
;
282 if (!attr_obj
|| !name
) {
283 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
284 "value-addr=%p, name-addr=%p", attr_obj
, name
);
288 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
289 if (!attr_field_obj
) {
290 BT_LOGD("Cannot find attributes object's field by name: "
291 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
295 value_obj
= bt_value_array_borrow_element_by_index(
296 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
298 BT_LOGE("Cannot get attribute array value's element by index: "
299 "value-addr=%p, index=%" PRIu64
, attr_field_obj
,
300 (uint64_t) BT_ATTR_VALUE_INDEX
);
308 int bt_attributes_freeze(const struct bt_value
*attr_obj
)
315 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
320 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
321 count
= bt_value_array_get_size(attr_obj
);
322 BT_ASSERT(count
>= 0);
325 * We do not freeze the array value object itself here, since
326 * internal stuff could need to modify/add attributes. Each
327 * attribute is frozen one by one.
329 for (i
= 0; i
< count
; ++i
) {
330 struct bt_value
*obj
= NULL
;
332 obj
= bt_attributes_borrow_field_value(
333 (void *) attr_obj
, i
);
335 BT_LOGE("Cannot get attributes object's field value by index: "
336 "value-addr=%p, index=%" PRIu64
,
342 bt_value_freeze(obj
);
This page took 0.037021 seconds and 4 git commands to generate.