From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36780) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yj8rv-00055E-O5 for qemu-devel@nongnu.org; Fri, 17 Apr 2015 12:11:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yj8rs-0003qD-GA for qemu-devel@nongnu.org; Fri, 17 Apr 2015 12:11:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52965) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yj8rs-0003q9-8y for qemu-devel@nongnu.org; Fri, 17 Apr 2015 12:11:48 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t3HGBlVh031231 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 17 Apr 2015 12:11:47 -0400 Message-ID: <553130C2.4010208@redhat.com> Date: Fri, 17 Apr 2015 10:11:46 -0600 From: Eric Blake MIME-Version: 1.0 References: <1429280557-8887-1-git-send-email-berrange@redhat.com> <1429280557-8887-5-git-send-email-berrange@redhat.com> In-Reply-To: <1429280557-8887-5-git-send-email-berrange@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="XSAFnscWt0nxv6XNhbUH2a9ba6v7Qb6Gx" Subject: Re: [Qemu-devel] [PATCH v1 RFC 04/34] qom: add object_new_propv / object_new_proplist constructors List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Daniel P. Berrange" , qemu-devel@nongnu.org Cc: Paolo Bonzini , Gerd Hoffmann , Stefan Hajnoczi This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --XSAFnscWt0nxv6XNhbUH2a9ba6v7Qb6Gx Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 04/17/2015 08:22 AM, Daniel P. Berrange wrote: > It is reasonably common to want to create an object, set a > number of properties, register it in the hierarchy and then > mark it as complete (if a user creatable type). This requires > quite alot of error prone, verbose, boilerplate code to achieve. s/alot/a lot/ >=20 > The object_new_propv / object_new_proplist constructors will > simplify this task by performing all required steps in one go, > accepting the property names/values as variadic args. >=20 > Usage would be: >=20 > Error *err =3D NULL; > Object *obj; > obj =3D object_new_propv(TYPE_MEMORY_BACKEND_FILE, > "hostmem0", > &err, > "share", "yes", > "mem-path", "/dev/shm/somefile", > "prealloc", "yes", > "size": "1048576", s/:/,/ > NULL); >=20 > Note all property values are passed in string form and will > be parsed into their required data types. >=20 > Signed-off-by: Daniel P. Berrange > --- > include/qom/object.h | 58 ++++++++++++++++++++++++++++++++++++++++++++= +++ > qom/object.c | 64 ++++++++++++++++++++++++++++++++++++++++++++= ++++++++ > tests/Makefile | 2 +- > 3 files changed, 123 insertions(+), 1 deletion(-) >=20 > diff --git a/include/qom/object.h b/include/qom/object.h > index d2d7748..223b577 100644 > --- a/include/qom/object.h > + * > + * obj =3D object_new_propv(TYPE_MEMORY_BACKEND_FILE, > + * "hostmem0", > + * &err, > + * "share", "yes", > + * "mem-path", "/dev/shm/somefile", > + * "prealloc", "yes", > + * "size": "1048576", and again > + * NULL); > + * > + * if (!obj) { > + * g_printerr("Cannot create memory backend: %s\n", > + * error_get_pretty(err)); > + * } > + * > + * Returns: The newly allocated, instantiated & initialized object. > + */ > +Object *object_new_propv(const char *typename, > + const char *id, > + Error **errp, > + ...); You probably want to use the gcc __attribute__((__sentinel__)), so that the compiler can ensure that the caller NULL-terminates their list. > +Object *object_new_proplist(const char *typename, > + const char *id, > + Error **errp, > + va_list vargs) > +{ > + Object *obj; > + const char *propname; > + > + obj =3D object_new(typename); > + > + if (object_class_is_abstract(object_get_class(obj))) { > + error_setg(errp, "object type '%s' is abstract", typename); > + goto error; > + } > + > + propname =3D va_arg(vargs, char *); > + while (propname !=3D NULL) { > + const char *value =3D va_arg(vargs, char *); > + > + object_property_parse(obj, value, propname, errp); Is it worth a sanity check of assert(value) prior to calling object_property_parse(), as I have the suspicion that it doesn't handle NULL very well? --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --XSAFnscWt0nxv6XNhbUH2a9ba6v7Qb6Gx Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJVMTDCAAoJEKeha0olJ0Nq5k8H/jF1cX3vmonPlT3OW8kvt+fX z1mk8F9QqjQOL+a4ZkQnjrlOhu0xpdJYawtpD5LKe9gANJ72aTiz0pSH1X1DK5Lt pgEkLMA0EMJ6Mf0r6xpeDGG3V9e1D3GZIbQSztgLoATxs6eqXSDWmpRRNYwyjAIf X20ToBH7fma31kb/cTpZqXZyS5Gg+WDiJsJkPkgzfMmUxeBYWHfvhEZYGjlDhTva NnyW/7NgaA2+1oELRueCM9rDFvr73zuBbNxT1u0VcYY7BNsG+rh2NtkZFPWqrrPM 4ZToI+xIJJaQK5HoTwPV+RkbkvTrogTVWDgBYXsDVakkKMGDCd43nbvwGx8vuDw= =9yBn -----END PGP SIGNATURE----- --XSAFnscWt0nxv6XNhbUH2a9ba6v7Qb6Gx--