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