Document libbabeltrace2's C API
[babeltrace.git] / src / lib / trace-ir / attributes.c
CommitLineData
44e0a4f5 1/*
44e0a4f5
JG
2 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
3 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
350ad6c1 24#define BT_LOG_TAG "LIB/ATTRS"
c2d9d9cf 25#include "lib/logging.h"
0f5e83e5 26
91d81473 27#include "common/macros.h"
3fadfbc0 28#include <babeltrace2/value.h>
578e048b
MJ
29#include "lib/assert-pre.h"
30#include "lib/object.h"
43c59509 31#include <babeltrace2/value.h>
578e048b
MJ
32#include "lib/value.h"
33#include "attributes.h"
561ad8c1 34#include <inttypes.h>
578e048b
MJ
35#include "compat/string.h"
36#include "common/assert.h"
44e0a4f5 37
50842bdc
PP
38#define BT_ATTR_NAME_INDEX 0
39#define BT_ATTR_VALUE_INDEX 1
44e0a4f5
JG
40
41BT_HIDDEN
05e21286 42struct bt_value *bt_attributes_create(void)
44e0a4f5 43{
05e21286 44 struct bt_value *attr_obj;
561ad8c1 45
44e0a4f5 46 /*
dac5c838
PP
47 * Attributes: array value object of array value objects, each one
48 * containing two entries: a string value object (attributes
49 * field name), and a value object (attributes field value).
44e0a4f5
JG
50 *
51 * Example (JSON representation):
52 *
53 * [
54 * ["hostname", "eeppdesk"],
55 * ["sysname", "Linux"],
56 * ["tracer_major", 2],
57 * ["tracer_minor", 5]
58 * ]
59 */
561ad8c1 60 BT_LOGD_STR("Creating attributes object.");
05e21286 61 attr_obj = bt_value_array_create();
561ad8c1 62 if (!attr_obj) {
870631a2 63 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
561ad8c1
PP
64 } else {
65 BT_LOGD("Created attributes object: addr=%p",
66 attr_obj);
67 }
68
69 return attr_obj;
44e0a4f5
JG
70}
71
72BT_HIDDEN
05e21286 73void bt_attributes_destroy(struct bt_value *attr_obj)
44e0a4f5 74{
561ad8c1 75 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
238b7404 76 BT_OBJECT_PUT_REF_AND_RESET(attr_obj);
44e0a4f5
JG
77}
78
79BT_HIDDEN
99b4b64b 80uint64_t bt_attributes_get_count(const struct bt_value *attr_obj)
44e0a4f5 81{
393729a6 82 return bt_value_array_get_length(attr_obj);
44e0a4f5
JG
83}
84
85BT_HIDDEN
05e21286 86const char *bt_attributes_get_field_name(const struct bt_value *attr_obj,
dcf0cc71 87 uint64_t index)
44e0a4f5 88{
05e21286
PP
89 const struct bt_value *attr_field_obj = NULL;
90 const struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 91
98b15851
PP
92 BT_ASSERT_DBG(attr_obj);
93 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
05e21286 94 attr_field_obj = bt_value_array_borrow_element_by_index_const(
da91b29a 95 attr_obj, index);
44e0a4f5 96
da91b29a 97 attr_field_name_obj =
05e21286 98 bt_value_array_borrow_element_by_index_const(attr_field_obj,
da91b29a 99 BT_ATTR_NAME_INDEX);
44e0a4f5 100
33f4e1fd 101 return bt_value_string_get(attr_field_name_obj);
44e0a4f5
JG
102}
103
104BT_HIDDEN
05e21286
PP
105struct bt_value *bt_attributes_borrow_field_value(
106 struct bt_value *attr_obj, uint64_t index)
44e0a4f5 107{
05e21286 108 struct bt_value *attr_field_obj = NULL;
44e0a4f5 109
98b15851
PP
110 BT_ASSERT_DBG(attr_obj);
111 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
33f4e1fd 112
da91b29a 113 attr_field_obj =
05e21286 114 bt_value_array_borrow_element_by_index(attr_obj, index);
44e0a4f5 115
33f4e1fd
FD
116 return bt_value_array_borrow_element_by_index( attr_field_obj,
117 BT_ATTR_VALUE_INDEX);
44e0a4f5
JG
118}
119
120static
05e21286
PP
121struct bt_value *bt_attributes_borrow_field_by_name(
122 struct bt_value *attr_obj, const char *name)
44e0a4f5 123{
f80e9ec1 124 uint64_t i, attr_size;
05e21286
PP
125 struct bt_value *value_obj = NULL;
126 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 127
393729a6 128 attr_size = bt_value_array_get_length(attr_obj);
44e0a4f5 129 for (i = 0; i < attr_size; ++i) {
44e0a4f5
JG
130 const char *field_name;
131
05e21286 132 value_obj = bt_value_array_borrow_element_by_index(
da91b29a 133 attr_obj, i);
44e0a4f5 134
da91b29a 135 attr_field_name_obj =
05e21286 136 bt_value_array_borrow_element_by_index(
da91b29a 137 value_obj, BT_ATTR_NAME_INDEX);
44e0a4f5 138
05e21286 139 field_name = bt_value_string_get(attr_field_name_obj);
44e0a4f5 140
2242b43d 141 if (strcmp(field_name, name) == 0) {
44e0a4f5
JG
142 break;
143 }
144
094ff7c0 145 value_obj = NULL;
44e0a4f5
JG
146 }
147
148 return value_obj;
44e0a4f5
JG
149}
150
151BT_HIDDEN
05e21286
PP
152int bt_attributes_set_field_value(struct bt_value *attr_obj,
153 const char *name, struct bt_value *value_obj)
44e0a4f5
JG
154{
155 int ret = 0;
05e21286 156 struct bt_value *attr_field_obj = NULL;
44e0a4f5 157
870631a2
PP
158 BT_ASSERT(attr_obj);
159 BT_ASSERT(name);
160 BT_ASSERT(value_obj);
094ff7c0 161 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 162 if (attr_field_obj) {
05e21286 163 ret = bt_value_array_set_element_by_index(
da91b29a 164 attr_field_obj, BT_ATTR_VALUE_INDEX,
05e21286 165 value_obj);
094ff7c0 166 attr_field_obj = NULL;
44e0a4f5
JG
167 goto end;
168 }
169
05e21286 170 attr_field_obj = bt_value_array_create();
44e0a4f5 171 if (!attr_field_obj) {
870631a2 172 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
44e0a4f5
JG
173 ret = -1;
174 goto end;
175 }
176
05e21286 177 ret = bt_value_array_append_string_element(attr_field_obj,
da91b29a 178 name);
05e21286
PP
179 ret |= bt_value_array_append_element(attr_field_obj,
180 value_obj);
44e0a4f5 181 if (ret) {
870631a2
PP
182 BT_LIB_LOGE_APPEND_CAUSE(
183 "Cannot append elements to array value: %!+v",
561ad8c1 184 attr_field_obj);
44e0a4f5
JG
185 goto end;
186 }
187
05e21286
PP
188 ret = bt_value_array_append_element(attr_obj,
189 attr_field_obj);
561ad8c1 190 if (ret) {
870631a2
PP
191 BT_LIB_LOGE_APPEND_CAUSE(
192 "Cannot append element to array value: "
193 "%![array-value-]+v, %![element-value-]+v",
561ad8c1
PP
194 attr_obj, attr_field_obj);
195 }
44e0a4f5
JG
196
197end:
65300d60 198 bt_object_put_ref(attr_field_obj);
44e0a4f5
JG
199 return ret;
200}
201
202BT_HIDDEN
05e21286
PP
203struct bt_value *bt_attributes_borrow_field_value_by_name(
204 struct bt_value *attr_obj, const char *name)
44e0a4f5 205{
05e21286
PP
206 struct bt_value *value_obj = NULL;
207 struct bt_value *attr_field_obj = NULL;
44e0a4f5 208
98b15851
PP
209 BT_ASSERT_DBG(attr_obj);
210 BT_ASSERT_DBG(name);
094ff7c0 211 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 212 if (!attr_field_obj) {
561ad8c1
PP
213 BT_LOGD("Cannot find attributes object's field by name: "
214 "value-addr=%p, name=\"%s\"", attr_obj, name);
44e0a4f5
JG
215 goto end;
216 }
217
05e21286 218 value_obj = bt_value_array_borrow_element_by_index(
da91b29a 219 attr_field_obj, BT_ATTR_VALUE_INDEX);
44e0a4f5
JG
220
221end:
44e0a4f5
JG
222 return value_obj;
223}
224
225BT_HIDDEN
05e21286 226int bt_attributes_freeze(const struct bt_value *attr_obj)
44e0a4f5 227{
f80e9ec1 228 uint64_t i, count;
44e0a4f5
JG
229 int ret = 0;
230
870631a2 231 BT_ASSERT(attr_obj);
561ad8c1 232 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
f80e9ec1 233
393729a6 234 count = bt_value_array_get_length(attr_obj);
44e0a4f5
JG
235
236 /*
dac5c838
PP
237 * We do not freeze the array value object itself here, since
238 * internal stuff could need to modify/add attributes. Each
239 * attribute is frozen one by one.
44e0a4f5
JG
240 */
241 for (i = 0; i < count; ++i) {
05e21286 242 struct bt_value *obj = NULL;
44e0a4f5 243
05e21286
PP
244 obj = bt_attributes_borrow_field_value(
245 (void *) attr_obj, i);
44e0a4f5 246 if (!obj) {
870631a2
PP
247 BT_LIB_LOGE_APPEND_CAUSE(
248 "Cannot get attributes object's field value by index: "
249 "%![value-]+v, index=%" PRIu64,
561ad8c1 250 attr_obj, i);
44e0a4f5
JG
251 ret = -1;
252 goto end;
253 }
254
05e21286 255 bt_value_freeze(obj);
44e0a4f5
JG
256 }
257
258end:
44e0a4f5
JG
259 return ret;
260}
This page took 0.089618 seconds and 4 git commands to generate.