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