Add type class/type structure management
[babeltrace.git] / types / float.c
CommitLineData
0a46062b 1/*
ccd7e1c8 2 * float.c
0a46062b 3 *
ccd7e1c8 4 * BabelTrace - Float Type Converter
0a46062b 5 *
c054553d 6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
0a46062b 7 *
ccd7e1c8
MD
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:
0a46062b 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
0a46062b
MD
17 */
18
19#include <babeltrace/compiler.h>
4c8bfb7e 20#include <babeltrace/format.h>
0a46062b 21
c054553d
MD
22static
23struct type_float *_float_type_new(struct type_class *type_class,
24 struct declaration_scope *parent_scope);
25static
26void _float_type_free(struct type *type);
27
4c8bfb7e
MD
28void float_copy(struct stream_pos *destp,
29 const struct format *fdest,
30 struct stream_pos *srcp,
31 const struct format *fsrc,
c054553d 32 struct type *type)
0a46062b 33{
c054553d
MD
34 struct type_float *_float = container_of(type, struct type_float, p);
35 struct type_class_float *float_class = _float->_class;
0a46062b
MD
36
37 if (fsrc->float_copy == fdest->float_copy) {
4c8bfb7e 38 fsrc->float_copy(destp, srcp, float_class);
0a46062b
MD
39 } else {
40 double v;
41
4c8bfb7e
MD
42 v = fsrc->double_read(srcp, float_class);
43 fdest->double_write(destp, float_class, v);
0a46062b
MD
44 }
45}
46
c054553d
MD
47static
48void _float_type_class_free(struct type_class *type_class)
90b676d7
MD
49{
50 struct type_class_float *float_class =
51 container_of(type_class, struct type_class_float, p);
c054553d
MD
52
53 type_class_unref(&float_class->exp->p);
54 type_class_unref(&float_class->mantissa->p);
55 type_class_unref(&float_class->sign->p);
56 g_free(float_class);
90b676d7
MD
57}
58
c054553d
MD
59struct type_class_float *
60float_type_class_new(const char *name, size_t mantissa_len,
61 size_t exp_len, int byte_order, size_t alignment)
0a46062b
MD
62{
63 struct type_class_float *float_class;
bed864a7 64 struct type_class *type_class;
0a46062b
MD
65 int ret;
66
0a46062b 67 float_class = g_new(struct type_class_float, 1);
bed864a7 68 type_class = &float_class->p;
bed864a7
MD
69 type_class->name = g_quark_from_string(name);
70 type_class->alignment = alignment;
71 type_class->copy = float_copy;
c054553d
MD
72 type_class->class_free = _float_type_class_free;
73 type_class->type_new = _float_type_new;
74 type_class->type_free = _float_type_free;
4c8bfb7e 75 type_class->ref = 1;
0a46062b 76 float_class->byte_order = byte_order;
bed864a7 77
7fe00194
MD
78 float_class->sign = integer_type_new(NULL, 1,
79 byte_order, false, 1);
11d43b90
MD
80 if (!float_class->mantissa)
81 goto error_sign;
7fe00194
MD
82 float_class->mantissa = integer_type_new(NULL, mantissa_len - 1,
83 byte_order, false, 1);
bed864a7
MD
84 if (!float_class->mantissa)
85 goto error_mantissa;
7fe00194
MD
86 float_class->exp = integer_type_new(NULL, exp_len,
87 byte_order, true, 1);
bed864a7
MD
88 if (!float_class->exp)
89 goto error_exp;
90
0a46062b 91 if (float_class->p.name) {
be85c1c7 92 ret = register_type(&float_class->p);
bed864a7
MD
93 if (ret)
94 goto error_register;
0a46062b
MD
95 }
96 return float_class;
bed864a7
MD
97
98error_register:
c054553d 99 type_class_unref(&float_class->exp->p);
bed864a7 100error_exp:
c054553d 101 type_class_unref(&float_class->mantissa->p);
bed864a7 102error_mantissa:
c054553d 103 type_class_unref(&float_class->sign->p);
11d43b90 104error_sign:
bed864a7
MD
105 g_free(float_class);
106 return NULL;
0a46062b 107}
c054553d
MD
108
109static
110struct type_float *_float_type_new(struct type_class *type_class,
111 struct declaration_scope *parent_scope)
112{
113 struct type_class_float *float_class =
114 container_of(type_class, struct type_class_float, p);
115 struct type_float *_float;
116
117 _float = g_new(struct type_float, 1);
118 type_class_ref(&_float_class->p);
119 _float->p._class = _float_class;
120 _float->p.ref = 1;
121 _float->value = 0.0;
122 return &_float->p;
123}
124
125static
126void _float_type_free(struct type *type)
127{
128 struct type_float *_float = container_of(type, struct type_float, p);
129
130 type_class_unref(_float->p._class);
131 g_free(_float);
132}
This page took 0.028917 seconds and 4 git commands to generate.