On Fri, Oct 16, 2026 at 03:09:55PM +0100, Peter Maydell wrote:
> On Fri, 16 Oct 2026 at 11:53, Daniel P. Berrangé <[email protected]> wrote:
> >
> > On Fri, Oct 16, 2026 at 11:14:16AM +0100, Peter Maydell wrote:
> > > On Fri, 16 Oct 2026 at 10:31, Daniel P. Berrangé <[email protected]> 
> > > wrote:
> 
> > > > +#define qdev_realize_chain_parent(dev, dc, errp) \
> > > > +    
> > > > DEVICE_CLASS(object_class_get_parent(OBJECT_CLASS(dc)))->realize(dev, 
> > > > errp)
> > >
> > > I'm tempted to suggest that we drop the 'dc' argument here and
> > > have the macro get the class from the dev:
> > >     
> > > DEVICE_CLASS(object_class_get_parent(object_get_class(OBJECT(dev))))->realize(dev,
> > > errp)
> > >
> > > because I think almost all realize methods wanting to chain back up
> > > won't have the class pointer to hand.
> > >
> > > We could maybe have an extra layer of abstraction here such that
> > > you write something like:
> > >
> > >    parent_invoke(DEVICE_CLASS, dev)->realize(dev, errp);
> > >
> > > Or is that trying to be too clever?
> >
> > This won't work if we have multiple levels in the class hierarchy
> > that need chaining up, as object_class_get_parent(object_get_class(dev))
> > will always just get the first level parent class of 'dev'.
> 
> Isn't that always what you want, though? In a class hierarchy
> Object->Device->A->B->C, C's realize method always wants to
> chain to B's realize method, and more generally "invoke the
> superclass method" from C wants B's implementation, not A's
> (assuming B actually implemented it).

Yes, C wants to invoke B's realize method, but then B also
wants to invoke A's realize method.

> > Somewhere/how we need to identify the current class level we're
> > calling from, so we know what parent to ask for.
> >
> > Another option is to do what GObject does and provide a simple
> > helper for getting the parent class, from OBJECT_DEFINE_TYPE*
> > macros:
> >
> >     ...
> >     static inline ObjectClass * \
> >     module_obj_name##_parent_class() { \
> >         return object_class_by_name(TYPE_##PARENT_MODULE_OBJ_NAME); \
> >     } \
> >     ...
> >
> > which would be used
> >
> >     DeviceClass *dc = DEVICE_CLASS(pl050_kbd_parent_class());
> >
> >     dc->realize(dev, errp);
> >
> > but many (most) of qdev impls don't use the relatively new
> > OBJECT_DEFINE_TYPE macros. They would have to instead explicitly
> > reference the parent type name:
> >
> >     DeviceClass *dc = DEVICE_CLASS(object_class_by_name(TYPE_PL050));
> >     dc->realize(dev, errp);
> 
> I still don't understand why we need to get the parent's
> object class by name here, rather than saying "give me
> the parent class of this object's class".

No matter which realize() method we are executing in, the "dev"
object parameter will always be an instance of the leaf class "C"
and thus the result of object_class_get_parent(objct_get_class(dev))
will always be B. That is only correct if executgnig "C"s realize
method, so breaks if we need to chain up realize many times.

With regards,
Daniel
-- 
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|


Reply via email to