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