]>
Commit | Line | Data |
---|---|---|
2f28d2ff AL |
1 | /* |
2 | * QEMU Object Model | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
9bbc853b | 13 | #include "qemu/osdep.h" |
da34e65c | 14 | #include "qapi/error.h" |
14cccb61 | 15 | #include "qom/object.h" |
a31bdae5 | 16 | #include "qom/object_interfaces.h" |
f348b6d1 | 17 | #include "qemu/cutils.h" |
7b1b5d19 | 18 | #include "qapi/visitor.h" |
b2cd7dee PB |
19 | #include "qapi/string-input-visitor.h" |
20 | #include "qapi/string-output-visitor.h" | |
eb815e24 | 21 | #include "qapi/qapi-builtin-visit.h" |
7b1b5d19 | 22 | #include "qapi/qmp/qerror.h" |
fa131d94 | 23 | #include "trace.h" |
2f28d2ff | 24 | |
7b7b7d18 PB |
25 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
26 | * of the QOM core on QObject? */ | |
14cccb61 | 27 | #include "qom/qom-qobject.h" |
7b1b5d19 | 28 | #include "qapi/qmp/qbool.h" |
15280c36 | 29 | #include "qapi/qmp/qnum.h" |
7b1b5d19 | 30 | #include "qapi/qmp/qstring.h" |
7b7b7d18 | 31 | |
2f28d2ff AL |
32 | #define MAX_INTERFACES 32 |
33 | ||
34 | typedef struct InterfaceImpl InterfaceImpl; | |
35 | typedef struct TypeImpl TypeImpl; | |
36 | ||
37 | struct InterfaceImpl | |
38 | { | |
33e95c63 | 39 | const char *typename; |
2f28d2ff AL |
40 | }; |
41 | ||
42 | struct TypeImpl | |
43 | { | |
44 | const char *name; | |
45 | ||
46 | size_t class_size; | |
47 | ||
48 | size_t instance_size; | |
49 | ||
50 | void (*class_init)(ObjectClass *klass, void *data); | |
3b50e311 | 51 | void (*class_base_init)(ObjectClass *klass, void *data); |
2f28d2ff AL |
52 | void (*class_finalize)(ObjectClass *klass, void *data); |
53 | ||
54 | void *class_data; | |
55 | ||
56 | void (*instance_init)(Object *obj); | |
8231c2dd | 57 | void (*instance_post_init)(Object *obj); |
2f28d2ff AL |
58 | void (*instance_finalize)(Object *obj); |
59 | ||
60 | bool abstract; | |
61 | ||
62 | const char *parent; | |
63 | TypeImpl *parent_type; | |
64 | ||
65 | ObjectClass *class; | |
66 | ||
67 | int num_interfaces; | |
68 | InterfaceImpl interfaces[MAX_INTERFACES]; | |
69 | }; | |
70 | ||
9970bd88 PB |
71 | static Type type_interface; |
72 | ||
2f28d2ff AL |
73 | static GHashTable *type_table_get(void) |
74 | { | |
75 | static GHashTable *type_table; | |
76 | ||
77 | if (type_table == NULL) { | |
78 | type_table = g_hash_table_new(g_str_hash, g_str_equal); | |
79 | } | |
80 | ||
81 | return type_table; | |
82 | } | |
83 | ||
f54c19ca HP |
84 | static bool enumerating_types; |
85 | ||
2f28d2ff AL |
86 | static void type_table_add(TypeImpl *ti) |
87 | { | |
f54c19ca | 88 | assert(!enumerating_types); |
2f28d2ff AL |
89 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); |
90 | } | |
91 | ||
92 | static TypeImpl *type_table_lookup(const char *name) | |
93 | { | |
94 | return g_hash_table_lookup(type_table_get(), name); | |
95 | } | |
96 | ||
b061dc41 | 97 | static TypeImpl *type_new(const TypeInfo *info) |
2f28d2ff AL |
98 | { |
99 | TypeImpl *ti = g_malloc0(sizeof(*ti)); | |
33e95c63 | 100 | int i; |
2f28d2ff AL |
101 | |
102 | g_assert(info->name != NULL); | |
103 | ||
73093354 AL |
104 | if (type_table_lookup(info->name) != NULL) { |
105 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); | |
106 | abort(); | |
107 | } | |
108 | ||
2f28d2ff AL |
109 | ti->name = g_strdup(info->name); |
110 | ti->parent = g_strdup(info->parent); | |
111 | ||
112 | ti->class_size = info->class_size; | |
113 | ti->instance_size = info->instance_size; | |
114 | ||
115 | ti->class_init = info->class_init; | |
3b50e311 | 116 | ti->class_base_init = info->class_base_init; |
2f28d2ff AL |
117 | ti->class_finalize = info->class_finalize; |
118 | ti->class_data = info->class_data; | |
119 | ||
120 | ti->instance_init = info->instance_init; | |
8231c2dd | 121 | ti->instance_post_init = info->instance_post_init; |
2f28d2ff AL |
122 | ti->instance_finalize = info->instance_finalize; |
123 | ||
124 | ti->abstract = info->abstract; | |
125 | ||
33e95c63 AL |
126 | for (i = 0; info->interfaces && info->interfaces[i].type; i++) { |
127 | ti->interfaces[i].typename = g_strdup(info->interfaces[i].type); | |
2f28d2ff | 128 | } |
33e95c63 | 129 | ti->num_interfaces = i; |
2f28d2ff | 130 | |
b061dc41 PB |
131 | return ti; |
132 | } | |
2f28d2ff | 133 | |
b061dc41 PB |
134 | static TypeImpl *type_register_internal(const TypeInfo *info) |
135 | { | |
136 | TypeImpl *ti; | |
137 | ti = type_new(info); | |
138 | ||
139 | type_table_add(ti); | |
2f28d2ff AL |
140 | return ti; |
141 | } | |
142 | ||
049cb3cf PB |
143 | TypeImpl *type_register(const TypeInfo *info) |
144 | { | |
145 | assert(info->parent); | |
146 | return type_register_internal(info); | |
147 | } | |
148 | ||
2f28d2ff AL |
149 | TypeImpl *type_register_static(const TypeInfo *info) |
150 | { | |
151 | return type_register(info); | |
152 | } | |
153 | ||
aa04c9d2 IM |
154 | void type_register_static_array(const TypeInfo *infos, int nr_infos) |
155 | { | |
156 | int i; | |
157 | ||
158 | for (i = 0; i < nr_infos; i++) { | |
159 | type_register_static(&infos[i]); | |
160 | } | |
161 | } | |
162 | ||
2f28d2ff AL |
163 | static TypeImpl *type_get_by_name(const char *name) |
164 | { | |
165 | if (name == NULL) { | |
166 | return NULL; | |
167 | } | |
168 | ||
169 | return type_table_lookup(name); | |
170 | } | |
171 | ||
172 | static TypeImpl *type_get_parent(TypeImpl *type) | |
173 | { | |
174 | if (!type->parent_type && type->parent) { | |
175 | type->parent_type = type_get_by_name(type->parent); | |
176 | g_assert(type->parent_type != NULL); | |
177 | } | |
178 | ||
179 | return type->parent_type; | |
180 | } | |
181 | ||
182 | static bool type_has_parent(TypeImpl *type) | |
183 | { | |
184 | return (type->parent != NULL); | |
185 | } | |
186 | ||
187 | static size_t type_class_get_size(TypeImpl *ti) | |
188 | { | |
189 | if (ti->class_size) { | |
190 | return ti->class_size; | |
191 | } | |
192 | ||
193 | if (type_has_parent(ti)) { | |
194 | return type_class_get_size(type_get_parent(ti)); | |
195 | } | |
196 | ||
197 | return sizeof(ObjectClass); | |
198 | } | |
199 | ||
aca59af6 IM |
200 | static size_t type_object_get_size(TypeImpl *ti) |
201 | { | |
202 | if (ti->instance_size) { | |
203 | return ti->instance_size; | |
204 | } | |
205 | ||
206 | if (type_has_parent(ti)) { | |
207 | return type_object_get_size(type_get_parent(ti)); | |
208 | } | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
3f97b53a BR |
213 | size_t object_type_get_instance_size(const char *typename) |
214 | { | |
215 | TypeImpl *type = type_get_by_name(typename); | |
216 | ||
217 | g_assert(type != NULL); | |
218 | return type_object_get_size(type); | |
219 | } | |
220 | ||
33e95c63 | 221 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
2f28d2ff | 222 | { |
33e95c63 AL |
223 | assert(target_type); |
224 | ||
b30d8054 | 225 | /* Check if target_type is a direct ancestor of type */ |
33e95c63 AL |
226 | while (type) { |
227 | if (type == target_type) { | |
228 | return true; | |
229 | } | |
2f28d2ff | 230 | |
33e95c63 AL |
231 | type = type_get_parent(type); |
232 | } | |
233 | ||
234 | return false; | |
235 | } | |
236 | ||
237 | static void type_initialize(TypeImpl *ti); | |
238 | ||
b061dc41 PB |
239 | static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type, |
240 | TypeImpl *parent_type) | |
33e95c63 AL |
241 | { |
242 | InterfaceClass *new_iface; | |
243 | TypeInfo info = { }; | |
244 | TypeImpl *iface_impl; | |
245 | ||
b061dc41 PB |
246 | info.parent = parent_type->name; |
247 | info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name); | |
33e95c63 AL |
248 | info.abstract = true; |
249 | ||
b061dc41 PB |
250 | iface_impl = type_new(&info); |
251 | iface_impl->parent_type = parent_type; | |
33e95c63 AL |
252 | type_initialize(iface_impl); |
253 | g_free((char *)info.name); | |
254 | ||
255 | new_iface = (InterfaceClass *)iface_impl->class; | |
256 | new_iface->concrete_class = ti->class; | |
b061dc41 | 257 | new_iface->interface_type = interface_type; |
33e95c63 AL |
258 | |
259 | ti->class->interfaces = g_slist_append(ti->class->interfaces, | |
260 | iface_impl->class); | |
2f28d2ff AL |
261 | } |
262 | ||
16bf7f52 DB |
263 | static void object_property_free(gpointer data) |
264 | { | |
265 | ObjectProperty *prop = data; | |
266 | ||
267 | g_free(prop->name); | |
268 | g_free(prop->type); | |
269 | g_free(prop->description); | |
270 | g_free(prop); | |
271 | } | |
272 | ||
ac451033 | 273 | static void type_initialize(TypeImpl *ti) |
2f28d2ff | 274 | { |
745549c8 | 275 | TypeImpl *parent; |
2f28d2ff AL |
276 | |
277 | if (ti->class) { | |
278 | return; | |
279 | } | |
280 | ||
281 | ti->class_size = type_class_get_size(ti); | |
aca59af6 | 282 | ti->instance_size = type_object_get_size(ti); |
1c6d75d5 EH |
283 | /* Any type with zero instance_size is implicitly abstract. |
284 | * This means interface types are all abstract. | |
285 | */ | |
286 | if (ti->instance_size == 0) { | |
287 | ti->abstract = true; | |
288 | } | |
2f28d2ff AL |
289 | |
290 | ti->class = g_malloc0(ti->class_size); | |
2f28d2ff | 291 | |
745549c8 PB |
292 | parent = type_get_parent(ti); |
293 | if (parent) { | |
ac451033 | 294 | type_initialize(parent); |
33e95c63 AL |
295 | GSList *e; |
296 | int i; | |
2f28d2ff | 297 | |
8438a135 | 298 | g_assert_cmpint(parent->class_size, <=, ti->class_size); |
745549c8 | 299 | memcpy(ti->class, parent->class, parent->class_size); |
3e407de4 | 300 | ti->class->interfaces = NULL; |
16bf7f52 DB |
301 | ti->class->properties = g_hash_table_new_full( |
302 | g_str_hash, g_str_equal, g_free, object_property_free); | |
33e95c63 AL |
303 | |
304 | for (e = parent->class->interfaces; e; e = e->next) { | |
b061dc41 PB |
305 | InterfaceClass *iface = e->data; |
306 | ObjectClass *klass = OBJECT_CLASS(iface); | |
307 | ||
308 | type_initialize_interface(ti, iface->interface_type, klass->type); | |
33e95c63 AL |
309 | } |
310 | ||
311 | for (i = 0; i < ti->num_interfaces; i++) { | |
312 | TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); | |
313 | for (e = ti->class->interfaces; e; e = e->next) { | |
314 | TypeImpl *target_type = OBJECT_CLASS(e->data)->type; | |
315 | ||
316 | if (type_is_ancestor(target_type, t)) { | |
317 | break; | |
318 | } | |
319 | } | |
320 | ||
321 | if (e) { | |
322 | continue; | |
323 | } | |
324 | ||
b061dc41 | 325 | type_initialize_interface(ti, t, t); |
33e95c63 | 326 | } |
16bf7f52 DB |
327 | } else { |
328 | ti->class->properties = g_hash_table_new_full( | |
329 | g_str_hash, g_str_equal, g_free, object_property_free); | |
745549c8 | 330 | } |
2f28d2ff | 331 | |
745549c8 | 332 | ti->class->type = ti; |
3b50e311 | 333 | |
745549c8 PB |
334 | while (parent) { |
335 | if (parent->class_base_init) { | |
336 | parent->class_base_init(ti->class, ti->class_data); | |
3b50e311 | 337 | } |
745549c8 | 338 | parent = type_get_parent(parent); |
2f28d2ff AL |
339 | } |
340 | ||
2f28d2ff AL |
341 | if (ti->class_init) { |
342 | ti->class_init(ti->class, ti->class_data); | |
343 | } | |
2f28d2ff AL |
344 | } |
345 | ||
346 | static void object_init_with_type(Object *obj, TypeImpl *ti) | |
347 | { | |
2f28d2ff AL |
348 | if (type_has_parent(ti)) { |
349 | object_init_with_type(obj, type_get_parent(ti)); | |
350 | } | |
351 | ||
2f28d2ff AL |
352 | if (ti->instance_init) { |
353 | ti->instance_init(obj); | |
354 | } | |
355 | } | |
356 | ||
8231c2dd EH |
357 | static void object_post_init_with_type(Object *obj, TypeImpl *ti) |
358 | { | |
359 | if (ti->instance_post_init) { | |
360 | ti->instance_post_init(obj); | |
361 | } | |
362 | ||
363 | if (type_has_parent(ti)) { | |
364 | object_post_init_with_type(obj, type_get_parent(ti)); | |
365 | } | |
366 | } | |
367 | ||
63f7b10b | 368 | static void object_initialize_with_type(void *data, size_t size, TypeImpl *type) |
2f28d2ff AL |
369 | { |
370 | Object *obj = data; | |
371 | ||
372 | g_assert(type != NULL); | |
ac451033 | 373 | type_initialize(type); |
aca59af6 | 374 | |
8438a135 | 375 | g_assert_cmpint(type->instance_size, >=, sizeof(Object)); |
2f28d2ff | 376 | g_assert(type->abstract == false); |
8438a135 | 377 | g_assert_cmpint(size, >=, type->instance_size); |
2f28d2ff AL |
378 | |
379 | memset(obj, 0, type->instance_size); | |
380 | obj->class = type->class; | |
764b6312 | 381 | object_ref(obj); |
b604a854 PF |
382 | obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal, |
383 | NULL, object_property_free); | |
2f28d2ff | 384 | object_init_with_type(obj, type); |
8231c2dd | 385 | object_post_init_with_type(obj, type); |
2f28d2ff AL |
386 | } |
387 | ||
213f0c4f | 388 | void object_initialize(void *data, size_t size, const char *typename) |
2f28d2ff AL |
389 | { |
390 | TypeImpl *type = type_get_by_name(typename); | |
391 | ||
5b9237f6 | 392 | object_initialize_with_type(data, size, type); |
2f28d2ff AL |
393 | } |
394 | ||
5d9d3f47 AF |
395 | static inline bool object_property_is_child(ObjectProperty *prop) |
396 | { | |
397 | return strstart(prop->type, "child<", NULL); | |
398 | } | |
399 | ||
57c9fafe AL |
400 | static void object_property_del_all(Object *obj) |
401 | { | |
b604a854 PF |
402 | ObjectProperty *prop; |
403 | GHashTableIter iter; | |
404 | gpointer key, value; | |
405 | bool released; | |
406 | ||
407 | do { | |
408 | released = false; | |
409 | g_hash_table_iter_init(&iter, obj->properties); | |
410 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
411 | prop = value; | |
412 | if (prop->release) { | |
413 | prop->release(obj, prop->name, prop->opaque); | |
414 | prop->release = NULL; | |
415 | released = true; | |
416 | break; | |
417 | } | |
418 | g_hash_table_iter_remove(&iter); | |
57c9fafe | 419 | } |
b604a854 | 420 | } while (released); |
57c9fafe | 421 | |
b604a854 | 422 | g_hash_table_unref(obj->properties); |
57c9fafe AL |
423 | } |
424 | ||
425 | static void object_property_del_child(Object *obj, Object *child, Error **errp) | |
426 | { | |
427 | ObjectProperty *prop; | |
b604a854 PF |
428 | GHashTableIter iter; |
429 | gpointer key, value; | |
57c9fafe | 430 | |
b604a854 PF |
431 | g_hash_table_iter_init(&iter, obj->properties); |
432 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
433 | prop = value; | |
434 | if (object_property_is_child(prop) && prop->opaque == child) { | |
435 | if (prop->release) { | |
436 | prop->release(obj, prop->name, prop->opaque); | |
437 | prop->release = NULL; | |
438 | } | |
439 | break; | |
440 | } | |
441 | } | |
442 | g_hash_table_iter_init(&iter, obj->properties); | |
443 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
444 | prop = value; | |
5d9d3f47 | 445 | if (object_property_is_child(prop) && prop->opaque == child) { |
b604a854 | 446 | g_hash_table_iter_remove(&iter); |
6c1fdcf9 | 447 | break; |
57c9fafe AL |
448 | } |
449 | } | |
450 | } | |
451 | ||
452 | void object_unparent(Object *obj) | |
453 | { | |
e998fa8d MT |
454 | if (obj->parent) { |
455 | object_property_del_child(obj->parent, obj, NULL); | |
456 | } | |
57c9fafe AL |
457 | } |
458 | ||
2f28d2ff AL |
459 | static void object_deinit(Object *obj, TypeImpl *type) |
460 | { | |
461 | if (type->instance_finalize) { | |
462 | type->instance_finalize(obj); | |
463 | } | |
464 | ||
2f28d2ff AL |
465 | if (type_has_parent(type)) { |
466 | object_deinit(obj, type_get_parent(type)); | |
467 | } | |
468 | } | |
469 | ||
339c2708 | 470 | static void object_finalize(void *data) |
2f28d2ff AL |
471 | { |
472 | Object *obj = data; | |
473 | TypeImpl *ti = obj->class->type; | |
474 | ||
57c9fafe | 475 | object_property_del_all(obj); |
76a6e1cc | 476 | object_deinit(obj, ti); |
db85b575 | 477 | |
8438a135 | 478 | g_assert_cmpint(obj->ref, ==, 0); |
fde9bf44 PB |
479 | if (obj->free) { |
480 | obj->free(obj); | |
481 | } | |
2f28d2ff AL |
482 | } |
483 | ||
63f7b10b | 484 | static Object *object_new_with_type(Type type) |
2f28d2ff AL |
485 | { |
486 | Object *obj; | |
487 | ||
488 | g_assert(type != NULL); | |
ac451033 | 489 | type_initialize(type); |
2f28d2ff AL |
490 | |
491 | obj = g_malloc(type->instance_size); | |
5b9237f6 | 492 | object_initialize_with_type(obj, type->instance_size, type); |
fde9bf44 | 493 | obj->free = g_free; |
2f28d2ff AL |
494 | |
495 | return obj; | |
496 | } | |
497 | ||
498 | Object *object_new(const char *typename) | |
499 | { | |
500 | TypeImpl *ti = type_get_by_name(typename); | |
501 | ||
502 | return object_new_with_type(ti); | |
503 | } | |
504 | ||
a31bdae5 DB |
505 | |
506 | Object *object_new_with_props(const char *typename, | |
507 | Object *parent, | |
508 | const char *id, | |
509 | Error **errp, | |
510 | ...) | |
511 | { | |
512 | va_list vargs; | |
513 | Object *obj; | |
514 | ||
515 | va_start(vargs, errp); | |
516 | obj = object_new_with_propv(typename, parent, id, errp, vargs); | |
517 | va_end(vargs); | |
518 | ||
519 | return obj; | |
520 | } | |
521 | ||
522 | ||
523 | Object *object_new_with_propv(const char *typename, | |
524 | Object *parent, | |
525 | const char *id, | |
526 | Error **errp, | |
527 | va_list vargs) | |
528 | { | |
529 | Object *obj; | |
530 | ObjectClass *klass; | |
531 | Error *local_err = NULL; | |
532 | ||
533 | klass = object_class_by_name(typename); | |
534 | if (!klass) { | |
535 | error_setg(errp, "invalid object type: %s", typename); | |
536 | return NULL; | |
537 | } | |
538 | ||
539 | if (object_class_is_abstract(klass)) { | |
540 | error_setg(errp, "object type '%s' is abstract", typename); | |
541 | return NULL; | |
542 | } | |
543 | obj = object_new(typename); | |
544 | ||
545 | if (object_set_propv(obj, &local_err, vargs) < 0) { | |
546 | goto error; | |
547 | } | |
548 | ||
549 | object_property_add_child(parent, id, obj, &local_err); | |
550 | if (local_err) { | |
551 | goto error; | |
552 | } | |
553 | ||
554 | if (object_dynamic_cast(obj, TYPE_USER_CREATABLE)) { | |
555 | user_creatable_complete(obj, &local_err); | |
556 | if (local_err) { | |
557 | object_unparent(obj); | |
558 | goto error; | |
559 | } | |
560 | } | |
561 | ||
562 | object_unref(OBJECT(obj)); | |
563 | return obj; | |
564 | ||
565 | error: | |
621ff94d | 566 | error_propagate(errp, local_err); |
a31bdae5 DB |
567 | object_unref(obj); |
568 | return NULL; | |
569 | } | |
570 | ||
571 | ||
572 | int object_set_props(Object *obj, | |
573 | Error **errp, | |
574 | ...) | |
575 | { | |
576 | va_list vargs; | |
577 | int ret; | |
578 | ||
579 | va_start(vargs, errp); | |
580 | ret = object_set_propv(obj, errp, vargs); | |
581 | va_end(vargs); | |
582 | ||
583 | return ret; | |
584 | } | |
585 | ||
586 | ||
587 | int object_set_propv(Object *obj, | |
588 | Error **errp, | |
589 | va_list vargs) | |
590 | { | |
591 | const char *propname; | |
592 | Error *local_err = NULL; | |
593 | ||
594 | propname = va_arg(vargs, char *); | |
595 | while (propname != NULL) { | |
596 | const char *value = va_arg(vargs, char *); | |
597 | ||
598 | g_assert(value != NULL); | |
599 | object_property_parse(obj, value, propname, &local_err); | |
600 | if (local_err) { | |
601 | error_propagate(errp, local_err); | |
602 | return -1; | |
603 | } | |
604 | propname = va_arg(vargs, char *); | |
605 | } | |
606 | ||
607 | return 0; | |
608 | } | |
609 | ||
610 | ||
2f28d2ff AL |
611 | Object *object_dynamic_cast(Object *obj, const char *typename) |
612 | { | |
b7f43fe4 | 613 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
acc4af3f PB |
614 | return obj; |
615 | } | |
616 | ||
2f28d2ff AL |
617 | return NULL; |
618 | } | |
619 | ||
be17f18b PB |
620 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
621 | const char *file, int line, const char *func) | |
2f28d2ff | 622 | { |
fa131d94 PB |
623 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
624 | typename, file, line, func); | |
625 | ||
3556c233 | 626 | #ifdef CONFIG_QOM_CAST_DEBUG |
03587328 AL |
627 | int i; |
628 | Object *inst; | |
629 | ||
95916abc | 630 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd | 631 | if (atomic_read(&obj->class->object_cast_cache[i]) == typename) { |
03587328 AL |
632 | goto out; |
633 | } | |
634 | } | |
635 | ||
636 | inst = object_dynamic_cast(obj, typename); | |
2f28d2ff | 637 | |
b7f43fe4 | 638 | if (!inst && obj) { |
be17f18b PB |
639 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
640 | file, line, func, obj, typename); | |
2f28d2ff AL |
641 | abort(); |
642 | } | |
643 | ||
3556c233 | 644 | assert(obj == inst); |
03587328 | 645 | |
95916abc | 646 | if (obj && obj == inst) { |
03587328 | 647 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd AB |
648 | atomic_set(&obj->class->object_cast_cache[i - 1], |
649 | atomic_read(&obj->class->object_cast_cache[i])); | |
03587328 | 650 | } |
b6b3ccfd | 651 | atomic_set(&obj->class->object_cast_cache[i - 1], typename); |
03587328 AL |
652 | } |
653 | ||
654 | out: | |
3556c233 PB |
655 | #endif |
656 | return obj; | |
2f28d2ff AL |
657 | } |
658 | ||
659 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
660 | const char *typename) | |
661 | { | |
33e95c63 | 662 | ObjectClass *ret = NULL; |
bf0fda34 PB |
663 | TypeImpl *target_type; |
664 | TypeImpl *type; | |
2f28d2ff | 665 | |
bf0fda34 PB |
666 | if (!class) { |
667 | return NULL; | |
668 | } | |
669 | ||
793c96b5 | 670 | /* A simple fast path that can trigger a lot for leaf classes. */ |
bf0fda34 | 671 | type = class->type; |
793c96b5 PB |
672 | if (type->name == typename) { |
673 | return class; | |
674 | } | |
675 | ||
bf0fda34 | 676 | target_type = type_get_by_name(typename); |
9ab880b3 AG |
677 | if (!target_type) { |
678 | /* target class type unknown, so fail the cast */ | |
679 | return NULL; | |
680 | } | |
681 | ||
00e2ceae PC |
682 | if (type->class->interfaces && |
683 | type_is_ancestor(target_type, type_interface)) { | |
33e95c63 AL |
684 | int found = 0; |
685 | GSList *i; | |
2f28d2ff | 686 | |
33e95c63 AL |
687 | for (i = class->interfaces; i; i = i->next) { |
688 | ObjectClass *target_class = i->data; | |
689 | ||
690 | if (type_is_ancestor(target_class->type, target_type)) { | |
691 | ret = target_class; | |
692 | found++; | |
693 | } | |
694 | } | |
695 | ||
696 | /* The match was ambiguous, don't allow a cast */ | |
697 | if (found > 1) { | |
698 | ret = NULL; | |
699 | } | |
700 | } else if (type_is_ancestor(type, target_type)) { | |
701 | ret = class; | |
2f28d2ff AL |
702 | } |
703 | ||
33e95c63 | 704 | return ret; |
2f28d2ff AL |
705 | } |
706 | ||
707 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
be17f18b PB |
708 | const char *typename, |
709 | const char *file, int line, | |
710 | const char *func) | |
2f28d2ff | 711 | { |
fa131d94 PB |
712 | ObjectClass *ret; |
713 | ||
714 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", | |
715 | typename, file, line, func); | |
2f28d2ff | 716 | |
03587328 AL |
717 | #ifdef CONFIG_QOM_CAST_DEBUG |
718 | int i; | |
719 | ||
9d6a3d58 | 720 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd | 721 | if (atomic_read(&class->class_cast_cache[i]) == typename) { |
03587328 AL |
722 | ret = class; |
723 | goto out; | |
724 | } | |
725 | } | |
726 | #else | |
9d6a3d58 | 727 | if (!class || !class->interfaces) { |
3556c233 PB |
728 | return class; |
729 | } | |
730 | #endif | |
731 | ||
fa131d94 | 732 | ret = object_class_dynamic_cast(class, typename); |
bf0fda34 | 733 | if (!ret && class) { |
be17f18b PB |
734 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
735 | file, line, func, class, typename); | |
2f28d2ff AL |
736 | abort(); |
737 | } | |
738 | ||
03587328 | 739 | #ifdef CONFIG_QOM_CAST_DEBUG |
9d6a3d58 | 740 | if (class && ret == class) { |
03587328 | 741 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd AB |
742 | atomic_set(&class->class_cast_cache[i - 1], |
743 | atomic_read(&class->class_cast_cache[i])); | |
03587328 | 744 | } |
b6b3ccfd | 745 | atomic_set(&class->class_cast_cache[i - 1], typename); |
03587328 AL |
746 | } |
747 | out: | |
748 | #endif | |
2f28d2ff AL |
749 | return ret; |
750 | } | |
751 | ||
8f5d58ef | 752 | const char *object_get_typename(const Object *obj) |
2f28d2ff AL |
753 | { |
754 | return obj->class->type->name; | |
755 | } | |
756 | ||
757 | ObjectClass *object_get_class(Object *obj) | |
758 | { | |
759 | return obj->class; | |
760 | } | |
761 | ||
17862378 AF |
762 | bool object_class_is_abstract(ObjectClass *klass) |
763 | { | |
764 | return klass->type->abstract; | |
765 | } | |
766 | ||
2f28d2ff AL |
767 | const char *object_class_get_name(ObjectClass *klass) |
768 | { | |
769 | return klass->type->name; | |
770 | } | |
771 | ||
772 | ObjectClass *object_class_by_name(const char *typename) | |
773 | { | |
774 | TypeImpl *type = type_get_by_name(typename); | |
775 | ||
776 | if (!type) { | |
777 | return NULL; | |
778 | } | |
779 | ||
ac451033 | 780 | type_initialize(type); |
2f28d2ff AL |
781 | |
782 | return type->class; | |
783 | } | |
784 | ||
e7cce67f PB |
785 | ObjectClass *object_class_get_parent(ObjectClass *class) |
786 | { | |
787 | TypeImpl *type = type_get_parent(class->type); | |
788 | ||
789 | if (!type) { | |
790 | return NULL; | |
791 | } | |
792 | ||
793 | type_initialize(type); | |
794 | ||
795 | return type->class; | |
796 | } | |
797 | ||
2f28d2ff AL |
798 | typedef struct OCFData |
799 | { | |
800 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
801 | const char *implements_type; |
802 | bool include_abstract; | |
2f28d2ff AL |
803 | void *opaque; |
804 | } OCFData; | |
805 | ||
806 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
807 | gpointer opaque) | |
808 | { | |
809 | OCFData *data = opaque; | |
810 | TypeImpl *type = value; | |
93c511a1 | 811 | ObjectClass *k; |
2f28d2ff | 812 | |
ac451033 | 813 | type_initialize(type); |
93c511a1 | 814 | k = type->class; |
2f28d2ff | 815 | |
93c511a1 AL |
816 | if (!data->include_abstract && type->abstract) { |
817 | return; | |
818 | } | |
819 | ||
820 | if (data->implements_type && | |
821 | !object_class_dynamic_cast(k, data->implements_type)) { | |
822 | return; | |
823 | } | |
824 | ||
825 | data->fn(k, data->opaque); | |
2f28d2ff AL |
826 | } |
827 | ||
828 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 829 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
830 | void *opaque) |
831 | { | |
93c511a1 | 832 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff | 833 | |
f54c19ca | 834 | enumerating_types = true; |
2f28d2ff | 835 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
f54c19ca | 836 | enumerating_types = false; |
2f28d2ff | 837 | } |
57c9fafe | 838 | |
d714b8de PC |
839 | static int do_object_child_foreach(Object *obj, |
840 | int (*fn)(Object *child, void *opaque), | |
841 | void *opaque, bool recurse) | |
32efc535 | 842 | { |
b604a854 PF |
843 | GHashTableIter iter; |
844 | ObjectProperty *prop; | |
32efc535 PB |
845 | int ret = 0; |
846 | ||
b604a854 PF |
847 | g_hash_table_iter_init(&iter, obj->properties); |
848 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
32efc535 | 849 | if (object_property_is_child(prop)) { |
d714b8de PC |
850 | Object *child = prop->opaque; |
851 | ||
852 | ret = fn(child, opaque); | |
32efc535 PB |
853 | if (ret != 0) { |
854 | break; | |
855 | } | |
d714b8de PC |
856 | if (recurse) { |
857 | do_object_child_foreach(child, fn, opaque, true); | |
858 | } | |
32efc535 PB |
859 | } |
860 | } | |
861 | return ret; | |
862 | } | |
863 | ||
d714b8de PC |
864 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
865 | void *opaque) | |
866 | { | |
867 | return do_object_child_foreach(obj, fn, opaque, false); | |
868 | } | |
869 | ||
870 | int object_child_foreach_recursive(Object *obj, | |
871 | int (*fn)(Object *child, void *opaque), | |
872 | void *opaque) | |
873 | { | |
874 | return do_object_child_foreach(obj, fn, opaque, true); | |
875 | } | |
876 | ||
418ba9e5 AF |
877 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
878 | { | |
879 | GSList **list = opaque; | |
880 | ||
881 | *list = g_slist_prepend(*list, klass); | |
882 | } | |
883 | ||
884 | GSList *object_class_get_list(const char *implements_type, | |
885 | bool include_abstract) | |
886 | { | |
887 | GSList *list = NULL; | |
888 | ||
889 | object_class_foreach(object_class_get_list_tramp, | |
890 | implements_type, include_abstract, &list); | |
891 | return list; | |
892 | } | |
893 | ||
47c66009 PB |
894 | static gint object_class_cmp(gconstpointer a, gconstpointer b) |
895 | { | |
896 | return strcasecmp(object_class_get_name((ObjectClass *)a), | |
897 | object_class_get_name((ObjectClass *)b)); | |
898 | } | |
899 | ||
900 | GSList *object_class_get_list_sorted(const char *implements_type, | |
901 | bool include_abstract) | |
902 | { | |
903 | return g_slist_sort(object_class_get_list(implements_type, include_abstract), | |
904 | object_class_cmp); | |
905 | } | |
906 | ||
57c9fafe AL |
907 | void object_ref(Object *obj) |
908 | { | |
8ffad850 PC |
909 | if (!obj) { |
910 | return; | |
911 | } | |
8438a135 | 912 | atomic_inc(&obj->ref); |
57c9fafe AL |
913 | } |
914 | ||
915 | void object_unref(Object *obj) | |
916 | { | |
8ffad850 PC |
917 | if (!obj) { |
918 | return; | |
919 | } | |
8438a135 | 920 | g_assert_cmpint(obj->ref, >, 0); |
57c9fafe AL |
921 | |
922 | /* parent always holds a reference to its children */ | |
f08c03f3 | 923 | if (atomic_fetch_dec(&obj->ref) == 1) { |
57c9fafe AL |
924 | object_finalize(obj); |
925 | } | |
926 | } | |
927 | ||
64607d08 PB |
928 | ObjectProperty * |
929 | object_property_add(Object *obj, const char *name, const char *type, | |
930 | ObjectPropertyAccessor *get, | |
931 | ObjectPropertyAccessor *set, | |
932 | ObjectPropertyRelease *release, | |
933 | void *opaque, Error **errp) | |
57c9fafe | 934 | { |
54852b03 | 935 | ObjectProperty *prop; |
33965904 PC |
936 | size_t name_len = strlen(name); |
937 | ||
938 | if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { | |
939 | int i; | |
940 | ObjectProperty *ret; | |
941 | char *name_no_array = g_strdup(name); | |
942 | ||
943 | name_no_array[name_len - 3] = '\0'; | |
944 | for (i = 0; ; ++i) { | |
945 | char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); | |
946 | ||
947 | ret = object_property_add(obj, full_name, type, get, set, | |
948 | release, opaque, NULL); | |
949 | g_free(full_name); | |
950 | if (ret) { | |
951 | break; | |
952 | } | |
953 | } | |
954 | g_free(name_no_array); | |
955 | return ret; | |
956 | } | |
54852b03 | 957 | |
16bf7f52 | 958 | if (object_property_find(obj, name, NULL) != NULL) { |
b604a854 | 959 | error_setg(errp, "attempt to add duplicate property '%s'" |
16bf7f52 DB |
960 | " to object (type '%s')", name, |
961 | object_get_typename(obj)); | |
b604a854 | 962 | return NULL; |
54852b03 PM |
963 | } |
964 | ||
965 | prop = g_malloc0(sizeof(*prop)); | |
57c9fafe AL |
966 | |
967 | prop->name = g_strdup(name); | |
968 | prop->type = g_strdup(type); | |
969 | ||
970 | prop->get = get; | |
971 | prop->set = set; | |
972 | prop->release = release; | |
973 | prop->opaque = opaque; | |
974 | ||
b604a854 | 975 | g_hash_table_insert(obj->properties, prop->name, prop); |
64607d08 | 976 | return prop; |
57c9fafe AL |
977 | } |
978 | ||
16bf7f52 DB |
979 | ObjectProperty * |
980 | object_class_property_add(ObjectClass *klass, | |
981 | const char *name, | |
982 | const char *type, | |
983 | ObjectPropertyAccessor *get, | |
984 | ObjectPropertyAccessor *set, | |
985 | ObjectPropertyRelease *release, | |
986 | void *opaque, | |
987 | Error **errp) | |
988 | { | |
989 | ObjectProperty *prop; | |
990 | ||
991 | if (object_class_property_find(klass, name, NULL) != NULL) { | |
992 | error_setg(errp, "attempt to add duplicate property '%s'" | |
993 | " to object (type '%s')", name, | |
994 | object_class_get_name(klass)); | |
995 | return NULL; | |
996 | } | |
997 | ||
998 | prop = g_malloc0(sizeof(*prop)); | |
999 | ||
1000 | prop->name = g_strdup(name); | |
1001 | prop->type = g_strdup(type); | |
1002 | ||
1003 | prop->get = get; | |
1004 | prop->set = set; | |
1005 | prop->release = release; | |
1006 | prop->opaque = opaque; | |
1007 | ||
1008 | g_hash_table_insert(klass->properties, g_strdup(name), prop); | |
1009 | ||
1010 | return prop; | |
1011 | } | |
1012 | ||
89bfe000 PB |
1013 | ObjectProperty *object_property_find(Object *obj, const char *name, |
1014 | Error **errp) | |
57c9fafe AL |
1015 | { |
1016 | ObjectProperty *prop; | |
16bf7f52 DB |
1017 | ObjectClass *klass = object_get_class(obj); |
1018 | ||
1019 | prop = object_class_property_find(klass, name, NULL); | |
1020 | if (prop) { | |
1021 | return prop; | |
1022 | } | |
57c9fafe | 1023 | |
b604a854 PF |
1024 | prop = g_hash_table_lookup(obj->properties, name); |
1025 | if (prop) { | |
1026 | return prop; | |
57c9fafe AL |
1027 | } |
1028 | ||
f231b88d | 1029 | error_setg(errp, "Property '.%s' not found", name); |
57c9fafe AL |
1030 | return NULL; |
1031 | } | |
1032 | ||
7746abd8 DB |
1033 | void object_property_iter_init(ObjectPropertyIterator *iter, |
1034 | Object *obj) | |
a00c9482 | 1035 | { |
7746abd8 DB |
1036 | g_hash_table_iter_init(&iter->iter, obj->properties); |
1037 | iter->nextclass = object_get_class(obj); | |
a00c9482 DB |
1038 | } |
1039 | ||
1040 | ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter) | |
1041 | { | |
b604a854 | 1042 | gpointer key, val; |
16bf7f52 DB |
1043 | while (!g_hash_table_iter_next(&iter->iter, &key, &val)) { |
1044 | if (!iter->nextclass) { | |
1045 | return NULL; | |
1046 | } | |
1047 | g_hash_table_iter_init(&iter->iter, iter->nextclass->properties); | |
1048 | iter->nextclass = object_class_get_parent(iter->nextclass); | |
a00c9482 | 1049 | } |
b604a854 | 1050 | return val; |
a00c9482 DB |
1051 | } |
1052 | ||
961c47bb AK |
1053 | void object_class_property_iter_init(ObjectPropertyIterator *iter, |
1054 | ObjectClass *klass) | |
1055 | { | |
1056 | g_hash_table_iter_init(&iter->iter, klass->properties); | |
1057 | iter->nextclass = klass; | |
1058 | } | |
1059 | ||
16bf7f52 DB |
1060 | ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, |
1061 | Error **errp) | |
1062 | { | |
1063 | ObjectProperty *prop; | |
1064 | ObjectClass *parent_klass; | |
1065 | ||
1066 | parent_klass = object_class_get_parent(klass); | |
1067 | if (parent_klass) { | |
1068 | prop = object_class_property_find(parent_klass, name, NULL); | |
1069 | if (prop) { | |
1070 | return prop; | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | prop = g_hash_table_lookup(klass->properties, name); | |
1075 | if (!prop) { | |
1076 | error_setg(errp, "Property '.%s' not found", name); | |
1077 | } | |
1078 | return prop; | |
1079 | } | |
1080 | ||
57c9fafe AL |
1081 | void object_property_del(Object *obj, const char *name, Error **errp) |
1082 | { | |
b604a854 PF |
1083 | ObjectProperty *prop = g_hash_table_lookup(obj->properties, name); |
1084 | ||
1085 | if (!prop) { | |
1086 | error_setg(errp, "Property '.%s' not found", name); | |
0866aca1 AL |
1087 | return; |
1088 | } | |
57c9fafe | 1089 | |
0866aca1 AL |
1090 | if (prop->release) { |
1091 | prop->release(obj, name, prop->opaque); | |
1092 | } | |
b604a854 | 1093 | g_hash_table_remove(obj->properties, name); |
57c9fafe AL |
1094 | } |
1095 | ||
1096 | void object_property_get(Object *obj, Visitor *v, const char *name, | |
1097 | Error **errp) | |
1098 | { | |
89bfe000 | 1099 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1100 | if (prop == NULL) { |
57c9fafe AL |
1101 | return; |
1102 | } | |
1103 | ||
1104 | if (!prop->get) { | |
c6bd8c70 | 1105 | error_setg(errp, QERR_PERMISSION_DENIED); |
57c9fafe | 1106 | } else { |
d7bce999 | 1107 | prop->get(obj, v, name, prop->opaque, errp); |
57c9fafe AL |
1108 | } |
1109 | } | |
1110 | ||
1111 | void object_property_set(Object *obj, Visitor *v, const char *name, | |
1112 | Error **errp) | |
1113 | { | |
89bfe000 | 1114 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1115 | if (prop == NULL) { |
57c9fafe AL |
1116 | return; |
1117 | } | |
1118 | ||
1119 | if (!prop->set) { | |
c6bd8c70 | 1120 | error_setg(errp, QERR_PERMISSION_DENIED); |
57c9fafe | 1121 | } else { |
d7bce999 | 1122 | prop->set(obj, v, name, prop->opaque, errp); |
57c9fafe AL |
1123 | } |
1124 | } | |
1125 | ||
7b7b7d18 PB |
1126 | void object_property_set_str(Object *obj, const char *value, |
1127 | const char *name, Error **errp) | |
1128 | { | |
1129 | QString *qstr = qstring_from_str(value); | |
1130 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); | |
1131 | ||
1132 | QDECREF(qstr); | |
1133 | } | |
1134 | ||
1135 | char *object_property_get_str(Object *obj, const char *name, | |
1136 | Error **errp) | |
1137 | { | |
1138 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1139 | QString *qstring; | |
1140 | char *retval; | |
1141 | ||
1142 | if (!ret) { | |
1143 | return NULL; | |
1144 | } | |
7dc847eb | 1145 | qstring = qobject_to(QString, ret); |
7b7b7d18 | 1146 | if (!qstring) { |
c6bd8c70 | 1147 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); |
7b7b7d18 PB |
1148 | retval = NULL; |
1149 | } else { | |
1150 | retval = g_strdup(qstring_get_str(qstring)); | |
1151 | } | |
1152 | ||
560f19f1 | 1153 | qobject_decref(ret); |
7b7b7d18 PB |
1154 | return retval; |
1155 | } | |
1156 | ||
1d9c5a12 PB |
1157 | void object_property_set_link(Object *obj, Object *value, |
1158 | const char *name, Error **errp) | |
1159 | { | |
d3c49316 PC |
1160 | if (value) { |
1161 | gchar *path = object_get_canonical_path(value); | |
1162 | object_property_set_str(obj, path, name, errp); | |
1163 | g_free(path); | |
1164 | } else { | |
1165 | object_property_set_str(obj, "", name, errp); | |
1166 | } | |
1d9c5a12 PB |
1167 | } |
1168 | ||
1169 | Object *object_property_get_link(Object *obj, const char *name, | |
1170 | Error **errp) | |
1171 | { | |
1172 | char *str = object_property_get_str(obj, name, errp); | |
1173 | Object *target = NULL; | |
1174 | ||
1175 | if (str && *str) { | |
1176 | target = object_resolve_path(str, NULL); | |
1177 | if (!target) { | |
75158ebb MA |
1178 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1179 | "Device '%s' not found", str); | |
1d9c5a12 PB |
1180 | } |
1181 | } | |
1182 | ||
1183 | g_free(str); | |
1184 | return target; | |
1185 | } | |
1186 | ||
7b7b7d18 PB |
1187 | void object_property_set_bool(Object *obj, bool value, |
1188 | const char *name, Error **errp) | |
1189 | { | |
fc48ffc3 | 1190 | QBool *qbool = qbool_from_bool(value); |
7b7b7d18 PB |
1191 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); |
1192 | ||
1193 | QDECREF(qbool); | |
1194 | } | |
1195 | ||
1196 | bool object_property_get_bool(Object *obj, const char *name, | |
1197 | Error **errp) | |
1198 | { | |
1199 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1200 | QBool *qbool; | |
1201 | bool retval; | |
1202 | ||
1203 | if (!ret) { | |
1204 | return false; | |
1205 | } | |
7dc847eb | 1206 | qbool = qobject_to(QBool, ret); |
7b7b7d18 | 1207 | if (!qbool) { |
c6bd8c70 | 1208 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); |
7b7b7d18 PB |
1209 | retval = false; |
1210 | } else { | |
fc48ffc3 | 1211 | retval = qbool_get_bool(qbool); |
7b7b7d18 PB |
1212 | } |
1213 | ||
560f19f1 | 1214 | qobject_decref(ret); |
7b7b7d18 PB |
1215 | return retval; |
1216 | } | |
1217 | ||
1218 | void object_property_set_int(Object *obj, int64_t value, | |
1219 | const char *name, Error **errp) | |
1220 | { | |
01b2ffce MAL |
1221 | QNum *qnum = qnum_from_int(value); |
1222 | object_property_set_qobject(obj, QOBJECT(qnum), name, errp); | |
7b7b7d18 | 1223 | |
01b2ffce | 1224 | QDECREF(qnum); |
7b7b7d18 PB |
1225 | } |
1226 | ||
1227 | int64_t object_property_get_int(Object *obj, const char *name, | |
1228 | Error **errp) | |
1229 | { | |
1230 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
01b2ffce | 1231 | QNum *qnum; |
7b7b7d18 PB |
1232 | int64_t retval; |
1233 | ||
1234 | if (!ret) { | |
1235 | return -1; | |
1236 | } | |
01b2ffce | 1237 | |
7dc847eb | 1238 | qnum = qobject_to(QNum, ret); |
01b2ffce | 1239 | if (!qnum || !qnum_get_try_int(qnum, &retval)) { |
c6bd8c70 | 1240 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); |
7b7b7d18 | 1241 | retval = -1; |
7b7b7d18 PB |
1242 | } |
1243 | ||
560f19f1 | 1244 | qobject_decref(ret); |
7b7b7d18 | 1245 | return retval; |
b2cd7dee PB |
1246 | } |
1247 | ||
3152779c MAL |
1248 | void object_property_set_uint(Object *obj, uint64_t value, |
1249 | const char *name, Error **errp) | |
1250 | { | |
1251 | QNum *qnum = qnum_from_uint(value); | |
1252 | ||
1253 | object_property_set_qobject(obj, QOBJECT(qnum), name, errp); | |
1254 | QDECREF(qnum); | |
1255 | } | |
1256 | ||
1257 | uint64_t object_property_get_uint(Object *obj, const char *name, | |
1258 | Error **errp) | |
1259 | { | |
1260 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1261 | QNum *qnum; | |
1262 | uint64_t retval; | |
1263 | ||
1264 | if (!ret) { | |
1265 | return 0; | |
1266 | } | |
7dc847eb | 1267 | qnum = qobject_to(QNum, ret); |
3152779c MAL |
1268 | if (!qnum || !qnum_get_try_uint(qnum, &retval)) { |
1269 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint"); | |
1270 | retval = 0; | |
1271 | } | |
1272 | ||
1273 | qobject_decref(ret); | |
1274 | return retval; | |
1275 | } | |
1276 | ||
a8e3fbed | 1277 | typedef struct EnumProperty { |
f7abe0ec | 1278 | const QEnumLookup *lookup; |
a8e3fbed DB |
1279 | int (*get)(Object *, Error **); |
1280 | void (*set)(Object *, int, Error **); | |
1281 | } EnumProperty; | |
1282 | ||
1f21772d | 1283 | int object_property_get_enum(Object *obj, const char *name, |
a3590dac | 1284 | const char *typename, Error **errp) |
1f21772d | 1285 | { |
4715d42e | 1286 | Error *err = NULL; |
7a0525c7 | 1287 | Visitor *v; |
976620ac | 1288 | char *str; |
1f21772d | 1289 | int ret; |
a3590dac DB |
1290 | ObjectProperty *prop = object_property_find(obj, name, errp); |
1291 | EnumProperty *enumprop; | |
1292 | ||
1293 | if (prop == NULL) { | |
1294 | return 0; | |
1295 | } | |
1296 | ||
1297 | if (!g_str_equal(prop->type, typename)) { | |
1298 | error_setg(errp, "Property %s on %s is not '%s' enum type", | |
1299 | name, object_class_get_name( | |
1300 | object_get_class(obj)), typename); | |
1301 | return 0; | |
1302 | } | |
1303 | ||
1304 | enumprop = prop->opaque; | |
1f21772d | 1305 | |
3b098d56 | 1306 | v = string_output_visitor_new(false, &str); |
e7ca5656 | 1307 | object_property_get(obj, v, name, &err); |
4715d42e MA |
1308 | if (err) { |
1309 | error_propagate(errp, err); | |
e7ca5656 | 1310 | visit_free(v); |
4715d42e MA |
1311 | return 0; |
1312 | } | |
3b098d56 | 1313 | visit_complete(v, &str); |
e7ca5656 | 1314 | visit_free(v); |
7a0525c7 | 1315 | v = string_input_visitor_new(str); |
f7abe0ec | 1316 | visit_type_enum(v, name, &ret, enumprop->lookup, errp); |
976620ac CF |
1317 | |
1318 | g_free(str); | |
7a0525c7 | 1319 | visit_free(v); |
1f21772d HT |
1320 | |
1321 | return ret; | |
1322 | } | |
1323 | ||
1324 | void object_property_get_uint16List(Object *obj, const char *name, | |
1325 | uint16List **list, Error **errp) | |
1326 | { | |
4715d42e | 1327 | Error *err = NULL; |
7a0525c7 | 1328 | Visitor *v; |
976620ac | 1329 | char *str; |
1f21772d | 1330 | |
3b098d56 EB |
1331 | v = string_output_visitor_new(false, &str); |
1332 | object_property_get(obj, v, name, &err); | |
4715d42e MA |
1333 | if (err) { |
1334 | error_propagate(errp, err); | |
1335 | goto out; | |
1336 | } | |
3b098d56 EB |
1337 | visit_complete(v, &str); |
1338 | visit_free(v); | |
7a0525c7 EB |
1339 | v = string_input_visitor_new(str); |
1340 | visit_type_uint16List(v, NULL, list, errp); | |
976620ac CF |
1341 | |
1342 | g_free(str); | |
4715d42e | 1343 | out: |
3b098d56 | 1344 | visit_free(v); |
1f21772d HT |
1345 | } |
1346 | ||
b2cd7dee PB |
1347 | void object_property_parse(Object *obj, const char *string, |
1348 | const char *name, Error **errp) | |
1349 | { | |
7a0525c7 EB |
1350 | Visitor *v = string_input_visitor_new(string); |
1351 | object_property_set(obj, v, name, errp); | |
1352 | visit_free(v); | |
b2cd7dee PB |
1353 | } |
1354 | ||
0b7593e0 | 1355 | char *object_property_print(Object *obj, const char *name, bool human, |
b2cd7dee PB |
1356 | Error **errp) |
1357 | { | |
3b098d56 | 1358 | Visitor *v; |
3a53009f GA |
1359 | char *string = NULL; |
1360 | Error *local_err = NULL; | |
b2cd7dee | 1361 | |
3b098d56 EB |
1362 | v = string_output_visitor_new(human, &string); |
1363 | object_property_get(obj, v, name, &local_err); | |
3a53009f GA |
1364 | if (local_err) { |
1365 | error_propagate(errp, local_err); | |
1366 | goto out; | |
1367 | } | |
1368 | ||
3b098d56 | 1369 | visit_complete(v, &string); |
3a53009f GA |
1370 | |
1371 | out: | |
3b098d56 | 1372 | visit_free(v); |
b2cd7dee | 1373 | return string; |
7b7b7d18 PB |
1374 | } |
1375 | ||
57c9fafe AL |
1376 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
1377 | { | |
89bfe000 | 1378 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1379 | if (prop == NULL) { |
57c9fafe AL |
1380 | return NULL; |
1381 | } | |
1382 | ||
1383 | return prop->type; | |
1384 | } | |
1385 | ||
1386 | Object *object_get_root(void) | |
1387 | { | |
8b45d447 | 1388 | static Object *root; |
57c9fafe | 1389 | |
8b45d447 AL |
1390 | if (!root) { |
1391 | root = object_new("container"); | |
57c9fafe AL |
1392 | } |
1393 | ||
8b45d447 | 1394 | return root; |
57c9fafe AL |
1395 | } |
1396 | ||
bc2256c4 DB |
1397 | Object *object_get_objects_root(void) |
1398 | { | |
1399 | return container_get(object_get_root(), "/objects"); | |
1400 | } | |
1401 | ||
7c47c4ea PX |
1402 | Object *object_get_internal_root(void) |
1403 | { | |
1404 | static Object *internal_root; | |
1405 | ||
1406 | if (!internal_root) { | |
1407 | internal_root = object_new("container"); | |
1408 | } | |
1409 | ||
1410 | return internal_root; | |
1411 | } | |
1412 | ||
d7bce999 EB |
1413 | static void object_get_child_property(Object *obj, Visitor *v, |
1414 | const char *name, void *opaque, | |
1415 | Error **errp) | |
57c9fafe AL |
1416 | { |
1417 | Object *child = opaque; | |
1418 | gchar *path; | |
1419 | ||
1420 | path = object_get_canonical_path(child); | |
51e72bc1 | 1421 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1422 | g_free(path); |
1423 | } | |
1424 | ||
64607d08 PB |
1425 | static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part) |
1426 | { | |
1427 | return opaque; | |
1428 | } | |
1429 | ||
db85b575 AL |
1430 | static void object_finalize_child_property(Object *obj, const char *name, |
1431 | void *opaque) | |
1432 | { | |
1433 | Object *child = opaque; | |
1434 | ||
bffc687d PB |
1435 | if (child->class->unparent) { |
1436 | (child->class->unparent)(child); | |
1437 | } | |
1438 | child->parent = NULL; | |
db85b575 AL |
1439 | object_unref(child); |
1440 | } | |
1441 | ||
57c9fafe AL |
1442 | void object_property_add_child(Object *obj, const char *name, |
1443 | Object *child, Error **errp) | |
1444 | { | |
b0ed5e9f | 1445 | Error *local_err = NULL; |
57c9fafe | 1446 | gchar *type; |
64607d08 | 1447 | ObjectProperty *op; |
57c9fafe | 1448 | |
8faa2f85 PC |
1449 | if (child->parent != NULL) { |
1450 | error_setg(errp, "child object is already parented"); | |
1451 | return; | |
1452 | } | |
1453 | ||
57c9fafe AL |
1454 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
1455 | ||
64607d08 PB |
1456 | op = object_property_add(obj, name, type, object_get_child_property, NULL, |
1457 | object_finalize_child_property, child, &local_err); | |
b0ed5e9f PB |
1458 | if (local_err) { |
1459 | error_propagate(errp, local_err); | |
1460 | goto out; | |
1461 | } | |
64607d08 PB |
1462 | |
1463 | op->resolve = object_resolve_child_property; | |
57c9fafe | 1464 | object_ref(child); |
57c9fafe AL |
1465 | child->parent = obj; |
1466 | ||
b0ed5e9f | 1467 | out: |
57c9fafe AL |
1468 | g_free(type); |
1469 | } | |
1470 | ||
8f5d58ef | 1471 | void object_property_allow_set_link(const Object *obj, const char *name, |
39f72ef9 SH |
1472 | Object *val, Error **errp) |
1473 | { | |
1474 | /* Allow the link to be set, always */ | |
1475 | } | |
1476 | ||
9561fda8 SH |
1477 | typedef struct { |
1478 | Object **child; | |
8f5d58ef | 1479 | void (*check)(const Object *, const char *, Object *, Error **); |
9561fda8 SH |
1480 | ObjectPropertyLinkFlags flags; |
1481 | } LinkProperty; | |
1482 | ||
d7bce999 EB |
1483 | static void object_get_link_property(Object *obj, Visitor *v, |
1484 | const char *name, void *opaque, | |
1485 | Error **errp) | |
57c9fafe | 1486 | { |
9561fda8 SH |
1487 | LinkProperty *lprop = opaque; |
1488 | Object **child = lprop->child; | |
57c9fafe AL |
1489 | gchar *path; |
1490 | ||
1491 | if (*child) { | |
1492 | path = object_get_canonical_path(*child); | |
51e72bc1 | 1493 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1494 | g_free(path); |
1495 | } else { | |
1496 | path = (gchar *)""; | |
51e72bc1 | 1497 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1498 | } |
1499 | } | |
1500 | ||
f5ec6704 SH |
1501 | /* |
1502 | * object_resolve_link: | |
1503 | * | |
1504 | * Lookup an object and ensure its type matches the link property type. This | |
1505 | * is similar to object_resolve_path() except type verification against the | |
1506 | * link property is performed. | |
1507 | * | |
1508 | * Returns: The matched object or NULL on path lookup failures. | |
1509 | */ | |
1510 | static Object *object_resolve_link(Object *obj, const char *name, | |
1511 | const char *path, Error **errp) | |
1512 | { | |
1513 | const char *type; | |
1514 | gchar *target_type; | |
1515 | bool ambiguous = false; | |
1516 | Object *target; | |
1517 | ||
1518 | /* Go from link<FOO> to FOO. */ | |
1519 | type = object_property_get_type(obj, name, NULL); | |
1520 | target_type = g_strndup(&type[5], strlen(type) - 6); | |
1521 | target = object_resolve_path_type(path, target_type, &ambiguous); | |
1522 | ||
1523 | if (ambiguous) { | |
455b0fde EB |
1524 | error_setg(errp, "Path '%s' does not uniquely identify an object", |
1525 | path); | |
f5ec6704 SH |
1526 | } else if (!target) { |
1527 | target = object_resolve_path(path, &ambiguous); | |
1528 | if (target || ambiguous) { | |
c6bd8c70 | 1529 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); |
f5ec6704 | 1530 | } else { |
75158ebb MA |
1531 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1532 | "Device '%s' not found", path); | |
f5ec6704 SH |
1533 | } |
1534 | target = NULL; | |
1535 | } | |
1536 | g_free(target_type); | |
1537 | ||
1538 | return target; | |
1539 | } | |
1540 | ||
d7bce999 EB |
1541 | static void object_set_link_property(Object *obj, Visitor *v, |
1542 | const char *name, void *opaque, | |
1543 | Error **errp) | |
57c9fafe | 1544 | { |
c6aed983 | 1545 | Error *local_err = NULL; |
9561fda8 SH |
1546 | LinkProperty *prop = opaque; |
1547 | Object **child = prop->child; | |
c6aed983 SH |
1548 | Object *old_target = *child; |
1549 | Object *new_target = NULL; | |
1550 | char *path = NULL; | |
57c9fafe | 1551 | |
51e72bc1 | 1552 | visit_type_str(v, name, &path, &local_err); |
57c9fafe | 1553 | |
c6aed983 SH |
1554 | if (!local_err && strcmp(path, "") != 0) { |
1555 | new_target = object_resolve_link(obj, name, path, &local_err); | |
57c9fafe AL |
1556 | } |
1557 | ||
1558 | g_free(path); | |
c6aed983 SH |
1559 | if (local_err) { |
1560 | error_propagate(errp, local_err); | |
1561 | return; | |
1562 | } | |
f0cdc966 | 1563 | |
39f72ef9 SH |
1564 | prop->check(obj, name, new_target, &local_err); |
1565 | if (local_err) { | |
1566 | error_propagate(errp, local_err); | |
1567 | return; | |
1568 | } | |
1569 | ||
8ffad850 | 1570 | object_ref(new_target); |
c6aed983 | 1571 | *child = new_target; |
8ffad850 | 1572 | object_unref(old_target); |
57c9fafe AL |
1573 | } |
1574 | ||
64607d08 PB |
1575 | static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part) |
1576 | { | |
1577 | LinkProperty *lprop = opaque; | |
1578 | ||
1579 | return *lprop->child; | |
1580 | } | |
1581 | ||
9561fda8 SH |
1582 | static void object_release_link_property(Object *obj, const char *name, |
1583 | void *opaque) | |
1584 | { | |
1585 | LinkProperty *prop = opaque; | |
1586 | ||
1587 | if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { | |
1588 | object_unref(*prop->child); | |
1589 | } | |
1590 | g_free(prop); | |
1591 | } | |
1592 | ||
57c9fafe AL |
1593 | void object_property_add_link(Object *obj, const char *name, |
1594 | const char *type, Object **child, | |
8f5d58ef | 1595 | void (*check)(const Object *, const char *, |
39f72ef9 | 1596 | Object *, Error **), |
9561fda8 | 1597 | ObjectPropertyLinkFlags flags, |
57c9fafe AL |
1598 | Error **errp) |
1599 | { | |
9561fda8 SH |
1600 | Error *local_err = NULL; |
1601 | LinkProperty *prop = g_malloc(sizeof(*prop)); | |
57c9fafe | 1602 | gchar *full_type; |
64607d08 | 1603 | ObjectProperty *op; |
57c9fafe | 1604 | |
9561fda8 | 1605 | prop->child = child; |
39f72ef9 | 1606 | prop->check = check; |
9561fda8 SH |
1607 | prop->flags = flags; |
1608 | ||
57c9fafe AL |
1609 | full_type = g_strdup_printf("link<%s>", type); |
1610 | ||
64607d08 PB |
1611 | op = object_property_add(obj, name, full_type, |
1612 | object_get_link_property, | |
1613 | check ? object_set_link_property : NULL, | |
1614 | object_release_link_property, | |
1615 | prop, | |
1616 | &local_err); | |
9561fda8 SH |
1617 | if (local_err) { |
1618 | error_propagate(errp, local_err); | |
1619 | g_free(prop); | |
64607d08 | 1620 | goto out; |
9561fda8 | 1621 | } |
57c9fafe | 1622 | |
64607d08 PB |
1623 | op->resolve = object_resolve_link_property; |
1624 | ||
1625 | out: | |
57c9fafe AL |
1626 | g_free(full_type); |
1627 | } | |
1628 | ||
fb9e7e33 PB |
1629 | void object_property_add_const_link(Object *obj, const char *name, |
1630 | Object *target, Error **errp) | |
1631 | { | |
1632 | char *link_type; | |
1633 | ObjectProperty *op; | |
1634 | ||
1635 | link_type = g_strdup_printf("link<%s>", object_get_typename(target)); | |
1636 | op = object_property_add(obj, name, link_type, | |
1637 | object_get_child_property, NULL, | |
1638 | NULL, target, errp); | |
1639 | if (op != NULL) { | |
1640 | op->resolve = object_resolve_child_property; | |
1641 | } | |
1642 | g_free(link_type); | |
1643 | } | |
1644 | ||
11f590b1 SH |
1645 | gchar *object_get_canonical_path_component(Object *obj) |
1646 | { | |
1647 | ObjectProperty *prop = NULL; | |
b604a854 | 1648 | GHashTableIter iter; |
11f590b1 SH |
1649 | |
1650 | g_assert(obj); | |
1651 | g_assert(obj->parent != NULL); | |
1652 | ||
b604a854 PF |
1653 | g_hash_table_iter_init(&iter, obj->parent->properties); |
1654 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
11f590b1 SH |
1655 | if (!object_property_is_child(prop)) { |
1656 | continue; | |
1657 | } | |
1658 | ||
1659 | if (prop->opaque == obj) { | |
1660 | return g_strdup(prop->name); | |
1661 | } | |
1662 | } | |
1663 | ||
1664 | /* obj had a parent but was not a child, should never happen */ | |
1665 | g_assert_not_reached(); | |
1666 | return NULL; | |
1667 | } | |
1668 | ||
57c9fafe AL |
1669 | gchar *object_get_canonical_path(Object *obj) |
1670 | { | |
1671 | Object *root = object_get_root(); | |
11f590b1 | 1672 | char *newpath, *path = NULL; |
57c9fafe AL |
1673 | |
1674 | while (obj != root) { | |
11f590b1 | 1675 | char *component = object_get_canonical_path_component(obj); |
57c9fafe | 1676 | |
11f590b1 SH |
1677 | if (path) { |
1678 | newpath = g_strdup_printf("%s/%s", component, path); | |
1679 | g_free(component); | |
1680 | g_free(path); | |
1681 | path = newpath; | |
1682 | } else { | |
1683 | path = component; | |
57c9fafe AL |
1684 | } |
1685 | ||
57c9fafe AL |
1686 | obj = obj->parent; |
1687 | } | |
1688 | ||
11f590b1 | 1689 | newpath = g_strdup_printf("/%s", path ? path : ""); |
57c9fafe AL |
1690 | g_free(path); |
1691 | ||
1692 | return newpath; | |
1693 | } | |
1694 | ||
3e84b483 | 1695 | Object *object_resolve_path_component(Object *parent, const gchar *part) |
a612b2a6 | 1696 | { |
89bfe000 | 1697 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
a612b2a6 PB |
1698 | if (prop == NULL) { |
1699 | return NULL; | |
1700 | } | |
1701 | ||
64607d08 PB |
1702 | if (prop->resolve) { |
1703 | return prop->resolve(parent, prop->opaque, part); | |
a612b2a6 PB |
1704 | } else { |
1705 | return NULL; | |
1706 | } | |
1707 | } | |
1708 | ||
57c9fafe AL |
1709 | static Object *object_resolve_abs_path(Object *parent, |
1710 | gchar **parts, | |
02fe2db6 | 1711 | const char *typename, |
57c9fafe AL |
1712 | int index) |
1713 | { | |
57c9fafe AL |
1714 | Object *child; |
1715 | ||
1716 | if (parts[index] == NULL) { | |
02fe2db6 | 1717 | return object_dynamic_cast(parent, typename); |
57c9fafe AL |
1718 | } |
1719 | ||
1720 | if (strcmp(parts[index], "") == 0) { | |
02fe2db6 | 1721 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
57c9fafe AL |
1722 | } |
1723 | ||
a612b2a6 | 1724 | child = object_resolve_path_component(parent, parts[index]); |
57c9fafe AL |
1725 | if (!child) { |
1726 | return NULL; | |
1727 | } | |
1728 | ||
02fe2db6 | 1729 | return object_resolve_abs_path(child, parts, typename, index + 1); |
57c9fafe AL |
1730 | } |
1731 | ||
1732 | static Object *object_resolve_partial_path(Object *parent, | |
1733 | gchar **parts, | |
02fe2db6 | 1734 | const char *typename, |
57c9fafe AL |
1735 | bool *ambiguous) |
1736 | { | |
1737 | Object *obj; | |
b604a854 | 1738 | GHashTableIter iter; |
57c9fafe AL |
1739 | ObjectProperty *prop; |
1740 | ||
02fe2db6 | 1741 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
57c9fafe | 1742 | |
b604a854 PF |
1743 | g_hash_table_iter_init(&iter, parent->properties); |
1744 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
57c9fafe AL |
1745 | Object *found; |
1746 | ||
5d9d3f47 | 1747 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
1748 | continue; |
1749 | } | |
1750 | ||
02fe2db6 PB |
1751 | found = object_resolve_partial_path(prop->opaque, parts, |
1752 | typename, ambiguous); | |
57c9fafe AL |
1753 | if (found) { |
1754 | if (obj) { | |
ebcc479e | 1755 | *ambiguous = true; |
57c9fafe AL |
1756 | return NULL; |
1757 | } | |
1758 | obj = found; | |
1759 | } | |
1760 | ||
ebcc479e | 1761 | if (*ambiguous) { |
57c9fafe AL |
1762 | return NULL; |
1763 | } | |
1764 | } | |
1765 | ||
1766 | return obj; | |
1767 | } | |
1768 | ||
02fe2db6 | 1769 | Object *object_resolve_path_type(const char *path, const char *typename, |
ebcc479e | 1770 | bool *ambiguousp) |
57c9fafe | 1771 | { |
57c9fafe AL |
1772 | Object *obj; |
1773 | gchar **parts; | |
1774 | ||
1775 | parts = g_strsplit(path, "/", 0); | |
2e1103f6 | 1776 | assert(parts); |
57c9fafe | 1777 | |
2e1103f6 | 1778 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
ebcc479e | 1779 | bool ambiguous = false; |
02fe2db6 | 1780 | obj = object_resolve_partial_path(object_get_root(), parts, |
ebcc479e EH |
1781 | typename, &ambiguous); |
1782 | if (ambiguousp) { | |
1783 | *ambiguousp = ambiguous; | |
1784 | } | |
57c9fafe | 1785 | } else { |
02fe2db6 | 1786 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
57c9fafe AL |
1787 | } |
1788 | ||
1789 | g_strfreev(parts); | |
1790 | ||
1791 | return obj; | |
1792 | } | |
1793 | ||
02fe2db6 PB |
1794 | Object *object_resolve_path(const char *path, bool *ambiguous) |
1795 | { | |
1796 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); | |
1797 | } | |
1798 | ||
57c9fafe AL |
1799 | typedef struct StringProperty |
1800 | { | |
1801 | char *(*get)(Object *, Error **); | |
1802 | void (*set)(Object *, const char *, Error **); | |
1803 | } StringProperty; | |
1804 | ||
d7bce999 EB |
1805 | static void property_get_str(Object *obj, Visitor *v, const char *name, |
1806 | void *opaque, Error **errp) | |
57c9fafe AL |
1807 | { |
1808 | StringProperty *prop = opaque; | |
1809 | char *value; | |
e1c8237d | 1810 | Error *err = NULL; |
57c9fafe | 1811 | |
e1c8237d MA |
1812 | value = prop->get(obj, &err); |
1813 | if (err) { | |
1814 | error_propagate(errp, err); | |
1815 | return; | |
57c9fafe | 1816 | } |
e1c8237d | 1817 | |
51e72bc1 | 1818 | visit_type_str(v, name, &value, errp); |
e1c8237d | 1819 | g_free(value); |
57c9fafe AL |
1820 | } |
1821 | ||
d7bce999 EB |
1822 | static void property_set_str(Object *obj, Visitor *v, const char *name, |
1823 | void *opaque, Error **errp) | |
57c9fafe AL |
1824 | { |
1825 | StringProperty *prop = opaque; | |
1826 | char *value; | |
1827 | Error *local_err = NULL; | |
1828 | ||
51e72bc1 | 1829 | visit_type_str(v, name, &value, &local_err); |
57c9fafe AL |
1830 | if (local_err) { |
1831 | error_propagate(errp, local_err); | |
1832 | return; | |
1833 | } | |
1834 | ||
1835 | prop->set(obj, value, errp); | |
1836 | g_free(value); | |
1837 | } | |
1838 | ||
7b7b7d18 PB |
1839 | static void property_release_str(Object *obj, const char *name, |
1840 | void *opaque) | |
57c9fafe AL |
1841 | { |
1842 | StringProperty *prop = opaque; | |
1843 | g_free(prop); | |
1844 | } | |
1845 | ||
1846 | void object_property_add_str(Object *obj, const char *name, | |
1847 | char *(*get)(Object *, Error **), | |
1848 | void (*set)(Object *, const char *, Error **), | |
1849 | Error **errp) | |
1850 | { | |
a01aedc8 | 1851 | Error *local_err = NULL; |
57c9fafe AL |
1852 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
1853 | ||
1854 | prop->get = get; | |
1855 | prop->set = set; | |
1856 | ||
1857 | object_property_add(obj, name, "string", | |
7b7b7d18 PB |
1858 | get ? property_get_str : NULL, |
1859 | set ? property_set_str : NULL, | |
1860 | property_release_str, | |
a01aedc8 SH |
1861 | prop, &local_err); |
1862 | if (local_err) { | |
1863 | error_propagate(errp, local_err); | |
1864 | g_free(prop); | |
1865 | } | |
57c9fafe | 1866 | } |
745549c8 | 1867 | |
16bf7f52 DB |
1868 | void object_class_property_add_str(ObjectClass *klass, const char *name, |
1869 | char *(*get)(Object *, Error **), | |
1870 | void (*set)(Object *, const char *, | |
1871 | Error **), | |
1872 | Error **errp) | |
1873 | { | |
1874 | Error *local_err = NULL; | |
1875 | StringProperty *prop = g_malloc0(sizeof(*prop)); | |
1876 | ||
1877 | prop->get = get; | |
1878 | prop->set = set; | |
1879 | ||
1880 | object_class_property_add(klass, name, "string", | |
1881 | get ? property_get_str : NULL, | |
1882 | set ? property_set_str : NULL, | |
1883 | property_release_str, | |
1884 | prop, &local_err); | |
1885 | if (local_err) { | |
1886 | error_propagate(errp, local_err); | |
1887 | g_free(prop); | |
1888 | } | |
1889 | } | |
1890 | ||
0e558843 AL |
1891 | typedef struct BoolProperty |
1892 | { | |
1893 | bool (*get)(Object *, Error **); | |
1894 | void (*set)(Object *, bool, Error **); | |
1895 | } BoolProperty; | |
1896 | ||
d7bce999 EB |
1897 | static void property_get_bool(Object *obj, Visitor *v, const char *name, |
1898 | void *opaque, Error **errp) | |
0e558843 AL |
1899 | { |
1900 | BoolProperty *prop = opaque; | |
1901 | bool value; | |
4715d42e MA |
1902 | Error *err = NULL; |
1903 | ||
1904 | value = prop->get(obj, &err); | |
1905 | if (err) { | |
1906 | error_propagate(errp, err); | |
1907 | return; | |
1908 | } | |
0e558843 | 1909 | |
51e72bc1 | 1910 | visit_type_bool(v, name, &value, errp); |
0e558843 AL |
1911 | } |
1912 | ||
d7bce999 EB |
1913 | static void property_set_bool(Object *obj, Visitor *v, const char *name, |
1914 | void *opaque, Error **errp) | |
0e558843 AL |
1915 | { |
1916 | BoolProperty *prop = opaque; | |
1917 | bool value; | |
1918 | Error *local_err = NULL; | |
1919 | ||
51e72bc1 | 1920 | visit_type_bool(v, name, &value, &local_err); |
0e558843 AL |
1921 | if (local_err) { |
1922 | error_propagate(errp, local_err); | |
1923 | return; | |
1924 | } | |
1925 | ||
1926 | prop->set(obj, value, errp); | |
1927 | } | |
1928 | ||
1929 | static void property_release_bool(Object *obj, const char *name, | |
1930 | void *opaque) | |
1931 | { | |
1932 | BoolProperty *prop = opaque; | |
1933 | g_free(prop); | |
1934 | } | |
1935 | ||
1936 | void object_property_add_bool(Object *obj, const char *name, | |
1937 | bool (*get)(Object *, Error **), | |
1938 | void (*set)(Object *, bool, Error **), | |
1939 | Error **errp) | |
1940 | { | |
a01aedc8 | 1941 | Error *local_err = NULL; |
0e558843 AL |
1942 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
1943 | ||
1944 | prop->get = get; | |
1945 | prop->set = set; | |
1946 | ||
1947 | object_property_add(obj, name, "bool", | |
1948 | get ? property_get_bool : NULL, | |
1949 | set ? property_set_bool : NULL, | |
1950 | property_release_bool, | |
a01aedc8 SH |
1951 | prop, &local_err); |
1952 | if (local_err) { | |
1953 | error_propagate(errp, local_err); | |
1954 | g_free(prop); | |
1955 | } | |
0e558843 AL |
1956 | } |
1957 | ||
16bf7f52 DB |
1958 | void object_class_property_add_bool(ObjectClass *klass, const char *name, |
1959 | bool (*get)(Object *, Error **), | |
1960 | void (*set)(Object *, bool, Error **), | |
1961 | Error **errp) | |
1962 | { | |
1963 | Error *local_err = NULL; | |
1964 | BoolProperty *prop = g_malloc0(sizeof(*prop)); | |
1965 | ||
1966 | prop->get = get; | |
1967 | prop->set = set; | |
1968 | ||
1969 | object_class_property_add(klass, name, "bool", | |
1970 | get ? property_get_bool : NULL, | |
1971 | set ? property_set_bool : NULL, | |
1972 | property_release_bool, | |
1973 | prop, &local_err); | |
1974 | if (local_err) { | |
1975 | error_propagate(errp, local_err); | |
1976 | g_free(prop); | |
1977 | } | |
1978 | } | |
1979 | ||
d7bce999 EB |
1980 | static void property_get_enum(Object *obj, Visitor *v, const char *name, |
1981 | void *opaque, Error **errp) | |
a8e3fbed DB |
1982 | { |
1983 | EnumProperty *prop = opaque; | |
1984 | int value; | |
4715d42e MA |
1985 | Error *err = NULL; |
1986 | ||
1987 | value = prop->get(obj, &err); | |
1988 | if (err) { | |
1989 | error_propagate(errp, err); | |
1990 | return; | |
1991 | } | |
a8e3fbed | 1992 | |
f7abe0ec | 1993 | visit_type_enum(v, name, &value, prop->lookup, errp); |
a8e3fbed DB |
1994 | } |
1995 | ||
d7bce999 EB |
1996 | static void property_set_enum(Object *obj, Visitor *v, const char *name, |
1997 | void *opaque, Error **errp) | |
a8e3fbed DB |
1998 | { |
1999 | EnumProperty *prop = opaque; | |
2000 | int value; | |
4715d42e | 2001 | Error *err = NULL; |
a8e3fbed | 2002 | |
f7abe0ec | 2003 | visit_type_enum(v, name, &value, prop->lookup, &err); |
4715d42e MA |
2004 | if (err) { |
2005 | error_propagate(errp, err); | |
2006 | return; | |
2007 | } | |
a8e3fbed DB |
2008 | prop->set(obj, value, errp); |
2009 | } | |
2010 | ||
2011 | static void property_release_enum(Object *obj, const char *name, | |
2012 | void *opaque) | |
2013 | { | |
2014 | EnumProperty *prop = opaque; | |
2015 | g_free(prop); | |
2016 | } | |
2017 | ||
2018 | void object_property_add_enum(Object *obj, const char *name, | |
2019 | const char *typename, | |
f7abe0ec | 2020 | const QEnumLookup *lookup, |
a8e3fbed DB |
2021 | int (*get)(Object *, Error **), |
2022 | void (*set)(Object *, int, Error **), | |
2023 | Error **errp) | |
2024 | { | |
2025 | Error *local_err = NULL; | |
2026 | EnumProperty *prop = g_malloc(sizeof(*prop)); | |
2027 | ||
f7abe0ec | 2028 | prop->lookup = lookup; |
a8e3fbed DB |
2029 | prop->get = get; |
2030 | prop->set = set; | |
2031 | ||
2032 | object_property_add(obj, name, typename, | |
2033 | get ? property_get_enum : NULL, | |
2034 | set ? property_set_enum : NULL, | |
2035 | property_release_enum, | |
2036 | prop, &local_err); | |
2037 | if (local_err) { | |
2038 | error_propagate(errp, local_err); | |
2039 | g_free(prop); | |
2040 | } | |
2041 | } | |
2042 | ||
16bf7f52 DB |
2043 | void object_class_property_add_enum(ObjectClass *klass, const char *name, |
2044 | const char *typename, | |
f7abe0ec | 2045 | const QEnumLookup *lookup, |
16bf7f52 DB |
2046 | int (*get)(Object *, Error **), |
2047 | void (*set)(Object *, int, Error **), | |
2048 | Error **errp) | |
2049 | { | |
2050 | Error *local_err = NULL; | |
2051 | EnumProperty *prop = g_malloc(sizeof(*prop)); | |
2052 | ||
f7abe0ec | 2053 | prop->lookup = lookup; |
16bf7f52 DB |
2054 | prop->get = get; |
2055 | prop->set = set; | |
2056 | ||
2057 | object_class_property_add(klass, name, typename, | |
2058 | get ? property_get_enum : NULL, | |
2059 | set ? property_set_enum : NULL, | |
2060 | property_release_enum, | |
2061 | prop, &local_err); | |
2062 | if (local_err) { | |
2063 | error_propagate(errp, local_err); | |
2064 | g_free(prop); | |
2065 | } | |
2066 | } | |
2067 | ||
8e099d14 DG |
2068 | typedef struct TMProperty { |
2069 | void (*get)(Object *, struct tm *, Error **); | |
2070 | } TMProperty; | |
2071 | ||
d7bce999 EB |
2072 | static void property_get_tm(Object *obj, Visitor *v, const char *name, |
2073 | void *opaque, Error **errp) | |
8e099d14 DG |
2074 | { |
2075 | TMProperty *prop = opaque; | |
2076 | Error *err = NULL; | |
2077 | struct tm value; | |
2078 | ||
2079 | prop->get(obj, &value, &err); | |
2080 | if (err) { | |
2081 | goto out; | |
2082 | } | |
2083 | ||
337283df | 2084 | visit_start_struct(v, name, NULL, 0, &err); |
8e099d14 DG |
2085 | if (err) { |
2086 | goto out; | |
2087 | } | |
51e72bc1 | 2088 | visit_type_int32(v, "tm_year", &value.tm_year, &err); |
8e099d14 DG |
2089 | if (err) { |
2090 | goto out_end; | |
2091 | } | |
51e72bc1 | 2092 | visit_type_int32(v, "tm_mon", &value.tm_mon, &err); |
8e099d14 DG |
2093 | if (err) { |
2094 | goto out_end; | |
2095 | } | |
51e72bc1 | 2096 | visit_type_int32(v, "tm_mday", &value.tm_mday, &err); |
8e099d14 DG |
2097 | if (err) { |
2098 | goto out_end; | |
2099 | } | |
51e72bc1 | 2100 | visit_type_int32(v, "tm_hour", &value.tm_hour, &err); |
8e099d14 DG |
2101 | if (err) { |
2102 | goto out_end; | |
2103 | } | |
51e72bc1 | 2104 | visit_type_int32(v, "tm_min", &value.tm_min, &err); |
8e099d14 DG |
2105 | if (err) { |
2106 | goto out_end; | |
2107 | } | |
51e72bc1 | 2108 | visit_type_int32(v, "tm_sec", &value.tm_sec, &err); |
8e099d14 DG |
2109 | if (err) { |
2110 | goto out_end; | |
2111 | } | |
15c2f669 | 2112 | visit_check_struct(v, &err); |
8e099d14 | 2113 | out_end: |
1158bb2a | 2114 | visit_end_struct(v, NULL); |
8e099d14 DG |
2115 | out: |
2116 | error_propagate(errp, err); | |
2117 | ||
2118 | } | |
2119 | ||
2120 | static void property_release_tm(Object *obj, const char *name, | |
2121 | void *opaque) | |
2122 | { | |
2123 | TMProperty *prop = opaque; | |
2124 | g_free(prop); | |
2125 | } | |
2126 | ||
2127 | void object_property_add_tm(Object *obj, const char *name, | |
2128 | void (*get)(Object *, struct tm *, Error **), | |
2129 | Error **errp) | |
2130 | { | |
2131 | Error *local_err = NULL; | |
2132 | TMProperty *prop = g_malloc0(sizeof(*prop)); | |
2133 | ||
2134 | prop->get = get; | |
2135 | ||
2136 | object_property_add(obj, name, "struct tm", | |
2137 | get ? property_get_tm : NULL, NULL, | |
2138 | property_release_tm, | |
2139 | prop, &local_err); | |
2140 | if (local_err) { | |
2141 | error_propagate(errp, local_err); | |
2142 | g_free(prop); | |
2143 | } | |
2144 | } | |
2145 | ||
16bf7f52 DB |
2146 | void object_class_property_add_tm(ObjectClass *klass, const char *name, |
2147 | void (*get)(Object *, struct tm *, Error **), | |
2148 | Error **errp) | |
2149 | { | |
2150 | Error *local_err = NULL; | |
2151 | TMProperty *prop = g_malloc0(sizeof(*prop)); | |
2152 | ||
2153 | prop->get = get; | |
2154 | ||
2155 | object_class_property_add(klass, name, "struct tm", | |
2156 | get ? property_get_tm : NULL, NULL, | |
2157 | property_release_tm, | |
2158 | prop, &local_err); | |
2159 | if (local_err) { | |
2160 | error_propagate(errp, local_err); | |
2161 | g_free(prop); | |
2162 | } | |
2163 | } | |
2164 | ||
2f262e06 PB |
2165 | static char *qdev_get_type(Object *obj, Error **errp) |
2166 | { | |
2167 | return g_strdup(object_get_typename(obj)); | |
2168 | } | |
2169 | ||
d7bce999 EB |
2170 | static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name, |
2171 | void *opaque, Error **errp) | |
e732ea63 MT |
2172 | { |
2173 | uint8_t value = *(uint8_t *)opaque; | |
51e72bc1 | 2174 | visit_type_uint8(v, name, &value, errp); |
e732ea63 MT |
2175 | } |
2176 | ||
d7bce999 EB |
2177 | static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name, |
2178 | void *opaque, Error **errp) | |
e732ea63 MT |
2179 | { |
2180 | uint16_t value = *(uint16_t *)opaque; | |
51e72bc1 | 2181 | visit_type_uint16(v, name, &value, errp); |
e732ea63 MT |
2182 | } |
2183 | ||
d7bce999 EB |
2184 | static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name, |
2185 | void *opaque, Error **errp) | |
e732ea63 MT |
2186 | { |
2187 | uint32_t value = *(uint32_t *)opaque; | |
51e72bc1 | 2188 | visit_type_uint32(v, name, &value, errp); |
e732ea63 MT |
2189 | } |
2190 | ||
d7bce999 EB |
2191 | static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name, |
2192 | void *opaque, Error **errp) | |
e732ea63 MT |
2193 | { |
2194 | uint64_t value = *(uint64_t *)opaque; | |
51e72bc1 | 2195 | visit_type_uint64(v, name, &value, errp); |
e732ea63 MT |
2196 | } |
2197 | ||
2198 | void object_property_add_uint8_ptr(Object *obj, const char *name, | |
2199 | const uint8_t *v, Error **errp) | |
2200 | { | |
2201 | object_property_add(obj, name, "uint8", property_get_uint8_ptr, | |
2202 | NULL, NULL, (void *)v, errp); | |
2203 | } | |
2204 | ||
16bf7f52 DB |
2205 | void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name, |
2206 | const uint8_t *v, Error **errp) | |
2207 | { | |
2208 | object_class_property_add(klass, name, "uint8", property_get_uint8_ptr, | |
2209 | NULL, NULL, (void *)v, errp); | |
2210 | } | |
2211 | ||
e732ea63 MT |
2212 | void object_property_add_uint16_ptr(Object *obj, const char *name, |
2213 | const uint16_t *v, Error **errp) | |
2214 | { | |
2215 | object_property_add(obj, name, "uint16", property_get_uint16_ptr, | |
2216 | NULL, NULL, (void *)v, errp); | |
2217 | } | |
2218 | ||
16bf7f52 DB |
2219 | void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name, |
2220 | const uint16_t *v, Error **errp) | |
2221 | { | |
2222 | object_class_property_add(klass, name, "uint16", property_get_uint16_ptr, | |
2223 | NULL, NULL, (void *)v, errp); | |
2224 | } | |
2225 | ||
e732ea63 MT |
2226 | void object_property_add_uint32_ptr(Object *obj, const char *name, |
2227 | const uint32_t *v, Error **errp) | |
2228 | { | |
2229 | object_property_add(obj, name, "uint32", property_get_uint32_ptr, | |
2230 | NULL, NULL, (void *)v, errp); | |
2231 | } | |
2232 | ||
16bf7f52 DB |
2233 | void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name, |
2234 | const uint32_t *v, Error **errp) | |
2235 | { | |
2236 | object_class_property_add(klass, name, "uint32", property_get_uint32_ptr, | |
2237 | NULL, NULL, (void *)v, errp); | |
2238 | } | |
2239 | ||
e732ea63 MT |
2240 | void object_property_add_uint64_ptr(Object *obj, const char *name, |
2241 | const uint64_t *v, Error **errp) | |
2242 | { | |
2243 | object_property_add(obj, name, "uint64", property_get_uint64_ptr, | |
2244 | NULL, NULL, (void *)v, errp); | |
2245 | } | |
2246 | ||
16bf7f52 DB |
2247 | void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name, |
2248 | const uint64_t *v, Error **errp) | |
2249 | { | |
2250 | object_class_property_add(klass, name, "uint64", property_get_uint64_ptr, | |
2251 | NULL, NULL, (void *)v, errp); | |
2252 | } | |
2253 | ||
ef7c7ff6 SH |
2254 | typedef struct { |
2255 | Object *target_obj; | |
1590d266 | 2256 | char *target_name; |
ef7c7ff6 SH |
2257 | } AliasProperty; |
2258 | ||
d7bce999 EB |
2259 | static void property_get_alias(Object *obj, Visitor *v, const char *name, |
2260 | void *opaque, Error **errp) | |
ef7c7ff6 SH |
2261 | { |
2262 | AliasProperty *prop = opaque; | |
2263 | ||
2264 | object_property_get(prop->target_obj, v, prop->target_name, errp); | |
2265 | } | |
2266 | ||
d7bce999 EB |
2267 | static void property_set_alias(Object *obj, Visitor *v, const char *name, |
2268 | void *opaque, Error **errp) | |
ef7c7ff6 SH |
2269 | { |
2270 | AliasProperty *prop = opaque; | |
2271 | ||
2272 | object_property_set(prop->target_obj, v, prop->target_name, errp); | |
2273 | } | |
2274 | ||
64607d08 PB |
2275 | static Object *property_resolve_alias(Object *obj, void *opaque, |
2276 | const gchar *part) | |
2277 | { | |
2278 | AliasProperty *prop = opaque; | |
2279 | ||
2280 | return object_resolve_path_component(prop->target_obj, prop->target_name); | |
2281 | } | |
2282 | ||
ef7c7ff6 SH |
2283 | static void property_release_alias(Object *obj, const char *name, void *opaque) |
2284 | { | |
2285 | AliasProperty *prop = opaque; | |
2286 | ||
1590d266 | 2287 | g_free(prop->target_name); |
ef7c7ff6 SH |
2288 | g_free(prop); |
2289 | } | |
2290 | ||
2291 | void object_property_add_alias(Object *obj, const char *name, | |
2292 | Object *target_obj, const char *target_name, | |
2293 | Error **errp) | |
2294 | { | |
2295 | AliasProperty *prop; | |
64607d08 | 2296 | ObjectProperty *op; |
ef7c7ff6 | 2297 | ObjectProperty *target_prop; |
d190698e | 2298 | gchar *prop_type; |
8ae9a9ef | 2299 | Error *local_err = NULL; |
ef7c7ff6 SH |
2300 | |
2301 | target_prop = object_property_find(target_obj, target_name, errp); | |
2302 | if (!target_prop) { | |
2303 | return; | |
2304 | } | |
2305 | ||
d190698e PB |
2306 | if (object_property_is_child(target_prop)) { |
2307 | prop_type = g_strdup_printf("link%s", | |
2308 | target_prop->type + strlen("child")); | |
2309 | } else { | |
2310 | prop_type = g_strdup(target_prop->type); | |
2311 | } | |
2312 | ||
ef7c7ff6 SH |
2313 | prop = g_malloc(sizeof(*prop)); |
2314 | prop->target_obj = target_obj; | |
1590d266 | 2315 | prop->target_name = g_strdup(target_name); |
ef7c7ff6 | 2316 | |
d190698e | 2317 | op = object_property_add(obj, name, prop_type, |
64607d08 PB |
2318 | property_get_alias, |
2319 | property_set_alias, | |
2320 | property_release_alias, | |
8ae9a9ef GA |
2321 | prop, &local_err); |
2322 | if (local_err) { | |
2323 | error_propagate(errp, local_err); | |
2324 | g_free(prop); | |
2325 | goto out; | |
2326 | } | |
64607d08 | 2327 | op->resolve = property_resolve_alias; |
d190698e | 2328 | |
a18bb417 | 2329 | object_property_set_description(obj, op->name, |
80742642 GA |
2330 | target_prop->description, |
2331 | &error_abort); | |
2332 | ||
8ae9a9ef | 2333 | out: |
d190698e | 2334 | g_free(prop_type); |
ef7c7ff6 SH |
2335 | } |
2336 | ||
80742642 GA |
2337 | void object_property_set_description(Object *obj, const char *name, |
2338 | const char *description, Error **errp) | |
2339 | { | |
2340 | ObjectProperty *op; | |
2341 | ||
2342 | op = object_property_find(obj, name, errp); | |
2343 | if (!op) { | |
2344 | return; | |
2345 | } | |
2346 | ||
2347 | g_free(op->description); | |
2348 | op->description = g_strdup(description); | |
2349 | } | |
2350 | ||
16bf7f52 DB |
2351 | void object_class_property_set_description(ObjectClass *klass, |
2352 | const char *name, | |
2353 | const char *description, | |
2354 | Error **errp) | |
2355 | { | |
2356 | ObjectProperty *op; | |
2357 | ||
2358 | op = g_hash_table_lookup(klass->properties, name); | |
2359 | if (!op) { | |
2360 | error_setg(errp, "Property '.%s' not found", name); | |
2361 | return; | |
2362 | } | |
2363 | ||
2364 | g_free(op->description); | |
2365 | op->description = g_strdup(description); | |
2366 | } | |
2367 | ||
2f262e06 PB |
2368 | static void object_instance_init(Object *obj) |
2369 | { | |
2370 | object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); | |
2371 | } | |
2372 | ||
745549c8 PB |
2373 | static void register_types(void) |
2374 | { | |
2375 | static TypeInfo interface_info = { | |
2376 | .name = TYPE_INTERFACE, | |
33e95c63 | 2377 | .class_size = sizeof(InterfaceClass), |
745549c8 PB |
2378 | .abstract = true, |
2379 | }; | |
2380 | ||
2381 | static TypeInfo object_info = { | |
2382 | .name = TYPE_OBJECT, | |
2383 | .instance_size = sizeof(Object), | |
2f262e06 | 2384 | .instance_init = object_instance_init, |
745549c8 PB |
2385 | .abstract = true, |
2386 | }; | |
2387 | ||
049cb3cf PB |
2388 | type_interface = type_register_internal(&interface_info); |
2389 | type_register_internal(&object_info); | |
745549c8 PB |
2390 | } |
2391 | ||
2392 | type_init(register_types) |