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