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