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 <babeltrace2/value.h>
12 #include "lib/object.h"
13 #include <babeltrace2/value.h>
14 #include "lib/value.h"
15 #include "attributes.h"
17 #include "common/assert.h"
19 #define BT_ATTR_NAME_INDEX 0
20 #define BT_ATTR_VALUE_INDEX 1
22 struct bt_value
*bt_attributes_create(void)
24 struct bt_value
*attr_obj
;
27 * Attributes: array value object of array value objects, each one
28 * containing two entries: a string value object (attributes
29 * field name), and a value object (attributes field value).
31 * Example (JSON representation):
34 * ["hostname", "eeppdesk"],
35 * ["sysname", "Linux"],
36 * ["tracer_major", 2],
40 BT_LOGD_STR("Creating attributes object.");
41 attr_obj
= bt_value_array_create();
43 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
45 BT_LOGD("Created attributes object: addr=%p",
52 void bt_attributes_destroy(struct bt_value
*attr_obj
)
54 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
55 BT_OBJECT_PUT_REF_AND_RESET(attr_obj
);
58 uint64_t bt_attributes_get_count(const struct bt_value
*attr_obj
)
60 return bt_value_array_get_length(attr_obj
);
63 const char *bt_attributes_get_field_name(const struct bt_value
*attr_obj
,
66 const struct bt_value
*attr_field_obj
= NULL
;
67 const struct bt_value
*attr_field_name_obj
= NULL
;
69 BT_ASSERT_DBG(attr_obj
);
70 BT_ASSERT_DBG(index
< bt_value_array_get_length(attr_obj
));
71 attr_field_obj
= bt_value_array_borrow_element_by_index_const(
75 bt_value_array_borrow_element_by_index_const(attr_field_obj
,
78 return bt_value_string_get(attr_field_name_obj
);
81 struct bt_value
*bt_attributes_borrow_field_value(
82 struct bt_value
*attr_obj
, uint64_t index
)
84 struct bt_value
*attr_field_obj
= NULL
;
86 BT_ASSERT_DBG(attr_obj
);
87 BT_ASSERT_DBG(index
< bt_value_array_get_length(attr_obj
));
90 bt_value_array_borrow_element_by_index(attr_obj
, index
);
92 return bt_value_array_borrow_element_by_index( attr_field_obj
,
97 struct bt_value
*bt_attributes_borrow_field_by_name(
98 struct bt_value
*attr_obj
, const char *name
)
100 uint64_t i
, attr_size
;
101 struct bt_value
*value_obj
= NULL
;
102 struct bt_value
*attr_field_name_obj
= NULL
;
104 attr_size
= bt_value_array_get_length(attr_obj
);
105 for (i
= 0; i
< attr_size
; ++i
) {
106 const char *field_name
;
108 value_obj
= bt_value_array_borrow_element_by_index(
111 attr_field_name_obj
=
112 bt_value_array_borrow_element_by_index(
113 value_obj
, BT_ATTR_NAME_INDEX
);
115 field_name
= bt_value_string_get(attr_field_name_obj
);
117 if (strcmp(field_name
, name
) == 0) {
127 int bt_attributes_set_field_value(struct bt_value
*attr_obj
,
128 const char *name
, struct bt_value
*value_obj
)
131 struct bt_value
*attr_field_obj
= NULL
;
135 BT_ASSERT(value_obj
);
136 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
137 if (attr_field_obj
) {
138 ret
= bt_value_array_set_element_by_index(
139 attr_field_obj
, BT_ATTR_VALUE_INDEX
,
141 attr_field_obj
= NULL
;
145 attr_field_obj
= bt_value_array_create();
146 if (!attr_field_obj
) {
147 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
152 ret
= bt_value_array_append_string_element(attr_field_obj
,
154 ret
|= bt_value_array_append_element(attr_field_obj
,
157 BT_LIB_LOGE_APPEND_CAUSE(
158 "Cannot append elements to array value: %!+v",
163 ret
= bt_value_array_append_element(attr_obj
,
166 BT_LIB_LOGE_APPEND_CAUSE(
167 "Cannot append element to array value: "
168 "%![array-value-]+v, %![element-value-]+v",
169 attr_obj
, attr_field_obj
);
173 bt_object_put_ref(attr_field_obj
);
177 struct bt_value
*bt_attributes_borrow_field_value_by_name(
178 struct bt_value
*attr_obj
, const char *name
)
180 struct bt_value
*value_obj
= NULL
;
181 struct bt_value
*attr_field_obj
= NULL
;
183 BT_ASSERT_DBG(attr_obj
);
185 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
186 if (!attr_field_obj
) {
187 BT_LOGD("Cannot find attributes object's field by name: "
188 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
192 value_obj
= bt_value_array_borrow_element_by_index(
193 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
199 int bt_attributes_freeze(const struct bt_value
*attr_obj
)
205 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
207 count
= bt_value_array_get_length(attr_obj
);
210 * We do not freeze the array value object itself here, since
211 * internal stuff could need to modify/add attributes. Each
212 * attribute is frozen one by one.
214 for (i
= 0; i
< count
; ++i
) {
215 struct bt_value
*obj
= NULL
;
217 obj
= bt_attributes_borrow_field_value(
218 (void *) attr_obj
, i
);
220 BT_LIB_LOGE_APPEND_CAUSE(
221 "Cannot get attributes object's field value by index: "
222 "%![value-]+v, index=%" PRIu64
,
228 bt_value_freeze(obj
);
This page took 0.033246 seconds and 4 git commands to generate.