From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 14/16] qom: don't pass string table to object_get_enum method
Date: Fri, 8 May 2015 14:07:59 +0200 [thread overview]
Message-ID: <1431086881-17922-15-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1431086881-17922-1-git-send-email-pbonzini@redhat.com>
From: "Daniel P. Berrange" <berrange@redhat.com>
Now that properties can be explicitly registered as an enum
type, there is no need to pass the string table to the
object_get_enum method. The object property registration
already has a pointer to the string table.
In changing this method signature, the hostmem backend object
has to be converted to use the new enum property registration
code, which simplifies it somewhat.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Message-Id: <1430476206-26034-8-git-send-email-berrange@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
backends/hostmem.c | 22 ++++++++--------------
include/qom/object.h | 4 ++--
numa.c | 2 +-
qom/object.c | 32 ++++++++++++++++++++++++--------
tests/check-qom-proplist.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 81 insertions(+), 25 deletions(-)
diff --git a/backends/hostmem.c b/backends/hostmem.c
index f6db33c..7d74be0 100644
--- a/backends/hostmem.c
+++ b/backends/hostmem.c
@@ -113,24 +113,17 @@ host_memory_backend_set_host_nodes(Object *obj, Visitor *v, void *opaque,
#endif
}
-static void
-host_memory_backend_get_policy(Object *obj, Visitor *v, void *opaque,
- const char *name, Error **errp)
+static int
+host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
{
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
- int policy = backend->policy;
-
- visit_type_enum(v, &policy, HostMemPolicy_lookup, NULL, name, errp);
+ return backend->policy;
}
static void
-host_memory_backend_set_policy(Object *obj, Visitor *v, void *opaque,
- const char *name, Error **errp)
+host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
{
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
- int policy;
-
- visit_type_enum(v, &policy, HostMemPolicy_lookup, NULL, name, errp);
backend->policy = policy;
#ifndef CONFIG_NUMA
@@ -252,9 +245,10 @@ static void host_memory_backend_init(Object *obj)
object_property_add(obj, "host-nodes", "int",
host_memory_backend_get_host_nodes,
host_memory_backend_set_host_nodes, NULL, NULL, NULL);
- object_property_add(obj, "policy", "HostMemPolicy",
- host_memory_backend_get_policy,
- host_memory_backend_set_policy, NULL, NULL, NULL);
+ object_property_add_enum(obj, "policy", "HostMemPolicy",
+ HostMemPolicy_lookup,
+ host_memory_backend_get_policy,
+ host_memory_backend_set_policy, NULL);
}
MemoryRegion *
diff --git a/include/qom/object.h b/include/qom/object.h
index f6a2a9d..fc347b9 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -1012,7 +1012,7 @@ int64_t object_property_get_int(Object *obj, const char *name,
* object_property_get_enum:
* @obj: the object
* @name: the name of the property
- * @strings: strings corresponding to enums
+ * @typename: the name of the enum data type
* @errp: returns an error if this function fails
*
* Returns: the value of the property, converted to an integer, or
@@ -1020,7 +1020,7 @@ int64_t object_property_get_int(Object *obj, const char *name,
* an enum).
*/
int object_property_get_enum(Object *obj, const char *name,
- const char * const strings[], Error **errp);
+ const char *typename, Error **errp);
/**
* object_property_get_uint16List:
diff --git a/numa.c b/numa.c
index c975fb2..a64279a 100644
--- a/numa.c
+++ b/numa.c
@@ -457,7 +457,7 @@ static int query_memdev(Object *obj, void *opaque)
m->value->policy = object_property_get_enum(obj,
"policy",
- HostMemPolicy_lookup,
+ "HostMemPolicy",
&err);
if (err) {
goto error;
diff --git a/qom/object.c b/qom/object.c
index ba0e4b8..6d2a2a9 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1026,13 +1026,35 @@ int64_t object_property_get_int(Object *obj, const char *name,
return retval;
}
+typedef struct EnumProperty {
+ const char * const *strings;
+ int (*get)(Object *, Error **);
+ void (*set)(Object *, int, Error **);
+} EnumProperty;
+
+
int object_property_get_enum(Object *obj, const char *name,
- const char * const strings[], Error **errp)
+ const char *typename, Error **errp)
{
StringOutputVisitor *sov;
StringInputVisitor *siv;
char *str;
int ret;
+ ObjectProperty *prop = object_property_find(obj, name, errp);
+ EnumProperty *enumprop;
+
+ if (prop == NULL) {
+ return 0;
+ }
+
+ if (!g_str_equal(prop->type, typename)) {
+ error_setg(errp, "Property %s on %s is not '%s' enum type",
+ name, object_class_get_name(
+ object_get_class(obj)), typename);
+ return 0;
+ }
+
+ enumprop = prop->opaque;
sov = string_output_visitor_new(false);
object_property_get(obj, string_output_get_visitor(sov), name, errp);
@@ -1040,7 +1062,7 @@ int object_property_get_enum(Object *obj, const char *name,
siv = string_input_visitor_new(str);
string_output_visitor_cleanup(sov);
visit_type_enum(string_input_get_visitor(siv),
- &ret, strings, NULL, name, errp);
+ &ret, enumprop->strings, NULL, name, errp);
g_free(str);
string_input_visitor_cleanup(siv);
@@ -1609,12 +1631,6 @@ void object_property_add_bool(Object *obj, const char *name,
}
}
-typedef struct EnumProperty {
- const char * const *strings;
- int (*get)(Object *, Error **);
- void (*set)(Object *, int, Error **);
-} EnumProperty;
-
static void property_get_enum(Object *obj, Visitor *v, void *opaque,
const char *name, Error **errp)
{
diff --git a/tests/check-qom-proplist.c b/tests/check-qom-proplist.c
index de142e3..d5cd38b 100644
--- a/tests/check-qom-proplist.c
+++ b/tests/check-qom-proplist.c
@@ -249,6 +249,51 @@ static void test_dummy_badenum(void)
}
+
+static void test_dummy_getenum(void)
+{
+ Error *err = NULL;
+ int val;
+ Object *parent = container_get(object_get_root(),
+ "/objects");
+ DummyObject *dobj = DUMMY_OBJECT(
+ object_new_propv(TYPE_DUMMY,
+ parent,
+ "dummy0",
+ &err,
+ "av", "platypus",
+ NULL));
+
+ g_assert(dobj != NULL);
+ g_assert(err == NULL);
+ g_assert(dobj->av == DUMMY_PLATYPUS);
+
+ val = object_property_get_enum(OBJECT(dobj),
+ "av",
+ "DummyAnimal",
+ &err);
+ g_assert(err == NULL);
+ g_assert(val == DUMMY_PLATYPUS);
+
+ /* A bad enum type name */
+ val = object_property_get_enum(OBJECT(dobj),
+ "av",
+ "BadAnimal",
+ &err);
+ g_assert(err != NULL);
+ error_free(err);
+ err = NULL;
+
+ /* A non-enum property name */
+ val = object_property_get_enum(OBJECT(dobj),
+ "iv",
+ "DummyAnimal",
+ &err);
+ g_assert(err != NULL);
+ error_free(err);
+}
+
+
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
@@ -259,6 +304,7 @@ int main(int argc, char **argv)
g_test_add_func("/qom/proplist/createlist", test_dummy_createlist);
g_test_add_func("/qom/proplist/createv", test_dummy_createv);
g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
+ g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
return g_test_run();
}
--
2.3.5
next prev parent reply other threads:[~2015-05-08 12:08 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-08 12:07 [Qemu-devel] [PULL 00/16] KVM, QOM, NBD, build fixes for 2015-05-08 Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 01/16] kvm: Silence warning from valgrind Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 02/16] apic_common: improve readability of apic_reset_common Paolo Bonzini
2015-05-08 12:30 ` Andreas Färber
2015-05-08 12:07 ` [Qemu-devel] [PULL 03/16] mtree: tag & indent a bit better Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 04/16] mtree: also print disabled regions Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 05/16] kvm: add support for memory transaction attributes Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 06/16] exec: move rcu_read_lock/unlock to address_space_translate callers Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 07/16] configure: require __thread support Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 08/16] qom: fix typename of 'policy' enum property in hostmem obj Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 09/16] qom: document user creatable object types in help text Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 10/16] qom: create objects in two phases Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 11/16] qom: add object_new_propv / object_new_proplist constructors Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 12/16] qom: make enum string tables const-correct Paolo Bonzini
2015-05-08 12:07 ` [Qemu-devel] [PULL 13/16] qom: add a object_property_add_enum helper method Paolo Bonzini
2015-05-08 12:07 ` Paolo Bonzini [this message]
2015-05-08 12:08 ` [Qemu-devel] [PULL 16/16] qemu-nbd: only send a limited number of errno codes on the wire Paolo Bonzini
2015-05-08 12:29 ` [Qemu-devel] [PULL 00/16] KVM, QOM, NBD, build fixes for 2015-05-08 Andreas Färber
2015-05-08 12:34 ` Daniel P. Berrange
2015-05-08 12:39 ` Andreas Färber
2015-05-08 12:42 ` Daniel P. Berrange
2015-05-12 16:57 ` Daniel P. Berrange
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1431086881-17922-15-git-send-email-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).