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é <[email protected]> --- 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) \ + \ + 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
