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