diff mbox series

[RFC,1/3] hw/qdev: Introduce QDEV_DECLARE_DEV_BUS_TYPES() macro

Message ID 20230213105609.6173-2-philmd@linaro.org
State New
Headers show
Series qdev: Introduce QDEV_DECLARE_DEV_BUS_TYPES() macro | expand

Commit Message

Philippe Mathieu-Daudé Feb. 13, 2023, 10:56 a.m. UTC
Similarly to QOM OBJECT_DECLARE_TYPE() equivalent, introduce
a QDev macro to declare common helpers for device sitting on
a bus.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/hw/qdev-core.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

Comments

Daniel P. Berrangé March 1, 2023, 2:59 p.m. UTC | #1
On Mon, Feb 13, 2023 at 11:56:07AM +0100, Philippe Mathieu-Daudé wrote:
> Similarly to QOM OBJECT_DECLARE_TYPE() equivalent, introduce
> a QDev macro to declare common helpers for device sitting on
> a bus.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  include/hw/qdev-core.h | 28 ++++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
> 
> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
> index 93718be156..dc9909a2e7 100644
> --- a/include/hw/qdev-core.h
> +++ b/include/hw/qdev-core.h
> @@ -717,6 +717,34 @@ void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
>  
>  BusState *qdev_get_parent_bus(const DeviceState *dev);
>  
> +/**
> + * QDEV_DECLARE_DEV_BUS_TYPES:
> + * @DeviceInstanceType: device instance struct name
> + * @DeviceClassType: device class struct name
> + * @DEVICE_OBJ_NAME: the device name in uppercase with underscore separators
> + * @BusInstanceType: bus instance struct name
> + * @DeviceClassType: bus class struct name
> + * @BUS_OBJ_NAME: the bus 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 four standard type cast functions
> + *
> + * The device state struct, device class struct, bus state struct need
> + * to be declared manually.
> + */
> +#define QDEV_DECLARE_DEV_BUS_TYPES(DeviceInstanceType, DeviceClassType, \
> +                                                       DEVICE_OBJ_NAME, \
> +                                   BusInstanceType, BUS_OBJ_NAME) \
> +    OBJECT_DECLARE_TYPE(DeviceInstanceType, DeviceClassType, DEVICE_OBJ_NAME) \
> +    OBJECT_DECLARE_SIMPLE_TYPE(BusInstanceType, BUS_OBJ_NAME) \

I'm not especially a fan of putting the declaration of two distinct
types behind one macro. This also makes it mismatch with the need
to use distinct OBJECT_DEFINE macros for each of the types.

> +    \
> +    static inline G_GNUC_UNUSED BusInstanceType * \
> +    DEVICE_OBJ_NAME##_GET_BUS(const DeviceInstanceType *dev) \
> +    { return BUS_OBJ_NAME(qdev_get_parent_bus(DEVICE(dev))); }
> +
>  /*** BUS API. ***/
>  
>  DeviceState *qdev_find_recursive(BusState *bus, const char *id);
> -- 
> 2.38.1
> 

With regards,
Daniel
diff mbox series

Patch

diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 93718be156..dc9909a2e7 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -717,6 +717,34 @@  void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
 
 BusState *qdev_get_parent_bus(const DeviceState *dev);
 
+/**
+ * QDEV_DECLARE_DEV_BUS_TYPES:
+ * @DeviceInstanceType: device instance struct name
+ * @DeviceClassType: device class struct name
+ * @DEVICE_OBJ_NAME: the device name in uppercase with underscore separators
+ * @BusInstanceType: bus instance struct name
+ * @DeviceClassType: bus class struct name
+ * @BUS_OBJ_NAME: the bus 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 four standard type cast functions
+ *
+ * The device state struct, device class struct, bus state struct need
+ * to be declared manually.
+ */
+#define QDEV_DECLARE_DEV_BUS_TYPES(DeviceInstanceType, DeviceClassType, \
+                                                       DEVICE_OBJ_NAME, \
+                                   BusInstanceType, BUS_OBJ_NAME) \
+    OBJECT_DECLARE_TYPE(DeviceInstanceType, DeviceClassType, DEVICE_OBJ_NAME) \
+    OBJECT_DECLARE_SIMPLE_TYPE(BusInstanceType, BUS_OBJ_NAME) \
+    \
+    static inline G_GNUC_UNUSED BusInstanceType * \
+    DEVICE_OBJ_NAME##_GET_BUS(const DeviceInstanceType *dev) \
+    { return BUS_OBJ_NAME(qdev_get_parent_bus(DEVICE(dev))); }
+
 /*** BUS API. ***/
 
 DeviceState *qdev_find_recursive(BusState *bus, const char *id);