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
25 struct bt_value
*bt_attributes_create(void)
27 struct bt_value
*attr_obj
;
30 * Attributes: array value object of array value objects, each one
31 * containing two entries: a string value object (attributes
32 * field name), and a value object (attributes field value).
34 * Example (JSON representation):
37 * ["hostname", "eeppdesk"],
38 * ["sysname", "Linux"],
39 * ["tracer_major", 2],
43 BT_LOGD_STR("Creating attributes object.");
44 attr_obj
= bt_value_array_create();
46 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
48 BT_LOGD("Created attributes object: addr=%p",
55 void bt_attributes_destroy(struct bt_value
*attr_obj
)
57 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
58 BT_OBJECT_PUT_REF_AND_RESET(attr_obj
);
61 uint64_t bt_attributes_get_count(const struct bt_value
*attr_obj
)
63 return bt_value_array_get_length(attr_obj
);
66 const char *bt_attributes_get_field_name(const struct bt_value
*attr_obj
,
69 const struct bt_value
*attr_field_obj
= NULL
;
70 const struct bt_value
*attr_field_name_obj
= NULL
;
72 BT_ASSERT_DBG(attr_obj
);
73 BT_ASSERT_DBG(index
< bt_value_array_get_length(attr_obj
));
74 attr_field_obj
= bt_value_array_borrow_element_by_index_const(
78 bt_value_array_borrow_element_by_index_const(attr_field_obj
,
81 return bt_value_string_get(attr_field_name_obj
);
84 struct bt_value
*bt_attributes_borrow_field_value(
85 struct bt_value
*attr_obj
, uint64_t index
)
87 struct bt_value
*attr_field_obj
= NULL
;
89 BT_ASSERT_DBG(attr_obj
);
90 BT_ASSERT_DBG(index
< bt_value_array_get_length(attr_obj
));
93 bt_value_array_borrow_element_by_index(attr_obj
, index
);
95 return bt_value_array_borrow_element_by_index( attr_field_obj
,
100 struct bt_value
*bt_attributes_borrow_field_by_name(
101 struct bt_value
*attr_obj
, const char *name
)
103 uint64_t i
, attr_size
;
104 struct bt_value
*value_obj
= NULL
;
105 struct bt_value
*attr_field_name_obj
= NULL
;
107 attr_size
= bt_value_array_get_length(attr_obj
);
108 for (i
= 0; i
< attr_size
; ++i
) {
109 const char *field_name
;
111 value_obj
= bt_value_array_borrow_element_by_index(
114 attr_field_name_obj
=
115 bt_value_array_borrow_element_by_index(
116 value_obj
, BT_ATTR_NAME_INDEX
);
118 field_name
= bt_value_string_get(attr_field_name_obj
);
120 if (strcmp(field_name
, name
) == 0) {
130 int bt_attributes_set_field_value(struct bt_value
*attr_obj
,
131 const char *name
, struct bt_value
*value_obj
)
134 struct bt_value
*attr_field_obj
= NULL
;
138 BT_ASSERT(value_obj
);
139 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
140 if (attr_field_obj
) {
141 ret
= bt_value_array_set_element_by_index(
142 attr_field_obj
, BT_ATTR_VALUE_INDEX
,
144 attr_field_obj
= NULL
;
148 attr_field_obj
= bt_value_array_create();
149 if (!attr_field_obj
) {
150 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
155 ret
= bt_value_array_append_string_element(attr_field_obj
,
157 ret
|= bt_value_array_append_element(attr_field_obj
,
160 BT_LIB_LOGE_APPEND_CAUSE(
161 "Cannot append elements to array value: %!+v",
166 ret
= bt_value_array_append_element(attr_obj
,
169 BT_LIB_LOGE_APPEND_CAUSE(
170 "Cannot append element to array value: "
171 "%![array-value-]+v, %![element-value-]+v",
172 attr_obj
, attr_field_obj
);
176 bt_object_put_ref(attr_field_obj
);
180 struct bt_value
*bt_attributes_borrow_field_value_by_name(
181 struct bt_value
*attr_obj
, const char *name
)
183 struct bt_value
*value_obj
= NULL
;
184 struct bt_value
*attr_field_obj
= NULL
;
186 BT_ASSERT_DBG(attr_obj
);
188 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
189 if (!attr_field_obj
) {
190 BT_LOGD("Cannot find attributes object's field by name: "
191 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
195 value_obj
= bt_value_array_borrow_element_by_index(
196 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
202 int bt_attributes_freeze(const struct bt_value
*attr_obj
)
208 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
210 count
= bt_value_array_get_length(attr_obj
);
213 * We do not freeze the array value object itself here, since
214 * internal stuff could need to modify/add attributes. Each
215 * attribute is frozen one by one.
217 for (i
= 0; i
< count
; ++i
) {
218 struct bt_value
*obj
= NULL
;
220 obj
= bt_attributes_borrow_field_value(
221 (void *) attr_obj
, i
);
223 BT_LIB_LOGE_APPEND_CAUSE(
224 "Cannot get attributes object's field value by index: "
225 "%![value-]+v, index=%" PRIu64
,
231 bt_value_freeze(obj
);
This page took 0.036365 seconds and 5 git commands to generate.