I/O structures accessors generation (work in progress)
[babeltrace.git] / types / sequence.c
index f6025af61b3470fdda7c43ff2e8feefbdfa373e1..9d84d0eece59e8a9d4a1f878e5da8cb5296a7818 100644 (file)
@@ -3,7 +3,7 @@
  *
  * BabelTrace - Sequence Type Converter
  *
- * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
 #include <babeltrace/compiler.h>
 #include <babeltrace/format.h>
 
+#ifndef max
+#define max(a, b)      ((a) < (b) ? (b) : (a))
+#endif
+
+static
+struct declaration *_sequence_declaration_new(struct type *type,
+                                       struct declaration_scope *parent_scope);
+static
+void _sequence_declaration_free(struct declaration *declaration);
+
 void sequence_copy(struct stream_pos *dest, const struct format *fdest, 
                   struct stream_pos *src, const struct format *fsrc,
-                  const struct type_class *type_class)
+                  struct declaration *declaration)
 {
-       struct type_class_sequence *sequence_class =
-               container_of(type_class, struct type_class_sequence, p);
-       unsigned int i;
-       size_t len;
+       struct declaration_sequence *sequence =
+               container_of(declaration, struct declaration_sequence, p);
+       struct type_sequence *sequence_type = sequence->type;
+       uint64_t i;
 
-       fsrc->sequence_begin(src, sequence_class);
-       fdest->sequence_begin(dest, sequence_class);
+       fsrc->sequence_begin(src, sequence_type);
+       fdest->sequence_begin(dest, sequence_type);
 
-       len = fsrc->uint_read(src, sequence_class->len_class);
-       fdest->uint_write(dest, sequence_class->len_class, len);
+       sequence->len->p.type->copy(dest, fdest, src, fsrc,
+                                   &sequence->len->p);
 
-       for (i = 0; i < len; i++) {
-               struct type_class *elem_class = sequence_class->elem;
-               elem_class->copy(dest, fdest, src, fsrc, elem_class);
+       for (i = 0; i < sequence->len->value._unsigned; i++) {
+               struct declaration *elem =
+                       sequence->current_element.declaration;
+               elem->type->copy(dest, fdest, src, fsrc, elem);
        }
-       fsrc->sequence_end(src, sequence_class);
-       fdest->sequence_end(dest, sequence_class);
+       fsrc->sequence_end(src, sequence_type);
+       fdest->sequence_end(dest, sequence_type);
 }
 
-void sequence_type_free(struct type_class_sequence *sequence_class)
+static
+void _sequence_type_free(struct type *type)
 {
-       sequence_class->elem->free(sequence_class->elem);
-       type_unref(&sequence_class->len_class->p);
-       type_unref(sequence_class->elem);
-       g_free(sequence_class);
-}
+       struct type_sequence *sequence_type =
+               container_of(type, struct type_sequence, p);
 
-static void _sequence_type_free(struct type_class *type_class)
-{
-       struct type_class_sequence *sequence_class =
-               container_of(type_class, struct type_class_sequence, p);
-       sequence_type_free(sequence_class);
+       free_type_scope(sequence_type->scope);
+       type_unref(&sequence_type->len_type->p);
+       type_unref(sequence_type->elem);
+       g_free(sequence_type);
 }
 
-struct type_class_sequence *
-sequence_type_new(const char *name, struct type_class_integer *len_class,
-                 struct type_class *elem)
+struct type_sequence *
+       sequence_type_new(const char *name, struct type_integer *len_type,
+                         struct type *elem_type,
+                         struct type_scope *parent_scope)
 {
-       struct type_class_sequence *sequence_class;
-       struct type_class *type_class;
-       int ret;
+       struct type_sequence *sequence_type;
+       struct type *type;
 
-       sequence_class = g_new(struct type_class_sequence, 1);
-       type_class = &sequence_class->p;
+       sequence_type = g_new(struct type_sequence, 1);
+       type = &sequence_type->p;
+       assert(!len_type->signedness);
+       type_ref(&len_type->p);
+       sequence_type->len_type = len_type;
+       type_ref(elem_type);
+       sequence_type->elem = elem_type;
+       sequence_type->scope = new_type_scope(parent_scope);
+       type->id = CTF_TYPE_SEQUENCE;
+       type->name = g_quark_from_string(name);
+       type->alignment = max(len_type->p.alignment, elem_type->alignment);
+       type->copy = sequence_copy;
+       type->type_free = _sequence_type_free;
+       type->declaration_new = _sequence_declaration_new;
+       type->declaration_free = _sequence_declaration_free;
+       type->ref = 1;
+       return sequence_type;
+}
 
-       assert(!len_class->signedness);
+static
+struct declaration *_sequence_declaration_new(struct type *type,
+                               struct declaration_scope *parent_scope)
+{
+       struct type_sequence *sequence_type =
+               container_of(type, struct type_sequence, p);
+       struct declaration_sequence *sequence;
+       struct declaration *len_parent;
 
-       type_ref(&len_class->p);
-       sequence_class->len_class = len_class;
-       type_ref(elem);
-       sequence_class->elem = elem;
-       type_class->name = g_quark_from_string(name);
-       type_class->alignment = max(len_class->p.alignment,
-                                   elem->alignment);
-       type_class->copy = sequence_copy;
-       type_class->free = _sequence_type_free;
-       type_class->ref = 1;
+       sequence = g_new(struct declaration_sequence, 1);
+       type_ref(&sequence_type->p);
+       sequence->p.type = type;
+       sequence->type = sequence_type;
+       sequence->p.ref = 1;
+       sequence->scope = new_declaration_scope(parent_scope);
+       len_parent = sequence_type->len_type->p.declaration_new(&sequence_type->len_type->p,
+                                                               parent_scope);
+       sequence->len =
+               container_of(len_parent, struct declaration_integer, p);
+       sequence->current_element.declaration =
+               sequence_type->elem->declaration_new(sequence_type->elem,
+                                                    parent_scope);
+       return &sequence->p;
+}
 
-       if (type_class->name) {
-               ret = ctf_register_type(type_class);
-               if (ret)
-                       goto error_register;
-       }
-       return sequence_class;
+static
+void _sequence_declaration_free(struct declaration *declaration)
+{
+       struct declaration_sequence *sequence =
+               container_of(declaration, struct declaration_sequence, p);
+       struct declaration *len_declaration = &sequence->len->p;
+       struct declaration *elem_declaration =
+               sequence->current_element.declaration;
 
-error_register:
-       type_unref(&len_class->p);
-       type_unref(elem);
-       g_free(sequence_class);
-       return NULL;
+       len_declaration->type->declaration_free(len_declaration);
+       elem_declaration->type->declaration_free(elem_declaration);
+       free_declaration_scope(sequence->scope);
+       type_unref(sequence->p.type);
+       g_free(sequence);
 }
This page took 0.024555 seconds and 4 git commands to generate.