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
;
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
);
37 Object
*user_creatable_add_type(const char *type
, const char *id
,
39 Visitor
*v
, Error
**errp
)
44 Error
*local_err
= NULL
;
46 klass
= object_class_by_name(type
);
48 error_setg(errp
, "invalid object type: %s", type
);
52 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
53 error_setg(errp
, "object type '%s' isn't supported by object-add",
58 if (object_class_is_abstract(klass
)) {
59 error_setg(errp
, "object type '%s' is abstract", type
);
64 obj
= object_new(type
);
65 if (object_property_find(obj
, "id", NULL
)) {
66 object_property_set_str(obj
, id
, "id", &local_err
);
71 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
75 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
76 object_property_set(obj
, v
, e
->key
, &local_err
);
82 visit_check_struct(v
, &local_err
);
84 visit_end_struct(v
, NULL
);
89 object_property_add_child(object_get_objects_root(),
95 user_creatable_complete(obj
, &local_err
);
97 object_property_del(object_get_objects_root(),
103 error_propagate(errp
, local_err
);
111 Object
*user_creatable_add_opts(QemuOpts
*opts
, Error
**errp
)
116 const char *id
= qemu_opts_id(opts
);
117 char *type
= qemu_opt_get_del(opts
, "qom-type");
120 error_setg(errp
, QERR_MISSING_PARAMETER
, "qom-type");
124 error_setg(errp
, QERR_MISSING_PARAMETER
, "id");
125 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
130 qemu_opts_set_id(opts
, NULL
);
131 pdict
= qemu_opts_to_qdict(opts
, NULL
);
133 v
= opts_visitor_new(opts
);
134 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
137 qemu_opts_set_id(opts
, (char *) id
);
138 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
145 int user_creatable_add_opts_foreach(void *opaque
, QemuOpts
*opts
, Error
**errp
)
147 bool (*type_predicate
)(const char *) = opaque
;
152 type
= qemu_opt_get(opts
, "qom-type");
153 if (type
&& type_predicate
&&
154 !type_predicate(type
)) {
158 obj
= user_creatable_add_opts(opts
, &err
);
160 error_report_err(err
);
168 void user_creatable_del(const char *id
, Error
**errp
)
173 container
= object_get_objects_root();
174 obj
= object_resolve_path_component(container
, id
);
176 error_setg(errp
, "object '%s' not found", id
);
180 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
), errp
)) {
181 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
184 object_unparent(obj
);
187 static void register_types(void)
189 static const TypeInfo uc_interface_info
= {
190 .name
= TYPE_USER_CREATABLE
,
191 .parent
= TYPE_INTERFACE
,
192 .class_size
= sizeof(UserCreatableClass
),
195 type_register_static(&uc_interface_info
);
198 type_init(register_types
)