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