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