]>
Commit | Line | Data |
---|---|---|
2f28d2ff AL |
1 | /* |
2 | * QEMU Object Model | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
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 | ||
13 | #include "qemu/object.h" | |
14 | #include "qemu-common.h" | |
57c9fafe | 15 | #include "qapi/qapi-visit-core.h" |
b2cd7dee PB |
16 | #include "qapi/string-input-visitor.h" |
17 | #include "qapi/string-output-visitor.h" | |
2f28d2ff | 18 | |
7b7b7d18 PB |
19 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
20 | * of the QOM core on QObject? */ | |
21 | #include "qemu/qom-qobject.h" | |
22 | #include "qobject.h" | |
23 | #include "qbool.h" | |
24 | #include "qint.h" | |
25 | #include "qstring.h" | |
26 | ||
2f28d2ff AL |
27 | #define MAX_INTERFACES 32 |
28 | ||
29 | typedef struct InterfaceImpl InterfaceImpl; | |
30 | typedef struct TypeImpl TypeImpl; | |
31 | ||
32 | struct InterfaceImpl | |
33 | { | |
34 | const char *parent; | |
35 | void (*interface_initfn)(ObjectClass *class, void *data); | |
36 | TypeImpl *type; | |
37 | }; | |
38 | ||
39 | struct TypeImpl | |
40 | { | |
41 | const char *name; | |
42 | ||
43 | size_t class_size; | |
44 | ||
45 | size_t instance_size; | |
46 | ||
47 | void (*class_init)(ObjectClass *klass, void *data); | |
3b50e311 | 48 | void (*class_base_init)(ObjectClass *klass, void *data); |
2f28d2ff AL |
49 | void (*class_finalize)(ObjectClass *klass, void *data); |
50 | ||
51 | void *class_data; | |
52 | ||
53 | void (*instance_init)(Object *obj); | |
54 | void (*instance_finalize)(Object *obj); | |
55 | ||
56 | bool abstract; | |
57 | ||
58 | const char *parent; | |
59 | TypeImpl *parent_type; | |
60 | ||
61 | ObjectClass *class; | |
62 | ||
63 | int num_interfaces; | |
64 | InterfaceImpl interfaces[MAX_INTERFACES]; | |
65 | }; | |
66 | ||
67 | typedef struct Interface | |
68 | { | |
69 | Object parent; | |
70 | Object *obj; | |
71 | } Interface; | |
72 | ||
73 | #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE) | |
74 | ||
9970bd88 PB |
75 | static Type type_interface; |
76 | ||
2f28d2ff AL |
77 | static GHashTable *type_table_get(void) |
78 | { | |
79 | static GHashTable *type_table; | |
80 | ||
81 | if (type_table == NULL) { | |
82 | type_table = g_hash_table_new(g_str_hash, g_str_equal); | |
83 | } | |
84 | ||
85 | return type_table; | |
86 | } | |
87 | ||
88 | static void type_table_add(TypeImpl *ti) | |
89 | { | |
90 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); | |
91 | } | |
92 | ||
93 | static TypeImpl *type_table_lookup(const char *name) | |
94 | { | |
95 | return g_hash_table_lookup(type_table_get(), name); | |
96 | } | |
97 | ||
98 | TypeImpl *type_register(const TypeInfo *info) | |
99 | { | |
100 | TypeImpl *ti = g_malloc0(sizeof(*ti)); | |
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; | |
121 | ti->instance_finalize = info->instance_finalize; | |
122 | ||
123 | ti->abstract = info->abstract; | |
124 | ||
125 | if (info->interfaces) { | |
126 | int i; | |
127 | ||
128 | for (i = 0; info->interfaces[i].type; i++) { | |
129 | ti->interfaces[i].parent = info->interfaces[i].type; | |
130 | ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn; | |
131 | ti->num_interfaces++; | |
132 | } | |
133 | } | |
134 | ||
135 | type_table_add(ti); | |
136 | ||
137 | return ti; | |
138 | } | |
139 | ||
140 | TypeImpl *type_register_static(const TypeInfo *info) | |
141 | { | |
142 | return type_register(info); | |
143 | } | |
144 | ||
145 | static TypeImpl *type_get_by_name(const char *name) | |
146 | { | |
147 | if (name == NULL) { | |
148 | return NULL; | |
149 | } | |
150 | ||
151 | return type_table_lookup(name); | |
152 | } | |
153 | ||
154 | static TypeImpl *type_get_parent(TypeImpl *type) | |
155 | { | |
156 | if (!type->parent_type && type->parent) { | |
157 | type->parent_type = type_get_by_name(type->parent); | |
158 | g_assert(type->parent_type != NULL); | |
159 | } | |
160 | ||
161 | return type->parent_type; | |
162 | } | |
163 | ||
164 | static bool type_has_parent(TypeImpl *type) | |
165 | { | |
166 | return (type->parent != NULL); | |
167 | } | |
168 | ||
169 | static size_t type_class_get_size(TypeImpl *ti) | |
170 | { | |
171 | if (ti->class_size) { | |
172 | return ti->class_size; | |
173 | } | |
174 | ||
175 | if (type_has_parent(ti)) { | |
176 | return type_class_get_size(type_get_parent(ti)); | |
177 | } | |
178 | ||
179 | return sizeof(ObjectClass); | |
180 | } | |
181 | ||
aca59af6 IM |
182 | static size_t type_object_get_size(TypeImpl *ti) |
183 | { | |
184 | if (ti->instance_size) { | |
185 | return ti->instance_size; | |
186 | } | |
187 | ||
188 | if (type_has_parent(ti)) { | |
189 | return type_object_get_size(type_get_parent(ti)); | |
190 | } | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
2f28d2ff AL |
195 | static void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface) |
196 | { | |
197 | TypeInfo info = { | |
198 | .instance_size = sizeof(Interface), | |
199 | .parent = iface->parent, | |
200 | .class_size = sizeof(InterfaceClass), | |
201 | .class_init = iface->interface_initfn, | |
202 | .abstract = true, | |
203 | }; | |
204 | char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent); | |
205 | ||
206 | info.name = name; | |
207 | iface->type = type_register(&info); | |
208 | g_free(name); | |
209 | } | |
210 | ||
ac451033 | 211 | static void type_initialize(TypeImpl *ti) |
2f28d2ff AL |
212 | { |
213 | size_t class_size = sizeof(ObjectClass); | |
214 | int i; | |
215 | ||
216 | if (ti->class) { | |
217 | return; | |
218 | } | |
219 | ||
220 | ti->class_size = type_class_get_size(ti); | |
aca59af6 | 221 | ti->instance_size = type_object_get_size(ti); |
2f28d2ff AL |
222 | |
223 | ti->class = g_malloc0(ti->class_size); | |
224 | ti->class->type = ti; | |
225 | ||
226 | if (type_has_parent(ti)) { | |
227 | TypeImpl *parent = type_get_parent(ti); | |
228 | ||
ac451033 | 229 | type_initialize(parent); |
2f28d2ff AL |
230 | |
231 | class_size = parent->class_size; | |
232 | g_assert(parent->class_size <= ti->class_size); | |
233 | ||
234 | memcpy((void *)ti->class + sizeof(ObjectClass), | |
235 | (void *)parent->class + sizeof(ObjectClass), | |
236 | parent->class_size - sizeof(ObjectClass)); | |
3b50e311 PB |
237 | |
238 | while (parent) { | |
239 | if (parent->class_base_init) { | |
240 | parent->class_base_init(ti->class, ti->class_data); | |
241 | } | |
242 | parent = type_get_parent(parent); | |
243 | } | |
2f28d2ff AL |
244 | } |
245 | ||
246 | memset((void *)ti->class + class_size, 0, ti->class_size - class_size); | |
247 | ||
248 | for (i = 0; i < ti->num_interfaces; i++) { | |
249 | type_class_interface_init(ti, &ti->interfaces[i]); | |
250 | } | |
251 | ||
252 | if (ti->class_init) { | |
253 | ti->class_init(ti->class, ti->class_data); | |
254 | } | |
255 | } | |
256 | ||
257 | static void object_interface_init(Object *obj, InterfaceImpl *iface) | |
258 | { | |
259 | TypeImpl *ti = iface->type; | |
260 | Interface *iface_obj; | |
261 | ||
262 | iface_obj = INTERFACE(object_new(ti->name)); | |
263 | iface_obj->obj = obj; | |
264 | ||
265 | obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj); | |
266 | } | |
267 | ||
268 | static void object_init_with_type(Object *obj, TypeImpl *ti) | |
269 | { | |
270 | int i; | |
271 | ||
272 | if (type_has_parent(ti)) { | |
273 | object_init_with_type(obj, type_get_parent(ti)); | |
274 | } | |
275 | ||
276 | for (i = 0; i < ti->num_interfaces; i++) { | |
277 | object_interface_init(obj, &ti->interfaces[i]); | |
278 | } | |
279 | ||
280 | if (ti->instance_init) { | |
281 | ti->instance_init(obj); | |
282 | } | |
283 | } | |
284 | ||
285 | void object_initialize_with_type(void *data, TypeImpl *type) | |
286 | { | |
287 | Object *obj = data; | |
288 | ||
289 | g_assert(type != NULL); | |
ac451033 | 290 | type_initialize(type); |
aca59af6 IM |
291 | |
292 | g_assert(type->instance_size >= sizeof(Object)); | |
2f28d2ff AL |
293 | g_assert(type->abstract == false); |
294 | ||
295 | memset(obj, 0, type->instance_size); | |
296 | obj->class = type->class; | |
57c9fafe | 297 | QTAILQ_INIT(&obj->properties); |
2f28d2ff AL |
298 | object_init_with_type(obj, type); |
299 | } | |
300 | ||
301 | void object_initialize(void *data, const char *typename) | |
302 | { | |
303 | TypeImpl *type = type_get_by_name(typename); | |
304 | ||
305 | object_initialize_with_type(data, type); | |
306 | } | |
307 | ||
5d9d3f47 AF |
308 | static inline bool object_property_is_child(ObjectProperty *prop) |
309 | { | |
310 | return strstart(prop->type, "child<", NULL); | |
311 | } | |
312 | ||
313 | static inline bool object_property_is_link(ObjectProperty *prop) | |
314 | { | |
315 | return strstart(prop->type, "link<", NULL); | |
316 | } | |
317 | ||
57c9fafe AL |
318 | static void object_property_del_all(Object *obj) |
319 | { | |
320 | while (!QTAILQ_EMPTY(&obj->properties)) { | |
321 | ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); | |
322 | ||
323 | QTAILQ_REMOVE(&obj->properties, prop, node); | |
324 | ||
325 | if (prop->release) { | |
326 | prop->release(obj, prop->name, prop->opaque); | |
327 | } | |
328 | ||
329 | g_free(prop->name); | |
330 | g_free(prop->type); | |
331 | g_free(prop); | |
332 | } | |
333 | } | |
334 | ||
335 | static void object_property_del_child(Object *obj, Object *child, Error **errp) | |
336 | { | |
337 | ObjectProperty *prop; | |
338 | ||
339 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
5d9d3f47 | 340 | if (object_property_is_child(prop) && prop->opaque == child) { |
57c9fafe | 341 | object_property_del(obj, prop->name, errp); |
6c1fdcf9 | 342 | break; |
57c9fafe AL |
343 | } |
344 | } | |
345 | } | |
346 | ||
347 | void object_unparent(Object *obj) | |
348 | { | |
349 | if (obj->parent) { | |
350 | object_property_del_child(obj->parent, obj, NULL); | |
351 | } | |
352 | } | |
353 | ||
2f28d2ff AL |
354 | static void object_deinit(Object *obj, TypeImpl *type) |
355 | { | |
356 | if (type->instance_finalize) { | |
357 | type->instance_finalize(obj); | |
358 | } | |
359 | ||
360 | while (obj->interfaces) { | |
361 | Interface *iface_obj = obj->interfaces->data; | |
362 | obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces); | |
363 | object_delete(OBJECT(iface_obj)); | |
364 | } | |
365 | ||
366 | if (type_has_parent(type)) { | |
367 | object_deinit(obj, type_get_parent(type)); | |
368 | } | |
57c9fafe AL |
369 | |
370 | object_unparent(obj); | |
2f28d2ff AL |
371 | } |
372 | ||
373 | void object_finalize(void *data) | |
374 | { | |
375 | Object *obj = data; | |
376 | TypeImpl *ti = obj->class->type; | |
377 | ||
378 | object_deinit(obj, ti); | |
57c9fafe | 379 | object_property_del_all(obj); |
db85b575 AL |
380 | |
381 | g_assert(obj->ref == 0); | |
2f28d2ff AL |
382 | } |
383 | ||
384 | Object *object_new_with_type(Type type) | |
385 | { | |
386 | Object *obj; | |
387 | ||
388 | g_assert(type != NULL); | |
ac451033 | 389 | type_initialize(type); |
2f28d2ff AL |
390 | |
391 | obj = g_malloc(type->instance_size); | |
392 | object_initialize_with_type(obj, type); | |
db85b575 | 393 | object_ref(obj); |
2f28d2ff AL |
394 | |
395 | return obj; | |
396 | } | |
397 | ||
398 | Object *object_new(const char *typename) | |
399 | { | |
400 | TypeImpl *ti = type_get_by_name(typename); | |
401 | ||
402 | return object_new_with_type(ti); | |
403 | } | |
404 | ||
405 | void object_delete(Object *obj) | |
406 | { | |
db85b575 AL |
407 | object_unref(obj); |
408 | g_assert(obj->ref == 0); | |
2f28d2ff AL |
409 | g_free(obj); |
410 | } | |
411 | ||
acc4af3f | 412 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
2f28d2ff | 413 | { |
acc4af3f | 414 | assert(target_type); |
2f28d2ff AL |
415 | |
416 | /* Check if typename is a direct ancestor of type */ | |
417 | while (type) { | |
418 | if (type == target_type) { | |
419 | return true; | |
420 | } | |
421 | ||
422 | type = type_get_parent(type); | |
423 | } | |
424 | ||
acc4af3f PB |
425 | return false; |
426 | } | |
2f28d2ff | 427 | |
9970bd88 | 428 | static bool object_is_type(Object *obj, TypeImpl *target_type) |
acc4af3f | 429 | { |
9970bd88 | 430 | return !target_type || type_is_ancestor(obj->class->type, target_type); |
2f28d2ff AL |
431 | } |
432 | ||
433 | Object *object_dynamic_cast(Object *obj, const char *typename) | |
434 | { | |
9970bd88 | 435 | TypeImpl *target_type = type_get_by_name(typename); |
2f28d2ff AL |
436 | GSList *i; |
437 | ||
acc4af3f PB |
438 | /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT, |
439 | * we want to go back from interfaces to the parent. | |
440 | */ | |
9970bd88 | 441 | if (target_type && object_is_type(obj, target_type)) { |
acc4af3f PB |
442 | return obj; |
443 | } | |
444 | ||
445 | /* Check if obj is an interface and its containing object is a direct | |
446 | * ancestor of typename. In principle we could do this test at the very | |
447 | * beginning of object_dynamic_cast, avoiding a second call to | |
448 | * object_is_type. However, casting between interfaces is relatively | |
9970bd88 | 449 | * rare, and object_is_type(obj, type_interface) would fail almost always. |
acc4af3f PB |
450 | * |
451 | * Perhaps we could add a magic value to the object header for increased | |
452 | * (run-time) type safety and to speed up tests like this one. If we ever | |
453 | * do that we can revisit the order here. | |
454 | */ | |
9970bd88 | 455 | if (object_is_type(obj, type_interface)) { |
acc4af3f PB |
456 | assert(!obj->interfaces); |
457 | obj = INTERFACE(obj)->obj; | |
9970bd88 | 458 | if (object_is_type(obj, target_type)) { |
acc4af3f PB |
459 | return obj; |
460 | } | |
461 | } | |
462 | ||
9970bd88 | 463 | if (!target_type) { |
2f28d2ff AL |
464 | return obj; |
465 | } | |
466 | ||
467 | /* Check if obj has an interface of typename */ | |
468 | for (i = obj->interfaces; i; i = i->next) { | |
469 | Interface *iface = i->data; | |
470 | ||
9970bd88 | 471 | if (object_is_type(OBJECT(iface), target_type)) { |
2f28d2ff AL |
472 | return OBJECT(iface); |
473 | } | |
474 | } | |
475 | ||
2f28d2ff AL |
476 | return NULL; |
477 | } | |
478 | ||
479 | ||
83f7d43a | 480 | static void register_types(void) |
2f28d2ff AL |
481 | { |
482 | static TypeInfo interface_info = { | |
483 | .name = TYPE_INTERFACE, | |
484 | .instance_size = sizeof(Interface), | |
485 | .abstract = true, | |
486 | }; | |
487 | ||
9970bd88 | 488 | type_interface = type_register_static(&interface_info); |
2f28d2ff AL |
489 | } |
490 | ||
83f7d43a | 491 | type_init(register_types) |
2f28d2ff AL |
492 | |
493 | Object *object_dynamic_cast_assert(Object *obj, const char *typename) | |
494 | { | |
495 | Object *inst; | |
496 | ||
497 | inst = object_dynamic_cast(obj, typename); | |
498 | ||
499 | if (!inst) { | |
500 | fprintf(stderr, "Object %p is not an instance of type %s\n", | |
501 | obj, typename); | |
502 | abort(); | |
503 | } | |
504 | ||
505 | return inst; | |
506 | } | |
507 | ||
508 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
509 | const char *typename) | |
510 | { | |
511 | TypeImpl *target_type = type_get_by_name(typename); | |
512 | TypeImpl *type = class->type; | |
513 | ||
514 | while (type) { | |
515 | if (type == target_type) { | |
516 | return class; | |
517 | } | |
518 | ||
519 | type = type_get_parent(type); | |
520 | } | |
521 | ||
522 | return NULL; | |
523 | } | |
524 | ||
525 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
526 | const char *typename) | |
527 | { | |
528 | ObjectClass *ret = object_class_dynamic_cast(class, typename); | |
529 | ||
530 | if (!ret) { | |
531 | fprintf(stderr, "Object %p is not an instance of type %s\n", | |
532 | class, typename); | |
533 | abort(); | |
534 | } | |
535 | ||
536 | return ret; | |
537 | } | |
538 | ||
539 | const char *object_get_typename(Object *obj) | |
540 | { | |
541 | return obj->class->type->name; | |
542 | } | |
543 | ||
544 | ObjectClass *object_get_class(Object *obj) | |
545 | { | |
546 | return obj->class; | |
547 | } | |
548 | ||
549 | const char *object_class_get_name(ObjectClass *klass) | |
550 | { | |
551 | return klass->type->name; | |
552 | } | |
553 | ||
554 | ObjectClass *object_class_by_name(const char *typename) | |
555 | { | |
556 | TypeImpl *type = type_get_by_name(typename); | |
557 | ||
558 | if (!type) { | |
559 | return NULL; | |
560 | } | |
561 | ||
ac451033 | 562 | type_initialize(type); |
2f28d2ff AL |
563 | |
564 | return type->class; | |
565 | } | |
566 | ||
e7cce67f PB |
567 | ObjectClass *object_class_get_parent(ObjectClass *class) |
568 | { | |
569 | TypeImpl *type = type_get_parent(class->type); | |
570 | ||
571 | if (!type) { | |
572 | return NULL; | |
573 | } | |
574 | ||
575 | type_initialize(type); | |
576 | ||
577 | return type->class; | |
578 | } | |
579 | ||
2f28d2ff AL |
580 | typedef struct OCFData |
581 | { | |
582 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
583 | const char *implements_type; |
584 | bool include_abstract; | |
2f28d2ff AL |
585 | void *opaque; |
586 | } OCFData; | |
587 | ||
588 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
589 | gpointer opaque) | |
590 | { | |
591 | OCFData *data = opaque; | |
592 | TypeImpl *type = value; | |
93c511a1 | 593 | ObjectClass *k; |
2f28d2ff | 594 | |
ac451033 | 595 | type_initialize(type); |
93c511a1 | 596 | k = type->class; |
2f28d2ff | 597 | |
93c511a1 AL |
598 | if (!data->include_abstract && type->abstract) { |
599 | return; | |
600 | } | |
601 | ||
602 | if (data->implements_type && | |
603 | !object_class_dynamic_cast(k, data->implements_type)) { | |
604 | return; | |
605 | } | |
606 | ||
607 | data->fn(k, data->opaque); | |
2f28d2ff AL |
608 | } |
609 | ||
610 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 611 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
612 | void *opaque) |
613 | { | |
93c511a1 | 614 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff AL |
615 | |
616 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); | |
617 | } | |
57c9fafe | 618 | |
32efc535 PB |
619 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
620 | void *opaque) | |
621 | { | |
622 | ObjectProperty *prop; | |
623 | int ret = 0; | |
624 | ||
625 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
626 | if (object_property_is_child(prop)) { | |
627 | ret = fn(prop->opaque, opaque); | |
628 | if (ret != 0) { | |
629 | break; | |
630 | } | |
631 | } | |
632 | } | |
633 | return ret; | |
634 | } | |
635 | ||
418ba9e5 AF |
636 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
637 | { | |
638 | GSList **list = opaque; | |
639 | ||
640 | *list = g_slist_prepend(*list, klass); | |
641 | } | |
642 | ||
643 | GSList *object_class_get_list(const char *implements_type, | |
644 | bool include_abstract) | |
645 | { | |
646 | GSList *list = NULL; | |
647 | ||
648 | object_class_foreach(object_class_get_list_tramp, | |
649 | implements_type, include_abstract, &list); | |
650 | return list; | |
651 | } | |
652 | ||
57c9fafe AL |
653 | void object_ref(Object *obj) |
654 | { | |
655 | obj->ref++; | |
656 | } | |
657 | ||
658 | void object_unref(Object *obj) | |
659 | { | |
660 | g_assert(obj->ref > 0); | |
661 | obj->ref--; | |
662 | ||
663 | /* parent always holds a reference to its children */ | |
664 | if (obj->ref == 0) { | |
665 | object_finalize(obj); | |
666 | } | |
667 | } | |
668 | ||
669 | void object_property_add(Object *obj, const char *name, const char *type, | |
670 | ObjectPropertyAccessor *get, | |
671 | ObjectPropertyAccessor *set, | |
672 | ObjectPropertyRelease *release, | |
673 | void *opaque, Error **errp) | |
674 | { | |
675 | ObjectProperty *prop = g_malloc0(sizeof(*prop)); | |
676 | ||
677 | prop->name = g_strdup(name); | |
678 | prop->type = g_strdup(type); | |
679 | ||
680 | prop->get = get; | |
681 | prop->set = set; | |
682 | prop->release = release; | |
683 | prop->opaque = opaque; | |
684 | ||
685 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); | |
686 | } | |
687 | ||
688 | static ObjectProperty *object_property_find(Object *obj, const char *name) | |
689 | { | |
690 | ObjectProperty *prop; | |
691 | ||
692 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
693 | if (strcmp(prop->name, name) == 0) { | |
694 | return prop; | |
695 | } | |
696 | } | |
697 | ||
698 | return NULL; | |
699 | } | |
700 | ||
701 | void object_property_del(Object *obj, const char *name, Error **errp) | |
702 | { | |
703 | ObjectProperty *prop = object_property_find(obj, name); | |
704 | ||
705 | QTAILQ_REMOVE(&obj->properties, prop, node); | |
706 | ||
707 | prop->release(obj, prop->name, prop->opaque); | |
708 | ||
709 | g_free(prop->name); | |
710 | g_free(prop->type); | |
711 | g_free(prop); | |
712 | } | |
713 | ||
714 | void object_property_get(Object *obj, Visitor *v, const char *name, | |
715 | Error **errp) | |
716 | { | |
717 | ObjectProperty *prop = object_property_find(obj, name); | |
718 | ||
719 | if (prop == NULL) { | |
720 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); | |
721 | return; | |
722 | } | |
723 | ||
724 | if (!prop->get) { | |
725 | error_set(errp, QERR_PERMISSION_DENIED); | |
726 | } else { | |
727 | prop->get(obj, v, prop->opaque, name, errp); | |
728 | } | |
729 | } | |
730 | ||
731 | void object_property_set(Object *obj, Visitor *v, const char *name, | |
732 | Error **errp) | |
733 | { | |
734 | ObjectProperty *prop = object_property_find(obj, name); | |
735 | ||
736 | if (prop == NULL) { | |
737 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); | |
738 | return; | |
739 | } | |
740 | ||
741 | if (!prop->set) { | |
742 | error_set(errp, QERR_PERMISSION_DENIED); | |
743 | } else { | |
744 | prop->set(obj, v, prop->opaque, name, errp); | |
745 | } | |
746 | } | |
747 | ||
7b7b7d18 PB |
748 | void object_property_set_str(Object *obj, const char *value, |
749 | const char *name, Error **errp) | |
750 | { | |
751 | QString *qstr = qstring_from_str(value); | |
752 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); | |
753 | ||
754 | QDECREF(qstr); | |
755 | } | |
756 | ||
757 | char *object_property_get_str(Object *obj, const char *name, | |
758 | Error **errp) | |
759 | { | |
760 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
761 | QString *qstring; | |
762 | char *retval; | |
763 | ||
764 | if (!ret) { | |
765 | return NULL; | |
766 | } | |
767 | qstring = qobject_to_qstring(ret); | |
768 | if (!qstring) { | |
769 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); | |
770 | retval = NULL; | |
771 | } else { | |
772 | retval = g_strdup(qstring_get_str(qstring)); | |
773 | } | |
774 | ||
775 | QDECREF(qstring); | |
776 | return retval; | |
777 | } | |
778 | ||
1d9c5a12 PB |
779 | void object_property_set_link(Object *obj, Object *value, |
780 | const char *name, Error **errp) | |
781 | { | |
782 | object_property_set_str(obj, object_get_canonical_path(value), | |
783 | name, errp); | |
784 | } | |
785 | ||
786 | Object *object_property_get_link(Object *obj, const char *name, | |
787 | Error **errp) | |
788 | { | |
789 | char *str = object_property_get_str(obj, name, errp); | |
790 | Object *target = NULL; | |
791 | ||
792 | if (str && *str) { | |
793 | target = object_resolve_path(str, NULL); | |
794 | if (!target) { | |
795 | error_set(errp, QERR_DEVICE_NOT_FOUND, str); | |
796 | } | |
797 | } | |
798 | ||
799 | g_free(str); | |
800 | return target; | |
801 | } | |
802 | ||
7b7b7d18 PB |
803 | void object_property_set_bool(Object *obj, bool value, |
804 | const char *name, Error **errp) | |
805 | { | |
806 | QBool *qbool = qbool_from_int(value); | |
807 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); | |
808 | ||
809 | QDECREF(qbool); | |
810 | } | |
811 | ||
812 | bool object_property_get_bool(Object *obj, const char *name, | |
813 | Error **errp) | |
814 | { | |
815 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
816 | QBool *qbool; | |
817 | bool retval; | |
818 | ||
819 | if (!ret) { | |
820 | return false; | |
821 | } | |
822 | qbool = qobject_to_qbool(ret); | |
823 | if (!qbool) { | |
824 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); | |
825 | retval = false; | |
826 | } else { | |
827 | retval = qbool_get_int(qbool); | |
828 | } | |
829 | ||
830 | QDECREF(qbool); | |
831 | return retval; | |
832 | } | |
833 | ||
834 | void object_property_set_int(Object *obj, int64_t value, | |
835 | const char *name, Error **errp) | |
836 | { | |
837 | QInt *qint = qint_from_int(value); | |
838 | object_property_set_qobject(obj, QOBJECT(qint), name, errp); | |
839 | ||
840 | QDECREF(qint); | |
841 | } | |
842 | ||
843 | int64_t object_property_get_int(Object *obj, const char *name, | |
844 | Error **errp) | |
845 | { | |
846 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
847 | QInt *qint; | |
848 | int64_t retval; | |
849 | ||
850 | if (!ret) { | |
851 | return -1; | |
852 | } | |
853 | qint = qobject_to_qint(ret); | |
854 | if (!qint) { | |
855 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); | |
856 | retval = -1; | |
857 | } else { | |
858 | retval = qint_get_int(qint); | |
859 | } | |
860 | ||
861 | QDECREF(qint); | |
862 | return retval; | |
b2cd7dee PB |
863 | } |
864 | ||
865 | void object_property_parse(Object *obj, const char *string, | |
866 | const char *name, Error **errp) | |
867 | { | |
868 | StringInputVisitor *mi; | |
869 | mi = string_input_visitor_new(string); | |
870 | object_property_set(obj, string_input_get_visitor(mi), name, errp); | |
871 | ||
872 | string_input_visitor_cleanup(mi); | |
873 | } | |
874 | ||
875 | char *object_property_print(Object *obj, const char *name, | |
876 | Error **errp) | |
877 | { | |
878 | StringOutputVisitor *mo; | |
879 | char *string; | |
880 | ||
881 | mo = string_output_visitor_new(); | |
8185bfc1 | 882 | object_property_get(obj, string_output_get_visitor(mo), name, errp); |
b2cd7dee PB |
883 | string = string_output_get_string(mo); |
884 | string_output_visitor_cleanup(mo); | |
885 | return string; | |
7b7b7d18 PB |
886 | } |
887 | ||
57c9fafe AL |
888 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
889 | { | |
890 | ObjectProperty *prop = object_property_find(obj, name); | |
891 | ||
892 | if (prop == NULL) { | |
893 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); | |
894 | return NULL; | |
895 | } | |
896 | ||
897 | return prop->type; | |
898 | } | |
899 | ||
900 | Object *object_get_root(void) | |
901 | { | |
8b45d447 | 902 | static Object *root; |
57c9fafe | 903 | |
8b45d447 AL |
904 | if (!root) { |
905 | root = object_new("container"); | |
57c9fafe AL |
906 | } |
907 | ||
8b45d447 | 908 | return root; |
57c9fafe AL |
909 | } |
910 | ||
911 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, | |
912 | const char *name, Error **errp) | |
913 | { | |
914 | Object *child = opaque; | |
915 | gchar *path; | |
916 | ||
917 | path = object_get_canonical_path(child); | |
918 | visit_type_str(v, &path, name, errp); | |
919 | g_free(path); | |
920 | } | |
921 | ||
db85b575 AL |
922 | static void object_finalize_child_property(Object *obj, const char *name, |
923 | void *opaque) | |
924 | { | |
925 | Object *child = opaque; | |
926 | ||
927 | object_unref(child); | |
928 | } | |
929 | ||
57c9fafe AL |
930 | void object_property_add_child(Object *obj, const char *name, |
931 | Object *child, Error **errp) | |
932 | { | |
933 | gchar *type; | |
934 | ||
a1e7efdc PB |
935 | /* Registering an interface object in the composition tree will mightily |
936 | * confuse object_get_canonical_path (which, on the other hand, knows how | |
937 | * to get the canonical path of an interface object). | |
938 | */ | |
939 | assert(!object_is_type(obj, type_interface)); | |
940 | ||
57c9fafe AL |
941 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
942 | ||
943 | object_property_add(obj, name, type, object_get_child_property, | |
db85b575 | 944 | NULL, object_finalize_child_property, child, errp); |
57c9fafe AL |
945 | |
946 | object_ref(child); | |
947 | g_assert(child->parent == NULL); | |
948 | child->parent = obj; | |
949 | ||
950 | g_free(type); | |
951 | } | |
952 | ||
953 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, | |
954 | const char *name, Error **errp) | |
955 | { | |
956 | Object **child = opaque; | |
957 | gchar *path; | |
958 | ||
959 | if (*child) { | |
960 | path = object_get_canonical_path(*child); | |
961 | visit_type_str(v, &path, name, errp); | |
962 | g_free(path); | |
963 | } else { | |
964 | path = (gchar *)""; | |
965 | visit_type_str(v, &path, name, errp); | |
966 | } | |
967 | } | |
968 | ||
969 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, | |
970 | const char *name, Error **errp) | |
971 | { | |
972 | Object **child = opaque; | |
f0cdc966 | 973 | Object *old_target; |
57c9fafe AL |
974 | bool ambiguous = false; |
975 | const char *type; | |
976 | char *path; | |
11e35bfd | 977 | gchar *target_type; |
57c9fafe AL |
978 | |
979 | type = object_property_get_type(obj, name, NULL); | |
980 | ||
981 | visit_type_str(v, &path, name, errp); | |
982 | ||
f0cdc966 AB |
983 | old_target = *child; |
984 | *child = NULL; | |
57c9fafe AL |
985 | |
986 | if (strcmp(path, "") != 0) { | |
987 | Object *target; | |
988 | ||
11e35bfd PB |
989 | /* Go from link<FOO> to FOO. */ |
990 | target_type = g_strndup(&type[5], strlen(type) - 6); | |
991 | target = object_resolve_path_type(path, target_type, &ambiguous); | |
57c9fafe | 992 | |
11e35bfd PB |
993 | if (ambiguous) { |
994 | error_set(errp, QERR_AMBIGUOUS_PATH, path); | |
995 | } else if (target) { | |
996 | object_ref(target); | |
997 | *child = target; | |
57c9fafe | 998 | } else { |
11e35bfd PB |
999 | target = object_resolve_path(path, &ambiguous); |
1000 | if (target || ambiguous) { | |
1001 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); | |
1002 | } else { | |
1003 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); | |
1004 | } | |
57c9fafe | 1005 | } |
11e35bfd | 1006 | g_free(target_type); |
57c9fafe AL |
1007 | } |
1008 | ||
1009 | g_free(path); | |
f0cdc966 AB |
1010 | |
1011 | if (old_target != NULL) { | |
1012 | object_unref(old_target); | |
1013 | } | |
57c9fafe AL |
1014 | } |
1015 | ||
1016 | void object_property_add_link(Object *obj, const char *name, | |
1017 | const char *type, Object **child, | |
1018 | Error **errp) | |
1019 | { | |
1020 | gchar *full_type; | |
1021 | ||
1022 | full_type = g_strdup_printf("link<%s>", type); | |
1023 | ||
1024 | object_property_add(obj, name, full_type, | |
1025 | object_get_link_property, | |
1026 | object_set_link_property, | |
1027 | NULL, child, errp); | |
1028 | ||
1029 | g_free(full_type); | |
1030 | } | |
1031 | ||
1032 | gchar *object_get_canonical_path(Object *obj) | |
1033 | { | |
1034 | Object *root = object_get_root(); | |
1035 | char *newpath = NULL, *path = NULL; | |
1036 | ||
a1e7efdc PB |
1037 | if (object_is_type(obj, type_interface)) { |
1038 | obj = INTERFACE(obj)->obj; | |
1039 | } | |
1040 | ||
57c9fafe AL |
1041 | while (obj != root) { |
1042 | ObjectProperty *prop = NULL; | |
1043 | ||
1044 | g_assert(obj->parent != NULL); | |
1045 | ||
1046 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { | |
5d9d3f47 | 1047 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
1048 | continue; |
1049 | } | |
1050 | ||
1051 | if (prop->opaque == obj) { | |
1052 | if (path) { | |
1053 | newpath = g_strdup_printf("%s/%s", prop->name, path); | |
1054 | g_free(path); | |
1055 | path = newpath; | |
1056 | } else { | |
1057 | path = g_strdup(prop->name); | |
1058 | } | |
1059 | break; | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | g_assert(prop != NULL); | |
1064 | ||
1065 | obj = obj->parent; | |
1066 | } | |
1067 | ||
1068 | newpath = g_strdup_printf("/%s", path); | |
1069 | g_free(path); | |
1070 | ||
1071 | return newpath; | |
1072 | } | |
1073 | ||
a612b2a6 PB |
1074 | Object *object_resolve_path_component(Object *parent, gchar *part) |
1075 | { | |
1076 | ObjectProperty *prop = object_property_find(parent, part); | |
1077 | if (prop == NULL) { | |
1078 | return NULL; | |
1079 | } | |
1080 | ||
5d9d3f47 | 1081 | if (object_property_is_link(prop)) { |
a612b2a6 | 1082 | return *(Object **)prop->opaque; |
5d9d3f47 | 1083 | } else if (object_property_is_child(prop)) { |
a612b2a6 PB |
1084 | return prop->opaque; |
1085 | } else { | |
1086 | return NULL; | |
1087 | } | |
1088 | } | |
1089 | ||
57c9fafe AL |
1090 | static Object *object_resolve_abs_path(Object *parent, |
1091 | gchar **parts, | |
02fe2db6 | 1092 | const char *typename, |
57c9fafe AL |
1093 | int index) |
1094 | { | |
57c9fafe AL |
1095 | Object *child; |
1096 | ||
1097 | if (parts[index] == NULL) { | |
02fe2db6 | 1098 | return object_dynamic_cast(parent, typename); |
57c9fafe AL |
1099 | } |
1100 | ||
1101 | if (strcmp(parts[index], "") == 0) { | |
02fe2db6 | 1102 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
57c9fafe AL |
1103 | } |
1104 | ||
a612b2a6 | 1105 | child = object_resolve_path_component(parent, parts[index]); |
57c9fafe AL |
1106 | if (!child) { |
1107 | return NULL; | |
1108 | } | |
1109 | ||
02fe2db6 | 1110 | return object_resolve_abs_path(child, parts, typename, index + 1); |
57c9fafe AL |
1111 | } |
1112 | ||
1113 | static Object *object_resolve_partial_path(Object *parent, | |
1114 | gchar **parts, | |
02fe2db6 | 1115 | const char *typename, |
57c9fafe AL |
1116 | bool *ambiguous) |
1117 | { | |
1118 | Object *obj; | |
1119 | ObjectProperty *prop; | |
1120 | ||
02fe2db6 | 1121 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
57c9fafe AL |
1122 | |
1123 | QTAILQ_FOREACH(prop, &parent->properties, node) { | |
1124 | Object *found; | |
1125 | ||
5d9d3f47 | 1126 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
1127 | continue; |
1128 | } | |
1129 | ||
02fe2db6 PB |
1130 | found = object_resolve_partial_path(prop->opaque, parts, |
1131 | typename, ambiguous); | |
57c9fafe AL |
1132 | if (found) { |
1133 | if (obj) { | |
1134 | if (ambiguous) { | |
1135 | *ambiguous = true; | |
1136 | } | |
1137 | return NULL; | |
1138 | } | |
1139 | obj = found; | |
1140 | } | |
1141 | ||
1142 | if (ambiguous && *ambiguous) { | |
1143 | return NULL; | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | return obj; | |
1148 | } | |
1149 | ||
02fe2db6 PB |
1150 | Object *object_resolve_path_type(const char *path, const char *typename, |
1151 | bool *ambiguous) | |
57c9fafe AL |
1152 | { |
1153 | bool partial_path = true; | |
1154 | Object *obj; | |
1155 | gchar **parts; | |
1156 | ||
1157 | parts = g_strsplit(path, "/", 0); | |
1158 | if (parts == NULL || parts[0] == NULL) { | |
1159 | g_strfreev(parts); | |
1160 | return object_get_root(); | |
1161 | } | |
1162 | ||
1163 | if (strcmp(parts[0], "") == 0) { | |
1164 | partial_path = false; | |
1165 | } | |
1166 | ||
1167 | if (partial_path) { | |
1168 | if (ambiguous) { | |
1169 | *ambiguous = false; | |
1170 | } | |
02fe2db6 PB |
1171 | obj = object_resolve_partial_path(object_get_root(), parts, |
1172 | typename, ambiguous); | |
57c9fafe | 1173 | } else { |
02fe2db6 | 1174 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
57c9fafe AL |
1175 | } |
1176 | ||
1177 | g_strfreev(parts); | |
1178 | ||
1179 | return obj; | |
1180 | } | |
1181 | ||
02fe2db6 PB |
1182 | Object *object_resolve_path(const char *path, bool *ambiguous) |
1183 | { | |
1184 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); | |
1185 | } | |
1186 | ||
57c9fafe AL |
1187 | typedef struct StringProperty |
1188 | { | |
1189 | char *(*get)(Object *, Error **); | |
1190 | void (*set)(Object *, const char *, Error **); | |
1191 | } StringProperty; | |
1192 | ||
7b7b7d18 PB |
1193 | static void property_get_str(Object *obj, Visitor *v, void *opaque, |
1194 | const char *name, Error **errp) | |
57c9fafe AL |
1195 | { |
1196 | StringProperty *prop = opaque; | |
1197 | char *value; | |
1198 | ||
1199 | value = prop->get(obj, errp); | |
1200 | if (value) { | |
1201 | visit_type_str(v, &value, name, errp); | |
1202 | g_free(value); | |
1203 | } | |
1204 | } | |
1205 | ||
7b7b7d18 PB |
1206 | static void property_set_str(Object *obj, Visitor *v, void *opaque, |
1207 | const char *name, Error **errp) | |
57c9fafe AL |
1208 | { |
1209 | StringProperty *prop = opaque; | |
1210 | char *value; | |
1211 | Error *local_err = NULL; | |
1212 | ||
1213 | visit_type_str(v, &value, name, &local_err); | |
1214 | if (local_err) { | |
1215 | error_propagate(errp, local_err); | |
1216 | return; | |
1217 | } | |
1218 | ||
1219 | prop->set(obj, value, errp); | |
1220 | g_free(value); | |
1221 | } | |
1222 | ||
7b7b7d18 PB |
1223 | static void property_release_str(Object *obj, const char *name, |
1224 | void *opaque) | |
57c9fafe AL |
1225 | { |
1226 | StringProperty *prop = opaque; | |
1227 | g_free(prop); | |
1228 | } | |
1229 | ||
1230 | void object_property_add_str(Object *obj, const char *name, | |
1231 | char *(*get)(Object *, Error **), | |
1232 | void (*set)(Object *, const char *, Error **), | |
1233 | Error **errp) | |
1234 | { | |
1235 | StringProperty *prop = g_malloc0(sizeof(*prop)); | |
1236 | ||
1237 | prop->get = get; | |
1238 | prop->set = set; | |
1239 | ||
1240 | object_property_add(obj, name, "string", | |
7b7b7d18 PB |
1241 | get ? property_get_str : NULL, |
1242 | set ? property_set_str : NULL, | |
1243 | property_release_str, | |
57c9fafe AL |
1244 | prop, errp); |
1245 | } |