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