cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / lib / trace-ir / attributes.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/ATTRS"
9 #include "lib/logging.h"
10
11 #include <babeltrace2/value.h>
12 #include "lib/object.h"
13 #include <babeltrace2/value.h>
14 #include "lib/value.h"
15 #include "attributes.h"
16 #include <inttypes.h>
17 #include "common/assert.h"
18
19 #define BT_ATTR_NAME_INDEX 0
20 #define BT_ATTR_VALUE_INDEX 1
21
22 struct bt_value *bt_attributes_create(void)
23 {
24 struct bt_value *attr_obj;
25
26 /*
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).
30 *
31 * Example (JSON representation):
32 *
33 * [
34 * ["hostname", "eeppdesk"],
35 * ["sysname", "Linux"],
36 * ["tracer_major", 2],
37 * ["tracer_minor", 5]
38 * ]
39 */
40 BT_LOGD_STR("Creating attributes object.");
41 attr_obj = bt_value_array_create();
42 if (!attr_obj) {
43 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
44 } else {
45 BT_LOGD("Created attributes object: addr=%p",
46 attr_obj);
47 }
48
49 return attr_obj;
50 }
51
52 void bt_attributes_destroy(struct bt_value *attr_obj)
53 {
54 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
55 BT_OBJECT_PUT_REF_AND_RESET(attr_obj);
56 }
57
58 uint64_t bt_attributes_get_count(const struct bt_value *attr_obj)
59 {
60 return bt_value_array_get_length(attr_obj);
61 }
62
63 const char *bt_attributes_get_field_name(const struct bt_value *attr_obj,
64 uint64_t index)
65 {
66 const struct bt_value *attr_field_obj = NULL;
67 const struct bt_value *attr_field_name_obj = NULL;
68
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(
72 attr_obj, index);
73
74 attr_field_name_obj =
75 bt_value_array_borrow_element_by_index_const(attr_field_obj,
76 BT_ATTR_NAME_INDEX);
77
78 return bt_value_string_get(attr_field_name_obj);
79 }
80
81 struct bt_value *bt_attributes_borrow_field_value(
82 struct bt_value *attr_obj, uint64_t index)
83 {
84 struct bt_value *attr_field_obj = NULL;
85
86 BT_ASSERT_DBG(attr_obj);
87 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
88
89 attr_field_obj =
90 bt_value_array_borrow_element_by_index(attr_obj, index);
91
92 return bt_value_array_borrow_element_by_index( attr_field_obj,
93 BT_ATTR_VALUE_INDEX);
94 }
95
96 static
97 struct bt_value *bt_attributes_borrow_field_by_name(
98 struct bt_value *attr_obj, const char *name)
99 {
100 uint64_t i, attr_size;
101 struct bt_value *value_obj = NULL;
102 struct bt_value *attr_field_name_obj = NULL;
103
104 attr_size = bt_value_array_get_length(attr_obj);
105 for (i = 0; i < attr_size; ++i) {
106 const char *field_name;
107
108 value_obj = bt_value_array_borrow_element_by_index(
109 attr_obj, i);
110
111 attr_field_name_obj =
112 bt_value_array_borrow_element_by_index(
113 value_obj, BT_ATTR_NAME_INDEX);
114
115 field_name = bt_value_string_get(attr_field_name_obj);
116
117 if (strcmp(field_name, name) == 0) {
118 break;
119 }
120
121 value_obj = NULL;
122 }
123
124 return value_obj;
125 }
126
127 int bt_attributes_set_field_value(struct bt_value *attr_obj,
128 const char *name, struct bt_value *value_obj)
129 {
130 int ret = 0;
131 struct bt_value *attr_field_obj = NULL;
132
133 BT_ASSERT(attr_obj);
134 BT_ASSERT(name);
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,
140 value_obj);
141 attr_field_obj = NULL;
142 goto end;
143 }
144
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.");
148 ret = -1;
149 goto end;
150 }
151
152 ret = bt_value_array_append_string_element(attr_field_obj,
153 name);
154 ret |= bt_value_array_append_element(attr_field_obj,
155 value_obj);
156 if (ret) {
157 BT_LIB_LOGE_APPEND_CAUSE(
158 "Cannot append elements to array value: %!+v",
159 attr_field_obj);
160 goto end;
161 }
162
163 ret = bt_value_array_append_element(attr_obj,
164 attr_field_obj);
165 if (ret) {
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);
170 }
171
172 end:
173 bt_object_put_ref(attr_field_obj);
174 return ret;
175 }
176
177 struct bt_value *bt_attributes_borrow_field_value_by_name(
178 struct bt_value *attr_obj, const char *name)
179 {
180 struct bt_value *value_obj = NULL;
181 struct bt_value *attr_field_obj = NULL;
182
183 BT_ASSERT_DBG(attr_obj);
184 BT_ASSERT_DBG(name);
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);
189 goto end;
190 }
191
192 value_obj = bt_value_array_borrow_element_by_index(
193 attr_field_obj, BT_ATTR_VALUE_INDEX);
194
195 end:
196 return value_obj;
197 }
198
199 int bt_attributes_freeze(const struct bt_value *attr_obj)
200 {
201 uint64_t i, count;
202 int ret = 0;
203
204 BT_ASSERT(attr_obj);
205 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
206
207 count = bt_value_array_get_length(attr_obj);
208
209 /*
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.
213 */
214 for (i = 0; i < count; ++i) {
215 struct bt_value *obj = NULL;
216
217 obj = bt_attributes_borrow_field_value(
218 (void *) attr_obj, i);
219 if (!obj) {
220 BT_LIB_LOGE_APPEND_CAUSE(
221 "Cannot get attributes object's field value by index: "
222 "%![value-]+v, index=%" PRIu64,
223 attr_obj, i);
224 ret = -1;
225 goto end;
226 }
227
228 bt_value_freeze(obj);
229 }
230
231 end:
232 return ret;
233 }
This page took 0.034105 seconds and 4 git commands to generate.