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 * g_printerr("Cannot create memory backend: %s\n", 675 * error_get_pretty(err)); 676 * } 677 * </programlisting> 678 * </example> 679 * 680 * The returned object will have one stable reference maintained 681 * for as long as it is present in the object hierarchy. 682 * 683 * Returns: The newly allocated, instantiated & initialized object. 684 */ 685 Object *object_new_with_props(const char *typename, 686 Object *parent, 687 const char *id, 688 Error **errp, 689 ...) QEMU_SENTINEL; 690 691 /** 692 * object_new_with_propv: 693 * @typename: The name of the type of the object to instantiate. 694 * @parent: the parent object 695 * @id: The unique ID of the object 696 * @errp: pointer to error object 697 * @vargs: list of property names and values 698 * 699 * See object_new_with_props() for documentation. 700 */ 701 Object *object_new_with_propv(const char *typename, 702 Object *parent, 703 const char *id, 704 Error **errp, 705 va_list vargs); 706 707 void object_apply_global_props(Object *obj, const GPtrArray *props, 708 Error **errp); 709 void object_set_machine_compat_props(GPtrArray *compat_props); 710 void object_set_accelerator_compat_props(GPtrArray *compat_props); 711 void object_register_sugar_prop(const char *driver, const char *prop, const char *value); 712 void object_apply_compat_props(Object *obj); 713 714 /** 715 * object_set_props: 716 * @obj: the object instance to set properties on 717 * @errp: pointer to error object 718 * @...: list of property names and values 719 * 720 * This function will set a list of properties on an existing object 721 * instance. 722 * 723 * The variadic parameters are a list of pairs of (propname, propvalue) 724 * strings. The propname of %NULL indicates the end of the property 725 * list. 726 * 727 * <example> 728 * <title>Update an object's properties</title> 729 * <programlisting> 730 * Error *err = NULL; 731 * Object *obj = ...get / create object...; 732 * 733 * obj = object_set_props(obj, 734 * &err, 735 * "share", "yes", 736 * "mem-path", "/dev/shm/somefile", 737 * "prealloc", "yes", 738 * "size", "1048576", 739 * NULL); 740 * 741 * if (!obj) { 742 * g_printerr("Cannot set properties: %s\n", 743 * error_get_pretty(err)); 744 * } 745 * </programlisting> 746 * </example> 747 * 748 * The returned object will have one stable reference maintained 749 * for as long as it is present in the object hierarchy. 750 * 751 * Returns: -1 on error, 0 on success 752 */ 753 int object_set_props(Object *obj, 754 Error **errp, 755 ...) QEMU_SENTINEL; 756 757 /** 758 * object_set_propv: 759 * @obj: the object instance to set properties on 760 * @errp: pointer to error object 761 * @vargs: list of property names and values 762 * 763 * See object_set_props() for documentation. 764 * 765 * Returns: -1 on error, 0 on success 766 */ 767 int object_set_propv(Object *obj, 768 Error **errp, 769 va_list vargs); 770 771 /** 772 * object_initialize: 773 * @obj: A pointer to the memory to be used for the object. 774 * @size: The maximum size available at @obj for the object. 775 * @typename: The name of the type of the object to instantiate. 776 * 777 * This function will initialize an object. The memory for the object should 778 * have already been allocated. The returned object has a reference count of 1, 779 * and will be finalized when the last reference is dropped. 780 */ 781 void object_initialize(void *obj, size_t size, const char *typename); 782 783 /** 784 * object_initialize_child: 785 * @parentobj: The parent object to add a property to 786 * @propname: The name of the property 787 * @childobj: A pointer to the memory to be used for the object. 788 * @size: The maximum size available at @childobj for the object. 789 * @type: The name of the type of the object to instantiate. 790 * @errp: If an error occurs, a pointer to an area to store the error 791 * @...: list of property names and values 792 * 793 * This function will initialize an object. The memory for the object should 794 * have already been allocated. The object will then be added as child property 795 * to a parent with object_property_add_child() function. The returned object 796 * has a reference count of 1 (for the "child<...>" property from the parent), 797 * so the object will be finalized automatically when the parent gets removed. 798 * 799 * The variadic parameters are a list of pairs of (propname, propvalue) 800 * strings. The propname of %NULL indicates the end of the property list. 801 * If the object implements the user creatable interface, the object will 802 * be marked complete once all the properties have been processed. 803 */ 804 void object_initialize_child(Object *parentobj, const char *propname, 805 void *childobj, size_t size, const char *type, 806 Error **errp, ...) QEMU_SENTINEL; 807 808 /** 809 * object_initialize_childv: 810 * @parentobj: The parent object to add a property to 811 * @propname: The name of the property 812 * @childobj: A pointer to the memory to be used for the object. 813 * @size: The maximum size available at @childobj for the object. 814 * @type: The name of the type of the object to instantiate. 815 * @errp: If an error occurs, a pointer to an area to store the error 816 * @vargs: list of property names and values 817 * 818 * See object_initialize_child() for documentation. 819 */ 820 void object_initialize_childv(Object *parentobj, const char *propname, 821 void *childobj, size_t size, const char *type, 822 Error **errp, va_list vargs); 823 824 /** 825 * object_dynamic_cast: 826 * @obj: The object to cast. 827 * @typename: The @typename to cast to. 828 * 829 * This function will determine if @obj is-a @typename. @obj can refer to an 830 * object or an interface associated with an object. 831 * 832 * Returns: This function returns @obj on success or #NULL on failure. 833 */ 834 Object *object_dynamic_cast(Object *obj, const char *typename); 835 836 /** 837 * object_dynamic_cast_assert: 838 * 839 * See object_dynamic_cast() for a description of the parameters of this 840 * function. The only difference in behavior is that this function asserts 841 * instead of returning #NULL on failure if QOM cast debugging is enabled. 842 * This function is not meant to be called directly, but only through 843 * the wrapper macro OBJECT_CHECK. 844 */ 845 Object *object_dynamic_cast_assert(Object *obj, const char *typename, 846 const char *file, int line, const char *func); 847 848 /** 849 * object_get_class: 850 * @obj: A derivative of #Object 851 * 852 * Returns: The #ObjectClass of the type associated with @obj. 853 */ 854 ObjectClass *object_get_class(Object *obj); 855 856 /** 857 * object_get_typename: 858 * @obj: A derivative of #Object. 859 * 860 * Returns: The QOM typename of @obj. 861 */ 862 const char *object_get_typename(const Object *obj); 863 864 /** 865 * type_register_static: 866 * @info: The #TypeInfo of the new type. 867 * 868 * @info and all of the strings it points to should exist for the life time 869 * that the type is registered. 870 * 871 * Returns: the new #Type. 872 */ 873 Type type_register_static(const TypeInfo *info); 874 875 /** 876 * type_register: 877 * @info: The #TypeInfo of the new type 878 * 879 * Unlike type_register_static(), this call does not require @info or its 880 * string members to continue to exist after the call returns. 881 * 882 * Returns: the new #Type. 883 */ 884 Type type_register(const TypeInfo *info); 885 886 /** 887 * type_register_static_array: 888 * @infos: The array of the new type #TypeInfo structures. 889 * @nr_infos: number of entries in @infos 890 * 891 * @infos and all of the strings it points to should exist for the life time 892 * that the type is registered. 893 */ 894 void type_register_static_array(const TypeInfo *infos, int nr_infos); 895 896 /** 897 * DEFINE_TYPES: 898 * @type_array: The array containing #TypeInfo structures to register 899 * 900 * @type_array should be static constant that exists for the life time 901 * that the type is registered. 902 */ 903 #define DEFINE_TYPES(type_array) \ 904 static void do_qemu_init_ ## type_array(void) \ 905 { \ 906 type_register_static_array(type_array, ARRAY_SIZE(type_array)); \ 907 } \ 908 type_init(do_qemu_init_ ## type_array) 909 910 /** 911 * object_class_dynamic_cast_assert: 912 * @klass: The #ObjectClass to attempt to cast. 913 * @typename: The QOM typename of the class to cast to. 914 * 915 * See object_class_dynamic_cast() for a description of the parameters 916 * of this function. The only difference in behavior is that this function 917 * asserts instead of returning #NULL on failure if QOM cast debugging is 918 * enabled. This function is not meant to be called directly, but only through 919 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK. 920 */ 921 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass, 922 const char *typename, 923 const char *file, int line, 924 const char *func); 925 926 /** 927 * object_class_dynamic_cast: 928 * @klass: The #ObjectClass to attempt to cast. 929 * @typename: The QOM typename of the class to cast to. 930 * 931 * Returns: If @typename is a class, this function returns @klass if 932 * @typename is a subtype of @klass, else returns #NULL. 933 * 934 * If @typename is an interface, this function returns the interface 935 * definition for @klass if @klass implements it unambiguously; #NULL 936 * is returned if @klass does not implement the interface or if multiple 937 * classes or interfaces on the hierarchy leading to @klass implement 938 * it. (FIXME: perhaps this can be detected at type definition time?) 939 */ 940 ObjectClass *object_class_dynamic_cast(ObjectClass *klass, 941 const char *typename); 942 943 /** 944 * object_class_get_parent: 945 * @klass: The class to obtain the parent for. 946 * 947 * Returns: The parent for @klass or %NULL if none. 948 */ 949 ObjectClass *object_class_get_parent(ObjectClass *klass); 950 951 /** 952 * object_class_get_name: 953 * @klass: The class to obtain the QOM typename for. 954 * 955 * Returns: The QOM typename for @klass. 956 */ 957 const char *object_class_get_name(ObjectClass *klass); 958 959 /** 960 * object_class_is_abstract: 961 * @klass: The class to obtain the abstractness for. 962 * 963 * Returns: %true if @klass is abstract, %false otherwise. 964 */ 965 bool object_class_is_abstract(ObjectClass *klass); 966 967 /** 968 * object_class_by_name: 969 * @typename: The QOM typename to obtain the class for. 970 * 971 * Returns: The class for @typename or %NULL if not found. 972 */ 973 ObjectClass *object_class_by_name(const char *typename); 974 975 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), 976 const char *implements_type, bool include_abstract, 977 void *opaque); 978 979 /** 980 * object_class_get_list: 981 * @implements_type: The type to filter for, including its derivatives. 982 * @include_abstract: Whether to include abstract classes. 983 * 984 * Returns: A singly-linked list of the classes in reverse hashtable order. 985 */ 986 GSList *object_class_get_list(const char *implements_type, 987 bool include_abstract); 988 989 /** 990 * object_class_get_list_sorted: 991 * @implements_type: The type to filter for, including its derivatives. 992 * @include_abstract: Whether to include abstract classes. 993 * 994 * Returns: A singly-linked list of the classes in alphabetical 995 * case-insensitive order. 996 */ 997 GSList *object_class_get_list_sorted(const char *implements_type, 998 bool include_abstract); 999 1000 /** 1001 * object_ref: 1002 * @obj: the object 1003 * 1004 * Increase the reference count of a object. A object cannot be freed as long 1005 * as its reference count is greater than zero. 1006 * Returns: @obj 1007 */ 1008 Object *object_ref(Object *obj); 1009 1010 /** 1011 * object_unref: 1012 * @obj: the object 1013 * 1014 * Decrease the reference count of a object. A object cannot be freed as long 1015 * as its reference count is greater than zero. 1016 */ 1017 void object_unref(Object *obj); 1018 1019 /** 1020 * object_property_add: 1021 * @obj: the object to add a property to 1022 * @name: the name of the property. This can contain any character except for 1023 * a forward slash. In general, you should use hyphens '-' instead of 1024 * underscores '_' when naming properties. 1025 * @type: the type name of the property. This namespace is pretty loosely 1026 * defined. Sub namespaces are constructed by using a prefix and then 1027 * to angle brackets. For instance, the type 'virtio-net-pci' in the 1028 * 'link' namespace would be 'link<virtio-net-pci>'. 1029 * @get: The getter to be called to read a property. If this is NULL, then 1030 * the property cannot be read. 1031 * @set: the setter to be called to write a property. If this is NULL, 1032 * then the property cannot be written. 1033 * @release: called when the property is removed from the object. This is 1034 * meant to allow a property to free its opaque upon object 1035 * destruction. This may be NULL. 1036 * @opaque: an opaque pointer to pass to the callbacks for the property 1037 * 1038 * Returns: The #ObjectProperty; this can be used to set the @resolve 1039 * callback for child and link properties. 1040 */ 1041 ObjectProperty *object_property_add(Object *obj, const char *name, 1042 const char *type, 1043 ObjectPropertyAccessor *get, 1044 ObjectPropertyAccessor *set, 1045 ObjectPropertyRelease *release, 1046 void *opaque); 1047 1048 void object_property_del(Object *obj, const char *name); 1049 1050 ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name, 1051 const char *type, 1052 ObjectPropertyAccessor *get, 1053 ObjectPropertyAccessor *set, 1054 ObjectPropertyRelease *release, 1055 void *opaque); 1056 1057 /** 1058 * object_property_set_default_bool: 1059 * @prop: the property to set 1060 * @value: the value to be written to the property 1061 * 1062 * Set the property default value. 1063 */ 1064 void object_property_set_default_bool(ObjectProperty *prop, bool value); 1065 1066 /** 1067 * object_property_set_default_str: 1068 * @prop: the property to set 1069 * @value: the value to be written to the property 1070 * 1071 * Set the property default value. 1072 */ 1073 void object_property_set_default_str(ObjectProperty *prop, const char *value); 1074 1075 /** 1076 * object_property_set_default_int: 1077 * @prop: the property to set 1078 * @value: the value to be written to the property 1079 * 1080 * Set the property default value. 1081 */ 1082 void object_property_set_default_int(ObjectProperty *prop, int64_t value); 1083 1084 /** 1085 * object_property_set_default_uint: 1086 * @prop: the property to set 1087 * @value: the value to be written to the property 1088 * 1089 * Set the property default value. 1090 */ 1091 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value); 1092 1093 /** 1094 * object_property_find: 1095 * @obj: the object 1096 * @name: the name of the property 1097 * @errp: returns an error if this function fails 1098 * 1099 * Look up a property for an object and return its #ObjectProperty if found. 1100 */ 1101 ObjectProperty *object_property_find(Object *obj, const char *name, 1102 Error **errp); 1103 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, 1104 Error **errp); 1105 1106 typedef struct ObjectPropertyIterator { 1107 ObjectClass *nextclass; 1108 GHashTableIter iter; 1109 } ObjectPropertyIterator; 1110 1111 /** 1112 * object_property_iter_init: 1113 * @obj: the object 1114 * 1115 * Initializes an iterator for traversing all properties 1116 * registered against an object instance, its class and all parent classes. 1117 * 1118 * It is forbidden to modify the property list while iterating, 1119 * whether removing or adding properties. 1120 * 1121 * Typical usage pattern would be 1122 * 1123 * <example> 1124 * <title>Using object property iterators</title> 1125 * <programlisting> 1126 * ObjectProperty *prop; 1127 * ObjectPropertyIterator iter; 1128 * 1129 * object_property_iter_init(&iter, obj); 1130 * while ((prop = object_property_iter_next(&iter))) { 1131 * ... do something with prop ... 1132 * } 1133 * </programlisting> 1134 * </example> 1135 */ 1136 void object_property_iter_init(ObjectPropertyIterator *iter, 1137 Object *obj); 1138 1139 /** 1140 * object_class_property_iter_init: 1141 * @klass: the class 1142 * 1143 * Initializes an iterator for traversing all properties 1144 * registered against an object class and all parent classes. 1145 * 1146 * It is forbidden to modify the property list while iterating, 1147 * whether removing or adding properties. 1148 * 1149 * This can be used on abstract classes as it does not create a temporary 1150 * instance. 1151 */ 1152 void object_class_property_iter_init(ObjectPropertyIterator *iter, 1153 ObjectClass *klass); 1154 1155 /** 1156 * object_property_iter_next: 1157 * @iter: the iterator instance 1158 * 1159 * Return the next available property. If no further properties 1160 * are available, a %NULL value will be returned and the @iter 1161 * pointer should not be used again after this point without 1162 * re-initializing it. 1163 * 1164 * Returns: the next property, or %NULL when all properties 1165 * have been traversed. 1166 */ 1167 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter); 1168 1169 void object_unparent(Object *obj); 1170 1171 /** 1172 * object_property_get: 1173 * @obj: the object 1174 * @v: the visitor that will receive the property value. This should be an 1175 * Output visitor and the data will be written with @name as the name. 1176 * @name: the name of the property 1177 * @errp: returns an error if this function fails 1178 * 1179 * Reads a property from a object. 1180 */ 1181 void object_property_get(Object *obj, Visitor *v, const char *name, 1182 Error **errp); 1183 1184 /** 1185 * object_property_set_str: 1186 * @value: the value to be written to the property 1187 * @name: the name of the property 1188 * @errp: returns an error if this function fails 1189 * 1190 * Writes a string value to a property. 1191 */ 1192 void object_property_set_str(Object *obj, const char *value, 1193 const char *name, Error **errp); 1194 1195 /** 1196 * object_property_get_str: 1197 * @obj: the object 1198 * @name: the name of the property 1199 * @errp: returns an error if this function fails 1200 * 1201 * Returns: the value of the property, converted to a C string, or NULL if 1202 * an error occurs (including when the property value is not a string). 1203 * The caller should free the string. 1204 */ 1205 char *object_property_get_str(Object *obj, const char *name, 1206 Error **errp); 1207 1208 /** 1209 * object_property_set_link: 1210 * @value: the value to be written to the property 1211 * @name: the name of the property 1212 * @errp: returns an error if this function fails 1213 * 1214 * Writes an object's canonical path to a property. 1215 * 1216 * If the link property was created with 1217 * <code>OBJ_PROP_LINK_STRONG</code> bit, the old target object is 1218 * unreferenced, and a reference is added to the new target object. 1219 * 1220 */ 1221 void object_property_set_link(Object *obj, Object *value, 1222 const char *name, Error **errp); 1223 1224 /** 1225 * object_property_get_link: 1226 * @obj: the object 1227 * @name: the name of the property 1228 * @errp: returns an error if this function fails 1229 * 1230 * Returns: the value of the property, resolved from a path to an Object, 1231 * or NULL if an error occurs (including when the property value is not a 1232 * string or not a valid object path). 1233 */ 1234 Object *object_property_get_link(Object *obj, const char *name, 1235 Error **errp); 1236 1237 /** 1238 * object_property_set_bool: 1239 * @value: the value to be written to the property 1240 * @name: the name of the property 1241 * @errp: returns an error if this function fails 1242 * 1243 * Writes a bool value to a property. 1244 */ 1245 void object_property_set_bool(Object *obj, bool value, 1246 const char *name, Error **errp); 1247 1248 /** 1249 * object_property_get_bool: 1250 * @obj: the object 1251 * @name: the name of the property 1252 * @errp: returns an error if this function fails 1253 * 1254 * Returns: the value of the property, converted to a boolean, or NULL if 1255 * an error occurs (including when the property value is not a bool). 1256 */ 1257 bool object_property_get_bool(Object *obj, const char *name, 1258 Error **errp); 1259 1260 /** 1261 * object_property_set_int: 1262 * @value: the value to be written to the property 1263 * @name: the name of the property 1264 * @errp: returns an error if this function fails 1265 * 1266 * Writes an integer value to a property. 1267 */ 1268 void object_property_set_int(Object *obj, int64_t value, 1269 const char *name, Error **errp); 1270 1271 /** 1272 * object_property_get_int: 1273 * @obj: the object 1274 * @name: the name of the property 1275 * @errp: returns an error if this function fails 1276 * 1277 * Returns: the value of the property, converted to an integer, or negative if 1278 * an error occurs (including when the property value is not an integer). 1279 */ 1280 int64_t object_property_get_int(Object *obj, const char *name, 1281 Error **errp); 1282 1283 /** 1284 * object_property_set_uint: 1285 * @value: the value to be written to the property 1286 * @name: the name of the property 1287 * @errp: returns an error if this function fails 1288 * 1289 * Writes an unsigned integer value to a property. 1290 */ 1291 void object_property_set_uint(Object *obj, uint64_t value, 1292 const char *name, Error **errp); 1293 1294 /** 1295 * object_property_get_uint: 1296 * @obj: the object 1297 * @name: the name of the property 1298 * @errp: returns an error if this function fails 1299 * 1300 * Returns: the value of the property, converted to an unsigned integer, or 0 1301 * an error occurs (including when the property value is not an integer). 1302 */ 1303 uint64_t object_property_get_uint(Object *obj, const char *name, 1304 Error **errp); 1305 1306 /** 1307 * object_property_get_enum: 1308 * @obj: the object 1309 * @name: the name of the property 1310 * @typename: the name of the enum data type 1311 * @errp: returns an error if this function fails 1312 * 1313 * Returns: the value of the property, converted to an integer, or 1314 * undefined if an error occurs (including when the property value is not 1315 * an enum). 1316 */ 1317 int object_property_get_enum(Object *obj, const char *name, 1318 const char *typename, Error **errp); 1319 1320 /** 1321 * object_property_set: 1322 * @obj: the object 1323 * @v: the visitor that will be used to write the property value. This should 1324 * be an Input visitor and the data will be first read with @name as the 1325 * name and then written as the property value. 1326 * @name: the name of the property 1327 * @errp: returns an error if this function fails 1328 * 1329 * Writes a property to a object. 1330 */ 1331 void object_property_set(Object *obj, Visitor *v, const char *name, 1332 Error **errp); 1333 1334 /** 1335 * object_property_parse: 1336 * @obj: the object 1337 * @string: the string that will be used to parse the property value. 1338 * @name: the name of the property 1339 * @errp: returns an error if this function fails 1340 * 1341 * Parses a string and writes the result into a property of an object. 1342 */ 1343 void object_property_parse(Object *obj, const char *string, 1344 const char *name, Error **errp); 1345 1346 /** 1347 * object_property_print: 1348 * @obj: the object 1349 * @name: the name of the property 1350 * @human: if true, print for human consumption 1351 * @errp: returns an error if this function fails 1352 * 1353 * Returns a string representation of the value of the property. The 1354 * caller shall free the string. 1355 */ 1356 char *object_property_print(Object *obj, const char *name, bool human, 1357 Error **errp); 1358 1359 /** 1360 * object_property_get_type: 1361 * @obj: the object 1362 * @name: the name of the property 1363 * @errp: returns an error if this function fails 1364 * 1365 * Returns: The type name of the property. 1366 */ 1367 const char *object_property_get_type(Object *obj, const char *name, 1368 Error **errp); 1369 1370 /** 1371 * object_get_root: 1372 * 1373 * Returns: the root object of the composition tree 1374 */ 1375 Object *object_get_root(void); 1376 1377 1378 /** 1379 * object_get_objects_root: 1380 * 1381 * Get the container object that holds user created 1382 * object instances. This is the object at path 1383 * "/objects" 1384 * 1385 * Returns: the user object container 1386 */ 1387 Object *object_get_objects_root(void); 1388 1389 /** 1390 * object_get_internal_root: 1391 * 1392 * Get the container object that holds internally used object 1393 * instances. Any object which is put into this container must not be 1394 * user visible, and it will not be exposed in the QOM tree. 1395 * 1396 * Returns: the internal object container 1397 */ 1398 Object *object_get_internal_root(void); 1399 1400 /** 1401 * object_get_canonical_path_component: 1402 * 1403 * Returns: The final component in the object's canonical path. The canonical 1404 * path is the path within the composition tree starting from the root. 1405 * %NULL if the object doesn't have a parent (and thus a canonical path). 1406 */ 1407 char *object_get_canonical_path_component(Object *obj); 1408 1409 /** 1410 * object_get_canonical_path: 1411 * 1412 * Returns: The canonical path for a object. This is the path within the 1413 * composition tree starting from the root. 1414 */ 1415 char *object_get_canonical_path(Object *obj); 1416 1417 /** 1418 * object_resolve_path: 1419 * @path: the path to resolve 1420 * @ambiguous: returns true if the path resolution failed because of an 1421 * ambiguous match 1422 * 1423 * There are two types of supported paths--absolute paths and partial paths. 1424 * 1425 * Absolute paths are derived from the root object and can follow child<> or 1426 * link<> properties. Since they can follow link<> properties, they can be 1427 * arbitrarily long. Absolute paths look like absolute filenames and are 1428 * prefixed with a leading slash. 1429 * 1430 * Partial paths look like relative filenames. They do not begin with a 1431 * prefix. The matching rules for partial paths are subtle but designed to make 1432 * specifying objects easy. At each level of the composition tree, the partial 1433 * path is matched as an absolute path. The first match is not returned. At 1434 * least two matches are searched for. A successful result is only returned if 1435 * only one match is found. If more than one match is found, a flag is 1436 * returned to indicate that the match was ambiguous. 1437 * 1438 * Returns: The matched object or NULL on path lookup failure. 1439 */ 1440 Object *object_resolve_path(const char *path, bool *ambiguous); 1441 1442 /** 1443 * object_resolve_path_type: 1444 * @path: the path to resolve 1445 * @typename: the type to look for. 1446 * @ambiguous: returns true if the path resolution failed because of an 1447 * ambiguous match 1448 * 1449 * This is similar to object_resolve_path. However, when looking for a 1450 * partial path only matches that implement the given type are considered. 1451 * This restricts the search and avoids spuriously flagging matches as 1452 * ambiguous. 1453 * 1454 * For both partial and absolute paths, the return value goes through 1455 * a dynamic cast to @typename. This is important if either the link, 1456 * or the typename itself are of interface types. 1457 * 1458 * Returns: The matched object or NULL on path lookup failure. 1459 */ 1460 Object *object_resolve_path_type(const char *path, const char *typename, 1461 bool *ambiguous); 1462 1463 /** 1464 * object_resolve_path_component: 1465 * @parent: the object in which to resolve the path 1466 * @part: the component to resolve. 1467 * 1468 * This is similar to object_resolve_path with an absolute path, but it 1469 * only resolves one element (@part) and takes the others from @parent. 1470 * 1471 * Returns: The resolved object or NULL on path lookup failure. 1472 */ 1473 Object *object_resolve_path_component(Object *parent, const char *part); 1474 1475 /** 1476 * object_property_add_child: 1477 * @obj: the object to add a property to 1478 * @name: the name of the property 1479 * @child: the child object 1480 * 1481 * Child properties form the composition tree. All objects need to be a child 1482 * of another object. Objects can only be a child of one object. 1483 * 1484 * There is no way for a child to determine what its parent is. It is not 1485 * a bidirectional relationship. This is by design. 1486 * 1487 * The value of a child property as a C string will be the child object's 1488 * canonical path. It can be retrieved using object_property_get_str(). 1489 * The child object itself can be retrieved using object_property_get_link(). 1490 * 1491 * Returns: The newly added property on success, or %NULL on failure. 1492 */ 1493 ObjectProperty *object_property_add_child(Object *obj, const char *name, 1494 Object *child); 1495 1496 typedef enum { 1497 /* Unref the link pointer when the property is deleted */ 1498 OBJ_PROP_LINK_STRONG = 0x1, 1499 1500 /* private */ 1501 OBJ_PROP_LINK_DIRECT = 0x2, 1502 OBJ_PROP_LINK_CLASS = 0x4, 1503 } ObjectPropertyLinkFlags; 1504 1505 /** 1506 * object_property_allow_set_link: 1507 * 1508 * The default implementation of the object_property_add_link() check() 1509 * callback function. It allows the link property to be set and never returns 1510 * an error. 1511 */ 1512 void object_property_allow_set_link(const Object *, const char *, 1513 Object *, Error **); 1514 1515 /** 1516 * object_property_add_link: 1517 * @obj: the object to add a property to 1518 * @name: the name of the property 1519 * @type: the qobj type of the link 1520 * @targetp: a pointer to where the link object reference is stored 1521 * @check: callback to veto setting or NULL if the property is read-only 1522 * @flags: additional options for the link 1523 * 1524 * Links establish relationships between objects. Links are unidirectional 1525 * although two links can be combined to form a bidirectional relationship 1526 * between objects. 1527 * 1528 * Links form the graph in the object model. 1529 * 1530 * The <code>@check()</code> callback is invoked when 1531 * object_property_set_link() is called and can raise an error to prevent the 1532 * link being set. If <code>@check</code> is NULL, the property is read-only 1533 * and cannot be set. 1534 * 1535 * Ownership of the pointer that @child points to is transferred to the 1536 * link property. The reference count for <code>*@child</code> is 1537 * managed by the property from after the function returns till the 1538 * property is deleted with object_property_del(). If the 1539 * <code>@flags</code> <code>OBJ_PROP_LINK_STRONG</code> bit is set, 1540 * the reference count is decremented when the property is deleted or 1541 * modified. 1542 * 1543 * Returns: The newly added property on success, or %NULL on failure. 1544 */ 1545 ObjectProperty *object_property_add_link(Object *obj, const char *name, 1546 const char *type, Object **targetp, 1547 void (*check)(const Object *obj, const char *name, 1548 Object *val, Error **errp), 1549 ObjectPropertyLinkFlags flags); 1550 1551 ObjectProperty *object_class_property_add_link(ObjectClass *oc, 1552 const char *name, 1553 const char *type, ptrdiff_t offset, 1554 void (*check)(const Object *obj, const char *name, 1555 Object *val, Error **errp), 1556 ObjectPropertyLinkFlags flags); 1557 1558 /** 1559 * object_property_add_str: 1560 * @obj: the object to add a property to 1561 * @name: the name of the property 1562 * @get: the getter or NULL if the property is write-only. This function must 1563 * return a string to be freed by g_free(). 1564 * @set: the setter or NULL if the property is read-only 1565 * 1566 * Add a string property using getters/setters. This function will add a 1567 * property of type 'string'. 1568 * 1569 * Returns: The newly added property on success, or %NULL on failure. 1570 */ 1571 ObjectProperty *object_property_add_str(Object *obj, const char *name, 1572 char *(*get)(Object *, Error **), 1573 void (*set)(Object *, const char *, Error **)); 1574 1575 ObjectProperty *object_class_property_add_str(ObjectClass *klass, 1576 const char *name, 1577 char *(*get)(Object *, Error **), 1578 void (*set)(Object *, const char *, 1579 Error **)); 1580 1581 /** 1582 * object_property_add_bool: 1583 * @obj: the object to add a property to 1584 * @name: the name of the property 1585 * @get: the getter or NULL if the property is write-only. 1586 * @set: the setter or NULL if the property is read-only 1587 * 1588 * Add a bool property using getters/setters. This function will add a 1589 * property of type 'bool'. 1590 * 1591 * Returns: The newly added property on success, or %NULL on failure. 1592 */ 1593 ObjectProperty *object_property_add_bool(Object *obj, const char *name, 1594 bool (*get)(Object *, Error **), 1595 void (*set)(Object *, bool, Error **)); 1596 1597 ObjectProperty *object_class_property_add_bool(ObjectClass *klass, 1598 const char *name, 1599 bool (*get)(Object *, Error **), 1600 void (*set)(Object *, bool, Error **)); 1601 1602 /** 1603 * object_property_add_enum: 1604 * @obj: the object to add a property to 1605 * @name: the name of the property 1606 * @typename: the name of the enum data type 1607 * @get: the getter or %NULL if the property is write-only. 1608 * @set: the setter or %NULL if the property is read-only 1609 * 1610 * Add an enum property using getters/setters. This function will add a 1611 * property of type '@typename'. 1612 * 1613 * Returns: The newly added property on success, or %NULL on failure. 1614 */ 1615 ObjectProperty *object_property_add_enum(Object *obj, const char *name, 1616 const char *typename, 1617 const QEnumLookup *lookup, 1618 int (*get)(Object *, Error **), 1619 void (*set)(Object *, int, Error **)); 1620 1621 ObjectProperty *object_class_property_add_enum(ObjectClass *klass, 1622 const char *name, 1623 const char *typename, 1624 const QEnumLookup *lookup, 1625 int (*get)(Object *, Error **), 1626 void (*set)(Object *, int, Error **)); 1627 1628 /** 1629 * object_property_add_tm: 1630 * @obj: the object to add a property to 1631 * @name: the name of the property 1632 * @get: the getter or NULL if the property is write-only. 1633 * 1634 * Add a read-only struct tm valued property using a getter function. 1635 * This function will add a property of type 'struct tm'. 1636 * 1637 * Returns: The newly added property on success, or %NULL on failure. 1638 */ 1639 ObjectProperty *object_property_add_tm(Object *obj, const char *name, 1640 void (*get)(Object *, struct tm *, Error **)); 1641 1642 ObjectProperty *object_class_property_add_tm(ObjectClass *klass, 1643 const char *name, 1644 void (*get)(Object *, struct tm *, Error **)); 1645 1646 typedef enum { 1647 /* Automatically add a getter to the property */ 1648 OBJ_PROP_FLAG_READ = 1 << 0, 1649 /* Automatically add a setter to the property */ 1650 OBJ_PROP_FLAG_WRITE = 1 << 1, 1651 /* Automatically add a getter and a setter to the property */ 1652 OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE), 1653 } ObjectPropertyFlags; 1654 1655 /** 1656 * object_property_add_uint8_ptr: 1657 * @obj: the object to add a property to 1658 * @name: the name of the property 1659 * @v: pointer to value 1660 * @flags: bitwise-or'd ObjectPropertyFlags 1661 * 1662 * Add an integer property in memory. This function will add a 1663 * property of type 'uint8'. 1664 * 1665 * Returns: The newly added property on success, or %NULL on failure. 1666 */ 1667 ObjectProperty *object_property_add_uint8_ptr(Object *obj, const char *name, 1668 const uint8_t *v, 1669 ObjectPropertyFlags flags); 1670 1671 ObjectProperty *object_class_property_add_uint8_ptr(ObjectClass *klass, 1672 const char *name, 1673 const uint8_t *v, 1674 ObjectPropertyFlags flags); 1675 1676 /** 1677 * object_property_add_uint16_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 * 1683 * Add an integer property in memory. This function will add a 1684 * property of type 'uint16'. 1685 * 1686 * Returns: The newly added property on success, or %NULL on failure. 1687 */ 1688 ObjectProperty *object_property_add_uint16_ptr(Object *obj, const char *name, 1689 const uint16_t *v, 1690 ObjectPropertyFlags flags); 1691 1692 ObjectProperty *object_class_property_add_uint16_ptr(ObjectClass *klass, 1693 const char *name, 1694 const uint16_t *v, 1695 ObjectPropertyFlags flags); 1696 1697 /** 1698 * object_property_add_uint32_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 * 1704 * Add an integer property in memory. This function will add a 1705 * property of type 'uint32'. 1706 * 1707 * Returns: The newly added property on success, or %NULL on failure. 1708 */ 1709 ObjectProperty *object_property_add_uint32_ptr(Object *obj, const char *name, 1710 const uint32_t *v, 1711 ObjectPropertyFlags flags); 1712 1713 ObjectProperty *object_class_property_add_uint32_ptr(ObjectClass *klass, 1714 const char *name, 1715 const uint32_t *v, 1716 ObjectPropertyFlags flags); 1717 1718 /** 1719 * object_property_add_uint64_ptr: 1720 * @obj: the object to add a property to 1721 * @name: the name of the property 1722 * @v: pointer to value 1723 * @flags: bitwise-or'd ObjectPropertyFlags 1724 * 1725 * Add an integer property in memory. This function will add a 1726 * property of type 'uint64'. 1727 * 1728 * Returns: The newly added property on success, or %NULL on failure. 1729 */ 1730 ObjectProperty *object_property_add_uint64_ptr(Object *obj, const char *name, 1731 const uint64_t *v, 1732 ObjectPropertyFlags flags); 1733 1734 ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass, 1735 const char *name, 1736 const uint64_t *v, 1737 ObjectPropertyFlags flags); 1738 1739 /** 1740 * object_property_add_alias: 1741 * @obj: the object to add a property to 1742 * @name: the name of the property 1743 * @target_obj: the object to forward property access to 1744 * @target_name: the name of the property on the forwarded object 1745 * 1746 * Add an alias for a property on an object. This function will add a property 1747 * of the same type as the forwarded property. 1748 * 1749 * The caller must ensure that <code>@target_obj</code> stays alive as long as 1750 * this property exists. In the case of a child object or an alias on the same 1751 * object this will be the case. For aliases to other objects the caller is 1752 * responsible for taking a reference. 1753 * 1754 * Returns: The newly added property on success, or %NULL on failure. 1755 */ 1756 ObjectProperty *object_property_add_alias(Object *obj, const char *name, 1757 Object *target_obj, const char *target_name); 1758 1759 /** 1760 * object_property_add_const_link: 1761 * @obj: the object to add a property to 1762 * @name: the name of the property 1763 * @target: the object to be referred by the link 1764 * 1765 * Add an unmodifiable link for a property on an object. This function will 1766 * add a property of type link<TYPE> where TYPE is the type of @target. 1767 * 1768 * The caller must ensure that @target stays alive as long as 1769 * this property exists. In the case @target is a child of @obj, 1770 * this will be the case. Otherwise, the caller is responsible for 1771 * taking a reference. 1772 * 1773 * Returns: The newly added property on success, or %NULL on failure. 1774 */ 1775 ObjectProperty *object_property_add_const_link(Object *obj, const char *name, 1776 Object *target); 1777 1778 /** 1779 * object_property_set_description: 1780 * @obj: the object owning the property 1781 * @name: the name of the property 1782 * @description: the description of the property on the object 1783 * 1784 * Set an object property's description. 1785 * 1786 */ 1787 void object_property_set_description(Object *obj, const char *name, 1788 const char *description); 1789 void object_class_property_set_description(ObjectClass *klass, const char *name, 1790 const char *description); 1791 1792 /** 1793 * object_child_foreach: 1794 * @obj: the object whose children will be navigated 1795 * @fn: the iterator function to be called 1796 * @opaque: an opaque value that will be passed to the iterator 1797 * 1798 * Call @fn passing each child of @obj and @opaque to it, until @fn returns 1799 * non-zero. 1800 * 1801 * It is forbidden to add or remove children from @obj from the @fn 1802 * callback. 1803 * 1804 * Returns: The last value returned by @fn, or 0 if there is no child. 1805 */ 1806 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), 1807 void *opaque); 1808 1809 /** 1810 * object_child_foreach_recursive: 1811 * @obj: the object whose children will be navigated 1812 * @fn: the iterator function to be called 1813 * @opaque: an opaque value that will be passed to the iterator 1814 * 1815 * Call @fn passing each child of @obj and @opaque to it, until @fn returns 1816 * non-zero. Calls recursively, all child nodes of @obj will also be passed 1817 * all the way down to the leaf nodes of the tree. Depth first ordering. 1818 * 1819 * It is forbidden to add or remove children from @obj (or its 1820 * child nodes) from the @fn callback. 1821 * 1822 * Returns: The last value returned by @fn, or 0 if there is no child. 1823 */ 1824 int object_child_foreach_recursive(Object *obj, 1825 int (*fn)(Object *child, void *opaque), 1826 void *opaque); 1827 /** 1828 * container_get: 1829 * @root: root of the #path, e.g., object_get_root() 1830 * @path: path to the container 1831 * 1832 * Return a container object whose path is @path. Create more containers 1833 * along the path if necessary. 1834 * 1835 * Returns: the container object. 1836 */ 1837 Object *container_get(Object *root, const char *path); 1838 1839 /** 1840 * object_type_get_instance_size: 1841 * @typename: Name of the Type whose instance_size is required 1842 * 1843 * Returns the instance_size of the given @typename. 1844 */ 1845 size_t object_type_get_instance_size(const char *typename); 1846 1847 /** 1848 * object_property_help: 1849 * @name: the name of the property 1850 * @type: the type of the property 1851 * @defval: the default value 1852 * @description: description of the property 1853 * 1854 * Returns: a user-friendly formatted string describing the property 1855 * for help purposes. 1856 */ 1857 char *object_property_help(const char *name, const char *type, 1858 QObject *defval, const char *description); 1859 1860 G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref) 1861 1862 #endif 1863