]> git.proxmox.com Git - mirror_qemu.git/blame - include/qom/object.h
qom: Drop object_property_set_description() parameter @errp
[mirror_qemu.git] / include / qom / object.h
CommitLineData
2f28d2ff
AL
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
eb815e24 17#include "qapi/qapi-builtin-types.h"
0b8fa32f 18#include "qemu/module.h"
57c9fafe 19
2f28d2ff
AL
20struct TypeImpl;
21typedef struct TypeImpl *Type;
22
2f28d2ff
AL
23typedef struct Object Object;
24
25typedef struct TypeInfo TypeInfo;
26
27typedef struct InterfaceClass InterfaceClass;
28typedef struct InterfaceInfo InterfaceInfo;
29
745549c8 30#define TYPE_OBJECT "object"
2f28d2ff
AL
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 *
0815a859
PB
52 * // No new virtual functions: we can reuse the typedef for the
53 * // superclass.
54 * typedef DeviceClass MyDeviceClass;
2f28d2ff
AL
55 * typedef struct MyDevice
56 * {
57 * DeviceState parent;
58 *
59 * int reg0, reg1, reg2;
60 * } MyDevice;
61 *
8c43a6f0 62 * static const TypeInfo my_device_info = {
2f28d2ff
AL
63 * .name = TYPE_MY_DEVICE,
64 * .parent = TYPE_DEVICE,
65 * .instance_size = sizeof(MyDevice),
66 * };
67 *
83f7d43a 68 * static void my_device_register_types(void)
2f28d2ff
AL
69 * {
70 * type_register_static(&my_device_info);
71 * }
72 *
83f7d43a 73 * type_init(my_device_register_types)
2f28d2ff
AL
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 *
38b5d79b
IM
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 *
2f28d2ff
AL
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
0815a859
PB
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>
2f28d2ff
AL
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
93148aa5 143 * its virtual functions. Here is how the above example might be modified
0815a859
PB
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 *
8c43a6f0 157 * static const TypeInfo my_device_info = {
0815a859
PB
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 *
782beb52
AF
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:
0815a859
PB
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 *
8c43a6f0 182 * static const TypeInfo my_device_info = {
0815a859
PB
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>
2f28d2ff
AL
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
55deffdb
GK
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.
782beb52
AF
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.
085d8134 227 * The original function is not automatically invoked. It is the responsibility
782beb52
AF
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;
70392912 274 * } DerivedClass;
782beb52
AF
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),
f824e8ed 298 * .class_init = derived_class_init,
782beb52
AF
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.
2f28d2ff
AL
309 */
310
57c9fafe 311
2a1be4b3
MAL
312typedef struct ObjectProperty ObjectProperty;
313
57c9fafe
AL
314/**
315 * ObjectPropertyAccessor:
316 * @obj: the object that owns the property
317 * @v: the visitor that contains the property data
57c9fafe 318 * @name: the name of the property
d7bce999 319 * @opaque: the object property opaque
57c9fafe
AL
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 */
324typedef void (ObjectPropertyAccessor)(Object *obj,
4fa45492 325 Visitor *v,
57c9fafe 326 const char *name,
d7bce999 327 void *opaque,
e82df248 328 Error **errp);
57c9fafe 329
64607d08
PB
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 */
345typedef Object *(ObjectPropertyResolve)(Object *obj,
346 void *opaque,
347 const char *part);
348
57c9fafe
AL
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 */
357typedef void (ObjectPropertyRelease)(Object *obj,
358 const char *name,
359 void *opaque);
360
2a1be4b3
MAL
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 */
368typedef void (ObjectPropertyInit)(Object *obj, ObjectProperty *prop);
369
370struct ObjectProperty
57c9fafe 371{
ddfb0baa
MA
372 char *name;
373 char *type;
374 char *description;
57c9fafe
AL
375 ObjectPropertyAccessor *get;
376 ObjectPropertyAccessor *set;
64607d08 377 ObjectPropertyResolve *resolve;
57c9fafe 378 ObjectPropertyRelease *release;
2a1be4b3 379 ObjectPropertyInit *init;
57c9fafe 380 void *opaque;
0e76ed0a 381 QObject *defval;
2a1be4b3 382};
57c9fafe 383
667d22d1
PB
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 */
391typedef void (ObjectUnparent)(Object *obj);
392
fde9bf44
PB
393/**
394 * ObjectFree:
395 * @obj: the object being freed
396 *
397 * Called when an object's last reference is removed.
398 */
399typedef void (ObjectFree)(void *obj);
400
03587328
AL
401#define OBJECT_CLASS_CAST_CACHE 4
402
2f28d2ff
AL
403/**
404 * ObjectClass:
405 *
406 * The base for all classes. The only thing that #ObjectClass contains is an
407 * integer type handle.
408 */
409struct ObjectClass
410{
411 /*< private >*/
412 Type type;
33e95c63 413 GSList *interfaces;
667d22d1 414
0ab4c94c
PC
415 const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
416 const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
03587328 417
667d22d1 418 ObjectUnparent *unparent;
16bf7f52
DB
419
420 GHashTable *properties;
2f28d2ff
AL
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.
2f28d2ff
AL
434 */
435struct Object
436{
437 /*< private >*/
438 ObjectClass *class;
fde9bf44 439 ObjectFree *free;
b604a854 440 GHashTable *properties;
57c9fafe
AL
441 uint32_t ref;
442 Object *parent;
2f28d2ff
AL
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.
8231c2dd
EH
455 * @instance_post_init: This function is called to finish initialization of
456 * an object, after all @instance_init functions were called.
2f28d2ff
AL
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
441dd5eb 469 * has occurred to allow a class to set its default virtual method pointers.
2f28d2ff
AL
470 * This is also the function to use to override virtual methods from a parent
471 * class.
3b50e311
PB
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
39a1075a 475 * memcpy from the parent class to the descendants.
37fdb2c5
MAL
476 * @class_data: Data to pass to the @class_init,
477 * @class_base_init. This can be useful when building dynamic
3b50e311 478 * classes.
2f28d2ff
AL
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 */
483struct TypeInfo
484{
485 const char *name;
486 const char *parent;
487
488 size_t instance_size;
489 void (*instance_init)(Object *obj);
8231c2dd 490 void (*instance_post_init)(Object *obj);
2f28d2ff
AL
491 void (*instance_finalize)(Object *obj);
492
493 bool abstract;
494 size_t class_size;
495
496 void (*class_init)(ObjectClass *klass, void *data);
3b50e311 497 void (*class_base_init)(ObjectClass *klass, void *data);
2f28d2ff
AL
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
1ed5b918
PB
513/**
514 * OBJECT_CLASS:
a0dbf408 515 * @class: A derivative of #ObjectClass.
1ed5b918
PB
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
2f28d2ff
AL
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) \
be17f18b
PB
537 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
538 __FILE__, __LINE__, __func__))
2f28d2ff
AL
539
540/**
541 * OBJECT_CLASS_CHECK:
b30d8054
C
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.
2f28d2ff 545 *
1ed5b918
PB
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.
2f28d2ff 549 */
b30d8054
C
550#define OBJECT_CLASS_CHECK(class_type, class, name) \
551 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
be17f18b 552 __FILE__, __LINE__, __func__))
2f28d2ff
AL
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
33e95c63
AL
567/**
568 * InterfaceInfo:
569 * @type: The name of the interface.
570 *
571 * The information associated with an interface.
572 */
573struct InterfaceInfo {
574 const char *type;
575};
576
2f28d2ff
AL
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 */
584struct InterfaceClass
585{
586 ObjectClass parent_class;
33e95c63
AL
587 /*< private >*/
588 ObjectClass *concrete_class;
b061dc41 589 Type interface_type;
2f28d2ff
AL
590};
591
33e95c63
AL
592#define TYPE_INTERFACE "interface"
593
2f28d2ff 594/**
33e95c63
AL
595 * INTERFACE_CLASS:
596 * @klass: class to cast from
597 * Returns: An #InterfaceClass or raise an error if cast is invalid
2f28d2ff 598 */
33e95c63
AL
599#define INTERFACE_CLASS(klass) \
600 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
2f28d2ff 601
33e95c63
AL
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) \
be17f18b
PB
611 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
612 __FILE__, __LINE__, __func__))
2f28d2ff 613
3c75e12e
PB
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 */
624Object *object_new_with_class(ObjectClass *klass);
625
2f28d2ff
AL
626/**
627 * object_new:
628 * @typename: The name of the type of the object to instantiate.
629 *
b76facc3
PB
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.
2f28d2ff
AL
633 *
634 * Returns: The newly allocated and instantiated object.
635 */
636Object *object_new(const char *typename);
637
a31bdae5
DB
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 */
687Object *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 */
703Object *object_new_with_propv(const char *typename,
704 Object *parent,
705 const char *id,
706 Error **errp,
707 va_list vargs);
708
ea9ce893
MAL
709void object_apply_global_props(Object *obj, const GPtrArray *props,
710 Error **errp);
617902af
MA
711void object_set_machine_compat_props(GPtrArray *compat_props);
712void object_set_accelerator_compat_props(GPtrArray *compat_props);
1fff3c20 713void object_register_sugar_prop(const char *driver, const char *prop, const char *value);
617902af 714void object_apply_compat_props(Object *obj);
ea9ce893 715
a31bdae5
DB
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 */
755int 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 */
769int object_set_propv(Object *obj,
770 Error **errp,
771 va_list vargs);
772
2f28d2ff
AL
773/**
774 * object_initialize:
775 * @obj: A pointer to the memory to be used for the object.
213f0c4f 776 * @size: The maximum size available at @obj for the object.
2f28d2ff
AL
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
b76facc3
PB
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.
2f28d2ff 782 */
213f0c4f 783void object_initialize(void *obj, size_t size, const char *typename);
2f28d2ff 784
0210b39d
TH
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 */
806void 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 */
822void object_initialize_childv(Object *parentobj, const char *propname,
823 void *childobj, size_t size, const char *type,
824 Error **errp, va_list vargs);
825
2f28d2ff
AL
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 */
836Object *object_dynamic_cast(Object *obj, const char *typename);
837
838/**
438e1c79 839 * object_dynamic_cast_assert:
2f28d2ff
AL
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
3556c233
PB
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.
2f28d2ff 846 */
be17f18b
PB
847Object *object_dynamic_cast_assert(Object *obj, const char *typename,
848 const char *file, int line, const char *func);
2f28d2ff
AL
849
850/**
851 * object_get_class:
852 * @obj: A derivative of #Object
853 *
854 * Returns: The #ObjectClass of the type associated with @obj.
855 */
856ObjectClass *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 */
8f5d58ef 864const char *object_get_typename(const Object *obj);
2f28d2ff
AL
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 *
31b93521 873 * Returns: the new #Type.
2f28d2ff
AL
874 */
875Type type_register_static(const TypeInfo *info);
876
877/**
878 * type_register:
879 * @info: The #TypeInfo of the new type
880 *
93148aa5 881 * Unlike type_register_static(), this call does not require @info or its
2f28d2ff
AL
882 * string members to continue to exist after the call returns.
883 *
31b93521 884 * Returns: the new #Type.
2f28d2ff
AL
885 */
886Type type_register(const TypeInfo *info);
887
aa04c9d2
IM
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 */
896void type_register_static_array(const TypeInfo *infos, int nr_infos);
897
38b5d79b
IM
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) \
906static void do_qemu_init_ ## type_array(void) \
907{ \
908 type_register_static_array(type_array, ARRAY_SIZE(type_array)); \
909} \
910type_init(do_qemu_init_ ## type_array)
911
2f28d2ff
AL
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 *
33bc94eb
PB
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
3556c233
PB
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.
2f28d2ff
AL
922 */
923ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
be17f18b
PB
924 const char *typename,
925 const char *file, int line,
926 const char *func);
2f28d2ff 927
33bc94eb
PB
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 */
2f28d2ff
AL
942ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
943 const char *typename);
944
e7cce67f
PB
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 */
951ObjectClass *object_class_get_parent(ObjectClass *klass);
952
2f28d2ff
AL
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 */
959const char *object_class_get_name(ObjectClass *klass);
960
17862378
AF
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 */
967bool object_class_is_abstract(ObjectClass *klass);
968
0466e458
PB
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 */
2f28d2ff
AL
975ObjectClass *object_class_by_name(const char *typename);
976
977void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
93c511a1 978 const char *implements_type, bool include_abstract,
2f28d2ff 979 void *opaque);
418ba9e5
AF
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 */
988GSList *object_class_get_list(const char *implements_type,
989 bool include_abstract);
47c66009
PB
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 */
999GSList *object_class_get_list_sorted(const char *implements_type,
1000 bool include_abstract);
418ba9e5 1001
57c9fafe
AL
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.
b77ade9b 1008 * Returns: @obj
57c9fafe 1009 */
b77ade9b 1010Object *object_ref(Object *obj);
57c9fafe
AL
1011
1012/**
ada03a0e 1013 * object_unref:
57c9fafe
AL
1014 * @obj: the object
1015 *
1016 * Decrease the reference count of a object. A object cannot be freed as long
1017 * as its reference count is greater than zero.
1018 */
1019void object_unref(Object *obj);
1020
1021/**
1022 * object_property_add:
1023 * @obj: the object to add a property to
1024 * @name: the name of the property. This can contain any character except for
1025 * a forward slash. In general, you should use hyphens '-' instead of
1026 * underscores '_' when naming properties.
1027 * @type: the type name of the property. This namespace is pretty loosely
1028 * defined. Sub namespaces are constructed by using a prefix and then
1029 * to angle brackets. For instance, the type 'virtio-net-pci' in the
1030 * 'link' namespace would be 'link<virtio-net-pci>'.
1031 * @get: The getter to be called to read a property. If this is NULL, then
1032 * the property cannot be read.
1033 * @set: the setter to be called to write a property. If this is NULL,
1034 * then the property cannot be written.
1035 * @release: called when the property is removed from the object. This is
1036 * meant to allow a property to free its opaque upon object
1037 * destruction. This may be NULL.
1038 * @opaque: an opaque pointer to pass to the callbacks for the property
1039 * @errp: returns an error if this function fails
64607d08
PB
1040 *
1041 * Returns: The #ObjectProperty; this can be used to set the @resolve
1042 * callback for child and link properties.
57c9fafe 1043 */
64607d08
PB
1044ObjectProperty *object_property_add(Object *obj, const char *name,
1045 const char *type,
1046 ObjectPropertyAccessor *get,
1047 ObjectPropertyAccessor *set,
1048 ObjectPropertyRelease *release,
1049 void *opaque, Error **errp);
57c9fafe 1050
e82df248 1051void object_property_del(Object *obj, const char *name, Error **errp);
57c9fafe 1052
16bf7f52
DB
1053ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name,
1054 const char *type,
1055 ObjectPropertyAccessor *get,
1056 ObjectPropertyAccessor *set,
1057 ObjectPropertyRelease *release,
1058 void *opaque, Error **errp);
1059
0e76ed0a
MAL
1060/**
1061 * object_property_set_default_bool:
1062 * @prop: the property to set
1063 * @value: the value to be written to the property
1064 *
1065 * Set the property default value.
1066 */
1067void object_property_set_default_bool(ObjectProperty *prop, bool value);
1068
1069/**
1070 * object_property_set_default_str:
1071 * @prop: the property to set
1072 * @value: the value to be written to the property
1073 *
1074 * Set the property default value.
1075 */
1076void object_property_set_default_str(ObjectProperty *prop, const char *value);
1077
1078/**
1079 * object_property_set_default_int:
1080 * @prop: the property to set
1081 * @value: the value to be written to the property
1082 *
1083 * Set the property default value.
1084 */
1085void object_property_set_default_int(ObjectProperty *prop, int64_t value);
1086
1087/**
1088 * object_property_set_default_uint:
1089 * @prop: the property to set
1090 * @value: the value to be written to the property
1091 *
1092 * Set the property default value.
1093 */
1094void object_property_set_default_uint(ObjectProperty *prop, uint64_t value);
1095
8cb6789a
PB
1096/**
1097 * object_property_find:
1098 * @obj: the object
1099 * @name: the name of the property
89bfe000 1100 * @errp: returns an error if this function fails
8cb6789a
PB
1101 *
1102 * Look up a property for an object and return its #ObjectProperty if found.
1103 */
89bfe000 1104ObjectProperty *object_property_find(Object *obj, const char *name,
e82df248 1105 Error **errp);
16bf7f52
DB
1106ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1107 Error **errp);
8cb6789a 1108
7746abd8
DB
1109typedef struct ObjectPropertyIterator {
1110 ObjectClass *nextclass;
1111 GHashTableIter iter;
1112} ObjectPropertyIterator;
a00c9482
DB
1113
1114/**
1115 * object_property_iter_init:
1116 * @obj: the object
1117 *
1118 * Initializes an iterator for traversing all properties
16bf7f52 1119 * registered against an object instance, its class and all parent classes.
a00c9482
DB
1120 *
1121 * It is forbidden to modify the property list while iterating,
1122 * whether removing or adding properties.
1123 *
1124 * Typical usage pattern would be
1125 *
1126 * <example>
1127 * <title>Using object property iterators</title>
1128 * <programlisting>
1129 * ObjectProperty *prop;
7746abd8 1130 * ObjectPropertyIterator iter;
a00c9482 1131 *
7746abd8
DB
1132 * object_property_iter_init(&iter, obj);
1133 * while ((prop = object_property_iter_next(&iter))) {
a00c9482
DB
1134 * ... do something with prop ...
1135 * }
a00c9482
DB
1136 * </programlisting>
1137 * </example>
a00c9482 1138 */
7746abd8
DB
1139void object_property_iter_init(ObjectPropertyIterator *iter,
1140 Object *obj);
a00c9482 1141
961c47bb
AK
1142/**
1143 * object_class_property_iter_init:
1144 * @klass: the class
1145 *
1146 * Initializes an iterator for traversing all properties
1147 * registered against an object class and all parent classes.
1148 *
1149 * It is forbidden to modify the property list while iterating,
1150 * whether removing or adding properties.
1151 *
1152 * This can be used on abstract classes as it does not create a temporary
1153 * instance.
1154 */
1155void object_class_property_iter_init(ObjectPropertyIterator *iter,
1156 ObjectClass *klass);
1157
a00c9482
DB
1158/**
1159 * object_property_iter_next:
1160 * @iter: the iterator instance
1161 *
7746abd8
DB
1162 * Return the next available property. If no further properties
1163 * are available, a %NULL value will be returned and the @iter
1164 * pointer should not be used again after this point without
1165 * re-initializing it.
1166 *
a00c9482
DB
1167 * Returns: the next property, or %NULL when all properties
1168 * have been traversed.
1169 */
1170ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter);
1171
57c9fafe
AL
1172void object_unparent(Object *obj);
1173
1174/**
1175 * object_property_get:
1176 * @obj: the object
1177 * @v: the visitor that will receive the property value. This should be an
1178 * Output visitor and the data will be written with @name as the name.
1179 * @name: the name of the property
1180 * @errp: returns an error if this function fails
1181 *
1182 * Reads a property from a object.
1183 */
4fa45492 1184void object_property_get(Object *obj, Visitor *v, const char *name,
e82df248 1185 Error **errp);
57c9fafe 1186
7b7b7d18
PB
1187/**
1188 * object_property_set_str:
1189 * @value: the value to be written to the property
1190 * @name: the name of the property
1191 * @errp: returns an error if this function fails
1192 *
1193 * Writes a string value to a property.
1194 */
1195void object_property_set_str(Object *obj, const char *value,
e82df248 1196 const char *name, Error **errp);
7b7b7d18
PB
1197
1198/**
1199 * object_property_get_str:
1200 * @obj: the object
1201 * @name: the name of the property
1202 * @errp: returns an error if this function fails
1203 *
1204 * Returns: the value of the property, converted to a C string, or NULL if
1205 * an error occurs (including when the property value is not a string).
1206 * The caller should free the string.
1207 */
1208char *object_property_get_str(Object *obj, const char *name,
e82df248 1209 Error **errp);
7b7b7d18 1210
1d9c5a12
PB
1211/**
1212 * object_property_set_link:
1213 * @value: the value to be written to the property
1214 * @name: the name of the property
1215 * @errp: returns an error if this function fails
1216 *
1217 * Writes an object's canonical path to a property.
265b578c
MAL
1218 *
1219 * If the link property was created with
1220 * <code>OBJ_PROP_LINK_STRONG</code> bit, the old target object is
1221 * unreferenced, and a reference is added to the new target object.
1222 *
1d9c5a12
PB
1223 */
1224void object_property_set_link(Object *obj, Object *value,
e82df248 1225 const char *name, Error **errp);
1d9c5a12
PB
1226
1227/**
1228 * object_property_get_link:
1229 * @obj: the object
1230 * @name: the name of the property
1231 * @errp: returns an error if this function fails
1232 *
1233 * Returns: the value of the property, resolved from a path to an Object,
1234 * or NULL if an error occurs (including when the property value is not a
1235 * string or not a valid object path).
1236 */
1237Object *object_property_get_link(Object *obj, const char *name,
e82df248 1238 Error **errp);
1d9c5a12 1239
7b7b7d18
PB
1240/**
1241 * object_property_set_bool:
1242 * @value: the value to be written to the property
1243 * @name: the name of the property
1244 * @errp: returns an error if this function fails
1245 *
1246 * Writes a bool value to a property.
1247 */
1248void object_property_set_bool(Object *obj, bool value,
e82df248 1249 const char *name, Error **errp);
7b7b7d18
PB
1250
1251/**
1252 * object_property_get_bool:
1253 * @obj: the object
1254 * @name: the name of the property
1255 * @errp: returns an error if this function fails
1256 *
1257 * Returns: the value of the property, converted to a boolean, or NULL if
1258 * an error occurs (including when the property value is not a bool).
1259 */
1260bool object_property_get_bool(Object *obj, const char *name,
e82df248 1261 Error **errp);
7b7b7d18
PB
1262
1263/**
1264 * object_property_set_int:
1265 * @value: the value to be written to the property
1266 * @name: the name of the property
1267 * @errp: returns an error if this function fails
1268 *
1269 * Writes an integer value to a property.
1270 */
1271void object_property_set_int(Object *obj, int64_t value,
e82df248 1272 const char *name, Error **errp);
7b7b7d18
PB
1273
1274/**
1275 * object_property_get_int:
1276 * @obj: the object
1277 * @name: the name of the property
1278 * @errp: returns an error if this function fails
1279 *
b29b47e9 1280 * Returns: the value of the property, converted to an integer, or negative if
7b7b7d18
PB
1281 * an error occurs (including when the property value is not an integer).
1282 */
1283int64_t object_property_get_int(Object *obj, const char *name,
e82df248 1284 Error **errp);
7b7b7d18 1285
3152779c
MAL
1286/**
1287 * object_property_set_uint:
1288 * @value: the value to be written to the property
1289 * @name: the name of the property
1290 * @errp: returns an error if this function fails
1291 *
1292 * Writes an unsigned integer value to a property.
1293 */
1294void object_property_set_uint(Object *obj, uint64_t value,
1295 const char *name, Error **errp);
1296
1297/**
1298 * object_property_get_uint:
1299 * @obj: the object
1300 * @name: the name of the property
1301 * @errp: returns an error if this function fails
1302 *
1303 * Returns: the value of the property, converted to an unsigned integer, or 0
1304 * an error occurs (including when the property value is not an integer).
1305 */
1306uint64_t object_property_get_uint(Object *obj, const char *name,
1307 Error **errp);
1308
1f21772d
HT
1309/**
1310 * object_property_get_enum:
1311 * @obj: the object
1312 * @name: the name of the property
a3590dac 1313 * @typename: the name of the enum data type
1f21772d
HT
1314 * @errp: returns an error if this function fails
1315 *
1316 * Returns: the value of the property, converted to an integer, or
1317 * undefined if an error occurs (including when the property value is not
1318 * an enum).
1319 */
1320int object_property_get_enum(Object *obj, const char *name,
a3590dac 1321 const char *typename, Error **errp);
1f21772d 1322
57c9fafe
AL
1323/**
1324 * object_property_set:
1325 * @obj: the object
1326 * @v: the visitor that will be used to write the property value. This should
1327 * be an Input visitor and the data will be first read with @name as the
1328 * name and then written as the property value.
1329 * @name: the name of the property
1330 * @errp: returns an error if this function fails
1331 *
1332 * Writes a property to a object.
1333 */
4fa45492 1334void object_property_set(Object *obj, Visitor *v, const char *name,
e82df248 1335 Error **errp);
57c9fafe 1336
b2cd7dee
PB
1337/**
1338 * object_property_parse:
1339 * @obj: the object
1340 * @string: the string that will be used to parse the property value.
1341 * @name: the name of the property
1342 * @errp: returns an error if this function fails
1343 *
1344 * Parses a string and writes the result into a property of an object.
1345 */
1346void object_property_parse(Object *obj, const char *string,
e82df248 1347 const char *name, Error **errp);
b2cd7dee
PB
1348
1349/**
1350 * object_property_print:
1351 * @obj: the object
1352 * @name: the name of the property
0b7593e0 1353 * @human: if true, print for human consumption
b2cd7dee
PB
1354 * @errp: returns an error if this function fails
1355 *
1356 * Returns a string representation of the value of the property. The
1357 * caller shall free the string.
1358 */
0b7593e0 1359char *object_property_print(Object *obj, const char *name, bool human,
e82df248 1360 Error **errp);
b2cd7dee 1361
57c9fafe 1362/**
438e1c79 1363 * object_property_get_type:
57c9fafe
AL
1364 * @obj: the object
1365 * @name: the name of the property
1366 * @errp: returns an error if this function fails
1367 *
1368 * Returns: The type name of the property.
1369 */
1370const char *object_property_get_type(Object *obj, const char *name,
e82df248 1371 Error **errp);
57c9fafe
AL
1372
1373/**
1374 * object_get_root:
1375 *
1376 * Returns: the root object of the composition tree
1377 */
1378Object *object_get_root(void);
1379
bc2256c4
DB
1380
1381/**
1382 * object_get_objects_root:
1383 *
1384 * Get the container object that holds user created
1385 * object instances. This is the object at path
1386 * "/objects"
1387 *
1388 * Returns: the user object container
1389 */
1390Object *object_get_objects_root(void);
1391
7c47c4ea
PX
1392/**
1393 * object_get_internal_root:
1394 *
1395 * Get the container object that holds internally used object
1396 * instances. Any object which is put into this container must not be
1397 * user visible, and it will not be exposed in the QOM tree.
1398 *
1399 * Returns: the internal object container
1400 */
1401Object *object_get_internal_root(void);
1402
11f590b1
SH
1403/**
1404 * object_get_canonical_path_component:
1405 *
1406 * Returns: The final component in the object's canonical path. The canonical
1407 * path is the path within the composition tree starting from the root.
770dec26 1408 * %NULL if the object doesn't have a parent (and thus a canonical path).
11f590b1 1409 */
ddfb0baa 1410char *object_get_canonical_path_component(Object *obj);
11f590b1 1411
57c9fafe
AL
1412/**
1413 * object_get_canonical_path:
1414 *
1415 * Returns: The canonical path for a object. This is the path within the
1416 * composition tree starting from the root.
1417 */
ddfb0baa 1418char *object_get_canonical_path(Object *obj);
57c9fafe
AL
1419
1420/**
1421 * object_resolve_path:
1422 * @path: the path to resolve
1423 * @ambiguous: returns true if the path resolution failed because of an
1424 * ambiguous match
1425 *
1426 * There are two types of supported paths--absolute paths and partial paths.
1427 *
1428 * Absolute paths are derived from the root object and can follow child<> or
1429 * link<> properties. Since they can follow link<> properties, they can be
1430 * arbitrarily long. Absolute paths look like absolute filenames and are
1431 * prefixed with a leading slash.
1432 *
1433 * Partial paths look like relative filenames. They do not begin with a
1434 * prefix. The matching rules for partial paths are subtle but designed to make
1435 * specifying objects easy. At each level of the composition tree, the partial
1436 * path is matched as an absolute path. The first match is not returned. At
1437 * least two matches are searched for. A successful result is only returned if
02fe2db6
PB
1438 * only one match is found. If more than one match is found, a flag is
1439 * returned to indicate that the match was ambiguous.
57c9fafe
AL
1440 *
1441 * Returns: The matched object or NULL on path lookup failure.
1442 */
1443Object *object_resolve_path(const char *path, bool *ambiguous);
1444
02fe2db6
PB
1445/**
1446 * object_resolve_path_type:
1447 * @path: the path to resolve
1448 * @typename: the type to look for.
1449 * @ambiguous: returns true if the path resolution failed because of an
1450 * ambiguous match
1451 *
1452 * This is similar to object_resolve_path. However, when looking for a
1453 * partial path only matches that implement the given type are considered.
1454 * This restricts the search and avoids spuriously flagging matches as
1455 * ambiguous.
1456 *
1457 * For both partial and absolute paths, the return value goes through
1458 * a dynamic cast to @typename. This is important if either the link,
1459 * or the typename itself are of interface types.
1460 *
1461 * Returns: The matched object or NULL on path lookup failure.
1462 */
1463Object *object_resolve_path_type(const char *path, const char *typename,
1464 bool *ambiguous);
1465
a612b2a6
PB
1466/**
1467 * object_resolve_path_component:
1468 * @parent: the object in which to resolve the path
1469 * @part: the component to resolve.
1470 *
1471 * This is similar to object_resolve_path with an absolute path, but it
1472 * only resolves one element (@part) and takes the others from @parent.
1473 *
1474 * Returns: The resolved object or NULL on path lookup failure.
1475 */
ddfb0baa 1476Object *object_resolve_path_component(Object *parent, const char *part);
a612b2a6 1477
57c9fafe
AL
1478/**
1479 * object_property_add_child:
1480 * @obj: the object to add a property to
1481 * @name: the name of the property
1482 * @child: the child object
0210b39d 1483 * @errp: if an error occurs, a pointer to an area to store the error
57c9fafe
AL
1484 *
1485 * Child properties form the composition tree. All objects need to be a child
1486 * of another object. Objects can only be a child of one object.
1487 *
1488 * There is no way for a child to determine what its parent is. It is not
1489 * a bidirectional relationship. This is by design.
358b5465
AB
1490 *
1491 * The value of a child property as a C string will be the child object's
1492 * canonical path. It can be retrieved using object_property_get_str().
1493 * The child object itself can be retrieved using object_property_get_link().
70251887
MA
1494 *
1495 * Returns: The newly added property on success, or %NULL on failure.
57c9fafe 1496 */
70251887
MA
1497ObjectProperty *object_property_add_child(Object *obj, const char *name,
1498 Object *child, Error **errp);
57c9fafe 1499
9561fda8
SH
1500typedef enum {
1501 /* Unref the link pointer when the property is deleted */
265b578c 1502 OBJ_PROP_LINK_STRONG = 0x1,
9941d37b
MAL
1503
1504 /* private */
1505 OBJ_PROP_LINK_DIRECT = 0x2,
840ecdfb 1506 OBJ_PROP_LINK_CLASS = 0x4,
9561fda8
SH
1507} ObjectPropertyLinkFlags;
1508
39f72ef9
SH
1509/**
1510 * object_property_allow_set_link:
1511 *
1512 * The default implementation of the object_property_add_link() check()
1513 * callback function. It allows the link property to be set and never returns
1514 * an error.
1515 */
8f5d58ef 1516void object_property_allow_set_link(const Object *, const char *,
39f72ef9
SH
1517 Object *, Error **);
1518
57c9fafe
AL
1519/**
1520 * object_property_add_link:
1521 * @obj: the object to add a property to
1522 * @name: the name of the property
1523 * @type: the qobj type of the link
36854207 1524 * @targetp: a pointer to where the link object reference is stored
39f72ef9 1525 * @check: callback to veto setting or NULL if the property is read-only
9561fda8 1526 * @flags: additional options for the link
0210b39d 1527 * @errp: if an error occurs, a pointer to an area to store the error
57c9fafe
AL
1528 *
1529 * Links establish relationships between objects. Links are unidirectional
1530 * although two links can be combined to form a bidirectional relationship
1531 * between objects.
1532 *
1533 * Links form the graph in the object model.
6c232d2f 1534 *
39f72ef9
SH
1535 * The <code>@check()</code> callback is invoked when
1536 * object_property_set_link() is called and can raise an error to prevent the
1537 * link being set. If <code>@check</code> is NULL, the property is read-only
1538 * and cannot be set.
1539 *
6c232d2f
PB
1540 * Ownership of the pointer that @child points to is transferred to the
1541 * link property. The reference count for <code>*@child</code> is
1542 * managed by the property from after the function returns till the
9561fda8 1543 * property is deleted with object_property_del(). If the
265b578c
MAL
1544 * <code>@flags</code> <code>OBJ_PROP_LINK_STRONG</code> bit is set,
1545 * the reference count is decremented when the property is deleted or
1546 * modified.
70251887
MA
1547 *
1548 * Returns: The newly added property on success, or %NULL on failure.
57c9fafe 1549 */
70251887 1550ObjectProperty *object_property_add_link(Object *obj, const char *name,
36854207 1551 const char *type, Object **targetp,
8f5d58ef 1552 void (*check)(const Object *obj, const char *name,
39f72ef9 1553 Object *val, Error **errp),
9561fda8 1554 ObjectPropertyLinkFlags flags,
e82df248 1555 Error **errp);
57c9fafe 1556
840ecdfb
MAL
1557ObjectProperty *object_class_property_add_link(ObjectClass *oc,
1558 const char *name,
1559 const char *type, ptrdiff_t offset,
1560 void (*check)(const Object *obj, const char *name,
1561 Object *val, Error **errp),
1562 ObjectPropertyLinkFlags flags,
1563 Error **errp);
1564
57c9fafe
AL
1565/**
1566 * object_property_add_str:
1567 * @obj: the object to add a property to
1568 * @name: the name of the property
1569 * @get: the getter or NULL if the property is write-only. This function must
1570 * return a string to be freed by g_free().
1571 * @set: the setter or NULL if the property is read-only
1572 * @errp: if an error occurs, a pointer to an area to store the error
1573 *
1574 * Add a string property using getters/setters. This function will add a
1575 * property of type 'string'.
70251887
MA
1576 *
1577 * Returns: The newly added property on success, or %NULL on failure.
57c9fafe 1578 */
70251887 1579ObjectProperty *object_property_add_str(Object *obj, const char *name,
e82df248
MT
1580 char *(*get)(Object *, Error **),
1581 void (*set)(Object *, const char *, Error **),
1582 Error **errp);
2f28d2ff 1583
a3a16211
MAL
1584ObjectProperty *object_class_property_add_str(ObjectClass *klass,
1585 const char *name,
16bf7f52
DB
1586 char *(*get)(Object *, Error **),
1587 void (*set)(Object *, const char *,
1588 Error **),
1589 Error **errp);
1590
0e558843
AL
1591/**
1592 * object_property_add_bool:
1593 * @obj: the object to add a property to
1594 * @name: the name of the property
1595 * @get: the getter or NULL if the property is write-only.
1596 * @set: the setter or NULL if the property is read-only
1597 * @errp: if an error occurs, a pointer to an area to store the error
1598 *
1599 * Add a bool property using getters/setters. This function will add a
1600 * property of type 'bool'.
70251887
MA
1601 *
1602 * Returns: The newly added property on success, or %NULL on failure.
0e558843 1603 */
70251887 1604ObjectProperty *object_property_add_bool(Object *obj, const char *name,
e82df248
MT
1605 bool (*get)(Object *, Error **),
1606 void (*set)(Object *, bool, Error **),
1607 Error **errp);
0e558843 1608
a3a16211
MAL
1609ObjectProperty *object_class_property_add_bool(ObjectClass *klass,
1610 const char *name,
16bf7f52
DB
1611 bool (*get)(Object *, Error **),
1612 void (*set)(Object *, bool, Error **),
1613 Error **errp);
1614
a8e3fbed
DB
1615/**
1616 * object_property_add_enum:
1617 * @obj: the object to add a property to
1618 * @name: the name of the property
1619 * @typename: the name of the enum data type
1620 * @get: the getter or %NULL if the property is write-only.
1621 * @set: the setter or %NULL if the property is read-only
1622 * @errp: if an error occurs, a pointer to an area to store the error
1623 *
1624 * Add an enum property using getters/setters. This function will add a
1625 * property of type '@typename'.
70251887
MA
1626 *
1627 * Returns: The newly added property on success, or %NULL on failure.
a8e3fbed 1628 */
70251887 1629ObjectProperty *object_property_add_enum(Object *obj, const char *name,
a8e3fbed 1630 const char *typename,
f7abe0ec 1631 const QEnumLookup *lookup,
a8e3fbed
DB
1632 int (*get)(Object *, Error **),
1633 void (*set)(Object *, int, Error **),
1634 Error **errp);
1635
a3a16211
MAL
1636ObjectProperty *object_class_property_add_enum(ObjectClass *klass,
1637 const char *name,
16bf7f52 1638 const char *typename,
f7abe0ec 1639 const QEnumLookup *lookup,
16bf7f52
DB
1640 int (*get)(Object *, Error **),
1641 void (*set)(Object *, int, Error **),
1642 Error **errp);
1643
8e099d14
DG
1644/**
1645 * object_property_add_tm:
1646 * @obj: the object to add a property to
1647 * @name: the name of the property
1648 * @get: the getter or NULL if the property is write-only.
1649 * @errp: if an error occurs, a pointer to an area to store the error
1650 *
1651 * Add a read-only struct tm valued property using a getter function.
1652 * This function will add a property of type 'struct tm'.
70251887
MA
1653 *
1654 * Returns: The newly added property on success, or %NULL on failure.
8e099d14 1655 */
70251887 1656ObjectProperty *object_property_add_tm(Object *obj, const char *name,
8e099d14
DG
1657 void (*get)(Object *, struct tm *, Error **),
1658 Error **errp);
1659
a3a16211
MAL
1660ObjectProperty *object_class_property_add_tm(ObjectClass *klass,
1661 const char *name,
16bf7f52
DB
1662 void (*get)(Object *, struct tm *, Error **),
1663 Error **errp);
1664
836e1b38
FF
1665typedef enum {
1666 /* Automatically add a getter to the property */
1667 OBJ_PROP_FLAG_READ = 1 << 0,
1668 /* Automatically add a setter to the property */
1669 OBJ_PROP_FLAG_WRITE = 1 << 1,
1670 /* Automatically add a getter and a setter to the property */
1671 OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE),
1672} ObjectPropertyFlags;
1673
a25ebcac
MT
1674/**
1675 * object_property_add_uint8_ptr:
1676 * @obj: the object to add a property to
1677 * @name: the name of the property
1678 * @v: pointer to value
836e1b38 1679 * @flags: bitwise-or'd ObjectPropertyFlags
a25ebcac
MT
1680 * @errp: if an error occurs, a pointer to an area to store the error
1681 *
1682 * Add an integer property in memory. This function will add a
1683 * property of type 'uint8'.
70251887
MA
1684 *
1685 * Returns: The newly added property on success, or %NULL on failure.
a25ebcac 1686 */
70251887 1687ObjectProperty *object_property_add_uint8_ptr(Object *obj, const char *name,
836e1b38
FF
1688 const uint8_t *v, ObjectPropertyFlags flags,
1689 Error **errp);
1690
a3a16211
MAL
1691ObjectProperty *object_class_property_add_uint8_ptr(ObjectClass *klass,
1692 const char *name,
836e1b38
FF
1693 const uint8_t *v,
1694 ObjectPropertyFlags flags,
1695 Error **errp);
a25ebcac
MT
1696
1697/**
1698 * object_property_add_uint16_ptr:
1699 * @obj: the object to add a property to
1700 * @name: the name of the property
1701 * @v: pointer to value
836e1b38 1702 * @flags: bitwise-or'd ObjectPropertyFlags
a25ebcac
MT
1703 * @errp: if an error occurs, a pointer to an area to store the error
1704 *
1705 * Add an integer property in memory. This function will add a
1706 * property of type 'uint16'.
70251887
MA
1707 *
1708 * Returns: The newly added property on success, or %NULL on failure.
a25ebcac 1709 */
70251887 1710ObjectProperty *object_property_add_uint16_ptr(Object *obj, const char *name,
836e1b38
FF
1711 const uint16_t *v,
1712 ObjectPropertyFlags flags,
1713 Error **errp);
1714
a3a16211
MAL
1715ObjectProperty *object_class_property_add_uint16_ptr(ObjectClass *klass,
1716 const char *name,
836e1b38
FF
1717 const uint16_t *v,
1718 ObjectPropertyFlags flags,
1719 Error **errp);
a25ebcac
MT
1720
1721/**
1722 * object_property_add_uint32_ptr:
1723 * @obj: the object to add a property to
1724 * @name: the name of the property
1725 * @v: pointer to value
836e1b38 1726 * @flags: bitwise-or'd ObjectPropertyFlags
a25ebcac
MT
1727 * @errp: if an error occurs, a pointer to an area to store the error
1728 *
1729 * Add an integer property in memory. This function will add a
1730 * property of type 'uint32'.
70251887
MA
1731 *
1732 * Returns: The newly added property on success, or %NULL on failure.
a25ebcac 1733 */
70251887 1734ObjectProperty *object_property_add_uint32_ptr(Object *obj, const char *name,
836e1b38
FF
1735 const uint32_t *v,
1736 ObjectPropertyFlags flags,
1737 Error **errp);
1738
a3a16211
MAL
1739ObjectProperty *object_class_property_add_uint32_ptr(ObjectClass *klass,
1740 const char *name,
836e1b38
FF
1741 const uint32_t *v,
1742 ObjectPropertyFlags flags,
1743 Error **errp);
a25ebcac
MT
1744
1745/**
1746 * object_property_add_uint64_ptr:
1747 * @obj: the object to add a property to
1748 * @name: the name of the property
1749 * @v: pointer to value
836e1b38 1750 * @flags: bitwise-or'd ObjectPropertyFlags
a25ebcac
MT
1751 * @errp: if an error occurs, a pointer to an area to store the error
1752 *
1753 * Add an integer property in memory. This function will add a
1754 * property of type 'uint64'.
70251887
MA
1755 *
1756 * Returns: The newly added property on success, or %NULL on failure.
a25ebcac 1757 */
70251887 1758ObjectProperty *object_property_add_uint64_ptr(Object *obj, const char *name,
836e1b38
FF
1759 const uint64_t *v,
1760 ObjectPropertyFlags flags,
1761 Error **Errp);
1762
a3a16211
MAL
1763ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass,
1764 const char *name,
836e1b38
FF
1765 const uint64_t *v,
1766 ObjectPropertyFlags flags,
1767 Error **Errp);
a25ebcac 1768
ef7c7ff6
SH
1769/**
1770 * object_property_add_alias:
1771 * @obj: the object to add a property to
1772 * @name: the name of the property
1773 * @target_obj: the object to forward property access to
1774 * @target_name: the name of the property on the forwarded object
1775 * @errp: if an error occurs, a pointer to an area to store the error
1776 *
1777 * Add an alias for a property on an object. This function will add a property
1778 * of the same type as the forwarded property.
1779 *
1780 * The caller must ensure that <code>@target_obj</code> stays alive as long as
1781 * this property exists. In the case of a child object or an alias on the same
1782 * object this will be the case. For aliases to other objects the caller is
1783 * responsible for taking a reference.
70251887
MA
1784 *
1785 * Returns: The newly added property on success, or %NULL on failure.
ef7c7ff6 1786 */
70251887 1787ObjectProperty *object_property_add_alias(Object *obj, const char *name,
ef7c7ff6
SH
1788 Object *target_obj, const char *target_name,
1789 Error **errp);
1790
fb9e7e33
PB
1791/**
1792 * object_property_add_const_link:
1793 * @obj: the object to add a property to
1794 * @name: the name of the property
1795 * @target: the object to be referred by the link
1796 * @errp: if an error occurs, a pointer to an area to store the error
1797 *
1798 * Add an unmodifiable link for a property on an object. This function will
1799 * add a property of type link<TYPE> where TYPE is the type of @target.
1800 *
1801 * The caller must ensure that @target stays alive as long as
1802 * this property exists. In the case @target is a child of @obj,
1803 * this will be the case. Otherwise, the caller is responsible for
1804 * taking a reference.
70251887
MA
1805 *
1806 * Returns: The newly added property on success, or %NULL on failure.
fb9e7e33 1807 */
70251887 1808ObjectProperty *object_property_add_const_link(Object *obj, const char *name,
fb9e7e33
PB
1809 Object *target, Error **errp);
1810
80742642
GA
1811/**
1812 * object_property_set_description:
1813 * @obj: the object owning the property
1814 * @name: the name of the property
1815 * @description: the description of the property on the object
80742642
GA
1816 *
1817 * Set an object property's description.
1818 *
1819 */
1820void object_property_set_description(Object *obj, const char *name,
7eecec7d 1821 const char *description);
16bf7f52 1822void object_class_property_set_description(ObjectClass *klass, const char *name,
7eecec7d 1823 const char *description);
80742642 1824
32efc535
PB
1825/**
1826 * object_child_foreach:
1827 * @obj: the object whose children will be navigated
1828 * @fn: the iterator function to be called
1829 * @opaque: an opaque value that will be passed to the iterator
1830 *
1831 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1832 * non-zero.
1833 *
b604a854
PF
1834 * It is forbidden to add or remove children from @obj from the @fn
1835 * callback.
1836 *
32efc535
PB
1837 * Returns: The last value returned by @fn, or 0 if there is no child.
1838 */
1839int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1840 void *opaque);
1841
d714b8de
PC
1842/**
1843 * object_child_foreach_recursive:
1844 * @obj: the object whose children will be navigated
1845 * @fn: the iterator function to be called
1846 * @opaque: an opaque value that will be passed to the iterator
1847 *
1848 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1849 * non-zero. Calls recursively, all child nodes of @obj will also be passed
1850 * all the way down to the leaf nodes of the tree. Depth first ordering.
1851 *
b604a854
PF
1852 * It is forbidden to add or remove children from @obj (or its
1853 * child nodes) from the @fn callback.
1854 *
d714b8de
PC
1855 * Returns: The last value returned by @fn, or 0 if there is no child.
1856 */
1857int object_child_foreach_recursive(Object *obj,
1858 int (*fn)(Object *child, void *opaque),
1859 void *opaque);
a612b2a6
PB
1860/**
1861 * container_get:
dfe47e70 1862 * @root: root of the #path, e.g., object_get_root()
a612b2a6
PB
1863 * @path: path to the container
1864 *
1865 * Return a container object whose path is @path. Create more containers
1866 * along the path if necessary.
1867 *
1868 * Returns: the container object.
1869 */
dfe47e70 1870Object *container_get(Object *root, const char *path);
a612b2a6 1871
3f97b53a
BR
1872/**
1873 * object_type_get_instance_size:
1874 * @typename: Name of the Type whose instance_size is required
1875 *
1876 * Returns the instance_size of the given @typename.
1877 */
1878size_t object_type_get_instance_size(const char *typename);
f60a1cdc 1879
4df81616
MAL
1880/**
1881 * object_property_help:
1882 * @name: the name of the property
1883 * @type: the type of the property
1884 * @defval: the default value
1885 * @description: description of the property
1886 *
1887 * Returns: a user-friendly formatted string describing the property
1888 * for help purposes.
1889 */
1890char *object_property_help(const char *name, const char *type,
1891 QObject *defval, const char *description);
1892
f60a1cdc
MAL
1893G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref)
1894
2f28d2ff 1895#endif