xref: /openbmc/qemu/qobject/qbool.c (revision 1fd6bb44)
1 /*
2  * QBool Module
3  *
4  * 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 
14 #include "qapi/qmp/qbool.h"
15 #include "qapi/qmp/qobject.h"
16 #include "qemu-common.h"
17 
18 static void qbool_destroy_obj(QObject *obj);
19 
20 static const QType qbool_type = {
21     .code = QTYPE_QBOOL,
22     .destroy = qbool_destroy_obj,
23 };
24 
25 /**
26  * qbool_from_int(): Create a new QBool from an int
27  *
28  * Return strong reference.
29  */
30 QBool *qbool_from_int(int value)
31 {
32     QBool *qb;
33 
34     qb = g_malloc(sizeof(*qb));
35     qb->value = value;
36     QOBJECT_INIT(qb, &qbool_type);
37 
38     return qb;
39 }
40 
41 /**
42  * qbool_get_int(): Get the stored int
43  */
44 int qbool_get_int(const QBool *qb)
45 {
46     return qb->value;
47 }
48 
49 /**
50  * qobject_to_qbool(): Convert a QObject into a QBool
51  */
52 QBool *qobject_to_qbool(const QObject *obj)
53 {
54     if (qobject_type(obj) != QTYPE_QBOOL)
55         return NULL;
56 
57     return container_of(obj, QBool, base);
58 }
59 
60 /**
61  * qbool_destroy_obj(): Free all memory allocated by a
62  * QBool object
63  */
64 static void qbool_destroy_obj(QObject *obj)
65 {
66     assert(obj != NULL);
67     g_free(qobject_to_qbool(obj));
68 }
69