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 "compat/string.h"
18 #include "common/assert.h"
20 #define BT_ATTR_NAME_INDEX 0
21 #define BT_ATTR_VALUE_INDEX 1
23 struct bt_value
*bt_attributes_create(void)
25 struct bt_value
*attr_obj
;
28 * Attributes: array value object of array value objects, each one
29 * containing two entries: a string value object (attributes
30 * field name), and a value object (attributes field value).
32 * Example (JSON representation):
35 * ["hostname", "eeppdesk"],
36 * ["sysname", "Linux"],
37 * ["tracer_major", 2],
41 BT_LOGD_STR("Creating attributes object.");
42 attr_obj
= bt_value_array_create();
44 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
46 BT_LOGD("Created attributes object: addr=%p",
53 void bt_attributes_destroy(struct bt_value
*attr_obj
)
55 BT_LOGD("Destroying attributes object: addr=%p", attr_obj
);
56 BT_OBJECT_PUT_REF_AND_RESET(attr_obj
);
59 uint64_t bt_attributes_get_count(const struct bt_value
*attr_obj
)
61 return bt_value_array_get_length(attr_obj
);
64 const char *bt_attributes_get_field_name(const struct bt_value
*attr_obj
,
67 const struct bt_value
*attr_field_obj
= NULL
;
68 const struct bt_value
*attr_field_name_obj
= NULL
;
70 BT_ASSERT_DBG(attr_obj
);
71 BT_ASSERT_DBG(index
< bt_value_array_get_length(attr_obj
));
72 attr_field_obj
= bt_value_array_borrow_element_by_index_const(
76 bt_value_array_borrow_element_by_index_const(attr_field_obj
,
79 return bt_value_string_get(attr_field_name_obj
);
82 struct bt_value
*bt_attributes_borrow_field_value(
83 struct bt_value
*attr_obj
, uint64_t index
)
85 struct bt_value
*attr_field_obj
= NULL
;
87 BT_ASSERT_DBG(attr_obj
);
88 BT_ASSERT_DBG(index
< bt_value_array_get_length(attr_obj
));
91 bt_value_array_borrow_element_by_index(attr_obj
, index
);
93 return bt_value_array_borrow_element_by_index( attr_field_obj
,
98 struct bt_value
*bt_attributes_borrow_field_by_name(
99 struct bt_value
*attr_obj
, const char *name
)
101 uint64_t i
, attr_size
;
102 struct bt_value
*value_obj
= NULL
;
103 struct bt_value
*attr_field_name_obj
= NULL
;
105 attr_size
= bt_value_array_get_length(attr_obj
);
106 for (i
= 0; i
< attr_size
; ++i
) {
107 const char *field_name
;
109 value_obj
= bt_value_array_borrow_element_by_index(
112 attr_field_name_obj
=
113 bt_value_array_borrow_element_by_index(
114 value_obj
, BT_ATTR_NAME_INDEX
);
116 field_name
= bt_value_string_get(attr_field_name_obj
);
118 if (strcmp(field_name
, name
) == 0) {
128 int bt_attributes_set_field_value(struct bt_value
*attr_obj
,
129 const char *name
, struct bt_value
*value_obj
)
132 struct bt_value
*attr_field_obj
= NULL
;
136 BT_ASSERT(value_obj
);
137 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
138 if (attr_field_obj
) {
139 ret
= bt_value_array_set_element_by_index(
140 attr_field_obj
, BT_ATTR_VALUE_INDEX
,
142 attr_field_obj
= NULL
;
146 attr_field_obj
= bt_value_array_create();
147 if (!attr_field_obj
) {
148 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
153 ret
= bt_value_array_append_string_element(attr_field_obj
,
155 ret
|= bt_value_array_append_element(attr_field_obj
,
158 BT_LIB_LOGE_APPEND_CAUSE(
159 "Cannot append elements to array value: %!+v",
164 ret
= bt_value_array_append_element(attr_obj
,
167 BT_LIB_LOGE_APPEND_CAUSE(
168 "Cannot append element to array value: "
169 "%![array-value-]+v, %![element-value-]+v",
170 attr_obj
, attr_field_obj
);
174 bt_object_put_ref(attr_field_obj
);
178 struct bt_value
*bt_attributes_borrow_field_value_by_name(
179 struct bt_value
*attr_obj
, const char *name
)
181 struct bt_value
*value_obj
= NULL
;
182 struct bt_value
*attr_field_obj
= NULL
;
184 BT_ASSERT_DBG(attr_obj
);
186 attr_field_obj
= bt_attributes_borrow_field_by_name(attr_obj
, name
);
187 if (!attr_field_obj
) {
188 BT_LOGD("Cannot find attributes object's field by name: "
189 "value-addr=%p, name=\"%s\"", attr_obj
, name
);
193 value_obj
= bt_value_array_borrow_element_by_index(
194 attr_field_obj
, BT_ATTR_VALUE_INDEX
);
200 int bt_attributes_freeze(const struct bt_value
*attr_obj
)
206 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj
);
208 count
= bt_value_array_get_length(attr_obj
);
211 * We do not freeze the array value object itself here, since
212 * internal stuff could need to modify/add attributes. Each
213 * attribute is frozen one by one.
215 for (i
= 0; i
< count
; ++i
) {
216 struct bt_value
*obj
= NULL
;
218 obj
= bt_attributes_borrow_field_value(
219 (void *) attr_obj
, i
);
221 BT_LIB_LOGE_APPEND_CAUSE(
222 "Cannot get attributes object's field value by index: "
223 "%![value-]+v, index=%" PRIu64
,
229 bt_value_freeze(obj
);
This page took 0.046048 seconds and 4 git commands to generate.