When creating new QOM types, there is a lot of boilerplate code that
must be repeated using a standard pattern. This is tedious to write
and liable to suffer from subtle inconsistencies. Thus it would
benefit from some simple automation.
QOM was loosely inspired by GLib's GObject, and indeed GObject suffers
from the same burden of boilerplate code, but has long provided a set of
macros to eliminate this burden in the source implementation. More
recently it has also provided a set of macros to eliminate this burden
in the header declaration.
In GLib there are the G_DECLARE_* and G_DEFINE_* family of macros
for the header declaration and source implementation respectively:
https://developer.gnome.org/gobject/stable/chapter-gobject.html
https://developer.gnome.org/gobject/stable/howto-gobject.html
This patch takes inspiration from GObject to provide the equivalent
functionality for QOM.
In the header file, instead of:
typedef struct MyDevice MyDevice;
typedef struct MyDeviceClass MyDeviceClass;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDeviceClass, object_unref)
#define MY_DEVICE_GET_CLASS(void *obj) \
OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
#define MY_DEVICE_CLASS(void *klass) \
OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
#define MY_DEVICE(void *obj)
OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
struct MyDeviceClass {
DeviceClass parent_class;
};
We now have
OBJECT_DECLARE_SIMPLE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
In cases where the class needs some virtual methods, it can be left
to be implemented manually using
OBJECT_DECLARE_TYPE(MyDevice, my_device, MY_DEVICE)
Note that these macros are including support for g_autoptr() for the
object types, which is something previously only supported for variables
declared as the base Object * type.
Meanwhile in the source file, instead of:
static void my_device_finalize(Object *obj);
static void my_device_class_init(ObjectClass *oc, void *data);
static void my_device_init(Object *obj);
static const TypeInfo my_device_info = {
.parent = TYPE_DEVICE,
.name = TYPE_MY_DEVICE,
.instance_size = sizeof(MyDevice),
.instance_init = my_device_init,
.instance_finalize = my_device_finalize,
.class_size = sizeof(MyDeviceClass),
.class_init = my_device_class_init,
};
static void
my_device_register_types(void)
{
type_register_static(&my_device_info);
}
type_init(my_device_register_types);
We now have
OBJECT_DEFINE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
Or, if a class needs to implement interfaces:
OBJECT_DEFINE_TYPE_WITH_INTERFACES(MyDevice, my_device, MY_DEVICE, DEVICE,
{ TYPE_USER_CREATABLE }, { NULL })
Or, if a class needs to be abstract
OBJECT_DEFINE_ABSTRACT_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
IOW, in both cases the maintainer now only has to think about the
interesting part of the code which implements useful functionality
and avoids much of the boilerplate.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
include/qom/object.h | 277 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 277 insertions(+)
diff --git a/include/qom/object.h b/include/qom/object.h
index 1f8aa2d48e..be64421089 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -304,6 +304,119 @@ typedef struct InterfaceInfo InterfaceInfo;
*
* The first example of such a QOM method was #CPUClass.reset,
* another example is #DeviceClass.realize.
+ *
+ * # Standard type declaration and definition macros #
+ *
+ * A lot of the code outlined above follows a standard pattern and naming
+ * convention. To reduce the amount of boilerplate code that needs to be
+ * written for a new type there are two sets of macros to generate the
+ * common parts in a standard format.
+ *
+ * A type is declared using the OBJECT_DECLARE macro family. In types
+ * which do not require any virtual functions in the class, the
+ * OBJECT_DECLARE_SIMPLE_TYPE macro is suitable, and is commonly placed
+ * in the header file:
+ *
+ * <example>
+ * <title>Declaring a simple type</title>
+ * <programlisting>
+ * OBJECT_DECLARE_SIMPLE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
+ * </programlisting>
+ * </example>
+ *
+ * This is equivalent to the following:
+ *
+ * <example>
+ * <title>Expansion from declaring a simple type</title>
+ * <programlisting>
+ * typedef struct MyDevice MyDevice;
+ * typedef struct MyDeviceClass MyDeviceClass;
+ *
+ * G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDeviceClass, object_unref)
+ *
+ * #define MY_DEVICE_GET_CLASS(void *obj) \
+ * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
+ * #define MY_DEVICE_CLASS(void *klass) \
+ * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
+ * #define MY_DEVICE(void *obj)
+ * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
+ *
+ * struct MyDeviceClass {
+ * DeviceClass parent_class;
+ * };
+ * </programlisting>
+ * </example>
+ *
+ * The 'struct MyDevice' needs to be declared separately.
+ * If the type requires virtual functions to be declared in the class
+ * struct, then the alternative OBJECT_DECLARE_TYPE() macro can be
+ * used. This does the same as OBJECT_DECLARE_SIMPLE_TYPE(), but without
+ * the 'struct MyDeviceClass' definition.
+ *
+ * To implement the type, the OBJECT_DEFINE macro family is available.
+ * In the simple case the OBJECT_DEFINE_TYPE macro is suitable:
+ *
+ * <example>
+ * <title>Defining a simple type</title>
+ * <programlisting>
+ * OBJECT_DEFINE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
+ * </programlisting>
+ * </example>
+ *
+ * This is equivalent to the following:
+ *
+ * <example>
+ * <title>Expansion from defining a simple type</title>
+ * <programlisting>
+ * static void my_device_finalize(Object *obj);
+ * static void my_device_class_init(ObjectClass *oc, void *data);
+ * static void my_device_init(Object *obj);
+ *
+ * static const TypeInfo my_device_info = {
+ * .parent = TYPE_DEVICE,
+ * .name = TYPE_MY_DEVICE,
+ * .instance_size = sizeof(MyDevice),
+ * .instance_init = my_device_init,
+ * .instance_finalize = my_device_finalize,
+ * .class_size = sizeof(MyDeviceClass),
+ * .class_init = my_device_class_init,
+ * };
+ *
+ * static void
+ * my_device_register_types(void)
+ * {
+ * type_register_static(&my_device_info);
+ * }
+ * type_init(my_device_register_types);
+ * </programlisting>
+ * </example>
+ *
+ * This is sufficient to get the type registered with the type
+ * system, and the three standard methods now need to be implemented
+ * along with any other logic required for the type.
+ *
+ * If the type needs to implement one or more interfaces, then the
+ * OBJECT_DEFINE_TYPE_WITH_INTERFACES() macro can be used instead.
+ * This accepts an array of interface type names.
+ *
+ * <example>
+ * <title>Defining a simple type implementing interfaces</title>
+ * <programlisting>
+ * OBJECT_DEFINE_TYPE_WITH_INTERFACES(MyDevice, my_device,
+ * MY_DEVICE, DEVICE,
+ * { TYPE_USER_CREATABLE }, { NULL })
+ * </programlisting>
+ * </example>
+ *
+ * If the type is not intended to be instantiated, then then
+ * the OBJECT_DEFINE_ABSTRACT_TYPE() macro can be used instead:
+ *
+ * <example>
+ * <title>Defining a simple type</title>
+ * <programlisting>
+ * OBJECT_DEFINE_ABSTRACT_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
+ * </programlisting>
+ * </example>
*/
@@ -440,6 +553,170 @@ struct Object
Object *parent;
};
+/**
+ * OBJECT_DECLARE_TYPE:
+ * @ModuleObjName: the object name with initial capitalization
+ * @module_obj_name: the object name in lowercase with underscore separators
+ * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
+ *
+ * This macro is typically used in a header file, and will:
+ *
+ * - create the typedefs for the object and class structs
+ * - register the type for use with g_autoptr
+ * - provide three standard type cast functions
+ *
+ * The object struct and class struct need to be declared manually.
+ */
+#define OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \
+ typedef struct ModuleObjName ModuleObjName; \
+ typedef struct ModuleObjName##Class ModuleObjName##Class; \
+ \
+ G_DEFINE_AUTOPTR_CLEANUP_FUNC(ModuleObjName##Class, object_unref) \
+ \
+ static inline G_GNUC_UNUSED ModuleObjName##Class * \
+ MODULE_OBJ_NAME##_GET_CLASS(void *obj) \
+ { return OBJECT_GET_CLASS(ModuleObjName##Class, obj, \
+ TYPE_##MODULE_OBJ_NAME); } \
+ \
+ static inline G_GNUC_UNUSED ModuleObjName##Class * \
+ MODULE_OBJ_NAME##_CLASS(void *klass) \
+ { return OBJECT_CLASS_CHECK(ModuleObjName##Class, klass, \
+ TYPE_##MODULE_OBJ_NAME); } \
+ \
+ static inline G_GNUC_UNUSED ModuleObjName * \
+ MODULE_OBJ_NAME(void *obj) \
+ { return OBJECT_CHECK(ModuleObjName, obj, \
+ TYPE_##MODULE_OBJ_NAME); }
+
+/**
+ * OBJECT_DECLARE_SIMPLE_TYPE:
+ * @ModuleObjName: the object name with initial caps
+ * @module_obj_name: the object name in lowercase with underscore separators
+ * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
+ * @ParentModuleObjName: the parent object name with initial caps
+ *
+ * This does the same as OBJECT_DECLARE_TYPE(), but also declares
+ * the class struct, thus only the object struct needs to be declare
+ * manually.
+ *
+ * This macro should be used unless the class struct needs to have
+ * virtual methods declared.
+ */
+#define OBJECT_DECLARE_SIMPLE_TYPE(ModuleObjName, module_obj_name, \
+ MODULE_OBJ_NAME, ParentModuleObjName) \
+ OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \
+ struct ModuleObjName##Class { ParentModuleObjName##Class parent_class; };
+
+
+/**
+ * OBJECT_DEFINE_TYPE_EXTENDED:
+ * @ModuleObjName: the object name with initial caps
+ * @module_obj_name: the object name in lowercase with underscore separators
+ * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
+ * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
+ * separators
+ * @ABSTRACT: boolean flag to indicate whether the object can be instantiated
+ * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
+ *
+ * This macro is typically used in a source file, and will:
+ *
+ * - declare prototypes for _finalize, _class_init and _init methods
+ * - declare the TypeInfo struct instance
+ * - provide the constructor to register the type
+ *
+ * After using this macro, implementations of the _finalize, _class_init,
+ * and _init methods need to be written. Any of these can be zero-line
+ * no-op impls if no special logic is required for a given type.
+ *
+ * This macro should rarely be used, instead one of the more specialized
+ * macros is usually a better choice.
+ */
+#define OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
+ MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
+ ABSTRACT, ...) \
+ static void \
+ module_obj_name##_finalize(Object *obj); \
+ static void \
+ module_obj_name##_class_init(ObjectClass *oc, void *data); \
+ static void \
+ module_obj_name##_init(Object *obj); \
+ \
+ static const TypeInfo module_obj_name##_info = { \
+ .parent = TYPE_##PARENT_MODULE_OBJ_NAME, \
+ .name = TYPE_##MODULE_OBJ_NAME, \
+ .instance_size = sizeof(ModuleObjName), \
+ .instance_init = module_obj_name##_init, \
+ .instance_finalize = module_obj_name##_finalize, \
+ .class_size = sizeof(ModuleObjName##Class), \
+ .class_init = module_obj_name##_class_init, \
+ .abstract = ABSTRACT, \
+ .interfaces = (InterfaceInfo[]) { __VA_ARGS__ } , \
+ }; \
+ \
+ static void \
+ module_obj_name##_register_types(void) \
+ { \
+ type_register_static(&module_obj_name##_info); \
+ } \
+ type_init(module_obj_name##_register_types);
+
+/**
+ * OBJECT_DEFINE_TYPE:
+ * @ModuleObjName: the object name with initial caps
+ * @module_obj_name: the object name in lowercase with underscore separators
+ * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
+ * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
+ * separators
+ *
+ * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
+ * for the common case of a non-abstract type, without any interfaces.
+ */
+#define OBJECT_DEFINE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME, \
+ PARENT_MODULE_OBJ_NAME) \
+ OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
+ MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
+ false, { NULL })
+
+/**
+ * OBJECT_DEFINE_TYPE_WITH_INTERFACES:
+ * @ModuleObjName: the object name with initial caps
+ * @module_obj_name: the object name in lowercase with underscore separators
+ * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
+ * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
+ * separators
+ * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
+ *
+ * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
+ * for the common case of a non-abstract type, with one or more implemented
+ * interfaces.
+ *
+ * Note when passing the list of interfaces, be sure to include the final
+ * NULL entry, e.g. { TYPE_USER_CREATABLE }, { NULL }
+ */
+#define OBJECT_DEFINE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \
+ MODULE_OBJ_NAME, \
+ PARENT_MODULE_OBJ_NAME, ...) \
+ OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
+ MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
+ false, __VA_ARGS__)
+
+/**
+ * OBJECT_DEFINE_ABSTRACT_TYPE:
+ * @ModuleObjName: the object name with initial caps
+ * @module_obj_name: the object name in lowercase with underscore separators
+ * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
+ * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
+ * separators
+ *
+ * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
+ * for defining an abstract type, without any interfaces.
+ */
+#define OBJECT_DEFINE_ABSTRACT_TYPE(ModuleObjName, module_obj_name, \
+ MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \
+ OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
+ MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
+ true, { NULL })
+
/**
* TypeInfo:
* @name: The name of the type.
--
2.26.2
On 7/23/20 1:14 PM, Daniel P. Berrangé wrote:
> When creating new QOM types, there is a lot of boilerplate code that
> must be repeated using a standard pattern. This is tedious to write
> and liable to suffer from subtle inconsistencies. Thus it would
> benefit from some simple automation.
>
> QOM was loosely inspired by GLib's GObject, and indeed GObject suffers
> from the same burden of boilerplate code, but has long provided a set of
> macros to eliminate this burden in the source implementation. More
> recently it has also provided a set of macros to eliminate this burden
> in the header declaration.
>
> In GLib there are the G_DECLARE_* and G_DEFINE_* family of macros
> for the header declaration and source implementation respectively:
>
> https://developer.gnome.org/gobject/stable/chapter-gobject.html
> https://developer.gnome.org/gobject/stable/howto-gobject.html
>
> This patch takes inspiration from GObject to provide the equivalent
> functionality for QOM.
>
>
> IOW, in both cases the maintainer now only has to think about the
> interesting part of the code which implements useful functionality
> and avoids much of the boilerplate.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> include/qom/object.h | 277 +++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 277 insertions(+)
>
> diff --git a/include/qom/object.h b/include/qom/object.h
> index 1f8aa2d48e..be64421089 100644
> --- a/include/qom/object.h
> +++ b/include/qom/object.h
> @@ -304,6 +304,119 @@ typedef struct InterfaceInfo InterfaceInfo;
> *
> * The first example of such a QOM method was #CPUClass.reset,
> * another example is #DeviceClass.realize.
> + *
> + * # Standard type declaration and definition macros #
> + *
> + * A lot of the code outlined above follows a standard pattern and naming
> + * convention. To reduce the amount of boilerplate code that needs to be
> + * written for a new type there are two sets of macros to generate the
> + * common parts in a standard format.
> + *
> + * A type is declared using the OBJECT_DECLARE macro family. In types
> + * which do not require any virtual functions in the class, the
> + * OBJECT_DECLARE_SIMPLE_TYPE macro is suitable, and is commonly placed
> + * in the header file:
> + *
> + * <example>
> + * <title>Declaring a simple type</title>
> + * <programlisting>
> + * OBJECT_DECLARE_SIMPLE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
How sensitive is this macro to trailing semicolon? Must the user omit
it (as shown here), supply it (by tweaking the macro to be a syntax
error if one is not supplied), or is it optional? I guess whatever glib
does is fine to copy, though.
Hmm. I think you meant to use s/ DEVICE/ Device/ here...
> + * </programlisting>
> + * </example>
> + *
> + * This is equivalent to the following:
> + *
> + * <example>
> + * <title>Expansion from declaring a simple type</title>
> + * <programlisting>
> + * typedef struct MyDevice MyDevice;
> + * typedef struct MyDeviceClass MyDeviceClass;
> + *
> + * G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDeviceClass, object_unref)
> + *
> + * #define MY_DEVICE_GET_CLASS(void *obj) \
> + * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
How'd you manage to invoke #define inside the OBJECT_DECLARE_SIMPLE_TYPE
macro expansion?
/me reads ahead
Oh, you didn't; you used a static inline function instead. But the
effect is the same, so claiming the equivalence here, while slightly
misleading, is not horrible.
> + * #define MY_DEVICE_CLASS(void *klass) \
> + * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
> + * #define MY_DEVICE(void *obj)
> + * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
> + *
> + * struct MyDeviceClass {
> + * DeviceClass parent_class;
...given that this line is constructed as arg4##Class, and the fact that
we have DeviceClass, not DEVICEClass.
> + * };
> + * </programlisting>
> + * </example>
> + *
> + * The 'struct MyDevice' needs to be declared separately.
> + * If the type requires virtual functions to be declared in the class
> + * struct, then the alternative OBJECT_DECLARE_TYPE() macro can be
> + * used. This does the same as OBJECT_DECLARE_SIMPLE_TYPE(), but without
> + * the 'struct MyDeviceClass' definition.
> + *
> + * To implement the type, the OBJECT_DEFINE macro family is available.
> + * In the simple case the OBJECT_DEFINE_TYPE macro is suitable:
> + *
> + * <example>
> + * <title>Defining a simple type</title>
> + * <programlisting>
> + * OBJECT_DEFINE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
Unlike the declare, here, using DEVICE looks correct...
> + * </programlisting>
> + * </example>
> + *
> + * This is equivalent to the following:
> + *
> + * <example>
> + * <title>Expansion from defining a simple type</title>
> + * <programlisting>
> + * static void my_device_finalize(Object *obj);
> + * static void my_device_class_init(ObjectClass *oc, void *data);
> + * static void my_device_init(Object *obj);
> + *
> + * static const TypeInfo my_device_info = {
> + * .parent = TYPE_DEVICE,
...given the expansion here.
> + * .name = TYPE_MY_DEVICE,
> + * .instance_size = sizeof(MyDevice),
> + * .instance_init = my_device_init,
> + * .instance_finalize = my_device_finalize,
> + * .class_size = sizeof(MyDeviceClass),
> + * .class_init = my_device_class_init,
> + * };
> + *
> + * static void
> + * my_device_register_types(void)
> + * {
> + * type_register_static(&my_device_info);
> + * }
> + * type_init(my_device_register_types);
> + * </programlisting>
> + * </example>
> + *
> + * This is sufficient to get the type registered with the type
> + * system, and the three standard methods now need to be implemented
> + * along with any other logic required for the type.
> + *
> + * If the type needs to implement one or more interfaces, then the
> + * OBJECT_DEFINE_TYPE_WITH_INTERFACES() macro can be used instead.
> + * This accepts an array of interface type names.
> + *
> + * <example>
> + * <title>Defining a simple type implementing interfaces</title>
> + * <programlisting>
> + * OBJECT_DEFINE_TYPE_WITH_INTERFACES(MyDevice, my_device,
> + * MY_DEVICE, DEVICE,
> + * { TYPE_USER_CREATABLE }, { NULL })
> + * </programlisting>
> + * </example>
> + *
> + * If the type is not intended to be instantiated, then then
> + * the OBJECT_DEFINE_ABSTRACT_TYPE() macro can be used instead:
> + *
> + * <example>
> + * <title>Defining a simple type</title>
This title should probably mention 'abstract'.
> + * <programlisting>
> + * OBJECT_DEFINE_ABSTRACT_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
> + * </programlisting>
> + * </example>
> */
>
>
> @@ -440,6 +553,170 @@ struct Object
> Object *parent;
> };
>
> +/**
> + * OBJECT_DECLARE_TYPE:
> + * @ModuleObjName: the object name with initial capitalization
> + * @module_obj_name: the object name in lowercase with underscore separators
> + * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
> + *
> + * This macro is typically used in a header file, and will:
> + *
> + * - create the typedefs for the object and class structs
> + * - register the type for use with g_autoptr
> + * - provide three standard type cast functions
> + *
> + * The object struct and class struct need to be declared manually.
> + */
> +#define OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \
> + typedef struct ModuleObjName ModuleObjName; \
> + typedef struct ModuleObjName##Class ModuleObjName##Class; \
> + \
> + G_DEFINE_AUTOPTR_CLEANUP_FUNC(ModuleObjName##Class, object_unref) \
> + \
> + static inline G_GNUC_UNUSED ModuleObjName##Class * \
> + MODULE_OBJ_NAME##_GET_CLASS(void *obj) \
> + { return OBJECT_GET_CLASS(ModuleObjName##Class, obj, \
> + TYPE_##MODULE_OBJ_NAME); } \
> + \
> + static inline G_GNUC_UNUSED ModuleObjName##Class * \
> + MODULE_OBJ_NAME##_CLASS(void *klass) \
> + { return OBJECT_CLASS_CHECK(ModuleObjName##Class, klass, \
> + TYPE_##MODULE_OBJ_NAME); } \
> + \
> + static inline G_GNUC_UNUSED ModuleObjName * \
> + MODULE_OBJ_NAME(void *obj) \
> + { return OBJECT_CHECK(ModuleObjName, obj, \
> + TYPE_##MODULE_OBJ_NAME); }
> +
> +/**
> + * OBJECT_DECLARE_SIMPLE_TYPE:
> + * @ModuleObjName: the object name with initial caps
> + * @module_obj_name: the object name in lowercase with underscore separators
> + * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
> + * @ParentModuleObjName: the parent object name with initial caps
> + *
> + * This does the same as OBJECT_DECLARE_TYPE(), but also declares
> + * the class struct, thus only the object struct needs to be declare
declared
> + * manually.
> + *
> + * This macro should be used unless the class struct needs to have
> + * virtual methods declared.
> + */
> +#define OBJECT_DECLARE_SIMPLE_TYPE(ModuleObjName, module_obj_name, \
> + MODULE_OBJ_NAME, ParentModuleObjName) \
> + OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \
> + struct ModuleObjName##Class { ParentModuleObjName##Class parent_class; };
> +
> +
> +/**
> + * OBJECT_DEFINE_TYPE_EXTENDED:
> + * @ModuleObjName: the object name with initial caps
> + * @module_obj_name: the object name in lowercase with underscore separators
> + * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
> + * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
> + * separators
> + * @ABSTRACT: boolean flag to indicate whether the object can be instantiated
> + * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
> + *
> + * This macro is typically used in a source file, and will:
> + *
> + * - declare prototypes for _finalize, _class_init and _init methods
> + * - declare the TypeInfo struct instance
> + * - provide the constructor to register the type
> + *
> + * After using this macro, implementations of the _finalize, _class_init,
> + * and _init methods need to be written. Any of these can be zero-line
> + * no-op impls if no special logic is required for a given type.
> + *
> + * This macro should rarely be used, instead one of the more specialized
> + * macros is usually a better choice.
> + */
> +#define OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
> + MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
> + ABSTRACT, ...) \
> + static void \
> + module_obj_name##_finalize(Object *obj); \
> + static void \
> + module_obj_name##_class_init(ObjectClass *oc, void *data); \
> + static void \
> + module_obj_name##_init(Object *obj); \
> + \
> + static const TypeInfo module_obj_name##_info = { \
> + .parent = TYPE_##PARENT_MODULE_OBJ_NAME, \
> + .name = TYPE_##MODULE_OBJ_NAME, \
> + .instance_size = sizeof(ModuleObjName), \
> + .instance_init = module_obj_name##_init, \
> + .instance_finalize = module_obj_name##_finalize, \
> + .class_size = sizeof(ModuleObjName##Class), \
> + .class_init = module_obj_name##_class_init, \
> + .abstract = ABSTRACT, \
> + .interfaces = (InterfaceInfo[]) { __VA_ARGS__ } , \
Odd space before comma.
> + }; \
> + \
> + static void \
> + module_obj_name##_register_types(void) \
> + { \
> + type_register_static(&module_obj_name##_info); \
> + } \
> + type_init(module_obj_name##_register_types);
> +
> +/**
> + * OBJECT_DEFINE_TYPE:
> + * @ModuleObjName: the object name with initial caps
> + * @module_obj_name: the object name in lowercase with underscore separators
> + * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
> + * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
> + * separators
> + *
> + * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
> + * for the common case of a non-abstract type, without any interfaces.
> + */
> +#define OBJECT_DEFINE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME, \
> + PARENT_MODULE_OBJ_NAME) \
> + OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
> + MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
> + false, { NULL })
> +
> +/**
> + * OBJECT_DEFINE_TYPE_WITH_INTERFACES:
> + * @ModuleObjName: the object name with initial caps
> + * @module_obj_name: the object name in lowercase with underscore separators
> + * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
> + * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
> + * separators
> + * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
> + *
> + * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
> + * for the common case of a non-abstract type, with one or more implemented
> + * interfaces.
> + *
> + * Note when passing the list of interfaces, be sure to include the final
> + * NULL entry, e.g. { TYPE_USER_CREATABLE }, { NULL }
> + */
> +#define OBJECT_DEFINE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \
> + MODULE_OBJ_NAME, \
> + PARENT_MODULE_OBJ_NAME, ...) \
> + OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
> + MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
> + false, __VA_ARGS__)
> +
> +/**
> + * OBJECT_DEFINE_ABSTRACT_TYPE:
> + * @ModuleObjName: the object name with initial caps
> + * @module_obj_name: the object name in lowercase with underscore separators
> + * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
> + * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
> + * separators
> + *
> + * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
> + * for defining an abstract type, without any interfaces.
> + */
> +#define OBJECT_DEFINE_ABSTRACT_TYPE(ModuleObjName, module_obj_name, \
> + MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \
> + OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
> + MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
> + true, { NULL })
> +
> /**
> * TypeInfo:
> * @name: The name of the type.
>
A couple minor spelling findings, but overall looks good.
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
On Thu, Jul 23, 2020 at 02:23:54PM -0500, Eric Blake wrote:
> On 7/23/20 1:14 PM, Daniel P. Berrangé wrote:
> > When creating new QOM types, there is a lot of boilerplate code that
> > must be repeated using a standard pattern. This is tedious to write
> > and liable to suffer from subtle inconsistencies. Thus it would
> > benefit from some simple automation.
> >
> > QOM was loosely inspired by GLib's GObject, and indeed GObject suffers
> > from the same burden of boilerplate code, but has long provided a set of
> > macros to eliminate this burden in the source implementation. More
> > recently it has also provided a set of macros to eliminate this burden
> > in the header declaration.
> >
> > In GLib there are the G_DECLARE_* and G_DEFINE_* family of macros
> > for the header declaration and source implementation respectively:
> >
> > https://developer.gnome.org/gobject/stable/chapter-gobject.html
> > https://developer.gnome.org/gobject/stable/howto-gobject.html
> >
> > This patch takes inspiration from GObject to provide the equivalent
> > functionality for QOM.
> >
>
> >
> > IOW, in both cases the maintainer now only has to think about the
> > interesting part of the code which implements useful functionality
> > and avoids much of the boilerplate.
> >
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> > include/qom/object.h | 277 +++++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 277 insertions(+)
> >
> > diff --git a/include/qom/object.h b/include/qom/object.h
> > index 1f8aa2d48e..be64421089 100644
> > --- a/include/qom/object.h
> > +++ b/include/qom/object.h
> > @@ -304,6 +304,119 @@ typedef struct InterfaceInfo InterfaceInfo;
> > *
> > * The first example of such a QOM method was #CPUClass.reset,
> > * another example is #DeviceClass.realize.
> > + *
> > + * # Standard type declaration and definition macros #
> > + *
> > + * A lot of the code outlined above follows a standard pattern and naming
> > + * convention. To reduce the amount of boilerplate code that needs to be
> > + * written for a new type there are two sets of macros to generate the
> > + * common parts in a standard format.
> > + *
> > + * A type is declared using the OBJECT_DECLARE macro family. In types
> > + * which do not require any virtual functions in the class, the
> > + * OBJECT_DECLARE_SIMPLE_TYPE macro is suitable, and is commonly placed
> > + * in the header file:
> > + *
> > + * <example>
> > + * <title>Declaring a simple type</title>
> > + * <programlisting>
> > + * OBJECT_DECLARE_SIMPLE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
>
> How sensitive is this macro to trailing semicolon? Must the user omit it
> (as shown here), supply it (by tweaking the macro to be a syntax error if
> one is not supplied), or is it optional? I guess whatever glib does is fine
> to copy, though.
Testing it appears it tolerates a ";" but GLib doesn't use it typically
in this case.
>
> Hmm. I think you meant to use s/ DEVICE/ Device/ here...
Yes.
>
> > + * </programlisting>
> > + * </example>
> > + *
> > + * This is equivalent to the following:
> > + *
> > + * <example>
> > + * <title>Expansion from declaring a simple type</title>
> > + * <programlisting>
> > + * typedef struct MyDevice MyDevice;
> > + * typedef struct MyDeviceClass MyDeviceClass;
> > + *
> > + * G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDeviceClass, object_unref)
> > + *
> > + * #define MY_DEVICE_GET_CLASS(void *obj) \
> > + * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
>
> How'd you manage to invoke #define inside the OBJECT_DECLARE_SIMPLE_TYPE
> macro expansion?
>
> /me reads ahead
>
> Oh, you didn't; you used a static inline function instead. But the effect
> is the same, so claiming the equivalence here, while slightly misleading, is
> not horrible.
Yes, I was in two minds here, whether to illustrate the real inline
function, or the macro. I choose the macro to make it clear what kind
of code is being replaced, rather than what kind of code it strictly
produces.
> > + * #define MY_DEVICE_CLASS(void *klass) \
> > + * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
> > + * #define MY_DEVICE(void *obj)
> > + * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
> > + *
> > + * struct MyDeviceClass {
> > + * DeviceClass parent_class;
>
> ...given that this line is constructed as arg4##Class, and the fact that we
> have DeviceClass, not DEVICEClass.
>
> > + * };
> > + * </programlisting>
> > + * </example>
> > + *
> > + * The 'struct MyDevice' needs to be declared separately.
> > + * If the type requires virtual functions to be declared in the class
> > + * struct, then the alternative OBJECT_DECLARE_TYPE() macro can be
> > + * used. This does the same as OBJECT_DECLARE_SIMPLE_TYPE(), but without
> > + * the 'struct MyDeviceClass' definition.
> > + *
> > + * To implement the type, the OBJECT_DEFINE macro family is available.
> > + * In the simple case the OBJECT_DEFINE_TYPE macro is suitable:
> > + *
> > + * <example>
> > + * <title>Defining a simple type</title>
> > + * <programlisting>
> > + * OBJECT_DEFINE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
>
> Unlike the declare, here, using DEVICE looks correct...
Yes.
Regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
There are implicit requirements in the macros that will be a problem in some
existing type definitions:
On Thu, Jul 23, 2020 at 07:14:08PM +0100, Daniel P. Berrangé wrote:
[...]
> +/**
> + * OBJECT_DECLARE_TYPE:
> + * @ModuleObjName: the object name with initial capitalization
> + * @module_obj_name: the object name in lowercase with underscore separators
> + * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
> + *
> + * This macro is typically used in a header file, and will:
> + *
> + * - create the typedefs for the object and class structs
> + * - register the type for use with g_autoptr
> + * - provide three standard type cast functions
> + *
> + * The object struct and class struct need to be declared manually.
> + */
> +#define OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \
> + typedef struct ModuleObjName ModuleObjName; \
> + typedef struct ModuleObjName##Class ModuleObjName##Class; \
The ModuleObjName / ModuleObjName##Class pattern will be a problem in the following cases:
hw/arm/mps2-tz.c:107:1: class typedef MPS2TZMachineClass doesn't match instance typedef MPS2TZMachineState
hw/arm/mps2.c:83:1: class typedef MPS2MachineClass doesn't match instance typedef MPS2MachineState
hw/arm/musca.c:89:1: class typedef MuscaMachineClass doesn't match instance typedef MuscaMachineState
hw/arm/raspi.c:53:1: class typedef RaspiMachineClass doesn't match instance typedef RaspiMachineState
hw/arm/spitz.c:59:1: class typedef SpitzMachineClass doesn't match instance typedef SpitzMachineState
hw/arm/vexpress.c:182:1: class typedef VexpressMachineClass doesn't match instance typedef VexpressMachineState
hw/avr/arduino.c:36:1: class typedef ArduinoMachineClass doesn't match instance typedef ArduinoMachineState
hw/block/m25p80.c:462:1: class typedef M25P80Class doesn't match instance typedef Flash
hw/i386/kvm/i8254.c:40:1: class typedef KVMPITClass doesn't match instance typedef KVMPITState
hw/input/adb-kbd.c:33:1: class typedef ADBKeyboardClass doesn't match instance typedef KBDState
hw/input/adb-mouse.c:32:1: class typedef ADBMouseClass doesn't match instance typedef MouseState
hw/intc/arm_gic_kvm.c:34:1: class typedef KVMARMGICClass doesn't match instance typedef GICState
hw/intc/arm_gicv3_its_kvm.c:32:1: class typedef KVMARMITSClass doesn't match instance typedef GICv3ITSState
hw/intc/arm_gicv3_kvm.c:44:1: class typedef KVMARMGICv3Class doesn't match instance typedef GICv3State
hw/misc/tmp421.c:74:1: class typedef TMP421Class doesn't match instance typedef TMP421State
hw/rtc/m48t59-isa.c:39:1: class typedef M48txxISADeviceClass doesn't match instance typedef M48txxISAState
hw/rtc/m48t59.c:49:1: class typedef M48txxSysBusDeviceClass doesn't match instance typedef M48txxSysBusState
hw/rx/rx-gdbsim.c:53:1: class typedef RxGdbSimMachineClass doesn't match instance typedef RxGdbSimMachineState
hw/scsi/scsi-disk.c:56:1: class typedef SCSIDiskClass doesn't match instance typedef SCSIDiskState
hw/virtio/virtio-pci.h:29:1: class typedef VirtioPCIBusClass doesn't match instance typedef VirtioPCIBusState
hw/virtio/virtio-pci.h:101:1: class typedef VirtioPCIClass doesn't match instance typedef VirtIOPCIProxy
include/hw/boards.h:24:1: class typedef MachineClass doesn't match instance typedef MachineState
include/hw/i386/pc.h:124:1: class typedef PCMachineClass doesn't match instance typedef PCMachineState
include/hw/qdev-core.h:15:1: class typedef DeviceClass doesn't match instance typedef DeviceState
include/hw/qdev-core.h:206:1: class typedef BusClass doesn't match instance typedef BusState
> + \
> + G_DEFINE_AUTOPTR_CLEANUP_FUNC(ModuleObjName##Class, object_unref) \
> + \
> + static inline G_GNUC_UNUSED ModuleObjName##Class * \
> + MODULE_OBJ_NAME##_GET_CLASS(void *obj) \
> + { return OBJECT_GET_CLASS(ModuleObjName##Class, obj, \
> + TYPE_##MODULE_OBJ_NAME); } \
The TYPE_##MODULE_OBJ_NAME pattern will be a problem in the following cases:
accel/tcg/tcg-all.c:47:1: macro name TCG_STATE doesn't match type name TYPE_TCG_ACCEL
chardev/baum.c:106:1: macro name BAUM_CHARDEV doesn't match type name TYPE_CHARDEV_BRAILLE
chardev/char-parallel.c:57:1: macro name PARALLEL_CHARDEV doesn't match type name TYPE_CHARDEV_PARALLEL
chardev/char-parallel.c:184:1: macro name PARALLEL_CHARDEV doesn't match type name TYPE_CHARDEV_PARALLEL
chardev/char-pty.c:45:1: macro name PTY_CHARDEV doesn't match type name TYPE_CHARDEV_PTY
chardev/char-ringbuf.c:42:1: macro name RINGBUF_CHARDEV doesn't match type name TYPE_CHARDEV_RINGBUF
chardev/char-socket.c:88:1: macro name SOCKET_CHARDEV doesn't match type name TYPE_CHARDEV_SOCKET
chardev/char-udp.c:45:1: macro name UDP_CHARDEV doesn't match type name TYPE_CHARDEV_UDP
chardev/char-win-stdio.c:40:1: macro name WIN_STDIO_CHARDEV doesn't match type name TYPE_CHARDEV_WIN_STDIO
chardev/msmouse.c:46:1: macro name MOUSE_CHARDEV doesn't match type name TYPE_CHARDEV_MSMOUSE
chardev/testdev.c:41:1: macro name TESTDEV_CHARDEV doesn't match type name TYPE_CHARDEV_TESTDEV
chardev/wctablet.c:87:1: macro name WCTABLET_CHARDEV doesn't match type name TYPE_CHARDEV_WCTABLET
hw/audio/intel-hda.c:205:1: macro name INTEL_HDA doesn't match type name TYPE_INTEL_HDA_GENERIC
hw/char/etraxfs_ser.c:52:1: macro name ETRAX_SERIAL doesn't match type name TYPE_ETRAX_FS_SERIAL
hw/char/virtio-console.c:24:1: macro name VIRTIO_CONSOLE doesn't match type name TYPE_VIRTIO_CONSOLE_SERIAL_PORT
hw/display/ramfb-standalone.c:8:1: macro name RAMFB doesn't match type name TYPE_RAMFB_DEVICE
hw/i386/kvm/i8254.c:40:1: macro name KVM_PIT doesn't match type name TYPE_KVM_I8254
hw/i386/kvm/i8254.c:42:1: macro name KVM_PIT_CLASS doesn't match type name TYPE_KVM_I8254
hw/i386/kvm/i8254.c:44:1: macro name KVM_PIT_GET_CLASS doesn't match type name TYPE_KVM_I8254
hw/i386/kvm/i8259.c:22:1: macro name KVM_PIC_CLASS doesn't match type name TYPE_KVM_I8259
hw/i386/kvm/i8259.c:24:1: macro name KVM_PIC_GET_CLASS doesn't match type name TYPE_KVM_I8259
hw/intc/i8259.c:40:1: macro name PIC_CLASS doesn't match type name TYPE_I8259
hw/intc/i8259.c:41:1: macro name PIC_GET_CLASS doesn't match type name TYPE_I8259
hw/mips/boston.c:46:1: macro name BOSTON doesn't match type name TYPE_MIPS_BOSTON
hw/misc/edu.c:36:1: macro name EDU doesn't match type name TYPE_PCI_EDU_DEVICE
hw/misc/pvpanic.c:31:1: macro name ISA_PVPANIC_DEVICE doesn't match type name TYPE_PVPANIC
hw/net/can/can_kvaser_pci.c:48:1: macro name KVASER_PCI_DEV doesn't match type name TYPE_CAN_PCI_DEV
hw/net/can/can_mioe3680_pci.c:44:1: macro name MIOe3680_PCI_DEV doesn't match type name TYPE_CAN_PCI_DEV
hw/net/can/can_pcm3680_pci.c:44:1: macro name PCM3680i_PCI_DEV doesn't match type name TYPE_CAN_PCI_DEV
hw/net/e1000.c:150:1: macro name E1000 doesn't match type name TYPE_E1000_BASE
hw/net/e1000.c:153:1: macro name E1000_DEVICE_CLASS doesn't match type name TYPE_E1000_BASE
hw/net/e1000.c:156:1: macro name E1000_DEVICE_GET_CLASS doesn't match type name TYPE_E1000_BASE
hw/net/vmxnet3.c:135:1: macro name VMXNET3_DEVICE_CLASS doesn't match type name TYPE_VMXNET3
hw/net/vmxnet3.c:138:1: macro name VMXNET3_DEVICE_GET_CLASS doesn't match type name TYPE_VMXNET3
hw/pci-bridge/pci_expander_bridge.c:41:1: macro name PXB_DEV doesn't match type name TYPE_PXB_DEVICE
hw/pci-bridge/pci_expander_bridge.c:44:1: macro name PXB_PCIE_DEV doesn't match type name TYPE_PXB_PCIE_DEVICE
hw/pci-host/versatile.c:159:1: macro name PCI_VPB doesn't match type name TYPE_VERSATILE_PCI
hw/pci-host/versatile.c:163:1: macro name PCI_VPB_HOST doesn't match type name TYPE_VERSATILE_PCIHOST
hw/ppc/rs6000_mc.c:31:1: macro name RS6000MC_DEVICE doesn't match type name TYPE_RS6000MC
hw/scsi/esp-pci.c:38:1: macro name PCI_ESP doesn't match type name TYPE_AM53C974_DEVICE
hw/scsi/esp-pci.c:417:1: macro name DC390 doesn't match type name TYPE_DC390_DEVICE
hw/scsi/megasas.c:133:1: macro name MEGASAS doesn't match type name TYPE_MEGASAS_BASE
hw/scsi/megasas.c:136:1: macro name MEGASAS_DEVICE_CLASS doesn't match type name TYPE_MEGASAS_BASE
hw/scsi/megasas.c:139:1: macro name MEGASAS_DEVICE_GET_CLASS doesn't match type name TYPE_MEGASAS_BASE
hw/scsi/mptsas.c:46:1: macro name MPT_SAS doesn't match type name TYPE_MPTSAS1068
hw/scsi/vmw_pvscsi.c:66:1: macro name PVSCSI_DEVICE_CLASS doesn't match type name TYPE_PVSCSI
hw/scsi/vmw_pvscsi.c:69:1: macro name PVSCSI_DEVICE_GET_CLASS doesn't match type name TYPE_PVSCSI
hw/sparc/sun4m.c:777:1: macro name SUN4M_RAM doesn't match type name TYPE_SUN4M_MEMORY
hw/sparc64/sun4u.c:490:1: macro name SUN4U_RAM doesn't match type name TYPE_SUN4U_MEMORY
hw/timer/etraxfs_timer.c:51:1: macro name ETRAX_TIMER doesn't match type name TYPE_ETRAX_FS_TIMER
hw/timer/i8254.c:38:1: macro name PIT_CLASS doesn't match type name TYPE_I8254
hw/timer/i8254.c:40:1: macro name PIT_GET_CLASS doesn't match type name TYPE_I8254
hw/tpm/tpm_crb.c:47:1: macro name CRB doesn't match type name TYPE_TPM_CRB
hw/tpm/tpm_spapr.c:31:1: macro name VIO_SPAPR_VTPM doesn't match type name TYPE_TPM_SPAPR
hw/usb/ccid-card-emulated.c:48:1: macro name EMULATED_CCID_CARD doesn't match type name TYPE_EMULATED_CCID
hw/usb/ccid-card-passthru.c:67:1: macro name PASSTHRU_CCID_CARD doesn't match type name TYPE_CCID_PASSTHRU
hw/usb/dev-serial.c:114:1: macro name USB_SERIAL_DEV doesn't match type name TYPE_USB_SERIAL
hw/usb/dev-smartcard-reader.c:63:1: macro name USB_CCID_DEV doesn't match type name CCID_DEV_NAME
hw/usb/dev-storage.c:71:1: macro name USB_STORAGE_DEV doesn't match type name TYPE_USB_STORAGE
hw/usb/redirect.c:147:1: macro name USB_REDIRECT doesn't match type name TYPE_USB_REDIR
hw/usb/tusb6010.c:33:1: macro name TUSB doesn't match type name TYPE_TUSB6010
hw/vfio/ap.c:38:1: macro name VFIO_AP_DEVICE doesn't match type name VFIO_AP_DEVICE_TYPE
hw/vfio/pci.c:46:1: macro name PCI_VFIO doesn't match type name TYPE_VFIO_PCI
net/filter-rewriter.c:25:1: macro name FILTER_COLO_REWRITER doesn't match type name TYPE_FILTER_REWRITER
tests/check-qom-proplist.c:35:1: macro name DUMMY_OBJECT doesn't match type name TYPE_DUMMY
tests/test-qdev-global-props.c:34:1: macro name STATIC_TYPE doesn't match type name TYPE_STATIC_PROPS
tests/test-qdev-global-props.c:130:1: macro name DYNAMIC_TYPE doesn't match type name TYPE_DYNAMIC_PROPS
ui/console.c:1091:1: macro name VC_CHARDEV doesn't match type name TYPE_CHARDEV_VC
ui/gtk.c:181:1: macro name VC_CHARDEV doesn't match type name TYPE_CHARDEV_VC
ui/spice-app.c:48:1: macro name VC_CHARDEV doesn't match type name TYPE_CHARDEV_VC
> + \
> + static inline G_GNUC_UNUSED ModuleObjName##Class * \
> + MODULE_OBJ_NAME##_CLASS(void *klass) \
> + { return OBJECT_CLASS_CHECK(ModuleObjName##Class, klass, \
> + TYPE_##MODULE_OBJ_NAME); } \
> + \
> + static inline G_GNUC_UNUSED ModuleObjName * \
> + MODULE_OBJ_NAME(void *obj) \
> + { return OBJECT_CHECK(ModuleObjName, obj, \
> + TYPE_##MODULE_OBJ_NAME); }
> +
[...]
--
Eduardo
I've just noticed this:
On Thu, Jul 23, 2020 at 07:14:08PM +0100, Daniel P. Berrangé wrote:
> When creating new QOM types, there is a lot of boilerplate code that
> must be repeated using a standard pattern. This is tedious to write
> and liable to suffer from subtle inconsistencies. Thus it would
> benefit from some simple automation.
>
> QOM was loosely inspired by GLib's GObject, and indeed GObject suffers
> from the same burden of boilerplate code, but has long provided a set of
> macros to eliminate this burden in the source implementation. More
> recently it has also provided a set of macros to eliminate this burden
> in the header declaration.
>
> In GLib there are the G_DECLARE_* and G_DEFINE_* family of macros
> for the header declaration and source implementation respectively:
>
> https://developer.gnome.org/gobject/stable/chapter-gobject.html
> https://developer.gnome.org/gobject/stable/howto-gobject.html
>
> This patch takes inspiration from GObject to provide the equivalent
> functionality for QOM.
>
> In the header file, instead of:
>
> typedef struct MyDevice MyDevice;
> typedef struct MyDeviceClass MyDeviceClass;
>
> G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDeviceClass, object_unref)
Isn't this supposed to be
G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDevice, object_unref)
?
>
> #define MY_DEVICE_GET_CLASS(void *obj) \
> OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
> #define MY_DEVICE_CLASS(void *klass) \
> OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
> #define MY_DEVICE(void *obj)
> OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
>
> struct MyDeviceClass {
> DeviceClass parent_class;
> };
>
> We now have
>
> OBJECT_DECLARE_SIMPLE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
>
> In cases where the class needs some virtual methods, it can be left
> to be implemented manually using
>
> OBJECT_DECLARE_TYPE(MyDevice, my_device, MY_DEVICE)
>
> Note that these macros are including support for g_autoptr() for the
> object types, which is something previously only supported for variables
> declared as the base Object * type.
[...]
> + * <example>
> + * <title>Expansion from declaring a simple type</title>
> + * <programlisting>
> + * typedef struct MyDevice MyDevice;
> + * typedef struct MyDeviceClass MyDeviceClass;
> + *
> + * G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDeviceClass, object_unref)
> + *
> + * #define MY_DEVICE_GET_CLASS(void *obj) \
> + * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
> + * #define MY_DEVICE_CLASS(void *klass) \
> + * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
> + * #define MY_DEVICE(void *obj)
> + * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
> + *
> + * struct MyDeviceClass {
> + * DeviceClass parent_class;
> + * };
> + * </programlisting>
> + * </example>
[...]
> +#define OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \
> + typedef struct ModuleObjName ModuleObjName; \
> + typedef struct ModuleObjName##Class ModuleObjName##Class; \
> + \
> + G_DEFINE_AUTOPTR_CLEANUP_FUNC(ModuleObjName##Class, object_unref) \
> + \
> + static inline G_GNUC_UNUSED ModuleObjName##Class * \
> + MODULE_OBJ_NAME##_GET_CLASS(void *obj) \
> + { return OBJECT_GET_CLASS(ModuleObjName##Class, obj, \
> + TYPE_##MODULE_OBJ_NAME); } \
> + \
> + static inline G_GNUC_UNUSED ModuleObjName##Class * \
> + MODULE_OBJ_NAME##_CLASS(void *klass) \
> + { return OBJECT_CLASS_CHECK(ModuleObjName##Class, klass, \
> + TYPE_##MODULE_OBJ_NAME); } \
> + \
> + static inline G_GNUC_UNUSED ModuleObjName * \
> + MODULE_OBJ_NAME(void *obj) \
> + { return OBJECT_CHECK(ModuleObjName, obj, \
> + TYPE_##MODULE_OBJ_NAME); }
> +
[...]
--
Eduardo
© 2016 - 2026 Red Hat, Inc.