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 * @ModuleObjName: the object name with initial capitalization 559 * @module_obj_name: the object name in lowercase with underscore separators 560 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 561 * 562 * This macro is typically used in a header file, and will: 563 * 564 * - create the typedefs for the object and class structs 565 * - register the type for use with g_autoptr 566 * - provide three standard type cast functions 567 * 568 * The object struct and class struct need to be declared manually. 569 */ 570 #define OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \ 571 typedef struct ModuleObjName ModuleObjName; \ 572 typedef struct ModuleObjName##Class ModuleObjName##Class; \ 573 \ 574 G_DEFINE_AUTOPTR_CLEANUP_FUNC(ModuleObjName, object_unref) \ 575 \ 576 static inline G_GNUC_UNUSED ModuleObjName##Class * \ 577 MODULE_OBJ_NAME##_GET_CLASS(void *obj) \ 578 { return OBJECT_GET_CLASS(ModuleObjName##Class, obj, \ 579 TYPE_##MODULE_OBJ_NAME); } \ 580 \ 581 static inline G_GNUC_UNUSED ModuleObjName##Class * \ 582 MODULE_OBJ_NAME##_CLASS(void *klass) \ 583 { return OBJECT_CLASS_CHECK(ModuleObjName##Class, klass, \ 584 TYPE_##MODULE_OBJ_NAME); } \ 585 \ 586 static inline G_GNUC_UNUSED ModuleObjName * \ 587 MODULE_OBJ_NAME(void *obj) \ 588 { return OBJECT_CHECK(ModuleObjName, obj, \ 589 TYPE_##MODULE_OBJ_NAME); } 590 591 /** 592 * OBJECT_DECLARE_SIMPLE_TYPE: 593 * @ModuleObjName: the object name with initial caps 594 * @module_obj_name: the object name in lowercase with underscore separators 595 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 596 * @ParentModuleObjName: the parent object name with initial caps 597 * 598 * This does the same as OBJECT_DECLARE_TYPE(), but also declares 599 * the class struct, thus only the object struct needs to be declare 600 * manually. 601 * 602 * This macro should be used unless the class struct needs to have 603 * virtual methods declared. 604 */ 605 #define OBJECT_DECLARE_SIMPLE_TYPE(ModuleObjName, module_obj_name, \ 606 MODULE_OBJ_NAME, ParentModuleObjName) \ 607 OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \ 608 struct ModuleObjName##Class { ParentModuleObjName##Class parent_class; }; 609 610 611 /** 612 * OBJECT_DEFINE_TYPE_EXTENDED: 613 * @ModuleObjName: the object name with initial caps 614 * @module_obj_name: the object name in lowercase with underscore separators 615 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 616 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 617 * separators 618 * @ABSTRACT: boolean flag to indicate whether the object can be instantiated 619 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces 620 * 621 * This macro is typically used in a source file, and will: 622 * 623 * - declare prototypes for _finalize, _class_init and _init methods 624 * - declare the TypeInfo struct instance 625 * - provide the constructor to register the type 626 * 627 * After using this macro, implementations of the _finalize, _class_init, 628 * and _init methods need to be written. Any of these can be zero-line 629 * no-op impls if no special logic is required for a given type. 630 * 631 * This macro should rarely be used, instead one of the more specialized 632 * macros is usually a better choice. 633 */ 634 #define OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 635 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 636 ABSTRACT, ...) \ 637 static void \ 638 module_obj_name##_finalize(Object *obj); \ 639 static void \ 640 module_obj_name##_class_init(ObjectClass *oc, void *data); \ 641 static void \ 642 module_obj_name##_init(Object *obj); \ 643 \ 644 static const TypeInfo module_obj_name##_info = { \ 645 .parent = TYPE_##PARENT_MODULE_OBJ_NAME, \ 646 .name = TYPE_##MODULE_OBJ_NAME, \ 647 .instance_size = sizeof(ModuleObjName), \ 648 .instance_init = module_obj_name##_init, \ 649 .instance_finalize = module_obj_name##_finalize, \ 650 .class_size = sizeof(ModuleObjName##Class), \ 651 .class_init = module_obj_name##_class_init, \ 652 .abstract = ABSTRACT, \ 653 .interfaces = (InterfaceInfo[]) { __VA_ARGS__ } , \ 654 }; \ 655 \ 656 static void \ 657 module_obj_name##_register_types(void) \ 658 { \ 659 type_register_static(&module_obj_name##_info); \ 660 } \ 661 type_init(module_obj_name##_register_types); 662 663 /** 664 * OBJECT_DEFINE_TYPE: 665 * @ModuleObjName: the object name with initial caps 666 * @module_obj_name: the object name in lowercase with underscore separators 667 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 668 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 669 * separators 670 * 671 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable 672 * for the common case of a non-abstract type, without any interfaces. 673 */ 674 #define OBJECT_DEFINE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME, \ 675 PARENT_MODULE_OBJ_NAME) \ 676 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 677 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 678 false, { NULL }) 679 680 /** 681 * OBJECT_DEFINE_TYPE_WITH_INTERFACES: 682 * @ModuleObjName: the object name with initial caps 683 * @module_obj_name: the object name in lowercase with underscore separators 684 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 685 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 686 * separators 687 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces 688 * 689 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable 690 * for the common case of a non-abstract type, with one or more implemented 691 * interfaces. 692 * 693 * Note when passing the list of interfaces, be sure to include the final 694 * NULL entry, e.g. { TYPE_USER_CREATABLE }, { NULL } 695 */ 696 #define OBJECT_DEFINE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \ 697 MODULE_OBJ_NAME, \ 698 PARENT_MODULE_OBJ_NAME, ...) \ 699 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 700 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 701 false, __VA_ARGS__) 702 703 /** 704 * OBJECT_DEFINE_ABSTRACT_TYPE: 705 * @ModuleObjName: the object name with initial caps 706 * @module_obj_name: the object name in lowercase with underscore separators 707 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators 708 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore 709 * separators 710 * 711 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable 712 * for defining an abstract type, without any interfaces. 713 */ 714 #define OBJECT_DEFINE_ABSTRACT_TYPE(ModuleObjName, module_obj_name, \ 715 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \ 716 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \ 717 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \ 718 true, { NULL }) 719 720 /** 721 * TypeInfo: 722 * @name: The name of the type. 723 * @parent: The name of the parent type. 724 * @instance_size: The size of the object (derivative of #Object). If 725 * @instance_size is 0, then the size of the object will be the size of the 726 * parent object. 727 * @instance_init: This function is called to initialize an object. The parent 728 * class will have already been initialized so the type is only responsible 729 * for initializing its own members. 730 * @instance_post_init: This function is called to finish initialization of 731 * an object, after all @instance_init functions were called. 732 * @instance_finalize: This function is called during object destruction. This 733 * is called before the parent @instance_finalize function has been called. 734 * An object should only free the members that are unique to its type in this 735 * function. 736 * @abstract: If this field is true, then the class is considered abstract and 737 * cannot be directly instantiated. 738 * @class_size: The size of the class object (derivative of #ObjectClass) 739 * for this object. If @class_size is 0, then the size of the class will be 740 * assumed to be the size of the parent class. This allows a type to avoid 741 * implementing an explicit class type if they are not adding additional 742 * virtual functions. 743 * @class_init: This function is called after all parent class initialization 744 * has occurred to allow a class to set its default virtual method pointers. 745 * This is also the function to use to override virtual methods from a parent 746 * class. 747 * @class_base_init: This function is called for all base classes after all 748 * parent class initialization has occurred, but before the class itself 749 * is initialized. This is the function to use to undo the effects of 750 * memcpy from the parent class to the descendants. 751 * @class_data: Data to pass to the @class_init, 752 * @class_base_init. This can be useful when building dynamic 753 * classes. 754 * @interfaces: The list of interfaces associated with this type. This 755 * should point to a static array that's terminated with a zero filled 756 * element. 757 */ 758 struct TypeInfo 759 { 760 const char *name; 761 const char *parent; 762 763 size_t instance_size; 764 void (*instance_init)(Object *obj); 765 void (*instance_post_init)(Object *obj); 766 void (*instance_finalize)(Object *obj); 767 768 bool abstract; 769 size_t class_size; 770 771 void (*class_init)(ObjectClass *klass, void *data); 772 void (*class_base_init)(ObjectClass *klass, void *data); 773 void *class_data; 774 775 InterfaceInfo *interfaces; 776 }; 777 778 /** 779 * OBJECT: 780 * @obj: A derivative of #Object 781 * 782 * Converts an object to a #Object. Since all objects are #Objects, 783 * this function will always succeed. 784 */ 785 #define OBJECT(obj) \ 786 ((Object *)(obj)) 787 788 /** 789 * OBJECT_CLASS: 790 * @class: A derivative of #ObjectClass. 791 * 792 * Converts a class to an #ObjectClass. Since all objects are #Objects, 793 * this function will always succeed. 794 */ 795 #define OBJECT_CLASS(class) \ 796 ((ObjectClass *)(class)) 797 798 /** 799 * OBJECT_CHECK: 800 * @type: The C type to use for the return value. 801 * @obj: A derivative of @type to cast. 802 * @name: The QOM typename of @type 803 * 804 * A type safe version of @object_dynamic_cast_assert. Typically each class 805 * will define a macro based on this type to perform type safe dynamic_casts to 806 * this object type. 807 * 808 * If an invalid object is passed to this function, a run time assert will be 809 * generated. 810 */ 811 #define OBJECT_CHECK(type, obj, name) \ 812 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \ 813 __FILE__, __LINE__, __func__)) 814 815 /** 816 * OBJECT_CLASS_CHECK: 817 * @class_type: The C type to use for the return value. 818 * @class: A derivative class of @class_type to cast. 819 * @name: the QOM typename of @class_type. 820 * 821 * A type safe version of @object_class_dynamic_cast_assert. This macro is 822 * typically wrapped by each type to perform type safe casts of a class to a 823 * specific class type. 824 */ 825 #define OBJECT_CLASS_CHECK(class_type, class, name) \ 826 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \ 827 __FILE__, __LINE__, __func__)) 828 829 /** 830 * OBJECT_GET_CLASS: 831 * @class: The C type to use for the return value. 832 * @obj: The object to obtain the class for. 833 * @name: The QOM typename of @obj. 834 * 835 * This function will return a specific class for a given object. Its generally 836 * used by each type to provide a type safe macro to get a specific class type 837 * from an object. 838 */ 839 #define OBJECT_GET_CLASS(class, obj, name) \ 840 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name) 841 842 /** 843 * InterfaceInfo: 844 * @type: The name of the interface. 845 * 846 * The information associated with an interface. 847 */ 848 struct InterfaceInfo { 849 const char *type; 850 }; 851 852 /** 853 * InterfaceClass: 854 * @parent_class: the base class 855 * 856 * The class for all interfaces. Subclasses of this class should only add 857 * virtual methods. 858 */ 859 struct InterfaceClass 860 { 861 ObjectClass parent_class; 862 /*< private >*/ 863 ObjectClass *concrete_class; 864 Type interface_type; 865 }; 866 867 #define TYPE_INTERFACE "interface" 868 869 /** 870 * INTERFACE_CLASS: 871 * @klass: class to cast from 872 * Returns: An #InterfaceClass or raise an error if cast is invalid 873 */ 874 #define INTERFACE_CLASS(klass) \ 875 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE) 876 877 /** 878 * INTERFACE_CHECK: 879 * @interface: the type to return 880 * @obj: the object to convert to an interface 881 * @name: the interface type name 882 * 883 * Returns: @obj casted to @interface if cast is valid, otherwise raise error. 884 */ 885 #define INTERFACE_CHECK(interface, obj, name) \ 886 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \ 887 __FILE__, __LINE__, __func__)) 888 889 /** 890 * object_new_with_class: 891 * @klass: The class to instantiate. 892 * 893 * This function will initialize a new object using heap allocated memory. 894 * The returned object has a reference count of 1, and will be freed when 895 * the last reference is dropped. 896 * 897 * Returns: The newly allocated and instantiated object. 898 */ 899 Object *object_new_with_class(ObjectClass *klass); 900 901 /** 902 * object_new: 903 * @typename: The name of the type of the object to instantiate. 904 * 905 * This function will initialize a new object using heap allocated memory. 906 * The returned object has a reference count of 1, and will be freed when 907 * the last reference is dropped. 908 * 909 * Returns: The newly allocated and instantiated object. 910 */ 911 Object *object_new(const char *typename); 912 913 /** 914 * object_new_with_props: 915 * @typename: The name of the type of the object to instantiate. 916 * @parent: the parent object 917 * @id: The unique ID of the object 918 * @errp: pointer to error object 919 * @...: list of property names and values 920 * 921 * This function will initialize a new object using heap allocated memory. 922 * The returned object has a reference count of 1, and will be freed when 923 * the last reference is dropped. 924 * 925 * The @id parameter will be used when registering the object as a 926 * child of @parent in the composition tree. 927 * 928 * The variadic parameters are a list of pairs of (propname, propvalue) 929 * strings. The propname of %NULL indicates the end of the property 930 * list. If the object implements the user creatable interface, the 931 * object will be marked complete once all the properties have been 932 * processed. 933 * 934 * <example> 935 * <title>Creating an object with properties</title> 936 * <programlisting> 937 * Error *err = NULL; 938 * Object *obj; 939 * 940 * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE, 941 * object_get_objects_root(), 942 * "hostmem0", 943 * &err, 944 * "share", "yes", 945 * "mem-path", "/dev/shm/somefile", 946 * "prealloc", "yes", 947 * "size", "1048576", 948 * NULL); 949 * 950 * if (!obj) { 951 * error_reportf_err(err, "Cannot create memory backend: "); 952 * } 953 * </programlisting> 954 * </example> 955 * 956 * The returned object will have one stable reference maintained 957 * for as long as it is present in the object hierarchy. 958 * 959 * Returns: The newly allocated, instantiated & initialized object. 960 */ 961 Object *object_new_with_props(const char *typename, 962 Object *parent, 963 const char *id, 964 Error **errp, 965 ...) QEMU_SENTINEL; 966 967 /** 968 * object_new_with_propv: 969 * @typename: The name of the type of the object to instantiate. 970 * @parent: the parent object 971 * @id: The unique ID of the object 972 * @errp: pointer to error object 973 * @vargs: list of property names and values 974 * 975 * See object_new_with_props() for documentation. 976 */ 977 Object *object_new_with_propv(const char *typename, 978 Object *parent, 979 const char *id, 980 Error **errp, 981 va_list vargs); 982 983 bool object_apply_global_props(Object *obj, const GPtrArray *props, 984 Error **errp); 985 void object_set_machine_compat_props(GPtrArray *compat_props); 986 void object_set_accelerator_compat_props(GPtrArray *compat_props); 987 void object_register_sugar_prop(const char *driver, const char *prop, const char *value); 988 void object_apply_compat_props(Object *obj); 989 990 /** 991 * object_set_props: 992 * @obj: the object instance to set properties on 993 * @errp: pointer to error object 994 * @...: list of property names and values 995 * 996 * This function will set a list of properties on an existing object 997 * instance. 998 * 999 * The variadic parameters are a list of pairs of (propname, propvalue) 1000 * strings. The propname of %NULL indicates the end of the property 1001 * list. 1002 * 1003 * <example> 1004 * <title>Update an object's properties</title> 1005 * <programlisting> 1006 * Error *err = NULL; 1007 * Object *obj = ...get / create object...; 1008 * 1009 * if (!object_set_props(obj, 1010 * &err, 1011 * "share", "yes", 1012 * "mem-path", "/dev/shm/somefile", 1013 * "prealloc", "yes", 1014 * "size", "1048576", 1015 * NULL)) { 1016 * error_reportf_err(err, "Cannot set properties: "); 1017 * } 1018 * </programlisting> 1019 * </example> 1020 * 1021 * The returned object will have one stable reference maintained 1022 * for as long as it is present in the object hierarchy. 1023 * 1024 * Returns: %true on success, %false on error. 1025 */ 1026 bool object_set_props(Object *obj, Error **errp, ...) QEMU_SENTINEL; 1027 1028 /** 1029 * object_set_propv: 1030 * @obj: the object instance to set properties on 1031 * @errp: pointer to error object 1032 * @vargs: list of property names and values 1033 * 1034 * See object_set_props() for documentation. 1035 * 1036 * Returns: %true on success, %false on error. 1037 */ 1038 bool object_set_propv(Object *obj, Error **errp, va_list vargs); 1039 1040 /** 1041 * object_initialize: 1042 * @obj: A pointer to the memory to be used for the object. 1043 * @size: The maximum size available at @obj for the object. 1044 * @typename: The name of the type of the object to instantiate. 1045 * 1046 * This function will initialize an object. The memory for the object should 1047 * have already been allocated. The returned object has a reference count of 1, 1048 * and will be finalized when the last reference is dropped. 1049 */ 1050 void object_initialize(void *obj, size_t size, const char *typename); 1051 1052 /** 1053 * object_initialize_child_with_props: 1054 * @parentobj: The parent object to add a property to 1055 * @propname: The name of the property 1056 * @childobj: A pointer to the memory to be used for the object. 1057 * @size: The maximum size available at @childobj for the object. 1058 * @type: The name of the type of the object to instantiate. 1059 * @errp: If an error occurs, a pointer to an area to store the error 1060 * @...: list of property names and values 1061 * 1062 * This function will initialize an object. The memory for the object should 1063 * have already been allocated. The object will then be added as child property 1064 * to a parent with object_property_add_child() function. The returned object 1065 * has a reference count of 1 (for the "child<...>" property from the parent), 1066 * so the object will be finalized automatically when the parent gets removed. 1067 * 1068 * The variadic parameters are a list of pairs of (propname, propvalue) 1069 * strings. The propname of %NULL indicates the end of the property list. 1070 * If the object implements the user creatable interface, the object will 1071 * be marked complete once all the properties have been processed. 1072 * 1073 * Returns: %true on success, %false on failure. 1074 */ 1075 bool object_initialize_child_with_props(Object *parentobj, 1076 const char *propname, 1077 void *childobj, size_t size, const char *type, 1078 Error **errp, ...) QEMU_SENTINEL; 1079 1080 /** 1081 * object_initialize_child_with_propsv: 1082 * @parentobj: The parent object to add a property to 1083 * @propname: The name of the property 1084 * @childobj: A pointer to the memory to be used for the object. 1085 * @size: The maximum size available at @childobj for the object. 1086 * @type: The name of the type of the object to instantiate. 1087 * @errp: If an error occurs, a pointer to an area to store the error 1088 * @vargs: list of property names and values 1089 * 1090 * See object_initialize_child() for documentation. 1091 * 1092 * Returns: %true on success, %false on failure. 1093 */ 1094 bool object_initialize_child_with_propsv(Object *parentobj, 1095 const char *propname, 1096 void *childobj, size_t size, const char *type, 1097 Error **errp, va_list vargs); 1098 1099 /** 1100 * object_initialize_child: 1101 * @parent: The parent object to add a property to 1102 * @propname: The name of the property 1103 * @child: A precisely typed pointer to the memory to be used for the 1104 * object. 1105 * @type: The name of the type of the object to instantiate. 1106 * 1107 * This is like 1108 * object_initialize_child_with_props(parent, propname, 1109 * child, sizeof(*child), type, 1110 * &error_abort, NULL) 1111 */ 1112 #define object_initialize_child(parent, propname, child, type) \ 1113 object_initialize_child_internal((parent), (propname), \ 1114 (child), sizeof(*(child)), (type)) 1115 void object_initialize_child_internal(Object *parent, const char *propname, 1116 void *child, size_t size, 1117 const char *type); 1118 1119 /** 1120 * object_dynamic_cast: 1121 * @obj: The object to cast. 1122 * @typename: The @typename to cast to. 1123 * 1124 * This function will determine if @obj is-a @typename. @obj can refer to an 1125 * object or an interface associated with an object. 1126 * 1127 * Returns: This function returns @obj on success or #NULL on failure. 1128 */ 1129 Object *object_dynamic_cast(Object *obj, const char *typename); 1130 1131 /** 1132 * object_dynamic_cast_assert: 1133 * 1134 * See object_dynamic_cast() for a description of the parameters of this 1135 * function. The only difference in behavior is that this function asserts 1136 * instead of returning #NULL on failure if QOM cast debugging is enabled. 1137 * This function is not meant to be called directly, but only through 1138 * the wrapper macro OBJECT_CHECK. 1139 */ 1140 Object *object_dynamic_cast_assert(Object *obj, const char *typename, 1141 const char *file, int line, const char *func); 1142 1143 /** 1144 * object_get_class: 1145 * @obj: A derivative of #Object 1146 * 1147 * Returns: The #ObjectClass of the type associated with @obj. 1148 */ 1149 ObjectClass *object_get_class(Object *obj); 1150 1151 /** 1152 * object_get_typename: 1153 * @obj: A derivative of #Object. 1154 * 1155 * Returns: The QOM typename of @obj. 1156 */ 1157 const char *object_get_typename(const Object *obj); 1158 1159 /** 1160 * type_register_static: 1161 * @info: The #TypeInfo of the new type. 1162 * 1163 * @info and all of the strings it points to should exist for the life time 1164 * that the type is registered. 1165 * 1166 * Returns: the new #Type. 1167 */ 1168 Type type_register_static(const TypeInfo *info); 1169 1170 /** 1171 * type_register: 1172 * @info: The #TypeInfo of the new type 1173 * 1174 * Unlike type_register_static(), this call does not require @info or its 1175 * string members to continue to exist after the call returns. 1176 * 1177 * Returns: the new #Type. 1178 */ 1179 Type type_register(const TypeInfo *info); 1180 1181 /** 1182 * type_register_static_array: 1183 * @infos: The array of the new type #TypeInfo structures. 1184 * @nr_infos: number of entries in @infos 1185 * 1186 * @infos and all of the strings it points to should exist for the life time 1187 * that the type is registered. 1188 */ 1189 void type_register_static_array(const TypeInfo *infos, int nr_infos); 1190 1191 /** 1192 * DEFINE_TYPES: 1193 * @type_array: The array containing #TypeInfo structures to register 1194 * 1195 * @type_array should be static constant that exists for the life time 1196 * that the type is registered. 1197 */ 1198 #define DEFINE_TYPES(type_array) \ 1199 static void do_qemu_init_ ## type_array(void) \ 1200 { \ 1201 type_register_static_array(type_array, ARRAY_SIZE(type_array)); \ 1202 } \ 1203 type_init(do_qemu_init_ ## type_array) 1204 1205 /** 1206 * object_class_dynamic_cast_assert: 1207 * @klass: The #ObjectClass to attempt to cast. 1208 * @typename: The QOM typename of the class to cast to. 1209 * 1210 * See object_class_dynamic_cast() for a description of the parameters 1211 * of this function. The only difference in behavior is that this function 1212 * asserts instead of returning #NULL on failure if QOM cast debugging is 1213 * enabled. This function is not meant to be called directly, but only through 1214 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK. 1215 */ 1216 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass, 1217 const char *typename, 1218 const char *file, int line, 1219 const char *func); 1220 1221 /** 1222 * object_class_dynamic_cast: 1223 * @klass: The #ObjectClass to attempt to cast. 1224 * @typename: The QOM typename of the class to cast to. 1225 * 1226 * Returns: If @typename is a class, this function returns @klass if 1227 * @typename is a subtype of @klass, else returns #NULL. 1228 * 1229 * If @typename is an interface, this function returns the interface 1230 * definition for @klass if @klass implements it unambiguously; #NULL 1231 * is returned if @klass does not implement the interface or if multiple 1232 * classes or interfaces on the hierarchy leading to @klass implement 1233 * it. (FIXME: perhaps this can be detected at type definition time?) 1234 */ 1235 ObjectClass *object_class_dynamic_cast(ObjectClass *klass, 1236 const char *typename); 1237 1238 /** 1239 * object_class_get_parent: 1240 * @klass: The class to obtain the parent for. 1241 * 1242 * Returns: The parent for @klass or %NULL if none. 1243 */ 1244 ObjectClass *object_class_get_parent(ObjectClass *klass); 1245 1246 /** 1247 * object_class_get_name: 1248 * @klass: The class to obtain the QOM typename for. 1249 * 1250 * Returns: The QOM typename for @klass. 1251 */ 1252 const char *object_class_get_name(ObjectClass *klass); 1253 1254 /** 1255 * object_class_is_abstract: 1256 * @klass: The class to obtain the abstractness for. 1257 * 1258 * Returns: %true if @klass is abstract, %false otherwise. 1259 */ 1260 bool object_class_is_abstract(ObjectClass *klass); 1261 1262 /** 1263 * object_class_by_name: 1264 * @typename: The QOM typename to obtain the class for. 1265 * 1266 * Returns: The class for @typename or %NULL if not found. 1267 */ 1268 ObjectClass *object_class_by_name(const char *typename); 1269 1270 /** 1271 * module_object_class_by_name: 1272 * @typename: The QOM typename to obtain the class for. 1273 * 1274 * For objects which might be provided by a module. Behaves like 1275 * object_class_by_name, but additionally tries to load the module 1276 * needed in case the class is not available. 1277 * 1278 * Returns: The class for @typename or %NULL if not found. 1279 */ 1280 ObjectClass *module_object_class_by_name(const char *typename); 1281 1282 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), 1283 const char *implements_type, bool include_abstract, 1284 void *opaque); 1285 1286 /** 1287 * object_class_get_list: 1288 * @implements_type: The type to filter for, including its derivatives. 1289 * @include_abstract: Whether to include abstract classes. 1290 * 1291 * Returns: A singly-linked list of the classes in reverse hashtable order. 1292 */ 1293 GSList *object_class_get_list(const char *implements_type, 1294 bool include_abstract); 1295 1296 /** 1297 * object_class_get_list_sorted: 1298 * @implements_type: The type to filter for, including its derivatives. 1299 * @include_abstract: Whether to include abstract classes. 1300 * 1301 * Returns: A singly-linked list of the classes in alphabetical 1302 * case-insensitive order. 1303 */ 1304 GSList *object_class_get_list_sorted(const char *implements_type, 1305 bool include_abstract); 1306 1307 /** 1308 * object_ref: 1309 * @obj: the object 1310 * 1311 * Increase the reference count of a object. A object cannot be freed as long 1312 * as its reference count is greater than zero. 1313 * Returns: @obj 1314 */ 1315 Object *object_ref(void *obj); 1316 1317 /** 1318 * object_unref: 1319 * @obj: the object 1320 * 1321 * Decrease the reference count of a object. A object cannot be freed as long 1322 * as its reference count is greater than zero. 1323 */ 1324 void object_unref(void *obj); 1325 1326 /** 1327 * object_property_try_add: 1328 * @obj: the object to add a property to 1329 * @name: the name of the property. This can contain any character except for 1330 * a forward slash. In general, you should use hyphens '-' instead of 1331 * underscores '_' when naming properties. 1332 * @type: the type name of the property. This namespace is pretty loosely 1333 * defined. Sub namespaces are constructed by using a prefix and then 1334 * to angle brackets. For instance, the type 'virtio-net-pci' in the 1335 * 'link' namespace would be 'link<virtio-net-pci>'. 1336 * @get: The getter to be called to read a property. If this is NULL, then 1337 * the property cannot be read. 1338 * @set: the setter to be called to write a property. If this is NULL, 1339 * then the property cannot be written. 1340 * @release: called when the property is removed from the object. This is 1341 * meant to allow a property to free its opaque upon object 1342 * destruction. This may be NULL. 1343 * @opaque: an opaque pointer to pass to the callbacks for the property 1344 * @errp: pointer to error object 1345 * 1346 * Returns: The #ObjectProperty; this can be used to set the @resolve 1347 * callback for child and link properties. 1348 */ 1349 ObjectProperty *object_property_try_add(Object *obj, const char *name, 1350 const char *type, 1351 ObjectPropertyAccessor *get, 1352 ObjectPropertyAccessor *set, 1353 ObjectPropertyRelease *release, 1354 void *opaque, Error **errp); 1355 1356 /** 1357 * object_property_add: 1358 * Same as object_property_try_add() with @errp hardcoded to 1359 * &error_abort. 1360 */ 1361 ObjectProperty *object_property_add(Object *obj, const char *name, 1362 const char *type, 1363 ObjectPropertyAccessor *get, 1364 ObjectPropertyAccessor *set, 1365 ObjectPropertyRelease *release, 1366 void *opaque); 1367 1368 void object_property_del(Object *obj, const char *name); 1369 1370 ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name, 1371 const char *type, 1372 ObjectPropertyAccessor *get, 1373 ObjectPropertyAccessor *set, 1374 ObjectPropertyRelease *release, 1375 void *opaque); 1376 1377 /** 1378 * object_property_set_default_bool: 1379 * @prop: the property to set 1380 * @value: the value to be written to the property 1381 * 1382 * Set the property default value. 1383 */ 1384 void object_property_set_default_bool(ObjectProperty *prop, bool value); 1385 1386 /** 1387 * object_property_set_default_str: 1388 * @prop: the property to set 1389 * @value: the value to be written to the property 1390 * 1391 * Set the property default value. 1392 */ 1393 void object_property_set_default_str(ObjectProperty *prop, const char *value); 1394 1395 /** 1396 * object_property_set_default_int: 1397 * @prop: the property to set 1398 * @value: the value to be written to the property 1399 * 1400 * Set the property default value. 1401 */ 1402 void object_property_set_default_int(ObjectProperty *prop, int64_t value); 1403 1404 /** 1405 * object_property_set_default_uint: 1406 * @prop: the property to set 1407 * @value: the value to be written to the property 1408 * 1409 * Set the property default value. 1410 */ 1411 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value); 1412 1413 /** 1414 * object_property_find: 1415 * @obj: the object 1416 * @name: the name of the property 1417 * @errp: returns an error if this function fails 1418 * 1419 * Look up a property for an object and return its #ObjectProperty if found. 1420 */ 1421 ObjectProperty *object_property_find(Object *obj, const char *name, 1422 Error **errp); 1423 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, 1424 Error **errp); 1425 1426 typedef struct ObjectPropertyIterator { 1427 ObjectClass *nextclass; 1428 GHashTableIter iter; 1429 } ObjectPropertyIterator; 1430 1431 /** 1432 * object_property_iter_init: 1433 * @obj: the object 1434 * 1435 * Initializes an iterator for traversing all properties 1436 * registered against an object instance, its class and all parent classes. 1437 * 1438 * It is forbidden to modify the property list while iterating, 1439 * whether removing or adding properties. 1440 * 1441 * Typical usage pattern would be 1442 * 1443 * <example> 1444 * <title>Using object property iterators</title> 1445 * <programlisting> 1446 * ObjectProperty *prop; 1447 * ObjectPropertyIterator iter; 1448 * 1449 * object_property_iter_init(&iter, obj); 1450 * while ((prop = object_property_iter_next(&iter))) { 1451 * ... do something with prop ... 1452 * } 1453 * </programlisting> 1454 * </example> 1455 */ 1456 void object_property_iter_init(ObjectPropertyIterator *iter, 1457 Object *obj); 1458 1459 /** 1460 * object_class_property_iter_init: 1461 * @klass: the class 1462 * 1463 * Initializes an iterator for traversing all properties 1464 * registered against an object class and all parent classes. 1465 * 1466 * It is forbidden to modify the property list while iterating, 1467 * whether removing or adding properties. 1468 * 1469 * This can be used on abstract classes as it does not create a temporary 1470 * instance. 1471 */ 1472 void object_class_property_iter_init(ObjectPropertyIterator *iter, 1473 ObjectClass *klass); 1474 1475 /** 1476 * object_property_iter_next: 1477 * @iter: the iterator instance 1478 * 1479 * Return the next available property. If no further properties 1480 * are available, a %NULL value will be returned and the @iter 1481 * pointer should not be used again after this point without 1482 * re-initializing it. 1483 * 1484 * Returns: the next property, or %NULL when all properties 1485 * have been traversed. 1486 */ 1487 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter); 1488 1489 void object_unparent(Object *obj); 1490 1491 /** 1492 * object_property_get: 1493 * @obj: the object 1494 * @name: the name of the property 1495 * @v: the visitor that will receive the property value. This should be an 1496 * Output visitor and the data will be written with @name as the name. 1497 * @errp: returns an error if this function fails 1498 * 1499 * Reads a property from a object. 1500 * 1501 * Returns: %true on success, %false on failure. 1502 */ 1503 bool object_property_get(Object *obj, const char *name, Visitor *v, 1504 Error **errp); 1505 1506 /** 1507 * object_property_set_str: 1508 * @name: the name of the property 1509 * @value: the value to be written to the property 1510 * @errp: returns an error if this function fails 1511 * 1512 * Writes a string value to a property. 1513 * 1514 * Returns: %true on success, %false on failure. 1515 */ 1516 bool object_property_set_str(Object *obj, const char *name, 1517 const char *value, Error **errp); 1518 1519 /** 1520 * object_property_get_str: 1521 * @obj: the object 1522 * @name: the name of the property 1523 * @errp: returns an error if this function fails 1524 * 1525 * Returns: the value of the property, converted to a C string, or NULL if 1526 * an error occurs (including when the property value is not a string). 1527 * The caller should free the string. 1528 */ 1529 char *object_property_get_str(Object *obj, const char *name, 1530 Error **errp); 1531 1532 /** 1533 * object_property_set_link: 1534 * @name: the name of the property 1535 * @value: the value to be written to the property 1536 * @errp: returns an error if this function fails 1537 * 1538 * Writes an object's canonical path to a property. 1539 * 1540 * If the link property was created with 1541 * <code>OBJ_PROP_LINK_STRONG</code> bit, the old target object is 1542 * unreferenced, and a reference is added to the new target object. 1543 * 1544 * Returns: %true on success, %false on failure. 1545 */ 1546 bool object_property_set_link(Object *obj, const char *name, 1547 Object *value, Error **errp); 1548 1549 /** 1550 * object_property_get_link: 1551 * @obj: the object 1552 * @name: the name of the property 1553 * @errp: returns an error if this function fails 1554 * 1555 * Returns: the value of the property, resolved from a path to an Object, 1556 * or NULL if an error occurs (including when the property value is not a 1557 * string or not a valid object path). 1558 */ 1559 Object *object_property_get_link(Object *obj, const char *name, 1560 Error **errp); 1561 1562 /** 1563 * object_property_set_bool: 1564 * @name: the name of the property 1565 * @value: the value to be written to the property 1566 * @errp: returns an error if this function fails 1567 * 1568 * Writes a bool value to a property. 1569 * 1570 * Returns: %true on success, %false on failure. 1571 */ 1572 bool object_property_set_bool(Object *obj, const char *name, 1573 bool value, Error **errp); 1574 1575 /** 1576 * object_property_get_bool: 1577 * @obj: the object 1578 * @name: the name of the property 1579 * @errp: returns an error if this function fails 1580 * 1581 * Returns: the value of the property, converted to a boolean, or NULL if 1582 * an error occurs (including when the property value is not a bool). 1583 */ 1584 bool object_property_get_bool(Object *obj, const char *name, 1585 Error **errp); 1586 1587 /** 1588 * object_property_set_int: 1589 * @name: the name of the property 1590 * @value: the value to be written to the property 1591 * @errp: returns an error if this function fails 1592 * 1593 * Writes an integer value to a property. 1594 * 1595 * Returns: %true on success, %false on failure. 1596 */ 1597 bool object_property_set_int(Object *obj, const char *name, 1598 int64_t value, Error **errp); 1599 1600 /** 1601 * object_property_get_int: 1602 * @obj: the object 1603 * @name: the name of the property 1604 * @errp: returns an error if this function fails 1605 * 1606 * Returns: the value of the property, converted to an integer, or negative if 1607 * an error occurs (including when the property value is not an integer). 1608 */ 1609 int64_t object_property_get_int(Object *obj, const char *name, 1610 Error **errp); 1611 1612 /** 1613 * object_property_set_uint: 1614 * @name: the name of the property 1615 * @value: the value to be written to the property 1616 * @errp: returns an error if this function fails 1617 * 1618 * Writes an unsigned integer value to a property. 1619 * 1620 * Returns: %true on success, %false on failure. 1621 */ 1622 bool object_property_set_uint(Object *obj, const char *name, 1623 uint64_t value, Error **errp); 1624 1625 /** 1626 * object_property_get_uint: 1627 * @obj: the object 1628 * @name: the name of the property 1629 * @errp: returns an error if this function fails 1630 * 1631 * Returns: the value of the property, converted to an unsigned integer, or 0 1632 * an error occurs (including when the property value is not an integer). 1633 */ 1634 uint64_t object_property_get_uint(Object *obj, const char *name, 1635 Error **errp); 1636 1637 /** 1638 * object_property_get_enum: 1639 * @obj: the object 1640 * @name: the name of the property 1641 * @typename: the name of the enum data type 1642 * @errp: returns an error if this function fails 1643 * 1644 * Returns: the value of the property, converted to an integer, or 1645 * undefined if an error occurs (including when the property value is not 1646 * an enum). 1647 */ 1648 int object_property_get_enum(Object *obj, const char *name, 1649 const char *typename, Error **errp); 1650 1651 /** 1652 * object_property_set: 1653 * @obj: the object 1654 * @name: the name of the property 1655 * @v: the visitor that will be used to write the property value. This should 1656 * be an Input visitor and the data will be first read with @name as the 1657 * name and then written as the property value. 1658 * @errp: returns an error if this function fails 1659 * 1660 * Writes a property to a object. 1661 * 1662 * Returns: %true on success, %false on failure. 1663 */ 1664 bool object_property_set(Object *obj, const char *name, Visitor *v, 1665 Error **errp); 1666 1667 /** 1668 * object_property_parse: 1669 * @obj: the object 1670 * @name: the name of the property 1671 * @string: the string that will be used to parse the property value. 1672 * @errp: returns an error if this function fails 1673 * 1674 * Parses a string and writes the result into a property of an object. 1675 * 1676 * Returns: %true on success, %false on failure. 1677 */ 1678 bool object_property_parse(Object *obj, const char *name, 1679 const char *string, Error **errp); 1680 1681 /** 1682 * object_property_print: 1683 * @obj: the object 1684 * @name: the name of the property 1685 * @human: if true, print for human consumption 1686 * @errp: returns an error if this function fails 1687 * 1688 * Returns a string representation of the value of the property. The 1689 * caller shall free the string. 1690 */ 1691 char *object_property_print(Object *obj, const char *name, bool human, 1692 Error **errp); 1693 1694 /** 1695 * object_property_get_type: 1696 * @obj: the object 1697 * @name: the name of the property 1698 * @errp: returns an error if this function fails 1699 * 1700 * Returns: The type name of the property. 1701 */ 1702 const char *object_property_get_type(Object *obj, const char *name, 1703 Error **errp); 1704 1705 /** 1706 * object_get_root: 1707 * 1708 * Returns: the root object of the composition tree 1709 */ 1710 Object *object_get_root(void); 1711 1712 1713 /** 1714 * object_get_objects_root: 1715 * 1716 * Get the container object that holds user created 1717 * object instances. This is the object at path 1718 * "/objects" 1719 * 1720 * Returns: the user object container 1721 */ 1722 Object *object_get_objects_root(void); 1723 1724 /** 1725 * object_get_internal_root: 1726 * 1727 * Get the container object that holds internally used object 1728 * instances. Any object which is put into this container must not be 1729 * user visible, and it will not be exposed in the QOM tree. 1730 * 1731 * Returns: the internal object container 1732 */ 1733 Object *object_get_internal_root(void); 1734 1735 /** 1736 * object_get_canonical_path_component: 1737 * 1738 * Returns: The final component in the object's canonical path. The canonical 1739 * path is the path within the composition tree starting from the root. 1740 * %NULL if the object doesn't have a parent (and thus a canonical path). 1741 */ 1742 const char *object_get_canonical_path_component(const Object *obj); 1743 1744 /** 1745 * object_get_canonical_path: 1746 * 1747 * Returns: The canonical path for a object, newly allocated. This is 1748 * the path within the composition tree starting from the root. Use 1749 * g_free() to free it. 1750 */ 1751 char *object_get_canonical_path(const Object *obj); 1752 1753 /** 1754 * object_resolve_path: 1755 * @path: the path to resolve 1756 * @ambiguous: returns true if the path resolution failed because of an 1757 * ambiguous match 1758 * 1759 * There are two types of supported paths--absolute paths and partial paths. 1760 * 1761 * Absolute paths are derived from the root object and can follow child<> or 1762 * link<> properties. Since they can follow link<> properties, they can be 1763 * arbitrarily long. Absolute paths look like absolute filenames and are 1764 * prefixed with a leading slash. 1765 * 1766 * Partial paths look like relative filenames. They do not begin with a 1767 * prefix. The matching rules for partial paths are subtle but designed to make 1768 * specifying objects easy. At each level of the composition tree, the partial 1769 * path is matched as an absolute path. The first match is not returned. At 1770 * least two matches are searched for. A successful result is only returned if 1771 * only one match is found. If more than one match is found, a flag is 1772 * returned to indicate that the match was ambiguous. 1773 * 1774 * Returns: The matched object or NULL on path lookup failure. 1775 */ 1776 Object *object_resolve_path(const char *path, bool *ambiguous); 1777 1778 /** 1779 * object_resolve_path_type: 1780 * @path: the path to resolve 1781 * @typename: the type to look for. 1782 * @ambiguous: returns true if the path resolution failed because of an 1783 * ambiguous match 1784 * 1785 * This is similar to object_resolve_path. However, when looking for a 1786 * partial path only matches that implement the given type are considered. 1787 * This restricts the search and avoids spuriously flagging matches as 1788 * ambiguous. 1789 * 1790 * For both partial and absolute paths, the return value goes through 1791 * a dynamic cast to @typename. This is important if either the link, 1792 * or the typename itself are of interface types. 1793 * 1794 * Returns: The matched object or NULL on path lookup failure. 1795 */ 1796 Object *object_resolve_path_type(const char *path, const char *typename, 1797 bool *ambiguous); 1798 1799 /** 1800 * object_resolve_path_component: 1801 * @parent: the object in which to resolve the path 1802 * @part: the component to resolve. 1803 * 1804 * This is similar to object_resolve_path with an absolute path, but it 1805 * only resolves one element (@part) and takes the others from @parent. 1806 * 1807 * Returns: The resolved object or NULL on path lookup failure. 1808 */ 1809 Object *object_resolve_path_component(Object *parent, const char *part); 1810 1811 /** 1812 * object_property_try_add_child: 1813 * @obj: the object to add a property to 1814 * @name: the name of the property 1815 * @child: the child object 1816 * @errp: pointer to error object 1817 * 1818 * Child properties form the composition tree. All objects need to be a child 1819 * of another object. Objects can only be a child of one object. 1820 * 1821 * There is no way for a child to determine what its parent is. It is not 1822 * a bidirectional relationship. This is by design. 1823 * 1824 * The value of a child property as a C string will be the child object's 1825 * canonical path. It can be retrieved using object_property_get_str(). 1826 * The child object itself can be retrieved using object_property_get_link(). 1827 * 1828 * Returns: The newly added property on success, or %NULL on failure. 1829 */ 1830 ObjectProperty *object_property_try_add_child(Object *obj, const char *name, 1831 Object *child, Error **errp); 1832 1833 /** 1834 * object_property_add_child: 1835 * Same as object_property_try_add_child() with @errp hardcoded to 1836 * &error_abort 1837 */ 1838 ObjectProperty *object_property_add_child(Object *obj, const char *name, 1839 Object *child); 1840 1841 typedef enum { 1842 /* Unref the link pointer when the property is deleted */ 1843 OBJ_PROP_LINK_STRONG = 0x1, 1844 1845 /* private */ 1846 OBJ_PROP_LINK_DIRECT = 0x2, 1847 OBJ_PROP_LINK_CLASS = 0x4, 1848 } ObjectPropertyLinkFlags; 1849 1850 /** 1851 * object_property_allow_set_link: 1852 * 1853 * The default implementation of the object_property_add_link() check() 1854 * callback function. It allows the link property to be set and never returns 1855 * an error. 1856 */ 1857 void object_property_allow_set_link(const Object *, const char *, 1858 Object *, Error **); 1859 1860 /** 1861 * object_property_add_link: 1862 * @obj: the object to add a property to 1863 * @name: the name of the property 1864 * @type: the qobj type of the link 1865 * @targetp: a pointer to where the link object reference is stored 1866 * @check: callback to veto setting or NULL if the property is read-only 1867 * @flags: additional options for the link 1868 * 1869 * Links establish relationships between objects. Links are unidirectional 1870 * although two links can be combined to form a bidirectional relationship 1871 * between objects. 1872 * 1873 * Links form the graph in the object model. 1874 * 1875 * The <code>@check()</code> callback is invoked when 1876 * object_property_set_link() is called and can raise an error to prevent the 1877 * link being set. If <code>@check</code> is NULL, the property is read-only 1878 * and cannot be set. 1879 * 1880 * Ownership of the pointer that @child points to is transferred to the 1881 * link property. The reference count for <code>*@child</code> is 1882 * managed by the property from after the function returns till the 1883 * property is deleted with object_property_del(). If the 1884 * <code>@flags</code> <code>OBJ_PROP_LINK_STRONG</code> bit is set, 1885 * the reference count is decremented when the property is deleted or 1886 * modified. 1887 * 1888 * Returns: The newly added property on success, or %NULL on failure. 1889 */ 1890 ObjectProperty *object_property_add_link(Object *obj, const char *name, 1891 const char *type, Object **targetp, 1892 void (*check)(const Object *obj, const char *name, 1893 Object *val, Error **errp), 1894 ObjectPropertyLinkFlags flags); 1895 1896 ObjectProperty *object_class_property_add_link(ObjectClass *oc, 1897 const char *name, 1898 const char *type, ptrdiff_t offset, 1899 void (*check)(const Object *obj, const char *name, 1900 Object *val, Error **errp), 1901 ObjectPropertyLinkFlags flags); 1902 1903 /** 1904 * object_property_add_str: 1905 * @obj: the object to add a property to 1906 * @name: the name of the property 1907 * @get: the getter or NULL if the property is write-only. This function must 1908 * return a string to be freed by g_free(). 1909 * @set: the setter or NULL if the property is read-only 1910 * 1911 * Add a string property using getters/setters. This function will add a 1912 * property of type 'string'. 1913 * 1914 * Returns: The newly added property on success, or %NULL on failure. 1915 */ 1916 ObjectProperty *object_property_add_str(Object *obj, const char *name, 1917 char *(*get)(Object *, Error **), 1918 void (*set)(Object *, const char *, Error **)); 1919 1920 ObjectProperty *object_class_property_add_str(ObjectClass *klass, 1921 const char *name, 1922 char *(*get)(Object *, Error **), 1923 void (*set)(Object *, const char *, 1924 Error **)); 1925 1926 /** 1927 * object_property_add_bool: 1928 * @obj: the object to add a property to 1929 * @name: the name of the property 1930 * @get: the getter or NULL if the property is write-only. 1931 * @set: the setter or NULL if the property is read-only 1932 * 1933 * Add a bool property using getters/setters. This function will add a 1934 * property of type 'bool'. 1935 * 1936 * Returns: The newly added property on success, or %NULL on failure. 1937 */ 1938 ObjectProperty *object_property_add_bool(Object *obj, const char *name, 1939 bool (*get)(Object *, Error **), 1940 void (*set)(Object *, bool, Error **)); 1941 1942 ObjectProperty *object_class_property_add_bool(ObjectClass *klass, 1943 const char *name, 1944 bool (*get)(Object *, Error **), 1945 void (*set)(Object *, bool, Error **)); 1946 1947 /** 1948 * object_property_add_enum: 1949 * @obj: the object to add a property to 1950 * @name: the name of the property 1951 * @typename: the name of the enum data type 1952 * @get: the getter or %NULL if the property is write-only. 1953 * @set: the setter or %NULL if the property is read-only 1954 * 1955 * Add an enum property using getters/setters. This function will add a 1956 * property of type '@typename'. 1957 * 1958 * Returns: The newly added property on success, or %NULL on failure. 1959 */ 1960 ObjectProperty *object_property_add_enum(Object *obj, const char *name, 1961 const char *typename, 1962 const QEnumLookup *lookup, 1963 int (*get)(Object *, Error **), 1964 void (*set)(Object *, int, Error **)); 1965 1966 ObjectProperty *object_class_property_add_enum(ObjectClass *klass, 1967 const char *name, 1968 const char *typename, 1969 const QEnumLookup *lookup, 1970 int (*get)(Object *, Error **), 1971 void (*set)(Object *, int, Error **)); 1972 1973 /** 1974 * object_property_add_tm: 1975 * @obj: the object to add a property to 1976 * @name: the name of the property 1977 * @get: the getter or NULL if the property is write-only. 1978 * 1979 * Add a read-only struct tm valued property using a getter function. 1980 * This function will add a property of type 'struct tm'. 1981 * 1982 * Returns: The newly added property on success, or %NULL on failure. 1983 */ 1984 ObjectProperty *object_property_add_tm(Object *obj, const char *name, 1985 void (*get)(Object *, struct tm *, Error **)); 1986 1987 ObjectProperty *object_class_property_add_tm(ObjectClass *klass, 1988 const char *name, 1989 void (*get)(Object *, struct tm *, Error **)); 1990 1991 typedef enum { 1992 /* Automatically add a getter to the property */ 1993 OBJ_PROP_FLAG_READ = 1 << 0, 1994 /* Automatically add a setter to the property */ 1995 OBJ_PROP_FLAG_WRITE = 1 << 1, 1996 /* Automatically add a getter and a setter to the property */ 1997 OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE), 1998 } ObjectPropertyFlags; 1999 2000 /** 2001 * object_property_add_uint8_ptr: 2002 * @obj: the object to add a property to 2003 * @name: the name of the property 2004 * @v: pointer to value 2005 * @flags: bitwise-or'd ObjectPropertyFlags 2006 * 2007 * Add an integer property in memory. This function will add a 2008 * property of type 'uint8'. 2009 * 2010 * Returns: The newly added property on success, or %NULL on failure. 2011 */ 2012 ObjectProperty *object_property_add_uint8_ptr(Object *obj, const char *name, 2013 const uint8_t *v, 2014 ObjectPropertyFlags flags); 2015 2016 ObjectProperty *object_class_property_add_uint8_ptr(ObjectClass *klass, 2017 const char *name, 2018 const uint8_t *v, 2019 ObjectPropertyFlags flags); 2020 2021 /** 2022 * object_property_add_uint16_ptr: 2023 * @obj: the object to add a property to 2024 * @name: the name of the property 2025 * @v: pointer to value 2026 * @flags: bitwise-or'd ObjectPropertyFlags 2027 * 2028 * Add an integer property in memory. This function will add a 2029 * property of type 'uint16'. 2030 * 2031 * Returns: The newly added property on success, or %NULL on failure. 2032 */ 2033 ObjectProperty *object_property_add_uint16_ptr(Object *obj, const char *name, 2034 const uint16_t *v, 2035 ObjectPropertyFlags flags); 2036 2037 ObjectProperty *object_class_property_add_uint16_ptr(ObjectClass *klass, 2038 const char *name, 2039 const uint16_t *v, 2040 ObjectPropertyFlags flags); 2041 2042 /** 2043 * object_property_add_uint32_ptr: 2044 * @obj: the object to add a property to 2045 * @name: the name of the property 2046 * @v: pointer to value 2047 * @flags: bitwise-or'd ObjectPropertyFlags 2048 * 2049 * Add an integer property in memory. This function will add a 2050 * property of type 'uint32'. 2051 * 2052 * Returns: The newly added property on success, or %NULL on failure. 2053 */ 2054 ObjectProperty *object_property_add_uint32_ptr(Object *obj, const char *name, 2055 const uint32_t *v, 2056 ObjectPropertyFlags flags); 2057 2058 ObjectProperty *object_class_property_add_uint32_ptr(ObjectClass *klass, 2059 const char *name, 2060 const uint32_t *v, 2061 ObjectPropertyFlags flags); 2062 2063 /** 2064 * object_property_add_uint64_ptr: 2065 * @obj: the object to add a property to 2066 * @name: the name of the property 2067 * @v: pointer to value 2068 * @flags: bitwise-or'd ObjectPropertyFlags 2069 * 2070 * Add an integer property in memory. This function will add a 2071 * property of type 'uint64'. 2072 * 2073 * Returns: The newly added property on success, or %NULL on failure. 2074 */ 2075 ObjectProperty *object_property_add_uint64_ptr(Object *obj, const char *name, 2076 const uint64_t *v, 2077 ObjectPropertyFlags flags); 2078 2079 ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass, 2080 const char *name, 2081 const uint64_t *v, 2082 ObjectPropertyFlags flags); 2083 2084 /** 2085 * object_property_add_alias: 2086 * @obj: the object to add a property to 2087 * @name: the name of the property 2088 * @target_obj: the object to forward property access to 2089 * @target_name: the name of the property on the forwarded object 2090 * 2091 * Add an alias for a property on an object. This function will add a property 2092 * of the same type as the forwarded property. 2093 * 2094 * The caller must ensure that <code>@target_obj</code> stays alive as long as 2095 * this property exists. In the case of a child object or an alias on the same 2096 * object this will be the case. For aliases to other objects the caller is 2097 * responsible for taking a reference. 2098 * 2099 * Returns: The newly added property on success, or %NULL on failure. 2100 */ 2101 ObjectProperty *object_property_add_alias(Object *obj, const char *name, 2102 Object *target_obj, const char *target_name); 2103 2104 /** 2105 * object_property_add_const_link: 2106 * @obj: the object to add a property to 2107 * @name: the name of the property 2108 * @target: the object to be referred by the link 2109 * 2110 * Add an unmodifiable link for a property on an object. This function will 2111 * add a property of type link<TYPE> where TYPE is the type of @target. 2112 * 2113 * The caller must ensure that @target stays alive as long as 2114 * this property exists. In the case @target is a child of @obj, 2115 * this will be the case. Otherwise, the caller is responsible for 2116 * taking a reference. 2117 * 2118 * Returns: The newly added property on success, or %NULL on failure. 2119 */ 2120 ObjectProperty *object_property_add_const_link(Object *obj, const char *name, 2121 Object *target); 2122 2123 /** 2124 * object_property_set_description: 2125 * @obj: the object owning the property 2126 * @name: the name of the property 2127 * @description: the description of the property on the object 2128 * 2129 * Set an object property's description. 2130 * 2131 * Returns: %true on success, %false on failure. 2132 */ 2133 void object_property_set_description(Object *obj, const char *name, 2134 const char *description); 2135 void object_class_property_set_description(ObjectClass *klass, const char *name, 2136 const char *description); 2137 2138 /** 2139 * object_child_foreach: 2140 * @obj: the object whose children will be navigated 2141 * @fn: the iterator function to be called 2142 * @opaque: an opaque value that will be passed to the iterator 2143 * 2144 * Call @fn passing each child of @obj and @opaque to it, until @fn returns 2145 * non-zero. 2146 * 2147 * It is forbidden to add or remove children from @obj from the @fn 2148 * callback. 2149 * 2150 * Returns: The last value returned by @fn, or 0 if there is no child. 2151 */ 2152 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), 2153 void *opaque); 2154 2155 /** 2156 * object_child_foreach_recursive: 2157 * @obj: the object whose children will be navigated 2158 * @fn: the iterator function to be called 2159 * @opaque: an opaque value that will be passed to the iterator 2160 * 2161 * Call @fn passing each child of @obj and @opaque to it, until @fn returns 2162 * non-zero. Calls recursively, all child nodes of @obj will also be passed 2163 * all the way down to the leaf nodes of the tree. Depth first ordering. 2164 * 2165 * It is forbidden to add or remove children from @obj (or its 2166 * child nodes) from the @fn callback. 2167 * 2168 * Returns: The last value returned by @fn, or 0 if there is no child. 2169 */ 2170 int object_child_foreach_recursive(Object *obj, 2171 int (*fn)(Object *child, void *opaque), 2172 void *opaque); 2173 /** 2174 * container_get: 2175 * @root: root of the #path, e.g., object_get_root() 2176 * @path: path to the container 2177 * 2178 * Return a container object whose path is @path. Create more containers 2179 * along the path if necessary. 2180 * 2181 * Returns: the container object. 2182 */ 2183 Object *container_get(Object *root, const char *path); 2184 2185 /** 2186 * object_type_get_instance_size: 2187 * @typename: Name of the Type whose instance_size is required 2188 * 2189 * Returns the instance_size of the given @typename. 2190 */ 2191 size_t object_type_get_instance_size(const char *typename); 2192 2193 /** 2194 * object_property_help: 2195 * @name: the name of the property 2196 * @type: the type of the property 2197 * @defval: the default value 2198 * @description: description of the property 2199 * 2200 * Returns: a user-friendly formatted string describing the property 2201 * for help purposes. 2202 */ 2203 char *object_property_help(const char *name, const char *type, 2204 QObject *defval, const char *description); 2205 2206 G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref) 2207 2208 #endif 2209