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