s390x/pci: merge msix init functions
[qemu.git] / qom / object_interfaces.c
blob03a95c327627a4bb56454259ad2a74341d020201
1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qom/object_interfaces.h"
4 #include "qemu/module.h"
5 #include "qapi-visit.h"
6 #include "qapi/opts-visitor.h"
8 void user_creatable_complete(Object *obj, Error **errp)
11 UserCreatableClass *ucc;
12 UserCreatable *uc =
13 (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
15 if (!uc) {
16 return;
19 ucc = USER_CREATABLE_GET_CLASS(uc);
20 if (ucc->complete) {
21 ucc->complete(uc, errp);
25 bool user_creatable_can_be_deleted(UserCreatable *uc, Error **errp)
28 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
30 if (ucc->can_be_deleted) {
31 return ucc->can_be_deleted(uc, errp);
32 } else {
33 return true;
37 Object *user_creatable_add_type(const char *type, const char *id,
38 const QDict *qdict,
39 Visitor *v, Error **errp)
41 Object *obj;
42 ObjectClass *klass;
43 const QDictEntry *e;
44 Error *local_err = NULL;
46 klass = object_class_by_name(type);
47 if (!klass) {
48 error_setg(errp, "invalid object type: %s", type);
49 return NULL;
52 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
53 error_setg(errp, "object type '%s' isn't supported by object-add",
54 type);
55 return NULL;
58 if (object_class_is_abstract(klass)) {
59 error_setg(errp, "object type '%s' is abstract", type);
60 return NULL;
63 assert(qdict);
64 obj = object_new(type);
65 if (object_property_find(obj, "id", NULL)) {
66 object_property_set_str(obj, id, "id", &local_err);
67 if (local_err) {
68 goto out;
71 visit_start_struct(v, NULL, NULL, 0, &local_err);
72 if (local_err) {
73 goto out;
75 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
76 object_property_set(obj, v, e->key, &local_err);
77 if (local_err) {
78 break;
81 if (!local_err) {
82 visit_check_struct(v, &local_err);
84 visit_end_struct(v, NULL);
85 if (local_err) {
86 goto out;
89 object_property_add_child(object_get_objects_root(),
90 id, obj, &local_err);
91 if (local_err) {
92 goto out;
95 user_creatable_complete(obj, &local_err);
96 if (local_err) {
97 object_property_del(object_get_objects_root(),
98 id, &error_abort);
99 goto out;
101 out:
102 if (local_err) {
103 error_propagate(errp, local_err);
104 object_unref(obj);
105 return NULL;
107 return obj;
111 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
113 Visitor *v;
114 QDict *pdict;
115 Object *obj;
116 const char *id = qemu_opts_id(opts);
117 const char *type = qemu_opt_get(opts, "qom-type");
119 if (!type) {
120 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
121 return NULL;
123 if (!id) {
124 error_setg(errp, QERR_MISSING_PARAMETER, "id");
125 return NULL;
128 pdict = qemu_opts_to_qdict(opts, NULL);
129 qdict_del(pdict, "qom-type");
130 qdict_del(pdict, "id");
132 v = opts_visitor_new(opts);
133 obj = user_creatable_add_type(type, id, pdict, v, errp);
134 visit_free(v);
136 QDECREF(pdict);
137 return obj;
141 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
143 bool (*type_predicate)(const char *) = opaque;
144 Object *obj = NULL;
145 Error *err = NULL;
146 const char *type;
148 type = qemu_opt_get(opts, "qom-type");
149 if (type && type_predicate &&
150 !type_predicate(type)) {
151 return 0;
154 obj = user_creatable_add_opts(opts, &err);
155 if (!obj) {
156 error_report_err(err);
157 return -1;
159 object_unref(obj);
160 return 0;
164 void user_creatable_del(const char *id, Error **errp)
166 Object *container;
167 Object *obj;
169 container = object_get_objects_root();
170 obj = object_resolve_path_component(container, id);
171 if (!obj) {
172 error_setg(errp, "object '%s' not found", id);
173 return;
176 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
177 error_setg(errp, "object '%s' is in use, can not be deleted", id);
178 return;
180 object_unparent(obj);
183 static void register_types(void)
185 static const TypeInfo uc_interface_info = {
186 .name = TYPE_USER_CREATABLE,
187 .parent = TYPE_INTERFACE,
188 .class_size = sizeof(UserCreatableClass),
191 type_register_static(&uc_interface_info);
194 type_init(register_types)