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