I/O structures accessors generation (work in progress)
[babeltrace.git] / types / variant.c
index 87cf9632a8a4b868602f92bb6c4e3e4d3375bf02..ea20808b7205f1e31ea1fee4dce603e5584359bf 100644 (file)
@@ -18,7 +18,7 @@
 
 #include <babeltrace/compiler.h>
 #include <babeltrace/format.h>
-
+#include <errno.h>
 
 static
 struct declaration *_variant_declaration_new(struct type *type,
@@ -35,14 +35,13 @@ void variant_copy(struct stream_pos *dest, const struct format *fdest,
        struct type_variant *variant_type = variant->type;
        struct field *field;
        struct type *field_type;
-       unsigned long i;
 
        fsrc->variant_begin(src, variant_type);
        fdest->variant_begin(dest, variant_type);
 
        field = variant_get_current_field(variant);
-       field_type = field->type->p.type;
-       field_type->copy(dest, fdest, src, fsrc, &field->type->p);
+       field_type = field->declaration->type;
+       field_type->copy(dest, fdest, src, fsrc, field->declaration);
 
        fsrc->variant_end(src, variant_type);
        fdest->variant_end(dest, variant_type);
@@ -55,23 +54,24 @@ void _variant_type_free(struct type *type)
                container_of(type, struct type_variant, p);
        unsigned long i;
 
+       free_type_scope(variant_type->scope);
        g_hash_table_destroy(variant_type->fields_by_tag);
 
        for (i = 0; i < variant_type->fields->len; i++) {
-               struct field *type_field =
+               struct type_field *type_field =
                        &g_array_index(variant_type->fields,
                                       struct type_field, i);
-               type_unref(field->type);
+               type_unref(type_field->type);
        }
        g_array_free(variant_type->fields, true);
        g_free(variant_type);
 }
 
-struct type_variant *variant_type_new(const char *name)
+struct type_variant *variant_type_new(const char *name,
+                                     struct type_scope *parent_scope)
 {
        struct type_variant *variant_type;
        struct type *type;
-       int ret;
 
        variant_type = g_new(struct type_variant, 1);
        type = &variant_type->p;
@@ -80,6 +80,8 @@ struct type_variant *variant_type_new(const char *name)
        variant_type->fields = g_array_sized_new(FALSE, TRUE,
                                                 sizeof(struct type_field),
                                                 DEFAULT_NR_STRUCT_FIELDS);
+       variant_type->scope = new_type_scope(parent_scope);
+       type->id = CTF_TYPE_VARIANT;
        type->name = g_quark_from_string(name);
        type->alignment = 1;
        type->copy = variant_copy;
@@ -87,19 +89,7 @@ struct type_variant *variant_type_new(const char *name)
        type->declaration_new = _variant_declaration_new;
        type->declaration_free = _variant_declaration_free;
        type->ref = 1;
-
-       if (type->name) {
-               ret = register_type(type);
-               if (ret)
-                       goto error_register;
-       }
        return variant_type;
-
-error_register:
-       g_hash_table_destroy(variant_type->fields_by_tag);
-       g_array_free(variant_type->fields, true);
-       g_free(variant_type);
-       return NULL;
 }
 
 static
@@ -110,26 +100,42 @@ struct declaration *
        struct type_variant *variant_type =
                container_of(type, struct type_variant, p);
        struct declaration_variant *variant;
+       unsigned long i;
 
        variant = g_new(struct declaration_variant, 1);
        type_ref(&variant_type->p);
-       variant->p.type = variant_type;
+       variant->p.type = type;
+       variant->type = variant_type;
        variant->p.ref = 1;
        variant->scope = new_declaration_scope(parent_scope);
        variant->fields = g_array_sized_new(FALSE, TRUE,
                                            sizeof(struct field),
                                            DEFAULT_NR_STRUCT_FIELDS);
+       g_array_set_size(variant->fields, variant_type->fields->len);
+       for (i = 0; i < variant_type->fields->len; i++) {
+               struct type_field *type_field =
+                       &g_array_index(variant_type->fields,
+                                      struct type_field, i);
+               struct field *field = &g_array_index(variant->fields,
+                                                    struct field, i);
+
+               field->name = type_field->name;
+               field->declaration =
+                       type_field->type->declaration_new(type_field->type,
+                                                         variant->scope);
+       }
        variant->current_field = NULL;
        return &variant->p;
 }
 
 static
-void variant_declaration_free(struct declaration *declaration)
+void _variant_declaration_free(struct declaration *declaration)
 {
        struct declaration_variant *variant =
                container_of(declaration, struct declaration_variant, p);
        unsigned long i;
 
+       assert(variant->fields->len == variant->type->fields->len);
        for (i = 0; i < variant->fields->len; i++) {
                struct field *field = &g_array_index(variant->fields,
                                                     struct field, i);
@@ -154,7 +160,7 @@ void variant_type_add_field(struct type_variant *variant_type,
        type_ref(tag_type);
        field->type = tag_type;
        /* Keep index in hash rather than pointer, because array can relocate */
-       g_hash_table_insert(variant_type->fields_by_name,
+       g_hash_table_insert(variant_type->fields_by_tag,
                            (gpointer) (unsigned long) field->name,
                            (gpointer) index);
        /*
@@ -227,7 +233,7 @@ struct field *variant_get_current_field(struct declaration_variant *variant)
 {
        struct declaration_enum *_enum =
                container_of(variant->enum_tag, struct declaration_enum, p);
-       struct variant_type *variant_type = variant->type;
+       struct type_variant *variant_type = variant->type;
        unsigned long index;
        GArray *tag_array;
        GQuark tag;
This page took 0.024511 seconds and 4 git commands to generate.