@samitouri / QOSamiQemu / commits / 608562c5a8

qom: add object_new_with_props_from_qdict

This will be used to replace user_creatable_add_type with an impl that shares most code with object_new_with_props, and is not tied to the user creatable interface. Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Daniel P. Berrangé committed Apr 17, 2026 at 16:40 UTC 608562c5a880c3c077bce8154b2d45b3e6987425
2 files changed +88 -6
include/qom/object.h
+19
@@ -700,6 +700,25 @@ Object *object_new_with_propv(const char *typename,
700 va_list vargs,
701 Error **errp);
702
703 +/**
704 + * object_new_with_props_from_qdict:
705 + * @typename: The name of the type of the object to instantiate.
706 + * @parent: the parent object
707 + * @id: The unique ID of the object
708 + * @props: dictionary of property names and values
709 + * @v: visitor to iterate over @props
710 + * @errp: pointer to error object
711 + *
712 + * A variant of object_new_with_props() which accepts the
713 + * properties in a QDict.
714 + */
715 +Object *object_new_with_props_from_qdict(const char *typename,
716 + Object *parent,
717 + const char *id,
718 + const QDict *props,
719 + Visitor *v,
720 + Error **errp);
721 +
722 /**
723 * object_set_props:
724 * @obj: the object instance to set properties on
qom/object.c
+69 -6
@@ -748,11 +748,14 @@ Object *object_new_with_props(const char *typename,
748 }
749
750
751 -Object *object_new_with_propv(const char *typename,
752 - Object *parent,
753 - const char *id,
754 - va_list vargs,
755 - Error **errp)
751 +static Object *
752 +object_new_with_props_helper(const char *typename,
753 + Object *parent,
754 + const char *id,
755 + void *props,
756 + bool (set_props)(Object *obj, void *props,
757 + Error **errp),
758 + Error **errp)
759 {
760 Object *obj;
761 ObjectClass *klass;
@@ -777,7 +780,7 @@ Object *object_new_with_propv(const char *typename,
780 }
781 obj = object_new_with_type(klass->type);
782
780 - if (!object_set_propv(obj, vargs, errp)) {
783 + if (!set_props(obj, props, errp)) {
784 goto error;
785 }
786
@@ -803,6 +806,66 @@ Object *object_new_with_propv(const char *typename,
806 return NULL;
807 }
808
809 +struct ObjectNewVargsData {
810 + va_list vargs;
811 +};
812 +
813 +static bool object_new_with_propv_setter(Object *obj,
814 + void *props,
815 + Error **errp)
816 +{
817 + struct ObjectNewVargsData *data = props;
818 + return object_set_propv(obj, data->vargs, errp);
819 +}
820 +
821 +Object *object_new_with_propv(const char *typename,
822 + Object *parent,
823 + const char *id,
824 + va_list vargs,
825 + Error **errp)
826 +{
827 + Object *obj;
828 + struct ObjectNewVargsData data;
829 +
830 + va_copy(data.vargs, vargs);
831 + obj = object_new_with_props_helper(typename,
832 + parent,
833 + id,
834 + &data,
835 + object_new_with_propv_setter,
836 + errp);
837 + va_end(data.vargs);
838 + return obj;
839 +}
840 +
841 +struct ObjectNewQDictData {
842 + const QDict *props;
843 + Visitor *v;
844 +};
845 +
846 +static bool object_new_with_qdict_setter(Object *obj,
847 + void *props,
848 + Error **errp)
849 +{
850 + struct ObjectNewQDictData *data = props;
851 + return object_set_props_from_qdict(obj, data->props, data->v, errp);
852 +}
853 +
854 +Object *object_new_with_props_from_qdict(const char *typename,
855 + Object *parent,
856 + const char *id,
857 + const QDict *props,
858 + Visitor *v,
859 + Error **errp)
860 +{
861 + struct ObjectNewQDictData data = { props, v };
862 + return object_new_with_props_helper(typename,
863 + parent,
864 + id,
865 + &data,
866 + object_new_with_qdict_setter,
867 + errp);
868 +}
869
870 bool object_set_props(Object *obj,
871 Error **errp,