blob: 7de0992dba7f1302cea3a0f366ba26d01e4ee2d9 [file] [log] [blame]
David 'Digit' Turner2910f182010-05-10 18:48:35 -07001/*
2 * QFloat Module
3 *
David 'Digit' Turner2910f182010-05-10 18:48:35 -07004 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +010014#include "qapi/qmp/qfloat.h"
15#include "qapi/qmp/qobject.h"
David 'Digit' Turner2910f182010-05-10 18:48:35 -070016#include "qemu-common.h"
17
18static void qfloat_destroy_obj(QObject *obj);
19
20static const QType qfloat_type = {
21 .code = QTYPE_QFLOAT,
22 .destroy = qfloat_destroy_obj,
23};
24
25/**
26 * qfloat_from_int(): Create a new QFloat from a float
27 *
28 * Return strong reference.
29 */
30QFloat *qfloat_from_double(double value)
31{
32 QFloat *qf;
33
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +010034 qf = g_malloc(sizeof(*qf));
David 'Digit' Turner2910f182010-05-10 18:48:35 -070035 qf->value = value;
36 QOBJECT_INIT(qf, &qfloat_type);
37
38 return qf;
39}
40
41/**
42 * qfloat_get_double(): Get the stored float
43 */
44double qfloat_get_double(const QFloat *qf)
45{
46 return qf->value;
47}
48
49/**
50 * qobject_to_qfloat(): Convert a QObject into a QFloat
51 */
52QFloat *qobject_to_qfloat(const QObject *obj)
53{
54 if (qobject_type(obj) != QTYPE_QFLOAT)
55 return NULL;
56
57 return container_of(obj, QFloat, base);
58}
59
60/**
61 * qfloat_destroy_obj(): Free all memory allocated by a
62 * QFloat object
63 */
64static void qfloat_destroy_obj(QObject *obj)
65{
66 assert(obj != NULL);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +010067 g_free(qobject_to_qfloat(obj));
David 'Digit' Turner2910f182010-05-10 18:48:35 -070068}