0cf0ca6860de0434bbeac35be9d910297d5da03b
4 * BabelTrace - Variant Type Converter
6 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
24 struct type
*variant_type_new(struct type_class
*type_class
,
25 struct declaration_scope
*parent_scope
);
27 void variant_type_free(struct type
*type
);
29 void variant_copy(struct stream_pos
*dest
, const struct format
*fdest
,
30 struct stream_pos
*src
, const struct format
*fsrc
,
33 struct type_variant
*variant
= container_of(type
, struct type_variant
,
35 struct type_class_variant
*variant_class
= variant
->_class
;
37 struct type_class
*field_class
;
40 fsrc
->variant_begin(src
, variant_class
);
41 fdest
->variant_begin(dest
, variant_class
);
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
);
47 fsrc
->variant_end(src
, variant_class
);
48 fdest
->variant_end(dest
, variant_class
);
52 void variant_type_class_free(struct type_class
*type_class
)
54 struct type_class_variant
*variant_class
=
55 container_of(type_class
, struct type_class_variant
, p
);
58 g_hash_table_destroy(struct_class
->fields_by_tag
);
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
);
66 g_array_free(variant_class
->fields
, true);
67 g_free(variant_class
);
70 struct type_class_variant
*
71 variant_type_class_new(const char *name
)
73 struct type_class_variant
*variant_class
;
74 struct type_class
*type_class
;
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
,
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
;
92 if (type_class
->name
) {
93 ret
= register_type(type_class
);
100 g_hash_table_destroy(variant_class
->fields_by_tag
);
101 g_array_free(variant_class
->fields
, true);
102 g_free(variant_class
);
107 struct type_variant
*_variant_type_new(struct type_class
*type_class
,
108 struct declaration_scope
*parent_scope
)
110 struct type_class_variant
*variant_class
=
111 container_of(type_class
, struct type_class_variant
, p
);
112 struct type_struct
*variant
;
114 variant
= g_new(struct type_variant
, 1);
115 type_class_ref(&variant_class
->p
);
116 variant
->p
._class
= variant_class
;
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
;
127 void variant_type_free(struct type
*type
)
129 struct type_variant
*variant
= container_of(type
, struct type_variant
,
133 for (i
= 0; i
< variant
->fields
->len
; i
++) {
134 struct field
*field
= &g_array_index(variant
->fields
,
136 type_unref(field
->type
);
138 free_declaration_scope(variant
->scope
);
139 type_class_unref(variant
->p
._class
);
143 void variant_type_class_add_field(struct type_class_variant
*variant_class
,
144 const char *tag_name
,
145 struct type_class
*type_class
)
147 struct type_class_field
*field
;
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
,
161 * Alignment of variant is based on the alignment of its currently
162 * selected choice, so we leave variant alignment as-is (statically
167 struct type_class_field
*
168 struct_type_class_get_field_from_tag(struct type_class_variant
*variant_class
,
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
);
179 * tag_instance is assumed to be an enumeration.
181 int variant_type_set_tag(struct type_variant
*variant
,
182 struct type
*enum_tag_instance
)
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;
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.
196 if (variant
->_class
->fields
->len
!= enum_get_nr_enumerators(enum_class
))
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
)) {
212 * Check the enumeration: it must map each value to one and only one
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().
219 /* Set the enum tag field */
220 variant
->enum_tag
= enum_tag_instance
;
225 * field returned only valid as long as the field structure is not appended to.
228 variant_type_get_current_field(struct type_variant
*variant
)
230 struct type_enum
*_enum
=
231 container_of(struct type_enum
, variant
->enum_tag
, p
);
232 struct variat_type_class
*variant_class
= variant
->_class
;
237 tag_array
= _enum
->value
;
239 * The 1 to 1 mapping from enumeration to value should have been already
240 * checked. (see TODO above)
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
);
246 return &g_array_index(variant_class
->fields
, struct field
, index
);
This page took 0.03453 seconds and 3 git commands to generate.