[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH qom v4 1/2] qom: object_property_add: Add automatic
From: |
Peter Crosthwaite |
Subject: |
[Qemu-devel] [PATCH qom v4 1/2] qom: object_property_add: Add automatic arrayification |
Date: |
Tue, 19 Aug 2014 23:55:52 -0700 |
If "[*]" is given as the last part of a QOM property name, treat that
as an array property. The added property is given the first available
name, replacing the * with a decimal number counting from 0.
First add with name "foo[*]" will be "foo[0]". Second "foo[1]" and so
on.
Callers may inspect the OjbectProperty * return value to see what
number the added property was given.
Signed-off-by: Peter Crosthwaite <address@hidden>
---
changed since v2: (AF review):
Add comment about return inspection for enumeration.
Fixed whitespace
Avoid un-needed local_err
changed since v1 (Paolo review):
Cache strlen result in variable
Use memcmp instead of strncmp
Suggest by Paolo and first pass discussion on list about the feature
here:
https://lists.nongnu.org/archive/html/qemu-devel/2014-06/msg03794.html
qom/object.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/qom/object.c b/qom/object.c
index 1b00831..2c35908 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -728,6 +728,27 @@ object_property_add(Object *obj, const char *name, const
char *type,
void *opaque, Error **errp)
{
ObjectProperty *prop;
+ size_t name_len = strlen(name);
+
+ if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
+ int i;
+ ObjectProperty *ret;
+ char *name_no_array = g_strdup(name);
+
+ name_no_array[name_len - 3] = '\0';
+ for (i = 0; ; ++i) {
+ char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
+
+ ret = object_property_add(obj, full_name, type, get, set,
+ release, opaque, NULL);
+ g_free(full_name);
+ if (ret) {
+ break;
+ }
+ }
+ g_free(name_no_array);
+ return ret;
+ }
QTAILQ_FOREACH(prop, &obj->properties, node) {
if (strcmp(prop->name, name) == 0) {
--
2.0.1.1.gfbfc394