Patchwork [3/3] Introduce QNumber data type

login
register
mail settings
Submitter Luiz Capitulino
Date Aug. 6, 2009, 1:52 p.m.
Message ID <1249566736-5020-4-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/30850/
State Superseded
Headers show

Comments

Luiz Capitulino - Aug. 6, 2009, 1:52 p.m.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 Makefile  |    2 +-
 qnumber.c |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qnumber.h |   20 +++++++++++
 qobject.h |    1 +
 4 files changed, 130 insertions(+), 1 deletions(-)
 create mode 100644 qnumber.c
 create mode 100644 qnumber.h
Avi Kivity - Aug. 6, 2009, 2:04 p.m.
On 08/06/2009 04:52 PM, Luiz Capitulino wrote:
> +
> +typedef struct QNumber {
> +    QObject base;
> +    union {
> +        int n_int;
> +        int64_t n_int64;
> +    } number;
> +} QNumber;
>    

Why not have an int64_t exclusively?

Also, we need floating point support.  So probably need to split this 
into QInt and QFloat.
François Revol - Aug. 6, 2009, 2:05 p.m.
> On 08/06/2009 04:52 PM, Luiz Capitulino wrote:
> > +
> > +typedef struct QNumber {
> > +    QObject base;
> > +    union {
> > +        int n_int;
> > +        int64_t n_int64;
> > +    } number;
> > +} QNumber;
> >    
> 
> Why not have an int64_t exclusively?
> 
> Also, we need floating point support.  So probably need to split this 
> into QInt and QFloat.

Aw you sure all this won't clash with some Qt class if some fold tries 
to mix both?
Now, I use the BeAPI so I don't really care, but still.

And I still wonder why not just use a real OO language instead of 
faking.

François.
Luiz Capitulino - Aug. 6, 2009, 2:10 p.m.
On Thu, 06 Aug 2009 17:04:50 +0300
Avi Kivity <avi@redhat.com> wrote:

> On 08/06/2009 04:52 PM, Luiz Capitulino wrote:
> > +
> > +typedef struct QNumber {
> > +    QObject base;
> > +    union {
> > +        int n_int;
> > +        int64_t n_int64;
> > +    } number;
> > +} QNumber;
> >    
> 
> Why not have an int64_t exclusively?
> 
> Also, we need floating point support.  So probably need to split this 
> into QInt and QFloat.

 Right, we can have QInt with int64_t.

 But the monitor doesn't use floats, so QFloat doesn't seem
needed, at least for now.
Avi Kivity - Aug. 6, 2009, 2:48 p.m.
On 08/06/2009 05:05 PM, François Revol wrote:
>> Also, we need floating point support.  So probably need to split this
>> into QInt and QFloat.
>>      
>
> Aw you sure all this won't clash with some Qt class if some fold tries
> to mix both?
>    

We can address that when it happens.

> And I still wonder why not just use a real OO language instead of
> faking.
>    

I'm all for it.
Luiz Capitulino - Aug. 6, 2009, 9:21 p.m.
On Thu, 06 Aug 2009 17:04:50 +0300
Avi Kivity <avi@redhat.com> wrote:

> On 08/06/2009 04:52 PM, Luiz Capitulino wrote:
> > +
> > +typedef struct QNumber {
> > +    QObject base;
> > +    union {
> > +        int n_int;
> > +        int64_t n_int64;
> > +    } number;
> > +} QNumber;
> >    
> 
> Why not have an int64_t exclusively?

 Something I was wondering: why does get_expr() use int64_t
instead of uint64_t?

 As far as I can understand it uses strtoull() and strtoul() to
convert from the user's string and command handlers use 64-bits
values only for addresses and (signed) 32-bits for anything else.

 We are doing some int64_t to uint64_t conversions today...

Patch

diff --git a/Makefile b/Makefile
index 9e3faec..c4cbbaf 100644
--- a/Makefile
+++ b/Makefile
@@ -104,7 +104,7 @@  obj-y += buffered_file.o migration.o migration-tcp.o net.o qemu-sockets.o
 obj-y += qemu-char.o aio.o net-checksum.o savevm.o
 obj-y += msmouse.o ps2.o
 obj-y += qdev.o qdev-properties.o ssi.o
