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