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