From patchwork Thu Aug 14 05:10:40 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Crosthwaite X-Patchwork-Id: 379809 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id C62F0140085 for ; Thu, 14 Aug 2014 15:11:44 +1000 (EST) Received: from localhost ([::1]:51896 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XHnK9-00008T-L5 for incoming@patchwork.ozlabs.org; Thu, 14 Aug 2014 01:11:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47240) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XHnJo-0008Jc-0P for qemu-devel@nongnu.org; Thu, 14 Aug 2014 01:11:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XHnJi-0005dJ-EP for qemu-devel@nongnu.org; Thu, 14 Aug 2014 01:11:19 -0400 Received: from mail-pd0-f177.google.com ([209.85.192.177]:40366) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XHnJi-0005dF-97 for qemu-devel@nongnu.org; Thu, 14 Aug 2014 01:11:14 -0400 Received: by mail-pd0-f177.google.com with SMTP id p10so907017pdj.8 for ; Wed, 13 Aug 2014 22:11:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=QXnTzD6qS5vEb/ZOh/VlAlgZlnZV8eWfsF65fMKuYrk=; b=NtBvoPxoU9tKqbIkqf45uuX2q2ZoI9RQILOpbwAEwE1tWzygv6qTu+5O5CSMS6NaxF My/CI9fmLEIZspZgCcARnfOvJbZnjAiC3h0HmvGsFiRfF0r81+AxEx424XeSjRdK1g1k f8fYRn74qkwGXjdnSpskh1WBx5IW184jDMB3CwtUO5z13XNcT5wvdMcr47QunIYb+lEv CQNmZKN0eMkvzOxH02+nF0iNZ2KRpNClAllHx/AgbffOo8BYqBPBMk3+mfUS4Yi28IGe HDzquWq4CCcOhBT1Bdv0CZfiXE3eyiDQqd5qW6OFHOKripF7fBTvu8N4SbZghncF3Qdc A1Rw== X-Gm-Message-State: ALoCoQlihT8KYlA/cvaE/yYNGLgcpCffVVD36t/zDgz7Ad3kqiPibUylWR1nXRtyJhhdEu5V3j2z X-Received: by 10.66.186.167 with SMTP id fl7mr2013508pac.14.1407993073004; Wed, 13 Aug 2014 22:11:13 -0700 (PDT) Received: from localhost ([149.199.62.254]) by mx.google.com with ESMTPSA id hi10sm3857519pbd.69.2014.08.13.22.11.11 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 13 Aug 2014 22:11:12 -0700 (PDT) From: Peter Crosthwaite To: qemu-devel@nongnu.org Date: Wed, 13 Aug 2014 22:10:40 -0700 Message-Id: X-Mailer: git-send-email 2.0.1.1.gfbfc394 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.85.192.177 Cc: pbonzini@redhat.com, afaerber@suse.de Subject: [Qemu-devel] [PATCH v3 1/2] qom: object_property_add: Add automatic arrayification X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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 0e8267b..98220f2 100644 --- a/qom/object.c +++ b/qom/object.c @@ -738,6 +738,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) {