Add type class/type structure management
[babeltrace.git] / types / string.c
index 3f9c1787f3c942e165c8e1284bdd1633862a54ba..e76ac77ae7620def6537f415642ef8375bd9210d 100644 (file)
@@ -1,38 +1,42 @@
 /*
- * BabelTrace - String Type Converter
+ * string.c
  *
- * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * BabelTrace - String Type Converter
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
  *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
  */
 
 #include <babeltrace/compiler.h>
 #include <babeltrace/align.h>
-#include <babeltrace/types.h>
+#include <babeltrace/format.h>
+
+static
+struct type_string *_string_type_new(struct type_class *type_class,
+                                    struct declaration_scope *parent_scope);
+static
+void _string_type_free(struct type *type);
 
 void string_copy(struct stream_pos *dest, const struct format *fdest, 
                 struct stream_pos *src, const struct format *fsrc,
-                const struct type_class *type_class)
+                struct type *type)
 {
-       struct type_class_string *string_class =
-               container_of(type_class, struct type_class_string, p);
+       struct type_string *string = container_of(type, struct type_string, p);
+       struct type_class_string *string_class = string->_class;
 
        if (fsrc->string_copy == fdest->string_copy) {
                fsrc->string_copy(dest, src, string_class);
        } else {
-               unsigned char *tmp = NULL;
+               char *tmp = NULL;
 
                fsrc->string_read(&tmp, src, string_class);
                fdest->string_write(dest, tmp, string_class);
@@ -40,19 +44,16 @@ void string_copy(struct stream_pos *dest, const struct format *fdest,
        }
 }
 
-void string_type_free(struct type_class_string *string_class)
-{
-       g_free(string_class);
-}
-
-static void _string_type_free(struct type_class *type_class)
+static
+void _string_type_class_free(struct type_class_string *string_class)
 {
        struct type_class_string *string_class =
                container_of(type_class, struct type_class_string, p);
-       string_type_free(string_class);
+       g_free(string_class);
 }
 
-struct type_class_string *string_type_new(const char *name)
+struct type_class_string *
+string_type_class_new(const char *name)
 {
        struct type_class_string *string_class;
        int ret;
@@ -61,9 +62,12 @@ struct type_class_string *string_type_new(const char *name)
        string_class->p.name = g_quark_from_string(name);
        string_class->p.alignment = CHAR_BIT;
        string_class->p.copy = string_copy;
-       string_class->p.free = _string_type_free;
+       string_class->p.class_free = _string_type_class_free;
+       string_class->p.type_new = _string_type_new;
+       string_class->p.type_free = _string_type_free;
+       string_class->p.ref = 1;
        if (string_class->p.name) {
-               ret = ctf_register_type(&string_class->p);
+               ret = register_type(&string_class->p);
                if (ret) {
                        g_free(string_class);
                        return NULL;
@@ -71,3 +75,30 @@ struct type_class_string *string_type_new(const char *name)
        }
        return string_class;
 }
+
+static
+struct type_string *_string_type_new(struct type_class *type_class,
+                                    struct declaration_scope *parent_scope)
+{
+       struct type_class_string *string_class =
+               container_of(type_class, struct type_class_string, p);
+       struct type_string *string;
+
+       string = g_new(struct type_string, 1);
+       type_class_ref(&string_class->p);
+       string->p._class = string_class;
+       string->p.ref = 1;
+       string->value = NULL;
+       return &string->p;
+}
+
+static
+void _string_type_free(struct type *type)
+{
+       struct type_string *string =
+               container_of(type, struct type_string, p);
+
+       type_class_unref(string->p._class);
+       g_free(string->value);
+       g_free(string);
+}
This page took 0.024077 seconds and 4 git commands to generate.