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