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