]>
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 | ||
14cccb61 | 13 | #include "qom/object.h" |
2f28d2ff | 14 | #include "qemu-common.h" |
7b1b5d19 | 15 | #include "qapi/visitor.h" |
1f21772d | 16 | #include "qapi-visit.h" |
b2cd7dee PB |
17 | #include "qapi/string-input-visitor.h" |
18 | #include "qapi/string-output-visitor.h" | |
7b1b5d19 | 19 | #include "qapi/qmp/qerror.h" |
fa131d94 | 20 | #include "trace.h" |
2f28d2ff | 21 | |
7b7b7d18 PB |
22 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
23 | * of the QOM core on QObject? */ | |
14cccb61 | 24 | #include "qom/qom-qobject.h" |
7b1b5d19 PB |
25 | #include "qapi/qmp/qobject.h" |
26 | #include "qapi/qmp/qbool.h" | |
27 | #include "qapi/qmp/qint.h" | |
28 | #include "qapi/qmp/qstring.h" | |
7b7b7d18 | 29 | |
2f28d2ff AL |
30 | #define MAX_INTERFACES 32 |
31 | ||
32 | typedef struct InterfaceImpl InterfaceImpl; | |
33 | typedef struct TypeImpl TypeImpl; | |
34 | ||
35 | struct InterfaceImpl | |
36 | { | |
33e95c63 | 37 | const char *typename; |
2f28d2ff AL |
38 | }; |
39 | ||
40 | struct TypeImpl | |
41 | { | |
42 | const char *name; | |
43 | ||
44 | size_t class_size; | |
45 | ||
46 | size_t instance_size; | |
47 | ||
48 | void (*class_init)(ObjectClass *klass, void *data); | |
3b50e311 | 49 | void (*class_base_init)(ObjectClass *klass, void *data); |
2f28d2ff AL |
50 | void (*class_finalize)(ObjectClass *klass, void *data); |
51 | ||
52 | void *class_data; | |
53 | ||
54 | void (*instance_init)(Object *obj); | |
8231c2dd | 55 | void (*instance_post_init)(Object *obj); |
2f28d2ff AL |
56 | void (*instance_finalize)(Object *obj); |
57 | ||
58 | bool abstract; | |
59 | ||
60 | const char *parent; | |
61 | TypeImpl *parent_type; | |
62 | ||
63 | ObjectClass *class; | |
64 | ||
65 | int num_interfaces; | |
66 | InterfaceImpl interfaces[MAX_INTERFACES]; | |
67 | }; | |
68 | ||
9970bd88 PB |
69 | static Type type_interface; |
70 | ||
2f28d2ff AL |
71 | static GHashTable *type_table_get(void) |
72 | { | |
73 | static GHashTable *type_table; | |
74 | ||
75 | if (type_table == NULL) { | |
76 | type_table = g_hash_table_new(g_str_hash, g_str_equal); | |
77 | } | |
78 | ||
79 | return type_table; | |
80 | } | |
81 | ||
f54c19ca HP |
82 | static bool enumerating_types; |
83 | ||
2f28d2ff AL |
84 | static void type_table_add(TypeImpl *ti) |
85 | { | |
f54c19ca | 86 | assert(!enumerating_types); |
2f28d2ff AL |
87 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); |
88 | } | |
89 | ||
90 | static TypeImpl *type_table_lookup(const char *name) | |
91 | { | |
92 | return g_hash_table_lookup(type_table_get(), name); | |
93 | } | |
94 | ||
b061dc41 | 95 | static TypeImpl *type_new(const TypeInfo *info) |
2f28d2ff AL |
96 | { |
97 | TypeImpl *ti = g_malloc0(sizeof(*ti)); | |
33e95c63 | 98 | int i; |
2f28d2ff AL |
99 | |
100 | g_assert(info->name != NULL); | |
101 | ||
73093354 AL |
102 | if (type_table_lookup(info->name) != NULL) { |
103 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); | |
104 | abort(); | |
105 | } | |
106 | ||
2f28d2ff AL |
107 | ti->name = g_strdup(info->name); |
108 | ti->parent = g_strdup(info->parent); | |
109 | ||
110 | ti->class_size = info->class_size; | |
111 | ti->instance_size = info->instance_size; | |
112 | ||
113 | ti->class_init = info->class_init; | |
3b50e311 | 114 | ti->class_base_init = info->class_base_init; |
2f28d2ff AL |
115 | ti->class_finalize = info->class_finalize; |
116 | ti->class_data = info->class_data; | |
117 | ||
118 | ti->instance_init = info->instance_init; | |
8231c2dd | 119 | ti->instance_post_init = info->instance_post_init; |
2f28d2ff AL |
120 | ti->instance_finalize = info->instance_finalize; |
121 | ||
122 | ti->abstract = info->abstract; | |
123 | ||
33e95c63 AL |
124 | for (i = 0; info->interfaces && info->interfaces[i].type; i++) { |
125 | ti->interfaces[i].typename = g_strdup(info->interfaces[i].type); | |
2f28d2ff | 126 | } |
33e95c63 | 127 | ti->num_interfaces = i; |
2f28d2ff | 128 | |
b061dc41 PB |
129 | return ti; |
130 | } | |
2f28d2ff | 131 | |
b061dc41 PB |
132 | static TypeImpl *type_register_internal(const TypeInfo *info) |
133 | { | |
134 | TypeImpl *ti; | |
135 | ti = type_new(info); | |
136 | ||
137 | type_table_add(ti); | |
2f28d2ff AL |
138 | return ti; |
139 | } | |
140 | ||
049cb3cf PB |
141 | TypeImpl *type_register(const TypeInfo *info) |
142 | { | |
143 | assert(info->parent); | |
144 | return type_register_internal(info); | |
145 | } | |
146 | ||
2f28d2ff AL |
147 | TypeImpl *type_register_static(const TypeInfo *info) |
148 | { | |
149 | return type_register(info); | |
150 | } | |
151 | ||
152 | static TypeImpl *type_get_by_name(const char *name) | |
153 | { | |
154 | if (name == NULL) { | |
155 | return NULL; | |
156 | } | |
157 | ||
158 | return type_table_lookup(name); | |
159 | } | |
160 | ||
161 | static TypeImpl *type_get_parent(TypeImpl *type) | |
162 | { | |
163 | if (!type->parent_type && type->parent) { | |
164 | type->parent_type = type_get_by_name(type->parent); | |
165 | g_assert(type->parent_type != NULL); | |
166 | } | |
167 | ||
168 | return type->parent_type; | |
169 | } | |
170 | ||
171 | static bool type_has_parent(TypeImpl *type) | |
172 | { | |
173 | return (type->parent != NULL); | |
174 | } | |
175 | ||
176 | static size_t type_class_get_size(TypeImpl *ti) | |
177 | { | |
178 | if (ti->class_size) { | |
179 | return ti->class_size; | |
180 | } | |
181 | ||
182 | if (type_has_parent(ti)) { | |
183 | return type_class_get_size(type_get_parent(ti)); | |
184 | } | |
185 | ||
186 | return sizeof(ObjectClass); | |
187 | } | |
188 | ||
aca59af6 IM |
189 | static size_t type_object_get_size(TypeImpl *ti) |
190 | { | |
191 | if (ti->instance_size) { | |
192 | return ti->instance_size; | |
193 | } | |
194 | ||
195 | if (type_has_parent(ti)) { | |
196 | return type_object_get_size(type_get_parent(ti)); | |
197 | } | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
33e95c63 | 202 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
2f28d2ff | 203 | { |
33e95c63 AL |
204 | assert(target_type); |
205 | ||
206 | /* Check if typename is a direct ancestor of type */ | |
207 | while (type) { | |
208 | if (type == target_type) { | |
209 | return true; | |
210 | } | |
2f28d2ff | 211 | |
33e95c63 AL |
212 | type = type_get_parent(type); |
213 | } | |
214 | ||
215 | return false; | |
216 | } | |
217 | ||
218 | static void type_initialize(TypeImpl *ti); | |
219 | ||
b061dc41 PB |
220 | static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type, |
221 | TypeImpl *parent_type) | |
33e95c63 AL |
222 | { |
223 | InterfaceClass *new_iface; | |
224 | TypeInfo info = { }; | |
225 | TypeImpl *iface_impl; | |
226 | ||
b061dc41 PB |
227 | info.parent = parent_type->name; |
228 | info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name); | |
33e95c63 AL |
229 | info.abstract = true; |
230 | ||
b061dc41 PB |
231 | iface_impl = type_new(&info); |
232 | iface_impl->parent_type = parent_type; | |
33e95c63 AL |
233 | type_initialize(iface_impl); |
234 | g_free((char *)info.name); | |
235 | ||
236 | new_iface = (InterfaceClass *)iface_impl->class; | |
237 | new_iface->concrete_class = ti->class; | |
b061dc41 | 238 | new_iface->interface_type = interface_type; |
33e95c63 AL |
239 | |
240 | ti->class->interfaces = g_slist_append(ti->class->interfaces, | |
241 | iface_impl->class); | |
2f28d2ff AL |
242 | } |
243 | ||
ac451033 | 244 | static void type_initialize(TypeImpl *ti) |
2f28d2ff | 245 | { |
745549c8 | 246 | TypeImpl *parent; |
2f28d2ff AL |
247 | |
248 | if (ti->class) { | |
249 | return; | |
250 | } | |
251 | ||
252 | ti->class_size = type_class_get_size(ti); | |
aca59af6 | 253 | ti->instance_size = type_object_get_size(ti); |
2f28d2ff AL |
254 | |
255 | ti->class = g_malloc0(ti->class_size); | |
2f28d2ff | 256 | |
745549c8 PB |
257 | parent = type_get_parent(ti); |
258 | if (parent) { | |
ac451033 | 259 | type_initialize(parent); |
33e95c63 AL |
260 | GSList *e; |
261 | int i; | |
2f28d2ff | 262 | |
2f28d2ff | 263 | g_assert(parent->class_size <= ti->class_size); |
745549c8 | 264 | memcpy(ti->class, parent->class, parent->class_size); |
3e407de4 | 265 | ti->class->interfaces = NULL; |
33e95c63 AL |
266 | |
267 | for (e = parent->class->interfaces; e; e = e->next) { | |
b061dc41 PB |
268 | InterfaceClass *iface = e->data; |
269 | ObjectClass *klass = OBJECT_CLASS(iface); | |
270 | ||
271 | type_initialize_interface(ti, iface->interface_type, klass->type); | |
33e95c63 AL |
272 | } |
273 | ||
274 | for (i = 0; i < ti->num_interfaces; i++) { | |
275 | TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); | |
276 | for (e = ti->class->interfaces; e; e = e->next) { | |
277 | TypeImpl *target_type = OBJECT_CLASS(e->data)->type; | |
278 | ||
279 | if (type_is_ancestor(target_type, t)) { | |
280 | break; | |
281 | } | |
282 | } | |
283 | ||
284 | if (e) { | |
285 | continue; | |
286 | } | |
287 | ||
b061dc41 | 288 | type_initialize_interface(ti, t, t); |
33e95c63 | 289 | } |
745549c8 | 290 | } |
2f28d2ff | 291 | |
745549c8 | 292 | ti->class->type = ti; |
3b50e311 | 293 | |
745549c8 PB |
294 | while (parent) { |
295 | if (parent->class_base_init) { | |
296 | parent->class_base_init(ti->class, ti->class_data); | |
3b50e311 | 297 | } |
745549c8 | 298 | parent = type_get_parent(parent); |
2f28d2ff AL |
299 | } |
300 | ||
2f28d2ff AL |
301 | if (ti->class_init) { |
302 | ti->class_init(ti->class, ti->class_data); | |
303 | } | |
2f28d2ff AL |
304 | } |
305 | ||
306 | static void object_init_with_type(Object *obj, TypeImpl *ti) | |
307 | { | |
2f28d2ff AL |
308 | if (type_has_parent(ti)) { |
309 | object_init_with_type(obj, type_get_parent(ti)); | |
310 | } | |
311 | ||
2f28d2ff AL |
312 | if (ti->instance_init) { |
313 | ti->instance_init(obj); | |
314 | } | |
315 | } | |
316 | ||
8231c2dd EH |
317 | static void object_post_init_with_type(Object *obj, TypeImpl *ti) |
318 | { | |
319 | if (ti->instance_post_init) { | |
320 | ti->instance_post_init(obj); | |
321 | } | |
322 | ||
323 | if (type_has_parent(ti)) { | |
324 | object_post_init_with_type(obj, type_get_parent(ti)); | |
325 | } | |
326 | } | |
327 | ||
5b9237f6 | 328 | void object_initialize_with_type(void *data, size_t size, TypeImpl *type) |
2f28d2ff AL |
329 | { |
330 | Object *obj = data; | |
331 | ||
332 | g_assert(type != NULL); | |
ac451033 | 333 | type_initialize(type); |
aca59af6 IM |
334 | |
335 | g_assert(type->instance_size >= sizeof(Object)); | |
2f28d2ff | 336 | g_assert(type->abstract == false); |
5b9237f6 | 337 | g_assert(size >= type->instance_size); |
2f28d2ff AL |
338 | |
339 | memset(obj, 0, type->instance_size); | |
340 | obj->class = type->class; | |
764b6312 | 341 | object_ref(obj); |
57c9fafe | 342 | QTAILQ_INIT(&obj->properties); |
2f28d2ff | 343 | object_init_with_type(obj, type); |
8231c2dd | 344 | object_post_init_with_type(obj, type); |
2f28d2ff AL |
345 | } |
346 | ||
213f0c4f | 347 | void object_initialize(void *data, size_t size, const char *typename) |
2f28d2ff AL |
348 | { |
349 | TypeImpl *type = type_get_by_name(typename); | |
350 | ||
5b9237f6 | 351 | object_initialize_with_type(data, size, type); |
2f28d2ff AL |
352 | } |
353 | ||
5d9d3f47 AF |
354 | static inline bool object_property_is_child(ObjectProperty *prop) |
355 | { | |
356 | return strstart(prop->type, "child<", NULL); | |
357 | } | |
358 | ||
57c9fafe AL |
359 | static void object_property_del_all(Object *obj) |
360 | { | |
361 | while (!QTAILQ_EMPTY(&obj->properties)) { | |
362 | ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); | |
363 | ||
364 | QTAILQ_REMOVE(&obj->properties, prop, node); | |
365 | ||
366 | if (prop->release) { | |
367 | prop->release(obj, prop->name, prop->opaque); | |
368 | } | |
369 | ||
370 | g_free(prop->name); | |
371 | g_free(prop->type); | |
80742642 | 372 | g_free(prop->description); |
57c9fafe AL |
373 | g_free(prop); |
374 | } | |
375 | } | |
376 | ||
377 | static void object_property_del_child(Object *obj, Object *child, Error **errp) | |
378 | { | |
379 | ObjectProperty *prop; | |
380 | ||
381 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
5d9d3f47 | 382 | if (object_property_is_child(prop) && prop->opaque == child) { |
57c9fafe | 383 | object_property_del(obj, prop->name, errp); |
6c1fdcf9 | 384 | break; |
57c9fafe AL |
385 | } |
386 | } | |
387 | } | |
388 | ||
389 | void object_unparent(Object *obj) | |
390 | { | |
e998fa8d MT |
391 | if (obj->parent) { |
392 | object_property_del_child(obj->parent, obj, NULL); | |
393 | } | |
57c9fafe AL |
394 | } |
395 | ||
2f28d2ff AL |
396 | static void object_deinit(Object *obj, TypeImpl *type) |
397 | { | |
398 | if (type->instance_finalize) { | |
399 | type->instance_finalize(obj); | |
400 | } | |
401 | ||
2f28d2ff AL |
402 | if (type_has_parent(type)) { |
403 | object_deinit(obj, type_get_parent(type)); | |
404 | } | |
405 | } | |
406 | ||
339c2708 | 407 | static void object_finalize(void *data) |
2f28d2ff AL |
408 | { |
409 | Object *obj = data; | |
410 | TypeImpl *ti = obj->class->type; | |
411 | ||
57c9fafe | 412 | object_property_del_all(obj); |
76a6e1cc | 413 | object_deinit(obj, ti); |
db85b575 AL |
414 | |
415 | g_assert(obj->ref == 0); | |
fde9bf44 PB |
416 | if (obj->free) { |
417 | obj->free(obj); | |
418 | } | |
2f28d2ff AL |
419 | } |
420 | ||
421 | Object *object_new_with_type(Type type) | |
422 | { | |
423 | Object *obj; | |
424 | ||
425 | g_assert(type != NULL); | |
ac451033 | 426 | type_initialize(type); |
2f28d2ff AL |
427 | |
428 | obj = g_malloc(type->instance_size); | |
5b9237f6 | 429 | object_initialize_with_type(obj, type->instance_size, type); |
fde9bf44 | 430 | obj->free = g_free; |
2f28d2ff AL |
431 | |
432 | return obj; | |
433 | } | |
434 | ||
435 | Object *object_new(const char *typename) | |
436 | { | |
437 | TypeImpl *ti = type_get_by_name(typename); | |
438 | ||
439 | return object_new_with_type(ti); | |
440 | } | |
441 | ||
2f28d2ff AL |
442 | Object *object_dynamic_cast(Object *obj, const char *typename) |
443 | { | |
b7f43fe4 | 444 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
acc4af3f PB |
445 | return obj; |
446 | } | |
447 | ||
2f28d2ff AL |
448 | return NULL; |
449 | } | |
450 | ||
be17f18b PB |
451 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
452 | const char *file, int line, const char *func) | |
2f28d2ff | 453 | { |
fa131d94 PB |
454 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
455 | typename, file, line, func); | |
456 | ||
3556c233 | 457 | #ifdef CONFIG_QOM_CAST_DEBUG |
03587328 AL |
458 | int i; |
459 | Object *inst; | |
460 | ||
95916abc | 461 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c | 462 | if (obj->class->object_cast_cache[i] == typename) { |
03587328 AL |
463 | goto out; |
464 | } | |
465 | } | |
466 | ||
467 | inst = object_dynamic_cast(obj, typename); | |
2f28d2ff | 468 | |
b7f43fe4 | 469 | if (!inst && obj) { |
be17f18b PB |
470 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
471 | file, line, func, obj, typename); | |
2f28d2ff AL |
472 | abort(); |
473 | } | |
474 | ||
3556c233 | 475 | assert(obj == inst); |
03587328 | 476 | |
95916abc | 477 | if (obj && obj == inst) { |
03587328 | 478 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c PC |
479 | obj->class->object_cast_cache[i - 1] = |
480 | obj->class->object_cast_cache[i]; | |
03587328 | 481 | } |
0ab4c94c | 482 | obj->class->object_cast_cache[i - 1] = typename; |
03587328 AL |
483 | } |
484 | ||
485 | out: | |
3556c233 PB |
486 | #endif |
487 | return obj; | |
2f28d2ff AL |
488 | } |
489 | ||
490 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
491 | const char *typename) | |
492 | { | |
33e95c63 | 493 | ObjectClass *ret = NULL; |
bf0fda34 PB |
494 | TypeImpl *target_type; |
495 | TypeImpl *type; | |
2f28d2ff | 496 | |
bf0fda34 PB |
497 | if (!class) { |
498 | return NULL; | |
499 | } | |
500 | ||
793c96b5 | 501 | /* A simple fast path that can trigger a lot for leaf classes. */ |
bf0fda34 | 502 | type = class->type; |
793c96b5 PB |
503 | if (type->name == typename) { |
504 | return class; | |
505 | } | |
506 | ||
bf0fda34 | 507 | target_type = type_get_by_name(typename); |
9ab880b3 AG |
508 | if (!target_type) { |
509 | /* target class type unknown, so fail the cast */ | |
510 | return NULL; | |
511 | } | |
512 | ||
00e2ceae PC |
513 | if (type->class->interfaces && |
514 | type_is_ancestor(target_type, type_interface)) { | |
33e95c63 AL |
515 | int found = 0; |
516 | GSList *i; | |
2f28d2ff | 517 | |
33e95c63 AL |
518 | for (i = class->interfaces; i; i = i->next) { |
519 | ObjectClass *target_class = i->data; | |
520 | ||
521 | if (type_is_ancestor(target_class->type, target_type)) { | |
522 | ret = target_class; | |
523 | found++; | |
524 | } | |
525 | } | |
526 | ||
527 | /* The match was ambiguous, don't allow a cast */ | |
528 | if (found > 1) { | |
529 | ret = NULL; | |
530 | } | |
531 | } else if (type_is_ancestor(type, target_type)) { | |
532 | ret = class; | |
2f28d2ff AL |
533 | } |
534 | ||
33e95c63 | 535 | return ret; |
2f28d2ff AL |
536 | } |
537 | ||
538 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
be17f18b PB |
539 | const char *typename, |
540 | const char *file, int line, | |
541 | const char *func) | |
2f28d2ff | 542 | { |
fa131d94 PB |
543 | ObjectClass *ret; |
544 | ||
545 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", | |
546 | typename, file, line, func); | |
2f28d2ff | 547 | |
03587328 AL |
548 | #ifdef CONFIG_QOM_CAST_DEBUG |
549 | int i; | |
550 | ||
9d6a3d58 | 551 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c | 552 | if (class->class_cast_cache[i] == typename) { |
03587328 AL |
553 | ret = class; |
554 | goto out; | |
555 | } | |
556 | } | |
557 | #else | |
9d6a3d58 | 558 | if (!class || !class->interfaces) { |
3556c233 PB |
559 | return class; |
560 | } | |
561 | #endif | |
562 | ||
fa131d94 | 563 | ret = object_class_dynamic_cast(class, typename); |
bf0fda34 | 564 | if (!ret && class) { |
be17f18b PB |
565 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
566 | file, line, func, class, typename); | |
2f28d2ff AL |
567 | abort(); |
568 | } | |
569 | ||
03587328 | 570 | #ifdef CONFIG_QOM_CAST_DEBUG |
9d6a3d58 | 571 | if (class && ret == class) { |
03587328 | 572 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c | 573 | class->class_cast_cache[i - 1] = class->class_cast_cache[i]; |
03587328 | 574 | } |
0ab4c94c | 575 | class->class_cast_cache[i - 1] = typename; |
03587328 AL |
576 | } |
577 | out: | |
578 | #endif | |
2f28d2ff AL |
579 | return ret; |
580 | } | |
581 | ||
582 | const char *object_get_typename(Object *obj) | |
583 | { | |
584 | return obj->class->type->name; | |
585 | } | |
586 | ||
587 | ObjectClass *object_get_class(Object *obj) | |
588 | { | |
589 | return obj->class; | |
590 | } | |
591 | ||
17862378 AF |
592 | bool object_class_is_abstract(ObjectClass *klass) |
593 | { | |
594 | return klass->type->abstract; | |
595 | } | |
596 | ||
2f28d2ff AL |
597 | const char *object_class_get_name(ObjectClass *klass) |
598 | { | |
599 | return klass->type->name; | |
600 | } | |
601 | ||
602 | ObjectClass *object_class_by_name(const char *typename) | |
603 | { | |
604 | TypeImpl *type = type_get_by_name(typename); | |
605 | ||
606 | if (!type) { | |
607 | return NULL; | |
608 | } | |
609 | ||
ac451033 | 610 | type_initialize(type); |
2f28d2ff AL |
611 | |
612 | return type->class; | |
613 | } | |
614 | ||
e7cce67f PB |
615 | ObjectClass *object_class_get_parent(ObjectClass *class) |
616 | { | |
617 | TypeImpl *type = type_get_parent(class->type); | |
618 | ||
619 | if (!type) { | |
620 | return NULL; | |
621 | } | |
622 | ||
623 | type_initialize(type); | |
624 | ||
625 | return type->class; | |
626 | } | |
627 | ||
2f28d2ff AL |
628 | typedef struct OCFData |
629 | { | |
630 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
631 | const char *implements_type; |
632 | bool include_abstract; | |
2f28d2ff AL |
633 | void *opaque; |
634 | } OCFData; | |
635 | ||
636 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
637 | gpointer opaque) | |
638 | { | |
639 | OCFData *data = opaque; | |
640 | TypeImpl *type = value; | |
93c511a1 | 641 | ObjectClass *k; |
2f28d2ff | 642 | |
ac451033 | 643 | type_initialize(type); |
93c511a1 | 644 | k = type->class; |
2f28d2ff | 645 | |
93c511a1 AL |
646 | if (!data->include_abstract && type->abstract) { |
647 | return; | |
648 | } | |
649 | ||
650 | if (data->implements_type && | |
651 | !object_class_dynamic_cast(k, data->implements_type)) { | |
652 | return; | |
653 | } | |
654 | ||
655 | data->fn(k, data->opaque); | |
2f28d2ff AL |
656 | } |
657 | ||
658 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 659 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
660 | void *opaque) |
661 | { | |
93c511a1 | 662 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff | 663 | |
f54c19ca | 664 | enumerating_types = true; |
2f28d2ff | 665 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
f54c19ca | 666 | enumerating_types = false; |
2f28d2ff | 667 | } |
57c9fafe | 668 | |
32efc535 PB |
669 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
670 | void *opaque) | |
671 | { | |
8af734ca | 672 | ObjectProperty *prop, *next; |
32efc535 PB |
673 | int ret = 0; |
674 | ||
8af734ca | 675 | QTAILQ_FOREACH_SAFE(prop, &obj->properties, node, next) { |
32efc535 PB |
676 | if (object_property_is_child(prop)) { |
677 | ret = fn(prop->opaque, opaque); | |
678 | if (ret != 0) { | |
679 | break; | |
680 | } | |
681 | } | |
682 | } | |
683 | return ret; | |
684 | } | |
685 | ||
418ba9e5 AF |
686 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
687 | { | |
688 | GSList **list = opaque; | |
689 | ||
690 | *list = g_slist_prepend(*list, klass); | |
691 | } | |
692 | ||
693 | GSList *object_class_get_list(const char *implements_type, | |
694 | bool include_abstract) | |
695 | { | |
696 | GSList *list = NULL; | |
697 | ||
698 | object_class_foreach(object_class_get_list_tramp, | |
699 | implements_type, include_abstract, &list); | |
700 | return list; | |
701 | } | |
702 | ||
57c9fafe AL |
703 | void object_ref(Object *obj) |
704 | { | |
8ffad850 PC |
705 | if (!obj) { |
706 | return; | |
707 | } | |
f08c03f3 | 708 | atomic_inc(&obj->ref); |
57c9fafe AL |
709 | } |
710 | ||
711 | void object_unref(Object *obj) | |
712 | { | |
8ffad850 PC |
713 | if (!obj) { |
714 | return; | |
715 | } | |
57c9fafe | 716 | g_assert(obj->ref > 0); |
57c9fafe AL |
717 | |
718 | /* parent always holds a reference to its children */ | |
f08c03f3 | 719 | if (atomic_fetch_dec(&obj->ref) == 1) { |
57c9fafe AL |
720 | object_finalize(obj); |
721 | } | |
722 | } | |
723 | ||
64607d08 PB |
724 | ObjectProperty * |
725 | object_property_add(Object *obj, const char *name, const char *type, | |
726 | ObjectPropertyAccessor *get, | |
727 | ObjectPropertyAccessor *set, | |
728 | ObjectPropertyRelease *release, | |
729 | void *opaque, Error **errp) | |
57c9fafe | 730 | { |
54852b03 | 731 | ObjectProperty *prop; |
33965904 PC |
732 | size_t name_len = strlen(name); |
733 | ||
734 | if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { | |
735 | int i; | |
736 | ObjectProperty *ret; | |
737 | char *name_no_array = g_strdup(name); | |
738 | ||
739 | name_no_array[name_len - 3] = '\0'; | |
740 | for (i = 0; ; ++i) { | |
741 | char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); | |
742 | ||
743 | ret = object_property_add(obj, full_name, type, get, set, | |
744 | release, opaque, NULL); | |
745 | g_free(full_name); | |
746 | if (ret) { | |
747 | break; | |
748 | } | |
749 | } | |
750 | g_free(name_no_array); | |
751 | return ret; | |
752 | } | |
54852b03 PM |
753 | |
754 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
755 | if (strcmp(prop->name, name) == 0) { | |
756 | error_setg(errp, "attempt to add duplicate property '%s'" | |
757 | " to object (type '%s')", name, | |
758 | object_get_typename(obj)); | |
64607d08 | 759 | return NULL; |
54852b03 PM |
760 | } |
761 | } | |
762 | ||
763 | prop = g_malloc0(sizeof(*prop)); | |
57c9fafe AL |
764 | |
765 | prop->name = g_strdup(name); | |
766 | prop->type = g_strdup(type); | |
767 | ||
768 | prop->get = get; | |
769 | prop->set = set; | |
770 | prop->release = release; | |
771 | prop->opaque = opaque; | |
772 | ||
773 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); | |
64607d08 | 774 | return prop; |
57c9fafe AL |
775 | } |
776 | ||
89bfe000 PB |
777 | ObjectProperty *object_property_find(Object *obj, const char *name, |
778 | Error **errp) | |
57c9fafe AL |
779 | { |
780 | ObjectProperty *prop; | |
781 | ||
782 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
783 | if (strcmp(prop->name, name) == 0) { | |
784 | return prop; | |
785 | } | |
786 | } | |
787 | ||
f231b88d | 788 | error_setg(errp, "Property '.%s' not found", name); |
57c9fafe AL |
789 | return NULL; |
790 | } | |
791 | ||
792 | void object_property_del(Object *obj, const char *name, Error **errp) | |
793 | { | |
89bfe000 | 794 | ObjectProperty *prop = object_property_find(obj, name, errp); |
0866aca1 | 795 | if (prop == NULL) { |
0866aca1 AL |
796 | return; |
797 | } | |
57c9fafe | 798 | |
0866aca1 AL |
799 | if (prop->release) { |
800 | prop->release(obj, name, prop->opaque); | |
801 | } | |
802 | ||
803 | QTAILQ_REMOVE(&obj->properties, prop, node); | |
57c9fafe AL |
804 | |
805 | g_free(prop->name); | |
806 | g_free(prop->type); | |
80742642 | 807 | g_free(prop->description); |
57c9fafe AL |
808 | g_free(prop); |
809 | } | |
810 | ||
811 | void object_property_get(Object *obj, Visitor *v, const char *name, | |
812 | Error **errp) | |
813 | { | |
89bfe000 | 814 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 815 | if (prop == NULL) { |
57c9fafe AL |
816 | return; |
817 | } | |
818 | ||
819 | if (!prop->get) { | |
820 | error_set(errp, QERR_PERMISSION_DENIED); | |
821 | } else { | |
822 | prop->get(obj, v, prop->opaque, name, errp); | |
823 | } | |
824 | } | |
825 | ||
826 | void object_property_set(Object *obj, Visitor *v, const char *name, | |
827 | Error **errp) | |
828 | { | |
89bfe000 | 829 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 830 | if (prop == NULL) { |
57c9fafe AL |
831 | return; |
832 | } | |
833 | ||
834 | if (!prop->set) { | |
835 | error_set(errp, QERR_PERMISSION_DENIED); | |
836 | } else { | |
837 | prop->set(obj, v, prop->opaque, name, errp); | |
838 | } | |
839 | } | |
840 | ||
7b7b7d18 PB |
841 | void object_property_set_str(Object *obj, const char *value, |
842 | const char *name, Error **errp) | |
843 | { | |
844 | QString *qstr = qstring_from_str(value); | |
845 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); | |
846 | ||
847 | QDECREF(qstr); | |
848 | } | |
849 | ||
850 | char *object_property_get_str(Object *obj, const char *name, | |
851 | Error **errp) | |
852 | { | |
853 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
854 | QString *qstring; | |
855 | char *retval; | |
856 | ||
857 | if (!ret) { | |
858 | return NULL; | |
859 | } | |
860 | qstring = qobject_to_qstring(ret); | |
861 | if (!qstring) { | |
862 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); | |
863 | retval = NULL; | |
864 | } else { | |
865 | retval = g_strdup(qstring_get_str(qstring)); | |
866 | } | |
867 | ||
868 | QDECREF(qstring); | |
869 | return retval; | |
870 | } | |
871 | ||
1d9c5a12 PB |
872 | void object_property_set_link(Object *obj, Object *value, |
873 | const char *name, Error **errp) | |
874 | { | |
d3c49316 PC |
875 | if (value) { |
876 | gchar *path = object_get_canonical_path(value); | |
877 | object_property_set_str(obj, path, name, errp); | |
878 | g_free(path); | |
879 | } else { | |
880 | object_property_set_str(obj, "", name, errp); | |
881 | } | |
1d9c5a12 PB |
882 | } |
883 | ||
884 | Object *object_property_get_link(Object *obj, const char *name, | |
885 | Error **errp) | |
886 | { | |
887 | char *str = object_property_get_str(obj, name, errp); | |
888 | Object *target = NULL; | |
889 | ||
890 | if (str && *str) { | |
891 | target = object_resolve_path(str, NULL); | |
892 | if (!target) { | |
893 | error_set(errp, QERR_DEVICE_NOT_FOUND, str); | |
894 | } | |
895 | } | |
896 | ||
897 | g_free(str); | |
898 | return target; | |
899 | } | |
900 | ||
7b7b7d18 PB |
901 | void object_property_set_bool(Object *obj, bool value, |
902 | const char *name, Error **errp) | |
903 | { | |
904 | QBool *qbool = qbool_from_int(value); | |
905 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); | |
906 | ||
907 | QDECREF(qbool); | |
908 | } | |
909 | ||
910 | bool object_property_get_bool(Object *obj, const char *name, | |
911 | Error **errp) | |
912 | { | |
913 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
914 | QBool *qbool; | |
915 | bool retval; | |
916 | ||
917 | if (!ret) { | |
918 | return false; | |
919 | } | |
920 | qbool = qobject_to_qbool(ret); | |
921 | if (!qbool) { | |
922 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); | |
923 | retval = false; | |
924 | } else { | |
925 | retval = qbool_get_int(qbool); | |
926 | } | |
927 | ||
928 | QDECREF(qbool); | |
929 | return retval; | |
930 | } | |
931 | ||
932 | void object_property_set_int(Object *obj, int64_t value, | |
933 | const char *name, Error **errp) | |
934 | { | |
935 | QInt *qint = qint_from_int(value); | |
936 | object_property_set_qobject(obj, QOBJECT(qint), name, errp); | |
937 | ||
938 | QDECREF(qint); | |
939 | } | |
940 | ||
941 | int64_t object_property_get_int(Object *obj, const char *name, | |
942 | Error **errp) | |
943 | { | |
944 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
945 | QInt *qint; | |
946 | int64_t retval; | |
947 | ||
948 | if (!ret) { | |
949 | return -1; | |
950 | } | |
951 | qint = qobject_to_qint(ret); | |
952 | if (!qint) { | |
953 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); | |
954 | retval = -1; | |
955 | } else { | |
956 | retval = qint_get_int(qint); | |
957 | } | |
958 | ||
959 | QDECREF(qint); | |
960 | return retval; | |
b2cd7dee PB |
961 | } |
962 | ||
1f21772d HT |
963 | int object_property_get_enum(Object *obj, const char *name, |
964 | const char *strings[], Error **errp) | |
965 | { | |
966 | StringOutputVisitor *sov; | |
967 | StringInputVisitor *siv; | |
976620ac | 968 | char *str; |
1f21772d HT |
969 | int ret; |
970 | ||
971 | sov = string_output_visitor_new(false); | |
972 | object_property_get(obj, string_output_get_visitor(sov), name, errp); | |
976620ac CF |
973 | str = string_output_get_string(sov); |
974 | siv = string_input_visitor_new(str); | |
1f21772d HT |
975 | string_output_visitor_cleanup(sov); |
976 | visit_type_enum(string_input_get_visitor(siv), | |
977 | &ret, strings, NULL, name, errp); | |
976620ac CF |
978 | |
979 | g_free(str); | |
1f21772d HT |
980 | string_input_visitor_cleanup(siv); |
981 | ||
982 | return ret; | |
983 | } | |
984 | ||
985 | void object_property_get_uint16List(Object *obj, const char *name, | |
986 | uint16List **list, Error **errp) | |
987 | { | |
988 | StringOutputVisitor *ov; | |
989 | StringInputVisitor *iv; | |
976620ac | 990 | char *str; |
1f21772d HT |
991 | |
992 | ov = string_output_visitor_new(false); | |
993 | object_property_get(obj, string_output_get_visitor(ov), | |
994 | name, errp); | |
976620ac CF |
995 | str = string_output_get_string(ov); |
996 | iv = string_input_visitor_new(str); | |
1f21772d HT |
997 | visit_type_uint16List(string_input_get_visitor(iv), |
998 | list, NULL, errp); | |
976620ac CF |
999 | |
1000 | g_free(str); | |
1f21772d HT |
1001 | string_output_visitor_cleanup(ov); |
1002 | string_input_visitor_cleanup(iv); | |
1003 | } | |
1004 | ||
b2cd7dee PB |
1005 | void object_property_parse(Object *obj, const char *string, |
1006 | const char *name, Error **errp) | |
1007 | { | |
1008 | StringInputVisitor *mi; | |
1009 | mi = string_input_visitor_new(string); | |
1010 | object_property_set(obj, string_input_get_visitor(mi), name, errp); | |
1011 | ||
1012 | string_input_visitor_cleanup(mi); | |
1013 | } | |
1014 | ||
0b7593e0 | 1015 | char *object_property_print(Object *obj, const char *name, bool human, |
b2cd7dee PB |
1016 | Error **errp) |
1017 | { | |
1018 | StringOutputVisitor *mo; | |
3a53009f GA |
1019 | char *string = NULL; |
1020 | Error *local_err = NULL; | |
b2cd7dee | 1021 | |
0b7593e0 | 1022 | mo = string_output_visitor_new(human); |
3a53009f GA |
1023 | object_property_get(obj, string_output_get_visitor(mo), name, &local_err); |
1024 | if (local_err) { | |
1025 | error_propagate(errp, local_err); | |
1026 | goto out; | |
1027 | } | |
1028 | ||
b2cd7dee | 1029 | string = string_output_get_string(mo); |
3a53009f GA |
1030 | |
1031 | out: | |
b2cd7dee PB |
1032 | string_output_visitor_cleanup(mo); |
1033 | return string; | |
7b7b7d18 PB |
1034 | } |
1035 | ||
57c9fafe AL |
1036 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
1037 | { | |
89bfe000 | 1038 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1039 | if (prop == NULL) { |
57c9fafe AL |
1040 | return NULL; |
1041 | } | |
1042 | ||
1043 | return prop->type; | |
1044 | } | |
1045 | ||
1046 | Object *object_get_root(void) | |
1047 | { | |
8b45d447 | 1048 | static Object *root; |
57c9fafe | 1049 | |
8b45d447 AL |
1050 | if (!root) { |
1051 | root = object_new("container"); | |
57c9fafe AL |
1052 | } |
1053 | ||
8b45d447 | 1054 | return root; |
57c9fafe AL |
1055 | } |
1056 | ||
1057 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, | |
1058 | const char *name, Error **errp) | |
1059 | { | |
1060 | Object *child = opaque; | |
1061 | gchar *path; | |
1062 | ||
1063 | path = object_get_canonical_path(child); | |
1064 | visit_type_str(v, &path, name, errp); | |
1065 | g_free(path); | |
1066 | } | |
1067 | ||
64607d08 PB |
1068 | static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part) |
1069 | { | |
1070 | return opaque; | |
1071 | } | |
1072 | ||
db85b575 AL |
1073 | static void object_finalize_child_property(Object *obj, const char *name, |
1074 | void *opaque) | |
1075 | { | |
1076 | Object *child = opaque; | |
1077 | ||
bffc687d PB |
1078 | if (child->class->unparent) { |
1079 | (child->class->unparent)(child); | |
1080 | } | |
1081 | child->parent = NULL; | |
db85b575 AL |
1082 | object_unref(child); |
1083 | } | |
1084 | ||
57c9fafe AL |
1085 | void object_property_add_child(Object *obj, const char *name, |
1086 | Object *child, Error **errp) | |
1087 | { | |
b0ed5e9f | 1088 | Error *local_err = NULL; |
57c9fafe | 1089 | gchar *type; |
64607d08 | 1090 | ObjectProperty *op; |
57c9fafe | 1091 | |
8faa2f85 PC |
1092 | if (child->parent != NULL) { |
1093 | error_setg(errp, "child object is already parented"); | |
1094 | return; | |
1095 | } | |
1096 | ||
57c9fafe AL |
1097 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
1098 | ||
64607d08 PB |
1099 | op = object_property_add(obj, name, type, object_get_child_property, NULL, |
1100 | object_finalize_child_property, child, &local_err); | |
b0ed5e9f PB |
1101 | if (local_err) { |
1102 | error_propagate(errp, local_err); | |
1103 | goto out; | |
1104 | } | |
64607d08 PB |
1105 | |
1106 | op->resolve = object_resolve_child_property; | |
57c9fafe | 1107 | object_ref(child); |
57c9fafe AL |
1108 | child->parent = obj; |
1109 | ||
b0ed5e9f | 1110 | out: |
57c9fafe AL |
1111 | g_free(type); |
1112 | } | |
1113 | ||
39f72ef9 SH |
1114 | void object_property_allow_set_link(Object *obj, const char *name, |
1115 | Object *val, Error **errp) | |
1116 | { | |
1117 | /* Allow the link to be set, always */ | |
1118 | } | |
1119 | ||
9561fda8 SH |
1120 | typedef struct { |
1121 | Object **child; | |
39f72ef9 | 1122 | void (*check)(Object *, const char *, Object *, Error **); |
9561fda8 SH |
1123 | ObjectPropertyLinkFlags flags; |
1124 | } LinkProperty; | |
1125 | ||
57c9fafe AL |
1126 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, |
1127 | const char *name, Error **errp) | |
1128 | { | |
9561fda8 SH |
1129 | LinkProperty *lprop = opaque; |
1130 | Object **child = lprop->child; | |
57c9fafe AL |
1131 | gchar *path; |
1132 | ||
1133 | if (*child) { | |
1134 | path = object_get_canonical_path(*child); | |
1135 | visit_type_str(v, &path, name, errp); | |
1136 | g_free(path); | |
1137 | } else { | |
1138 | path = (gchar *)""; | |
1139 | visit_type_str(v, &path, name, errp); | |
1140 | } | |
1141 | } | |
1142 | ||
f5ec6704 SH |
1143 | /* |
1144 | * object_resolve_link: | |
1145 | * | |
1146 | * Lookup an object and ensure its type matches the link property type. This | |
1147 | * is similar to object_resolve_path() except type verification against the | |
1148 | * link property is performed. | |
1149 | * | |
1150 | * Returns: The matched object or NULL on path lookup failures. | |
1151 | */ | |
1152 | static Object *object_resolve_link(Object *obj, const char *name, | |
1153 | const char *path, Error **errp) | |
1154 | { | |
1155 | const char *type; | |
1156 | gchar *target_type; | |
1157 | bool ambiguous = false; | |
1158 | Object *target; | |
1159 | ||
1160 | /* Go from link<FOO> to FOO. */ | |
1161 | type = object_property_get_type(obj, name, NULL); | |
1162 | target_type = g_strndup(&type[5], strlen(type) - 6); | |
1163 | target = object_resolve_path_type(path, target_type, &ambiguous); | |
1164 | ||
1165 | if (ambiguous) { | |
f231b88d CR |
1166 | error_set(errp, ERROR_CLASS_GENERIC_ERROR, |
1167 | "Path '%s' does not uniquely identify an object", path); | |
f5ec6704 SH |
1168 | } else if (!target) { |
1169 | target = object_resolve_path(path, &ambiguous); | |
1170 | if (target || ambiguous) { | |
1171 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); | |
1172 | } else { | |
1173 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); | |
1174 | } | |
1175 | target = NULL; | |
1176 | } | |
1177 | g_free(target_type); | |
1178 | ||
1179 | return target; | |
1180 | } | |
1181 | ||
57c9fafe AL |
1182 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, |
1183 | const char *name, Error **errp) | |
1184 | { | |
c6aed983 | 1185 | Error *local_err = NULL; |
9561fda8 SH |
1186 | LinkProperty *prop = opaque; |
1187 | Object **child = prop->child; | |
c6aed983 SH |
1188 | Object *old_target = *child; |
1189 | Object *new_target = NULL; | |
1190 | char *path = NULL; | |
57c9fafe | 1191 | |
c6aed983 | 1192 | visit_type_str(v, &path, name, &local_err); |
57c9fafe | 1193 | |
c6aed983 SH |
1194 | if (!local_err && strcmp(path, "") != 0) { |
1195 | new_target = object_resolve_link(obj, name, path, &local_err); | |
57c9fafe AL |
1196 | } |
1197 | ||
1198 | g_free(path); | |
c6aed983 SH |
1199 | if (local_err) { |
1200 | error_propagate(errp, local_err); | |
1201 | return; | |
1202 | } | |
f0cdc966 | 1203 | |
39f72ef9 SH |
1204 | prop->check(obj, name, new_target, &local_err); |
1205 | if (local_err) { | |
1206 | error_propagate(errp, local_err); | |
1207 | return; | |
1208 | } | |
1209 | ||
8ffad850 | 1210 | object_ref(new_target); |
c6aed983 | 1211 | *child = new_target; |
8ffad850 | 1212 | object_unref(old_target); |
57c9fafe AL |
1213 | } |
1214 | ||
64607d08 PB |
1215 | static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part) |
1216 | { | |
1217 | LinkProperty *lprop = opaque; | |
1218 | ||
1219 | return *lprop->child; | |
1220 | } | |
1221 | ||
9561fda8 SH |
1222 | static void object_release_link_property(Object *obj, const char *name, |
1223 | void *opaque) | |
1224 | { | |
1225 | LinkProperty *prop = opaque; | |
1226 | ||
1227 | if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { | |
1228 | object_unref(*prop->child); | |
1229 | } | |
1230 | g_free(prop); | |
1231 | } | |
1232 | ||
57c9fafe AL |
1233 | void object_property_add_link(Object *obj, const char *name, |
1234 | const char *type, Object **child, | |
39f72ef9 SH |
1235 | void (*check)(Object *, const char *, |
1236 | Object *, Error **), | |
9561fda8 | 1237 | ObjectPropertyLinkFlags flags, |
57c9fafe AL |
1238 | Error **errp) |
1239 | { | |
9561fda8 SH |
1240 | Error *local_err = NULL; |
1241 | LinkProperty *prop = g_malloc(sizeof(*prop)); | |
57c9fafe | 1242 | gchar *full_type; |
64607d08 | 1243 | ObjectProperty *op; |
57c9fafe | 1244 | |
9561fda8 | 1245 | prop->child = child; |
39f72ef9 | 1246 | prop->check = check; |
9561fda8 SH |
1247 | prop->flags = flags; |
1248 | ||
57c9fafe AL |
1249 | full_type = g_strdup_printf("link<%s>", type); |
1250 | ||
64607d08 PB |
1251 | op = object_property_add(obj, name, full_type, |
1252 | object_get_link_property, | |
1253 | check ? object_set_link_property : NULL, | |
1254 | object_release_link_property, | |
1255 | prop, | |
1256 | &local_err); | |
9561fda8 SH |
1257 | if (local_err) { |
1258 | error_propagate(errp, local_err); | |
1259 | g_free(prop); | |
64607d08 | 1260 | goto out; |
9561fda8 | 1261 | } |
57c9fafe | 1262 | |
64607d08 PB |
1263 | op->resolve = object_resolve_link_property; |
1264 | ||
1265 | out: | |
57c9fafe AL |
1266 | g_free(full_type); |
1267 | } | |
1268 | ||
11f590b1 SH |
1269 | gchar *object_get_canonical_path_component(Object *obj) |
1270 | { | |
1271 | ObjectProperty *prop = NULL; | |
1272 | ||
1273 | g_assert(obj); | |
1274 | g_assert(obj->parent != NULL); | |
1275 | ||
1276 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { | |
1277 | if (!object_property_is_child(prop)) { | |
1278 | continue; | |
1279 | } | |
1280 | ||
1281 | if (prop->opaque == obj) { | |
1282 | return g_strdup(prop->name); | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | /* obj had a parent but was not a child, should never happen */ | |
1287 | g_assert_not_reached(); | |
1288 | return NULL; | |
1289 | } | |
1290 | ||
57c9fafe AL |
1291 | gchar *object_get_canonical_path(Object *obj) |
1292 | { | |
1293 | Object *root = object_get_root(); | |
11f590b1 | 1294 | char *newpath, *path = NULL; |
57c9fafe AL |
1295 | |
1296 | while (obj != root) { | |
11f590b1 | 1297 | char *component = object_get_canonical_path_component(obj); |
57c9fafe | 1298 | |
11f590b1 SH |
1299 | if (path) { |
1300 | newpath = g_strdup_printf("%s/%s", component, path); | |
1301 | g_free(component); | |
1302 | g_free(path); | |
1303 | path = newpath; | |
1304 | } else { | |
1305 | path = component; | |
57c9fafe AL |
1306 | } |
1307 | ||
57c9fafe AL |
1308 | obj = obj->parent; |
1309 | } | |
1310 | ||
11f590b1 | 1311 | newpath = g_strdup_printf("/%s", path ? path : ""); |
57c9fafe AL |
1312 | g_free(path); |
1313 | ||
1314 | return newpath; | |
1315 | } | |
1316 | ||
3e84b483 | 1317 | Object *object_resolve_path_component(Object *parent, const gchar *part) |
a612b2a6 | 1318 | { |
89bfe000 | 1319 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
a612b2a6 PB |
1320 | if (prop == NULL) { |
1321 | return NULL; | |
1322 | } | |
1323 | ||
64607d08 PB |
1324 | if (prop->resolve) { |
1325 | return prop->resolve(parent, prop->opaque, part); | |
a612b2a6 PB |
1326 | } else { |
1327 | return NULL; | |
1328 | } | |
1329 | } | |
1330 | ||
57c9fafe AL |
1331 | static Object *object_resolve_abs_path(Object *parent, |
1332 | gchar **parts, | |
02fe2db6 | 1333 | const char *typename, |
57c9fafe AL |
1334 | int index) |
1335 | { | |
57c9fafe AL |
1336 | Object *child; |
1337 | ||
1338 | if (parts[index] == NULL) { | |
02fe2db6 | 1339 | return object_dynamic_cast(parent, typename); |
57c9fafe AL |
1340 | } |
1341 | ||
1342 | if (strcmp(parts[index], "") == 0) { | |
02fe2db6 | 1343 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
57c9fafe AL |
1344 | } |
1345 | ||
a612b2a6 | 1346 | child = object_resolve_path_component(parent, parts[index]); |
57c9fafe AL |
1347 | if (!child) { |
1348 | return NULL; | |
1349 | } | |
1350 | ||
02fe2db6 | 1351 | return object_resolve_abs_path(child, parts, typename, index + 1); |
57c9fafe AL |
1352 | } |
1353 | ||
1354 | static Object *object_resolve_partial_path(Object *parent, | |
1355 | gchar **parts, | |
02fe2db6 | 1356 | const char *typename, |
57c9fafe AL |
1357 | bool *ambiguous) |
1358 | { | |
1359 | Object *obj; | |
1360 | ObjectProperty *prop; | |
1361 | ||
02fe2db6 | 1362 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
57c9fafe AL |
1363 | |
1364 | QTAILQ_FOREACH(prop, &parent->properties, node) { | |
1365 | Object *found; | |
1366 | ||
5d9d3f47 | 1367 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
1368 | continue; |
1369 | } | |
1370 | ||
02fe2db6 PB |
1371 | found = object_resolve_partial_path(prop->opaque, parts, |
1372 | typename, ambiguous); | |
57c9fafe AL |
1373 | if (found) { |
1374 | if (obj) { | |
1375 | if (ambiguous) { | |
1376 | *ambiguous = true; | |
1377 | } | |
1378 | return NULL; | |
1379 | } | |
1380 | obj = found; | |
1381 | } | |
1382 | ||
1383 | if (ambiguous && *ambiguous) { | |
1384 | return NULL; | |
1385 | } | |
1386 | } | |
1387 | ||
1388 | return obj; | |
1389 | } | |
1390 | ||
02fe2db6 PB |
1391 | Object *object_resolve_path_type(const char *path, const char *typename, |
1392 | bool *ambiguous) | |
57c9fafe | 1393 | { |
57c9fafe AL |
1394 | Object *obj; |
1395 | gchar **parts; | |
1396 | ||
1397 | parts = g_strsplit(path, "/", 0); | |
2e1103f6 | 1398 | assert(parts); |
57c9fafe | 1399 | |
2e1103f6 | 1400 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
57c9fafe AL |
1401 | if (ambiguous) { |
1402 | *ambiguous = false; | |
1403 | } | |
02fe2db6 PB |
1404 | obj = object_resolve_partial_path(object_get_root(), parts, |
1405 | typename, ambiguous); | |
57c9fafe | 1406 | } else { |
02fe2db6 | 1407 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
57c9fafe AL |
1408 | } |
1409 | ||
1410 | g_strfreev(parts); | |
1411 | ||
1412 | return obj; | |
1413 | } | |
1414 | ||
02fe2db6 PB |
1415 | Object *object_resolve_path(const char *path, bool *ambiguous) |
1416 | { | |
1417 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); | |
1418 | } | |
1419 | ||
57c9fafe AL |
1420 | typedef struct StringProperty |
1421 | { | |
1422 | char *(*get)(Object *, Error **); | |
1423 | void (*set)(Object *, const char *, Error **); | |
1424 | } StringProperty; | |
1425 | ||
7b7b7d18 PB |
1426 | static void property_get_str(Object *obj, Visitor *v, void *opaque, |
1427 | const char *name, Error **errp) | |
57c9fafe AL |
1428 | { |
1429 | StringProperty *prop = opaque; | |
1430 | char *value; | |
1431 | ||
1432 | value = prop->get(obj, errp); | |
1433 | if (value) { | |
1434 | visit_type_str(v, &value, name, errp); | |
1435 | g_free(value); | |
1436 | } | |
1437 | } | |
1438 | ||
7b7b7d18 PB |
1439 | static void property_set_str(Object *obj, Visitor *v, void *opaque, |
1440 | const char *name, Error **errp) | |
57c9fafe AL |
1441 | { |
1442 | StringProperty *prop = opaque; | |
1443 | char *value; | |
1444 | Error *local_err = NULL; | |
1445 | ||
1446 | visit_type_str(v, &value, name, &local_err); | |
1447 | if (local_err) { | |
1448 | error_propagate(errp, local_err); | |
1449 | return; | |
1450 | } | |
1451 | ||
1452 | prop->set(obj, value, errp); | |
1453 | g_free(value); | |
1454 | } | |
1455 | ||
7b7b7d18 PB |
1456 | static void property_release_str(Object *obj, const char *name, |
1457 | void *opaque) | |
57c9fafe AL |
1458 | { |
1459 | StringProperty *prop = opaque; | |
1460 | g_free(prop); | |
1461 | } | |
1462 | ||
1463 | void object_property_add_str(Object *obj, const char *name, | |
1464 | char *(*get)(Object *, Error **), | |
1465 | void (*set)(Object *, const char *, Error **), | |
1466 | Error **errp) | |
1467 | { | |
a01aedc8 | 1468 | Error *local_err = NULL; |
57c9fafe AL |
1469 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
1470 | ||
1471 | prop->get = get; | |
1472 | prop->set = set; | |
1473 | ||
1474 | object_property_add(obj, name, "string", | |
7b7b7d18 PB |
1475 | get ? property_get_str : NULL, |
1476 | set ? property_set_str : NULL, | |
1477 | property_release_str, | |
a01aedc8 SH |
1478 | prop, &local_err); |
1479 | if (local_err) { | |
1480 | error_propagate(errp, local_err); | |
1481 | g_free(prop); | |
1482 | } | |
57c9fafe | 1483 | } |
745549c8 | 1484 | |
0e558843 AL |
1485 | typedef struct BoolProperty |
1486 | { | |
1487 | bool (*get)(Object *, Error **); | |
1488 | void (*set)(Object *, bool, Error **); | |
1489 | } BoolProperty; | |
1490 | ||
1491 | static void property_get_bool(Object *obj, Visitor *v, void *opaque, | |
1492 | const char *name, Error **errp) | |
1493 | { | |
1494 | BoolProperty *prop = opaque; | |
1495 | bool value; | |
1496 | ||
1497 | value = prop->get(obj, errp); | |
1498 | visit_type_bool(v, &value, name, errp); | |
1499 | } | |
1500 | ||
1501 | static void property_set_bool(Object *obj, Visitor *v, void *opaque, | |
1502 | const char *name, Error **errp) | |
1503 | { | |
1504 | BoolProperty *prop = opaque; | |
1505 | bool value; | |
1506 | Error *local_err = NULL; | |
1507 | ||
1508 | visit_type_bool(v, &value, name, &local_err); | |
1509 | if (local_err) { | |
1510 | error_propagate(errp, local_err); | |
1511 | return; | |
1512 | } | |
1513 | ||
1514 | prop->set(obj, value, errp); | |
1515 | } | |
1516 | ||
1517 | static void property_release_bool(Object *obj, const char *name, | |
1518 | void *opaque) | |
1519 | { | |
1520 | BoolProperty *prop = opaque; | |
1521 | g_free(prop); | |
1522 | } | |
1523 | ||
1524 | void object_property_add_bool(Object *obj, const char *name, | |
1525 | bool (*get)(Object *, Error **), | |
1526 | void (*set)(Object *, bool, Error **), | |
1527 | Error **errp) | |
1528 | { | |
a01aedc8 | 1529 | Error *local_err = NULL; |
0e558843 AL |
1530 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
1531 | ||
1532 | prop->get = get; | |
1533 | prop->set = set; | |
1534 | ||
1535 | object_property_add(obj, name, "bool", | |
1536 | get ? property_get_bool : NULL, | |
1537 | set ? property_set_bool : NULL, | |
1538 | property_release_bool, | |
a01aedc8 SH |
1539 | prop, &local_err); |
1540 | if (local_err) { | |
1541 | error_propagate(errp, local_err); | |
1542 | g_free(prop); | |
1543 | } | |
0e558843 AL |
1544 | } |
1545 | ||
2f262e06 PB |
1546 | static char *qdev_get_type(Object *obj, Error **errp) |
1547 | { | |
1548 | return g_strdup(object_get_typename(obj)); | |
1549 | } | |
1550 | ||
e732ea63 MT |
1551 | static void property_get_uint8_ptr(Object *obj, Visitor *v, |
1552 | void *opaque, const char *name, | |
1553 | Error **errp) | |
1554 | { | |
1555 | uint8_t value = *(uint8_t *)opaque; | |
1556 | visit_type_uint8(v, &value, name, errp); | |
1557 | } | |
1558 | ||
1559 | static void property_get_uint16_ptr(Object *obj, Visitor *v, | |
1560 | void *opaque, const char *name, | |
1561 | Error **errp) | |
1562 | { | |
1563 | uint16_t value = *(uint16_t *)opaque; | |
1564 | visit_type_uint16(v, &value, name, errp); | |
1565 | } | |
1566 | ||
1567 | static void property_get_uint32_ptr(Object *obj, Visitor *v, | |
1568 | void *opaque, const char *name, | |
1569 | Error **errp) | |
1570 | { | |
1571 | uint32_t value = *(uint32_t *)opaque; | |
1572 | visit_type_uint32(v, &value, name, errp); | |
1573 | } | |
1574 | ||
1575 | static void property_get_uint64_ptr(Object *obj, Visitor *v, | |
1576 | void *opaque, const char *name, | |
1577 | Error **errp) | |
1578 | { | |
1579 | uint64_t value = *(uint64_t *)opaque; | |
1580 | visit_type_uint64(v, &value, name, errp); | |
1581 | } | |
1582 | ||
1583 | void object_property_add_uint8_ptr(Object *obj, const char *name, | |
1584 | const uint8_t *v, Error **errp) | |
1585 | { | |
1586 | object_property_add(obj, name, "uint8", property_get_uint8_ptr, | |
1587 | NULL, NULL, (void *)v, errp); | |
1588 | } | |
1589 | ||
1590 | void object_property_add_uint16_ptr(Object *obj, const char *name, | |
1591 | const uint16_t *v, Error **errp) | |
1592 | { | |
1593 | object_property_add(obj, name, "uint16", property_get_uint16_ptr, | |
1594 | NULL, NULL, (void *)v, errp); | |
1595 | } | |
1596 | ||
1597 | void object_property_add_uint32_ptr(Object *obj, const char *name, | |
1598 | const uint32_t *v, Error **errp) | |
1599 | { | |
1600 | object_property_add(obj, name, "uint32", property_get_uint32_ptr, | |
1601 | NULL, NULL, (void *)v, errp); | |
1602 | } | |
1603 | ||
1604 | void object_property_add_uint64_ptr(Object *obj, const char *name, | |
1605 | const uint64_t *v, Error **errp) | |
1606 | { | |
1607 | object_property_add(obj, name, "uint64", property_get_uint64_ptr, | |
1608 | NULL, NULL, (void *)v, errp); | |
1609 | } | |
1610 | ||
ef7c7ff6 SH |
1611 | typedef struct { |
1612 | Object *target_obj; | |
1613 | const char *target_name; | |
1614 | } AliasProperty; | |
1615 | ||
1616 | static void property_get_alias(Object *obj, struct Visitor *v, void *opaque, | |
1617 | const char *name, Error **errp) | |
1618 | { | |
1619 | AliasProperty *prop = opaque; | |
1620 | ||
1621 | object_property_get(prop->target_obj, v, prop->target_name, errp); | |
1622 | } | |
1623 | ||
1624 | static void property_set_alias(Object *obj, struct Visitor *v, void *opaque, | |
1625 | const char *name, Error **errp) | |
1626 | { | |
1627 | AliasProperty *prop = opaque; | |
1628 | ||
1629 | object_property_set(prop->target_obj, v, prop->target_name, errp); | |
1630 | } | |
1631 | ||
64607d08 PB |
1632 | static Object *property_resolve_alias(Object *obj, void *opaque, |
1633 | const gchar *part) | |
1634 | { | |
1635 | AliasProperty *prop = opaque; | |
1636 | ||
1637 | return object_resolve_path_component(prop->target_obj, prop->target_name); | |
1638 | } | |
1639 | ||
ef7c7ff6 SH |
1640 | static void property_release_alias(Object *obj, const char *name, void *opaque) |
1641 | { | |
1642 | AliasProperty *prop = opaque; | |
1643 | ||
1644 | g_free(prop); | |
1645 | } | |
1646 | ||
1647 | void object_property_add_alias(Object *obj, const char *name, | |
1648 | Object *target_obj, const char *target_name, | |
1649 | Error **errp) | |
1650 | { | |
1651 | AliasProperty *prop; | |
64607d08 | 1652 | ObjectProperty *op; |
ef7c7ff6 | 1653 | ObjectProperty *target_prop; |
d190698e | 1654 | gchar *prop_type; |
8ae9a9ef | 1655 | Error *local_err = NULL; |
ef7c7ff6 SH |
1656 | |
1657 | target_prop = object_property_find(target_obj, target_name, errp); | |
1658 | if (!target_prop) { | |
1659 | return; | |
1660 | } | |
1661 | ||
d190698e PB |
1662 | if (object_property_is_child(target_prop)) { |
1663 | prop_type = g_strdup_printf("link%s", | |
1664 | target_prop->type + strlen("child")); | |
1665 | } else { | |
1666 | prop_type = g_strdup(target_prop->type); | |
1667 | } | |
1668 | ||
ef7c7ff6 SH |
1669 | prop = g_malloc(sizeof(*prop)); |
1670 | prop->target_obj = target_obj; | |
1671 | prop->target_name = target_name; | |
1672 | ||
d190698e | 1673 | op = object_property_add(obj, name, prop_type, |
64607d08 PB |
1674 | property_get_alias, |
1675 | property_set_alias, | |
1676 | property_release_alias, | |
8ae9a9ef GA |
1677 | prop, &local_err); |
1678 | if (local_err) { | |
1679 | error_propagate(errp, local_err); | |
1680 | g_free(prop); | |
1681 | goto out; | |
1682 | } | |
64607d08 | 1683 | op->resolve = property_resolve_alias; |
d190698e | 1684 | |
80742642 GA |
1685 | object_property_set_description(obj, name, |
1686 | target_prop->description, | |
1687 | &error_abort); | |
1688 | ||
8ae9a9ef | 1689 | out: |
d190698e | 1690 | g_free(prop_type); |
ef7c7ff6 SH |
1691 | } |
1692 | ||
80742642 GA |
1693 | void object_property_set_description(Object *obj, const char *name, |
1694 | const char *description, Error **errp) | |
1695 | { | |
1696 | ObjectProperty *op; | |
1697 | ||
1698 | op = object_property_find(obj, name, errp); | |
1699 | if (!op) { | |
1700 | return; | |
1701 | } | |
1702 | ||
1703 | g_free(op->description); | |
1704 | op->description = g_strdup(description); | |
1705 | } | |
1706 | ||
2f262e06 PB |
1707 | static void object_instance_init(Object *obj) |
1708 | { | |
1709 | object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); | |
1710 | } | |
1711 | ||
745549c8 PB |
1712 | static void register_types(void) |
1713 | { | |
1714 | static TypeInfo interface_info = { | |
1715 | .name = TYPE_INTERFACE, | |
33e95c63 | 1716 | .class_size = sizeof(InterfaceClass), |
745549c8 PB |
1717 | .abstract = true, |
1718 | }; | |
1719 | ||
1720 | static TypeInfo object_info = { | |
1721 | .name = TYPE_OBJECT, | |
1722 | .instance_size = sizeof(Object), | |
2f262e06 | 1723 | .instance_init = object_instance_init, |
745549c8 PB |
1724 | .abstract = true, |
1725 | }; | |
1726 | ||
049cb3cf PB |
1727 | type_interface = type_register_internal(&interface_info); |
1728 | type_register_internal(&object_info); | |
745549c8 PB |
1729 | } |
1730 | ||
1731 | type_init(register_types) |