2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
8 #define BT_LOG_TAG "LIB/ATTRS"
9 #include "lib/logging.h"
11 #include "common/macros.h"
12 #include <babeltrace2/value.h>
13 #include "lib/assert-cond.h"
14 #include "lib/object.h"
15 #include <babeltrace2/value.h>
16 #include "lib/value.h"
17 #include "attributes.h"
19 #include "compat/string.h"
20 #include "common/assert.h"
22 #define BT_ATTR_NAME_INDEX 0
23 #define BT_ATTR_VALUE_INDEX 1
26 struct bt_value
*bt_attributes_create(void)
28 struct bt_value
*attr_obj
;
31 * Attributes: array value object of array value objects, each one
32 * containing two entries: a string value object (attributes
33 * field name), and a value object (attributes field value).
35 * Example (JSON representation):
38 * ["hostname", "eeppdesk"],
39 * ["sysname", "Linux"],
40 * ["tracer_major", 2],
44 BT_LOGD_STR("Creating attributes object.");
45 attr_obj
= bt_value_array_create();
47 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
49 BT_LOGD("Created attributes object: addr=%p",
57 void bt_attributes_destroy(struct bt_value
*attr_obj
)
59 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
60 BT_OBJECT_PUT_REF_AND_RESET(attr_obj
);
64 uint64_t bt_attributes_get_count(const struct bt_value
*attr_obj
)
66 return bt_value_array_get_length(attr_obj
);
70 const char *bt_attributes_get_field_name(const struct bt_value
*attr_obj
,
73 const struct bt_value
*attr_field_obj
= NULL
;
74 const struct bt_value
*attr_field_name_obj
= NULL
;
76 BT_ASSERT_DBG(attr_obj
);
77 BT_ASSERT_DBG(index
< bt_value_array_get_length(attr_obj
));
78 attr_field_obj
= bt_value_array_borrow_element_by_index_const(
82 bt_value_array_borrow_element_by_index_const(attr_field_obj
,
85 return bt_value_string_get(attr_field_name_obj
);
89 struct bt_value
*bt_attributes_borrow_field_value(
90 struct bt_value
*attr_obj
, uint64_t index
)
92 struct bt_value
*attr_field_obj
= NULL
;
94 BT_ASSERT_DBG(attr_obj
);
95 BT_ASSERT_DBG(index
< bt_value_array_get_length(attr_obj
));
98 bt_value_array_borrow_element_by_index(attr_obj
, index
);
100 return bt_value_array_borrow_element_by_index( attr_field_obj
,
101 BT_ATTR_VALUE_INDEX
);
105 struct bt_value
*bt_attributes_borrow_field_by_name(
106 struct bt_value
*attr_obj
, const char *name
)
108 uint64_t i
, attr_size
;
109 struct bt_value
*value_obj
= NULL
;
110 struct bt_value
*attr_field_name_obj
= NULL
;
112 attr_size
= bt_value_array_get_length(attr_obj
);
113 for (i
= 0; i
< attr_size
; ++i
) {
114 const char *field_name
;
116 value_obj
= bt_value_array_borrow_element_by_index(
119 attr_field_name_obj
=
120 bt_value_array_borrow_element_by_index(
121 value_obj
, BT_ATTR_NAME_INDEX
);
123 field_name
= bt_value_string_get(attr_field_name_obj
);
125 if (strcmp(field_name
, name
) == 0) {
136 int bt_attributes_set_field_value(struct bt_value
*attr_obj
,
137 const char *name
, struct bt_value
*value_obj
)
140 struct bt_value
*attr_field_obj
= NULL
;
144 BT_ASSERT(value_obj
);
145 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
146 if (attr_field_obj
) {
147 ret
= bt_value_array_set_element_by_index(
148 attr_field_obj
, BT_ATTR_VALUE_INDEX
,
150 attr_field_obj
= NULL
;
154 attr_field_obj
= bt_value_array_create();
155 if (!attr_field_obj
) {
156 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
161 ret
= bt_value_array_append_string_element(attr_field_obj
,
163 ret
|= bt_value_array_append_element(attr_field_obj
,
166 BT_LIB_LOGE_APPEND_CAUSE(
167 "Cannot append elements to array value: %!+v",
172 ret
= bt_value_array_append_element(attr_obj
,
175 BT_LIB_LOGE_APPEND_CAUSE(
176 "Cannot append element to array value: "
177 "%![array-value-]+v, %![element-value-]+v",
178 attr_obj
, attr_field_obj
);
182 bt_object_put_ref(attr_field_obj
);
187 struct bt_value
*bt_attributes_borrow_field_value_by_name(
188 struct bt_value
*attr_obj
, const char *name
)
190 struct bt_value
*value_obj
= NULL
;
191 struct bt_value
*attr_field_obj
= NULL
;
193 BT_ASSERT_DBG(attr_obj
);
195 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
196 if (!attr_field_obj
) {
197 BT_LOGD("Cannot find attributes object's field by name: "
198 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
202 value_obj
= bt_value_array_borrow_element_by_index(
203 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
210 int bt_attributes_freeze(const struct bt_value
*attr_obj
)
216 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
218 count
= bt_value_array_get_length(attr_obj
);
221 * We do not freeze the array value object itself here, since
222 * internal stuff could need to modify/add attributes. Each
223 * attribute is frozen one by one.
225 for (i
= 0; i
< count
; ++i
) {
226 struct bt_value
*obj
= NULL
;
228 obj
= bt_attributes_borrow_field_value(
229 (void *) attr_obj
, i
);
231 BT_LIB_LOGE_APPEND_CAUSE(
232 "Cannot get attributes object's field value by index: "
233 "%![value-]+v, index=%" PRIu64
,
239 bt_value_freeze(obj
);
This page took 0.03354 seconds and 4 git commands to generate.