blob: 21bb5ff1495960bdc2b0c7e725b9ca6eee64c577 [file] [log] [blame]
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001/*
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>
Markus Armbrusterda34e652016-03-14 09:01:28 +010018#include "qapi-types.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/queue.h"
Anthony Liguori57c9faf2012-01-30 08:55:55 -060020
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060021struct TypeImpl;
22typedef struct TypeImpl *Type;
23
24typedef struct ObjectClass ObjectClass;
25typedef struct Object Object;
26
27typedef struct TypeInfo TypeInfo;
28
29typedef struct InterfaceClass InterfaceClass;
30typedef struct InterfaceInfo InterfaceInfo;
31
Paolo Bonzini745549c2012-03-31 16:45:54 +020032#define TYPE_OBJECT "object"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060033
34/**
35 * SECTION:object.h
36 * @title:Base Object Type System
37 * @short_description: interfaces for creating new types and objects
38 *
39 * The QEMU Object Model provides a framework for registering user creatable
40 * types and instantiating objects from those types. QOM provides the following
41 * features:
42 *
43 * - System for dynamically registering types
44 * - Support for single-inheritance of types
45 * - Multiple inheritance of stateless interfaces
46 *
47 * <example>
48 * <title>Creating a minimal type</title>
49 * <programlisting>
50 * #include "qdev.h"
51 *
52 * #define TYPE_MY_DEVICE "my-device"
53 *
Paolo Bonzini0815a852012-02-03 12:51:53 +010054 * // No new virtual functions: we can reuse the typedef for the
55 * // superclass.
56 * typedef DeviceClass MyDeviceClass;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060057 * typedef struct MyDevice
58 * {
59 * DeviceState parent;
60 *
61 * int reg0, reg1, reg2;
62 * } MyDevice;
63 *
Andreas Färber8c43a6f2013-01-10 16:19:07 +010064 * static const TypeInfo my_device_info = {
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060065 * .name = TYPE_MY_DEVICE,
66 * .parent = TYPE_DEVICE,
67 * .instance_size = sizeof(MyDevice),
68 * };
69 *
Andreas Färber83f7d432012-02-09 15:20:55 +010070 * static void my_device_register_types(void)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060071 * {
72 * type_register_static(&my_device_info);
73 * }
74 *
Andreas Färber83f7d432012-02-09 15:20:55 +010075 * type_init(my_device_register_types)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060076 * </programlisting>
77 * </example>
78 *
79 * In the above example, we create a simple type that is described by #TypeInfo.
80 * #TypeInfo describes information about the type including what it inherits
81 * from, the instance and class size, and constructor/destructor hooks.
82 *
83 * Every type has an #ObjectClass associated with it. #ObjectClass derivatives
84 * are instantiated dynamically but there is only ever one instance for any
85 * given type. The #ObjectClass typically holds a table of function pointers
86 * for the virtual methods implemented by this type.
87 *
88 * Using object_new(), a new #Object derivative will be instantiated. You can
89 * cast an #Object to a subclass (or base-class) type using
Paolo Bonzini0815a852012-02-03 12:51:53 +010090 * object_dynamic_cast(). You typically want to define macro wrappers around
91 * OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
92 * specific type:
93 *
94 * <example>
95 * <title>Typecasting macros</title>
96 * <programlisting>
97 * #define MY_DEVICE_GET_CLASS(obj) \
98 * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
99 * #define MY_DEVICE_CLASS(klass) \
100 * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
101 * #define MY_DEVICE(obj) \
102 * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
103 * </programlisting>
104 * </example>
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600105 *
106 * # Class Initialization #
107 *
108 * Before an object is initialized, the class for the object must be
109 * initialized. There is only one class object for all instance objects
110 * that is created lazily.
111 *
112 * Classes are initialized by first initializing any parent classes (if
113 * necessary). After the parent class object has initialized, it will be
114 * copied into the current class object and any additional storage in the
115 * class object is zero filled.
116 *
117 * The effect of this is that classes automatically inherit any virtual
118 * function pointers that the parent class has already initialized. All
119 * other fields will be zero filled.
120 *
121 * Once all of the parent classes have been initialized, #TypeInfo::class_init
122 * is called to let the class being instantiated provide default initialize for
Stefan Weil93148aa2012-02-26 18:46:12 +0100123 * its virtual functions. Here is how the above example might be modified
Paolo Bonzini0815a852012-02-03 12:51:53 +0100124 * to introduce an overridden virtual function:
125 *
126 * <example>
127 * <title>Overriding a virtual function</title>
128 * <programlisting>
129 * #include "qdev.h"
130 *
131 * void my_device_class_init(ObjectClass *klass, void *class_data)
132 * {
133 * DeviceClass *dc = DEVICE_CLASS(klass);
134 * dc->reset = my_device_reset;
135 * }
136 *
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100137 * static const TypeInfo my_device_info = {
Paolo Bonzini0815a852012-02-03 12:51:53 +0100138 * .name = TYPE_MY_DEVICE,
139 * .parent = TYPE_DEVICE,
140 * .instance_size = sizeof(MyDevice),
141 * .class_init = my_device_class_init,
142 * };
143 * </programlisting>
144 * </example>
145 *
Andreas Färber782beb52013-01-17 08:31:50 +0100146 * Introducing new virtual methods requires a class to define its own
147 * struct and to add a .class_size member to the #TypeInfo. Each method
148 * will also have a wrapper function to call it easily:
Paolo Bonzini0815a852012-02-03 12:51:53 +0100149 *
150 * <example>
151 * <title>Defining an abstract class</title>
152 * <programlisting>
153 * #include "qdev.h"
154 *
155 * typedef struct MyDeviceClass
156 * {
157 * DeviceClass parent;
158 *
159 * void (*frobnicate) (MyDevice *obj);
160 * } MyDeviceClass;
161 *
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100162 * static const TypeInfo my_device_info = {
Paolo Bonzini0815a852012-02-03 12:51:53 +0100163 * .name = TYPE_MY_DEVICE,
164 * .parent = TYPE_DEVICE,
165 * .instance_size = sizeof(MyDevice),
166 * .abstract = true, // or set a default in my_device_class_init
167 * .class_size = sizeof(MyDeviceClass),
168 * };
169 *
170 * void my_device_frobnicate(MyDevice *obj)
171 * {
172 * MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
173 *
174 * klass->frobnicate(obj);
175 * }
176 * </programlisting>
177 * </example>
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600178 *
179 * # Interfaces #
180 *
181 * Interfaces allow a limited form of multiple inheritance. Instances are
182 * similar to normal types except for the fact that are only defined by
183 * their classes and never carry any state. You can dynamically cast an object
184 * to one of its #Interface types and vice versa.
Andreas Färber782beb52013-01-17 08:31:50 +0100185 *
186 * # Methods #
187 *
188 * A <emphasis>method</emphasis> is a function within the namespace scope of
189 * a class. It usually operates on the object instance by passing it as a
190 * strongly-typed first argument.
191 * If it does not operate on an object instance, it is dubbed
192 * <emphasis>class method</emphasis>.
193 *
194 * Methods cannot be overloaded. That is, the #ObjectClass and method name
195 * uniquely identity the function to be called; the signature does not vary
196 * except for trailing varargs.
197 *
198 * Methods are always <emphasis>virtual</emphasis>. Overriding a method in
199 * #TypeInfo.class_init of a subclass leads to any user of the class obtained
200 * via OBJECT_GET_CLASS() accessing the overridden function.
Peter Maydell085d8132013-03-18 17:20:07 +0000201 * The original function is not automatically invoked. It is the responsibility
Andreas Färber782beb52013-01-17 08:31:50 +0100202 * of the overriding class to determine whether and when to invoke the method
203 * being overridden.
204 *
205 * To invoke the method being overridden, the preferred solution is to store
206 * the original value in the overriding class before overriding the method.
207 * This corresponds to |[ {super,base}.method(...) ]| in Java and C#
208 * respectively; this frees the overriding class from hardcoding its parent
209 * class, which someone might choose to change at some point.
210 *
211 * <example>
212 * <title>Overriding a virtual method</title>
213 * <programlisting>
214 * typedef struct MyState MyState;
215 *
216 * typedef void (*MyDoSomething)(MyState *obj);
217 *
218 * typedef struct MyClass {
219 * ObjectClass parent_class;
220 *
221 * MyDoSomething do_something;
222 * } MyClass;
223 *
224 * static void my_do_something(MyState *obj)
225 * {
226 * // do something
227 * }
228 *
229 * static void my_class_init(ObjectClass *oc, void *data)
230 * {
231 * MyClass *mc = MY_CLASS(oc);
232 *
233 * mc->do_something = my_do_something;
234 * }
235 *
236 * static const TypeInfo my_type_info = {
237 * .name = TYPE_MY,
238 * .parent = TYPE_OBJECT,
239 * .instance_size = sizeof(MyState),
240 * .class_size = sizeof(MyClass),
241 * .class_init = my_class_init,
242 * };
243 *
244 * typedef struct DerivedClass {
245 * MyClass parent_class;
246 *
247 * MyDoSomething parent_do_something;
Peter Chubb70392912013-08-07 12:31:55 +1000248 * } DerivedClass;
Andreas Färber782beb52013-01-17 08:31:50 +0100249 *
250 * static void derived_do_something(MyState *obj)
251 * {
252 * DerivedClass *dc = DERIVED_GET_CLASS(obj);
253 *
254 * // do something here
255 * dc->parent_do_something(obj);
256 * // do something else here
257 * }
258 *
259 * static void derived_class_init(ObjectClass *oc, void *data)
260 * {
261 * MyClass *mc = MY_CLASS(oc);
262 * DerivedClass *dc = DERIVED_CLASS(oc);
263 *
264 * dc->parent_do_something = mc->do_something;
265 * mc->do_something = derived_do_something;
266 * }
267 *
268 * static const TypeInfo derived_type_info = {
269 * .name = TYPE_DERIVED,
270 * .parent = TYPE_MY,
271 * .class_size = sizeof(DerivedClass),
Zhoujianf824e8e2015-02-12 15:43:02 +0800272 * .class_init = derived_class_init,
Andreas Färber782beb52013-01-17 08:31:50 +0100273 * };
274 * </programlisting>
275 * </example>
276 *
277 * Alternatively, object_class_by_name() can be used to obtain the class and
278 * its non-overridden methods for a specific type. This would correspond to
279 * |[ MyClass::method(...) ]| in C++.
280 *
281 * The first example of such a QOM method was #CPUClass.reset,
282 * another example is #DeviceClass.realize.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600283 */
284
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600285
286/**
287 * ObjectPropertyAccessor:
288 * @obj: the object that owns the property
289 * @v: the visitor that contains the property data
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600290 * @name: the name of the property
Eric Blaked7bce992016-01-29 06:48:55 -0700291 * @opaque: the object property opaque
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600292 * @errp: a pointer to an Error that is filled if getting/setting fails.
293 *
294 * Called when trying to get/set a property.
295 */
296typedef void (ObjectPropertyAccessor)(Object *obj,
Eric Blake4fa45492016-01-29 06:48:53 -0700297 Visitor *v,
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600298 const char *name,
Eric Blaked7bce992016-01-29 06:48:55 -0700299 void *opaque,
Michael S. Tsirkine82df242013-09-22 10:08:14 +0300300 Error **errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600301
302/**
Paolo Bonzini64607d02014-06-05 13:11:51 +0200303 * ObjectPropertyResolve:
304 * @obj: the object that owns the property
305 * @opaque: the opaque registered with the property
306 * @part: the name of the property
307 *
308 * Resolves the #Object corresponding to property @part.
309 *
310 * The returned object can also be used as a starting point
311 * to resolve a relative path starting with "@part".
312 *
313 * Returns: If @path is the path that led to @obj, the function
314 * returns the #Object corresponding to "@path/@part".
315 * If "@path/@part" is not a valid object path, it returns #NULL.
316 */
317typedef Object *(ObjectPropertyResolve)(Object *obj,
318 void *opaque,
319 const char *part);
320
321/**
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600322 * ObjectPropertyRelease:
323 * @obj: the object that owns the property
324 * @name: the name of the property
325 * @opaque: the opaque registered with the property
326 *
327 * Called when a property is removed from a object.
328 */
329typedef void (ObjectPropertyRelease)(Object *obj,
330 const char *name,
331 void *opaque);
332
333typedef struct ObjectProperty
334{
335 gchar *name;
336 gchar *type;
Gonglei80742642014-10-07 14:33:21 +0800337 gchar *description;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600338 ObjectPropertyAccessor *get;
339 ObjectPropertyAccessor *set;
Paolo Bonzini64607d02014-06-05 13:11:51 +0200340 ObjectPropertyResolve *resolve;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600341 ObjectPropertyRelease *release;
342 void *opaque;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600343} ObjectProperty;
344
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600345/**
Paolo Bonzini667d22d2012-11-23 09:47:13 +0100346 * ObjectUnparent:
347 * @obj: the object that is being removed from the composition tree
348 *
349 * Called when an object is being removed from the QOM composition tree.
350 * The function should remove any backlinks from children objects to @obj.
351 */
352typedef void (ObjectUnparent)(Object *obj);
353
354/**
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100355 * ObjectFree:
356 * @obj: the object being freed
357 *
358 * Called when an object's last reference is removed.
359 */
360typedef void (ObjectFree)(void *obj);
361
Anthony Liguori03587322013-05-13 15:22:24 -0500362#define OBJECT_CLASS_CAST_CACHE 4
363
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100364/**
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600365 * ObjectClass:
366 *
367 * The base for all classes. The only thing that #ObjectClass contains is an
368 * integer type handle.
369 */
370struct ObjectClass
371{
372 /*< private >*/
373 Type type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000374 GSList *interfaces;
Paolo Bonzini667d22d2012-11-23 09:47:13 +0100375
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800376 const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
377 const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
Anthony Liguori03587322013-05-13 15:22:24 -0500378
Paolo Bonzini667d22d2012-11-23 09:47:13 +0100379 ObjectUnparent *unparent;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100380
381 GHashTable *properties;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600382};
383
384/**
385 * Object:
386 *
387 * The base for all objects. The first member of this object is a pointer to
388 * a #ObjectClass. Since C guarantees that the first member of a structure
389 * always begins at byte 0 of that structure, as long as any sub-object places
390 * its parent as the first member, we can cast directly to a #Object.
391 *
392 * As a result, #Object contains a reference to the objects type as its
393 * first member. This allows identification of the real type of the object at
394 * run time.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600395 */
396struct Object
397{
398 /*< private >*/
399 ObjectClass *class;
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100400 ObjectFree *free;
Pavel Fedinb604a852015-10-13 13:37:45 +0100401 GHashTable *properties;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600402 uint32_t ref;
403 Object *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600404};
405
406/**
407 * TypeInfo:
408 * @name: The name of the type.
409 * @parent: The name of the parent type.
410 * @instance_size: The size of the object (derivative of #Object). If
411 * @instance_size is 0, then the size of the object will be the size of the
412 * parent object.
413 * @instance_init: This function is called to initialize an object. The parent
414 * class will have already been initialized so the type is only responsible
415 * for initializing its own members.
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300416 * @instance_post_init: This function is called to finish initialization of
417 * an object, after all @instance_init functions were called.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600418 * @instance_finalize: This function is called during object destruction. This
419 * is called before the parent @instance_finalize function has been called.
420 * An object should only free the members that are unique to its type in this
421 * function.
422 * @abstract: If this field is true, then the class is considered abstract and
423 * cannot be directly instantiated.
424 * @class_size: The size of the class object (derivative of #ObjectClass)
425 * for this object. If @class_size is 0, then the size of the class will be
426 * assumed to be the size of the parent class. This allows a type to avoid
427 * implementing an explicit class type if they are not adding additional
428 * virtual functions.
429 * @class_init: This function is called after all parent class initialization
Stefan Weil441dd5e2012-02-25 13:47:09 +0100430 * has occurred to allow a class to set its default virtual method pointers.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600431 * This is also the function to use to override virtual methods from a parent
432 * class.
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200433 * @class_base_init: This function is called for all base classes after all
434 * parent class initialization has occurred, but before the class itself
435 * is initialized. This is the function to use to undo the effects of
436 * memcpy from the parent class to the descendents.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600437 * @class_finalize: This function is called during class destruction and is
438 * meant to release and dynamic parameters allocated by @class_init.
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200439 * @class_data: Data to pass to the @class_init, @class_base_init and
440 * @class_finalize functions. This can be useful when building dynamic
441 * classes.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600442 * @interfaces: The list of interfaces associated with this type. This
443 * should point to a static array that's terminated with a zero filled
444 * element.
445 */
446struct TypeInfo
447{
448 const char *name;
449 const char *parent;
450
451 size_t instance_size;
452 void (*instance_init)(Object *obj);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300453 void (*instance_post_init)(Object *obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600454 void (*instance_finalize)(Object *obj);
455
456 bool abstract;
457 size_t class_size;
458
459 void (*class_init)(ObjectClass *klass, void *data);
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200460 void (*class_base_init)(ObjectClass *klass, void *data);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600461 void (*class_finalize)(ObjectClass *klass, void *data);
462 void *class_data;
463
464 InterfaceInfo *interfaces;
465};
466
467/**
468 * OBJECT:
469 * @obj: A derivative of #Object
470 *
471 * Converts an object to a #Object. Since all objects are #Objects,
472 * this function will always succeed.
473 */
474#define OBJECT(obj) \
475 ((Object *)(obj))
476
477/**
Paolo Bonzini1ed5b912012-02-03 11:48:11 +0100478 * OBJECT_CLASS:
Andreas Färbera0dbf402012-02-16 18:03:18 +0100479 * @class: A derivative of #ObjectClass.
Paolo Bonzini1ed5b912012-02-03 11:48:11 +0100480 *
481 * Converts a class to an #ObjectClass. Since all objects are #Objects,
482 * this function will always succeed.
483 */
484#define OBJECT_CLASS(class) \
485 ((ObjectClass *)(class))
486
487/**
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600488 * OBJECT_CHECK:
489 * @type: The C type to use for the return value.
490 * @obj: A derivative of @type to cast.
491 * @name: The QOM typename of @type
492 *
493 * A type safe version of @object_dynamic_cast_assert. Typically each class
494 * will define a macro based on this type to perform type safe dynamic_casts to
495 * this object type.
496 *
497 * If an invalid object is passed to this function, a run time assert will be
498 * generated.
499 */
500#define OBJECT_CHECK(type, obj, name) \
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200501 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
502 __FILE__, __LINE__, __func__))
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600503
504/**
505 * OBJECT_CLASS_CHECK:
Cao jinb30d8052015-11-03 10:36:42 +0800506 * @class_type: The C type to use for the return value.
507 * @class: A derivative class of @class_type to cast.
508 * @name: the QOM typename of @class_type.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600509 *
Paolo Bonzini1ed5b912012-02-03 11:48:11 +0100510 * A type safe version of @object_class_dynamic_cast_assert. This macro is
511 * typically wrapped by each type to perform type safe casts of a class to a
512 * specific class type.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600513 */
Cao jinb30d8052015-11-03 10:36:42 +0800514#define OBJECT_CLASS_CHECK(class_type, class, name) \
515 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200516 __FILE__, __LINE__, __func__))
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600517
518/**
519 * OBJECT_GET_CLASS:
520 * @class: The C type to use for the return value.
521 * @obj: The object to obtain the class for.
522 * @name: The QOM typename of @obj.
523 *
524 * This function will return a specific class for a given object. Its generally
525 * used by each type to provide a type safe macro to get a specific class type
526 * from an object.
527 */
528#define OBJECT_GET_CLASS(class, obj, name) \
529 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
530
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600531/**
Anthony Liguori33e95c62012-08-10 13:16:10 +1000532 * InterfaceInfo:
533 * @type: The name of the interface.
534 *
535 * The information associated with an interface.
536 */
537struct InterfaceInfo {
538 const char *type;
539};
540
541/**
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600542 * InterfaceClass:
543 * @parent_class: the base class
544 *
545 * The class for all interfaces. Subclasses of this class should only add
546 * virtual methods.
547 */
548struct InterfaceClass
549{
550 ObjectClass parent_class;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000551 /*< private >*/
552 ObjectClass *concrete_class;
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100553 Type interface_type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600554};
555
556#define TYPE_INTERFACE "interface"
557
558/**
Anthony Liguori33e95c62012-08-10 13:16:10 +1000559 * INTERFACE_CLASS:
560 * @klass: class to cast from
561 * Returns: An #InterfaceClass or raise an error if cast is invalid
562 */
563#define INTERFACE_CLASS(klass) \
564 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
565
566/**
567 * INTERFACE_CHECK:
568 * @interface: the type to return
569 * @obj: the object to convert to an interface
570 * @name: the interface type name
571 *
572 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
573 */
574#define INTERFACE_CHECK(interface, obj, name) \
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200575 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
576 __FILE__, __LINE__, __func__))
Anthony Liguori33e95c62012-08-10 13:16:10 +1000577
578/**
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600579 * object_new:
580 * @typename: The name of the type of the object to instantiate.
581 *
Paolo Bonzinib76facc2013-01-25 14:12:39 +0100582 * This function will initialize a new object using heap allocated memory.
583 * The returned object has a reference count of 1, and will be freed when
584 * the last reference is dropped.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600585 *
586 * Returns: The newly allocated and instantiated object.
587 */
588Object *object_new(const char *typename);
589
590/**
591 * object_new_with_type:
592 * @type: The type of the object to instantiate.
593 *
Paolo Bonzinib76facc2013-01-25 14:12:39 +0100594 * This function will initialize a new object using heap allocated memory.
595 * The returned object has a reference count of 1, and will be freed when
596 * the last reference is dropped.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600597 *
598 * Returns: The newly allocated and instantiated object.
599 */
600Object *object_new_with_type(Type type);
601
602/**
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100603 * object_new_with_props:
604 * @typename: The name of the type of the object to instantiate.
605 * @parent: the parent object
606 * @id: The unique ID of the object
607 * @errp: pointer to error object
608 * @...: list of property names and values
609 *
610 * This function will initialize a new object using heap allocated memory.
611 * The returned object has a reference count of 1, and will be freed when
612 * the last reference is dropped.
613 *
614 * The @id parameter will be used when registering the object as a
615 * child of @parent in the composition tree.
616 *
617 * The variadic parameters are a list of pairs of (propname, propvalue)
618 * strings. The propname of %NULL indicates the end of the property
619 * list. If the object implements the user creatable interface, the
620 * object will be marked complete once all the properties have been
621 * processed.
622 *
623 * <example>
624 * <title>Creating an object with properties</title>
625 * <programlisting>
626 * Error *err = NULL;
627 * Object *obj;
628 *
629 * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE,
630 * object_get_objects_root(),
631 * "hostmem0",
632 * &err,
633 * "share", "yes",
634 * "mem-path", "/dev/shm/somefile",
635 * "prealloc", "yes",
636 * "size", "1048576",
637 * NULL);
638 *
639 * if (!obj) {
640 * g_printerr("Cannot create memory backend: %s\n",
641 * error_get_pretty(err));
642 * }
643 * </programlisting>
644 * </example>
645 *
646 * The returned object will have one stable reference maintained
647 * for as long as it is present in the object hierarchy.
648 *
649 * Returns: The newly allocated, instantiated & initialized object.
650 */
651Object *object_new_with_props(const char *typename,
652 Object *parent,
653 const char *id,
654 Error **errp,
655 ...) QEMU_SENTINEL;
656
657/**
658 * object_new_with_propv:
659 * @typename: The name of the type of the object to instantiate.
660 * @parent: the parent object
661 * @id: The unique ID of the object
662 * @errp: pointer to error object
663 * @vargs: list of property names and values
664 *
665 * See object_new_with_props() for documentation.
666 */
667Object *object_new_with_propv(const char *typename,
668 Object *parent,
669 const char *id,
670 Error **errp,
671 va_list vargs);
672
673/**
674 * object_set_props:
675 * @obj: the object instance to set properties on
676 * @errp: pointer to error object
677 * @...: list of property names and values
678 *
679 * This function will set a list of properties on an existing object
680 * instance.
681 *
682 * The variadic parameters are a list of pairs of (propname, propvalue)
683 * strings. The propname of %NULL indicates the end of the property
684 * list.
685 *
686 * <example>
687 * <title>Update an object's properties</title>
688 * <programlisting>
689 * Error *err = NULL;
690 * Object *obj = ...get / create object...;
691 *
692 * obj = object_set_props(obj,
693 * &err,
694 * "share", "yes",
695 * "mem-path", "/dev/shm/somefile",
696 * "prealloc", "yes",
697 * "size", "1048576",
698 * NULL);
699 *
700 * if (!obj) {
701 * g_printerr("Cannot set properties: %s\n",
702 * error_get_pretty(err));
703 * }
704 * </programlisting>
705 * </example>
706 *
707 * The returned object will have one stable reference maintained
708 * for as long as it is present in the object hierarchy.
709 *
710 * Returns: -1 on error, 0 on success
711 */
712int object_set_props(Object *obj,
713 Error **errp,
714 ...) QEMU_SENTINEL;
715
716/**
717 * object_set_propv:
718 * @obj: the object instance to set properties on
719 * @errp: pointer to error object
720 * @vargs: list of property names and values
721 *
722 * See object_set_props() for documentation.
723 *
724 * Returns: -1 on error, 0 on success
725 */
726int object_set_propv(Object *obj,
727 Error **errp,
728 va_list vargs);
729
730/**
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600731 * object_initialize_with_type:
Andreas Färber53caad92013-08-24 01:12:33 +0200732 * @data: A pointer to the memory to be used for the object.
Andreas Färber5b9237f2013-08-30 18:28:37 +0200733 * @size: The maximum size available at @data for the object.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600734 * @type: The type of the object to instantiate.
735 *
736 * This function will initialize an object. The memory for the object should
Paolo Bonzinib76facc2013-01-25 14:12:39 +0100737 * have already been allocated. The returned object has a reference count of 1,
738 * and will be finalized when the last reference is dropped.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600739 */
Andreas Färber5b9237f2013-08-30 18:28:37 +0200740void object_initialize_with_type(void *data, size_t size, Type type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600741
742/**
743 * object_initialize:
744 * @obj: A pointer to the memory to be used for the object.
Andreas Färber213f0c42013-08-23 19:37:12 +0200745 * @size: The maximum size available at @obj for the object.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600746 * @typename: The name of the type of the object to instantiate.
747 *
748 * This function will initialize an object. The memory for the object should
Paolo Bonzinib76facc2013-01-25 14:12:39 +0100749 * have already been allocated. The returned object has a reference count of 1,
750 * and will be finalized when the last reference is dropped.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600751 */
Andreas Färber213f0c42013-08-23 19:37:12 +0200752void object_initialize(void *obj, size_t size, const char *typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600753
754/**
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600755 * object_dynamic_cast:
756 * @obj: The object to cast.
757 * @typename: The @typename to cast to.
758 *
759 * This function will determine if @obj is-a @typename. @obj can refer to an
760 * object or an interface associated with an object.
761 *
762 * Returns: This function returns @obj on success or #NULL on failure.
763 */
764Object *object_dynamic_cast(Object *obj, const char *typename);
765
766/**
Andreas Färber438e1c72012-02-16 18:03:19 +0100767 * object_dynamic_cast_assert:
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600768 *
769 * See object_dynamic_cast() for a description of the parameters of this
770 * function. The only difference in behavior is that this function asserts
Paolo Bonzini3556c232013-05-10 14:16:40 +0200771 * instead of returning #NULL on failure if QOM cast debugging is enabled.
772 * This function is not meant to be called directly, but only through
773 * the wrapper macro OBJECT_CHECK.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600774 */
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200775Object *object_dynamic_cast_assert(Object *obj, const char *typename,
776 const char *file, int line, const char *func);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600777
778/**
779 * object_get_class:
780 * @obj: A derivative of #Object
781 *
782 * Returns: The #ObjectClass of the type associated with @obj.
783 */
784ObjectClass *object_get_class(Object *obj);
785
786/**
787 * object_get_typename:
788 * @obj: A derivative of #Object.
789 *
790 * Returns: The QOM typename of @obj.
791 */
792const char *object_get_typename(Object *obj);
793
794/**
795 * type_register_static:
796 * @info: The #TypeInfo of the new type.
797 *
798 * @info and all of the strings it points to should exist for the life time
799 * that the type is registered.
800 *
801 * Returns: 0 on failure, the new #Type on success.
802 */
803Type type_register_static(const TypeInfo *info);
804
805/**
806 * type_register:
807 * @info: The #TypeInfo of the new type
808 *
Stefan Weil93148aa2012-02-26 18:46:12 +0100809 * Unlike type_register_static(), this call does not require @info or its
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600810 * string members to continue to exist after the call returns.
811 *
812 * Returns: 0 on failure, the new #Type on success.
813 */
814Type type_register(const TypeInfo *info);
815
816/**
817 * object_class_dynamic_cast_assert:
818 * @klass: The #ObjectClass to attempt to cast.
819 * @typename: The QOM typename of the class to cast to.
820 *
Paolo Bonzini33bc94e2013-05-10 14:16:35 +0200821 * See object_class_dynamic_cast() for a description of the parameters
822 * of this function. The only difference in behavior is that this function
Paolo Bonzini3556c232013-05-10 14:16:40 +0200823 * asserts instead of returning #NULL on failure if QOM cast debugging is
824 * enabled. This function is not meant to be called directly, but only through
825 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600826 */
827ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200828 const char *typename,
829 const char *file, int line,
830 const char *func);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600831
Paolo Bonzini33bc94e2013-05-10 14:16:35 +0200832/**
833 * object_class_dynamic_cast:
834 * @klass: The #ObjectClass to attempt to cast.
835 * @typename: The QOM typename of the class to cast to.
836 *
837 * Returns: If @typename is a class, this function returns @klass if
838 * @typename is a subtype of @klass, else returns #NULL.
839 *
840 * If @typename is an interface, this function returns the interface
841 * definition for @klass if @klass implements it unambiguously; #NULL
842 * is returned if @klass does not implement the interface or if multiple
843 * classes or interfaces on the hierarchy leading to @klass implement
844 * it. (FIXME: perhaps this can be detected at type definition time?)
845 */
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600846ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
847 const char *typename);
848
849/**
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200850 * object_class_get_parent:
851 * @klass: The class to obtain the parent for.
852 *
853 * Returns: The parent for @klass or %NULL if none.
854 */
855ObjectClass *object_class_get_parent(ObjectClass *klass);
856
857/**
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600858 * object_class_get_name:
859 * @klass: The class to obtain the QOM typename for.
860 *
861 * Returns: The QOM typename for @klass.
862 */
863const char *object_class_get_name(ObjectClass *klass);
864
Paolo Bonzini0466e452012-05-02 13:30:53 +0200865/**
Andreas Färber17862372013-01-23 12:20:18 +0100866 * object_class_is_abstract:
867 * @klass: The class to obtain the abstractness for.
868 *
869 * Returns: %true if @klass is abstract, %false otherwise.
870 */
871bool object_class_is_abstract(ObjectClass *klass);
872
873/**
Paolo Bonzini0466e452012-05-02 13:30:53 +0200874 * object_class_by_name:
875 * @typename: The QOM typename to obtain the class for.
876 *
877 * Returns: The class for @typename or %NULL if not found.
878 */
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600879ObjectClass *object_class_by_name(const char *typename);
880
881void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600882 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600883 void *opaque);
Andreas Färber418ba9e2012-02-25 23:07:34 +0100884
885/**
886 * object_class_get_list:
887 * @implements_type: The type to filter for, including its derivatives.
888 * @include_abstract: Whether to include abstract classes.
889 *
890 * Returns: A singly-linked list of the classes in reverse hashtable order.
891 */
892GSList *object_class_get_list(const char *implements_type,
893 bool include_abstract);
894
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600895/**
896 * object_ref:
897 * @obj: the object
898 *
899 * Increase the reference count of a object. A object cannot be freed as long
900 * as its reference count is greater than zero.
901 */
902void object_ref(Object *obj);
903
904/**
905 * qdef_unref:
906 * @obj: the object
907 *
908 * Decrease the reference count of a object. A object cannot be freed as long
909 * as its reference count is greater than zero.
910 */
911void object_unref(Object *obj);
912
913/**
914 * object_property_add:
915 * @obj: the object to add a property to
916 * @name: the name of the property. This can contain any character except for
917 * a forward slash. In general, you should use hyphens '-' instead of
918 * underscores '_' when naming properties.
919 * @type: the type name of the property. This namespace is pretty loosely
920 * defined. Sub namespaces are constructed by using a prefix and then
921 * to angle brackets. For instance, the type 'virtio-net-pci' in the
922 * 'link' namespace would be 'link<virtio-net-pci>'.
923 * @get: The getter to be called to read a property. If this is NULL, then
924 * the property cannot be read.
925 * @set: the setter to be called to write a property. If this is NULL,
926 * then the property cannot be written.
927 * @release: called when the property is removed from the object. This is
928 * meant to allow a property to free its opaque upon object
929 * destruction. This may be NULL.
930 * @opaque: an opaque pointer to pass to the callbacks for the property
931 * @errp: returns an error if this function fails
Paolo Bonzini64607d02014-06-05 13:11:51 +0200932 *
933 * Returns: The #ObjectProperty; this can be used to set the @resolve
934 * callback for child and link properties.
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600935 */
Paolo Bonzini64607d02014-06-05 13:11:51 +0200936ObjectProperty *object_property_add(Object *obj, const char *name,
937 const char *type,
938 ObjectPropertyAccessor *get,
939 ObjectPropertyAccessor *set,
940 ObjectPropertyRelease *release,
941 void *opaque, Error **errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600942
Michael S. Tsirkine82df242013-09-22 10:08:14 +0300943void object_property_del(Object *obj, const char *name, Error **errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600944
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100945ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name,
946 const char *type,
947 ObjectPropertyAccessor *get,
948 ObjectPropertyAccessor *set,
949 ObjectPropertyRelease *release,
950 void *opaque, Error **errp);
951
Paolo Bonzini8cb67892012-03-30 14:54:31 +0200952/**
953 * object_property_find:
954 * @obj: the object
955 * @name: the name of the property
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200956 * @errp: returns an error if this function fails
Paolo Bonzini8cb67892012-03-30 14:54:31 +0200957 *
958 * Look up a property for an object and return its #ObjectProperty if found.
959 */
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200960ObjectProperty *object_property_find(Object *obj, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +0300961 Error **errp);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100962ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
963 Error **errp);
Paolo Bonzini8cb67892012-03-30 14:54:31 +0200964
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000965typedef struct ObjectPropertyIterator {
966 ObjectClass *nextclass;
967 GHashTableIter iter;
968} ObjectPropertyIterator;
Daniel P. Berrangea00c9482015-10-13 13:37:40 +0100969
970/**
971 * object_property_iter_init:
972 * @obj: the object
973 *
974 * Initializes an iterator for traversing all properties
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100975 * registered against an object instance, its class and all parent classes.
Daniel P. Berrangea00c9482015-10-13 13:37:40 +0100976 *
977 * It is forbidden to modify the property list while iterating,
978 * whether removing or adding properties.
979 *
980 * Typical usage pattern would be
981 *
982 * <example>
983 * <title>Using object property iterators</title>
984 * <programlisting>
985 * ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000986 * ObjectPropertyIterator iter;
Daniel P. Berrangea00c9482015-10-13 13:37:40 +0100987 *
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000988 * object_property_iter_init(&iter, obj);
989 * while ((prop = object_property_iter_next(&iter))) {
Daniel P. Berrangea00c9482015-10-13 13:37:40 +0100990 * ... do something with prop ...
991 * }
Daniel P. Berrangea00c9482015-10-13 13:37:40 +0100992 * </programlisting>
993 * </example>
Daniel P. Berrangea00c9482015-10-13 13:37:40 +0100994 */
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000995void object_property_iter_init(ObjectPropertyIterator *iter,
996 Object *obj);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +0100997
998/**
999 * object_property_iter_next:
1000 * @iter: the iterator instance
1001 *
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001002 * Return the next available property. If no further properties
1003 * are available, a %NULL value will be returned and the @iter
1004 * pointer should not be used again after this point without
1005 * re-initializing it.
1006 *
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001007 * Returns: the next property, or %NULL when all properties
1008 * have been traversed.
1009 */
1010ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter);
1011
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001012void object_unparent(Object *obj);
1013
1014/**
1015 * object_property_get:
1016 * @obj: the object
1017 * @v: the visitor that will receive the property value. This should be an
1018 * Output visitor and the data will be written with @name as the name.
1019 * @name: the name of the property
1020 * @errp: returns an error if this function fails
1021 *
1022 * Reads a property from a object.
1023 */
Eric Blake4fa45492016-01-29 06:48:53 -07001024void object_property_get(Object *obj, Visitor *v, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001025 Error **errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001026
1027/**
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001028 * object_property_set_str:
1029 * @value: the value to be written to the property
1030 * @name: the name of the property
1031 * @errp: returns an error if this function fails
1032 *
1033 * Writes a string value to a property.
1034 */
1035void object_property_set_str(Object *obj, const char *value,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001036 const char *name, Error **errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001037
1038/**
1039 * object_property_get_str:
1040 * @obj: the object
1041 * @name: the name of the property
1042 * @errp: returns an error if this function fails
1043 *
1044 * Returns: the value of the property, converted to a C string, or NULL if
1045 * an error occurs (including when the property value is not a string).
1046 * The caller should free the string.
1047 */
1048char *object_property_get_str(Object *obj, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001049 Error **errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001050
1051/**
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001052 * object_property_set_link:
1053 * @value: the value to be written to the property
1054 * @name: the name of the property
1055 * @errp: returns an error if this function fails
1056 *
1057 * Writes an object's canonical path to a property.
1058 */
1059void object_property_set_link(Object *obj, Object *value,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001060 const char *name, Error **errp);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001061
1062/**
1063 * object_property_get_link:
1064 * @obj: the object
1065 * @name: the name of the property
1066 * @errp: returns an error if this function fails
1067 *
1068 * Returns: the value of the property, resolved from a path to an Object,
1069 * or NULL if an error occurs (including when the property value is not a
1070 * string or not a valid object path).
1071 */
1072Object *object_property_get_link(Object *obj, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001073 Error **errp);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001074
1075/**
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001076 * object_property_set_bool:
1077 * @value: the value to be written to the property
1078 * @name: the name of the property
1079 * @errp: returns an error if this function fails
1080 *
1081 * Writes a bool value to a property.
1082 */
1083void object_property_set_bool(Object *obj, bool value,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001084 const char *name, Error **errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001085
1086/**
1087 * object_property_get_bool:
1088 * @obj: the object
1089 * @name: the name of the property
1090 * @errp: returns an error if this function fails
1091 *
1092 * Returns: the value of the property, converted to a boolean, or NULL if
1093 * an error occurs (including when the property value is not a bool).
1094 */
1095bool object_property_get_bool(Object *obj, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001096 Error **errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001097
1098/**
1099 * object_property_set_int:
1100 * @value: the value to be written to the property
1101 * @name: the name of the property
1102 * @errp: returns an error if this function fails
1103 *
1104 * Writes an integer value to a property.
1105 */
1106void object_property_set_int(Object *obj, int64_t value,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001107 const char *name, Error **errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001108
1109/**
1110 * object_property_get_int:
1111 * @obj: the object
1112 * @name: the name of the property
1113 * @errp: returns an error if this function fails
1114 *
Alistair Francisb29b47e2016-01-18 10:42:01 -08001115 * Returns: the value of the property, converted to an integer, or negative if
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001116 * an error occurs (including when the property value is not an integer).
1117 */
1118int64_t object_property_get_int(Object *obj, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001119 Error **errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001120
1121/**
Hu Tao1f217722014-05-14 17:43:33 +08001122 * object_property_get_enum:
1123 * @obj: the object
1124 * @name: the name of the property
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001125 * @typename: the name of the enum data type
Hu Tao1f217722014-05-14 17:43:33 +08001126 * @errp: returns an error if this function fails
1127 *
1128 * Returns: the value of the property, converted to an integer, or
1129 * undefined if an error occurs (including when the property value is not
1130 * an enum).
1131 */
1132int object_property_get_enum(Object *obj, const char *name,
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001133 const char *typename, Error **errp);
Hu Tao1f217722014-05-14 17:43:33 +08001134
1135/**
1136 * object_property_get_uint16List:
1137 * @obj: the object
1138 * @name: the name of the property
1139 * @list: the returned int list
1140 * @errp: returns an error if this function fails
1141 *
1142 * Returns: the value of the property, converted to integers, or
1143 * undefined if an error occurs (including when the property value is not
1144 * an list of integers).
1145 */
1146void object_property_get_uint16List(Object *obj, const char *name,
1147 uint16List **list, Error **errp);
1148
1149/**
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001150 * object_property_set:
1151 * @obj: the object
1152 * @v: the visitor that will be used to write the property value. This should
1153 * be an Input visitor and the data will be first read with @name as the
1154 * name and then written as the property value.
1155 * @name: the name of the property
1156 * @errp: returns an error if this function fails
1157 *
1158 * Writes a property to a object.
1159 */
Eric Blake4fa45492016-01-29 06:48:53 -07001160void object_property_set(Object *obj, Visitor *v, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001161 Error **errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001162
1163/**
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001164 * object_property_parse:
1165 * @obj: the object
1166 * @string: the string that will be used to parse the property value.
1167 * @name: the name of the property
1168 * @errp: returns an error if this function fails
1169 *
1170 * Parses a string and writes the result into a property of an object.
1171 */
1172void object_property_parse(Object *obj, const char *string,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001173 const char *name, Error **errp);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001174
1175/**
1176 * object_property_print:
1177 * @obj: the object
1178 * @name: the name of the property
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001179 * @human: if true, print for human consumption
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001180 * @errp: returns an error if this function fails
1181 *
1182 * Returns a string representation of the value of the property. The
1183 * caller shall free the string.
1184 */
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001185char *object_property_print(Object *obj, const char *name, bool human,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001186 Error **errp);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001187
1188/**
Andreas Färber438e1c72012-02-16 18:03:19 +01001189 * object_property_get_type:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001190 * @obj: the object
1191 * @name: the name of the property
1192 * @errp: returns an error if this function fails
1193 *
1194 * Returns: The type name of the property.
1195 */
1196const char *object_property_get_type(Object *obj, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001197 Error **errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001198
1199/**
1200 * object_get_root:
1201 *
1202 * Returns: the root object of the composition tree
1203 */
1204Object *object_get_root(void);
1205
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +01001206
1207/**
1208 * object_get_objects_root:
1209 *
1210 * Get the container object that holds user created
1211 * object instances. This is the object at path
1212 * "/objects"
1213 *
1214 * Returns: the user object container
1215 */
1216Object *object_get_objects_root(void);
1217
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001218/**
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001219 * object_get_canonical_path_component:
1220 *
1221 * Returns: The final component in the object's canonical path. The canonical
1222 * path is the path within the composition tree starting from the root.
1223 */
1224gchar *object_get_canonical_path_component(Object *obj);
1225
1226/**
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001227 * object_get_canonical_path:
1228 *
1229 * Returns: The canonical path for a object. This is the path within the
1230 * composition tree starting from the root.
1231 */
1232gchar *object_get_canonical_path(Object *obj);
1233
1234/**
1235 * object_resolve_path:
1236 * @path: the path to resolve
1237 * @ambiguous: returns true if the path resolution failed because of an
1238 * ambiguous match
1239 *
1240 * There are two types of supported paths--absolute paths and partial paths.
1241 *
1242 * Absolute paths are derived from the root object and can follow child<> or
1243 * link<> properties. Since they can follow link<> properties, they can be
1244 * arbitrarily long. Absolute paths look like absolute filenames and are
1245 * prefixed with a leading slash.
1246 *
1247 * Partial paths look like relative filenames. They do not begin with a
1248 * prefix. The matching rules for partial paths are subtle but designed to make
1249 * specifying objects easy. At each level of the composition tree, the partial
1250 * path is matched as an absolute path. The first match is not returned. At
1251 * least two matches are searched for. A successful result is only returned if
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001252 * only one match is found. If more than one match is found, a flag is
1253 * returned to indicate that the match was ambiguous.
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001254 *
1255 * Returns: The matched object or NULL on path lookup failure.
1256 */
1257Object *object_resolve_path(const char *path, bool *ambiguous);
1258
1259/**
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001260 * object_resolve_path_type:
1261 * @path: the path to resolve
1262 * @typename: the type to look for.
1263 * @ambiguous: returns true if the path resolution failed because of an
1264 * ambiguous match
1265 *
1266 * This is similar to object_resolve_path. However, when looking for a
1267 * partial path only matches that implement the given type are considered.
1268 * This restricts the search and avoids spuriously flagging matches as
1269 * ambiguous.
1270 *
1271 * For both partial and absolute paths, the return value goes through
1272 * a dynamic cast to @typename. This is important if either the link,
1273 * or the typename itself are of interface types.
1274 *
1275 * Returns: The matched object or NULL on path lookup failure.
1276 */
1277Object *object_resolve_path_type(const char *path, const char *typename,
1278 bool *ambiguous);
1279
1280/**
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001281 * object_resolve_path_component:
1282 * @parent: the object in which to resolve the path
1283 * @part: the component to resolve.
1284 *
1285 * This is similar to object_resolve_path with an absolute path, but it
1286 * only resolves one element (@part) and takes the others from @parent.
1287 *
1288 * Returns: The resolved object or NULL on path lookup failure.
1289 */
Andreas Färber3e84b482013-01-15 02:55:10 +01001290Object *object_resolve_path_component(Object *parent, const gchar *part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001291
1292/**
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001293 * object_property_add_child:
1294 * @obj: the object to add a property to
1295 * @name: the name of the property
1296 * @child: the child object
1297 * @errp: if an error occurs, a pointer to an area to store the area
1298 *
1299 * Child properties form the composition tree. All objects need to be a child
1300 * of another object. Objects can only be a child of one object.
1301 *
1302 * There is no way for a child to determine what its parent is. It is not
1303 * a bidirectional relationship. This is by design.
Alexander Barabash358b5462012-02-21 12:14:22 +02001304 *
1305 * The value of a child property as a C string will be the child object's
1306 * canonical path. It can be retrieved using object_property_get_str().
1307 * The child object itself can be retrieved using object_property_get_link().
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001308 */
1309void object_property_add_child(Object *obj, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001310 Object *child, Error **errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001311
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001312typedef enum {
1313 /* Unref the link pointer when the property is deleted */
1314 OBJ_PROP_LINK_UNREF_ON_RELEASE = 0x1,
1315} ObjectPropertyLinkFlags;
1316
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001317/**
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001318 * object_property_allow_set_link:
1319 *
1320 * The default implementation of the object_property_add_link() check()
1321 * callback function. It allows the link property to be set and never returns
1322 * an error.
1323 */
1324void object_property_allow_set_link(Object *, const char *,
1325 Object *, Error **);
1326
1327/**
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001328 * object_property_add_link:
1329 * @obj: the object to add a property to
1330 * @name: the name of the property
1331 * @type: the qobj type of the link
1332 * @child: a pointer to where the link object reference is stored
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001333 * @check: callback to veto setting or NULL if the property is read-only
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001334 * @flags: additional options for the link
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001335 * @errp: if an error occurs, a pointer to an area to store the area
1336 *
1337 * Links establish relationships between objects. Links are unidirectional
1338 * although two links can be combined to form a bidirectional relationship
1339 * between objects.
1340 *
1341 * Links form the graph in the object model.
Paolo Bonzini6c232d22013-01-25 14:12:31 +01001342 *
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001343 * The <code>@check()</code> callback is invoked when
1344 * object_property_set_link() is called and can raise an error to prevent the
1345 * link being set. If <code>@check</code> is NULL, the property is read-only
1346 * and cannot be set.
1347 *
Paolo Bonzini6c232d22013-01-25 14:12:31 +01001348 * Ownership of the pointer that @child points to is transferred to the
1349 * link property. The reference count for <code>*@child</code> is
1350 * managed by the property from after the function returns till the
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001351 * property is deleted with object_property_del(). If the
1352 * <code>@flags</code> <code>OBJ_PROP_LINK_UNREF_ON_RELEASE</code> bit is set,
1353 * the reference count is decremented when the property is deleted.
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001354 */
1355void object_property_add_link(Object *obj, const char *name,
1356 const char *type, Object **child,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001357 void (*check)(Object *obj, const char *name,
1358 Object *val, Error **errp),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001359 ObjectPropertyLinkFlags flags,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001360 Error **errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001361
1362/**
1363 * object_property_add_str:
1364 * @obj: the object to add a property to
1365 * @name: the name of the property
1366 * @get: the getter or NULL if the property is write-only. This function must
1367 * return a string to be freed by g_free().
1368 * @set: the setter or NULL if the property is read-only
1369 * @errp: if an error occurs, a pointer to an area to store the error
1370 *
1371 * Add a string property using getters/setters. This function will add a
1372 * property of type 'string'.
1373 */
1374void object_property_add_str(Object *obj, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001375 char *(*get)(Object *, Error **),
1376 void (*set)(Object *, const char *, Error **),
1377 Error **errp);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001378
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001379void object_class_property_add_str(ObjectClass *klass, const char *name,
1380 char *(*get)(Object *, Error **),
1381 void (*set)(Object *, const char *,
1382 Error **),
1383 Error **errp);
1384
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001385/**
Anthony Liguori0e558842012-06-25 10:32:46 -05001386 * object_property_add_bool:
1387 * @obj: the object to add a property to
1388 * @name: the name of the property
1389 * @get: the getter or NULL if the property is write-only.
1390 * @set: the setter or NULL if the property is read-only
1391 * @errp: if an error occurs, a pointer to an area to store the error
1392 *
1393 * Add a bool property using getters/setters. This function will add a
1394 * property of type 'bool'.
1395 */
1396void object_property_add_bool(Object *obj, const char *name,
Michael S. Tsirkine82df242013-09-22 10:08:14 +03001397 bool (*get)(Object *, Error **),
1398 void (*set)(Object *, bool, Error **),
1399 Error **errp);
Anthony Liguori0e558842012-06-25 10:32:46 -05001400
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001401void object_class_property_add_bool(ObjectClass *klass, const char *name,
1402 bool (*get)(Object *, Error **),
1403 void (*set)(Object *, bool, Error **),
1404 Error **errp);
1405
Anthony Liguori0e558842012-06-25 10:32:46 -05001406/**
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001407 * object_property_add_enum:
1408 * @obj: the object to add a property to
1409 * @name: the name of the property
1410 * @typename: the name of the enum data type
1411 * @get: the getter or %NULL if the property is write-only.
1412 * @set: the setter or %NULL if the property is read-only
1413 * @errp: if an error occurs, a pointer to an area to store the error
1414 *
1415 * Add an enum property using getters/setters. This function will add a
1416 * property of type '@typename'.
1417 */
1418void object_property_add_enum(Object *obj, const char *name,
1419 const char *typename,
1420 const char * const *strings,
1421 int (*get)(Object *, Error **),
1422 void (*set)(Object *, int, Error **),
1423 Error **errp);
1424
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001425void object_class_property_add_enum(ObjectClass *klass, const char *name,
1426 const char *typename,
1427 const char * const *strings,
1428 int (*get)(Object *, Error **),
1429 void (*set)(Object *, int, Error **),
1430 Error **errp);
1431
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001432/**
David Gibson8e099d12015-02-06 14:55:45 +11001433 * object_property_add_tm:
1434 * @obj: the object to add a property to
1435 * @name: the name of the property
1436 * @get: the getter or NULL if the property is write-only.
1437 * @errp: if an error occurs, a pointer to an area to store the error
1438 *
1439 * Add a read-only struct tm valued property using a getter function.
1440 * This function will add a property of type 'struct tm'.
1441 */
1442void object_property_add_tm(Object *obj, const char *name,
1443 void (*get)(Object *, struct tm *, Error **),
1444 Error **errp);
1445
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001446void object_class_property_add_tm(ObjectClass *klass, const char *name,
1447 void (*get)(Object *, struct tm *, Error **),
1448 Error **errp);
1449
David Gibson8e099d12015-02-06 14:55:45 +11001450/**
Michael S. Tsirkina25ebca2013-10-07 12:35:01 +03001451 * object_property_add_uint8_ptr:
1452 * @obj: the object to add a property to
1453 * @name: the name of the property
1454 * @v: pointer to value
1455 * @errp: if an error occurs, a pointer to an area to store the error
1456 *
1457 * Add an integer property in memory. This function will add a
1458 * property of type 'uint8'.
1459 */
1460void object_property_add_uint8_ptr(Object *obj, const char *name,
1461 const uint8_t *v, Error **errp);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001462void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
1463 const uint8_t *v, Error **errp);
Michael S. Tsirkina25ebca2013-10-07 12:35:01 +03001464
1465/**
1466 * object_property_add_uint16_ptr:
1467 * @obj: the object to add a property to
1468 * @name: the name of the property
1469 * @v: pointer to value
1470 * @errp: if an error occurs, a pointer to an area to store the error
1471 *
1472 * Add an integer property in memory. This function will add a
1473 * property of type 'uint16'.
1474 */
1475void object_property_add_uint16_ptr(Object *obj, const char *name,
1476 const uint16_t *v, Error **errp);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001477void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
1478 const uint16_t *v, Error **errp);
Michael S. Tsirkina25ebca2013-10-07 12:35:01 +03001479
1480/**
1481 * object_property_add_uint32_ptr:
1482 * @obj: the object to add a property to
1483 * @name: the name of the property
1484 * @v: pointer to value
1485 * @errp: if an error occurs, a pointer to an area to store the error
1486 *
1487 * Add an integer property in memory. This function will add a
1488 * property of type 'uint32'.
1489 */
1490void object_property_add_uint32_ptr(Object *obj, const char *name,
1491 const uint32_t *v, Error **errp);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001492void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
1493 const uint32_t *v, Error **errp);
Michael S. Tsirkina25ebca2013-10-07 12:35:01 +03001494
1495/**
1496 * object_property_add_uint64_ptr:
1497 * @obj: the object to add a property to
1498 * @name: the name of the property
1499 * @v: pointer to value
1500 * @errp: if an error occurs, a pointer to an area to store the error
1501 *
1502 * Add an integer property in memory. This function will add a
1503 * property of type 'uint64'.
1504 */
1505void object_property_add_uint64_ptr(Object *obj, const char *name,
1506 const uint64_t *v, Error **Errp);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001507void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
1508 const uint64_t *v, Error **Errp);
Michael S. Tsirkina25ebca2013-10-07 12:35:01 +03001509
1510/**
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001511 * object_property_add_alias:
1512 * @obj: the object to add a property to
1513 * @name: the name of the property
1514 * @target_obj: the object to forward property access to
1515 * @target_name: the name of the property on the forwarded object
1516 * @errp: if an error occurs, a pointer to an area to store the error
1517 *
1518 * Add an alias for a property on an object. This function will add a property
1519 * of the same type as the forwarded property.
1520 *
1521 * The caller must ensure that <code>@target_obj</code> stays alive as long as
1522 * this property exists. In the case of a child object or an alias on the same
1523 * object this will be the case. For aliases to other objects the caller is
1524 * responsible for taking a reference.
1525 */
1526void object_property_add_alias(Object *obj, const char *name,
1527 Object *target_obj, const char *target_name,
1528 Error **errp);
1529
1530/**
Paolo Bonzinifb9e7e32015-05-05 18:29:00 +02001531 * object_property_add_const_link:
1532 * @obj: the object to add a property to
1533 * @name: the name of the property
1534 * @target: the object to be referred by the link
1535 * @errp: if an error occurs, a pointer to an area to store the error
1536 *
1537 * Add an unmodifiable link for a property on an object. This function will
1538 * add a property of type link<TYPE> where TYPE is the type of @target.
1539 *
1540 * The caller must ensure that @target stays alive as long as
1541 * this property exists. In the case @target is a child of @obj,
1542 * this will be the case. Otherwise, the caller is responsible for
1543 * taking a reference.
1544 */
1545void object_property_add_const_link(Object *obj, const char *name,
1546 Object *target, Error **errp);
1547
1548/**
Gonglei80742642014-10-07 14:33:21 +08001549 * object_property_set_description:
1550 * @obj: the object owning the property
1551 * @name: the name of the property
1552 * @description: the description of the property on the object
1553 * @errp: if an error occurs, a pointer to an area to store the error
1554 *
1555 * Set an object property's description.
1556 *
1557 */
1558void object_property_set_description(Object *obj, const char *name,
1559 const char *description, Error **errp);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001560void object_class_property_set_description(ObjectClass *klass, const char *name,
1561 const char *description,
1562 Error **errp);
Gonglei80742642014-10-07 14:33:21 +08001563
1564/**
Paolo Bonzini32efc532012-04-11 23:30:20 +02001565 * object_child_foreach:
1566 * @obj: the object whose children will be navigated
1567 * @fn: the iterator function to be called
1568 * @opaque: an opaque value that will be passed to the iterator
1569 *
1570 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1571 * non-zero.
1572 *
Pavel Fedinb604a852015-10-13 13:37:45 +01001573 * It is forbidden to add or remove children from @obj from the @fn
1574 * callback.
1575 *
Paolo Bonzini32efc532012-04-11 23:30:20 +02001576 * Returns: The last value returned by @fn, or 0 if there is no child.
1577 */
1578int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1579 void *opaque);
1580
1581/**
Peter Crosthwaited714b8d2015-09-08 17:38:43 +01001582 * object_child_foreach_recursive:
1583 * @obj: the object whose children will be navigated
1584 * @fn: the iterator function to be called
1585 * @opaque: an opaque value that will be passed to the iterator
1586 *
1587 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1588 * non-zero. Calls recursively, all child nodes of @obj will also be passed
1589 * all the way down to the leaf nodes of the tree. Depth first ordering.
1590 *
Pavel Fedinb604a852015-10-13 13:37:45 +01001591 * It is forbidden to add or remove children from @obj (or its
1592 * child nodes) from the @fn callback.
1593 *
Peter Crosthwaited714b8d2015-09-08 17:38:43 +01001594 * Returns: The last value returned by @fn, or 0 if there is no child.
1595 */
1596int object_child_foreach_recursive(Object *obj,
1597 int (*fn)(Object *child, void *opaque),
1598 void *opaque);
1599/**
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001600 * container_get:
Andreas Färberdfe47e72012-04-05 13:21:46 +02001601 * @root: root of the #path, e.g., object_get_root()
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001602 * @path: path to the container
1603 *
1604 * Return a container object whose path is @path. Create more containers
1605 * along the path if necessary.
1606 *
1607 * Returns: the container object.
1608 */
Andreas Färberdfe47e72012-04-05 13:21:46 +02001609Object *container_get(Object *root, const char *path);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001610
1611
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001612#endif