diff mbox

[v4,3/5] qobject: introduce qobject_get_str()

Message ID 1390488396-16538-4-git-send-email-akong@redhat.com
State New
Headers show

Commit Message

Amos Kong Jan. 23, 2014, 2:46 p.m. UTC
Signed-off-by: Amos Kong <akong@redhat.com>
---
 include/qapi/qmp/qstring.h |  1 +
 qobject/qstring.c          | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

Comments

Eric Blake Feb. 4, 2014, 12:20 a.m. UTC | #1
On 01/23/2014 07:46 AM, Amos Kong wrote:
> Signed-off-by: Amos Kong <akong@redhat.com>
> ---
>  include/qapi/qmp/qstring.h |  1 +
>  qobject/qstring.c          | 19 +++++++++++++++++++
>  2 files changed, 20 insertions(+)

Is there anything you can add to the testsuite to validate that this
works as expected?  I'm not quite sure if check-qdict.c is the best fit,
but it's the sort of test I have in mind.

> +++ b/qobject/qstring.c
> @@ -135,6 +135,25 @@ const char *qstring_get_str(const QString *qstring)
>  }
>  
>  /**
> + * qobject_to_str(): Convert a QObject to QString and return
> + * a pointer to the stored string
> + */
> +const char *qobject_get_str(const QObject *data)
> +{
> +    QString *qstr;
> +
> +    if (!data) {
> +        return NULL;
> +    }
> +    qstr = qobject_to_qstring(data);
> +    if (qstr) {
> +        return qstring_get_str(qstr);
> +    } else {
> +        return NULL;
> +    }

It looks like this is shorthand for getting at the string value of a
QTYPE_QSTRING object.  I'm not sure how you were planning on using it,
or if it saves much code.  This is where you could use your commit
message to explain why this shorthand will be useful.
diff mbox

Patch

diff --git a/include/qapi/qmp/qstring.h b/include/qapi/qmp/qstring.h
index 1bc3666..56b17cb 100644
--- a/include/qapi/qmp/qstring.h
+++ b/include/qapi/qmp/qstring.h
@@ -28,6 +28,7 @@  QString *qstring_from_str(const char *str);
 QString *qstring_from_substr(const char *str, int start, int end);
 size_t qstring_get_length(const QString *qstring);
 const char *qstring_get_str(const QString *qstring);
+const char *qobject_get_str(const QObject *obj);
 void qstring_append_int(QString *qstring, int64_t value);
 void qstring_append(QString *qstring, const char *str);
 void qstring_append_chr(QString *qstring, int c);
diff --git a/qobject/qstring.c b/qobject/qstring.c
index 607b7a1..c470a86 100644
--- a/qobject/qstring.c
+++ b/qobject/qstring.c
@@ -135,6 +135,25 @@  const char *qstring_get_str(const QString *qstring)
 }
 
 /**
+ * qobject_to_str(): Convert a QObject to QString and return
+ * a pointer to the stored string
+ */
+const char *qobject_get_str(const QObject *data)
+{
+    QString *qstr;
+
+    if (!data) {
+        return NULL;
+    }
+    qstr = qobject_to_qstring(data);
+    if (qstr) {
+        return qstring_get_str(qstr);
+    } else {
+        return NULL;
+    }
+}
+
+/**
  * qstring_destroy_obj(): Free all memory allocated by a QString
  * object
  */