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