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