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 */ 13 14 #ifndef QEMU_OBJECT_H 15 #define QEMU_OBJECT_H 16 17 #include "qapi/qapi-builtin-types.h" 18 #include "qemu/module.h" 19 20 struct TypeImpl; 21 typedef struct TypeImpl *Type; 22 23 typedef struct TypeInfo TypeInfo; 24 25 typedef struct InterfaceClass InterfaceClass; 26 typedef struct InterfaceInfo InterfaceInfo; 27 28 #define TYPE_OBJECT "object" 29 30 typedef struct ObjectProperty ObjectProperty; 31 32 /** 33 * typedef ObjectPropertyAccessor: 34 * @obj: the object that owns the property 35 * @v: the visitor that contains the property data 36 * @name: the name of the property 37 * @opaque: the object property opaque 38 * @errp: a pointer to an Error that is filled if getting/setting fails. 39 * 40 * Called when trying to get/set a property. 41 */ 42 typedef void (ObjectPropertyAccessor)(Object *obj, 43 Visitor *v, 44 const char *name, 45 void *opaque, 46 Error **errp); 47 48 /** 49 * typedef ObjectPropertyResolve: 50 * @obj: the object that owns the property 51 * @opaque: the opaque registered with the property 52 * @part: the name of the property 53 * 54 * Resolves the #Object corresponding to property @part. 55 * 56 * The returned object can also be used as a starting point 57 * to resolve a relative path starting with "@part". 58 * 59 * Returns: If @path is the path that led to @obj, the function 60 * returns the #Object corresponding to "@path/@part". 61 * If "@path/@part" is not a valid object path, it returns #NULL. 62 */ 63 typedef Object *(ObjectPropertyResolve)(Object *obj, 64 void *opaque, 65 const char *part); 66 67 /** 68 * typedef ObjectPropertyRelease: 69 * @obj: the object that owns the property 70 * @name: the name of the property 71 * @opaque: the opaque registered with the property 72 * 73 * Called when a property is removed from a object. 74 */ 75 typedef void (ObjectPropertyRelease)(Object *obj, 76 const char *name, 77 void *opaque); 78 79 /** 80 * typedef ObjectPropertyInit: 81 * @obj: the object that owns the property 82 * @prop: the property to set 83 * 84 * Called when a property is initialized. 85 */ 86 typedef void (ObjectPropertyInit)(Object *obj, ObjectProperty *prop); 87 88 struct ObjectProperty 89 { 90 char *name; 91 char *type; 92 char *description; 93 ObjectPropertyAccessor *get; 94 ObjectPropertyAccessor *set; 95 ObjectPropertyResolve *resolve; 96 ObjectPropertyRelease *release; 97 ObjectPropertyInit *init; 98 void *opaque; 99 QObject *defval; 100 }; 101 102 /** 103 * typedef ObjectUnparent: 104 * @obj: the object that is being removed from the composition tree 105 * 106 * Called when an object is being removed from the QOM composition tree. 107 * The function should remove any backlinks from children objects to @obj. 108 */ 109 typedef void (ObjectUnparent)(Object *obj); 110 111 /** 112 * typedef ObjectFree: 113 * @obj: the object being freed 114 * 115 * Called when an object's last reference is removed. 116 */ 117 typedef void (ObjectFree)(void *obj); 118 119 #define OBJECT_CLASS_CAST_CACHE 4 120 121 /** 122 * struct ObjectClass: 123 * 124 * The base for all classes. The only thing that #ObjectClass contains is an 125 * integer type handle. 126 */ 127 struct ObjectClass 128 { 129 /* private: */ 130 Type type; 131 GSList *interfaces; 132 133 const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE]; 134 const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE]; 135 136 ObjectUnparent *unparent; 137 138 GHashTable *properties; 139 }; 140 141 /** 142 * struct Object: 143 * 144 * The base for all objects. The first member of this object is a pointer to 145 * a #ObjectClass. Since C guarantees that the first member of a structure 146 * always begins at byte 0 of that structure, as long as any sub-object places 147 * its parent as the first member, we can cast directly to a #Object. 148 * 149 * As a result, #Object contains a reference to the objects type as its 150 * first member. This allows identification of the real type of the object at 151 * run time. 152 */ 153 struct Object 154 { 155 /* private: */ 156 ObjectClass *class; 157 ObjectFree *free; 158 GHashTable *properties; 159 uint32_t ref; 160 Object *parent; 161 }; 162 163 /** 164 * DECLARE_INSTANCE_CHECKER: 165 * @InstanceType: instance struct name 166 * @OBJ_NAME: the object name in uppercase with underscore separators 167 * @TYPENAME: type name 168 * 169 * Direct usage of this macro should be avoided, and the complete 170 * OBJECT_DECLARE_TYPE macro is recommended instead. 171 * 172 * This macro will provide the instance type cast functions for a 173 * QOM type. 174 */ 175 #define DECLARE_INSTANCE_CHECKER(InstanceType, OBJ_NAME, TYPENAME) \ 176 static inline G_GNUC_UNUSED InstanceType * \ 177 OBJ_NAME(const void *obj) \ 178 { return OBJECT_CHECK(InstanceType, obj, TYPENAME); } 179 180 /** 181 * DECLARE_CLASS_CHECKERS: 182 * @ClassType: class struct name 183 * @OBJ_NAME: the object name in uppercase with underscore separators 184 * @TYPENAME: type name 185 * 186 * Direct usage of this macro should be avoided, and the complete 187 * OBJECT_DECLARE_TYPE macro is recommended instead. 188 * 189 * This macro will provide the class type cast functions for a 190 * QOM type. 191 */ 192 #define DECLARE_CLASS_CHECKERS(ClassType, OBJ_NAME, TYPENAME) \ 193 static inline G_GNUC_UNUSED ClassType * \ 194 OBJ_NAME##_GET_CLASS(const void *obj) \ 195 { return OBJECT_GET_CLASS(ClassType, obj, TYPENAME); } \ 196 \ 197 static inline G_GNUC_UNUSED ClassType * \ 198 OBJ_NAME##_CLASS(const void *klass) \ 199 { return OBJECT_CLASS_CHECK(ClassType, klass, TYPENAME); } 200 201 /** 202 * DECLARE_OBJ_CHECKERS: 203 * @InstanceType: instance struct name 204 * @ClassType: class struct name 205 * @OBJ_NAME: the object name in uppercase with underscore separators 206 * @TYPENAME: type name 207 * 208 * Direct usage of this macro should be avoided, and the complete 209 * OBJECT_DECLARE_TYPE macro is recommended instead. 210 * 211 * This macro will provide the three standard type cast functions for a 212 * QOM type. 213 */ 214 #define DECLARE_OBJ_CHECKERS(InstanceType, ClassType, OBJ_NAME, TYPENAME) \ 215 DECLARE_INSTANCE_CHECKER(InstanceType, OBJ_NAME, TYPENAME) \ 216 \ 217 DECLARE_CLASS_CHECKERS(ClassType, OBJ_NAME, TYPENAME) 218 219 /** 220 * OBJECT_DECLARE_TYPE: 221 * @InstanceType: instance struct name 222 * @ClassType: class struct name 223 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 224 * 225 * This macro is typically used in a header file, and will: 226 * 227 * - create the typedefs for the object and class structs 228 * - register the type for use with g_autoptr 229 * - provide three standard type cast functions 230 * 231 * The object struct and class struct need to be declared manually. 232 */ 233 #define OBJECT_DECLARE_TYPE(InstanceType, ClassType, MODULE_OBJ_NAME) \ 234 typedef struct InstanceType InstanceType; \ 235 typedef struct ClassType ClassType; \ 236 \ 237 G_DEFINE_AUTOPTR_CLEANUP_FUNC(InstanceType, object_unref) \ 238 \ 239 DECLARE_OBJ_CHECKERS(InstanceType, ClassType, \ 240 MODULE_OBJ_NAME, TYPE_##MODULE_OBJ_NAME) 241 242 /** 243 * OBJECT_DECLARE_SIMPLE_TYPE: 244 * @InstanceType: instance struct name 245 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 246 * 247 * This does the same as OBJECT_DECLARE_TYPE(), but with no class struct 248 * declared. 249 * 250 * This macro should be used unless the class struct needs to have 251 * virtual methods declared. 252 */ 253 #define OBJECT_DECLARE_SIMPLE_TYPE(InstanceType, MODULE_OBJ_NAME) \ 254 typedef struct InstanceType InstanceType; \ 255 \ 256 G_DEFINE_AUTOPTR_CLEANUP_FUNC(InstanceType, object_unref) \ 257 \ 258 DECLARE_INSTANCE_CHECKER(InstanceType, MODULE_OBJ_NAME, TYPE_##MODULE_OBJ_NAME) 259 260 261 /** 262 * DO_OBJECT_DEFINE_TYPE_EXTENDED: 263 * @ModuleObjName: the object name with initial caps 264 * @module_obj_name: the object name in lowercase with underscore separators 265 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 266 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 267 * separators 268 * @ABSTRACT: boolean flag to indicate whether the object can be instantiated 269 * @CLASS_SIZE: size of the type's class 270 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces 271 * 272 * This is the base macro used to implement all the OBJECT_DEFINE_* 273 * macros. It should never be used directly in a source file. 274 */ 275 #define DO_OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 276 MODULE_OBJ_NAME, \ 277 PARENT_MODULE_OBJ_NAME, \ 278 ABSTRACT, CLASS_SIZE, ...) \ 279 static void \ 280 module_obj_name##_finalize(Object *obj); \ 281 static void \ 282 module_obj_name##_class_init(ObjectClass *oc, void *data); \ 283 static void \ 284 module_obj_name##_init(Object *obj); \ 285 \ 286 static const TypeInfo module_obj_name##_info = { \ 287 .parent = TYPE_##PARENT_MODULE_OBJ_NAME, \ 288 .name = TYPE_##MODULE_OBJ_NAME, \ 289 .instance_size = sizeof(ModuleObjName), \ 290 .instance_align = __alignof__(ModuleObjName), \ 291 .instance_init = module_obj_name##_init, \ 292 .instance_finalize = module_obj_name##_finalize, \ 293 .class_size = CLASS_SIZE, \ 294 .class_init = module_obj_name##_class_init, \ 295 .abstract = ABSTRACT, \ 296 .interfaces = (InterfaceInfo[]) { __VA_ARGS__ } , \ 297 }; \ 298 \ 299 static void \ 300 module_obj_name##_register_types(void) \ 301 { \ 302 type_register_static(&module_obj_name##_info); \ 303 } \ 304 type_init(module_obj_name##_register_types); 305 306 /** 307 * OBJECT_DEFINE_TYPE_EXTENDED: 308 * @ModuleObjName: the object name with initial caps 309 * @module_obj_name: the object name in lowercase with underscore separators 310 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 311 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 312 * separators 313 * @ABSTRACT: boolean flag to indicate whether the object can be instantiated 314 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces 315 * 316 * This macro is typically used in a source file, and will: 317 * 318 * - declare prototypes for _finalize, _class_init and _init methods 319 * - declare the TypeInfo struct instance 320 * - provide the constructor to register the type 321 * 322 * After using this macro, implementations of the _finalize, _class_init, 323 * and _init methods need to be written. Any of these can be zero-line 324 * no-op impls if no special logic is required for a given type. 325 * 326 * This macro should rarely be used, instead one of the more specialized 327 * macros is usually a better choice. 328 */ 329 #define OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 330 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 331 ABSTRACT, ...) \ 332 DO_OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 333 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 334 ABSTRACT, sizeof(ModuleObjName##Class), \ 335 __VA_ARGS__) 336 337 /** 338 * OBJECT_DEFINE_TYPE: 339 * @ModuleObjName: the object name with initial caps 340 * @module_obj_name: the object name in lowercase with underscore separators 341 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 342 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 343 * separators 344 * 345 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable 346 * for the common case of a non-abstract type, without any interfaces. 347 */ 348 #define OBJECT_DEFINE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME, \ 349 PARENT_MODULE_OBJ_NAME) \ 350 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 351 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 352 false, { NULL }) 353 354 /** 355 * OBJECT_DEFINE_TYPE_WITH_INTERFACES: 356 * @ModuleObjName: the object name with initial caps 357 * @module_obj_name: the object name in lowercase with underscore separators 358 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 359 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 360 * separators 361 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces 362 * 363 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable 364 * for the common case of a non-abstract type, with one or more implemented 365 * interfaces. 366 * 367 * Note when passing the list of interfaces, be sure to include the final 368 * NULL entry, e.g. { TYPE_USER_CREATABLE }, { NULL } 369 */ 370 #define OBJECT_DEFINE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \ 371 MODULE_OBJ_NAME, \ 372 PARENT_MODULE_OBJ_NAME, ...) \ 373 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 374 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 375 false, __VA_ARGS__) 376 377 /** 378 * OBJECT_DEFINE_ABSTRACT_TYPE: 379 * @ModuleObjName: the object name with initial caps 380 * @module_obj_name: the object name in lowercase with underscore separators 381 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 382 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 383 * separators 384 * 385 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable 386 * for defining an abstract type, without any interfaces. 387 */ 388 #define OBJECT_DEFINE_ABSTRACT_TYPE(ModuleObjName, module_obj_name, \ 389 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \ 390 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 391 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 392 true, { NULL }) 393 394 /** 395 * OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES: 396 * @ModuleObjName: the object name with initial caps 397 * @module_obj_name: the object name in lowercase with underscore separators 398 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 399 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 400 * separators 401 * 402 * This is a variant of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable for 403 * the case of a non-abstract type, with interfaces, and with no requirement 404 * for a class struct. 405 */ 406 #define OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(ModuleObjName, \ 407 module_obj_name, \ 408 MODULE_OBJ_NAME, \ 409 PARENT_MODULE_OBJ_NAME, ...) \ 410 DO_OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 411 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 412 false, 0, __VA_ARGS__) 413 414 /** 415 * OBJECT_DEFINE_SIMPLE_TYPE: 416 * @ModuleObjName: the object name with initial caps 417 * @module_obj_name: the object name in lowercase with underscore separators 418 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 419 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 420 * separators 421 * 422 * This is a variant of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable for 423 * the common case of a non-abstract type, without any interfaces, and with 424 * no requirement for a class struct. If you declared your type with 425 * OBJECT_DECLARE_SIMPLE_TYPE then this is probably the right choice for 426 * defining it. 427 */ 428 #define OBJECT_DEFINE_SIMPLE_TYPE(ModuleObjName, module_obj_name, \ 429 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \ 430 OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \ 431 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, { NULL }) 432 433 /** 434 * struct TypeInfo: 435 * @name: The name of the type. 436 * @parent: The name of the parent type. 437 * @instance_size: The size of the object (derivative of #Object). If 438 * @instance_size is 0, then the size of the object will be the size of the 439 * parent object. 440 * @instance_align: The required alignment of the object. If @instance_align 441 * is 0, then normal malloc alignment is sufficient; if non-zero, then we 442 * must use qemu_memalign for allocation. 443 * @instance_init: This function is called to initialize an object. The parent 444 * class will have already been initialized so the type is only responsible 445 * for initializing its own members. 446 * @instance_post_init: This function is called to finish initialization of 447 * an object, after all @instance_init functions were called. 448 * @instance_finalize: This function is called during object destruction. This 449 * is called before the parent @instance_finalize function has been called. 450 * An object should only free the members that are unique to its type in this 451 * function. 452 * @abstract: If this field is true, then the class is considered abstract and 453 * cannot be directly instantiated. 454 * @class_size: The size of the class object (derivative of #ObjectClass) 455 * for this object. If @class_size is 0, then the size of the class will be 456 * assumed to be the size of the parent class. This allows a type to avoid 457 * implementing an explicit class type if they are not adding additional 458 * virtual functions. 459 * @class_init: This function is called after all parent class initialization 460 * has occurred to allow a class to set its default virtual method pointers. 461 * This is also the function to use to override virtual methods from a parent 462 * class. 463 * @class_base_init: This function is called for all base classes after all 464 * parent class initialization has occurred, but before the class itself 465 * is initialized. This is the function to use to undo the effects of 466 * memcpy from the parent class to the descendants. 467 * @class_data: Data to pass to the @class_init, 468 * @class_base_init. This can be useful when building dynamic 469 * classes. 470 * @interfaces: The list of interfaces associated with this type. This 471 * should point to a static array that's terminated with a zero filled 472 * element. 473 */ 474 struct TypeInfo 475 { 476 const char *name; 477 const char *parent; 478 479 size_t instance_size; 480 size_t instance_align; 481 void (*instance_init)(Object *obj); 482 void (*instance_post_init)(Object *obj); 483 void (*instance_finalize)(Object *obj); 484 485 bool abstract; 486 size_t class_size; 487 488 void (*class_init)(ObjectClass *klass, void *data); 489 void (*class_base_init)(ObjectClass *klass, void *data); 490 void *class_data; 491 492 InterfaceInfo *interfaces; 493 }; 494 495 /** 496 * OBJECT: 497 * @obj: A derivative of #Object 498 * 499 * Converts an object to a #Object. Since all objects are #Objects, 500 * this function will always succeed. 501 */ 502 #define OBJECT(obj) \ 503 ((Object *)(obj)) 504 505 /** 506 * OBJECT_CLASS: 507 * @class: A derivative of #ObjectClass. 508 * 509 * Converts a class to an #ObjectClass. Since all objects are #Objects, 510 * this function will always succeed. 511 */ 512 #define OBJECT_CLASS(class) \ 513 ((ObjectClass *)(class)) 514 515 /** 516 * OBJECT_CHECK: 517 * @type: The C type to use for the return value. 518 * @obj: A derivative of @type to cast. 519 * @name: The QOM typename of @type 520 * 521 * A type safe version of @object_dynamic_cast_assert. Typically each class 522 * will define a macro based on this type to perform type safe dynamic_casts to 523 * this object type. 524 * 525 * If an invalid object is passed to this function, a run time assert will be 526 * generated. 527 */ 528 #define OBJECT_CHECK(type, obj, name) \ 529 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \ 530 __FILE__, __LINE__, __func__)) 531 532 /** 533 * OBJECT_CLASS_CHECK: 534 * @class_type: The C type to use for the return value. 535 * @class: A derivative class of @class_type to cast. 536 * @name: the QOM typename of @class_type. 537 * 538 * A type safe version of @object_class_dynamic_cast_assert. This macro is 539 * typically wrapped by each type to perform type safe casts of a class to a 540 * specific class type. 541 */ 542 #define OBJECT_CLASS_CHECK(class_type, class, name) \ 543 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \ 544 __FILE__, __LINE__, __func__)) 545 546 /** 547 * OBJECT_GET_CLASS: 548 * @class: The C type to use for the return value. 549 * @obj: The object to obtain the class for. 550 * @name: The QOM typename of @obj. 551 * 552 * This function will return a specific class for a given object. Its generally 553 * used by each type to provide a type safe macro to get a specific class type 554 * from an object. 555 */ 556 #define OBJECT_GET_CLASS(class, obj, name) \ 557 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name) 558 559 /** 560 * struct InterfaceInfo: 561 * @type: The name of the interface. 562 * 563 * The information associated with an interface. 564 */ 565 struct InterfaceInfo { 566 const char *type; 567 }; 568 569 /** 570 * struct InterfaceClass: 571 * @parent_class: the base class 572 * 573 * The class for all interfaces. Subclasses of this class should only add 574 * virtual methods. 575 */ 576 struct InterfaceClass 577 { 578 ObjectClass parent_class; 579 /* private: */ 580 ObjectClass *concrete_class; 581 Type interface_type; 582 }; 583 584 #define TYPE_INTERFACE "interface" 585 586 /** 587 * INTERFACE_CLASS: 588 * @klass: class to cast from 589 * Returns: An #InterfaceClass or raise an error if cast is invalid 590 */ 591 #define INTERFACE_CLASS(klass) \ 592 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE) 593 594 /** 595 * INTERFACE_CHECK: 596 * @interface: the type to return 597 * @obj: the object to convert to an interface 598 * @name: the interface type name 599 * 600 * Returns: @obj casted to @interface if cast is valid, otherwise raise error. 601 */ 602 #define INTERFACE_CHECK(interface, obj, name) \ 603 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \ 604 __FILE__, __LINE__, __func__)) 605 606 /** 607 * object_new_with_class: 608 * @klass: The class to instantiate. 609 * 610 * This function will initialize a new object using heap allocated memory. 611 * The returned object has a reference count of 1, and will be freed when 612 * the last reference is dropped. 613 * 614 * Returns: The newly allocated and instantiated object. 615 */ 616 Object *object_new_with_class(ObjectClass *klass); 617 618 /** 619 * object_new: 620 * @typename: The name of the type of the object to instantiate. 621 * 622 * This function will initialize a new object using heap allocated memory. 623 * The returned object has a reference count of 1, and will be freed when 624 * the last reference is dropped. 625 * 626 * Returns: The newly allocated and instantiated object. 627 */ 628 Object *object_new(const char *typename); 629 630 /** 631 * object_new_with_props: 632 * @typename: The name of the type of the object to instantiate. 633 * @parent: the parent object 634 * @id: The unique ID of the object 635 * @errp: pointer to error object 636 * @...: list of property names and values 637 * 638 * This function will initialize a new object using heap allocated memory. 639 * The returned object has a reference count of 1, and will be freed when 640 * the last reference is dropped. 641 * 642 * The @id parameter will be used when registering the object as a 643 * child of @parent in the composition tree. 644 * 645 * The variadic parameters are a list of pairs of (propname, propvalue) 646 * strings. The propname of %NULL indicates the end of the property 647 * list. If the object implements the user creatable interface, the 648 * object will be marked complete once all the properties have been 649 * processed. 650 * 651 * .. code-block:: c 652 * :caption: Creating an object with properties 653 * 654 * Error *err = NULL; 655 * Object *obj; 656 * 657 * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE, 658 * object_get_objects_root(), 659 * "hostmem0", 660 * &err, 661 * "share", "yes", 662 * "mem-path", "/dev/shm/somefile", 663 * "prealloc", "yes", 664 * "size", "1048576", 665 * NULL); 666 * 667 * if (!obj) { 668 * error_reportf_err(err, "Cannot create memory backend: "); 669 * } 670 * 671 * The returned object will have one stable reference maintained 672 * for as long as it is present in the object hierarchy. 673 * 674 * Returns: The newly allocated, instantiated & initialized object. 675 */ 676 Object *object_new_with_props(const char *typename, 677 Object *parent, 678 const char *id, 679 Error **errp, 680 ...) G_GNUC_NULL_TERMINATED; 681 682 /** 683 * object_new_with_propv: 684 * @typename: The name of the type of the object to instantiate. 685 * @parent: the parent object 686 * @id: The unique ID of the object 687 * @errp: pointer to error object 688 * @vargs: list of property names and values 689 * 690 * See object_new_with_props() for documentation. 691 */ 692 Object *object_new_with_propv(const char *typename, 693 Object *parent, 694 const char *id, 695 Error **errp, 696 va_list vargs); 697 698 bool object_apply_global_props(Object *obj, const GPtrArray *props, 699 Error **errp); 700 void object_set_machine_compat_props(GPtrArray *compat_props); 701 void object_set_accelerator_compat_props(GPtrArray *compat_props); 702 void object_register_sugar_prop(const char *driver, const char *prop, 703 const char *value, bool optional); 704 void object_apply_compat_props(Object *obj); 705 706 /** 707 * object_set_props: 708 * @obj: the object instance to set properties on 709 * @errp: pointer to error object 710 * @...: list of property names and values 711 * 712 * This function will set a list of properties on an existing object 713 * instance. 714 * 715 * The variadic parameters are a list of pairs of (propname, propvalue) 716 * strings. The propname of %NULL indicates the end of the property 717 * list. 718 * 719 * .. code-block:: c 720 * :caption: Update an object's properties 721 * 722 * Error *err = NULL; 723 * Object *obj = ...get / create object...; 724 * 725 * if (!object_set_props(obj, 726 * &err, 727 * "share", "yes", 728 * "mem-path", "/dev/shm/somefile", 729 * "prealloc", "yes", 730 * "size", "1048576", 731 * NULL)) { 732 * error_reportf_err(err, "Cannot set properties: "); 733 * } 734 * 735 * The returned object will have one stable reference maintained 736 * for as long as it is present in the object hierarchy. 737 * 738 * Returns: %true on success, %false on error. 739 */ 740 bool object_set_props(Object *obj, Error **errp, ...) G_GNUC_NULL_TERMINATED; 741 742 /** 743 * object_set_propv: 744 * @obj: the object instance to set properties on 745 * @errp: pointer to error object 746 * @vargs: list of property names and values 747 * 748 * See object_set_props() for documentation. 749 * 750 * Returns: %true on success, %false on error. 751 */ 752 bool object_set_propv(Object *obj, Error **errp, va_list vargs); 753 754 /** 755 * object_initialize: 756 * @obj: A pointer to the memory to be used for the object. 757 * @size: The maximum size available at @obj for the object. 758 * @typename: The name of the type of the object to instantiate. 759 * 760 * This function will initialize an object. The memory for the object should 761 * have already been allocated. The returned object has a reference count of 1, 762 * and will be finalized when the last reference is dropped. 763 */ 764 void object_initialize(void *obj, size_t size, const char *typename); 765 766 /** 767 * object_initialize_child_with_props: 768 * @parentobj: The parent object to add a property to 769 * @propname: The name of the property 770 * @childobj: A pointer to the memory to be used for the object. 771 * @size: The maximum size available at @childobj for the object. 772 * @type: The name of the type of the object to instantiate. 773 * @errp: If an error occurs, a pointer to an area to store the error 774 * @...: list of property names and values 775 * 776 * This function will initialize an object. The memory for the object should 777 * have already been allocated. The object will then be added as child property 778 * to a parent with object_property_add_child() function. The returned object 779 * has a reference count of 1 (for the "child<...>" property from the parent), 780 * so the object will be finalized automatically when the parent gets removed. 781 * 782 * The variadic parameters are a list of pairs of (propname, propvalue) 783 * strings. The propname of %NULL indicates the end of the property list. 784 * If the object implements the user creatable interface, the object will 785 * be marked complete once all the properties have been processed. 786 * 787 * Returns: %true on success, %false on failure. 788 */ 789 bool object_initialize_child_with_props(Object *parentobj, 790 const char *propname, 791 void *childobj, size_t size, const char *type, 792 Error **errp, ...) G_GNUC_NULL_TERMINATED; 793 794 /** 795 * object_initialize_child_with_propsv: 796 * @parentobj: The parent object to add a property to 797 * @propname: The name of the property 798 * @childobj: A pointer to the memory to be used for the object. 799 * @size: The maximum size available at @childobj for the object. 800 * @type: The name of the type of the object to instantiate. 801 * @errp: If an error occurs, a pointer to an area to store the error 802 * @vargs: list of property names and values 803 * 804 * See object_initialize_child() for documentation. 805 * 806 * Returns: %true on success, %false on failure. 807 */ 808 bool object_initialize_child_with_propsv(Object *parentobj, 809 const char *propname, 810 void *childobj, size_t size, const char *type, 811 Error **errp, va_list vargs); 812 813 /** 814 * object_initialize_child: 815 * @parent: The parent object to add a property to 816 * @propname: The name of the property 817 * @child: A precisely typed pointer to the memory to be used for the 818 * object. 819 * @type: The name of the type of the object to instantiate. 820 * 821 * This is like:: 822 * 823 * object_initialize_child_with_props(parent, propname, 824 * child, sizeof(*child), type, 825 * &error_abort, NULL) 826 */ 827 #define object_initialize_child(parent, propname, child, type) \ 828 object_initialize_child_internal((parent), (propname), \ 829 (child), sizeof(*(child)), (type)) 830 void object_initialize_child_internal(Object *parent, const char *propname, 831 void *child, size_t size, 832 const char *type); 833 834 /** 835 * object_dynamic_cast: 836 * @obj: The object to cast. 837 * @typename: The @typename to cast to. 838 * 839 * This function will determine if @obj is-a @typename. @obj can refer to an 840 * object or an interface associated with an object. 841 * 842 * Returns: This function returns @obj on success or #NULL on failure. 843 */ 844 Object *object_dynamic_cast(Object *obj, const char *typename); 845 846 /** 847 * object_dynamic_cast_assert: 848 * @obj: The object to cast. 849 * @typename: The @typename to cast to. 850 * @file: Source code file where function was called 851 * @line: Source code line where function was called 852 * @func: Name of function where this function was called 853 * 854 * See object_dynamic_cast() for a description of the parameters of this 855 * function. The only difference in behavior is that this function asserts 856 * instead of returning #NULL on failure if QOM cast debugging is enabled. 857 * This function is not meant to be called directly, but only through 858 * the wrapper macro OBJECT_CHECK. 859 */ 860 Object *object_dynamic_cast_assert(Object *obj, const char *typename, 861 const char *file, int line, const char *func); 862 863 /** 864 * object_get_class: 865 * @obj: A derivative of #Object 866 * 867 * Returns: The #ObjectClass of the type associated with @obj. 868 */ 869 ObjectClass *object_get_class(Object *obj); 870 871 /** 872 * object_get_typename: 873 * @obj: A derivative of #Object. 874 * 875 * Returns: The QOM typename of @obj. 876 */ 877 const char *object_get_typename(const Object *obj); 878 879 /** 880 * type_register_static: 881 * @info: The #TypeInfo of the new type. 882 * 883 * Returns: the new #Type. 884 */ 885 Type type_register_static(const TypeInfo *info); 886 887 /** 888 * type_register_static_array: 889 * @infos: The array of the new type #TypeInfo structures. 890 * @nr_infos: number of entries in @infos 891 * 892 * @infos and all of the strings it points to should exist for the life time 893 * that the type is registered. 894 */ 895 void type_register_static_array(const TypeInfo *infos, int nr_infos); 896 897 /** 898 * DEFINE_TYPES: 899 * @type_array: The array containing #TypeInfo structures to register 900 * 901 * @type_array should be static constant that exists for the life time 902 * that the type is registered. 903 */ 904 #define DEFINE_TYPES(type_array) \ 905 static void do_qemu_init_ ## type_array(void) \ 906 { \ 907 type_register_static_array(type_array, ARRAY_SIZE(type_array)); \ 908 } \ 909 type_init(do_qemu_init_ ## type_array) 910 911 /** 912 * type_print_class_properties: 913 * @type: a QOM class name 914 * 915 * Print the object's class properties to stdout or the monitor. 916 * Return whether an object was found. 917 */ 918 bool type_print_class_properties(const char *type); 919 920 /** 921 * object_set_properties_from_keyval: 922 * @obj: a QOM object 923 * @qdict: a dictionary with the properties to be set 924 * @from_json: true if leaf values of @qdict are typed, false if they 925 * are strings 926 * @errp: pointer to error object 927 * 928 * For each key in the dictionary, parse the value string if needed, 929 * then set the corresponding property in @obj. 930 */ 931 void object_set_properties_from_keyval(Object *obj, const QDict *qdict, 932 bool from_json, Error **errp); 933 934 /** 935 * object_class_dynamic_cast_assert: 936 * @klass: The #ObjectClass to attempt to cast. 937 * @typename: The QOM typename of the class to cast to. 938 * @file: Source code file where function was called 939 * @line: Source code line where function was called 940 * @func: Name of function where this function was called 941 * 942 * See object_class_dynamic_cast() for a description of the parameters 943 * of this function. The only difference in behavior is that this function 944 * asserts instead of returning #NULL on failure if QOM cast debugging is 945 * enabled. This function is not meant to be called directly, but only through 946 * the wrapper macro OBJECT_CLASS_CHECK. 947 */ 948 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass, 949 const char *typename, 950 const char *file, int line, 951 const char *func); 952 953 /** 954 * object_class_dynamic_cast: 955 * @klass: The #ObjectClass to attempt to cast. 956 * @typename: The QOM typename of the class to cast to. 957 * 958 * Returns: If @typename is a class, this function returns @klass if 959 * @typename is a subtype of @klass, else returns #NULL. 960 * 961 * If @typename is an interface, this function returns the interface 962 * definition for @klass if @klass implements it unambiguously; #NULL 963 * is returned if @klass does not implement the interface or if multiple 964 * classes or interfaces on the hierarchy leading to @klass implement 965 * it. (FIXME: perhaps this can be detected at type definition time?) 966 */ 967 ObjectClass *object_class_dynamic_cast(ObjectClass *klass, 968 const char *typename); 969 970 /** 971 * object_class_get_parent: 972 * @klass: The class to obtain the parent for. 973 * 974 * Returns: The parent for @klass or %NULL if none. 975 */ 976 ObjectClass *object_class_get_parent(ObjectClass *klass); 977 978 /** 979 * object_class_get_name: 980 * @klass: The class to obtain the QOM typename for. 981 * 982 * Returns: The QOM typename for @klass. 983 */ 984 const char *object_class_get_name(ObjectClass *klass); 985 986 /** 987 * object_class_is_abstract: 988 * @klass: The class to obtain the abstractness for. 989 * 990 * Returns: %true if @klass is abstract, %false otherwise. 991 */ 992 bool object_class_is_abstract(ObjectClass *klass); 993 994 /** 995 * object_class_by_name: 996 * @typename: The QOM typename to obtain the class for. 997 * 998 * Returns: The class for @typename or %NULL if not found. 999 */ 1000 ObjectClass *object_class_by_name(const char *typename); 1001 1002 /** 1003 * module_object_class_by_name: 1004 * @typename: The QOM typename to obtain the class for. 1005 * 1006 * For objects which might be provided by a module. Behaves like 1007 * object_class_by_name, but additionally tries to load the module 1008 * needed in case the class is not available. 1009 * 1010 * Returns: The class for @typename or %NULL if not found. 1011 */ 1012 ObjectClass *module_object_class_by_name(const char *typename); 1013 1014 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), 1015 const char *implements_type, bool include_abstract, 1016 void *opaque); 1017 1018 /** 1019 * object_class_get_list: 1020 * @implements_type: The type to filter for, including its derivatives. 1021 * @include_abstract: Whether to include abstract classes. 1022 * 1023 * Returns: A singly-linked list of the classes in reverse hashtable order. 1024 */ 1025 GSList *object_class_get_list(const char *implements_type, 1026 bool include_abstract); 1027 1028 /** 1029 * object_class_get_list_sorted: 1030 * @implements_type: The type to filter for, including its derivatives. 1031 * @include_abstract: Whether to include abstract classes. 1032 * 1033 * Returns: A singly-linked list of the classes in alphabetical 1034 * case-insensitive order. 1035 */ 1036 GSList *object_class_get_list_sorted(const char *implements_type, 1037 bool include_abstract); 1038 1039 /** 1040 * object_ref: 1041 * @obj: the object 1042 * 1043 * Increase the reference count of a object. A object cannot be freed as long 1044 * as its reference count is greater than zero. 1045 * Returns: @obj 1046 */ 1047 Object *object_ref(void *obj); 1048 1049 /** 1050 * object_unref: 1051 * @obj: the object 1052 * 1053 * Decrease the reference count of a object. A object cannot be freed as long 1054 * as its reference count is greater than zero. 1055 */ 1056 void object_unref(void *obj); 1057 1058 /** 1059 * object_property_try_add: 1060 * @obj: the object to add a property to 1061 * @name: the name of the property. This can contain any character except for 1062 * a forward slash. In general, you should use hyphens '-' instead of 1063 * underscores '_' when naming properties. 1064 * @type: the type name of the property. This namespace is pretty loosely 1065 * defined. Sub namespaces are constructed by using a prefix and then 1066 * to angle brackets. For instance, the type 'virtio-net-pci' in the 1067 * 'link' namespace would be 'link<virtio-net-pci>'. 1068 * @get: The getter to be called to read a property. If this is NULL, then 1069 * the property cannot be read. 1070 * @set: the setter to be called to write a property. If this is NULL, 1071 * then the property cannot be written. 1072 * @release: called when the property is removed from the object. This is 1073 * meant to allow a property to free its opaque upon object 1074 * destruction. This may be NULL. 1075 * @opaque: an opaque pointer to pass to the callbacks for the property 1076 * @errp: pointer to error object 1077 * 1078 * Returns: The #ObjectProperty; this can be used to set the @resolve 1079 * callback for child and link properties. 1080 */ 1081 ObjectProperty *object_property_try_add(Object *obj, const char *name, 1082 const char *type, 1083 ObjectPropertyAccessor *get, 1084 ObjectPropertyAccessor *set, 1085 ObjectPropertyRelease *release, 1086 void *opaque, Error **errp); 1087 1088 /** 1089 * object_property_add: 1090 * Same as object_property_try_add() with @errp hardcoded to 1091 * &error_abort. 1092 * 1093 * @obj: the object to add a property to 1094 * @name: the name of the property. This can contain any character except for 1095 * a forward slash. In general, you should use hyphens '-' instead of 1096 * underscores '_' when naming properties. 1097 * @type: the type name of the property. This namespace is pretty loosely 1098 * defined. Sub namespaces are constructed by using a prefix and then 1099 * to angle brackets. For instance, the type 'virtio-net-pci' in the 1100 * 'link' namespace would be 'link<virtio-net-pci>'. 1101 * @get: The getter to be called to read a property. If this is NULL, then 1102 * the property cannot be read. 1103 * @set: the setter to be called to write a property. If this is NULL, 1104 * then the property cannot be written. 1105 * @release: called when the property is removed from the object. This is 1106 * meant to allow a property to free its opaque upon object 1107 * destruction. This may be NULL. 1108 * @opaque: an opaque pointer to pass to the callbacks for the property 1109 */ 1110 ObjectProperty *object_property_add(Object *obj, const char *name, 1111 const char *type, 1112 ObjectPropertyAccessor *get, 1113 ObjectPropertyAccessor *set, 1114 ObjectPropertyRelease *release, 1115 void *opaque); 1116 1117 void object_property_del(Object *obj, const char *name); 1118 1119 ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name, 1120 const char *type, 1121 ObjectPropertyAccessor *get, 1122 ObjectPropertyAccessor *set, 1123 ObjectPropertyRelease *release, 1124 void *opaque); 1125 1126 /** 1127 * object_property_set_default_bool: 1128 * @prop: the property to set 1129 * @value: the value to be written to the property 1130 * 1131 * Set the property default value. 1132 */ 1133 void object_property_set_default_bool(ObjectProperty *prop, bool value); 1134 1135 /** 1136 * object_property_set_default_str: 1137 * @prop: the property to set 1138 * @value: the value to be written to the property 1139 * 1140 * Set the property default value. 1141 */ 1142 void object_property_set_default_str(ObjectProperty *prop, const char *value); 1143 1144 /** 1145 * object_property_set_default_list: 1146 * @prop: the property to set 1147 * 1148 * Set the property default value to be an empty list. 1149 */ 1150 void object_property_set_default_list(ObjectProperty *prop); 1151 1152 /** 1153 * object_property_set_default_int: 1154 * @prop: the property to set 1155 * @value: the value to be written to the property 1156 * 1157 * Set the property default value. 1158 */ 1159 void object_property_set_default_int(ObjectProperty *prop, int64_t value); 1160 1161 /** 1162 * object_property_set_default_uint: 1163 * @prop: the property to set 1164 * @value: the value to be written to the property 1165 * 1166 * Set the property default value. 1167 */ 1168 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value); 1169 1170 /** 1171 * object_property_find: 1172 * @obj: the object 1173 * @name: the name of the property 1174 * 1175 * Look up a property for an object. 1176 * 1177 * Return its #ObjectProperty if found, or NULL. 1178 */ 1179 ObjectProperty *object_property_find(Object *obj, const char *name); 1180 1181 /** 1182 * object_property_find_err: 1183 * @obj: the object 1184 * @name: the name of the property 1185 * @errp: returns an error if this function fails 1186 * 1187 * Look up a property for an object. 1188 * 1189 * Return its #ObjectProperty if found, or NULL. 1190 */ 1191 ObjectProperty *object_property_find_err(Object *obj, 1192 const char *name, 1193 Error **errp); 1194 1195 /** 1196 * object_class_property_find: 1197 * @klass: the object class 1198 * @name: the name of the property 1199 * 1200 * Look up a property for an object class. 1201 * 1202 * Return its #ObjectProperty if found, or NULL. 1203 */ 1204 ObjectProperty *object_class_property_find(ObjectClass *klass, 1205 const char *name); 1206 1207 /** 1208 * object_class_property_find_err: 1209 * @klass: the object class 1210 * @name: the name of the property 1211 * @errp: returns an error if this function fails 1212 * 1213 * Look up a property for an object class. 1214 * 1215 * Return its #ObjectProperty if found, or NULL. 1216 */ 1217 ObjectProperty *object_class_property_find_err(ObjectClass *klass, 1218 const char *name, 1219 Error **errp); 1220 1221 typedef struct ObjectPropertyIterator { 1222 ObjectClass *nextclass; 1223 GHashTableIter iter; 1224 } ObjectPropertyIterator; 1225 1226 /** 1227 * object_property_iter_init: 1228 * @iter: the iterator instance 1229 * @obj: the object 1230 * 1231 * Initializes an iterator for traversing all properties 1232 * registered against an object instance, its class and all parent classes. 1233 * 1234 * It is forbidden to modify the property list while iterating, 1235 * whether removing or adding properties. 1236 * 1237 * Typical usage pattern would be 1238 * 1239 * .. code-block:: c 1240 * :caption: Using object property iterators 1241 * 1242 * ObjectProperty *prop; 1243 * ObjectPropertyIterator iter; 1244 * 1245 * object_property_iter_init(&iter, obj); 1246 * while ((prop = object_property_iter_next(&iter))) { 1247 * ... do something with prop ... 1248 * } 1249 */ 1250 void object_property_iter_init(ObjectPropertyIterator *iter, 1251 Object *obj); 1252 1253 /** 1254 * object_class_property_iter_init: 1255 * @iter: the iterator instance 1256 * @klass: the class 1257 * 1258 * Initializes an iterator for traversing all properties 1259 * registered against an object class and all parent classes. 1260 * 1261 * It is forbidden to modify the property list while iterating, 1262 * whether removing or adding properties. 1263 * 1264 * This can be used on abstract classes as it does not create a temporary 1265 * instance. 1266 */ 1267 void object_class_property_iter_init(ObjectPropertyIterator *iter, 1268 ObjectClass *klass); 1269 1270 /** 1271 * object_property_iter_next: 1272 * @iter: the iterator instance 1273 * 1274 * Return the next available property. If no further properties 1275 * are available, a %NULL value will be returned and the @iter 1276 * pointer should not be used again after this point without 1277 * re-initializing it. 1278 * 1279 * Returns: the next property, or %NULL when all properties 1280 * have been traversed. 1281 */ 1282 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter); 1283 1284 void object_unparent(Object *obj); 1285 1286 /** 1287 * object_property_get: 1288 * @obj: the object 1289 * @name: the name of the property 1290 * @v: the visitor that will receive the property value. This should be an 1291 * Output visitor and the data will be written with @name as the name. 1292 * @errp: returns an error if this function fails 1293 * 1294 * Reads a property from a object. 1295 * 1296 * Returns: %true on success, %false on failure. 1297 */ 1298 bool object_property_get(Object *obj, const char *name, Visitor *v, 1299 Error **errp); 1300 1301 /** 1302 * object_property_set_str: 1303 * @obj: the object 1304 * @name: the name of the property 1305 * @value: the value to be written to the property 1306 * @errp: returns an error if this function fails 1307 * 1308 * Writes a string value to a property. 1309 * 1310 * Returns: %true on success, %false on failure. 1311 */ 1312 bool object_property_set_str(Object *obj, const char *name, 1313 const char *value, Error **errp); 1314 1315 /** 1316 * object_property_get_str: 1317 * @obj: the object 1318 * @name: the name of the property 1319 * @errp: returns an error if this function fails 1320 * 1321 * Returns: the value of the property, converted to a C string, or NULL if 1322 * an error occurs (including when the property value is not a string). 1323 * The caller should free the string. 1324 */ 1325 char *object_property_get_str(Object *obj, const char *name, 1326 Error **errp); 1327 1328 /** 1329 * object_property_set_link: 1330 * @obj: the object 1331 * @name: the name of the property 1332 * @value: the value to be written to the property 1333 * @errp: returns an error if this function fails 1334 * 1335 * Writes an object's canonical path to a property. 1336 * 1337 * If the link property was created with 1338 * %OBJ_PROP_LINK_STRONG bit, the old target object is 1339 * unreferenced, and a reference is added to the new target object. 1340 * 1341 * Returns: %true on success, %false on failure. 1342 */ 1343 bool object_property_set_link(Object *obj, const char *name, 1344 Object *value, Error **errp); 1345 1346 /** 1347 * object_property_get_link: 1348 * @obj: the object 1349 * @name: the name of the property 1350 * @errp: returns an error if this function fails 1351 * 1352 * Returns: the value of the property, resolved from a path to an Object, 1353 * or NULL if an error occurs (including when the property value is not a 1354 * string or not a valid object path). 1355 */ 1356 Object *object_property_get_link(Object *obj, const char *name, 1357 Error **errp); 1358 1359 /** 1360 * object_property_set_bool: 1361 * @obj: the object 1362 * @name: the name of the property 1363 * @value: the value to be written to the property 1364 * @errp: returns an error if this function fails 1365 * 1366 * Writes a bool value to a property. 1367 * 1368 * Returns: %true on success, %false on failure. 1369 */ 1370 bool object_property_set_bool(Object *obj, const char *name, 1371 bool value, Error **errp); 1372 1373 /** 1374 * object_property_get_bool: 1375 * @obj: the object 1376 * @name: the name of the property 1377 * @errp: returns an error if this function fails 1378 * 1379 * Returns: the value of the property, converted to a boolean, or false if 1380 * an error occurs (including when the property value is not a bool). 1381 */ 1382 bool object_property_get_bool(Object *obj, const char *name, 1383 Error **errp); 1384 1385 /** 1386 * object_property_set_int: 1387 * @obj: the object 1388 * @name: the name of the property 1389 * @value: the value to be written to the property 1390 * @errp: returns an error if this function fails 1391 * 1392 * Writes an integer value to a property. 1393 * 1394 * Returns: %true on success, %false on failure. 1395 */ 1396 bool object_property_set_int(Object *obj, const char *name, 1397 int64_t value, Error **errp); 1398 1399 /** 1400 * object_property_get_int: 1401 * @obj: the object 1402 * @name: the name of the property 1403 * @errp: returns an error if this function fails 1404 * 1405 * Returns: the value of the property, converted to an integer, or -1 if 1406 * an error occurs (including when the property value is not an integer). 1407 */ 1408 int64_t object_property_get_int(Object *obj, const char *name, 1409 Error **errp); 1410 1411 /** 1412 * object_property_set_uint: 1413 * @obj: the object 1414 * @name: the name of the property 1415 * @value: the value to be written to the property 1416 * @errp: returns an error if this function fails 1417 * 1418 * Writes an unsigned integer value to a property. 1419 * 1420 * Returns: %true on success, %false on failure. 1421 */ 1422 bool object_property_set_uint(Object *obj, const char *name, 1423 uint64_t value, Error **errp); 1424 1425 /** 1426 * object_property_get_uint: 1427 * @obj: the object 1428 * @name: the name of the property 1429 * @errp: returns an error if this function fails 1430 * 1431 * Returns: the value of the property, converted to an unsigned integer, or 0 1432 * an error occurs (including when the property value is not an integer). 1433 */ 1434 uint64_t object_property_get_uint(Object *obj, const char *name, 1435 Error **errp); 1436 1437 /** 1438 * object_property_get_enum: 1439 * @obj: the object 1440 * @name: the name of the property 1441 * @typename: the name of the enum data type 1442 * @errp: returns an error if this function fails 1443 * 1444 * Returns: the value of the property, converted to an integer (which 1445 * can't be negative), or -1 on error (including when the property 1446 * value is not an enum). 1447 */ 1448 int object_property_get_enum(Object *obj, const char *name, 1449 const char *typename, Error **errp); 1450 1451 /** 1452 * object_property_set: 1453 * @obj: the object 1454 * @name: the name of the property 1455 * @v: the visitor that will be used to write the property value. This should 1456 * be an Input visitor and the data will be first read with @name as the 1457 * name and then written as the property value. 1458 * @errp: returns an error if this function fails 1459 * 1460 * Writes a property to a object. 1461 * 1462 * Returns: %true on success, %false on failure. 1463 */ 1464 bool object_property_set(Object *obj, const char *name, Visitor *v, 1465 Error **errp); 1466 1467 /** 1468 * object_property_parse: 1469 * @obj: the object 1470 * @name: the name of the property 1471 * @string: the string that will be used to parse the property value. 1472 * @errp: returns an error if this function fails 1473 * 1474 * Parses a string and writes the result into a property of an object. 1475 * 1476 * Returns: %true on success, %false on failure. 1477 */ 1478 bool object_property_parse(Object *obj, const char *name, 1479 const char *string, Error **errp); 1480 1481 /** 1482 * object_property_print: 1483 * @obj: the object 1484 * @name: the name of the property 1485 * @human: if true, print for human consumption 1486 * @errp: returns an error if this function fails 1487 * 1488 * Returns a string representation of the value of the property. The 1489 * caller shall free the string. 1490 */ 1491 char *object_property_print(Object *obj, const char *name, bool human, 1492 Error **errp); 1493 1494 /** 1495 * object_property_get_type: 1496 * @obj: the object 1497 * @name: the name of the property 1498 * @errp: returns an error if this function fails 1499 * 1500 * Returns: The type name of the property. 1501 */ 1502 const char *object_property_get_type(Object *obj, const char *name, 1503 Error **errp); 1504 1505 /** 1506 * object_get_root: 1507 * 1508 * Returns: the root object of the composition tree 1509 */ 1510 Object *object_get_root(void); 1511 1512 1513 /** 1514 * object_get_objects_root: 1515 * 1516 * Get the container object that holds user created 1517 * object instances. This is the object at path 1518 * "/objects" 1519 * 1520 * Returns: the user object container 1521 */ 1522 Object *object_get_objects_root(void); 1523 1524 /** 1525 * object_get_internal_root: 1526 * 1527 * Get the container object that holds internally used object 1528 * instances. Any object which is put into this container must not be 1529 * user visible, and it will not be exposed in the QOM tree. 1530 * 1531 * Returns: the internal object container 1532 */ 1533 Object *object_get_internal_root(void); 1534 1535 /** 1536 * object_get_canonical_path_component: 1537 * @obj: the object 1538 * 1539 * Returns: The final component in the object's canonical path. The canonical 1540 * path is the path within the composition tree starting from the root. 1541 * %NULL if the object doesn't have a parent (and thus a canonical path). 1542 */ 1543 const char *object_get_canonical_path_component(const Object *obj); 1544 1545 /** 1546 * object_get_canonical_path: 1547 * @obj: the object 1548 * 1549 * Returns: The canonical path for a object, newly allocated. This is 1550 * the path within the composition tree starting from the root. Use 1551 * g_free() to free it. 1552 */ 1553 char *object_get_canonical_path(const Object *obj); 1554 1555 /** 1556 * object_resolve_path: 1557 * @path: the path to resolve 1558 * @ambiguous: (out) (optional): location to store whether the lookup failed 1559 * because it was ambiguous, or %NULL. Set to %false on success. 1560 * 1561 * There are two types of supported paths--absolute paths and partial paths. 1562 * 1563 * Absolute paths are derived from the root object and can follow child<> or 1564 * link<> properties. Since they can follow link<> properties, they can be 1565 * arbitrarily long. Absolute paths look like absolute filenames and are 1566 * prefixed with a leading slash. 1567 * 1568 * Partial paths look like relative filenames. They do not begin with a 1569 * prefix. The matching rules for partial paths are subtle but designed to make 1570 * specifying objects easy. At each level of the composition tree, the partial 1571 * path is matched as an absolute path. The first match is not returned. At 1572 * least two matches are searched for. A successful result is only returned if 1573 * only one match is found. If more than one match is found, a flag is 1574 * returned to indicate that the match was ambiguous. 1575 * 1576 * Returns: The matched object or %NULL on path lookup failure. 1577 */ 1578 Object *object_resolve_path(const char *path, bool *ambiguous); 1579 1580 /** 1581 * object_resolve_path_type: 1582 * @path: the path to resolve 1583 * @typename: the type to look for. 1584 * @ambiguous: (out) (optional): location to store whether the lookup failed 1585 * because it was ambiguous, or %NULL. Set to %false on success. 1586 * 1587 * This is similar to object_resolve_path(). However, when looking for a 1588 * partial path only matches that implement the given type are considered. 1589 * This restricts the search and avoids spuriously flagging matches as 1590 * ambiguous. 1591 * 1592 * For both partial and absolute paths, the return value goes through 1593 * a dynamic cast to @typename. This is important if either the link, 1594 * or the typename itself are of interface types. 1595 * 1596 * Returns: The matched object or NULL on path lookup failure. 1597 */ 1598 Object *object_resolve_path_type(const char *path, const char *typename, 1599 bool *ambiguous); 1600 1601 /** 1602 * object_resolve_type_unambiguous: 1603 * @typename: the type to look for 1604 * @errp: pointer to error object 1605 * 1606 * Return the only object in the QOM tree of type @typename. 1607 * If no match or more than one match is found, an error is 1608 * returned. 1609 * 1610 * Returns: The matched object or NULL on path lookup failure. 1611 */ 1612 Object *object_resolve_type_unambiguous(const char *typename, Error **errp); 1613 1614 /** 1615 * object_resolve_path_at: 1616 * @parent: the object in which to resolve the path 1617 * @path: the path to resolve 1618 * 1619 * This is like object_resolve_path(), except paths not starting with 1620 * a slash are relative to @parent. 1621 * 1622 * Returns: The resolved object or NULL on path lookup failure. 1623 */ 1624 Object *object_resolve_path_at(Object *parent, const char *path); 1625 1626 /** 1627 * object_resolve_path_component: 1628 * @parent: the object in which to resolve the path 1629 * @part: the component to resolve. 1630 * 1631 * This is similar to object_resolve_path with an absolute path, but it 1632 * only resolves one element (@part) and takes the others from @parent. 1633 * 1634 * Returns: The resolved object or NULL on path lookup failure. 1635 */ 1636 Object *object_resolve_path_component(Object *parent, const char *part); 1637 1638 /** 1639 * object_property_try_add_child: 1640 * @obj: the object to add a property to 1641 * @name: the name of the property 1642 * @child: the child object 1643 * @errp: pointer to error object 1644 * 1645 * Child properties form the composition tree. All objects need to be a child 1646 * of another object. Objects can only be a child of one object. 1647 * 1648 * There is no way for a child to determine what its parent is. It is not 1649 * a bidirectional relationship. This is by design. 1650 * 1651 * The value of a child property as a C string will be the child object's 1652 * canonical path. It can be retrieved using object_property_get_str(). 1653 * The child object itself can be retrieved using object_property_get_link(). 1654 * 1655 * Returns: The newly added property on success, or %NULL on failure. 1656 */ 1657 ObjectProperty *object_property_try_add_child(Object *obj, const char *name, 1658 Object *child, Error **errp); 1659 1660 /** 1661 * object_property_add_child: 1662 * @obj: the object to add a property to 1663 * @name: the name of the property 1664 * @child: the child object 1665 * 1666 * Same as object_property_try_add_child() with @errp hardcoded to 1667 * &error_abort 1668 */ 1669 ObjectProperty *object_property_add_child(Object *obj, const char *name, 1670 Object *child); 1671 1672 typedef enum { 1673 /* Unref the link pointer when the property is deleted */ 1674 OBJ_PROP_LINK_STRONG = 0x1, 1675 1676 /* private */ 1677 OBJ_PROP_LINK_DIRECT = 0x2, 1678 OBJ_PROP_LINK_CLASS = 0x4, 1679 } ObjectPropertyLinkFlags; 1680 1681 /** 1682 * object_property_allow_set_link: 1683 * @obj: the object to add a property to 1684 * @name: the name of the property 1685 * @child: the child object 1686 * @errp: pointer to error object 1687 * 1688 * The default implementation of the object_property_add_link() check() 1689 * callback function. It allows the link property to be set and never returns 1690 * an error. 1691 */ 1692 void object_property_allow_set_link(const Object *obj, const char *name, 1693 Object *child, Error **errp); 1694 1695 /** 1696 * object_property_add_link: 1697 * @obj: the object to add a property to 1698 * @name: the name of the property 1699 * @type: the qobj type of the link 1700 * @targetp: a pointer to where the link object reference is stored 1701 * @check: callback to veto setting or NULL if the property is read-only 1702 * @flags: additional options for the link 1703 * 1704 * Links establish relationships between objects. Links are unidirectional 1705 * although two links can be combined to form a bidirectional relationship 1706 * between objects. 1707 * 1708 * Links form the graph in the object model. 1709 * 1710 * The @check() callback is invoked when 1711 * object_property_set_link() is called and can raise an error to prevent the 1712 * link being set. If @check is NULL, the property is read-only 1713 * and cannot be set. 1714 * 1715 * Ownership of the pointer that @child points to is transferred to the 1716 * link property. The reference count for *@child is 1717 * managed by the property from after the function returns till the 1718 * property is deleted with object_property_del(). If the 1719 * @flags %OBJ_PROP_LINK_STRONG bit is set, 1720 * the reference count is decremented when the property is deleted or 1721 * modified. 1722 * 1723 * Returns: The newly added property on success, or %NULL on failure. 1724 */ 1725 ObjectProperty *object_property_add_link(Object *obj, const char *name, 1726 const char *type, Object **targetp, 1727 void (*check)(const Object *obj, const char *name, 1728 Object *val, Error **errp), 1729 ObjectPropertyLinkFlags flags); 1730 1731 ObjectProperty *object_class_property_add_link(ObjectClass *oc, 1732 const char *name, 1733 const char *type, ptrdiff_t offset, 1734 void (*check)(const Object *obj, const char *name, 1735 Object *val, Error **errp), 1736 ObjectPropertyLinkFlags flags); 1737 1738 /** 1739 * object_property_add_str: 1740 * @obj: the object to add a property to 1741 * @name: the name of the property 1742 * @get: the getter or NULL if the property is write-only. This function must 1743 * return a string to be freed by g_free(). 1744 * @set: the setter or NULL if the property is read-only 1745 * 1746 * Add a string property using getters/setters. This function will add a 1747 * property of type 'string'. 1748 * 1749 * Returns: The newly added property on success, or %NULL on failure. 1750 */ 1751 ObjectProperty *object_property_add_str(Object *obj, const char *name, 1752 char *(*get)(Object *, Error **), 1753 void (*set)(Object *, const char *, Error **)); 1754 1755 ObjectProperty *object_class_property_add_str(ObjectClass *klass, 1756 const char *name, 1757 char *(*get)(Object *, Error **), 1758 void (*set)(Object *, const char *, 1759 Error **)); 1760 1761 /** 1762 * object_property_add_bool: 1763 * @obj: the object to add a property to 1764 * @name: the name of the property 1765 * @get: the getter or NULL if the property is write-only. 1766 * @set: the setter or NULL if the property is read-only 1767 * 1768 * Add a bool property using getters/setters. This function will add a 1769 * property of type 'bool'. 1770 * 1771 * Returns: The newly added property on success, or %NULL on failure. 1772 */ 1773 ObjectProperty *object_property_add_bool(Object *obj, const char *name, 1774 bool (*get)(Object *, Error **), 1775 void (*set)(Object *, bool, Error **)); 1776 1777 ObjectProperty *object_class_property_add_bool(ObjectClass *klass, 1778 const char *name, 1779 bool (*get)(Object *, Error **), 1780 void (*set)(Object *, bool, Error **)); 1781 1782 /** 1783 * object_property_add_enum: 1784 * @obj: the object to add a property to 1785 * @name: the name of the property 1786 * @typename: the name of the enum data type 1787 * @lookup: enum value namelookup table 1788 * @get: the getter or %NULL if the property is write-only. 1789 * @set: the setter or %NULL if the property is read-only 1790 * 1791 * Add an enum property using getters/setters. This function will add a 1792 * property of type '@typename'. 1793 * 1794 * Returns: The newly added property on success, or %NULL on failure. 1795 */ 1796 ObjectProperty *object_property_add_enum(Object *obj, const char *name, 1797 const char *typename, 1798 const QEnumLookup *lookup, 1799 int (*get)(Object *, Error **), 1800 void (*set)(Object *, int, Error **)); 1801 1802 ObjectProperty *object_class_property_add_enum(ObjectClass *klass, 1803 const char *name, 1804 const char *typename, 1805 const QEnumLookup *lookup, 1806 int (*get)(Object *, Error **), 1807 void (*set)(Object *, int, Error **)); 1808 1809 /** 1810 * object_property_add_tm: 1811 * @obj: the object to add a property to 1812 * @name: the name of the property 1813 * @get: the getter or NULL if the property is write-only. 1814 * 1815 * Add a read-only struct tm valued property using a getter function. 1816 * This function will add a property of type 'struct tm'. 1817 * 1818 * Returns: The newly added property on success, or %NULL on failure. 1819 */ 1820 ObjectProperty *object_property_add_tm(Object *obj, const char *name, 1821 void (*get)(Object *, struct tm *, Error **)); 1822 1823 ObjectProperty *object_class_property_add_tm(ObjectClass *klass, 1824 const char *name, 1825 void (*get)(Object *, struct tm *, Error **)); 1826 1827 typedef enum { 1828 /* Automatically add a getter to the property */ 1829 OBJ_PROP_FLAG_READ = 1 << 0, 1830 /* Automatically add a setter to the property */ 1831 OBJ_PROP_FLAG_WRITE = 1 << 1, 1832 /* Automatically add a getter and a setter to the property */ 1833 OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE), 1834 } ObjectPropertyFlags; 1835 1836 /** 1837 * object_property_add_uint8_ptr: 1838 * @obj: the object to add a property to 1839 * @name: the name of the property 1840 * @v: pointer to value 1841 * @flags: bitwise-or'd ObjectPropertyFlags 1842 * 1843 * Add an integer property in memory. This function will add a 1844 * property of type 'uint8'. 1845 * 1846 * Returns: The newly added property on success, or %NULL on failure. 1847 */ 1848 ObjectProperty *object_property_add_uint8_ptr(Object *obj, const char *name, 1849 const uint8_t *v, 1850 ObjectPropertyFlags flags); 1851 1852 ObjectProperty *object_class_property_add_uint8_ptr(ObjectClass *klass, 1853 const char *name, 1854 const uint8_t *v, 1855 ObjectPropertyFlags flags); 1856 1857 /** 1858 * object_property_add_uint16_ptr: 1859 * @obj: the object to add a property to 1860 * @name: the name of the property 1861 * @v: pointer to value 1862 * @flags: bitwise-or'd ObjectPropertyFlags 1863 * 1864 * Add an integer property in memory. This function will add a 1865 * property of type 'uint16'. 1866 * 1867 * Returns: The newly added property on success, or %NULL on failure. 1868 */ 1869 ObjectProperty *object_property_add_uint16_ptr(Object *obj, const char *name, 1870 const uint16_t *v, 1871 ObjectPropertyFlags flags); 1872 1873 ObjectProperty *object_class_property_add_uint16_ptr(ObjectClass *klass, 1874 const char *name, 1875 const uint16_t *v, 1876 ObjectPropertyFlags flags); 1877 1878 /** 1879 * object_property_add_uint32_ptr: 1880 * @obj: the object to add a property to 1881 * @name: the name of the property 1882 * @v: pointer to value 1883 * @flags: bitwise-or'd ObjectPropertyFlags 1884 * 1885 * Add an integer property in memory. This function will add a 1886 * property of type 'uint32'. 1887 * 1888 * Returns: The newly added property on success, or %NULL on failure. 1889 */ 1890 ObjectProperty *object_property_add_uint32_ptr(Object *obj, const char *name, 1891 const uint32_t *v, 1892 ObjectPropertyFlags flags); 1893 1894 ObjectProperty *object_class_property_add_uint32_ptr(ObjectClass *klass, 1895 const char *name, 1896 const uint32_t *v, 1897 ObjectPropertyFlags flags); 1898 1899 /** 1900 * object_property_add_uint64_ptr: 1901 * @obj: the object to add a property to 1902 * @name: the name of the property 1903 * @v: pointer to value 1904 * @flags: bitwise-or'd ObjectPropertyFlags 1905 * 1906 * Add an integer property in memory. This function will add a 1907 * property of type 'uint64'. 1908 * 1909 * Returns: The newly added property on success, or %NULL on failure. 1910 */ 1911 ObjectProperty *object_property_add_uint64_ptr(Object *obj, const char *name, 1912 const uint64_t *v, 1913 ObjectPropertyFlags flags); 1914 1915 ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass, 1916 const char *name, 1917 const uint64_t *v, 1918 ObjectPropertyFlags flags); 1919 1920 /** 1921 * object_property_add_alias: 1922 * @obj: the object to add a property to 1923 * @name: the name of the property 1924 * @target_obj: the object to forward property access to 1925 * @target_name: the name of the property on the forwarded object 1926 * 1927 * Add an alias for a property on an object. This function will add a property 1928 * of the same type as the forwarded property. 1929 * 1930 * The caller must ensure that @target_obj stays alive as long as 1931 * this property exists. In the case of a child object or an alias on the same 1932 * object this will be the case. For aliases to other objects the caller is 1933 * responsible for taking a reference. 1934 * 1935 * Returns: The newly added property on success, or %NULL on failure. 1936 */ 1937 ObjectProperty *object_property_add_alias(Object *obj, const char *name, 1938 Object *target_obj, const char *target_name); 1939 1940 /** 1941 * object_property_add_const_link: 1942 * @obj: the object to add a property to 1943 * @name: the name of the property 1944 * @target: the object to be referred by the link 1945 * 1946 * Add an unmodifiable link for a property on an object. This function will 1947 * add a property of type link<TYPE> where TYPE is the type of @target. 1948 * 1949 * The caller must ensure that @target stays alive as long as 1950 * this property exists. In the case @target is a child of @obj, 1951 * this will be the case. Otherwise, the caller is responsible for 1952 * taking a reference. 1953 * 1954 * Returns: The newly added property on success, or %NULL on failure. 1955 */ 1956 ObjectProperty *object_property_add_const_link(Object *obj, const char *name, 1957 Object *target); 1958 1959 /** 1960 * object_property_set_description: 1961 * @obj: the object owning the property 1962 * @name: the name of the property 1963 * @description: the description of the property on the object 1964 * 1965 * Set an object property's description. 1966 * 1967 * Returns: %true on success, %false on failure. 1968 */ 1969 void object_property_set_description(Object *obj, const char *name, 1970 const char *description); 1971 void object_class_property_set_description(ObjectClass *klass, const char *name, 1972 const char *description); 1973 1974 /** 1975 * object_child_foreach: 1976 * @obj: the object whose children will be navigated 1977 * @fn: the iterator function to be called 1978 * @opaque: an opaque value that will be passed to the iterator 1979 * 1980 * Call @fn passing each child of @obj and @opaque to it, until @fn returns 1981 * non-zero. 1982 * 1983 * It is forbidden to add or remove children from @obj from the @fn 1984 * callback. 1985 * 1986 * Returns: The last value returned by @fn, or 0 if there is no child. 1987 */ 1988 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), 1989 void *opaque); 1990 1991 /** 1992 * object_child_foreach_recursive: 1993 * @obj: the object whose children will be navigated 1994 * @fn: the iterator function to be called 1995 * @opaque: an opaque value that will be passed to the iterator 1996 * 1997 * Call @fn passing each child of @obj and @opaque to it, until @fn returns 1998 * non-zero. Calls recursively, all child nodes of @obj will also be passed 1999 * all the way down to the leaf nodes of the tree. Depth first ordering. 2000 * 2001 * It is forbidden to add or remove children from @obj (or its 2002 * child nodes) from the @fn callback. 2003 * 2004 * Returns: The last value returned by @fn, or 0 if there is no child. 2005 */ 2006 int object_child_foreach_recursive(Object *obj, 2007 int (*fn)(Object *child, void *opaque), 2008 void *opaque); 2009 /** 2010 * container_get: 2011 * @root: root of the #path, e.g., object_get_root() 2012 * @path: path to the container 2013 * 2014 * Return a container object whose path is @path. Create more containers 2015 * along the path if necessary. 2016 * 2017 * Returns: the container object. 2018 */ 2019 Object *container_get(Object *root, const char *path); 2020 2021 /** 2022 * object_property_help: 2023 * @name: the name of the property 2024 * @type: the type of the property 2025 * @defval: the default value 2026 * @description: description of the property 2027 * 2028 * Returns: a user-friendly formatted string describing the property 2029 * for help purposes. 2030 */ 2031 char *object_property_help(const char *name, const char *type, 2032 QObject *defval, const char *description); 2033 2034 G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref) 2035 2036 #endif 2037