Rename: type_class, type -> type, declaration
[babeltrace.git] / types / integer.c
1 /*
2 * integer.c
3 *
4 * BabelTrace - Integer 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 #include <stdint.h>
23
24 static
25 struct declaration *_integer_declaration_new(struct type *type,
26 struct declaration_scope *parent_scope);
27 static
28 void _integer_declaration_free(struct declaration *declaration);
29
30 void integer_copy(struct stream_pos *dest, const struct format *fdest,
31 struct stream_pos *src, const struct format *fsrc,
32 struct declaration *declaration)
33 {
34 struct declaration_integer *integer =
35 container_of(type, struct declaration_integer, p);
36 struct type_integer *integer_type = integer->type;
37
38 if (!integer_type->signedness) {
39 uint64_t v;
40
41 v = fsrc->uint_read(src, integer_type);
42 fdest->uint_write(dest, integer_type, v);
43 } else {
44 int64_t v;
45
46 v = fsrc->int_read(src, integer_type);
47 fdest->int_write(dest, integer_type, v);
48 }
49 }
50
51 static
52 void _integer_type_free(struct type *type)
53 {
54 struct type_integer *integer_type =
55 container_of(type, struct type_integer, p);
56 g_free(integer_type);
57 }
58
59 struct type_integer *
60 integer_type_new(const char *name, size_t len, int byte_order,
61 int signedness, size_t alignment)
62 {
63 struct type_integer *integer_type;
64 int ret;
65
66 integer_type = g_new(struct type_integer, 1);
67 integer_type->p.name = g_quark_from_string(name);
68 integer_type->p.alignment = alignment;
69 integer_type->p.copy = integer_copy;
70 integer_type->p.type_free = _integer_type_free;
71 integer_type->p.declaration_free = _integer_declaration_free;
72 integer_type->p.declaration_new = _integer_declaration_new;
73 integer_type->p.ref = 1;
74 integer_type->len = len;
75 integer_type->byte_order = byte_order;
76 integer_type->signedness = signedness;
77 if (integer_type->p.name) {
78 ret = register_type(&integer_type->p);
79 if (ret) {
80 g_free(integer_type);
81 return NULL;
82 }
83 }
84 return integer_type;
85 }
86
87 static
88 struct declaration *
89 _integer_declaration_new(struct type *type,
90 struct declaration_scope *parent_scope)
91 {
92 struct type_integer *integer_type =
93 container_of(type, struct type_integer, p);
94 struct declaration_integer *integer;
95
96 integer = g_new(struct declaration_integer, 1);
97 type_ref(&integer_type->p);
98 integer->p.type = integer_type;
99 integer->p.ref = 1;
100 integer->value._unsigned = 0;
101 return &integer->p;
102 }
103
104 static
105 void _integer_declaration_free(struct declaration *declaration)
106 {
107 struct declaration_integer *integer =
108 container_of(declaration, struct declaration_integer, p);
109
110 type_unref(integer->p.type);
111 g_free(integer);
112 }
This page took 0.031453 seconds and 5 git commands to generate.