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