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/qmp-output-visitor.h"
7 #include "qapi/opts-visitor.h"
9 void user_creatable_complete(Object
*obj
, Error
**errp
)
12 UserCreatableClass
*ucc
;
14 (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
20 ucc
= USER_CREATABLE_GET_CLASS(uc
);
22 ucc
->complete(uc
, errp
);
26 bool user_creatable_can_be_deleted(UserCreatable
*uc
, Error
**errp
)
29 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
31 if (ucc
->can_be_deleted
) {
32 return ucc
->can_be_deleted(uc
, errp
);
39 Object
*user_creatable_add(const QDict
*qdict
,
40 Visitor
*v
, Error
**errp
)
45 Error
*local_err
= NULL
, *end_err
= NULL
;
48 pdict
= qdict_clone_shallow(qdict
);
50 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
55 qdict_del(pdict
, "qom-type");
56 visit_type_str(v
, "qom-type", &type
, &local_err
);
61 qdict_del(pdict
, "id");
62 visit_type_str(v
, "id", &id
, &local_err
);
67 obj
= user_creatable_add_type(type
, id
, pdict
, v
, &local_err
);
73 visit_end_struct(v
, &end_err
);
75 error_propagate(&local_err
, end_err
);
77 user_creatable_del(id
, NULL
);
87 error_propagate(errp
, local_err
);
95 Object
*user_creatable_add_type(const char *type
, const char *id
,
97 Visitor
*v
, Error
**errp
)
102 Error
*local_err
= NULL
;
104 klass
= object_class_by_name(type
);
106 error_setg(errp
, "invalid object type: %s", type
);
110 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
111 error_setg(errp
, "object type '%s' isn't supported by object-add",
116 if (object_class_is_abstract(klass
)) {
117 error_setg(errp
, "object type '%s' is abstract", type
);
121 obj
= object_new(type
);
123 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
124 object_property_set(obj
, v
, e
->key
, &local_err
);
131 object_property_add_child(object_get_objects_root(),
132 id
, obj
, &local_err
);
137 user_creatable_complete(obj
, &local_err
);
139 object_property_del(object_get_objects_root(),
145 error_propagate(errp
, local_err
);
153 Object
*user_creatable_add_opts(QemuOpts
*opts
, Error
**errp
)
159 ov
= opts_visitor_new(opts
);
160 pdict
= qemu_opts_to_qdict(opts
, NULL
);
162 obj
= user_creatable_add(pdict
, opts_get_visitor(ov
), errp
);
163 opts_visitor_cleanup(ov
);
169 int user_creatable_add_opts_foreach(void *opaque
, QemuOpts
*opts
, Error
**errp
)
171 bool (*type_predicate
)(const char *) = opaque
;
175 type
= qemu_opt_get(opts
, "qom-type");
176 if (type
&& type_predicate
&&
177 !type_predicate(type
)) {
181 obj
= user_creatable_add_opts(opts
, errp
);
190 void user_creatable_del(const char *id
, Error
**errp
)
195 container
= object_get_objects_root();
196 obj
= object_resolve_path_component(container
, id
);
198 error_setg(errp
, "object '%s' not found", id
);
202 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
), errp
)) {
203 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
206 object_unparent(obj
);
209 static void register_types(void)
211 static const TypeInfo uc_interface_info
= {
212 .name
= TYPE_USER_CREATABLE
,
213 .parent
= TYPE_INTERFACE
,
214 .class_size
= sizeof(UserCreatableClass
),
217 type_register_static(&uc_interface_info
);
220 type_init(register_types
)