1 #include "qemu/osdep.h"
2 #include "qom/object_interfaces.h"
3 #include "qemu/module.h"
4 #include "qapi-visit.h"
5 #include "qapi/qmp-output-visitor.h"
6 #include "qapi/opts-visitor.h"
8 void user_creatable_complete(Object
*obj
, Error
**errp
)
11 UserCreatableClass
*ucc
;
13 (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
19 ucc
= USER_CREATABLE_GET_CLASS(uc
);
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
);
38 Object
*user_creatable_add(const QDict
*qdict
,
39 Visitor
*v
, Error
**errp
)
44 Error
*local_err
= NULL
, *end_err
= NULL
;
47 pdict
= qdict_clone_shallow(qdict
);
49 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
54 qdict_del(pdict
, "qom-type");
55 visit_type_str(v
, "qom-type", &type
, &local_err
);
60 qdict_del(pdict
, "id");
61 visit_type_str(v
, "id", &id
, &local_err
);
66 obj
= user_creatable_add_type(type
, id
, pdict
, v
, &local_err
);
72 visit_end_struct(v
, &end_err
);
74 error_propagate(&local_err
, end_err
);
76 user_creatable_del(id
, NULL
);
86 error_propagate(errp
, local_err
);
94 Object
*user_creatable_add_type(const char *type
, const char *id
,
96 Visitor
*v
, Error
**errp
)
101 Error
*local_err
= NULL
;
103 klass
= object_class_by_name(type
);
105 error_setg(errp
, "invalid object type: %s", type
);
109 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
110 error_setg(errp
, "object type '%s' isn't supported by object-add",
115 if (object_class_is_abstract(klass
)) {
116 error_setg(errp
, "object type '%s' is abstract", type
);
120 obj
= object_new(type
);
122 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
123 object_property_set(obj
, v
, e
->key
, &local_err
);
130 object_property_add_child(object_get_objects_root(),
131 id
, obj
, &local_err
);
136 user_creatable_complete(obj
, &local_err
);
138 object_property_del(object_get_objects_root(),
144 error_propagate(errp
, local_err
);
152 Object
*user_creatable_add_opts(QemuOpts
*opts
, Error
**errp
)
158 ov
= opts_visitor_new(opts
);
159 pdict
= qemu_opts_to_qdict(opts
, NULL
);
161 obj
= user_creatable_add(pdict
, opts_get_visitor(ov
), errp
);
162 opts_visitor_cleanup(ov
);
168 int user_creatable_add_opts_foreach(void *opaque
, QemuOpts
*opts
, Error
**errp
)
170 bool (*type_predicate
)(const char *) = opaque
;
174 type
= qemu_opt_get(opts
, "qom-type");
175 if (type
&& type_predicate
&&
176 !type_predicate(type
)) {
180 obj
= user_creatable_add_opts(opts
, errp
);
189 void user_creatable_del(const char *id
, Error
**errp
)
194 container
= object_get_objects_root();
195 obj
= object_resolve_path_component(container
, id
);
197 error_setg(errp
, "object '%s' not found", id
);
201 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
), errp
)) {
202 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
205 object_unparent(obj
);
208 static void register_types(void)
210 static const TypeInfo uc_interface_info
= {
211 .name
= TYPE_USER_CREATABLE
,
212 .parent
= TYPE_INTERFACE
,
213 .class_size
= sizeof(UserCreatableClass
),
216 type_register_static(&uc_interface_info
);
219 type_init(register_types
)