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