remember last current field in variant
[babeltrace.git] / types / variant.c
CommitLineData
c054553d
MD
1/*
2 * variant.c
3 *
4 * BabelTrace - Variant Type Converter
5 *
6 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19#include <babeltrace/compiler.h>
20#include <babeltrace/format.h>
21
22
23static
24struct type *variant_type_new(struct type_class *type_class,
25 struct declaration_scope *parent_scope);
26static
27void variant_type_free(struct type *type);
28
29void variant_copy(struct stream_pos *dest, const struct format *fdest,
30 struct stream_pos *src, const struct format *fsrc,
31 struct type *type)
32{
33 struct type_variant *variant = container_of(type, struct type_variant,
34 p);
35 struct type_class_variant *variant_class = variant->_class;
36 struct field *field;
37 struct type_class *field_class;
38 unsigned long i;
39
40 fsrc->variant_begin(src, variant_class);
41 fdest->variant_begin(dest, variant_class);
42
43 field = variant_type_get_current_field(variant);
44 field_class = field->type->p._class;
45 field_class->copy(dest, fdest, src, fsrc, &field->type->p);
46
47 fsrc->variant_end(src, variant_class);
48 fdest->variant_end(dest, variant_class);
49}
50
51static
52void variant_type_class_free(struct type_class *type_class)
53{
54 struct type_class_variant *variant_class =
55 container_of(type_class, struct type_class_variant, p);
56 unsigned long i;
57
58 g_hash_table_destroy(struct_class->fields_by_tag);
59
60 for (i = 0; i < variant_class->fields->len; i++) {
61 struct field *type_class_field =
62 &g_array_index(variant_class->fields,
63 struct type_class_field, i);
64 type_class_unref(field->type_class);
65 }
66 g_array_free(variant_class->fields, true);
67 g_free(variant_class);
68}
69
70struct type_class_variant *
71variant_type_class_new(const char *name)
72{
73 struct type_class_variant *variant_class;
74 struct type_class *type_class;
75 int ret;
76
77 variant_class = g_new(struct type_class_variant, 1);
78 type_class = &variant_class->p;
79 variant_class->fields_by_tag = g_hash_table_new(g_direct_hash,
80 g_direct_equal);
81 variant_class->fields = g_array_sized_new(FALSE, TRUE,
82 sizeof(struct type_class_field),
83 DEFAULT_NR_STRUCT_FIELDS);
84 type_class->name = g_quark_from_string(name);
85 type_class->alignment = 1;
86 type_class->copy = variant_copy;
87 type_class->class_free = _variant_type_class_free;
88 type_class->type_new = _variant_type_new;
89 type_class->type_free = _variant_type_free;
90 type_class->ref = 1;
91
92 if (type_class->name) {
93 ret = register_type(type_class);
94 if (ret)
95 goto error_register;
96 }
97 return struct_class;
98
99error_register:
100 g_hash_table_destroy(variant_class->fields_by_tag);
101 g_array_free(variant_class->fields, true);
102 g_free(variant_class);
103 return NULL;
104}
105
106static
107struct type_variant *_variant_type_new(struct type_class *type_class,
108 struct declaration_scope *parent_scope)
109{
110 struct type_class_variant *variant_class =
111 container_of(type_class, struct type_class_variant, p);
112 struct type_struct *variant;
113
114 variant = g_new(struct type_variant, 1);
115 type_class_ref(&variant_class->p);
116 variant->p._class = variant_class;
117 variant->p.ref = 1;
118 variant->scope = new_declaration_scope(parent_scope);
119 variant->fields = g_array_sized_new(FALSE, TRUE,
120 sizeof(struct field),
121 DEFAULT_NR_STRUCT_FIELDS);
122 variant->current_field = NULL;
123 return &variant->p;
124}
125
126static
127void variant_type_free(struct type *type)
128{
129 struct type_variant *variant = container_of(type, struct type_variant,
130 p);
131 unsigned long i;
132
133 for (i = 0; i < variant->fields->len; i++) {
134 struct field *field = &g_array_index(variant->fields,
135 struct field, i);
136 type_unref(field->type);
137 }
138 free_declaration_scope(variant->scope);
139 type_class_unref(variant->p._class);
140 g_free(variant);
141}
142
143void variant_type_class_add_field(struct type_class_variant *variant_class,
144 const char *tag_name,
145 struct type_class *type_class)
146{
147 struct type_class_field *field;
148 unsigned long index;
149
150 g_array_set_size(variant_class->fields, variant_class->fields->len + 1);
151 index = variant_class->fields->len - 1; /* last field (new) */
152 field = &g_array_index(variant_class->fields, struct type_class_field, index);
153 field->name = g_quark_from_string(tag_name);
154 type_ref(type_class);
155 field->type_class = type_class;
156 /* Keep index in hash rather than pointer, because array can relocate */
157 g_hash_table_insert(variant_class->fields_by_name,
158 (gpointer) (unsigned long) field->name,
159 (gpointer) index);
160 /*
161 * Alignment of variant is based on the alignment of its currently
162 * selected choice, so we leave variant alignment as-is (statically
163 * speaking).
164 */
165}
166
167struct type_class_field *
168struct_type_class_get_field_from_tag(struct type_class_variant *variant_class,
169 GQuark tag)
170{
171 unsigned long index;
172
173 index = (unsigned long) g_hash_table_lookup(variant_class->fields_by_tag,
174 (gconstpointer) (unsigned long) tag);
175 return &g_array_index(variant_class->fields, struct type_class_field, index);
176}
177
178/*
179 * tag_instance is assumed to be an enumeration.
180 */
181int variant_type_set_tag(struct type_variant *variant,
182 struct type *enum_tag_instance)
183{
184 struct type_enum *_enum =
185 container_of(struct type_enum, variant->enum_tag, p);
186 struct type_class_enum *enum_class = _enum->_class;
187 int missing_field = 0;
188 unsigned long i;
189
190 /*
191 * Strictly speaking, each enumerator must map to a field of the
192 * variant. However, we are even stricter here by requiring that each
193 * variant choice map to an enumerator too. We then validate that the
194 * number of enumerators equals the number of variant choices.
195 */
196 if (variant->_class->fields->len != enum_get_nr_enumerators(enum_class))
197 return -EPERM;
198
199 for (i = 0; i < variant->_class->fields->len; i++) {
200 struct type_class_field *field_class =
201 &g_array_index(variant->_class->fields,
202 struct type_class_field, i);
203 if (!enum_quark_to_range_set(enum_class, field_class->name)) {
204 missing_field = 1;
205 break;
206 }
207 }
208 if (missing_field)
209 return -EPERM;
210
211 /*
212 * Check the enumeration: it must map each value to one and only one
213 * enumerator tag.
214 * TODO: we should also check that each range map to one and only one
215 * tag. For the moment, we will simply check this dynamically in
216 * variant_type_get_current_field().
217 */
218
219 /* Set the enum tag field */
220 variant->enum_tag = enum_tag_instance;
221 return 0;
222}
223
224/*
225 * field returned only valid as long as the field structure is not appended to.
226 */
227struct field *
228variant_type_get_current_field(struct type_variant *variant)
229{
230 struct type_enum *_enum =
231 container_of(struct type_enum, variant->enum_tag, p);
232 struct variat_type_class *variant_class = variant->_class;
233 unsigned long index;
234 GArray *tag_array;
235 GQuark tag;
236
237 tag_array = _enum->value;
238 /*
239 * The 1 to 1 mapping from enumeration to value should have been already
240 * checked. (see TODO above)
241 */
242 assert(tag_array->len == 1);
243 tag = g_array_index(tag_array, GQuark, 0);
244 index = (unsigned long) g_hash_table_lookup(variant_class->fields_by_tag,
245 (gconstpointer) (unsigned long) tag);
96354dac
MD
246 variant->current_field = &g_array_index(variant_class->fields, struct field, index);
247 return variant->current_field;
c054553d 248}
This page took 0.029949 seconds and 4 git commands to generate.