Add Python bindings for CTF-IR event-types getters
[babeltrace.git] / bindings / python / python-complements.c
CommitLineData
24a3136a
DS
1/*
2 * python-complements.c
3 *
4 * Babeltrace Python module complements, required for Python bindings
5 *
6 * Copyright 2012 EfficiOS Inc.
7 *
8 * Author: Danny Serres <danny.serres@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21#include "python-complements.h"
adc315b8
JG
22#include <babeltrace/ctf-ir/event-types-internal.h>
23#include <babeltrace/ctf-ir/event-fields-internal.h>
b4b5e4b5 24#include <babeltrace/ctf-ir/event-types.h>
24a3136a
DS
25
26/* List-related functions
27 ----------------------------------------------------
28*/
29
30/* ctf-field-list */
2c0df204 31struct bt_definition **_bt_python_field_listcaller(
24a3136a 32 const struct bt_ctf_event *ctf_event,
cebae8c3
JG
33 const struct bt_definition *scope,
34 unsigned int *len)
24a3136a 35{
2c0df204 36 struct bt_definition **list;
24a3136a
DS
37 int ret;
38
39 ret = bt_ctf_get_field_list(ctf_event, scope,
cebae8c3 40 (const struct bt_definition * const **)&list, len);
24a3136a
DS
41
42 if (ret < 0) /* For python to know an error occured */
43 list = NULL;
24a3136a
DS
44
45 return list;
46}
47
2c0df204
XH
48struct bt_definition *_bt_python_field_one_from_list(
49 struct bt_definition **list, int index)
24a3136a
DS
50{
51 return list[index];
52}
53
54/* event_decl_list */
55struct bt_ctf_event_decl **_bt_python_event_decl_listcaller(
cebae8c3
JG
56 int handle_id,
57 struct bt_context *ctx,
58 unsigned int *len)
24a3136a
DS
59{
60 struct bt_ctf_event_decl **list;
24a3136a
DS
61 int ret;
62
63 ret = bt_ctf_get_event_decl_list(handle_id, ctx,
cebae8c3 64 (struct bt_ctf_event_decl * const **)&list, len);
24a3136a
DS
65
66 if (ret < 0) /* For python to know an error occured */
67 list = NULL;
24a3136a
DS
68
69 return list;
70}
71
72struct bt_ctf_event_decl *_bt_python_decl_one_from_list(
73 struct bt_ctf_event_decl **list, int index)
74{
75 return list[index];
76}
77
78/* decl_fields */
79struct bt_ctf_field_decl **_by_python_field_decl_listcaller(
80 struct bt_ctf_event_decl *event_decl,
cebae8c3
JG
81 enum bt_ctf_scope scope,
82 unsigned int *len)
24a3136a
DS
83{
84 struct bt_ctf_field_decl **list;
24a3136a
DS
85 int ret;
86
87 ret = bt_ctf_get_decl_fields(event_decl, scope,
cebae8c3 88 (const struct bt_ctf_field_decl * const **)&list, len);
24a3136a
DS
89
90 if (ret < 0) /* For python to know an error occured */
91 list = NULL;
24a3136a
DS
92
93 return list;
94}
95
96struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list(
97 struct bt_ctf_field_decl **list, int index)
98{
99 return list[index];
100}
3c2ce778
XH
101
102struct definition_array *_bt_python_get_array_from_def(
103 struct bt_definition *field)
104{
105 const struct bt_declaration *array_decl;
106 struct definition_array *array = NULL;
107
108 if (!field) {
109 goto end;
110 }
111
112 array_decl = bt_ctf_get_decl_from_def(field);
113 if (bt_ctf_field_type(array_decl) == CTF_TYPE_ARRAY) {
114 array = container_of(field, struct definition_array, p);
115 }
116end:
117 return array;
118}
786207e0 119
5792eb34
JG
120struct bt_declaration *_bt_python_get_array_element_declaration(
121 struct bt_declaration *field)
122{
123 struct declaration_array *array_decl;
124 struct bt_declaration *ret = NULL;
125
126 if (!field) {
127 goto end;
128 }
129
130 array_decl = container_of(field, struct declaration_array, p);
131 ret = array_decl->elem;
132end:
133 return ret;
134}
135
3866c850
JG
136struct bt_declaration *_bt_python_get_sequence_element_declaration(
137 struct bt_declaration *field)
138{
139 struct declaration_sequence *sequence_decl;
140 struct bt_declaration *ret = NULL;
141
142 if (!field) {
143 goto end;
144 }
145
146 sequence_decl = container_of(field, struct declaration_sequence, p);
147 ret = sequence_decl->elem;
148end:
149 return ret;
150}
151
5792eb34
JG
152const char *_bt_python_get_array_string(struct bt_definition *field)
153{
154 struct definition_array *array;
155 const char *ret = NULL;
156
157 if (!field) {
158 goto end;
159 }
160
161 array = container_of(field, struct definition_array, p);
162 ret = array->string->str;
163end:
164 return ret;
165}
166
3866c850
JG
167const char *_bt_python_get_sequence_string(struct bt_definition *field)
168{
169 struct definition_sequence *sequence;
170 const char *ret = NULL;
171
172 if (!field) {
173 goto end;
174 }
175
176 sequence = container_of(field, struct definition_sequence, p);
177 ret = sequence->string->str;
178end:
179 return ret;
180}
181
786207e0
XH
182struct definition_sequence *_bt_python_get_sequence_from_def(
183 struct bt_definition *field)
184{
185 if (field && bt_ctf_field_type(
186 bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) {
187 return container_of(field, struct definition_sequence, p);
188 }
189
190 return NULL;
191}
ec8c88d7
JG
192
193int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field)
194{
195 int ret;
196
197 if (!field || field->type->declaration->id != CTF_TYPE_INTEGER) {
198 ret = -1;
199 goto end;
200 }
201
202 const struct bt_ctf_field_type_integer *type = container_of(field->type,
203 const struct bt_ctf_field_type_integer, parent);
204 ret = type->declaration.signedness;
205end:
206 return ret;
207}
208
209enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field)
210{
211 enum ctf_type_id type_id = CTF_TYPE_UNKNOWN;
212
213 if (!field) {
214 goto end;
215 }
216
217 type_id = field->type->declaration->id;
218end:
219 return type_id;
220}
b4b5e4b5
JG
221
222/*
223 * Swig doesn't handle returning pointers via output arguments properly...
224 * These functions only wrap the ctf-ir functions to provide them directly
225 * as regular return values.
226 */
227const char *_bt_python_ctf_field_type_enumeration_get_mapping(
228 struct bt_ctf_field_type *enumeration, size_t index,
229 int64_t *range_start, int64_t *range_end)
230{
231 int ret;
232 const char *name;
233
234 ret = bt_ctf_field_type_enumeration_get_mapping(enumeration, index,
235 &name, range_start, range_end);
236 return !ret ? name : NULL;
237}
238
239const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned(
240 struct bt_ctf_field_type *enumeration, size_t index,
241 uint64_t *range_start, uint64_t *range_end)
242{
243 int ret;
244 const char *name;
245
246 ret = bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration,
247 index, &name, range_start, range_end);
248 return !ret ? name : NULL;
249}
250
251const char *_bt_python_ctf_field_type_structure_get_field_name(
252 struct bt_ctf_field_type *structure, size_t index)
253{
254 int ret;
255 const char *name;
256 struct bt_ctf_field_type *type;
257
258 ret = bt_ctf_field_type_structure_get_field(structure, &name, &type,
259 index);
260 if (ret) {
261 name = NULL;
262 goto end;
263 }
264
265 bt_ctf_field_type_put(type);
266end:
267 return name;
268}
269
270struct bt_ctf_field_type *_bt_python_ctf_field_type_structure_get_field_type(
271 struct bt_ctf_field_type *structure, size_t index)
272{
273 int ret;
274 const char *name;
275 struct bt_ctf_field_type *type;
276
277 ret = bt_ctf_field_type_structure_get_field(structure, &name, &type,
278 index);
279 return !ret ? type : NULL;
280}
281
282const char *_bt_python_ctf_field_type_variant_get_field_name(
283 struct bt_ctf_field_type *variant, size_t index)
284{
285 int ret;
286 const char *name;
287 struct bt_ctf_field_type *type;
288
289 ret = bt_ctf_field_type_variant_get_field(variant, &name, &type,
290 index);
291 if (ret) {
292 name = NULL;
293 goto end;
294 }
295
296 bt_ctf_field_type_put(type);
297end:
298 return name;
299}
300
301struct bt_ctf_field_type *_bt_python_ctf_field_type_variant_get_field_type(
302 struct bt_ctf_field_type *variant, size_t index)
303{
304 int ret;
305 const char *name;
306 struct bt_ctf_field_type *type;
307
308 ret = bt_ctf_field_type_variant_get_field(variant, &name, &type,
309 index);
310 return !ret ? type : NULL;
311}
This page took 0.039135 seconds and 4 git commands to generate.