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