hw/arm: Add palmetto-bmc machine
[qemu.git] / qom / object_interfaces.c
blobc2f6e2998e454e96985372547db5f15f1d3fdc69
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;
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;
38 Object *user_creatable_add(const QDict *qdict,
39 Visitor *v, Error **errp)
41 char *type = NULL;
42 char *id = NULL;
43 Object *obj = NULL;
44 Error *local_err = NULL, *end_err = NULL;
45 QDict *pdict;
47 pdict = qdict_clone_shallow(qdict);
49 visit_start_struct(v, NULL, NULL, 0, &local_err);
50 if (local_err) {
51 goto out;
54 qdict_del(pdict, "qom-type");
55 visit_type_str(v, "qom-type", &type, &local_err);
56 if (local_err) {
57 goto out_visit;
60 qdict_del(pdict, "id");
61 visit_type_str(v, "id", &id, &local_err);
62 if (local_err) {
63 goto out_visit;
66 obj = user_creatable_add_type(type, id, pdict, v, &local_err);
67 if (local_err) {
68 goto out_visit;
71 out_visit:
72 visit_end_struct(v, &end_err);
73 if (end_err) {
74 error_propagate(&local_err, end_err);
75 if (obj) {
76 user_creatable_del(id, NULL);
78 goto out;
81 out:
82 QDECREF(pdict);
83 g_free(id);
84 g_free(type);
85 if (local_err) {
86 error_propagate(errp, local_err);
87 object_unref(obj);
88 return NULL;
90 return obj;
94 Object *user_creatable_add_type(const char *type, const char *id,
95 const QDict *qdict,
96 Visitor *v, Error **errp)
98 Object *obj;
99 ObjectClass *klass;
100 const QDictEntry *e;
101 Error *local_err = NULL;
103 klass = object_class_by_name(type);
104 if (!klass) {
105 error_setg(errp, "invalid object type: %s", type);
106 return NULL;
109 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
110 error_setg(errp, "object type '%s' isn't supported by object-add",
111 type);
112 return NULL;
115 if (object_class_is_abstract(klass)) {
116 error_setg(errp, "object type '%s' is abstract", type);
117 return NULL;
120 obj = object_new(type);
121 if (qdict) {
122 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
123 object_property_set(obj, v, e->key, &local_err);
124 if (local_err) {
125 goto out;
130 object_property_add_child(object_get_objects_root(),
131 id, obj, &local_err);
132 if (local_err) {
133 goto out;
136 user_creatable_complete(obj, &local_err);
137 if (local_err) {
138 object_property_del(object_get_objects_root(),
139 id, &error_abort);
140 goto out;
142 out:
143 if (local_err) {
144 error_propagate(errp, local_err);
145 object_unref(obj);
146 return NULL;
148 return obj;
152 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
154 OptsVisitor *ov;
155 QDict *pdict;
156 Object *obj = NULL;
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);
163 QDECREF(pdict);
164 return obj;
168 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
170 bool (*type_predicate)(const char *) = opaque;
171 Object *obj = NULL;
172 const char *type;
174 type = qemu_opt_get(opts, "qom-type");
175 if (type && type_predicate &&
176 !type_predicate(type)) {
177 return 0;
180 obj = user_creatable_add_opts(opts, errp);
181 if (!obj) {
182 return -1;
184 object_unref(obj);
185 return 0;
189 void user_creatable_del(const char *id, Error **errp)
191 Object *container;
192 Object *obj;
194 container = object_get_objects_root();
195 obj = object_resolve_path_component(container, id);
196 if (!obj) {
197 error_setg(errp, "object '%s' not found", id);
198 return;
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);
203 return;
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)