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