* QOM: should you be able to cast from an interface class to the concrete class?
@ 2022-11-08 16:01 Peter Maydell
2022-11-08 17:52 ` Daniel P. Berrangé
0 siblings, 1 reply; 2+ messages in thread
From: Peter Maydell @ 2022-11-08 16:01 UTC (permalink / raw)
To: QEMU Developers
Cc: Paolo Bonzini, Daniel P. Berrange, Eduardo Habkost,
Alex Bennée
Hi; in the QOM model, are you supposed to be able to cast from
an interface class to the concrete class that is implementing it?
To give a specific example, if I have a ResettableClass *rc
should I be able to do DeviceClass *dc = DEVICE_CLASS(rc);
(assuming that the rc I have is actually from a DeviceClass) ?
If I'm reading the code correctly, at the moment this isn't possible:
object_class_dynamic_cast() has code for "if the class we're
casting from implements interfaces and the class we're casting to
is an interface, then look through the list of interfaces to
see if we should be returning the class pointer from the interface
list", which means you can cast from the concrete class to the
interface class. But there's no code there to say "if the class
we're casting from is an interface, try the concrete class".
As far as I can see we do actually record the information we need
to do this -- InterfaceClass has a field concrete_class that points
to the concrete class that's implementing it. But this field is
currently only written, never read.
Should we:
(a) support casting from the interface class back to the concrete
class, by adding some extra code in object_class_dynamic_cast(), or
(b) decide that that isn't something we should be wanting to do,
and remove the dead concrete_class struct field ?
(The thing I was trying to do when I ran into this was
effectively to add a class method, as opposed to an object method,
to an interface class. Specifically, the only reason that
the ResettableClass::get_transitional_function takes an Object*
is so that it can do an object-to-class cast to get to the
class pointer for the concrete class, and I had somewhere where
I wanted to also call that function where I only had the
ResettableClass *rc, not a specific object.)
thanks
-- PMM
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: QOM: should you be able to cast from an interface class to the concrete class?
2022-11-08 16:01 QOM: should you be able to cast from an interface class to the concrete class? Peter Maydell
@ 2022-11-08 17:52 ` Daniel P. Berrangé
0 siblings, 0 replies; 2+ messages in thread
From: Daniel P. Berrangé @ 2022-11-08 17:52 UTC (permalink / raw)
To: Peter Maydell
Cc: QEMU Developers, Paolo Bonzini, Eduardo Habkost, Alex Bennée
On Tue, Nov 08, 2022 at 04:01:56PM +0000, Peter Maydell wrote:
> Hi; in the QOM model, are you supposed to be able to cast from
> an interface class to the concrete class that is implementing it?
>
> To give a specific example, if I have a ResettableClass *rc
> should I be able to do DeviceClass *dc = DEVICE_CLASS(rc);
> (assuming that the rc I have is actually from a DeviceClass) ?
>
> If I'm reading the code correctly, at the moment this isn't possible:
> object_class_dynamic_cast() has code for "if the class we're
> casting from implements interfaces and the class we're casting to
> is an interface, then look through the list of interfaces to
> see if we should be returning the class pointer from the interface
> list", which means you can cast from the concrete class to the
> interface class. But there's no code there to say "if the class
> we're casting from is an interface, try the concrete class".
>
> As far as I can see we do actually record the information we need
> to do this -- InterfaceClass has a field concrete_class that points
> to the concrete class that's implementing it. But this field is
> currently only written, never read.
>
> Should we:
> (a) support casting from the interface class back to the concrete
> class, by adding some extra code in object_class_dynamic_cast(), or
> (b) decide that that isn't something we should be wanting to do,
> and remove the dead concrete_class struct field ?
My rule of thumb would be, if it is possible with GObject, then
it is reasonable to want it in QOM, and indeed you can cast
from an interface in GObject, back to the concrete type that
has implemented it. So I'd go for (a).
With 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 :|
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-11-08 17:53 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-08 16:01 QOM: should you be able to cast from an interface class to the concrete class? Peter Maydell
2022-11-08 17:52 ` Daniel P. Berrangé
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).