]>
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); | |
372 | g_free(prop); | |
373 | } | |
374 | } | |
375 | ||
376 | static void object_property_del_child(Object *obj, Object *child, Error **errp) | |
377 | { | |
378 | ObjectProperty *prop; | |
379 | ||
380 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
5d9d3f47 | 381 | if (object_property_is_child(prop) && prop->opaque == child) { |
57c9fafe | 382 | object_property_del(obj, prop->name, errp); |
6c1fdcf9 | 383 | break; |
57c9fafe AL |
384 | } |
385 | } | |
386 | } | |
387 | ||
388 | void object_unparent(Object *obj) | |
389 | { | |
e998fa8d MT |
390 | if (obj->parent) { |
391 | object_property_del_child(obj->parent, obj, NULL); | |
392 | } | |
57c9fafe AL |
393 | } |
394 | ||
2f28d2ff AL |
395 | static void object_deinit(Object *obj, TypeImpl *type) |
396 | { | |
397 | if (type->instance_finalize) { | |
398 | type->instance_finalize(obj); | |
399 | } | |
400 | ||
2f28d2ff AL |
401 | if (type_has_parent(type)) { |
402 | object_deinit(obj, type_get_parent(type)); | |
403 | } | |
404 | } | |
405 | ||
339c2708 | 406 | static void object_finalize(void *data) |
2f28d2ff AL |
407 | { |
408 | Object *obj = data; | |
409 | TypeImpl *ti = obj->class->type; | |
410 | ||
57c9fafe | 411 | object_property_del_all(obj); |
76a6e1cc | 412 | object_deinit(obj, ti); |
db85b575 AL |
413 | |
414 | g_assert(obj->ref == 0); | |
fde9bf44 PB |
415 | if (obj->free) { |
416 | obj->free(obj); | |
417 | } | |
2f28d2ff AL |
418 | } |
419 | ||
420 | Object *object_new_with_type(Type type) | |
421 | { | |
422 | Object *obj; | |
423 | ||
424 | g_assert(type != NULL); | |
ac451033 | 425 | type_initialize(type); |
2f28d2ff AL |
426 | |
427 | obj = g_malloc(type->instance_size); | |
5b9237f6 | 428 | object_initialize_with_type(obj, type->instance_size, type); |
fde9bf44 | 429 | obj->free = g_free; |
2f28d2ff AL |
430 | |
431 | return obj; | |
432 | } | |
433 | ||
434 | Object *object_new(const char *typename) | |
435 | { | |
436 | TypeImpl *ti = type_get_by_name(typename); | |
437 | ||
438 | return object_new_with_type(ti); | |
439 | } | |
440 | ||
2f28d2ff AL |
441 | Object *object_dynamic_cast(Object *obj, const char *typename) |
442 | { | |
b7f43fe4 | 443 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
acc4af3f PB |
444 | return obj; |
445 | } | |
446 | ||
2f28d2ff AL |
447 | return NULL; |
448 | } | |
449 | ||
be17f18b PB |
450 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
451 | const char *file, int line, const char *func) | |
2f28d2ff | 452 | { |
fa131d94 PB |
453 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
454 | typename, file, line, func); | |
455 | ||
3556c233 | 456 | #ifdef CONFIG_QOM_CAST_DEBUG |
03587328 AL |
457 | int i; |
458 | Object *inst; | |
459 | ||
95916abc | 460 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c | 461 | if (obj->class->object_cast_cache[i] == typename) { |
03587328 AL |
462 | goto out; |
463 | } | |
464 | } | |
465 | ||
466 | inst = object_dynamic_cast(obj, typename); | |
2f28d2ff | 467 | |
b7f43fe4 | 468 | if (!inst && obj) { |
be17f18b PB |
469 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
470 | file, line, func, obj, typename); | |
2f28d2ff AL |
471 | abort(); |
472 | } | |
473 | ||
3556c233 | 474 | assert(obj == inst); |
03587328 | 475 | |
95916abc | 476 | if (obj && obj == inst) { |
03587328 | 477 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c PC |
478 | obj->class->object_cast_cache[i - 1] = |
479 | obj->class->object_cast_cache[i]; | |
03587328 | 480 | } |
0ab4c94c | 481 | obj->class->object_cast_cache[i - 1] = typename; |
03587328 AL |
482 | } |
483 | ||
484 | out: | |
3556c233 PB |
485 | #endif |
486 | return obj; | |
2f28d2ff AL |
487 | } |
488 | ||
489 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
490 | const char *typename) | |
491 | { | |
33e95c63 | 492 | ObjectClass *ret = NULL; |
bf0fda34 PB |
493 | TypeImpl *target_type; |
494 | TypeImpl *type; | |
2f28d2ff | 495 | |
bf0fda34 PB |
496 | if (!class) { |
497 | return NULL; | |
498 | } | |
499 | ||
793c96b5 | 500 | /* A simple fast path that can trigger a lot for leaf classes. */ |
bf0fda34 | 501 | type = class->type; |
793c96b5 PB |
502 | if (type->name == typename) { |
503 | return class; | |
504 | } | |
505 | ||
bf0fda34 | 506 | target_type = type_get_by_name(typename); |
9ab880b3 AG |
507 | if (!target_type) { |
508 | /* target class type unknown, so fail the cast */ | |
509 | return NULL; | |
510 | } | |
511 | ||
00e2ceae PC |
512 | if (type->class->interfaces && |
513 | type_is_ancestor(target_type, type_interface)) { | |
33e95c63 AL |
514 | int found = 0; |
515 | GSList *i; | |
2f28d2ff | 516 | |
33e95c63 AL |
517 | for (i = class->interfaces; i; i = i->next) { |
518 | ObjectClass *target_class = i->data; | |
519 | ||
520 | if (type_is_ancestor(target_class->type, target_type)) { | |
521 | ret = target_class; | |
522 | found++; | |
523 | } | |
524 | } | |
525 | ||
526 | /* The match was ambiguous, don't allow a cast */ | |
527 | if (found > 1) { | |
528 | ret = NULL; | |
529 | } | |
530 | } else if (type_is_ancestor(type, target_type)) { | |
531 | ret = class; | |
2f28d2ff AL |
532 | } |
533 | ||
33e95c63 | 534 | return ret; |
2f28d2ff AL |
535 | } |
536 | ||
537 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
be17f18b PB |
538 | const char *typename, |
539 | const char *file, int line, | |
540 | const char *func) | |
2f28d2ff | 541 | { |
fa131d94 PB |
542 | ObjectClass *ret; |
543 | ||
544 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", | |
545 | typename, file, line, func); | |
2f28d2ff | 546 | |
03587328 AL |
547 | #ifdef CONFIG_QOM_CAST_DEBUG |
548 | int i; | |
549 | ||
9d6a3d58 | 550 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c | 551 | if (class->class_cast_cache[i] == typename) { |
03587328 AL |
552 | ret = class; |
553 | goto out; | |
554 | } | |
555 | } | |
556 | #else | |
9d6a3d58 | 557 | if (!class || !class->interfaces) { |
3556c233 PB |
558 | return class; |
559 | } | |
560 | #endif | |
561 | ||
fa131d94 | 562 | ret = object_class_dynamic_cast(class, typename); |
bf0fda34 | 563 | if (!ret && class) { |
be17f18b PB |
564 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
565 | file, line, func, class, typename); | |
2f28d2ff AL |
566 | abort(); |
567 | } | |
568 | ||
03587328 | 569 | #ifdef CONFIG_QOM_CAST_DEBUG |
9d6a3d58 | 570 | if (class && ret == class) { |
03587328 | 571 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c | 572 | class->class_cast_cache[i - 1] = class->class_cast_cache[i]; |
03587328 | 573 | } |
0ab4c94c | 574 | class->class_cast_cache[i - 1] = typename; |
03587328 AL |
575 | } |
576 | out: | |
577 | #endif | |
2f28d2ff AL |
578 | return ret; |
579 | } | |
580 | ||
581 | const char *object_get_typename(Object *obj) | |
582 | { | |
583 | return obj->class->type->name; | |
584 | } | |
585 | ||
586 | ObjectClass *object_get_class(Object *obj) | |
587 | { | |
588 | return obj->class; | |
589 | } | |
590 | ||
17862378 AF |
591 | bool object_class_is_abstract(ObjectClass *klass) |
592 | { | |
593 | return klass->type->abstract; | |
594 | } | |
595 | ||
2f28d2ff AL |
596 | const char *object_class_get_name(ObjectClass *klass) |
597 | { | |
598 | return klass->type->name; | |
599 | } | |
600 | ||
601 | ObjectClass *object_class_by_name(const char *typename) | |
602 | { | |
603 | TypeImpl *type = type_get_by_name(typename); | |
604 | ||
605 | if (!type) { | |
606 | return NULL; | |
607 | } | |
608 | ||
ac451033 | 609 | type_initialize(type); |
2f28d2ff AL |
610 | |
611 | return type->class; | |
612 | } | |
613 | ||
e7cce67f PB |
614 | ObjectClass *object_class_get_parent(ObjectClass *class) |
615 | { | |
616 | TypeImpl *type = type_get_parent(class->type); | |
617 | ||
618 | if (!type) { | |
619 | return NULL; | |
620 | } | |
621 | ||
622 | type_initialize(type); | |
623 | ||
624 | return type->class; | |
625 | } | |
626 | ||
2f28d2ff AL |
627 | typedef struct OCFData |
628 | { | |
629 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
630 | const char *implements_type; |
631 | bool include_abstract; | |
2f28d2ff AL |
632 | void *opaque; |
633 | } OCFData; | |
634 | ||
635 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
636 | gpointer opaque) | |
637 | { | |
638 | OCFData *data = opaque; | |
639 | TypeImpl *type = value; | |
93c511a1 | 640 | ObjectClass *k; |
2f28d2ff | 641 | |
ac451033 | 642 | type_initialize(type); |
93c511a1 | 643 | k = type->class; |
2f28d2ff | 644 | |
93c511a1 AL |
645 | if (!data->include_abstract && type->abstract) { |
646 | return; | |
647 | } | |
648 | ||
649 | if (data->implements_type && | |
650 | !object_class_dynamic_cast(k, data->implements_type)) { | |
651 | return; | |
652 | } | |
653 | ||
654 | data->fn(k, data->opaque); | |
2f28d2ff AL |
655 | } |
656 | ||
657 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 658 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
659 | void *opaque) |
660 | { | |
93c511a1 | 661 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff | 662 | |
f54c19ca | 663 | enumerating_types = true; |
2f28d2ff | 664 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
f54c19ca | 665 | enumerating_types = false; |
2f28d2ff | 666 | } |
57c9fafe | 667 | |
32efc535 PB |
668 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
669 | void *opaque) | |
670 | { | |
8af734ca | 671 | ObjectProperty *prop, *next; |
32efc535 PB |
672 | int ret = 0; |
673 | ||
8af734ca | 674 | QTAILQ_FOREACH_SAFE(prop, &obj->properties, node, next) { |
32efc535 PB |
675 | if (object_property_is_child(prop)) { |
676 | ret = fn(prop->opaque, opaque); | |
677 | if (ret != 0) { | |
678 | break; | |
679 | } | |
680 | } | |
681 | } | |
682 | return ret; | |
683 | } | |
684 | ||
418ba9e5 AF |
685 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
686 | { | |
687 | GSList **list = opaque; | |
688 | ||
689 | *list = g_slist_prepend(*list, klass); | |
690 | } | |
691 | ||
692 | GSList *object_class_get_list(const char *implements_type, | |
693 | bool include_abstract) | |
694 | { | |
695 | GSList *list = NULL; | |
696 | ||
697 | object_class_foreach(object_class_get_list_tramp, | |
698 | implements_type, include_abstract, &list); | |
699 | return list; | |
700 | } | |
701 | ||
57c9fafe AL |
702 | void object_ref(Object *obj) |
703 | { | |
8ffad850 PC |
704 | if (!obj) { |
705 | return; | |
706 | } | |
f08c03f3 | 707 | atomic_inc(&obj->ref); |
57c9fafe AL |
708 | } |
709 | ||
710 | void object_unref(Object *obj) | |
711 | { | |
8ffad850 PC |
712 | if (!obj) { |
713 | return; | |
714 | } | |
57c9fafe | 715 | g_assert(obj->ref > 0); |
57c9fafe AL |
716 | |
717 | /* parent always holds a reference to its children */ | |
f08c03f3 | 718 | if (atomic_fetch_dec(&obj->ref) == 1) { |
57c9fafe AL |
719 | object_finalize(obj); |
720 | } | |
721 | } | |
722 | ||
64607d08 PB |
723 | ObjectProperty * |
724 | object_property_add(Object *obj, const char *name, const char *type, | |
725 | ObjectPropertyAccessor *get, | |
726 | ObjectPropertyAccessor *set, | |
727 | ObjectPropertyRelease *release, | |
728 | void *opaque, Error **errp) | |
57c9fafe | 729 | { |
54852b03 | 730 | ObjectProperty *prop; |
33965904 PC |
731 | size_t name_len = strlen(name); |
732 | ||
733 | if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { | |
734 | int i; | |
735 | ObjectProperty *ret; | |
736 | char *name_no_array = g_strdup(name); | |
737 | ||
738 | name_no_array[name_len - 3] = '\0'; | |
739 | for (i = 0; ; ++i) { | |
740 | char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); | |
741 | ||
742 | ret = object_property_add(obj, full_name, type, get, set, | |
743 | release, opaque, NULL); | |
744 | g_free(full_name); | |
745 | if (ret) { | |
746 | break; | |
747 | } | |
748 | } | |
749 | g_free(name_no_array); | |
750 | return ret; | |
751 | } | |
54852b03 PM |
752 | |
753 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
754 | if (strcmp(prop->name, name) == 0) { | |
755 | error_setg(errp, "attempt to add duplicate property '%s'" | |
756 | " to object (type '%s')", name, | |
757 | object_get_typename(obj)); | |
64607d08 | 758 | return NULL; |
54852b03 PM |
759 | } |
760 | } | |
761 | ||
762 | prop = g_malloc0(sizeof(*prop)); | |
57c9fafe AL |
763 | |
764 | prop->name = g_strdup(name); | |
765 | prop->type = g_strdup(type); | |
766 | ||
767 | prop->get = get; | |
768 | prop->set = set; | |
769 | prop->release = release; | |
770 | prop->opaque = opaque; | |
771 | ||
772 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); | |
64607d08 | 773 | return prop; |
57c9fafe AL |
774 | } |
775 | ||
89bfe000 PB |
776 | ObjectProperty *object_property_find(Object *obj, const char *name, |
777 | Error **errp) | |
57c9fafe AL |
778 | { |
779 | ObjectProperty *prop; | |
780 | ||
781 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
782 | if (strcmp(prop->name, name) == 0) { | |
783 | return prop; | |
784 | } | |
785 | } | |
786 | ||
f231b88d | 787 | error_setg(errp, "Property '.%s' not found", name); |
57c9fafe AL |
788 | return NULL; |
789 | } | |
790 | ||
791 | void object_property_del(Object *obj, const char *name, Error **errp) | |
792 | { | |
89bfe000 | 793 | ObjectProperty *prop = object_property_find(obj, name, errp); |
0866aca1 | 794 | if (prop == NULL) { |
0866aca1 AL |
795 | return; |
796 | } | |
57c9fafe | 797 | |
0866aca1 AL |
798 | if (prop->release) { |
799 | prop->release(obj, name, prop->opaque); | |
800 | } | |
801 | ||
802 | QTAILQ_REMOVE(&obj->properties, prop, node); | |
57c9fafe AL |
803 | |
804 | g_free(prop->name); | |
805 | g_free(prop->type); | |
806 | g_free(prop); | |
807 | } | |
808 | ||
809 | void object_property_get(Object *obj, Visitor *v, const char *name, | |
810 | Error **errp) | |
811 | { | |
89bfe000 | 812 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 813 | if (prop == NULL) { |
57c9fafe AL |
814 | return; |
815 | } | |
816 | ||
817 | if (!prop->get) { | |
818 | error_set(errp, QERR_PERMISSION_DENIED); | |
819 | } else { | |
820 | prop->get(obj, v, prop->opaque, name, errp); | |
821 | } | |
822 | } | |
823 | ||
824 | void object_property_set(Object *obj, Visitor *v, const char *name, | |
825 | Error **errp) | |
826 | { | |
89bfe000 | 827 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 828 | if (prop == NULL) { |
57c9fafe AL |
829 | return; |
830 | } | |
831 | ||
832 | if (!prop->set) { | |
833 | error_set(errp, QERR_PERMISSION_DENIED); | |
834 | } else { | |
835 | prop->set(obj, v, prop->opaque, name, errp); | |
836 | } | |
837 | } | |
838 | ||
7b7b7d18 PB |
839 | void object_property_set_str(Object *obj, const char *value, |
840 | const char *name, Error **errp) | |
841 | { | |
842 | QString *qstr = qstring_from_str(value); | |
843 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); | |
844 | ||
845 | QDECREF(qstr); | |
846 | } | |
847 | ||
848 | char *object_property_get_str(Object *obj, const char *name, | |
849 | Error **errp) | |
850 | { | |
851 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
852 | QString *qstring; | |
853 | char *retval; | |
854 | ||
855 | if (!ret) { | |
856 | return NULL; | |
857 | } | |
858 | qstring = qobject_to_qstring(ret); | |
859 | if (!qstring) { | |
860 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); | |
861 | retval = NULL; | |
862 | } else { | |
863 | retval = g_strdup(qstring_get_str(qstring)); | |
864 | } | |
865 | ||
866 | QDECREF(qstring); | |
867 | return retval; | |
868 | } | |
869 | ||
1d9c5a12 PB |
870 | void object_property_set_link(Object *obj, Object *value, |
871 | const char *name, Error **errp) | |
872 | { | |
2d3aa28c VY |
873 | gchar *path = object_get_canonical_path(value); |
874 | object_property_set_str(obj, path, name, errp); | |
875 | g_free(path); | |
1d9c5a12 PB |
876 | } |
877 | ||
878 | Object *object_property_get_link(Object *obj, const char *name, | |
879 | Error **errp) | |
880 | { | |
881 | char *str = object_property_get_str(obj, name, errp); | |
882 | Object *target = NULL; | |
883 | ||
884 | if (str && *str) { | |
885 | target = object_resolve_path(str, NULL); | |
886 | if (!target) { | |
887 | error_set(errp, QERR_DEVICE_NOT_FOUND, str); | |
888 | } | |
889 | } | |
890 | ||
891 | g_free(str); | |
892 | return target; | |
893 | } | |
894 | ||
7b7b7d18 PB |
895 | void object_property_set_bool(Object *obj, bool value, |
896 | const char *name, Error **errp) | |
897 | { | |
898 | QBool *qbool = qbool_from_int(value); | |
899 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); | |
900 | ||
901 | QDECREF(qbool); | |
902 | } | |
903 | ||
904 | bool object_property_get_bool(Object *obj, const char *name, | |
905 | Error **errp) | |
906 | { | |
907 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
908 | QBool *qbool; | |
909 | bool retval; | |
910 | ||
911 | if (!ret) { | |
912 | return false; | |
913 | } | |
914 | qbool = qobject_to_qbool(ret); | |
915 | if (!qbool) { | |
916 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); | |
917 | retval = false; | |
918 | } else { | |
919 | retval = qbool_get_int(qbool); | |
920 | } | |
921 | ||
922 | QDECREF(qbool); | |
923 | return retval; | |
924 | } | |
925 | ||
926 | void object_property_set_int(Object *obj, int64_t value, | |
927 | const char *name, Error **errp) | |
928 | { | |
929 | QInt *qint = qint_from_int(value); | |
930 | object_property_set_qobject(obj, QOBJECT(qint), name, errp); | |
931 | ||
932 | QDECREF(qint); | |
933 | } | |
934 | ||
935 | int64_t object_property_get_int(Object *obj, const char *name, | |
936 | Error **errp) | |
937 | { | |
938 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
939 | QInt *qint; | |
940 | int64_t retval; | |
941 | ||
942 | if (!ret) { | |
943 | return -1; | |
944 | } | |
945 | qint = qobject_to_qint(ret); | |
946 | if (!qint) { | |
947 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); | |
948 | retval = -1; | |
949 | } else { | |
950 | retval = qint_get_int(qint); | |
951 | } | |
952 | ||
953 | QDECREF(qint); | |
954 | return retval; | |
b2cd7dee PB |
955 | } |
956 | ||
1f21772d HT |
957 | int object_property_get_enum(Object *obj, const char *name, |
958 | const char *strings[], Error **errp) | |
959 | { | |
960 | StringOutputVisitor *sov; | |
961 | StringInputVisitor *siv; | |
976620ac | 962 | char *str; |
1f21772d HT |
963 | int ret; |
964 | ||
965 | sov = string_output_visitor_new(false); | |
966 | object_property_get(obj, string_output_get_visitor(sov), name, errp); | |
976620ac CF |
967 | str = string_output_get_string(sov); |
968 | siv = string_input_visitor_new(str); | |
1f21772d HT |
969 | string_output_visitor_cleanup(sov); |
970 | visit_type_enum(string_input_get_visitor(siv), | |
971 | &ret, strings, NULL, name, errp); | |
976620ac CF |
972 | |
973 | g_free(str); | |
1f21772d HT |
974 | string_input_visitor_cleanup(siv); |
975 | ||
976 | return ret; | |
977 | } | |
978 | ||
979 | void object_property_get_uint16List(Object *obj, const char *name, | |
980 | uint16List **list, Error **errp) | |
981 | { | |
982 | StringOutputVisitor *ov; | |
983 | StringInputVisitor *iv; | |
976620ac | 984 | char *str; |
1f21772d HT |
985 | |
986 | ov = string_output_visitor_new(false); | |
987 | object_property_get(obj, string_output_get_visitor(ov), | |
988 | name, errp); | |
976620ac CF |
989 | str = string_output_get_string(ov); |
990 | iv = string_input_visitor_new(str); | |
1f21772d HT |
991 | visit_type_uint16List(string_input_get_visitor(iv), |
992 | list, NULL, errp); | |
976620ac CF |
993 | |
994 | g_free(str); | |
1f21772d HT |
995 | string_output_visitor_cleanup(ov); |
996 | string_input_visitor_cleanup(iv); | |
997 | } | |
998 | ||
b2cd7dee PB |
999 | void object_property_parse(Object *obj, const char *string, |
1000 | const char *name, Error **errp) | |
1001 | { | |
1002 | StringInputVisitor *mi; | |
1003 | mi = string_input_visitor_new(string); | |
1004 | object_property_set(obj, string_input_get_visitor(mi), name, errp); | |
1005 | ||
1006 | string_input_visitor_cleanup(mi); | |
1007 | } | |
1008 | ||
0b7593e0 | 1009 | char *object_property_print(Object *obj, const char *name, bool human, |
b2cd7dee PB |
1010 | Error **errp) |
1011 | { | |
1012 | StringOutputVisitor *mo; | |
3a53009f GA |
1013 | char *string = NULL; |
1014 | Error *local_err = NULL; | |
b2cd7dee | 1015 | |
0b7593e0 | 1016 | mo = string_output_visitor_new(human); |
3a53009f GA |
1017 | object_property_get(obj, string_output_get_visitor(mo), name, &local_err); |
1018 | if (local_err) { | |
1019 | error_propagate(errp, local_err); | |
1020 | goto out; | |
1021 | } | |
1022 | ||
b2cd7dee | 1023 | string = string_output_get_string(mo); |
3a53009f GA |
1024 | |
1025 | out: | |
b2cd7dee PB |
1026 | string_output_visitor_cleanup(mo); |
1027 | return string; | |
7b7b7d18 PB |
1028 | } |
1029 | ||
57c9fafe AL |
1030 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
1031 | { | |
89bfe000 | 1032 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1033 | if (prop == NULL) { |
57c9fafe AL |
1034 | return NULL; |
1035 | } | |
1036 | ||
1037 | return prop->type; | |
1038 | } | |
1039 | ||
1040 | Object *object_get_root(void) | |
1041 | { | |
8b45d447 | 1042 | static Object *root; |
57c9fafe | 1043 | |
8b45d447 AL |
1044 | if (!root) { |
1045 | root = object_new("container"); | |
57c9fafe AL |
1046 | } |
1047 | ||
8b45d447 | 1048 | return root; |
57c9fafe AL |
1049 | } |
1050 | ||
1051 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, | |
1052 | const char *name, Error **errp) | |
1053 | { | |
1054 | Object *child = opaque; | |
1055 | gchar *path; | |
1056 | ||
1057 | path = object_get_canonical_path(child); | |
1058 | visit_type_str(v, &path, name, errp); | |
1059 | g_free(path); | |
1060 | } | |
1061 | ||
64607d08 PB |
1062 | static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part) |
1063 | { | |
1064 | return opaque; | |
1065 | } | |
1066 | ||
db85b575 AL |
1067 | static void object_finalize_child_property(Object *obj, const char *name, |
1068 | void *opaque) | |
1069 | { | |
1070 | Object *child = opaque; | |
1071 | ||
bffc687d PB |
1072 | if (child->class->unparent) { |
1073 | (child->class->unparent)(child); | |
1074 | } | |
1075 | child->parent = NULL; | |
db85b575 AL |
1076 | object_unref(child); |
1077 | } | |
1078 | ||
57c9fafe AL |
1079 | void object_property_add_child(Object *obj, const char *name, |
1080 | Object *child, Error **errp) | |
1081 | { | |
b0ed5e9f | 1082 | Error *local_err = NULL; |
57c9fafe | 1083 | gchar *type; |
64607d08 | 1084 | ObjectProperty *op; |
57c9fafe AL |
1085 | |
1086 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); | |
1087 | ||
64607d08 PB |
1088 | op = object_property_add(obj, name, type, object_get_child_property, NULL, |
1089 | object_finalize_child_property, child, &local_err); | |
b0ed5e9f PB |
1090 | if (local_err) { |
1091 | error_propagate(errp, local_err); | |
1092 | goto out; | |
1093 | } | |
64607d08 PB |
1094 | |
1095 | op->resolve = object_resolve_child_property; | |
57c9fafe AL |
1096 | object_ref(child); |
1097 | g_assert(child->parent == NULL); | |
1098 | child->parent = obj; | |
1099 | ||
b0ed5e9f | 1100 | out: |
57c9fafe AL |
1101 | g_free(type); |
1102 | } | |
1103 | ||
39f72ef9 SH |
1104 | void object_property_allow_set_link(Object *obj, const char *name, |
1105 | Object *val, Error **errp) | |
1106 | { | |
1107 | /* Allow the link to be set, always */ | |
1108 | } | |
1109 | ||
9561fda8 SH |
1110 | typedef struct { |
1111 | Object **child; | |
39f72ef9 | 1112 | void (*check)(Object *, const char *, Object *, Error **); |
9561fda8 SH |
1113 | ObjectPropertyLinkFlags flags; |
1114 | } LinkProperty; | |
1115 | ||
57c9fafe AL |
1116 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, |
1117 | const char *name, Error **errp) | |
1118 | { | |
9561fda8 SH |
1119 | LinkProperty *lprop = opaque; |
1120 | Object **child = lprop->child; | |
57c9fafe AL |
1121 | gchar *path; |
1122 | ||
1123 | if (*child) { | |
1124 | path = object_get_canonical_path(*child); | |
1125 | visit_type_str(v, &path, name, errp); | |
1126 | g_free(path); | |
1127 | } else { | |
1128 | path = (gchar *)""; | |
1129 | visit_type_str(v, &path, name, errp); | |
1130 | } | |
1131 | } | |
1132 | ||
f5ec6704 SH |
1133 | /* |
1134 | * object_resolve_link: | |
1135 | * | |
1136 | * Lookup an object and ensure its type matches the link property type. This | |
1137 | * is similar to object_resolve_path() except type verification against the | |
1138 | * link property is performed. | |
1139 | * | |
1140 | * Returns: The matched object or NULL on path lookup failures. | |
1141 | */ | |
1142 | static Object *object_resolve_link(Object *obj, const char *name, | |
1143 | const char *path, Error **errp) | |
1144 | { | |
1145 | const char *type; | |
1146 | gchar *target_type; | |
1147 | bool ambiguous = false; | |
1148 | Object *target; | |
1149 | ||
1150 | /* Go from link<FOO> to FOO. */ | |
1151 | type = object_property_get_type(obj, name, NULL); | |
1152 | target_type = g_strndup(&type[5], strlen(type) - 6); | |
1153 | target = object_resolve_path_type(path, target_type, &ambiguous); | |
1154 | ||
1155 | if (ambiguous) { | |
f231b88d CR |
1156 | error_set(errp, ERROR_CLASS_GENERIC_ERROR, |
1157 | "Path '%s' does not uniquely identify an object", path); | |
f5ec6704 SH |
1158 | } else if (!target) { |
1159 | target = object_resolve_path(path, &ambiguous); | |
1160 | if (target || ambiguous) { | |
1161 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); | |
1162 | } else { | |
1163 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); | |
1164 | } | |
1165 | target = NULL; | |
1166 | } | |
1167 | g_free(target_type); | |
1168 | ||
1169 | return target; | |
1170 | } | |
1171 | ||
57c9fafe AL |
1172 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, |
1173 | const char *name, Error **errp) | |
1174 | { | |
c6aed983 | 1175 | Error *local_err = NULL; |
9561fda8 SH |
1176 | LinkProperty *prop = opaque; |
1177 | Object **child = prop->child; | |
c6aed983 SH |
1178 | Object *old_target = *child; |
1179 | Object *new_target = NULL; | |
1180 | char *path = NULL; | |
57c9fafe | 1181 | |
c6aed983 | 1182 | visit_type_str(v, &path, name, &local_err); |
57c9fafe | 1183 | |
c6aed983 SH |
1184 | if (!local_err && strcmp(path, "") != 0) { |
1185 | new_target = object_resolve_link(obj, name, path, &local_err); | |
57c9fafe AL |
1186 | } |
1187 | ||
1188 | g_free(path); | |
c6aed983 SH |
1189 | if (local_err) { |
1190 | error_propagate(errp, local_err); | |
1191 | return; | |
1192 | } | |
f0cdc966 | 1193 | |
39f72ef9 SH |
1194 | prop->check(obj, name, new_target, &local_err); |
1195 | if (local_err) { | |
1196 | error_propagate(errp, local_err); | |
1197 | return; | |
1198 | } | |
1199 | ||
8ffad850 | 1200 | object_ref(new_target); |
c6aed983 | 1201 | *child = new_target; |
8ffad850 | 1202 | object_unref(old_target); |
57c9fafe AL |
1203 | } |
1204 | ||
64607d08 PB |
1205 | static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part) |
1206 | { | |
1207 | LinkProperty *lprop = opaque; | |
1208 | ||
1209 | return *lprop->child; | |
1210 | } | |
1211 | ||
9561fda8 SH |
1212 | static void object_release_link_property(Object *obj, const char *name, |
1213 | void *opaque) | |
1214 | { | |
1215 | LinkProperty *prop = opaque; | |
1216 | ||
1217 | if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { | |
1218 | object_unref(*prop->child); | |
1219 | } | |
1220 | g_free(prop); | |
1221 | } | |
1222 | ||
57c9fafe AL |
1223 | void object_property_add_link(Object *obj, const char *name, |
1224 | const char *type, Object **child, | |
39f72ef9 SH |
1225 | void (*check)(Object *, const char *, |
1226 | Object *, Error **), | |
9561fda8 | 1227 | ObjectPropertyLinkFlags flags, |
57c9fafe AL |
1228 | Error **errp) |
1229 | { | |
9561fda8 SH |
1230 | Error *local_err = NULL; |
1231 | LinkProperty *prop = g_malloc(sizeof(*prop)); | |
57c9fafe | 1232 | gchar *full_type; |
64607d08 | 1233 | ObjectProperty *op; |
57c9fafe | 1234 | |
9561fda8 | 1235 | prop->child = child; |
39f72ef9 | 1236 | prop->check = check; |
9561fda8 SH |
1237 | prop->flags = flags; |
1238 | ||
57c9fafe AL |
1239 | full_type = g_strdup_printf("link<%s>", type); |
1240 | ||
64607d08 PB |
1241 | op = object_property_add(obj, name, full_type, |
1242 | object_get_link_property, | |
1243 | check ? object_set_link_property : NULL, | |
1244 | object_release_link_property, | |
1245 | prop, | |
1246 | &local_err); | |
9561fda8 SH |
1247 | if (local_err) { |
1248 | error_propagate(errp, local_err); | |
1249 | g_free(prop); | |
64607d08 | 1250 | goto out; |
9561fda8 | 1251 | } |
57c9fafe | 1252 | |
64607d08 PB |
1253 | op->resolve = object_resolve_link_property; |
1254 | ||
1255 | out: | |
57c9fafe AL |
1256 | g_free(full_type); |
1257 | } | |
1258 | ||
11f590b1 SH |
1259 | gchar *object_get_canonical_path_component(Object *obj) |
1260 | { | |
1261 | ObjectProperty *prop = NULL; | |
1262 | ||
1263 | g_assert(obj); | |
1264 | g_assert(obj->parent != NULL); | |
1265 | ||
1266 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { | |
1267 | if (!object_property_is_child(prop)) { | |
1268 | continue; | |
1269 | } | |
1270 | ||
1271 | if (prop->opaque == obj) { | |
1272 | return g_strdup(prop->name); | |
1273 | } | |
1274 | } | |
1275 | ||
1276 | /* obj had a parent but was not a child, should never happen */ | |
1277 | g_assert_not_reached(); | |
1278 | return NULL; | |
1279 | } | |
1280 | ||
57c9fafe AL |
1281 | gchar *object_get_canonical_path(Object *obj) |
1282 | { | |
1283 | Object *root = object_get_root(); | |
11f590b1 | 1284 | char *newpath, *path = NULL; |
57c9fafe AL |
1285 | |
1286 | while (obj != root) { | |
11f590b1 | 1287 | char *component = object_get_canonical_path_component(obj); |
57c9fafe | 1288 | |
11f590b1 SH |
1289 | if (path) { |
1290 | newpath = g_strdup_printf("%s/%s", component, path); | |
1291 | g_free(component); | |
1292 | g_free(path); | |
1293 | path = newpath; | |
1294 | } else { | |
1295 | path = component; | |
57c9fafe AL |
1296 | } |
1297 | ||
57c9fafe AL |
1298 | obj = obj->parent; |
1299 | } | |
1300 | ||
11f590b1 | 1301 | newpath = g_strdup_printf("/%s", path ? path : ""); |
57c9fafe AL |
1302 | g_free(path); |
1303 | ||
1304 | return newpath; | |
1305 | } | |
1306 | ||
3e84b483 | 1307 | Object *object_resolve_path_component(Object *parent, const gchar *part) |
a612b2a6 | 1308 | { |
89bfe000 | 1309 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
a612b2a6 PB |
1310 | if (prop == NULL) { |
1311 | return NULL; | |
1312 | } | |
1313 | ||
64607d08 PB |
1314 | if (prop->resolve) { |
1315 | return prop->resolve(parent, prop->opaque, part); | |
a612b2a6 PB |
1316 | } else { |
1317 | return NULL; | |
1318 | } | |
1319 | } | |
1320 | ||
57c9fafe AL |
1321 | static Object *object_resolve_abs_path(Object *parent, |
1322 | gchar **parts, | |
02fe2db6 | 1323 | const char *typename, |
57c9fafe AL |
1324 | int index) |
1325 | { | |
57c9fafe AL |
1326 | Object *child; |
1327 | ||
1328 | if (parts[index] == NULL) { | |
02fe2db6 | 1329 | return object_dynamic_cast(parent, typename); |
57c9fafe AL |
1330 | } |
1331 | ||
1332 | if (strcmp(parts[index], "") == 0) { | |
02fe2db6 | 1333 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
57c9fafe AL |
1334 | } |
1335 | ||
a612b2a6 | 1336 | child = object_resolve_path_component(parent, parts[index]); |
57c9fafe AL |
1337 | if (!child) { |
1338 | return NULL; | |
1339 | } | |
1340 | ||
02fe2db6 | 1341 | return object_resolve_abs_path(child, parts, typename, index + 1); |
57c9fafe AL |
1342 | } |
1343 | ||
1344 | static Object *object_resolve_partial_path(Object *parent, | |
1345 | gchar **parts, | |
02fe2db6 | 1346 | const char *typename, |
57c9fafe AL |
1347 | bool *ambiguous) |
1348 | { | |
1349 | Object *obj; | |
1350 | ObjectProperty *prop; | |
1351 | ||
02fe2db6 | 1352 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
57c9fafe AL |
1353 | |
1354 | QTAILQ_FOREACH(prop, &parent->properties, node) { | |
1355 | Object *found; | |
1356 | ||
5d9d3f47 | 1357 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
1358 | continue; |
1359 | } | |
1360 | ||
02fe2db6 PB |
1361 | found = object_resolve_partial_path(prop->opaque, parts, |
1362 | typename, ambiguous); | |
57c9fafe AL |
1363 | if (found) { |
1364 | if (obj) { | |
1365 | if (ambiguous) { | |
1366 | *ambiguous = true; | |
1367 | } | |
1368 | return NULL; | |
1369 | } | |
1370 | obj = found; | |
1371 | } | |
1372 | ||
1373 | if (ambiguous && *ambiguous) { | |
1374 | return NULL; | |
1375 | } | |
1376 | } | |
1377 | ||
1378 | return obj; | |
1379 | } | |
1380 | ||
02fe2db6 PB |
1381 | Object *object_resolve_path_type(const char *path, const char *typename, |
1382 | bool *ambiguous) | |
57c9fafe | 1383 | { |
57c9fafe AL |
1384 | Object *obj; |
1385 | gchar **parts; | |
1386 | ||
1387 | parts = g_strsplit(path, "/", 0); | |
2e1103f6 | 1388 | assert(parts); |
57c9fafe | 1389 | |
2e1103f6 | 1390 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
57c9fafe AL |
1391 | if (ambiguous) { |
1392 | *ambiguous = false; | |
1393 | } | |
02fe2db6 PB |
1394 | obj = object_resolve_partial_path(object_get_root(), parts, |
1395 | typename, ambiguous); | |
57c9fafe | 1396 | } else { |
02fe2db6 | 1397 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
57c9fafe AL |
1398 | } |
1399 | ||
1400 | g_strfreev(parts); | |
1401 | ||
1402 | return obj; | |
1403 | } | |
1404 | ||
02fe2db6 PB |
1405 | Object *object_resolve_path(const char *path, bool *ambiguous) |
1406 | { | |
1407 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); | |
1408 | } | |
1409 | ||
57c9fafe AL |
1410 | typedef struct StringProperty |
1411 | { | |
1412 | char *(*get)(Object *, Error **); | |
1413 | void (*set)(Object *, const char *, Error **); | |
1414 | } StringProperty; | |
1415 | ||
7b7b7d18 PB |
1416 | static void property_get_str(Object *obj, Visitor *v, void *opaque, |
1417 | const char *name, Error **errp) | |
57c9fafe AL |
1418 | { |
1419 | StringProperty *prop = opaque; | |
1420 | char *value; | |
1421 | ||
1422 | value = prop->get(obj, errp); | |
1423 | if (value) { | |
1424 | visit_type_str(v, &value, name, errp); | |
1425 | g_free(value); | |
1426 | } | |
1427 | } | |
1428 | ||
7b7b7d18 PB |
1429 | static void property_set_str(Object *obj, Visitor *v, void *opaque, |
1430 | const char *name, Error **errp) | |
57c9fafe AL |
1431 | { |
1432 | StringProperty *prop = opaque; | |
1433 | char *value; | |
1434 | Error *local_err = NULL; | |
1435 | ||
1436 | visit_type_str(v, &value, name, &local_err); | |
1437 | if (local_err) { | |
1438 | error_propagate(errp, local_err); | |
1439 | return; | |
1440 | } | |
1441 | ||
1442 | prop->set(obj, value, errp); | |
1443 | g_free(value); | |
1444 | } | |
1445 | ||
7b7b7d18 PB |
1446 | static void property_release_str(Object *obj, const char *name, |
1447 | void *opaque) | |
57c9fafe AL |
1448 | { |
1449 | StringProperty *prop = opaque; | |
1450 | g_free(prop); | |
1451 | } | |
1452 | ||
1453 | void object_property_add_str(Object *obj, const char *name, | |
1454 | char *(*get)(Object *, Error **), | |
1455 | void (*set)(Object *, const char *, Error **), | |
1456 | Error **errp) | |
1457 | { | |
a01aedc8 | 1458 | Error *local_err = NULL; |
57c9fafe AL |
1459 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
1460 | ||
1461 | prop->get = get; | |
1462 | prop->set = set; | |
1463 | ||
1464 | object_property_add(obj, name, "string", | |
7b7b7d18 PB |
1465 | get ? property_get_str : NULL, |
1466 | set ? property_set_str : NULL, | |
1467 | property_release_str, | |
a01aedc8 SH |
1468 | prop, &local_err); |
1469 | if (local_err) { | |
1470 | error_propagate(errp, local_err); | |
1471 | g_free(prop); | |
1472 | } | |
57c9fafe | 1473 | } |
745549c8 | 1474 | |
0e558843 AL |
1475 | typedef struct BoolProperty |
1476 | { | |
1477 | bool (*get)(Object *, Error **); | |
1478 | void (*set)(Object *, bool, Error **); | |
1479 | } BoolProperty; | |
1480 | ||
1481 | static void property_get_bool(Object *obj, Visitor *v, void *opaque, | |
1482 | const char *name, Error **errp) | |
1483 | { | |
1484 | BoolProperty *prop = opaque; | |
1485 | bool value; | |
1486 | ||
1487 | value = prop->get(obj, errp); | |
1488 | visit_type_bool(v, &value, name, errp); | |
1489 | } | |
1490 | ||
1491 | static void property_set_bool(Object *obj, Visitor *v, void *opaque, | |
1492 | const char *name, Error **errp) | |
1493 | { | |
1494 | BoolProperty *prop = opaque; | |
1495 | bool value; | |
1496 | Error *local_err = NULL; | |
1497 | ||
1498 | visit_type_bool(v, &value, name, &local_err); | |
1499 | if (local_err) { | |
1500 | error_propagate(errp, local_err); | |
1501 | return; | |
1502 | } | |
1503 | ||
1504 | prop->set(obj, value, errp); | |
1505 | } | |
1506 | ||
1507 | static void property_release_bool(Object *obj, const char *name, | |
1508 | void *opaque) | |
1509 | { | |
1510 | BoolProperty *prop = opaque; | |
1511 | g_free(prop); | |
1512 | } | |
1513 | ||
1514 | void object_property_add_bool(Object *obj, const char *name, | |
1515 | bool (*get)(Object *, Error **), | |
1516 | void (*set)(Object *, bool, Error **), | |
1517 | Error **errp) | |
1518 | { | |
a01aedc8 | 1519 | Error *local_err = NULL; |
0e558843 AL |
1520 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
1521 | ||
1522 | prop->get = get; | |
1523 | prop->set = set; | |
1524 | ||
1525 | object_property_add(obj, name, "bool", | |
1526 | get ? property_get_bool : NULL, | |
1527 | set ? property_set_bool : NULL, | |
1528 | property_release_bool, | |
a01aedc8 SH |
1529 | prop, &local_err); |
1530 | if (local_err) { | |
1531 | error_propagate(errp, local_err); | |
1532 | g_free(prop); | |
1533 | } | |
0e558843 AL |
1534 | } |
1535 | ||
2f262e06 PB |
1536 | static char *qdev_get_type(Object *obj, Error **errp) |
1537 | { | |
1538 | return g_strdup(object_get_typename(obj)); | |
1539 | } | |
1540 | ||
e732ea63 MT |
1541 | static void property_get_uint8_ptr(Object *obj, Visitor *v, |
1542 | void *opaque, const char *name, | |
1543 | Error **errp) | |
1544 | { | |
1545 | uint8_t value = *(uint8_t *)opaque; | |
1546 | visit_type_uint8(v, &value, name, errp); | |
1547 | } | |
1548 | ||
1549 | static void property_get_uint16_ptr(Object *obj, Visitor *v, | |
1550 | void *opaque, const char *name, | |
1551 | Error **errp) | |
1552 | { | |
1553 | uint16_t value = *(uint16_t *)opaque; | |
1554 | visit_type_uint16(v, &value, name, errp); | |
1555 | } | |
1556 | ||
1557 | static void property_get_uint32_ptr(Object *obj, Visitor *v, | |
1558 | void *opaque, const char *name, | |
1559 | Error **errp) | |
1560 | { | |
1561 | uint32_t value = *(uint32_t *)opaque; | |
1562 | visit_type_uint32(v, &value, name, errp); | |
1563 | } | |
1564 | ||
1565 | static void property_get_uint64_ptr(Object *obj, Visitor *v, | |
1566 | void *opaque, const char *name, | |
1567 | Error **errp) | |
1568 | { | |
1569 | uint64_t value = *(uint64_t *)opaque; | |
1570 | visit_type_uint64(v, &value, name, errp); | |
1571 | } | |
1572 | ||
1573 | void object_property_add_uint8_ptr(Object *obj, const char *name, | |
1574 | const uint8_t *v, Error **errp) | |
1575 | { | |
1576 | object_property_add(obj, name, "uint8", property_get_uint8_ptr, | |
1577 | NULL, NULL, (void *)v, errp); | |
1578 | } | |
1579 | ||
1580 | void object_property_add_uint16_ptr(Object *obj, const char *name, | |
1581 | const uint16_t *v, Error **errp) | |
1582 | { | |
1583 | object_property_add(obj, name, "uint16", property_get_uint16_ptr, | |
1584 | NULL, NULL, (void *)v, errp); | |
1585 | } | |
1586 | ||
1587 | void object_property_add_uint32_ptr(Object *obj, const char *name, | |
1588 | const uint32_t *v, Error **errp) | |
1589 | { | |
1590 | object_property_add(obj, name, "uint32", property_get_uint32_ptr, | |
1591 | NULL, NULL, (void *)v, errp); | |
1592 | } | |
1593 | ||
1594 | void object_property_add_uint64_ptr(Object *obj, const char *name, | |
1595 | const uint64_t *v, Error **errp) | |
1596 | { | |
1597 | object_property_add(obj, name, "uint64", property_get_uint64_ptr, | |
1598 | NULL, NULL, (void *)v, errp); | |
1599 | } | |
1600 | ||
ef7c7ff6 SH |
1601 | typedef struct { |
1602 | Object *target_obj; | |
1603 | const char *target_name; | |
1604 | } AliasProperty; | |
1605 | ||
1606 | static void property_get_alias(Object *obj, struct Visitor *v, void *opaque, | |
1607 | const char *name, Error **errp) | |
1608 | { | |
1609 | AliasProperty *prop = opaque; | |
1610 | ||
1611 | object_property_get(prop->target_obj, v, prop->target_name, errp); | |
1612 | } | |
1613 | ||
1614 | static void property_set_alias(Object *obj, struct Visitor *v, void *opaque, | |
1615 | const char *name, Error **errp) | |
1616 | { | |
1617 | AliasProperty *prop = opaque; | |
1618 | ||
1619 | object_property_set(prop->target_obj, v, prop->target_name, errp); | |
1620 | } | |
1621 | ||
64607d08 PB |
1622 | static Object *property_resolve_alias(Object *obj, void *opaque, |
1623 | const gchar *part) | |
1624 | { | |
1625 | AliasProperty *prop = opaque; | |
1626 | ||
1627 | return object_resolve_path_component(prop->target_obj, prop->target_name); | |
1628 | } | |
1629 | ||
ef7c7ff6 SH |
1630 | static void property_release_alias(Object *obj, const char *name, void *opaque) |
1631 | { | |
1632 | AliasProperty *prop = opaque; | |
1633 | ||
1634 | g_free(prop); | |
1635 | } | |
1636 | ||
1637 | void object_property_add_alias(Object *obj, const char *name, | |
1638 | Object *target_obj, const char *target_name, | |
1639 | Error **errp) | |
1640 | { | |
1641 | AliasProperty *prop; | |
64607d08 | 1642 | ObjectProperty *op; |
ef7c7ff6 | 1643 | ObjectProperty *target_prop; |
d190698e | 1644 | gchar *prop_type; |
8ae9a9ef | 1645 | Error *local_err = NULL; |
ef7c7ff6 SH |
1646 | |
1647 | target_prop = object_property_find(target_obj, target_name, errp); | |
1648 | if (!target_prop) { | |
1649 | return; | |
1650 | } | |
1651 | ||
d190698e PB |
1652 | if (object_property_is_child(target_prop)) { |
1653 | prop_type = g_strdup_printf("link%s", | |
1654 | target_prop->type + strlen("child")); | |
1655 | } else { | |
1656 | prop_type = g_strdup(target_prop->type); | |
1657 | } | |
1658 | ||
ef7c7ff6 SH |
1659 | prop = g_malloc(sizeof(*prop)); |
1660 | prop->target_obj = target_obj; | |
1661 | prop->target_name = target_name; | |
1662 | ||
d190698e | 1663 | op = object_property_add(obj, name, prop_type, |
64607d08 PB |
1664 | property_get_alias, |
1665 | property_set_alias, | |
1666 | property_release_alias, | |
8ae9a9ef GA |
1667 | prop, &local_err); |
1668 | if (local_err) { | |
1669 | error_propagate(errp, local_err); | |
1670 | g_free(prop); | |
1671 | goto out; | |
1672 | } | |
64607d08 | 1673 | op->resolve = property_resolve_alias; |
d190698e | 1674 | |
8ae9a9ef | 1675 | out: |
d190698e | 1676 | g_free(prop_type); |
ef7c7ff6 SH |
1677 | } |
1678 | ||
2f262e06 PB |
1679 | static void object_instance_init(Object *obj) |
1680 | { | |
1681 | object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); | |
1682 | } | |
1683 | ||
745549c8 PB |
1684 | static void register_types(void) |
1685 | { | |
1686 | static TypeInfo interface_info = { | |
1687 | .name = TYPE_INTERFACE, | |
33e95c63 | 1688 | .class_size = sizeof(InterfaceClass), |
745549c8 PB |
1689 | .abstract = true, |
1690 | }; | |
1691 | ||
1692 | static TypeInfo object_info = { | |
1693 | .name = TYPE_OBJECT, | |
1694 | .instance_size = sizeof(Object), | |
2f262e06 | 1695 | .instance_init = object_instance_init, |
745549c8 PB |
1696 | .abstract = true, |
1697 | }; | |
1698 | ||
049cb3cf PB |
1699 | type_interface = type_register_internal(&interface_info); |
1700 | type_register_internal(&object_info); | |
745549c8 PB |
1701 | } |
1702 | ||
1703 | type_init(register_types) |