1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "qemu/osdep.h"
16#include "qapi/qmp/qnum.h"
17
18
19
20
21
22
23QNum *qnum_from_int(int64_t value)
24{
25 QNum *qn = g_new(QNum, 1);
26
27 qobject_init(QOBJECT(qn), QTYPE_QNUM);
28 qn->kind = QNUM_I64;
29 qn->u.i64 = value;
30
31 return qn;
32}
33
34
35
36
37
38
39QNum *qnum_from_uint(uint64_t value)
40{
41 QNum *qn = g_new(QNum, 1);
42
43 qobject_init(QOBJECT(qn), QTYPE_QNUM);
44 qn->kind = QNUM_U64;
45 qn->u.u64 = value;
46
47 return qn;
48}
49
50
51
52
53
54
55QNum *qnum_from_double(double value)
56{
57 QNum *qn = g_new(QNum, 1);
58
59 qobject_init(QOBJECT(qn), QTYPE_QNUM);
60 qn->kind = QNUM_DOUBLE;
61 qn->u.dbl = value;
62
63 return qn;
64}
65
66
67
68
69
70
71bool qnum_get_try_int(const QNum *qn, int64_t *val)
72{
73 switch (qn->kind) {
74 case QNUM_I64:
75 *val = qn->u.i64;
76 return true;
77 case QNUM_U64:
78 if (qn->u.u64 > INT64_MAX) {
79 return false;
80 }
81 *val = qn->u.u64;
82 return true;
83 case QNUM_DOUBLE:
84 return false;
85 }
86
87 assert(0);
88 return false;
89}
90
91
92
93
94
95
96int64_t qnum_get_int(const QNum *qn)
97{
98 int64_t val;
99 bool success = qnum_get_try_int(qn, &val);
100 assert(success);
101 return val;
102}
103
104
105
106
107
108
109bool qnum_get_try_uint(const QNum *qn, uint64_t *val)
110{
111 switch (qn->kind) {
112 case QNUM_I64:
113 if (qn->u.i64 < 0) {
114 return false;
115 }
116 *val = qn->u.i64;
117 return true;
118 case QNUM_U64:
119 *val = qn->u.u64;
120 return true;
121 case QNUM_DOUBLE:
122 return false;
123 }
124
125 assert(0);
126 return false;
127}
128
129
130
131
132
133
134uint64_t qnum_get_uint(const QNum *qn)
135{
136 uint64_t val;
137 bool success = qnum_get_try_uint(qn, &val);
138 assert(success);
139 return val;
140}
141
142
143
144
145
146
147double qnum_get_double(QNum *qn)
148{
149 switch (qn->kind) {
150 case QNUM_I64:
151 return qn->u.i64;
152 case QNUM_U64:
153 return qn->u.u64;
154 case QNUM_DOUBLE:
155 return qn->u.dbl;
156 }
157
158 assert(0);
159 return 0.0;
160}
161
162char *qnum_to_string(QNum *qn)
163{
164 char *buffer;
165 int len;
166
167 switch (qn->kind) {
168 case QNUM_I64:
169 return g_strdup_printf("%" PRId64, qn->u.i64);
170 case QNUM_U64:
171 return g_strdup_printf("%" PRIu64, qn->u.u64);
172 case QNUM_DOUBLE:
173
174
175
176
177
178
179
180
181
182 buffer = g_strdup_printf("%f" , qn->u.dbl);
183 len = strlen(buffer);
184 while (len > 0 && buffer[len - 1] == '0') {
185 len--;
186 }
187
188 if (len && buffer[len - 1] == '.') {
189 buffer[len - 1] = 0;
190 } else {
191 buffer[len] = 0;
192 }
193
194 return buffer;
195 }
196
197 assert(0);
198 return NULL;
199}
200
201
202
203
204
205
206
207
208
209
210bool qnum_is_equal(const QObject *x, const QObject *y)
211{
212 QNum *num_x = qobject_to(QNum, x);
213 QNum *num_y = qobject_to(QNum, y);
214
215 switch (num_x->kind) {
216 case QNUM_I64:
217 switch (num_y->kind) {
218 case QNUM_I64:
219
220 return num_x->u.i64 == num_y->u.i64;
221 case QNUM_U64:
222
223
224 return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
225 case QNUM_DOUBLE:
226 return false;
227 }
228 abort();
229 case QNUM_U64:
230 switch (num_y->kind) {
231 case QNUM_I64:
232 return qnum_is_equal(y, x);
233 case QNUM_U64:
234
235 return num_x->u.u64 == num_y->u.u64;
236 case QNUM_DOUBLE:
237 return false;
238 }
239 abort();
240 case QNUM_DOUBLE:
241 switch (num_y->kind) {
242 case QNUM_I64:
243 case QNUM_U64:
244 return false;
245 case QNUM_DOUBLE:
246
247 return num_x->u.dbl == num_y->u.dbl;
248 }
249 abort();
250 }
251
252 abort();
253}
254
255
256
257
258
259void qnum_destroy_obj(QObject *obj)
260{
261 assert(obj != NULL);
262 g_free(qobject_to(QNum, obj));
263}
264