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