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