-obj-y += qdict.o qstring.o
+obj-y += qdict.o qstring.o qnumber.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
 
diff --git a/qnumber.c b/qnumber.c
new file mode 100644
index 0000000..cd30150
--- /dev/null
+++ b/qnumber.c
@@ -0,0 +1,108 @@ 
+/*
+ * QNumber data type.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <lcapitulino@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+#include "qobject.h"
+#include "qnumber.h"
+#include "qemu-common.h"
+
+static QType qnumber_type;
+
+
+/**
+ * qnumber_alloc(): Allocate a new QNumber
+ */
+static QNumber *qnumber_alloc(void)
+{
+    QNumber *qnum;
+
+    qnum = qemu_mallocz(sizeof(*qnum));
+    qnum->base.type = &qnumber_type;
+
+    return qnum;
+}
+
+/**
+ * qnumber_from_int(): Create a new QNumber from an int
+ *
+ * return new QNumber.
+ */
+QNumber *qnumber_from_int(int value)
+{
+    QNumber *qnum;
+
+    qnum = qnumber_alloc();
+    qnum->number.n_int = value;
+
+    return qnum;
+}
+
+/**
+ * qnumber_from_int64(): Create a new QNumber from an int64_t
+ *
+ * return new QNumber.
+ */
+QNumber *qnumber_from_int64(int64_t value)
+{
+    QNumber *qnum;
+
+    qnum = qnumber_alloc();
+    qnum->number.n_int64 = value;
+
+    return qnum;
+}
+
+/**
+ * qnumber_destroy(): Free all memory allocated by a QNumber
+ * object
+ */
+void qnumber_destroy(QNumber *qnum)
+{
+    qemu_free(qnum);
+}
+
+/**
+ * qnumber_to_int(): Export QNumber to int type
+ */
+int qnumber_to_int(const QNumber *qnum)
+{
+    return qnum->number.n_int;
+}
+
+/**
+ * qnumber_destroy_obj(): Destroy a QNumber object
+ */
+static void qnumber_destroy_obj(QObject *obj)
+{
+    QNumber *qnum = container_of(obj, QNumber, base);
+    qnumber_destroy(qnum);
+}
+
+/**
+ * qnumber_clone_obj(): Clone a QNumber object
+ *
+ * return a copy of the provided QNumber object.
+ */
+static QObject *qnumber_clone_obj(const QObject *obj)
+{
+    QNumber *old, *new;
+
+    new = qemu_malloc(sizeof(*new));
+    old = container_of(obj, QNumber, base);
+    memcpy(new, old, sizeof(*new));
+
+    return &new->base;
+}
+
+static QType qnumber_type = {
+    .code = QTYPE_QNUMBER,
+    .clone = qnumber_clone_obj,
+    .destroy = qnumber_destroy_obj,
+};
diff --git a/qnumber.h b/qnumber.h
new file mode 100644
index 0000000..918637d
--- /dev/null
+++ b/qnumber.h
@@ -0,0 +1,20 @@ 
+#ifndef QNUMBER_H
+#define QNUMBER_H
+
+#include "qobject.h"
+#include <stdint.h>
+
+typedef struct QNumber {
+    QObject base;
+    union {
+        int n_int;
+        int64_t n_int64;
+    } number;
+} QNumber;
+
+QNumber *qnumber_from_int(int value);
+QNumber *qnumber_from_int64(int64_t value);
+void qnumber_destroy(QNumber *qnum);
+int qnumber_to_int(const QNumber *qnum);
+
+#endif /* QNUMBER_H */
diff --git a/qobject.h b/qobject.h
index d3378b1..20b9a99 100644
--- a/qobject.h
+++ b/qobject.h
@@ -17,6 +17,7 @@ 
 typedef enum {
     QTYPE_NONE,
     QTYPE_QSTRING,
+    QTYPE_QNUMBER,
 } qtype_t;
 
 struct QObject;