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