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