Add type class/type structure management
[babeltrace.git] / types / string.c
1 /*
2 * string.c
3 *
4 * BabelTrace - String Type Converter
5 *
6 * Copyright 2010, 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/align.h>
21 #include <babeltrace/format.h>
22
23 static
24 struct type_string *_string_type_new(struct type_class *type_class,
25 struct declaration_scope *parent_scope);
26 static
27 void _string_type_free(struct type *type);
28
29 void string_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_string *string = container_of(type, struct type_string, p);
34 struct type_class_string *string_class = string->_class;
35
36 if (fsrc->string_copy == fdest->string_copy) {
37 fsrc->string_copy(dest, src, string_class);
38 } else {
39 char *tmp = NULL;
40
41 fsrc->string_read(&tmp, src, string_class);
42 fdest->string_write(dest, tmp, string_class);
43 fsrc->string_free_temp(tmp);
44 }
45 }
46
47 static
48 void _string_type_class_free(struct type_class_string *string_class)
49 {
50 struct type_class_string *string_class =
51 container_of(type_class, struct type_class_string, p);
52 g_free(string_class);
53 }
54
55 struct type_class_string *
56 string_type_class_new(const char *name)
57 {
58 struct type_class_string *string_class;
59 int ret;
60
61 string_class = g_new(struct type_class_string, 1);
62 string_class->p.name = g_quark_from_string(name);
63 string_class->p.alignment = CHAR_BIT;
64 string_class->p.copy = string_copy;
65 string_class->p.class_free = _string_type_class_free;
66 string_class->p.type_new = _string_type_new;
67 string_class->p.type_free = _string_type_free;
68 string_class->p.ref = 1;
69 if (string_class->p.name) {
70 ret = register_type(&string_class->p);
71 if (ret) {
72 g_free(string_class);
73 return NULL;
74 }
75 }
76 return string_class;
77 }
78
79 static
80 struct type_string *_string_type_new(struct type_class *type_class,
81 struct declaration_scope *parent_scope)
82 {
83 struct type_class_string *string_class =
84 container_of(type_class, struct type_class_string, p);
85 struct type_string *string;
86
87 string = g_new(struct type_string, 1);
88 type_class_ref(&string_class->p);
89 string->p._class = string_class;
90 string->p.ref = 1;
91 string->value = NULL;
92 return &string->p;
93 }
94
95 static
96 void _string_type_free(struct type *type)
97 {
98 struct type_string *string =
99 container_of(type, struct type_string, p);
100
101 type_class_unref(string->p._class);
102 g_free(string->value);
103 g_free(string);
104 }
This page took 0.031617 seconds and 5 git commands to generate.