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