]>
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 | ||
9bbc853b | 13 | #include "qemu/osdep.h" |
da34e65c | 14 | #include "qapi/error.h" |
14cccb61 | 15 | #include "qom/object.h" |
a31bdae5 | 16 | #include "qom/object_interfaces.h" |
f348b6d1 | 17 | #include "qemu/cutils.h" |
7b1b5d19 | 18 | #include "qapi/visitor.h" |
b2cd7dee PB |
19 | #include "qapi/string-input-visitor.h" |
20 | #include "qapi/string-output-visitor.h" | |
eb815e24 | 21 | #include "qapi/qapi-builtin-visit.h" |
7b1b5d19 | 22 | #include "qapi/qmp/qerror.h" |
fa131d94 | 23 | #include "trace.h" |
2f28d2ff | 24 | |
7b7b7d18 PB |
25 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
26 | * of the QOM core on QObject? */ | |
14cccb61 | 27 | #include "qom/qom-qobject.h" |
7b1b5d19 | 28 | #include "qapi/qmp/qbool.h" |
15280c36 | 29 | #include "qapi/qmp/qnum.h" |
7b1b5d19 | 30 | #include "qapi/qmp/qstring.h" |
7b7b7d18 | 31 | |
2f28d2ff AL |
32 | #define MAX_INTERFACES 32 |
33 | ||
34 | typedef struct InterfaceImpl InterfaceImpl; | |
35 | typedef struct TypeImpl TypeImpl; | |
36 | ||
37 | struct InterfaceImpl | |
38 | { | |
33e95c63 | 39 | const char *typename; |
2f28d2ff AL |
40 | }; |
41 | ||
42 | struct TypeImpl | |
43 | { | |
44 | const char *name; | |
45 | ||
46 | size_t class_size; | |
47 | ||
48 | size_t instance_size; | |
49 | ||
50 | void (*class_init)(ObjectClass *klass, void *data); | |
3b50e311 | 51 | void (*class_base_init)(ObjectClass *klass, void *data); |
2f28d2ff AL |
52 | |
53 | void *class_data; | |
54 | ||
55 | void (*instance_init)(Object *obj); | |
8231c2dd | 56 | void (*instance_post_init)(Object *obj); |
2f28d2ff AL |
57 | void (*instance_finalize)(Object *obj); |
58 | ||
59 | bool abstract; | |
60 | ||
61 | const char *parent; | |
62 | TypeImpl *parent_type; | |
63 | ||
64 | ObjectClass *class; | |
65 | ||
66 | int num_interfaces; | |
67 | InterfaceImpl interfaces[MAX_INTERFACES]; | |
68 | }; | |
69 | ||
9970bd88 PB |
70 | static Type type_interface; |
71 | ||
2f28d2ff AL |
72 | static GHashTable *type_table_get(void) |
73 | { | |
74 | static GHashTable *type_table; | |
75 | ||
76 | if (type_table == NULL) { | |
77 | type_table = g_hash_table_new(g_str_hash, g_str_equal); | |
78 | } | |
79 | ||
80 | return type_table; | |
81 | } | |
82 | ||
f54c19ca HP |
83 | static bool enumerating_types; |
84 | ||
2f28d2ff AL |
85 | static void type_table_add(TypeImpl *ti) |
86 | { | |
f54c19ca | 87 | assert(!enumerating_types); |
2f28d2ff AL |
88 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); |
89 | } | |
90 | ||
91 | static TypeImpl *type_table_lookup(const char *name) | |
92 | { | |
93 | return g_hash_table_lookup(type_table_get(), name); | |
94 | } | |
95 | ||
b061dc41 | 96 | static TypeImpl *type_new(const TypeInfo *info) |
2f28d2ff AL |
97 | { |
98 | TypeImpl *ti = g_malloc0(sizeof(*ti)); | |
33e95c63 | 99 | int i; |
2f28d2ff AL |
100 | |
101 | g_assert(info->name != NULL); | |
102 | ||
73093354 AL |
103 | if (type_table_lookup(info->name) != NULL) { |
104 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); | |
105 | abort(); | |
106 | } | |
107 | ||
2f28d2ff AL |
108 | ti->name = g_strdup(info->name); |
109 | ti->parent = g_strdup(info->parent); | |
110 | ||
111 | ti->class_size = info->class_size; | |
112 | ti->instance_size = info->instance_size; | |
113 | ||
114 | ti->class_init = info->class_init; | |
3b50e311 | 115 | ti->class_base_init = info->class_base_init; |
2f28d2ff AL |
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 | ||
aa04c9d2 IM |
152 | void type_register_static_array(const TypeInfo *infos, int nr_infos) |
153 | { | |
154 | int i; | |
155 | ||
156 | for (i = 0; i < nr_infos; i++) { | |
157 | type_register_static(&infos[i]); | |
158 | } | |
159 | } | |
160 | ||
2f28d2ff AL |
161 | static TypeImpl *type_get_by_name(const char *name) |
162 | { | |
163 | if (name == NULL) { | |
164 | return NULL; | |
165 | } | |
166 | ||
167 | return type_table_lookup(name); | |
168 | } | |
169 | ||
170 | static TypeImpl *type_get_parent(TypeImpl *type) | |
171 | { | |
172 | if (!type->parent_type && type->parent) { | |
173 | type->parent_type = type_get_by_name(type->parent); | |
174 | g_assert(type->parent_type != NULL); | |
175 | } | |
176 | ||
177 | return type->parent_type; | |
178 | } | |
179 | ||
180 | static bool type_has_parent(TypeImpl *type) | |
181 | { | |
182 | return (type->parent != NULL); | |
183 | } | |
184 | ||
185 | static size_t type_class_get_size(TypeImpl *ti) | |
186 | { | |
187 | if (ti->class_size) { | |
188 | return ti->class_size; | |
189 | } | |
190 | ||
191 | if (type_has_parent(ti)) { | |
192 | return type_class_get_size(type_get_parent(ti)); | |
193 | } | |
194 | ||
195 | return sizeof(ObjectClass); | |
196 | } | |
197 | ||
aca59af6 IM |
198 | static size_t type_object_get_size(TypeImpl *ti) |
199 | { | |
200 | if (ti->instance_size) { | |
201 | return ti->instance_size; | |
202 | } | |
203 | ||
204 | if (type_has_parent(ti)) { | |
205 | return type_object_get_size(type_get_parent(ti)); | |
206 | } | |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
3f97b53a BR |
211 | size_t object_type_get_instance_size(const char *typename) |
212 | { | |
213 | TypeImpl *type = type_get_by_name(typename); | |
214 | ||
215 | g_assert(type != NULL); | |
216 | return type_object_get_size(type); | |
217 | } | |
218 | ||
33e95c63 | 219 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
2f28d2ff | 220 | { |
33e95c63 AL |
221 | assert(target_type); |
222 | ||
b30d8054 | 223 | /* Check if target_type is a direct ancestor of type */ |
33e95c63 AL |
224 | while (type) { |
225 | if (type == target_type) { | |
226 | return true; | |
227 | } | |
2f28d2ff | 228 | |
33e95c63 AL |
229 | type = type_get_parent(type); |
230 | } | |
231 | ||
232 | return false; | |
233 | } | |
234 | ||
235 | static void type_initialize(TypeImpl *ti); | |
236 | ||
b061dc41 PB |
237 | static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type, |
238 | TypeImpl *parent_type) | |
33e95c63 AL |
239 | { |
240 | InterfaceClass *new_iface; | |
241 | TypeInfo info = { }; | |
242 | TypeImpl *iface_impl; | |
243 | ||
b061dc41 PB |
244 | info.parent = parent_type->name; |
245 | info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name); | |
33e95c63 AL |
246 | info.abstract = true; |
247 | ||
b061dc41 PB |
248 | iface_impl = type_new(&info); |
249 | iface_impl->parent_type = parent_type; | |
33e95c63 AL |
250 | type_initialize(iface_impl); |
251 | g_free((char *)info.name); | |
252 | ||
253 | new_iface = (InterfaceClass *)iface_impl->class; | |
254 | new_iface->concrete_class = ti->class; | |
b061dc41 | 255 | new_iface->interface_type = interface_type; |
33e95c63 AL |
256 | |
257 | ti->class->interfaces = g_slist_append(ti->class->interfaces, | |
258 | iface_impl->class); | |
2f28d2ff AL |
259 | } |
260 | ||
16bf7f52 DB |
261 | static void object_property_free(gpointer data) |
262 | { | |
263 | ObjectProperty *prop = data; | |
264 | ||
265 | g_free(prop->name); | |
266 | g_free(prop->type); | |
267 | g_free(prop->description); | |
268 | g_free(prop); | |
269 | } | |
270 | ||
ac451033 | 271 | static void type_initialize(TypeImpl *ti) |
2f28d2ff | 272 | { |
745549c8 | 273 | TypeImpl *parent; |
2f28d2ff AL |
274 | |
275 | if (ti->class) { | |
276 | return; | |
277 | } | |
278 | ||
279 | ti->class_size = type_class_get_size(ti); | |
aca59af6 | 280 | ti->instance_size = type_object_get_size(ti); |
1c6d75d5 EH |
281 | /* Any type with zero instance_size is implicitly abstract. |
282 | * This means interface types are all abstract. | |
283 | */ | |
284 | if (ti->instance_size == 0) { | |
285 | ti->abstract = true; | |
286 | } | |
422ca143 MAL |
287 | if (type_is_ancestor(ti, type_interface)) { |
288 | assert(ti->instance_size == 0); | |
289 | assert(ti->abstract); | |
290 | assert(!ti->instance_init); | |
291 | assert(!ti->instance_post_init); | |
292 | assert(!ti->instance_finalize); | |
293 | assert(!ti->num_interfaces); | |
294 | } | |
2f28d2ff | 295 | ti->class = g_malloc0(ti->class_size); |
2f28d2ff | 296 | |
745549c8 PB |
297 | parent = type_get_parent(ti); |
298 | if (parent) { | |
ac451033 | 299 | type_initialize(parent); |
33e95c63 AL |
300 | GSList *e; |
301 | int i; | |
2f28d2ff | 302 | |
719a3077 | 303 | g_assert(parent->class_size <= ti->class_size); |
745549c8 | 304 | memcpy(ti->class, parent->class, parent->class_size); |
3e407de4 | 305 | ti->class->interfaces = NULL; |
16bf7f52 DB |
306 | ti->class->properties = g_hash_table_new_full( |
307 | g_str_hash, g_str_equal, g_free, object_property_free); | |
33e95c63 AL |
308 | |
309 | for (e = parent->class->interfaces; e; e = e->next) { | |
b061dc41 PB |
310 | InterfaceClass *iface = e->data; |
311 | ObjectClass *klass = OBJECT_CLASS(iface); | |
312 | ||
313 | type_initialize_interface(ti, iface->interface_type, klass->type); | |
33e95c63 AL |
314 | } |
315 | ||
316 | for (i = 0; i < ti->num_interfaces; i++) { | |
317 | TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); | |
318 | for (e = ti->class->interfaces; e; e = e->next) { | |
319 | TypeImpl *target_type = OBJECT_CLASS(e->data)->type; | |
320 | ||
321 | if (type_is_ancestor(target_type, t)) { | |
322 | break; | |
323 | } | |
324 | } | |
325 | ||
326 | if (e) { | |
327 | continue; | |
328 | } | |
329 | ||
b061dc41 | 330 | type_initialize_interface(ti, t, t); |
33e95c63 | 331 | } |
16bf7f52 DB |
332 | } else { |
333 | ti->class->properties = g_hash_table_new_full( | |
334 | g_str_hash, g_str_equal, g_free, object_property_free); | |
745549c8 | 335 | } |
2f28d2ff | 336 | |
745549c8 | 337 | ti->class->type = ti; |
3b50e311 | 338 | |
745549c8 PB |
339 | while (parent) { |
340 | if (parent->class_base_init) { | |
341 | parent->class_base_init(ti->class, ti->class_data); | |
3b50e311 | 342 | } |
745549c8 | 343 | parent = type_get_parent(parent); |
2f28d2ff AL |
344 | } |
345 | ||
2f28d2ff AL |
346 | if (ti->class_init) { |
347 | ti->class_init(ti->class, ti->class_data); | |
348 | } | |
2f28d2ff AL |
349 | } |
350 | ||
351 | static void object_init_with_type(Object *obj, TypeImpl *ti) | |
352 | { | |
2f28d2ff AL |
353 | if (type_has_parent(ti)) { |
354 | object_init_with_type(obj, type_get_parent(ti)); | |
355 | } | |
356 | ||
2f28d2ff AL |
357 | if (ti->instance_init) { |
358 | ti->instance_init(obj); | |
359 | } | |
360 | } | |
361 | ||
8231c2dd EH |
362 | static void object_post_init_with_type(Object *obj, TypeImpl *ti) |
363 | { | |
364 | if (ti->instance_post_init) { | |
365 | ti->instance_post_init(obj); | |
366 | } | |
367 | ||
368 | if (type_has_parent(ti)) { | |
369 | object_post_init_with_type(obj, type_get_parent(ti)); | |
370 | } | |
371 | } | |
372 | ||
ea9ce893 MAL |
373 | void object_apply_global_props(Object *obj, const GPtrArray *props, Error **errp) |
374 | { | |
ea9ce893 MAL |
375 | int i; |
376 | ||
377 | if (!props) { | |
378 | return; | |
379 | } | |
380 | ||
381 | for (i = 0; i < props->len; i++) { | |
382 | GlobalProperty *p = g_ptr_array_index(props, i); | |
d769f0df | 383 | Error *err = NULL; |
ea9ce893 MAL |
384 | |
385 | if (object_dynamic_cast(obj, p->driver) == NULL) { | |
386 | continue; | |
387 | } | |
d7741743 EH |
388 | if (p->optional && !object_property_find(obj, p->property, NULL)) { |
389 | continue; | |
390 | } | |
ea9ce893 MAL |
391 | p->used = true; |
392 | object_property_parse(obj, p->value, p->property, &err); | |
393 | if (err != NULL) { | |
394 | error_prepend(&err, "can't apply global %s.%s=%s: ", | |
395 | p->driver, p->property, p->value); | |
50545b2c MAL |
396 | /* |
397 | * If errp != NULL, propagate error and return. | |
398 | * If errp == NULL, report a warning, but keep going | |
399 | * with the remaining globals. | |
400 | */ | |
401 | if (errp) { | |
402 | error_propagate(errp, err); | |
403 | return; | |
404 | } else { | |
405 | warn_report_err(err); | |
406 | } | |
ea9ce893 MAL |
407 | } |
408 | } | |
409 | } | |
410 | ||
617902af MA |
411 | /* |
412 | * Global property defaults | |
413 | * Slot 0: accelerator's global property defaults | |
414 | * Slot 1: machine's global property defaults | |
415 | * Each is a GPtrArray of of GlobalProperty. | |
416 | * Applied in order, later entries override earlier ones. | |
417 | */ | |
418 | static GPtrArray *object_compat_props[2]; | |
419 | ||
420 | /* | |
421 | * Set machine's global property defaults to @compat_props. | |
422 | * May be called at most once. | |
423 | */ | |
424 | void object_set_machine_compat_props(GPtrArray *compat_props) | |
425 | { | |
426 | assert(!object_compat_props[1]); | |
427 | object_compat_props[1] = compat_props; | |
428 | } | |
429 | ||
430 | /* | |
431 | * Set accelerator's global property defaults to @compat_props. | |
432 | * May be called at most once. | |
433 | */ | |
434 | void object_set_accelerator_compat_props(GPtrArray *compat_props) | |
435 | { | |
436 | assert(!object_compat_props[0]); | |
437 | object_compat_props[0] = compat_props; | |
438 | } | |
439 | ||
440 | void object_apply_compat_props(Object *obj) | |
441 | { | |
442 | int i; | |
443 | ||
444 | for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) { | |
445 | object_apply_global_props(obj, object_compat_props[i], | |
446 | &error_abort); | |
447 | } | |
448 | } | |
449 | ||
63f7b10b | 450 | static void object_initialize_with_type(void *data, size_t size, TypeImpl *type) |
2f28d2ff AL |
451 | { |
452 | Object *obj = data; | |
453 | ||
454 | g_assert(type != NULL); | |
ac451033 | 455 | type_initialize(type); |
aca59af6 | 456 | |
719a3077 | 457 | g_assert(type->instance_size >= sizeof(Object)); |
2f28d2ff | 458 | g_assert(type->abstract == false); |
719a3077 | 459 | g_assert(size >= type->instance_size); |
2f28d2ff AL |
460 | |
461 | memset(obj, 0, type->instance_size); | |
462 | obj->class = type->class; | |
764b6312 | 463 | object_ref(obj); |
b604a854 PF |
464 | obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal, |
465 | NULL, object_property_free); | |
2f28d2ff | 466 | object_init_with_type(obj, type); |
8231c2dd | 467 | object_post_init_with_type(obj, type); |
2f28d2ff AL |
468 | } |
469 | ||
213f0c4f | 470 | void object_initialize(void *data, size_t size, const char *typename) |
2f28d2ff AL |
471 | { |
472 | TypeImpl *type = type_get_by_name(typename); | |
473 | ||
5b9237f6 | 474 | object_initialize_with_type(data, size, type); |
2f28d2ff AL |
475 | } |
476 | ||
0210b39d TH |
477 | void object_initialize_child(Object *parentobj, const char *propname, |
478 | void *childobj, size_t size, const char *type, | |
479 | Error **errp, ...) | |
480 | { | |
481 | va_list vargs; | |
482 | ||
483 | va_start(vargs, errp); | |
484 | object_initialize_childv(parentobj, propname, childobj, size, type, errp, | |
485 | vargs); | |
486 | va_end(vargs); | |
487 | } | |
488 | ||
489 | void object_initialize_childv(Object *parentobj, const char *propname, | |
490 | void *childobj, size_t size, const char *type, | |
491 | Error **errp, va_list vargs) | |
492 | { | |
493 | Error *local_err = NULL; | |
494 | Object *obj; | |
3650b2de | 495 | UserCreatable *uc; |
0210b39d TH |
496 | |
497 | object_initialize(childobj, size, type); | |
498 | obj = OBJECT(childobj); | |
499 | ||
500 | object_set_propv(obj, &local_err, vargs); | |
501 | if (local_err) { | |
502 | goto out; | |
503 | } | |
504 | ||
505 | object_property_add_child(parentobj, propname, obj, &local_err); | |
506 | if (local_err) { | |
507 | goto out; | |
508 | } | |
509 | ||
3650b2de MAL |
510 | uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE); |
511 | if (uc) { | |
512 | user_creatable_complete(uc, &local_err); | |
0210b39d TH |
513 | if (local_err) { |
514 | object_unparent(obj); | |
515 | goto out; | |
516 | } | |
517 | } | |
518 | ||
519 | /* | |
520 | * Since object_property_add_child added a reference to the child object, | |
521 | * we can drop the reference added by object_initialize(), so the child | |
522 | * property will own the only reference to the object. | |
523 | */ | |
524 | object_unref(obj); | |
525 | ||
526 | out: | |
527 | if (local_err) { | |
528 | error_propagate(errp, local_err); | |
529 | object_unref(obj); | |
530 | } | |
531 | } | |
532 | ||
5d9d3f47 AF |
533 | static inline bool object_property_is_child(ObjectProperty *prop) |
534 | { | |
535 | return strstart(prop->type, "child<", NULL); | |
536 | } | |
537 | ||
57c9fafe AL |
538 | static void object_property_del_all(Object *obj) |
539 | { | |
b604a854 PF |
540 | ObjectProperty *prop; |
541 | GHashTableIter iter; | |
542 | gpointer key, value; | |
543 | bool released; | |
544 | ||
545 | do { | |
546 | released = false; | |
547 | g_hash_table_iter_init(&iter, obj->properties); | |
548 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
549 | prop = value; | |
550 | if (prop->release) { | |
551 | prop->release(obj, prop->name, prop->opaque); | |
552 | prop->release = NULL; | |
553 | released = true; | |
554 | break; | |
555 | } | |
556 | g_hash_table_iter_remove(&iter); | |
57c9fafe | 557 | } |
b604a854 | 558 | } while (released); |
57c9fafe | 559 | |
b604a854 | 560 | g_hash_table_unref(obj->properties); |
57c9fafe AL |
561 | } |
562 | ||
563 | static void object_property_del_child(Object *obj, Object *child, Error **errp) | |
564 | { | |
565 | ObjectProperty *prop; | |
b604a854 PF |
566 | GHashTableIter iter; |
567 | gpointer key, value; | |
57c9fafe | 568 | |
b604a854 PF |
569 | g_hash_table_iter_init(&iter, obj->properties); |
570 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
571 | prop = value; | |
572 | if (object_property_is_child(prop) && prop->opaque == child) { | |
573 | if (prop->release) { | |
574 | prop->release(obj, prop->name, prop->opaque); | |
575 | prop->release = NULL; | |
576 | } | |
577 | break; | |
578 | } | |
579 | } | |
580 | g_hash_table_iter_init(&iter, obj->properties); | |
581 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
582 | prop = value; | |
5d9d3f47 | 583 | if (object_property_is_child(prop) && prop->opaque == child) { |
b604a854 | 584 | g_hash_table_iter_remove(&iter); |
6c1fdcf9 | 585 | break; |
57c9fafe AL |
586 | } |
587 | } | |
588 | } | |
589 | ||
590 | void object_unparent(Object *obj) | |
591 | { | |
e998fa8d MT |
592 | if (obj->parent) { |
593 | object_property_del_child(obj->parent, obj, NULL); | |
594 | } | |
57c9fafe AL |
595 | } |
596 | ||
2f28d2ff AL |
597 | static void object_deinit(Object *obj, TypeImpl *type) |
598 | { | |
599 | if (type->instance_finalize) { | |
600 | type->instance_finalize(obj); | |
601 | } | |
602 | ||
2f28d2ff AL |
603 | if (type_has_parent(type)) { |
604 | object_deinit(obj, type_get_parent(type)); | |
605 | } | |
606 | } | |
607 | ||
339c2708 | 608 | static void object_finalize(void *data) |
2f28d2ff AL |
609 | { |
610 | Object *obj = data; | |
611 | TypeImpl *ti = obj->class->type; | |
612 | ||
57c9fafe | 613 | object_property_del_all(obj); |
76a6e1cc | 614 | object_deinit(obj, ti); |
db85b575 | 615 | |
719a3077 | 616 | g_assert(obj->ref == 0); |
fde9bf44 PB |
617 | if (obj->free) { |
618 | obj->free(obj); | |
619 | } | |
2f28d2ff AL |
620 | } |
621 | ||
63f7b10b | 622 | static Object *object_new_with_type(Type type) |
2f28d2ff AL |
623 | { |
624 | Object *obj; | |
625 | ||
626 | g_assert(type != NULL); | |
ac451033 | 627 | type_initialize(type); |
2f28d2ff AL |
628 | |
629 | obj = g_malloc(type->instance_size); | |
5b9237f6 | 630 | object_initialize_with_type(obj, type->instance_size, type); |
fde9bf44 | 631 | obj->free = g_free; |
2f28d2ff AL |
632 | |
633 | return obj; | |
634 | } | |
635 | ||
636 | Object *object_new(const char *typename) | |
637 | { | |
638 | TypeImpl *ti = type_get_by_name(typename); | |
639 | ||
640 | return object_new_with_type(ti); | |
641 | } | |
642 | ||
a31bdae5 DB |
643 | |
644 | Object *object_new_with_props(const char *typename, | |
645 | Object *parent, | |
646 | const char *id, | |
647 | Error **errp, | |
648 | ...) | |
649 | { | |
650 | va_list vargs; | |
651 | Object *obj; | |
652 | ||
653 | va_start(vargs, errp); | |
654 | obj = object_new_with_propv(typename, parent, id, errp, vargs); | |
655 | va_end(vargs); | |
656 | ||
657 | return obj; | |
658 | } | |
659 | ||
660 | ||
661 | Object *object_new_with_propv(const char *typename, | |
662 | Object *parent, | |
663 | const char *id, | |
664 | Error **errp, | |
665 | va_list vargs) | |
666 | { | |
667 | Object *obj; | |
668 | ObjectClass *klass; | |
669 | Error *local_err = NULL; | |
3650b2de | 670 | UserCreatable *uc; |
a31bdae5 DB |
671 | |
672 | klass = object_class_by_name(typename); | |
673 | if (!klass) { | |
674 | error_setg(errp, "invalid object type: %s", typename); | |
675 | return NULL; | |
676 | } | |
677 | ||
678 | if (object_class_is_abstract(klass)) { | |
679 | error_setg(errp, "object type '%s' is abstract", typename); | |
680 | return NULL; | |
681 | } | |
682 | obj = object_new(typename); | |
683 | ||
684 | if (object_set_propv(obj, &local_err, vargs) < 0) { | |
685 | goto error; | |
686 | } | |
687 | ||
6134d752 DB |
688 | if (id != NULL) { |
689 | object_property_add_child(parent, id, obj, &local_err); | |
690 | if (local_err) { | |
691 | goto error; | |
692 | } | |
a31bdae5 DB |
693 | } |
694 | ||
3650b2de MAL |
695 | uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE); |
696 | if (uc) { | |
697 | user_creatable_complete(uc, &local_err); | |
a31bdae5 | 698 | if (local_err) { |
6134d752 DB |
699 | if (id != NULL) { |
700 | object_unparent(obj); | |
701 | } | |
a31bdae5 DB |
702 | goto error; |
703 | } | |
704 | } | |
705 | ||
706 | object_unref(OBJECT(obj)); | |
707 | return obj; | |
708 | ||
709 | error: | |
621ff94d | 710 | error_propagate(errp, local_err); |
a31bdae5 DB |
711 | object_unref(obj); |
712 | return NULL; | |
713 | } | |
714 | ||
715 | ||
716 | int object_set_props(Object *obj, | |
717 | Error **errp, | |
718 | ...) | |
719 | { | |
720 | va_list vargs; | |
721 | int ret; | |
722 | ||
723 | va_start(vargs, errp); | |
724 | ret = object_set_propv(obj, errp, vargs); | |
725 | va_end(vargs); | |
726 | ||
727 | return ret; | |
728 | } | |
729 | ||
730 | ||
731 | int object_set_propv(Object *obj, | |
732 | Error **errp, | |
733 | va_list vargs) | |
734 | { | |
735 | const char *propname; | |
736 | Error *local_err = NULL; | |
737 | ||
738 | propname = va_arg(vargs, char *); | |
739 | while (propname != NULL) { | |
740 | const char *value = va_arg(vargs, char *); | |
741 | ||
742 | g_assert(value != NULL); | |
743 | object_property_parse(obj, value, propname, &local_err); | |
744 | if (local_err) { | |
745 | error_propagate(errp, local_err); | |
746 | return -1; | |
747 | } | |
748 | propname = va_arg(vargs, char *); | |
749 | } | |
750 | ||
751 | return 0; | |
752 | } | |
753 | ||
754 | ||
2f28d2ff AL |
755 | Object *object_dynamic_cast(Object *obj, const char *typename) |
756 | { | |
b7f43fe4 | 757 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
acc4af3f PB |
758 | return obj; |
759 | } | |
760 | ||
2f28d2ff AL |
761 | return NULL; |
762 | } | |
763 | ||
be17f18b PB |
764 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
765 | const char *file, int line, const char *func) | |
2f28d2ff | 766 | { |
fa131d94 PB |
767 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
768 | typename, file, line, func); | |
769 | ||
3556c233 | 770 | #ifdef CONFIG_QOM_CAST_DEBUG |
03587328 AL |
771 | int i; |
772 | Object *inst; | |
773 | ||
95916abc | 774 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd | 775 | if (atomic_read(&obj->class->object_cast_cache[i]) == typename) { |
03587328 AL |
776 | goto out; |
777 | } | |
778 | } | |
779 | ||
780 | inst = object_dynamic_cast(obj, typename); | |
2f28d2ff | 781 | |
b7f43fe4 | 782 | if (!inst && obj) { |
be17f18b PB |
783 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
784 | file, line, func, obj, typename); | |
2f28d2ff AL |
785 | abort(); |
786 | } | |
787 | ||
3556c233 | 788 | assert(obj == inst); |
03587328 | 789 | |
95916abc | 790 | if (obj && obj == inst) { |
03587328 | 791 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd AB |
792 | atomic_set(&obj->class->object_cast_cache[i - 1], |
793 | atomic_read(&obj->class->object_cast_cache[i])); | |
03587328 | 794 | } |
b6b3ccfd | 795 | atomic_set(&obj->class->object_cast_cache[i - 1], typename); |
03587328 AL |
796 | } |
797 | ||
798 | out: | |
3556c233 PB |
799 | #endif |
800 | return obj; | |
2f28d2ff AL |
801 | } |
802 | ||
803 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
804 | const char *typename) | |
805 | { | |
33e95c63 | 806 | ObjectClass *ret = NULL; |
bf0fda34 PB |
807 | TypeImpl *target_type; |
808 | TypeImpl *type; | |
2f28d2ff | 809 | |
bf0fda34 PB |
810 | if (!class) { |
811 | return NULL; | |
812 | } | |
813 | ||
793c96b5 | 814 | /* A simple fast path that can trigger a lot for leaf classes. */ |
bf0fda34 | 815 | type = class->type; |
793c96b5 PB |
816 | if (type->name == typename) { |
817 | return class; | |
818 | } | |
819 | ||
bf0fda34 | 820 | target_type = type_get_by_name(typename); |
9ab880b3 AG |
821 | if (!target_type) { |
822 | /* target class type unknown, so fail the cast */ | |
823 | return NULL; | |
824 | } | |
825 | ||
00e2ceae PC |
826 | if (type->class->interfaces && |
827 | type_is_ancestor(target_type, type_interface)) { | |
33e95c63 AL |
828 | int found = 0; |
829 | GSList *i; | |
2f28d2ff | 830 | |
33e95c63 AL |
831 | for (i = class->interfaces; i; i = i->next) { |
832 | ObjectClass *target_class = i->data; | |
833 | ||
834 | if (type_is_ancestor(target_class->type, target_type)) { | |
835 | ret = target_class; | |
836 | found++; | |
837 | } | |
838 | } | |
839 | ||
840 | /* The match was ambiguous, don't allow a cast */ | |
841 | if (found > 1) { | |
842 | ret = NULL; | |
843 | } | |
844 | } else if (type_is_ancestor(type, target_type)) { | |
845 | ret = class; | |
2f28d2ff AL |
846 | } |
847 | ||
33e95c63 | 848 | return ret; |
2f28d2ff AL |
849 | } |
850 | ||
851 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
be17f18b PB |
852 | const char *typename, |
853 | const char *file, int line, | |
854 | const char *func) | |
2f28d2ff | 855 | { |
fa131d94 PB |
856 | ObjectClass *ret; |
857 | ||
858 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", | |
859 | typename, file, line, func); | |
2f28d2ff | 860 | |
03587328 AL |
861 | #ifdef CONFIG_QOM_CAST_DEBUG |
862 | int i; | |
863 | ||
9d6a3d58 | 864 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd | 865 | if (atomic_read(&class->class_cast_cache[i]) == typename) { |
03587328 AL |
866 | ret = class; |
867 | goto out; | |
868 | } | |
869 | } | |
870 | #else | |
9d6a3d58 | 871 | if (!class || !class->interfaces) { |
3556c233 PB |
872 | return class; |
873 | } | |
874 | #endif | |
875 | ||
fa131d94 | 876 | ret = object_class_dynamic_cast(class, typename); |
bf0fda34 | 877 | if (!ret && class) { |
be17f18b PB |
878 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
879 | file, line, func, class, typename); | |
2f28d2ff AL |
880 | abort(); |
881 | } | |
882 | ||
03587328 | 883 | #ifdef CONFIG_QOM_CAST_DEBUG |
9d6a3d58 | 884 | if (class && ret == class) { |
03587328 | 885 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd AB |
886 | atomic_set(&class->class_cast_cache[i - 1], |
887 | atomic_read(&class->class_cast_cache[i])); | |
03587328 | 888 | } |
b6b3ccfd | 889 | atomic_set(&class->class_cast_cache[i - 1], typename); |
03587328 AL |
890 | } |
891 | out: | |
892 | #endif | |
2f28d2ff AL |
893 | return ret; |
894 | } | |
895 | ||
8f5d58ef | 896 | const char *object_get_typename(const Object *obj) |
2f28d2ff AL |
897 | { |
898 | return obj->class->type->name; | |
899 | } | |
900 | ||
901 | ObjectClass *object_get_class(Object *obj) | |
902 | { | |
903 | return obj->class; | |
904 | } | |
905 | ||
17862378 AF |
906 | bool object_class_is_abstract(ObjectClass *klass) |
907 | { | |
908 | return klass->type->abstract; | |
909 | } | |
910 | ||
2f28d2ff AL |
911 | const char *object_class_get_name(ObjectClass *klass) |
912 | { | |
913 | return klass->type->name; | |
914 | } | |
915 | ||
916 | ObjectClass *object_class_by_name(const char *typename) | |
917 | { | |
918 | TypeImpl *type = type_get_by_name(typename); | |
919 | ||
920 | if (!type) { | |
921 | return NULL; | |
922 | } | |
923 | ||
ac451033 | 924 | type_initialize(type); |
2f28d2ff AL |
925 | |
926 | return type->class; | |
927 | } | |
928 | ||
e7cce67f PB |
929 | ObjectClass *object_class_get_parent(ObjectClass *class) |
930 | { | |
931 | TypeImpl *type = type_get_parent(class->type); | |
932 | ||
933 | if (!type) { | |
934 | return NULL; | |
935 | } | |
936 | ||
937 | type_initialize(type); | |
938 | ||
939 | return type->class; | |
940 | } | |
941 | ||
2f28d2ff AL |
942 | typedef struct OCFData |
943 | { | |
944 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
945 | const char *implements_type; |
946 | bool include_abstract; | |
2f28d2ff AL |
947 | void *opaque; |
948 | } OCFData; | |
949 | ||
950 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
951 | gpointer opaque) | |
952 | { | |
953 | OCFData *data = opaque; | |
954 | TypeImpl *type = value; | |
93c511a1 | 955 | ObjectClass *k; |
2f28d2ff | 956 | |
ac451033 | 957 | type_initialize(type); |
93c511a1 | 958 | k = type->class; |
2f28d2ff | 959 | |
93c511a1 AL |
960 | if (!data->include_abstract && type->abstract) { |
961 | return; | |
962 | } | |
963 | ||
964 | if (data->implements_type && | |
965 | !object_class_dynamic_cast(k, data->implements_type)) { | |
966 | return; | |
967 | } | |
968 | ||
969 | data->fn(k, data->opaque); | |
2f28d2ff AL |
970 | } |
971 | ||
972 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 973 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
974 | void *opaque) |
975 | { | |
93c511a1 | 976 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff | 977 | |
f54c19ca | 978 | enumerating_types = true; |
2f28d2ff | 979 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
f54c19ca | 980 | enumerating_types = false; |
2f28d2ff | 981 | } |
57c9fafe | 982 | |
d714b8de PC |
983 | static int do_object_child_foreach(Object *obj, |
984 | int (*fn)(Object *child, void *opaque), | |
985 | void *opaque, bool recurse) | |
32efc535 | 986 | { |
b604a854 PF |
987 | GHashTableIter iter; |
988 | ObjectProperty *prop; | |
32efc535 PB |
989 | int ret = 0; |
990 | ||
b604a854 PF |
991 | g_hash_table_iter_init(&iter, obj->properties); |
992 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
32efc535 | 993 | if (object_property_is_child(prop)) { |
d714b8de PC |
994 | Object *child = prop->opaque; |
995 | ||
996 | ret = fn(child, opaque); | |
32efc535 PB |
997 | if (ret != 0) { |
998 | break; | |
999 | } | |
d714b8de PC |
1000 | if (recurse) { |
1001 | do_object_child_foreach(child, fn, opaque, true); | |
1002 | } | |
32efc535 PB |
1003 | } |
1004 | } | |
1005 | return ret; | |
1006 | } | |
1007 | ||
d714b8de PC |
1008 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
1009 | void *opaque) | |
1010 | { | |
1011 | return do_object_child_foreach(obj, fn, opaque, false); | |
1012 | } | |
1013 | ||
1014 | int object_child_foreach_recursive(Object *obj, | |
1015 | int (*fn)(Object *child, void *opaque), | |
1016 | void *opaque) | |
1017 | { | |
1018 | return do_object_child_foreach(obj, fn, opaque, true); | |
1019 | } | |
1020 | ||
418ba9e5 AF |
1021 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
1022 | { | |
1023 | GSList **list = opaque; | |
1024 | ||
1025 | *list = g_slist_prepend(*list, klass); | |
1026 | } | |
1027 | ||
1028 | GSList *object_class_get_list(const char *implements_type, | |
1029 | bool include_abstract) | |
1030 | { | |
1031 | GSList *list = NULL; | |
1032 | ||
1033 | object_class_foreach(object_class_get_list_tramp, | |
1034 | implements_type, include_abstract, &list); | |
1035 | return list; | |
1036 | } | |
1037 | ||
47c66009 PB |
1038 | static gint object_class_cmp(gconstpointer a, gconstpointer b) |
1039 | { | |
1040 | return strcasecmp(object_class_get_name((ObjectClass *)a), | |
1041 | object_class_get_name((ObjectClass *)b)); | |
1042 | } | |
1043 | ||
1044 | GSList *object_class_get_list_sorted(const char *implements_type, | |
1045 | bool include_abstract) | |
1046 | { | |
1047 | return g_slist_sort(object_class_get_list(implements_type, include_abstract), | |
1048 | object_class_cmp); | |
1049 | } | |
1050 | ||
57c9fafe AL |
1051 | void object_ref(Object *obj) |
1052 | { | |
8ffad850 PC |
1053 | if (!obj) { |
1054 | return; | |
1055 | } | |
8438a135 | 1056 | atomic_inc(&obj->ref); |
57c9fafe AL |
1057 | } |
1058 | ||
1059 | void object_unref(Object *obj) | |
1060 | { | |
8ffad850 PC |
1061 | if (!obj) { |
1062 | return; | |
1063 | } | |
719a3077 | 1064 | g_assert(obj->ref > 0); |
57c9fafe AL |
1065 | |
1066 | /* parent always holds a reference to its children */ | |
f08c03f3 | 1067 | if (atomic_fetch_dec(&obj->ref) == 1) { |
57c9fafe AL |
1068 | object_finalize(obj); |
1069 | } | |
1070 | } | |
1071 | ||
64607d08 PB |
1072 | ObjectProperty * |
1073 | object_property_add(Object *obj, const char *name, const char *type, | |
1074 | ObjectPropertyAccessor *get, | |
1075 | ObjectPropertyAccessor *set, | |
1076 | ObjectPropertyRelease *release, | |
1077 | void *opaque, Error **errp) | |
57c9fafe | 1078 | { |
54852b03 | 1079 | ObjectProperty *prop; |
33965904 PC |
1080 | size_t name_len = strlen(name); |
1081 | ||
1082 | if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { | |
1083 | int i; | |
1084 | ObjectProperty *ret; | |
1085 | char *name_no_array = g_strdup(name); | |
1086 | ||
1087 | name_no_array[name_len - 3] = '\0'; | |
1088 | for (i = 0; ; ++i) { | |
1089 | char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); | |
1090 | ||
1091 | ret = object_property_add(obj, full_name, type, get, set, | |
1092 | release, opaque, NULL); | |
1093 | g_free(full_name); | |
1094 | if (ret) { | |
1095 | break; | |
1096 | } | |
1097 | } | |
1098 | g_free(name_no_array); | |
1099 | return ret; | |
1100 | } | |
54852b03 | 1101 | |
16bf7f52 | 1102 | if (object_property_find(obj, name, NULL) != NULL) { |
b604a854 | 1103 | error_setg(errp, "attempt to add duplicate property '%s'" |
16bf7f52 DB |
1104 | " to object (type '%s')", name, |
1105 | object_get_typename(obj)); | |
b604a854 | 1106 | return NULL; |
54852b03 PM |
1107 | } |
1108 | ||
1109 | prop = g_malloc0(sizeof(*prop)); | |
57c9fafe AL |
1110 | |
1111 | prop->name = g_strdup(name); | |
1112 | prop->type = g_strdup(type); | |
1113 | ||
1114 | prop->get = get; | |
1115 | prop->set = set; | |
1116 | prop->release = release; | |
1117 | prop->opaque = opaque; | |
1118 | ||
b604a854 | 1119 | g_hash_table_insert(obj->properties, prop->name, prop); |
64607d08 | 1120 | return prop; |
57c9fafe AL |
1121 | } |
1122 | ||
16bf7f52 DB |
1123 | ObjectProperty * |
1124 | object_class_property_add(ObjectClass *klass, | |
1125 | const char *name, | |
1126 | const char *type, | |
1127 | ObjectPropertyAccessor *get, | |
1128 | ObjectPropertyAccessor *set, | |
1129 | ObjectPropertyRelease *release, | |
1130 | void *opaque, | |
1131 | Error **errp) | |
1132 | { | |
1133 | ObjectProperty *prop; | |
1134 | ||
1135 | if (object_class_property_find(klass, name, NULL) != NULL) { | |
1136 | error_setg(errp, "attempt to add duplicate property '%s'" | |
1137 | " to object (type '%s')", name, | |
1138 | object_class_get_name(klass)); | |
1139 | return NULL; | |
1140 | } | |
1141 | ||
1142 | prop = g_malloc0(sizeof(*prop)); | |
1143 | ||
1144 | prop->name = g_strdup(name); | |
1145 | prop->type = g_strdup(type); | |
1146 | ||
1147 | prop->get = get; | |
1148 | prop->set = set; | |
1149 | prop->release = release; | |
1150 | prop->opaque = opaque; | |
1151 | ||
1152 | g_hash_table_insert(klass->properties, g_strdup(name), prop); | |
1153 | ||
1154 | return prop; | |
1155 | } | |
1156 | ||
89bfe000 PB |
1157 | ObjectProperty *object_property_find(Object *obj, const char *name, |
1158 | Error **errp) | |
57c9fafe AL |
1159 | { |
1160 | ObjectProperty *prop; | |
16bf7f52 DB |
1161 | ObjectClass *klass = object_get_class(obj); |
1162 | ||
1163 | prop = object_class_property_find(klass, name, NULL); | |
1164 | if (prop) { | |
1165 | return prop; | |
1166 | } | |
57c9fafe | 1167 | |
b604a854 PF |
1168 | prop = g_hash_table_lookup(obj->properties, name); |
1169 | if (prop) { | |
1170 | return prop; | |
57c9fafe AL |
1171 | } |
1172 | ||
f231b88d | 1173 | error_setg(errp, "Property '.%s' not found", name); |
57c9fafe AL |
1174 | return NULL; |
1175 | } | |
1176 | ||
7746abd8 DB |
1177 | void object_property_iter_init(ObjectPropertyIterator *iter, |
1178 | Object *obj) | |
a00c9482 | 1179 | { |
7746abd8 DB |
1180 | g_hash_table_iter_init(&iter->iter, obj->properties); |
1181 | iter->nextclass = object_get_class(obj); | |
a00c9482 DB |
1182 | } |
1183 | ||
1184 | ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter) | |
1185 | { | |
b604a854 | 1186 | gpointer key, val; |
16bf7f52 DB |
1187 | while (!g_hash_table_iter_next(&iter->iter, &key, &val)) { |
1188 | if (!iter->nextclass) { | |
1189 | return NULL; | |
1190 | } | |
1191 | g_hash_table_iter_init(&iter->iter, iter->nextclass->properties); | |
1192 | iter->nextclass = object_class_get_parent(iter->nextclass); | |
a00c9482 | 1193 | } |
b604a854 | 1194 | return val; |
a00c9482 DB |
1195 | } |
1196 | ||
961c47bb AK |
1197 | void object_class_property_iter_init(ObjectPropertyIterator *iter, |
1198 | ObjectClass *klass) | |
1199 | { | |
1200 | g_hash_table_iter_init(&iter->iter, klass->properties); | |
684546d8 | 1201 | iter->nextclass = object_class_get_parent(klass); |
961c47bb AK |
1202 | } |
1203 | ||
16bf7f52 DB |
1204 | ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, |
1205 | Error **errp) | |
1206 | { | |
1207 | ObjectProperty *prop; | |
1208 | ObjectClass *parent_klass; | |
1209 | ||
1210 | parent_klass = object_class_get_parent(klass); | |
1211 | if (parent_klass) { | |
1212 | prop = object_class_property_find(parent_klass, name, NULL); | |
1213 | if (prop) { | |
1214 | return prop; | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | prop = g_hash_table_lookup(klass->properties, name); | |
1219 | if (!prop) { | |
1220 | error_setg(errp, "Property '.%s' not found", name); | |
1221 | } | |
1222 | return prop; | |
1223 | } | |
1224 | ||
57c9fafe AL |
1225 | void object_property_del(Object *obj, const char *name, Error **errp) |
1226 | { | |
b604a854 PF |
1227 | ObjectProperty *prop = g_hash_table_lookup(obj->properties, name); |
1228 | ||
1229 | if (!prop) { | |
1230 | error_setg(errp, "Property '.%s' not found", name); | |
0866aca1 AL |
1231 | return; |
1232 | } | |
57c9fafe | 1233 | |
0866aca1 AL |
1234 | if (prop->release) { |
1235 | prop->release(obj, name, prop->opaque); | |
1236 | } | |
b604a854 | 1237 | g_hash_table_remove(obj->properties, name); |
57c9fafe AL |
1238 | } |
1239 | ||
1240 | void object_property_get(Object *obj, Visitor *v, const char *name, | |
1241 | Error **errp) | |
1242 | { | |
89bfe000 | 1243 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1244 | if (prop == NULL) { |
57c9fafe AL |
1245 | return; |
1246 | } | |
1247 | ||
1248 | if (!prop->get) { | |
c6bd8c70 | 1249 | error_setg(errp, QERR_PERMISSION_DENIED); |
57c9fafe | 1250 | } else { |
d7bce999 | 1251 | prop->get(obj, v, name, prop->opaque, errp); |
57c9fafe AL |
1252 | } |
1253 | } | |
1254 | ||
1255 | void object_property_set(Object *obj, Visitor *v, const char *name, | |
1256 | Error **errp) | |
1257 | { | |
89bfe000 | 1258 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1259 | if (prop == NULL) { |
57c9fafe AL |
1260 | return; |
1261 | } | |
1262 | ||
1263 | if (!prop->set) { | |
c6bd8c70 | 1264 | error_setg(errp, QERR_PERMISSION_DENIED); |
57c9fafe | 1265 | } else { |
d7bce999 | 1266 | prop->set(obj, v, name, prop->opaque, errp); |
57c9fafe AL |
1267 | } |
1268 | } | |
1269 | ||
7b7b7d18 PB |
1270 | void object_property_set_str(Object *obj, const char *value, |
1271 | const char *name, Error **errp) | |
1272 | { | |
1273 | QString *qstr = qstring_from_str(value); | |
1274 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); | |
1275 | ||
cb3e7f08 | 1276 | qobject_unref(qstr); |
7b7b7d18 PB |
1277 | } |
1278 | ||
1279 | char *object_property_get_str(Object *obj, const char *name, | |
1280 | Error **errp) | |
1281 | { | |
1282 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
7b7b7d18 PB |
1283 | char *retval; |
1284 | ||
1285 | if (!ret) { | |
1286 | return NULL; | |
1287 | } | |
aafb21a0 PX |
1288 | |
1289 | retval = g_strdup(qobject_get_try_str(ret)); | |
1290 | if (!retval) { | |
c6bd8c70 | 1291 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); |
7b7b7d18 PB |
1292 | } |
1293 | ||
cb3e7f08 | 1294 | qobject_unref(ret); |
7b7b7d18 PB |
1295 | return retval; |
1296 | } | |
1297 | ||
1d9c5a12 PB |
1298 | void object_property_set_link(Object *obj, Object *value, |
1299 | const char *name, Error **errp) | |
1300 | { | |
d3c49316 PC |
1301 | if (value) { |
1302 | gchar *path = object_get_canonical_path(value); | |
1303 | object_property_set_str(obj, path, name, errp); | |
1304 | g_free(path); | |
1305 | } else { | |
1306 | object_property_set_str(obj, "", name, errp); | |
1307 | } | |
1d9c5a12 PB |
1308 | } |
1309 | ||
1310 | Object *object_property_get_link(Object *obj, const char *name, | |
1311 | Error **errp) | |
1312 | { | |
1313 | char *str = object_property_get_str(obj, name, errp); | |
1314 | Object *target = NULL; | |
1315 | ||
1316 | if (str && *str) { | |
1317 | target = object_resolve_path(str, NULL); | |
1318 | if (!target) { | |
75158ebb MA |
1319 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1320 | "Device '%s' not found", str); | |
1d9c5a12 PB |
1321 | } |
1322 | } | |
1323 | ||
1324 | g_free(str); | |
1325 | return target; | |
1326 | } | |
1327 | ||
7b7b7d18 PB |
1328 | void object_property_set_bool(Object *obj, bool value, |
1329 | const char *name, Error **errp) | |
1330 | { | |
fc48ffc3 | 1331 | QBool *qbool = qbool_from_bool(value); |
7b7b7d18 PB |
1332 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); |
1333 | ||
cb3e7f08 | 1334 | qobject_unref(qbool); |
7b7b7d18 PB |
1335 | } |
1336 | ||
1337 | bool object_property_get_bool(Object *obj, const char *name, | |
1338 | Error **errp) | |
1339 | { | |
1340 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1341 | QBool *qbool; | |
1342 | bool retval; | |
1343 | ||
1344 | if (!ret) { | |
1345 | return false; | |
1346 | } | |
7dc847eb | 1347 | qbool = qobject_to(QBool, ret); |
7b7b7d18 | 1348 | if (!qbool) { |
c6bd8c70 | 1349 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); |
7b7b7d18 PB |
1350 | retval = false; |
1351 | } else { | |
fc48ffc3 | 1352 | retval = qbool_get_bool(qbool); |
7b7b7d18 PB |
1353 | } |
1354 | ||
cb3e7f08 | 1355 | qobject_unref(ret); |
7b7b7d18 PB |
1356 | return retval; |
1357 | } | |
1358 | ||
1359 | void object_property_set_int(Object *obj, int64_t value, | |
1360 | const char *name, Error **errp) | |
1361 | { | |
01b2ffce MAL |
1362 | QNum *qnum = qnum_from_int(value); |
1363 | object_property_set_qobject(obj, QOBJECT(qnum), name, errp); | |
7b7b7d18 | 1364 | |
cb3e7f08 | 1365 | qobject_unref(qnum); |
7b7b7d18 PB |
1366 | } |
1367 | ||
1368 | int64_t object_property_get_int(Object *obj, const char *name, | |
1369 | Error **errp) | |
1370 | { | |
1371 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
01b2ffce | 1372 | QNum *qnum; |
7b7b7d18 PB |
1373 | int64_t retval; |
1374 | ||
1375 | if (!ret) { | |
1376 | return -1; | |
1377 | } | |
01b2ffce | 1378 | |
7dc847eb | 1379 | qnum = qobject_to(QNum, ret); |
01b2ffce | 1380 | if (!qnum || !qnum_get_try_int(qnum, &retval)) { |
c6bd8c70 | 1381 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); |
7b7b7d18 | 1382 | retval = -1; |
7b7b7d18 PB |
1383 | } |
1384 | ||
cb3e7f08 | 1385 | qobject_unref(ret); |
7b7b7d18 | 1386 | return retval; |
b2cd7dee PB |
1387 | } |
1388 | ||
3152779c MAL |
1389 | void object_property_set_uint(Object *obj, uint64_t value, |
1390 | const char *name, Error **errp) | |
1391 | { | |
1392 | QNum *qnum = qnum_from_uint(value); | |
1393 | ||
1394 | object_property_set_qobject(obj, QOBJECT(qnum), name, errp); | |
cb3e7f08 | 1395 | qobject_unref(qnum); |
3152779c MAL |
1396 | } |
1397 | ||
1398 | uint64_t object_property_get_uint(Object *obj, const char *name, | |
1399 | Error **errp) | |
1400 | { | |
1401 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1402 | QNum *qnum; | |
1403 | uint64_t retval; | |
1404 | ||
1405 | if (!ret) { | |
1406 | return 0; | |
1407 | } | |
7dc847eb | 1408 | qnum = qobject_to(QNum, ret); |
3152779c MAL |
1409 | if (!qnum || !qnum_get_try_uint(qnum, &retval)) { |
1410 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint"); | |
1411 | retval = 0; | |
1412 | } | |
1413 | ||
cb3e7f08 | 1414 | qobject_unref(ret); |
3152779c MAL |
1415 | return retval; |
1416 | } | |
1417 | ||
a8e3fbed | 1418 | typedef struct EnumProperty { |
f7abe0ec | 1419 | const QEnumLookup *lookup; |
a8e3fbed DB |
1420 | int (*get)(Object *, Error **); |
1421 | void (*set)(Object *, int, Error **); | |
1422 | } EnumProperty; | |
1423 | ||
1f21772d | 1424 | int object_property_get_enum(Object *obj, const char *name, |
a3590dac | 1425 | const char *typename, Error **errp) |
1f21772d | 1426 | { |
4715d42e | 1427 | Error *err = NULL; |
7a0525c7 | 1428 | Visitor *v; |
976620ac | 1429 | char *str; |
1f21772d | 1430 | int ret; |
a3590dac DB |
1431 | ObjectProperty *prop = object_property_find(obj, name, errp); |
1432 | EnumProperty *enumprop; | |
1433 | ||
1434 | if (prop == NULL) { | |
1435 | return 0; | |
1436 | } | |
1437 | ||
1438 | if (!g_str_equal(prop->type, typename)) { | |
1439 | error_setg(errp, "Property %s on %s is not '%s' enum type", | |
1440 | name, object_class_get_name( | |
1441 | object_get_class(obj)), typename); | |
1442 | return 0; | |
1443 | } | |
1444 | ||
1445 | enumprop = prop->opaque; | |
1f21772d | 1446 | |
3b098d56 | 1447 | v = string_output_visitor_new(false, &str); |
e7ca5656 | 1448 | object_property_get(obj, v, name, &err); |
4715d42e MA |
1449 | if (err) { |
1450 | error_propagate(errp, err); | |
e7ca5656 | 1451 | visit_free(v); |
4715d42e MA |
1452 | return 0; |
1453 | } | |
3b098d56 | 1454 | visit_complete(v, &str); |
e7ca5656 | 1455 | visit_free(v); |
7a0525c7 | 1456 | v = string_input_visitor_new(str); |
f7abe0ec | 1457 | visit_type_enum(v, name, &ret, enumprop->lookup, errp); |
976620ac CF |
1458 | |
1459 | g_free(str); | |
7a0525c7 | 1460 | visit_free(v); |
1f21772d HT |
1461 | |
1462 | return ret; | |
1463 | } | |
1464 | ||
1465 | void object_property_get_uint16List(Object *obj, const char *name, | |
1466 | uint16List **list, Error **errp) | |
1467 | { | |
4715d42e | 1468 | Error *err = NULL; |
7a0525c7 | 1469 | Visitor *v; |
976620ac | 1470 | char *str; |
1f21772d | 1471 | |
3b098d56 EB |
1472 | v = string_output_visitor_new(false, &str); |
1473 | object_property_get(obj, v, name, &err); | |
4715d42e MA |
1474 | if (err) { |
1475 | error_propagate(errp, err); | |
1476 | goto out; | |
1477 | } | |
3b098d56 EB |
1478 | visit_complete(v, &str); |
1479 | visit_free(v); | |
7a0525c7 EB |
1480 | v = string_input_visitor_new(str); |
1481 | visit_type_uint16List(v, NULL, list, errp); | |
976620ac CF |
1482 | |
1483 | g_free(str); | |
4715d42e | 1484 | out: |
3b098d56 | 1485 | visit_free(v); |
1f21772d HT |
1486 | } |
1487 | ||
b2cd7dee PB |
1488 | void object_property_parse(Object *obj, const char *string, |
1489 | const char *name, Error **errp) | |
1490 | { | |
7a0525c7 EB |
1491 | Visitor *v = string_input_visitor_new(string); |
1492 | object_property_set(obj, v, name, errp); | |
1493 | visit_free(v); | |
b2cd7dee PB |
1494 | } |
1495 | ||
0b7593e0 | 1496 | char *object_property_print(Object *obj, const char *name, bool human, |
b2cd7dee PB |
1497 | Error **errp) |
1498 | { | |
3b098d56 | 1499 | Visitor *v; |
3a53009f GA |
1500 | char *string = NULL; |
1501 | Error *local_err = NULL; | |
b2cd7dee | 1502 | |
3b098d56 EB |
1503 | v = string_output_visitor_new(human, &string); |
1504 | object_property_get(obj, v, name, &local_err); | |
3a53009f GA |
1505 | if (local_err) { |
1506 | error_propagate(errp, local_err); | |
1507 | goto out; | |
1508 | } | |
1509 | ||
3b098d56 | 1510 | visit_complete(v, &string); |
3a53009f GA |
1511 | |
1512 | out: | |
3b098d56 | 1513 | visit_free(v); |
b2cd7dee | 1514 | return string; |
7b7b7d18 PB |
1515 | } |
1516 | ||
57c9fafe AL |
1517 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
1518 | { | |
89bfe000 | 1519 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1520 | if (prop == NULL) { |
57c9fafe AL |
1521 | return NULL; |
1522 | } | |
1523 | ||
1524 | return prop->type; | |
1525 | } | |
1526 | ||
1527 | Object *object_get_root(void) | |
1528 | { | |
8b45d447 | 1529 | static Object *root; |
57c9fafe | 1530 | |
8b45d447 AL |
1531 | if (!root) { |
1532 | root = object_new("container"); | |
57c9fafe AL |
1533 | } |
1534 | ||
8b45d447 | 1535 | return root; |
57c9fafe AL |
1536 | } |
1537 | ||
bc2256c4 DB |
1538 | Object *object_get_objects_root(void) |
1539 | { | |
1540 | return container_get(object_get_root(), "/objects"); | |
1541 | } | |
1542 | ||
7c47c4ea PX |
1543 | Object *object_get_internal_root(void) |
1544 | { | |
1545 | static Object *internal_root; | |
1546 | ||
1547 | if (!internal_root) { | |
1548 | internal_root = object_new("container"); | |
1549 | } | |
1550 | ||
1551 | return internal_root; | |
1552 | } | |
1553 | ||
d7bce999 EB |
1554 | static void object_get_child_property(Object *obj, Visitor *v, |
1555 | const char *name, void *opaque, | |
1556 | Error **errp) | |
57c9fafe AL |
1557 | { |
1558 | Object *child = opaque; | |
1559 | gchar *path; | |
1560 | ||
1561 | path = object_get_canonical_path(child); | |
51e72bc1 | 1562 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1563 | g_free(path); |
1564 | } | |
1565 | ||
64607d08 PB |
1566 | static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part) |
1567 | { | |
1568 | return opaque; | |
1569 | } | |
1570 | ||
db85b575 AL |
1571 | static void object_finalize_child_property(Object *obj, const char *name, |
1572 | void *opaque) | |
1573 | { | |
1574 | Object *child = opaque; | |
1575 | ||
bffc687d PB |
1576 | if (child->class->unparent) { |
1577 | (child->class->unparent)(child); | |
1578 | } | |
1579 | child->parent = NULL; | |
db85b575 AL |
1580 | object_unref(child); |
1581 | } | |
1582 | ||
57c9fafe AL |
1583 | void object_property_add_child(Object *obj, const char *name, |
1584 | Object *child, Error **errp) | |
1585 | { | |
b0ed5e9f | 1586 | Error *local_err = NULL; |
57c9fafe | 1587 | gchar *type; |
64607d08 | 1588 | ObjectProperty *op; |
57c9fafe | 1589 | |
8faa2f85 PC |
1590 | if (child->parent != NULL) { |
1591 | error_setg(errp, "child object is already parented"); | |
1592 | return; | |
1593 | } | |
1594 | ||
57c9fafe AL |
1595 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
1596 | ||
64607d08 PB |
1597 | op = object_property_add(obj, name, type, object_get_child_property, NULL, |
1598 | object_finalize_child_property, child, &local_err); | |
b0ed5e9f PB |
1599 | if (local_err) { |
1600 | error_propagate(errp, local_err); | |
1601 | goto out; | |
1602 | } | |
64607d08 PB |
1603 | |
1604 | op->resolve = object_resolve_child_property; | |
57c9fafe | 1605 | object_ref(child); |
57c9fafe AL |
1606 | child->parent = obj; |
1607 | ||
b0ed5e9f | 1608 | out: |
57c9fafe AL |
1609 | g_free(type); |
1610 | } | |
1611 | ||
8f5d58ef | 1612 | void object_property_allow_set_link(const Object *obj, const char *name, |
39f72ef9 SH |
1613 | Object *val, Error **errp) |
1614 | { | |
1615 | /* Allow the link to be set, always */ | |
1616 | } | |
1617 | ||
9561fda8 SH |
1618 | typedef struct { |
1619 | Object **child; | |
8f5d58ef | 1620 | void (*check)(const Object *, const char *, Object *, Error **); |
9561fda8 SH |
1621 | ObjectPropertyLinkFlags flags; |
1622 | } LinkProperty; | |
1623 | ||
d7bce999 EB |
1624 | static void object_get_link_property(Object *obj, Visitor *v, |
1625 | const char *name, void *opaque, | |
1626 | Error **errp) | |
57c9fafe | 1627 | { |
9561fda8 SH |
1628 | LinkProperty *lprop = opaque; |
1629 | Object **child = lprop->child; | |
57c9fafe AL |
1630 | gchar *path; |
1631 | ||
1632 | if (*child) { | |
1633 | path = object_get_canonical_path(*child); | |
51e72bc1 | 1634 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1635 | g_free(path); |
1636 | } else { | |
1637 | path = (gchar *)""; | |
51e72bc1 | 1638 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1639 | } |
1640 | } | |
1641 | ||
f5ec6704 SH |
1642 | /* |
1643 | * object_resolve_link: | |
1644 | * | |
1645 | * Lookup an object and ensure its type matches the link property type. This | |
1646 | * is similar to object_resolve_path() except type verification against the | |
1647 | * link property is performed. | |
1648 | * | |
1649 | * Returns: The matched object or NULL on path lookup failures. | |
1650 | */ | |
1651 | static Object *object_resolve_link(Object *obj, const char *name, | |
1652 | const char *path, Error **errp) | |
1653 | { | |
1654 | const char *type; | |
1655 | gchar *target_type; | |
1656 | bool ambiguous = false; | |
1657 | Object *target; | |
1658 | ||
1659 | /* Go from link<FOO> to FOO. */ | |
1660 | type = object_property_get_type(obj, name, NULL); | |
1661 | target_type = g_strndup(&type[5], strlen(type) - 6); | |
1662 | target = object_resolve_path_type(path, target_type, &ambiguous); | |
1663 | ||
1664 | if (ambiguous) { | |
455b0fde EB |
1665 | error_setg(errp, "Path '%s' does not uniquely identify an object", |
1666 | path); | |
f5ec6704 SH |
1667 | } else if (!target) { |
1668 | target = object_resolve_path(path, &ambiguous); | |
1669 | if (target || ambiguous) { | |
c6bd8c70 | 1670 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); |
f5ec6704 | 1671 | } else { |
75158ebb MA |
1672 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1673 | "Device '%s' not found", path); | |
f5ec6704 SH |
1674 | } |
1675 | target = NULL; | |
1676 | } | |
1677 | g_free(target_type); | |
1678 | ||
1679 | return target; | |
1680 | } | |
1681 | ||
d7bce999 EB |
1682 | static void object_set_link_property(Object *obj, Visitor *v, |
1683 | const char *name, void *opaque, | |
1684 | Error **errp) | |
57c9fafe | 1685 | { |
c6aed983 | 1686 | Error *local_err = NULL; |
9561fda8 SH |
1687 | LinkProperty *prop = opaque; |
1688 | Object **child = prop->child; | |
c6aed983 SH |
1689 | Object *old_target = *child; |
1690 | Object *new_target = NULL; | |
1691 | char *path = NULL; | |
57c9fafe | 1692 | |
51e72bc1 | 1693 | visit_type_str(v, name, &path, &local_err); |
57c9fafe | 1694 | |
c6aed983 SH |
1695 | if (!local_err && strcmp(path, "") != 0) { |
1696 | new_target = object_resolve_link(obj, name, path, &local_err); | |
57c9fafe AL |
1697 | } |
1698 | ||
1699 | g_free(path); | |
c6aed983 SH |
1700 | if (local_err) { |
1701 | error_propagate(errp, local_err); | |
1702 | return; | |
1703 | } | |
f0cdc966 | 1704 | |
39f72ef9 SH |
1705 | prop->check(obj, name, new_target, &local_err); |
1706 | if (local_err) { | |
1707 | error_propagate(errp, local_err); | |
1708 | return; | |
1709 | } | |
1710 | ||
c6aed983 | 1711 | *child = new_target; |
265b578c MAL |
1712 | if (prop->flags == OBJ_PROP_LINK_STRONG) { |
1713 | object_ref(new_target); | |
1714 | object_unref(old_target); | |
1715 | } | |
57c9fafe AL |
1716 | } |
1717 | ||
64607d08 PB |
1718 | static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part) |
1719 | { | |
1720 | LinkProperty *lprop = opaque; | |
1721 | ||
1722 | return *lprop->child; | |
1723 | } | |
1724 | ||
9561fda8 SH |
1725 | static void object_release_link_property(Object *obj, const char *name, |
1726 | void *opaque) | |
1727 | { | |
1728 | LinkProperty *prop = opaque; | |
1729 | ||
265b578c | 1730 | if ((prop->flags & OBJ_PROP_LINK_STRONG) && *prop->child) { |
9561fda8 SH |
1731 | object_unref(*prop->child); |
1732 | } | |
1733 | g_free(prop); | |
1734 | } | |
1735 | ||
57c9fafe AL |
1736 | void object_property_add_link(Object *obj, const char *name, |
1737 | const char *type, Object **child, | |
8f5d58ef | 1738 | void (*check)(const Object *, const char *, |
39f72ef9 | 1739 | Object *, Error **), |
9561fda8 | 1740 | ObjectPropertyLinkFlags flags, |
57c9fafe AL |
1741 | Error **errp) |
1742 | { | |
9561fda8 SH |
1743 | Error *local_err = NULL; |
1744 | LinkProperty *prop = g_malloc(sizeof(*prop)); | |
57c9fafe | 1745 | gchar *full_type; |
64607d08 | 1746 | ObjectProperty *op; |
57c9fafe | 1747 | |
9561fda8 | 1748 | prop->child = child; |
39f72ef9 | 1749 | prop->check = check; |
9561fda8 SH |
1750 | prop->flags = flags; |
1751 | ||
57c9fafe AL |
1752 | full_type = g_strdup_printf("link<%s>", type); |
1753 | ||
64607d08 PB |
1754 | op = object_property_add(obj, name, full_type, |
1755 | object_get_link_property, | |
1756 | check ? object_set_link_property : NULL, | |
1757 | object_release_link_property, | |
1758 | prop, | |
1759 | &local_err); | |
9561fda8 SH |
1760 | if (local_err) { |
1761 | error_propagate(errp, local_err); | |
1762 | g_free(prop); | |
64607d08 | 1763 | goto out; |
9561fda8 | 1764 | } |
57c9fafe | 1765 | |
64607d08 PB |
1766 | op->resolve = object_resolve_link_property; |
1767 | ||
1768 | out: | |
57c9fafe AL |
1769 | g_free(full_type); |
1770 | } | |
1771 | ||
fb9e7e33 PB |
1772 | void object_property_add_const_link(Object *obj, const char *name, |
1773 | Object *target, Error **errp) | |
1774 | { | |
1775 | char *link_type; | |
1776 | ObjectProperty *op; | |
1777 | ||
1778 | link_type = g_strdup_printf("link<%s>", object_get_typename(target)); | |
1779 | op = object_property_add(obj, name, link_type, | |
1780 | object_get_child_property, NULL, | |
1781 | NULL, target, errp); | |
1782 | if (op != NULL) { | |
1783 | op->resolve = object_resolve_child_property; | |
1784 | } | |
1785 | g_free(link_type); | |
1786 | } | |
1787 | ||
11f590b1 SH |
1788 | gchar *object_get_canonical_path_component(Object *obj) |
1789 | { | |
1790 | ObjectProperty *prop = NULL; | |
b604a854 | 1791 | GHashTableIter iter; |
11f590b1 | 1792 | |
770dec26 PB |
1793 | if (obj->parent == NULL) { |
1794 | return NULL; | |
1795 | } | |
11f590b1 | 1796 | |
b604a854 PF |
1797 | g_hash_table_iter_init(&iter, obj->parent->properties); |
1798 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
11f590b1 SH |
1799 | if (!object_property_is_child(prop)) { |
1800 | continue; | |
1801 | } | |
1802 | ||
1803 | if (prop->opaque == obj) { | |
1804 | return g_strdup(prop->name); | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | /* obj had a parent but was not a child, should never happen */ | |
1809 | g_assert_not_reached(); | |
1810 | return NULL; | |
1811 | } | |
1812 | ||
57c9fafe AL |
1813 | gchar *object_get_canonical_path(Object *obj) |
1814 | { | |
1815 | Object *root = object_get_root(); | |
11f590b1 | 1816 | char *newpath, *path = NULL; |
57c9fafe | 1817 | |
e40077fd PB |
1818 | if (obj == root) { |
1819 | return g_strdup("/"); | |
1820 | } | |
1821 | ||
1822 | do { | |
11f590b1 | 1823 | char *component = object_get_canonical_path_component(obj); |
57c9fafe | 1824 | |
e40077fd PB |
1825 | if (!component) { |
1826 | /* A canonical path must be complete, so discard what was | |
1827 | * collected so far. | |
1828 | */ | |
11f590b1 | 1829 | g_free(path); |
e40077fd | 1830 | return NULL; |
57c9fafe AL |
1831 | } |
1832 | ||
e40077fd PB |
1833 | newpath = g_strdup_printf("/%s%s", component, path ? path : ""); |
1834 | g_free(path); | |
1835 | g_free(component); | |
1836 | path = newpath; | |
57c9fafe | 1837 | obj = obj->parent; |
e40077fd | 1838 | } while (obj != root); |
57c9fafe | 1839 | |
e40077fd | 1840 | return path; |
57c9fafe AL |
1841 | } |
1842 | ||
3e84b483 | 1843 | Object *object_resolve_path_component(Object *parent, const gchar *part) |
a612b2a6 | 1844 | { |
89bfe000 | 1845 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
a612b2a6 PB |
1846 | if (prop == NULL) { |
1847 | return NULL; | |
1848 | } | |
1849 | ||
64607d08 PB |
1850 | if (prop->resolve) { |
1851 | return prop->resolve(parent, prop->opaque, part); | |
a612b2a6 PB |
1852 | } else { |
1853 | return NULL; | |
1854 | } | |
1855 | } | |
1856 | ||
57c9fafe AL |
1857 | static Object *object_resolve_abs_path(Object *parent, |
1858 | gchar **parts, | |
02fe2db6 | 1859 | const char *typename, |
57c9fafe AL |
1860 | int index) |
1861 | { | |
57c9fafe AL |
1862 | Object *child; |
1863 | ||
1864 | if (parts[index] == NULL) { | |
02fe2db6 | 1865 | return object_dynamic_cast(parent, typename); |
57c9fafe AL |
1866 | } |
1867 | ||
1868 | if (strcmp(parts[index], "") == 0) { | |
02fe2db6 | 1869 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
57c9fafe AL |
1870 | } |
1871 | ||
a612b2a6 | 1872 | child = object_resolve_path_component(parent, parts[index]); |
57c9fafe AL |
1873 | if (!child) { |
1874 | return NULL; | |
1875 | } | |
1876 | ||
02fe2db6 | 1877 | return object_resolve_abs_path(child, parts, typename, index + 1); |
57c9fafe AL |
1878 | } |
1879 | ||
1880 | static Object *object_resolve_partial_path(Object *parent, | |
1881 | gchar **parts, | |
02fe2db6 | 1882 | const char *typename, |
57c9fafe AL |
1883 | bool *ambiguous) |
1884 | { | |
1885 | Object *obj; | |
b604a854 | 1886 | GHashTableIter iter; |
57c9fafe AL |
1887 | ObjectProperty *prop; |
1888 | ||
02fe2db6 | 1889 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
57c9fafe | 1890 | |
b604a854 PF |
1891 | g_hash_table_iter_init(&iter, parent->properties); |
1892 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
57c9fafe AL |
1893 | Object *found; |
1894 | ||
5d9d3f47 | 1895 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
1896 | continue; |
1897 | } | |
1898 | ||
02fe2db6 PB |
1899 | found = object_resolve_partial_path(prop->opaque, parts, |
1900 | typename, ambiguous); | |
57c9fafe AL |
1901 | if (found) { |
1902 | if (obj) { | |
ebcc479e | 1903 | *ambiguous = true; |
57c9fafe AL |
1904 | return NULL; |
1905 | } | |
1906 | obj = found; | |
1907 | } | |
1908 | ||
ebcc479e | 1909 | if (*ambiguous) { |
57c9fafe AL |
1910 | return NULL; |
1911 | } | |
1912 | } | |
1913 | ||
1914 | return obj; | |
1915 | } | |
1916 | ||
02fe2db6 | 1917 | Object *object_resolve_path_type(const char *path, const char *typename, |
ebcc479e | 1918 | bool *ambiguousp) |
57c9fafe | 1919 | { |
57c9fafe AL |
1920 | Object *obj; |
1921 | gchar **parts; | |
1922 | ||
1923 | parts = g_strsplit(path, "/", 0); | |
2e1103f6 | 1924 | assert(parts); |
57c9fafe | 1925 | |
2e1103f6 | 1926 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
ebcc479e | 1927 | bool ambiguous = false; |
02fe2db6 | 1928 | obj = object_resolve_partial_path(object_get_root(), parts, |
ebcc479e EH |
1929 | typename, &ambiguous); |
1930 | if (ambiguousp) { | |
1931 | *ambiguousp = ambiguous; | |
1932 | } | |
57c9fafe | 1933 | } else { |
02fe2db6 | 1934 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
57c9fafe AL |
1935 | } |
1936 | ||
1937 | g_strfreev(parts); | |
1938 | ||
1939 | return obj; | |
1940 | } | |
1941 | ||
02fe2db6 PB |
1942 | Object *object_resolve_path(const char *path, bool *ambiguous) |
1943 | { | |
1944 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); | |
1945 | } | |
1946 | ||
57c9fafe AL |
1947 | typedef struct StringProperty |
1948 | { | |
1949 | char *(*get)(Object *, Error **); | |
1950 | void (*set)(Object *, const char *, Error **); | |
1951 | } StringProperty; | |
1952 | ||
d7bce999 EB |
1953 | static void property_get_str(Object *obj, Visitor *v, const char *name, |
1954 | void *opaque, Error **errp) | |
57c9fafe AL |
1955 | { |
1956 | StringProperty *prop = opaque; | |
1957 | char *value; | |
e1c8237d | 1958 | Error *err = NULL; |
57c9fafe | 1959 | |
e1c8237d MA |
1960 | value = prop->get(obj, &err); |
1961 | if (err) { | |
1962 | error_propagate(errp, err); | |
1963 | return; | |
57c9fafe | 1964 | } |
e1c8237d | 1965 | |
51e72bc1 | 1966 | visit_type_str(v, name, &value, errp); |
e1c8237d | 1967 | g_free(value); |
57c9fafe AL |
1968 | } |
1969 | ||
d7bce999 EB |
1970 | static void property_set_str(Object *obj, Visitor *v, const char *name, |
1971 | void *opaque, Error **errp) | |
57c9fafe AL |
1972 | { |
1973 | StringProperty *prop = opaque; | |
1974 | char *value; | |
1975 | Error *local_err = NULL; | |
1976 | ||
51e72bc1 | 1977 | visit_type_str(v, name, &value, &local_err); |
57c9fafe AL |
1978 | if (local_err) { |
1979 | error_propagate(errp, local_err); | |
1980 | return; | |
1981 | } | |
1982 | ||
1983 | prop->set(obj, value, errp); | |
1984 | g_free(value); | |
1985 | } | |
1986 | ||
7b7b7d18 PB |
1987 | static void property_release_str(Object *obj, const char *name, |
1988 | void *opaque) | |
57c9fafe AL |
1989 | { |
1990 | StringProperty *prop = opaque; | |
1991 | g_free(prop); | |
1992 | } | |
1993 | ||
1994 | void object_property_add_str(Object *obj, const char *name, | |
1995 | char *(*get)(Object *, Error **), | |
1996 | void (*set)(Object *, const char *, Error **), | |
1997 | Error **errp) | |
1998 | { | |
a01aedc8 | 1999 | Error *local_err = NULL; |
57c9fafe AL |
2000 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
2001 | ||
2002 | prop->get = get; | |
2003 | prop->set = set; | |
2004 | ||
2005 | object_property_add(obj, name, "string", | |
7b7b7d18 PB |
2006 | get ? property_get_str : NULL, |
2007 | set ? property_set_str : NULL, | |
2008 | property_release_str, | |
a01aedc8 SH |
2009 | prop, &local_err); |
2010 | if (local_err) { | |
2011 | error_propagate(errp, local_err); | |
2012 | g_free(prop); | |
2013 | } | |
57c9fafe | 2014 | } |
745549c8 | 2015 | |
16bf7f52 DB |
2016 | void object_class_property_add_str(ObjectClass *klass, const char *name, |
2017 | char *(*get)(Object *, Error **), | |
2018 | void (*set)(Object *, const char *, | |
2019 | Error **), | |
2020 | Error **errp) | |
2021 | { | |
2022 | Error *local_err = NULL; | |
2023 | StringProperty *prop = g_malloc0(sizeof(*prop)); | |
2024 | ||
2025 | prop->get = get; | |
2026 | prop->set = set; | |
2027 | ||
2028 | object_class_property_add(klass, name, "string", | |
2029 | get ? property_get_str : NULL, | |
2030 | set ? property_set_str : NULL, | |
2031 | property_release_str, | |
2032 | prop, &local_err); | |
2033 | if (local_err) { | |
2034 | error_propagate(errp, local_err); | |
2035 | g_free(prop); | |
2036 | } | |
2037 | } | |
2038 | ||
0e558843 AL |
2039 | typedef struct BoolProperty |
2040 | { | |
2041 | bool (*get)(Object *, Error **); | |
2042 | void (*set)(Object *, bool, Error **); | |
2043 | } BoolProperty; | |
2044 | ||
d7bce999 EB |
2045 | static void property_get_bool(Object *obj, Visitor *v, const char *name, |
2046 | void *opaque, Error **errp) | |
0e558843 AL |
2047 | { |
2048 | BoolProperty *prop = opaque; | |
2049 | bool value; | |
4715d42e MA |
2050 | Error *err = NULL; |
2051 | ||
2052 | value = prop->get(obj, &err); | |
2053 | if (err) { | |
2054 | error_propagate(errp, err); | |
2055 | return; | |
2056 | } | |
0e558843 | 2057 | |
51e72bc1 | 2058 | visit_type_bool(v, name, &value, errp); |
0e558843 AL |
2059 | } |
2060 | ||
d7bce999 EB |
2061 | static void property_set_bool(Object *obj, Visitor *v, const char *name, |
2062 | void *opaque, Error **errp) | |
0e558843 AL |
2063 | { |
2064 | BoolProperty *prop = opaque; | |
2065 | bool value; | |
2066 | Error *local_err = NULL; | |
2067 | ||
51e72bc1 | 2068 | visit_type_bool(v, name, &value, &local_err); |
0e558843 AL |
2069 | if (local_err) { |
2070 | error_propagate(errp, local_err); | |
2071 | return; | |
2072 | } | |
2073 | ||
2074 | prop->set(obj, value, errp); | |
2075 | } | |
2076 | ||
2077 | static void property_release_bool(Object *obj, const char *name, | |
2078 | void *opaque) | |
2079 | { | |
2080 | BoolProperty *prop = opaque; | |
2081 | g_free(prop); | |
2082 | } | |
2083 | ||
2084 | void object_property_add_bool(Object *obj, const char *name, | |
2085 | bool (*get)(Object *, Error **), | |
2086 | void (*set)(Object *, bool, Error **), | |
2087 | Error **errp) | |
2088 | { | |
a01aedc8 | 2089 | Error *local_err = NULL; |
0e558843 AL |
2090 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
2091 | ||
2092 | prop->get = get; | |
2093 | prop->set = set; | |
2094 | ||
2095 | object_property_add(obj, name, "bool", | |
2096 | get ? property_get_bool : NULL, | |
2097 | set ? property_set_bool : NULL, | |
2098 | property_release_bool, | |
a01aedc8 SH |
2099 | prop, &local_err); |
2100 | if (local_err) { | |
2101 | error_propagate(errp, local_err); | |
2102 | g_free(prop); | |
2103 | } | |
0e558843 AL |
2104 | } |
2105 | ||
16bf7f52 DB |
2106 | void object_class_property_add_bool(ObjectClass *klass, const char *name, |
2107 | bool (*get)(Object *, Error **), | |
2108 | void (*set)(Object *, bool, Error **), | |
2109 | Error **errp) | |
2110 | { | |
2111 | Error *local_err = NULL; | |
2112 | BoolProperty *prop = g_malloc0(sizeof(*prop)); | |
2113 | ||
2114 | prop->get = get; | |
2115 | prop->set = set; | |
2116 | ||
2117 | object_class_property_add(klass, name, "bool", | |
2118 | get ? property_get_bool : NULL, | |
2119 | set ? property_set_bool : NULL, | |
2120 | property_release_bool, | |
2121 | prop, &local_err); | |
2122 | if (local_err) { | |
2123 | error_propagate(errp, local_err); | |
2124 | g_free(prop); | |
2125 | } | |
2126 | } | |
2127 | ||
d7bce999 EB |
2128 | static void property_get_enum(Object *obj, Visitor *v, const char *name, |
2129 | void *opaque, Error **errp) | |
a8e3fbed DB |
2130 | { |
2131 | EnumProperty *prop = opaque; | |
2132 | int value; | |
4715d42e MA |
2133 | Error *err = NULL; |
2134 | ||
2135 | value = prop->get(obj, &err); | |
2136 | if (err) { | |
2137 | error_propagate(errp, err); | |
2138 | return; | |
2139 | } | |
a8e3fbed | 2140 | |
f7abe0ec | 2141 | visit_type_enum(v, name, &value, prop->lookup, errp); |
a8e3fbed DB |
2142 | } |
2143 | ||
d7bce999 EB |
2144 | static void property_set_enum(Object *obj, Visitor *v, const char *name, |
2145 | void *opaque, Error **errp) | |
a8e3fbed DB |
2146 | { |
2147 | EnumProperty *prop = opaque; | |
2148 | int value; | |
4715d42e | 2149 | Error *err = NULL; |
a8e3fbed | 2150 | |
f7abe0ec | 2151 | visit_type_enum(v, name, &value, prop->lookup, &err); |
4715d42e MA |
2152 | if (err) { |
2153 | error_propagate(errp, err); | |
2154 | return; | |
2155 | } | |
a8e3fbed DB |
2156 | prop->set(obj, value, errp); |
2157 | } | |
2158 | ||
2159 | static void property_release_enum(Object *obj, const char *name, | |
2160 | void *opaque) | |
2161 | { | |
2162 | EnumProperty *prop = opaque; | |
2163 | g_free(prop); | |
2164 | } | |
2165 | ||
2166 | void object_property_add_enum(Object *obj, const char *name, | |
2167 | const char *typename, | |
f7abe0ec | 2168 | const QEnumLookup *lookup, |
a8e3fbed DB |
2169 | int (*get)(Object *, Error **), |
2170 | void (*set)(Object *, int, Error **), | |
2171 | Error **errp) | |
2172 | { | |
2173 | Error *local_err = NULL; | |
2174 | EnumProperty *prop = g_malloc(sizeof(*prop)); | |
2175 | ||
f7abe0ec | 2176 | prop->lookup = lookup; |
a8e3fbed DB |
2177 | prop->get = get; |
2178 | prop->set = set; | |
2179 | ||
2180 | object_property_add(obj, name, typename, | |
2181 | get ? property_get_enum : NULL, | |
2182 | set ? property_set_enum : NULL, | |
2183 | property_release_enum, | |
2184 | prop, &local_err); | |
2185 | if (local_err) { | |
2186 | error_propagate(errp, local_err); | |
2187 | g_free(prop); | |
2188 | } | |
2189 | } | |
2190 | ||
16bf7f52 DB |
2191 | void object_class_property_add_enum(ObjectClass *klass, const char *name, |
2192 | const char *typename, | |
f7abe0ec | 2193 | const QEnumLookup *lookup, |
16bf7f52 DB |
2194 | int (*get)(Object *, Error **), |
2195 | void (*set)(Object *, int, Error **), | |
2196 | Error **errp) | |
2197 | { | |
2198 | Error *local_err = NULL; | |
2199 | EnumProperty *prop = g_malloc(sizeof(*prop)); | |
2200 | ||
f7abe0ec | 2201 | prop->lookup = lookup; |
16bf7f52 DB |
2202 | prop->get = get; |
2203 | prop->set = set; | |
2204 | ||
2205 | object_class_property_add(klass, name, typename, | |
2206 | get ? property_get_enum : NULL, | |
2207 | set ? property_set_enum : NULL, | |
2208 | property_release_enum, | |
2209 | prop, &local_err); | |
2210 | if (local_err) { | |
2211 | error_propagate(errp, local_err); | |
2212 | g_free(prop); | |
2213 | } | |
2214 | } | |
2215 | ||
8e099d14 DG |
2216 | typedef struct TMProperty { |
2217 | void (*get)(Object *, struct tm *, Error **); | |
2218 | } TMProperty; | |
2219 | ||
d7bce999 EB |
2220 | static void property_get_tm(Object *obj, Visitor *v, const char *name, |
2221 | void *opaque, Error **errp) | |
8e099d14 DG |
2222 | { |
2223 | TMProperty *prop = opaque; | |
2224 | Error *err = NULL; | |
2225 | struct tm value; | |
2226 | ||
2227 | prop->get(obj, &value, &err); | |
2228 | if (err) { | |
2229 | goto out; | |
2230 | } | |
2231 | ||
337283df | 2232 | visit_start_struct(v, name, NULL, 0, &err); |
8e099d14 DG |
2233 | if (err) { |
2234 | goto out; | |
2235 | } | |
51e72bc1 | 2236 | visit_type_int32(v, "tm_year", &value.tm_year, &err); |
8e099d14 DG |
2237 | if (err) { |
2238 | goto out_end; | |
2239 | } | |
51e72bc1 | 2240 | visit_type_int32(v, "tm_mon", &value.tm_mon, &err); |
8e099d14 DG |
2241 | if (err) { |
2242 | goto out_end; | |
2243 | } | |
51e72bc1 | 2244 | visit_type_int32(v, "tm_mday", &value.tm_mday, &err); |
8e099d14 DG |
2245 | if (err) { |
2246 | goto out_end; | |
2247 | } | |
51e72bc1 | 2248 | visit_type_int32(v, "tm_hour", &value.tm_hour, &err); |
8e099d14 DG |
2249 | if (err) { |
2250 | goto out_end; | |
2251 | } | |
51e72bc1 | 2252 | visit_type_int32(v, "tm_min", &value.tm_min, &err); |
8e099d14 DG |
2253 | if (err) { |
2254 | goto out_end; | |
2255 | } | |
51e72bc1 | 2256 | visit_type_int32(v, "tm_sec", &value.tm_sec, &err); |
8e099d14 DG |
2257 | if (err) { |
2258 | goto out_end; | |
2259 | } | |
15c2f669 | 2260 | visit_check_struct(v, &err); |
8e099d14 | 2261 | out_end: |
1158bb2a | 2262 | visit_end_struct(v, NULL); |
8e099d14 DG |
2263 | out: |
2264 | error_propagate(errp, err); | |
2265 | ||
2266 | } | |
2267 | ||
2268 | static void property_release_tm(Object *obj, const char *name, | |
2269 | void *opaque) | |
2270 | { | |
2271 | TMProperty *prop = opaque; | |
2272 | g_free(prop); | |
2273 | } | |
2274 | ||
2275 | void object_property_add_tm(Object *obj, const char *name, | |
2276 | void (*get)(Object *, struct tm *, Error **), | |
2277 | Error **errp) | |
2278 | { | |
2279 | Error *local_err = NULL; | |
2280 | TMProperty *prop = g_malloc0(sizeof(*prop)); | |
2281 | ||
2282 | prop->get = get; | |
2283 | ||
2284 | object_property_add(obj, name, "struct tm", | |
2285 | get ? property_get_tm : NULL, NULL, | |
2286 | property_release_tm, | |
2287 | prop, &local_err); | |
2288 | if (local_err) { | |
2289 | error_propagate(errp, local_err); | |
2290 | g_free(prop); | |
2291 | } | |
2292 | } | |
2293 | ||
16bf7f52 DB |
2294 | void object_class_property_add_tm(ObjectClass *klass, const char *name, |
2295 | void (*get)(Object *, struct tm *, Error **), | |
2296 | Error **errp) | |
2297 | { | |
2298 | Error *local_err = NULL; | |
2299 | TMProperty *prop = g_malloc0(sizeof(*prop)); | |
2300 | ||
2301 | prop->get = get; | |
2302 | ||
2303 | object_class_property_add(klass, name, "struct tm", | |
2304 | get ? property_get_tm : NULL, NULL, | |
2305 | property_release_tm, | |
2306 | prop, &local_err); | |
2307 | if (local_err) { | |
2308 | error_propagate(errp, local_err); | |
2309 | g_free(prop); | |
2310 | } | |
2311 | } | |
2312 | ||
2f262e06 PB |
2313 | static char *qdev_get_type(Object *obj, Error **errp) |
2314 | { | |
2315 | return g_strdup(object_get_typename(obj)); | |
2316 | } | |
2317 | ||
d7bce999 EB |
2318 | static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name, |
2319 | void *opaque, Error **errp) | |
e732ea63 MT |
2320 | { |
2321 | uint8_t value = *(uint8_t *)opaque; | |
51e72bc1 | 2322 | visit_type_uint8(v, name, &value, errp); |
e732ea63 MT |
2323 | } |
2324 | ||
d7bce999 EB |
2325 | static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name, |
2326 | void *opaque, Error **errp) | |
e732ea63 MT |
2327 | { |
2328 | uint16_t value = *(uint16_t *)opaque; | |
51e72bc1 | 2329 | visit_type_uint16(v, name, &value, errp); |
e732ea63 MT |
2330 | } |
2331 | ||
d7bce999 EB |
2332 | static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name, |
2333 | void *opaque, Error **errp) | |
e732ea63 MT |
2334 | { |
2335 | uint32_t value = *(uint32_t *)opaque; | |
51e72bc1 | 2336 | visit_type_uint32(v, name, &value, errp); |
e732ea63 MT |
2337 | } |
2338 | ||
d7bce999 EB |
2339 | static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name, |
2340 | void *opaque, Error **errp) | |
e732ea63 MT |
2341 | { |
2342 | uint64_t value = *(uint64_t *)opaque; | |
51e72bc1 | 2343 | visit_type_uint64(v, name, &value, errp); |
e732ea63 MT |
2344 | } |
2345 | ||
2346 | void object_property_add_uint8_ptr(Object *obj, const char *name, | |
2347 | const uint8_t *v, Error **errp) | |
2348 | { | |
2349 | object_property_add(obj, name, "uint8", property_get_uint8_ptr, | |
2350 | NULL, NULL, (void *)v, errp); | |
2351 | } | |
2352 | ||
16bf7f52 DB |
2353 | void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name, |
2354 | const uint8_t *v, Error **errp) | |
2355 | { | |
2356 | object_class_property_add(klass, name, "uint8", property_get_uint8_ptr, | |
2357 | NULL, NULL, (void *)v, errp); | |
2358 | } | |
2359 | ||
e732ea63 MT |
2360 | void object_property_add_uint16_ptr(Object *obj, const char *name, |
2361 | const uint16_t *v, Error **errp) | |
2362 | { | |
2363 | object_property_add(obj, name, "uint16", property_get_uint16_ptr, | |
2364 | NULL, NULL, (void *)v, errp); | |
2365 | } | |
2366 | ||
16bf7f52 DB |
2367 | void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name, |
2368 | const uint16_t *v, Error **errp) | |
2369 | { | |
2370 | object_class_property_add(klass, name, "uint16", property_get_uint16_ptr, | |
2371 | NULL, NULL, (void *)v, errp); | |
2372 | } | |
2373 | ||
e732ea63 MT |
2374 | void object_property_add_uint32_ptr(Object *obj, const char *name, |
2375 | const uint32_t *v, Error **errp) | |
2376 | { | |
2377 | object_property_add(obj, name, "uint32", property_get_uint32_ptr, | |
2378 | NULL, NULL, (void *)v, errp); | |
2379 | } | |
2380 | ||
16bf7f52 DB |
2381 | void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name, |
2382 | const uint32_t *v, Error **errp) | |
2383 | { | |
2384 | object_class_property_add(klass, name, "uint32", property_get_uint32_ptr, | |
2385 | NULL, NULL, (void *)v, errp); | |
2386 | } | |
2387 | ||
e732ea63 MT |
2388 | void object_property_add_uint64_ptr(Object *obj, const char *name, |
2389 | const uint64_t *v, Error **errp) | |
2390 | { | |
2391 | object_property_add(obj, name, "uint64", property_get_uint64_ptr, | |
2392 | NULL, NULL, (void *)v, errp); | |
2393 | } | |
2394 | ||
16bf7f52 DB |
2395 | void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name, |
2396 | const uint64_t *v, Error **errp) | |
2397 | { | |
2398 | object_class_property_add(klass, name, "uint64", property_get_uint64_ptr, | |
2399 | NULL, NULL, (void *)v, errp); | |
2400 | } | |
2401 | ||
ef7c7ff6 SH |
2402 | typedef struct { |
2403 | Object *target_obj; | |
1590d266 | 2404 | char *target_name; |
ef7c7ff6 SH |
2405 | } AliasProperty; |
2406 | ||
d7bce999 EB |
2407 | static void property_get_alias(Object *obj, Visitor *v, const char *name, |
2408 | void *opaque, Error **errp) | |
ef7c7ff6 SH |
2409 | { |
2410 | AliasProperty *prop = opaque; | |
2411 | ||
2412 | object_property_get(prop->target_obj, v, prop->target_name, errp); | |
2413 | } | |
2414 | ||
d7bce999 EB |
2415 | static void property_set_alias(Object *obj, Visitor *v, const char *name, |
2416 | void *opaque, Error **errp) | |
ef7c7ff6 SH |
2417 | { |
2418 | AliasProperty *prop = opaque; | |
2419 | ||
2420 | object_property_set(prop->target_obj, v, prop->target_name, errp); | |
2421 | } | |
2422 | ||
64607d08 PB |
2423 | static Object *property_resolve_alias(Object *obj, void *opaque, |
2424 | const gchar *part) | |
2425 | { | |
2426 | AliasProperty *prop = opaque; | |
2427 | ||
2428 | return object_resolve_path_component(prop->target_obj, prop->target_name); | |
2429 | } | |
2430 | ||
ef7c7ff6 SH |
2431 | static void property_release_alias(Object *obj, const char *name, void *opaque) |
2432 | { | |
2433 | AliasProperty *prop = opaque; | |
2434 | ||
1590d266 | 2435 | g_free(prop->target_name); |
ef7c7ff6 SH |
2436 | g_free(prop); |
2437 | } | |
2438 | ||
2439 | void object_property_add_alias(Object *obj, const char *name, | |
2440 | Object *target_obj, const char *target_name, | |
2441 | Error **errp) | |
2442 | { | |
2443 | AliasProperty *prop; | |
64607d08 | 2444 | ObjectProperty *op; |
ef7c7ff6 | 2445 | ObjectProperty *target_prop; |
d190698e | 2446 | gchar *prop_type; |
8ae9a9ef | 2447 | Error *local_err = NULL; |
ef7c7ff6 SH |
2448 | |
2449 | target_prop = object_property_find(target_obj, target_name, errp); | |
2450 | if (!target_prop) { | |
2451 | return; | |
2452 | } | |
2453 | ||
d190698e PB |
2454 | if (object_property_is_child(target_prop)) { |
2455 | prop_type = g_strdup_printf("link%s", | |
2456 | target_prop->type + strlen("child")); | |
2457 | } else { | |
2458 | prop_type = g_strdup(target_prop->type); | |
2459 | } | |
2460 | ||
ef7c7ff6 SH |
2461 | prop = g_malloc(sizeof(*prop)); |
2462 | prop->target_obj = target_obj; | |
1590d266 | 2463 | prop->target_name = g_strdup(target_name); |
ef7c7ff6 | 2464 | |
d190698e | 2465 | op = object_property_add(obj, name, prop_type, |
64607d08 PB |
2466 | property_get_alias, |
2467 | property_set_alias, | |
2468 | property_release_alias, | |
8ae9a9ef GA |
2469 | prop, &local_err); |
2470 | if (local_err) { | |
2471 | error_propagate(errp, local_err); | |
2472 | g_free(prop); | |
2473 | goto out; | |
2474 | } | |
64607d08 | 2475 | op->resolve = property_resolve_alias; |
d190698e | 2476 | |
a18bb417 | 2477 | object_property_set_description(obj, op->name, |
80742642 GA |
2478 | target_prop->description, |
2479 | &error_abort); | |
2480 | ||
8ae9a9ef | 2481 | out: |
d190698e | 2482 | g_free(prop_type); |
ef7c7ff6 SH |
2483 | } |
2484 | ||
80742642 GA |
2485 | void object_property_set_description(Object *obj, const char *name, |
2486 | const char *description, Error **errp) | |
2487 | { | |
2488 | ObjectProperty *op; | |
2489 | ||
2490 | op = object_property_find(obj, name, errp); | |
2491 | if (!op) { | |
2492 | return; | |
2493 | } | |
2494 | ||
2495 | g_free(op->description); | |
2496 | op->description = g_strdup(description); | |
2497 | } | |
2498 | ||
16bf7f52 DB |
2499 | void object_class_property_set_description(ObjectClass *klass, |
2500 | const char *name, | |
2501 | const char *description, | |
2502 | Error **errp) | |
2503 | { | |
2504 | ObjectProperty *op; | |
2505 | ||
2506 | op = g_hash_table_lookup(klass->properties, name); | |
2507 | if (!op) { | |
2508 | error_setg(errp, "Property '.%s' not found", name); | |
2509 | return; | |
2510 | } | |
2511 | ||
2512 | g_free(op->description); | |
2513 | op->description = g_strdup(description); | |
2514 | } | |
2515 | ||
7439a036 | 2516 | static void object_class_init(ObjectClass *klass, void *data) |
2f262e06 | 2517 | { |
7439a036 MAL |
2518 | object_class_property_add_str(klass, "type", qdev_get_type, |
2519 | NULL, &error_abort); | |
2f262e06 PB |
2520 | } |
2521 | ||
745549c8 PB |
2522 | static void register_types(void) |
2523 | { | |
2524 | static TypeInfo interface_info = { | |
2525 | .name = TYPE_INTERFACE, | |
33e95c63 | 2526 | .class_size = sizeof(InterfaceClass), |
745549c8 PB |
2527 | .abstract = true, |
2528 | }; | |
2529 | ||
2530 | static TypeInfo object_info = { | |
2531 | .name = TYPE_OBJECT, | |
2532 | .instance_size = sizeof(Object), | |
7439a036 | 2533 | .class_init = object_class_init, |
745549c8 PB |
2534 | .abstract = true, |
2535 | }; | |
2536 | ||
049cb3cf PB |
2537 | type_interface = type_register_internal(&interface_info); |
2538 | type_register_internal(&object_info); | |
745549c8 PB |
2539 | } |
2540 | ||
2541 | type_init(register_types) |