]> git.proxmox.com Git - qemu.git/blame - include/qemu/object.h
object: add object_property_add_bool (v2)
[qemu.git] / include / qemu / 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
17#include <glib.h>
18#include <stdint.h>
19#include <stdbool.h>
57c9fafe
AL
20#include "qemu-queue.h"
21
22struct Visitor;
23struct Error;
2f28d2ff
AL
24
25struct TypeImpl;
26typedef struct TypeImpl *Type;
27
28typedef struct ObjectClass ObjectClass;
29typedef struct Object Object;
30
31typedef struct TypeInfo TypeInfo;
32
33typedef struct InterfaceClass InterfaceClass;
34typedef struct InterfaceInfo InterfaceInfo;
35
745549c8 36#define TYPE_OBJECT "object"
2f28d2ff
AL
37
38/**
39 * SECTION:object.h
40 * @title:Base Object Type System
41 * @short_description: interfaces for creating new types and objects
42 *
43 * The QEMU Object Model provides a framework for registering user creatable
44 * types and instantiating objects from those types. QOM provides the following
45 * features:
46 *
47 * - System for dynamically registering types
48 * - Support for single-inheritance of types
49 * - Multiple inheritance of stateless interfaces
50 *
51 * <example>
52 * <title>Creating a minimal type</title>
53 * <programlisting>
54 * #include "qdev.h"
55 *
56 * #define TYPE_MY_DEVICE "my-device"
57 *
0815a859
PB
58 * // No new virtual functions: we can reuse the typedef for the
59 * // superclass.
60 * typedef DeviceClass MyDeviceClass;
2f28d2ff
AL
61 * typedef struct MyDevice
62 * {
63 * DeviceState parent;
64 *
65 * int reg0, reg1, reg2;
66 * } MyDevice;
67 *
68 * static TypeInfo my_device_info = {
69 * .name = TYPE_MY_DEVICE,
70 * .parent = TYPE_DEVICE,
71 * .instance_size = sizeof(MyDevice),
72 * };
73 *
83f7d43a 74 * static void my_device_register_types(void)
2f28d2ff
AL
75 * {
76 * type_register_static(&my_device_info);
77 * }
78 *
83f7d43a 79 * type_init(my_device_register_types)
2f28d2ff
AL
80 * </programlisting>
81 * </example>
82 *
83 * In the above example, we create a simple type that is described by #TypeInfo.
84 * #TypeInfo describes information about the type including what it inherits
85 * from, the instance and class size, and constructor/destructor hooks.
86 *
87 * Every type has an #ObjectClass associated with it. #ObjectClass derivatives
88 * are instantiated dynamically but there is only ever one instance for any
89 * given type. The #ObjectClass typically holds a table of function pointers
90 * for the virtual methods implemented by this type.
91 *
92 * Using object_new(), a new #Object derivative will be instantiated. You can
93 * cast an #Object to a subclass (or base-class) type using
0815a859
PB
94 * object_dynamic_cast(). You typically want to define macro wrappers around
95 * OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
96 * specific type:
97 *
98 * <example>
99 * <title>Typecasting macros</title>
100 * <programlisting>
101 * #define MY_DEVICE_GET_CLASS(obj) \
102 * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
103 * #define MY_DEVICE_CLASS(klass) \
104 * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
105 * #define MY_DEVICE(obj) \
106 * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
107 * </programlisting>
108 * </example>
2f28d2ff
AL
109 *
110 * # Class Initialization #
111 *
112 * Before an object is initialized, the class for the object must be
113 * initialized. There is only one class object for all instance objects
114 * that is created lazily.
115 *
116 * Classes are initialized by first initializing any parent classes (if
117 * necessary). After the parent class object has initialized, it will be
118 * copied into the current class object and any additional storage in the
119 * class object is zero filled.
120 *
121 * The effect of this is that classes automatically inherit any virtual
122 * function pointers that the parent class has already initialized. All
123 * other fields will be zero filled.
124 *
125 * Once all of the parent classes have been initialized, #TypeInfo::class_init
126 * is called to let the class being instantiated provide default initialize for
93148aa5 127 * its virtual functions. Here is how the above example might be modified
0815a859
PB
128 * to introduce an overridden virtual function:
129 *
130 * <example>
131 * <title>Overriding a virtual function</title>
132 * <programlisting>
133 * #include "qdev.h"
134 *
135 * void my_device_class_init(ObjectClass *klass, void *class_data)
136 * {
137 * DeviceClass *dc = DEVICE_CLASS(klass);
138 * dc->reset = my_device_reset;
139 * }
140 *
141 * static TypeInfo my_device_info = {
142 * .name = TYPE_MY_DEVICE,
143 * .parent = TYPE_DEVICE,
144 * .instance_size = sizeof(MyDevice),
145 * .class_init = my_device_class_init,
146 * };
147 * </programlisting>
148 * </example>
149 *
150 * Introducing new virtual functions requires a class to define its own
151 * struct and to add a .class_size member to the TypeInfo. Each function
152 * will also have a wrapper to call it easily:
153 *
154 * <example>
155 * <title>Defining an abstract class</title>
156 * <programlisting>
157 * #include "qdev.h"
158 *
159 * typedef struct MyDeviceClass
160 * {
161 * DeviceClass parent;
162 *
163 * void (*frobnicate) (MyDevice *obj);
164 * } MyDeviceClass;
165 *
166 * static TypeInfo my_device_info = {
167 * .name = TYPE_MY_DEVICE,
168 * .parent = TYPE_DEVICE,
169 * .instance_size = sizeof(MyDevice),
170 * .abstract = true, // or set a default in my_device_class_init
171 * .class_size = sizeof(MyDeviceClass),
172 * };
173 *
174 * void my_device_frobnicate(MyDevice *obj)
175 * {
176 * MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
177 *
178 * klass->frobnicate(obj);
179 * }
180 * </programlisting>
181 * </example>
2f28d2ff
AL
182 *
183 * # Interfaces #
184 *
185 * Interfaces allow a limited form of multiple inheritance. Instances are
186 * similar to normal types except for the fact that are only defined by
187 * their classes and never carry any state. You can dynamically cast an object
188 * to one of its #Interface types and vice versa.
189 */
190
57c9fafe
AL
191
192/**
193 * ObjectPropertyAccessor:
194 * @obj: the object that owns the property
195 * @v: the visitor that contains the property data
196 * @opaque: the object property opaque
197 * @name: the name of the property
198 * @errp: a pointer to an Error that is filled if getting/setting fails.
199 *
200 * Called when trying to get/set a property.
201 */
202typedef void (ObjectPropertyAccessor)(Object *obj,
203 struct Visitor *v,
204 void *opaque,
205 const char *name,
206 struct Error **errp);
207
208/**
209 * ObjectPropertyRelease:
210 * @obj: the object that owns the property
211 * @name: the name of the property
212 * @opaque: the opaque registered with the property
213 *
214 * Called when a property is removed from a object.
215 */
216typedef void (ObjectPropertyRelease)(Object *obj,
217 const char *name,
218 void *opaque);
219
220typedef struct ObjectProperty
221{
222 gchar *name;
223 gchar *type;
224 ObjectPropertyAccessor *get;
225 ObjectPropertyAccessor *set;
226 ObjectPropertyRelease *release;
227 void *opaque;
228
229 QTAILQ_ENTRY(ObjectProperty) node;
230} ObjectProperty;
231
2f28d2ff
AL
232/**
233 * ObjectClass:
234 *
235 * The base for all classes. The only thing that #ObjectClass contains is an
236 * integer type handle.
237 */
238struct ObjectClass
239{
240 /*< private >*/
241 Type type;
33e95c63 242 GSList *interfaces;
2f28d2ff
AL
243};
244
245/**
246 * Object:
247 *
248 * The base for all objects. The first member of this object is a pointer to
249 * a #ObjectClass. Since C guarantees that the first member of a structure
250 * always begins at byte 0 of that structure, as long as any sub-object places
251 * its parent as the first member, we can cast directly to a #Object.
252 *
253 * As a result, #Object contains a reference to the objects type as its
254 * first member. This allows identification of the real type of the object at
255 * run time.
256 *
257 * #Object also contains a list of #Interfaces that this object
258 * implements.
259 */
260struct Object
261{
262 /*< private >*/
263 ObjectClass *class;
57c9fafe
AL
264 QTAILQ_HEAD(, ObjectProperty) properties;
265 uint32_t ref;
266 Object *parent;
2f28d2ff
AL
267};
268
269/**
270 * TypeInfo:
271 * @name: The name of the type.
272 * @parent: The name of the parent type.
273 * @instance_size: The size of the object (derivative of #Object). If
274 * @instance_size is 0, then the size of the object will be the size of the
275 * parent object.
276 * @instance_init: This function is called to initialize an object. The parent
277 * class will have already been initialized so the type is only responsible
278 * for initializing its own members.
279 * @instance_finalize: This function is called during object destruction. This
280 * is called before the parent @instance_finalize function has been called.
281 * An object should only free the members that are unique to its type in this
282 * function.
283 * @abstract: If this field is true, then the class is considered abstract and
284 * cannot be directly instantiated.
285 * @class_size: The size of the class object (derivative of #ObjectClass)
286 * for this object. If @class_size is 0, then the size of the class will be
287 * assumed to be the size of the parent class. This allows a type to avoid
288 * implementing an explicit class type if they are not adding additional
289 * virtual functions.
290 * @class_init: This function is called after all parent class initialization
441dd5eb 291 * has occurred to allow a class to set its default virtual method pointers.
2f28d2ff
AL
292 * This is also the function to use to override virtual methods from a parent
293 * class.
3b50e311
PB
294 * @class_base_init: This function is called for all base classes after all
295 * parent class initialization has occurred, but before the class itself
296 * is initialized. This is the function to use to undo the effects of
297 * memcpy from the parent class to the descendents.
2f28d2ff
AL
298 * @class_finalize: This function is called during class destruction and is
299 * meant to release and dynamic parameters allocated by @class_init.
3b50e311
PB
300 * @class_data: Data to pass to the @class_init, @class_base_init and
301 * @class_finalize functions. This can be useful when building dynamic
302 * classes.
2f28d2ff
AL
303 * @interfaces: The list of interfaces associated with this type. This
304 * should point to a static array that's terminated with a zero filled
305 * element.
306 */
307struct TypeInfo
308{
309 const char *name;
310 const char *parent;
311
312 size_t instance_size;
313 void (*instance_init)(Object *obj);
314 void (*instance_finalize)(Object *obj);
315
316 bool abstract;
317 size_t class_size;
318
319 void (*class_init)(ObjectClass *klass, void *data);
3b50e311 320 void (*class_base_init)(ObjectClass *klass, void *data);
2f28d2ff
AL
321 void (*class_finalize)(ObjectClass *klass, void *data);
322 void *class_data;
323
324 InterfaceInfo *interfaces;
325};
326
327/**
328 * OBJECT:
329 * @obj: A derivative of #Object
330 *
331 * Converts an object to a #Object. Since all objects are #Objects,
332 * this function will always succeed.
333 */
334#define OBJECT(obj) \
335 ((Object *)(obj))
336
1ed5b918
PB
337/**
338 * OBJECT_CLASS:
a0dbf408 339 * @class: A derivative of #ObjectClass.
1ed5b918
PB
340 *
341 * Converts a class to an #ObjectClass. Since all objects are #Objects,
342 * this function will always succeed.
343 */
344#define OBJECT_CLASS(class) \
345 ((ObjectClass *)(class))
346
2f28d2ff
AL
347/**
348 * OBJECT_CHECK:
349 * @type: The C type to use for the return value.
350 * @obj: A derivative of @type to cast.
351 * @name: The QOM typename of @type
352 *
353 * A type safe version of @object_dynamic_cast_assert. Typically each class
354 * will define a macro based on this type to perform type safe dynamic_casts to
355 * this object type.
356 *
357 * If an invalid object is passed to this function, a run time assert will be
358 * generated.
359 */
360#define OBJECT_CHECK(type, obj, name) \
1ed5b918 361 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name)))
2f28d2ff
AL
362
363/**
364 * OBJECT_CLASS_CHECK:
365 * @class: The C type to use for the return value.
366 * @obj: A derivative of @type to cast.
367 * @name: the QOM typename of @class.
368 *
1ed5b918
PB
369 * A type safe version of @object_class_dynamic_cast_assert. This macro is
370 * typically wrapped by each type to perform type safe casts of a class to a
371 * specific class type.
2f28d2ff
AL
372 */
373#define OBJECT_CLASS_CHECK(class, obj, name) \
1ed5b918 374 ((class *)object_class_dynamic_cast_assert(OBJECT_CLASS(obj), (name)))
2f28d2ff
AL
375
376/**
377 * OBJECT_GET_CLASS:
378 * @class: The C type to use for the return value.
379 * @obj: The object to obtain the class for.
380 * @name: The QOM typename of @obj.
381 *
382 * This function will return a specific class for a given object. Its generally
383 * used by each type to provide a type safe macro to get a specific class type
384 * from an object.
385 */
386#define OBJECT_GET_CLASS(class, obj, name) \
387 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
388
33e95c63
AL
389/**
390 * InterfaceInfo:
391 * @type: The name of the interface.
392 *
393 * The information associated with an interface.
394 */
395struct InterfaceInfo {
396 const char *type;
397};
398
2f28d2ff
AL
399/**
400 * InterfaceClass:
401 * @parent_class: the base class
402 *
403 * The class for all interfaces. Subclasses of this class should only add
404 * virtual methods.
405 */
406struct InterfaceClass
407{
408 ObjectClass parent_class;
33e95c63
AL
409 /*< private >*/
410 ObjectClass *concrete_class;
2f28d2ff
AL
411};
412
33e95c63
AL
413#define TYPE_INTERFACE "interface"
414
2f28d2ff 415/**
33e95c63
AL
416 * INTERFACE_CLASS:
417 * @klass: class to cast from
418 * Returns: An #InterfaceClass or raise an error if cast is invalid
2f28d2ff 419 */
33e95c63
AL
420#define INTERFACE_CLASS(klass) \
421 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
2f28d2ff 422
33e95c63
AL
423/**
424 * INTERFACE_CHECK:
425 * @interface: the type to return
426 * @obj: the object to convert to an interface
427 * @name: the interface type name
428 *
429 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
430 */
431#define INTERFACE_CHECK(interface, obj, name) \
432 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name)))
2f28d2ff
AL
433
434/**
435 * object_new:
436 * @typename: The name of the type of the object to instantiate.
437 *
438 * This function will initialize a new object using heap allocated memory. This
439 * function should be paired with object_delete() to free the resources
440 * associated with the object.
441 *
442 * Returns: The newly allocated and instantiated object.
443 */
444Object *object_new(const char *typename);
445
446/**
447 * object_new_with_type:
448 * @type: The type of the object to instantiate.
449 *
450 * This function will initialize a new object using heap allocated memory. This
451 * function should be paired with object_delete() to free the resources
452 * associated with the object.
453 *
454 * Returns: The newly allocated and instantiated object.
455 */
456Object *object_new_with_type(Type type);
457
458/**
459 * object_delete:
460 * @obj: The object to free.
461 *
462 * Finalize an object and then free the memory associated with it. This should
463 * be paired with object_new() to free the resources associated with an object.
464 */
465void object_delete(Object *obj);
466
467/**
468 * object_initialize_with_type:
469 * @obj: A pointer to the memory to be used for the object.
470 * @type: The type of the object to instantiate.
471 *
472 * This function will initialize an object. The memory for the object should
473 * have already been allocated.
474 */
475void object_initialize_with_type(void *data, Type type);
476
477/**
478 * object_initialize:
479 * @obj: A pointer to the memory to be used for the object.
480 * @typename: The name of the type of the object to instantiate.
481 *
482 * This function will initialize an object. The memory for the object should
483 * have already been allocated.
484 */
485void object_initialize(void *obj, const char *typename);
486
487/**
488 * object_finalize:
489 * @obj: The object to finalize.
490 *
491 * This function destroys and object without freeing the memory associated with
492 * it.
493 */
494void object_finalize(void *obj);
495
496/**
497 * object_dynamic_cast:
498 * @obj: The object to cast.
499 * @typename: The @typename to cast to.
500 *
501 * This function will determine if @obj is-a @typename. @obj can refer to an
502 * object or an interface associated with an object.
503 *
504 * Returns: This function returns @obj on success or #NULL on failure.
505 */
506Object *object_dynamic_cast(Object *obj, const char *typename);
507
508/**
438e1c79 509 * object_dynamic_cast_assert:
2f28d2ff
AL
510 *
511 * See object_dynamic_cast() for a description of the parameters of this
512 * function. The only difference in behavior is that this function asserts
513 * instead of returning #NULL on failure.
514 */
515Object *object_dynamic_cast_assert(Object *obj, const char *typename);
516
517/**
518 * object_get_class:
519 * @obj: A derivative of #Object
520 *
521 * Returns: The #ObjectClass of the type associated with @obj.
522 */
523ObjectClass *object_get_class(Object *obj);
524
525/**
526 * object_get_typename:
527 * @obj: A derivative of #Object.
528 *
529 * Returns: The QOM typename of @obj.
530 */
531const char *object_get_typename(Object *obj);
532
533/**
534 * type_register_static:
535 * @info: The #TypeInfo of the new type.
536 *
537 * @info and all of the strings it points to should exist for the life time
538 * that the type is registered.
539 *
540 * Returns: 0 on failure, the new #Type on success.
541 */
542Type type_register_static(const TypeInfo *info);
543
544/**
545 * type_register:
546 * @info: The #TypeInfo of the new type
547 *
93148aa5 548 * Unlike type_register_static(), this call does not require @info or its
2f28d2ff
AL
549 * string members to continue to exist after the call returns.
550 *
551 * Returns: 0 on failure, the new #Type on success.
552 */
553Type type_register(const TypeInfo *info);
554
555/**
556 * object_class_dynamic_cast_assert:
557 * @klass: The #ObjectClass to attempt to cast.
558 * @typename: The QOM typename of the class to cast to.
559 *
560 * Returns: This function always returns @klass and asserts on failure.
561 */
562ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
563 const char *typename);
564
565ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
566 const char *typename);
567
e7cce67f
PB
568/**
569 * object_class_get_parent:
570 * @klass: The class to obtain the parent for.
571 *
572 * Returns: The parent for @klass or %NULL if none.
573 */
574ObjectClass *object_class_get_parent(ObjectClass *klass);
575
2f28d2ff
AL
576/**
577 * object_class_get_name:
578 * @klass: The class to obtain the QOM typename for.
579 *
580 * Returns: The QOM typename for @klass.
581 */
582const char *object_class_get_name(ObjectClass *klass);
583
0466e458
PB
584/**
585 * object_class_by_name:
586 * @typename: The QOM typename to obtain the class for.
587 *
588 * Returns: The class for @typename or %NULL if not found.
589 */
2f28d2ff
AL
590ObjectClass *object_class_by_name(const char *typename);
591
592void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
93c511a1 593 const char *implements_type, bool include_abstract,
2f28d2ff 594 void *opaque);
418ba9e5
AF
595
596/**
597 * object_class_get_list:
598 * @implements_type: The type to filter for, including its derivatives.
599 * @include_abstract: Whether to include abstract classes.
600 *
601 * Returns: A singly-linked list of the classes in reverse hashtable order.
602 */
603GSList *object_class_get_list(const char *implements_type,
604 bool include_abstract);
605
57c9fafe
AL
606/**
607 * object_ref:
608 * @obj: the object
609 *
610 * Increase the reference count of a object. A object cannot be freed as long
611 * as its reference count is greater than zero.
612 */
613void object_ref(Object *obj);
614
615/**
616 * qdef_unref:
617 * @obj: the object
618 *
619 * Decrease the reference count of a object. A object cannot be freed as long
620 * as its reference count is greater than zero.
621 */
622void object_unref(Object *obj);
623
624/**
625 * object_property_add:
626 * @obj: the object to add a property to
627 * @name: the name of the property. This can contain any character except for
628 * a forward slash. In general, you should use hyphens '-' instead of
629 * underscores '_' when naming properties.
630 * @type: the type name of the property. This namespace is pretty loosely
631 * defined. Sub namespaces are constructed by using a prefix and then
632 * to angle brackets. For instance, the type 'virtio-net-pci' in the
633 * 'link' namespace would be 'link<virtio-net-pci>'.
634 * @get: The getter to be called to read a property. If this is NULL, then
635 * the property cannot be read.
636 * @set: the setter to be called to write a property. If this is NULL,
637 * then the property cannot be written.
638 * @release: called when the property is removed from the object. This is
639 * meant to allow a property to free its opaque upon object
640 * destruction. This may be NULL.
641 * @opaque: an opaque pointer to pass to the callbacks for the property
642 * @errp: returns an error if this function fails
643 */
644void object_property_add(Object *obj, const char *name, const char *type,
645 ObjectPropertyAccessor *get,
646 ObjectPropertyAccessor *set,
647 ObjectPropertyRelease *release,
648 void *opaque, struct Error **errp);
649
650void object_property_del(Object *obj, const char *name, struct Error **errp);
651
8cb6789a
PB
652/**
653 * object_property_find:
654 * @obj: the object
655 * @name: the name of the property
89bfe000 656 * @errp: returns an error if this function fails
8cb6789a
PB
657 *
658 * Look up a property for an object and return its #ObjectProperty if found.
659 */
89bfe000
PB
660ObjectProperty *object_property_find(Object *obj, const char *name,
661 struct Error **errp);
8cb6789a 662
57c9fafe
AL
663void object_unparent(Object *obj);
664
665/**
666 * object_property_get:
667 * @obj: the object
668 * @v: the visitor that will receive the property value. This should be an
669 * Output visitor and the data will be written with @name as the name.
670 * @name: the name of the property
671 * @errp: returns an error if this function fails
672 *
673 * Reads a property from a object.
674 */
675void object_property_get(Object *obj, struct Visitor *v, const char *name,
676 struct Error **errp);
677
7b7b7d18
PB
678/**
679 * object_property_set_str:
680 * @value: the value to be written to the property
681 * @name: the name of the property
682 * @errp: returns an error if this function fails
683 *
684 * Writes a string value to a property.
685 */
686void object_property_set_str(Object *obj, const char *value,
687 const char *name, struct Error **errp);
688
689/**
690 * object_property_get_str:
691 * @obj: the object
692 * @name: the name of the property
693 * @errp: returns an error if this function fails
694 *
695 * Returns: the value of the property, converted to a C string, or NULL if
696 * an error occurs (including when the property value is not a string).
697 * The caller should free the string.
698 */
699char *object_property_get_str(Object *obj, const char *name,
700 struct Error **errp);
701
1d9c5a12
PB
702/**
703 * object_property_set_link:
704 * @value: the value to be written to the property
705 * @name: the name of the property
706 * @errp: returns an error if this function fails
707 *
708 * Writes an object's canonical path to a property.
709 */
710void object_property_set_link(Object *obj, Object *value,
711 const char *name, struct Error **errp);
712
713/**
714 * object_property_get_link:
715 * @obj: the object
716 * @name: the name of the property
717 * @errp: returns an error if this function fails
718 *
719 * Returns: the value of the property, resolved from a path to an Object,
720 * or NULL if an error occurs (including when the property value is not a
721 * string or not a valid object path).
722 */
723Object *object_property_get_link(Object *obj, const char *name,
724 struct Error **errp);
725
7b7b7d18
PB
726/**
727 * object_property_set_bool:
728 * @value: the value to be written to the property
729 * @name: the name of the property
730 * @errp: returns an error if this function fails
731 *
732 * Writes a bool value to a property.
733 */
734void object_property_set_bool(Object *obj, bool value,
735 const char *name, struct Error **errp);
736
737/**
738 * object_property_get_bool:
739 * @obj: the object
740 * @name: the name of the property
741 * @errp: returns an error if this function fails
742 *
743 * Returns: the value of the property, converted to a boolean, or NULL if
744 * an error occurs (including when the property value is not a bool).
745 */
746bool object_property_get_bool(Object *obj, const char *name,
747 struct Error **errp);
748
749/**
750 * object_property_set_int:
751 * @value: the value to be written to the property
752 * @name: the name of the property
753 * @errp: returns an error if this function fails
754 *
755 * Writes an integer value to a property.
756 */
757void object_property_set_int(Object *obj, int64_t value,
758 const char *name, struct Error **errp);
759
760/**
761 * object_property_get_int:
762 * @obj: the object
763 * @name: the name of the property
764 * @errp: returns an error if this function fails
765 *
766 * Returns: the value of the property, converted to an integer, or NULL if
767 * an error occurs (including when the property value is not an integer).
768 */
769int64_t object_property_get_int(Object *obj, const char *name,
770 struct Error **errp);
771
57c9fafe
AL
772/**
773 * object_property_set:
774 * @obj: the object
775 * @v: the visitor that will be used to write the property value. This should
776 * be an Input visitor and the data will be first read with @name as the
777 * name and then written as the property value.
778 * @name: the name of the property
779 * @errp: returns an error if this function fails
780 *
781 * Writes a property to a object.
782 */
783void object_property_set(Object *obj, struct Visitor *v, const char *name,
784 struct Error **errp);
785
b2cd7dee
PB
786/**
787 * object_property_parse:
788 * @obj: the object
789 * @string: the string that will be used to parse the property value.
790 * @name: the name of the property
791 * @errp: returns an error if this function fails
792 *
793 * Parses a string and writes the result into a property of an object.
794 */
795void object_property_parse(Object *obj, const char *string,
796 const char *name, struct Error **errp);
797
798/**
799 * object_property_print:
800 * @obj: the object
801 * @name: the name of the property
802 * @errp: returns an error if this function fails
803 *
804 * Returns a string representation of the value of the property. The
805 * caller shall free the string.
806 */
807char *object_property_print(Object *obj, const char *name,
808 struct Error **errp);
809
57c9fafe 810/**
438e1c79 811 * object_property_get_type:
57c9fafe
AL
812 * @obj: the object
813 * @name: the name of the property
814 * @errp: returns an error if this function fails
815 *
816 * Returns: The type name of the property.
817 */
818const char *object_property_get_type(Object *obj, const char *name,
819 struct Error **errp);
820
821/**
822 * object_get_root:
823 *
824 * Returns: the root object of the composition tree
825 */
826Object *object_get_root(void);
827
828/**
829 * object_get_canonical_path:
830 *
831 * Returns: The canonical path for a object. This is the path within the
832 * composition tree starting from the root.
833 */
834gchar *object_get_canonical_path(Object *obj);
835
836/**
837 * object_resolve_path:
838 * @path: the path to resolve
839 * @ambiguous: returns true if the path resolution failed because of an
840 * ambiguous match
841 *
842 * There are two types of supported paths--absolute paths and partial paths.
843 *
844 * Absolute paths are derived from the root object and can follow child<> or
845 * link<> properties. Since they can follow link<> properties, they can be
846 * arbitrarily long. Absolute paths look like absolute filenames and are
847 * prefixed with a leading slash.
848 *
849 * Partial paths look like relative filenames. They do not begin with a
850 * prefix. The matching rules for partial paths are subtle but designed to make
851 * specifying objects easy. At each level of the composition tree, the partial
852 * path is matched as an absolute path. The first match is not returned. At
853 * least two matches are searched for. A successful result is only returned if
02fe2db6
PB
854 * only one match is found. If more than one match is found, a flag is
855 * returned to indicate that the match was ambiguous.
57c9fafe
AL
856 *
857 * Returns: The matched object or NULL on path lookup failure.
858 */
859Object *object_resolve_path(const char *path, bool *ambiguous);
860
02fe2db6
PB
861/**
862 * object_resolve_path_type:
863 * @path: the path to resolve
864 * @typename: the type to look for.
865 * @ambiguous: returns true if the path resolution failed because of an
866 * ambiguous match
867 *
868 * This is similar to object_resolve_path. However, when looking for a
869 * partial path only matches that implement the given type are considered.
870 * This restricts the search and avoids spuriously flagging matches as
871 * ambiguous.
872 *
873 * For both partial and absolute paths, the return value goes through
874 * a dynamic cast to @typename. This is important if either the link,
875 * or the typename itself are of interface types.
876 *
877 * Returns: The matched object or NULL on path lookup failure.
878 */
879Object *object_resolve_path_type(const char *path, const char *typename,
880 bool *ambiguous);
881
a612b2a6
PB
882/**
883 * object_resolve_path_component:
884 * @parent: the object in which to resolve the path
885 * @part: the component to resolve.
886 *
887 * This is similar to object_resolve_path with an absolute path, but it
888 * only resolves one element (@part) and takes the others from @parent.
889 *
890 * Returns: The resolved object or NULL on path lookup failure.
891 */
892Object *object_resolve_path_component(Object *parent, gchar *part);
893
57c9fafe
AL
894/**
895 * object_property_add_child:
896 * @obj: the object to add a property to
897 * @name: the name of the property
898 * @child: the child object
899 * @errp: if an error occurs, a pointer to an area to store the area
900 *
901 * Child properties form the composition tree. All objects need to be a child
902 * of another object. Objects can only be a child of one object.
903 *
904 * There is no way for a child to determine what its parent is. It is not
905 * a bidirectional relationship. This is by design.
358b5465
AB
906 *
907 * The value of a child property as a C string will be the child object's
908 * canonical path. It can be retrieved using object_property_get_str().
909 * The child object itself can be retrieved using object_property_get_link().
57c9fafe
AL
910 */
911void object_property_add_child(Object *obj, const char *name,
912 Object *child, struct Error **errp);
913
914/**
915 * object_property_add_link:
916 * @obj: the object to add a property to
917 * @name: the name of the property
918 * @type: the qobj type of the link
919 * @child: a pointer to where the link object reference is stored
920 * @errp: if an error occurs, a pointer to an area to store the area
921 *
922 * Links establish relationships between objects. Links are unidirectional
923 * although two links can be combined to form a bidirectional relationship
924 * between objects.
925 *
926 * Links form the graph in the object model.
927 */
928void object_property_add_link(Object *obj, const char *name,
929 const char *type, Object **child,
930 struct Error **errp);
931
932/**
933 * object_property_add_str:
934 * @obj: the object to add a property to
935 * @name: the name of the property
936 * @get: the getter or NULL if the property is write-only. This function must
937 * return a string to be freed by g_free().
938 * @set: the setter or NULL if the property is read-only
939 * @errp: if an error occurs, a pointer to an area to store the error
940 *
941 * Add a string property using getters/setters. This function will add a
942 * property of type 'string'.
943 */
944void object_property_add_str(Object *obj, const char *name,
945 char *(*get)(Object *, struct Error **),
946 void (*set)(Object *, const char *, struct Error **),
947 struct Error **errp);
2f28d2ff 948
0e558843
AL
949/**
950 * object_property_add_bool:
951 * @obj: the object to add a property to
952 * @name: the name of the property
953 * @get: the getter or NULL if the property is write-only.
954 * @set: the setter or NULL if the property is read-only
955 * @errp: if an error occurs, a pointer to an area to store the error
956 *
957 * Add a bool property using getters/setters. This function will add a
958 * property of type 'bool'.
959 */
960void object_property_add_bool(Object *obj, const char *name,
961 bool (*get)(Object *, struct Error **),
962 void (*set)(Object *, bool, struct Error **),
963 struct Error **errp);
964
32efc535
PB
965/**
966 * object_child_foreach:
967 * @obj: the object whose children will be navigated
968 * @fn: the iterator function to be called
969 * @opaque: an opaque value that will be passed to the iterator
970 *
971 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
972 * non-zero.
973 *
974 * Returns: The last value returned by @fn, or 0 if there is no child.
975 */
976int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
977 void *opaque);
978
a612b2a6
PB
979/**
980 * container_get:
dfe47e70 981 * @root: root of the #path, e.g., object_get_root()
a612b2a6
PB
982 * @path: path to the container
983 *
984 * Return a container object whose path is @path. Create more containers
985 * along the path if necessary.
986 *
987 * Returns: the container object.
988 */
dfe47e70 989Object *container_get(Object *root, const char *path);
a612b2a6
PB
990
991
2f28d2ff 992#endif