1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qapi/qmp/qdict.h"
4 #include "qapi/qmp/qerror.h"
5 #include "qom/object_interfaces.h"
6 #include "qemu/module.h"
7 #include "qemu/option.h"
8 #include "qapi/opts-visitor.h"
9 #include "qemu/config-file.h"
11 void user_creatable_complete(Object
*obj
, Error
**errp
)
14 UserCreatableClass
*ucc
;
16 (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
22 ucc
= USER_CREATABLE_GET_CLASS(uc
);
24 ucc
->complete(uc
, errp
);
28 bool user_creatable_can_be_deleted(UserCreatable
*uc
)
31 UserCreatableClass
*ucc
= USER_CREATABLE_GET_CLASS(uc
);
33 if (ucc
->can_be_deleted
) {
34 return ucc
->can_be_deleted(uc
);
40 Object
*user_creatable_add_type(const char *type
, const char *id
,
42 Visitor
*v
, Error
**errp
)
47 Error
*local_err
= NULL
;
49 klass
= object_class_by_name(type
);
51 error_setg(errp
, "invalid object type: %s", type
);
55 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
56 error_setg(errp
, "object type '%s' isn't supported by object-add",
61 if (object_class_is_abstract(klass
)) {
62 error_setg(errp
, "object type '%s' is abstract", type
);
67 obj
= object_new(type
);
68 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
72 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
73 object_property_set(obj
, v
, e
->key
, &local_err
);
79 visit_check_struct(v
, &local_err
);
81 visit_end_struct(v
, NULL
);
86 object_property_add_child(object_get_objects_root(),
92 user_creatable_complete(obj
, &local_err
);
94 object_property_del(object_get_objects_root(),
100 error_propagate(errp
, local_err
);
108 Object
*user_creatable_add_opts(QemuOpts
*opts
, Error
**errp
)
113 const char *id
= qemu_opts_id(opts
);
114 char *type
= qemu_opt_get_del(opts
, "qom-type");
117 error_setg(errp
, QERR_MISSING_PARAMETER
, "qom-type");
121 error_setg(errp
, QERR_MISSING_PARAMETER
, "id");
122 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
127 qemu_opts_set_id(opts
, NULL
);
128 pdict
= qemu_opts_to_qdict(opts
, NULL
);
130 v
= opts_visitor_new(opts
);
131 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
134 qemu_opts_set_id(opts
, (char *) id
);
135 qemu_opt_set(opts
, "qom-type", type
, &error_abort
);
137 qobject_unref(pdict
);
142 int user_creatable_add_opts_foreach(void *opaque
, QemuOpts
*opts
, Error
**errp
)
144 bool (*type_predicate
)(const char *) = opaque
;
149 type
= qemu_opt_get(opts
, "qom-type");
150 if (type
&& type_predicate
&&
151 !type_predicate(type
)) {
155 obj
= user_creatable_add_opts(opts
, &err
);
157 error_report_err(err
);
165 void user_creatable_del(const char *id
, Error
**errp
)
170 container
= object_get_objects_root();
171 obj
= object_resolve_path_component(container
, id
);
173 error_setg(errp
, "object '%s' not found", id
);
177 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
))) {
178 error_setg(errp
, "object '%s' is in use, can not be deleted", id
);
183 * if object was defined on the command-line, remove its corresponding
186 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort
),
189 object_unparent(obj
);
192 void user_creatable_cleanup(void)
194 object_unparent(object_get_objects_root());
197 static void register_types(void)
199 static const TypeInfo uc_interface_info
= {
200 .name
= TYPE_USER_CREATABLE
,
201 .parent
= TYPE_INTERFACE
,
202 .class_size
= sizeof(UserCreatableClass
),
205 type_register_static(&uc_interface_info
);
208 type_init(register_types
)