Use inheritance for trace descriptor and positions
[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 22static
f6625916 23struct definition *_float_definition_new(struct declaration *declaration,
05c749e5
MD
24 struct definition_scope *parent_scope,
25 GQuark field_name, int index);
c054553d 26static
e1151715 27void _float_definition_free(struct definition *definition);
c054553d 28
4c8bfb7e
MD
29void float_copy(struct stream_pos *destp,
30 const struct format *fdest,
31 struct stream_pos *srcp,
32 const struct format *fsrc,
e1151715 33 struct definition *definition)
0a46062b 34{
e1151715
MD
35 struct definition_float *_float =
36 container_of(definition, struct definition_float, p);
f6625916 37 struct declaration_float *float_declaration = _float->declaration;
dc48ecad 38 long double v;
0a46062b 39
dc48ecad
MD
40 v = fsrc->ldouble_read(srcp, float_declaration);
41 _float->value = v;
42 if (fdest)
43 fdest->ldouble_write(destp, float_declaration, v);
0a46062b
MD
44}
45
c054553d 46static
f6625916 47void _float_declaration_free(struct declaration *declaration)
90b676d7 48{
f6625916
MD
49 struct declaration_float *float_declaration =
50 container_of(declaration, struct declaration_float, p);
c054553d 51
f6625916
MD
52 declaration_unref(&float_declaration->exp->p);
53 declaration_unref(&float_declaration->mantissa->p);
54 declaration_unref(&float_declaration->sign->p);
55 g_free(float_declaration);
90b676d7
MD
56}
57
f6625916 58struct declaration_float *
add40b62 59 float_declaration_new(size_t mantissa_len,
e19c3d69 60 size_t exp_len, int byte_order, size_t alignment)
0a46062b 61{
f6625916
MD
62 struct declaration_float *float_declaration;
63 struct declaration *declaration;
0a46062b 64
f6625916
MD
65 float_declaration = g_new(struct declaration_float, 1);
66 declaration = &float_declaration->p;
67 declaration->id = CTF_TYPE_FLOAT;
f6625916
MD
68 declaration->alignment = alignment;
69 declaration->copy = float_copy;
70 declaration->declaration_free = _float_declaration_free;
71 declaration->definition_new = _float_definition_new;
72 declaration->definition_free = _float_definition_free;
73 declaration->ref = 1;
74 float_declaration->byte_order = byte_order;
e19c3d69 75
add40b62 76 float_declaration->sign = integer_declaration_new(1,
e19c3d69 77 byte_order, false, 1);
add40b62 78 float_declaration->mantissa = integer_declaration_new(mantissa_len - 1,
e19c3d69 79 byte_order, false, 1);
add40b62 80 float_declaration->exp = integer_declaration_new(exp_len,
e19c3d69 81 byte_order, true, 1);
f6625916 82 return float_declaration;
0a46062b 83}
c054553d
MD
84
85static
e1151715 86struct definition *
f6625916 87 _float_definition_new(struct declaration *declaration,
05c749e5
MD
88 struct definition_scope *parent_scope,
89 GQuark field_name, int index)
c054553d 90{
f6625916
MD
91 struct declaration_float *float_declaration =
92 container_of(declaration, struct declaration_float, p);
e1151715 93 struct definition_float *_float;
c054553d 94
e1151715 95 _float = g_new(struct definition_float, 1);
f6625916 96 declaration_ref(&float_declaration->p);
05c749e5
MD
97 _float->p.declaration = declaration;
98 _float->declaration = float_declaration;
c054553d 99 _float->p.ref = 1;
05c749e5 100 _float->p.index = index;
c054553d
MD
101 _float->value = 0.0;
102 return &_float->p;
103}
104
105static
e1151715 106void _float_definition_free(struct definition *definition)
c054553d 107{
e1151715
MD
108 struct definition_float *_float =
109 container_of(definition, struct definition_float, p);
c054553d 110
f6625916 111 declaration_unref(_float->p.declaration);
c054553d
MD
112 g_free(_float);
113}
This page took 0.027979 seconds and 4 git commands to generate.